agent

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SilentTurnNotice    = "Model returned no reply."
	PrefixRewriteNotice = "Request prefix changed: "
)
View Source
const CancelledOutput = "the call was cancelled"
View Source
const InterjectionSeparator = "\n\n"
View Source
const TitleStateKey = "title"
View Source
const (
	TokensPerPricedUnit = 1_000_000
)

Variables

View Source
var (
	ErrNoState       = errors.New("the provider does not expose conversation state")
	ErrStateReplaced = errors.New("the provider replaced append-only conversation state")
	ErrNoListing     = errors.New("it lists no models of its own, and only the registry describes it")
)

Functions

func CacheRebuildCost added in v0.12.0

func CacheRebuildCost(event Event, prices *TokenPrices) (float64, bool)

func CacheRebuildDollars added in v0.12.0

func CacheRebuildDollars(event Event, prices TokenPrices) float64

func CacheRebuildNotice

func CacheRebuildNotice(event Event, cost string) string

func FailureText

func FailureText(event Event) string

func IsCacheRebuildWorthNoticing added in v0.12.0

func IsCacheRebuildWorthNoticing(event Event, prices *TokenPrices) bool

func TitleFromEvent

func TitleFromEvent(event Event) (string, bool)

Types

type Agent

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

func New

func New(systemPrompt string, provider Provider, tools []tool.Tool) *Agent

func NewWithEnabledTools

func NewWithEnabledTools(systemPrompt string, provider Provider, tools []tool.Tool, enabledTools []tool.Tool) *Agent

func (*Agent) AddUserMessage

func (self *Agent) AddUserMessage(text string)

func (*Agent) CacheLifetime

func (self *Agent) CacheLifetime() time.Duration

func (*Agent) Dump

func (self *Agent) Dump() ([]json.RawMessage, error)

func (*Agent) IsToolEnabled

func (self *Agent) IsToolEnabled(name string) bool

func (*Agent) Load

func (self *Agent) Load(items []json.RawMessage) error

func (*Agent) RestoreCache

func (self *Agent) RestoreCache(cacheReading CacheReading)

func (*Agent) RestoreState

func (self *Agent) RestoreState(events []Event) error

func (*Agent) Send

func (self *Agent) Send(ctx context.Context, message string) (string, error)

func (*Agent) StorePicturesWith

func (self *Agent) StorePicturesWith(store func(tool.Image) *Picture)

func (*Agent) Stream

func (self *Agent) Stream(ctx context.Context, message string, interjections *Interjections) iter.Seq2[Update, error]

func (*Agent) TakeRetryWaitsAtOnce

func (self *Agent) TakeRetryWaitsAtOnce()

func (*Agent) TakeTimeFrom

func (self *Agent) TakeTimeFrom(clock func() time.Time)

func (*Agent) Tool

func (self *Agent) Tool(name string) (tool.Tool, bool)

type CacheCause

type CacheCause string
const (
	CacheReopened CacheCause = "reopened"
	CacheExpired  CacheCause = "expired"
	CacheRebuilt  CacheCause = "rebuilt"
	CacheSettling CacheCause = "settling"
)

type CacheLifetimeReporter

type CacheLifetimeReporter interface {
	CacheLifetime() time.Duration
}

type CacheReading

type CacheReading struct {
	ReadTokens int
	At         time.Time
}

type CacheUsage

type CacheUsage struct {
	ReadTokens  int `json:"read_tokens"`
	WriteTokens int `json:"write_tokens,omitempty"`
}

type CallFaulted

type CallFaulted interface {
	error

	FaultedCall() ToolCall
}

type Delta

type Delta struct {
	Kind Kind
	Text string
}

type Event

type Event struct {
	FallbackRendering

	Kind      Kind                  `json:"kind"`
	Text      string                `json:"text,omitempty"`
	Failure   *Failure              `json:"failure,omitempty"`
	ID        string                `json:"id,omitempty"`
	Name      string                `json:"name,omitempty"`
	Arguments string                `json:"arguments,omitempty"`
	Status    Status                `json:"status,omitempty"`
	Took      time.Duration         `json:"took,omitempty"`
	Attempt   int                   `json:"attempt,omitempty"`
	Metrics   *tool.ToolCallMetrics `json:"stats,omitempty"`
	Picture   *Picture              `json:"picture,omitempty"`
	State     json.RawMessage       `json:"state,omitempty"`
	Usage     *Usage                `json:"usage,omitempty"`
}

