llm

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultModelCapabilities = NewCapabilityRegistry(map[string]ModelCapabilities{
	"gpt-4o":          {SupportsVision: true, SupportsToolUse: true, SupportsThinking: false},
	"gpt-4.1":         {SupportsVision: true, SupportsToolUse: true, SupportsThinking: false},
	"gpt-5.4":         {SupportsVision: true, SupportsToolUse: true, SupportsThinking: true},
	"gpt-5.4-mini":    {SupportsVision: true, SupportsToolUse: true, SupportsThinking: true},
	"claude-sonnet-4": {SupportsVision: true, SupportsToolUse: true, SupportsThinking: true},
	"qwen3.6-flash":   {SupportsVision: true, SupportsToolUse: true, SupportsThinking: true},
	"qwen3.6-plus":    {SupportsVision: true, SupportsToolUse: true, SupportsThinking: true},
	"qwen3.6-pro":     {SupportsVision: true, SupportsToolUse: true, SupportsThinking: true},
	"qwen3-vl-flash":  {SupportsVision: true, SupportsToolUse: true, SupportsThinking: true},
	"qwen3-vl-plus":   {SupportsVision: true, SupportsToolUse: true, SupportsThinking: true},
})

Functions

func IsContextTooLongMessage

func IsContextTooLongMessage(message string) bool

func IsInvalidRole

func IsInvalidRole(err error) bool

func ValidateMessage

func ValidateMessage(message Message) error

func ValidatePart

func ValidatePart(part Part) error

Types

type AssetID

type AssetID string

type CapabilityRegistry

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

func NewCapabilityRegistry

func NewCapabilityRegistry(static map[string]ModelCapabilities) *CapabilityRegistry

func (*CapabilityRegistry) Learn

func (r *CapabilityRegistry) Learn(model string, caps ModelCapabilities)

func (*CapabilityRegistry) Resolve

func (r *CapabilityRegistry) Resolve(model string) ModelCapabilities

func (*CapabilityRegistry) SetOverride

func (r *CapabilityRegistry) SetOverride(model string, caps ModelCapabilities)

type ChatRequest

type ChatRequest struct {
	Model       string
	Messages    []Message
	Tools       []ToolDefinition
	Assets      map[AssetID]ImageAsset
	Temperature float64

	// OnStreamProgress is called during streaming when the provider receives
	// non-content progress updates (e.g. reasoning_content chunks from
	// DeepSeek-style models). charCount is the cumulative number of
	// characters received so far; active is true while reasoning is still
	// streaming.
	OnStreamProgress func(charCount int, active bool)
}

type Client

type Client interface {
	CreateMessage(ctx context.Context, req ChatRequest) (Message, error)
	StreamMessage(ctx context.Context, req ChatRequest, onDelta func(string)) (Message, error)
}

type ErrorCode

type ErrorCode string
const (
	ErrorCodeImageTooLarge        ErrorCode = "image_too_large"
	ErrorCodeUnsupportedImage     ErrorCode = "unsupported_image"
	ErrorCodeAssetNotFound        ErrorCode = "asset_not_found"
	ErrorCodeClipboardUnavailable ErrorCode = "clipboard_unavailable"
	ErrorCodeImageDecodeFailed    ErrorCode = "image_decode_failed"
	ErrorCodeRateLimited          ErrorCode = "rate_limited"
	ErrorCodeContextTooLong       ErrorCode = "context_too_long"
	ErrorCodeInvalidToolCall      ErrorCode = "invalid_tool_call"
	ErrorCodeUnknown              ErrorCode = "unknown"
)

type FunctionDefinition

type FunctionDefinition struct {
	Name        string         `json:"name"`
	Description string         `json:"description"`
	Parameters  map[string]any `json:"parameters"`
}

type ImageAsset

type ImageAsset struct {
	MediaType string
	Data      []byte
}

type ImagePartRef

type ImagePartRef struct {
	AssetID AssetID `json:"asset_id"`
}

type InternalRequest

type InternalRequest struct {
	Model    string
	Messages []Message
	Tools    []ToolDefinition
	Assets   map[AssetID]ImageAsset
	Stream   bool
}

type Message

