Files
magpie/internal/gui/sessions_manage.go
T
yetone cd201d888c sessions: a Sessions page lists an agent's sessions by folder, to resume or delete, and deleted ones wait in magpie's trash to be restored (TJHHHH on Discord: session management like CC Switch's, deleting above all, in the GUI) — sessions were only seen on Usage, with no way to remove one. The new Sessions tab shows, per agent, every session under the folder it ran in, with its title or first prompt, when, how many messages and how big, a filter, its resume command to copy and, in the Mac app, Open in terminal. Picked sessions (or one, by its own button) are deleted after magpie's own dialog: their files (Claude Code's .jsonl, its subagents folder and what it keeps by the id under file-history, session-env and todos; every segment of a Codex rollout; Qoder's, WorkBuddy's, Pi's, oh-my-pi's) are moved into magpie's folder, trash/sessions/<agent>/<when>-<id>, with a manifest, never erased; Trash lists them and Restore puts them back where they were. A session written to in the last minute is left, and said so, as the agent may still be running it. Agents' own indexes (Codex's session_index.jsonl and state database, Claude Code's history.jsonl) are not edited: they are written while the agent runs, and a stale entry there is harmless. OpenCode, ZCode, dsh, Cline and Grok keep sessions in databases magpie doesn't write, and show no delete. With nine tabs the narrowest header gets a further step that draws the tabs in, and past that they scroll sideways rather than push the page wider (560px in English, or at 150% text). The dialog uses the shared confirmAsk/closeConfirmAsk, as the gateway keys' do. TestListAgent, TestDeleteRestoreClaude, TestDeleteRestoreCodex, TestDeleteActiveRefused (fails without the running check) and TestDeleteOnlyKnownAgents run in a temp HOME; sessions-manage.test.cjs (Chromium, WebKit, en, zh) fails without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; sessions-manage, click-scroll, old-webkit, text-size, session-terminal, sessions-today, brand, rail-tip, web-page, omarchy, model-test-one, gateway-caller-keys, gateway-connect-keys, api-key-usage, api-key-theme pass.
2026-10-01 22:11:46 +08:00

149 lines
4.2 KiB
Go

package gui
import (
"cmp"
"encoding/json"
"errors"
"net/http"
"runtime"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/sessions"
)
// The Sessions page: every session of an agent, by folder, to pick up again
// or delete. A delete moves the session's files into magpie's trash, and a
// restore moves them back; nothing is ever removed for good here.
type manageAgentJSON struct {
sessions.AgentCount
Name string `json:"name"`
Icon string `json:"icon"`
}
type trashedJSON struct {
sessions.Trashed
Name string `json:"name"`
Icon string `json:"icon"`
}
func agentLooks() map[string]*agent.Agent {
out := map[string]*agent.Agent{}
for _, a := range agent.Clients() {
out[a.ID] = a
}
return out
}
func trashJSON(looks map[string]*agent.Agent) []trashedJSON {
out := []trashedJSON{}
for _, t := range sessions.Trash() {
j := trashedJSON{Trashed: t, Name: t.Agent, Icon: "generic"}
if a := looks[t.Agent]; a != nil {
j.Name, j.Icon = a.Name, a.Icon
}
j.Cwd = tilde(j.Cwd)
for i := range j.Items {
j.Items[i].From = tilde(j.Items[i].From)
}
out = append(out, j)
}
return out
}
func sessionManageRoutes(mux *http.ServeMux, w Windows) {
// manage is the agents with sessions, and every session of ?agent=
// (the one with the most when none is named), and the trash.
mux.HandleFunc("GET /api/sessions/manage", func(rw http.ResponseWriter, r *http.Request) {
looks := agentLooks()
out := struct {
Agents []manageAgentJSON `json:"agents"`
Agent string `json:"agent"`
Sessions []sessions.Managed `json:"sessions"`
Terminal bool `json:"terminal"`
Trash []trashedJSON `json:"trash"`
TrashDir string `json:"trashDir"`
}{Agents: []manageAgentJSON{}, Sessions: []sessions.Managed{}, Terminal: runtime.GOOS == "darwin" && !isWeb(w),
Trash: trashJSON(looks), TrashDir: tilde(sessions.TrashDir())}
want := r.URL.Query().Get("agent")
for _, a := range sessions.Agents() {
j := manageAgentJSON{AgentCount: a, Name: a.Agent, Icon: "generic"}
if l := looks[a.Agent]; l != nil {
j.Name, j.Icon = l.Name, l.Icon
}
out.Agents = append(out.Agents, j)
if a.Agent == want {
out.Agent = want
}
}
if out.Agent == "" && len(out.Agents) > 0 {
out.Agent = out.Agents[0].Agent
}
if out.Agent != "" {
out.Sessions = sessions.ListAgent(out.Agent)
for i := range out.Sessions {
out.Sessions[i].Path = tilde(out.Sessions[i].Path)
}
}
writeJSON(rw, out)
})
// delete moves the sessions named to magpie's trash, one by one; one
// still being written is left, and said so.
mux.HandleFunc("POST /api/sessions/delete", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
Agent string `json:"agent"`
IDs []string `json:"ids"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
if !sessions.Deletable(in.Agent) {
fail(rw, errors.New("magpie can't delete this agent's sessions"))
return
}
type refused struct {
ID string `json:"id"`
Error string `json:"error"`
Active bool `json:"active"`
}
out := struct {
Deleted []string `json:"deleted"`
Refused []refused `json:"refused"`
}{Deleted: []string{}, Refused: []refused{}}
for _, id := range in.IDs {
if _, err := sessions.Delete(in.Agent, id); err != nil {
out.Refused = append(out.Refused, refused{ID: id, Error: err.Error(), Active: errors.Is(err, sessions.ErrActive)})
continue
}
out.Deleted = append(out.Deleted, id)
}
forgetStats()
writeJSON(rw, out)
})
mux.HandleFunc("POST /api/sessions/restore", func(rw http.ResponseWriter, r *http.Request) {
var in struct {
Key string `json:"key"`
}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
fail(rw, err)
return
}
t, err := sessions.Restore(in.Key)
if err != nil {
fail(rw, err)
return
}
forgetStats()
writeJSON(rw, map[string]string{"agent": t.Agent, "id": t.ID, "title": cmp.Or(t.Title, t.ID)})
})
}
// forgetStats has the Usage page's session stats read again: a session
// deleted or restored changes them.
func forgetStats() {
statsMemo.Lock()
statsMemo.m = nil
statsMemo.Unlock()
}