diff --git a/wwwroot/api/cockpit_chat.dspy b/wwwroot/api/cockpit_chat.dspy index e3a39d6..8f1bb7c 100644 --- a/wwwroot/api/cockpit_chat.dspy +++ b/wwwroot/api/cockpit_chat.dspy @@ -475,17 +475,21 @@ if action == 'send_message': env_text = f"【当前项目: {ctx['pname'] or '未选择'}】— 所有操作在当前项目内完成\n工作空间: {ctx['ws'] or '未设置'} | 仓库: {repos_str}" system = AGENT_PROMPT.replace('__TOOLS__',TOOLS_TEXT).replace('__ENV__',env_text) msgs = [{"role":"system","content":system}] - for h in (await sor.sqlExe("SELECT role,content FROM pipeline_conversations WHERE created_by=${u}$ ORDER BY created_at ASC LIMIT 20",{"u":uid}) or []): + # 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']} + for h in (await sor.sqlExe(sql, params) or []): c = getattr(h,'content','') or '' r = getattr(h,'role','') or '' - # Skip raw tool_call JSON and contaminated old messages + # 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 - if 'tetris' in c.lower() or '俄罗斯方块' in c: - continue msgs.append({"role":'user' if r=='user' else 'assistant',"content":c}) msgs.append({"role":"user","content":message_text}) - await sor.C('pipeline_conversations',{'id':getID(),'role':'user','content':message_text,'msg_type':'text','org_id':org_id,'created_by':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 ''}) agent_reply = '' for turn in range(10): @@ -521,7 +525,7 @@ if action == 'send_message': d = json.dumps(_w_text(agent_reply), ensure_ascii=False)+'\n' debug(f"YIELD final: {d[:60]}") yield d - await sor.C('pipeline_conversations',{'id':getID(),'role':'agent','content':agent_reply,'msg_type':'text','org_id':org_id,'created_by':'system'}) + await sor.C('pipeline_conversations',{'id':getID(),'role':'agent','content':agent_reply,'msg_type':'text','org_id':org_id,'created_by':'system','iteration_id':ctx['pid'] or ''}) return await stream_response(request, agent_stream, 'text/plain; charset=utf-8')