Documentation
¶
Overview ¶
Package model resolves providers, model metadata, auth, and thinking levels.
Index ¶
- func DefaultModelPerProvider() map[string]string
- func FacingMessage(message *database.MessageEntity) database.MessageEntity
- func FacingMessages(messages []database.MessageEntity) []database.MessageEntity
- func IsFacingMessage(message *database.MessageEntity) bool
- func IsFacingRole(role database.Role) bool
- func ModelsAreEqual(left, right *Model) bool
- func ProviderDisplayNames() map[string]string
- type CachedDiscoveryOptions
- type ConfigReader
- type Cost
- type DiscoveryOptions
- type InputMode
- type Model
- type Registry
- func (registry *Registry) All() []Model
- func (registry *Registry) Available() []Model
- func (registry *Registry) ConfigError() error
- func (registry *Registry) DiscoveryError() error
- func (registry *Registry) DiscoveryOptions() DiscoveryOptions
- func (registry *Registry) Error() error
- func (registry *Registry) HasAuth(provider string) bool
- func (registry *Registry) Refresh()
- func (registry *Registry) RefreshContext(ctx context.Context)
- func (registry *Registry) RequestAuth(provider string) RequestAuth
- func (registry *Registry) RequestAuthContext(ctx context.Context, provider string) RequestAuth
- type RegistryOptions
- type RequestAuth
- type ThinkingLevel
- type TokenContributor
- type TokenUsage
- type UsageAggregate
- type UsageProvenance
- type UsageTotals
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultModelPerProvider ¶
DefaultModelPerProvider maps supported provider IDs to librecode's default model IDs.
func FacingMessage ¶
func FacingMessage(message *database.MessageEntity) database.MessageEntity
FacingMessage converts persisted summary roles into model-facing user messages.
func FacingMessages ¶
func FacingMessages(messages []database.MessageEntity) []database.MessageEntity
FacingMessages filters and converts persisted messages for model replay.
func IsFacingMessage ¶
func IsFacingMessage(message *database.MessageEntity) bool
IsFacingMessage reports whether a persisted message has model-facing content.
func IsFacingRole ¶
IsFacingRole reports whether a persisted message role is replayed to models.
func ModelsAreEqual ¶
ModelsAreEqual compares provider and model ID.
func ProviderDisplayNames ¶
ProviderDisplayNames maps built-in provider IDs to user-facing names.
Types ¶
type CachedDiscoveryOptions ¶
type CachedDiscoveryOptions struct {
Client *http.Client
CachePath string
SourceURL string
CacheTTL time.Duration
FetchTimeout time.Duration
Enabled bool
}
CachedDiscoveryOptions configures cached model discovery.
type ConfigReader ¶
ConfigReader reads model registry configuration from database-backed runtime documents.
type Cost ¶
type Cost struct {
Input float64 `json:"input"`
Output float64 `json:"output"`
CacheRead float64 `json:"cache_read"`
CacheWrite float64 `json:"cache_write"`
}
Cost describes per-token pricing metadata.
type DiscoveryOptions ¶
type DiscoveryOptions struct {
Client *http.Client
CachePath string
SourceURL string
CacheTTL time.Duration
FetchTimeout time.Duration
Enabled bool
}
DiscoveryOptions configures remote model catalog discovery.
type Model ¶
type Model struct {
ThinkingLevelMap map[ThinkingLevel]*string `json:"thinking_level_map,omitempty"`
Headers map[string]string `json:"headers,omitempty"`
Compat map[string]any `json:"compat,omitempty"`
Provider string `json:"provider"`
ID string `json:"id"`
Name string `json:"name"`
API string `json:"api,omitempty"`
BaseURL string `json:"base_url,omitempty"`
Input []InputMode `json:"input,omitempty"`
Cost Cost `json:"cost"`
ContextWindow int `json:"context_window,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Reasoning bool `json:"reasoning"`
}
Model describes a provider model.
func BuiltInModels ¶
func BuiltInModels() []Model
BuiltInModels returns a deterministic built-in model catalog.
func DiscoverModels ¶
func DiscoverModels(ctx context.Context, options DiscoveryOptions) ([]Model, error)
DiscoverModels fetches model metadata from a models.dev-compatible API.
func DiscoverModelsCached ¶
func DiscoverModelsCached(ctx context.Context, options CachedDiscoveryOptions) ([]Model, error)
DiscoverModelsCached fetches model metadata with a stale-if-fetch-fails disk cache.
func ParseDiscoveredModels ¶
ParseDiscoveredModels parses a models.dev-compatible provider catalog into librecode models.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry loads built-in and custom models and resolves provider request auth.
func NewRegistry ¶
func NewRegistry(options *RegistryOptions) *Registry
NewRegistry creates and refreshes a registry.
func NewRegistryContext ¶
func NewRegistryContext(ctx context.Context, options *RegistryOptions) *Registry
NewRegistryContext creates and refreshes a registry using ctx for discovery.
func (*Registry) ConfigError ¶
ConfigError returns the latest custom model configuration error.
func (*Registry) DiscoveryError ¶
DiscoveryError returns the latest model discovery error.
func (*Registry) DiscoveryOptions ¶
func (registry *Registry) DiscoveryOptions() DiscoveryOptions
DiscoveryOptions returns the registry's configured discovery settings.
func (*Registry) Refresh ¶
func (registry *Registry) Refresh()
Refresh reloads models from disk and registered built-ins.
func (*Registry) RefreshContext ¶
RefreshContext reloads models from custom config, discovery, and registered built-ins.
func (*Registry) RequestAuth ¶
func (registry *Registry) RequestAuth(provider string) RequestAuth
RequestAuth returns auth and headers for a model request.
func (*Registry) RequestAuthContext ¶
func (registry *Registry) RequestAuthContext(ctx context.Context, provider string) RequestAuth
RequestAuthContext returns auth and headers, refreshing OAuth credentials when needed.
type RegistryOptions ¶
type RegistryOptions struct {
ConfigReader ConfigReader `json:"-"`
Auth *auth.Storage `json:"-"`
ModelsPath string `json:"models_path"`
BuiltIns []Model `json:"built_ins"`
Discovery DiscoveryOptions `json:"discovery"`
}
RegistryOptions configures a model registry.
type RequestAuth ¶
type RequestAuth struct {
Headers map[string]string `json:"headers,omitempty"`
APIKey string `json:"api_key,omitempty"`
Error string `json:"error,omitempty"`
OK bool `json:"ok"`
}
RequestAuth contains resolved per-provider request auth and headers.
type ThinkingLevel ¶
type ThinkingLevel string
ThinkingLevel controls model reasoning depth.
const ( // ThinkingOff disables reasoning where supported. ThinkingOff ThinkingLevel = "off" // ThinkingMinimal selects minimal reasoning. ThinkingMinimal ThinkingLevel = "minimal" // ThinkingLow selects low reasoning. ThinkingLow ThinkingLevel = "low" // ThinkingMedium selects medium reasoning. ThinkingMedium ThinkingLevel = "medium" // ThinkingHigh selects high reasoning. ThinkingHigh ThinkingLevel = "high" // ThinkingXHigh selects extra-high reasoning. ThinkingXHigh ThinkingLevel = "xhigh" // ThinkingMax selects maximum reasoning. ThinkingMax ThinkingLevel = "max" )
type TokenContributor ¶
type TokenContributor struct {
Label string `json:"label"`
Role string `json:"role,omitempty"`
Preview string `json:"preview,omitempty"`
Tokens int `json:"tokens"`
Chars int `json:"chars"`
}
TokenContributor describes a large piece of model-facing context.
func CloneTokenContributors ¶
func CloneTokenContributors(contributors []TokenContributor) []TokenContributor
CloneTokenContributors copies token contributor slices, preserving nil/empty-as-nil semantics.
type TokenUsage ¶
type TokenUsage struct {
Breakdown map[string]int `json:"breakdown,omitempty"`
Provenance UsageProvenance `json:"provenance,omitempty"`
TopContributors []TokenContributor `json:"top_contributors,omitempty"`
ContextWindow int `json:"context_window,omitempty"`
ContextTokens int `json:"context_tokens,omitempty"`
InputTokens int `json:"input_tokens,omitempty"`
OutputTokens int `json:"output_tokens,omitempty"`
}
TokenUsage tracks model context and request/response token counts. InputTokens and OutputTokens are cumulative across provider rounds in one completion. ContextTokens is the input size of the latest provider request.
func EmptyTokenUsage ¶
func EmptyTokenUsage() TokenUsage
EmptyTokenUsage returns a zero-value token usage with explicit fields.
func (*TokenUsage) ContextPercent ¶
func (usage *TokenUsage) ContextPercent() int
ContextPercent returns the context-window usage percentage, if known.
func (*TokenUsage) HasAny ¶
func (usage *TokenUsage) HasAny() bool
HasAny reports whether any usage field is populated.
func (*TokenUsage) Reported ¶
func (usage *TokenUsage) Reported() bool
Reported reports whether the provider supplied a usage object.
func (*TokenUsage) TotalTokens ¶
func (usage *TokenUsage) TotalTokens() int
TotalTokens returns input plus output tokens reported for the turn.
func (*TokenUsage) WithReported ¶
func (usage *TokenUsage) WithReported() TokenUsage
WithReported marks usage as explicitly reported by a provider, including a zero-token report.
type UsageAggregate ¶
type UsageAggregate struct {
Usage UsageTotals
Known int
Total int
}
UsageAggregate describes checked aggregation across a set of runs.
func AggregateUsage ¶
func AggregateUsage(usages []UsageTotals) (UsageAggregate, error)
AggregateUsage sums each supplied run once and tracks partial knowledge.
type UsageProvenance ¶
type UsageProvenance string
UsageProvenance identifies how context usage was calculated.
const ( // UsageProviderReported identifies usage reported directly by a provider. UsageProviderReported UsageProvenance = "provider_reported" // UsageProviderAnchorEstimate identifies usage based on a provider anchor plus local estimates. UsageProviderAnchorEstimate UsageProvenance = "provider_anchor_plus_estimate" // UsageLocalEstimate identifies usage estimated entirely locally. UsageLocalEstimate UsageProvenance = "local_estimate" )
type UsageTotals ¶
type UsageTotals struct {
InputTokens int64 `json:"input_tokens"`
OutputTokens int64 `json:"output_tokens"`
ProviderRoundTrips int64 `json:"provider_round_trips"`
Reported bool `json:"-"`
}
UsageTotals is cumulative provider-reported usage for one run. Reported distinguishes an unknown snapshot from a valid report containing zero tokens.
func UsageTotalsFromTokenUsage ¶
func UsageTotalsFromTokenUsage(usage *TokenUsage) (UsageTotals, error)
UsageTotalsFromTokenUsage converts one provider usage observation into cumulative usage. Reported preserves whether the source usage was explicitly reported by a provider, including a zero-token report.
func (UsageTotals) Add ¶
func (usage UsageTotals) Add(other UsageTotals) (UsageTotals, error)
Add returns the checked cumulative usage of two snapshots.
func (UsageTotals) MarshalJSON ¶
func (usage UsageTotals) MarshalJSON() ([]byte, error)
MarshalJSON preserves the persisted usage field names while retaining the distinction between unknown usage and a reported zero.
func (UsageTotals) TotalTokens ¶
func (usage UsageTotals) TotalTokens() (int64, error)
TotalTokens returns the checked sum of provider input and output tokens.
func (*UsageTotals) UnmarshalJSON ¶
func (usage *UsageTotals) UnmarshalJSON(data []byte) error
UnmarshalJSON accepts legacy snapshots and ignores unknown fields. Positive legacy fields imply reported usage; an explicit reported field is authoritative.
func (UsageTotals) Validate ¶
func (usage UsageTotals) Validate() error
Validate rejects values which cannot be valid provider usage.