diff --git a/wwwroot/api/cockpit_chat.dspy b/wwwroot/api/cockpit_chat.dspy index 7c7e4f8..295d428 100644 --- a/wwwroot/api/cockpit_chat.dspy +++ b/wwwroot/api/cockpit_chat.dspy @@ -25,8 +25,7 @@ AGENT_PROMPT = """你是开发产线驾驶舱 Agent。用工具获取数据, ## 核心原则 - 环境信息中已显示当前项目。所有操作在当前项目内完成 -- **除非用户原话以"切换到"开头,否则绝不要调用 switch_project** -- 提到项目名不等于要切换——只有在用户明确表达切换意图时才切换 +- 用户输入为主要依据,历史记录仅作参考 ## 工作流 - 切换项目:用户说"切换到XXX项目" → switch_project @@ -503,21 +502,26 @@ if action == 'send_message': system = AGENT_PROMPT.replace('__TOOLS__',TOOLS_TEXT).replace('__ENV__',env_text) debug(f"SYSTEM PROMPT env: {env_text}") msgs = [{"role":"system","content":system}] - # Load history scoped to current project (via iteration_id = project_id) - sql = "SELECT role,content FROM pipeline_conversations WHERE created_by=${u}$ ORDER BY created_at ASC LIMIT 20" - params = {"u":uid} - if ctx['pid']: - sql = "SELECT role,content FROM pipeline_conversations WHERE created_by=${u}$ AND iteration_id=${p}$ ORDER BY created_at ASC LIMIT 20" - params = {"u":uid,"p":ctx['pid']} - hist_rows = await sor.sqlExe(sql, params) - debug(f"HISTORY: loaded {len(hist_rows or [])} rows, project={ctx['pid'][:8] if ctx['pid'] else 'none'}") - for h in (hist_rows or []): - c = getattr(h,'content','') or '' - r = getattr(h,'role','') or '' - # Skip raw tool_call JSON — prevents LLM from mimicking old tool calls - if c.startswith('{"action":"tool_call"') or c.startswith('{"action": "tool_call"'): - continue - msgs.append({"role":'user' if r=='user' else 'assistant',"content":c}) + # Aggregate history as reference, not as independent messages + hist_parts = [] + if uid: + sql = "SELECT role,content FROM pipeline_conversations WHERE created_by=${u}$ ORDER BY created_at ASC LIMIT 10" + params = {"u":uid} + if ctx['pid']: + sql = "SELECT role,content FROM pipeline_conversations WHERE created_by=${u}$ AND iteration_id=${p}$ ORDER BY created_at ASC LIMIT 10" + params = {"u":uid,"p":ctx['pid']} + hist_rows = await sor.sqlExe(sql, params) + debug(f"HISTORY: loaded {len(hist_rows or [])} rows, project={ctx['pid'][:8] if ctx['pid'] else 'none'}") + for h in (hist_rows or []): + c = getattr(h,'content','') or '' + r = getattr(h,'role','') or '' + if c.startswith('{"action":"tool_call"') or c.startswith('{"action": "tool_call"'): + continue + prefix = "用户" if r == 'user' else "助手" + hist_parts.append(f"{prefix}: {c[:300]}") + if hist_parts: + history_text = "参考历史记录:\n" + '\n'.join(hist_parts[-6:]) # last 6 exchanges only + msgs.append({"role":"system","content":history_text}) msgs.append({"role":"user","content":message_text}) if uid: await sor.C('pipeline_conversations',{'id':getID(),'role':'user','content':message_text,'msg_type':'text','org_id':org_id,'created_by':uid,'iteration_id':ctx['pid'] or ''})