Mark the last-used model in the launcher menu, grow the loading quips

AI Assisted
This commit is contained in:
jparkerweb committed 2026-11-03 03:21:20 -08:00
1 parent 4704922286
commit 1eef797dc0
3 files changed
+846 -72

No files matched your search

+351 -5
View File
@@ -23,9 +23,11 @@ const NOTICE = "will use the model you last used in it";
/**
* A temp home and a bin dir holding stand-ins for `clis`; each writes its args to <bin>/<cli>.json.
* `installed: "X.Y.Z"` runs an installed copy instead, as install.js lays it out: the launcher,
* models.json and update-check.mjs in <home>/.plan2code/bin/ beside a version.json saying X.Y.Z.
* models.json, update-check.mjs, agents.json and agent-lookup.mjs in <home>/.plan2code/bin/
* beside a version.json saying X.Y.Z (`lookup: false` leaves agent-lookup.mjs out, `agents`
* replaces agents.json's contents).
*/
function sandbox(clis, { installed, project = true } = {}) {
function sandbox(clis, { installed, project = true, lookup = true, agents } = {}) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-launcher-"));
// `dir` is the launcher's cwd below; mark it as a project so the project-confirmation gate
// does not fire in every other test here. The gate's own tests pass `project: false`.
@@ -43,6 +45,10 @@ function sandbox(clis, { installed, project = true } = {}) {
fs.copyFileSync(LAUNCHER, launcher);
fs.copyFileSync(path.join(path.dirname(LAUNCHER), "models.json"), path.join(binCopy, "models.json"));
fs.copyFileSync(path.join(ROOT, "src", "web-console", "update-check.mjs"), path.join(binCopy, "update-check.mjs"));
const agentsFile = agents || path.join(path.dirname(LAUNCHER), "agents.json");
if (typeof agentsFile === "string" && fs.existsSync(agentsFile)) fs.copyFileSync(agentsFile, path.join(binCopy, "agents.json"));
else if (agentsFile) fs.writeFileSync(path.join(binCopy, "agents.json"), JSON.stringify(agentsFile));
if (lookup) fs.copyFileSync(path.join(ROOT, "src", "web-console", "agent-lookup.mjs"), path.join(binCopy, "agent-lookup.mjs"));
fs.writeFileSync(path.join(binCopy, "version.json"), JSON.stringify({ name: "Plan2Code", version: installed }));
}
const recorder = path.join(bin, "record.js");
@@ -239,6 +245,22 @@ test("launcher: a forwarded --model skips the model notice", (t) => {
}
});
test("launcher: a launched model is remembered per agent in launcher.json", (t) => {
const box = sandbox(["claude", "codex"]);
t.after(box.cleanup);
fs.mkdirSync(path.dirname(box.stateFile), { recursive: true });
fs.writeFileSync(box.stateFile, JSON.stringify({ lastFolder: box.home }));
assert.equal(box.run(["--cli", "claude", "--model", "opus"]).status, 0);
assert.equal(box.run(["--cli", "codex", "-m", "x"]).status, 0);
assert.equal(box.run(["--cli", "codex", "-my"]).status, 0);
assert.deepEqual(JSON.parse(fs.readFileSync(box.stateFile, "utf8")), {
lastFolder: box.home,
lastCli: "codex",
lastModels: { claude: "opus", codex: "y" },
});
});
test("launcher: --cli picks the CLI and is never forwarded; the pick is merged into launcher.json", (t) => {
const box = sandbox(["claude", "devin"]);
t.after(box.cleanup);
@@ -287,7 +309,7 @@ test("launcher: an unknown --cli exits 2, a missing CLI 127, and nothing is star
assert.equal(missing.status, 127);
assert.match(missing.stderr, /the `devin` CLI was not found/);
assert.match(missing.stderr, /\n\nTo use Devin:\n/);
assert.ok(missing.stderr.includes("1. Install it: https://devin.ai"));
assert.ok(missing.stderr.includes("1. Install it: https://devin.ai (https://docs.devin.ai/work-with-devin/devin-cli)"));
assert.ok(missing.stderr.includes("start Devin and type /plan2code."));
// Only the steps for the requested CLI: no "install one of" list that contradicts it.
assert.doesNotMatch(missing.stderr, /Install one:/);
@@ -339,7 +361,8 @@ test("launcher: with no CLI installed it exits 127 and explains how to get one a
]) {
assert.ok(result.stderr.includes(part), part);
}
assert.ok(result.stderr.includes("Add it in User Preferences (web console) under Agents, then run `plan2code` again."));
assert.ok(result.stderr.includes("Add it by editing `~/.plan2code/agents.json`, then run `plan2code` again."));
assert.doesNotMatch(result.stderr, /User Preferences/);
assert.ok(result.stderr.includes("type /plan2code ($plan2code in Codex)"));
assert.match(result.stderr, /cd into the project/);
});
@@ -564,6 +587,8 @@ test("launcher app: modelFrom reads --model, --model= and Codex's -m", () => {
assert.equal(modelFrom(["--model=x"], "claude"), "x");
assert.equal(modelFrom(["-m", "x"], "codex"), "x");
assert.equal(modelFrom(["-m=x"], "codex"), "x");
assert.equal(modelFrom(["-mx"], "codex"), "x");
assert.equal(modelFrom(["-m"], "codex"), null);
assert.equal(modelFrom(["-m", "x"], "claude"), null);
assert.equal(modelFrom(["--verbose"], "claude"), null);
});
@@ -810,7 +835,7 @@ test("launcher agents: a custom agent whose command is not found is left out and
const result = none.run([]);
assert.equal(result.status, 127);
assert.match(result.stderr, /Custom agent Ghost: ghost-cmd not found/);
assert.ok(result.stderr.includes("User Preferences"));
assert.ok(result.stderr.includes("agents.json"));
});
test("launcher agents: a malformed agents.json warns on a terminal, never crashes, and gives no agents", (t) => {
@@ -848,6 +873,79 @@ test("launcher agents: the app hand-over passes a custom id and its model unchan
assert.equal(JSON.parse(fs.readFileSync(box.stateFile, "utf8")).lastCli, "goose");
});
/* ------------------------- the shipped agent list (installed layout) ------------------------- */
const OS_KEY = IS_WINDOWS ? "win" : process.platform === "darwin" ? "mac" : "linux";
const SHIPPED_AGENTS = JSON.parse(fs.readFileSync(path.join(path.dirname(LAUNCHER), "agents.json"), "utf8"));
const builtin = (id) => SHIPPED_AGENTS.agents.find((a) => a.id === id);
const leadRun = (id) => builtin(id).install[OS_KEY][0].run;
test("launcher list: the menu and launch come from the sandboxed agents.json + agent-lookup.mjs", (t) => {
const box = sandbox(["claude", "devin", "codex"], { installed: "0.0.0" });
t.after(box.cleanup);
const result = box.run(["--cli", "devin", "--verbose"]);
assert.equal(result.status, 0, result.stderr);
assert.deepEqual(box.argsOf("devin"), ["--permission-mode", "bypass", "--verbose", "--", "/plan2code"]);
// The menu's order and shape come from the list: --cli errors list the same ids.
const bad = box.run(["--cli", "bogus"]);
assert.equal(bad.status, 2);
assert.match(bad.stderr, /--cli takes claude, devin or codex, not "bogus"/);
});
test("launcher list: a forwarded --model fills {model} in place; Codex's -m forwards untouched", (t) => {
const box = sandbox(["claude", "codex"], { installed: "0.0.0" });
t.after(box.cleanup);
assert.equal(box.run(["--cli", "claude", "--model", "opus"]).status, 0);
assert.deepEqual(box.argsOf("claude"), ["/plan2code", "--permission-mode", "bypassPermissions", "--model", "opus"]);
assert.equal(box.run(["--cli", "codex", "-m", "x"]).status, 0);
assert.deepEqual(box.argsOf("codex"), ["--dangerously-bypass-approvals-and-sandbox", "-m", "x", "$plan2code"]);
});
test("launcher list: the last-used notice names the gone agent and the pick, on a terminal only", () => {
const { lastUsedNotice } = require(LAUNCHER);
const all = [
{ id: "claude", label: "Claude Code" },
{ id: "devin", label: "Devin" },
];
const installed = [all[0]];
assert.equal(lastUsedNotice(installed, all, "devin"), "Devin (last used) was not found, using Claude Code");
assert.equal(lastUsedNotice(installed, all, "claude"), null, "still runnable");
assert.equal(lastUsedNotice(installed, all, "ghost"), "ghost (last used) was not found, using Claude Code");
assert.equal(lastUsedNotice([], all, "devin"), null, "nothing to use");
assert.equal(lastUsedNotice(installed, all, undefined), null);
});
test("launcher list: a missing agent-lookup.mjs falls back to the built-in list", (t) => {
const box = sandbox(["claude"], { installed: "0.0.0", lookup: false });
t.after(box.cleanup);
const result = box.run([]);
assert.equal(result.status, 0, result.stderr);
assert.deepEqual(box.argsOf("claude"), ["/plan2code", "--permission-mode", "bypassPermissions"]);
});
test("launcher list: with no agent and no terminal it prints this OS's install steps and exits 127", (t) => {
const box = sandbox([], { installed: "0.0.0" });
t.after(box.cleanup);
const result = box.run([]);
assert.equal(result.status, 127);
for (const id of ["claude", "devin", "codex"]) {
assert.ok(result.stderr.includes(leadRun(id)), `${id}: ${leadRun(id)}`);
assert.ok(result.stderr.includes(builtin(id).installUrl), id);
}
assert.ok(result.stderr.includes("add another agent by editing `~/.plan2code/agents.json`, then run `plan2code` again") || result.stderr.includes("Add it by editing `~/.plan2code/agents.json`, then run `plan2code` again."));
assert.doesNotMatch(result.stderr, /User Preferences/);
});
test("launcher list: --cli for a missing built-in prints its lead install line and exits 127", (t) => {
const box = sandbox(["claude"], { installed: "0.0.0" });
t.after(box.cleanup);
const result = box.run(["--cli", "devin"]);
assert.equal(result.status, 127);
assert.match(result.stderr, /the `devin` CLI was not found/);
assert.ok(result.stderr.includes(leadRun("devin")), leadRun("devin"));
assert.ok(result.stderr.includes(builtin("devin").installUrl));
});
test("launcher agents: a full-path command starts, and on Windows a .cmd in a folder with a space too", (t) => {
const box = sandbox([]);
t.after(box.cleanup);
@@ -872,3 +970,251 @@ test("launcher agents: a full-path command starts, and on Windows a .cmd in a fo
assert.deepEqual(box.argsOf("spaced"), ["/plan2code", "--note", "a b"]);
assert.equal(box.argsOf("decoy"), null);
});
/* ------------------------- first-run setup (the browser setup page) ------------------------- */
// When no agent can run and a person can see a page, the launcher opens the console's
// agent-less `setup` workflow (`open --workflow setup`, plus `--for-cli` when one agent
// was asked for), then waits on the session until a send carries `__start`. These drive
// runSetupFlow() directly against a stand-in console.mjs that records each call and
// answers `wait` from a step list in P2C_FAKE_STEPS.
// The app-window variables scrubbedEnv() must strip from the console child (NFR-2).
const APP_VARS = [
"PLAN2CODE_IN_APP",
"PLAN2CODE_APP_TOKEN",
"PLAN2CODE_APP_ANNOUNCE",
"PLAN2CODE_EMBED_ORIGIN",
"PLAN2CODE_CONSOLE_SID",
"PLAN2CODE_APP_VERSION",
];
// The stand-in console: every call is recorded (argv plus which app vars leaked through),
// `open` prints a setup session's JSON, `wait` plays back the numbered step. A step the
// test forgot to write ends as a Cancel rather than hanging the four-hour cap.
const FAKE_CONSOLE = [
'import fs from "node:fs";',
'import path from "node:path";',
'const dir = path.dirname(process.argv[1]);',
`const APP = ${JSON.stringify(APP_VARS)};`,
'fs.appendFileSync(path.join(dir, "calls.jsonl"), JSON.stringify({ argv: process.argv.slice(2), appVars: APP.filter((k) => k in process.env) }) + "\\n");',
'if (process.argv[2] === "open") {',
' console.log(JSON.stringify({ sid: "setup-sid", url: "http://127.0.0.1:9/s/signtok/", setup: { url: "http://127.0.0.1:9/s/signtok/", token: "handover-tok" } }));',
'} else if (process.argv[2] === "wait") {',
' const file = path.join(dir, "waits");',
' const n = fs.existsSync(file) ? Number(fs.readFileSync(file, "utf8")) : 0;',
' fs.writeFileSync(file, String(n + 1));',
' const steps = JSON.parse(process.env.P2C_FAKE_STEPS || "[]");',
' const step = steps[n] || { exit: 30 };',
' if (step.print) console.log(JSON.stringify(step.print));',
' process.exit(step.exit === undefined ? 0 : step.exit);',
'}',
].join("\n");
// A folder holding the stand-in console.mjs (the wait counter lives beside it); calls()
// replays what it was asked, oldest first.
function setupConsoleDir() {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-setup-console-"));
fs.writeFileSync(path.join(dir, "console.mjs"), FAKE_CONSOLE);
return {
dir,
calls: () =>
fs.existsSync(path.join(dir, "calls.jsonl"))
? fs.readFileSync(path.join(dir, "calls.jsonl"), "utf8").trim().split("\n").map(JSON.parse)
: [],
cleanup: () => fs.rmSync(dir, { recursive: true, force: true }),
};
}
// Point the console search at the stand-in and hand it the wait steps; restores
// PLAN2CODE_CONSOLE_DIR, P2C_FAKE_STEPS and any app vars `extra` set afterwards.
function setupEnv(t, dir, steps, extra = {}) {
const keys = ["PLAN2CODE_CONSOLE_DIR", "P2C_FAKE_STEPS", ...APP_VARS];
const saved = Object.fromEntries(keys.map((k) => [k, process.env[k]]));
process.env.PLAN2CODE_CONSOLE_DIR = dir;
process.env.P2C_FAKE_STEPS = JSON.stringify(steps);
for (const [k, v] of Object.entries(extra)) process.env[k] = v;
t.after(() => {
for (const k of keys) {
if (saved[k] === undefined) delete process.env[k];
else process.env[k] = saved[k];
}
});
}
// A builtin-shaped list for runSetupFlow's re-lookup, and the send that starts claude.
const SETUP_CLIS = [{ id: "claude", label: "Claude Code", entry: { command: "claude" } }];
const START_CLAUDE = { print: { session: [{ i: "__start", agent: "claude" }] }, exit: 0 };
const foundAt = (file) => () => ({ state: "found", path: file });
test("launcher setup: a Start re-finds the agent and hands back the announce address and token", (t) => {
const box = setupConsoleDir();
t.after(box.cleanup);
setupEnv(t, box.dir, [START_CLAUDE]);
const { runSetupFlow } = require(LAUNCHER);
const agentPath = path.join(box.dir, "claude.exe");
const out = runSetupFlow("claude", SETUP_CLIS, foundAt(agentPath));
assert.equal(out.cli.id, "claude");
assert.equal(out.cli.path, agentPath, "the named agent is looked up again after the Start");
assert.deepEqual(out.setup, { announce: "http://127.0.0.1:9/s/signtok/", token: "handover-tok" });
assert.deepEqual(
box.calls().map((c) => c.argv),
[
["open", "--workflow", "setup", "--for-cli", "claude"],
["wait", "--session", "setup-sid", "--seconds", "240"],
]
);
});
test("launcher setup: the console is spawned without the six app-window variables", (t) => {
const box = setupConsoleDir();
t.after(box.cleanup);
// Every one set on this process: none may reach the console child.
setupEnv(t, box.dir, [START_CLAUDE], Object.fromEntries(APP_VARS.map((k) => [k, "live"])));
const { runSetupFlow } = require(LAUNCHER);
runSetupFlow(undefined, SETUP_CLIS, foundAt(path.join(box.dir, "claude.exe")));
const calls = box.calls();
assert.deepEqual(calls[0].argv, ["open", "--workflow", "setup"], "no --for-cli without a named agent");
for (const call of calls) assert.deepEqual(call.appVars, [], `app vars leaked into the console's ${call.argv[0]}`);
});
test("launcher setup: a send that is not a Start and a still-waiting slice keep the wait going", (t) => {
const box = setupConsoleDir();
t.after(box.cleanup);
setupEnv(t, box.dir, [
{ print: { session: [{ i: "q1", type: "answer", kind: "choice", k: "A" }] }, exit: 0 },
{ exit: 10 },
START_CLAUDE,
]);
const { runSetupFlow } = require(LAUNCHER);
const out = runSetupFlow(undefined, SETUP_CLIS, foundAt(path.join(box.dir, "claude.exe")));
assert.equal(out.cli.id, "claude");
assert.equal(box.calls().filter((c) => c.argv[0] === "wait").length, 3, "exit 10 re-runs the wait, exit 0 without a Start keeps it");
});
test("launcher setup: cancel, a gone server and a Start for an agent that still cannot run all end as 127", (t) => {
const { runSetupFlow } = require(LAUNCHER);
const cases = [
["cancel", [{ exit: 30 }]],
["server gone", [{ exit: 20 }]],
["Start for an agent the re-lookup misses", [{ print: { session: [{ i: "__start", agent: "ghost-cli" }] }, exit: 0 }]],
];
for (const [name, steps] of cases) {
const box = setupConsoleDir();
t.after(box.cleanup);
setupEnv(t, box.dir, steps);
const out = runSetupFlow(undefined, SETUP_CLIS, foundAt(path.join(box.dir, "claude.exe")));
assert.deepEqual(out, { code: 127 }, name);
}
});
test("launcher setup: an older console's open line still yields the announce address and token from the url", (t) => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-setup-console-"));
t.after(() => fs.rmSync(dir, { recursive: true, force: true }));
// No setup block in the open output: sid and the /s/<token>/ url alone must do.
fs.writeFileSync(
path.join(dir, "console.mjs"),
[
'if (process.argv[2] === "open") {',
' console.log(JSON.stringify({ sid: "setup-sid", url: "http://127.0.0.1:9/s/signtok/" }));',
'} else if (process.argv[2] === "wait") {',
' console.log(JSON.stringify({ session: [{ i: "__start", agent: "claude" }] }));',
'}',
].join("\n")
);
setupEnv(t, dir, []);
const out = require(LAUNCHER).runSetupFlow(undefined, SETUP_CLIS, foundAt("x"));
assert.equal(out.cli.id, "claude");
assert.deepEqual(out.setup, { announce: "http://127.0.0.1:9", token: "signtok" });
});
test("launcher setup: with no console.mjs anywhere, runSetupFlow returns null rather than guessing", (t) => {
const launcherPath = require.resolve(LAUNCHER);
const home = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-no-console-"));
t.after(() => fs.rmSync(home, { recursive: true, force: true }));
// Every place findConsoleMjs() looks is derived from the home or
// PLAN2CODE_CONSOLE_DIR, so a fresh module under an empty home finds nothing.
const keys = ["HOME", "USERPROFILE", "PLAN2CODE_CONSOLE_HOME", "PLAN2CODE_CONSOLE_DIR"];
const saved = Object.fromEntries(keys.map((k) => [k, process.env[k]]));
delete require.cache[launcherPath];
try {
process.env.HOME = home;
process.env.USERPROFILE = home;
delete process.env.PLAN2CODE_CONSOLE_HOME;
delete process.env.PLAN2CODE_CONSOLE_DIR;
assert.equal(require(LAUNCHER).runSetupFlow("claude", SETUP_CLIS, foundAt("x")), null);
} finally {
for (const k of keys) {
if (saved[k] === undefined) delete process.env[k];
else process.env[k] = saved[k];
}
// Re-require with the real home so later tests see the host's own module.
delete require.cache[launcherPath];
require(LAUNCHER);
}
});
test("launcher setup: a Start can name a custom agent; the launcher re-reads ~/.plan2code/agents.json", (t) => {
const launcherPath = require.resolve(LAUNCHER);
const home = fs.mkdtempSync(path.join(os.tmpdir(), "p2c-setup-home-"));
const box = setupConsoleDir();
t.after(() => {
fs.rmSync(home, { recursive: true, force: true });
box.cleanup();
});
// USER_AGENTS_FILE is fixed at require time from the home, so the module needs
// a fresh instance whose home is the scratch one.
const keys = ["HOME", "USERPROFILE", "PLAN2CODE_CONSOLE_HOME"];
const saved = Object.fromEntries(keys.map((k) => [k, process.env[k]]));
delete require.cache[launcherPath];
try {
process.env.HOME = home;
process.env.USERPROFILE = home;
process.env.PLAN2CODE_CONSOLE_HOME = path.join(home, ".plan2code", "console");
const { runSetupFlow } = require(LAUNCHER);
const agent = path.join(box.dir, "myagent.cmd");
fs.writeFileSync(agent, "@rem stand-in\r\n");
fs.mkdirSync(path.join(home, ".plan2code"), { recursive: true });
fs.writeFileSync(
path.join(home, ".plan2code", "agents.json"),
JSON.stringify({ schema: 1, agents: [{ id: "myagent", name: "My Agent", command: agent, args: ["{prompt}"] }] })
);
setupEnv(t, box.dir, [{ print: { session: [{ i: "__start", agent: "myagent" }] }, exit: 0 }]);
const out = runSetupFlow(undefined, [], null);
assert.equal(out.cli.id, "myagent");
assert.equal(out.cli.custom, true);
assert.equal(out.cli.path, agent);
assert.equal(out.cli.fullPath, true);
} finally {
for (const k of keys) {
if (saved[k] === undefined) delete process.env[k];
else process.env[k] = saved[k];
}
delete require.cache[launcherPath];
require(LAUNCHER);
}
});
test("launcher setup: with the app installed and no agent runnable, the app opens on the folder alone", (t) => {
const box = sandbox([], { installed: "0.0.0" });
t.after(box.cleanup);
// npm's real global folder could hold a real agent; an empty prefix keeps "none runnable" certain.
const display = process.platform === "linux" ? { DISPLAY: ":0" } : {};
const result = box.run([], { PLAN2CODE_APP: box.appFile, npm_config_prefix: path.join(box.dir, "no-npm"), ...display });
assert.equal(result.status, 0, result.stderr);
const appArgs = box.appArgs();
assert.ok(appArgs, "the stand-in app was never started");
assert.equal(appArgs[0], "--project");
assert.equal(fs.realpathSync(appArgs[1]), fs.realpathSync(box.dir));
assert.deepEqual(appArgs.slice(2), [], "no --agent and no --model when none can run");
assert.match(result.stdout, /Opened the Plan2Code app/);
});
test("launcher setup: no agent and no display keeps the old 127 and install steps", { skip: process.platform !== "linux" }, (t) => {
const box = sandbox([], { installed: "0.0.0" });
t.after(box.cleanup);
const result = box.run([], { PLAN2CODE_APP: box.appFile, DISPLAY: "", WAYLAND_DISPLAY: "", npm_config_prefix: path.join(box.dir, "no-npm") });
assert.equal(result.status, 127);
assert.match(result.stderr, /none was found/);
assert.equal(box.appExists(), false);
});