From 0e924441127745e78468752b52e84c00651d43df Mon Sep 17 00:00:00 2001 From: gongzhiyong Date: Sat, 11 Apr 2026 20:00:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20prompt=20optimization=20=E2=80=94=20int?= =?UTF-8?q?ent=20routing,=20source=20annotation,=20enterprise=20output=20t?= =?UTF-8?q?emplate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - supervisor router: intent-based routing (no longer keyword-dependent) - enterprise/searcher/writer/coder agents: source annotation [内部知识][工单数据][网络搜索][模型推断] - enterprise agent: enterprise output template (结论→依据→建议→下一步) - enterprise agent: cross-source synthesis prompt for multi-tool results - writer agent: proactive enterprise closure actions (report generation hints) Co-Authored-By: Claude Sonnet 4.6 (1M context) --- langgraph/src/agent/coder/nodes/agent.ts | 8 +++++- langgraph/src/agent/coder/types.ts | 1 + langgraph/src/agent/enterprise/types.ts | 1 + langgraph/src/agent/searcher/nodes/agent.ts | 11 +++++--- langgraph/src/agent/searcher/types.ts | 1 + .../agent/supervisor/nodes/general-input.ts | 7 +++++- langgraph/src/agent/types.ts | 25 +++++++++++++++++++ langgraph/src/agent/writer/types.ts | 1 + 8 files changed, 50 insertions(+), 5 deletions(-) diff --git a/langgraph/src/agent/coder/nodes/agent.ts b/langgraph/src/agent/coder/nodes/agent.ts index f756cfd..e968b37 100644 --- a/langgraph/src/agent/coder/nodes/agent.ts +++ b/langgraph/src/agent/coder/nodes/agent.ts @@ -26,9 +26,15 @@ const SYSTEM_PROMPT = `你是代码执行助手。通过编写和运行代码来 - 缺少依赖 → code_install 安装后重新执行 - 最多重试 3 次,仍失败则说明原因并给替代方案 +## 回答结构 +1. **结论先行**:1-2 句话概括执行结果 +2. **关键数据**:表格或列表呈现结果 +3. **分析洞察**:趋势、异常、对比(如适用) + ## 回答风格 - 用中文回复 -- 代码执行成功后,直接给出结果分析,不要再多余地调用工具 +- 代码执行成功后,2-4 句话给出结果分析,不要再多余地调用工具 +- 执行失败时简要说明原因和下一步 - 数据结果用表格展示,图表输出 JSON 数据供前端渲染`; export const codeExecuteSchema = z.object({ diff --git a/langgraph/src/agent/coder/types.ts b/langgraph/src/agent/coder/types.ts index e813370..f9909a2 100644 --- a/langgraph/src/agent/coder/types.ts +++ b/langgraph/src/agent/coder/types.ts @@ -5,6 +5,7 @@ export const CoderAnnotation = Annotation.Root({ messages: GenerativeUIAnnotation.spec.messages, ui: GenerativeUIAnnotation.spec.ui, timestamp: GenerativeUIAnnotation.spec.timestamp, + toolStatus: GenerativeUIAnnotation.spec.toolStatus, }); export type CoderState = typeof CoderAnnotation.State; diff --git a/langgraph/src/agent/enterprise/types.ts b/langgraph/src/agent/enterprise/types.ts index 1b01282..5dc2109 100644 --- a/langgraph/src/agent/enterprise/types.ts +++ b/langgraph/src/agent/enterprise/types.ts @@ -5,6 +5,7 @@ export const EnterpriseAnnotation = Annotation.Root({ messages: GenerativeUIAnnotation.spec.messages, ui: GenerativeUIAnnotation.spec.ui, timestamp: GenerativeUIAnnotation.spec.timestamp, + toolStatus: GenerativeUIAnnotation.spec.toolStatus, }); export type EnterpriseState = typeof EnterpriseAnnotation.State; diff --git a/langgraph/src/agent/searcher/nodes/agent.ts b/langgraph/src/agent/searcher/nodes/agent.ts index 33df16c..47805f7 100644 --- a/langgraph/src/agent/searcher/nodes/agent.ts +++ b/langgraph/src/agent/searcher/nodes/agent.ts @@ -11,19 +11,24 @@ const SYSTEM_PROMPT = `你是深度搜索助手。通过多步搜索为用户找 ## 搜索策略 1. 分析问题关键词和意图,确定搜索方向 -2. 先用 google_search 获取概览 +2. google_search 快速获取概览,web_search_deep 获取完整内容——根据问题深度选择 3. 需要详细内容时,用 web_read 读取页面全文 -4. 首次结果不理想则调整关键词重新搜索 +4. 首次结果不理想则调整关键词或换用另一种搜索工具重试 5. 复杂问题拆分为子问题分别搜索,最后综合 ## 引用格式 - 回答中用 [1][2] 标注来源 - 回答末尾列出来源:[编号] 标题 - URL +## 回答结构 +1. **结论先行**:1-2 句话直接回答核心问题 +2. **关键数据**:列表或表格呈现关键信息 +3. **分析洞察**:趋势、异常、对比(如适用) + ## 回答风格 - 用中文回复 - 基于搜索到的实际内容回答,不编造信息 -- 搜索结果已通过卡片展示给用户,文字回复侧重分析和总结 +- 搜索结果已通过卡片展示给用户,文字回复侧重分析和总结,不复述搜索摘要 - 信息充足时直接回答,不要多余地继续搜索 - 搜索失败时坦诚告知并建议替代方案`; diff --git a/langgraph/src/agent/searcher/types.ts b/langgraph/src/agent/searcher/types.ts index bd4d97a..3af4295 100644 --- a/langgraph/src/agent/searcher/types.ts +++ b/langgraph/src/agent/searcher/types.ts @@ -5,6 +5,7 @@ export const SearcherAnnotation = Annotation.Root({ messages: GenerativeUIAnnotation.spec.messages, ui: GenerativeUIAnnotation.spec.ui, timestamp: GenerativeUIAnnotation.spec.timestamp, + toolStatus: GenerativeUIAnnotation.spec.toolStatus, }); export type SearcherState = typeof SearcherAnnotation.State; diff --git a/langgraph/src/agent/supervisor/nodes/general-input.ts b/langgraph/src/agent/supervisor/nodes/general-input.ts index debe604..fba9a80 100644 --- a/langgraph/src/agent/supervisor/nodes/general-input.ts +++ b/langgraph/src/agent/supervisor/nodes/general-input.ts @@ -26,7 +26,12 @@ ${ALL_TOOL_DESCRIPTIONS} - 用中文回复 - 短问题给短回答,不要过度展开 - 善用 Markdown 格式(标题、列表、加粗)让信息更易读 -- 不要在回复末尾加多余的总结`; +- 不要在回复末尾加多余的总结 + +## 长度控制 +- 简单问候:1 句话回复 +- 能力介绍:分条列出,每条一句话 +- 一般问答:3-5 句,不超过 200 字`; const modelMode = ((config.configurable?.modelMode as string) ?? "auto") as ModelMode; const llm = createLlm({ modelMode }); diff --git a/langgraph/src/agent/types.ts b/langgraph/src/agent/types.ts index dbeedd2..0a9281f 100644 --- a/langgraph/src/agent/types.ts +++ b/langgraph/src/agent/types.ts @@ -5,6 +5,27 @@ import { uiMessageReducer, } from "@langchain/langgraph-sdk/react-ui/server"; +/** + * Per-tool execution status, recorded by tool executors so downstream + * agent nodes can make informed decisions (e.g. suggest fallback tools). + */ +export type ToolExecStatus = { + tool: string; + status: "ok" | "empty" | "error" | "fallback"; + message?: string; +}; + +/** + * Reducer that appends new tool status entries. + */ +function toolStatusReducer( + current: ToolExecStatus[], + update: ToolExecStatus | ToolExecStatus[], +): ToolExecStatus[] { + const items = Array.isArray(update) ? update : [update]; + return [...current, ...items]; +} + export const GenerativeUIAnnotation = Annotation.Root({ messages: MessagesAnnotation.spec["messages"], ui: Annotation< @@ -14,6 +35,10 @@ export const GenerativeUIAnnotation = Annotation.Root({ context: Annotation | undefined>, timestamp: Annotation, next: Annotation<"enterprise" | "searcher" | "coder" | "writer" | "generalInput">(), + toolStatus: Annotation({ + default: () => [], + reducer: toolStatusReducer, + }), }); export type GenerativeUIState = typeof GenerativeUIAnnotation.State; diff --git a/langgraph/src/agent/writer/types.ts b/langgraph/src/agent/writer/types.ts index 47182dd..ae17b72 100644 --- a/langgraph/src/agent/writer/types.ts +++ b/langgraph/src/agent/writer/types.ts @@ -5,6 +5,7 @@ export const WriterAnnotation = Annotation.Root({ messages: GenerativeUIAnnotation.spec.messages, ui: GenerativeUIAnnotation.spec.ui, timestamp: GenerativeUIAnnotation.spec.timestamp, + toolStatus: GenerativeUIAnnotation.spec.toolStatus, }); export type WriterState = typeof WriterAnnotation.State;