Skip to content

项目管理 API

本文档描述了 Trae 项目管理功能的 API 接口。

概述

项目管理 API 提供了项目创建、配置、团队管理、权限控制等功能,帮助团队高效协作开发。

端点

创建项目

http
POST /api/projects

请求参数

参数类型必需描述
namestring项目名称
descriptionstring项目描述
templatestring项目模板
visibilitystring可见性 (public/private)
settingsobject项目设置

响应

json
{
  "project_id": "proj_123",
  "name": "我的新项目",
  "description": "这是一个示例项目",
  "visibility": "private",
  "created_at": "2024-01-01T00:00:00Z",
  "owner": {
    "user_id": "user_1",
    "username": "alice"
  },
  "settings": {
    "auto_save": true,
    "git_integration": true
  }
}

获取项目列表

http
GET /api/projects?limit={limit}&offset={offset}&filter={filter}

查询参数

参数类型必需描述
limitnumber返回数量限制
offsetnumber偏移量
filterstring过滤条件 (owned/shared/public)
searchstring搜索关键词

响应

json
{
  "projects": [
    {
      "project_id": "proj_123",
      "name": "我的项目",
      "description": "项目描述",
      "visibility": "private",
      "created_at": "2024-01-01T00:00:00Z",
      "updated_at": "2024-01-01T00:00:00Z",
      "owner": {
        "user_id": "user_1",
        "username": "alice"
      },
      "role": "owner"
    }
  ],
  "total": 1
}

获取项目详情

http
GET /api/projects/{project_id}

响应

json
{
  "project_id": "proj_123",
  "name": "我的项目",
  "description": "项目描述",
  "visibility": "private",
  "created_at": "2024-01-01T00:00:00Z",
  "updated_at": "2024-01-01T00:00:00Z",
  "owner": {
    "user_id": "user_1",
    "username": "alice",
    "email": "alice@example.com"
  },
  "settings": {
    "auto_save": true,
    "git_integration": true,
    "ai_assistance": true
  },
  "statistics": {
    "files_count": 25,
    "lines_of_code": 1500,
    "contributors": 3,
    "last_activity": "2024-01-01T00:00:00Z"
  }
}

更新项目

http
PUT /api/projects/{project_id}

请求参数

参数类型必需描述
namestring项目名称
descriptionstring项目描述
visibilitystring可见性
settingsobject项目设置

删除项目

http
DELETE /api/projects/{project_id}

项目成员管理

获取成员列表

http
GET /api/projects/{project_id}/members

响应

json
{
  "members": [
    {
      "user_id": "user_1",
      "username": "alice",
      "email": "alice@example.com",
      "role": "owner",
      "joined_at": "2024-01-01T00:00:00Z",
      "last_active": "2024-01-01T00:00:00Z"
    },
    {
      "user_id": "user_2",
      "username": "bob",
      "email": "bob@example.com",
      "role": "developer",
      "joined_at": "2024-01-01T00:00:00Z",
      "last_active": "2024-01-01T00:00:00Z"
    }
  ],
  "total": 2
}

邀请成员

http
POST /api/projects/{project_id}/members/invite

请求参数

参数类型必需描述
emailstring邀请邮箱
rolestring角色 (viewer/developer/admin)
messagestring邀请信息

响应

json
{
  "invitation_id": "inv_123",
  "email": "new-member@example.com",
  "role": "developer",
  "status": "pending",
  "expires_at": "2024-01-08T00:00:00Z"
}

更新成员角色

http
PUT /api/projects/{project_id}/members/{user_id}

请求参数

参数类型必需描述
rolestring新角色

移除成员

http
DELETE /api/projects/{project_id}/members/{user_id}

项目模板

获取模板列表

http
GET /api/projects/templates

响应

json
{
  "templates": [
    {
      "template_id": "tpl_react",
      "name": "React 应用",
      "description": "基于 React 的前端应用模板",
      "category": "frontend",
      "technologies": ["React", "TypeScript", "Vite"],
      "preview_url": "https://templates.trae.ai/react"
    },
    {
      "template_id": "tpl_node_api",
      "name": "Node.js API",
      "description": "RESTful API 服务模板",
      "category": "backend",
      "technologies": ["Node.js", "Express", "MongoDB"]
    }
  ]
}

从模板创建项目

