| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "slices" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // One function decides what a person's Assistant may do (spec |
| 15 | // 2026-11-26-assistant-abilities §5.5): switches, read in the transaction |
| 16 | // that acts. Can comes from Base over the host channel and decides what |
| 17 | // may happen; Draws comes from Base-Capabilities, an unauthenticated |
| 18 | // header, and only decides presentation. |
| 19 | |
| 20 | // abilitiesExpiry is how long a copy of the board counts after the last |
| 21 | // answer that counted (§5.3). |
| 22 | const abilitiesExpiry = 10 * time.Minute |
| 23 | |
| 24 | // Switches is a person's effective abilities: Base's copy AND the |
| 25 | // operator's fixed keys AND the implications (§3). A missing, revoked or |
| 26 | // expired copy denies everything switched. |
| 27 | type Switches struct { |
| 28 | abilities map[string]bool |
| 29 | admin bool |
| 30 | ask string |
| 31 | caps []string |
| 32 | // known: there is a copy that counts (else every refusal is |
| 33 | // unavailable, not ability_off). |
| 34 | known bool |
| 35 | } |
| 36 | |
| 37 | // Can reports whether the person may use ability. |
| 38 | func (s Switches) Can(ability string) bool { return s.abilities[ability] } |
| 39 | |
| 40 | // Admin reports whether the person administers their Base (the owner): |
| 41 | // what MCP's review gate reads. |
| 42 | func (s Switches) Admin() bool { return s.admin } |
| 43 | |
| 44 | // Draws reports whether the person's Base, and every client they use, |
| 45 | // draws capability: presentation only. |
| 46 | func (s Switches) Draws(capability string) bool { return slices.Contains(s.caps, capability) } |
| 47 | |
| 48 | // Off are the keys that are off, in §3's order. |
| 49 | func (s Switches) Off() []string { |
| 50 | var out []string |
| 51 | for _, k := range wire.Abilities { |
| 52 | if !s.abilities[k] { |
| 53 | out = append(out, k) |
| 54 | } |
| 55 | } |
| 56 | return out |
| 57 | } |
| 58 | |
| 59 | // Ask is who can change the person's switches (Base's owner's name). |
| 60 | func (s Switches) Ask() string { return s.ask } |
| 61 | |
| 62 | // Refusal is the code a refusal under ability gives: ability_off when |
| 63 | // the switch is off, unavailable when there is no copy that counts. |
| 64 | func (s Switches) Refusal(ability string) string { |
| 65 | if !s.known { |
| 66 | return "unavailable" |
| 67 | } |
| 68 | return wire.CodeAbilityOff |
| 69 | } |
| 70 | |
| 71 | // Fixed is the operator's narrowing for everyone (EQUIPO_ABILITIES, |
| 72 | // §5.4): a key off, or only for people who administer their Base. |
| 73 | type Fixed map[string]string |
| 74 | |
| 75 | // The values of a fixed key. |
| 76 | const ( |
| 77 | FixedOff = "off" |
| 78 | FixedOwners = "owners" |
| 79 | ) |
| 80 | |
| 81 | // ParseFixed reads EQUIPO_ABILITIES: comma-separated key=off or |
| 82 | // key=owners entries. An unknown key or value, or a key given twice, is |
| 83 | // an error. |
| 84 | func ParseFixed(raw string) (Fixed, error) { |
| 85 | out := Fixed{} |
| 86 | for _, entry := range strings.Split(raw, ",") { |
| 87 | entry = strings.TrimSpace(entry) |
| 88 | if entry == "" { |
| 89 | continue |
| 90 | } |
| 91 | key, value, ok := strings.Cut(entry, "=") |
| 92 | key, value = strings.TrimSpace(key), strings.TrimSpace(value) |
| 93 | switch { |
| 94 | case !ok || !wire.KnownAbility(key): |
| 95 | return nil, fmt.Errorf("EQUIPO_ABILITIES: unknown ability in %q", entry) |
| 96 | case value != FixedOff && value != FixedOwners: |
| 97 | return nil, fmt.Errorf("EQUIPO_ABILITIES: %q must be off or owners", entry) |
| 98 | case out[key] != "": |
| 99 | return nil, fmt.Errorf("EQUIPO_ABILITIES: %s given twice", key) |
| 100 | } |
| 101 | out[key] = value |
| 102 | } |
| 103 | return out, nil |
| 104 | } |
| 105 | |
| 106 | // SetFixed sets the operator's fixed keys. |
| 107 | func (s *Store) SetFixed(f Fixed) { |
| 108 | c := Fixed{} |
| 109 | for k, v := range f { |
| 110 | c[k] = v |
| 111 | } |
| 112 | s.fixed.Store(&c) |
| 113 | } |
| 114 | |
| 115 | // Fixed is the operator's fixed keys. |
| 116 | func (s *Store) Fixed() Fixed { |
| 117 | if p := s.fixed.Load(); p != nil { |
| 118 | return *p |
| 119 | } |
| 120 | return Fixed{} |
| 121 | } |
| 122 | |
| 123 | // switches reads the person's switches in the caller's transaction: their |
| 124 | // host and its stored board, the operator's fixed keys and the newest |
| 125 | // capabilities. A read error denies, as everything off. |
| 126 | func (t *txn) switches(person string) (Switches, error) { |
| 127 | var h assistantHostRecord |
| 128 | if err := t.db.Where("person = ?", person).Limit(1).Find(&h).Error; err != nil { |
| 129 | return Switches{}, err |
| 130 | } |
| 131 | sw := Switches{abilities: map[string]bool{}, caps: splitCaps(h.Capabilities)} |
| 132 | // The roster first, in this transaction: someone no longer on it, or |
| 133 | // not active, has nothing switched, whatever their copy says (a read |
| 134 | // error denies too). |
| 135 | owner := false |
| 136 | if t.s.authority.Me != nil { |
| 137 | me, err := t.s.authority.Me(t.db, person) |
| 138 | // Me refuses someone removed or no longer active. |
| 139 | if errors.Is(err, ErrNotFound) || errors.Is(err, ErrForbidden) { |
| 140 | return sw, nil |
| 141 | } |
| 142 | if err != nil { |
| 143 | return Switches{}, err |
| 144 | } |
| 145 | owner = me.Role == "owner" |
| 146 | } |
| 147 | if len(t.s.authority.AssistantHosts) == 0 { |
| 148 | // A test instance: the defaults, and Equipo's owners administer. |
| 149 | for _, k := range wire.Abilities { |
| 150 | sw.abilities[k] = true |
| 151 | } |
| 152 | sw.known, sw.admin = true, owner |
| 153 | } else if h.Person != "" && slices.Contains(t.s.authority.AssistantHosts, h.Origin) { |
| 154 | var r abilitiesRecord |
| 155 | if err := t.db.Where("person = ? AND origin = ?", person, h.Origin).Limit(1).Find(&r).Error; err != nil { |
| 156 | return Switches{}, err |
| 157 | } |
| 158 | if r.Person != "" && r.RevokedAt == nil && r.ConfirmedAt != nil && !t.now().After(r.ConfirmedAt.Add(abilitiesExpiry)) { |
| 159 | b, err := r.board() |
| 160 | if err != nil { |
| 161 | return Switches{}, err |
| 162 | } |
| 163 | for _, k := range wire.Abilities { |
| 164 | // A key Base doesn't send is one it doesn't know yet: its |
| 165 | // default, on, as Base itself would have it. |
| 166 | on, sent := b.Abilities[k] |
| 167 | sw.abilities[k] = on || !sent |
| 168 | } |
| 169 | sw.admin, sw.ask, sw.known = b.Admin, b.Ask, true |
| 170 | } |
| 171 | } |
| 172 | for k, v := range t.s.Fixed() { |
| 173 | if v == FixedOff || v == FixedOwners && !sw.admin { |
| 174 | sw.abilities[k] = false |
| 175 | } |
| 176 | } |
| 177 | if !sw.abilities[wire.AbilityAppsRead] { |
| 178 | sw.abilities[wire.AbilityAppsWrite] = false |
| 179 | } |
| 180 | if !sw.abilities[wire.AbilityServices] { |
| 181 | sw.abilities[wire.AbilityServicesAdd] = false |
| 182 | } |
| 183 | return sw, nil |
| 184 | } |
| 185 | |
| 186 | // splitCaps is a Base-Capabilities value's known capabilities, in order, |
| 187 | // each once. |
| 188 | func splitCaps(raw string) []string { |
| 189 | var out []string |
| 190 | for _, c := range strings.Split(raw, ",") { |
| 191 | c = strings.TrimSpace(c) |
| 192 | if (c == wire.CapJobActions || c == wire.CapLookupAnswers) && !slices.Contains(out, c) { |
| 193 | out = append(out, c) |
| 194 | } |
| 195 | } |
| 196 | slices.Sort(out) |
| 197 | return out |
| 198 | } |
| 199 | |
| 200 | // SetCapabilities records the newest Base-Capabilities value the |
| 201 | // person's Base sent (its known capabilities), writing only on change. A |
| 202 | // person who never registered a host gets a row of their own for it, |
| 203 | // which names no host. |
| 204 | func (s *Store) SetCapabilities(ctx context.Context, person, value string) error { |
| 205 | caps := strings.Join(splitCaps(value), ",") |
| 206 | var have []string |
| 207 | if err := s.db.G.WithContext(ctx).Model(&assistantHostRecord{}).Where("person = ?", person).Pluck("coalesce(capabilities, '')", &have).Error; err == nil && len(have) == 1 && have[0] == caps { |
| 208 | return nil |
| 209 | } |
| 210 | err := s.tx(ctx, func(t *txn) error { |
| 211 | var h assistantHostRecord |
| 212 | if err := t.db.Where("person = ?", person).Limit(1).Find(&h).Error; err != nil { |
| 213 | return err |
| 214 | } |
| 215 | if h.Person == "" { |
| 216 | return t.db.Create(&assistantHostRecord{Person: person, Capabilities: caps, UpdatedAt: t.now()}).Error |
| 217 | } |
| 218 | if h.Capabilities == caps { |
| 219 | return nil |
| 220 | } |
| 221 | return t.db.Model(&assistantHostRecord{}).Where("person = ?", person).Update("capabilities", caps).Error |
| 222 | }) |
| 223 | if err == nil { |
| 224 | // What is offered depends on it (web_lookup): listed afresh. |
| 225 | if ops := s.operations(); ops != nil { |
| 226 | ops.Forget(person) |
| 227 | } |
| 228 | } |
| 229 | return err |
| 230 | } |
| 231 | |
| 232 | // ownAbilities is GET /api/v1/assistant/abilities's answer for the |
| 233 | // person: every key, the operator's fixed keys and who to ask. |
| 234 | func (t *txn) ownAbilities(person string) (wire.AssistantAbilities, error) { |
| 235 | sw, err := t.switches(person) |
| 236 | if err != nil { |
| 237 | return wire.AssistantAbilities{}, err |
| 238 | } |
| 239 | out := wire.AssistantAbilities{Abilities: map[string]bool{}, Fixed: []string{}, Ask: sw.ask} |
| 240 | for _, k := range wire.Abilities { |
| 241 | out.Abilities[k] = sw.Can(k) |
| 242 | } |
| 243 | fixed := t.s.Fixed() |
| 244 | for _, k := range wire.Abilities { |
| 245 | if fixed[k] != "" { |
| 246 | out.Fixed = append(out.Fixed, k) |
| 247 | } |
| 248 | } |
| 249 | return out, nil |
| 250 | } |
| 251 | |