type Message struct {
	ID        string      `json:"id,omitempty"`
	Role      Role        `json:"role"`
	Parts     []Part      `json:"content,omitempty"`
	CreatedAt string      `json:"created_at,omitempty"`
	Meta      MessageMeta `json:"meta,omitempty"`
	Usage     *Usage      `json:"usage,omitempty"`

	// Legacy compatibility fields used by existing runner/tui/provider paths.
	Content    string     `json:"-"`
	Name       string     `json:"-"`
	ToolCallID string     `json:"-"`
	ToolCalls  []ToolCall `json:"-"`
}

func ApplyCapabilities

func ApplyCapabilities(messages []Message, caps ModelCapabilities) []Message

func NewAssistantTextMessage

func NewAssistantTextMessage(text string) Message

func NewTextMessage

func NewTextMessage(role Role, text string) Message

func NewToolResultMessage

func NewToolResultMessage(toolUseID, content string) Message

func NewUserTextMessage

func NewUserTextMessage(text string) Message

func (*Message) AppendPart

func (m *Message) AppendPart(part Part)

func (*Message) AppendText

func (m *Message) AppendText(value string)

func (Message) MarshalJSON

func (m Message) MarshalJSON() ([]byte, error)

func (*Message) Normalize

func (m *Message) Normalize()

func (Message) Text

func (m Message) Text() string

func (*Message) UnmarshalJSON

func (m *Message) UnmarshalJSON(data []byte) error

type MessageMeta

type MessageMeta map[string]any

type ModelCapabilities

type ModelCapabilities struct {
	SupportsVision   bool
	SupportsToolUse  bool
	SupportsThinking bool
}

type Part

type Part struct {
	Type       PartType        `json:"type"`
	Text       *TextPart       `json:"text,omitempty"`
	Image      *ImagePartRef   `json:"image,omitempty"`
	ToolUse    *ToolUsePart    `json:"tool_use,omitempty"`
	ToolResult *ToolResultPart `json:"tool_result,omitempty"`
	Thinking   *ThinkingPart   `json:"thinking,omitempty"`
}

one-of: exactly one payload should be populated.

type PartType

type PartType string
const (
	PartText       PartType = "text"
	PartImageRef   PartType = "image_ref"
	PartToolUse    PartType = "tool_use"
	PartToolResult PartType = "tool_result"
	PartThinking   PartType = "thinking"
)

type ProviderClient

type ProviderClient interface {
	Chat(ctx context.Context, req InternalRequest) (<-chan StreamEvent, error)
}

type ProviderError

type ProviderError struct {
	Code      ErrorCode `json:"code"`
	Provider  string    `json:"provider,omitempty"`
	Message   string    `json:"message,omitempty"`
	Status    int       `json:"status,omitempty"`
	Retryable bool      `json:"retryable,omitempty"`
}

func MapProviderError

func MapProviderError(provider string, status int, body string, fallback error) *ProviderError

func WrapError

func WrapError(provider string, code ErrorCode, err error) *ProviderError

func (*ProviderError) Error

func (e *ProviderError) Error() string

type Role

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

type StreamEvent

type StreamEvent struct {
	Type    StreamEventType
	Text    string
	ToolUse *ToolUsePart
	Err     *ProviderError
}

type StreamEventType

type StreamEventType string
const (
	EventTextDelta StreamEventType = "text_delta"
	EventToolUse   StreamEventType = "tool_use"
	EventThinking  StreamEventType = "thinking"
	EventDone      StreamEventType = "done"
	EventError     StreamEventType = "error"
)

type TextPart

type TextPart struct {
	Value string `json:"value"`
}

type ThinkingPart

type ThinkingPart struct {
	Value string `json:"value"`
}

type ToolCall

type ToolCall struct {
	ID       string           `json:"id"`
	Type     string           `json:"type"`
	Function ToolFunctionCall `json:"function"`
}

type ToolDefinition

type ToolDefinition struct {
	Type     string             `json:"type"`
	Function FunctionDefinition `json:"function"`
}

type ToolFunctionCall

type ToolFunctionCall struct {
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type ToolResultPart

type ToolResultPart struct {
	ToolUseID string `json:"tool_use_id"`
	Content   string `json:"content"`
	IsError   bool   `json:"is_error,omitempty"`
}

type ToolUsePart

type ToolUsePart struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Arguments string `json:"arguments"`
}

type Usage

type Usage struct {
	InputTokens   int `json:"input_tokens,omitempty"`
	OutputTokens  int `json:"output_tokens,omitempty"`
	ContextTokens int `json:"context_tokens,omitempty"`
	TotalTokens   int `json:"total_tokens,omitempty"`
}

Jump to

Keyboard shortcuts

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