Skip to content

Commit 6804441

Browse files
feat: mem优化
1 parent 962b01e commit 6804441

File tree

1 file changed

+44
-9
lines changed

1 file changed

+44
-9
lines changed

backend/service/mcp_client.py

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: ai-business-hql [email protected]
33
Date: 2025-06-16 16:50:17
44
LastEditors: ai-business-hql [email protected]
5-
LastEditTime: 2025-11-20 17:44:52
5+
LastEditTime: 2025-11-25 15:37:50
66
FilePath: /comfyui_copilot/backend/service/mcp-client.py
77
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
88
'''
@@ -20,7 +20,7 @@
2020
from agents.mcp import MCPServerSse
2121
from agents.run import Runner
2222
from agents.tracing import set_tracing_disabled
23-
from agents import handoff, RunContextWrapper
23+
from agents import handoff, RunContextWrapper, HandoffInputData
2424
from agents.extensions import handoff_filters
2525
if not hasattr(__import__('agents'), 'Agent'):
2626
raise ImportError
@@ -157,17 +157,52 @@ class HandoffRewriteData(BaseModel):
157157
async def on_handoff(ctx: RunContextWrapper[None], input_data: HandoffRewriteData):
158158
get_rewrite_context().rewrite_intent = input_data.latest_rewrite_intent
159159
log.info(f"Rewrite agent called with intent: {input_data.latest_rewrite_intent}")
160-
161-
# 重置消息上下文,只保留intent作为唯一的用户输入
162-
# 这样 RewriteAgent 就不会看到之前的全局对话历史,只专注于当前的修改诉求
163-
if hasattr(ctx, 'messages') and isinstance(ctx.messages, list):
164-
ctx.messages.clear()
165-
ctx.messages.append({"role": "user", "content": input_data.latest_rewrite_intent})
166160

161+
def rewrite_handoff_input_filter(data: HandoffInputData) -> HandoffInputData:
162+
"""Filter to replace message history with just the rewrite intent"""
163+
intent = get_rewrite_context().rewrite_intent
164+
log.info(f"Rewrite handoff filter called. Intent: {intent}")
165+
166+
# Construct a new HandoffInputData with cleared history
167+
# We keep new_items (which contains the handoff tool call) so the agent sees the immediate trigger
168+
# But we clear input_history to remove the conversation context
169+
170+
new_history = ()
171+
try:
172+
# Attempt to find a user message in history to clone/modify
173+
# This is a best-effort attempt to make the agent see the intent as a user message
174+
for item in data.input_history:
175+
# Check if item looks like a user message (has role='user')
176+
if hasattr(item, 'role') and getattr(item, 'role') == 'user':
177+
# Try to create a copy with new content if it's a Pydantic model
178+
if hasattr(item, 'model_copy'):
179+
# Pydantic V2
180+
new_item = item.model_copy(update={"content": intent})
181+
new_history = (new_item,)
182+
log.info("Successfully constructed new user message item for handoff (Pydantic V2)")
183+
break
184+
elif hasattr(item, 'copy'):
185+
# Pydantic V1
186+
new_item = item.copy(update={"content": intent})
187+
new_history = (new_item,)
188+
log.info("Successfully constructed new user message item for handoff (Pydantic V1)")
189+
break
190+
except Exception as e:
191+
log.warning(f"Failed to construct user message item: {e}")
192+
193+
# If we couldn't construct a user message, we return empty history.
194+
# The agent will still see the handoff tool call in new_items, which contains the intent.
195+
196+
return HandoffInputData(
197+
input_history=new_history,
198+
pre_handoff_items=(), # Clear pre-handoff items
199+
new_items=tuple(data.new_items), # Keep the handoff tool call
200+
)
201+
167202
handoff_rewrite = handoff(
168203
agent=workflow_rewrite_agent_instance,
169204
input_type=HandoffRewriteData,
170-
input_filter=handoff_filters.remove_all_tools,
205+
input_filter=rewrite_handoff_input_filter,
171206
on_handoff=on_handoff,
172207
)
173208

0 commit comments

Comments
 (0)