oficina / base Public

Fake Equipo: replies with lookups and the plain fallback

ReplyWithLookups answers a turn with lookups on the reply. The fake
serves them only to calls carrying Base-Capabilities: lookup_answers, and
folds them into the text otherwise, in the snapshot, the messages page
and the event stream, as Equipo's fallback does. Tested in
internal/equipo with the fake's other tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev ca47e98e11be4472133fb980f8a91de9ca03280d parent 0ef8b7c
2 files changed, +118 −5
  • internal/equipo/equipotest/chat.go +56 −5
  • internal/equipo/lookups_test.go +62 −0
diff --git a/internal/equipo/equipotest/chat.go b/internal/equipo/equipotest/chat.go
index 9169a5c..3c5514b 100644
--- a/internal/equipo/equipotest/chat.go
+++ b/internal/equipo/equipotest/chat.go
@@ -32,6 +32,43 @@ type message struct {
// Attachments are the files sent with a person's message, in order;
// omitted when there are none (wire.Message).
Attachments []file `json:"attachments,omitempty"`
+ // Lookups are what the Assistant looked up for a reply, sent only to
+ // a Base that draws them (asRead).
+ Lookups []equipo.Lookup `json:"lookups,omitempty"`
+}
+
+// drawsLookups reports whether r says Base draws lookup answers
+// (Base-Capabilities), as Equipo reads it.
+func drawsLookups(r *http.Request) bool {
+ for _, v := range r.Header.Values(equipo.CapabilitiesHeader) {
+ for _, c := range strings.Split(v, ",") {
+ if strings.TrimSpace(c) == equipo.CapLookupAnswers {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+// asRead is m as a read with draws gets it: its lookups as they are, or,
+// for a Base that can't draw them, folded into its text, as Equipo's
+// plain-text fallback does (computed at read time, never stored).
+func asRead(m message, draws bool) message {
+ if draws || len(m.Lookups) == 0 {
+ return m
+ }
+ m.Text = equipo.Message{Text: m.Text, Lookups: m.Lookups}.PlainLookups().Text
+ m.Lookups = nil
+ return m
+}
+
+// read is msgs as a read with draws gets them (asRead).
+func read(msgs []message, draws bool) []message {
+ out := make([]message, len(msgs))
+ for i, m := range msgs {
+ out[i] = asRead(m, draws)
+ }
+ return out
}
type turn struct {
@@ -390,7 +427,7 @@ func (f *Fake) serveChat(w http.ResponseWriter, r *http.Request) {
f.snapshot(w, r)
case r.Method == http.MethodGet && p == "/messages":
f.cmu.Lock()
- writeJSON(w, 200, map[string]any{"items": f.tail(), "before": ""})
+ writeJSON(w, 200, map[string]any{"items": read(f.tail(), drawsLookups(r)), "before": ""})
f.cmu.Unlock()
case r.Method == http.MethodPost && p == "/messages":
f.send(w, r, d.id)
@@ -445,7 +482,7 @@ func (f *Fake) snapshot(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{
"me": map[string]any{"id": f.address, "address": f.address, "role": "owner",
"person": map[string]any{"name": "Paul"}, "team_id": f.Instance, "team_name": "Tito"},
- "messages": f.tail(), "turns": turns, "cards": cards, "cursor": f.cursor,
+ "messages": read(f.tail(), drawsLookups(r)), "turns": turns, "cards": cards, "cursor": f.cursor,
"available": f.runner == "", "can_attach": f.canAttach(),
})
}
@@ -600,7 +637,17 @@ func (f *Fake) answerTurn(id, text string) {
// Answer answers the running turn with id (one Hold kept running): with
// deltas, the reply streamed in word by word first (Stream); then the
// reply message, and the turn replied.
-func (f *Fake) Answer(id, reply string, deltas bool) {
+func (f *Fake) Answer(id, reply string, deltas bool) { f.answerWith(id, reply, deltas, nil) }
+
+// ReplyWithLookups answers the running turn with id as Answer does (its
+// words streamed first), with lookups on the reply, as Equipo attaches
+// what the Assistant looked up for it. A read that doesn't say Base draws
+// them gets them folded into the text.
+func (f *Fake) ReplyWithLookups(id, reply string, lookups ...equipo.Lookup) {
+ f.answerWith(id, reply, true, lookups)
+}
+
+func (f *Fake) answerWith(id, reply string, deltas bool, lookups []equipo.Lookup) {
if deltas {
f.Stream(id, reply)
}
@@ -609,7 +656,8 @@ func (f *Fake) Answer(id, reply string, deltas bool) {
if f.turns[id].State != "running" {
return
}
- m := message{ID: f.next("m"), Revision: 1, Role: "assistant", Text: reply, TurnID: id, CreatedAt: time.Now().UTC()}
+ m := message{ID: f.next("m"), Revision: 1, Role: "assistant", Text: reply, TurnID: id, CreatedAt: time.Now().UTC(),
+ Lookups: append([]equipo.Lookup(nil), lookups...)}
f.messages = append(f.messages, m)
f.emit("message", "message", m.ID, 1, id, m)
t := f.turns[id]
@@ -934,7 +982,7 @@ func (f *Fake) stream(w http.ResponseWriter, r *http.Request) {
}
after = n
}
- paused := pausedOK(r)
+ paused, draws := pausedOK(r), drawsLookups(r)
rc := http.NewResponseController(w)
w.Header().Set("Content-Type", "text/event-stream")
w.WriteHeader(200)
@@ -956,6 +1004,9 @@ func (f *Fake) stream(w http.ResponseWriter, r *http.Request) {
if c, ok := e.Data.(card); ok {
e.Data = asSeen(c, paused)
}
+ if m, ok := e.Data.(message); ok {
+ e.Data = asRead(m, draws)
+ }
raw, _ := json.Marshal(e)
id := ""
if e.Cursor != 0 {
diff --git a/internal/equipo/lookups_test.go b/internal/equipo/lookups_test.go
index 98b806e..a693e09 100644
--- a/internal/equipo/lookups_test.go
+++ b/internal/equipo/lookups_test.go
@@ -1,13 +1,17 @@
package equipo_test
import (
+ "bufio"
+ "context"
"encoding/json"
"reflect"
"strings"
"testing"
+ "time"
"unicode/utf8"
"amadan.net/oficina/base/internal/equipo"
+ "amadan.net/oficina/base/internal/equipo/equipotest"
)
func src(n int) equipo.Source {
@@ -91,3 +95,61 @@ func TestAbilityKeysMatchSpec(t *testing.T) {
t.Errorf("AbilityKeys %v, want %v", equipo.AbilityKeys, want)
}
}
+
+// The fake serves a reply's lookups only to a call that says Base draws
+// them, and otherwise the plain text Equipo falls back to, in the
+// snapshot, the messages page and the event stream alike.
+func TestFakeLookupFallbackWithoutCapability(t *testing.T) {
+ f := equipotest.New(t)
+ f.Hold = true
+ c := equipo.New(f.URL)
+ cred := signIn(t, f, c)
+ sent, err := c.Send(context.Background(), cred.Token, "Weather in Dublin?", "k-1", nil)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for f.Turn(sent.Turn.ID) != "running" {
+ time.Sleep(5 * time.Millisecond)
+ }
+ lookup := equipo.Lookup{Answer: "Rain later.", Sources: []equipo.Source{{URL: "https://met.test/dublin", Title: "Met"}}}
+ f.ReplyWithLookups(sent.Turn.ID, "Here it is.", lookup)
+
+ drawn := equipo.WithCapabilities(context.Background(), []string{equipo.CapLookupAnswers})
+ s, err := c.Snapshot(drawn, cred.Token)
+ if err != nil {
+ t.Fatal(err)
+ }
+ reply := s.Messages[len(s.Messages)-1]
+ if reply.Text != "Here it is." || !reflect.DeepEqual(reply.Lookups, []equipo.Lookup{lookup}) {
+ t.Errorf("with the capability: %+v", reply)
+ }
+ plain := equipo.Message{Role: "assistant", Text: "Here it is.", Lookups: []equipo.Lookup{lookup}}.PlainLookups().Text
+ for _, ctx := range []context.Context{context.Background(), equipo.WithCapabilities(context.Background(), []string{"job_actions"})} {
+ s, err := c.Snapshot(ctx, cred.Token)
+ if err != nil {
+ t.Fatal(err)
+ }
+ reply := s.Messages[len(s.Messages)-1]
+ if reply.Text != plain || reply.Lookups != nil {
+ t.Errorf("without the capability: %+v", reply)
+ }
+ }
+ for name, ctx := range map[string]context.Context{"drawn": drawn, "plain": context.Background()} {
+ res, err := c.Events(ctx, cred.Token, "0")
+ if err != nil {
+ t.Fatal(err)
+ }
+ sc := bufio.NewScanner(res.Body)
+ var got string
+ for sc.Scan() {
+ if line := sc.Text(); strings.HasPrefix(line, "data: ") && strings.Contains(line, `"role":"assistant"`) {
+ got = line
+ break
+ }
+ }
+ res.Body.Close()
+ if has := strings.Contains(got, `"lookups"`); has != (name == "drawn") || name == "plain" && !strings.Contains(got, "Sources:") {
+ t.Errorf("%s event: %s", name, got)
+ }
+ }
+}