diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index d09a3b4b8..a065b1ac5 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -550,7 +550,12 @@ are read (`sessions.Calls`, from `callSources` in Code's, Claude Desktop's and Cowork's, Codex's (and both in WSL), and, read a file at a time (`wholeCallReaders` in [`call_cache.go`](../../internal/sessions/call_cache.go)), OpenCode's -(#680), ZCode's, DeepSeek Harness's (`dsh`), WorkBuddy's, CodeBuddy +(#680), ZCode's, DeepSeek Harness's (`dsh`), WorkBuddy's, WorkBuddy +AI's (the international build's own `~/.workbuddy-ai`, read as +WorkBuddy's with its own models.json, listed and deletable under its own +name, and read once as WorkBuddy's when it is a link to `~/.workbuddy`: +`TestWorkBuddyAI`, `TestWorkBuddyAIGatewayIsItsOwn`, +`TestWorkBuddyAISessionsListedUnderTheirName`), CodeBuddy Code's (#1779; its sessions are on the Sessions page too, from the same `projects//.jsonl` files read as WorkBuddy's, resumed with `codebuddy --resume ` and deletable, #2657: `TestCodeBuddy`, diff --git a/internal/gui/sessions_workbuddy_ai_test.go b/internal/gui/sessions_workbuddy_ai_test.go new file mode 100644 index 000000000..eea53638d --- /dev/null +++ b/internal/gui/sessions_workbuddy_ai_test.go @@ -0,0 +1,115 @@ +package gui + +import ( + "encoding/json" + "io/fs" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/yetone/magpie/internal/sessions" +) + +// WorkBuddy AI's sessions (~/.workbuddy-ai/projects, the international +// build's folder, here holding the sessions package's WorkBuddy fixture) +// reach the Sessions page's agents and Usage > Sessions under WorkBuddy +// AI's name and icon, beside the China build's, and the page offers them to +// the trash (maxleebeta on Discord: signed in to both, the international +// build was missing from the agents). +func TestWorkBuddyAISessionsListedUnderTheirName(t *testing.T) { + h := sandboxHome(t) + src := filepath.Join("..", "sessions", "testdata", "workbuddy") + dst := filepath.Join(h, ".workbuddy-ai") + if err := filepath.WalkDir(src, func(p string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + rel, _ := filepath.Rel(src, p) + if d.IsDir() { + return os.MkdirAll(filepath.Join(dst, rel), 0o755) + } + b, err := os.ReadFile(p) + if err != nil { + return err + } + return os.WriteFile(filepath.Join(dst, rel), b, 0o644) + }); err != nil { + t.Fatal(err) + } + sessions.Reset() + t.Cleanup(sessions.Reset) + // the stats another test kept are not this HOME's + forgetStats() + t.Cleanup(forgetStats) + + mux := http.NewServeMux() + sessionRoutes(mux, folderOnly{}) + sessionManageRoutes(mux, folderOnly{}) + get := func(path string, v any) { + t.Helper() + w := httptest.NewRecorder() + mux.ServeHTTP(w, httptest.NewRequest("GET", path, nil)) + if w.Code != 200 { + t.Fatalf("%s: %d %s", path, w.Code, w.Body) + } + if err := json.Unmarshal(w.Body.Bytes(), v); err != nil { + t.Fatal(err) + } + } + type named struct { + Agent string `json:"agent"` + Name string `json:"name"` + Icon string `json:"icon"` + Resume string `json:"resume"` + } + var list struct{ Sessions []named } + get("/api/sessions", &list) + n := 0 + for _, s := range list.Sessions { + if s.Agent != "workbuddy-ai" { + continue + } + n++ + if s.Name != "WorkBuddy AI" || s.Icon != "workbuddy-color" { + t.Errorf("listed as %q (%s), want WorkBuddy AI (workbuddy-color)", s.Name, s.Icon) + } + // a desktop app: nothing resumes it from a terminal + if s.Resume != "" { + t.Errorf("resume %q", s.Resume) + } + } + if n != 3 { + t.Fatalf("want 3 WorkBuddy AI sessions, got %d: %+v", n, list.Sessions) + } + var manage struct { + Agents []struct { + named + Deletable bool `json:"deletable"` + } + } + get("/api/sessions/manage", &manage) + found := false + for _, a := range manage.Agents { + if a.Agent == "workbuddy-ai" { + found = true + if a.Name != "WorkBuddy AI" { + t.Errorf("the Sessions page's agent is %q, want WorkBuddy AI", a.Name) + } + if !a.Deletable { + t.Error("the Sessions page offers no trash for WorkBuddy AI's sessions") + } + } + } + if !found { + t.Fatalf("no workbuddy-ai among the Sessions page's agents: %+v", manage.Agents) + } + var stats struct { + Agents map[string]string `json:"agents"` + } + get("/api/sessions/stats?days=0", &stats) + if stats.Agents["workbuddy-ai"] != "WorkBuddy AI" { + t.Errorf("Usage > Sessions' agent strip names it %q: %v", stats.Agents["workbuddy-ai"], stats.Agents) + } +} diff --git a/internal/sessions/access.go b/internal/sessions/access.go index e471cfa7c..fbcaa46b2 100644 --- a/internal/sessions/access.go +++ b/internal/sessions/access.go @@ -207,6 +207,7 @@ func Found() []string { add("qoder-cn", filepath.Join(QoderDir("qoder-cn"), "projects")) add("grok", filepath.Join(GrokDir(), "sessions")) add("workbuddy", filepath.Join(WorkBuddyDir(), "projects")) + add("workbuddy-ai", filepath.Join(WorkBuddyAIDir(), "projects")) add("codebuddy", filepath.Join(CodeBuddyDir(), "projects")) add("omp", filepath.Join(OmpDir(), "sessions")) add("droid", filepath.Join(FactoryDir(), "sessions")) diff --git a/internal/sessions/call_cache.go b/internal/sessions/call_cache.go index 06287e4a0..344c4923a 100644 --- a/internal/sessions/call_cache.go +++ b/internal/sessions/call_cache.go @@ -242,10 +242,11 @@ func keepCalls(path string, st *callFile) { // in callFiles needs an entry here, in lineCallReaders or in // resumeCallReaders, or its calls are never read. var wholeCallReaders = map[string]func(file) (*callFile, bool){ - "opencode": readOpenCodeCalls, - "zcode": readZCodeCalls, - "workbuddy": readWorkBuddyCalls, - "codebuddy": readWorkBuddyCalls, // #1779 + "opencode": readOpenCodeCalls, + "zcode": readZCodeCalls, + "workbuddy": readWorkBuddyCalls, + "workbuddy-ai": readWorkBuddyCalls, + "codebuddy": readWorkBuddyCalls, // #1779 // #1605 "minimax-code": readMiniMaxCalls, "agy": readAgyCalls, // #2097 diff --git a/internal/sessions/calls.go b/internal/sessions/calls.go index 8547a3bc9..42f8f9276 100644 --- a/internal/sessions/calls.go +++ b/internal/sessions/calls.go @@ -339,7 +339,7 @@ func callSourcesWith(codex []file) []file { return callSourcesInto(nil, codex) } // callSourcesInto is callSourcesWith appended to out. func callSourcesInto(out []file, codex []file) []file { lists := append(callFileLists(codex), openCodeCallFiles(), zcodeCallFiles(), dshCallFiles(), - minimaxCallFiles(), kimiCallFiles(), wslFiles("kimi"), piCallFiles(), codebuddyCallFiles(), + minimaxCallFiles(), kimiCallFiles(), wslFiles("kimi"), piCallFiles(), codebuddyCallFiles(), workbuddyAIFiles(), agyCallFiles(), // #2097 workbuddyCallFiles(), grokCallFiles()) // #4423 if out == nil { diff --git a/internal/sessions/calls_workbuddy.go b/internal/sessions/calls_workbuddy.go index ec057724d..c4a39125d 100644 --- a/internal/sessions/calls_workbuddy.go +++ b/internal/sessions/calls_workbuddy.go @@ -59,8 +59,11 @@ func CodeBuddyDir() string { // buddyDir is the folder an agent read here keeps its models.json in. func buddyDir(agent string) string { - if agent == "codebuddy" { + switch agent { + case "codebuddy": return CodeBuddyDir() + case "workbuddy-ai": + return WorkBuddyAIDir() } return WorkBuddyDir() } diff --git a/internal/sessions/manage.go b/internal/sessions/manage.go index fdad2274f..677c09531 100644 --- a/internal/sessions/manage.go +++ b/internal/sessions/manage.go @@ -75,7 +75,7 @@ type AgentCount struct { // those kept as files of their own, in a layout magpie knows whole. func Deletable(agent string) bool { switch agent { - case "claude", "qoder", "qoder-cn", "workbuddy", "codebuddy", "codex", "pi", "raft", "cindy", "omp", "cursor", "dsh", "droid", "agy": + case "claude", "qoder", "qoder-cn", "workbuddy", "workbuddy-ai", "codebuddy", "codex", "pi", "raft", "cindy", "omp", "cursor", "dsh", "droid", "agy": return true } return false diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 6744f78c9..2df75add4 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -120,7 +120,7 @@ type Session struct { // Gateway: seen only through magpie's gateway, with no file of the // agent's on this computer (external.go) Gateway bool `json:"gateway,omitempty"` - Agent string `json:"agent"` // magpie agent id: claude, codex, opencode, pi, omp, zcode, dsh, cline, qoder, qoder-cn, grok, workbuddy, codebuddy, droid, cursor, hermes, alma, kimi, agy + Agent string `json:"agent"` // magpie agent id: claude, codex, opencode, pi, omp, zcode, dsh, cline, qoder, qoder-cn, grok, workbuddy, workbuddy-ai, codebuddy, droid, cursor, hermes, alma, kimi, agy ID string `json:"id"` Cwd string `json:"cwd"` Title string `json:"title"` // the name it was given, else the agent's own title, else the first prompt @@ -698,7 +698,7 @@ func allFiles() []file { // agent's name is held to that agent too return allowed(distinctOf(append(callFileLists(codexFiles()), openCodeFiles(), piFiles(), raftFiles(), cindyFiles(), wslFiles("pi", "raft", "omp", "kimi"), zcodeFiles(), dshFiles(), clineFiles(), ccFiles("qoder", QoderDir("qoder")), ccFiles("qoder-cn", QoderDir("qoder-cn")), - grokFiles(), workbuddyFiles(), codebuddyFiles(), droidFiles(), ompFiles(), cursorFiles(), hermesFiles(), almaFiles(), kimiFiles(), agyFiles())...)) + grokFiles(), workbuddyFiles(), workbuddyAIFiles(), codebuddyFiles(), droidFiles(), ompFiles(), cursorFiles(), hermesFiles(), almaFiles(), kimiFiles(), agyFiles())...)) } // distinctOf is lists one after another with each path kept once, the @@ -785,6 +785,7 @@ func Dirs() []string { {QoderDir("qoder-cn"), filepath.Join(QoderDir("qoder-cn"), "projects")}, {GrokDir(), filepath.Join(GrokDir(), "sessions")}, {WorkBuddyDir(), filepath.Join(WorkBuddyDir(), "projects")}, + {WorkBuddyAIDir(), filepath.Join(WorkBuddyAIDir(), "projects")}, {CodeBuddyDir(), filepath.Join(CodeBuddyDir(), "projects")}, {OmpDir(), filepath.Join(OmpDir(), "sessions")}, {FactoryDir(), filepath.Join(FactoryDir(), "sessions")}, @@ -1918,7 +1919,7 @@ func parserFor(agent string) sessionParser { return sessionParser{line: codexBody} case "pi", "raft", "cindy", "omp": return sessionParser{line: piParse} - case "workbuddy", "codebuddy": + case "workbuddy", "workbuddy-ai", "codebuddy": return sessionParser{line: workbuddyLine} default: return sessionParser{line: claudeLine, claude: true} diff --git a/internal/sessions/skills_every_agent_test.go b/internal/sessions/skills_every_agent_test.go index 9fe9db2f0..895291185 100644 --- a/internal/sessions/skills_every_agent_test.go +++ b/internal/sessions/skills_every_agent_test.go @@ -44,6 +44,7 @@ var skillsCounted = map[string]struct { "kimi": {none: "Kimi Code's wire tool.call is counted by name, but how it brings in a skill was not seen in a real session (only the retired Python kimi-cli 1.52 was run)"}, "workbuddy": {none: "its Skill function_call lines are not read; no real session with tool lines"}, "codebuddy": {none: "reads with WorkBuddy's parser, which counts no skills"}, + "workbuddy-ai": {none: "reads with WorkBuddy's parser, which counts no skills"}, "hermes": {none: "its skill_view tool is not read; hermes is not run in a sandbox (it rewrites the live launcher)"}, "cursor": {none: "selected skills live in selectedContext; needs a signed-in Cursor for a real session"}, "agy": {none: "skill_lookup's keys are not known from a real session"}, diff --git a/internal/sessions/workbuddy.go b/internal/sessions/workbuddy.go index 9cb3d49da..ca51cfffb 100644 --- a/internal/sessions/workbuddy.go +++ b/internal/sessions/workbuddy.go @@ -40,6 +40,27 @@ func WorkBuddyDir() string { // a session's own .jsonl, and a subagent's under /subagents/. func workbuddyFiles() []file { return buddyFiles("workbuddy", WorkBuddyDir()) } +// WorkBuddyAIDir is WorkBuddy AI's folder, the international build's +// (www.workbuddy.ai): ~/.workbuddy-ai, its cli/product.json's +// dataFolderName, as internal/agent's workbuddy.go wires it. +func WorkBuddyAIDir() string { + home, _ := os.UserHomeDir() + return filepath.Join(home, ".workbuddy-ai") +} + +// workbuddyAIFiles are WorkBuddy AI's sessions, kept as the China build +// keeps its own, in its own folder: listed under its own agent, not +// WorkBuddy's, so someone signed in to both apps finds each one's +// (maxleebeta on Discord: the international build was missing from the +// Sessions page's agents). None when its folder is WorkBuddy's (a link), +// which workbuddyFiles reads already. +func workbuddyAIFiles() []file { + if sameFolder(WorkBuddyAIDir(), WorkBuddyDir()) { + return nil + } + return buddyFiles("workbuddy-ai", WorkBuddyAIDir()) +} + // codebuddyFiles are the same under CodeBuddy Code's projects/: the CLI keeps // them as its desktop sibling does, one agent's folder apart (#1779). None // when its folder is WorkBuddy's, which workbuddyFiles reads already. @@ -69,8 +90,10 @@ func sameFolder(a, b string) bool { } // isBuddy is whether agent keeps its sessions the way WorkBuddy does: its -// own, or CodeBuddy Code's. -func isBuddy(agent string) bool { return agent == "workbuddy" || agent == "codebuddy" } +// own, WorkBuddy AI's, or CodeBuddy Code's. +func isBuddy(agent string) bool { + return agent == "workbuddy" || agent == "workbuddy-ai" || agent == "codebuddy" +} // buddyFiles are the sessions under projects/ in dir, as agent's: WorkBuddy's, // or CodeBuddy Code's, which keeps them the same way. diff --git a/internal/sessions/workbuddy_ai_test.go b/internal/sessions/workbuddy_ai_test.go new file mode 100644 index 000000000..c6bf13b57 --- /dev/null +++ b/internal/sessions/workbuddy_ai_test.go @@ -0,0 +1,117 @@ +package sessions + +import ( + "os" + "path/filepath" + "slices" + "testing" + "time" +) + +// WorkBuddy AI, the international build, keeps its sessions as the China +// build does, in ~/.workbuddy-ai: someone signed in to both apps finds each +// one's sessions under its own agent on the Sessions page, its calls on the +// Usage page and its folder among those the reading consent lists +// (maxleebeta on Discord: the international build was missing from the +// agents). The fixture is the China build's own files, as the two builds +// are one app writing to its own folder. +func TestWorkBuddyAI(t *testing.T) { + inZone(t, 0) + home := setupAgents(t) + copyTree(t, "testdata/workbuddy", filepath.Join(home, ".workbuddy-ai")) + Reset() + ss := List(0) + if n := count(ss, "workbuddy-ai"); n != 3 { + t.Fatalf("want 3 WorkBuddy AI sessions, got %d", n) + } + // the China build's stay its own, each listed once + if n := count(ss, "workbuddy"); n != 3 { + t.Fatalf("want 3 WorkBuddy sessions, got %d", n) + } + w := find(t, ss, "workbuddy-ai", wbID) + if w.Cwd != "/work/wb" || w.Title != "Launch notes draft" || w.Resume != "" { + t.Fatalf("workbuddy-ai: %+v", w) + } + if m := model(w, "hy3"); m.Tokens != (Tokens{2422, 968, 8777, 0, 0}) { + t.Fatalf("hy3: %+v", m) + } + // brought over from its older history: the cwd from its meta file + if o := find(t, ss, "workbuddy-ai", wbOldID); o.Cwd != "/work/wb-old" { + t.Fatalf("old: %+v", o) + } + if !Deletable("workbuddy-ai") { + t.Fatal("WorkBuddy AI's sessions are not deletable") + } + if !slices.Contains(Found(), "workbuddy-ai") { + t.Fatalf("the reading consent doesn't list WorkBuddy AI: %v", Found()) + } + + // its calls reach the usage ledger, the one its own models.json names + // as magpie's gateway left to the gateway, as WorkBuddy's are (its own + // calls alone: the fixture's message ids are WorkBuddy's, which the + // ledger counts once over every agent) + cs := map[string]Call{} + for _, c := range AgentCalls(time.Time{}, "workbuddy-ai") { + if c.Agent == "workbuddy-ai" && c.Msg != "" { + cs[c.Msg] = c + } + } + if m := cs["m1"]; m.Model != "hy3" || m.Tokens != (Tokens{200, 80, 1000, 0, 0}) { + t.Fatalf("WorkBuddy AI's reply: %+v (%v)", m, cs) + } + if _, dup := cs["wm1"]; dup { + t.Fatalf("a call made to magpie's gateway was read from the file too: %+v", cs["wm1"]) + } + if _, ok := cs["wm2"]; !ok { + t.Fatalf("no call for the user's own custom provider: %+v", cs) + } + + // a ~/.workbuddy-ai that is a link to ~/.workbuddy is read once, as + // WorkBuddy's + if err := os.RemoveAll(filepath.Join(home, ".workbuddy-ai")); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(home, ".workbuddy"), filepath.Join(home, ".workbuddy-ai")); err != nil { + t.Skip("no symlinks here:", err) + } + Reset() + ss = List(0) + if n, m := count(ss, "workbuddy-ai"), count(ss, "workbuddy"); n != 0 || m != 3 { + t.Fatalf("one folder under two names: %d WorkBuddy AI and %d WorkBuddy sessions, want 0 and 3", n, m) + } +} + +// WorkBuddy AI's own models.json says which of its calls went through +// magpie's gateway, not WorkBuddy's: with the China build's model pointed at +// magpie and the international build's same id at the user's own provider, +// the international build's call is its own, read from its file. +func TestWorkBuddyAIGatewayIsItsOwn(t *testing.T) { + inZone(t, 0) + home := setupAgents(t) + copyTree(t, "testdata/workbuddy", filepath.Join(home, ".workbuddy-ai")) + // WorkBuddy's own-proxy/glm-5 is magpie's; WorkBuddy AI's keeps the + // fixture's models.json, where it is the user's + viaMagpie := `[{"id": "own-proxy/glm-5", "name": "GLM-5", "vendor": "magpie", + "apiKey": "magpie-workbuddy", "url": "http://127.0.0.1:3425/v1/chat/completions"}] +` + if err := os.WriteFile(filepath.Join(home, ".workbuddy", "models.json"), []byte(viaMagpie), 0o600); err != nil { + t.Fatal(err) + } + Reset() + if !workbuddyGateway("custom-local:own-proxy/glm-5") { + t.Fatal("WorkBuddy's models.json is not read as naming magpie's gateway") + } + if buddyGateway(WorkBuddyAIDir(), "custom-local:own-proxy/glm-5") { + t.Fatal("WorkBuddy AI's own provider is read as magpie's gateway") + } + var ai, wb bool + for _, c := range Calls(time.Time{}) { + if c.Msg == "wm2" { + ai = ai || c.Agent == "workbuddy-ai" + wb = wb || c.Agent == "workbuddy" + } + } + if !ai || wb { + t.Fatalf("the call to the user's own provider: read as WorkBuddy AI's %v, as WorkBuddy's %v; want true, false", ai, wb) + } +}