// Package actor 为 Orca 框架实现 Actor 模型。 package actor import ( "context" "fmt" "github.com/orca/orca/pkg/bus" ) // Worker 是一个处理任务并进行工具调用的智能体。 // // Worker 是 Actor 系统中的执行单元。它们接收来自编排器的 // 任务请求,处理这些任务(可能需要进行工具调用),并返回结果。 type Worker struct { *BaseAgent } // NewWorker 使用给定的 ID 创建一个新的 Worker 智能体。 // 智能体在创建时会自动启动。 func NewWorker(id string) *Worker { w := &Worker{ BaseAgent: NewBaseAgent(id, "worker"), } w.SetHandler(w.handleMessage) if err := w.Start(); err != nil { panic(fmt.Sprintf("worker: failed to start: %v", err)) } return w } // handleMessage 将传入的消息路由到适当的处理程序。 func (w *Worker) handleMessage(ctx context.Context, msg bus.Message) (bus.Message, error) { switch msg.Type { case bus.MsgTypeTaskRequest: return w.handleTask(ctx, msg) case bus.MsgTypeToolCall: return w.handleToolCall(ctx, msg) case bus.MsgTypeSystem: return w.handleSystem(ctx, msg) default: return bus.Message{}, fmt.Errorf("worker %s: unsupported message type %s", w.ID(), msg.Type) } } // handleTask 处理任务请求并返回任务响应。 func (w *Worker) handleTask(ctx context.Context, msg bus.Message) (bus.Message, error) { // Process the task - in a real implementation this would involve // the LLM, tool calls, etc. return bus.Message{ ID: msg.ID + "-response", Type: bus.MsgTypeTaskResponse, From: w.ID(), To: msg.From, Content: msg.Content, Metadata: map[string]string{ "processed_by": w.ID(), }, }, nil } // handleToolCall 处理工具调用请求,转换到 WaitingForTool 状态,并返回结果。 func (w *Worker) handleToolCall(ctx context.Context, msg bus.Message) (bus.Message, error) { w.setStatus(StatusWaitingForTool) defer w.setStatus(StatusProcessing) // In a real implementation, this would invoke the actual tool. // For now, acknowledge the tool call. return bus.Message{ ID: msg.ID + "-result", Type: bus.MsgTypeToolResult, From: w.ID(), To: msg.From, Content: msg.Content, }, nil } // handleSystem 处理内部系统消息。 func (w *Worker) handleSystem(ctx context.Context, msg bus.Message) (bus.Message, error) { return bus.Message{ ID: msg.ID + "-ack", Type: bus.MsgTypeSystem, From: w.ID(), To: msg.From, Content: "worker acknowledged", }, nil }