fix: cockpit_chat_v2 输出 content 流式格式(AgentIO→AgentOut 期望 content/reasoning_content/error,非 widget JSON)
This commit is contained in:
parent
f6e919aa84
commit
803afa5cfe
@ -41,7 +41,7 @@ if action == 'send_message':
|
|||||||
stop_keywords = ['停止', '取消', '停下', '终止', '停', 'stop', 'cancel', 'abort']
|
stop_keywords = ['停止', '取消', '停下', '终止', '停', 'stop', 'cancel', 'abort']
|
||||||
if any(kw == prompt.strip().lower() or kw in prompt.strip().lower() for kw in stop_keywords):
|
if any(kw == prompt.strip().lower() or kw in prompt.strip().lower() for kw in stop_keywords):
|
||||||
async def _stop_stream():
|
async def _stop_stream():
|
||||||
yield json.dumps(_w_md("已停止当前任务。"), ensure_ascii=False) + '\n'
|
yield json.dumps({"content": "已停止当前任务。"}, ensure_ascii=False) + '\n'
|
||||||
return await stream_response(request, _stop_stream, 'text/plain; charset=utf-8')
|
return await stream_response(request, _stop_stream, 'text/plain; charset=utf-8')
|
||||||
|
|
||||||
uid = await get_user()
|
uid = await get_user()
|
||||||
@ -76,40 +76,32 @@ if action == 'send_message':
|
|||||||
async def agent_stream():
|
async def agent_stream():
|
||||||
# 项目上下文
|
# 项目上下文
|
||||||
if ctx['name']:
|
if ctx['name']:
|
||||||
d = json.dumps(_w_progress("项目: " + ctx['name']), ensure_ascii=False) + '\n'
|
yield json.dumps({"reasoning_content": "项目: " + ctx['name'] + "\n"}, ensure_ascii=False) + '\n'
|
||||||
yield d
|
|
||||||
|
|
||||||
# 运行 AgentExecutor
|
# 运行 AgentExecutor(输出 content 流式格式,供前端 AgentIO→AgentOut 渲染)
|
||||||
async for chunk in executor.run(prompt):
|
async for chunk in executor.run(prompt):
|
||||||
data = json.loads(chunk)
|
data = json.loads(chunk)
|
||||||
t = data.get('type', '')
|
t = data.get('type', '')
|
||||||
|
|
||||||
if t == 'progress':
|
if t == 'progress':
|
||||||
d = json.dumps(_w_progress(data.get('message', '')), ensure_ascii=False) + '\n'
|
yield json.dumps({"reasoning_content": data.get('message', '') + "\n"}, ensure_ascii=False) + '\n'
|
||||||
elif t == 'auto_tool':
|
elif t == 'auto_tool':
|
||||||
d = json.dumps(_w_progress("🔄 " + data.get('message', '')), ensure_ascii=False) + '\n'
|
yield json.dumps({"reasoning_content": "🔄 " + data.get('message', '') + "\n"}, ensure_ascii=False) + '\n'
|
||||||
elif t == 'debug':
|
elif t == 'debug':
|
||||||
d = '' # skip debug in UI
|
continue # skip debug in UI
|
||||||
continue
|
|
||||||
elif t == 'tool_call':
|
elif t == 'tool_call':
|
||||||
tool = data.get('tool', '')
|
tool = data.get('tool', '')
|
||||||
d = json.dumps(_w_card("调用: " + tool, "参数: " + json.dumps(data.get('params', {}), ensure_ascii=False), "info"), ensure_ascii=False) + '\n'
|
params = json.dumps(data.get('params', {}), ensure_ascii=False)
|
||||||
|
yield json.dumps({"content": "**🔧 调用: " + tool + "**\n```\n" + params + "\n```\n\n"}, ensure_ascii=False) + '\n'
|
||||||
elif t == 'tool_result':
|
elif t == 'tool_result':
|
||||||
tool = data.get('tool', '')
|
|
||||||
result = data.get('result', '')
|
result = data.get('result', '')
|
||||||
good = not result.startswith('ERROR') and not result.startswith('未知工具')
|
yield json.dumps({"content": result + "\n\n"}, ensure_ascii=False) + '\n'
|
||||||
# markdown 内容用 MdWidget 渲染(表格、列表等)
|
|
||||||
is_md = result.startswith('|') or result.startswith('#') or result.startswith('- ')
|
|
||||||
body = _w_md(result[:2000]) if is_md else result[:600]
|
|
||||||
d = json.dumps(_w_card(tool + " 结果", body, "success" if good else "error"), ensure_ascii=False) + '\n'
|
|
||||||
elif t == 'reply':
|
elif t == 'reply':
|
||||||
d = json.dumps(_w_md(data.get('message', '')), ensure_ascii=False) + '\n'
|
yield json.dumps({"content": data.get('message', '')}, ensure_ascii=False) + '\n'
|
||||||
elif t == 'error':
|
elif t == 'error':
|
||||||
d = json.dumps(_w_card("错误", data.get('message', ''), "error"), ensure_ascii=False) + '\n'
|
yield json.dumps({"error": data.get('message', '')}, ensure_ascii=False) + '\n'
|
||||||
else:
|
else:
|
||||||
d = chunk # 透传
|
yield json.dumps({"content": chunk}, ensure_ascii=False) + '\n'
|
||||||
|
|
||||||
yield d
|
|
||||||
|
|
||||||
return await stream_response(request, agent_stream, 'text/plain; charset=utf-8')
|
return await stream_response(request, agent_stream, 'text/plain; charset=utf-8')
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user