api

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: May 7, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractText

func ExtractText(parts []ContentPart) string

ExtractText returns the concatenated text of all text-type parts.

func ExtractThinking

func ExtractThinking(parts []ContentPart) string

ExtractThinking returns the concatenated text of all thinking-type parts.

Types

type Agent

type Agent struct {
	Name         string
	Code         bool
	SystemPrompt string
	Toolbox      []ToolSpec
}

Agent represents a configured agent with its properties.

type ChatCompletionProvider

type ChatCompletionProvider interface {
	// CreateChatCompletion generates a chat completion response to the
	// provided messages.
	CreateChatCompletion(
		ctx context.Context,
		params RequestParameters,
		messages []Message,
	) (*Message, error)

	// CreateChatCompletionStream streams the response via the output callback.
	CreateChatCompletionStream(
		ctx context.Context,
		params RequestParameters,
		messages []Message,
		output func(Chunk),
	) (*Message, error)
}

ChatCompletionProvider is the interface for LLM chat completion backends.

type Chunk

type Chunk struct {
	Kind       ChunkKind
	Text       string
	TokenCount uint
}

Chunk represents a streaming token or content fragment from a LLM response.

type ChunkKind added in v0.6.2

type ChunkKind int

ChunkKind identifies the variant of a streaming response chunk.

const (
	ChunkContent  ChunkKind = iota // text content
	ChunkThinking                  // reasoning / thinking
	ChunkToolUse                   // tool call partial JSON
)

type ContentPart

type ContentPart struct {
	Type     ContentPartType `json:"type"`
	Text     string          `json:"text,omitempty"`     // text and thinking parts
	MIMEType string          `json:"mimeType,omitempty"` // image parts (e.g. "image/png")
	Data     []byte          `json:"data,omitempty"`     // raw bytes - base64 encoded when serialized
}

ContentPart represents a single part of a message's content. Messages are composed of one or more content parts — text, thinking (reasoning), or image data. This mirrors the multi-part content model used by Anthropic and OpenAI APIs.

func BuildContentParts

func BuildContentParts(content, reasoning string) []ContentPart

BuildContentParts builds a Parts slice from legacy Content and ReasoningContent strings.

func NewImagePart

func NewImagePart(mimeType string, data []byte) ContentPart

NewImagePart returns an image content part with the given MIME type and raw image bytes.

func NewTextPart

func NewTextPart(text string) ContentPart

NewTextPart returns a text content part.

func NewThinkingPart

func NewThinkingPart(text string) ContentPart

NewThinkingPart returns a thinking/reasoning content part.

type ContentPartType

type ContentPartType string

ContentPartType identifies the kind of a single content part within a message.

const (
	ContentPartText     ContentPartType = "text"
	ContentPartThinking ContentPartType = "thinking"
	ContentPartImage    ContentPartType = "image"
)

type Event

type Event struct {
	Type    EventType
	Chunk   Chunk    // EventChunk
	Message *Message // EventResponseComplete
	Error   error    // EventError
}

Event represents a single event emitted during a completion step. Only the fields relevant to the EventType are populated; the rest are zero-valued. Message is a pointer so that chunk events (the most frequent) carry no weight from it.

type EventType

type EventType int

EventType identifies the kind of event emitted during a completion step.

const (
	EventChunk            EventType = iota // Streaming chunk received
	EventResponseComplete                  // Full response message (may contain tool calls)
	EventError                             // An error occurred
)

type Items

type Items struct {
	Type string `json:"type"` // e.g. "string"
}

Items describes the element type for array tool parameters.

type Message

type Message struct {
	Parts       []ContentPart
	Role        MessageRole
	ToolCalls   []ToolCall
	ToolResults []ToolResult
	Metadata    MessageMeta
}

func NewMessageWithAssistant

func NewMessageWithAssistant(content string, reasoning string) *Message

func NewMessageWithToolCalls

func NewMessageWithToolCalls(content string, toolCalls []ToolCall) *Message

func (*Message) TextContent

func (m *Message) TextContent() string

TextContent returns the concatenated text of all text-type parts.

func (*Message) ThinkingContent

func (m *Message) ThinkingContent() string

ThinkingContent returns the concatenated text of all thinking-type parts.

type MessageBuilder

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

MessageBuilder accumulates streaming content for a message The accumulated content is presented as []ContentPart via Parts().

func NewMessageBuilder

func NewMessageBuilder(role MessageRole) *MessageBuilder

NewMessageBuilder creates a builder for a message with the given role.

func (*MessageBuilder) AppendContent

func (b *MessageBuilder) AppendContent(chunk string)

AppendContent appends a streaming text chunk.

func (*MessageBuilder) AppendReasoning

func (b *MessageBuilder) AppendReasoning(chunk string)

AppendReasoning appends a streaming reasoning/thinking chunk.

func (*MessageBuilder) Build

func (b *MessageBuilder) Build() *Message

Build finalizes the builder into a Message.

func (*MessageBuilder) InitFromParts

