| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // Models chosen per step (connectors spec rev 3.3, §16, slice S5b). A |
| 10 | // metered connection is its key plus an allowed set of the models the |
| 11 | // key lists that Equipo can price and tier; the runner chooses one of |
| 12 | // them for each step. The set changes only by the person's own durable |
| 13 | // models frame, revision-checked; the runner applies it in one thin.db |
| 14 | // transaction and then acks it, and hello and sync carry the set it |
| 15 | // applied so a reconnect sends the frame again when it is behind. |
| 16 | |
| 17 | // FrameModels (instance to runner, durable) is a metered connection's |
| 18 | // allowed set at a revision. It replaces S1's model frame from |
| 19 | // ProtocolModels on; Encode refuses it below. |
| 20 | const FrameModels = "models" |
| 21 | |
| 22 | // PromptModels is the models card of a connecting API-key connection: |
| 23 | // every model the key listed (ConnectPrompt.Choices), the ones that can |
| 24 | // be allowed ticked by default. Below ProtocolModels it is said as |
| 25 | // PromptModel with the eligible models only. |
| 26 | const PromptModels = "models" |
| 27 | |
| 28 | // Model states on a metered connection (§16.1, §16.4). |
| 29 | const ( |
| 30 | // ModelUnprobed: allowed, never answered yet; probed on first use. |
| 31 | ModelUnprobed = "unprobed" |
| 32 | // ModelHealthy: its probe was answered by that model. |
| 33 | ModelHealthy = "healthy" |
| 34 | // ModelUnhealthy: not found at the provider, answered by another |
| 35 | // model, or reported as another model in a step (model_mismatch). |
| 36 | ModelUnhealthy = "unhealthy" |
| 37 | ) |
| 38 | |
| 39 | // Why a model is unhealthy. |
| 40 | const ( |
| 41 | ModelNotFound = "not_found" |
| 42 | ModelOtherAnswered = "other_model" |
| 43 | ModelMismatch = "model_mismatch" |
| 44 | ) |
| 45 | |
| 46 | // ReasonModelUnavailable fails a question whose session's model was |
| 47 | // removed from its connection's allowed set or is unhealthy (§16.3): |
| 48 | // the question never moves to another model or connection. |
| 49 | const ReasonModelUnavailable = "model_unavailable" |
| 50 | |
| 51 | // ModelsCommand is a metered connection's allowed set at Revision. |
| 52 | // Revisions rise by one per change; the runner refuses any revision not |
| 53 | // above the one it holds. |
| 54 | type ModelsCommand struct { |
| 55 | Connection string `json:"connection"` |
| 56 | Person string `json:"person"` |
| 57 | LocalID string `json:"local_id"` |
| 58 | Allowed []string `json:"allowed"` |
| 59 | Revision int64 `json:"revision"` |
| 60 | } |
| 61 | |
| 62 | // ModelChoice is one model a key listed: Eligible when Equipo has its |
| 63 | // price and its tier, so it can be allowed (else "No price yet"). |
| 64 | type ModelChoice struct { |
| 65 | ID string `json:"id"` |
| 66 | Eligible bool `json:"eligible"` |
| 67 | Family string `json:"family,omitempty"` |
| 68 | Tier string `json:"tier,omitempty"` |
| 69 | } |
| 70 | |
| 71 | // ModelState is one allowed model as the runner holds it. |
| 72 | type ModelState struct { |
| 73 | ID string `json:"id"` |
| 74 | State string `json:"state"` |
| 75 | FailedAt *time.Time `json:"failed_at,omitempty"` |
| 76 | Reason string `json:"reason,omitempty"` |
| 77 | Family string `json:"family,omitempty"` |
| 78 | Tier string `json:"tier,omitempty"` |
| 79 | } |
| 80 | |
| 81 | // ConnectionModel is one model on a metered connection's page: whether |
| 82 | // it can be allowed (Eligible), whether the person allows it (Allowed, |
| 83 | // the latest set) and whether the runner applied that (Applied), with |
| 84 | // its state on the runner. |
| 85 | type ConnectionModel struct { |
| 86 | ID string `json:"id"` |
| 87 | Eligible bool `json:"eligible"` |
| 88 | Allowed bool `json:"allowed"` |
| 89 | Applied bool `json:"applied"` |
| 90 | State string `json:"state,omitempty"` |
| 91 | Reason string `json:"reason,omitempty"` |
| 92 | FailedAt *time.Time `json:"failed_at,omitempty"` |
| 93 | Family string `json:"family,omitempty"` |
| 94 | Tier string `json:"tier,omitempty"` |
| 95 | } |
| 96 | |
| 97 | // SetModels is PUT /api/v1/connections/{id}/models: the allowed set |
| 98 | // against the revision the person saw (0 while the connection is still |
| 99 | // connecting and has none). |
| 100 | type SetModels struct { |
| 101 | Allowed []string `json:"allowed"` |
| 102 | Revision int64 `json:"revision"` |
| 103 | } |
| 104 | |
| 105 | // ValidAllowed reports whether a set is one the person may send: at |
| 106 | // least one model, at most MaxModels, each a plain model id, once. |
| 107 | func ValidAllowed(allowed []string) bool { |
| 108 | if len(allowed) == 0 || len(allowed) > MaxModels { |
| 109 | return false |
| 110 | } |
| 111 | seen := map[string]bool{} |
| 112 | for _, m := range allowed { |
| 113 | if m == "" || len(m) > MaxModelName || seen[m] { |
| 114 | return false |
| 115 | } |
| 116 | seen[m] = true |
| 117 | } |
| 118 | return true |
| 119 | } |
| 120 | |
| 121 | // downgradeModels takes what ProtocolModels added out of what an older |
| 122 | // peer is sent (in new values; the caller's are untouched): the models |
| 123 | // frame is refused; the models card is said as the one-model pick of the |
| 124 | // eligible models; a held connection's set and states, a usage row's |
| 125 | // family, tier, tier version and mismatch, and a dispatch's avoid_family |
| 126 | // go; a question's model_unavailable is said as account_unavailable, |
| 127 | // which every older instance knows. It reports whether anything changed. |
| 128 | func downgradeModels(body any, protocol int) (bool, error) { |
| 129 | held := func(cs *[]HeldConnection) bool { |
| 130 | if !slices.ContainsFunc(*cs, func(h HeldConnection) bool { return h.Models != nil || h.ModelsRevision != 0 || h.Choices != nil }) { |
| 131 | return false |
| 132 | } |
| 133 | out := slices.Clone(*cs) |
| 134 | for i := range out { |
| 135 | out[i].Models, out[i].ModelsRevision, out[i].Choices = nil, 0, nil |
| 136 | } |
| 137 | *cs = out |
| 138 | return true |
| 139 | } |
| 140 | rows := func(us *[]Usage) bool { |
| 141 | if !slices.ContainsFunc(*us, func(u Usage) bool { |
| 142 | return u.Family != "" || u.Tier != "" || u.TierVersion != "" || u.ModelMismatch |
| 143 | }) { |
| 144 | return false |
| 145 | } |
| 146 | out := slices.Clone(*us) |
| 147 | for i := range out { |
| 148 | out[i].Family, out[i].Tier, out[i].TierVersion, out[i].ModelMismatch = "", "", "", false |
| 149 | } |
| 150 | *us = out |
| 151 | return true |
| 152 | } |
| 153 | switch v := body.(type) { |
| 154 | case *ModelsCommand: |
| 155 | return false, fmt.Errorf("%w: a models frame at protocol %d", ErrProtocol, protocol) |
| 156 | case *Hello: |
| 157 | return held(&v.Connections), nil |
| 158 | case *Sync: |
| 159 | return held(&v.Connections), nil |
| 160 | case *ConnectionReport: |
| 161 | if v.Prompt == nil || v.Prompt.Kind != PromptModels && v.Prompt.Choices == nil { |
| 162 | return false, nil |
| 163 | } |
| 164 | p := *v.Prompt |
| 165 | models := []string{} |
| 166 | for _, c := range p.Choices { |
| 167 | if c.Eligible { |
| 168 | models = append(models, c.ID) |
| 169 | } |
| 170 | } |
| 171 | if p.Kind == PromptModels { |
| 172 | p.Kind, p.Models = PromptModel, models |
| 173 | } |
| 174 | p.Choices = nil |
| 175 | v.Prompt = &p |
| 176 | return true, nil |
| 177 | case *DispatchCommand: |
| 178 | set := v.AvoidFamily != "" |
| 179 | v.AvoidFamily = "" |
| 180 | return set, nil |
| 181 | case *UsageReport: |
| 182 | return rows(&v.Rows), nil |
| 183 | case *StepReport: |
| 184 | return rows(&v.Usage), nil |
| 185 | case *AnswerReport: |
| 186 | changed := rows(&v.Usage) |
| 187 | if v.Reason == ReasonModelUnavailable { |
| 188 | v.Reason, changed = "account_unavailable", true |
| 189 | } |
| 190 | return changed, nil |
| 191 | case *Reply: |
| 192 | return rows(&v.Usage), nil |
| 193 | } |
| 194 | return false, nil |
| 195 | } |
| 196 | |