endpoint

package
v0.11.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package endpoint owns the set of model endpoints nib can talk to — the config.yaml default, the named endpoints config.yaml declares, and the built-in provider registry — and the rule that decides which one a new session starts on. It holds no session state and makes no request, so the precedence rule is testable on its own.

Index

Constants

View Source
const DefaultID = "config"

DefaultID is the ID of config.yaml's top-level block.

View Source
const DefaultName = "config.yaml"

DefaultName is how the default endpoint is shown.

View Source
const NamedPrefix = "@"

NamedPrefix marks a config.yaml named endpoint, so no yaml name can ever shadow (or be shadowed by) a registry provider ID.

Variables

This section is empty.

Functions

func ClearSaved added in v0.11.1

func ClearSaved(path string) error

ClearSaved removes the saved pick. A missing file (or an empty path) is not an error: there is nothing left to forget.

func Fingerprint added in v0.11.1

func Fingerprint(cfg types.Config, sv Saved) string

Fingerprint identifies the parts of cfg that decide where a session starts, as seen by the saved pick sv. Saved records it at every write, and Reconcile compares it with the config at the next start: when they differ, the config changed after the pick, and the last change wins.

It covers the default block's provider, base_url, model and api_key_env, and, when sv picks a named endpoint, that entry's same fields. The entry's model counts only when sv saved a model of its own: a bare pick follows the entry's model live, so editing it does not conflict with the pick. Other named endpoints are left out, so editing them keeps the pick. API key values never go in, not even hashed: api_key_env is the variable's name.

func WriteSaved

func WriteSaved(path string, sv Saved) error

WriteSaved records the pick atomically, creating the directory if needed. An empty path is a no-op, mirroring LoadSaved's treatment of "": a Session built without a state directory (as some low-level tests do) has nowhere to persist to, and must not go writing a stray file into the process's working directory instead.

Types

type Entry

type Entry struct {
	ID   string // DefaultID, "@name", or a registry provider ID
	Name string // display name
	Kind Kind
	// Model is what this entry would run, empty when it must be picked.
	Model string
	// Status is a short human description: an address, or a login state.
	Status string
	// Ready means requests can authenticate right now.
	Ready bool
	// Stored means a /login credential exists (so /logout can remove it).
	Stored bool
	// NeedsBaseURL means logging in must also collect an endpoint.
	NeedsBaseURL bool
	// EnvVar is the environment variable that can hold the key instead.
	EnvVar string
	// Def is the registry definition; the zero value for yaml entries.
	Def provider.Definition
}

Entry is one row of the endpoint picker.

type Kind

type Kind string

Kind classifies an Entry.

const (
	KindDefault  Kind = "default"
	KindNamed    Kind = "named"
	KindProvider Kind = "provider"
)

type Saved

type Saved struct {
	ID    string `json:"id"`
	Model string `json:"model"`

	// ConfigFingerprint is Fingerprint of the config when the pick was
	// written, so a later start can tell whether config.yaml changed since
	// (see Set.Reconcile). Empty in a file written by an older nib.
	ConfigFingerprint string `json:"config_fingerprint,omitempty"`

	// Provider is the pre-endpoints field name, read for backward
	// compatibility and never written. An install saved before named
	// endpoints carries a registry ID here.
	Provider string `json:"provider,omitempty"`
}

Saved is the endpoint a previous session picked, kept next to credentials.json so the next session starts on it. It is nib-managed state rather than a config.yaml edit: config.yaml keeps describing its own endpoints, and nib never writes to it.

func LoadSaved

func LoadSaved(path string) Saved

LoadSaved reads the saved pick. Any problem (absent, unreadable, corrupt) is the zero value: a session always has the config.yaml default to fall back to, and a startup must not fail over this file.

type Set

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

Set is the resolved collection of endpoints for one config.

func New

func New(cfg types.Config, creds *auth.Store) (*Set, []error)

New builds the set. The returned errors are the rejected yaml endpoints, for the caller to show; the set itself is always usable.

func (*Set) Config

func (s *Set) Config(id string) (types.ModelProviderConfig, error)

Config resolves an entry to the connection config a client is built from.

func (*Set) Fingerprint added in v0.11.1

func (s *Set) Fingerprint(sv Saved) string

Fingerprint is Fingerprint over the config this set was built from.

func (*Set) List

func (s *Set) List() []Entry

List returns the default endpoint, the named endpoints in file order, then the provider registry in its display order.

func (*Set) Lookup

func (s *Set) Lookup(id string) (Entry, bool)

Lookup finds one entry by ID.

func (*Set) Reconcile added in v0.11.1

func (s *Set) Reconcile(sv Saved) (Saved, bool, string)

Reconcile checks a saved pick against the config this set was built from, before Startup applies it. It returns the pick to start from, whether provider.json must be rewritten with it (a zero Saved means remove it), and a note for the boot log.

  • Fingerprint matches: the pick is the last change and sticks.
  • Fingerprint differs: config.yaml changed after the pick, so the config wins. The pick is dropped, so the note shows once.
  • No fingerprint (written by an older nib): the pick sticks, and the current fingerprint is backfilled so later edits are detected.

A pick with nothing to override (nothing saved, or a bare pick of the default endpoint) and a pick that no longer resolves are left as they are: the latter is Startup's to report, with a more precise note.

func (*Set) Startup

func (s *Set) Startup(sv Saved) (Entry, types.ModelProviderConfig, string)

Startup resolves the endpoint a new session starts on: the saved pick when it still resolves, otherwise the config.yaml default. The third return is a user-visible note, non-empty only when a saved pick had to be dropped — silently changing the model between sessions is what this replaces.

Jump to

Keyboard shortcuts

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