func (b *MessageBuilder) InitFromParts(parts []ContentPart)

InitFromParts seeds the builder with existing content parts. Used when continuing to stream into an existing assistant message (prefill).

func (*MessageBuilder) Parts

func (b *MessageBuilder) Parts() []ContentPart

Parts returns the accumulated content as a parts slice. The slice is constructed fresh on each call (from the internal string builders), so callers can use it directly without copying.

func (*MessageBuilder) Reset

func (b *MessageBuilder) Reset()

Reset clears the builder for reuse.

type MessageMeta

type MessageMeta struct {
	GenerationProvider *string         `json:"generation_provider,omitempty"`
	GenerationModel    *string         `json:"generation_model,omitempty"`
	TimeToFirstToken   *time.Duration  `json:"time_to_first_token,omitempty"`
	TotalDuration      *time.Duration  `json:"total_duration,omitempty"`
	ThinkingSignature  *string         `json:"thinking_signature,omitempty"`
	ReasoningDetails   json.RawMessage `json:"reasoning_details,omitempty"` // preserved reasoning_detail blocks (OpenRouter)
}

MessageMeta contains metadata about a message's generation.

func (*MessageMeta) Scan

func (m *MessageMeta) Scan(value any) error

Scan implements sql.Scanner for MessageMeta.

func (MessageMeta) Value

func (m MessageMeta) Value() (driver.Value, error)

Value implements driver.Valuer for MessageMeta.

type MessageRole

type MessageRole string
const (
	MessageRoleSystem     MessageRole = "system"
	MessageRoleUser       MessageRole = "user"
	MessageRoleAssistant  MessageRole = "assistant"
	MessageRoleToolCall   MessageRole = "tool_call"
	MessageRoleToolResult MessageRole = "tool_result"
)

func (MessageRole) FriendlyRole

func (m MessageRole) FriendlyRole() string

FriendlyRole returns a human friendly signifier for the message's role.

func (MessageRole) IsAssistant

func (m MessageRole) IsAssistant() bool

func (MessageRole) IsSystem

func (m MessageRole) IsSystem() bool

func (MessageRole) IsUser

func (m MessageRole) IsUser() bool

type Model

type Model struct {
	Name        string
	Provider    *Provider
	MaxTokens   *int
	Temperature *float32
}

Model represents a resolved model ready for making completion requests. It references its Provider for the client connection.

type Provider

type Provider struct {
	Name    string
	Display string
	Client  ChatCompletionProvider
	Models  []Model
}

Provider holds a fully initialized provider backend with its resolved models.

type ReasoningEffort

type ReasoningEffort string

ReasoningEffort controls how much reasoning the model should perform.

const (
	XHigh   ReasoningEffort = "xhigh"
	High    ReasoningEffort = "high"
	Medium  ReasoningEffort = "medium"
	Low     ReasoningEffort = "low"
	Minimal ReasoningEffort = "minimal"
	None    ReasoningEffort = "none"
)

type RequestParameters

type RequestParameters struct {
	Model       *Model
	Agent       *Agent
	Effort      ReasoningEffort
	Temperature float32
	MaxTokens   int
}

RequestParameters contains everything needed for a single completion request.

func NewRequestParameters

func NewRequestParameters(model *Model, agent *Agent, effort ReasoningEffort, temperature float32, maxTokens int) RequestParameters

NewRequestParameters builds RequestParameters for a completion request.

type ToolCall

type ToolCall struct {
	ID         string         `json:"id"`
	Name       string         `json:"name"`
	Parameters map[string]any `json:"parameters"`
}

type ToolConfirmation

type ToolConfirmation struct {
	Approved bool
	Reason   string
}

ToolConfirmation represents the user's decision for a single tool call. Approved must be set for every tool call (the slice length must match). When Approved is false, Reason is optionally included in the rejection message sent back to the model, giving the user a way to steer the agent.

type ToolParameter

type ToolParameter struct {
	Name        string   `json:"name"`
	Type        string   `json:"type"` // "string", "integer", "boolean", "array"
	Required    bool     `json:"required"`
	Description string   `json:"description"`
	Enum        []string `json:"enum,omitempty"`
	Items       *Items   `json:"items,omitempty"` // for array types
}

type ToolPermission

type ToolPermission string

ToolPermission classifies the kind of access a tool performs.

const (
	ToolPermRead  ToolPermission = "read"  // read-only (Read, Grep, Glob)
	ToolPermWrite ToolPermission = "write" // write (Write, Edit)
	ToolPermExec  ToolPermission = "exec"  // execute (Bash)
)

type ToolResult

type ToolResult struct {
	ToolCallID string `json:"toolCallID"`
	ToolName   string `json:"toolName,omitempty"`
	Result     string `json:"result,omitempty"`
}

type ToolSpec

type ToolSpec struct {
	Name        string
	Description string
	Permission  ToolPermission
	Parameters  []ToolParameter
	Impl        func(*ToolSpec, map[string]any) (string, error)
}

Jump to

Keyboard shortcuts

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