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.
This commit is contained in:
Max 2025-11-30 11:57:02 +08:00
parent 0fe0843b43
commit 1af1afaaee

View file

@ -49,19 +49,13 @@ func (ast *Assistant) handleDelegation(
return nil, fmt.Errorf("failed to load delegated assistant '%s': %w", delegate.AgentID, err) return nil, fmt.Errorf("failed to load delegated assistant '%s': %w", delegate.AgentID, err)
} }
// Create a new context for the delegated call // Call the delegated assistant with the same context
// Copy relevant fields from the parent context // The delegated assistant's Stream method will:
delegatedCtx := &agentContext.Context{ // 1. Call EnterStack() to push itself onto the Stack (creating parent-child relationship)
Context: ctx.Context, // 2. Execute with the same Context (preserving ID, Space, Writer, etc.)
Locale: ctx.Locale, // 3. Call done() to pop from Stack when finished
Stack: ctx.Stack, // Maintain the call stack // This ensures proper Stack tracing: parent assistant -> delegated assistant
Authorized: ctx.Authorized, return targetAssistant.Stream(ctx, delegate.Messages, streamHandler)
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)
} }
// buildStandardResponse builds the standard agent response when no custom Next hook processing is needed // buildStandardResponse builds the standard agent response when no custom Next hook processing is needed