pi-lean-host 0.1.1 → 0.2.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 +144 -43
- package/core/api-guide-types.ts +22 -1
- package/core/api-toggle.ts +20 -1
- package/core/auth.ts +149 -0
- package/core/helpers.ts +146 -50
- package/core/parse-api-guide.ts +219 -42
- package/core/secrets-command.ts +315 -0
- package/core/secrets-store.ts +170 -0
- package/core/transport.ts +126 -24
- package/package.json +4 -1
- package/tools/api-fetch.ts +110 -17
- package/tools/api-guide.ts +10 -6
- package/tools/api-probe.ts +298 -15
package/core/parse-api-guide.ts
CHANGED
|
@@ -139,13 +139,9 @@ function requireHttpUrl(
|
|
|
139
139
|
try {
|
|
140
140
|
const u = new URL(value);
|
|
141
141
|
if (u.protocol !== "http:" && u.protocol !== "https:") {
|
|
142
|
-
return fail(
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
"an http or https URL",
|
|
146
|
-
`protocol "${u.protocol}"`,
|
|
147
|
-
{ ...(opts?.protocolFix ? { fix: opts.protocolFix } : {}) },
|
|
148
|
-
);
|
|
142
|
+
return fail(file, key, "an http or https URL", `protocol "${u.protocol}"`, {
|
|
143
|
+
...(opts?.protocolFix ? { fix: opts.protocolFix } : {}),
|
|
144
|
+
});
|
|
149
145
|
}
|
|
150
146
|
} catch {
|
|
151
147
|
return fail(file, key, "a valid http/https URL", `"${value}"`, {
|
|
@@ -396,15 +392,9 @@ function validateAuth(
|
|
|
396
392
|
return { kind: "none" };
|
|
397
393
|
}
|
|
398
394
|
if (raw === null || typeof raw !== "object" || Array.isArray(raw)) {
|
|
399
|
-
return fail(
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
"a YAML mapping (kind: ...)",
|
|
403
|
-
describeFound(raw),
|
|
404
|
-
{
|
|
405
|
-
snippet: snippetFor(fm, "auth"),
|
|
406
|
-
},
|
|
407
|
-
);
|
|
395
|
+
return fail(file, "auth", "a YAML mapping (kind: ...)", describeFound(raw), {
|
|
396
|
+
snippet: snippetFor(fm, "auth"),
|
|
397
|
+
});
|
|
408
398
|
}
|
|
409
399
|
const a = raw as Record<string, unknown>;
|
|
410
400
|
const kindRaw = a["kind"];
|
|
@@ -422,9 +412,16 @@ function validateAuth(
|
|
|
422
412
|
"auth.kind",
|
|
423
413
|
"one of: none | static-key | oauth2",
|
|
424
414
|
kindRaw,
|
|
425
|
-
{ fix: "Use `kind: none` (
|
|
415
|
+
{ fix: "Use `kind: none` (public) or `kind: static-key` (keyed header)" },
|
|
426
416
|
);
|
|
427
417
|
}
|
|
418
|
+
// oauth2 is a declared seam, not realized — reject at parse so a recipe
|
|
419
|
+
// can't load an auth mode the transport can't honor.
|
|
420
|
+
if (kindRaw === "oauth2") {
|
|
421
|
+
return fail(file, "auth.kind", "one of: none | static-key", "oauth2", {
|
|
422
|
+
fix: "OAuth2 is not yet implemented. Use kind: none (public) or kind: static-key (keyed header) for now.",
|
|
423
|
+
});
|
|
424
|
+
}
|
|
428
425
|
// Parse optional headers (per-kind extra headers, e.g. X-Api-Key for DEMO_KEY).
|
|
429
426
|
let headers: Record<string, string> | undefined;
|
|
430
427
|
const headersRaw = a["headers"];
|
|
@@ -445,8 +442,176 @@ function validateAuth(
|
|
|
445
442
|
headers = headersRaw as Record<string, string>;
|
|
446
443
|
}
|
|
447
444
|
|
|
445
|
+
// static-key reference fields (secretRefs/requires/optional).
|
|
446
|
+
let secretRefs: Record<string, string> | undefined;
|
|
447
|
+
const refsRaw = a["secretRefs"];
|
|
448
|
+
if (refsRaw !== undefined) {
|
|
449
|
+
if (
|
|
450
|
+
refsRaw === null ||
|
|
451
|
+
typeof refsRaw !== "object" ||
|
|
452
|
+
Array.isArray(refsRaw) ||
|
|
453
|
+
Object.values(refsRaw).some((v) => typeof v !== "string")
|
|
454
|
+
) {
|
|
455
|
+
return fail(
|
|
456
|
+
file,
|
|
457
|
+
"auth.secretRefs",
|
|
458
|
+
"a YAML mapping of header name → secret name",
|
|
459
|
+
describeFound(refsRaw),
|
|
460
|
+
{ snippet: snippetFor(fm, "auth") },
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
secretRefs = refsRaw as Record<string, string>;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
// Query-param secrets: maps query param name → secret store name.
|
|
467
|
+
// Same shape + consistency rules as secretRefs; the collision-with-op-`params`
|
|
468
|
+
// rule is enforced in parseApiGuide (auth is parsed before operations).
|
|
469
|
+
let secretQueryRefs: Record<string, string> | undefined;
|
|
470
|
+
const sqrRaw = a["secretQueryRefs"];
|
|
471
|
+
if (sqrRaw !== undefined) {
|
|
472
|
+
if (
|
|
473
|
+
sqrRaw === null ||
|
|
474
|
+
typeof sqrRaw !== "object" ||
|
|
475
|
+
Array.isArray(sqrRaw) ||
|
|
476
|
+
Object.values(sqrRaw).some((v) => typeof v !== "string")
|
|
477
|
+
) {
|
|
478
|
+
return fail(
|
|
479
|
+
file,
|
|
480
|
+
"auth.secretQueryRefs",
|
|
481
|
+
"a YAML mapping of query param name → secret name",
|
|
482
|
+
describeFound(sqrRaw),
|
|
483
|
+
{ snippet: snippetFor(fm, "auth") },
|
|
484
|
+
);
|
|
485
|
+
}
|
|
486
|
+
secretQueryRefs = sqrRaw as Record<string, string>;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
let requires: string[] | undefined;
|
|
490
|
+
const reqRaw = a["requires"];
|
|
491
|
+
if (reqRaw !== undefined) {
|
|
492
|
+
if (
|
|
493
|
+
!Array.isArray(reqRaw) ||
|
|
494
|
+
reqRaw.length === 0 ||
|
|
495
|
+
reqRaw.some((v) => typeof v !== "string")
|
|
496
|
+
) {
|
|
497
|
+
return fail(
|
|
498
|
+
file,
|
|
499
|
+
"auth.requires",
|
|
500
|
+
"a non-empty list of secret names",
|
|
501
|
+
describeFound(reqRaw),
|
|
502
|
+
{ snippet: snippetFor(fm, "auth") },
|
|
503
|
+
);
|
|
504
|
+
}
|
|
505
|
+
requires = reqRaw as string[];
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
let optional: string[] | undefined;
|
|
509
|
+
const optRaw = a["optional"];
|
|
510
|
+
if (optRaw !== undefined) {
|
|
511
|
+
if (
|
|
512
|
+
!Array.isArray(optRaw) ||
|
|
513
|
+
optRaw.length === 0 ||
|
|
514
|
+
optRaw.some((v) => typeof v !== "string")
|
|
515
|
+
) {
|
|
516
|
+
return fail(
|
|
517
|
+
file,
|
|
518
|
+
"auth.optional",
|
|
519
|
+
"a non-empty list of secret names",
|
|
520
|
+
describeFound(optRaw),
|
|
521
|
+
{ snippet: snippetFor(fm, "auth") },
|
|
522
|
+
);
|
|
523
|
+
}
|
|
524
|
+
optional = optRaw as string[];
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// Fail-closed consistency rules.
|
|
528
|
+
if (
|
|
529
|
+
kindRaw === "none" &&
|
|
530
|
+
(secretRefs || secretQueryRefs || requires || optional)
|
|
531
|
+
) {
|
|
532
|
+
return fail(
|
|
533
|
+
file,
|
|
534
|
+
"auth.secretRefs",
|
|
535
|
+
"absent when auth.kind is none",
|
|
536
|
+
"secretRefs/secretQueryRefs/requires/optional with kind: none",
|
|
537
|
+
{
|
|
538
|
+
fix: "Use auth.kind: static-key to reference stored secrets, or remove the auth fields for a public API.",
|
|
539
|
+
},
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
if (requires || optional || secretRefs || secretQueryRefs) {
|
|
543
|
+
const declared = new Set([...(requires ?? []), ...(optional ?? [])]);
|
|
544
|
+
const refNames = new Set([
|
|
545
|
+
...Object.values(secretRefs ?? {}),
|
|
546
|
+
...Object.values(secretQueryRefs ?? {}),
|
|
547
|
+
]);
|
|
548
|
+
const checkRefs = (
|
|
549
|
+
refs: Record<string, string>,
|
|
550
|
+
field: string,
|
|
551
|
+
): ParseApiGuideResult | null => {
|
|
552
|
+
for (const outName of Object.keys(refs)) {
|
|
553
|
+
const secretName = refs[outName]!;
|
|
554
|
+
if (!declared.has(secretName)) {
|
|
555
|
+
return fail(
|
|
556
|
+
file,
|
|
557
|
+
`${field}.${outName}`,
|
|
558
|
+
"a secret name declared in auth.requires or auth.optional",
|
|
559
|
+
`"${secretName}" is not in requires/optional`,
|
|
560
|
+
{
|
|
561
|
+
fix: `Add "${secretName}" to auth.requires or auth.optional (or fix the reference).`,
|
|
562
|
+
},
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return null;
|
|
567
|
+
};
|
|
568
|
+
const r1 = checkRefs(secretRefs ?? {}, "auth.secretRefs");
|
|
569
|
+
if (r1) return r1;
|
|
570
|
+
const r2 = checkRefs(secretQueryRefs ?? {}, "auth.secretQueryRefs");
|
|
571
|
+
if (r2) return r2;
|
|
572
|
+
const both = (requires ?? []).filter((n) => (optional ?? []).includes(n));
|
|
573
|
+
if (both.length > 0) {
|
|
574
|
+
return fail(
|
|
575
|
+
file,
|
|
576
|
+
"auth.requires",
|
|
577
|
+
"secret names not duplicated across requires and optional",
|
|
578
|
+
`in both: ${both.join(", ")}`,
|
|
579
|
+
{
|
|
580
|
+
fix: `Move "${both[0]}" to either requires or optional, not both.`,
|
|
581
|
+
},
|
|
582
|
+
);
|
|
583
|
+
}
|
|
584
|
+
const checkDeclared = (
|
|
585
|
+
names: string[],
|
|
586
|
+
field: string,
|
|
587
|
+
): ParseApiGuideResult | null => {
|
|
588
|
+
for (const name of names) {
|
|
589
|
+
if (!refNames.has(name)) {
|
|
590
|
+
return fail(
|
|
591
|
+
file,
|
|
592
|
+
field,
|
|
593
|
+
"a secret name referenced by auth.secretRefs or auth.secretQueryRefs",
|
|
594
|
+
`"${name}" is declared here but not referenced by any header/query ref`,
|
|
595
|
+
{
|
|
596
|
+
fix: `Reference "${name}" from auth.secretRefs or auth.secretQueryRefs, or remove it from ${field}.`,
|
|
597
|
+
},
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
return null;
|
|
602
|
+
};
|
|
603
|
+
const r3 = checkDeclared(requires ?? [], "auth.requires");
|
|
604
|
+
if (r3) return r3;
|
|
605
|
+
const r4 = checkDeclared(optional ?? [], "auth.optional");
|
|
606
|
+
if (r4) return r4;
|
|
607
|
+
}
|
|
608
|
+
|
|
448
609
|
const result: AuthConfig = { kind: kindRaw as AuthKind };
|
|
449
610
|
if (headers !== undefined) result.headers = headers;
|
|
611
|
+
if (secretRefs !== undefined) result.secretRefs = secretRefs;
|
|
612
|
+
if (secretQueryRefs !== undefined) result.secretQueryRefs = secretQueryRefs;
|
|
613
|
+
if (requires !== undefined) result.requires = requires;
|
|
614
|
+
if (optional !== undefined) result.optional = optional;
|
|
450
615
|
return result;
|
|
451
616
|
}
|
|
452
617
|
|
|
@@ -570,8 +735,7 @@ function validateOperation(
|
|
|
570
735
|
);
|
|
571
736
|
}
|
|
572
737
|
const s = spec as Record<string, unknown>;
|
|
573
|
-
const onlyDescription =
|
|
574
|
-
Object.keys(s).length === 1 && "description" in s;
|
|
738
|
+
const onlyDescription = Object.keys(s).length === 1 && "description" in s;
|
|
575
739
|
if (!onlyDescription) {
|
|
576
740
|
return fail(
|
|
577
741
|
file,
|
|
@@ -723,11 +887,7 @@ function validateOperation(
|
|
|
723
887
|
const opPaginationRaw = o["pagination"];
|
|
724
888
|
let opPagination: PaginationConfig | undefined;
|
|
725
889
|
if (opPaginationRaw !== undefined) {
|
|
726
|
-
const pr = validatePagination(
|
|
727
|
-
opPaginationRaw,
|
|
728
|
-
fieldPath("pagination"),
|
|
729
|
-
file,
|
|
730
|
-
);
|
|
890
|
+
const pr = validatePagination(opPaginationRaw, fieldPath("pagination"), file);
|
|
731
891
|
if (!("style" in pr && typeof pr.style === "string")) {
|
|
732
892
|
return pr as ParseApiGuideResult;
|
|
733
893
|
}
|
|
@@ -775,13 +935,13 @@ function validateOperation(
|
|
|
775
935
|
params,
|
|
776
936
|
pathParams,
|
|
777
937
|
...(Object.keys(pathParamDocs).length > 0 ? { pathParamDocs } : {}),
|
|
778
|
-
...(helper
|
|
779
|
-
...(transform
|
|
780
|
-
...(passthrough
|
|
781
|
-
...(dateParams
|
|
938
|
+
...(helper === undefined ? {} : { helper }),
|
|
939
|
+
...(transform === undefined ? {} : { transform }),
|
|
940
|
+
...(passthrough === undefined ? {} : { passthrough }),
|
|
941
|
+
...(dateParams === undefined ? {} : { dateParams }),
|
|
782
942
|
...(parseOverride ? { parse: parseOverride } : {}),
|
|
783
943
|
...(opPagination ? { pagination: opPagination } : {}),
|
|
784
|
-
...(opGatherAllMax
|
|
944
|
+
...(opGatherAllMax === undefined ? {} : { gatherAllMax: opGatherAllMax }),
|
|
785
945
|
};
|
|
786
946
|
return operation;
|
|
787
947
|
}
|
|
@@ -1008,6 +1168,30 @@ export function parseApiGuide(
|
|
|
1008
1168
|
operations.push(opRes);
|
|
1009
1169
|
}
|
|
1010
1170
|
|
|
1171
|
+
// Cross-field: a secretQueryRefs param name must not appear in any
|
|
1172
|
+
// operation's `params` map — the agent must never be able to supply a
|
|
1173
|
+
// secretly-injected param. Checked here because auth is parsed before
|
|
1174
|
+
// operations. `passthrough` ops are NOT rejected (the runtime skips
|
|
1175
|
+
// secretQueryRefs keys in buildQueryParams' passthrough branch); only a
|
|
1176
|
+
// declared-op colliding with the injected param is a parse error.
|
|
1177
|
+
if (auth.kind === "static-key" && auth.secretQueryRefs) {
|
|
1178
|
+
for (const paramName of Object.keys(auth.secretQueryRefs)) {
|
|
1179
|
+
for (const dOp of operations) {
|
|
1180
|
+
if (paramName in dOp.params) {
|
|
1181
|
+
return fail(
|
|
1182
|
+
file,
|
|
1183
|
+
`auth.secretQueryRefs.${paramName}`,
|
|
1184
|
+
"a query param name not declared in any operation's params",
|
|
1185
|
+
`also a param of operation "${dOp.name}"`,
|
|
1186
|
+
{
|
|
1187
|
+
fix: `Remove "${paramName}" from operation "${dOp.name}"'s params map — it is code-injected from the secrets store and the agent must not be able to set it.`,
|
|
1188
|
+
},
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1011
1195
|
const guide: ApiGuide = {
|
|
1012
1196
|
content,
|
|
1013
1197
|
updated,
|
|
@@ -1168,11 +1352,11 @@ export function formatApiGuideCatalog(loaded: LoadedApiGuides): string {
|
|
|
1168
1352
|
for (const [name, guide] of Object.entries(loaded.guides)) {
|
|
1169
1353
|
if (guide.organization) {
|
|
1170
1354
|
const idx = orgIndex.get(guide.organization);
|
|
1171
|
-
if (idx
|
|
1172
|
-
orgRows[idx]!.guides.push(guide);
|
|
1173
|
-
} else {
|
|
1355
|
+
if (idx === undefined) {
|
|
1174
1356
|
orgIndex.set(guide.organization, orgRows.length);
|
|
1175
1357
|
orgRows.push({ org: guide.organization, guides: [guide] });
|
|
1358
|
+
} else {
|
|
1359
|
+
orgRows[idx]!.guides.push(guide);
|
|
1176
1360
|
}
|
|
1177
1361
|
} else {
|
|
1178
1362
|
orgless.push({ name, guide });
|
|
@@ -1190,9 +1374,7 @@ export function formatApiGuideCatalog(loaded: LoadedApiGuides): string {
|
|
|
1190
1374
|
}
|
|
1191
1375
|
for (const { name, guide } of orgless) {
|
|
1192
1376
|
const domains =
|
|
1193
|
-
guide.domains && guide.domains.length > 0
|
|
1194
|
-
? guide.domains.join(", ")
|
|
1195
|
-
: name;
|
|
1377
|
+
guide.domains && guide.domains.length > 0 ? guide.domains.join(", ") : name;
|
|
1196
1378
|
lines.push(
|
|
1197
1379
|
` ${guide.icon} ${guide.shortName} — ${domains} (verified ${guide.verified}, ${guide.operations.length} ops)`,
|
|
1198
1380
|
);
|
|
@@ -1202,13 +1384,8 @@ export function formatApiGuideCatalog(loaded: LoadedApiGuides): string {
|
|
|
1202
1384
|
` ⚠ malformed — ${mal.filename}: ${mal.error.field} — expected ${mal.error.expected}; found ${mal.error.found}`,
|
|
1203
1385
|
);
|
|
1204
1386
|
}
|
|
1205
|
-
if (
|
|
1206
|
-
|
|
1207
|
-
loaded.malformed.length === 0
|
|
1208
|
-
) {
|
|
1209
|
-
lines.push(
|
|
1210
|
-
" (no guides — call api-learn({domain, recipe}) to author one)",
|
|
1211
|
-
);
|
|
1387
|
+
if (Object.keys(loaded.guides).length === 0 && loaded.malformed.length === 0) {
|
|
1388
|
+
lines.push(" (no guides — call api-learn({domain, recipe}) to author one)");
|
|
1212
1389
|
}
|
|
1213
1390
|
lines.push("");
|
|
1214
1391
|
lines.push(
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `/api secrets [domain [name]]` — list and provision stored API secrets.
|
|
3
|
+
*
|
|
4
|
+
* - `secrets [--help]` — list stored domains + names (names only), or show help.
|
|
5
|
+
* - `secrets <domain>` — detail view + assisted entry for a domain.
|
|
6
|
+
* - `secrets <domain> <name>` — manual entry (escape valve for the chicken-and-egg case).
|
|
7
|
+
* - `secrets <domain> --delete` — delete all secrets for a domain (interactive confirm).
|
|
8
|
+
* - `secrets <domain> <name> --delete` — delete a single secret (no confirm).
|
|
9
|
+
*
|
|
10
|
+
* All values are entered via `ctx.ui.input()` and land only in the store; the
|
|
11
|
+
* value never touches a tool result, `pi.sendMessage`, or the session file.
|
|
12
|
+
* Returns metadata-only status lines (names, never values).
|
|
13
|
+
*
|
|
14
|
+
* Headless (`ctx.hasUI` false): print direct-file-write instructions; never
|
|
15
|
+
* prompt or hang. Full usage/file-format lives behind `--help`; the bare list
|
|
16
|
+
* shows a one-line hint instead of the whole block.
|
|
17
|
+
*
|
|
18
|
+
* Focus-mode guard is **not** applied — this is a peer of `status`/`helpers`/
|
|
19
|
+
* bare `/api`, not an actuation.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
23
|
+
import {
|
|
24
|
+
deleteDomain,
|
|
25
|
+
deleteSecret,
|
|
26
|
+
getSecretsDir,
|
|
27
|
+
listDomains,
|
|
28
|
+
listNames,
|
|
29
|
+
readSecret,
|
|
30
|
+
writeSecret,
|
|
31
|
+
} from "./secrets-store.js";
|
|
32
|
+
import { findGuidesByDomain } from "./guide-store.js";
|
|
33
|
+
|
|
34
|
+
/** One-line pointer to `--help`, used in place of the full instructions block. */
|
|
35
|
+
const HELP_HINT = "Run /api secrets --help for usage & storage details.";
|
|
36
|
+
|
|
37
|
+
/** Direct-file-write instructions for headless mode / manual provisioning. */
|
|
38
|
+
function fileWriteInstructions(): string {
|
|
39
|
+
return (
|
|
40
|
+
"Secrets are stored per-domain as JSON (0600), one file per domain:\n" +
|
|
41
|
+
` ${getSecretsDir()}/<domain>.json\n\n` +
|
|
42
|
+
"Each file maps secret name → value. To provision manually, write:\n" +
|
|
43
|
+
' { "api_key": "<value>" }\n' +
|
|
44
|
+
"into `<domain>.json`, then run /api secrets <domain> to verify."
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Full usage + storage docs, surfaced by `--help`. */
|
|
49
|
+
function helpText(): string {
|
|
50
|
+
return [
|
|
51
|
+
"Usage: /api secrets [domain [name]] [--delete]",
|
|
52
|
+
" /api secrets list stored secrets (names only)",
|
|
53
|
+
" /api secrets <domain> view + provision secrets for a domain",
|
|
54
|
+
" /api secrets <domain> <name> set a single secret by name",
|
|
55
|
+
" /api secrets <domain> --delete delete all secrets for a domain (confirm)",
|
|
56
|
+
" /api secrets <domain> <name> --delete delete a single secret",
|
|
57
|
+
" /api secrets --help this help",
|
|
58
|
+
"",
|
|
59
|
+
fileWriteInstructions(),
|
|
60
|
+
].join("\n");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Handle the `secrets` subcommand of `/api`.
|
|
65
|
+
*
|
|
66
|
+
* @param args The text after "secrets" ("" / "<domain>" / "<domain> <name>")
|
|
67
|
+
* @param ctx The extension command context
|
|
68
|
+
*/
|
|
69
|
+
export async function handleSecretsSubcommand(
|
|
70
|
+
args: string,
|
|
71
|
+
ctx: ExtensionCommandContext,
|
|
72
|
+
): Promise<void> {
|
|
73
|
+
const parts = args.trim().split(/\s+/).filter(Boolean);
|
|
74
|
+
|
|
75
|
+
// `--help` (or `help`) short-circuits before domain dispatch — it must
|
|
76
|
+
// never be mistaken for a domain name.
|
|
77
|
+
if (parts.includes("--help") || parts.includes("help")) {
|
|
78
|
+
ctx.ui.notify(helpText(), "info");
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Extract the `--delete` flag; it isn't a domain or secret name.
|
|
83
|
+
const deleteFlag = parts.includes("--delete");
|
|
84
|
+
const tokens = parts.filter((p) => p !== "--delete");
|
|
85
|
+
const domain = tokens[0];
|
|
86
|
+
const name = tokens[1];
|
|
87
|
+
|
|
88
|
+
if (deleteFlag && !domain) {
|
|
89
|
+
ctx.ui.notify(
|
|
90
|
+
"Usage: /api secrets <domain> [name] --delete — see /api secrets --help.",
|
|
91
|
+
"warning",
|
|
92
|
+
);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (domain && name) {
|
|
96
|
+
if (deleteFlag) {
|
|
97
|
+
await deleteOne(ctx, domain, name);
|
|
98
|
+
} else {
|
|
99
|
+
await provisionOne(ctx, domain, name);
|
|
100
|
+
}
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (domain) {
|
|
104
|
+
if (deleteFlag) {
|
|
105
|
+
await deleteAllSecrets(ctx, domain);
|
|
106
|
+
} else {
|
|
107
|
+
await assistedEntry(ctx, domain);
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
listAll(ctx);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ── No-arg list ────────────────────────────────────────────────────
|
|
115
|
+
|
|
116
|
+
function listAll(ctx: ExtensionCommandContext): void {
|
|
117
|
+
const domains = listDomains();
|
|
118
|
+
if (domains.length === 0) {
|
|
119
|
+
ctx.ui.notify("(no secrets stored)\n\n" + HELP_HINT, "info");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const lines = domains.map((d) => {
|
|
124
|
+
const names = listNames(d);
|
|
125
|
+
const listing = names.length ? names.join(", ") : "(no names)";
|
|
126
|
+
return ` · ${d}: ${listing}`;
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
ctx.ui.notify(
|
|
130
|
+
["Stored secrets (names only):", ...lines, "", HELP_HINT].join("\n"),
|
|
131
|
+
"info",
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Declared secret store-names for a domain, from every registered guide's
|
|
137
|
+
* `auth.secretRefs` and `auth.secretQueryRefs` (values). Empty for guides
|
|
138
|
+
* without static-key auth.
|
|
139
|
+
*/
|
|
140
|
+
function declaredSecretNames(domain: string): string[] {
|
|
141
|
+
const names = new Set<string>();
|
|
142
|
+
for (const { guide } of findGuidesByDomain(domain)) {
|
|
143
|
+
if (guide.auth.kind !== "static-key") continue;
|
|
144
|
+
for (const n of Object.values(guide.auth.secretRefs ?? {})) names.add(n);
|
|
145
|
+
for (const n of Object.values(guide.auth.secretQueryRefs ?? {})) names.add(n);
|
|
146
|
+
}
|
|
147
|
+
return [...names].sort((a, b) => a.localeCompare(b));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ── Assisted entry: /api secrets <domain> ──────────────────────────
|
|
151
|
+
|
|
152
|
+
async function assistedEntry(
|
|
153
|
+
ctx: ExtensionCommandContext,
|
|
154
|
+
domain: string,
|
|
155
|
+
): Promise<void> {
|
|
156
|
+
const stored = listNames(domain);
|
|
157
|
+
const storedLine = stored.length ? stored.join(", ") : "(none)";
|
|
158
|
+
const declared = declaredSecretNames(domain);
|
|
159
|
+
|
|
160
|
+
if (!ctx.hasUI) {
|
|
161
|
+
ctx.ui.notify(
|
|
162
|
+
`Stored secrets for '${domain}': ${storedLine}\n\n` +
|
|
163
|
+
fileWriteInstructions(),
|
|
164
|
+
"info",
|
|
165
|
+
);
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const detailLines = [`🔐 Secrets for '${domain}'`, ` Stored: ${storedLine}`];
|
|
170
|
+
if (declared.length > 0) {
|
|
171
|
+
detailLines.push(` Declared (guide): ${declared.join(", ")}`);
|
|
172
|
+
const missing = declared.filter((n) => !stored.includes(n));
|
|
173
|
+
if (missing.length) detailLines.push(` Missing: ${missing.join(", ")}`);
|
|
174
|
+
}
|
|
175
|
+
ctx.ui.notify(detailLines.join("\n"), "info");
|
|
176
|
+
|
|
177
|
+
// Guide-aware assisted entry: exactly one declared secret name → prompt
|
|
178
|
+
// its value directly; multiple → a picker. No declared names (public API
|
|
179
|
+
// or no registered guide) → fall back to a name + value prompt.
|
|
180
|
+
if (declared.length === 1) {
|
|
181
|
+
await promptAndStore(ctx, domain, declared[0]!);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
if (declared.length > 1) {
|
|
185
|
+
const picked = await ctx.ui.select(
|
|
186
|
+
`Secret to provision for '${domain}'`,
|
|
187
|
+
declared,
|
|
188
|
+
);
|
|
189
|
+
if (picked === undefined) {
|
|
190
|
+
ctx.ui.notify("Cancelled — nothing stored.", "info");
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
await promptAndStore(ctx, domain, picked);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const newName = await ctx.ui.input(
|
|
198
|
+
`Secret name for '${domain}'`,
|
|
199
|
+
"e.g. api_key",
|
|
200
|
+
);
|
|
201
|
+
if (newName === undefined) return; // cancelled
|
|
202
|
+
const trimmedName = newName.trim();
|
|
203
|
+
if (!trimmedName) {
|
|
204
|
+
ctx.ui.notify("Aborted — empty secret name.", "warning");
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
await promptAndStore(ctx, domain, trimmedName);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Prompt for a secret's value and store it. Value never leaves the store. */
|
|
211
|
+
async function promptAndStore(
|
|
212
|
+
ctx: ExtensionCommandContext,
|
|
213
|
+
domain: string,
|
|
214
|
+
name: string,
|
|
215
|
+
): Promise<void> {
|
|
216
|
+
const value = await ctx.ui.input(
|
|
217
|
+
`Value for '${domain}'.${name}`,
|
|
218
|
+
"paste the secret value",
|
|
219
|
+
);
|
|
220
|
+
if (value === undefined) return; // cancelled
|
|
221
|
+
const trimmed = value.trim();
|
|
222
|
+
if (!trimmed) {
|
|
223
|
+
ctx.ui.notify("Aborted — empty value.", "warning");
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
writeSecret(domain, name, trimmed);
|
|
227
|
+
ctx.ui.notify(`Stored secret '${name}' for '${domain}'.`, "info");
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// ── Delete: /api secrets <domain> [name] --delete ──────────────────
|
|
231
|
+
|
|
232
|
+
// Delete a single secret. Explicit by name — no confirmation, headless or not
|
|
233
|
+
// (deleting needs no value from the user, so headless never hangs).
|
|
234
|
+
async function deleteOne(
|
|
235
|
+
ctx: ExtensionCommandContext,
|
|
236
|
+
domain: string,
|
|
237
|
+
name: string,
|
|
238
|
+
): Promise<void> {
|
|
239
|
+
if (readSecret(domain, name) === null) {
|
|
240
|
+
ctx.ui.notify(`No secret '${name}' stored for '${domain}'.`, "warning");
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
deleteSecret(domain, name);
|
|
244
|
+
ctx.ui.notify(`Deleted secret '${name}' for '${domain}'.`, "info");
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Delete all secrets for a domain. The bigger hammer (wipes several keys at
|
|
248
|
+
// once) — confirm once in interactive mode. Headless skips the confirm and
|
|
249
|
+
// just does it; no prompt, no hang.
|
|
250
|
+
async function deleteAllSecrets(
|
|
251
|
+
ctx: ExtensionCommandContext,
|
|
252
|
+
domain: string,
|
|
253
|
+
): Promise<void> {
|
|
254
|
+
const names = listNames(domain);
|
|
255
|
+
if (names.length === 0) {
|
|
256
|
+
ctx.ui.notify(`No secrets stored for '${domain}'.`, "warning");
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (ctx.hasUI) {
|
|
260
|
+
const ok = await ctx.ui.confirm(
|
|
261
|
+
`Delete all secrets for '${domain}'?`,
|
|
262
|
+
`This removes: ${names.join(", ")}.`,
|
|
263
|
+
);
|
|
264
|
+
if (!ok) {
|
|
265
|
+
ctx.ui.notify("Cancelled — nothing deleted.", "info");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
deleteDomain(domain);
|
|
270
|
+
ctx.ui.notify(`Deleted all secrets for '${domain}'.`, "info");
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ── Manual entry: /api secrets <domain> <name> ─────────────────────
|
|
274
|
+
|
|
275
|
+
async function provisionOne(
|
|
276
|
+
ctx: ExtensionCommandContext,
|
|
277
|
+
domain: string,
|
|
278
|
+
name: string,
|
|
279
|
+
): Promise<void> {
|
|
280
|
+
if (!ctx.hasUI) {
|
|
281
|
+
ctx.ui.notify(
|
|
282
|
+
`No interactive UI — write the value for '${name}' in ` +
|
|
283
|
+
`${getSecretsDir()}/${domain}.json manually.\n\n` +
|
|
284
|
+
fileWriteInstructions(),
|
|
285
|
+
"info",
|
|
286
|
+
);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Escape-valve validation: when a guide declares secret names, a manual
|
|
291
|
+
// name outside them is likely a typo — warn but still store (this is the
|
|
292
|
+
// chicken-and-egg escape hatch).
|
|
293
|
+
const declared = declaredSecretNames(domain);
|
|
294
|
+
if (declared.length > 0 && !declared.includes(name)) {
|
|
295
|
+
ctx.ui.notify(
|
|
296
|
+
`⚠ '${name}' is not a declared secret for '${domain}' ` +
|
|
297
|
+
`(guide declares: ${declared.join(", ")}). Storing anyway.`,
|
|
298
|
+
"warning",
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const value = await ctx.ui.input(
|
|
303
|
+
`Value for '${domain}'.${name}`,
|
|
304
|
+
"paste the secret value",
|
|
305
|
+
);
|
|
306
|
+
if (value === undefined) return; // cancelled
|
|
307
|
+
const trimmed = value.trim();
|
|
308
|
+
if (!trimmed) {
|
|
309
|
+
ctx.ui.notify("Aborted — empty value.", "warning");
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
writeSecret(domain, name, trimmed);
|
|
314
|
+
ctx.ui.notify(`Stored secret '${name}' for '${domain}'.`, "info");
|
|
315
|
+
}
|