| 1 | package assist |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "net/http" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // Connection is a person's connection to oficina-connect: where it is |
| 13 | // and a current access token (not ok when the person has none), and |
| 14 | // Rejected, told when connect refused a token so the next Host gets a |
| 15 | // fresh one. |
| 16 | type Connection interface { |
| 17 | Host(ctx context.Context, person string) (origin, token string, ok bool, err error) |
| 18 | Rejected(person string) |
| 19 | } |
| 20 | |
| 21 | // Hinter is a provider with something to say in look while the person |
| 22 | // has not connected it: ok when query is something it could do. |
| 23 | type Hinter interface { |
| 24 | Hint(query string) (text string, ok bool) |
| 25 | } |
| 26 | |
| 27 | // oficinaHint is look's model-facing line for the Oficina apps while the |
| 28 | // person has not connected them. |
| 29 | const oficinaHint = "- not connected: once the person connects their Oficina apps you can use Calendar, Docs, Sheets, Slides, Memoria notes and Calls for them." |
| 30 | |
| 31 | // oficinaWords are what the Oficina apps do, for matching look's query |
| 32 | // while nothing is listed. |
| 33 | var oficinaWords = []string{"oficina", "calendar", "event", "meeting", "schedule", "free", "busy", "agenda", "doc", "document", "write", "sheet", "spreadsheet", "table", "slide", "deck", "presentation", "note", "memoria", "call", "video"} |
| 34 | |
| 35 | // oficinaProvider is the Oficina apps through oficina-connect: one |
| 36 | // provider for every app the person's connection reaches. It speaks the |
| 37 | // HTTP provider's contract to connect's own /api/v1, and adds what |
| 38 | // agent-access leaves to the client: the app (from the scope), the |
| 39 | // client_id key (agent-access's idempotency convention) and the team. |
| 40 | type oficinaProvider struct { |
| 41 | http *httpProvider |
| 42 | conn Connection |
| 43 | } |
| 44 | |
| 45 | // NewOficinaProvider is the provider for the Oficina apps behind conn. |
| 46 | // Only here is a write's client_id inferred as its key (clientIDKeyed): |
| 47 | // other HTTP providers keep needing an explicit one. allowed, when set, |
| 48 | // is asked before every authenticated request (the connection read, a |
| 49 | // token and its refresh, listing, an operation): with reading apps off |
| 50 | // for the person, the connection is kept but never used (spec |
| 51 | // 2026-11-26-assistant-abilities §6.1). |
| 52 | func NewOficinaProvider(name string, conn Connection, client *http.Client, allowed func(ctx context.Context, person string) error) Provider { |
| 53 | host := conn.Host |
| 54 | if allowed != nil { |
| 55 | host = func(ctx context.Context, person string) (string, string, bool, error) { |
| 56 | if err := allowed(ctx, person); err != nil { |
| 57 | return "", "", false, err |
| 58 | } |
| 59 | return conn.Host(ctx, person) |
| 60 | } |
| 61 | } |
| 62 | h := newHTTPProvider(name, host, client) |
| 63 | // Checked again right before each request: a token refresh may |
| 64 | // have taken long enough for the switch to go off. |
| 65 | h.beforeSend = allowed |
| 66 | return &oficinaProvider{http: h, conn: conn} |
| 67 | } |
| 68 | |
| 69 | func (p *oficinaProvider) Name() string { return p.http.name } |
| 70 | |
| 71 | // again makes call, and once more when connect refused the token. |
| 72 | func again[T any](p *oficinaProvider, person string, call func() (T, error)) (T, error) { |
| 73 | out, err := call() |
| 74 | var f Failure |
| 75 | if errors.As(err, &f) && f.Code == "unauthenticated" { |
| 76 | p.conn.Rejected(person) |
| 77 | out, err = call() |
| 78 | } |
| 79 | return out, err |
| 80 | } |
| 81 | |
| 82 | func (p *oficinaProvider) List(ctx context.Context, person string) ([]Operation, error) { |
| 83 | ops, err := again(p, person, func() ([]Operation, error) { return p.http.List(ctx, person) }) |
| 84 | if err != nil { |
| 85 | return nil, err |
| 86 | } |
| 87 | teams, err := p.teams(ctx, person) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | for i := range ops { |
| 92 | op := &ops[i] |
| 93 | if op.App == "" { |
| 94 | op.App = appOf(op.Scope) |
| 95 | } |
| 96 | op.KeyField, op.Teams = "", nil |
| 97 | if clientIDKeyed(*op) { |
| 98 | op.KeyField = "client_id" |
| 99 | op.Input = schemaWithout(op.Input, "client_id", true) |
| 100 | } |
| 101 | if schemaRequires(op.Input, "team") { |
| 102 | if len(teams) == 1 { |
| 103 | op.Input = schemaWithout(op.Input, "team", false) |
| 104 | } else if len(teams) > 1 { |
| 105 | op.Teams = teams |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | return ops, nil |
| 110 | } |
| 111 | |
| 112 | // Invoke fills the team when the model gave none and the connection |
| 113 | // has exactly one; with more, the model must choose, and nothing is sent. |
| 114 | func (p *oficinaProvider) Invoke(ctx context.Context, person, operation string, input json.RawMessage, key string) (json.RawMessage, error) { |
| 115 | fields := map[string]json.RawMessage{} |
| 116 | if t := strings.TrimSpace(string(input)); t != "" && t != "null" { |
| 117 | if err := json.Unmarshal(input, &fields); err != nil { |
| 118 | return nil, Failure{Code: "invalid_input", Message: "The input must be a JSON object."} |
| 119 | } |
| 120 | } |
| 121 | if _, has := fields["team"]; !has { |
| 122 | teams, err := p.teams(ctx, person) |
| 123 | if err != nil { |
| 124 | return nil, err |
| 125 | } |
| 126 | switch len(teams) { |
| 127 | case 0: |
| 128 | case 1: |
| 129 | fields["team"], _ = json.Marshal(teams[0]) |
| 130 | default: |
| 131 | return nil, Failure{Code: "team_needed", Message: "Choose the team for this in team: one of " + strings.Join(teams, ", ") + "."} |
| 132 | } |
| 133 | } |
| 134 | in, err := json.Marshal(fields) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | return again(p, person, func() (json.RawMessage, error) { return p.http.Invoke(ctx, person, operation, in, key) }) |
| 139 | } |
| 140 | |
| 141 | // teams are the connection's teams, as connect reports them. |
| 142 | func (p *oficinaProvider) teams(ctx context.Context, person string) ([]string, error) { |
| 143 | body, err := again(p, person, func() ([]byte, error) { |
| 144 | return p.http.do(ctx, person, http.MethodGet, "/api/v1/connection", nil, "", maxBody, nil) |
| 145 | }) |
| 146 | if err != nil { |
| 147 | return nil, err |
| 148 | } |
| 149 | var c struct { |
| 150 | Teams []string `json:"teams"` |
| 151 | } |
| 152 | if err := json.Unmarshal(body, &c); err != nil { |
| 153 | return nil, Failure{Code: "bad_response", Message: "The provider's connection was not understood."} |
| 154 | } |
| 155 | return c.Teams, nil |
| 156 | } |
| 157 | |
| 158 | // Hint is the provider's line while the person is not connected: for an |
| 159 | // empty query, or one about something the apps do. |
| 160 | func (p *oficinaProvider) Hint(query string) (string, bool) { |
| 161 | q := strings.ToLower(strings.TrimSpace(query)) |
| 162 | if q == "" { |
| 163 | return oficinaHint, true |
| 164 | } |
| 165 | for _, w := range oficinaWords { |
| 166 | if strings.Contains(q, w) || len(q) >= 3 && strings.Contains(w, q) { |
| 167 | return oficinaHint, true |
| 168 | } |
| 169 | } |
| 170 | return "", false |
| 171 | } |
| 172 | |
| 173 | // appOf is an app's name from an operation's scope: "calendar:write" is |
| 174 | // Calendar. |
| 175 | func appOf(scope string) string { |
| 176 | app, _, _ := strings.Cut(scope, ":") |
| 177 | if app == "" { |
| 178 | return "" |
| 179 | } |
| 180 | return strings.ToUpper(app[:1]) + app[1:] |
| 181 | } |
| 182 | |
| 183 | // schema is the part of a JSON Schema object the inference reads. |
| 184 | type schema struct { |
| 185 | Required []string `json:"required"` |
| 186 | Properties map[string]json.RawMessage `json:"properties"` |
| 187 | } |
| 188 | |
| 189 | // clientIDKeyed: agent-access's idempotency convention, read from the |
| 190 | // descriptor: an idempotent write whose input requires client_id, a |
| 191 | // string that holds a UUID (format uuid, or at most 36 characters, the |
| 192 | // length the apps declare). |
| 193 | func clientIDKeyed(op Operation) bool { |
| 194 | if op.ReadOnly || !op.Idempotent { |
| 195 | return false |
| 196 | } |
| 197 | var s schema |
| 198 | if json.Unmarshal(op.Input, &s) != nil || !slices.Contains(s.Required, "client_id") { |
| 199 | return false |
| 200 | } |
| 201 | var prop struct { |
| 202 | Type string `json:"type"` |
| 203 | Format string `json:"format"` |
| 204 | MaxLength *int `json:"maxLength"` |
| 205 | } |
| 206 | if json.Unmarshal(s.Properties["client_id"], &prop) != nil || prop.Type != "string" { |
| 207 | return false |
| 208 | } |
| 209 | return prop.Format == "uuid" || prop.MaxLength != nil && *prop.MaxLength == 36 |
| 210 | } |
| 211 | |
| 212 | func schemaRequires(raw json.RawMessage, field string) bool { |
| 213 | var s schema |
| 214 | return json.Unmarshal(raw, &s) == nil && slices.Contains(s.Required, field) |
| 215 | } |
| 216 | |
| 217 | // schemaWithout is raw with field no longer required, and, with drop, |
| 218 | // no longer a property either: what the instance fills in itself. Raw |
| 219 | // it cannot read is returned as it is. |
| 220 | func schemaWithout(raw json.RawMessage, field string, drop bool) json.RawMessage { |
| 221 | var s map[string]json.RawMessage |
| 222 | if json.Unmarshal(raw, &s) != nil { |
| 223 | return raw |
| 224 | } |
| 225 | var required []string |
| 226 | if json.Unmarshal(s["required"], &required) == nil && s["required"] != nil { |
| 227 | required = slices.DeleteFunc(required, func(r string) bool { return r == field }) |
| 228 | s["required"], _ = json.Marshal(required) |
| 229 | } |
| 230 | if drop { |
| 231 | var props map[string]json.RawMessage |
| 232 | if json.Unmarshal(s["properties"], &props) == nil && props != nil { |
| 233 | delete(props, field) |
| 234 | s["properties"], _ = json.Marshal(props) |
| 235 | } |
| 236 | } |
| 237 | out, err := json.Marshal(s) |
| 238 | if err != nil { |
| 239 | return raw |
| 240 | } |
| 241 | return out |
| 242 | } |
| 243 | |