sdtk-code-kit 0.3.0 → 0.3.2
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 +204 -204
- package/assets/manifest/toolkit-bundle.manifest.json +147 -147
- package/assets/manifest/toolkit-bundle.sha256.txt +5 -5
- package/bin/sdtk-code.js +14 -14
- package/package.json +44 -44
- package/src/commands/agent-team.js +479 -174
- package/src/commands/build.js +53 -53
- package/src/commands/doctor.js +51 -51
- package/src/commands/generate.js +11 -11
- package/src/commands/guardrails.js +215 -215
- package/src/commands/help.js +266 -257
- package/src/commands/init.js +98 -98
- package/src/commands/plan.js +53 -53
- package/src/commands/readiness.js +77 -77
- package/src/commands/resume.js +61 -61
- package/src/commands/runtime.js +224 -224
- package/src/commands/ship.js +59 -59
- package/src/commands/sleep.js +132 -132
- package/src/commands/start.js +89 -89
- package/src/commands/status.js +77 -77
- package/src/commands/update.js +11 -11
- package/src/commands/verify.js +71 -71
- package/src/index.js +112 -112
- package/src/lib/agent-team-author.js +552 -0
- package/src/lib/agent-team-evidence.js +302 -0
- package/src/lib/agent-team-execute.js +892 -0
- package/src/lib/agent-team-export.js +411 -409
- package/src/lib/agent-team-plan.js +514 -514
- package/src/lib/agent-team-readiness.js +161 -0
- package/src/lib/agent-team-report.js +165 -0
- package/src/lib/agent-team-sleep-execute.js +524 -0
- package/src/lib/agent-team-sleep-session.js +278 -0
- package/src/lib/args.js +107 -107
- package/src/lib/errors.js +41 -41
- package/src/lib/model-provider.js +219 -0
- package/src/lib/powershell.js +65 -65
- package/src/lib/project-target-guard.js +53 -53
- package/src/lib/scope.js +72 -72
- package/src/lib/ship-readiness.js +281 -281
- package/src/lib/sleep-plan.js +175 -175
- package/src/lib/sleep-report.js +229 -229
- package/src/lib/toolkit-payload.js +72 -72
- package/src/lib/trust-events.js +264 -185
- package/src/lib/trust-file-scope.js +154 -154
- package/src/lib/trust-policy.js +212 -212
- package/src/lib/trust-redact.js +58 -35
- package/src/lib/trust-trace.js +179 -179
- package/src/lib/update.js +344 -344
- package/src/lib/workflow-artifact.js +503 -503
- package/src/lib/workflow-build.js +127 -127
- package/src/lib/workflow-doctor.js +83 -83
- package/src/lib/workflow-handoff.js +1029 -1029
- package/src/lib/workflow-intake.js +101 -101
- package/src/lib/workflow-plan.js +193 -193
- package/src/lib/workflow-review-packet.js +650 -650
- package/src/lib/workflow-ship.js +213 -213
- package/src/lib/workflow-state.js +438 -438
- package/src/lib/workflow-verify.js +393 -393
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const crypto = require("crypto");
|
|
4
|
+
const { ValidationError } = require("./errors");
|
|
5
|
+
const { safeSegment } = require("./agent-team-export");
|
|
6
|
+
const { redact } = require("./trust-redact");
|
|
7
|
+
|
|
8
|
+
const SESSION_MAX_ITERATIONS = 3;
|
|
9
|
+
const SESSION_MAX_COST_USD = 3.00;
|
|
10
|
+
const SESSION_MAX_OUTPUT_TOKENS = 48000;
|
|
11
|
+
const SESSION_WALLCLOCK_TIMEOUT_MS = 30 * 60 * 1000;
|
|
12
|
+
const SESSION_DOC_TARGET_PATTERNS = Object.freeze([
|
|
13
|
+
"docs/**/*.md",
|
|
14
|
+
"governance/**/*.md",
|
|
15
|
+
"README.md",
|
|
16
|
+
"AGENTS.md",
|
|
17
|
+
]);
|
|
18
|
+
const SESSION_ALLOWED_COMMANDS = Object.freeze([
|
|
19
|
+
"python3 scripts/ci/docs_lint.py",
|
|
20
|
+
"python3 scripts/ci/path_check.py",
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
const activeSessionIds = new Set();
|
|
24
|
+
|
|
25
|
+
function asArray(value) {
|
|
26
|
+
return Array.isArray(value) ? value : [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function sha256(value) {
|
|
30
|
+
return crypto.createHash("sha256").update(String(value || "")).digest("hex");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function makeSessionId(featureKey, seed) {
|
|
34
|
+
if (seed) return safeSegment(seed, "session_id");
|
|
35
|
+
const stamp = Date.now().toString(36);
|
|
36
|
+
const rand = crypto.randomUUID().replace(/-/g, "").slice(0, 12);
|
|
37
|
+
return safeSegment(`${featureKey}_${stamp}_${rand}`, "session_id");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function normalizeCommand(value) {
|
|
41
|
+
return String(value || "").trim();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function pinAllowedCommands(commands) {
|
|
45
|
+
const seen = new Set();
|
|
46
|
+
const out = [];
|
|
47
|
+
for (const command of asArray(commands).map(normalizeCommand)) {
|
|
48
|
+
if (SESSION_ALLOWED_COMMANDS.includes(command) && !seen.has(command)) {
|
|
49
|
+
seen.add(command);
|
|
50
|
+
out.push(command);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (out.length === 0) {
|
|
54
|
+
throw new ValidationError("FAIL_NO_ALLOWED_COMMANDS: no role verification command matches the Phase E session allowlist.");
|
|
55
|
+
}
|
|
56
|
+
return Object.freeze(out);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function pinAllowedScopes(ctx) {
|
|
60
|
+
const scopes = asArray(ctx && ctx.allowedFileScopes).map((scope) => String(scope || "").trim()).filter(Boolean);
|
|
61
|
+
if (scopes.length === 0) {
|
|
62
|
+
throw new ValidationError("FAIL_EXECUTE_SCOPE: Phase E requires pinned allowed_file_scopes.");
|
|
63
|
+
}
|
|
64
|
+
return Object.freeze(scopes);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function collectTaskList(plan) {
|
|
68
|
+
const items = [];
|
|
69
|
+
for (const value of asArray(plan && plan.implementation_slices)) {
|
|
70
|
+
if (value) items.push(String(value));
|
|
71
|
+
}
|
|
72
|
+
for (const role of asArray(plan && plan.roles)) {
|
|
73
|
+
if (role && role.objective) items.push(String(role.objective));
|
|
74
|
+
for (const value of asArray(role && role.evidence_required)) {
|
|
75
|
+
items.push(`evidence_required:${role.role}:${value}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return items;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function pinScope(ctx) {
|
|
82
|
+
const source = ctx || {};
|
|
83
|
+
return Object.freeze({
|
|
84
|
+
feature_key: source.featureKey,
|
|
85
|
+
run_id: source.runId,
|
|
86
|
+
role: source.role && source.role.role ? String(source.role.role) : "dev",
|
|
87
|
+
allowed_file_scopes: pinAllowedScopes(source),
|
|
88
|
+
doc_target_patterns: SESSION_DOC_TARGET_PATTERNS,
|
|
89
|
+
verification_commands: pinAllowedCommands(source.verificationCommands),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function redactedTaskList(plan) {
|
|
94
|
+
return collectTaskList(plan).map((item) => redact(item));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function enumerateCarriedState(data) {
|
|
98
|
+
const source = data || {};
|
|
99
|
+
const priorPatchHashes = asArray(source.priorPatchHashes).map((hash) => String(hash));
|
|
100
|
+
return Object.freeze({
|
|
101
|
+
cumulativeWorktreePath: source.cumulativeWorktreePath ? redact(String(source.cumulativeWorktreePath)) : null,
|
|
102
|
+
taskListFromPinnedPlan: redactedTaskList(source.plan),
|
|
103
|
+
priorPatchHashes: Object.freeze(priorPatchHashes),
|
|
104
|
+
accumulators: Object.freeze({
|
|
105
|
+
iteration: Number(source.iteration || 0),
|
|
106
|
+
session_cost_usd: Number(source.session_cost_usd || 0),
|
|
107
|
+
session_output_tokens: Number(source.session_output_tokens || 0),
|
|
108
|
+
}),
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function projectedCost(projectedNextCall) {
|
|
113
|
+
const call = projectedNextCall || {};
|
|
114
|
+
if (call.pricing_unknown === true || call.model_id_unknown === true) {
|
|
115
|
+
throw new ValidationError("FAIL_MODEL_PRICING_UNKNOWN: model pricing is not pinned for this session iteration.");
|
|
116
|
+
}
|
|
117
|
+
const value = call.projected_cost_usd === undefined ? 0 : Number(call.projected_cost_usd);
|
|
118
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
119
|
+
throw new ValidationError("FAIL_MODEL_PRICING_UNKNOWN: model pricing is not pinned for this session iteration.");
|
|
120
|
+
}
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function projectedTokens(projectedNextCall) {
|
|
125
|
+
const value = projectedNextCall && projectedNextCall.projected_output_tokens !== undefined
|
|
126
|
+
? Number(projectedNextCall.projected_output_tokens)
|
|
127
|
+
: 0;
|
|
128
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
129
|
+
throw new ValidationError("FAIL_SESSION_TOKEN_CAP: projected output tokens are not evaluable.");
|
|
130
|
+
}
|
|
131
|
+
return value;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function createSleepSession(input) {
|
|
135
|
+
const options = input || {};
|
|
136
|
+
const featureKey = safeSegment(options.featureKey, "feature_key");
|
|
137
|
+
const sessionId = makeSessionId(featureKey, options.sessionId);
|
|
138
|
+
const now = typeof options.nowMs === "function" ? options.nowMs : () => Date.now();
|
|
139
|
+
const startedAtMs = Number(options.startedAtMs === undefined ? now() : options.startedAtMs);
|
|
140
|
+
const pinnedScope = options.pinnedScope || null;
|
|
141
|
+
let iteration = 0;
|
|
142
|
+
let sessionCostUsd = 0;
|
|
143
|
+
let sessionOutputTokens = 0;
|
|
144
|
+
let killed = false;
|
|
145
|
+
const priorPatchHashes = [];
|
|
146
|
+
|
|
147
|
+
function snapshot(extra) {
|
|
148
|
+
const info = extra || {};
|
|
149
|
+
return {
|
|
150
|
+
session_id: sessionId,
|
|
151
|
+
feature_key: featureKey,
|
|
152
|
+
iteration,
|
|
153
|
+
session_cost_usd: sessionCostUsd,
|
|
154
|
+
session_output_tokens: sessionOutputTokens,
|
|
155
|
+
killed,
|
|
156
|
+
started_at_ms: startedAtMs,
|
|
157
|
+
pinned_scope: pinnedScope,
|
|
158
|
+
prior_patch_hashes: priorPatchHashes.slice(),
|
|
159
|
+
carried_state: enumerateCarriedState({
|
|
160
|
+
cumulativeWorktreePath: info.cumulativeWorktreePath,
|
|
161
|
+
plan: info.plan,
|
|
162
|
+
priorPatchHashes,
|
|
163
|
+
iteration,
|
|
164
|
+
session_cost_usd: sessionCostUsd,
|
|
165
|
+
session_output_tokens: sessionOutputTokens,
|
|
166
|
+
}),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function assertCanStartIteration(projectedNextCall) {
|
|
171
|
+
if (killed) {
|
|
172
|
+
throw new ValidationError("FAIL_SESSION_KILLED: sleep execute session was killed.");
|
|
173
|
+
}
|
|
174
|
+
if (iteration >= SESSION_MAX_ITERATIONS) {
|
|
175
|
+
throw new ValidationError(`FAIL_SESSION_ITERATION_CAP: max iterations reached (${SESSION_MAX_ITERATIONS}).`);
|
|
176
|
+
}
|
|
177
|
+
if (now() - startedAtMs >= SESSION_WALLCLOCK_TIMEOUT_MS) {
|
|
178
|
+
throw new ValidationError("FAIL_SESSION_WALLCLOCK_CAP: 30-minute session wall-clock exceeded.");
|
|
179
|
+
}
|
|
180
|
+
const nextCost = projectedCost(projectedNextCall);
|
|
181
|
+
const nextTokens = projectedTokens(projectedNextCall);
|
|
182
|
+
if (sessionCostUsd + nextCost > SESSION_MAX_COST_USD) {
|
|
183
|
+
throw new ValidationError("FAIL_SESSION_COST_CAP: projected session cost exceeds $3.00.");
|
|
184
|
+
}
|
|
185
|
+
if (sessionOutputTokens + nextTokens > SESSION_MAX_OUTPUT_TOKENS) {
|
|
186
|
+
throw new ValidationError("FAIL_SESSION_TOKEN_CAP: projected session output tokens exceed 48000.");
|
|
187
|
+
}
|
|
188
|
+
return true;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function recordIteration(usage) {
|
|
192
|
+
const info = usage || {};
|
|
193
|
+
const cost = Number(info.cost_usd === undefined ? 0 : info.cost_usd);
|
|
194
|
+
const tokens = Number(info.output_tokens === undefined ? 0 : info.output_tokens);
|
|
195
|
+
if (!Number.isFinite(cost) || cost < 0 || !Number.isFinite(tokens) || tokens < 0) {
|
|
196
|
+
throw new ValidationError("unknown_risk_stop: session usage must be non-negative and evaluable.");
|
|
197
|
+
}
|
|
198
|
+
const patchHash = info.produced_patch_sha256 ? String(info.produced_patch_sha256) : null;
|
|
199
|
+
sessionCostUsd += cost;
|
|
200
|
+
sessionOutputTokens += tokens;
|
|
201
|
+
iteration += 1;
|
|
202
|
+
if (patchHash) priorPatchHashes.push(patchHash);
|
|
203
|
+
return snapshot(info);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function killSession(reason) {
|
|
207
|
+
killed = true;
|
|
208
|
+
return { killed: true, reason: reason || "killed" };
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return Object.freeze({
|
|
212
|
+
session_id: sessionId,
|
|
213
|
+
feature_key: featureKey,
|
|
214
|
+
started_at_ms: startedAtMs,
|
|
215
|
+
pinned_scope: pinnedScope,
|
|
216
|
+
assertCanStartIteration,
|
|
217
|
+
recordIteration,
|
|
218
|
+
killSession,
|
|
219
|
+
snapshot,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function assertPinnedScope(session, ctx) {
|
|
224
|
+
if (!session || !session.pinned_scope) {
|
|
225
|
+
throw new ValidationError("scope_lock: Phase E session scope is not pinned.");
|
|
226
|
+
}
|
|
227
|
+
const current = pinScope(ctx);
|
|
228
|
+
const pinned = session.pinned_scope;
|
|
229
|
+
const sameScopes = JSON.stringify(current.allowed_file_scopes) === JSON.stringify(pinned.allowed_file_scopes);
|
|
230
|
+
const sameCommands = JSON.stringify(current.verification_commands) === JSON.stringify(pinned.verification_commands);
|
|
231
|
+
const sameRole = current.role === pinned.role;
|
|
232
|
+
if (!sameScopes || !sameCommands || !sameRole) {
|
|
233
|
+
throw new ValidationError("scope_lock: pinned Phase E session scope changed during the session.");
|
|
234
|
+
}
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async function withSessionLease(sessionId, fn) {
|
|
239
|
+
const safeSessionId = safeSegment(sessionId, "session_id");
|
|
240
|
+
if (activeSessionIds.has(safeSessionId)) {
|
|
241
|
+
throw new ValidationError("FAIL_SESSION_REENTRY: sleep execute session is already active; resume/reentry is not available in Preview.");
|
|
242
|
+
}
|
|
243
|
+
activeSessionIds.add(safeSessionId);
|
|
244
|
+
try {
|
|
245
|
+
return await fn();
|
|
246
|
+
} finally {
|
|
247
|
+
activeSessionIds.delete(safeSessionId);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function sessionStopConditionFromError(error) {
|
|
252
|
+
const message = error && error.message ? String(error.message) : String(error || "unknown_risk_stop");
|
|
253
|
+
if (message.includes("FAIL_SESSION_COST_CAP")) return "FAIL_SESSION_COST_CAP";
|
|
254
|
+
if (message.includes("FAIL_SESSION_TOKEN_CAP")) return "FAIL_SESSION_TOKEN_CAP";
|
|
255
|
+
if (message.includes("FAIL_SESSION_ITERATION_CAP")) return "action_count_cap";
|
|
256
|
+
if (message.includes("FAIL_SESSION_WALLCLOCK_CAP")) return "action_count_cap";
|
|
257
|
+
if (message.includes("FAIL_MODEL_PRICING_UNKNOWN")) return "FAIL_MODEL_PRICING_UNKNOWN";
|
|
258
|
+
if (message.includes("scope_lock")) return "scope_lock";
|
|
259
|
+
if (message.includes("FAIL_SESSION_KILLED")) return "unknown_risk_stop";
|
|
260
|
+
if (message.includes("FAIL_SESSION_REENTRY")) return "unknown_risk_stop";
|
|
261
|
+
return "unknown_risk_stop";
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
module.exports = {
|
|
265
|
+
SESSION_MAX_ITERATIONS,
|
|
266
|
+
SESSION_MAX_COST_USD,
|
|
267
|
+
SESSION_MAX_OUTPUT_TOKENS,
|
|
268
|
+
SESSION_WALLCLOCK_TIMEOUT_MS,
|
|
269
|
+
SESSION_DOC_TARGET_PATTERNS,
|
|
270
|
+
SESSION_ALLOWED_COMMANDS,
|
|
271
|
+
sha256,
|
|
272
|
+
pinScope,
|
|
273
|
+
assertPinnedScope,
|
|
274
|
+
enumerateCarriedState,
|
|
275
|
+
createSleepSession,
|
|
276
|
+
withSessionLease,
|
|
277
|
+
sessionStopConditionFromError,
|
|
278
|
+
};
|
package/src/lib/args.js
CHANGED
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { ValidationError } = require("./errors");
|
|
4
|
-
|
|
5
|
-
function parseFlags(argv, defs) {
|
|
6
|
-
const flags = {};
|
|
7
|
-
const positional = [];
|
|
8
|
-
const aliasMap = {};
|
|
9
|
-
|
|
10
|
-
for (const [name, def] of Object.entries(defs)) {
|
|
11
|
-
if (def.alias) {
|
|
12
|
-
aliasMap[def.alias] = name;
|
|
13
|
-
}
|
|
14
|
-
if (def.type === "boolean") {
|
|
15
|
-
flags[name] = false;
|
|
16
|
-
} else if (def.multiple) {
|
|
17
|
-
flags[name] = [];
|
|
18
|
-
} else {
|
|
19
|
-
flags[name] = undefined;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
let i = 0;
|
|
24
|
-
while (i < argv.length) {
|
|
25
|
-
const arg = argv[i];
|
|
26
|
-
|
|
27
|
-
if (!arg.startsWith("-")) {
|
|
28
|
-
positional.push(arg);
|
|
29
|
-
i++;
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let key;
|
|
34
|
-
let inlineValue;
|
|
35
|
-
if (arg.includes("=")) {
|
|
36
|
-
const eqIdx = arg.indexOf("=");
|
|
37
|
-
key = arg.slice(0, eqIdx);
|
|
38
|
-
inlineValue = arg.slice(eqIdx + 1);
|
|
39
|
-
} else {
|
|
40
|
-
key = arg;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const stripped = key.replace(/^-{1,2}/, "");
|
|
44
|
-
const resolved = aliasMap[stripped] || stripped;
|
|
45
|
-
if (!(resolved in defs)) {
|
|
46
|
-
throw new ValidationError(`Unknown flag: ${key}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const def = defs[resolved];
|
|
50
|
-
if (def.type === "boolean") {
|
|
51
|
-
flags[resolved] = true;
|
|
52
|
-
i++;
|
|
53
|
-
continue;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
let value = inlineValue;
|
|
57
|
-
if (value === undefined) {
|
|
58
|
-
i++;
|
|
59
|
-
if (i >= argv.length) {
|
|
60
|
-
throw new ValidationError(`Flag ${key} requires a value.`);
|
|
61
|
-
}
|
|
62
|
-
value = argv[i];
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
if (def.multiple) {
|
|
66
|
-
flags[resolved].push(value);
|
|
67
|
-
} else {
|
|
68
|
-
flags[resolved] = value;
|
|
69
|
-
}
|
|
70
|
-
i++;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return { flags, positional };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function requireFlag(flags, name, label) {
|
|
77
|
-
const value = flags[name];
|
|
78
|
-
if (value === undefined || value === null || value === "") {
|
|
79
|
-
throw new ValidationError(`Missing required flag: --${label || name}`);
|
|
80
|
-
}
|
|
81
|
-
return value;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function validateChoice(value, choices, label) {
|
|
85
|
-
if (!choices.includes(value)) {
|
|
86
|
-
throw new ValidationError(
|
|
87
|
-
`Invalid value for --${label}: "${value}". Must be one of: ${choices.join(", ")}`
|
|
88
|
-
);
|
|
89
|
-
}
|
|
90
|
-
return value;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function validatePattern(value, regex, label, hint) {
|
|
94
|
-
if (!regex.test(value)) {
|
|
95
|
-
throw new ValidationError(
|
|
96
|
-
`Invalid value for --${label}: "${value}". ${hint || `Must match ${regex}`}`
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
return value;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
module.exports = {
|
|
103
|
-
parseFlags,
|
|
104
|
-
requireFlag,
|
|
105
|
-
validateChoice,
|
|
106
|
-
validatePattern,
|
|
107
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const { ValidationError } = require("./errors");
|
|
4
|
+
|
|
5
|
+
function parseFlags(argv, defs) {
|
|
6
|
+
const flags = {};
|
|
7
|
+
const positional = [];
|
|
8
|
+
const aliasMap = {};
|
|
9
|
+
|
|
10
|
+
for (const [name, def] of Object.entries(defs)) {
|
|
11
|
+
if (def.alias) {
|
|
12
|
+
aliasMap[def.alias] = name;
|
|
13
|
+
}
|
|
14
|
+
if (def.type === "boolean") {
|
|
15
|
+
flags[name] = false;
|
|
16
|
+
} else if (def.multiple) {
|
|
17
|
+
flags[name] = [];
|
|
18
|
+
} else {
|
|
19
|
+
flags[name] = undefined;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let i = 0;
|
|
24
|
+
while (i < argv.length) {
|
|
25
|
+
const arg = argv[i];
|
|
26
|
+
|
|
27
|
+
if (!arg.startsWith("-")) {
|
|
28
|
+
positional.push(arg);
|
|
29
|
+
i++;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let key;
|
|
34
|
+
let inlineValue;
|
|
35
|
+
if (arg.includes("=")) {
|
|
36
|
+
const eqIdx = arg.indexOf("=");
|
|
37
|
+
key = arg.slice(0, eqIdx);
|
|
38
|
+
inlineValue = arg.slice(eqIdx + 1);
|
|
39
|
+
} else {
|
|
40
|
+
key = arg;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const stripped = key.replace(/^-{1,2}/, "");
|
|
44
|
+
const resolved = aliasMap[stripped] || stripped;
|
|
45
|
+
if (!(resolved in defs)) {
|
|
46
|
+
throw new ValidationError(`Unknown flag: ${key}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const def = defs[resolved];
|
|
50
|
+
if (def.type === "boolean") {
|
|
51
|
+
flags[resolved] = true;
|
|
52
|
+
i++;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let value = inlineValue;
|
|
57
|
+
if (value === undefined) {
|
|
58
|
+
i++;
|
|
59
|
+
if (i >= argv.length) {
|
|
60
|
+
throw new ValidationError(`Flag ${key} requires a value.`);
|
|
61
|
+
}
|
|
62
|
+
value = argv[i];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (def.multiple) {
|
|
66
|
+
flags[resolved].push(value);
|
|
67
|
+
} else {
|
|
68
|
+
flags[resolved] = value;
|
|
69
|
+
}
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return { flags, positional };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function requireFlag(flags, name, label) {
|
|
77
|
+
const value = flags[name];
|
|
78
|
+
if (value === undefined || value === null || value === "") {
|
|
79
|
+
throw new ValidationError(`Missing required flag: --${label || name}`);
|
|
80
|
+
}
|
|
81
|
+
return value;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function validateChoice(value, choices, label) {
|
|
85
|
+
if (!choices.includes(value)) {
|
|
86
|
+
throw new ValidationError(
|
|
87
|
+
`Invalid value for --${label}: "${value}". Must be one of: ${choices.join(", ")}`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function validatePattern(value, regex, label, hint) {
|
|
94
|
+
if (!regex.test(value)) {
|
|
95
|
+
throw new ValidationError(
|
|
96
|
+
`Invalid value for --${label}: "${value}". ${hint || `Must match ${regex}`}`
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
module.exports = {
|
|
103
|
+
parseFlags,
|
|
104
|
+
requireFlag,
|
|
105
|
+
validateChoice,
|
|
106
|
+
validatePattern,
|
|
107
|
+
};
|
package/src/lib/errors.js
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
class CliError extends Error {
|
|
4
|
-
constructor(message, exitCode = 4) {
|
|
5
|
-
super(message);
|
|
6
|
-
this.name = this.constructor.name;
|
|
7
|
-
this.exitCode = exitCode;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
class ValidationError extends CliError {
|
|
12
|
-
constructor(message) {
|
|
13
|
-
super(message, 1);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class DependencyError extends CliError {
|
|
18
|
-
constructor(message) {
|
|
19
|
-
super(message, 2);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
class IntegrityError extends CliError {
|
|
24
|
-
constructor(message) {
|
|
25
|
-
super(message, 3);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
class InternalError extends CliError {
|
|
30
|
-
constructor(message) {
|
|
31
|
-
super(message, 4);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
module.exports = {
|
|
36
|
-
CliError,
|
|
37
|
-
ValidationError,
|
|
38
|
-
DependencyError,
|
|
39
|
-
IntegrityError,
|
|
40
|
-
InternalError,
|
|
41
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
class CliError extends Error {
|
|
4
|
+
constructor(message, exitCode = 4) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = this.constructor.name;
|
|
7
|
+
this.exitCode = exitCode;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class ValidationError extends CliError {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message, 1);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
class DependencyError extends CliError {
|
|
18
|
+
constructor(message) {
|
|
19
|
+
super(message, 2);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class IntegrityError extends CliError {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message, 3);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
class InternalError extends CliError {
|
|
30
|
+
constructor(message) {
|
|
31
|
+
super(message, 4);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = {
|
|
36
|
+
CliError,
|
|
37
|
+
ValidationError,
|
|
38
|
+
DependencyError,
|
|
39
|
+
IntegrityError,
|
|
40
|
+
InternalError,
|
|
41
|
+
};
|