source

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 21 Imported by: 0 modules, 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 source defines optional, read-only provider data sources: cc-switch's local database and the harness config files that say which provider a tool is currently pointing at. Sources feed attribution (which gateway or account a request used). A source that is missing or unreadable is skipped, never fatal: attribution must work with no data source at all.

Index

Constants

View Source
const LocalProxyOrigin = "http://127.0.0.1:15721"

LocalProxyOrigin is the address cc-switch itself listens on. It is a local proxy, not a gateway, so it is never reconciled against.

Variables

This section is empty.

Functions

func CCSwitchPath

func CCSwitchPath() string

CCSwitchPath returns the default cc-switch database location.

func IsLocalProxy

func IsLocalProxy(base string) bool

Origin normalizes a base URL to scheme://host[:port], lowercased and without a path. It reports false for anything unusable (no scheme, no host, or a scheme that is not http/https). IsLocalProxy reports whether base points at cc-switch's local proxy, which forwards to whichever provider is selected in cc-switch: it says nothing about the gateway a request reached.

func KeyID

func KeyID(key string) string

KeyID is the stable, non-reversible identifier persisted instead of a key: hex(sha256("mytoken-key:" + key))[:12].

func Origin

func Origin(rawURL string) (string, bool)

func SecretReveals

func SecretReveals() int64

SecretReveals reports how many times a key was revealed in this process. It exists for the privacy tests and nothing else.

Types

type CCSwitch

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

CCSwitch reads the cc-switch SQLite database, opened read-only. It supplies the resolver with three things: request-log matching (which provider served an event), cc-switch's current selection per harness (timeline anchor), the provider inventory. Credentials found inside it are never returned.

func NewCCSwitch

func NewCCSwitch(path string) *CCSwitch

NewCCSwitch returns a source for the cc-switch database at path.

func (*CCSwitch) Close

func (c *CCSwitch) Close() error

func (*CCSwitch) Credentials

func (c *CCSwitch) Credentials(ctx context.Context) ([]Credential, error)

Credentials enumerates the gateway credentials cc-switch holds, read fresh on every call: a provider selected, enabled or re-keyed in cc-switch must be visible to the next reconciliation without a restart. The key is only kept inside the returned Secret and never written to disk by this package.

func (*CCSwitch) CurrentConfigs

func (c *CCSwitch) CurrentConfigs() []CurrentConfig

CurrentConfigs returns cc-switch's current selection per harness.

func (*CCSwitch) Load

func (c *CCSwitch) Load(ctx context.Context) error

Load re-reads the database when it changed on disk. A missing database is not an error: cc-switch is optional.

func (*CCSwitch) Match

func (c *CCSwitch) Match(e model.UsageEvent) (string, bool)

Match attributes an event to a provider from cc-switch's request log, accepting the closest entry within 120 seconds of the event.

func (*CCSwitch) Name

func (c *CCSwitch) Name() string

func (*CCSwitch) Providers

func (c *CCSwitch) Providers() []ProviderInfo

Providers lists the providers known to cc-switch. It never returns keys: HasKey only records that a credential is configured, and Origin/KeyID are the gateway's normalized address and a one-way id for the key.

func (*CCSwitch) Status

func (c *CCSwitch) Status() Status

Status returns the state of the last Load: whether the database exists, when it was read, how many providers it holds and any error.

type Configs

type Configs interface {
	CurrentConfigs() []CurrentConfig
}

Configs is implemented by sources that know what is selected right now, for backfilling the provider_configs timeline.

type Credential

type Credential struct {
	Origin   string        // https://host[:port], lowercased, no path
	KeyID    string        // KeyID(secret)
	Provider string        // provider name as attribution sees it ("XLAB")
	Harness  model.Harness // "" when the source is app-wide
	Source   string        // "cc-switch", "harness-config"
	Secret   Secret
}

Credential is one key that a harness or cc-switch already sends to a site. Secret is the only field holding the key and it redacts itself everywhere.

type Credentials

type Credentials interface {
	Credentials(ctx context.Context) ([]Credential, error)
}

Credentials is an optional Source capability. Implementations read keys on each call and must not cache them beyond the call.

type CurrentConfig

type CurrentConfig struct {
	Harness  model.Harness
	Base     string
	Provider string
	// SeedAtZero marks a selection that predates MyToken's own timeline: it is
	// written at time 0 and only when the harness has no entries yet, so
	// historical events still attribute to it (cc-switch's is_current).
	SeedAtZero bool
}

CurrentConfig is one source's view of the provider a harness is pointing at right now. The resolver maps Base to a provider name and appends it to the provider_configs timeline when it changed.

