UNPKG

@code-yeongyu/senpi

Version:

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

58 lines • 2.61 kB
import { claimOwnerIsLive, readSessionPathClaims } from "./host-reservations.js"; const BYTES_PER_MB = 1024 * 1024; export function parseSessionRows(reply) { if (!isRecord(reply) || !Array.isArray(reply.sessions)) return []; return reply.sessions.flatMap((entry) => { if (!isRecord(entry)) return []; return [ { id: typeof entry.sessionId === "string" ? entry.sessionId : "", kind: typeof entry.kind === "string" ? entry.kind : "interactive", session_path: typeof entry.sessionPath === "string" ? entry.sessionPath : null, cwd: typeof entry.cwd === "string" ? entry.cwd : null, name: typeof entry.name === "string" ? entry.name : null, attachments: typeof entry.attachments === "number" ? entry.attachments : 0, context: stringRecord(entry.context), memory: parseRowMemory(entry.memory), }, ]; }); } /** The wire block `{ main_heap_bytes, kernel_heap_bytes, kernel_count }` as megabytes; zeros when absent. */ function parseRowMemory(value) { if (!isRecord(value)) return { main_heap_mb: 0, kernel_heap_mb: 0, kernel_count: 0 }; const toMb = (bytes) => typeof bytes === "number" && Number.isFinite(bytes) ? Math.round(bytes / BYTES_PER_MB) : 0; const count = value.kernel_count; return { main_heap_mb: toMb(value.main_heap_bytes), kernel_heap_mb: toMb(value.kernel_heap_bytes), kernel_count: typeof count === "number" && Number.isFinite(count) ? count : 0, }; } export async function readClaimRows(paths, generations) { const rows = []; for (const claim of await readSessionPathClaims(paths.reservationsDir)) { const owner = claim.owner; rows.push({ session_path: owner.sessionPath, owner_pid: owner.pid, instance_id: owner.instanceId, generation: generations.find((row) => row.instanceId === owner.instanceId)?.generation ?? null, attached: owner.attached ?? null, live: await claimOwnerIsLive(owner), }); } return rows.sort((left, right) => left.session_path.localeCompare(right.session_path)); } function stringRecord(value) { if (!isRecord(value)) return null; return Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === "string")); } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } //# sourceMappingURL=host-status-rows.js.map