Files
magpie/internal/sessions/carry.go
T
yetone b96d55fc99 sessions: a session's conversation can be read in magpie, and a Pi session carried on in oh-my-pi (sxwedo, #845) — the Sessions page could resume a session only in the agent that wrote it, and showed nothing of what was said. A session's details now have Show conversation: magpie reads the agent's own session file (Claude Code, Qoder, Qoder CN, Codex, Pi, oh-my-pi; found by agent and id, never a path from the page; read only, no copy kept, 400 KB at most with "There was more than is shown here") and lists the user's words, the model's, each tool call by name and its result, thinking and summaries folded. For Pi and oh-my-pi it follows the branch the session is on (parentId from the last entry), not the turns left on an abandoned one. A Pi session also has Continue in, the app's menu (no <select>): oh-my-pi's omp --fork <file> in the session's folder, copied or opened in the session terminal (the server makes the command from the session it finds; the page sends agent, id, in), and the details say it. omp --fork loads the Pi file, migrates it and writes a new oh-my-pi session, leaving Pi's untouched; with the owner's Pi sessions copied into a sandbox HOME, oh-my-pi's own SessionManager forked them whole (4→5 and 46→47 messages, its system line added; text, thinking and tool calls kept) and the source files' sha256 didn't change. Only that pair is offered: Pi can't read an oh-my-pi file (it wants the session header first, oh-my-pi writes a title line first), oh-my-pi takes Claude Code and Codex only from its own import picker, not a given session, and the other agents' formats differ, so carrying them on would mean converting them, which isn't done. zh, ja, de strings. TestTranscriptClaude, TestTranscriptCodex, TestTranscriptPiBranch, TestTranscriptOmp, TestTranscriptUnsupported, TestTranscriptCut, TestCarryPiToOmp; sessions-talk.test.cjs (WebKit, en and zh) fails on the old code. sessions-*, click-scroll, gui-ja and gui-de pass on WebKit; session-terminal and session-identity fail the same on origin/main. go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-05 10:08:35 +08:00

69 lines
2.0 KiB
Go

package sessions
// Carrying a session on in another agent (#845): only where that agent reads
// the session's own file as it is, so nothing is converted, and the file is
// left as it was.
//
// oh-my-pi grew from Pi and reads Pi's sessions: `omp --fork <file>` loads a
// Pi session (its version-3 entries, messages, tool calls and results,
// thinking; the model from its assistant messages, as omp names models
// provider/id) and writes a new omp session from it, the Pi file untouched.
// The other way doesn't work: Pi opens a file only when its first line is
// the session header, and omp's first line is its title. Other agents keep
// their sessions in forms of their own, which would have to be converted.
import (
"os"
"os/exec"
"runtime"
"strings"
)
// Carry is another agent a session can be carried on in, and the shell
// line that does it in the session's folder.
type Carry struct {
Agent string `json:"agent"`
Command string `json:"command"`
}
// ompThere says whether oh-my-pi is installed: its command, or its folder.
// A var so tests can say.
var ompThere = func() bool {
if _, err := exec.LookPath("omp"); err == nil {
return true
}
_, err := os.Stat(OmpDir())
return err == nil
}
// carries are the other agents a session can be carried on in.
func carries(s Session) []Carry {
if s.ReadOnly || s.WSL != "" || s.Path == "" || s.Cwd == "" {
return nil
}
switch s.Agent {
case "pi":
if !ompThere() {
return nil
}
return []Carry{{Agent: "omp", Command: inFolder(s.Cwd, "omp --fork "+argQuote(s.Path))}}
}
return nil
}
// inFolder is a shell line running run in the folder cwd.
func inFolder(cwd, run string) string {
if runtime.GOOS == "windows" {
return "Set-Location -LiteralPath " + argQuote(cwd) + "; " + run
}
return "cd " + argQuote(cwd) + " && " + run
}
// argQuote is s as one argument of this system's shell.
func argQuote(s string) string {
if runtime.GOOS == "windows" {
return "'" + strings.ReplaceAll(s, "'", "''") + "'"
}
return shellQuote(s)
}