|
2 | 2 | Author: ai-business-hql [email protected] |
3 | 3 | Date: 2025-06-16 16:50:17 |
4 | 4 | LastEditors: ai-business-hql [email protected] |
5 | | -LastEditTime: 2025-11-20 17:44:52 |
| 5 | +LastEditTime: 2025-11-25 15:37:50 |
6 | 6 | FilePath: /comfyui_copilot/backend/service/mcp-client.py |
7 | 7 | Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE |
8 | 8 | ''' |
|
20 | 20 | from agents.mcp import MCPServerSse |
21 | 21 | from agents.run import Runner |
22 | 22 | from agents.tracing import set_tracing_disabled |
23 | | - from agents import handoff, RunContextWrapper |
| 23 | + from agents import handoff, RunContextWrapper, HandoffInputData |
24 | 24 | from agents.extensions import handoff_filters |
25 | 25 | if not hasattr(__import__('agents'), 'Agent'): |
26 | 26 | raise ImportError |
@@ -157,17 +157,52 @@ class HandoffRewriteData(BaseModel): |
157 | 157 | async def on_handoff(ctx: RunContextWrapper[None], input_data: HandoffRewriteData): |
158 | 158 | get_rewrite_context().rewrite_intent = input_data.latest_rewrite_intent |
159 | 159 | 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}) |
166 | 160 |
|
| 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 | + |
167 | 202 | handoff_rewrite = handoff( |
168 | 203 | agent=workflow_rewrite_agent_instance, |
169 | 204 | input_type=HandoffRewriteData, |
170 | | - input_filter=handoff_filters.remove_all_tools, |
| 205 | + input_filter=rewrite_handoff_input_filter, |
171 | 206 | on_handoff=on_handoff, |
172 | 207 | ) |
173 | 208 |
|
|
0 commit comments