pi-lean-host 0.4.0 → 0.6.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 +194 -557
- package/core/api-guide-types.ts +250 -47
- package/core/api-toggle.ts +244 -39
- package/core/auth.ts +997 -76
- package/core/delete-command.ts +1 -1
- package/core/fs-0600.ts +39 -0
- package/core/guide-catalog.ts +349 -0
- package/core/guide-picker.ts +76 -61
- package/core/guide-store.ts +2 -2
- package/core/helpers.ts +532 -151
- package/core/local-helpers.ts +7 -0
- package/core/oauth-command.ts +913 -0
- package/core/oauth-flow.ts +392 -0
- package/core/oauth-store.ts +306 -0
- package/core/parse-api-guide.ts +1125 -596
- package/core/path-template.ts +101 -0
- package/core/portal-projection.ts +3 -4
- package/core/resolve-op.ts +113 -67
- package/core/response-spill.ts +10 -7
- package/core/secrets-command.ts +18 -13
- package/core/secrets-store.ts +11 -44
- package/core/select-picker.ts +171 -0
- package/core/staging.ts +29 -0
- package/core/transport.ts +322 -35
- package/core/verify-command.ts +93 -65
- package/docs/migration-v1.md +56 -0
- package/index.ts +12 -10
- package/package.json +16 -6
- package/tools/api-fetch.ts +45 -18
- package/tools/api-guide.ts +37 -67
- package/tools/api-learn.ts +148 -103
- package/tools/api-probe.ts +458 -294
- package/tools/api-scaffold.ts +34 -63
- package/tools/api-store.ts +381 -0
- package/tools/oauth-mint.ts +338 -0
- package/tools/utils.ts +70 -1
- package/core/verify-ship-manifest.ts +0 -118
- package/tools/index.ts +0 -8
package/tools/api-scaffold.ts
CHANGED
|
@@ -9,10 +9,10 @@
|
|
|
9
9
|
* - `{domain, helper: true}` → commented-out helper stub (default +
|
|
10
10
|
* `transform` exports) with doc comments. Self-documenting.
|
|
11
11
|
* - `{domain, verify: true}` → `verify.json` with `"__FILL_ME__"` sentinels
|
|
12
|
-
* for every unsatisfiable param (path `{token}
|
|
13
|
-
* query, each `requiresAnyOf`
|
|
14
|
-
*
|
|
15
|
-
* unsatisfiable params.
|
|
12
|
+
* for every unsatisfiable param (a path `{token}` NOT store-filled via
|
|
13
|
+
* `secretPathRefs`, required-no-default query, each `requiresAnyOf`
|
|
14
|
+
* member). Existing guides-dir `verify.json` real values are
|
|
15
|
+
* additive-merged; new sentinels added for newly-unsatisfiable params.
|
|
16
16
|
* - `{domain, verify: true, helper: true}` → both files, same staged dir.
|
|
17
17
|
*
|
|
18
18
|
* Refuses to overwrite an existing staged sibling (delete from /tmp +
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
28
28
|
import { Type } from "@earendil-works/pi-ai";
|
|
29
29
|
import { Text } from "@earendil-works/pi-tui";
|
|
30
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
appendFooter,
|
|
32
|
+
contentText,
|
|
33
|
+
disambiguationMenuResult,
|
|
34
|
+
shortNameErrorResult,
|
|
35
|
+
} from "./utils.js";
|
|
31
36
|
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
32
|
-
import { tmpdir } from "node:os";
|
|
33
37
|
import { join } from "node:path";
|
|
34
|
-
import {
|
|
35
|
-
formatGuideListings,
|
|
36
|
-
selectGuideByShortName,
|
|
37
|
-
shortNameErrorText,
|
|
38
|
-
} from "../core/parse-api-guide.js";
|
|
38
|
+
import { selectGuideByShortName } from "../core/guide-catalog.js";
|
|
39
39
|
import type { ApiGuide } from "../core/api-guide-types.js";
|
|
40
40
|
import { findGuidesByDomain, getUserGuidesDir } from "../core/guide-store.js";
|
|
41
41
|
import { assertSafeDomain, slug } from "../core/path-template.js";
|
|
@@ -44,18 +44,10 @@ import {
|
|
|
44
44
|
renderForSentinels,
|
|
45
45
|
loadVerifyJson,
|
|
46
46
|
} from "../core/verify-command.js";
|
|
47
|
+
import { secretPathTokenNames } from "../core/auth.js";
|
|
48
|
+
import { stagingDirFor } from "../core/staging.js";
|
|
47
49
|
|
|
48
|
-
//
|
|
49
|
-
// Staged working copy (/tmp) — same root api-learn writes to
|
|
50
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
51
|
-
|
|
52
|
-
let _stagingRoot = join(tmpdir(), "pi-lean-host");
|
|
53
|
-
|
|
54
|
-
/** Test override — keeps scaffolds out of the real /tmp root (mirrors
|
|
55
|
-
* api-learn's `setStagingRoot`). */
|
|
56
|
-
export function setStagingRoot(dir: string): void {
|
|
57
|
-
_stagingRoot = dir;
|
|
58
|
-
}
|
|
50
|
+
// Staged working copy (/tmp) — same root api-learn writes to (core/staging.js).
|
|
59
51
|
|
|
60
52
|
// ═══════════════════════════════════════════════════════════════════
|
|
61
53
|
// Authoring manual + templates
|
|
@@ -117,8 +109,10 @@ function mergeVerifySentinels(
|
|
|
117
109
|
for (const [opName, params] of Object.entries(existing)) {
|
|
118
110
|
merged[opName] = { ...params };
|
|
119
111
|
}
|
|
112
|
+
// Path tokens owned by secretPathRefs are store-filled — no sentinel.
|
|
113
|
+
const secretPathTokens = secretPathTokenNames(guide.auth);
|
|
120
114
|
for (const op of guide.operations) {
|
|
121
|
-
const sentinels = renderForSentinels(unsatisfiable(op, {}));
|
|
115
|
+
const sentinels = renderForSentinels(unsatisfiable(op, {}, secretPathTokens));
|
|
122
116
|
if (sentinels.length === 0) continue; // runnable op → no entry
|
|
123
117
|
const entry = merged[op.name] ?? {};
|
|
124
118
|
for (const p of sentinels) {
|
|
@@ -155,7 +149,7 @@ export const apiScaffoldTool = defineTool({
|
|
|
155
149
|
verify: Type.Optional(
|
|
156
150
|
Type.Boolean({
|
|
157
151
|
description:
|
|
158
|
-
'true → scaffold a starter verify.json with "__FILL_ME__" sentinels for every unsatisfiable param.',
|
|
152
|
+
'true → scaffold a starter verify.json with "__FILL_ME__" sentinels for every unsatisfiable param (path tokens store-filled via secretPathRefs are excluded).',
|
|
159
153
|
}),
|
|
160
154
|
),
|
|
161
155
|
helper: Type.Optional(
|
|
@@ -187,7 +181,7 @@ export const apiScaffoldTool = defineTool({
|
|
|
187
181
|
type: "text",
|
|
188
182
|
text:
|
|
189
183
|
`⚠ Nothing to scaffold — at least one of verify: true or helper: true is required.\n` +
|
|
190
|
-
` verify: true → starter verify.json (sentinels for unsatisfiable params)\n` +
|
|
184
|
+
` verify: true → starter verify.json (sentinels for unsatisfiable params — store-filled path tokens excluded)\n` +
|
|
191
185
|
` helper: true → commented-out helper.ts stub`,
|
|
192
186
|
},
|
|
193
187
|
],
|
|
@@ -217,7 +211,7 @@ export const apiScaffoldTool = defineTool({
|
|
|
217
211
|
// domain (api-learn {domain, new:true} stages by domain), the user is
|
|
218
212
|
// mid-authoring — tell them to save it first. Otherwise keep the
|
|
219
213
|
// browse + author-one-first guidance.
|
|
220
|
-
const stagedDraftDir =
|
|
214
|
+
const stagedDraftDir = stagingDirFor(domain);
|
|
221
215
|
const hasStagedDraft = existsSync(join(stagedDraftDir, "guide.md"));
|
|
222
216
|
return {
|
|
223
217
|
content: [
|
|
@@ -242,49 +236,26 @@ export const apiScaffoldTool = defineTool({
|
|
|
242
236
|
} else if (guideSelector) {
|
|
243
237
|
const sel = selectGuideByShortName(matches, guideSelector);
|
|
244
238
|
if (!sel.ok) {
|
|
245
|
-
return
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
domain,
|
|
252
|
-
guideSelector,
|
|
253
|
-
`Call api-scaffold({domain: "${domain}", verify: true}) to see the menu.`,
|
|
254
|
-
),
|
|
255
|
-
},
|
|
256
|
-
],
|
|
257
|
-
details:
|
|
258
|
-
sel.reason === "no_match"
|
|
259
|
-
? { error: "no_guide_by_shortname", domain, guide: guideSelector }
|
|
260
|
-
: {
|
|
261
|
-
error: "ambiguous_shortname",
|
|
262
|
-
domain,
|
|
263
|
-
guide: guideSelector,
|
|
264
|
-
directories: sel.directories,
|
|
265
|
-
},
|
|
266
|
-
};
|
|
239
|
+
return shortNameErrorResult(
|
|
240
|
+
sel,
|
|
241
|
+
domain,
|
|
242
|
+
guideSelector,
|
|
243
|
+
`Call api-scaffold({domain: "${domain}", verify: true}) to see the menu.`,
|
|
244
|
+
);
|
|
267
245
|
}
|
|
268
246
|
selected = sel;
|
|
269
247
|
} else {
|
|
270
|
-
// N guides, no selector → disambiguation menu
|
|
271
|
-
return
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
formatGuideListings(matches),
|
|
278
|
-
`Call api-scaffold({domain: "${domain}", guide: "${matches[0]!.guide.shortName}", verify: true}) to scaffold one.`,
|
|
279
|
-
].join("\n"),
|
|
280
|
-
},
|
|
281
|
-
],
|
|
282
|
-
details: { mode: "menu", domain, disambiguation: matches.length },
|
|
283
|
-
};
|
|
248
|
+
// N guides, no selector → disambiguation menu.
|
|
249
|
+
return disambiguationMenuResult(
|
|
250
|
+
domain,
|
|
251
|
+
matches,
|
|
252
|
+
(shortName) =>
|
|
253
|
+
`Call api-scaffold({domain: "${domain}", guide: "${shortName}", verify: true}) to scaffold one.`,
|
|
254
|
+
);
|
|
284
255
|
}
|
|
285
256
|
|
|
286
257
|
const { guide, dirName } = selected;
|
|
287
|
-
const stagedDir =
|
|
258
|
+
const stagedDir = stagingDirFor(slug(guide.shortName));
|
|
288
259
|
|
|
289
260
|
// ── Refuse-to-overwrite (before any write) ────────────
|
|
290
261
|
if (helper && existsSync(join(stagedDir, "helper.ts"))) {
|
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* api-store tool definition.
|
|
3
|
+
*
|
|
4
|
+
* Read-only, learn-gated inspection of BOTH credential stores in one call —
|
|
5
|
+
* the agent-facing view of `/api secrets` + `/api oauth --status`. The
|
|
6
|
+
* authoring question is never "show me tokens" in isolation; it is what
|
|
7
|
+
* credentials exist for a domain, what's declared vs provisioned vs minted,
|
|
8
|
+
* what's expired, and what needs minting next.
|
|
9
|
+
*
|
|
10
|
+
* - Bare call (no domain, no apiHost) → orphan view: unscoped secret
|
|
11
|
+
* domains + token domains with no guide (authoring-bootstrap view).
|
|
12
|
+
* - With domain (or apiHost, resolved via the probe's store-domain seam —
|
|
13
|
+
* `resolveProvisionedParentDomain` + `hostnameOf` from core/auth) →
|
|
14
|
+
* combined per-domain report: provisioned/declared/gap secret names,
|
|
15
|
+
* token slots (issuer, granted scope, expiry, refreshable) and
|
|
16
|
+
* declared-slot gaps ("guide declares X: no token minted" — the pointer
|
|
17
|
+
* to `oauth-mint` that replaces a trial-and-error 401).
|
|
18
|
+
*
|
|
19
|
+
* Strictly read-only — no mint/refresh/revoke surface; those stay
|
|
20
|
+
* human-typed (`/api oauth`) or human-consented (`oauth-mint`).
|
|
21
|
+
*
|
|
22
|
+
* Redaction posture: names + metadata only. `accessToken`/`refreshToken`
|
|
23
|
+
* values are dropped at the collection boundary (TokenSlotMeta carries no
|
|
24
|
+
* token object), so they can't leak into rendered text OR structured
|
|
25
|
+
* `details`. Secret values never enter this file — `listNames` returns
|
|
26
|
+
* names only.
|
|
27
|
+
*
|
|
28
|
+
* Two-layer learn gate (same as probe's former `listSecrets` arm): the tool
|
|
29
|
+
* registers via the `api-learn` ToolsetSpec (masked when off) AND the
|
|
30
|
+
* handler re-checks `isApiLearnEnabled()` at runtime — belt and suspenders.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
34
|
+
import { Type } from "@earendil-works/pi-ai";
|
|
35
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
36
|
+
import { appendFooter, contentText } from "./utils.js";
|
|
37
|
+
import { listDomains, listNames } from "../core/secrets-store.js";
|
|
38
|
+
import { listSlots, listTokenDomains, slotKey } from "../core/oauth-store.js";
|
|
39
|
+
import { findGuidesByDomain, loadAllGuides } from "../core/guide-store.js";
|
|
40
|
+
import {
|
|
41
|
+
declaredSecretRefNames,
|
|
42
|
+
grantedScopes,
|
|
43
|
+
hostnameOf,
|
|
44
|
+
resolveProvisionedParentDomain,
|
|
45
|
+
} from "../core/auth.js";
|
|
46
|
+
import { isApiLearnEnabled } from "../core/api-toggle.js";
|
|
47
|
+
|
|
48
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
49
|
+
// Types — metadata only; no token/secret value ever lands in these
|
|
50
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
51
|
+
|
|
52
|
+
/** One token slot's renderable metadata (no `accessToken`/`refreshToken`). */
|
|
53
|
+
export interface TokenSlotMeta {
|
|
54
|
+
slot: string;
|
|
55
|
+
grant: string;
|
|
56
|
+
/** The token endpoint the token was minted from (self-describing store). */
|
|
57
|
+
issuer: string;
|
|
58
|
+
/** Granted scope — what the provider echoed, or the requested scopes with an "(assumed)" marker (RFC 6749 §5.1). */
|
|
59
|
+
granted: string;
|
|
60
|
+
/** Human expiry line ("in 6h (refreshable)" / "expired 2h ago" / "never"). */
|
|
61
|
+
expires: string;
|
|
62
|
+
refreshable: boolean;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A guide-declared oauth2 slot with no minted token — the next step is oauth-mint. */
|
|
66
|
+
export interface UnclaimedSlot {
|
|
67
|
+
guide: string;
|
|
68
|
+
grant: string;
|
|
69
|
+
tokenUrl: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface DomainReport {
|
|
73
|
+
domain: string;
|
|
74
|
+
secrets: {
|
|
75
|
+
provisioned: string[];
|
|
76
|
+
/** Secret names the matching guides declare (oauth2 clientId/SecretRef included). */
|
|
77
|
+
declared: string[];
|
|
78
|
+
/** Declared but not provisioned. */
|
|
79
|
+
gaps: string[];
|
|
80
|
+
/** Directories of the guides that made the declaration (empty → no guide). */
|
|
81
|
+
guides: string[];
|
|
82
|
+
};
|
|
83
|
+
tokens: {
|
|
84
|
+
slots: TokenSlotMeta[];
|
|
85
|
+
unclaimed: UnclaimedSlot[];
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface UnscopedView {
|
|
90
|
+
/** Provisioned secret domains with no matching guide. */
|
|
91
|
+
secretDomains: string[];
|
|
92
|
+
/** Token-store domains with no matching guide. */
|
|
93
|
+
tokenDomains: string[];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
97
|
+
// Collection (pure over the two stores + guide cache)
|
|
98
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
99
|
+
|
|
100
|
+
function relSpan(ms: number): string {
|
|
101
|
+
const m = Math.max(1, Math.round(ms / 60_000));
|
|
102
|
+
if (m < 90) return `${m}m`;
|
|
103
|
+
const h = Math.round(m / 60);
|
|
104
|
+
if (h < 48) return `${h}h`;
|
|
105
|
+
return `${Math.round(h / 24)}d`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** Human expiry line. Absent expiresAt → "never"; refresh token → "(refreshable)". */
|
|
109
|
+
function expiresLine(
|
|
110
|
+
expiresAt: number | undefined,
|
|
111
|
+
refreshable: boolean,
|
|
112
|
+
): string {
|
|
113
|
+
let base: string;
|
|
114
|
+
if (expiresAt === undefined) base = "never";
|
|
115
|
+
else {
|
|
116
|
+
const diff = expiresAt - Date.now();
|
|
117
|
+
base = diff > 0 ? `in ${relSpan(diff)}` : `expired ${relSpan(-diff)} ago`;
|
|
118
|
+
}
|
|
119
|
+
return refreshable ? `${base} (refreshable)` : base;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Granted scope line from the shared kernel (RFC 6749 §5.1 fallback). */
|
|
123
|
+
function grantedLine(scope: string | undefined, requested: string[]): string {
|
|
124
|
+
const { scopes, assumed } = grantedScopes(scope, requested);
|
|
125
|
+
if (scopes.length === 0) return "(none recorded)";
|
|
126
|
+
const list = scopes.join(", ");
|
|
127
|
+
return assumed
|
|
128
|
+
? `${list} (assumed)`
|
|
129
|
+
: requested.length > 0
|
|
130
|
+
? `${list} (per mint)`
|
|
131
|
+
: list;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Combined per-domain view: secrets (provisioned/declared/gaps) + token
|
|
135
|
+
* slots + declared-slot gaps. Metadata only by construction. */
|
|
136
|
+
export function collectDomainReport(domain: string): DomainReport {
|
|
137
|
+
const provisioned = listNames(domain);
|
|
138
|
+
const matches = findGuidesByDomain(domain);
|
|
139
|
+
|
|
140
|
+
const declared = new Set<string>();
|
|
141
|
+
for (const { guide } of matches)
|
|
142
|
+
for (const name of declaredSecretRefNames(guide)) declared.add(name);
|
|
143
|
+
const declaredList = [...declared];
|
|
144
|
+
|
|
145
|
+
const slots = listSlots(domain).map((s) => slotMeta(s));
|
|
146
|
+
const claimed = new Set(slots.map((s) => s.slot));
|
|
147
|
+
const unclaimed: UnclaimedSlot[] = [];
|
|
148
|
+
for (const { guide, dirName } of matches) {
|
|
149
|
+
if (guide.auth.kind !== "oauth2") continue;
|
|
150
|
+
if (!claimed.has(slotKey(guide.auth.grant, guide.auth.tokenUrl))) {
|
|
151
|
+
unclaimed.push({
|
|
152
|
+
guide: dirName,
|
|
153
|
+
grant: guide.auth.grant,
|
|
154
|
+
tokenUrl: guide.auth.tokenUrl,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
domain,
|
|
161
|
+
secrets: {
|
|
162
|
+
provisioned,
|
|
163
|
+
declared: declaredList,
|
|
164
|
+
gaps: declaredList.filter((d) => !provisioned.includes(d)),
|
|
165
|
+
guides: matches.map((m) => m.dirName),
|
|
166
|
+
},
|
|
167
|
+
tokens: { slots, unclaimed },
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function slotMeta(s: {
|
|
172
|
+
slot: string;
|
|
173
|
+
grant: string;
|
|
174
|
+
tokenUrl: string;
|
|
175
|
+
token: { refreshToken?: string; expiresAt?: number; scope?: string };
|
|
176
|
+
}): TokenSlotMeta {
|
|
177
|
+
const requested = requestedScopesFor(s.grant, s.tokenUrl);
|
|
178
|
+
const refreshable = s.token.refreshToken !== undefined;
|
|
179
|
+
return {
|
|
180
|
+
slot: s.slot,
|
|
181
|
+
grant: s.grant,
|
|
182
|
+
issuer: s.tokenUrl,
|
|
183
|
+
granted: grantedLine(s.token.scope, requested),
|
|
184
|
+
expires: expiresLine(s.token.expiresAt, refreshable),
|
|
185
|
+
refreshable,
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Requested scopes for a slot from any matching guide's oauth2 block —
|
|
190
|
+
* the RFC 6749 §5.1 fallback when the provider echoed no scope. Empty for
|
|
191
|
+
* guide-less tokens (nothing requested on record). */
|
|
192
|
+
function requestedScopesFor(grant: string, tokenUrl: string): string[] {
|
|
193
|
+
// Declared-scope fallback is domain-agnostic: the same (grant, tokenUrl)
|
|
194
|
+
// identifies the guide's oauth2 block regardless of routing domain, so
|
|
195
|
+
// scan every loaded guide — including ones whose domain has no minted
|
|
196
|
+
// tokens yet (those were invisible to the old token-domain-derived scan).
|
|
197
|
+
for (const guide of Object.values(loadAllGuides().guides)) {
|
|
198
|
+
if (guide.auth.kind !== "oauth2") continue;
|
|
199
|
+
if (
|
|
200
|
+
guide.auth.grant === grant &&
|
|
201
|
+
guide.auth.tokenUrl === tokenUrl &&
|
|
202
|
+
guide.auth.scopes?.length
|
|
203
|
+
) {
|
|
204
|
+
return guide.auth.scopes;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return [];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Bare-call orphan view across both stores. Token orphan filtering matches
|
|
211
|
+
* guides via the same store-domain seam as minting: the token domain and
|
|
212
|
+
* its provisioned parent are both checked, so an `api.`-subdomain token
|
|
213
|
+
* resolves to its parent's guides instead of false-positive as guideless. */
|
|
214
|
+
export function collectUnscoped(): UnscopedView {
|
|
215
|
+
const guideless = (d: string): boolean =>
|
|
216
|
+
findGuidesByDomain(d).length === 0 &&
|
|
217
|
+
findGuidesByDomain(resolveProvisionedParentDomain(d)).length === 0;
|
|
218
|
+
return {
|
|
219
|
+
secretDomains: listDomains().filter(guideless),
|
|
220
|
+
tokenDomains: listTokenDomains().filter(guideless),
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
225
|
+
// Formatting
|
|
226
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
227
|
+
|
|
228
|
+
export function formatOrphanView(v: UnscopedView): string {
|
|
229
|
+
const lines: string[] = ["🔐 store overview", ""];
|
|
230
|
+
lines.push(
|
|
231
|
+
` unscoped secret domains: ${v.secretDomains.length > 0 ? v.secretDomains.join(", ") : "none"}`,
|
|
232
|
+
);
|
|
233
|
+
lines.push(
|
|
234
|
+
` token domains with no guide: ${v.tokenDomains.length > 0 ? v.tokenDomains.join(", ") : "none"}`,
|
|
235
|
+
);
|
|
236
|
+
lines.push("");
|
|
237
|
+
lines.push(
|
|
238
|
+
" (names + metadata only — secret and token values never leave the store)",
|
|
239
|
+
);
|
|
240
|
+
return lines.join("\n");
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export function formatDomainReport(r: DomainReport): string {
|
|
244
|
+
const lines: string[] = [`🔐 store: ${r.domain}`, "", "secrets"];
|
|
245
|
+
const s = r.secrets;
|
|
246
|
+
lines.push(
|
|
247
|
+
` provisioned: ${s.provisioned.length > 0 ? s.provisioned.join(", ") : "(none)"}`,
|
|
248
|
+
);
|
|
249
|
+
if (s.guides.length > 0) {
|
|
250
|
+
lines.push(
|
|
251
|
+
` declared: ${s.declared.length > 0 ? s.declared.join(", ") : "(none)"} (guide: ${s.guides.join(", ")})`,
|
|
252
|
+
);
|
|
253
|
+
if (s.gaps.length > 0) lines.push(` gaps: ${s.gaps.join(", ")}`);
|
|
254
|
+
}
|
|
255
|
+
lines.push("", "tokens");
|
|
256
|
+
if (r.tokens.slots.length === 0 && r.tokens.unclaimed.length === 0) {
|
|
257
|
+
lines.push(" (no tokens minted, none declared)");
|
|
258
|
+
}
|
|
259
|
+
for (const t of r.tokens.slots) {
|
|
260
|
+
lines.push(` ${t.slot}`);
|
|
261
|
+
lines.push(` issuer: ${t.issuer}`);
|
|
262
|
+
lines.push(` granted: ${t.granted}`);
|
|
263
|
+
lines.push(` expires: ${t.expires}`);
|
|
264
|
+
}
|
|
265
|
+
for (const u of r.tokens.unclaimed) {
|
|
266
|
+
lines.push(
|
|
267
|
+
` — guide (${u.guide}) declares ${u.grant} via ${u.tokenUrl}: no token minted`,
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
return lines.join("\n");
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** Collect + wrap a per-domain report into the tool result shape. */
|
|
274
|
+
function finishDomainReport(domain: string): {
|
|
275
|
+
content: { type: "text"; text: string }[];
|
|
276
|
+
details: DomainReport;
|
|
277
|
+
} {
|
|
278
|
+
const report = collectDomainReport(domain);
|
|
279
|
+
return {
|
|
280
|
+
content: [{ type: "text", text: formatDomainReport(report) }],
|
|
281
|
+
details: report,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
286
|
+
// Tool definition
|
|
287
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
288
|
+
|
|
289
|
+
export const apiStoreTool = defineTool({
|
|
290
|
+
name: "api-store",
|
|
291
|
+
label: "API Store",
|
|
292
|
+
description:
|
|
293
|
+
"Read-only inspection of both credential stores (secrets + OAuth2 tokens) in " +
|
|
294
|
+
"one call — learn mode only (run /api learn first). Bare call (no domain, no " +
|
|
295
|
+
"apiHost): orphan view — provisioned-but-guideless secret domains + guideless " +
|
|
296
|
+
"token domains, the authoring-bootstrap check. With domain (or apiHost): " +
|
|
297
|
+
"combined report — provisioned vs declared secret names and their gaps, minted " +
|
|
298
|
+
"token slots (issuer, granted scope, expiry, refreshable), and declared-slot " +
|
|
299
|
+
"gaps (a guide-declared oauth2 grant with no token minted → next step is " +
|
|
300
|
+
"oauth-mint). Names + metadata only — secret and token values never leave the " +
|
|
301
|
+
"store. Strictly read-only: mint/refresh/revoke stay human-typed (/api oauth) " +
|
|
302
|
+
"or human-consented (oauth-mint).",
|
|
303
|
+
|
|
304
|
+
parameters: Type.Object({
|
|
305
|
+
domain: Type.Optional(
|
|
306
|
+
Type.String({
|
|
307
|
+
description:
|
|
308
|
+
"Store domain to inspect (e.g. 'github.com'). Omit domain and apiHost for the bare orphan view.",
|
|
309
|
+
}),
|
|
310
|
+
),
|
|
311
|
+
apiHost: Type.Optional(
|
|
312
|
+
Type.String({
|
|
313
|
+
description:
|
|
314
|
+
"API base URL — the store domain defaults to its hostname (or the longest provisioned parent, e.g. pro-api.example.dev → example.dev). Ignored when domain is set.",
|
|
315
|
+
}),
|
|
316
|
+
),
|
|
317
|
+
}),
|
|
318
|
+
|
|
319
|
+
async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
|
|
320
|
+
const { domain, apiHost } = params as {
|
|
321
|
+
domain?: string;
|
|
322
|
+
apiHost?: string;
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
// Runtime re-check (the toolset mask is the first layer): a stale
|
|
326
|
+
// registration must not read the stores outside learn mode.
|
|
327
|
+
if (!isApiLearnEnabled()) {
|
|
328
|
+
return {
|
|
329
|
+
content: [
|
|
330
|
+
{
|
|
331
|
+
type: "text",
|
|
332
|
+
text:
|
|
333
|
+
"api-store: store inspection is learn mode only — run /api learn first.",
|
|
334
|
+
},
|
|
335
|
+
],
|
|
336
|
+
details: { error: "learn_mode_only" },
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (domain) return finishDomainReport(resolveProvisionedParentDomain(domain));
|
|
341
|
+
if (apiHost)
|
|
342
|
+
return finishDomainReport(
|
|
343
|
+
resolveProvisionedParentDomain(hostnameOf(apiHost)),
|
|
344
|
+
);
|
|
345
|
+
const unscoped = collectUnscoped();
|
|
346
|
+
return {
|
|
347
|
+
content: [{ type: "text", text: formatOrphanView(unscoped) }],
|
|
348
|
+
details: { unscoped },
|
|
349
|
+
};
|
|
350
|
+
},
|
|
351
|
+
|
|
352
|
+
renderCall(args, theme, _context) {
|
|
353
|
+
const parts: string[] = [theme.fg("toolTitle", theme.bold("api-store "))];
|
|
354
|
+
if (args.domain) parts.push(theme.fg("accent", `"${args.domain}"`));
|
|
355
|
+
else if (args.apiHost) parts.push(theme.fg("accent", `"${args.apiHost}"`));
|
|
356
|
+
else parts.push(theme.fg("dim", "orphan view"));
|
|
357
|
+
return new Text(parts.join(" "), 0, 0);
|
|
358
|
+
},
|
|
359
|
+
|
|
360
|
+
renderResult(result, { expanded, isPartial }, theme, _context) {
|
|
361
|
+
if (isPartial) return new Text(theme.fg("warning", "Inspecting…"), 0, 0);
|
|
362
|
+
const d = result.details as Record<string, unknown> | undefined;
|
|
363
|
+
if (d?.error) {
|
|
364
|
+
return new Text(theme.fg("error", `⚠ ${contentText(result, "?")}`), 0, 0);
|
|
365
|
+
}
|
|
366
|
+
const unscoped = d?.unscoped as UnscopedView | undefined;
|
|
367
|
+
let text = theme.fg("accent", theme.bold("🔐 api-store"));
|
|
368
|
+
if (unscoped) {
|
|
369
|
+
text += ` — ${unscoped.secretDomains.length} unscoped secret domain(s) · ${unscoped.tokenDomains.length} guideless token domain(s)`;
|
|
370
|
+
} else {
|
|
371
|
+
const secrets = d?.secrets as DomainReport["secrets"] | undefined;
|
|
372
|
+
const tokens = d?.tokens as DomainReport["tokens"] | undefined;
|
|
373
|
+
if (secrets && tokens) {
|
|
374
|
+
text += ` — ${d?.domain} · ${secrets.provisioned.length} provisioned · ${tokens.slots.length} token slot(s)`;
|
|
375
|
+
if (tokens.unclaimed.length > 0)
|
|
376
|
+
text += ` · ${tokens.unclaimed.length} unclaimed`;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return new Text(appendFooter(text, expanded, result, theme, 1000), 0, 0);
|
|
380
|
+
},
|
|
381
|
+
});
|