Files
PlantAI-dev/docs/agent-language-switching-plan.md
T
2026-08-09 11:38:03 +08:00

2.5 KiB
Raw Blame History

Plan: Agent 跟随页面语言切换

Context

页面已有中英文切换(currentLang + switchLang()),但内嵌的 Agent 对话始终使用默认中文回复。需要在语言切换后将语言参数传递给后端,让 Agent 用对应语言回应。

受影响的 Agent 入口

前端 fetch 调用(index_ml.html):

调用点 行号 端点 用途
RAG Agent sendMessage ~7211 /api/agent/execute RAG Agent 组件对话
requestAgentChatWithProgress ~22179 /api/agent/chat/stream 主 Agent 流式对话
showInterpretationModal ~9533 /api/agent/interpret 深度解读报告

后端 Request 模型(main.py):

模型 行号 对应端点
AgentChatRequest 16424 /api/agent/chat, /api/agent/chat/stream
AgentExecuteRequest 18198 /api/agent/execute
GenerateInterpretationRequest 23733 /api/agent/interpret

(AgentPlanRequest 18489 和 AgentRunRequest 22814 也可顺带加上,但影响较小。)

实现方案

前端(1 个文件)

在每个 Agent fetch payload 中加一行:

language: currentLang.value  // "zh" 或 "en"

位置:

  1. sendMessage() body ~7215
  2. requestAgentChatWithProgress() 的 payload ~22546
  3. showInterpretationModal() body ~9535

后端(1 个文件)

Step 1 — 三个 Request 模型各加一个字段:

language: str = "zh"  # "zh" or "en"

Step 2 — 在核心处理函数中注入语言指令。当 language == "en" 时,在用户消息前拼接指令:

  • _run_agent_chat() (~17131):在传给 RouterAgent 的 payload["query"] 前拼接英文指令
  • agent_execute() (~18218):在 request.message 存入 task 之前拼接,或存入 task state 供后续 LLM 调用使用
  • generate_interpretation() (~23751):在构造 LLM 提示词时根据 request.language 追加语言指令

修改文件

文件 改动数
index_ml.html ~3 行新增
main.py ~3 个 model 字段 + ~3 处语言注入逻辑

验证

  1. 启动服务,页面切换到英文
  2. 在主 Agent 输入框发送消息,确认 Agent 用英文回复
  3. 切回中文,发送消息,确认恢复中文回复
  4. 测试 RAG Agent 组件和深度解读同样生效