oficina / base Public

Equipo client: lookups on a message, cleaned and bounded

Message gains Lookups (answer and sources). Clean keeps them only on an
assistant's message: answers up to 1,200 characters, https sources only,
at most 5 per lookup with titles cut to 120 characters, at most 2
lookups. PlainLookups folds them into the text for clients that can't
draw them. AbilityKeys pins the seven keys in the spec's order.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 0ef8b7cbd5a328c7d876187351286d97cc04c6bc parent d88194e
6 files changed, +219 −4
  • internal/equipo/abilities.go +5 −0
  • internal/equipo/chat.go +4 −1
  • internal/equipo/files.go +4 −2
  • internal/equipo/lookups.go +111 −0
  • internal/equipo/lookups_test.go +93 −0
  • internal/web/ask_relay.go +2 −1
diff --git a/internal/equipo/abilities.go b/internal/equipo/abilities.go
index 0ec51ad..c8114bf 100644
--- a/internal/equipo/abilities.go
+++ b/internal/equipo/abilities.go
@@ -15,6 +15,11 @@ const CapLookupAnswers = "lookup_answers"
// sent for a member only while each of their clients can draw it.
var KnownCapabilities = []string{CapLookupAnswers}
+// AbilityKeys are what the Base owner can turn off for a member, in the
+// order the person page lists them. Equipo's internal/wire/abilities.go
+// has the same list; Base can't import it.
+var AbilityKeys = []string{"web", "apps_read", "apps_write", "build", "go_live", "services", "services_add"}
+
// CapabilitiesHeader is the header that carries them on every call.
const CapabilitiesHeader = "Base-Capabilities"
diff --git a/internal/equipo/chat.go b/internal/equipo/chat.go
index f8e4aaa..ae126ce 100644
--- a/internal/equipo/chat.go
+++ b/internal/equipo/chat.go
@@ -30,7 +30,9 @@ func InFlight(state string) bool {
// Message is one line of the conversation; Role is person or assistant.
// Attachments are the files the person sent with it, in their order
-// (Clean keeps those Base passes on).
+// (Clean keeps those Base passes on); Lookups, on the Assistant's reply,
+// what it looked up on the web for it, in the order it did (Clean keeps
+// them within bounds).
type Message struct {
ID string `json:"id"`
Role string `json:"role"`
@@ -38,6 +40,7 @@ type Message struct {
TurnID string `json:"turn_id"`
CreatedAt time.Time `json:"created_at"`
Attachments []Attachment `json:"attachments,omitempty"`
+ Lookups []Lookup `json:"lookups,omitempty"`
}
// Turn is the Assistant's answer to the person's messages.
diff --git a/internal/equipo/files.go b/internal/equipo/files.go
index 50637f4..0f54582 100644
--- a/internal/equipo/files.go
+++ b/internal/equipo/files.go
@@ -37,8 +37,9 @@ func ValidAttachmentID(id string) bool { return attachmentID.MatchString(id) }
var attachmentID = regexp.MustCompile(`^at_[a-z0-9]{8,60}$`)
-// Clean is the message as Base passes it on: only the attachments whose id
-// it would call Equipo with, each with a kind Base draws (an unknown kind
+// Clean is the message as Base passes it on: its lookups within bounds
+// (cleanLookups), and only the attachments whose id it would call Equipo
+// with, each with a kind Base draws (an unknown kind
// is a file), a name of valid UTF-8 (at most 255 bytes; "file" when
// empty), and no negative size or side. Nil when none are left, so a
// message without files is what it always was.
@@ -63,6 +64,7 @@ func (m Message) Clean() Message {
out = append(out, a)
}
m.Attachments = out
+ m.Lookups = cleanLookups(m.Role, m.Lookups)
return m
}
diff --git a/internal/equipo/lookups.go b/internal/equipo/lookups.go
new file mode 100644
index 0000000..90f3da8
--- /dev/null
+++ b/internal/equipo/lookups.go
@@ -0,0 +1,111 @@
+package equipo
+
+import (
+ "net/url"
+ "strings"
+ "unicode/utf8"
+)
+
+// Lookup is one thing the Assistant looked up on the web for a reply: a
+// short answer, and the pages it came from. Equipo sends a message's
+// lookups only to a Base that said it draws them (CapLookupAnswers).
+type Lookup struct {
+ Answer string `json:"answer"`
+ Sources []Source `json:"sources"`
+}
+
+// Source is a page a lookup's answer came from.
+type Source struct {
+ URL string `json:"url"`
+ Title string `json:"title"`
+}
+
+// The bounds Clean keeps a message's lookups to: an answer of at most
+// MaxLookupAnswer characters (a longer one is dropped with its lookup), at
+// most MaxLookupSources sources each, titles cut to MaxLookupTitle
+// characters, and at most MaxLookups lookups.
+const (
+ MaxLookupAnswer = 1200
+ MaxLookupSources = 5
+ MaxLookupTitle = 120
+ MaxLookups = 2
+ // maxSourceURL bounds a source's address, in bytes.
+ maxSourceURL = 2 << 10
+)
+
+// fallbackSources heads a lookup's addresses in the plain text a client
+// that can't draw lookups gets (copy ask.fallback_sources, approved by
+// Paul, 2026-11-28, copy review mi3kgi7nubnsf7kxn235tqwrbu).
+const fallbackSources = "Sources:"
+
+// cleanLookups is lookups as Base passes them on (Message.Clean): only an
+// assistant's message has any; each answer is valid UTF-8, not blank and
+// at most MaxLookupAnswer characters; each source an absolute https
+// address (sourceURL) with its title cut to MaxLookupTitle characters; a
+// lookup left without a source is dropped. Nil when none are left.
+func cleanLookups(role string, lookups []Lookup) []Lookup {
+ if role != "assistant" {
+ return nil
+ }
+ var out []Lookup
+ for _, l := range lookups {
+ if len(out) == MaxLookups {
+ break
+ }
+ answer := strings.ToValidUTF8(l.Answer, "")
+ if strings.TrimSpace(answer) == "" || utf8.RuneCountInString(answer) > MaxLookupAnswer {
+ continue
+ }
+ var sources []Source
+ for _, s := range l.Sources {
+ if len(sources) == MaxLookupSources {
+ break
+ }
+ u := sourceURL(s.URL)
+ if u == "" {
+ continue
+ }
+ title := []rune(strings.ToValidUTF8(s.Title, ""))
+ if len(title) > MaxLookupTitle {
+ title = title[:MaxLookupTitle]
+ }
+ sources = append(sources, Source{URL: u, Title: string(title)})
+ }
+ if len(sources) > 0 {
+ out = append(out, Lookup{Answer: answer, Sources: sources})
+ }
+ }
+ return out
+}
+
+// sourceURL is raw if it is an absolute https address with a host and no
+// user info, of at most maxSourceURL bytes, else "".
+func sourceURL(raw string) string {
+ if len(raw) > maxSourceURL {
+ return ""
+ }
+ u := WebURL(raw)
+ if p, err := url.Parse(u); u == "" || err != nil || p.Scheme != "https" {
+ return ""
+ }
+ return u
+}
+
+// PlainLookups is m for a client that can't draw lookups: each answer
+// folded into the text after the message's own words, followed by
+// "Sources:" and its sources' addresses, one per line. Lookups is nil.
+func (m Message) PlainLookups() Message {
+ parts := []string{}
+ if m.Text != "" {
+ parts = append(parts, m.Text)
+ }
+ for _, l := range m.Lookups {
+ urls := make([]string, 0, len(l.Sources))
+ for _, s := range l.Sources {
+ urls = append(urls, s.URL)
+ }
+ parts = append(parts, l.Answer, fallbackSources+"\n"+strings.Join(urls, "\n"))
+ }
+ m.Text, m.Lookups = strings.Join(parts, "\n\n"), nil
+ return m
+}
diff --git a/internal/equipo/lookups_test.go b/internal/equipo/lookups_test.go
new file mode 100644
index 0000000..98b806e
--- /dev/null
+++ b/internal/equipo/lookups_test.go
@@ -0,0 +1,93 @@
+package equipo_test
+
+import (
+ "encoding/json"
+ "reflect"
+ "strings"
+ "testing"
+ "unicode/utf8"
+
+ "amadan.net/oficina/base/internal/equipo"
+)
+
+func src(n int) equipo.Source {
+ return equipo.Source{URL: "https://site.test/" + strings.Repeat("p", n), Title: "Page"}
+}
+
+// Clean keeps an answer of at most 1,200 characters and drops a longer
+// one, keeps at most 5 sources and 2 lookups, and cuts a title to 120
+// characters at a character boundary.
+func TestCleanLookupBounds(t *testing.T) {
+ long := strings.Repeat("é", equipo.MaxLookupAnswer)
+ m := equipo.Message{ID: "m1", Role: "assistant", Lookups: []equipo.Lookup{
+ {Answer: long, Sources: []equipo.Source{src(1), src(2), src(3), src(4), src(5), src(6)}},
+ {Answer: long + "x", Sources: []equipo.Source{src(1)}},
+ {Answer: "Second", Sources: []equipo.Source{{URL: "https://b.test/", Title: strings.Repeat("ü", equipo.MaxLookupTitle+1)}}},
+ {Answer: "Third", Sources: []equipo.Source{src(1)}},
+ {Answer: "Fourth", Sources: []equipo.Source{src(1)}},
+ }}.Clean()
+ if len(m.Lookups) != equipo.MaxLookups {
+ t.Fatalf("%d lookups kept, want %d: %+v", len(m.Lookups), equipo.MaxLookups, m.Lookups)
+ }
+ if m.Lookups[0].Answer != long || len(m.Lookups[0].Sources) != equipo.MaxLookupSources {
+ t.Errorf("first lookup: %d characters, %d sources", utf8.RuneCountInString(m.Lookups[0].Answer), len(m.Lookups[0].Sources))
+ }
+ if m.Lookups[1].Answer != "Second" {
+ t.Errorf("the answer of 1,201 characters was kept: %q", m.Lookups[1].Answer[:20])
+ }
+ title := m.Lookups[1].Sources[0].Title
+ if !utf8.ValidString(title) || utf8.RuneCountInString(title) != equipo.MaxLookupTitle {
+ t.Errorf("title cut to %d characters (valid %v)", utf8.RuneCountInString(title), utf8.ValidString(title))
+ }
+ // A person's message carries none, and a message without any is what
+ // it always was.
+ if p := (equipo.Message{Role: "person", Lookups: []equipo.Lookup{{Answer: "x", Sources: []equipo.Source{src(1)}}}}).Clean(); p.Lookups != nil {
+ t.Errorf("a person's message kept lookups: %+v", p.Lookups)
+ }
+ raw, _ := json.Marshal(equipo.Message{ID: "m2", Role: "assistant"}.Clean())
+ if strings.Contains(string(raw), "lookups") {
+ t.Errorf("a message without lookups: %s", raw)
+ }
+}
+
+// Only https sources are kept; a lookup left without any is dropped, as
+// is one with an empty answer.
+func TestCleanLookupHTTPSOnly(t *testing.T) {
+ m := equipo.Message{Role: "assistant", Lookups: []equipo.Lookup{
+ {Answer: "Kept", Sources: []equipo.Source{{URL: "http://plain.test/", Title: "a"}, {URL: "javascript:alert(1)", Title: "b"},
+ {URL: "/relative", Title: "c"}, {URL: "https://user:pw@x.test/", Title: "d"}, {URL: "https://ok.test/a?b=1", Title: "e"}}},
+ {Answer: "No sources", Sources: []equipo.Source{{URL: "http://plain.test/"}}},
+ {Answer: " ", Sources: []equipo.Source{src(1)}},
+ }}.Clean()
+ want := []equipo.Lookup{{Answer: "Kept", Sources: []equipo.Source{{URL: "https://ok.test/a?b=1", Title: "e"}}}}
+ if !reflect.DeepEqual(m.Lookups, want) {
+ t.Errorf("lookups %+v, want %+v", m.Lookups, want)
+ }
+}
+
+// PlainLookups folds each answer and its sources' addresses into the
+// text, for a client that can't draw them.
+func TestPlainLookupsFolds(t *testing.T) {
+ m := equipo.Message{Role: "assistant", Text: "Here you go.", Lookups: []equipo.Lookup{
+ {Answer: "Rain later.", Sources: []equipo.Source{{URL: "https://met.test/dublin", Title: "Met"}, {URL: "https://b.test/", Title: "B"}}},
+ {Answer: "Open at 10.", Sources: []equipo.Source{{URL: "https://museum.test/", Title: "Museum"}}},
+ }}.PlainLookups()
+ want := "Here you go.\n\nRain later.\n\nSources:\nhttps://met.test/dublin\nhttps://b.test/\n\nOpen at 10.\n\nSources:\nhttps://museum.test/"
+ if m.Text != want || m.Lookups != nil {
+ t.Errorf("folded %q, lookups %v\nwant %q", m.Text, m.Lookups, want)
+ }
+ if got := (equipo.Message{Role: "assistant", Lookups: []equipo.Lookup{{Answer: "A.", Sources: []equipo.Source{src(1)}}}}).PlainLookups(); got.Text != "A.\n\nSources:\nhttps://site.test/p" {
+ t.Errorf("no words of its own: %q", got.Text)
+ }
+ if got := (equipo.Message{Text: "Plain"}).PlainLookups(); got.Text != "Plain" {
+ t.Errorf("no lookups: %q", got.Text)
+ }
+}
+
+// The ability keys, in the spec's order (§3).
+func TestAbilityKeysMatchSpec(t *testing.T) {
+ want := []string{"web", "apps_read", "apps_write", "build", "go_live", "services", "services_add"}
+ if !reflect.DeepEqual(equipo.AbilityKeys, want) {
+ t.Errorf("AbilityKeys %v, want %v", equipo.AbilityKeys, want)
+ }
+}
diff --git a/internal/web/ask_relay.go b/internal/web/ask_relay.go
index f2c680c..70aa709 100644
--- a/internal/web/ask_relay.go
+++ b/internal/web/ask_relay.go
@@ -179,7 +179,8 @@ func (a *app) askAnswer(w http.ResponseWriter, r *http.Request) {
}
out.Card, _ = in.Card.CleanAt(a.cfg.Origin)
if in.Message.ID != "" {
- out.Message = &in.Message
+ m := in.Message.Clean()
+ out.Message = &m
}
if in.Turn.ID != "" {
out.Turn = &in.Turn