specpilot 2.14.0 → 2.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -18
- package/dist/cli.js +3 -2
- package/dist/cli.js.map +1 -1
- package/dist/commands/serve.d.ts.map +1 -1
- package/dist/commands/serve.js +6 -4
- package/dist/commands/serve.js.map +1 -1
- package/dist/core/chatFlow.js +2 -2
- package/dist/core/chatFlow.js.map +1 -1
- package/dist/utils/mcpLocal.d.ts +10 -0
- package/dist/utils/mcpLocal.d.ts.map +1 -1
- package/dist/utils/mcpLocal.js +61 -2
- package/dist/utils/mcpLocal.js.map +1 -1
- package/dist/utils/specServer.d.ts +4 -4
- package/dist/utils/specServer.d.ts.map +1 -1
- package/dist/utils/specServer.js +187 -115
- package/dist/utils/specServer.js.map +1 -1
- package/dist/utils/specSetup.d.ts +21 -23
- package/dist/utils/specSetup.d.ts.map +1 -1
- package/dist/utils/specSetup.js +63 -56
- package/dist/utils/specSetup.js.map +1 -1
- package/dist/utils/specValidator.d.ts.map +1 -1
- package/dist/utils/specValidator.js +4 -1
- package/dist/utils/specValidator.js.map +1 -1
- package/package.json +1 -1
- package/ui/app.css +40 -110
- package/ui/app.js +113 -304
- package/ui/index.html +21 -27
- package/ui/route.js +3 -101
package/dist/utils/specServer.js
CHANGED
|
@@ -33,8 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.MAX_EVENT_STREAMS = exports.
|
|
37
|
-
exports.chatCoreScript = chatCoreScript;
|
|
36
|
+
exports.MAX_EVENT_STREAMS = exports.MAX_MCP_BODY = exports.MAX_MOVE_BODY = void 0;
|
|
38
37
|
exports.isAllowedHost = isAllowedHost;
|
|
39
38
|
exports.readBranch = readBranch;
|
|
40
39
|
exports.buildSpecsPayload = buildSpecsPayload;
|
|
@@ -53,6 +52,7 @@ const taskMover_1 = require("./taskMover");
|
|
|
53
52
|
const specPaths_1 = require("./specPaths");
|
|
54
53
|
const specPoller_1 = require("./specPoller");
|
|
55
54
|
const specSetup_1 = require("./specSetup");
|
|
55
|
+
const chatFlow_1 = require("../core/chatFlow");
|
|
56
56
|
const gitClone_1 = require("./gitClone");
|
|
57
57
|
const folderPicker_1 = require("./folderPicker");
|
|
58
58
|
const projectRegistry_1 = require("./projectRegistry");
|
|
@@ -70,12 +70,6 @@ const mcpLocal_1 = require("./mcpLocal");
|
|
|
70
70
|
// functions for an AI IDE on this machine (BL-PM-007, mcpLocal.ts).
|
|
71
71
|
/** Resolved from this module's own location, never from cwd: dist/utils → <package>/ui. */
|
|
72
72
|
const UI_DIR = (0, path_1.join)(__dirname, '..', '..', 'ui');
|
|
73
|
-
/** The chat core's compiled file, which the page runs as /assets/chat-core.js (BL-PM-004b, REQ-002.I.7). */
|
|
74
|
-
const CHAT_CORE = (0, path_1.join)(__dirname, '..', 'core', 'chatFlow.js');
|
|
75
|
-
/** tsc's CommonJS output of `src/core/chatFlow.ts` as a page script: `exports` supplied, the source map line dropped. */
|
|
76
|
-
function chatCoreScript(compiled) {
|
|
77
|
-
return `(function (exports) {\n${compiled.replace(/^\/\/# sourceMappingURL=.*$/m, '')}\n})(window.SpecPilotChat = {});\n`;
|
|
78
|
-
}
|
|
79
73
|
const UI_ROUTES = {
|
|
80
74
|
'/': ['index.html', 'text/html; charset=utf-8'],
|
|
81
75
|
'/assets/app.css': ['app.css', 'text/css; charset=utf-8'],
|
|
@@ -263,12 +257,15 @@ function sendJson(res, status, body) {
|
|
|
263
257
|
}
|
|
264
258
|
/** Largest accepted request body for a move (BL-053) and the other write routes. */
|
|
265
259
|
exports.MAX_MOVE_BODY = 16 * 1024;
|
|
266
|
-
/** Largest accepted body for a setup, a new project or a preview, which carry the 23 optional fields (BL-PM-004b). */
|
|
267
|
-
exports.MAX_SETUP_BODY = 64 * 1024;
|
|
268
260
|
/** Where index.html receives the per-start CSRF token (nothing with --read-only). */
|
|
269
261
|
const TOKEN_SLOT = '<!-- specpilot-token -->';
|
|
270
262
|
/** Where index.html learns that /mcp is on, and whose token it takes: `page` or `env` (BL-PM-007). */
|
|
271
263
|
const MCP_SLOT = '<!-- specpilot-mcp -->';
|
|
264
|
+
/** Largest accepted body on /mcp: the setup answers, as the page's setup routes took them (BL-104). */
|
|
265
|
+
exports.MAX_MCP_BODY = 64 * 1024;
|
|
266
|
+
// The CLI's question list as the chat core takes it: the page's own rule, also read here since BL-104.
|
|
267
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
268
|
+
const { flowQuestions } = require('../../ui/route.js');
|
|
272
269
|
/** Most `/api/events` streams open at once; the next one gets 503 (SEC-004.9). */
|
|
273
270
|
exports.MAX_EVENT_STREAMS = 8;
|
|
274
271
|
function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
@@ -320,7 +317,13 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
320
317
|
// /mcp's token (BL-PM-007): its own variable, because a null `token` is what turns the page's writes off.
|
|
321
318
|
const mcpToken = !opts.mcp ? null : (opts.mcpToken ?? token ?? (0, crypto_1.randomBytes)(32).toString('hex'));
|
|
322
319
|
let writeLock = Promise.resolve(); // writes run strictly one after another, in every project
|
|
323
|
-
|
|
320
|
+
// The MCP client whose initialize /mcp last answered (BL-103): null until then; `name` sanitized, '' when none was sent.
|
|
321
|
+
let mcpClient = null;
|
|
322
|
+
// `named`: whether the Setup view is offered there (REQ-002.H.15's current-directory rule, BL-103).
|
|
323
|
+
const payload = (i) => {
|
|
324
|
+
const p = buildSpecsPayload(roots[i], specpilotVersion);
|
|
325
|
+
return { ...p, project: { ...p.project, named: namedFlags[i] }, projects: projectList(roots), mcp: { client: mcpClient } };
|
|
326
|
+
};
|
|
324
327
|
/** Run `fn` under the write lock and settle with its outcome; a throw never breaks the chain for later writes. */
|
|
325
328
|
const locked = (fn) => {
|
|
326
329
|
const run = writeLock.then(fn);
|
|
@@ -462,61 +465,6 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
462
465
|
});
|
|
463
466
|
});
|
|
464
467
|
};
|
|
465
|
-
// ---- guided setup (BL-055): only for a named root (command line, or opened from the page) that has no .specs/
|
|
466
|
-
const named = (i) => namedFlags[i];
|
|
467
|
-
const setupIndex = (url) => {
|
|
468
|
-
const i = projectIndex(url, roots.length);
|
|
469
|
-
return i !== null && named(i) && (0, specSetup_1.specsMissing)(roots[i]) ? i : null;
|
|
470
|
-
};
|
|
471
|
-
const handleSetupGet = (res, url) => {
|
|
472
|
-
const i = setupIndex(url);
|
|
473
|
-
if (i === null)
|
|
474
|
-
return send(res, 404, TEXT, 'Not Found\n');
|
|
475
|
-
(0, specSetup_1.setupQuestions)(roots[i]).then(q => sendJson(res, 200, q), err => sendJson(res, 422, { error: `This folder could not be read: ${err.message}` }));
|
|
476
|
-
};
|
|
477
|
-
const handleSetupPost = (req, res, url) => {
|
|
478
|
-
if (!writeAllowed(req, res, exports.MAX_SETUP_BODY))
|
|
479
|
-
return;
|
|
480
|
-
const i = projectIndex(url, roots.length);
|
|
481
|
-
if (i === null || !named(i))
|
|
482
|
-
return send(res, 404, TEXT, 'Not Found\n');
|
|
483
|
-
readJson(req, res, body => {
|
|
484
|
-
const problem = (0, specSetup_1.answersShapeError)(body);
|
|
485
|
-
if (problem)
|
|
486
|
-
return sendJson(res, 422, { error: problem });
|
|
487
|
-
writeLock = writeLock
|
|
488
|
-
.then(() => (0, specSetup_1.setupProject)(roots[i], body))
|
|
489
|
-
.then(out => {
|
|
490
|
-
if (out.status === 200)
|
|
491
|
-
return sendJson(res, 200, { specs: payload(i), kept: out.kept, notice: out.notice });
|
|
492
|
-
if (out.status === 409 && out.specsExists)
|
|
493
|
-
return sendJson(res, 409, { error: out.error, specs: payload(i) });
|
|
494
|
-
return sendJson(res, out.status, { error: out.error });
|
|
495
|
-
})
|
|
496
|
-
.catch(() => {
|
|
497
|
-
if (!res.headersSent)
|
|
498
|
-
sendJson(res, 500, { error: 'Setup could not finish.' });
|
|
499
|
-
});
|
|
500
|
-
}, exports.MAX_SETUP_BODY);
|
|
501
|
-
};
|
|
502
|
-
// ---- preview (BL-PM-004b): what a create would write, rendered and returned; no lock, nothing written
|
|
503
|
-
const handlePreview = (req, res, url) => {
|
|
504
|
-
if (!writeAllowed(req, res, exports.MAX_SETUP_BODY))
|
|
505
|
-
return;
|
|
506
|
-
const guided = url.searchParams.has('project');
|
|
507
|
-
const i = guided ? projectIndex(url, roots.length) : null;
|
|
508
|
-
if (guided && (i === null || !named(i)))
|
|
509
|
-
return send(res, 404, TEXT, 'Not Found\n');
|
|
510
|
-
if (!guided && !registry)
|
|
511
|
-
return send(res, 404, TEXT, 'Not Found\n');
|
|
512
|
-
readJson(req, res, body => {
|
|
513
|
-
const problem = guided ? (0, specSetup_1.answersShapeError)(body) : (0, specSetup_1.newProjectShapeError)(body, true);
|
|
514
|
-
if (problem)
|
|
515
|
-
return sendJson(res, 422, { error: problem });
|
|
516
|
-
const b = body;
|
|
517
|
-
Promise.resolve(guided ? (0, specSetup_1.previewSetup)(roots[i], b) : (0, specSetup_1.previewNewProject)(b)).then(out => ('status' in out ? sendJson(res, out.status, { error: out.error }) : sendJson(res, 200, out)), () => sendJson(res, 500, { error: 'The preview could not be made.' }));
|
|
518
|
-
}, exports.MAX_SETUP_BODY);
|
|
519
|
-
};
|
|
520
468
|
// ---- project registry (BL-067): list, open a folder, forget an entry; all absent with --read-only
|
|
521
469
|
const registry = token && opts.registry ? opts.registry : null;
|
|
522
470
|
const home = (0, projectRegistry_1.homeDir)();
|
|
@@ -562,14 +510,13 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
562
510
|
* Serve `root` as the next index and record it in the registry: the success body of an open, or null
|
|
563
511
|
* after answering 422 because the folder's allowlisted files cannot be read (it is then not half-opened).
|
|
564
512
|
*/
|
|
565
|
-
const
|
|
513
|
+
const addRoot = (root) => {
|
|
566
514
|
let specs;
|
|
567
515
|
try {
|
|
568
516
|
specs = buildSpecsPayload(root, specpilotVersion);
|
|
569
517
|
}
|
|
570
518
|
catch (err) {
|
|
571
|
-
|
|
572
|
-
return null;
|
|
519
|
+
return { error: `This folder could not be read: ${err.message}` };
|
|
573
520
|
}
|
|
574
521
|
const project = projectFor(root); // built before anything is pushed, so the three lists never disagree
|
|
575
522
|
const n = roots.length;
|
|
@@ -577,7 +524,16 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
577
524
|
namedFlags.push(true);
|
|
578
525
|
projects.push(project);
|
|
579
526
|
const error = changeRegistry(entries => (0, projectRegistry_1.upsertEntry)(entries, root, new Date()));
|
|
580
|
-
return { project: n, specs: { ...specs, projects: projectList(roots) }, registry: { ...registryBody(), ...(error ? { error } : {}) } };
|
|
527
|
+
return { project: n, specs: { ...specs, project: { ...specs.project, named: true }, projects: projectList(roots), mcp: { client: mcpClient } }, registry: { ...registryBody(), ...(error ? { error } : {}) } };
|
|
528
|
+
};
|
|
529
|
+
/** `addRoot()` for a route: answers 422 itself when the folder cannot be read (it is then not half-opened). */
|
|
530
|
+
const serveRoot = (res, root) => {
|
|
531
|
+
const out = addRoot(root);
|
|
532
|
+
if ('error' in out) {
|
|
533
|
+
sendJson(res, 422, { error: out.error });
|
|
534
|
+
return null;
|
|
535
|
+
}
|
|
536
|
+
return out;
|
|
581
537
|
};
|
|
582
538
|
const handleProjectAdd = (req, res) => {
|
|
583
539
|
if (!writeAllowed(req, res))
|
|
@@ -600,34 +556,29 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
600
556
|
});
|
|
601
557
|
});
|
|
602
558
|
};
|
|
603
|
-
// ----
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
catch (err) {
|
|
609
|
-
sendJson(res, 422, { error: `The OS username could not be read: ${err.message}` });
|
|
610
|
-
}
|
|
611
|
-
};
|
|
612
|
-
const handleProjectNew = (req, res) => {
|
|
613
|
-
if (!writeAllowed(req, res, exports.MAX_SETUP_BODY))
|
|
559
|
+
// ---- the Setup view's pending path (BL-103, SEC-004.26): where a new project may be made, checked, nothing
|
|
560
|
+
// created; one slot for the run, replaced by the next registration, never written anywhere.
|
|
561
|
+
let pendingTarget = null;
|
|
562
|
+
const handlePending = (req, res) => {
|
|
563
|
+
if (!writeAllowed(req, res))
|
|
614
564
|
return;
|
|
615
565
|
readJson(req, res, body => {
|
|
616
|
-
const problem = (0, specSetup_1.
|
|
566
|
+
const problem = (0, specSetup_1.pendingShapeError)(body);
|
|
617
567
|
if (problem)
|
|
618
568
|
return sendJson(res, 422, { error: problem });
|
|
619
|
-
const { parent, name
|
|
620
|
-
underLock(res,
|
|
569
|
+
const { parent, name } = body;
|
|
570
|
+
underLock(res, () => {
|
|
621
571
|
if (roots.length + (cloningTarget ? 1 : 0) >= projectRegistry_1.MAX_PROJECTS)
|
|
622
|
-
return sendJson(res, 409, { error: TOO_MANY_PROJECTS });
|
|
623
|
-
const out =
|
|
624
|
-
if (
|
|
572
|
+
return sendJson(res, 409, { error: TOO_MANY_PROJECTS });
|
|
573
|
+
const out = (0, specSetup_1.checkTarget)(parent, name, roots, home, cloningTarget);
|
|
574
|
+
if ('status' in out)
|
|
575
|
+
pendingTarget = null; // a refusal leaves no path writable that the page no longer shows (BL-104)
|
|
576
|
+
if ('status' in out)
|
|
625
577
|
return sendJson(res, out.status, out.project === undefined ? { error: out.error } : { error: out.error, project: out.project });
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
sendJson(res, 200, { ...body200, kept: out.kept, notice: out.notice });
|
|
578
|
+
pendingTarget = out.target;
|
|
579
|
+
sendJson(res, 200, { target: out.target });
|
|
629
580
|
});
|
|
630
|
-
}
|
|
581
|
+
});
|
|
631
582
|
};
|
|
632
583
|
// ---- clone a repository (BL-PM-002): the user's git fills <parent>/<name>, then served like an opened folder.
|
|
633
584
|
// One at a time, outside the write lock: the lock covers the checks and the mkdir, and serving the folder.
|
|
@@ -774,7 +725,7 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
774
725
|
[taskMover_1.STALE_ERROR]: 'planning/tasks.md changed on disk since you last read it. The move was not made.',
|
|
775
726
|
[taskMover_1.NEW_STALE_ERROR]: 'planning/tasks.md changed on disk since you last read it, so the task was not added. Call specpilot_list_tasks for the current sha256, then try again.',
|
|
776
727
|
};
|
|
777
|
-
const mcpTools = mcpLocal_1.MCP_TOOLS.filter(t => token || !t.writes); // --read-only: no write tools
|
|
728
|
+
const mcpTools = mcpLocal_1.MCP_TOOLS.filter(t => token || (!t.writes && !t.setup)); // --read-only: no write tools, no setup tools (D3)
|
|
778
729
|
const mcpProject = (args) => {
|
|
779
730
|
const n = args.project ?? 0;
|
|
780
731
|
if (!Number.isInteger(n) || n < 0 || n >= roots.length)
|
|
@@ -802,7 +753,135 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
802
753
|
}
|
|
803
754
|
return out;
|
|
804
755
|
};
|
|
756
|
+
const BACKFILL = (p) => `${p} already has .specs/. To add missing SpecPilot sections there, run specpilot backfill.`;
|
|
757
|
+
/** D2: a served root named or opened from the page without .specs/, or exactly the pending target. Run on every call and again under the lock. */
|
|
758
|
+
const setupTarget = (path) => {
|
|
759
|
+
if (typeof path !== 'string' || !path.trim() || path.includes('\0'))
|
|
760
|
+
throw new mcpLocal_1.ToolRefusal('path is required: the folder as the SpecPilot page shows it.');
|
|
761
|
+
const expanded = path === '~' ? home : path.startsWith('~/') ? (0, path_1.join)(home, path.slice(2)) : path;
|
|
762
|
+
const abs = (0, path_1.resolve)(expanded);
|
|
763
|
+
let norm = abs;
|
|
764
|
+
try {
|
|
765
|
+
norm = (0, fs_1.existsSync)(abs) ? (0, fs_1.realpathSync)(abs) : (0, path_1.join)((0, fs_1.realpathSync)((0, path_1.dirname)(abs)), (0, path_1.basename)(abs));
|
|
766
|
+
}
|
|
767
|
+
catch {
|
|
768
|
+
/* a parent that is gone: compared as given */
|
|
769
|
+
}
|
|
770
|
+
// A served root as given or by its real path (the command line's roots are real paths already).
|
|
771
|
+
const real = (r) => {
|
|
772
|
+
try {
|
|
773
|
+
return (0, fs_1.realpathSync)(r);
|
|
774
|
+
}
|
|
775
|
+
catch {
|
|
776
|
+
return r;
|
|
777
|
+
}
|
|
778
|
+
};
|
|
779
|
+
const index = roots.findIndex(r => r === norm || real(r) === norm);
|
|
780
|
+
if (index >= 0 && namedFlags[index]) {
|
|
781
|
+
if (!(0, specSetup_1.specsMissing)(roots[index]))
|
|
782
|
+
throw new mcpLocal_1.ToolRefusal(BACKFILL(norm));
|
|
783
|
+
return { kind: 'existing', root: roots[index], index };
|
|
784
|
+
}
|
|
785
|
+
if (pendingTarget !== null && norm === pendingTarget && (0, path_1.isAbsolute)(norm)) {
|
|
786
|
+
if ((0, fs_1.existsSync)(norm) && !(0, specSetup_1.specsMissing)(norm))
|
|
787
|
+
throw new mcpLocal_1.ToolRefusal(BACKFILL(norm));
|
|
788
|
+
return { kind: 'new', target: norm };
|
|
789
|
+
}
|
|
790
|
+
throw new mcpLocal_1.ToolRefusal(`${norm} is not a folder this SpecPilot page is setting up. Open it in the page (Open a Project Folder, Clone, or Start a New Project) first.`);
|
|
791
|
+
};
|
|
792
|
+
/** `event: setup` to every open stream: state and counts only, never a path (SEC-004.9). */
|
|
793
|
+
const emitSetup = (t, data) => {
|
|
794
|
+
const line = JSON.stringify({ ...(t.kind === 'existing' ? { project: t.index } : { pending: true }), ...data });
|
|
795
|
+
allStreams().forEach(st => st.write(`event: setup\ndata: ${line}\n\n`));
|
|
796
|
+
};
|
|
797
|
+
/** The questions for a target: the CLI's, then the 23 optional fields with the chat core's wording, labels and options. */
|
|
798
|
+
const setupQuestionList = async (t) => {
|
|
799
|
+
let q;
|
|
800
|
+
try {
|
|
801
|
+
q = t.kind === 'existing' ? await (0, specSetup_1.setupQuestions)(t.root) : (0, specSetup_1.newProjectQuestions)();
|
|
802
|
+
}
|
|
803
|
+
catch (err) {
|
|
804
|
+
throw new mcpLocal_1.ToolRefusal(t.kind === 'existing' ? `This folder could not be read: ${err.message}` : `The OS username could not be read: ${err.message}`);
|
|
805
|
+
}
|
|
806
|
+
const mode = t.kind === 'existing' ? 'setup' : 'new';
|
|
807
|
+
// Answers that open every optional field's question: databases and realtime follow a REST or GraphQL API, sync
|
|
808
|
+
// follows an offline store, constraint detail follows a technical constraint. The agent answers what applies.
|
|
809
|
+
const open = { apiParadigm: 'rest', localDatabases: ['SQLite'], technicalConstraints: ['Budget cap'] };
|
|
810
|
+
const core = (0, chatFlow_1.chatQuestions)(q.questions, flowQuestions(q, {}, []), open, mode, q.files.outside);
|
|
811
|
+
const cli = q.questions.map(x => ({
|
|
812
|
+
key: x.key, label: x.label ?? x.key, question: x.message, required: !!x.required,
|
|
813
|
+
...(x.choices ? { choices: x.choices.map(c => ({ value: c.value, text: c.name })) } : {}),
|
|
814
|
+
...(x.key === 'framework' && !x.choices && q.frameworks ? { choicesByLanguage: q.frameworks } : {}),
|
|
815
|
+
...(x.when ? { when: x.when } : {}),
|
|
816
|
+
}));
|
|
817
|
+
const fields = core.filter(c => c.core && c.key in chatFlow_1.OPTIONAL_FIELD_LIMITS).map(c => ({
|
|
818
|
+
key: c.key, label: c.label, question: c.chat, kind: chatFlow_1.OPTIONAL_FIELD_LIMITS[c.key].kind, max: chatFlow_1.OPTIONAL_FIELD_LIMITS[c.key].max,
|
|
819
|
+
...(c.options?.length ? { options: c.options.filter(o => !o.todo).map(o => o.label) } : {}),
|
|
820
|
+
...(c.categories ? { categories: c.categories.map(k => ({ id: k.id, label: k.label, options: k.options })) } : {}),
|
|
821
|
+
}));
|
|
822
|
+
const detected = 'detected' in q && q.detected ? q.detected.rows.map(r => ({ label: r.label, value: r.value })) : [];
|
|
823
|
+
const name = t.kind === 'new' ? (0, path_1.basename)(t.target) : ('detected' in q && q.detected ? q.detected.name : (0, path_1.basename)(t.root));
|
|
824
|
+
return { questions: [...cli, ...fields], detected, name };
|
|
825
|
+
};
|
|
826
|
+
const setupProjectTool = async (args) => {
|
|
827
|
+
const first = setupTarget(args.path);
|
|
828
|
+
const answers = args.answers;
|
|
829
|
+
if (!answers || typeof answers !== 'object' || Array.isArray(answers))
|
|
830
|
+
throw new mcpLocal_1.ToolRefusal('answers must be an object: one value per question key from specpilot_setup_questions.');
|
|
831
|
+
if (args.asked !== undefined && (!Array.isArray(args.asked) || args.asked.some(k => typeof k !== 'string')))
|
|
832
|
+
throw new mcpLocal_1.ToolRefusal('asked must be a list of answer keys.');
|
|
833
|
+
const a = answers;
|
|
834
|
+
if (first.kind === 'new' && ('parent' in a || 'name' in a))
|
|
835
|
+
throw new mcpLocal_1.ToolRefusal('"parent" and "name" come from the path; leave them out of answers.');
|
|
836
|
+
const problem = first.kind === 'existing' ? (0, specSetup_1.answersShapeError)(a) : (0, specSetup_1.newProjectShapeError)({ parent: (0, path_1.dirname)(first.target), name: (0, path_1.basename)(first.target), ...a });
|
|
837
|
+
if (problem)
|
|
838
|
+
throw new mcpLocal_1.ToolRefusal(problem);
|
|
839
|
+
const labels = new Map((await setupQuestionList(first)).questions.map(x => [x.key, x.label]));
|
|
840
|
+
const asked = [...new Set(args.asked ?? [])].filter(k => k in a);
|
|
841
|
+
const counts = { fromCode: Object.keys(a).length - asked.length, fromUser: asked.length, topics: asked.flatMap(k => (labels.has(k) ? [labels.get(k)] : [])) };
|
|
842
|
+
const written = await locked(async () => {
|
|
843
|
+
const t = setupTarget(args.path); // again under the lock: the folder may have changed since (SEC-004.27)
|
|
844
|
+
if (t.kind !== first.kind)
|
|
845
|
+
throw new mcpLocal_1.ToolRefusal(`${t.kind === 'new' ? t.target : t.root} changed while it was being set up. Call specpilot_setup_questions again.`);
|
|
846
|
+
emitSetup(t, { row: 'questions', state: 'done', ...counts });
|
|
847
|
+
emitSetup(t, { row: 'write', state: 'running' });
|
|
848
|
+
let out;
|
|
849
|
+
if (t.kind === 'existing')
|
|
850
|
+
out = await (0, specSetup_1.setupProject)(t.root, a);
|
|
851
|
+
else if (roots.length + (cloningTarget ? 1 : 0) >= projectRegistry_1.MAX_PROJECTS)
|
|
852
|
+
out = { status: 409, error: TOO_MANY_PROJECTS };
|
|
853
|
+
else
|
|
854
|
+
out = await (0, specSetup_1.createProject)((0, path_1.dirname)(t.target), (0, path_1.basename)(t.target), a, roots, home, cloningTarget);
|
|
855
|
+
if (out.status !== 200) {
|
|
856
|
+
emitSetup(t, { row: 'write', state: 'idle' });
|
|
857
|
+
throw new mcpLocal_1.ToolRefusal(out.error);
|
|
858
|
+
}
|
|
859
|
+
emitSetup(t, { row: 'write', state: 'done', files: out.written.length });
|
|
860
|
+
if (t.kind === 'existing')
|
|
861
|
+
return { t, root: t.root, project: t.index, out };
|
|
862
|
+
pendingTarget = null;
|
|
863
|
+
const served = addRoot(t.target);
|
|
864
|
+
if ('error' in served)
|
|
865
|
+
throw new mcpLocal_1.ToolRefusal(`${t.target} was set up, but it could not be read to be served (${served.error}). Open it with Open a Project Folder. Written: ${out.written.join(', ')}`);
|
|
866
|
+
return { t, root: t.target, project: served.project, out };
|
|
867
|
+
});
|
|
868
|
+
emitSetup(written.t, { row: 'validate', state: 'running' });
|
|
869
|
+
const r = await new specValidator_1.SpecValidator().validate(written.root, { fix: false, verbose: false });
|
|
870
|
+
emitSetup(written.t, { row: 'validate', state: 'done', errors: r.errors.length, warnings: r.warnings.length, ...(written.t.kind === 'new' ? { project: written.project } : {}) });
|
|
871
|
+
const { out } = written;
|
|
872
|
+
return asJson({ path: written.root, project: written.project, written: out.written, kept: out.kept, notice: out.notice, validate: { errors: r.errors, warnings: r.warnings } });
|
|
873
|
+
};
|
|
805
874
|
const callTool = async (name, args) => {
|
|
875
|
+
if (name === 'specpilot_setup_questions') {
|
|
876
|
+
const t = setupTarget(args.path);
|
|
877
|
+
emitSetup(t, { row: 'read', state: 'running' });
|
|
878
|
+
const list = await setupQuestionList(t);
|
|
879
|
+
emitSetup(t, { row: 'read', state: 'done', detected: list.detected.length });
|
|
880
|
+
emitSetup(t, { row: 'questions', state: 'running' });
|
|
881
|
+
return asJson({ mode: t.kind, path: t.kind === 'existing' ? t.root : t.target, name: list.name, detected: list.detected, questions: list.questions });
|
|
882
|
+
}
|
|
883
|
+
if (name === 'specpilot_setup_project')
|
|
884
|
+
return setupProjectTool(args);
|
|
806
885
|
if (name === 'specpilot_list_projects') {
|
|
807
886
|
return asJson({
|
|
808
887
|
projects: roots.map((root, n) => {
|
|
@@ -853,6 +932,13 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
853
932
|
return regenerateReport(new specBackfiller_1.SpecBackfiller().backfillSlashCommands(roots[i], false));
|
|
854
933
|
}));
|
|
855
934
|
};
|
|
935
|
+
/** An answered client (BL-103): kept for /api/specs, and sent as `event: mcp` to every open stream when it changes. */
|
|
936
|
+
const seeClient = (name) => {
|
|
937
|
+
if (mcpClient?.name === name)
|
|
938
|
+
return;
|
|
939
|
+
mcpClient = { name };
|
|
940
|
+
allStreams().forEach(st => st.write(`event: mcp\ndata: ${JSON.stringify({ client: mcpClient })}\n\n`));
|
|
941
|
+
};
|
|
856
942
|
/** POST /mcp: the page's write checks in the same order, but Origin may be absent. A bad token is 403 too, never 401,
|
|
857
943
|
* which Claude Code takes for an OAuth challenge (SEC-004.20). */
|
|
858
944
|
const handleMcp = (req, res) => {
|
|
@@ -870,9 +956,12 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
870
956
|
}
|
|
871
957
|
if (!/^application\/json\s*(;|$)/i.test(req.headers['content-type'] ?? ''))
|
|
872
958
|
return sendJson(res, 415, (0, mcpLocal_1.rpcError)(null, -32000, 'Send JSON-RPC as application/json.'));
|
|
873
|
-
if (Number(req.headers['content-length'] ?? 0) > exports.
|
|
959
|
+
if (Number(req.headers['content-length'] ?? 0) > exports.MAX_MCP_BODY)
|
|
874
960
|
return sendJson(res, 413, (0, mcpLocal_1.rpcError)(null, -32000, 'The request is too large.'));
|
|
875
961
|
readJson(req, res, body => (0, mcpLocal_1.answerMcp)(body, (0, mcpLocal_1.mirroredHeaders)(req.rawHeaders), mcpTools, specpilotVersion, callTool).then(reply => {
|
|
962
|
+
const name = (0, mcpLocal_1.mcpClientName)(body);
|
|
963
|
+
if (name !== undefined && reply.status === 200 && 'result' in reply.body)
|
|
964
|
+
seeClient(name);
|
|
876
965
|
if (reply.status !== 202)
|
|
877
966
|
return sendJson(res, reply.status, reply.body);
|
|
878
967
|
res.writeHead(202, SECURITY_HEADERS);
|
|
@@ -880,7 +969,7 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
880
969
|
}, () => {
|
|
881
970
|
if (!res.headersSent)
|
|
882
971
|
sendJson(res, 500, (0, mcpLocal_1.rpcError)(null, -32603, 'The server could not finish this request.'));
|
|
883
|
-
}), exports.
|
|
972
|
+
}), exports.MAX_MCP_BODY, () => sendJson(res, 400, (0, mcpLocal_1.rpcError)(null, -32700, 'Parse error: the body is not valid JSON.')));
|
|
884
973
|
};
|
|
885
974
|
/** POST /api/quit (BL-098): 202, then the command's stop once the answer has gone out. */
|
|
886
975
|
let quitting = false;
|
|
@@ -911,12 +1000,10 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
911
1000
|
res.setHeader('Allow', registry ? 'GET, POST' : '');
|
|
912
1001
|
return send(res, 405, TEXT, 'Method Not Allowed\n');
|
|
913
1002
|
}
|
|
914
|
-
if (url.pathname === '/api/projects/
|
|
915
|
-
if (registry && req.method === 'GET')
|
|
916
|
-
return handleProjectNewGet(res);
|
|
1003
|
+
if (url.pathname === '/api/projects/pending') {
|
|
917
1004
|
if (registry && req.method === 'POST')
|
|
918
|
-
return
|
|
919
|
-
res.setHeader('Allow', registry ? '
|
|
1005
|
+
return handlePending(req, res);
|
|
1006
|
+
res.setHeader('Allow', registry ? 'POST' : '');
|
|
920
1007
|
return send(res, 405, TEXT, 'Method Not Allowed\n');
|
|
921
1008
|
}
|
|
922
1009
|
if (url.pathname === '/api/projects/clone') {
|
|
@@ -961,20 +1048,6 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
961
1048
|
res.setHeader('Allow', token ? 'POST' : '');
|
|
962
1049
|
return send(res, 405, TEXT, 'Method Not Allowed\n');
|
|
963
1050
|
}
|
|
964
|
-
if (url.pathname === '/api/preview') {
|
|
965
|
-
if (token && req.method === 'POST')
|
|
966
|
-
return handlePreview(req, res, url);
|
|
967
|
-
res.setHeader('Allow', token ? 'POST' : '');
|
|
968
|
-
return send(res, 405, TEXT, 'Method Not Allowed\n');
|
|
969
|
-
}
|
|
970
|
-
if (url.pathname === '/api/setup') {
|
|
971
|
-
if (token && req.method === 'GET')
|
|
972
|
-
return handleSetupGet(res, url);
|
|
973
|
-
if (token && req.method === 'POST')
|
|
974
|
-
return handleSetupPost(req, res, url);
|
|
975
|
-
res.setHeader('Allow', token ? 'GET, POST' : '');
|
|
976
|
-
return send(res, 405, TEXT, 'Method Not Allowed\n');
|
|
977
|
-
}
|
|
978
1051
|
if (req.method !== 'GET') {
|
|
979
1052
|
res.setHeader('Allow', 'GET');
|
|
980
1053
|
return send(res, 405, TEXT, 'Method Not Allowed\n');
|
|
@@ -984,8 +1057,6 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
984
1057
|
const mcpMeta = token && mcpToken ? `<meta name="specpilot-mcp" content="${opts.mcpToken ? 'env' : 'page'}">` : '';
|
|
985
1058
|
return send(res, 200, UI_ROUTES['/'][1], page.replace(TOKEN_SLOT, token ? `<meta name="specpilot-token" content="${token}">` : '').replace(MCP_SLOT, mcpMeta));
|
|
986
1059
|
}
|
|
987
|
-
if (url.pathname === '/assets/chat-core.js')
|
|
988
|
-
return send(res, 200, 'text/javascript; charset=utf-8', chatCoreScript((0, fs_1.readFileSync)(CHAT_CORE, 'utf-8')));
|
|
989
1060
|
const ui = UI_ROUTES[url.pathname];
|
|
990
1061
|
if (ui)
|
|
991
1062
|
return send(res, 200, ui[1], (0, fs_1.readFileSync)((0, path_1.join)(UI_DIR, ui[0])));
|
|
@@ -1017,6 +1088,7 @@ function createSpecServer(initialRoots, specpilotVersion, opts = {}) {
|
|
|
1017
1088
|
server,
|
|
1018
1089
|
mcpToken,
|
|
1019
1090
|
streams: () => allStreams().length,
|
|
1091
|
+
pending: () => pendingTarget,
|
|
1020
1092
|
stopClone,
|
|
1021
1093
|
close: async () => {
|
|
1022
1094
|
await stopClone();
|