orca.ai/pkg/actor/worker.go
大森 6b94476347 Initial commit: Orca Agent Framework
Core features:
- Microkernel architecture with Actor model
- Session management with JSONL persistence
- Tool system (5 built-in tools)
- Skill system with SKILL.md parsing
- Sandbox security execution
- Ollama integration with gemma4:e4b
- Prompt-based tool calling (compatible with native function calling)
- REPL interface

11 packages, all tests passing
2026-05-08 00:55:48 +08:00

89 lines
2.5 KiB
Go

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
}