yao/agent/robot/executor/standard/host.go
Max bc4787f857 Update executor to support V2 execution model and enhance event handling
- Implement V2 execution model in the standard executor, simplifying task execution to a single call without validation loops.
- Introduce support for resuming suspended executions, allowing for human input during task processing.
- Enhance event handling by pushing task completion and failure events to the event bus for better tracking and integration.
- Update tests to reflect changes in execution flow and ensure robust handling of task statuses and results.
2026-02-25 18:40:48 +08:00

62 lines
1.9 KiB
Go

package standard
import (
"encoding/json"
"fmt"
"github.com/yaoapp/kun/log"
robottypes "github.com/yaoapp/yao/agent/robot/types"
)
// CallHostAgent calls the Host Agent with structured input and parses structured output.
// The Host Agent mediates all human-robot interactions through three scenarios:
// - "assign": new task assignment with multi-round confirmation
// - "guide": guidance during execution
// - "clarify": answering questions from waiting tasks
func (e *Executor) CallHostAgent(ctx *robottypes.Context, robot *robottypes.Robot, input *robottypes.HostInput, chatID string) (*robottypes.HostOutput, error) {
if robot == nil {
return nil, fmt.Errorf("robot cannot be nil")
}
agentID := ""
if robot.Config != nil && robot.Config.Resources != nil {
agentID = robot.Config.Resources.GetPhaseAgent(robottypes.PhaseHost)
}
if agentID == "" {
return nil, fmt.Errorf("no Host Agent configured for robot %s", robot.MemberID)
}
inputJSON, err := json.Marshal(input)
if err != nil {
return nil, fmt.Errorf("failed to marshal host input: %w", err)
}
log.Info("calling Host Agent %s for scenario=%s chatID=%s", agentID, input.Scenario, chatID)
caller := NewConversationCaller(chatID)
result, err := caller.CallWithMessages(ctx, agentID, string(inputJSON))
if err != nil {
return nil, fmt.Errorf("host agent (%s) call failed: %w", agentID, err)
}
data, err := result.GetJSON()
if err != nil {
text := result.GetText()
log.Warn("Host Agent returned non-JSON response, treating as confirm: %s", text)
return &robottypes.HostOutput{
Reply: text,
Action: robottypes.HostActionConfirm,
}, nil
}
output := &robottypes.HostOutput{}
raw, _ := json.Marshal(data)
if err := json.Unmarshal(raw, output); err != nil {
return &robottypes.HostOutput{
Reply: result.GetText(),
Action: robottypes.HostActionConfirm,
}, nil
}
return output, nil
}