type Failure

type Failure struct {
	Kind       FailureKind `json:"kind"`
	Context    string      `json:"context,omitempty"`
	Message    string      `json:"message,omitempty"`
	HTTPStatus int         `json:"http_status,omitempty"`
	Code       string      `json:"code,omitempty"`
	Body       string      `json:"body,omitempty"`
	MediaType  string      `json:"media_type,omitempty"`
}

func FailureFrom

func FailureFrom(err error) *Failure

func (Failure) IsContextExceeded

func (self Failure) IsContextExceeded() bool

func (Failure) Text

func (self Failure) Text() string

type FailureKind

type FailureKind string
const (
	GenericFailure    FailureKind = "generic_error"
	HTTPStatusFailure FailureKind = "http_status"
)

type FallbackRendering

type FallbackRendering struct {
	RenderingKind string               `json:"rendering_kind,omitempty"`
	Subject       string               `json:"render,omitempty"`
	Note          string               `json:"detail,omitempty"`
	PathLine      string               `json:"path_line,omitempty"`
	Emphasis      tool.Emphasis        `json:"emphasis,omitzero"`
	Continuation  []tool.CallRendering `json:"continuation,omitempty"`
	Intent        string               `json:"intent,omitempty"`
	Introduces    string               `json:"introduces,omitempty"`
	Mentions      []string             `json:"mentions,omitempty"`
	ShowOutput    bool                 `json:"show_output,omitempty"`
	ReadOnly      bool                 `json:"read_only,omitempty"`
	MarksSuccess  bool                 `json:"marks_success,omitempty"`
}

func (*FallbackRendering) Describe

func (self *FallbackRendering) Describe(toolCall tool.ToolCall)

func (*FallbackRendering) SetRendering added in v0.11.0

func (self *FallbackRendering) SetRendering(rendering tool.CallRendering)

type Interjections

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

func (*Interjections) Add

func (self *Interjections) Add(text string) bool

func (*Interjections) HasMessages added in v0.9.0

func (self *Interjections) HasMessages() bool

func (*Interjections) Note

func (self *Interjections) Note(text string) bool

func (*Interjections) Peek

func (self *Interjections) Peek() []string

func (*Interjections) Take

func (self *Interjections) Take() (string, bool)

func (*Interjections) TakeLast

func (self *Interjections) TakeLast() (string, bool)

func (*Interjections) TakeNotes

func (self *Interjections) TakeNotes() (string, bool)

type Kind

type Kind string
const (
	StartupEvent         Kind = "session_startup"
	UserMessageEvent     Kind = "user_message"
	SilentTurnEvent      Kind = "silent_turn"
	PrefixRewriteEvent   Kind = "prefix_rewrite"
	CacheRebuildEvent    Kind = "cache_rebuild"
	ModelReasoningEvent  Kind = "model_reasoning"
	ModelMessageEvent    Kind = "model_message"
	ToolCallRequestEvent Kind = "tool_call_request"
	ToolCallResultEvent  Kind = "tool_call_result"
	StateChangeEvent     Kind = "state_change"
	InterruptionEvent    Kind = "turn_interruption"
	RetryingEvent        Kind = "request_retry"
	FailureEvent         Kind = "turn_failure"
)

type Lister

type Lister interface {
	Models(context context.Context) ([]Model, error)
}

type Model

type Model struct {
	ID                  string       `json:"id"`
	Name                string       `json:"name,omitempty"`
	EffortLevels        []string     `json:"efforts,omitempty"`
	IsEffortless        bool         `json:"effortless,omitempty"`
	ContextWindowTokens int          `json:"context,omitempty"`
	MaxOutputTokens     int          `json:"output,omitempty"`
	Prices              *TokenPrices `json:"prices,omitempty"`
}

type Output

