// The SSH transport's one-request command on the destination. The caller // sends JSON on stdin and keeps the response inside its own ccdeck server. // The destination's deck token stays in this process. import { isAbsolute } from "node:path"; import { askDeck, findDeck } from "./deck-client.mjs"; const MAX_INPUT = 8 * 1024; const validText = (value, max) => typeof value === "string" && value.length > 0 && value.length <= max && !/[\x00-\x1f\x7f]/u.test(value); const provider = value => value === "claude" || value === "codex"; const fail = code => ({ version: 1, ok: false, code }); function readQuestion(data) { let value; try { value = JSON.parse(data); } catch { return null; } if (value?.version !== 1) return null; if (value.action === "open" && provider(value.provider) && validText(value.sessionId, 200)) { return { action: "open", sessionId: value.sessionId, provider: value.provider }; } if (value.action === "launch" && provider(value.agent) && validText(value.cwd, 4096) && isAbsolute(value.cwd) && typeof value.key === "string" && /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(value.key)) { return { action: "launch", agent: value.agent, cwd: value.cwd, key: value.key }; } return null; } async function readInput(input) { if (input.isTTY) return null; const chunks = []; let size = 0; try { for await (const chunk of input) { const bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); size += bytes.length; if (size > MAX_INPUT) return null; chunks.push(bytes); } } catch { return null; } return readQuestion(Buffer.concat(chunks).toString("utf8")); } const ROUTE_FAILURES = new Set([ "folder-missing", "folder-denied", "folder-unreachable", "engine-unavailable", "no-terminal", "bad-cwd", "bad-agent", ]); export async function workspaceSshRequest(input, { discover = findDeck, ask = askDeck } = {}) { const question = await readInput(input); if (!question) return fail("bad_request"); const deck = await discover(); if (!deck) return fail("no_deck"); let body; if (question.action === "open") { const inventory = await ask(deck, { path: "/api/sessions" }); if (!inventory.ok || !Array.isArray(inventory.body?.sessions)) return fail("sessions_unavailable"); const session = inventory.body.sessions.find(row => row?.id === question.sessionId && row.provider === question.provider); if (!session || !validText(session.cwd, 4096) || !isAbsolute(session.cwd)) return fail("session_unavailable"); body = { sessionId: session.id, provider: session.provider, cwd: session.cwd }; } else { const { agent, cwd, key } = question; body = { agent, cwd, key }; } const reply = await ask(deck, { method: "POST", path: `/api/terminal/${question.action}`, body, timeoutMs: 15_000 }); if (reply.status === 404) return fail("terminal_unavailable"); if (reply.body?.ok === false && reply.body.target) return fail("not_attachable"); if (!reply.ok || reply.body?.ok !== true || typeof reply.body.ticket !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(reply.body.ticket)) { return fail(ROUTE_FAILURES.has(reply.body?.code) ? reply.body.code : "terminal_unavailable"); } const result = { version: 1, ok: true, port: deck.port, ticket: reply.body.ticket }; if (question.action === "open") return { ...result, sessionId: question.sessionId }; // A new Codex pane can exist before Codex writes a conversation ID. Keep // the launch key so that reconnecting uses the same pane during that gap. return { ...result, sessionId: validText(reply.body.sessionId, 200) ? reply.body.sessionId : null, launchKey: question.key, }; }