oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "strings"
6
7 "amadan.net/oficina/equipo/internal/equipo/assist"
8 "amadan.net/oficina/equipo/internal/wire"
9 "gorm.io/gorm"
10)
11
12// abilityFor is the one map of what every operation Equipo can run needs
13// (spec 2026-11-26-assistant-abilities §6.1): key "" is always on, and ok
14// false is an operation no ability covers, which is never run. A new
15// provider or operation is mapped here or refused.
16func abilityFor(op assist.Operation) (key string, ok bool) {
17 switch {
18 case op.Provider == "web":
19 if op.Name == "web_lookup" {
20 return wire.AbilityWeb, true
21 }
22 case op.Provider == OficinaProvider:
23 if op.ReadOnly {
24 return wire.AbilityAppsRead, true
25 }
26 return wire.AbilityAppsWrite, true
27 case op.Provider == "base":
28 return "", op.ReadOnly && strings.HasPrefix(op.Name, "base_")
29 case op.Provider == teamProvider{}.Name():
30 switch op.Name {
31 case opTeamRequest, "team_add", opTeamReply, "team_ask", opTeamNudge:
32 return wire.AbilityBuild, true
33 case opTeamStatus, opTeamCancel:
34 return "", true
35 case "connect_service":
36 return wire.AbilityServices, true
37 }
38 case strings.HasPrefix(op.Provider, "svc:"):
39 return wire.AbilityServices, true
40 }
41 return "", false
42}
43
44// providerAbility is the ability a whole provider needs, "" when its
45// operations need different ones (or none).
46func providerAbility(name string) string {
47 switch {
48 case name == "web":
49 return wire.AbilityWeb
50 case name == OficinaProvider:
51 // apps_write needs apps_read: off, the whole provider is.
52 return wire.AbilityAppsRead
53 case strings.HasPrefix(name, "svc:"):
54 return wire.AbilityServices
55 }
56 return ""
57}
58
59// abilityFilter is a person's switches as the registry's filter: what
60// look offers and describe finds. noLookups hides web_lookup from a turn
61// the person didn't start.
62type abilityFilter struct {
63 sw Switches
64 noLookups bool
65}
66
67func (f abilityFilter) Provider(name string) bool {
68 key := providerAbility(name)
69 return key == "" || f.sw.Can(key)
70}
71
72func (f abilityFilter) Operation(op assist.Operation) bool {
73 if f.noLookups && op.Provider == webProviderName {
74 return false
75 }
76 key, ok := abilityFor(op)
77 return ok && (key == "" || f.sw.Can(key))
78}
79
80// switchesNow reads the person's switches in a short transaction of its
81// own.
82func (s *Store) switchesNow(ctx context.Context, person string) (sw Switches, err error) {
83 err = s.read(ctx, func(tx *gorm.DB) error {
84 sw, err = (&txn{s: s, ctx: ctx, db: tx}).switches(person)
85 return err
86 })
87 return sw, err
88}
89
90// abilityOffText is the instance's model-facing refusal for an ability
91// that is off (§6.1), filled with its plain name and who to ask.
92func abilityOffText(name, ask string) string {
93 if ask == "" {
94 ask = "the person who runs their Base"
95 }
96 return "Not available: " + name + " is turned off for this person by " + ask + ". Tell them in one short line that you can't do that here and that " + ask + " can turn it on. Don't suggest a way round it."
97}
98
99// toolAbilityUnmapped is the refusal for an operation no ability covers.
100const toolAbilityUnmapped = "Not available: this can't be run from here."
101
102// abilityName is an ability's plain name (Equipo's copy, the same words
103// Base shows).
104func (s *Store) abilityName(key string) string {
105 if s.authority.Copy != nil {
106 if name := s.authority.Copy("assistant.ability." + key + ".name"); name != "" {
107 return name
108 }
109 }
110 return key
111}
112
113// abilityRefusal is the tool result refusing op under key for sw.
114func (s *Store) abilityRefusal(sw Switches, key string) wire.ToolResult {
115 if code := sw.Refusal(key); code != wire.CodeAbilityOff {
116 return refused(code, toolOpUnavailable)
117 }
118 return refused(wire.CodeAbilityOff, abilityOffText(s.abilityName(key), sw.Ask()))
119}
120
121// AllowedFor is Allowed for one ability, as a check a provider is given
122// to run before its authenticated requests.
123func (s *Store) AllowedFor(ability string) func(ctx context.Context, person string) error {
124 return func(ctx context.Context, person string) error { return s.Allowed(ctx, person, ability) }
125}
126
127// toolOperationChanged is the refusal for an operation that resolved
128// differently at send from the one checked.
129const toolOperationChanged = "Not available: the operation changed before it could be sent. Look it up again."
130
131// sendGate is the gate bound to the person and the operation runRemote
132// checked (bound): one short transaction reads the person's switches, and
133// it refuses an operation that resolved differently in provider, name,
134// read-only or scope (operation_changed), one no ability covers, and one
135// whose ability is now off.
136func (s *Store) sendGate(ctx context.Context, person string, bound assist.Operation) assist.GateFunc {
137 return func(op assist.Operation) error {
138 if op.Provider != bound.Provider || op.Name != bound.Name || op.ReadOnly != bound.ReadOnly || op.Scope != bound.Scope {
139 return assist.Refusal{Code: wire.CodeOperationChanged, Message: toolOperationChanged}
140 }
141 key, ok := abilityFor(op)
142 if !ok {
143 return assist.Refusal{Code: wire.CodeAbilityUnmapped, Message: toolAbilityUnmapped}
144 }
145 if key == "" {
146 return nil
147 }
148 sw, err := s.switchesNow(ctx, person)
149 if err != nil {
150 return assist.Refusal{Code: "unavailable", Message: toolOpUnavailable}
151 }
152 if !sw.Can(key) {
153 res := s.abilityRefusal(sw, key)
154 return assist.Refusal{Code: res.Code, Message: res.Text}
155 }
156 return nil
157 }
158}
159