package wire import ( "encoding/json" "errors" "slices" ) // The Assistant's abilities (spec 2026-11-26-assistant-abilities ยง3): what // the Base owner can turn off for a member. Base's internal/equipo has the // same keys, in the same order. const ( AbilityWeb = "web" AbilityAppsRead = "apps_read" AbilityAppsWrite = "apps_write" AbilityBuild = "build" AbilityGoLive = "go_live" AbilityServices = "services" AbilityServicesAdd = "services_add" ) // Abilities are the keys in the order Base lists them. var Abilities = []string{AbilityWeb, AbilityAppsRead, AbilityAppsWrite, AbilityBuild, AbilityGoLive, AbilityServices, AbilityServicesAdd} // KnownAbility reports whether key is one of Abilities. func KnownAbility(key string) bool { return slices.Contains(Abilities, key) } // The values of Base-Capabilities: what Base and every client its member // uses can draw. They only decide presentation, never what may happen. const ( CapJobActions = "job_actions" CapLookupAnswers = "lookup_answers" ) // The codes a switched-off ability is refused with. const ( // CodeAbilityOff: the person's ability is off (Base's switch, the // operator's fixed key, or one it needs). CodeAbilityOff = "ability_off" // CodeAbilityUnmapped: an operation no ability covers is never run. CodeAbilityUnmapped = "ability_unmapped" // CodeOperationChanged: the operation resolved at send is not the one // the call was checked for. CodeOperationChanged = "operation_changed" // CodeHostUnverified: PUT host's token was not honoured by Base. CodeHostUnverified = "host_unverified" // CodeHostElsewhere: the person's Base is another, which still // honours them. CodeHostElsewhere = "host_elsewhere" // CodeResearchOff: Research for a job whose person can't have web // pages read. CodeResearchOff = "research_off" ) // AbilityBoard is Base's answer to GET /api/v1/abilities with a member's // host token: the member's switches at a revision, whether they // administer Base, and who to ask. Keys Equipo doesn't know are dropped // as it is read. type AbilityBoard struct { Revision int64 `json:"revision"` Admin bool `json:"admin"` Ask string `json:"ask"` Abilities map[string]bool `json:"abilities"` } // UnmarshalJSON reads a board, keeping only known ability keys. It is // strict about the envelope: an object with revision (a number), admin // (true or false), ask (a string) and abilities (an object of true and // false), each present and not null. Anything else is not a board, so a // Base answering 200 with something else changes nothing. func (b *AbilityBoard) UnmarshalJSON(raw []byte) error { var fields map[string]json.RawMessage if err := json.Unmarshal(raw, &fields); err != nil || fields == nil { return errNotBoard } var out AbilityBoard var abilities map[string]bool for name, into := range map[string]any{"revision": &out.Revision, "admin": &out.Admin, "ask": &out.Ask, "abilities": &abilities} { v, ok := fields[name] if !ok || string(v) == "null" || json.Unmarshal(v, into) != nil { return errNotBoard } } if abilities == nil { return errNotBoard } out.Abilities = map[string]bool{} for k, v := range abilities { if KnownAbility(k) { out.Abilities[k] = v } } *b = out return nil } var errNotBoard = errors.New("wire: not an ability board") // AssistantAbilities is GET /api/v1/assistant/abilities: the caller's own // effective switches (every key), the keys the operator fixed for // everyone, and who can change them. type AssistantAbilities struct { Abilities map[string]bool `json:"abilities"` Fixed []string `json:"fixed"` Ask string `json:"ask"` } // AbilitiesRefreshed is POST /api/v1/assistant/abilities/refresh's 200: // the board's revision Equipo now holds for the person. type AbilitiesRefreshed struct { Revision int64 `json:"revision"` }