llm

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeToolArguments added in v0.4.0

func NormalizeToolArguments(raw json.RawMessage) json.RawMessage

NormalizeToolArguments returns tool-call arguments that are always valid JSON.

Providers often deliver arguments as a JSON string (possibly empty) or as a raw object. Streaming models may also truncate mid-object. Empty or whitespace-only input becomes {}, matching the OpenAI empty-arguments convention. Malformed/truncated payloads also become {}: json.RawMessage must be valid JSON or json.Marshal fails with "error calling MarshalJSON for type json.RawMessage: unexpected end of JSON input", which aborts HITL checkpoint persistence before the tool can validate input.

func ProfileSupports

func ProfileSupports(profile Profile, capability Capability, defaults ...Capability) bool

Types

type APIError

type APIError struct {
	Provider   string
	StatusCode int
	Status     string
	Body       string
}

func (APIError) Error

func (err APIError) Error() string

func (APIError) Retryable

func (err APIError) Retryable() bool

type Capability

type Capability int
const (
	CapChat Capability = iota
	CapToolCall
	CapStructuredOutput
	CapStream
	CapEmbed
)

func CapabilitiesFromStrings

func CapabilitiesFromStrings(values []string) []Capability

func ParseCapability

func ParseCapability(value string) (Capability, bool)

func (Capability) String

func (c Capability) String() string

type ChatChunk

type ChatChunk struct {
	Kind    ChunkKind `json:"kind,omitempty"`
	Content string    `json:"content,omitempty"`
	Done    bool      `json:"done,omitempty"`
	Error   string    `json:"error,omitempty"`
	// Err carries the structured provider error behind Error when one exists,
	// so retry classification (e.g. APIError.Retryable) survives the streaming
	// path instead of being flattened into an opaque string. It is process-local
	// only and never serialized.
	Err          error           `json:"-"`
	Usage        TokenUsage      `json:"usage,omitempty"`
	Paused       bool            `json:"paused,omitempty"`
	PauseToken   string          `json:"pause_token,omitempty"`
	PauseKind    string          `json:"pause_kind,omitempty"`
	ToolCallID   string          `json:"tool_call_id,omitempty"`
	ToolName     string          `json:"tool_name,omitempty"`
	ToolInput    json.RawMessage `json:"tool_input,omitempty"`
	ToolOutput   json.RawMessage `json:"tool_output,omitempty"`
	ToolError    string          `json:"tool_error,omitempty"`
	ToolProgress json.RawMessage `json:"tool_progress,omitempty"`
}

func (ChatChunk) IsAnswerContent added in v0.3.0

func (c ChatChunk) IsAnswerContent() bool

IsAnswerContent reports whether this chunk contributes to the final answer text aggregated by Engine.Stream. Tool progress/result events must not.

type ChatRequest

type ChatRequest struct {
	Messages        []Message         `json:"messages"`
	Temperature     *float32          `json:"temperature,omitempty"`
	TopP            *float32          `json:"top_p,omitempty"`
	MaxTokens       int               `json:"max_tokens,omitempty"`
	Thinking        ThinkingConfig    `json:"thinking,omitempty"`
	ReasoningEffort string            `json:"reasoning_effort,omitempty"`
	ExtraBody       map[string]any    `json:"extra_body,omitempty"`
	Metadata        map[string]string `json:"metadata,omitempty"`
}

type ChatResponse

type ChatResponse struct {
	Message      Message         `json:"message"`
	Usage        TokenUsage      `json:"usage"`
	FinishReason string          `json:"finish_reason,omitempty"`
	Raw          json.RawMessage `json:"raw,omitempty"`
}

type Chatter

type Chatter interface {
	Chat(ctx context.Context, profile string, req ChatRequest) (ChatResponse, error)
}

type ChunkKind added in v0.3.0

type ChunkKind string

ChunkKind discriminates Stream progress events. Empty Kind is treated as answer content for backward compatibility with provider token streams.

const (
	ChunkKindContent      ChunkKind = ""
	ChunkKindToolCall     ChunkKind = "tool_call"
	ChunkKindToolResult   ChunkKind = "tool_result"
	ChunkKindToolDenied   ChunkKind = "tool_denied"
	ChunkKindToolProgress ChunkKind = "tool_progress"
)

type Embedder