type Output struct {
	Kind       Kind
	Text       string
	Done       bool
	AwaitUsage bool
	Usage      *Usage
}

type Picture

type Picture struct {
	Digest    string `json:"digest"`
	MediaType string `json:"mediaType"`
	Width     int    `json:"width,omitempty"`
	Height    int    `json:"height,omitempty"`
}

type PriceTier

type PriceTier int
const (
	PriceUnknown PriceTier = iota
	PriceLow
	PriceMedium
	PriceHigh
	PriceExtreme
)

func PriceTiers

func PriceTiers() []PriceTier

func (PriceTier) String

func (self PriceTier) String() string

type Provider

type Provider interface {
	Configure(systemPrompt string, tools []tool.Definition)
	AddUserMessage(text string)
	AddToolResults(toolResults []ToolCallResult)
	Send(ctx context.Context, yield Yield) (Reply, error)
}

type Reply

type Reply struct {
	Calls         []ToolCall
	Usage         Usage
	PrefixRewrite string
}

type Resumable

type Resumable interface {
	error

	Resumable() bool
}

type Retriable

type Retriable interface {
	error

	Retriable() bool
	RetryAfter() time.Duration
}

type State

type State interface {
	Dump() []json.RawMessage
	Load(items []json.RawMessage)
}

type Status

type Status string
const (
	InfoStatus      Status = "info"
	SuccessStatus   Status = "success"
	WarningStatus   Status = "warning"
	ErrorStatus     Status = "error"
	CancelledStatus Status = "cancelled"
)

type TitleState

type TitleState struct {
	Title string `json:"title"`
}

type TokenPrices

type TokenPrices struct {
	Input      float64 `json:"input,omitempty"`
	Output     float64 `json:"output,omitempty"`
	CacheRead  float64 `json:"cache_read,omitempty"`
	CacheWrite float64 `json:"cache_write,omitempty"`
}

func (TokenPrices) BlendedRate

func (self TokenPrices) BlendedRate() float64

func (TokenPrices) IsKnown

func (self TokenPrices) IsKnown() bool

func (TokenPrices) Tier

func (self TokenPrices) Tier() PriceTier

type ToolCall

type ToolCall struct {
	ID        string
	Name      string
	Arguments string
}

type ToolCallResult

type ToolCallResult struct {
	ID      string
	Output  string
	Image   tool.Image
	IsError bool
}

type Update

type Update struct {
	Delta *Delta
	Event *Event
}

type Usage

type Usage struct {
	InputTokens  int         `json:"input_tokens,omitempty"`
	OutputTokens int         `json:"output_tokens,omitempty"`
	Cache        *CacheUsage `json:"cache,omitempty"`
}

type UsageAvailability

type UsageAvailability int
const (
	UsageAvailabilityUnknown UsageAvailability = iota
	UsageAvailabilityAllowed
	UsageAvailabilityLimited
)

type UsageLimitError

type UsageLimitError struct {
	Cause   error
	Windows []UsageWindow
}

func (*UsageLimitError) Error

func (self *UsageLimitError) Error() string

func (*UsageLimitError) Retriable

func (*UsageLimitError) Retriable() bool

func (*UsageLimitError) RetryAfter

func (*UsageLimitError) RetryAfter() time.Duration

func (*UsageLimitError) Unwrap

func (self *UsageLimitError) Unwrap() error

type UsageProbe

type UsageProbe struct {
	Windows      []UsageWindow
	Availability UsageAvailability
	RefreshAfter time.Duration
}

type UsageProber

type UsageProber interface {
	ProbeUsage(context context.Context) (UsageProbe, error)
}

type UsageReporter

type UsageReporter interface {
	IsAvailable() bool
	UsageWindows(context context.Context) ([]UsageWindow, error)
}

type UsageWindow

type UsageWindow struct {
	Duration  time.Duration `json:"duration"`
	Percent   float64       `json:"percent"`
	ResetsAt  time.Time     `json:"resets_at"`
	Scope     string        `json:"scope,omitempty"`
	IsLimited bool          `json:"limited,omitempty"`
}

type Yield

type Yield func(Output) bool

Jump to

Keyboard shortcuts

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