zot/packages/provider/cache.go
patriceckhart fa7d8d8be5 refactor: split source into packages/{provider,core,tui,agent}
Single Go module, four top-level packages under packages/. Import
paths become github.com/patriceckhart/zot/packages/<name>; downstream
consumers can depend on individual packages without pulling the rest.

Layout:
  packages/provider/     LLM clients + catalog
  packages/provider/auth/ credential store + OAuth + login server
  packages/core/         agent loop, sessions, cost
  packages/tui/          terminal toolkit + chat view
  packages/agent/        CLI wiring, system prompt
    extensions/ extproto/ modes/ tools/ skills/ swarm/
    sdk/  (was pkg/zotcore, package renamed zotcore -> sdk)
    ext/  (was pkg/zotext, package renamed zotext -> ext)

internal/ and pkg/ removed. The internal/assets logo moved into
packages/provider/auth/assets.

Public Go SDK identifiers renamed:
  pkg/zotcore (package zotcore) -> packages/agent/sdk (package sdk)
  pkg/zotext  (package zotext)  -> packages/agent/ext (package ext)

This breaks Go-based extensions and embedders; the JSON wire protocol
for extensions and RPC is unchanged, so non-Go extensions, already-
built extension binaries, and zot rpc consumers are unaffected.

Docs, examples, and the built-in write-zot-extension skill updated
for the new paths and identifiers. Shadow-bug fixes in code samples
(ext := ext.New -> e := ext.New).
2026-05-27 09:07:15 +02:00

64 lines
1.4 KiB
Go

package provider
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"time"
)
// ModelCache is the on-disk shape for discovered models.
type ModelCache struct {
FetchedAt time.Time `json:"fetched_at"`
Models []Model `json:"models"`
}
// CacheTTL is how long a discovered list is considered fresh.
const CacheTTL = 6 * time.Hour
// LoadCache reads the model cache from path. Returns an empty ModelCache
// (no error) if the file does not exist.
func LoadCache(path string) (ModelCache, error) {
var c ModelCache
b, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return c, nil
}
if err != nil {
return c, err
}
if err := json.Unmarshal(b, &c); err != nil {
return c, err
}
for i := range c.Models {
if c.Models[i].Source == "" {
c.Models[i].Source = "cache"
}
}
return c, nil
}
// SaveCache writes the cache atomically.
func SaveCache(path string, c ModelCache) error {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
tmp := path + ".tmp"
if err := os.WriteFile(tmp, b, 0o644); err != nil {
return err
}
return os.Rename(tmp, path)
}
// IsFresh reports whether the cache was fetched within CacheTTL.
func (c ModelCache) IsFresh() bool {
if c.FetchedAt.IsZero() {
return false
}
return time.Since(c.FetchedAt) < CacheTTL
}