package wire import ( "fmt" "slices" "time" ) // Models chosen per step (connectors spec rev 3.3, §16, slice S5b). A // metered connection is its key plus an allowed set of the models the // key lists that Equipo can price and tier; the runner chooses one of // them for each step. The set changes only by the person's own durable // models frame, revision-checked; the runner applies it in one thin.db // transaction and then acks it, and hello and sync carry the set it // applied so a reconnect sends the frame again when it is behind. // FrameModels (instance to runner, durable) is a metered connection's // allowed set at a revision. It replaces S1's model frame from // ProtocolModels on; Encode refuses it below. const FrameModels = "models" // PromptModels is the models card of a connecting API-key connection: // every model the key listed (ConnectPrompt.Choices), the ones that can // be allowed ticked by default. Below ProtocolModels it is said as // PromptModel with the eligible models only. const PromptModels = "models" // Model states on a metered connection (§16.1, §16.4). const ( // ModelUnprobed: allowed, never answered yet; probed on first use. ModelUnprobed = "unprobed" // ModelHealthy: its probe was answered by that model. ModelHealthy = "healthy" // ModelUnhealthy: not found at the provider, answered by another // model, or reported as another model in a step (model_mismatch). ModelUnhealthy = "unhealthy" ) // Why a model is unhealthy. const ( ModelNotFound = "not_found" ModelOtherAnswered = "other_model" ModelMismatch = "model_mismatch" ) // ReasonModelUnavailable fails a question whose session's model was // removed from its connection's allowed set or is unhealthy (§16.3): // the question never moves to another model or connection. const ReasonModelUnavailable = "model_unavailable" // ModelsCommand is a metered connection's allowed set at Revision. // Revisions rise by one per change; the runner refuses any revision not // above the one it holds. type ModelsCommand struct { Connection string `json:"connection"` Person string `json:"person"` LocalID string `json:"local_id"` Allowed []string `json:"allowed"` Revision int64 `json:"revision"` } // ModelChoice is one model a key listed: Eligible when Equipo has its // price and its tier, so it can be allowed (else "No price yet"). type ModelChoice struct { ID string `json:"id"` Eligible bool `json:"eligible"` Family string `json:"family,omitempty"` Tier string `json:"tier,omitempty"` } // ModelState is one allowed model as the runner holds it. type ModelState struct { ID string `json:"id"` State string `json:"state"` FailedAt *time.Time `json:"failed_at,omitempty"` Reason string `json:"reason,omitempty"` Family string `json:"family,omitempty"` Tier string `json:"tier,omitempty"` } // ConnectionModel is one model on a metered connection's page: whether // it can be allowed (Eligible), whether the person allows it (Allowed, // the latest set) and whether the runner applied that (Applied), with // its state on the runner. type ConnectionModel struct { ID string `json:"id"` Eligible bool `json:"eligible"` Allowed bool `json:"allowed"` Applied bool `json:"applied"` State string `json:"state,omitempty"` Reason string `json:"reason,omitempty"` FailedAt *time.Time `json:"failed_at,omitempty"` Family string `json:"family,omitempty"` Tier string `json:"tier,omitempty"` } // SetModels is PUT /api/v1/connections/{id}/models: the allowed set // against the revision the person saw (0 while the connection is still // connecting and has none). type SetModels struct { Allowed []string `json:"allowed"` Revision int64 `json:"revision"` } // ValidAllowed reports whether a set is one the person may send: at // least one model, at most MaxModels, each a plain model id, once. func ValidAllowed(allowed []string) bool { if len(allowed) == 0 || len(allowed) > MaxModels { return false } seen := map[string]bool{} for _, m := range allowed { if m == "" || len(m) > MaxModelName || seen[m] { return false } seen[m] = true } return true } // downgradeModels takes what ProtocolModels added out of what an older // peer is sent (in new values; the caller's are untouched): the models // frame is refused; the models card is said as the one-model pick of the // eligible models; a held connection's set and states, a usage row's // family, tier, tier version and mismatch, and a dispatch's avoid_family // go; a question's model_unavailable is said as account_unavailable, // which every older instance knows. It reports whether anything changed. func downgradeModels(body any, protocol int) (bool, error) { held := func(cs *[]HeldConnection) bool { if !slices.ContainsFunc(*cs, func(h HeldConnection) bool { return h.Models != nil || h.ModelsRevision != 0 || h.Choices != nil }) { return false } out := slices.Clone(*cs) for i := range out { out[i].Models, out[i].ModelsRevision, out[i].Choices = nil, 0, nil } *cs = out return true } rows := func(us *[]Usage) bool { if !slices.ContainsFunc(*us, func(u Usage) bool { return u.Family != "" || u.Tier != "" || u.TierVersion != "" || u.ModelMismatch }) { return false } out := slices.Clone(*us) for i := range out { out[i].Family, out[i].Tier, out[i].TierVersion, out[i].ModelMismatch = "", "", "", false } *us = out return true } switch v := body.(type) { case *ModelsCommand: return false, fmt.Errorf("%w: a models frame at protocol %d", ErrProtocol, protocol) case *Hello: return held(&v.Connections), nil case *Sync: return held(&v.Connections), nil case *ConnectionReport: if v.Prompt == nil || v.Prompt.Kind != PromptModels && v.Prompt.Choices == nil { return false, nil } p := *v.Prompt models := []string{} for _, c := range p.Choices { if c.Eligible { models = append(models, c.ID) } } if p.Kind == PromptModels { p.Kind, p.Models = PromptModel, models } p.Choices = nil v.Prompt = &p return true, nil case *DispatchCommand: set := v.AvoidFamily != "" v.AvoidFamily = "" return set, nil case *UsageReport: return rows(&v.Rows), nil case *StepReport: return rows(&v.Usage), nil case *AnswerReport: changed := rows(&v.Usage) if v.Reason == ReasonModelUnavailable { v.Reason, changed = "account_unavailable", true } return changed, nil case *Reply: return rows(&v.Usage), nil } return false, nil }