2026-09-28 19:37:41 -07:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
// Plan2Code Web Console - the CLI the agent drives.
|
|
|
|
|
//
|
|
|
|
|
// open create or resume a session, publish a payload, start the server, print the URL
|
|
|
|
|
// post publish a new payload (an update) to a running session
|
|
|
|
|
// wait block for a bounded slice, then report. Exit code says what happened.
|
|
|
|
|
// chat collect Quick question messages right now, never waiting
|
|
|
|
|
// keep copy a cited note attachment into specs/<idea>/attachments/
|
|
|
|
|
// status list sessions, or show one
|
|
|
|
|
// stop shut a session's server down
|
|
|
|
|
//
|
2026-10-18 15:39:24 -07:00
|
|
|
// This file is the command table and what every command shares (the parsed
|
|
|
|
|
// flags, print, die and the session lookups). The commands live beside it:
|
|
|
|
|
// cmd-open.mjs, cmd-post-wait.mjs (post and wait, which post --wait runs),
|
|
|
|
|
// cmd-chat.mjs, cmd-help.mjs, and cmd-small.mjs for the rest.
|
|
|
|
|
//
|
2026-09-28 19:37:41 -07:00
|
|
|
// The design point: the SERVER is long-lived and detached; the WAITER is a short
|
|
|
|
|
// foreground command the agent re-invokes. A twenty-minute human session then
|
|
|
|
|
// never has to live inside a single tool call, and nothing the harness does to
|
|
|
|
|
// background tasks can destroy the human's work.
|
|
|
|
|
//
|
|
|
|
|
// Exit codes from `wait`:
|
|
|
|
|
// 0 done. stdout is the result JSON.
|
|
|
|
|
// 10 still waiting. stdout is a one-line progress summary. Call wait again.
|
|
|
|
|
// 20 the server is gone. Run `open --resume <sid>` and re-share the URL.
|
|
|
|
|
// 30 the person pressed Cancel.
|
|
|
|
|
|
|
|
|
|
import fs from "node:fs";
|
|
|
|
|
import path from "node:path";
|
|
|
|
|
|
2026-10-18 15:39:24 -07:00
|
|
|
import { parseArgs, readJson } from "./files.mjs";
|
|
|
|
|
import { projectRoot, sessionDir } from "./sessions.mjs";
|
|
|
|
|
import { cmdOpen, useCli as useOpen } from "./cmd-open.mjs";
|
|
|
|
|
import { cmdPost, cmdWait, useCli as usePostWait } from "./cmd-post-wait.mjs";
|
|
|
|
|
import { cmdChat, useCli as useChat } from "./cmd-chat.mjs";
|
|
|
|
|
import { cmdHelp, useCli as useHelp } from "./cmd-help.mjs";
|
2026-09-28 19:37:41 -07:00
|
|
|
import {
|
2026-10-18 15:39:24 -07:00
|
|
|
cmdDeleteHandoff,
|
|
|
|
|
cmdDeleteSpec,
|
|
|
|
|
cmdForget,
|
|
|
|
|
cmdHandoff,
|
|
|
|
|
cmdKeep,
|
|
|
|
|
cmdStatus,
|
|
|
|
|
cmdStop,
|
|
|
|
|
useCli as useSmall,
|
|
|
|
|
} from "./cmd-small.mjs";
|
2026-09-28 19:37:41 -07:00
|
|
|
|
2026-10-18 15:39:24 -07:00
|
|
|
function print(obj) {
|
|
|
|
|
process.stdout.write(JSON.stringify(obj) + "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function die(msg, code = 1) {
|
|
|
|
|
process.stderr.write("console: " + msg + "\n");
|
|
|
|
|
process.exit(code);
|
|
|
|
|
}
|
2026-10-02 20:51:34 -07:00
|
|
|
|
2026-09-28 19:37:41 -07:00
|
|
|
const argv = process.argv.slice(2);
|
|
|
|
|
const cmd = argv[0];
|
|
|
|
|
const args = parseArgs(argv.slice(1));
|
|
|
|
|
|
|
|
|
|
/* ---------------------------------------------------------------- utils */
|
|
|
|
|
|
|
|
|
|
function mustSession() {
|
|
|
|
|
const raw = args.session || args.sid;
|
|
|
|
|
if (!raw) die("--session <dir or id> is required", 2);
|
|
|
|
|
const dir = fs.existsSync(raw) ? path.resolve(raw) : sessionDir(path.basename(raw));
|
|
|
|
|
if (!fs.existsSync(dir)) die(`no session at ${dir}`, 1);
|
|
|
|
|
return dir;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 16:39:25 -07:00
|
|
|
// The project a session belongs to, from its own state: a command run from
|
|
|
|
|
// another folder (an agent whose shell moved) still finds the session's
|
|
|
|
|
// server, rather than reporting it gone and starting a second one.
|
|
|
|
|
function sessionProject(dir) {
|
|
|
|
|
const state = readJson(path.join(dir, "state.json"));
|
|
|
|
|
return (state && typeof state.project === "string" && state.project) || projectRoot();
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-28 19:37:41 -07:00
|
|
|
/* ------------------------------------------------------------- dispatch */
|
|
|
|
|
|
|
|
|
|
const COMMANDS = {
|
|
|
|
|
open: cmdOpen,
|
|
|
|
|
post: cmdPost,
|
|
|
|
|
wait: cmdWait,
|
|
|
|
|
chat: cmdChat,
|
|
|
|
|
keep: cmdKeep,
|
Port upstream skill scripts into v2.4.1: deterministic workflow steps run as Node scripts beside each SKILL.md (specs, pathfinder, feedback payload, commit message, review scope, agent files, handoff path) with tests, the prompt limit rises to 20,000 chars, upstream review fixes are folded in (linked agent files never overwrite AGENTS.md, review stops with no base, paste-safe commit commands, Pathfinder number high-water mark and dry runs), and web console workspace folders are remembered per project and spec, with Finalize forgetting an archived spec's list
2026-10-04 12:17:05 -07:00
|
|
|
forget: cmdForget,
|
2026-10-16 19:28:11 -07:00
|
|
|
"delete-spec": cmdDeleteSpec,
|
2026-10-17 12:35:29 -07:00
|
|
|
handoff: cmdHandoff,
|
|
|
|
|
"delete-handoff": cmdDeleteHandoff,
|
2026-09-28 19:37:41 -07:00
|
|
|
status: cmdStatus,
|
|
|
|
|
stop: cmdStop,
|
|
|
|
|
help: cmdHelp,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Say which Node is too old, rather than dying later inside a health check with
|
|
|
|
|
// "fetch is not defined". Whatever agent is driving this has to be able to read
|
|
|
|
|
// the failure, tell the person in one line, and carry on in the terminal.
|
|
|
|
|
const nodeMajor = Number.parseInt(process.versions.node, 10);
|
|
|
|
|
if (Number.isFinite(nodeMajor) && nodeMajor < 18) {
|
|
|
|
|
process.stderr.write(
|
|
|
|
|
`console: needs Node 18 or newer, found ${process.versions.node}. ` +
|
|
|
|
|
`Say so in one line and carry on in the terminal.\n`
|
|
|
|
|
);
|
|
|
|
|
process.exit(2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!cmd || !Object.hasOwn(COMMANDS, cmd)) {
|
|
|
|
|
process.stderr.write(
|
2026-10-17 12:35:29 -07:00
|
|
|
"console: usage: console.mjs <open|post|wait|chat|keep|forget|delete-spec|handoff|delete-handoff|status|stop|help> [--session <id>] [--file <patch.json>] [--seconds N]\n"
|
2026-09-28 19:37:41 -07:00
|
|
|
);
|
|
|
|
|
process.exit(2);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-02 20:51:34 -07:00
|
|
|
// A flag given with no value parses as `true`, and a boolean handed on as a
|
|
|
|
|
// path or a name crashes somewhere deep (path.basename(true) throws). Every
|
|
|
|
|
// flag that takes a value is checked once, here, and refused as bad usage.
|
2026-10-17 12:35:29 -07:00
|
|
|
const VALUE_FLAGS = ["session", "sid", "resume", "file", "spec", "title", "workflow", "skill", "handoff", "upload", "name", "from", "seconds", "limit", "idle-ms"];
|
2026-10-02 20:51:34 -07:00
|
|
|
for (const flag of VALUE_FLAGS) {
|
|
|
|
|
if (flag in args && typeof args[flag] !== "string") die(`--${flag} needs a value`, 2);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-18 15:39:24 -07:00
|
|
|
// Every command file gets the same pieces before any of them runs.
|
|
|
|
|
const cli = { args, print, die, mustSession, sessionProject };
|
|
|
|
|
for (const use of [useOpen, usePostWait, useChat, useHelp, useSmall]) use(cli);
|
|
|
|
|
|
2026-09-28 19:37:41 -07:00
|
|
|
await COMMANDS[cmd]();
|