llm

package
v2.3.0 Latest Latest
Warning

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

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

Documentation

Overview

Package llm provides provider-agnostic LLM completion for the sx app. The user picks ONE configured provider (an installed CLI, a local Ollama server, or a hosted API reached with their own key) and every caller — extensions via the sx.llm bridge, future core features — goes through the same Complete call. Nothing in the request or response types is specific to any vendor.

Index

Constants

View Source
const (
	ProviderClaudeCLI = "claude-cli"
	ProviderCodexCLI  = "codex-cli"
	ProviderGeminiCLI = "gemini-cli"
	ProviderOllama    = "ollama"
	ProviderAnthropic = "anthropic"
	ProviderOpenAI    = "openai"
	ProviderGoogle    = "google"
)

Provider IDs. These are the values persisted in the app's LLM config and shown in the settings picker; they never change meaning.

View Source
const DefaultMaxTokens = 8192

DefaultMaxTokens bounds output when the caller doesn't ask for a specific budget.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Provider string `json:"provider"`
	Model    string `json:"model,omitempty"`
	BaseURL  string `json:"baseUrl,omitempty"`
}

Config is the user's provider selection, persisted by the app. The API key is resolved from the OS keyring at call time and never stored here. BaseURL overrides the provider's default endpoint: Ollama's server address, an OpenAI-compatible endpoint (OpenRouter, Groq, vLLM, LM Studio, …), or a gateway in front of Anthropic or Google. CLI providers ignore it.

type Message

type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

Message is one turn of a conversation. Role is "system", "user", or "assistant"; providers that have no native system slot fold system messages into the prompt.

type Provider

type Provider interface {
	ID() string
	Complete(ctx context.Context, req Request) (Response, error)
}

Provider is one way of reaching a model. Implementations must be safe for concurrent use.

func New

func New(cfg Config, apiKey string) (Provider, error)

New builds the Provider for a config. apiKey is the user's key for hosted-API providers (empty for CLI and Ollama providers).

type ProviderInfo

type ProviderInfo struct {
	ID     string `json:"id"`
	Kind   string `json:"kind"` // "cli" | "local" | "api"
	Label  string `json:"label"`
	Detail string `json:"detail,omitempty"` // binary path, server URL, …
	// Available: the provider can be selected right now. CLI providers
	// need the binary installed; Ollama needs a responding server; API
	// providers are always selectable (the key is entered in settings).
	Available bool `json:"available"`
	// Models: for Ollama, the locally pulled models to choose from.
	Models []string `json:"models,omitempty"`
	// NeedsAPIKey / NeedsModel drive which settings inputs to show.
	NeedsAPIKey bool `json:"needsApiKey"`
	NeedsModel  bool `json:"needsModel"`
}

ProviderInfo describes one provider option for the settings picker.

func DetectProviders

func DetectProviders(ctx context.Context, baseURL string) []ProviderInfo

DetectProviders enumerates every provider option and its current availability. baseURL is the user's configured Ollama address ("" for the default).

type Request

type Request struct {
	Messages  []Message       `json:"messages"`
	Schema    json.RawMessage `json:"schema,omitempty"`
	Model     string          `json:"model,omitempty"`
	MaxTokens int             `json:"maxTokens,omitempty"`
}

Request is a provider-neutral completion request. When Schema (a JSON Schema document) is set, the provider must return a JSON document matching it in Response.Text — natively where the backend supports constrained output, via prompt instruction plus extraction otherwise.

type Response

type Response struct {
	Text     string `json:"text"`
	Provider string `json:"provider"`
	Model    string `json:"model"`
	Usage    Usage  `json:"usage"`
}

Response is the result of a completion. Text holds plain prose, or a bare JSON document when the request carried a Schema.

type Usage

type Usage struct {
	InputTokens  int `json:"inputTokens"`
	OutputTokens int `json:"outputTokens"`
}

Usage reports token consumption when the backend exposes it; zero values mean "unknown" (CLI providers often can't report it).

Jump to

Keyboard shortcuts

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