Skip to content

Commit 962b01e

Browse files
feat: 整体优化上下文问题
1 parent e077560 commit 962b01e

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

backend/service/mcp_client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ async def on_handoff(ctx: RunContextWrapper[None], input_data: HandoffRewriteDat
177177
178178
When handing off to workflow rewrite agent or other agents, this session ID should be used for workflow data management.
179179
180+
### PRIMARY DIRECTIVE: INTENT CLASSIFICATION & HANDOFF
181+
You act as a router. Your FIRST step is to classify the user's intent.
182+
183+
**CASE 1: MODIFY/UPDATE/FIX CURRENT WORKFLOW (HIGHEST PRIORITY)**
184+
IF the user wants to:
185+
- Modify, enhance, update, or fix the CURRENT workflow/canvas.
186+
- Add nodes/features to the CURRENT workflow (e.g., "add LoRA", "add controlnet", "fix the error").
187+
- Change parameters in the CURRENT workflow.
188+
- Keywords: "modify", "update", "add", "change", "fix", "current", "canvas", "修改", "更新", "添加", "画布", "加一个", "换一个", "调一下".
189+
190+
**ACTION:**
191+
- You MUST IMMEDIATELY handoff to the `Workflow Rewrite Agent`.
192+
- DO NOT call any other tools (like search_node, gen_workflow).
193+
- DO NOT ask for more details. Just handoff.
194+
195+
**CASE 2: CREATE NEW / SEARCH WORKFLOW**
196+
IF the user wants to find or generate a NEW workflow from scratch.
197+
- Keywords: "create", "generate", "search", "find", "recommend", "生成", "查找", "推荐".
198+
- Action: Use `recall_workflow` AND `gen_workflow`.
199+
200+
### CONSTRAINT CHECKLIST
180201
You must adhere to the following constraints to complete the task:
181202
182203
- [Important!] Respond must in the language used by the user in their question. Regardless of the language returned by the tools being called, please return the results based on the language used in the user's query. For example, if user ask by English, you must return
@@ -198,8 +219,6 @@ async def on_handoff(ctx: RunContextWrapper[None], input_data: HandoffRewriteDat
198219
- If you cannot find the information needed to answer a query, consider using bing_search to obtain relevant information. For example, if search_node tool cannot find the node, you can use bing_search to obtain relevant information about those nodes or components.
199220
- If search_node tool cannot find the node, you MUST use bing_search to obtain relevant information about those nodes or components.
200221
201-
- [Critical!] **WORKFLOW REWRITE Intent** - When the user's intent is to functionally modify, enhance, or add NEW features to the current workflow OR modify the current canvas (keywords: "修改当前工作流", "更新工作流", "在当前工作流中添加", "添加功能", "enhance current workflow", "modify workflow", "add to current workflow", "update current workflow", "添加LoRA", "加个upscale", "add upscaling", "修改当前画布", "更新画布", "在画布中", "modify current canvas", "update canvas", "change canvas"), you MUST always handoff to the Workflow Rewrite Agent immediately.
202-
203222
- **ERROR MESSAGE ANALYSIS** - When a user pastes specific error text/logs (containing terms like "Failed", "Error", "Traceback", or stack traces), prioritize providing troubleshooting help rather than invoking search tools. Follow these steps:
204223
1. Analyze the error to identify the root cause (error type, affected component, missing dependencies, etc.)
205224
2. Explain the issue in simple terms

backend/service/workflow_rewrite_agent.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: ai-business-hql [email protected]
33
Date: 2025-07-24 17:10:23
44
LastEditors: ai-business-hql [email protected]
5-
LastEditTime: 2025-11-24 20:50:57
5+
LastEditTime: 2025-11-24 20:56:38
66
FilePath: /comfyui_copilot/backend/service/workflow_rewrite_agent.py
77
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
88
'''
@@ -61,7 +61,7 @@ def create_workflow_rewrite_agent():
6161
{}
6262
""".format(language, json.dumps(get_rewrite_export_schema())) + """
6363
64-
你可以根据用户的需求,从上面的专家经验中选择一个或多个经验(call get_rewrite_expert_by_name(name_list)),并结合经验内容进行工作流改写
64+
你必须先根据用户的需求,从上面的专家经验中选择经验(call get_rewrite_expert_by_name(name_list)),再结合经验内容进行工作流改写,但如果没有任何相关经验,则不参考专家经验
6565
6666
## 复杂工作流处理原则
6767
复杂工作流实际上是多个简单的功能性工作流的组合。例如:文生图→抠图取主体→图生图生成背景。

backend/service/workflow_rewrite_tools.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import json
44
import time
5+
import copy
56
from typing import Dict, Any, Optional
67

78
try:
@@ -98,13 +99,28 @@ async def get_node_info(node_class: str) -> str:
9899

99100
@function_tool
100101
async def get_node_infos(node_class_list: list[str]) -> str:
101-
"""获取多个节点的详细信息,包括输入输出参数。只做最小化有必要的查询,不要查询所有节点。尽量不要超过5个"""
102+
"""获取多个节点的详细信息,包括输入输出参数。只做最小化有必要的查询,不要查询很常见的LLM已知用法的节点。尽量不要超过5个"""
102103
try:
103104
object_info = await get_object_info()
104105
node_infos = {}
105106
for node_class in node_class_list:
106107
if node_class in object_info:
107-
node_infos[node_class] = object_info[node_class]
108+
# Deep copy to avoid modifying the original cached object info
109+
node_data = copy.deepcopy(object_info[node_class])
110+
111+
# Truncate long lists in input parameters to save context
112+
input_data = node_data.get("input", {})
113+
for req_opt in ["required", "optional"]:
114+
params = input_data.get(req_opt, {})
115+
for param_name, param_config in params.items():
116+
# param_config is typically [type_or_list, config_dict]
117+
if isinstance(param_config, list) and len(param_config) > 0:
118+
# Check if the first element is a list (which means it's a selection list)
119+
if isinstance(param_config[0], list):
120+
if len(param_config[0]) > 3:
121+
param_config[0] = param_config[0][:3]
122+
123+
node_infos[node_class] = node_data
108124
return json.dumps(node_infos)
109125
except Exception as e:
110126
return json.dumps({"error": f"Failed to get node infos of {','.join(node_class_list)}: {str(e)}"})
@@ -116,8 +132,9 @@ async def search_node_local(node_class: str = "", keywords: list[str] = None, li
116132
1. 优先使用 node_class 按节点类名精确查询。
117133
2. 如果没有命中,则在全部节点中使用 node_class 和 keywords 做联合模糊搜索:
118134
- 关键词会在节点类名、名称、显示名、分类、描述以及输入参数名中匹配。
135+
- 关键词是英文。
119136
- 关键词应该尽量具体(例如 "brightness"、"contrast"、"saturation"、"sharpness"),
120-
避免使用过于宽泛的词(例如 "image"、"图像" 等),以减少噪声结果。
137+
避免使用过于宽泛的词(例如 "image" 等),以减少噪声结果。
121138
"""
122139
try:
123140
node_class_str = (node_class or "").strip()

0 commit comments

Comments
 (0)