type HarnessConfig

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

HarnessConfig is the set of local config files that say which provider each harness is pointing at right now: ~/.claude/settings.json (ANTHROPIC_BASE_URL) and ~/.codex/config.toml (model_provider / base_url). Only the endpoint is read; credentials in the same files are ignored.

func NewHarnessConfig

func NewHarnessConfig(home string) *HarnessConfig

NewHarnessConfig returns the config files under home, which may not exist.

func (*HarnessConfig) Close

func (h *HarnessConfig) Close() error

Close implements Source; the config files need no handles.

func (*HarnessConfig) Credentials

func (h *HarnessConfig) Credentials(ctx context.Context) ([]Credential, error)

Credentials enumerates the gateway credentials the harness config files point at. The key is read out of the same file as the base URL, used to derive the key id, and handed over inside a Secret; it is never returned as text by any other method and never written to disk by this package.

func (*HarnessConfig) CurrentConfigs

func (h *HarnessConfig) CurrentConfigs() []CurrentConfig

CurrentConfigs returns what each harness is pointing at, if anything.

func (*HarnessConfig) Load

func (h *HarnessConfig) Load(ctx context.Context) error

Load re-reads both config files. Missing or unreadable files are skipped.

func (*HarnessConfig) Name

func (h *HarnessConfig) Name() string

func (*HarnessConfig) Status

func (h *HarnessConfig) Status() Status

Status returns the state of the last Load.

type Matcher

type Matcher interface {
	Match(e model.UsageEvent) (provider string, ok bool)
}

Matcher is implemented by sources that can attribute a single request (cc-switch's proxy request log).

type ProviderInfo

type ProviderInfo struct {
	Name    string        `json:"name"`
	Harness model.Harness `json:"harness,omitempty"`
	App     string        `json:"app,omitempty"` // source-native app id ("claude", "codex")
	BaseURL string        `json:"baseUrl,omitempty"`
	// Origin is BaseURL normalized to scheme://host[:port], and KeyID is a
	// stable, non-reversible id for the key configured for it. Together they
	// identify a gateway for 0.2 reconciliation without exposing the key.
	Origin  string `json:"origin,omitempty"`
	KeyID   string `json:"keyId,omitempty"`
	HasKey  bool   `json:"hasKey"`
	Current bool   `json:"current,omitempty"`
	Source  string `json:"source"` // Name() of the source that reported it
}

ProviderInfo describes one provider a source knows about. Keys are never returned or persisted: HasKey only reports whether one is configured, and callers that need the key read it from the source at that moment.

type Providers

type Providers interface {
	Providers() []ProviderInfo
}

Providers is implemented by sources that can list the providers they know about.

type Secret

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

Secret holds a credential in memory only. Every formatting path redacts it, so a Secret cannot leak into logs, error strings or JSON output by accident.

func NewSecret

func NewSecret(key string) Secret

NewSecret wraps a key that a source read from a harness or cc-switch config.

func (Secret) Format

func (s Secret) Format(f fmt.State, verb rune)

Format implements fmt.Formatter and always redacts.

func (Secret) GoString

func (s Secret) GoString() string

GoString implements fmt.GoStringer and always redacts.

func (Secret) MarshalJSON

func (s Secret) MarshalJSON() ([]byte, error)

MarshalJSON emits the redaction marker instead of the key.

func (Secret) Reveal

func (s Secret) Reveal() string

Reveal returns the key itself. Callers must only use it to build a request and must never store, log or serialize the result.

func (Secret) String

func (s Secret) String() string

String implements fmt.Stringer and always redacts.

type Source

type Source interface {
	Name() string
	Status() Status
	// Load refreshes the source. A missing or unreadable source records the
	// problem in Status and returns nil.
	Load(ctx context.Context) error
	// Close releases the source's handles.
	Close() error
}

Source is one optional provider data source.

type Status

type Status struct {
	Name      string    `json:"name"`
	Path      string    `json:"path,omitempty"`
	Exists    bool      `json:"exists"`
	ReadAt    time.Time `json:"readAt,omitzero"`
	Mod       time.Time `json:"-"`
	Err       string    `json:"err,omitempty"`
	Providers int       `json:"providers"`
	Matches   int       `json:"matches"`
	// Detail is a short human summary chosen by the source. Implementations
	// must never put credentials in it.
	Detail string `json:"detail,omitempty"`
}

Status is a snapshot of a source's state, for doctor and the GUI. Err is the last load problem, which is informational: a broken source never stops attribution.

Jump to

Keyboard shortcuts

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