项目管理 API
本文档描述了 Trae 项目管理功能的 API 接口。
概述
项目管理 API 提供了项目创建、配置、团队管理、权限控制等功能,帮助团队高效协作开发。
端点
创建项目
http
POST /api/projects请求参数
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
name | string | 是 | 项目名称 |
description | string | 否 | 项目描述 |
template | string | 否 | 项目模板 |
visibility | string | 否 | 可见性 (public/private) |
settings | object | 否 | 项目设置 |
响应
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}查询参数
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
limit | number | 否 | 返回数量限制 |
offset | number | 否 | 偏移量 |
filter | string | 否 | 过滤条件 (owned/shared/public) |
search | string | 否 | 搜索关键词 |
响应
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}请求参数
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
name | string | 否 | 项目名称 |
description | string | 否 | 项目描述 |
visibility | string | 否 | 可见性 |
settings | object | 否 | 项目设置 |
删除项目
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请求参数
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
email | string | 是 | 邀请邮箱 |
role | string | 是 | 角色 (viewer/developer/admin) |
message | string | 否 | 邀请信息 |
响应
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}请求参数
| 参数 | 类型 | 必需 | 描述 |
|---|---|---|---|
role | string | 是 | 新角色 |
移除成员
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_id | string | 是 | 模板 ID |
name | string | 是 | 项目名称 |
description | string | 否 | 项目描述 |
customizations | object | 否 | 自定义配置 |
项目设置
获取项目设置
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- 观察者,只能查看项目内容
权限矩阵
| 操作 | Owner | Admin | Developer | Viewer |
|---|---|---|---|---|
| 查看项目 | ✓ | ✓ | ✓ | ✓ |
| 编辑代码 | ✓ | ✓ | ✓ | ✗ |
| 管理文件 | ✓ | ✓ | ✓ | ✗ |
| 邀请成员 | ✓ | ✓ | ✗ | ✗ |
| 管理设置 | ✓ | ✓ | ✗ | ✗ |
| 删除项目 | ✓ | ✗ | ✗ | ✗ |
示例
创建新项目
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} 个邀请`);