oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package wire
2
3import "errors"
4
5// Metered connections (connectors spec 2026-11-12, §§2.1 to 2.3, §10,
6// slice S1). A connection made from a provider API key the person pastes,
7// sealed in the page to the runner as a Claude token is (KindAPIKey).
8// The provider's own limit is the only hard cap; Equipo keeps a soft
9// budget from the usage the harness reported, and a metered connection
10// runs one attempt at a time. Nothing here is a secret: the key stays on
11// the runner, in the connection's home.
12
13// KindAPIKey is a connection made from a pasted provider API key.
14// PromptAPIKey is its card (paste the key); PromptModel the card that
15// follows once the runner has the key and has read the provider's model
16// list: the person picks the connection's one model (Models).
17const (
18 KindAPIKey = "api_key"
19 PromptAPIKey = "api_key"
20 PromptModel = "model"
21)
22
23// Billing says how a connection is paid for (§2.1): a subscription, or
24// metered (billed per token, or a subscription that can bill overflow
25// with pay-as-you-go not confirmed off).
26const (
27 BillingSubscription = "subscription"
28 BillingMetered = "metered"
29)
30
31// Harnesses and providers of API-key connections. S1 adds TensorX with
32// Pi as its harness (§0 decision 1); S5 adds OpenCode Go with OpenCode
33// (§4.2, ProtocolOpenCode).
34const (
35 HarnessPi = "pi"
36 HarnessOpenCode = "opencode"
37 ProviderTensorX = "tensorx"
38 ProviderOpenCodeGo = "opencode-go"
39)
40
41// APIKeyProvider is one provider an API-key connection may be made for:
42// its harness, its origin (Upstream, the only host its connection may
43// reach) and what a sealed key is bound to (Endpoint). PerToken: billed
44// per token, always metered. Overflow: a subscription that can bill
45// overflow, metered unless the person confirms a standing setting turns
46// overflow off (§0 decision 7). Currency is its price table's.
47type APIKeyProvider struct {
48 Name string
49 Harness string
50 Upstream string
51 // APIPath is the provider's OpenAI-compatible base path under
52 // Upstream: completions at APIPath+"/chat/completions", and the
53 // models at APIPath+"/models" unless ModelsPath names them (§2.2,
54 // §4).
55 APIPath string
56 ModelsPath string
57 Endpoint string
58 PerToken bool
59 Overflow bool
60 Currency string
61}
62
63// APIKeyProviders are the providers an API key connects, by name.
64//
65// OpenCode Go is a subscription that can fall back to Console credit on
66// a limit (§2.1). No standing setting that turns that off is confirmed
67// (§0 decision 7: a zero Console balance is a snapshot, not a setting),
68// so it takes no pay-as-you-go confirmation and is always metered: it is
69// neither PerToken nor Overflow. Its chat completions are under
70// /inference/go/openai/v1, its model list at /inference/go/v1/models.
71var APIKeyProviders = map[string]APIKeyProvider{
72 ProviderTensorX: {Name: "TensorX", Harness: HarnessPi, Upstream: "https://api.tensorx.ai", APIPath: "/v1", Endpoint: "https://api.tensorx.ai:443", PerToken: true, Currency: "USD"},
73 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"},
74}
75
76// HarnessFacts are what a person is told about a metered harness's
77// reporting. PartialUsageUnderCounts: a request inside a turn that the
78// provider answers without usage counts as zero in the turn's figure, so
79// spend can be under-counted (OpenCode: connectors spec §4.2 and §12).
80// Pi's missing per-message usage makes the spend unknown instead.
81type HarnessFacts struct {
82 PartialUsageUnderCounts bool
83}
84
85// Harnesses are the metered harnesses' facts, by name.
86var Harnesses = map[string]HarnessFacts{
87 HarnessPi: {},
88 HarnessOpenCode: {PartialUsageUnderCounts: true},
89}
90
91// BaseURL is the provider's OpenAI-compatible base URL (a harness's
92// configuration names it).
93func (p APIKeyProvider) BaseURL() string { return p.Upstream + p.APIPath }
94
95// CompletionsURL is where its chat completions are (the one-token probe).
96func (p APIKeyProvider) CompletionsURL() string { return p.BaseURL() + "/chat/completions" }
97
98// ModelsURL is where its model list is.
99func (p APIKeyProvider) ModelsURL() string {
100 if p.ModelsPath != "" {
101 return p.Upstream + p.ModelsPath
102 }
103 return p.BaseURL() + "/models"
104}
105
106// openCodeOnly reports whether a provider is new at ProtocolOpenCode: a
107// peer below it knows nothing of it.
108func openCodeOnly(provider string) bool { return provider == ProviderOpenCodeGo }
109
110// Billing is how a new connection for p is billed, given the person's
111// pay-as-you-go confirmation: metered unless p is an overflow
112// subscription confirmed off.
113func (p APIKeyProvider) Billing(paygOff bool) string {
114 if !p.PerToken && p.Overflow && paygOff {
115 return BillingSubscription
116 }
117 return BillingMetered
118}
119
120// Holds a metered connection's guard gives (§2.3, §7): its one slot is
121// busy, its budget is reached, or its spend is unknown. A question held
122// by the guard reports one as its reason while it waits.
123const (
124 HoldBusy = "busy"
125 HoldBudget = "budget"
126 HoldSpendUnknown = "spend_unknown"
127)
128
129// ErrConfirmation refuses an API-key connection without the
130// confirmations its billing needs (§2.1).
131var ErrConfirmation = errors.New("wire: the connection needs its confirmations")
132
133// CheckConfirmations is the add-connection rule (§2.1, §0 decisions 3
134// and 7): a metered connection needs the provider-side limit confirmed;
135// a pay-as-you-go confirmation only means something for a subscription
136// that can bill overflow, so it is refused for any other.
137func (p APIKeyProvider) CheckConfirmations(limit, paygOff bool) error {
138 if paygOff && (p.PerToken || !p.Overflow) {
139 return ErrConfirmation
140 }
141 if p.Billing(paygOff) == BillingMetered && !limit {
142 return ErrConfirmation
143 }
144 return nil
145}
146
147// Budget is a metered connection's soft budget (§2.3): a day's and a
148// month's figure in micro-units of Currency, the price table's. Revision
149// orders changes: the runner applies a budget frame only above the
150// revision it holds.
151type Budget struct {
152 Day int64 `json:"day"`
153 Month int64 `json:"month"`
154 Currency string `json:"currency"`
155 Revision int64 `json:"revision"`
156}
157
158// Budget bounds: at most a thousand units a day and ten thousand a month
159// (well past any soft budget a person sets), never zero or negative.
160const (
161 MaxBudgetDay = 1000 * 1_000_000
162 MaxBudgetMonth = 10000 * 1_000_000
163 // DefaultBudgetDay and DefaultBudgetMonth pre-fill the form (§0
164 // decision 4): 1 and 10 in the price table's currency.
165 DefaultBudgetDay = 1_000_000
166 DefaultBudgetMonth = 10_000_000
167)
168
169// ValidBudget reports whether b's figures are in bounds.
170func ValidBudget(b Budget) bool {
171 return b.Day > 0 && b.Month > 0 && b.Day <= MaxBudgetDay && b.Month <= MaxBudgetMonth && b.Day <= b.Month && b.Revision >= 0
172}
173
174// Spend is what the runner holds a metered connection has spent, from
175// reported usage priced by its table (§2.3): today's and this month's
176// charges in micro-units of Currency (UTC days until a zone is known).
177// Unknown: an attempt ended without a complete, priced figure, and
178// nothing more is admitted until the person resumes with PauseRevision.
179// Running: an attempt holds the connection's one slot now.
180type Spend struct {
181 Day int64 `json:"day"`
182 Month int64 `json:"month"`
183 Currency string `json:"currency"`
184 Unknown bool `json:"unknown"`
185 PauseRevision int64 `json:"pause_revision"`
186 Running bool `json:"running"`
187}
188
189// Frames for metered connections (instance to runner, durable). Each is
190// refused below ProtocolMetered: an older runner never holds a metered
191// connection.
192const (
193 // FrameModel is the person's pick of a connecting API-key
194 // connection's one model, from the list the runner read.
195 FrameModel = "model"
196 // FrameBudget changes a metered connection's budget; acked once the
197 // runner applied it (until then the page says it is pending).
198 FrameBudget = "budget"
199 // FrameResumeSpend is the person's Resume of a connection paused
200 // with its spend unknown, bound to the pause it saw.
201 FrameResumeSpend = "resume_spend"
202)
203
204// ModelCommand picks the model of the person's connecting connection.
205type ModelCommand struct {
206 Connection string `json:"connection"`
207 Person string `json:"person"`
208 Model string `json:"model"`
209}
210
211// BudgetCommand sets a metered connection's budget.
212type BudgetCommand struct {
213 Connection string `json:"connection"`
214 Person string `json:"person"`
215 LocalID string `json:"local_id"`
216 Budget Budget `json:"budget"`
217}
218
219// ResumeSpendCommand resumes a connection whose spend is unknown. The
220// runner consumes it once, and only while PauseRevision is still the
221// connection's pause: a stale or repeated Resume leaves a newer pause.
222type ResumeSpendCommand struct {
223 Connection string `json:"connection"`
224 Person string `json:"person"`
225 LocalID string `json:"local_id"`
226 PauseRevision int64 `json:"pause_revision"`
227}
228
229// SetBudget is PUT /api/v1/connections/{id}/budget: the day's and the
230// month's figures (micro-units of the connection's currency) against
231// the budget revision the person saw.
232type SetBudget struct {
233 Day int64 `json:"day"`
234 Month int64 `json:"month"`
235 Revision int64 `json:"revision"`
236}
237
238// ResumeSpend is POST /api/v1/connections/{id}/resume.
239type ResumeSpend struct {
240 PauseRevision int64 `json:"pause_revision"`
241}
242
243// ChooseModel is POST /api/v1/connections/{id}/model: the connecting
244// card's pick.
245type ChooseModel struct {
246 Model string `json:"model"`
247}
248
249// MaxModels bounds a model list on a card; MaxModelName one model id.
250const (
251 MaxModels = 200
252 MaxModelName = 200
253)
254
255// UsageProbe is the role of a usage row from a metered connection's
256// health probe: one billable token on the selected model (§2.2).
257const UsageProbe = "probe"
258