diff --git a/internal/sessions/openmausbot.go b/internal/sessions/openmausbot.go index b2f81df58..fbe689bc4 100644 --- a/internal/sessions/openmausbot.go +++ b/internal/sessions/openmausbot.go @@ -2,6 +2,7 @@ package sessions import ( "bytes" + "cmp" "context" "database/sql" "encoding/json" @@ -15,6 +16,7 @@ import ( "time" "github.com/yetone/magpie/internal/appdir" + "github.com/yetone/magpie/internal/provider" ) // OpenMausBot (milind-soni/OpenMausBot, a chat app whose bots run on agent @@ -363,3 +365,70 @@ func parseOpenMausBot(f file) *state { } return s } + +// ombTranscript reads a conversation as OpenMausBot shows it, in one read +// transaction: the person's messages, the bot's lines (an options card's +// words too, as MausBot's first message is one), and each tool a turn ran +// with what it was given and what it answered. A line OpenMausBot itself +// adds (a notice, a queue or engine move) is an activity with no item of +// a turn and isn't said; nor is a turn's digest, which repeats the reply. +func ombTranscript(s Session, add func(bool, Part) bool) error { + i := strings.LastIndex(s.Path, "#") + if i < 0 || s.ID == "" { + return ErrNoTranscript + } + db, err := provider.OpenReadOnly(s.Path[:i]) + if err != nil { + return err + } + defer db.Close() + tx, err := db.BeginTx(context.Background(), &sql.TxOptions{ReadOnly: true}) + if err != nil { + return err + } + defer tx.Rollback() + rows, err := tx.Query("SELECT role, kind, COALESCE(text, ''), json FROM messages WHERE thread_id = ? ORDER BY at, rowid", s.ID) + if err != nil { + return err + } + defer rows.Close() + for rows.Next() { + var role, kind, text, raw string + if err := rows.Scan(&role, &kind, &text, &raw); err != nil { + return err + } + more := true + switch { + case role == "user" && kind == "text": + more = add(true, textPart("user", text)) + case role == "bot" && (kind == "text" || kind == "options"): + more = add(false, Part{Role: "assistant", Kind: "text", Text: text}) + case role == "bot" && kind == "activity": + var m struct { + Tool struct { + Name string `json:"name"` + Summary string `json:"summary"` + Input json.RawMessage `json:"input"` + Output json.RawMessage `json:"output"` + ItemID string `json:"itemId"` + } `json:"tool"` + } + if json.Unmarshal([]byte(raw), &m) != nil || m.Tool.Name == "" || m.Tool.ItemID == "" { + continue + } + in := pretty(m.Tool.Input) + if strings.TrimSpace(in) == "" || in == "null" { + // OpenMausBot kept no arguments: its one-line summary, else the name + in = cmp.Or(m.Tool.Summary, m.Tool.Name) + } + more = add(false, Part{Role: "assistant", Kind: "tool_use", Name: m.Tool.Name, Text: in}) + if more && len(m.Tool.Output) > 0 && string(m.Tool.Output) != "null" { + more = add(true, Part{Role: "tool", Kind: "tool_result", Name: m.Tool.Name, Text: resultText(m.Tool.Output)}) + } + } + if !more { + return nil + } + } + return rows.Err() +} diff --git a/internal/sessions/openmausbot_test.go b/internal/sessions/openmausbot_test.go index 8bbc4459b..1f80b272c 100644 --- a/internal/sessions/openmausbot_test.go +++ b/internal/sessions/openmausbot_test.go @@ -2,9 +2,11 @@ package sessions import ( "bytes" + "context" "database/sql" "os" "path/filepath" + "slices" "strings" "testing" "time" @@ -238,3 +240,125 @@ func TestOpenMausBotReadAgainWhenItChanges(t *testing.T) { t.Errorf("team not read again: %+v", s) } } + +// ombParts is a transcript's parts as role/kind/name: text, for a +// comparison that reads as the conversation does. +func ombParts(t *testing.T, s Session) []string { + t.Helper() + tr, err := TranscriptOf(s) + if err != nil { + t.Fatalf("TranscriptOf(%s): %v", s.ID, err) + } + var out []string + for _, p := range tr.Parts { + out = append(out, p.Role+"/"+p.Kind+"/"+p.Name+": "+p.Text) + } + return out +} + +// An OpenMausBot conversation is shown as the app shows it (@qingqingdilaiP +// on X: the row had no Show conversation): the person's messages, the +// bot's lines and the tool its turn ran with what it answered, in order, +// and not the turn's digest. It is found by its id as the page asks, its +// Markdown and a search read the same rows, and the database is only read. +func TestOpenMausBotTranscript(t *testing.T) { + setup(t) + dir := filepath.Join(t.TempDir(), "omb") + t.Setenv("OMB_DATA_DIR", dir) + ombFixture(t, dir) + db := filepath.Join(dir, "messages.db") + before, err := os.ReadFile(db) + if err != nil { + t.Fatal(err) + } + + for _, id := range []string{ombFruits, ombTeam} { + if s := ombSession(t, List(0), id); !s.Transcript { + t.Errorf("%s: no conversation to show: %+v", id, s) + } + if m := ombManaged(t, id); !m.Transcript { + t.Errorf("%s on the Sessions page: no conversation to show", id) + } + } + team, ok := Find("openmausbot", ombTeam) + if !ok { + t.Fatal("Find(openmausbot, team) found nothing") + } + got := ombParts(t, team) + want := []string{ + "user/text/: TOOL: who is on the team?", + "assistant/text/: Checking the team.", + "assistant/tool_use/agents_list_bots: agents_list_bots", + } + if len(got) != 5 || !slices.Equal(got[:3], want) || !strings.HasPrefix(got[3], "tool/tool_result/agents_list_bots: {") || + !strings.Contains(got[3], "Bots you can reach") || got[4] != "assistant/text/: Hello from the fake model, reply 2." { + t.Errorf("team transcript:\n%s", strings.Join(got, "\n")) + } + fruits, ok := Find("openmausbot", ombFruits) + if !ok { + t.Fatal("Find(openmausbot, fruits) found nothing") + } + if got, want := ombParts(t, fruits), []string{ + "user/text/: Say hi, then list three fruits", + "assistant/text/: Hello from the fake model, reply 2.", + "user/text/: 你好,再说一次", + "assistant/text/: Hello from the fake model, reply 3.", + }; !slices.Equal(got, want) { + t.Errorf("fruits transcript:\n%s", strings.Join(got, "\n")) + } + + var md bytes.Buffer + if err := WriteMarkdown(&md, fruits, "OpenMausBot"); err != nil || !strings.Contains(md.String(), "你好,再说一次") || !strings.Contains(md.String(), "reply 3.") { + t.Errorf("Markdown (%v):\n%s", err, md.String()) + } + for q, want := range map[string]bool{"再说一次": true, "reply 3": true, "no such words": false} { + if _, found, err := SearchSession(context.Background(), fruits, NeedleOf(q)); err != nil || found != want { + t.Errorf("search %q: found %v (%v), want %v", q, found, err, want) + } + } + + after, err := os.ReadFile(db) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(before, after) { + t.Error("showing an OpenMausBot conversation changed its database") + } +} + +// A tool OpenMausBot kept the arguments of is shown with them; a line the +// app adds itself (a notice, an activity of no turn's item) is not said. +// The rows are written as OpenMausBot writes them (server/index.js's +// item.started and item.completed, cloud-credit-moves.js's notice). +func TestOpenMausBotTranscriptToolsAndNotices(t *testing.T) { + setup(t) + dir := filepath.Join(t.TempDir(), "omb") + t.Setenv("OMB_DATA_DIR", dir) + ombFixture(t, dir) + db, err := sql.Open("sqlite", filepath.Join(dir, "messages.db")) + if err != nil { + t.Fatal(err) + } + defer db.Close() + add := func(id string, at int64, js string) { + t.Helper() + if _, err := db.Exec(`INSERT INTO messages (thread_id, id, at, role, kind, text, json) VALUES (?, ?, ?, 'bot', 'activity', NULL, ?)`, ombTeam, id, at, js); err != nil { + t.Fatal(err) + } + } + add("notice", 1794638536500, `{"id":"notice","at":1794638536500,"role":"bot","kind":"activity","tool":{"name":"notice: Scout moved to another engine","ok":true}}`) + add("read", 1794638536600, `{"id":"read","at":1794638536600,"role":"bot","kind":"activity","tool":{"name":"read_file","spoken":"reading a file","summary":"notes.md","input":{"path":"notes.md"},"itemId":"call_2","ok":false,"output":"no such file"},"turnId":"68914cf1-5ffb-49d6-ae64-88cf1b5cd435"}`) + team, ok := Find("openmausbot", ombTeam) + if !ok { + t.Fatal("Find(openmausbot, team) found nothing") + } + got := ombParts(t, team) + if len(got) != 7 || got[5] != "assistant/tool_use/read_file: {\n \"path\": \"notes.md\"\n}" || got[6] != "tool/tool_result/read_file: no such file" { + t.Errorf("team transcript:\n%s", strings.Join(got, "\n")) + } + for _, p := range got { + if strings.Contains(p, "notice") { + t.Errorf("OpenMausBot's own notice was said: %q", p) + } + } +} diff --git a/internal/sessions/transcript.go b/internal/sessions/transcript.go index d9a7ecf81..48fb95a34 100644 --- a/internal/sessions/transcript.go +++ b/internal/sessions/transcript.go @@ -32,7 +32,7 @@ var ErrNoTranscript = errors.New("this agent's conversations can't be shown") // transcript. func HasTranscript(agent string) bool { switch agent { - case "claude", "qoder", "qoder-cn", "codex", "pi", "raft", "cindy", "omp", "cursor", "opencode", "kimi", "agy": + case "claude", "qoder", "qoder-cn", "codex", "pi", "raft", "cindy", "omp", "cursor", "opencode", "kimi", "agy", "openmausbot": return true } return false @@ -96,6 +96,8 @@ func readTranscriptOf(s Session, keep func([]byte) bool, add func(bool, Part) bo return kimiTranscript(s.Path, keep, add) case "agy": return agyTranscript(s.Path, keep, add) + case "openmausbot": + return ombTranscript(s, add) default: if s.BranchOf != "" && keep == nil { return branchTranscript(s, add)