| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "slices" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // The Usage page and advice (connectors spec 2026-11-12 rev 3.2, §9 and |
| 9 | // §10, slice S4; Connections rev 3 §6): a reading's age on the wire, |
| 10 | // the person API's usage totals, and advice with its two actions. |
| 11 | |
| 12 | // downgradeReadings drops every headroom's reading time (an instance |
| 13 | // below ProtocolReadings decodes headroom strictly). It reports whether |
| 14 | // anything changed. |
| 15 | func downgradeReadings(body any) bool { |
| 16 | switch v := body.(type) { |
| 17 | case *Hello: |
| 18 | return WithoutReadingTimes(v) |
| 19 | case *Sync: |
| 20 | return withoutCheckedAt(&v.Connections) |
| 21 | case *ConnectionReport: |
| 22 | if v.Headroom == nil || v.Headroom.CheckedAt == nil { |
| 23 | return false |
| 24 | } |
| 25 | h := *v.Headroom |
| 26 | h.CheckedAt, v.Headroom = nil, &h |
| 27 | return true |
| 28 | } |
| 29 | return false |
| 30 | } |
| 31 | |
| 32 | // WithoutReadingTimes drops the reading time from a hello's connections |
| 33 | // (in new values, so the caller's are untouched). The runner's first |
| 34 | // hello on a connection is sent before the instance says its protocol, |
| 35 | // and an instance below ProtocolReadings decodes it strictly; the next |
| 36 | // hello carries the times once the welcome says protocol 15. |
| 37 | func WithoutReadingTimes(h *Hello) bool { return withoutCheckedAt(&h.Connections) } |
| 38 | |
| 39 | func withoutCheckedAt(cs *[]HeldConnection) bool { |
| 40 | if !slices.ContainsFunc(*cs, func(c HeldConnection) bool { return c.Headroom != nil && c.Headroom.CheckedAt != nil }) { |
| 41 | return false |
| 42 | } |
| 43 | out := slices.Clone(*cs) |
| 44 | for i := range out { |
| 45 | if out[i].Headroom != nil && out[i].Headroom.CheckedAt != nil { |
| 46 | h := *out[i].Headroom |
| 47 | h.CheckedAt = nil |
| 48 | out[i].Headroom = &h |
| 49 | } |
| 50 | } |
| 51 | *cs = out |
| 52 | return true |
| 53 | } |
| 54 | |
| 55 | // Advice kinds (§9), in their arbitration order: a metered connection |
| 56 | // near a budget, a preferred subscription near its limit while another |
| 57 | // has room, work that could wait so urgent work goes first, and low |
| 58 | // work waiting a week. |
| 59 | const ( |
| 60 | AdviceBudgetNear = "budget_near" |
| 61 | AdvicePreferSwitch = "prefer_switch" |
| 62 | AdviceDeferTasks = "defer_tasks" |
| 63 | AdviceHeldLong = "held_long" |
| 64 | ) |
| 65 | |
| 66 | // AdviceKinds are the kinds in arbitration order. |
| 67 | var AdviceKinds = []string{AdviceBudgetNear, AdvicePreferSwitch, AdviceDeferTasks, AdviceHeldLong} |
| 68 | |
| 69 | // Advice states. The advice table only moves forward: open, then acted, |
| 70 | // dismissed (Not now) or withdrawn (its facts stopped holding). |
| 71 | const ( |
| 72 | AdviceOpen = "open" |
| 73 | AdviceActed = "acted" |
| 74 | AdviceDismissed = "dismissed" |
| 75 | AdviceWithdrawn = "withdrawn" |
| 76 | ) |
| 77 | |
| 78 | // CardAdvice is advice's one quiet card in the conversation: never |
| 79 | // Needs you, never a push; its two choices are act and dismiss. |
| 80 | const CardAdvice = "advice" |
| 81 | |
| 82 | // Choices on an advice card. |
| 83 | const ( |
| 84 | ChoiceAct = "act" |
| 85 | ChoiceDismiss = "dismiss" |
| 86 | ) |
| 87 | |
| 88 | // Advice is GET /api/v1/advice's item: one card of fixed words, its |
| 89 | // action and its Not now, for the subject its key names. Revision must |
| 90 | // be named by act and dismiss. |
| 91 | type Advice struct { |
| 92 | ID string `json:"id"` |
| 93 | Kind string `json:"kind"` |
| 94 | Key string `json:"key"` |
| 95 | State string `json:"state"` |
| 96 | Subject AdviceSubject `json:"subject"` |
| 97 | Text string `json:"text"` |
| 98 | // Act and Dismiss are the two buttons' words. |
| 99 | Act string `json:"act"` |
| 100 | Dismiss string `json:"dismiss"` |
| 101 | Revision int64 `json:"revision"` |
| 102 | CreatedAt time.Time `json:"created_at"` |
| 103 | VisibleAt *time.Time `json:"visible_at"` |
| 104 | } |
| 105 | |
| 106 | // AdviceSubject is what advice is about: a connection (the instance's |
| 107 | // id), a role and the connection it would move to, or tasks. |
| 108 | type AdviceSubject struct { |
| 109 | Connection string `json:"connection,omitempty"` |
| 110 | Role string `json:"role,omitempty"` |
| 111 | Tasks []string `json:"tasks,omitempty"` |
| 112 | } |
| 113 | |
| 114 | // AdviceList is GET /api/v1/advice: the person's open advice that may |
| 115 | // show now (at most one). |
| 116 | type AdviceList struct { |
| 117 | Items []Advice `json:"items"` |
| 118 | } |
| 119 | |
| 120 | // AdviceAction is the body of POST /api/v1/advice/{id}/act and |
| 121 | // /dismiss: the advice's revision as the person saw it. |
| 122 | type AdviceAction struct { |
| 123 | Revision int64 `json:"revision"` |
| 124 | } |
| 125 | |
| 126 | // Usage periods and groupings (Connections rev 3 §6): today, this week |
| 127 | // from Monday and this month from the 1st, in the person's zone; rows by |
| 128 | // connection, role, project or task over one dataset. |
| 129 | const ( |
| 130 | UsageDay = "day" |
| 131 | UsageWeek = "week" |
| 132 | UsageMonth = "month" |
| 133 | |
| 134 | UsageByConnection = "connection" |
| 135 | UsageByRole = "role" |
| 136 | UsageByProject = "project" |
| 137 | UsageByTask = "task" |
| 138 | // UsageByModel groups by the model each row ran on (§16.7). |
| 139 | UsageByModel = "model" |
| 140 | ) |
| 141 | |
| 142 | // UsagePeriods and UsageGroupings are the values the page and the API take. |
| 143 | var ( |
| 144 | UsagePeriods = []string{UsageDay, UsageWeek, UsageMonth} |
| 145 | UsageGroupings = []string{UsageByConnection, UsageByRole, UsageByProject, UsageByTask, UsageByModel} |
| 146 | ) |
| 147 | |
| 148 | // UsageTotals is GET /api/v1/usage?period=&by=: the person's recorded |
| 149 | // token use for the period, one row per key. LeftOut says rows were |
| 150 | // left out because they overcount (an older runner's). Prices are the |
| 151 | // price table each metered connection was last charged by. |
| 152 | type UsageTotals struct { |
| 153 | Period string `json:"period"` |
| 154 | By string `json:"by"` |
| 155 | From time.Time `json:"from"` |
| 156 | To time.Time `json:"to"` |
| 157 | Rows []UsageRow `json:"rows"` |
| 158 | LeftOut bool `json:"left_out"` |
| 159 | Prices []PriceSource `json:"prices"` |
| 160 | } |
| 161 | |
| 162 | // UsageRow is one grouping's sums. ID is the connection's, project's or |
| 163 | // task's id or the role (a maker's step, Design's, is its profile: |
| 164 | // design-v1); Label the name the record carries. Partial: a |
| 165 | // row lacked a figure, so the sums are at least these. Cost is the |
| 166 | // estimate from the price table where every row was priced (null |
| 167 | // otherwise), in Currency. |
| 168 | type UsageRow struct { |
| 169 | ID string `json:"id"` |
| 170 | Label string `json:"label"` |
| 171 | Input int64 `json:"input"` |
| 172 | CachedInput int64 `json:"cached_input"` |
| 173 | Output int64 `json:"output"` |
| 174 | Partial bool `json:"partial"` |
| 175 | Cost *int64 `json:"cost"` |
| 176 | Currency string `json:"currency"` |
| 177 | Attempts int64 `json:"attempts"` |
| 178 | } |
| 179 | |
| 180 | // PriceSource is the price table a metered connection was last charged |
| 181 | // by: its version names the provider's public page and the day it was |
| 182 | // read (tensorx-2026-11-12). |
| 183 | type PriceSource struct { |
| 184 | Connection string `json:"connection"` |
| 185 | PriceVersion string `json:"price_version"` |
| 186 | // TierVersion is the tier table its last row was chosen under |
| 187 | // (connectors spec rev 3.3 §16.2): Equipo's own, dated. |
| 188 | TierVersion string `json:"tier_version,omitempty"` |
| 189 | } |
| 190 | |