多语言散落补全
This commit is contained in:
+42
-12
@@ -17693,25 +17693,47 @@ async def agent_chat(request: AgentChatRequest):
|
||||
AGENT_CHAT_HEARTBEAT_SECONDS = 5.0
|
||||
|
||||
_AGENT_CHAT_TOOL_PROGRESS = {
|
||||
"inspect_project_context": ("正在读取当前 job 的项目状态……", "当前 job 状态读取完成,主 Agent 正在继续处理……"),
|
||||
"inspect_analysis_results": ("正在读取当前 job 的分析结果……", "分析结果读取完成,主 Agent 正在继续处理……"),
|
||||
"inspect_job_file": ("正在读取当前 job 中的文件……", "文件读取完成,主 Agent 正在继续处理……"),
|
||||
"query_plantai_rag": ("正在检索知识库……", "知识库检索完成,主 Agent 正在继续处理……"),
|
||||
"recognize_analysis_intent": ("正在识别分析需求……", "分析需求识别完成,主 Agent 正在继续处理……"),
|
||||
"recognize_utility_intent": ("正在识别工具需求……", "工具需求识别完成,主 Agent 正在继续处理……"),
|
||||
"inspect_project_context": {
|
||||
"zh": ("正在读取当前 job 的项目状态……", "当前 job 状态读取完成,主 Agent 正在继续处理……"),
|
||||
"en": ("Reading current job project status...", "Job status read complete; agent continuing..."),
|
||||
},
|
||||
"inspect_analysis_results": {
|
||||
"zh": ("正在读取当前 job 的分析结果……", "分析结果读取完成,主 Agent 正在继续处理……"),
|
||||
"en": ("Reading current job analysis results...", "Analysis results read complete; agent continuing..."),
|
||||
},
|
||||
"inspect_job_file": {
|
||||
"zh": ("正在读取当前 job 中的文件……", "文件读取完成,主 Agent 正在继续处理……"),
|
||||
"en": ("Reading current job files...", "File read complete; agent continuing..."),
|
||||
},
|
||||
"query_plantai_rag": {
|
||||
"zh": ("正在检索知识库……", "知识库检索完成,主 Agent 正在继续处理……"),
|
||||
"en": ("Searching knowledge base...", "Knowledge base search complete; agent continuing..."),
|
||||
},
|
||||
"recognize_analysis_intent": {
|
||||
"zh": ("正在识别分析需求……", "分析需求识别完成,主 Agent 正在继续处理……"),
|
||||
"en": ("Recognizing analysis intent...", "Analysis intent recognized; agent continuing..."),
|
||||
},
|
||||
"recognize_utility_intent": {
|
||||
"zh": ("正在识别工具需求……", "工具需求识别完成,主 Agent 正在继续处理……"),
|
||||
"en": ("Recognizing utility intent...", "Utility intent recognized; agent continuing..."),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _agent_chat_progress_payload(event: dict, elapsed_seconds: int) -> dict:
|
||||
def _agent_chat_progress_payload(event: dict, elapsed_seconds: int, language: str = "zh") -> dict:
|
||||
stage = str(event.get("stage") or "")
|
||||
tool = str(event.get("tool") or "")
|
||||
messages = _AGENT_CHAT_TOOL_PROGRESS.get(tool)
|
||||
pair = _AGENT_CHAT_TOOL_PROGRESS.get(tool, {})
|
||||
messages = pair.get(language) or pair.get("zh")
|
||||
if stage == "tool_started" and messages:
|
||||
message = messages[0]
|
||||
elif stage == "tool_completed" and messages:
|
||||
message = messages[1]
|
||||
else:
|
||||
message = "主 Agent 正在处理请求……"
|
||||
message = {
|
||||
"zh": "主 Agent 正在处理请求……",
|
||||
"en": "Main Agent is processing your request...",
|
||||
}.get(language, "主 Agent 正在处理请求……")
|
||||
return {
|
||||
"stage": stage or "agent_running",
|
||||
"tool": tool,
|
||||
@@ -17726,6 +17748,8 @@ def _agent_chat_sse(event_name: str, payload: dict) -> str:
|
||||
|
||||
@app.post("/api/agent/chat/stream")
|
||||
async def agent_chat_stream(request: AgentChatRequest, http_request: Request):
|
||||
lang = request.language or DEFAULT_AGENT_LANG
|
||||
|
||||
async def event_stream():
|
||||
started_at = time.monotonic()
|
||||
progress_queue: asyncio.Queue[dict] = asyncio.Queue()
|
||||
@@ -17740,7 +17764,10 @@ async def agent_chat_stream(request: AgentChatRequest, http_request: Request):
|
||||
"progress",
|
||||
{
|
||||
"stage": "agent_started",
|
||||
"message": "正在理解你的问题并确定所需信息……",
|
||||
"message": {
|
||||
"zh": "正在理解你的问题并确定所需信息……",
|
||||
"en": "Understanding your question and determining required information...",
|
||||
}.get(lang, "正在理解你的问题并确定所需信息……"),
|
||||
"elapsed_seconds": 0,
|
||||
},
|
||||
)
|
||||
@@ -17760,7 +17787,10 @@ async def agent_chat_stream(request: AgentChatRequest, http_request: Request):
|
||||
"heartbeat",
|
||||
{
|
||||
"stage": "agent_running",
|
||||
"message": f"主 Agent 仍在处理,已用时 {elapsed} 秒……",
|
||||
"message": {
|
||||
"zh": f"主 Agent 仍在处理,已用时 {elapsed} 秒……",
|
||||
"en": f"Main Agent is still processing, {elapsed} s elapsed...",
|
||||
}.get(lang, f"主 Agent 仍在处理,已用时 {elapsed} 秒……"),
|
||||
"elapsed_seconds": elapsed,
|
||||
},
|
||||
)
|
||||
@@ -17768,7 +17798,7 @@ async def agent_chat_stream(request: AgentChatRequest, http_request: Request):
|
||||
elapsed = int(time.monotonic() - started_at)
|
||||
yield _agent_chat_sse(
|
||||
"progress",
|
||||
_agent_chat_progress_payload(event, elapsed),
|
||||
_agent_chat_progress_payload(event, elapsed, language=lang),
|
||||
)
|
||||
|
||||
result = await runner
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
# Plan: Agent 跟随页面语言切换
|
||||
|
||||
## Context
|
||||
|
||||
[页面已有中英文切换](apps/web/vue3/index_ml.html#L1250)(`currentLang` + `switchLang()`),但内嵌的 Agent 对话始终使用默认中文回复。需要在语言切换后将语言参数传递给后端,让 Agent 用对应语言回应。
|
||||
|
||||
## 受影响的 Agent 入口
|
||||
|
||||
**前端 fetch 调用([index_ml.html](apps/web/vue3/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](apps/web/vue3/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 中加一行:
|
||||
```js
|
||||
language: currentLang.value // "zh" 或 "en"
|
||||
```
|
||||
|
||||
位置:
|
||||
1. `sendMessage()` body ~7215
|
||||
2. `requestAgentChatWithProgress()` 的 payload ~22546
|
||||
3. `showInterpretationModal()` body ~9535
|
||||
|
||||
### 后端(1 个文件)
|
||||
|
||||
**Step 1** — 三个 Request 模型各加一个字段:
|
||||
```python
|
||||
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](apps/web/vue3/index_ml.html) | ~3 行新增 |
|
||||
| [main.py](apps/web/vue3/main.py) | ~3 个 model 字段 + ~3 处语言注入逻辑 |
|
||||
|
||||
## 验证
|
||||
|
||||
1. 启动服务,页面切换到英文
|
||||
2. 在主 Agent 输入框发送消息,确认 Agent 用英文回复
|
||||
3. 切回中文,发送消息,确认恢复中文回复
|
||||
4. 测试 RAG Agent 组件和深度解读同样生效
|
||||
Reference in New Issue
Block a user