agent

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package agent defines concepts for agentic applications

Index

Constants

This section is empty.

Variables

View Source
var NoParamsSchema = map[string]any{
	"type":       "object",
	"properties": map[string]any{},
}

Functions

func FindExternalTool

func FindExternalTool(a *Agent, name string) (llmproxy.ExternalTool, bool)

FindExternalTool searches for an ExternalTool by name in the agent's tools.

Types

type Agent

type Agent struct {
	// Name is the name of the agent.
	Name string `json:"name"`

	// Instructions act as system prompt and are the behavior guidelines for the agent.
	Instructions string `json:"instructions,omitempty"`

	// ResponseFormat defines the structured response format the agent should use if provided.
	ResponseFormat *llmproxy.ResponseFormat `json:"responseFormat,omitempty"`

	// Tools is the list of tools available to the agent (including handoff tools).
	Tools []llmproxy.Tool `json:"tools,omitempty"`

	// Options carries per-agent generation knobs (temperature, max_tokens, etc.) that
	// the runner copies into each ChatRequest. Nil means provider defaults.
	Options *llmproxy.Options `json:"options,omitempty"`
}

Agent represents an LLM agent with a set of tools and instructions. It encapsulates the agent's identity, behavior guidelines, and available functionalities. Can be reused across different LLM providers, models and runs.

func NewAgent

func NewAgent(name string, opts ...Option) *Agent

NewAgent creates a new Agent with the given name and applies any provided options.

func (*Agent) AsTool

func (a *Agent) AsTool(name, description string, runner *Runner) (llmproxy.Tool, error)

AsTool converts the Agent into a Tool that can be used by other agents. The tool, when called, runs the agent with the provided input prompt and returns the final output.

runner: The Runner instance to use for executing the agent when called as a tool. This allows the agent to be invoked with its own execution context, configuration like model or max turns, and state management.

type Event

type Event struct {
	Type       EventType
	Turn       int                       // current turn (1-based)
	Text       string                    // message / final output
	ToolCall   *llmproxy.ToolCallRequest // tool_call only
	ToolResult *llmproxy.ToolResult      // tool_result only
}

Event represents an occurrence during agent execution. The runner emits all event types. Errors are not emitted as events — they are returned as Go errors and the caller (handler) decides how to surface them (e.g. as SSE error events).

type EventHandler

type EventHandler func(Event)

EventHandler receives events during agent execution.

type EventType

type EventType string

EventType identifies the kind of event emitted during agent execution.

const (
	EventMessage    EventType = "message"
	EventToolCall   EventType = "tool_call"
	EventToolResult EventType = "tool_result"
	EventFinal      EventType = "final"
)

type Handoff

type Handoff struct {
	llmproxy.ExternalToolBase

	Agent *Agent `json:"-"`

	// Optional model override for the handoff agent. If not set, the runner's default model will be used instead.
	Model *llmproxy.ModelID `json:"-"`
}

Handoff is an external tool that represents handing off control to another agent.

func NewHandoff

func NewHandoff(name, description string, agent *Agent, model *llmproxy.ModelID) Handoff

NewHandoff creates a new Handoff tool that invokes the given agent.

type Option

type Option func(*Agent)

Option defines a configuration option for Agent.

func WithInstructions

func WithInstructions(instructions string) Option

WithInstructions sets the Instructions field of the Agent.

func WithOptions

func WithOptions(opts *llmproxy.Options) Option

WithOptions sets per-agent generation options (copied into each ChatRequest by the runner).

func WithResponseFormat

func WithResponseFormat(format *llmproxy.ResponseFormat) Option

WithResponseFormat sets the ResponseFormat field of the Agent.

func WithTools

func WithTools(tools ...llmproxy.Tool) Option

WithTools sets the Tools field of the Agent.

type RunResult

type RunResult struct {
	// FinalOutput is the output of the last agent.
	FinalOutput any

	// LastAgent is the agent that produced the final output.
	LastAgent *Agent

	// Turns is the number of turns (LLM invocations) taken by the runner.
	// This does not include turns taken by sub-agents called as tools.
	Turns int
}

RunResult contains the result of running an agent/workflow.

type Runner

type Runner struct {
	DefaultModel llmproxy.ModelID
	MaxTurns     *int
	// contains filtered or unexported fields
}

Runner executes agents using the configured RunConfig.

func NewRunner

func NewRunner(client llmClient, model llmproxy.ModelID, opts ...RunnerOption) *Runner

NewRunner creates a new Runner with the given model.

func (Runner) Run

func (r Runner) Run(ctx context.Context, startingAgent *Agent, input llmproxy.Input) (*RunResult, error)

Run executes a workflow starting at the given agent. The agent runs in a loop until a final output is generated or the maximum number of turns is reached.

Loop steps:

  1. Invoke the current agent with input.
  2. If a final output is produced (structured: any text; plain: text and no tool calls), return.
  3. If a handoff occurs, switch agent/model and continue with same context.
  4. If tool calls are present, execute them and update input for the next turn.
  5. Repeat until final output or max turns.

Returns an error if neither output nor tool calls are produced, or if max turns is exceeded.

type RunnerOption

type RunnerOption func(*Runner)

RunnerOption defines a configuration option for Runner.

func WithEventHandler

func WithEventHandler(h EventHandler) RunnerOption

WithEventHandler sets a callback that receives events during agent execution.

func WithLogger

func WithLogger(l zerolog.Logger) RunnerOption

WithLogger sets the structured logger used to emit Activity events ("llm_generate" per turn, "agent_run" on success). When unset, the runner stays silent — backend in-process callers (e.g. /llm/test-agent SSE) rely on this to avoid spurious agent_activity rows.

func WithMaxTurns

func WithMaxTurns(n int) RunnerOption

WithMaxTurns sets the maximum number of turns the agent can take before stopping. This does not include turns taken by sub-agents called as tools.

Jump to

Keyboard shortcuts

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