sdtk-spec-kit 0.3.16 → 0.3.18
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 +131 -2
- package/assets/atlas/build_atlas.py +865 -0
- package/assets/atlas/doc_atlas_viewer_template.html +3706 -0
- package/assets/atlas/vendor/mermaid.min.js +2029 -0
- package/assets/manifest/toolkit-bundle.manifest.json +6 -6
- package/assets/manifest/toolkit-bundle.sha256.txt +2 -2
- package/assets/toolkit/toolkit/SDTK_TOOLKIT.md +14 -0
- package/assets/toolkit/toolkit/skills/sdtk-orchestrator/SKILL.md +1 -1
- package/package.json +1 -1
- package/src/commands/atlas.js +379 -0
- package/src/commands/entitlement.js +163 -0
- package/src/commands/help.js +140 -6
- package/src/commands/project.js +51 -0
- package/src/index.js +10 -1
- package/src/lib/atlas-ask.js +170 -0
- package/src/lib/atlas-config.js +200 -0
- package/src/lib/atlas-runner.js +355 -0
- package/src/lib/browser-open.js +39 -0
- package/src/lib/capabilities.js +98 -0
- package/src/lib/entitlements.js +203 -0
- package/src/lib/github-access.js +75 -0
- package/src/lib/legacy-auth-migration.js +56 -0
- package/src/lib/premium-loader.js +126 -0
- package/src/lib/premium-packs.js +333 -0
- package/src/lib/premium-sync.js +235 -0
- package/src/lib/project-audit.js +332 -0
- package/src/lib/project-config.js +108 -0
- package/src/lib/project-ingest.js +450 -0
- package/src/lib/project-refresh.js +666 -0
- package/src/lib/project-target-guard.js +28 -2
- package/src/lib/state.js +37 -46
- package/src/lib/suite-state.js +65 -0
package/src/commands/help.js
CHANGED
|
@@ -8,11 +8,13 @@ function cmdHelp() {
|
|
|
8
8
|
" sdtk-spec <command> [options]",
|
|
9
9
|
"",
|
|
10
10
|
"Commands:",
|
|
11
|
-
" init
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
11
|
+
" init Initialize SDTK-SPEC workspace (runtime adapter + config)",
|
|
12
|
+
" atlas Build and browse a local document graph for your project",
|
|
13
|
+
" runtime Manage runtime asset installation (install, uninstall, status)",
|
|
14
|
+
" update Refresh the npm package and optionally re-apply managed project/runtime assets",
|
|
15
|
+
" auth Manage GitHub authentication and entitlement",
|
|
16
|
+
" entitlement Inspect local entitlement and capability state",
|
|
17
|
+
" generate Scaffold feature documentation from templates (17 files)",
|
|
16
18
|
"",
|
|
17
19
|
"Init options:",
|
|
18
20
|
" --runtime <codex|claude> Runtime adapter (default: codex)",
|
|
@@ -65,6 +67,10 @@ function cmdHelp() {
|
|
|
65
67
|
" --status Show authentication status",
|
|
66
68
|
" --logout Clear stored credentials",
|
|
67
69
|
"",
|
|
70
|
+
"Entitlement subcommands:",
|
|
71
|
+
" sdtk-spec entitlement status Show local entitlement and capability state (local-only, no network)",
|
|
72
|
+
" sdtk-spec entitlement sync Fetch and verify a signed entitlement manifest; install required SPEC packs",
|
|
73
|
+
"",
|
|
68
74
|
"Generate options:",
|
|
69
75
|
" --feature-key <KEY> Feature identifier (UPPER_SNAKE_CASE, required)",
|
|
70
76
|
" --feature-name \"<text>\" Human-readable feature name (required)",
|
|
@@ -73,6 +79,114 @@ function cmdHelp() {
|
|
|
73
79
|
" --validate-only Validate templates without writing files",
|
|
74
80
|
" --verbose Show detailed PowerShell script output",
|
|
75
81
|
"",
|
|
82
|
+
"Atlas subcommands:",
|
|
83
|
+
" sdtk-spec atlas init Initialize Atlas config and run first build (opens viewer)",
|
|
84
|
+
" sdtk-spec atlas build Rebuild the local document graph from project markdown",
|
|
85
|
+
" sdtk-spec atlas open Open the last successful atlas build in a local browser",
|
|
86
|
+
" sdtk-spec atlas watch Watch for markdown changes and rebuild automatically",
|
|
87
|
+
" sdtk-spec atlas status Show initialization state and last build summary",
|
|
88
|
+
" sdtk-spec atlas ask Ask grounded questions over local Atlas docs (Pro)",
|
|
89
|
+
"",
|
|
90
|
+
"Project subcommands (Pro):",
|
|
91
|
+
" sdtk-spec project ingest Build reusable project census and profile foundation from an existing repo (Pro)",
|
|
92
|
+
" sdtk-spec project audit Read-only audit: readiness score, gap list, risk list, next steps (Pro)",
|
|
93
|
+
" sdtk-spec project refresh Incremental managed refresh of .sdtk/project/ artifacts (Pro)",
|
|
94
|
+
"",
|
|
95
|
+
"Atlas options (init):",
|
|
96
|
+
" --project-path <path> Target project directory (default: cwd)",
|
|
97
|
+
" --output-dir <path> Atlas output directory (default: <project>/.sdtk/atlas)",
|
|
98
|
+
" --scan-root <path> Scan root (repeatable; default: project root)",
|
|
99
|
+
" --force Overwrite existing Atlas config",
|
|
100
|
+
" --no-build Skip initial build",
|
|
101
|
+
" --no-open Skip opening the viewer",
|
|
102
|
+
" --verbose Show detailed build output",
|
|
103
|
+
"",
|
|
104
|
+
"Atlas options (build):",
|
|
105
|
+
" --project-path <path> Target project directory (default: cwd)",
|
|
106
|
+
" --output-dir <path> Atlas output directory (default: <project>/.sdtk/atlas)",
|
|
107
|
+
" --scan-root <path> Scan root (repeatable; default: project root)",
|
|
108
|
+
" --verbose Show incremental build detail",
|
|
109
|
+
"",
|
|
110
|
+
"Atlas options (open):",
|
|
111
|
+
" --project-path <path> Target project directory (default: cwd)",
|
|
112
|
+
" --output-dir <path> Atlas output directory (default: <project>/.sdtk/atlas)",
|
|
113
|
+
" --host <host> Viewer server host (default: 127.0.0.1)",
|
|
114
|
+
" --port <port> Viewer server port (default: 8765)",
|
|
115
|
+
" --no-open Print viewer URL without opening browser",
|
|
116
|
+
"",
|
|
117
|
+
"Atlas options (watch):",
|
|
118
|
+
" --project-path <path> Target project directory (default: cwd)",
|
|
119
|
+
" --output-dir <path> Atlas output directory (default: <project>/.sdtk/atlas)",
|
|
120
|
+
" --scan-root <path> Scan root (repeatable; default: project root)",
|
|
121
|
+
" --port <port> Viewer server port (default: 8765)",
|
|
122
|
+
" --no-open Skip opening the viewer after initial build",
|
|
123
|
+
"",
|
|
124
|
+
"Atlas options (ask):",
|
|
125
|
+
" --project-path <path> Target project directory (default: cwd)",
|
|
126
|
+
" --output-dir <path> Atlas output directory (default: <project>/.sdtk/atlas)",
|
|
127
|
+
" --question <text> Question to ask (required; or provide as positional argument)",
|
|
128
|
+
" --source <path-or-id> Explicit grounding hint (repeatable)",
|
|
129
|
+
" --max-docs <number> Max documents hint passed to premium pack (default: 12)",
|
|
130
|
+
" --json Print answer as JSON",
|
|
131
|
+
" --verbose Show detailed output",
|
|
132
|
+
"",
|
|
133
|
+
"Atlas notes:",
|
|
134
|
+
" - Free Atlas commands (init, build, open, watch, status) require no authentication or entitlement.",
|
|
135
|
+
" - atlas ask is a Pro feature. It requires a valid Pro entitlement and a cached premium pack.",
|
|
136
|
+
" - Run 'sdtk-spec entitlement sync' to install the required premium pack for atlas ask.",
|
|
137
|
+
" - Atlas scans markdown files and builds a local document graph and static viewer.",
|
|
138
|
+
" - Artifacts are written under <project>/.sdtk/atlas/ (project-local, not global).",
|
|
139
|
+
" - Viewer server binds to 127.0.0.1 by default (loopback only).",
|
|
140
|
+
" - Requires Python 3.8+ in PATH for the build step.",
|
|
141
|
+
"",
|
|
142
|
+
"Project ingest options (Pro):",
|
|
143
|
+
" --project-path <path> Target project directory to ingest (default: cwd)",
|
|
144
|
+
" --output-dir <path> Foundation output directory (default: <project>/.sdtk/project)",
|
|
145
|
+
" --json Print result summary as JSON",
|
|
146
|
+
"",
|
|
147
|
+
"Project ingest notes:",
|
|
148
|
+
" - project ingest is a Pro feature. It requires a valid Pro entitlement and a cached premium pack.",
|
|
149
|
+
" - Run 'sdtk-spec entitlement sync' to install the required premium pack for project ingest.",
|
|
150
|
+
" - project ingest is not part of sdtk-spec init. It is a separate premium command.",
|
|
151
|
+
" - Foundation artifacts are written under <project>/.sdtk/project/ (project-local, not global).",
|
|
152
|
+
" - Artifacts include census, source inventory, profile, module graph, runtime markers, and evidence packs.",
|
|
153
|
+
" - project ingest now stages a draft SDTK docs baseline under .sdtk/project/DOC_SYNTHESIS_BASELINE/.",
|
|
154
|
+
" - Staged docs are draft / human-review-first outputs. Human review is required before use.",
|
|
155
|
+
" - Live project /docs/ is not modified by project ingest.",
|
|
156
|
+
" - Maintainer-root guard: project ingest refuses the SDTK maintainer monorepo root as an ingest target.",
|
|
157
|
+
"",
|
|
158
|
+
"Project audit options (Pro):",
|
|
159
|
+
" --project-path <path> Target project directory to audit (default: cwd)",
|
|
160
|
+
" --output-dir <path> Foundation read path (default: <project>/.sdtk/project)",
|
|
161
|
+
" --json Print audit result as deterministic JSON",
|
|
162
|
+
"",
|
|
163
|
+
"Project audit notes:",
|
|
164
|
+
" - project audit is a Pro feature. It requires a valid Pro entitlement and a cached premium pack.",
|
|
165
|
+
" - Run 'sdtk-spec entitlement sync' to install the required premium pack for project audit.",
|
|
166
|
+
" - project audit is read-only. It does not modify live /docs/ or write new staged docs.",
|
|
167
|
+
" - project audit does not write new .sdtk/project/ foundation artifacts.",
|
|
168
|
+
" - When cached ingest artifacts (.sdtk/project/) exist, project audit uses them as its source.",
|
|
169
|
+
" - When no prior ingest has been run, project audit performs bounded ephemeral analysis in memory.",
|
|
170
|
+
" - Output includes readiness score/band, gap list, risk list, and bounded next-step recommendations.",
|
|
171
|
+
" - Maintainer-root guard: project audit refuses the SDTK maintainer monorepo root as an audit target.",
|
|
172
|
+
"",
|
|
173
|
+
"Project refresh options (Pro):",
|
|
174
|
+
" --project-path <path> Target project directory to refresh (default: cwd)",
|
|
175
|
+
" --output-dir <path> Foundation output directory (default: <project>/.sdtk/project)",
|
|
176
|
+
" --json Print result summary as JSON",
|
|
177
|
+
"",
|
|
178
|
+
"Project refresh notes:",
|
|
179
|
+
" - project refresh is a Pro feature. It requires a valid Pro entitlement and a cached premium pack.",
|
|
180
|
+
" - Run 'sdtk-spec entitlement sync' to install the required premium pack for project refresh.",
|
|
181
|
+
" - project refresh is incremental. It requires an existing managed ingest baseline.",
|
|
182
|
+
" - If no prior ingest baseline exists, run 'sdtk-spec project ingest' first.",
|
|
183
|
+
" - project refresh updates only .sdtk/project/ artifacts (foundation JSON, evidence packs, staged docs).",
|
|
184
|
+
" - project refresh may remove stale managed staged-doc files from the staged baseline (.sdtk/project/DOC_SYNTHESIS_BASELINE/docs/).",
|
|
185
|
+
" - project refresh does not modify live /docs/.",
|
|
186
|
+
" - project refresh does not replace or extend project ingest (first-run bootstrap).",
|
|
187
|
+
" - project refresh does not perform a read-only audit; use 'project audit' for that.",
|
|
188
|
+
" - Maintainer-root guard: project refresh refuses the SDTK maintainer monorepo root as a refresh target.",
|
|
189
|
+
"",
|
|
76
190
|
"Generate behavior:",
|
|
77
191
|
" The generate command creates 17 scaffold files with TBD placeholders.",
|
|
78
192
|
" Scaffolds define document structure for each SDLC phase.",
|
|
@@ -91,9 +205,21 @@ function cmdHelp() {
|
|
|
91
205
|
" 4 Unexpected internal error",
|
|
92
206
|
"",
|
|
93
207
|
"Environment variables:",
|
|
94
|
-
" SDTK_ENTITLEMENT_REPO
|
|
208
|
+
" SDTK_ENTITLEMENT_REPO Override default entitlement repo (owner/repo format)",
|
|
209
|
+
" SDTK_ENTITLEMENT_MANIFEST_PATH Override entitlement manifest path in repo (default: entitlements/spec/entitlements.json)",
|
|
210
|
+
" SDTK_ENTITLEMENT_REF Override entitlement manifest ref/branch (default: main)",
|
|
211
|
+
" SDTK_ENTITLEMENT_PUBLIC_KEY RSA public key (PEM) used to verify entitlement manifests",
|
|
95
212
|
"",
|
|
96
213
|
"Examples:",
|
|
214
|
+
" sdtk-spec atlas init --project-path ./my-project",
|
|
215
|
+
" sdtk-spec atlas init --project-path ./my-project --no-open",
|
|
216
|
+
" sdtk-spec atlas build --project-path ./my-project --verbose",
|
|
217
|
+
" sdtk-spec atlas open --project-path ./my-project --port 9000",
|
|
218
|
+
" sdtk-spec atlas open --project-path ./my-project --no-open",
|
|
219
|
+
" sdtk-spec atlas watch --project-path ./my-project",
|
|
220
|
+
" sdtk-spec atlas status --project-path ./my-project",
|
|
221
|
+
" sdtk-spec atlas ask --project-path ./my-project --question \"How do I install this toolkit?\"",
|
|
222
|
+
" sdtk-spec atlas ask --project-path ./my-project --question \"What does this project say about deployment?\" --json",
|
|
97
223
|
" sdtk-spec init --runtime codex",
|
|
98
224
|
" sdtk-spec init --runtime codex --runtime-scope project --project-path ./my-project",
|
|
99
225
|
" sdtk-spec init --runtime claude --project-path ./my-project",
|
|
@@ -110,6 +236,14 @@ function cmdHelp() {
|
|
|
110
236
|
" sdtk-spec runtime install --runtime codex --scope project --project-path ./my-project",
|
|
111
237
|
" sdtk-spec runtime status --runtime claude",
|
|
112
238
|
" sdtk-spec runtime uninstall --runtime claude --scope project --all",
|
|
239
|
+
" sdtk-spec entitlement status",
|
|
240
|
+
" sdtk-spec entitlement sync",
|
|
241
|
+
" sdtk-spec project ingest --project-path ./my-project",
|
|
242
|
+
" sdtk-spec project ingest --project-path ./my-project --json",
|
|
243
|
+
" sdtk-spec project audit --project-path ./my-project",
|
|
244
|
+
" sdtk-spec project audit --project-path ./my-project --json",
|
|
245
|
+
" sdtk-spec project refresh --project-path ./my-project",
|
|
246
|
+
" sdtk-spec project refresh --project-path ./my-project --json",
|
|
113
247
|
"",
|
|
114
248
|
" # Override entitlement repo (bash/zsh):",
|
|
115
249
|
" export SDTK_ENTITLEMENT_REPO=owner/repo",
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { runProjectIngest } = require("../lib/project-ingest");
|
|
4
|
+
const { runProjectAudit } = require("../lib/project-audit");
|
|
5
|
+
const { runProjectRefresh } = require("../lib/project-refresh");
|
|
6
|
+
const { ValidationError } = require("../lib/errors");
|
|
7
|
+
|
|
8
|
+
const PROJECT_SUBCOMMANDS = new Set(["ingest", "audit", "refresh"]);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Top-level dispatcher for `sdtk-spec project <subcommand>`.
|
|
12
|
+
*
|
|
13
|
+
* Subcommands (BK-083 R1):
|
|
14
|
+
* ingest - Build reusable project census and profile foundation (Pro)
|
|
15
|
+
*
|
|
16
|
+
* Subcommands (BK-086 R1):
|
|
17
|
+
* audit - Read-only audit report: readiness, gaps, risks, next steps (Pro)
|
|
18
|
+
*
|
|
19
|
+
* Subcommands (BK-085 R1):
|
|
20
|
+
* refresh - Incremental managed refresh of .sdtk/project/ artifacts (Pro)
|
|
21
|
+
*
|
|
22
|
+
* @param {string[]} args - Remaining argv after "project"
|
|
23
|
+
* @returns {Promise<number>} CLI exit code
|
|
24
|
+
*/
|
|
25
|
+
async function cmdProject(args) {
|
|
26
|
+
if (!args || args.length === 0) {
|
|
27
|
+
console.error("[project] Usage: sdtk-spec project <subcommand> [options]");
|
|
28
|
+
console.error(" Premium subcommands: ingest, audit, refresh (requires Pro entitlement)");
|
|
29
|
+
console.error(" Run 'sdtk-spec --help' for full usage.");
|
|
30
|
+
return 1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const [subcommand, ...rest] = args;
|
|
34
|
+
|
|
35
|
+
if (!PROJECT_SUBCOMMANDS.has(subcommand)) {
|
|
36
|
+
throw new ValidationError(
|
|
37
|
+
`Unknown project subcommand: "${subcommand}". Valid subcommands: ingest, audit, refresh.`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
switch (subcommand) {
|
|
42
|
+
case "ingest":
|
|
43
|
+
return runProjectIngest(rest);
|
|
44
|
+
case "audit":
|
|
45
|
+
return runProjectAudit(rest);
|
|
46
|
+
case "refresh":
|
|
47
|
+
return runProjectRefresh(rest);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
module.exports = { cmdProject };
|
package/src/index.js
CHANGED
|
@@ -3,9 +3,12 @@
|
|
|
3
3
|
const { cmdHelp } = require("./commands/help");
|
|
4
4
|
const { cmdInit } = require("./commands/init");
|
|
5
5
|
const { cmdAuth } = require("./commands/auth");
|
|
6
|
+
const { cmdEntitlement } = require("./commands/entitlement");
|
|
6
7
|
const { cmdGenerate } = require("./commands/generate");
|
|
7
8
|
const { cmdRuntime } = require("./commands/runtime");
|
|
8
9
|
const { cmdUpdate } = require("./commands/update");
|
|
10
|
+
const { cmdAtlas } = require("./commands/atlas");
|
|
11
|
+
const { cmdProject } = require("./commands/project");
|
|
9
12
|
const { ValidationError, CliError } = require("./lib/errors");
|
|
10
13
|
|
|
11
14
|
function getVersion() {
|
|
@@ -27,7 +30,7 @@ function parseCommand(argv) {
|
|
|
27
30
|
return { command: first, args: rest };
|
|
28
31
|
}
|
|
29
32
|
|
|
30
|
-
const COMMANDS = new Set(["help", "version", "init", "auth", "generate", "runtime", "update"]);
|
|
33
|
+
const COMMANDS = new Set(["help", "version", "init", "auth", "entitlement", "generate", "runtime", "update", "atlas", "project"]);
|
|
31
34
|
|
|
32
35
|
async function run(argv) {
|
|
33
36
|
const { command, args } = parseCommand(argv);
|
|
@@ -48,12 +51,18 @@ async function run(argv) {
|
|
|
48
51
|
return cmdInit(args);
|
|
49
52
|
case "auth":
|
|
50
53
|
return cmdAuth(args);
|
|
54
|
+
case "entitlement":
|
|
55
|
+
return cmdEntitlement(args);
|
|
51
56
|
case "generate":
|
|
52
57
|
return cmdGenerate(args);
|
|
53
58
|
case "runtime":
|
|
54
59
|
return cmdRuntime(args);
|
|
55
60
|
case "update":
|
|
56
61
|
return cmdUpdate(args);
|
|
62
|
+
case "atlas":
|
|
63
|
+
return cmdAtlas(args);
|
|
64
|
+
case "project":
|
|
65
|
+
return cmdProject(args);
|
|
57
66
|
}
|
|
58
67
|
}
|
|
59
68
|
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const { parseFlags } = require("./args");
|
|
5
|
+
const { resolveAtlasConfig, isAtlasBuilt } = require("./atlas-config");
|
|
6
|
+
const { loadPremiumHandler } = require("./premium-loader");
|
|
7
|
+
|
|
8
|
+
const CAPABILITY = "spec.atlas.ask";
|
|
9
|
+
|
|
10
|
+
const ASK_FLAG_DEFS = {
|
|
11
|
+
"project-path": { type: "string" },
|
|
12
|
+
"output-dir": { type: "string" },
|
|
13
|
+
"scan-root": { type: "string" },
|
|
14
|
+
question: { type: "string" },
|
|
15
|
+
"max-docs": { type: "string" },
|
|
16
|
+
json: { type: "boolean" },
|
|
17
|
+
verbose: { type: "boolean" },
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Pre-filter repeated --source flags before delegating to parseFlags.
|
|
22
|
+
* parseFlags only captures the last occurrence for string flags.
|
|
23
|
+
*
|
|
24
|
+
* @param {string[]} args
|
|
25
|
+
* @returns {{ sources: string[], filteredArgs: string[] }}
|
|
26
|
+
*/
|
|
27
|
+
function extractSources(args) {
|
|
28
|
+
const sources = [];
|
|
29
|
+
const filteredArgs = [];
|
|
30
|
+
let i = 0;
|
|
31
|
+
while (i < args.length) {
|
|
32
|
+
const arg = args[i];
|
|
33
|
+
if (arg === "--source") {
|
|
34
|
+
i++;
|
|
35
|
+
if (i < args.length) {
|
|
36
|
+
sources.push(args[i]);
|
|
37
|
+
i++;
|
|
38
|
+
}
|
|
39
|
+
} else if (arg.startsWith("--source=")) {
|
|
40
|
+
sources.push(arg.slice("--source=".length));
|
|
41
|
+
i++;
|
|
42
|
+
} else {
|
|
43
|
+
filteredArgs.push(arg);
|
|
44
|
+
i++;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return { sources, filteredArgs };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Execute the sdtk-spec atlas ask premium command.
|
|
52
|
+
*
|
|
53
|
+
* Flow:
|
|
54
|
+
* 1. Parse and validate --question or positional question.
|
|
55
|
+
* 2. Resolve Atlas config via resolveAtlasConfig.
|
|
56
|
+
* 3. Require built Atlas artifacts via isAtlasBuilt.
|
|
57
|
+
* 4. Load and verify the premium handler via loadPremiumHandler.
|
|
58
|
+
* 5. Build the bounded context object.
|
|
59
|
+
* 6. Execute the premium handler.
|
|
60
|
+
* 7. Format and print the result.
|
|
61
|
+
*
|
|
62
|
+
* @param {string[]} args - Remaining argv after "atlas ask"
|
|
63
|
+
* @returns {Promise<number>} CLI exit code
|
|
64
|
+
*/
|
|
65
|
+
async function runAtlasAsk(args) {
|
|
66
|
+
const { sources, filteredArgs } = extractSources(args);
|
|
67
|
+
const { flags, positional } = parseFlags(filteredArgs, ASK_FLAG_DEFS);
|
|
68
|
+
|
|
69
|
+
// 1. Resolve question from --question flag or positional args
|
|
70
|
+
const questionRaw =
|
|
71
|
+
(flags.question && flags.question.trim()) ||
|
|
72
|
+
(positional.length > 0 ? positional.join(" ").trim() : "");
|
|
73
|
+
|
|
74
|
+
if (!questionRaw) {
|
|
75
|
+
console.error("[atlas ask] Error: question is required.");
|
|
76
|
+
console.error(
|
|
77
|
+
" Use --question <text> or provide the question as positional arguments."
|
|
78
|
+
);
|
|
79
|
+
console.error(
|
|
80
|
+
" Example: sdtk-spec atlas ask --question \"How do I install this toolkit?\""
|
|
81
|
+
);
|
|
82
|
+
return 1;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 2. Resolve Atlas config
|
|
86
|
+
let config;
|
|
87
|
+
try {
|
|
88
|
+
config = resolveAtlasConfig(flags);
|
|
89
|
+
} catch (err) {
|
|
90
|
+
console.error(`[atlas ask] Config error: ${err.message}`);
|
|
91
|
+
return 1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 3. Require built Atlas artifacts (SDTK_DOC_INDEX.json + viewer.html)
|
|
95
|
+
if (!isAtlasBuilt(config.outputDir)) {
|
|
96
|
+
console.error("[atlas ask] Atlas has not been built for this project.");
|
|
97
|
+
console.error(` Expected artifacts at: ${config.outputDir}`);
|
|
98
|
+
console.error(" Run: sdtk-spec atlas init");
|
|
99
|
+
console.error(" Or: sdtk-spec atlas build");
|
|
100
|
+
return 1;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 4. Load and verify premium handler (entitlement + pack verification)
|
|
104
|
+
const loaderResult = await loadPremiumHandler(CAPABILITY, "atlasAsk");
|
|
105
|
+
if (!loaderResult.ok) {
|
|
106
|
+
console.error(`[atlas ask] ${loaderResult.message}`);
|
|
107
|
+
return loaderResult.exitCode;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 5. Build bounded context object
|
|
111
|
+
const maxDocsRaw = flags["max-docs"] ? parseInt(flags["max-docs"], 10) : NaN;
|
|
112
|
+
const maxDocs = !isNaN(maxDocsRaw) ? maxDocsRaw : 12;
|
|
113
|
+
|
|
114
|
+
const context = {
|
|
115
|
+
capability: CAPABILITY,
|
|
116
|
+
projectPath: config.projectPath,
|
|
117
|
+
outputDir: config.outputDir,
|
|
118
|
+
configPath: config.configPath,
|
|
119
|
+
indexPath: path.join(config.outputDir, "SDTK_DOC_INDEX.json"),
|
|
120
|
+
graphPath: path.join(config.outputDir, "SDTK_DOC_GRAPH.json"),
|
|
121
|
+
question: questionRaw,
|
|
122
|
+
sources,
|
|
123
|
+
maxDocs,
|
|
124
|
+
json: !!flags.json,
|
|
125
|
+
argv: args,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// 6. Execute premium handler
|
|
129
|
+
let result;
|
|
130
|
+
try {
|
|
131
|
+
result = await loaderResult.handler(context);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
console.error(`[atlas ask] Premium pack handler error: ${err.message}`);
|
|
134
|
+
return 4;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 7. Format and print result
|
|
138
|
+
if (typeof result === "string") {
|
|
139
|
+
if (flags.json) {
|
|
140
|
+
console.log(JSON.stringify({ answer: result }));
|
|
141
|
+
} else {
|
|
142
|
+
console.log(result);
|
|
143
|
+
}
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (result !== null && typeof result === "object") {
|
|
148
|
+
const exitCode = typeof result.exitCode === "number" ? result.exitCode : 0;
|
|
149
|
+
if (flags.json) {
|
|
150
|
+
const output = Object.assign({}, result);
|
|
151
|
+
delete output.exitCode;
|
|
152
|
+
console.log(JSON.stringify(output));
|
|
153
|
+
} else {
|
|
154
|
+
if (typeof result.answer === "string") {
|
|
155
|
+
console.log(result.answer);
|
|
156
|
+
}
|
|
157
|
+
if (Array.isArray(result.citations) && result.citations.length > 0) {
|
|
158
|
+
console.log("\nSources:");
|
|
159
|
+
for (const c of result.citations) {
|
|
160
|
+
console.log(` - ${c}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return exitCode;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
module.exports = { runAtlasAsk };
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
const { ValidationError } = require("./errors");
|
|
6
|
+
|
|
7
|
+
const CONFIG_SCHEMA_VERSION = 1;
|
|
8
|
+
const DEFAULT_PORT = 8765;
|
|
9
|
+
const DEFAULT_HOST = "127.0.0.1";
|
|
10
|
+
|
|
11
|
+
const DEFAULT_EXCLUDES = [
|
|
12
|
+
".git",
|
|
13
|
+
".sdtk/atlas",
|
|
14
|
+
"node_modules",
|
|
15
|
+
".venv",
|
|
16
|
+
"venv",
|
|
17
|
+
"dist",
|
|
18
|
+
"build",
|
|
19
|
+
"coverage",
|
|
20
|
+
".next",
|
|
21
|
+
".turbo",
|
|
22
|
+
".cache",
|
|
23
|
+
"__pycache__",
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Resolve Atlas configuration from CLI flags and optional persisted config.
|
|
28
|
+
*
|
|
29
|
+
* Priority: CLI flags > persisted config > defaults.
|
|
30
|
+
*
|
|
31
|
+
* @param {Object} flags - Parsed CLI flags.
|
|
32
|
+
* @returns {AtlasConfig}
|
|
33
|
+
*/
|
|
34
|
+
function resolveAtlasConfig(flags) {
|
|
35
|
+
const projectPath = flags["project-path"]
|
|
36
|
+
? path.resolve(flags["project-path"])
|
|
37
|
+
: process.cwd();
|
|
38
|
+
|
|
39
|
+
if (!fs.existsSync(projectPath) || !fs.statSync(projectPath).isDirectory()) {
|
|
40
|
+
throw new ValidationError(
|
|
41
|
+
`--project-path is not a valid directory: ${projectPath}`
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const defaultOutputDir = path.join(projectPath, ".sdtk", "atlas");
|
|
46
|
+
const outputDir = flags["output-dir"]
|
|
47
|
+
? path.resolve(flags["output-dir"])
|
|
48
|
+
: defaultOutputDir;
|
|
49
|
+
|
|
50
|
+
// Read persisted config if it exists
|
|
51
|
+
const configPath = path.join(outputDir, "config.json");
|
|
52
|
+
let persisted = {};
|
|
53
|
+
if (fs.existsSync(configPath)) {
|
|
54
|
+
try {
|
|
55
|
+
persisted = JSON.parse(fs.readFileSync(configPath, "utf-8"));
|
|
56
|
+
} catch (_) {
|
|
57
|
+
// ignore corrupt config, use defaults
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Resolve scan roots: CLI flag > persisted > [projectPath]
|
|
62
|
+
let scanRoots;
|
|
63
|
+
if (flags["scan-root"] && flags["scan-root"].length > 0) {
|
|
64
|
+
scanRoots = flags["scan-root"].map((r) => path.resolve(r));
|
|
65
|
+
} else if (persisted.scanRoots && Array.isArray(persisted.scanRoots) && persisted.scanRoots.length > 0) {
|
|
66
|
+
scanRoots = persisted.scanRoots.map((r) => path.resolve(r));
|
|
67
|
+
} else {
|
|
68
|
+
scanRoots = [projectPath];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const excludes =
|
|
72
|
+
persisted.excludes && Array.isArray(persisted.excludes)
|
|
73
|
+
? persisted.excludes
|
|
74
|
+
: DEFAULT_EXCLUDES.slice();
|
|
75
|
+
|
|
76
|
+
const host = flags["host"] || persisted.host || DEFAULT_HOST;
|
|
77
|
+
const port = flags["port"]
|
|
78
|
+
? parseInt(flags["port"], 10)
|
|
79
|
+
: persisted.port || DEFAULT_PORT;
|
|
80
|
+
|
|
81
|
+
if (isNaN(port) || port < 1 || port > 65535) {
|
|
82
|
+
throw new ValidationError(`Invalid --port value: ${flags["port"]}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
projectPath,
|
|
87
|
+
outputDir,
|
|
88
|
+
configPath,
|
|
89
|
+
scanRoots,
|
|
90
|
+
excludes,
|
|
91
|
+
host,
|
|
92
|
+
port,
|
|
93
|
+
verbose: !!flags.verbose,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Write the persisted Atlas config to .sdtk/atlas/config.json.
|
|
99
|
+
* Creates the output directory if it does not exist.
|
|
100
|
+
*
|
|
101
|
+
* @param {Object} config - Atlas config object from resolveAtlasConfig.
|
|
102
|
+
*/
|
|
103
|
+
function writeAtlasConfig(config) {
|
|
104
|
+
fs.mkdirSync(config.outputDir, { recursive: true });
|
|
105
|
+
|
|
106
|
+
const payload = {
|
|
107
|
+
schemaVersion: CONFIG_SCHEMA_VERSION,
|
|
108
|
+
projectPath: config.projectPath,
|
|
109
|
+
outputDir: config.outputDir,
|
|
110
|
+
scanRoots: config.scanRoots,
|
|
111
|
+
excludes: config.excludes,
|
|
112
|
+
host: config.host,
|
|
113
|
+
port: config.port,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
fs.writeFileSync(config.configPath, JSON.stringify(payload, null, 2) + "\n", "utf-8");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Check whether Atlas has been initialized (config.json exists).
|
|
121
|
+
*
|
|
122
|
+
* @param {string} outputDir
|
|
123
|
+
* @returns {boolean}
|
|
124
|
+
*/
|
|
125
|
+
function isAtlasInitialized(outputDir) {
|
|
126
|
+
return fs.existsSync(path.join(outputDir, "config.json"));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Check whether a successful Atlas build exists (index artifact present).
|
|
131
|
+
*
|
|
132
|
+
* @param {string} outputDir
|
|
133
|
+
* @returns {boolean}
|
|
134
|
+
*/
|
|
135
|
+
function isAtlasBuilt(outputDir) {
|
|
136
|
+
return (
|
|
137
|
+
fs.existsSync(path.join(outputDir, "SDTK_DOC_INDEX.json")) &&
|
|
138
|
+
fs.existsSync(path.join(outputDir, "viewer.html"))
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Read last build metadata from SDTK_DOC_INDEX.json if it exists.
|
|
144
|
+
*
|
|
145
|
+
* @param {string} outputDir
|
|
146
|
+
* @returns {{ generated: string, count: number } | null}
|
|
147
|
+
*/
|
|
148
|
+
function readBuildMeta(outputDir) {
|
|
149
|
+
const indexPath = path.join(outputDir, "SDTK_DOC_INDEX.json");
|
|
150
|
+
if (!fs.existsSync(indexPath)) return null;
|
|
151
|
+
try {
|
|
152
|
+
const data = JSON.parse(fs.readFileSync(indexPath, "utf-8"));
|
|
153
|
+
return { generated: data.generated || null, count: data.count || 0 };
|
|
154
|
+
} catch (_) {
|
|
155
|
+
return null;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Read last graph metadata from SDTK_DOC_GRAPH.json if it exists.
|
|
161
|
+
*
|
|
162
|
+
* @param {string} outputDir
|
|
163
|
+
* @returns {{ nodeCount: number, edgeCount: number } | null}
|
|
164
|
+
*/
|
|
165
|
+
function readGraphMeta(outputDir) {
|
|
166
|
+
const graphPath = path.join(outputDir, "SDTK_DOC_GRAPH.json");
|
|
167
|
+
if (!fs.existsSync(graphPath)) return null;
|
|
168
|
+
try {
|
|
169
|
+
const data = JSON.parse(fs.readFileSync(graphPath, "utf-8"));
|
|
170
|
+
return {
|
|
171
|
+
nodeCount: data.node_count || 0,
|
|
172
|
+
edgeCount: data.edge_count || 0,
|
|
173
|
+
};
|
|
174
|
+
} catch (_) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Resolve the path to the packaged atlas builder Python script.
|
|
181
|
+
*
|
|
182
|
+
* @returns {string} Absolute path to build_atlas.py.
|
|
183
|
+
*/
|
|
184
|
+
function resolveBuilderPath() {
|
|
185
|
+
const p = path.resolve(__dirname, "..", "..", "assets", "atlas", "build_atlas.py");
|
|
186
|
+
return p;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
module.exports = {
|
|
190
|
+
resolveAtlasConfig,
|
|
191
|
+
writeAtlasConfig,
|
|
192
|
+
isAtlasInitialized,
|
|
193
|
+
isAtlasBuilt,
|
|
194
|
+
readBuildMeta,
|
|
195
|
+
readGraphMeta,
|
|
196
|
+
resolveBuilderPath,
|
|
197
|
+
DEFAULT_EXCLUDES,
|
|
198
|
+
DEFAULT_PORT,
|
|
199
|
+
DEFAULT_HOST,
|
|
200
|
+
};
|