http
POST /api/projects/from-template

请求参数

参数类型必需描述
template_idstring模板 ID
namestring项目名称
descriptionstring项目描述
customizationsobject自定义配置

项目设置

获取项目设置

http
GET /api/projects/{project_id}/settings

响应

json
{
  "general": {
    "auto_save": true,
    "auto_save_interval": 30,
    "backup_enabled": true
  },
  "git": {
    "integration_enabled": true,
    "auto_commit": false,
    "default_branch": "main"
  },
  "ai": {
    "assistance_enabled": true,
    "auto_completion": true,
    "code_suggestions": true
  },
  "collaboration": {
    "real_time_editing": true,
    "comments_enabled": true,
    "review_required": false
  }
}

更新项目设置

http
PUT /api/projects/{project_id}/settings

项目统计

获取项目统计信息

http
GET /api/projects/{project_id}/statistics?period={period}

响应

json
{
  "period": "last_30_days",
  "code_metrics": {
    "lines_added": 1250,
    "lines_removed": 340,
    "files_changed": 45,
    "commits": 28
  },
  "activity": {
    "active_days": 22,
    "total_sessions": 156,
    "average_session_duration": 45
  },
  "collaboration": {
    "comments": 18,
    "reviews": 5,
    "shared_sessions": 12
  }
}

角色权限

角色定义

  • owner - 拥有者,完全控制权限
  • admin - 管理员,除删除项目外的所有权限
  • developer - 开发者,可以编辑代码和管理文件
  • viewer - 观察者,只能查看项目内容

权限矩阵

操作OwnerAdminDeveloperViewer
查看项目
编辑代码
管理文件
邀请成员
管理设置
删除项目

示例

创建新项目

javascript
const response = await fetch('/api/projects', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify({
    name: '电商网站',
    description: '基于 React 的电商平台',
    template: 'tpl_react',
    visibility: 'private',
    settings: {
      auto_save: true,
      git_integration: true,
      ai_assistance: true
    }
  })
});

const project = await response.json();
console.log('项目已创建:', project.project_id);

邀请团队成员

javascript
const projectId = 'proj_123';

// 邀请开发者
const inviteResponse = await fetch(`/api/projects/${projectId}/members/invite`, {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify({
    email: 'developer@example.com',
    role: 'developer',
    message: '欢迎加入我们的项目团队!'
  })
});

const invitation = await inviteResponse.json();
console.log('邀请已发送:', invitation.invitation_id);

管理项目设置

javascript
const projectId = 'proj_123';

// 获取当前设置
const settingsResponse = await fetch(`/api/projects/${projectId}/settings`);
const currentSettings = await settingsResponse.json();

// 更新设置
const updatedSettings = {
  ...currentSettings,
  ai: {
    ...currentSettings.ai,
    auto_completion: false // 关闭自动补全
  },
  collaboration: {
    ...currentSettings.collaboration,
    review_required: true // 启用代码审查
  }
};

const updateResponse = await fetch(`/api/projects/${projectId}/settings`, {
  method: 'PUT',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify(updatedSettings)
});

console.log('设置已更新');

查看项目统计

javascript
const projectId = 'proj_123';

const statsResponse = await fetch(`/api/projects/${projectId}/statistics?period=last_7_days`);
const stats = await statsResponse.json();

console.log('项目统计 (最近7天):');
console.log(`- 新增代码行数: ${stats.code_metrics.lines_added}`);
console.log(`- 删除代码行数: ${stats.code_metrics.lines_removed}`);
console.log(`- 修改文件数: ${stats.code_metrics.files_changed}`);
console.log(`- 提交次数: ${stats.code_metrics.commits}`);
console.log(`- 活跃天数: ${stats.activity.active_days}`);

批量操作

javascript
// 批量邀请成员
const members = [
  { email: 'dev1@example.com', role: 'developer' },
  { email: 'dev2@example.com', role: 'developer' },
  { email: 'manager@example.com', role: 'admin' }
];

const invitations = await Promise.all(
  members.map(member => 
    fetch(`/api/projects/${projectId}/members/invite`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer your-token'
      },
      body: JSON.stringify(member)
    }).then(res => res.json())
  )
);

console.log(`已发送 ${invitations.length} 个邀请`);

您的终极 AI 驱动 IDE 学习指南