Documentation
¶
Overview ¶
Package catalog builds and validates Lightcode's provider/model catalog.
Index ¶
- Variables
- func BundledModelIDs() map[string]map[string]struct{}
- func BundledProviderHeaders(providerID string) map[string]string
- func CheckReservedKeys(body map[string]any) []string
- func DeepMergeCatalog(bundled, user map[string]any) map[string]any
- func DiscoveryAttemptRecent(home, providerID string, now time.Time) (bool, error)
- func DiscoveryRefreshCandidates(cat *Catalog, attempts map[string]time.Time, now time.Time) []string
- func ShallowMergeBody(layers ...map[string]any) map[string]any
- func WriteDiscoveryAttempt(home, providerID string, attemptedAt time.Time) error
- func WriteDiscoveryCache(home, providerID string, discovered DiscoveredProvider, fetchedAt time.Time) error
- type BuildInputs
- type BuildResult
- type Catalog
- func (c *Catalog) AllModels() []ModelRef
- func (c *Catalog) IncompleteModels() []Incomplete
- func (c *Catalog) Lookup(ref ModelRef) (*Provider, *Model, error)
- func (c *Catalog) LookupOrIncomplete(ref ModelRef) (*Provider, *Model, error)
- func (c *Catalog) MergeDiscoveredProvider(providerID string, discovered DiscoveredProvider) error
- func (c *Catalog) MergeDiscoveredProviderWithCostProtection(providerID string, discovered DiscoveredProvider, ...) error
- func (c *Catalog) VisibleModels() []ModelRef
- type Cost
- type DiscoveredModel
- type DiscoveredProvider
- type Incomplete
- type IncompleteReason
- type Loader
- type Modality
- type Model
- type ModelRef
- type ProtocolMetadata
- type Provider
- type RawDiscoveredModel
- type SystemRole
- type Transport
- type ValidationError
- type Warning
- func ReadDiscoveryCache(home string) (map[string]DiscoveredProvider, map[string]time.Time, []Warning)
- func RefreshProviderDiscovery(ctx context.Context, home string, cat *Catalog, providerID string) (bool, []Warning)
- func RefreshProviderDiscoveryWithConfigPath(ctx context.Context, home, configPath string, cat *Catalog, providerID string) (bool, []Warning)
Constants ¶
This section is empty.
Variables ¶
var ( ErrUnknownProvider = errors.New("catalog: unknown provider") ErrUnknownModel = errors.New("catalog: unknown model") ErrIncomplete = errors.New("catalog: incomplete model") )
Catalog lookup errors.
var ErrInvalidModelRef = coremodel.ErrInvalidModelRef
ErrInvalidModelRef is returned when a provider-prefixed model reference is malformed.
var ReservedKeys = []string{
"model",
"messages",
"tools",
"tool_choice",
"stream",
"stream_options",
"max_tokens",
"max_completion_tokens",
"n",
}
ReservedKeys lists request-body fields owned by the loop.
Functions ¶
func BundledModelIDs ¶ added in v0.0.2
BundledModelIDs returns, for each bundled built-in provider, the set of model IDs declared in its embedded catalog file. It lets callers classify a model's provenance (bundled vs discovered vs user-added) without re-reading the merged catalog, which has already collapsed the layers together.
func BundledProviderHeaders ¶ added in v0.0.2
BundledProviderHeaders returns the transport headers a bundled provider ships with (e.g. OpenRouter's attribution HTTP-Referer / X-Title). These are part of the built-in's identity and must not be user-editable.
func CheckReservedKeys ¶
CheckReservedKeys returns reserved keys present in body, in ReservedKeys order.
func DeepMergeCatalog ¶
DeepMergeCatalog merges catalog layers. Objects recurse; arrays, primitives, and null replace whole.
func DiscoveryAttemptRecent ¶
DiscoveryAttemptRecent reports whether provider discovery had a real network attempt inside the cache TTL.
func DiscoveryRefreshCandidates ¶
func DiscoveryRefreshCandidates(cat *Catalog, attempts map[string]time.Time, now time.Time) []string
DiscoveryRefreshCandidates returns enabled discovery providers that have not had a real discovery attempt in the last 24 hours.
func ShallowMergeBody ¶
ShallowMergeBody merges request body sidecars top-level only. Later layers win.
func WriteDiscoveryAttempt ¶
WriteDiscoveryAttempt records a real discovery network attempt without changing any cached model metadata.
func WriteDiscoveryCache ¶
func WriteDiscoveryCache(home, providerID string, discovered DiscoveredProvider, fetchedAt time.Time) error
WriteDiscoveryCache writes one provider's discovery cache file.
Types ¶
type BuildInputs ¶
type BuildInputs struct {
Bundled map[string]json.RawMessage
UserRaw map[string]any
Cache map[string]DiscoveredProvider
}
BuildInputs contains the raw catalog layers consumed by Build.
type BuildResult ¶
BuildResult contains the effective catalog and non-fatal warnings.
func Build ¶
func Build(inputs BuildInputs) BuildResult
Build constructs an effective catalog from bundled, discovery, and user layers.
type Catalog ¶
Catalog is the in-memory effective catalog.
func (*Catalog) IncompleteModels ¶
func (c *Catalog) IncompleteModels() []Incomplete
IncompleteModels returns all incomplete models in the catalog.
func (*Catalog) Lookup ¶
Lookup returns the provider and model for ref, rejecting incomplete models.
func (*Catalog) LookupOrIncomplete ¶
LookupOrIncomplete returns the provider and model for ref, allowing incomplete models.
func (*Catalog) MergeDiscoveredProvider ¶
func (c *Catalog) MergeDiscoveredProvider(providerID string, discovered DiscoveredProvider) error
MergeDiscoveredProvider gap-fills provider models from discovery data.
func (*Catalog) MergeDiscoveredProviderWithCostProtection ¶
func (c *Catalog) MergeDiscoveredProviderWithCostProtection(providerID string, discovered DiscoveredProvider, protected map[string]map[string]bool) error
MergeDiscoveredProviderWithCostProtection gap-fills provider models from discovery data while preserving user-declared cost subfields.
func (*Catalog) VisibleModels ¶
VisibleModels returns non-hidden provider/model refs sorted by prefix string.
type Cost ¶
type Cost struct {
Input *float64 `json:"input,omitempty"`
Output *float64 `json:"output,omitempty"`
CacheRead *float64 `json:"cache_read,omitempty"`
CacheWrite *float64 `json:"cache_write,omitempty"`
}
Cost is per-million-token USD pricing.
type DiscoveredModel ¶
type DiscoveredModel struct {
Name string
ContextWindow int
MaxOutputTokens int
Cost *Cost
// contains filtered or unexported fields
}
DiscoveredModel is model metadata loaded from discovery cache.
type DiscoveredProvider ¶
type DiscoveredProvider struct {
Models map[string]DiscoveredModel
}
DiscoveredProvider is provider metadata loaded from discovery cache.
func FetchDiscovery ¶
func FetchDiscovery(ctx context.Context, client *http.Client, provider *Provider) (DiscoveredProvider, error)
FetchDiscovery fetches and parses a provider's OpenAI-compatible /models list.
type Incomplete ¶
type Incomplete struct {
Ref ModelRef
Reasons []IncompleteReason
}
Incomplete describes a model entry missing required capacity metadata.
type IncompleteReason ¶
type IncompleteReason int
IncompleteReason explains why a model cannot be used yet.
const ( // ReasonMissingContextWindow means context_window is unset. ReasonMissingContextWindow IncompleteReason = iota )
type Loader ¶
type Loader struct {
AllowRefresh func(providerID string, provider *Provider) bool
// contains filtered or unexported fields
}
Loader reads catalog inputs from disk and delegates assembly to Build.
func NewLoaderWithConfigPath ¶ added in v0.0.2
NewLoaderWithConfigPath constructs a catalog loader that reads user provider overlays from the same config file the agent loaded at startup.
type Model ¶
type Model struct {
ID string `json:"id"`
Name string `json:"name"`
ContextWindow int `json:"context_window"`
MaxOutputTokens int `json:"max_output_tokens"`
InputModalities []Modality `json:"input_modalities"`
SystemRole SystemRole `json:"system_role"`
UsageInStream bool `json:"usage_in_stream"`
Hidden bool `json:"hidden,omitempty"`
ExtraBody map[string]any `json:"extra_body,omitempty"`
Cost *Cost `json:"cost,omitempty"`
ProtocolMetadata *ProtocolMetadata `json:"protocol_metadata,omitempty"`
}
Model is a resolved model entry in the effective catalog.
func (*Model) Incomplete ¶
func (m *Model) Incomplete() (Incomplete, bool)
Incomplete returns capacity fields missing from the model. Only context_window is required; max_output_tokens is optional and only acts as a request cap when set, so missing it does not block use.
type ModelRef ¶
ModelRef is the canonical internal identity of a model.
func ParseModelRef ¶
ParseModelRef parses a provider-prefixed model reference.
type ProtocolMetadata ¶
type ProtocolMetadata struct {
Family string `json:"family,omitempty"`
MustPreserve []string `json:"must_preserve,omitempty"`
Drop []string `json:"drop,omitempty"`
}
ProtocolMetadata describes provider-specific protocol-state policy.
type Provider ¶
type Provider struct {
ID string `json:"id"`
Name string `json:"name"`
Transport Transport `json:"transport"`
SystemRole SystemRole `json:"system_role"`
UsageInStream bool `json:"usage_in_stream"`
MaxTokensField string `json:"max_tokens_field"`
ExtraBody map[string]any `json:"extra_body,omitempty"`
Discovery bool `json:"discovery"`
Hidden bool `json:"hidden,omitempty"`
ProtocolMetadata *ProtocolMetadata `json:"protocol_metadata,omitempty"`
Models map[string]*Model `json:"models"`
Builtin bool `json:"-"`
}
Provider is a resolved provider entry in the effective catalog.
type RawDiscoveredModel ¶
RawDiscoveredModel is one provider-supplied model object from /v1/models.
type SystemRole ¶
type SystemRole string
SystemRole is the request role used for the system prompt.
const ( // RoleSystem uses the OpenAI system role. RoleSystem SystemRole = "system" // RoleUser uses the user role for system-prompt content. RoleUser SystemRole = "user" // RoleDeveloper uses the OpenAI developer role. RoleDeveloper SystemRole = "developer" )
type Transport ¶
type Transport struct {
BaseURL string `json:"base_url"`
APIKeyEnv string `json:"api_key_env"`
Headers map[string]string `json:"headers,omitempty"`
Options map[string]any `json:"options,omitempty"`
}
Transport holds provider HTTP-construction config.
type ValidationError ¶
ValidationError describes a catalog validation failure.
func ValidateEffective ¶
func ValidateEffective(p *Provider) []ValidationError
ValidateEffective checks a resolved provider and its models.
func ValidateRaw ¶
func ValidateRaw(provID string, raw map[string]any, strict bool) []ValidationError
ValidateRaw checks a raw provider envelope.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
Error returns a human-readable validation error.
type Warning ¶
Warning describes a recoverable catalog build problem.
func ReadDiscoveryCache ¶
func ReadDiscoveryCache(home string) (map[string]DiscoveredProvider, map[string]time.Time, []Warning)
ReadDiscoveryCache reads all on-disk discovery cache files and last-attempt timestamps. Old cache files without attempted_at use fetched_at as the attempt time for backwards compatibility.
func RefreshProviderDiscovery ¶
func RefreshProviderDiscovery(ctx context.Context, home string, cat *Catalog, providerID string) (bool, []Warning)
RefreshProviderDiscovery fetches one provider's live discovery data, merges it into the in-memory catalog, and writes the discovery cache. Discovery failures are returned as warnings so callers can continue startup.
func RefreshProviderDiscoveryWithConfigPath ¶ added in v0.0.2
func RefreshProviderDiscoveryWithConfigPath(ctx context.Context, home, configPath string, cat *Catalog, providerID string) (bool, []Warning)
RefreshProviderDiscoveryWithConfigPath is RefreshProviderDiscovery with an explicit config path for preserving user-owned cost fields during merges.