yao/agent/assistant/next.go
Max 5fb5035d8c Enhance Agent Response Structure and Dynamic Test Functionality
- Added support for tool call responses in the agent's response structure, allowing for better handling of tool execution results.
- Updated the `TurnResult` and `TurnResponse` types to include full agent responses, including tool call details and next hook data.
- Improved dynamic test execution by ensuring consistent chat session state across turns, enhancing the overall testing framework's capabilities.
- Enhanced documentation in README.md to reflect changes in response structure and dynamic testing output format.
2025-12-27 12:58:09 +08:00

78 lines
2.8 KiB
Go

package assistant
import (
"fmt"
agentContext "github.com/yaoapp/yao/agent/context"
"github.com/yaoapp/yao/agent/output/message"
)
// processNextResponse processes the Next hook's response and handles agent delegation or custom data
func (ast *Assistant) processNextResponse(npc *NextProcessContext) (*agentContext.Response, error) {
// If no Next hook response, return standard response
if npc.NextResponse == nil {
return ast.buildStandardResponse(npc), nil
}
// Handle Delegate: call another agent
if npc.NextResponse.Delegate != nil {
return ast.handleDelegation(npc.Context, npc.NextResponse.Delegate, npc.StreamHandler)
}
// Handle custom Data: return as-is wrapped in standard Response
if npc.NextResponse.Data != nil {
return &agentContext.Response{
ContextID: npc.Context.ID,
RequestID: npc.Context.RequestID(),
TraceID: npc.Context.TraceID(),
ChatID: npc.Context.ChatID,
AssistantID: ast.ID,
Create: npc.CreateResponse,
Next: npc.NextResponse.Data, // Put custom data in Next field
Completion: npc.CompletionResponse,
Tools: npc.ToolCallResponses,
}, nil
}
// No delegate or data, return standard response
return ast.buildStandardResponse(npc), nil
}
// handleDelegation handles calling another agent based on DelegateConfig
func (ast *Assistant) handleDelegation(
ctx *agentContext.Context,
delegate *agentContext.DelegateConfig,
streamHandler func(message.StreamChunkType, []byte) int,
) (*agentContext.Response, error) {
// Load the target assistant
targetAssistant, err := Get(delegate.AgentID)
if err != nil {
return nil, fmt.Errorf("failed to load delegated assistant '%s': %w", delegate.AgentID, err)
}
// 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
// Convert options map from delegate config to Options struct
delegateOpts := agentContext.OptionsFromMap(delegate.Options)
return targetAssistant.Stream(ctx, delegate.Messages, delegateOpts)
}
// buildStandardResponse builds the standard agent response when no custom Next hook processing is needed
func (ast *Assistant) buildStandardResponse(npc *NextProcessContext) *agentContext.Response {
return &agentContext.Response{
ContextID: npc.Context.ID,
RequestID: npc.Context.RequestID(),
TraceID: npc.Context.TraceID(),
ChatID: npc.Context.ChatID,
AssistantID: ast.ID,
Create: npc.CreateResponse,
Next: npc.NextResponse,
Completion: npc.CompletionResponse,
Tools: npc.ToolCallResponses,
}
}