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 "encoding/json"
6 "strings"
7 "sync"
8 "sync/atomic"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/equipo/assist"
12 "amadan.net/oficina/equipo/internal/websearch"
13 "amadan.net/oficina/equipo/internal/websource/guard"
14 "amadan.net/oficina/equipo/internal/wire"
15 "gorm.io/gorm"
16)
17
18// The web provider (spec 2026-11-26-assistant-abilities §4.1): one
19// read-only operation, web_lookup, with an empty input. The Assistant
20// says that something should be looked up, never what: the lookup works
21// from the person's own words, and its answer is shown to them, never
22// handed back.
23
24const (
25 webProviderName = "web"
26 opWebLookup = "web_lookup"
27)
28
29// webLookupOp is the one operation. Its description is for the model.
30var webLookupOp = assist.Operation{Provider: webProviderName, Name: opWebLookup, App: "Web", Scope: "web", ReadOnly: true, Idempotent: true,
31 Description: "Look something up on the public web for the person: weather, opening hours, news, prices, facts. Takes no input: it works from what the person asked. The answer is shown to them under your reply.",
32 Input: json.RawMessage(`{"type":"object","additionalProperties":false}`),
33 Output: json.RawMessage(`{"type":"object","properties":{"code":{"type":"string"},"sources":{"type":"integer"},"text":{"type":"string"}}}`)}
34
35// LookupConfig is what lookups run on: the search chain (FromConfig's,
36// usable), the page client (PolicyLookupPage, 5 redirects), the
37// inventory, and the hosts no lookup may reach whoever the person is (the
38// instance, every Assistant host, oficina-connect, the forge).
39type LookupConfig struct {
40 Search websearch.Chain
41 Pages *guard.Client
42 Inventory guard.Inventory
43 Deny []string
44}
45
46// lookupState is the store's lookup configuration and its in-memory
47// state: the per-host pace and the deny lists per person.
48type lookupState struct {
49 cfg atomic.Pointer[LookupConfig]
50 pace hostPacer
51 mu sync.Mutex
52 deny map[string]denyEntry
53}
54
55type denyEntry struct {
56 hosts []string
57 at time.Time
58}
59
60// SetLookups configures lookups; nil (no usable backend) leaves
61// web_lookup unlisted.
62func (s *Store) SetLookups(c *LookupConfig) { s.lookups.cfg.Store(c) }
63
64// WebProvider is the web provider, registered after the team's.
65func (s *Store) WebProvider() assist.Provider { return webProvider{s} }
66
67type webProvider struct{ s *Store }
68
69func (webProvider) Name() string { return webProviderName }
70
71// List offers web_lookup only when a lookup can run for the person now
72// (§4.2): a usable backend, the inventory ready, their primary at
73// protocol 36 or later, their Base drawing answers (lookup_answers). It
74// never depends on the call (the registry caches it per person): a turn
75// the person didn't start is filtered per call (abilityFilter), and
76// Invoke checks every condition again.
77func (p webProvider) List(ctx context.Context, person string) ([]assist.Operation, error) {
78 if p.s.lookupUnsupported(ctx, person) != "" {
79 return nil, nil
80 }
81 return []assist.Operation{webLookupOp}, nil
82}
83
84func (p webProvider) Invoke(ctx context.Context, person, operation string, input json.RawMessage, key string) (json.RawMessage, error) {
85 call, ok := callFrom(ctx)
86 if !ok || call.Person != person {
87 return nil, assist.Failure{Code: "unavailable"}
88 }
89 if operation != opWebLookup {
90 return nil, assist.Failure{Code: "unknown_operation"}
91 }
92 if in := strings.TrimSpace(string(input)); in != "" && in != "{}" && in != "null" {
93 return nil, assist.Failure{Code: "invalid_input", Message: toolLookupInput}
94 }
95 return p.s.webLookup(ctx, call)
96}
97
98// lookupUnsupported is why a lookup can't run for person now, "" when it
99// can.
100func (s *Store) lookupUnsupported(ctx context.Context, person string) string {
101 cfg := s.lookups.cfg.Load()
102 if cfg == nil || cfg.Pages == nil || cfg.Search.Primary == nil || cfg.Inventory == nil {
103 return "no_backend"
104 }
105 if cfg.Inventory.Ready(ctx) != nil {
106 return "inventory"
107 }
108 sw, err := s.switchesNow(ctx, person)
109 if err != nil || !sw.Draws(wire.CapLookupAnswers) {
110 return "client"
111 }
112 var runner string
113 if err := s.read(ctx, func(tx *gorm.DB) error {
114 var err error
115 runner, err = s.authority.PrimaryRunner(tx, person)
116 return err
117 }); err != nil || runner == "" {
118 return "runner"
119 }
120 s.live.mu.Lock()
121 c := s.live.conns[runner]
122 s.live.mu.Unlock()
123 if c == nil || c.protocol < wire.ProtocolLookup {
124 return "runner"
125 }
126 return ""
127}
128
129// turnKind is the call's turn's kind.
130func (s *Store) turnKind(ctx context.Context, call toolCallRecord) (string, error) {
131 var kinds []string
132 err := s.read(ctx, func(tx *gorm.DB) error {
133 return tx.Model(&turnRecord{}).Where("id = ? AND person = ?", call.TurnID, call.Person).Pluck("coalesce(kind, '')", &kinds).Error
134 })
135 if err != nil || len(kinds) != 1 {
136 return "", err
137 }
138 return kinds[0], nil
139}
140
141// hostPacer keeps requests to any one host at least a second apart.
142type hostPacer struct {
143 mu sync.Mutex
144 next map[string]time.Time
145}
146
147// wait is host's next slot, taken, then waited for (or ctx's end).
148func (p *hostPacer) wait(ctx context.Context, host string) error {
149 p.mu.Lock()
150 if p.next == nil {
151 p.next = map[string]time.Time{}
152 }
153 now := time.Now()
154 at := p.next[host]
155 if at.Before(now) {
156 at = now
157 }
158 p.next[host] = at.Add(time.Second)
159 p.mu.Unlock()
160 if d := time.Until(at); d > 0 {
161 t := time.NewTimer(d)
162 defer t.Stop()
163 select {
164 case <-ctx.Done():
165 return ctx.Err()
166 case <-t.C:
167 }
168 }
169 return nil
170}
171