package wire import ( "slices" "time" ) // The Usage page and advice (connectors spec 2026-11-12 rev 3.2, §9 and // §10, slice S4; Connections rev 3 §6): a reading's age on the wire, // the person API's usage totals, and advice with its two actions. // downgradeReadings drops every headroom's reading time (an instance // below ProtocolReadings decodes headroom strictly). It reports whether // anything changed. func downgradeReadings(body any) bool { switch v := body.(type) { case *Hello: return WithoutReadingTimes(v) case *Sync: return withoutCheckedAt(&v.Connections) case *ConnectionReport: if v.Headroom == nil || v.Headroom.CheckedAt == nil { return false } h := *v.Headroom h.CheckedAt, v.Headroom = nil, &h return true } return false } // WithoutReadingTimes drops the reading time from a hello's connections // (in new values, so the caller's are untouched). The runner's first // hello on a connection is sent before the instance says its protocol, // and an instance below ProtocolReadings decodes it strictly; the next // hello carries the times once the welcome says protocol 15. func WithoutReadingTimes(h *Hello) bool { return withoutCheckedAt(&h.Connections) } func withoutCheckedAt(cs *[]HeldConnection) bool { if !slices.ContainsFunc(*cs, func(c HeldConnection) bool { return c.Headroom != nil && c.Headroom.CheckedAt != nil }) { return false } out := slices.Clone(*cs) for i := range out { if out[i].Headroom != nil && out[i].Headroom.CheckedAt != nil { h := *out[i].Headroom h.CheckedAt = nil out[i].Headroom = &h } } *cs = out return true } // Advice kinds (§9), in their arbitration order: a metered connection // near a budget, a preferred subscription near its limit while another // has room, work that could wait so urgent work goes first, and low // work waiting a week. const ( AdviceBudgetNear = "budget_near" AdvicePreferSwitch = "prefer_switch" AdviceDeferTasks = "defer_tasks" AdviceHeldLong = "held_long" ) // AdviceKinds are the kinds in arbitration order. var AdviceKinds = []string{AdviceBudgetNear, AdvicePreferSwitch, AdviceDeferTasks, AdviceHeldLong} // Advice states. The advice table only moves forward: open, then acted, // dismissed (Not now) or withdrawn (its facts stopped holding). const ( AdviceOpen = "open" AdviceActed = "acted" AdviceDismissed = "dismissed" AdviceWithdrawn = "withdrawn" ) // CardAdvice is advice's one quiet card in the conversation: never // Needs you, never a push; its two choices are act and dismiss. const CardAdvice = "advice" // Choices on an advice card. const ( ChoiceAct = "act" ChoiceDismiss = "dismiss" ) // Advice is GET /api/v1/advice's item: one card of fixed words, its // action and its Not now, for the subject its key names. Revision must // be named by act and dismiss. type Advice struct { ID string `json:"id"` Kind string `json:"kind"` Key string `json:"key"` State string `json:"state"` Subject AdviceSubject `json:"subject"` Text string `json:"text"` // Act and Dismiss are the two buttons' words. Act string `json:"act"` Dismiss string `json:"dismiss"` Revision int64 `json:"revision"` CreatedAt time.Time `json:"created_at"` VisibleAt *time.Time `json:"visible_at"` } // AdviceSubject is what advice is about: a connection (the instance's // id), a role and the connection it would move to, or tasks. type AdviceSubject struct { Connection string `json:"connection,omitempty"` Role string `json:"role,omitempty"` Tasks []string `json:"tasks,omitempty"` } // AdviceList is GET /api/v1/advice: the person's open advice that may // show now (at most one). type AdviceList struct { Items []Advice `json:"items"` } // AdviceAction is the body of POST /api/v1/advice/{id}/act and // /dismiss: the advice's revision as the person saw it. type AdviceAction struct { Revision int64 `json:"revision"` } // Usage periods and groupings (Connections rev 3 §6): today, this week // from Monday and this month from the 1st, in the person's zone; rows by // connection, role, project or task over one dataset. const ( UsageDay = "day" UsageWeek = "week" UsageMonth = "month" UsageByConnection = "connection" UsageByRole = "role" UsageByProject = "project" UsageByTask = "task" // UsageByModel groups by the model each row ran on (§16.7). UsageByModel = "model" ) // UsagePeriods and UsageGroupings are the values the page and the API take. var ( UsagePeriods = []string{UsageDay, UsageWeek, UsageMonth} UsageGroupings = []string{UsageByConnection, UsageByRole, UsageByProject, UsageByTask, UsageByModel} ) // UsageTotals is GET /api/v1/usage?period=&by=: the person's recorded // token use for the period, one row per key. LeftOut says rows were // left out because they overcount (an older runner's). Prices are the // price table each metered connection was last charged by. type UsageTotals struct { Period string `json:"period"` By string `json:"by"` From time.Time `json:"from"` To time.Time `json:"to"` Rows []UsageRow `json:"rows"` LeftOut bool `json:"left_out"` Prices []PriceSource `json:"prices"` } // UsageRow is one grouping's sums. ID is the connection's, project's or // task's id or the role (a maker's step, Design's, is its profile: // design-v1); Label the name the record carries. Partial: a // row lacked a figure, so the sums are at least these. Cost is the // estimate from the price table where every row was priced (null // otherwise), in Currency. type UsageRow struct { ID string `json:"id"` Label string `json:"label"` Input int64 `json:"input"` CachedInput int64 `json:"cached_input"` Output int64 `json:"output"` Partial bool `json:"partial"` Cost *int64 `json:"cost"` Currency string `json:"currency"` Attempts int64 `json:"attempts"` } // PriceSource is the price table a metered connection was last charged // by: its version names the provider's public page and the day it was // read (tensorx-2026-11-12). type PriceSource struct { Connection string `json:"connection"` PriceVersion string `json:"price_version"` // TierVersion is the tier table its last row was chosen under // (connectors spec rev 3.3 §16.2): Equipo's own, dated. TierVersion string `json:"tier_version,omitempty"` }