package wire import "errors" // Metered connections (connectors spec 2026-11-12, §§2.1 to 2.3, §10, // slice S1). A connection made from a provider API key the person pastes, // sealed in the page to the runner as a Claude token is (KindAPIKey). // The provider's own limit is the only hard cap; Equipo keeps a soft // budget from the usage the harness reported, and a metered connection // runs one attempt at a time. Nothing here is a secret: the key stays on // the runner, in the connection's home. // KindAPIKey is a connection made from a pasted provider API key. // PromptAPIKey is its card (paste the key); PromptModel the card that // follows once the runner has the key and has read the provider's model // list: the person picks the connection's one model (Models). const ( KindAPIKey = "api_key" PromptAPIKey = "api_key" PromptModel = "model" ) // Billing says how a connection is paid for (§2.1): a subscription, or // metered (billed per token, or a subscription that can bill overflow // with pay-as-you-go not confirmed off). const ( BillingSubscription = "subscription" BillingMetered = "metered" ) // Harnesses and providers of API-key connections. S1 adds TensorX with // Pi as its harness (§0 decision 1); S5 adds OpenCode Go with OpenCode // (§4.2, ProtocolOpenCode). const ( HarnessPi = "pi" HarnessOpenCode = "opencode" ProviderTensorX = "tensorx" ProviderOpenCodeGo = "opencode-go" ) // APIKeyProvider is one provider an API-key connection may be made for: // its harness, its origin (Upstream, the only host its connection may // reach) and what a sealed key is bound to (Endpoint). PerToken: billed // per token, always metered. Overflow: a subscription that can bill // overflow, metered unless the person confirms a standing setting turns // overflow off (§0 decision 7). Currency is its price table's. type APIKeyProvider struct { Name string Harness string Upstream string // APIPath is the provider's OpenAI-compatible base path under // Upstream: completions at APIPath+"/chat/completions", and the // models at APIPath+"/models" unless ModelsPath names them (§2.2, // §4). APIPath string ModelsPath string Endpoint string PerToken bool Overflow bool Currency string } // APIKeyProviders are the providers an API key connects, by name. // // OpenCode Go is a subscription that can fall back to Console credit on // a limit (§2.1). No standing setting that turns that off is confirmed // (§0 decision 7: a zero Console balance is a snapshot, not a setting), // so it takes no pay-as-you-go confirmation and is always metered: it is // neither PerToken nor Overflow. Its chat completions are under // /inference/go/openai/v1, its model list at /inference/go/v1/models. var APIKeyProviders = map[string]APIKeyProvider{ ProviderTensorX: {Name: "TensorX", Harness: HarnessPi, Upstream: "https://api.tensorx.ai", APIPath: "/v1", Endpoint: "https://api.tensorx.ai:443", PerToken: true, Currency: "USD"}, ProviderOpenCodeGo: {Name: "OpenCode Go", Harness: HarnessOpenCode, Upstream: "https://opencode.ai", APIPath: "/inference/go/openai/v1", ModelsPath: "/inference/go/v1/models", Endpoint: "https://opencode.ai:443", Currency: "USD"}, } // HarnessFacts are what a person is told about a metered harness's // reporting. PartialUsageUnderCounts: a request inside a turn that the // provider answers without usage counts as zero in the turn's figure, so // spend can be under-counted (OpenCode: connectors spec §4.2 and §12). // Pi's missing per-message usage makes the spend unknown instead. type HarnessFacts struct { PartialUsageUnderCounts bool } // Harnesses are the metered harnesses' facts, by name. var Harnesses = map[string]HarnessFacts{ HarnessPi: {}, HarnessOpenCode: {PartialUsageUnderCounts: true}, } // BaseURL is the provider's OpenAI-compatible base URL (a harness's // configuration names it). func (p APIKeyProvider) BaseURL() string { return p.Upstream + p.APIPath } // CompletionsURL is where its chat completions are (the one-token probe). func (p APIKeyProvider) CompletionsURL() string { return p.BaseURL() + "/chat/completions" } // ModelsURL is where its model list is. func (p APIKeyProvider) ModelsURL() string { if p.ModelsPath != "" { return p.Upstream + p.ModelsPath } return p.BaseURL() + "/models" } // openCodeOnly reports whether a provider is new at ProtocolOpenCode: a // peer below it knows nothing of it. func openCodeOnly(provider string) bool { return provider == ProviderOpenCodeGo } // Billing is how a new connection for p is billed, given the person's // pay-as-you-go confirmation: metered unless p is an overflow // subscription confirmed off. func (p APIKeyProvider) Billing(paygOff bool) string { if !p.PerToken && p.Overflow && paygOff { return BillingSubscription } return BillingMetered } // Holds a metered connection's guard gives (§2.3, §7): its one slot is // busy, its budget is reached, or its spend is unknown. A question held // by the guard reports one as its reason while it waits. const ( HoldBusy = "busy" HoldBudget = "budget" HoldSpendUnknown = "spend_unknown" ) // ErrConfirmation refuses an API-key connection without the // confirmations its billing needs (§2.1). var ErrConfirmation = errors.New("wire: the connection needs its confirmations") // CheckConfirmations is the add-connection rule (§2.1, §0 decisions 3 // and 7): a metered connection needs the provider-side limit confirmed; // a pay-as-you-go confirmation only means something for a subscription // that can bill overflow, so it is refused for any other. func (p APIKeyProvider) CheckConfirmations(limit, paygOff bool) error { if paygOff && (p.PerToken || !p.Overflow) { return ErrConfirmation } if p.Billing(paygOff) == BillingMetered && !limit { return ErrConfirmation } return nil } // Budget is a metered connection's soft budget (§2.3): a day's and a // month's figure in micro-units of Currency, the price table's. Revision // orders changes: the runner applies a budget frame only above the // revision it holds. type Budget struct { Day int64 `json:"day"` Month int64 `json:"month"` Currency string `json:"currency"` Revision int64 `json:"revision"` } // Budget bounds: at most a thousand units a day and ten thousand a month // (well past any soft budget a person sets), never zero or negative. const ( MaxBudgetDay = 1000 * 1_000_000 MaxBudgetMonth = 10000 * 1_000_000 // DefaultBudgetDay and DefaultBudgetMonth pre-fill the form (§0 // decision 4): 1 and 10 in the price table's currency. DefaultBudgetDay = 1_000_000 DefaultBudgetMonth = 10_000_000 ) // ValidBudget reports whether b's figures are in bounds. func ValidBudget(b Budget) bool { return b.Day > 0 && b.Month > 0 && b.Day <= MaxBudgetDay && b.Month <= MaxBudgetMonth && b.Day <= b.Month && b.Revision >= 0 } // Spend is what the runner holds a metered connection has spent, from // reported usage priced by its table (§2.3): today's and this month's // charges in micro-units of Currency (UTC days until a zone is known). // Unknown: an attempt ended without a complete, priced figure, and // nothing more is admitted until the person resumes with PauseRevision. // Running: an attempt holds the connection's one slot now. type Spend struct { Day int64 `json:"day"` Month int64 `json:"month"` Currency string `json:"currency"` Unknown bool `json:"unknown"` PauseRevision int64 `json:"pause_revision"` Running bool `json:"running"` } // Frames for metered connections (instance to runner, durable). Each is // refused below ProtocolMetered: an older runner never holds a metered // connection. const ( // FrameModel is the person's pick of a connecting API-key // connection's one model, from the list the runner read. FrameModel = "model" // FrameBudget changes a metered connection's budget; acked once the // runner applied it (until then the page says it is pending). FrameBudget = "budget" // FrameResumeSpend is the person's Resume of a connection paused // with its spend unknown, bound to the pause it saw. FrameResumeSpend = "resume_spend" ) // ModelCommand picks the model of the person's connecting connection. type ModelCommand struct { Connection string `json:"connection"` Person string `json:"person"` Model string `json:"model"` } // BudgetCommand sets a metered connection's budget. type BudgetCommand struct { Connection string `json:"connection"` Person string `json:"person"` LocalID string `json:"local_id"` Budget Budget `json:"budget"` } // ResumeSpendCommand resumes a connection whose spend is unknown. The // runner consumes it once, and only while PauseRevision is still the // connection's pause: a stale or repeated Resume leaves a newer pause. type ResumeSpendCommand struct { Connection string `json:"connection"` Person string `json:"person"` LocalID string `json:"local_id"` PauseRevision int64 `json:"pause_revision"` } // SetBudget is PUT /api/v1/connections/{id}/budget: the day's and the // month's figures (micro-units of the connection's currency) against // the budget revision the person saw. type SetBudget struct { Day int64 `json:"day"` Month int64 `json:"month"` Revision int64 `json:"revision"` } // ResumeSpend is POST /api/v1/connections/{id}/resume. type ResumeSpend struct { PauseRevision int64 `json:"pause_revision"` } // ChooseModel is POST /api/v1/connections/{id}/model: the connecting // card's pick. type ChooseModel struct { Model string `json:"model"` } // MaxModels bounds a model list on a card; MaxModelName one model id. const ( MaxModels = 200 MaxModelName = 200 ) // UsageProbe is the role of a usage row from a metered connection's // health probe: one billable token on the selected model (§2.2). const UsageProbe = "probe"