catalogsync

package
v0.16.21 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2026 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package catalogsync owns the model-catalog data source registry: a DB-persisted, ordered list of catalog sources (llm-metadata / models.dev presets plus operator-added custom URLs) that are fetched in order and merged first-wins into the runtime catalog snapshot consumed by routing and the models marketplace.

Index

Constants

View Source
const (
	// SettingsAutoSyncKey is the settings-table key for the automatic
	// catalog sync toggle ("true"/"false"; missing = enabled).
	SettingsAutoSyncKey = "catalog_auto_sync_enabled"

	// SourceTypeOfficial marks a preset official dataset source.
	SourceTypeOfficial = "official"
	// SourceTypeCustom marks an operator-added source.
	SourceTypeCustom = "custom"
)

Variables

View Source
var ErrSyncBusy = errors.New("catalogsync: sync already running")

ErrSyncBusy reports an overlapping sync run.

Functions

func DefaultSourcePresets

func DefaultSourcePresets() []pricingcatalog.SourceSpec

DefaultSourcePresets is the seed registry: llm-metadata (primary, native-provider filtered, daily rebuilt) followed by models.dev (fallback), then the llm-metadata newapi ratio set (same origin as llm-metadata; supplies the NewAPI ratio multipliers used for relay-site cost estimates and tiered billing metadata). A legacy PRICING_CATALOG_URL value that differs from both presets is inserted in front at seed time (see EnsureDefaults).

Types

type Manager

type Manager struct {
	*Store
	// contains filtered or unexported fields
}

Manager owns the runtime catalog: the DB registry (Store) plus the pricingcatalog Provider that fetches/merges the sources and publishes the snapshot consumed by routing (cold-start cost) and the marketplace. Implements routing.CatalogPricingResolver structurally (unitCost, source).

func NewManager

func NewManager(db *sqlx.DB, opts Options) (*Manager, error)

NewManager builds a Manager, seeds the default registry when empty, and wires the provider to the persisted sources. Call Start to begin syncing.

func (*Manager) AutoSyncEnabled

func (m *Manager) AutoSyncEnabled() bool

AutoSyncEnabled reports the current auto-sync toggle.

func (*Manager) CreateSource

func (m *Manager) CreateSource(ctx context.Context, in SourceInput) (Source, error)

CreateSource persists a new source and re-wires the provider.

func (*Manager) DeleteSource

func (m *Manager) DeleteSource(ctx context.Context, id int64) error

DeleteSource removes a source and re-wires the provider.

func (*Manager) Interval

func (m *Manager) Interval() time.Duration

Interval returns the configured auto-sync period (0 = disabled).

func (*Manager) ReloadSources

func (m *Manager) ReloadSources(ctx context.Context) error

ReloadSources re-reads the registry into the provider after CRUD changes.

func (*Manager) ResolveCatalogPricing

func (m *Manager) ResolveCatalogPricing(siteID, accountID int64, modelName string) (unitCost *float64, source string)

ResolveCatalogPricing implements routing.CatalogPricingResolver structurally: it delegates to the underlying provider (site-classified official vs relay provenance).

func (*Manager) SetAutoSyncEnabled

func (m *Manager) SetAutoSyncEnabled(ctx context.Context, enabled bool) error

SetAutoSyncEnabled flips the toggle and persists it to settings.

func (*Manager) Snapshot

func (m *Manager) Snapshot() *pricingcatalog.CatalogSnapshot

Snapshot returns the published merged catalog snapshot (never nil).

func (*Manager) Start

func (m *Manager) Start()

Start begins an immediate background sync (when auto sync is enabled) plus the periodic loop. Idempotent.

func (*Manager) Status

func (m *Manager) Status(ctx context.Context) *SyncStatus

func (*Manager) Stop

func (m *Manager) Stop()

Stop halts the periodic loop. Safe to call multiple times.

func (*Manager) SyncNow

func (m *Manager) SyncNow(ctx context.Context, sourceID int64) (*SyncStatus, error)

SyncNow runs a sync pass: all enabled sources (sourceID <= 0) or one source. Concurrent runs are declined with ErrSyncBusy. Returns the full status payload for the panel.

func (*Manager) UpdateSource

func (m *Manager) UpdateSource(ctx context.Context, id int64, in SourceInput) (Source, error)

