sessions: an OpenMausBot conversation can be shown (@qingqingdilaiP on X)
@qingqingdilaiP's OpenMausBot rows in Usage → Sessions (b507b5f69, v0.1.4146) had a time, an id and ~\.openmausbot\messages.db#<id>, but no Show conversation: HasTranscript left openmausbot out, so the row had no transcript flag and /api/sessions/transcript, the Markdown export and the Sessions filter refused it. ombTranscript reads the conversation from messages.db (the listing's path up to its last "#", the session's id as thread_id), on its own read-only connection, in one read transaction, ordered by at and rowid as the listing reads it: - a user text is the person's message; - a bot text, and an options card's words (MausBot's first message is one), are the bot's; - an activity of a turn's item (tool.itemId, as server/index.js writes on item.started) is a tool_use with its input (else its summary, else its name: the sample's agents_list_bots kept no input), and its output a tool_result; - an activity with no item (a notice, a queue or engine move, which OpenMausBot adds itself) and a turn's digest, which repeats the reply, aren't said. Sibling check: Hermes and Alma, listed the same way (read-only database rows,5a4104632,dc3deaf09), have no Show conversation either, nor do zcode, dsh, cline, grok, droid, workbuddy, workbuddy-ai, codebuddy and antigravity. Filed as #6633; not changed here. Tests (testdata/openmausbot, OpenMausBot 0.1.155's own data folder asb507b5f69recorded it): - TestOpenMausBotTranscript: both conversations flagged in List and on the Sessions page; Find + TranscriptOf as the GUI route does gives the team thread's prompt, "Checking the team.", agents_list_bots, its result, the reply, and the fruits thread's 4 texts with no digest; WriteMarkdown and SearchSession read the same rows; messages.db's bytes unchanged. - TestOpenMausBotTranscriptToolsAndNotices: rows inserted in the shapes OpenMausBot writes (item.started/completed with input and output, a cloud-credit-moves notice): the tool shown with its arguments and result, the notice not said. Broken by hand, each red: openmausbot out of HasTranscript (no flag, "can't be shown"); the itemId check removed (the notice said as a tool). Run on mote: gofmt -l clean; go vet ./internal/sessions ./internal/gui, GOOS=windows go vet ./internal/sessions, GOOS=linux go vet -tags nogui ./internal/sessions; go test -count=1 ./internal/sessions ./internal/gui ok; node --test sessions-talk, sessions-talk-sheet, sessions-talk-model, sessions-export, sessions-search (Chromium and WebKit, their languages and widths): 54 of 54 pass (the GUI is unchanged: it shows the button from the row's transcript flag). On Windows (xiaomi-gpu): go test -run 'OpenMausBot|Alma|Hermes' ./internal/sessions ok. After a rebase onto80a315a70(no internal/sessions change; internal/gui's cache change): on mica (mote over its cores), go vet and go test -count=1 ./internal/sessions ./internal/gui ok, at the pushed tree. Not tried: the live GUI against a real OpenMausBot on Windows (the reporter's case); a conversation with images or cards other than options (secret, signin, connector are left out); not the full ./... suite (the change is inside internal/sessions, whose only callers here are internal/gui's routes, both run).
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user