yao/agent/assistant/next.go
Max 0fe0843b43 Refactor agent and assistant structure for improved clarity and performance
- Removed deprecated API and vision components, streamlining the agent's architecture.
- Updated the Load function to utilize a new agentDSL variable, enhancing the management of assistant capabilities.
- Enhanced context handling by introducing a message metadata store for thread-safe operations, improving message tracking and management.
- Refactored context methods to eliminate deprecated fields, ensuring cleaner and more maintainable code.
- Improved documentation and comments throughout the codebase to clarify changes and enhance developer understanding.
2025-11-30 11:47:25 +08:00

79 lines
2.6 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) (interface{}, 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,
}, 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,
) (interface{}, 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)
}
// 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)
}
// buildStandardResponse builds the standard agent response when no custom Next hook processing is needed
func (ast *Assistant) buildStandardResponse(npc *NextProcessContext) interface{} {
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,
}
}