添加全局协作规约功能
新功能:
- 新增 global_collaboration_rules 表
- POST /api/rules/global - 设置全局规约
- GET /api/rules/global - 获取全局规约
- 智能体领取任务时同时收到全局规约和项目规约
规约层级:
1. 全局规约 - 适用于所有项目和智能体
2. 项目规约 - 仅适用于特定项目
智能体领取任务时的响应示例:
{
"collaboration_rules": {
"global_rules": "全局规约内容",
"project_rules": "项目规约内容"
}
}
This commit is contained in:
+75
-8
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user