swarm

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2026 License: Apache-2.0 Imports: 36 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSessionAborted is returned when a session is aborted by user request
	ErrSessionAborted = goerr.New("session aborted by user")
)

Functions

This section is empty.

Types

type BudgetState

type BudgetState struct {
	CallsAfterSoft int
	TotalCalls     int
	Remaining      float64
	Elapsed        time.Duration
}

BudgetState holds a snapshot of the current budget state for strategy decisions.

type BudgetStatus

type BudgetStatus int

BudgetStatus represents the current state of the action budget.

const (
	// BudgetOK indicates budget is still available.
	BudgetOK BudgetStatus = iota
	// BudgetSoftLimit indicates budget is exhausted but a few more calls are allowed.
	BudgetSoftLimit
	// BudgetHardLimit indicates the hard limit has been reached and tool execution must stop.
	BudgetHardLimit
)

type BudgetStrategy

type BudgetStrategy interface {
	// InitialBudget returns the total budget for a task.
	InitialBudget() float64

	// BeforeToolCall returns the budget cost to consume before executing a tool.
	BeforeToolCall(ctx ToolCallContext) float64

	// AfterToolCall returns additional budget cost based on the tool execution result.
	AfterToolCall(ctx ToolCallContext) float64

	// ShouldExit determines whether tool execution must be forcibly stopped.
	// Called after the soft limit has been hit to decide if the task should be terminated.
	ShouldExit(state BudgetState) bool
}

BudgetStrategy defines the budget consumption logic.

type BudgetTracker

type BudgetTracker struct {
	// contains filtered or unexported fields
}

BudgetTracker tracks the budget state for a single task execution.

func (*BudgetTracker) AfterToolCall

func (t *BudgetTracker) AfterToolCall(toolName string, elapsed time.Duration, result map[string]any, err error)

AfterToolCall consumes additional budget after tool execution based on results.

func (*BudgetTracker) BeforeToolCall

func (t *BudgetTracker) BeforeToolCall(toolName string, elapsed time.Duration) BudgetStatus

BeforeToolCall consumes budget before tool execution and returns the current status.

func (*BudgetTracker) GenerateHandoverInfo

func (t *BudgetTracker) GenerateHandoverInfo() string

GenerateHandoverInfo generates handover information from the tool execution history.

func (*BudgetTracker) Remaining

func (t *BudgetTracker) Remaining() float64

Remaining returns the remaining budget.

type DefaultBudgetStrategy

type DefaultBudgetStrategy struct{}

DefaultBudgetStrategy implements BudgetStrategy with sensible defaults. Target: ~16 tool calls or ~3.5 minutes per task.

func NewDefaultBudgetStrategy

func NewDefaultBudgetStrategy() *DefaultBudgetStrategy

NewDefaultBudgetStrategy creates a new DefaultBudgetStrategy.

func (*DefaultBudgetStrategy) AfterToolCall

func (s *DefaultBudgetStrategy) AfterToolCall(_ ToolCallContext) float64

AfterToolCall always returns 0 for the default strategy.

func (*DefaultBudgetStrategy) BeforeToolCall

func (s *DefaultBudgetStrategy) BeforeToolCall(ctx ToolCallContext) float64

BeforeToolCall calculates the cost before tool execution. Cost = tool fixed cost + time cost delta.

func (*DefaultBudgetStrategy) InitialBudget

func (s *DefaultBudgetStrategy) InitialBudget() float64

InitialBudget returns 100.0 as the total budget.

func (*DefaultBudgetStrategy) ShouldExit

func (s *DefaultBudgetStrategy) ShouldExit(state BudgetState) bool

ShouldExit returns true when more than 3 tool calls have been made after the soft limit.

type Option

type Option func(*SwarmChat)

Option configures a SwarmChat.

func WithBudgetStrategy

func WithBudgetStrategy(s BudgetStrategy) Option

WithBudgetStrategy sets the budget strategy for task execution. When nil (default), budget tracking is disabled and tools execute without limits.

func WithFrontendURL

func WithFrontendURL(url string) Option

WithFrontendURL sets the frontend URL for session links.

func WithMaxPhases

func WithMaxPhases(n int) Option

WithMaxPhases sets the maximum number of execution phases.

func WithMemoryService

func WithMemoryService(svc *memory.Service) Option

WithMemoryService sets the memory service for agent memory integration.

func WithMonitorPollInterval

func WithMonitorPollInterval(d time.Duration) Option

WithMonitorPollInterval sets the session monitor polling interval.

func WithNoAuthorization

func WithNoAuthorization(noAuthz bool) Option

WithNoAuthorization disables policy-based authorization checks.

func WithSlackService

func WithSlackService(svc *slackService.Service) Option

WithSlackService sets the Slack service for message routing.

func WithStorageClient

func WithStorageClient(client interfaces.StorageClient) Option

WithStorageClient sets the storage client for history persistence.

func WithStoragePrefix

func WithStoragePrefix(prefix string) Option

WithStoragePrefix sets the storage prefix for history paths.

func WithSubAgents

func WithSubAgents(subAgents []*agent.SubAgent) Option

WithSubAgents sets the sub-agents available to the agent.

func WithTools

func WithTools(tools []gollem.ToolSet) Option

WithTools sets the tool sets available to the agent.

func WithTraceRepository

func WithTraceRepository(repo trace.Repository) Option

WithTraceRepository sets the trace repository for execution tracing.

func WithUserSystemPrompt

func WithUserSystemPrompt(prompt string) Option

WithUserSystemPrompt sets the user system prompt.

type PlanResult

type PlanResult struct {
	Message string     `json:"message"`
	Tasks   []TaskPlan `json:"tasks"`
}

PlanResult represents the LLM planning response.

type ReplanResult

type ReplanResult struct {
	Message  string     `json:"message,omitempty"`
	Tasks    []TaskPlan `json:"tasks"`
	Question string     `json:"question,omitempty"`
}

ReplanResult represents the LLM replan response.

type SwarmChat

type SwarmChat struct {
	// contains filtered or unexported fields
}

SwarmChat implements interfaces.ChatUseCase with parallel task execution.

func New

func New(repo interfaces.Repository, llmClient gollem.LLMClient, policyClient interfaces.PolicyClient, opts ...Option) *SwarmChat

New creates a new SwarmChat with the given dependencies and options.

func (*SwarmChat) Execute

func (c *SwarmChat) Execute(ctx context.Context, target *ticket.Ticket, message string) error

Execute processes a chat message for the specified ticket using parallel task execution.

type TaskPlan

type TaskPlan struct {
	ID                 string   `json:"id"`
	Title              string   `json:"title"`
	Description        string   `json:"description"`
	AcceptanceCriteria string   `json:"acceptance_criteria"`
	Tools              []string `json:"tools"`
	SubAgents          []string `json:"sub_agents"`
}

TaskPlan represents a single task in the parallel execution plan.

type TaskResult

type TaskResult struct {
	TaskID         string
	Title          string
	Result         string
	Error          error
	BudgetExceeded bool // true if the task was terminated due to budget exhaustion
}

TaskResult holds the outcome of a single task execution.

type ToolCallContext

type ToolCallContext struct {
	ToolName    string
	Elapsed     time.Duration
	PrevElapsed time.Duration // Elapsed time at the previous tool call (for time cost delta)
	CallCount   int
	Result      map[string]any
	Error       error
}

ToolCallContext holds all information about a tool call for cost calculation.

type ToolRecord

type ToolRecord struct {
	Name      string
	Cost      float64
	Timestamp time.Time
}

ToolRecord records a single tool execution for handover information.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL