diff --git a/internal/sessions/transcript.go b/internal/sessions/transcript.go index 1c5bb0fdc..f8883b17d 100644 --- a/internal/sessions/transcript.go +++ b/internal/sessions/transcript.go @@ -35,7 +35,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", "antigravity", "openmausbot", "liveagent": + case "claude", "qoder", "qoder-cn", "codex", "pi", "raft", "cindy", "omp", "cursor", "opencode", "kimi", "agy", "antigravity", "openmausbot", "liveagent", "workbuddy", "workbuddy-ai", "codebuddy": return true } return false @@ -168,6 +168,8 @@ func readTranscriptOf(s Session, keep func([]byte) bool, add func(bool, Part) bo return ombTranscript(s, add) case "liveagent": return liveagentTranscript(s.Path, add) + case "workbuddy", "workbuddy-ai", "codebuddy": + return wbTranscript(s.Path, add) default: if s.BranchOf != "" && keep == nil { return branchTranscript(s, add) diff --git a/internal/sessions/transcript_workbuddy.go b/internal/sessions/transcript_workbuddy.go new file mode 100644 index 000000000..4edd80537 --- /dev/null +++ b/internal/sessions/transcript_workbuddy.go @@ -0,0 +1,105 @@ +package sessions + +import ( + "bytes" + "cmp" + "encoding/json" + "strings" +) + +// wbTranscript tells add what a WorkBuddy, WorkBuddy AI or CodeBuddy Code +// session said, from the same lines its summary is read from +// (workbuddyLine): message lines (a user's input_text, a reply's +// output_text), a function_call with its arguments, and its +// function_call_result. +func wbTranscript(path string, add func(bool, Part) bool) error { + names := map[string]string{} // a call's tool name, by its callId + _, err := scanAt(path, 0, nil, func(b []byte, _, _ int64) bool { + if !bytes.Contains(b, []byte(`"type":"message"`)) && + !bytes.Contains(b, []byte(`"type":"function_call`)) { + return true + } + var l struct { + wbLine + Name string `json:"name"` + CallID string `json:"callId"` + Arguments json.RawMessage `json:"arguments"` + Output json.RawMessage `json:"output"` + } + if json.Unmarshal(b, &l) != nil { + return true + } + for _, p := range wbParts(l.Type, l.Role, l.Content, l.Name, l.CallID, l.Arguments, l.Output, l.ProviderData.Model, names) { + if !add(p.Role != "assistant", p) { + return false + } + } + return true + }) + return err +} + +func wbParts(typ, role string, content json.RawMessage, name, callID string, args, output json.RawMessage, model string, names map[string]string) []Part { + switch typ { + case "message": + if role != "user" && role != "assistant" { + return nil + } + // one part to a block: a user message holds WorkBuddy's own + // and @scene: blocks beside what was typed + var parts []Part + for _, text := range wbTexts(content) { + if role == "user" && wbScene.MatchString(strings.TrimSpace(text)) { + continue + } + p := textPart(role, text) + if role == "assistant" { + p.Model = model + } + parts = append(parts, p) + } + return parts + case "function_call": + names[callID] = name + var s string + if json.Unmarshal(args, &s) == nil { + args = json.RawMessage(s) + } + text := string(args) + if json.Valid(args) { + text = pretty(args) + } + return []Part{{Role: "assistant", Kind: "tool_use", Name: name, Text: text, Model: model}} + case "function_call_result": + var o struct{ Text string } + text := resultText(output) + if json.Unmarshal(output, &o) == nil && o.Text != "" { + text = o.Text + } + return []Part{{Role: "tool", Kind: "tool_result", Name: cmp.Or(names[callID], name), Text: text}} + } + return nil +} + +// wbTexts are the words of a message: its string, or each of its text +// blocks. +func wbTexts(content json.RawMessage) []string { + var text string + if json.Unmarshal(content, &text) == nil { + if strings.TrimSpace(text) == "" { + return nil + } + return []string{text} + } + var blocks []struct{ Type, Text string } + if json.Unmarshal(content, &blocks) != nil { + return nil + } + var out []string + for _, b := range blocks { + if (b.Type == "input_text" || b.Type == "output_text" || b.Type == "text") && strings.TrimSpace(b.Text) != "" { + out = append(out, b.Text) + } + } + return out +} diff --git a/internal/sessions/transcript_workbuddy_test.go b/internal/sessions/transcript_workbuddy_test.go new file mode 100644 index 000000000..8cc17be51 --- /dev/null +++ b/internal/sessions/transcript_workbuddy_test.go @@ -0,0 +1,106 @@ +package sessions + +import ( + "os" + "regexp" + "strings" + "testing" +) + +// listedAgents are the agent ids a Session's Agent names, from its doc +// comment: the agents whose sessions the Sessions page can list. +func listedAgents(t *testing.T) []string { + t.Helper() + src, err := os.ReadFile("sessions.go") + if err != nil { + t.Fatal(err) + } + m := regexp.MustCompile("json:\"agent\"`[^\\n]*magpie agent id: ([^\\n]+)").FindSubmatch(src) + if m == nil { + t.Fatal("Session.Agent's list of agent ids is gone") + } + var out []string + for _, a := range strings.Split(string(m[1]), ",") { + out = append(out, strings.TrimSpace(a)) + } + return out +} + +// noTranscriptYet are agents the Sessions page lists whose conversations +// are not read as a transcript, so "Learn from conversations" +// (memdistill, which reads sessions.TranscriptOf) skips them. An agent +// leaves this list when its reader is written, and may not be added to it +// without one being filed. +var noTranscriptYet = map[string]string{ + "zcode": "database rows", + "dsh": "session.v3.jsonl not read as a transcript", + "cline": "messages.json not read as a transcript", + "grok": "updates.jsonl not read as a transcript", + "droid": "not read as a transcript", + "hermes": "database rows", + "alma": "database rows", +} + +// Every agent the Sessions page lists is read by memory learning, or is +// named in noTranscriptYet: a new session reader can't be left out of it +// unseen (WorkBuddy's sessions showed on Sessions for weeks and were +// never learned from, #feedback bxgz233). +func TestEveryListedAgentHasATranscriptOrIsNamed(t *testing.T) { + for _, a := range listedAgents(t) { + _, parked := noTranscriptYet[a] + switch { + case HasTranscript(a) && parked: + t.Errorf("%s reads as a transcript: take it off noTranscriptYet", a) + case !HasTranscript(a) && !parked: + t.Errorf("%s has a session reader and no transcript: memory learning never reads it", a) + } + } + for a := range noTranscriptYet { + found := false + for _, l := range listedAgents(t) { + found = found || l == a + } + if !found { + t.Errorf("noTranscriptYet names %s, which is not a listed agent", a) + } + } +} + +// WorkBuddy's, WorkBuddy AI's and CodeBuddy Code's sessions are read as a +// conversation: what was typed (beside WorkBuddy's own reminder and +// @scene blocks), the replies, and each tool call with its result. +func TestTranscriptWorkBuddyFamily(t *testing.T) { + setupAgents(t) + n := 0 + for _, s := range List(100) { + if s.Agent != "workbuddy" && s.Agent != "codebuddy" || s.ParentSession != "" { + continue + } + n++ + if !s.Transcript { + t.Fatalf("%s session %s is not marked readable", s.Agent, s.ID) + } + tr, err := TranscriptOf(s) + if err != nil || len(tr.Parts) == 0 { + t.Fatalf("%s %s: %v, %d parts", s.Agent, s.ID, err, len(tr.Parts)) + } + if strings.HasPrefix(s.ID, "22222222") { + var typed, reply, call, result bool + for _, p := range tr.Parts { + typed = typed || p.Kind == "text" && p.Role == "user" && p.Text == "Draft the launch notes" + reply = reply || p.Kind == "text" && p.Role == "assistant" && p.Text == "Reading the notes." + call = call || p.Kind == "tool_use" && p.Name == "Read" + result = result || p.Kind == "tool_result" && p.Name == "Read" && p.Text == "notes" + if strings.HasPrefix(p.Text, "@scene") { + t.Errorf("WorkBuddy's @scene block read as said: %+v", p) + } + } + if !typed || !reply || !call || !result { + t.Fatalf("typed %v reply %v call %v result %v: %+v", typed, reply, call, result, tr.Parts) + } + } + } + if n < 3 { + t.Fatalf("only %d WorkBuddy-family sessions listed", n) + } +}