UNPKG

agentcrumbs

Version:
76 lines 2.79 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.session = session; const node_crypto_1 = require("node:crypto"); const node_fs_1 = __importDefault(require("node:fs")); const SESSION_FILE = "/tmp/agentcrumbs.session"; async function session(args) { const subcommand = args[0]; switch (subcommand) { case "start": return startSession(args.slice(1)); case "stop": return stopSession(args.slice(1)); default: process.stderr.write("Usage: agentcrumbs session <start|stop> [options]\n\n" + " start <name> Start a new session\n" + " stop [id] Stop the active session\n"); process.exit(1); } } function startSession(args) { const name = args[0] ?? "unnamed"; // Check for existing session try { const existing = node_fs_1.default.readFileSync(SESSION_FILE, "utf-8").trim(); if (existing) { process.stderr.write(`Session already active: ${existing}\n` + `Stop it first: agentcrumbs session stop\n`); process.exit(1); } } catch { // No existing session } const id = (0, node_crypto_1.randomUUID)().slice(0, 8); const sessionData = JSON.stringify({ id, name, startedAt: new Date().toISOString() }); node_fs_1.default.writeFileSync(SESSION_FILE, sessionData); process.stdout.write(`Session started: ${id} (${name})\n`); process.stdout.write(`All services will tag crumbs with this session.\n`); } function stopSession(args) { try { const content = node_fs_1.default.readFileSync(SESSION_FILE, "utf-8").trim(); if (!content) { process.stderr.write("No active session.\n"); process.exit(1); } let sessionInfo; try { sessionInfo = JSON.parse(content); } catch { sessionInfo = { id: content, name: "unknown", startedAt: "" }; } node_fs_1.default.unlinkSync(SESSION_FILE); const duration = sessionInfo.startedAt ? formatDuration(Date.now() - new Date(sessionInfo.startedAt).getTime()) : "unknown"; process.stdout.write(`Session stopped: ${sessionInfo.id} (${sessionInfo.name}) - ${duration}\n`); } catch { process.stderr.write("No active session.\n"); process.exit(1); } } function formatDuration(ms) { if (ms < 1000) return `${ms}ms`; if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`; return `${(ms / 60000).toFixed(1)}m`; } //# sourceMappingURL=session.js.map