diff --git a/API-V2.md b/API-V2.md index a5c3648..84ecda3 100644 --- a/API-V2.md +++ b/API-V2.md @@ -63,6 +63,31 @@ Content-Type: application/json ### 协作规约 +#### 设置全局协作规约 +```http +POST /api/rules/global +Content-Type: application/json + +{ + "content": "# 全局协作规约\n\n适用于所有项目...", + "created_by": "human" +} +``` + +#### 获取全局协作规约 +```http +GET /api/rules/global + +响应: +{ + "id": "xxx", + "content": "规约内容(Markdown)", + "created_by": "human", + "created_at": 1234567890, + "updated_at": 1234567890 +} +``` + #### 设置项目协作规约 ```http POST /api/projects/:projectId/rules @@ -155,7 +180,7 @@ GET /api/tasks/:id } ``` -#### 领取任务(智能体会收到协作规约) +#### 领取任务(智能体会同时收到全局规约和项目规约) ```http POST /api/tasks/:id/claim Content-Type: application/json @@ -164,11 +189,14 @@ Content-Type: application/json "agent_id": "智能体ID" } -响应(如果项目有协作规约): +响应(如果有规约): { "message": "任务领取成功", - "collaboration_rules": "项目协作规约内容", - "notice": "⚠️ 请严格遵守项目协作规约" + "collaboration_rules": { + "global_rules": "全局协作规约内容", + "project_rules": "项目协作规约内容" + }, + "notice": "⚠️ 请严格遵守协作规约" } ``` diff --git a/server-v2.js b/server-v2.js index 755d1a9..c0761dc 100644 --- a/server-v2.js +++ b/server-v2.js @@ -93,7 +93,18 @@ async function initDatabase() { ) `); - // 协作规约表 + // 全局协作规约表 + await client.query(` + CREATE TABLE IF NOT EXISTS global_collaboration_rules ( + id VARCHAR(255) PRIMARY KEY, + content TEXT NOT NULL, + created_by VARCHAR(255), + created_at BIGINT, + updated_at BIGINT + ) + `); + + // 项目协作规约表 await client.query(` CREATE TABLE IF NOT EXISTS collaboration_rules ( id VARCHAR(255) PRIMARY KEY, @@ -269,7 +280,48 @@ app.put('/api/projects/:id', async (req, res) => { // ==================== 协作规约 API ==================== -// 设置协作规约 +// 设置全局协作规约 +app.post('/api/rules/global', async (req, res) => { + const { content, created_by } = req.body; + + if (!content) { + return res.status(400).json({ error: '规约内容不能为空' }); + } + + const id = uuidv4(); + const now = Date.now(); + + try { + // 删除旧的全局规约(只保留一个) + await pool.query('DELETE FROM global_collaboration_rules'); + + // 创建新的全局规约 + await pool.query( + `INSERT INTO global_collaboration_rules (id, content, created_by, created_at, updated_at) + VALUES ($1, $2, $3, $4, $5)`, + [id, content, created_by || 'human', now, now] + ); + + res.json({ + id, + message: '全局协作规约设置成功' + }); + } catch (err) { + res.status(500).json({ error: err.message }); + } +}); + +// 获取全局协作规约 +app.get('/api/rules/global', async (req, res) => { + try { + const result = await pool.query('SELECT * FROM global_collaboration_rules'); + res.json(result.rows[0] || null); + } catch (err) { + res.status(500).json({ error: err.message }); + } +}); + +// 设置项目协作规约 app.post('/api/projects/:projectId/rules', async (req, res) => { const { projectId } = req.params; const { content, created_by } = req.body; @@ -598,8 +650,13 @@ app.post('/api/tasks/:id/claim', async (req, res) => { return res.status(400).json({ error: '任务状态不允许领取' }); } - // 获取项目的协作规约 - const rulesResult = await client.query( + // 获取全局协作规约 + const globalRulesResult = await client.query( + 'SELECT content FROM global_collaboration_rules' + ); + + // 获取项目协作规约 + const projectRulesResult = await client.query( 'SELECT content FROM collaboration_rules WHERE project_id = $1', [task.project_id] ); @@ -617,10 +674,20 @@ app.post('/api/tasks/:id/claim', async (req, res) => { message: '任务领取成功' }; - // 如果有协作规约,返回给智能体 - if (rulesResult.rows.length > 0) { - response.collaboration_rules = rulesResult.rows[0].content; - response.notice = '⚠️ 请严格遵守项目协作规约'; + // 返回规约给智能体 + const rules = {}; + + if (globalRulesResult.rows.length > 0) { + rules.global_rules = globalRulesResult.rows[0].content; + } + + if (projectRulesResult.rows.length > 0) { + rules.project_rules = projectRulesResult.rows[0].content; + } + + if (rules.global_rules || rules.project_rules) { + response.collaboration_rules = rules; + response.notice = '⚠️ 请严格遵守协作规约'; } res.json(response);