fix: switch_project/create_project 持久化当前项目 + auto-inject 新需求识别
- _persist_project 持久化 current_project_id 到 pipeline_agent_settings(只改内存字段导致下一轮回退旧项目) - auto-inject 兜底识别新需求描述 → create_task 而非 diagnose/list_tasks
This commit is contained in:
parent
92ee88d1f4
commit
07383a9dd5
@ -185,16 +185,28 @@ class AgentExecutor:
|
|||||||
if action_type == "reply":
|
if action_type == "reply":
|
||||||
# 任何轮次,只要没成功调过工具就 reply → 强制注入工具调用
|
# 任何轮次,只要没成功调过工具就 reply → 强制注入工具调用
|
||||||
if self._tool_call_count == 0:
|
if self._tool_call_count == 0:
|
||||||
hint_tool = "diagnose_project"
|
# 意图识别:新需求描述(长文本+需求特征词)→ create_task 推进
|
||||||
if any(kw in user_input.lower() for kw in ["任务", "task", "list"]):
|
req_keywords = ["实现", "开发", "建设", "系统", "功能", "需求", "模块",
|
||||||
|
"支持", "切换", "监控", "备份", "同步", "要做", "设计", "平台"]
|
||||||
|
is_requirement = (len(user_input) > 40 and
|
||||||
|
any(kw in user_input for kw in req_keywords))
|
||||||
|
if is_requirement:
|
||||||
|
hint_tool = "create_task"
|
||||||
|
elif any(kw in user_input.lower() for kw in ["任务", "task", "list"]):
|
||||||
hint_tool = "list_tasks"
|
hint_tool = "list_tasks"
|
||||||
elif any(kw in user_input.lower() for kw in ["问题", "question"]):
|
elif any(kw in user_input.lower() for kw in ["问题", "question"]):
|
||||||
hint_tool = "list_questions"
|
hint_tool = "list_questions"
|
||||||
|
else:
|
||||||
|
hint_tool = "diagnose_project"
|
||||||
yield json.dumps({
|
yield json.dumps({
|
||||||
"type": "auto_tool", "message": f"未调工具,自动注入 {hint_tool}",
|
"type": "auto_tool", "message": f"未调工具,自动注入 {hint_tool}",
|
||||||
}, ensure_ascii=False) + "\n"
|
}, ensure_ascii=False) + "\n"
|
||||||
self._msgs.append({"role": "assistant", "content": f"已调用 {hint_tool}"})
|
self._msgs.append({"role": "assistant", "content": f"已调用 {hint_tool}"})
|
||||||
self._msgs.append({"role": "user", "content": f"请调用 {hint_tool} 工具,只输出 {{\"action\":\"tool_call\",\"tool\":\"{hint_tool}\",\"params\":{{}}}}"})
|
if hint_tool == "create_task":
|
||||||
|
self._msgs.append({"role": "user", "content":
|
||||||
|
"用户描述了新需求,请用 create_task 创建任务推进(可先 role=requirement 做需求分析,再拆分到 develop),填好 title/role/description 参数,输出 tool_call JSON。"})
|
||||||
|
else:
|
||||||
|
self._msgs.append({"role": "user", "content": f"请调用 {hint_tool} 工具,只输出 {{\"action\":\"tool_call\",\"tool\":\"{hint_tool}\",\"params\":{{}}}}"})
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# 调了工具但太少(1-2次),LLM 想停 → 推它继续深入(最多3次)
|
# 调了工具但太少(1-2次),LLM 想停 → 推它继续深入(最多3次)
|
||||||
@ -576,6 +588,31 @@ class AgentExecutor:
|
|||||||
|
|
||||||
# ── 工具实现 ──
|
# ── 工具实现 ──
|
||||||
|
|
||||||
|
async def _persist_project(self, sor, pid):
|
||||||
|
"""持久化当前项目到 pipeline_agent_settings(user_id 唯一键)。
|
||||||
|
|
||||||
|
只改 self.project_id 不够——AgentExecutor 每轮新建,run 结束即销毁,
|
||||||
|
下一轮 cockpit_chat_v2 又从 pipeline_agent_settings 读回旧项目。
|
||||||
|
"""
|
||||||
|
if not self.user_id or not pid:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
from appPublic.uniqueID import getID
|
||||||
|
await sor.sqlExe(
|
||||||
|
"UPDATE pipeline_agent_settings SET current_project_id=${pid}$ "
|
||||||
|
"WHERE user_id=${uid}$",
|
||||||
|
{"pid": pid, "uid": self.user_id})
|
||||||
|
exists = await sor.sqlExe(
|
||||||
|
"SELECT 1 FROM pipeline_agent_settings WHERE user_id=${uid}$",
|
||||||
|
{"uid": self.user_id})
|
||||||
|
if not exists:
|
||||||
|
await sor.sqlExe(
|
||||||
|
"INSERT INTO pipeline_agent_settings (id, user_id, current_project_id) "
|
||||||
|
"VALUES (${id}$, ${uid}$, ${pid}$)",
|
||||||
|
{"id": getID(), "uid": self.user_id, "pid": pid})
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"persist project failed: {e}")
|
||||||
|
|
||||||
async def _t_switch_project(self, sor, p, pid):
|
async def _t_switch_project(self, sor, p, pid):
|
||||||
name = p.get("project_name", "").strip()
|
name = p.get("project_name", "").strip()
|
||||||
if not name:
|
if not name:
|
||||||
@ -587,6 +624,7 @@ class AgentExecutor:
|
|||||||
if recs:
|
if recs:
|
||||||
r = recs[0]
|
r = recs[0]
|
||||||
self.project_id = getattr(r, "id", "")
|
self.project_id = getattr(r, "id", "")
|
||||||
|
await self._persist_project(sor, self.project_id)
|
||||||
return f"OK: 已切换到 {getattr(r, 'name', name)}"
|
return f"OK: 已切换到 {getattr(r, 'name', name)}"
|
||||||
|
|
||||||
# LLM 分类匹配
|
# LLM 分类匹配
|
||||||
@ -605,6 +643,7 @@ class AgentExecutor:
|
|||||||
if recs2:
|
if recs2:
|
||||||
r = recs2[0]
|
r = recs2[0]
|
||||||
self.project_id = getattr(r, "id", "")
|
self.project_id = getattr(r, "id", "")
|
||||||
|
await self._persist_project(sor, self.project_id)
|
||||||
return f"OK: 已切换到 {getattr(r, 'name', matched)}"
|
return f"OK: 已切换到 {getattr(r, 'name', matched)}"
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
@ -630,6 +669,7 @@ class AgentExecutor:
|
|||||||
"iteration_type": "default", "status": "active", "priority": 1,
|
"iteration_type": "default", "status": "active", "priority": 1,
|
||||||
})
|
})
|
||||||
self.project_id = pid_val
|
self.project_id = pid_val
|
||||||
|
await self._persist_project(sor, pid_val)
|
||||||
return f"OK: 已创建项目 {name}"
|
return f"OK: 已创建项目 {name}"
|
||||||
|
|
||||||
async def _t_create_task(self, sor, p, pid):
|
async def _t_create_task(self, sor, p, pid):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user