From 1af1afaaeed8c2ec2c0308410dee6eb3ceae6ad5 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 30 Nov 2025 11:57:02 +0800 Subject: [PATCH] Refactor delegated assistant context handling in Stream method - Simplified context management by directly passing the existing context to the delegated assistant's Stream method, ensuring proper stack tracing. - Updated comments to clarify the delegation process and the relationship between parent and delegated assistants, enhancing code readability. --- agent/assistant/next.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/agent/assistant/next.go b/agent/assistant/next.go index 14b5c294..0baae34b 100644 --- a/agent/assistant/next.go +++ b/agent/assistant/next.go @@ -49,19 +49,13 @@ func (ast *Assistant) handleDelegation( return nil, fmt.Errorf("failed to load delegated assistant '%s': %w", delegate.AgentID, err) } - // Create a new context for the delegated call - // Copy relevant fields from the parent context - delegatedCtx := &agentContext.Context{ - Context: ctx.Context, - Locale: ctx.Locale, - Stack: ctx.Stack, // Maintain the call stack - Authorized: ctx.Authorized, - Metadata: ctx.Metadata, - } - - // Call the delegated assistant with provided messages - // The delegated assistant's Stream method will handle the Next hook recursively - return targetAssistant.Stream(delegatedCtx, delegate.Messages, streamHandler) + // Call the delegated assistant with the same context + // The delegated assistant's Stream method will: + // 1. Call EnterStack() to push itself onto the Stack (creating parent-child relationship) + // 2. Execute with the same Context (preserving ID, Space, Writer, etc.) + // 3. Call done() to pop from Stack when finished + // This ensures proper Stack tracing: parent assistant -> delegated assistant + return targetAssistant.Stream(ctx, delegate.Messages, streamHandler) } // buildStandardResponse builds the standard agent response when no custom Next hook processing is needed