Usage -> Sessions' conversation said only "Assistant", so which model wrote a reply couldn't be told, though the agents' files name it. - sessions.Part gains model (omitempty), set on assistant parts only, from the part's own line: Claude Code's message.model (the model that answered; none for its "<synthetic>" lines), Codex's turn_context.model (the model the turn was sent to; a /model between turns moves the next turn), Pi's and omp's message.model, OpenCode's modelID and OpenCode 2's model.id. - Kimi Code (its model is only on a usage.record written after the reply), Cursor (no model in the transcript) and gateway transcripts (GatewayTurn.Model is the model asked for, not the one that answered) name none. Nothing is guessed from nearby requests. - The reader (sessions client.js) says it muted after the role, once a reply at its first part and again only where it changes inside the reply; the tooltip is "Model: <id>" (an existing string, translated). ledSaid takes the model as an argument, so the Usage page's per-request content, whose row already names its model, is unchanged. No layout change: one muted span in the existing header line, ellipsized at narrow widths. Run: - TestTranscriptModelClaude/Codex/Pi/OpenCodeV2/OpenCodeFiles: all five fail on the old content.go/transcript*.go, pass on the new. - sessions-talk-model.test.cjs: 20/20 (chromium+webkit, en/zh/zh-TW/ ja/de, 1100 and 420). Fails on the old app.js/client.js, and with the once-a-reply rule removed by hand (label every assistant part). - sessions-talk, sessions-export, gateway-conversations, usage-ledger-content, usage-ledger-detail GUI tests: all pass. - go test -tags nogui ./internal/sessions ./internal/gui ./internal/gateway: ok. gofmt -l clean; go vet -tags nogui darwin/ linux/windows clean. Full suite for this push (#2257, #2251, #2254, #2253) at 53677ed8d: go test -tags nogui -p 4 -timeout 40m ./... with pipefail: every package ok. (An earlier full run caught davsync's TestSyncLibrary and profile's TestOldProfiles/TestProfileCarriesLibrary relying on a gone CLI after #2254; their sandboxes now have the CLIs, in #2254's commit.) Not tried: the real app against a live Codex rollout (fixtures follow the lines the readers already parse for usage).
133 lines
3.5 KiB
Go
133 lines
3.5 KiB
Go
package sessions
|
|
|
|
import (
|
|
"encoding/json"
|
|
"sort"
|
|
)
|
|
|
|
// openCodeTranscript reads the conversation in message order from the same
|
|
// store the listing chose, including while a v1 to v2 migration is underway.
|
|
func openCodeTranscript(s Session, add func(bool, Part) bool) error {
|
|
dbReadMu.Lock()
|
|
defer dbReadMu.Unlock()
|
|
defer closeDBs()
|
|
for _, f := range openCodeFiles() {
|
|
if f.sid != s.ID {
|
|
continue
|
|
}
|
|
if v, ok := f.oc.(ocV2DB); ok {
|
|
rows, err := v.db.Query("SELECT type,data FROM session_message WHERE session_id=? AND type IN ('user','assistant') ORDER BY seq", s.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var role string
|
|
var raw []byte
|
|
if err := rows.Scan(&role, &raw); err != nil {
|
|
return err
|
|
}
|
|
var m struct {
|
|
Text string `json:"text"`
|
|
Content []json.RawMessage `json:"content"`
|
|
Model struct {
|
|
ID string `json:"id"`
|
|
} `json:"model"`
|
|
}
|
|
if err := json.Unmarshal(raw, &m); err != nil {
|
|
return err
|
|
}
|
|
if role == "user" {
|
|
if !add(true, textPart(role, m.Text)) {
|
|
return nil
|
|
}
|
|
continue
|
|
}
|
|
for _, p := range m.Content {
|
|
if !openCodeTranscriptPart(role, m.Model.ID, p, add) {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return rows.Err()
|
|
}
|
|
var messages []ocMessage
|
|
for _, b := range f.oc.messages(f.sid) {
|
|
var m ocMessage
|
|
if json.Unmarshal(b, &m) == nil {
|
|
messages = append(messages, m)
|
|
}
|
|
}
|
|
sort.SliceStable(messages, func(i, j int) bool {
|
|
if messages[i].Time.Created != messages[j].Time.Created {
|
|
return messages[i].Time.Created < messages[j].Time.Created
|
|
}
|
|
return messages[i].ID < messages[j].ID
|
|
})
|
|
for _, m := range messages {
|
|
for _, p := range f.oc.parts(m.ID) {
|
|
if !openCodeTranscriptPart(m.Role, m.ModelID, p, add) {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
return ErrNoTranscript
|
|
}
|
|
|
|
// openCodeTranscriptPart tells add one part of a message; model is the
|
|
// message's own (an assistant's modelID, or model.id in OpenCode 2), said
|
|
// with each of its assistant parts (#2253).
|
|
func openCodeTranscriptPart(role, model string, raw []byte, add func(bool, Part) bool) bool {
|
|
say := add
|
|
add = func(input bool, q Part) bool {
|
|
if q.Role == "assistant" {
|
|
q.Model = model
|
|
}
|
|
return say(input, q)
|
|
}
|
|
var p struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
Name string `json:"name"`
|
|
Tool string `json:"tool"`
|
|
Synthetic bool `json:"synthetic"`
|
|
Ignored bool `json:"ignored"`
|
|
State struct {
|
|
Input json.RawMessage `json:"input"`
|
|
Output string `json:"output"`
|
|
Content []struct {
|
|
Type string `json:"type"`
|
|
Text string `json:"text"`
|
|
} `json:"content"`
|
|
} `json:"state"`
|
|
}
|
|
if json.Unmarshal(raw, &p) != nil || p.Synthetic || p.Ignored {
|
|
return true
|
|
}
|
|
switch p.Type {
|
|
case "text":
|
|
return add(role == "user", textPart(role, p.Text))
|
|
case "reasoning":
|
|
return add(false, Part{Role: role, Kind: "thinking", Text: p.Text})
|
|
case "tool":
|
|
name := p.Name
|
|
if name == "" {
|
|
name = p.Tool
|
|
}
|
|
if !add(false, Part{Role: role, Kind: "tool_use", Name: name, Text: string(p.State.Input)}) {
|
|
return false
|
|
}
|
|
if p.State.Output != "" && !add(false, Part{Role: "tool", Kind: "tool_result", Name: name, Text: p.State.Output}) {
|
|
return false
|
|
}
|
|
for _, r := range p.State.Content {
|
|
if r.Type == "text" && !add(false, Part{Role: "tool", Kind: "tool_result", Name: name, Text: r.Text}) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|