type Embedder interface {
	Embed(ctx context.Context, profile string, input []string) ([][]float32, error)
}

type Gateway

type Gateway interface {
	Supports(profile string, cap Capability) bool
	Chatter
}

type Message

type Message struct {
	Role             Role              `json:"role"`
	Content          string            `json:"content,omitempty"`
	ReasoningContent string            `json:"reasoning_content,omitempty"`
	ToolCalls        []ToolCall        `json:"tool_calls,omitempty"`
	Name             string            `json:"name,omitempty"`
	ToolCallID       string            `json:"tool_call_id,omitempty"`
	Metadata         map[string]string `json:"metadata,omitempty"`
}

func NormalizeMessageToolInputs added in v0.4.0

func NormalizeMessageToolInputs(messages []Message) []Message

NormalizeMessageToolInputs returns a shallow copy of messages whose assistant tool-call inputs are normalized for safe JSON marshaling (checkpoints, iteration snapshots). Messages without tool calls are reused as-is.

type Profile

type Profile struct {
	Name                string               `json:"name"`
	Provider            string               `json:"provider"`
	Model               string               `json:"model"`
	Endpoint            string               `json:"endpoint,omitempty"`
	APIKeyEnv           string               `json:"api_key_env,omitempty"`
	ContextWindowTokens int                  `json:"context_window_tokens,omitempty"`
	MaxOutputTokens     int                  `json:"max_output_tokens,omitempty"`
	Temperature         *float32             `json:"temperature,omitempty"`
	TopP                *float32             `json:"top_p,omitempty"`
	Timeout             time.Duration        `json:"timeout,omitempty"`
	Thinking            ThinkingConfig       `json:"thinking,omitempty"`
	ReasoningEffort     string               `json:"reasoning_effort,omitempty"`
	Context             contextwindow.Policy `json:"context,omitempty"`
	ExtraBody           map[string]any       `json:"extra_body,omitempty"`
	Capabilities        []Capability         `json:"capabilities,omitempty"`
	Metadata            map[string]string    `json:"metadata,omitempty"`
}

type Role

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

type Streamer

type Streamer interface {
	StreamChat(ctx context.Context, profile string, req ChatRequest) (<-chan ChatChunk, error)
}

type StructuredOutputter

type StructuredOutputter interface {
	StructuredChat(ctx context.Context, profile string, schema json.RawMessage, req ChatRequest) (json.RawMessage, error)
}

type ThinkingConfig

type ThinkingConfig struct {
	Enabled      bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
	BudgetTokens int  `json:"budget_tokens,omitempty" yaml:"budget_tokens,omitempty"`
}

type TokenUsage

type TokenUsage struct {
	InputTokens     int `json:"input_tokens,omitempty"`
	OutputTokens    int `json:"output_tokens,omitempty"`
	ReasoningTokens int `json:"reasoning_tokens,omitempty"`
	TotalTokens     int `json:"total_tokens,omitempty"`
}

type ToolCall

type ToolCall struct {
	ID    string          `json:"id"`
	Name  string          `json:"name"`
	Input json.RawMessage `json:"input,omitempty"`
}

func NormalizeToolCallInputs added in v0.4.0

func NormalizeToolCallInputs(calls []ToolCall) []ToolCall

NormalizeToolCallInputs rewrites every ToolCall.Input with NormalizeToolArguments.

type ToolCallRequest

type ToolCallRequest struct {
	ChatRequest
	Tools []ToolSpec `json:"tools,omitempty"`
}

type ToolCallResponse

type ToolCallResponse struct {
	ChatResponse
	ToolCalls []ToolCall `json:"tool_calls,omitempty"`
}

type ToolCallStreamer added in v0.3.0

type ToolCallStreamer interface {
	StreamChatWithTools(ctx context.Context, profile string, req ToolCallRequest) (<-chan ChatChunk, error)
}

ToolCallStreamer streams a tool-enabled chat turn. Content can precede ChunkKindToolCall frames, so consumers must classify the completed turn before committing content as a final answer.

type ToolCaller

type ToolCaller interface {
	ChatWithTools(ctx context.Context, profile string, req ToolCallRequest) (ToolCallResponse, error)
}

type ToolSpec

type ToolSpec struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Schema      json.RawMessage `json:"schema,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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