package actor import ( "context" "fmt" "github.com/orca/orca/pkg/bus" ) // Worker is an agent that processes tasks and makes tool calls. // // Workers are the execution units in the actor system. They receive // task requests from the orchestrator, process them (potentially making // tool calls), and return results. type Worker struct { *BaseAgent } // NewWorker creates a new Worker agent with the given id. // The agent is started automatically upon creation. 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 routes incoming messages to the appropriate handler. 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 processes a task request and returns a task response. 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 processes a tool call request, transitions to WaitingForTool // state, and returns the result. 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 processes internal system messages. 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 }