UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

44 lines • 2.31 kB
/** * Copies a session file into a new session under a caller-chosen id (senpi#5889), so work can go on from a session * that is read-only here: one that moved away, or a copy sealed by `revoke`. The copy's header carries the new id, so the * ownership entries it inherits name the old session and no longer apply (`ownership.ts`): the copy is writable. The * source is only read. */ import { readdir } from "node:fs/promises"; import { loadEntriesFromFileAsync, SessionManager, sessionFileNameId } from "../session-manager.js"; import { serializeByKey } from "../session-sidecar-store.js"; import { headerSessionId } from "./ownership.js"; class SessionCopyRefusal extends Error { constructor(code, detail) { super(detail); this.name = "SessionCopyRefusal"; this.code = code; } } async function idTaken(dir, sessionId) { const names = await readdir(dir).catch((error) => { if (error.code === "ENOENT") return []; throw error; }); return names.some((name) => sessionFileNameId(name) === sessionId); } export async function copySessionAs(options) { const entries = await loadEntriesFromFileAsync(options.sourceFile); const cwd = entries.find((entry) => entry.type === "session")?.cwd; if (!cwd) throw new Error(`The session at ${options.sourceFile} has no working directory in its header.`); if (headerSessionId(entries) === options.sessionId) throw new SessionCopyRefusal("invalid_move_request", "The copy needs a new session id: under the source's own id it would stay read-only."); const dir = options.sessionDir(cwd); return serializeByKey(`session-copy:${dir}:${options.sessionId}`, async () => { if (await idTaken(dir, options.sessionId)) throw new SessionCopyRefusal("move_target_occupied", `A session with id ${options.sessionId} already exists in ${dir}. Copy under another id.`); const copy = SessionManager.forkFrom(options.sourceFile, cwd, dir, { id: options.sessionId }); const sessionPath = copy.getSessionFile(); if (sessionPath === undefined) throw new Error("The copy was created without a session file."); return { session_id: options.sessionId, session_path: sessionPath }; }); } //# sourceMappingURL=copy.js.map