fix: project_id→tenant_id + task_detail default list + diagnose_project tool

This commit is contained in:
ymq 2026-08-09 22:22:27 +08:00
parent dbef268202
commit 6900a86782

View File

@ -22,6 +22,7 @@ AGENT_PROMPT = """你是开发产线驾驶舱 Agent。用工具获取数据
- 删除项目delete_project → 看到确认提示后,用户回复「确认删除 xxx」→ 再调 _sys_delete_project
- 查看进度list_tasks / task_detail / list_deliverables
- 提交任务:先确保在正确项目,再 create_task
- 诊断项目diagnose_project 找出卡点/失败任务/待回答问题
- 启动Agent任务提交后调 start_agents 让角色Agent执行
- 回答问题list_questions 看问题 → answer_question 回答
- clone_repo 用于克隆 git 仓库到工作空间
@ -47,6 +48,7 @@ TOOLS = [
{"name":"answer_question","description":"回答agent提出的问题","params":{"question_id":"问题ID","answer":"回答内容"}},
{"name":"create_task","description":"提交开发任务到指定角色","params":{"title":"标题","description":"描述","role":"requirement/design/develop/test/deploy"}},
{"name":"start_agents","description":"启动项目角色agents执行待办任务","params":{}},
{"name":"diagnose_project","description":"诊断项目状态:找出卡点、失败任务、待回答问题,给出推进建议","params":{}},
{"name":"check_progress","description":"查看项目整体进度","params":{"task_id":"任务ID(可选)"}},
{"name":"add_repo","description":"关联仓库到当前项目","params":{"url":"git地址","name":"名称(可选)"}},
{"name":"list_repos","description":"列出当前项目仓库","params":{}},
@ -133,7 +135,7 @@ def _w_card(title, body, kind="success"):
]}
def _w_progress(text): return {"widgettype":"Text","options":{"text":text,"style":{"color":"#f59e0b","fontSize":"12px","padding":"2px 8px"},"halign":"left"}}
_tool_labels = {'list_projects':'列出项目','switch_project':'切换项目','create_project':'创建项目','delete_project':'删除项目','list_tasks':'查询任务','task_detail':'任务详情','list_deliverables':'查看交付件','view_deliverable':'交付件内容','list_questions':'查看问题','answer_question':'回答问题','create_task':'创建任务','start_agents':'启动角色Agent','check_progress':'检查进度','add_repo':'关联仓库','list_repos':'查看仓库','clone_repo':'克隆仓库','add_bug':'报告Bug','run_command':'执行命令'}
_tool_labels = {'list_projects':'列出项目','switch_project':'切换项目','create_project':'创建项目','delete_project':'删除项目','list_tasks':'查询任务','task_detail':'任务详情','list_deliverables':'查看交付件','view_deliverable':'交付件内容','list_questions':'查看问题','answer_question':'回答问题','create_task':'创建任务','start_agents':'启动角色Agent','diagnose_project':'项目诊断','check_progress':'检查进度','add_repo':'关联仓库','list_repos':'查看仓库','clone_repo':'克隆仓库','add_bug':'报告Bug','run_command':'执行命令'}
async def _exec_tool(sor, tool, params, ctx, uid, org_id):
p = params or {}
@ -281,7 +283,12 @@ async def _exec_tool(sor, tool, params, ctx, uid, org_id):
return f'OK (rc={r.returncode}):\n{out}' if out else f'OK (rc={r.returncode})'
elif tool == 'task_detail':
tid = p.get('task_id','')
if not tid: return 'FAIL: 需要任务ID'
if not tid:
# No task_id provided — show first few pending/running tasks for context
if not ctx['pid']: return 'FAIL: 请先切换到项目并指定任务ID'
ts = await sor.sqlExe("SELECT id,title,state,role FROM pipeline_tasks WHERE tenant_id=${p}$ AND state IN ('submitted','running','waiting') ORDER BY created_at ASC LIMIT 5",{"p":ctx['pid']})
if not ts: return '无活跃任务,请用 list_tasks 查看全部'
return '当前活跃任务(请指定 task_id 查看详情):\n'+'\n'.join([f" [{getattr(t,'state','')}] {getattr(t,'id','')[:12]} [{getattr(t,'role','')}] {getattr(t,'title','')[:60]}" for t in ts])
ts = await sor.sqlExe("SELECT * FROM pipeline_tasks WHERE id=${t}$",{"t":tid})
if not ts: return 'FAIL: 任务不存在'
t = ts[0]
@ -308,7 +315,7 @@ async def _exec_tool(sor, tool, params, ctx, uid, org_id):
return f"类型: {getattr(d,'deliverable_type','')}\n标题: {getattr(d,'title','')}\n状态: {getattr(d,'review_status','')} | 评分: {getattr(d,'quality_score','')}\n\n{getattr(d,'content','')[:3000]}"
elif tool == 'list_questions':
if not ctx['pid']: return 'FAIL: 请先切换到项目'
qs = await sor.sqlExe("SELECT id,task_id,question,answer,status,created_at FROM pipeline_agent_questions WHERE project_id=${p}$ AND status='pending' ORDER BY created_at DESC LIMIT 10",{"p":ctx['pid']})
qs = await sor.sqlExe("SELECT id,task_id,question,answer,status,created_at FROM pipeline_agent_questions WHERE tenant_id=${p}$ AND status='pending' ORDER BY created_at DESC LIMIT 10",{"p":ctx['pid']})
if not qs: return '无待回答问题'
return '\n'.join([f"[{getattr(q,'id','')[:8]}] 任务:{getattr(q,'task_id','')[:8]} Q:{getattr(q,'question','')[:80]}" for q in qs])
elif tool == 'answer_question':
@ -333,6 +340,53 @@ async def _exec_tool(sor, tool, params, ctx, uid, org_id):
if pm_r['status'] != 'idle':
results.append(f"pm_review: {pm_r['status']} task={pm_r.get('task_id','')[:8]}")
return '\n'.join(results) if results else '无待执行任务'
elif tool == 'diagnose_project':
if not ctx['pid']: return 'FAIL: 请先切换到项目'
pid = ctx['pid']
# 1) Stuck submitted tasks
stuck = await sor.sqlExe("SELECT id,title,role,state,created_at FROM pipeline_tasks WHERE tenant_id=${p}$ AND state IN ('submitted','waiting') ORDER BY created_at ASC LIMIT 10",{"p":pid})
# 2) Failed tasks
failed = await sor.sqlExe("SELECT id,title,role,created_at FROM pipeline_tasks WHERE tenant_id=${p}$ AND state='failed' ORDER BY created_at DESC LIMIT 5",{"p":pid})
# 3) Pending questions
qs = await sor.sqlExe("SELECT id,question,status,task_id FROM pipeline_agent_questions WHERE tenant_id=${p}$ AND status='pending' LIMIT 5",{"p":pid})
# 4) Running tasks
running = await sor.sqlExe("SELECT id,title,role FROM pipeline_tasks WHERE tenant_id=${p}$ AND state='running' LIMIT 5",{"p":pid})
# 5) Review tasks
review = await sor.sqlExe("SELECT id,title,role FROM pipeline_tasks WHERE tenant_id=${p}$ AND state='review' LIMIT 5",{"p":pid})
lines = [f"项目诊断: {ctx['pname']}"]
if stuck:
lines.append(f"\n📌 卡住的任务({len(stuck)}个):")
for t in stuck:
lines.append(f" [{t.state}] [{t.role}] {t.title[:60]}")
if failed:
lines.append(f"\n❌ 失败任务({len(failed)}个):")
for t in failed:
lines.append(f" [{t.role}] {t.title[:60]}")
if running:
lines.append(f"\n🔄 运行中({len(running)}个):")
for t in running:
lines.append(f" [{t.role}] {t.title[:60]}")
if review:
lines.append(f"\n👀 待审核({len(review)}个):")
for t in review:
lines.append(f" [{t.role}] {t.title[:60]}")
if qs:
lines.append(f"\n❓ 待回答问题({len(qs)}个):")
for q in qs:
lines.append(f" [{q.id[:8]}] {q.question[:80]}")
if not stuck and not failed and not qs:
lines.append("\n✅ 项目运行正常,无卡点")
else:
lines.append(f"\n💡 建议:")
if stuck:
lines.append(f" - {len(stuck)}个任务待执行 → 调用 start_agents 启动角色Agent")
if failed:
lines.append(f" - {len(failed)}个任务失败 → 检查失败原因,考虑重新提交")
if qs:
lines.append(f" - {len(qs)}个问题待回答 → 调用 list_questions + answer_question")
if review:
lines.append(f" - {len(review)}个交付件待PM审核 → PM agent会自动处理")
return '\n'.join(lines)
elif tool == 'add_bug':
if not ctx['pid']: return 'FAIL: 请先切换到项目'
title = p.get('title','')[:100]