"Learn from conversations" (memdistill) reads a session only when the
Sessions list marks it Transcript, which is HasTranscript(agent). The
WorkBuddy family had a summary reader (workbuddyLine) for the Sessions
page and the usage ledger but no transcript reader, so its sessions were
listed and never learned from.
Add wbTranscript (transcript_workbuddy.go), over the same
projects/<cwd>/<id>.jsonl lines: a message's input_text/output_text
blocks (WorkBuddy's own <system-reminder> blocks stay context and its
@scene: blocks are dropped, so the typed words still count as said), a
function_call with its arguments, and its function_call_result by callId.
workbuddy, workbuddy-ai and codebuddy join HasTranscript.
Guard: TestEveryListedAgentHasATranscriptOrIsNamed reads the agent ids
Session.Agent lists and fails for any with no transcript that is not in
noTranscriptYet, and for any in that list that now has one. Siblings still
without a transcript, named there: zcode, dsh, cline, grok, droid, hermes,
alma (issues filed).
Run (mote): go vet ./internal/sessions ./internal/memdistill; go test
./internal/sessions ./internal/memdistill ./internal/memory ./internal/gui.
The new tests fail on the old transcript.go (workbuddy, workbuddy-ai and
codebuddy named, codebuddy not marked readable). internal/gui has one red
that is also red on origin/main 99cc8fa5d, TestSettingsSaveKeepsWhatItDoesNotSend
(RedactModelDir), filed. No GUI file changed. Fixtures are the existing
testdata/workbuddy and testdata/codebuddy session files. Not tried: a real
WorkBuddy account's session through the model.
106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
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
|
|
// <system-reminder> 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
|
|
}
|