UpdateSource patches a source and re-wires the provider.

type Options

type Options struct {
	// Interval is the auto-sync cadence. <= 0 disables the periodic loop
	// (the initial background fetch still runs when auto sync is enabled).
	Interval time.Duration
	// LegacyURL is the PRICING_CATALOG_URL env value; when it differs from
	// both presets it is seeded as the top-priority custom source.
	LegacyURL string
	// SiteSnapshotSource resolves site platform/URL for vendor
	// classification in ResolveCatalogPricing.
	SiteSnapshotSource pricingcatalog.SiteSnapshotSource
	// Logger defaults to slog.Default().
	Logger *slog.Logger
}

Options configures a Manager.

type SnapshotStatus

type SnapshotStatus struct {
	Source    string     `json:"source"`
	FetchedAt *time.Time `json:"fetchedAt"`
	Models    int        `json:"models"`
}

SnapshotStatus describes the published merged snapshot.

type Source

type Source struct {
	ID            int64      `json:"id"`
	Name          string     `json:"name"`
	URL           string     `json:"url"`
	Enabled       bool       `json:"enabled"`
	Type          string     `json:"type"`
	SortOrder     int        `json:"sortOrder"`
	LastSuccessAt *time.Time `json:"lastSuccessAt"`
	LastError     *string    `json:"lastError"`
	LastCount     int        `json:"lastCount"`
	LastAttemptAt *time.Time `json:"lastAttemptAt"`
}

Source is one persisted registry row (wire/domain shape).

type SourceInput

type SourceInput struct {
	Name    string `json:"name"`
	URL     string `json:"url"`
	Enabled *bool  `json:"enabled"`
	Type    string `json:"type"`
	// SortOrder, when >= 0, repositions the source (update only).
	SortOrder *int `json:"sortOrder"`
}

SourceInput is the create/update payload for a source row.

type Store

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

Store is the DB registry for catalog sources. Manager embeds it so CRUD goes through one owner that also re-syncs the runtime provider.

func NewStore

func NewStore(db *sqlx.DB) *Store

NewStore builds a registry store on the runtime DB.

func (*Store) AutoSyncEnabled

func (s *Store) AutoSyncEnabled(ctx context.Context) (bool, error)

AutoSyncEnabled reads the settings toggle (missing key = enabled).

func (*Store) CreateSource

func (s *Store) CreateSource(ctx context.Context, in SourceInput) (Source, error)

CreateSource inserts a new source at the end of the fetch order.

func (*Store) DeleteSource

func (s *Store) DeleteSource(ctx context.Context, id int64) error

DeleteSource removes a registry row.

func (*Store) EnsureDefaults

func (s *Store) EnsureDefaults(ctx context.Context, legacyURL string) error

EnsureDefaults seeds the registry with the preset sources when the table is empty. legacyURL (the PRICING_CATALOG_URL env value) is inserted as the top-priority custom source when it differs from both presets — this is the migration of the old single-URL configuration into the registry.

func (*Store) ListSources

func (s *Store) ListSources(ctx context.Context) ([]Source, error)

ListSources returns all registry rows in fetch order (sort_order, id).

func (*Store) RecordStatus

func (s *Store) RecordStatus(ctx context.Context, report pricingcatalog.SourceReport) error

RecordStatus persists one source's sync outcome (success time / error / entry count). Successful outcomes clear the error column.

func (*Store) SetAutoSyncEnabled

func (s *Store) SetAutoSyncEnabled(ctx context.Context, enabled bool) error

SetAutoSyncEnabled writes the settings toggle (upsert).

func (*Store) UpdateSource

func (s *Store) UpdateSource(ctx context.Context, id int64, in SourceInput) (Source, error)

UpdateSource patches name/url/enabled/type/sortOrder of a source. Passing sortOrder repositions the row (other rows are shifted to keep the order contiguous).

type SyncStatus

type SyncStatus struct {
	AutoSync    bool           `json:"autoSync"`
	IntervalMin int            `json:"intervalMin"`
	Snapshot    SnapshotStatus `json:"snapshot"`
	Sources     []Source       `json:"sources"`
}

SyncStatus is the status payload served to the panel.

Jump to

Keyboard shortcuts

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