diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index 4c4e92f78..55e6d87c6 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -264,6 +264,78 @@ and `TestGatewayConversationClientRequestPrefixedSession` for the two rules abov `gateway-conversations.test.cjs`, `sessions-talk.test.cjs`, `sessions-gateway-delete.test.cjs`, and locale checks. +## Carrying a session on in another agent + +A session row's Continue in (`carryPick` in the Sessions plugin's +`client.js`) lists the other agents that can go on from it, from +`Session.Carry` (`carries` in +[`internal/sessions/carry.go`](../../internal/sessions/carry.go)). Nothing is +offered for a read-only or WSL session, or one with no file or folder. There +are two kinds: + +- **From the file as it is.** oh-my-pi forks a Pi session: `Carry.Command` + is `omp --fork ` run in the session's folder (#845). +- **Converted** (`Carry.Convert`, no command). A Claude Code session can go + on in Codex, and a Codex one in Claude Code, when the other agent is + installed (`agentThere`: its binary on the PATH or its config folder, + checked at most every 30s). Listing converts nothing. When the user picks + it, `POST /api/sessions/carry` `{agent, id, to, open}` calls + `sessions.CarryOver` + ([`carry_convert.go`](../../internal/sessions/carry_convert.go)). It writes + a **new** session in the target's own store and returns `{agent, id, + command}`, the target's resume line. The page copies it, or with `open` + (macOS app only) opens it in the session terminal. + +What a conversion carries: + +- **The user's and the agent's words, in order.** For Claude Code, the + chain of `parentUuid` from the last main-thread line back to the latest + compact boundary. Sidechains, `isMeta` lines and Claude Code's own text + are left out: slash-command echoes, ``, the local-command + caveat. For Codex, the `response_item`s, or a `compacted` line's + `replacement_history` in place of what came before it. Codex's + ``, ``, AGENTS.md and developer + messages are left out. +- **Pasted images**, as data URLs, into the target's own image block. +- **Shell calls**, in the target's native shape with their output: Claude + Code's Bash and Codex's `exec_command`, `shell`, `shell_command` and + `local_shell_call`. Any other tool (Read, Edit, Write, `apply_patch` as a + custom tool, MCP tools) is written as the agent's own text: its name, + input and result in fences. A result is cut at 64 KiB. +- **Not carried:** thinking, reasoning, signatures and encrypted content, and + a compaction encrypted by OpenAI. Of compactions, only magpie's own + summary is read. + +What it writes: + +- **Claude Code:** `/projects//.jsonl`. Lines are chained by `parentUuid`, with the field order + of Claude Code's own and `version` from the newest Claude Code session + listed. Assistant lines have model ``, so the usage reader + counts no calls and no cost for them. `claude --resume ` opens it. +- **Codex:** `/sessions/YYYY/MM/DD/rollout--.jsonl`. A `session_meta` carries no `base_instructions`; + Codex uses its own. `model_provider` is what config.toml selects now + (`CodexProviderIn`), so the thread is listed under the provider in use. + `response_item`s are written, plus the `event_msg` user and agent messages + the TUI shows as history. Codex indexes the file into its state database + itself on resume, so magpie writes no index. `codex resume ` opens it. + +The source file is only read. The new file is written to a temp file +beside it, synced and renamed, and is never written over an existing path. +`CarryOver` keeps the result per source path, target, size and mtime while +the new file exists, so asking twice for an unchanged session gives the +same one. + +Not converted: Pi, omp, OpenCode, Kimi, Cursor and the other agents, and +sessions in WSL. + +Verification: `TestCarryClaudeToCodex`, `TestCarryCodexToClaude` (real +Claude Code 2.1.310 and Codex 0.162.1 files in `testdata/carry`), +`TestCarryConvertOffered`, `TestCarryImagesAndCompaction` and +`TestCarryPiToOmp`; browser `sessions-convert.test.cjs` and +`sessions-talk.test.cjs`. + ## Usage over the gateway, and subagents `GET /v1/magpie/usage` and `GET /v1/magpie/usage/requests` diff --git a/internal/gui/assets/plugins/sessions/client.js b/internal/gui/assets/plugins/sessions/client.js index 3d861a770..8409c5e0e 100644 --- a/internal/gui/assets/plugins/sessions/client.js +++ b/internal/gui/assets/plugins/sessions/client.js @@ -1019,7 +1019,17 @@ magpie.plugin("sessions", (ctx) => { if (s.cwd) line(t("Folder"), s.cwd); line(t("Session ID"), s.id, copyBtn(s.id, t("Session id"))); if (s.resume) line(t("Resume"), el("code", "", s.resume), copyBtn(s.resume, t("Resume command"))); - for (const c of s.carry || []) line(t("Continue in {agent}", { agent: agentOf(c.agent).name }), el("code", "", c.command), copyBtn(c.command, t("Command"))); + for (const c of s.carry || []) { + const agent = agentOf(c.agent).name; + if (!c.convert) line(t("Continue in {agent}", { agent }), el("code", "", c.command), copyBtn(c.command, t("Command"))); + else { + // made only when asked: a new session of that agent's, this one only read + const b = copyBtn("", t("Command")); + b.title = t("Convert, then copy the command"); + b.onclick = (e) => { e.stopPropagation(); carryOver(s, c.agent, false, b); }; + line(t("Continue in {agent}", { agent }), el("span", "v", t("A new {agent} session is made from this conversation", { agent })), b); + } + } sessUsageDetail(d, s, line); if (s.path) line(t("File"), s.path + (s.files > 1 ? " " + t("+{n} more", { n: s.files - 1 }) : "")); if (s.transcript) { @@ -1086,9 +1096,10 @@ magpie.plugin("sessions", (ctx) => { const iconItems = (opts) => document.querySelectorAll(".proto-menu .pm-item").forEach((b, i) => opts[i]?.icon && b.firstChild.after(icon(opts[i].icon))); const agentOf = (id) => data?.agents.find((x) => x.agent === id) || { agent: id, name: id, icon: id }; - // carryPick is the other agents that can carry a session on from its - // file as it is (#845; oh-my-pi forks a Pi session), in the app's menu: - // copy the command, or open it in the session terminal. + // carryPick is the other agents that can carry a session on (#845): + // from its file as it is (oh-my-pi forks a Pi session), or from a new + // session of theirs converted from it (Claude Code and Codex), in the + // app's menu: copy the command, or open it in the session terminal. function carryPick(s) { const b = el("button", "sess-pick sess-carry"); b.type = "button"; @@ -1102,15 +1113,17 @@ magpie.plugin("sessions", (ctx) => { const opts = []; for (const c of s.carry) { const { name, icon: ic } = agentOf(c.agent); - opts.push({ v: "copy\x01" + c.agent, name, icon: ic, literalName: true, note: "Copy the command", title: c.command }); - if (data.terminal) opts.push({ v: "term\x01" + c.agent, name, icon: ic, literalName: true, note: "Open in session terminal", title: c.command }); + const title = c.convert ? t("A new {agent} session is made from this conversation", { agent: name }) : c.command; + opts.push({ v: "copy\x01" + c.agent, name, icon: ic, literalName: true, note: c.convert ? "Convert, then copy the command" : "Copy the command", title }); + if (data.terminal) opts.push({ v: "term\x01" + c.agent, name, icon: ic, literalName: true, note: c.convert ? "Convert, then open in session terminal" : "Open in session terminal", title }); } openProtoMenu(b, opts, "", (v) => { b.focus({ preventScroll: true }); const [how, to] = v.split("\x01"); const c = s.carry.find((x) => x.agent === to); if (!c) return; - if (how === "copy") copy(c.command, t("Command")); + if (c.convert) carryOver(s, to, how === "term"); + else if (how === "copy") copy(c.command, t("Command")); else api("sessions/terminal", { agent: s.agent, id: s.id, in: to }).then(() => status(t("Opening in session terminal"), "ok"), (err) => status(err.message, "err")); }, "Continue in another agent", "sess-carry-menu", "right"); iconItems(opts); @@ -1118,6 +1131,17 @@ magpie.plugin("sessions", (ctx) => { return b; } + // carryOver converts a session for another agent (Claude Code's for + // Codex, Codex's for Claude Code) when the user picks it, then copies the + // line that opens the new one, or opens it in the session terminal + function carryOver(s, to, open, btn) { + const agent = agentOf(to).name; + api("sessions/carry", { agent: s.agent, id: s.id, to, open }).then((r) => { + if (open) status(t("Made a new {agent} session from this one, opening in session terminal", { agent }), "ok"); + else copy(r.command, t("Command"), btn, t("Made a new {agent} session from this one, and copied the command that opens it", { agent })); + }, (err) => status(err.message, "err")); + } + // drawTalk fills box with what was said in a session, read from the // agent's own file when it is first shown (only read, never written) function drawTalk(box, s, sub = "") { diff --git a/internal/gui/assets/plugins/sessions/i18n.js b/internal/gui/assets/plugins/sessions/i18n.js index 5c0253d18..eb2c1a038 100644 --- a/internal/gui/assets/plugins/sessions/i18n.js +++ b/internal/gui/assets/plugins/sessions/i18n.js @@ -23,6 +23,11 @@ magpie.strings({ "Show {n} more folders": "显示其余 {n} 个文件夹", "Copy the command that resumes it: {cmd}": "复制继续这个会话的命令:{cmd}", "Open in session terminal": "在所选终端中打开", + "Convert, then copy the command": "转换后复制命令", + "Convert, then open in session terminal": "转换后在所选终端中打开", + "A new {agent} session is made from this conversation": "将从这段对话新建一个 {agent} 会话", + "Made a new {agent} session from this one, opening in session terminal": "已从此会话新建 {agent} 会话,正在所选终端中打开", + "Made a new {agent} session from this one, and copied the command that opens it": "已从此会话新建 {agent} 会话,并复制了打开它的命令", "Opening in session terminal": "正在所选终端中打开", "Claude Code's, Codex's, Hermes's, OpenCode's and Pi's sessions on this computer show up here, by the folder they ran in.": "本机 Claude Code、Codex、Hermes、OpenCode 和 Pi 的会话按运行目录列在这里。", "magpie can list {agent}'s sessions and resume them, but not delete them: they aren't kept as files of their own.": "可列出并继续 {agent} 的会话,但无法删除:它们没有独立文件。", @@ -95,6 +100,11 @@ magpie.strings({ "Show {n} more folders": "顯示其餘 {n} 個資料夾", "Copy the command that resumes it: {cmd}": "複製繼續這個會話的命令:{cmd}", "Open in session terminal": "在所選終端機中開啟", + "Convert, then copy the command": "轉換後複製命令", + "Convert, then open in session terminal": "轉換後在所選終端機中開啟", + "A new {agent} session is made from this conversation": "將從這段對話新建一個 {agent} 會話", + "Made a new {agent} session from this one, opening in session terminal": "已從此會話新建 {agent} 會話,正在所選終端機中開啟", + "Made a new {agent} session from this one, and copied the command that opens it": "已從此會話新建 {agent} 會話,並複製了開啟它的命令", "Opening in session terminal": "正在所選終端機中開啟", "Claude Code's, Codex's, Hermes's, OpenCode's and Pi's sessions on this computer show up here, by the folder they ran in.": "本機 Claude Code、Codex、Hermes、OpenCode 和 Pi 的會話按執行目錄列在這裡。", "magpie can list {agent}'s sessions and resume them, but not delete them: they aren't kept as files of their own.": "可列出並繼續 {agent} 的會話,但無法刪除:它們沒有獨立檔案。", @@ -167,6 +177,11 @@ magpie.strings({ "Show {n} more folders": "さらに {n} 個のフォルダを表示", "Copy the command that resumes it: {cmd}": "再開するコマンドをコピー:{cmd}", "Open in session terminal": "セッション用ターミナルで開く", + "Convert, then copy the command": "変換してコマンドをコピー", + "Convert, then open in session terminal": "変換してセッション用ターミナルで開く", + "A new {agent} session is made from this conversation": "この会話から新しい {agent} セッションを作ります", + "Made a new {agent} session from this one, opening in session terminal": "このセッションから新しい {agent} セッションを作り、セッション用ターミナルで開いています", + "Made a new {agent} session from this one, and copied the command that opens it": "このセッションから新しい {agent} セッションを作り、開くコマンドをコピーしました", "Opening in session terminal": "セッション用ターミナルで開いています", "Claude Code's, Codex's, Hermes's, OpenCode's and Pi's sessions on this computer show up here, by the folder they ran in.": "このコンピュータ上の Claude Code、Codex、Hermes、OpenCode、Pi のセッションが、実行されたフォルダごとにここに表示されます。", "magpie can list {agent}'s sessions and resume them, but not delete them: they aren't kept as files of their own.": "magpie は {agent} のセッションを一覧表示・再開できますが、削除はできません。セッションごとのファイルとして保存されていないためです。", @@ -239,6 +254,11 @@ magpie.strings({ "Show {n} more folders": "{n} weitere Ordner anzeigen", "Copy the command that resumes it: {cmd}": "Befehl zum Fortsetzen kopieren: {cmd}", "Open in session terminal": "Im Sitzungsterminal öffnen", + "Convert, then copy the command": "Umwandeln, dann Befehl kopieren", + "Convert, then open in session terminal": "Umwandeln, dann im Sitzungsterminal öffnen", + "A new {agent} session is made from this conversation": "Aus diesem Gespräch wird eine neue {agent}-Sitzung erstellt", + "Made a new {agent} session from this one, opening in session terminal": "Neue {agent}-Sitzung aus dieser erstellt, wird im Sitzungsterminal geöffnet", + "Made a new {agent} session from this one, and copied the command that opens it": "Neue {agent}-Sitzung aus dieser erstellt und den Befehl zum Öffnen kopiert", "Opening in session terminal": "Wird im Sitzungsterminal geöffnet", "Claude Code's, Codex's, Hermes's, OpenCode's and Pi's sessions on this computer show up here, by the folder they ran in.": "Sitzungen von Claude Code, Codex, Hermes, OpenCode und Pi auf diesem Computer erscheinen hier, gruppiert nach dem Ordner, in dem sie liefen.", "magpie can list {agent}'s sessions and resume them, but not delete them: they aren't kept as files of their own.": "magpie kann die Sitzungen von {agent} auflisten und fortsetzen, aber nicht löschen, da sie nicht als eigene Dateien vorliegen.", diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index add8cbe66..e9a6863ff 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -335,6 +335,42 @@ func sessionRoutes(mux *http.ServeMux, w Windows) { } rw.WriteHeader(http.StatusNoContent) }) + // carry converts a session for another agent (one its Carry offers with + // Convert): a new session of that agent's, in its own folder, the + // source only read. It runs only when the user picks the agent, never + // on a listing. It answers with the line that opens the new one, and, + // with Open, opens Terminal on it. + mux.HandleFunc("POST /api/sessions/carry", func(rw http.ResponseWriter, r *http.Request) { + var in struct { + Agent, ID, To string + Open bool + } + if err := json.NewDecoder(r.Body).Decode(&in); err != nil { + fail(rw, err) + return + } + if in.Open && (runtime.GOOS != "darwin" || isWeb(w)) { + fail(rw, errors.New("opening Terminal is only for the Mac app")) + return + } + s, ok := sessions.Find(in.Agent, in.ID) + if !ok { + fail(rw, errors.New("no such session")) + return + } + c, err := sessions.CarryOver(s, in.To) + if err != nil { + fail(rw, err) + return + } + if in.Open { + if err := openTerminal(c.Command, settings.Load().SessionTerminal); err != nil { + fail(rw, err) + return + } + } + writeJSON(rw, map[string]any{"agent": c.Agent, "id": c.ID, "command": c.Command}) + }) } // sessionMarkdown is a session's conversation as Markdown, and the name its diff --git a/internal/gui/tests/sessions-convert.test.cjs b/internal/gui/tests/sessions-convert.test.cjs new file mode 100644 index 000000000..5a4193050 --- /dev/null +++ b/internal/gui/tests/sessions-convert.test.cjs @@ -0,0 +1,151 @@ +// Run with Node's test runner and Playwright on the module path; see README.md. +// A Claude Code session carried on in Codex, and a Codex one in Claude Code +// (0xPlato on X): Continue in offers the other agent, converted only when +// it is picked. Listing posts nothing; picking posts sessions/carry with +// the session's agent and id, to, and open, then copies the command the +// server made, or says it is opening in the session terminal. The details +// say a new session is made, and their copy button converts too. App menu, +// never a