agy's conversations were on no Sessions page: internal/sessions had no lister for them, only the calls reader of #2097. Now each ~/.gemini/antigravity-cli/brain/<id>/.system_generated/logs/transcript_full.jsonl is a session (agy.go). Its steps give the start and last times, the first prompt (taken out of agy's <USER_REQUEST> wrapper), prompts, replies, tool calls, and tokens under the model "agy", since no step names one. A step written again replaces its tokens, read on or read whole. The title and folder come from the conversation's conversation_summaries.db row: title, and workspace_uris through agyFolder, which turns file:///C:/... and file:///c%3A/... into C:\... and a host into \\host\share on Windows. agy writes the title after the transcript, so the row is the file's rev. The database is read again only when it or its -wal changed, and a failed read keeps the rows read before. A row with conversations/<id>.db and no transcript (#1718's case) is listed from the row alone, with no tokens and no conversation to open. Resume is "agy --conversation <id>". When agy is set to use magpie, it is the agent's launch command (gateway variables and --model, the PowerShell form on Windows) with --conversation after it, through sessions.AgyStart, which package agent sets. The transcript (user text, thinking, text, tool calls, tool results) can be opened and searched. Format read from real files: agy 1.3.5 and 1.4.7 in a sandbox HOME against a fake Gemini endpoint, with plain replies, a read_file tool call with thinking, invalid tool calls, and a conversation picked up again with --conversation from another folder. testdata/agy-sessions is three of those conversations with paths rewritten to /work. Resume tried for real in the sandbox: `agy --conversation <id> -p again` from another folder appended steps 2-4 to the same transcript and kept the row's folder. Interactive `agy --conversation <id>` (in tmux) showed the earlier turns. Run at this tree, rebased on origin/main: - gofmt -l internal clean - go vet: darwin; GOOS=linux -tags nogui; GOOS=windows - GOOS=windows go build ./...; GOOS=linux go build -tags nogui ./... - go test -count=1 ./internal/sessions ./internal/gui ./internal/agent: ok before the row-only change. After it, sessions ok, and gui had one red, TestMainWindowCorners killed at its 40s deadline at load ~300 (#1936). It passes alone (47s). - GUI (playwright 1.62.0, chromium and webkit, en and zh): i18n-keys, gui-de, gui-ja, gui-zh-tw, session-terminal, sessions-bar-names, sessions-manage, sessions-talk, sessions-toolbar: 49 of 49. No GUI asset changed. Tab names come from the agent registry. Fails without the change (broken by hand, each one red): - agyFiles out of allFiles: TestAgySessions, TestAgyTitleLater, TestAgyTranscript - agy out of refresh's DBRevision check: TestAgyTitleLater - no dropUse of a rewritten step: TestAgyGrows (4510 input, want 3010) - AgyStart ignored: TestAgySessions (wired resume) - drive letter not handled: TestAgyFolder - agy out of HasTranscript: TestAgySessions, TestAgyTranscript - the row-only listing off: TestAgyRowOnly; readable() off: TestAgyRowOnly Not tried: agy on Windows. The Windows box (10.0.0.132) didn't answer ssh, so the Windows paths are covered by unit tests only (agyFolder, the Set-Location resume form). A wired resume through magpie's real gateway was not tried. Not done: subagents (parent_conversation_id), which I never saw agy write. The model name, which is only in conversations/<id>.db's protobuf (#3147). Deleting an agy session from magpie. agy in WSL: #3595. Side effect: agy's self-updater ran in the sandbox and replaced the live ~/.local/bin/agy, 1.3.5 -> 1.4.7.
211 lines
6.0 KiB
Go
211 lines
6.0 KiB
Go
package sessions
|
|
|
|
// A session's whole conversation, read from the agent's own file when it is
|
|
// asked for: what was said, in order, by the user, the model and its tools.
|
|
// The file is only read; magpie keeps no copy.
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"math"
|
|
)
|
|
|
|
// Transcript is what was said in a session, in the order it was said.
|
|
type Transcript struct {
|
|
Parts []Part `json:"parts"`
|
|
Cut bool `json:"cut,omitempty"` // there was more than is kept here
|
|
Source string `json:"source,omitempty"`
|
|
Captured int `json:"captured,omitempty"`
|
|
}
|
|
|
|
// transcriptMax is the characters of all of a transcript's parts.
|
|
const transcriptMax = 400_000
|
|
|
|
// ErrNoTranscript is a session whose conversation magpie can't read: an
|
|
// agent that keeps it in a database, or in a form not read here.
|
|
var ErrNoTranscript = errors.New("this agent's conversations can't be shown")
|
|
|
|
// HasTranscript says whether an agent's sessions can be read as a
|
|
// transcript.
|
|
func HasTranscript(agent string) bool {
|
|
switch agent {
|
|
case "claude", "qoder", "qoder-cn", "codex", "pi", "omp", "cursor", "opencode", "kimi", "agy":
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// readable says whether a session's conversation can be read: its agent's
|
|
// can, and its file is the one they are read from (an agy conversation
|
|
// listed from its row alone has none).
|
|
func readable(s Session) bool {
|
|
return HasTranscript(s.Agent) && !(s.Agent == "agy" && agyRowOnly(s.Path))
|
|
}
|
|
|
|
func (t *Transcript) add(_ bool, p Part) bool {
|
|
size := 0
|
|
for _, x := range t.Parts {
|
|
size += len(x.Text)
|
|
}
|
|
if size >= transcriptMax {
|
|
t.Cut = true
|
|
return false
|
|
}
|
|
if p, ok := keep(p); ok {
|
|
t.Parts = append(t.Parts, p)
|
|
}
|
|
return true
|
|
}
|
|
|
|
// TranscriptOf reads a session's conversation from its (main) file.
|
|
func TranscriptOf(s Session) (Transcript, error) {
|
|
out := Transcript{Parts: []Part{}}
|
|
if !HasTranscript(s.Agent) || s.Path == "" {
|
|
return out, ErrNoTranscript
|
|
}
|
|
return out, readTranscript(s, out.add)
|
|
}
|
|
|
|
// readTranscript tells add each part of a session's conversation, in order,
|
|
// as its agent's file has it.
|
|
func readTranscript(s Session, add func(bool, Part) bool) error {
|
|
return readTranscriptOf(s, nil, add)
|
|
}
|
|
|
|
// readTranscriptOf is readTranscript decoding only the lines keep says yes
|
|
// to, where a line stands alone (Claude's, Codex's, Kimi's); Pi's lines are a tree,
|
|
// read whole.
|
|
func readTranscriptOf(s Session, keep func([]byte) bool, add func(bool, Part) bool) error {
|
|
whole := Call{Agent: s.Agent, File: s.Path, To: math.MaxInt64}
|
|
switch s.Agent {
|
|
case "opencode":
|
|
return openCodeTranscript(s, add)
|
|
case "cursor":
|
|
return cursorTranscript(s.Path, add)
|
|
case "codex":
|
|
return codexContentOf(whole, keep, add)
|
|
case "pi", "omp":
|
|
return piTranscript(s.Path, add)
|
|
case "kimi":
|
|
return kimiTranscript(s.Path, keep, add)
|
|
case "agy":
|
|
return agyTranscript(s.Path, keep, add)
|
|
default:
|
|
return claudeContentOf(whole, keep, add)
|
|
}
|
|
}
|
|
|
|
// piEntry is a line of a Pi (or oh-my-pi) session: an entry of its tree.
|
|
type piEntry struct {
|
|
Type string `json:"type"`
|
|
ID string `json:"id"`
|
|
ParentID string `json:"parentId"`
|
|
Summary string `json:"summary"` // compaction, branch_summary
|
|
Message *struct {
|
|
Role string `json:"role"`
|
|
ToolName string `json:"toolName"`
|
|
Model string `json:"model"` // an assistant's: the one it was answered by (#2253)
|
|
Content json.RawMessage `json:"content"`
|
|
} `json:"message"`
|
|
}
|
|
|
|
var piMessage = []byte(`"type":"message"`)
|
|
|
|
// piTranscript reads the branch the session is on: a Pi session is a tree,
|
|
// each entry naming its parent, and the last entry written is where it
|
|
// stands (an earlier branch, left by /tree or a retry, isn't said again).
|
|
func piTranscript(path string, add func(bool, Part) bool) error {
|
|
byID := map[string]piEntry{}
|
|
last := ""
|
|
_, err := scanAt(path, 0, nil, func(b []byte, _, _ int64) bool {
|
|
var e piEntry
|
|
if json.Unmarshal(b, &e) != nil || e.ID == "" || e.Type == "session" || e.Type == "title" {
|
|
return true
|
|
}
|
|
if !bytes.Contains(b, piMessage) && e.Type != "compaction" && e.Type != "branch_summary" {
|
|
// keep the tree whole without holding what isn't shown
|
|
e = piEntry{Type: e.Type, ID: e.ID, ParentID: e.ParentID}
|
|
}
|
|
byID[e.ID], last = e, e.ID
|
|
return true
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var branch []piEntry
|
|
for id, n := last, 0; id != "" && n <= len(byID); n++ {
|
|
e, ok := byID[id]
|
|
if !ok {
|
|
break
|
|
}
|
|
branch = append(branch, e)
|
|
id = e.ParentID
|
|
}
|
|
for i := len(branch) - 1; i >= 0; i-- {
|
|
for _, p := range piParts(branch[i]) {
|
|
if p.Role == "assistant" && branch[i].Message != nil {
|
|
p.Model = branch[i].Message.Model
|
|
}
|
|
if !add(p.Role != "assistant", p) {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// piParts are what an entry said.
|
|
func piParts(e piEntry) []Part {
|
|
switch e.Type {
|
|
case "compaction", "branch_summary":
|
|
return []Part{{Role: "user", Kind: "context", Text: e.Summary}}
|
|
case "message":
|
|
default:
|
|
return nil
|
|
}
|
|
if e.Message == nil {
|
|
return nil
|
|
}
|
|
m := e.Message
|
|
var role string
|
|
switch m.Role {
|
|
case "user":
|
|
role = "user"
|
|
case "assistant":
|
|
role = "assistant"
|
|
case "toolResult":
|
|
return []Part{{Role: "tool", Kind: "tool_result", Name: m.ToolName, Text: resultText(m.Content)}}
|
|
default:
|
|
return nil
|
|
}
|
|
var s string
|
|
if json.Unmarshal(m.Content, &s) == nil {
|
|
return []Part{textPart(role, s)}
|
|
}
|
|
var blocks []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
Thinking string `json:"thinking"`
|
|
Name string `json:"name"`
|
|
Arguments json.RawMessage `json:"arguments"`
|
|
}
|
|
if json.Unmarshal(m.Content, &blocks) != nil {
|
|
return nil
|
|
}
|
|
var parts []Part
|
|
for _, b := range blocks {
|
|
switch b.Type {
|
|
case "text":
|
|
parts = append(parts, textPart(role, b.Text))
|
|
case "thinking":
|
|
parts = append(parts, Part{Role: role, Kind: "thinking", Text: b.Thinking})
|
|
case "toolCall":
|
|
parts = append(parts, Part{Role: role, Kind: "tool_use", Name: b.Name, Text: pretty(b.Arguments)})
|
|
case "image":
|
|
parts = append(parts, Part{Role: role, Kind: "image", Text: "[image]"})
|
|
}
|
|
}
|
|
return parts
|
|
}
|