agent

package
v0.0.0-...-7a259a0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrThreadNotFound = errors.New("agent thread not found")

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	Body       string
}

func (*APIError) Error

func (e *APIError) Error() string

type EventType

type EventType int

EventType classifies a stream event.

const (
	EventText    EventType = iota // text token
	EventToolUse                  // model requested a tool call
	EventStop                     // generation stopped
	EventError                    // provider error
	EventUsage                    // nonterminal provider-reported usage snapshot
)

type Provider

type Provider interface {
	Stream(context.Context, StreamParams, func(StreamEvent) error) error
}

Provider streams model completions. The provider must:

  • Emit zero or more EventText events with text deltas
  • Emit zero or more EventToolUse events (ToolCall must be non-nil)
  • Emit EventUsage snapshots when counts arrive before terminal processing
  • Emit exactly one EventStop OR EventError as the final event
  • EventError is terminal: the provider must return after emitting it
  • Stop streaming if the callback returns a non-nil error

func NewProvider

func NewProvider(kind, apiKey, model, baseURL string) (Provider, error)

type ProviderMessage

type ProviderMessage struct {
	Role       Role
	Content    string
	ToolCalls  []ToolCall
	ToolResult *ToolResult
	// ProviderItems are opaque assistant output retained only in the current run.
	ProviderItems []json.RawMessage `json:"-"`
}

type Role

type Role string
const (
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
	RoleTool      Role = "tool"
)

type Runtime

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

func NewRuntime

func NewRuntime(provider Provider, tools toolExecutor, store *Store) *Runtime

func (*Runtime) DeleteThread

func (r *Runtime) DeleteThread(c *echo.Context) error

func (*Runtime) GetThread

func (r *Runtime) GetThread(c *echo.Context) error

func (*Runtime) ListThreads

func (r *Runtime) ListThreads(c *echo.Context) error

func (*Runtime) Register

func (r *Runtime) Register(group *echo.Group)

func (*Runtime) RenameThread

func (r *Runtime) RenameThread(c *echo.Context) error

func (*Runtime) Run

func (r *Runtime) Run(c *echo.Context) error

type Store

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

Store persists agent threads and runs.

func NewStore

func NewStore(db *sql.DB) *Store

NewStore returns an agent store backed by db.

func (*Store) DeleteThread

func (s *Store) DeleteThread(ctx context.Context, ownerID, threadID string) error

DeleteThread permanently removes one owner-scoped conversation and its runs.

func (*Store) FinishRun

func (s *Store) FinishRun(ctx context.Context, ownerID, threadID, runID string, messages []agtypes.Message, eventJSON [][]byte, truncated bool, runErr error) error

FinishRun persists the final conversation and the run outcome. The thread's messages_json is written wholesale: if two runs on the same thread finish concurrently, the later commit wins (last-write-wins) — concurrent runs on one thread are not a supported flow, and each run row still records its own events and outcome. truncated marks a run whose answer was cut off at the provider's token limit; it only applies when runErr is nil.

func (*Store) RenameThread

func (s *Store) RenameThread(ctx context.Context, ownerID, threadID, title string) error

RenameThread replaces the display title for one owner-scoped conversation.

func (*Store) StartRun

func (s *Store) StartRun(ctx context.Context, ownerID string, input agtypes.RunAgentInput) ([]agtypes.Message, error)

StartRun creates or updates the caller's thread and records the run as running. The SELECT-then-INSERT runs under BEGIN IMMEDIATE on a pinned connection so two concurrent first-runs on the same thread serialize instead of racing into a unique-constraint error.

func (*Store) Thread

func (s *Store) Thread(ctx context.Context, ownerID, threadID string) (Thread, error)

Thread returns one owner-scoped conversation.

func (*Store) Threads

func (s *Store) Threads(ctx context.Context, ownerID string, options ThreadListOptions) ([]ThreadSummary, error)

Threads returns owner-scoped conversation summaries in reverse update order.

type StreamEvent

type StreamEvent struct {
	Type          EventType
	Delta         string            // text token (EventText)
	ToolCall      *ToolCall         // completed tool call (EventToolUse)
	StopReason    string            // e.g. "end_turn", "tool_calls", "length", "max_tokens" (EventStop)
	ToolStep      bool              // a tool call was begun, including an unfinished call (EventStop)
	Error         string            // error message (EventError)
	Model         string            // model reported by the provider response
	Usage         *TokenUsage       // provider-reported token snapshot, including failed calls
	ProviderItems []json.RawMessage `json:"-"` // opaque continuation items (EventStop)
}

StreamEvent is a single event from the model stream; Type discriminates which payload field is set.

type StreamParams

type StreamParams struct {
	System    string
	Messages  []ProviderMessage
	Tools     []ToolDef
	MaxTokens int
}

type Thread

type Thread struct {
	ThreadID string            `json:"id"`
	Messages []agtypes.Message `json:"messages"`
	Updated  string            `json:"updated_at"`
}

Thread is a persisted agent conversation owned by one user.

type ThreadListOptions

type ThreadListOptions struct {
	Query         string
	Limit         int
	BeforeUpdated string
	BeforeID      string
}

ThreadListOptions controls owner-scoped conversation history queries. BeforeUpdated and BeforeID form one compound keyset cursor: callers must provide both or neither. Limit defaults to 30 and is capped at 101.

type ThreadSummary

type ThreadSummary struct {
	ThreadID string `json:"id"`
	Title    string `json:"title"`
	Updated  string `json:"updated_at"`
}

ThreadSummary is the compact representation returned by conversation history.

type TokenUsage

type TokenUsage struct {
	InputTokens      int
	OutputTokens     int
	ReasoningTokens  int
	CacheReadTokens  int
	CacheWriteTokens int
}

TokenUsage includes reasoning tokens in OutputTokens; ReasoningTokens is a subset.

type ToolCall

type ToolCall struct {
	ID    string
	Name  string
	Input string
}

type ToolDef

type ToolDef struct {
	Name        string `json:"name"`
	Description string `json:"description"`
	InputSchema any    `json:"input_schema"`
}

type ToolExecution

type ToolExecution struct {
	Content        string
	Structured     any
	AppResourceURI string
	IsError        bool
}

type ToolRegistry

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

func NewToolRegistry

func NewToolRegistry(ctx context.Context, server *mcp.Server) (*ToolRegistry, error)

func (*ToolRegistry) Close

func (r *ToolRegistry) Close() error

func (*ToolRegistry) Definitions

func (r *ToolRegistry) Definitions() []ToolDef

func (*ToolRegistry) Execute

func (r *ToolRegistry) Execute(ctx context.Context, call ToolCall) (ToolExecution, error)

func (*ToolRegistry) ReadOnly

func (r *ToolRegistry) ReadOnly(name string) bool

type ToolResult

type ToolResult struct {
	ToolCallID string
	Content    string
	IsError    bool
}

Jump to

Keyboard shortcuts

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