Files
magpie/internal/sessions/traces_gemini.go
T
tobitege 21702d01ff Add German translation; harden paths against unset env vars (#770)
* Add German translation and harden paths against unset env vars

Add a German locale to the GUI, tray menu, usage alerts and website home page, and refuse to start when the home folder is unset or relative so nothing is written under the working folder; the pi plugin no longer adopts the user's PI_CODING_AGENT_DIR, and an inherited APPIMAGE no longer selects portable mode. Scripts refuse to run from the wrong folder or empty a shared out folder, and a new internal/testenv sandboxes every test package from the user's real files, which also fixes the Claude picker and Termux installer tests on Windows.

* Makefile: know magpie's folder by its go.mod, so paths with spaces work

The check compared $(CURDIR) with $(dir $(abspath …)), which make splits on spaces, so a checkout under 'My Projects' or 'John Doe' refused every target. It now reads go.mod relative to the working folder and wants magpie's module there: make -C and a symlinked Makefile work, make -f from another folder still stops before any recipe runs.

* i18n: German for the strings main added since, with a parity test

75 strings main added (group order, patterns, slow-start wait, usage caps, RTK savings …) get their German, and two that main dropped go. gui-de.test.cjs checks, as gui-ja does, that de has every zh string with its placeholders and tags, and that the window reads in German when picked or on a German system.

* appdir: ignore relative folder variables when reading, keep them for children

CheckEnv no longer unsets anything. magpie reads the XDG folders, APPDATA/LOCALAPPDATA and every agent's folder variable through appdir.Getenv/LookupEnv, which pass over a relative value; the environment stays as it is, so the agents magpie starts get the variable as the user set it. That covers what proc.UserPath lends from the login shell too, which is set after main's check and reached the desktop app unchecked. On Windows a path rooted on the current drive (\Users\x, Git Bash's /c/x) counts as usable, and magpie version and help answer without a home, before run makes any folder.

* Sandbox the tests main added since

internal/wslrun gets testenv's TestMain, and four tests main added that set HOME alone set their home with testenv.SetHome, so on Windows they don't read the runner's profile through USERPROFILE.

* agentenv: PI_CONFIG_DIR and OPENCODE_DB take relative names on purpose

omp puts $PI_CONFIG_DIR under the home and OpenCode a relative $OPENCODE_DB in its data folder, so neither depends on the working folder: appdir no longer passes them over.

* i18n: German for the Library's multi-select and System One's decision API

The 19 strings main added since (#791's skill multi-select, the decision API snippets) get their German; the one main replaced (four APIs → five APIs) goes.

* appdir: tidy the import into magpie's group; say what Go makes of /c/x

The appdir import joins the group of magpie's own imports where a file has one. rooted's comment now says that Go takes a Git Bash path like /c/x as \c\x on the current drive, not C:\x.
2026-10-04 23:30:56 +08:00

362 lines
9.0 KiB
Go

package sessions
import (
"bufio"
"encoding/json"
"os"
"path/filepath"
"strings"
"time"
"github.com/yetone/magpie/internal/appdir"
)
// Gemini moved from rewritten JSON snapshots to append-only JSONL metadata,
// messages, $set/$patch and $rewindTo records. Both stores use the same message
// schema. Message/tool timestamps are event boundaries, not measured latency.
func geminiTraceFiles() []file {
home, _ := os.UserHomeDir()
if custom := appdir.Getenv("GEMINI_CLI_HOME"); custom != "" {
home = custom
}
root := filepath.Join(home, ".gemini", "tmp")
var out []file
for _, pattern := range []string{"*/chats/session-*.json", "*/chats/session-*.jsonl"} {
paths, _ := SessionGlob(filepath.Join(root, pattern))
for _, path := range paths {
f := file{agent: "gemini", path: path, main: true}
if stat(&f) {
out = append(out, f)
}
}
}
return out
}
type geminiTraceTool struct {
ID, Name, Status string
Timestamp time.Time
Args, Result json.RawMessage
}
type geminiTraceMessage struct {
ID, Type, Model string
Timestamp time.Time
Content json.RawMessage
ToolCalls []geminiTraceTool
Tokens *struct{ Input, Output, Cached, Thoughts int }
prompt bool
}
type geminiTraceState struct {
session string
order []string
messages map[string]*geminiTraceMessage
}
func (s *geminiTraceState) put(m geminiTraceMessage, bodies bool) {
if m.ID == "" {
return
}
// A recorded functionResponse is a tool-result turn, not a user prompt.
m.prompt = m.Type == "user"
var parts []struct{ FunctionResponse json.RawMessage }
if json.Unmarshal(m.Content, &parts) == nil {
for _, p := range parts {
if len(p.FunctionResponse) > 0 {
m.prompt = false
break
}
}
}
if !bodies {
m.Content = nil
for i := range m.ToolCalls {
m.ToolCalls[i].Args = nil
m.ToolCalls[i].Result = nil
}
}
if s.messages[m.ID] == nil {
s.order = append(s.order, m.ID)
}
s.messages[m.ID] = &m
// Keep the last user prompt plus a bounded window of recent messages.
if len(s.order) > 256 {
lastUser := ""
for _, id := range s.order {
if s.messages[id].prompt {
lastUser = id
}
}
keep := append([]string(nil), s.order[len(s.order)-255:]...)
if lastUser != "" && !containsTraceID(keep, lastUser) {
keep = append([]string{lastUser}, keep...)
}
retained := map[string]bool{}
for _, id := range keep {
retained[id] = true
}
for id := range s.messages {
if !retained[id] {
delete(s.messages, id)
}
}
s.order = keep
}
}
func containsTraceID(ids []string, id string) bool {
for _, v := range ids {
if v == id {
return true
}
}
return false
}
func (s *geminiTraceState) line(b []byte, bodies bool) {
var o struct {
SessionID string
Messages []geminiTraceMessage
Set *struct {
SessionID string
Messages []geminiTraceMessage
} `json:"$set"`
Rewind string `json:"$rewindTo"`
Patch *struct {
ID string
Content json.RawMessage
ToolCalls []struct {
ID string
Result json.RawMessage
}
Updates []json.RawMessage
RemoveIDs, OrderIDs []string
} `json:"$patch"`
}
if json.Unmarshal(b, &o) != nil {
return
}
if o.SessionID != "" {
s.session = o.SessionID
}
for _, m := range o.Messages {
s.put(m, bodies)
}
if o.Set != nil {
if o.Set.SessionID != "" {
s.session = o.Set.SessionID
}
if o.Set.Messages != nil {
s.messages = map[string]*geminiTraceMessage{}
s.order = nil
for _, m := range o.Set.Messages {
s.put(m, bodies)
}
}
}
if o.Rewind != "" {
for i, id := range s.order {
if id == o.Rewind {
for _, remove := range s.order[i+1:] {
delete(s.messages, remove)
}
s.order = s.order[:i+1]
break
}
}
}
if p := o.Patch; p != nil {
if m := s.messages[p.ID]; m != nil {
if p.Content != nil && bodies {
m.Content = p.Content
}
for _, tool := range p.ToolCalls {
for i := range m.ToolCalls {
if m.ToolCalls[i].ID == tool.ID && bodies {
m.ToolCalls[i].Result = tool.Result
}
}
}
}
for _, u := range p.Updates {
wrapped, _ := json.Marshal(map[string]json.RawMessage{"$patch": u})
s.line(wrapped, bodies)
}
for _, id := range p.RemoveIDs {
delete(s.messages, id)
}
var keep []string
for _, id := range s.order {
if s.messages[id] != nil && !containsTraceID(p.OrderIDs, id) {
keep = append(keep, id)
}
}
for _, id := range p.OrderIDs {
if s.messages[id] != nil {
keep = append(keep, id)
}
}
s.order = keep
}
var m geminiTraceMessage
if json.Unmarshal(b, &m) == nil && m.ID != "" && m.Type != "" {
s.put(m, bodies)
}
}
func (r *TraceReader) readGemini(f file, bodies bool, budget *int64) []TraceSpan {
snap := r.snapshot(f.path)
cursor := r.files[f.path]
if snap.gemini == nil {
snap.gemini = &geminiTraceState{messages: map[string]*geminiTraceMessage{}}
}
h, err := os.Open(f.path)
if err != nil {
return nil
}
defer h.Close()
info, err := h.Stat()
if err != nil {
return nil
}
if cursor.info != nil && (!os.SameFile(cursor.info, info) || f.size < cursor.offset) {
cursor.offset = 0
snap.gemini = &geminiTraceState{messages: map[string]*geminiTraceMessage{}}
}
cursor.info = info
if strings.HasSuffix(f.path, ".json") {
// Legacy files are atomically replaced; only parse complete snapshots.
if f.size > 32<<20 || *budget <= 0 {
return nil
}
b, err := os.ReadFile(f.path)
if err != nil || !json.Valid(b) {
return nil
}
*budget -= int64(len(b))
snap.gemini = &geminiTraceState{messages: map[string]*geminiTraceMessage{}}
snap.gemini.line(b, bodies)
cursor.offset = f.size
} else {
if _, err = h.Seek(cursor.offset, 0); err != nil {
return nil
}
reader := bufio.NewReader(h)
for *budget > 0 {
b, err := reader.ReadBytes('\n')
if err != nil {
break
}
cursor.offset += int64(len(b))
*budget -= int64(len(b))
snap.gemini.line(b, bodies)
}
}
if cursor.offset == f.size {
cursor.size, cursor.mod = f.size, f.mod
}
// Wait for the current file batch to settle: text, tokens and toolCalls can
// be appended separately. Tool completions and earlier responses export now.
settled := f.mod.Before(time.Now().Add(-2 * time.Second))
snap.settled = settled
return r.observations(snap, snap.gemini.spans(bodies, settled))
}
func (s *geminiTraceState) spans(bodies, settled bool) []TraceSpan {
c := &traceCursor{agent: "gemini", session: s.session}
var t *traceTurn
var out []TraceSpan
finish := func() {
if t != nil {
root := c.root(t, t.last, t.failed)
root.Inferred = true
out = append(out, root)
}
}
for i, id := range s.order {
m := s.messages[id]
if m == nil {
continue
}
if m.prompt {
finish()
t = &traceTurn{id: m.ID, start: m.Timestamp, last: m.Timestamp}
if bodies {
t.input = string(m.Content)
}
continue
}
if t == nil {
continue
}
if m.Type == "error" {
t.failed = true
t.last = m.Timestamp
continue
}
if m.Type != "gemini" {
continue
}
next := i < len(s.order)-1
if next || settled {
span := c.span(t.id, m.ID, "model "+m.Model, "generation", t.last, m.Timestamp)
span.Model, span.Provider, span.Inferred = m.Model, "google", true
if u := m.Tokens; u != nil {
span.Tokens = Tokens{Input: max(0, u.Input-u.Cached), Output: u.Output + u.Thoughts, CacheRead: u.Cached}
span.Reasoning = u.Thoughts
}
if bodies {
span.Input = t.input
span.Output = string(m.Content)
t.output = span.Output
}
out = append(out, span)
}
if m.Timestamp.After(t.last) {
t.last = m.Timestamp
}
for _, tool := range m.ToolCalls {
if tool.Status != "success" && tool.Status != "error" && tool.Status != "cancelled" {
continue
}
// Gemini stamps tool state updates. Its result record supplies the end;
// the owning model message supplies a conservative start boundary.
span := c.span(t.id, m.ID+":"+tool.ID, tool.Name, "tool", m.Timestamp, tool.Timestamp)
span.Inferred = true
span.Error = tool.Status != "success"
if bodies {
span.Input = string(tool.Args)
span.Output = geminiTraceResult(tool.Result, tool.ID, tool.Name)
}
out = append(out, span)
if tool.Timestamp.After(t.last) {
t.last = tool.Timestamp
}
t.failed = t.failed || span.Error
}
}
finish()
return out
}
// Some Gemini versions attach a whole parallel functionResponse turn to each
// tool. Select that tool's own response, rather than showing its siblings too.
func geminiTraceResult(raw json.RawMessage, id, name string) string {
var parts []json.RawMessage
if json.Unmarshal(raw, &parts) != nil {
return string(raw)
}
var selected []json.RawMessage
for _, part := range parts {
var p struct{ FunctionResponse *struct{ ID, Name string } }
if json.Unmarshal(part, &p) != nil {
continue
}
if p.FunctionResponse == nil || p.FunctionResponse.ID == id || p.FunctionResponse.ID == "" && p.FunctionResponse.Name == name {
selected = append(selected, part)
}
}
if len(selected) == 0 {
return ""
}
b, _ := json.Marshal(selected)
return string(b)
}