pi-lean-host 0.4.0 → 0.5.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 +996 -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 +529 -142
- package/core/local-helpers.ts +7 -0
- package/core/oauth-command.ts +911 -0
- package/core/oauth-flow.ts +376 -0
- package/core/oauth-store.ts +306 -0
- package/core/parse-api-guide.ts +1122 -591
- 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 +321 -34
- 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 +20 -11
- package/tools/api-learn.ts +131 -57
- package/tools/api-probe.ts +455 -282
- package/tools/api-scaffold.ts +15 -22
- package/tools/api-store.ts +381 -0
- package/tools/oauth-mint.ts +338 -0
- package/tools/utils.ts +1 -1
- package/core/verify-ship-manifest.ts +0 -118
- package/tools/index.ts +0 -8
package/core/verify-command.ts
CHANGED
|
@@ -41,26 +41,25 @@ import {
|
|
|
41
41
|
invalidateCache,
|
|
42
42
|
getUserGuidesDir,
|
|
43
43
|
} from "./guide-store.js";
|
|
44
|
-
import {
|
|
45
|
-
|
|
46
|
-
selectGuideByShortName,
|
|
47
|
-
shortNameErrorText,
|
|
48
|
-
stampFrontmatterField,
|
|
49
|
-
TODAY,
|
|
50
|
-
} from "./parse-api-guide.js";
|
|
51
|
-
import { pickGuide } from "./guide-picker.js";
|
|
44
|
+
import { stampFrontmatterField, TODAY } from "./parse-api-guide.js";
|
|
45
|
+
import { pickGuideForCommand } from "./guide-picker.js";
|
|
52
46
|
import {
|
|
53
47
|
resolveSecretHeaders,
|
|
54
48
|
resolveSecretQueryParams,
|
|
49
|
+
resolveSecretPathParams,
|
|
50
|
+
missingRequiredSecrets,
|
|
51
|
+
secretPathTokenNames,
|
|
55
52
|
canonicalStoreDomain,
|
|
53
|
+
hasUsableTokenPath,
|
|
56
54
|
} from "./auth.js";
|
|
55
|
+
import { readSecret } from "./secrets-store.js";
|
|
57
56
|
import { resolveOpForExecution, type ResolveOpResult } from "./resolve-op.js";
|
|
58
57
|
import {
|
|
59
58
|
HelperError,
|
|
60
59
|
type RestGetResult,
|
|
61
60
|
type PaginateResult,
|
|
62
61
|
} from "./helpers.js";
|
|
63
|
-
import type {
|
|
62
|
+
import type { Operation } from "./api-guide-types.js";
|
|
64
63
|
|
|
65
64
|
/** Usage + cost note, surfaced by `--help`. */
|
|
66
65
|
function helpText(): string {
|
|
@@ -71,8 +70,9 @@ function helpText(): string {
|
|
|
71
70
|
" /api verify <domain> --force stamp verified: today WITHOUT running any ops (human-attested)",
|
|
72
71
|
" /api verify --help this help",
|
|
73
72
|
"",
|
|
74
|
-
" Ops with unsatisfiable params (a path {token}
|
|
75
|
-
" are skipped, not failed. Scaffold a starter sidecar
|
|
73
|
+
" Ops with unsatisfiable params (a path {token} not store-filled via secretPathRefs, or a",
|
|
74
|
+
" required query param with no default) are skipped, not failed. Scaffold a starter sidecar",
|
|
75
|
+
" with api-scaffold({domain, verify: true})",
|
|
76
76
|
' (writes to /tmp, with "__FILL_ME__" sentinels for every blocking param), then save via',
|
|
77
77
|
" api-learn({domain, dir}) — the sidecar lives at:",
|
|
78
78
|
` ~/.pi/agent/pi-lean-host/api-guides/<dirName>/verify.json`,
|
|
@@ -124,41 +124,14 @@ export async function handleVerifySubcommand(
|
|
|
124
124
|
return;
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
sel,
|
|
136
|
-
domain,
|
|
137
|
-
guideSelector,
|
|
138
|
-
`Call /api verify ${domain} to see the menu.`,
|
|
139
|
-
),
|
|
140
|
-
"warning",
|
|
141
|
-
);
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
selected = sel;
|
|
145
|
-
} else {
|
|
146
|
-
// N guides, no selector → interactive pick (TUI) or the menu
|
|
147
|
-
// fallback (headless/RPC/print or cancelled), nothing run yet.
|
|
148
|
-
const picked = await pickGuide(ctx, matches);
|
|
149
|
-
if (!picked) {
|
|
150
|
-
ctx.ui.notify(
|
|
151
|
-
[
|
|
152
|
-
`${matches.length} API guides for '${domain}':`,
|
|
153
|
-
formatGuideListings(matches),
|
|
154
|
-
`Call /api verify ${domain} <shortName> to pick one.`,
|
|
155
|
-
].join("\n"),
|
|
156
|
-
"info",
|
|
157
|
-
);
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
selected = picked;
|
|
161
|
-
}
|
|
127
|
+
const selected = await pickGuideForCommand(
|
|
128
|
+
ctx,
|
|
129
|
+
domain,
|
|
130
|
+
guideSelector,
|
|
131
|
+
matches,
|
|
132
|
+
"/api verify",
|
|
133
|
+
);
|
|
134
|
+
if (!selected) return;
|
|
162
135
|
|
|
163
136
|
const { guide, dirName } = selected;
|
|
164
137
|
const storeDomain = canonicalStoreDomain(guide);
|
|
@@ -175,24 +148,54 @@ export async function handleVerifySubcommand(
|
|
|
175
148
|
return;
|
|
176
149
|
}
|
|
177
150
|
|
|
178
|
-
// Auth precheck (fail-fast): resolve the same secrets api-fetch does.
|
|
179
|
-
//
|
|
180
|
-
// do not run N ops that all fail
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
151
|
+
// Auth precheck (fail-fast): resolve the same secrets/token api-fetch does.
|
|
152
|
+
// If a required secret is unprovisioned or no OAuth2 token is mintable,
|
|
153
|
+
// short-circuit with ONE message — do not run N ops that all fail
|
|
154
|
+
// identically.
|
|
155
|
+
switch (guide.auth.kind) {
|
|
156
|
+
case "static-key": {
|
|
157
|
+
const headerRes = resolveSecretHeaders(guide.auth, storeDomain);
|
|
158
|
+
const queryRes = resolveSecretQueryParams(guide.auth, storeDomain);
|
|
159
|
+
const pathRes = resolveSecretPathParams(guide.auth, storeDomain);
|
|
160
|
+
const missingRequired = missingRequiredSecrets(headerRes, queryRes, pathRes);
|
|
161
|
+
if (missingRequired.length > 0) {
|
|
162
|
+
ctx.ui.notify(
|
|
163
|
+
`🔑 ${guide.shortName} requires a secret not yet provisioned: ` +
|
|
164
|
+
`${missingRequired.join(", ")}.\n` +
|
|
165
|
+
`Run /api secrets ${storeDomain} to provision it, then re-run /api verify.`,
|
|
166
|
+
"warning",
|
|
167
|
+
);
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case "oauth2": {
|
|
173
|
+
if (!hasUsableTokenPath(guide.auth, storeDomain)) {
|
|
174
|
+
// client_credentials reaching here ⇒ at least one credential secret
|
|
175
|
+
// is unprovisioned (hasUsableTokenPath would have returned true
|
|
176
|
+
// otherwise) — name the missing ones like the static-key arm above.
|
|
177
|
+
const missingCreds = [
|
|
178
|
+
guide.auth.clientId.secret,
|
|
179
|
+
...(guide.auth.clientSecret ? [guide.auth.clientSecret.secret] : []),
|
|
180
|
+
].filter((name) => readSecret(storeDomain, name) === null);
|
|
181
|
+
const hint =
|
|
182
|
+
guide.auth.grant === "authorization_code"
|
|
183
|
+
? `Run /api oauth ${storeDomain} to start the interactive flow`
|
|
184
|
+
: `Provision ${missingCreds.join(", ")} via /api secrets ${storeDomain}, then run /api oauth ${storeDomain}`;
|
|
185
|
+
ctx.ui.notify(
|
|
186
|
+
`🔑 ${guide.shortName} has no usable OAuth2 token for '${storeDomain}'.\n` +
|
|
187
|
+
`${hint}, then re-run /api verify.`,
|
|
188
|
+
"warning",
|
|
189
|
+
);
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case "none":
|
|
195
|
+
break;
|
|
196
|
+
default: {
|
|
197
|
+
const _exhaustive: never = guide.auth;
|
|
198
|
+
throw new Error(`Unhandled auth kind: ${_exhaustive}`);
|
|
196
199
|
}
|
|
197
200
|
}
|
|
198
201
|
|
|
@@ -207,6 +210,10 @@ export async function handleVerifySubcommand(
|
|
|
207
210
|
}
|
|
208
211
|
const verifyJson = sidecar && "data" in sidecar ? sidecar.data : {};
|
|
209
212
|
|
|
213
|
+
// Path tokens owned by secretPathRefs are store-filled, never
|
|
214
|
+
// caller-supplied — never unsatisfiable.
|
|
215
|
+
const secretPathTokens = secretPathTokenNames(guide.auth);
|
|
216
|
+
|
|
210
217
|
// ── Fetch loop ──────────────────────────────────────────────
|
|
211
218
|
const ops = guide.operations;
|
|
212
219
|
const report: string[] = [];
|
|
@@ -222,7 +229,9 @@ export async function handleVerifySubcommand(
|
|
|
222
229
|
for (const [k, v] of Object.entries(rawSupplied)) {
|
|
223
230
|
if (v !== "__FILL_ME__") supplied[k] = v;
|
|
224
231
|
}
|
|
225
|
-
const missing = renderForReport(
|
|
232
|
+
const missing = renderForReport(
|
|
233
|
+
unsatisfiable(op, supplied, secretPathTokens),
|
|
234
|
+
);
|
|
226
235
|
if (missing.length > 0) {
|
|
227
236
|
skipped++;
|
|
228
237
|
report.push(
|
|
@@ -255,6 +264,10 @@ export async function handleVerifySubcommand(
|
|
|
255
264
|
try {
|
|
256
265
|
outcome = await resolveOpForExecution(guide, op, dirName, {
|
|
257
266
|
userParams: run.params,
|
|
267
|
+
// Verify's whole point is a live check — never validate against
|
|
268
|
+
// a cached body. Auth-bearing ops never touch the cache anyway;
|
|
269
|
+
// this is belt-and-braces for no-auth guides.
|
|
270
|
+
fresh: true,
|
|
258
271
|
});
|
|
259
272
|
} catch (err) {
|
|
260
273
|
failed++;
|
|
@@ -279,6 +292,13 @@ export async function handleVerifySubcommand(
|
|
|
279
292
|
// remaining fan-out runs would skip identically, so stop here.
|
|
280
293
|
break;
|
|
281
294
|
}
|
|
295
|
+
if (outcome.reason === "oauth_token_missing") {
|
|
296
|
+
// Unmintable OAuth2 token — same nudge for every op, so fail once
|
|
297
|
+
// (the auth precheck will fail-fast before the loop).
|
|
298
|
+
failed++;
|
|
299
|
+
report.push(` ✗ ${opTag(op.name, run.tag)} — ${outcome.message}`);
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
282
302
|
// auth_required_not_provisioned — unreachable after the precheck
|
|
283
303
|
// (auth is per-guide constant); defensive.
|
|
284
304
|
failed++;
|
|
@@ -350,6 +370,12 @@ export async function handleVerifySubcommand(
|
|
|
350
370
|
* supplied; group members are governed by the group, not per-param required
|
|
351
371
|
* (the parser bans `required: true` on them), so they're skipped in the
|
|
352
372
|
* per-param loop.
|
|
373
|
+
*
|
|
374
|
+
* `secretPathTokens`: path-token names owned by the guide's
|
|
375
|
+
* `auth.secretPathRefs` — store-filled, never caller-supplied, so never
|
|
376
|
+
* unsatisfiable. Both call sites (verify-command loop + api-scaffold
|
|
377
|
+
* sentinels) must pass it, or secret-owned tokens get misreported as
|
|
378
|
+
* needing a verify.json sentinel.
|
|
353
379
|
*/
|
|
354
380
|
export type Unsatisfiable =
|
|
355
381
|
| { kind: "path"; param: string }
|
|
@@ -360,9 +386,11 @@ export type Unsatisfiable =
|
|
|
360
386
|
export function unsatisfiable(
|
|
361
387
|
op: Operation,
|
|
362
388
|
supplied: Record<string, unknown>,
|
|
389
|
+
secretPathTokens?: ReadonlySet<string>,
|
|
363
390
|
): Unsatisfiable[] {
|
|
364
391
|
const missing: Unsatisfiable[] = [];
|
|
365
392
|
for (const token of op.pathParams) {
|
|
393
|
+
if (secretPathTokens?.has(token)) continue; // store-filled (secretPathRefs)
|
|
366
394
|
if (supplied[token] === undefined)
|
|
367
395
|
missing.push({ kind: "path", param: token });
|
|
368
396
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Migrating guides to schema v1
|
|
2
|
+
|
|
3
|
+
> Fix for guides rejected with `schemaVersion: 0` (or no `schemaVersion`
|
|
4
|
+
> line). One breaking change: static-key auth went flat → nested.
|
|
5
|
+
|
|
6
|
+
## Before (v0) → After (v1)
|
|
7
|
+
|
|
8
|
+
```yaml
|
|
9
|
+
# v0
|
|
10
|
+
auth:
|
|
11
|
+
kind: static-key
|
|
12
|
+
requires: [api_key] # deleted
|
|
13
|
+
optional: [user_key] # deleted
|
|
14
|
+
headerPrefixes:
|
|
15
|
+
Authorization: "Bearer " # deleted
|
|
16
|
+
secretRefs:
|
|
17
|
+
Authorization: api_key
|
|
18
|
+
X-Api-Key: user_key
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```yaml
|
|
22
|
+
# v1
|
|
23
|
+
schemaVersion: 1
|
|
24
|
+
auth:
|
|
25
|
+
kind: static-key
|
|
26
|
+
secretRefs:
|
|
27
|
+
Authorization:
|
|
28
|
+
secret: api_key # store name (was a bare string)
|
|
29
|
+
prefix: "Bearer " # from headerPrefixes
|
|
30
|
+
X-Api-Key:
|
|
31
|
+
secret: user_key
|
|
32
|
+
optional: true # from the optional roster
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
| v0 | v1 |
|
|
36
|
+
|----|-----|
|
|
37
|
+
| `secretRefs: {<header>: <name>}` | `secretRefs: {<header>: {secret: <name>}}` |
|
|
38
|
+
| `requires: [<name>]` | delete — required is the default |
|
|
39
|
+
| `optional: [<name>]` | `optional: true` on the ref that uses it |
|
|
40
|
+
| `headerPrefixes: {<header>: "<p>"}` | `prefix: "<p>"` on that header's ref |
|
|
41
|
+
| no `schemaVersion` line | add `schemaVersion: 1` |
|
|
42
|
+
|
|
43
|
+
Fix = stamp `schemaVersion: 1`, wrap each flat `secretRefs` value as
|
|
44
|
+
`{secret: <name>}`, merge in `requires`/`optional`/`headerPrefixes`.
|
|
45
|
+
|
|
46
|
+
## Non-events
|
|
47
|
+
|
|
48
|
+
Additive changes (optional fields, new enum values, relaxed constraints)
|
|
49
|
+
never bump the version. Guides saved via current `api-learn` are stamped
|
|
50
|
+
already.
|
|
51
|
+
|
|
52
|
+
## Shelf life
|
|
53
|
+
|
|
54
|
+
Stays as long as v0 guides can exist: user-authored local guides
|
|
55
|
+
(`~/.pi/agent/pi-lean-host/api-guides/`) can hit the gate long after it
|
|
56
|
+
ships. Superseded only by a v2 migration doc.
|
package/index.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "./tools/
|
|
3
|
+
import { apiGuideTool } from "./tools/api-guide.js";
|
|
4
|
+
import { apiFetchTool } from "./tools/api-fetch.js";
|
|
5
|
+
import { apiLearnTool } from "./tools/api-learn.js";
|
|
6
|
+
import { apiProbeTool } from "./tools/api-probe.js";
|
|
7
|
+
import { apiScaffoldTool } from "./tools/api-scaffold.js";
|
|
8
|
+
import { apiStoreTool } from "./tools/api-store.js";
|
|
9
|
+
import { oauthMintTool } from "./tools/oauth-mint.js";
|
|
10
10
|
import initApiToggle from "./core/api-toggle.js";
|
|
11
11
|
import { registerPortalProjection } from "./core/portal-projection.js";
|
|
12
12
|
import { invalidateCache, loadAllGuides } from "./core/guide-store.js";
|
|
@@ -27,6 +27,8 @@ export default function (pi: ExtensionAPI): void {
|
|
|
27
27
|
pi.registerTool(apiLearnTool);
|
|
28
28
|
pi.registerTool(apiProbeTool);
|
|
29
29
|
pi.registerTool(apiScaffoldTool);
|
|
30
|
+
pi.registerTool(apiStoreTool);
|
|
31
|
+
pi.registerTool(oauthMintTool);
|
|
30
32
|
|
|
31
33
|
// ── Register /api command and session hooks ─────────────────
|
|
32
34
|
initApiToggle(pi);
|
|
@@ -38,12 +40,12 @@ export default function (pi: ExtensionAPI): void {
|
|
|
38
40
|
// session_start ensures registration even if host loads before portal.
|
|
39
41
|
pi.on("session_start", async (_event, ctx) => {
|
|
40
42
|
registerPortalProjection();
|
|
41
|
-
// Clear the guide-store cache so a folder rename
|
|
42
|
-
//
|
|
43
|
+
// Clear the guide-store cache so a folder rename (fixing the divergence
|
|
44
|
+
// check's "mv" instruction) made between sessions is picked up —
|
|
43
45
|
// regardless of whether pi re-evaluates the module or re-calls the entry
|
|
44
46
|
// function with persisted module-level state (the /resume path).
|
|
45
47
|
invalidateCache();
|
|
46
|
-
// Trigger a fresh scan with the UI notify channel so the
|
|
48
|
+
// Trigger a fresh scan with the UI notify channel so the schema-gate
|
|
47
49
|
// banner + per-guide warnings render through the Text component (wraps
|
|
48
50
|
// long lines, honors newlines) instead of raw console.warn (truncates
|
|
49
51
|
// + merges with the status bar). No-op on a warm cache; every later
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-lean-host",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "REST API client for Pi.
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Read-only REST API client for Pi. Your agent authors and calls YAML recipes handling the tedious parts: pagination, OAuth2, rate limits, backoff. Credentials live in a local store, injected in code, never in agent context. Churn-free research + retrieval",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
7
7
|
"pi-extension",
|
|
@@ -25,11 +25,13 @@
|
|
|
25
25
|
".": "./index.ts",
|
|
26
26
|
"./core/helpers.js": "./core/helpers.ts",
|
|
27
27
|
"./core/parse-api-guide.js": "./core/parse-api-guide.ts",
|
|
28
|
+
"./core/guide-catalog.js": "./core/guide-catalog.ts",
|
|
28
29
|
"./core/guide-store.js": "./core/guide-store.ts",
|
|
29
30
|
"./core/local-helpers.js": "./core/local-helpers.ts",
|
|
30
31
|
"./core/api-guide-types.js": "./core/api-guide-types.ts",
|
|
31
32
|
"./core/transport.js": "./core/transport.ts",
|
|
32
|
-
"./core/auth.js": "./core/auth.ts"
|
|
33
|
+
"./core/auth.js": "./core/auth.ts",
|
|
34
|
+
"./core/resolve-op.js": "./core/resolve-op.ts"
|
|
33
35
|
},
|
|
34
36
|
"files": [
|
|
35
37
|
"index.ts",
|
|
@@ -39,10 +41,12 @@
|
|
|
39
41
|
"core/guide-loader.ts",
|
|
40
42
|
"core/api-guide-types.ts",
|
|
41
43
|
"core/parse-api-guide.ts",
|
|
44
|
+
"core/guide-catalog.ts",
|
|
42
45
|
"core/guide-store.ts",
|
|
43
46
|
"core/transport.ts",
|
|
44
47
|
"core/helpers.ts",
|
|
45
48
|
"core/path-template.ts",
|
|
49
|
+
"core/fs-0600.ts",
|
|
46
50
|
"core/local-helpers.ts",
|
|
47
51
|
"core/helpers-command.ts",
|
|
48
52
|
"core/api-toggle.ts",
|
|
@@ -53,16 +57,22 @@
|
|
|
53
57
|
"core/response-spill.ts",
|
|
54
58
|
"core/resolve-op.ts",
|
|
55
59
|
"core/guide-picker.ts",
|
|
60
|
+
"core/select-picker.ts",
|
|
56
61
|
"core/verify-command.ts",
|
|
57
62
|
"core/delete-command.ts",
|
|
58
|
-
"core/
|
|
59
|
-
"
|
|
63
|
+
"core/oauth-store.ts",
|
|
64
|
+
"core/oauth-command.ts",
|
|
65
|
+
"core/oauth-flow.ts",
|
|
66
|
+
"core/staging.ts",
|
|
60
67
|
"tools/api-guide.ts",
|
|
61
68
|
"tools/api-fetch.ts",
|
|
62
69
|
"tools/api-learn.ts",
|
|
63
70
|
"tools/api-scaffold.ts",
|
|
71
|
+
"tools/api-store.ts",
|
|
72
|
+
"tools/oauth-mint.ts",
|
|
64
73
|
"tools/api-probe.ts",
|
|
65
74
|
"tools/utils.ts",
|
|
75
|
+
"docs/migration-v1.md",
|
|
66
76
|
"README.md"
|
|
67
77
|
],
|
|
68
78
|
"pi": {
|
|
@@ -76,7 +86,7 @@
|
|
|
76
86
|
},
|
|
77
87
|
"dependencies": {
|
|
78
88
|
"fast-xml-parser": "^5.7.0",
|
|
79
|
-
"pi-tool-masking": "^1.
|
|
89
|
+
"pi-tool-masking": "^1.3.0",
|
|
80
90
|
"undici": "^8.0.0",
|
|
81
91
|
"yaml": "^2.7.0"
|
|
82
92
|
},
|
package/tools/api-fetch.ts
CHANGED
|
@@ -30,11 +30,12 @@ import {
|
|
|
30
30
|
} from "../core/resolve-op.js";
|
|
31
31
|
import { canonicalStoreDomain, containsSecret } from "../core/auth.js";
|
|
32
32
|
import { provisionedDomainsSuffix } from "../core/secrets-store.js";
|
|
33
|
+
import { formatGuideListings } from "../core/guide-catalog.js";
|
|
33
34
|
import {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
spillResponse,
|
|
36
|
+
formatSpillNotice,
|
|
37
|
+
INLINE_LIMIT,
|
|
38
|
+
} from "../core/response-spill.js";
|
|
38
39
|
import { appendFooter, contentText } from "./utils.js";
|
|
39
40
|
import type { Operation, ApiGuide } from "../core/api-guide-types.js";
|
|
40
41
|
|
|
@@ -85,6 +86,12 @@ export const apiFetchTool = defineTool({
|
|
|
85
86
|
"When true, paginate to gather all items up to the guide's gatherAllMax ceiling.",
|
|
86
87
|
}),
|
|
87
88
|
),
|
|
89
|
+
fresh: Type.Optional(
|
|
90
|
+
Type.Boolean({
|
|
91
|
+
description:
|
|
92
|
+
"When true, bypass the response cache and force a full network fetch. Responses are cached only when the server grants freshness (Cache-Control: max-age, or an ETag that revalidates via 304); pass fresh: true when you need guaranteed-current data (e.g. polling a status).",
|
|
93
|
+
}),
|
|
94
|
+
),
|
|
88
95
|
}),
|
|
89
96
|
|
|
90
97
|
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
@@ -96,22 +103,28 @@ export const apiFetchTool = defineTool({
|
|
|
96
103
|
gatherAll?: boolean;
|
|
97
104
|
};
|
|
98
105
|
|
|
99
|
-
// `gatherAll` may be passed at the tool's top level (the
|
|
100
|
-
//
|
|
101
|
-
// holds the operation arguments). Resolve both, preferring
|
|
102
|
-
// and strip
|
|
103
|
-
// a `passthrough` query string or reach a domain helper. This
|
|
104
|
-
// before `executeParams` is built below.
|
|
106
|
+
// `gatherAll` and `fresh` may be passed at the tool's top level (the
|
|
107
|
+
// documented positions) or nested inside `params` (the natural place,
|
|
108
|
+
// since `params` holds the operation arguments). Resolve both, preferring
|
|
109
|
+
// the top level, and strip both out of the forwarded params so they can't
|
|
110
|
+
// leak onto a `passthrough` query string or reach a domain helper. This
|
|
111
|
+
// must happen before `executeParams` is built below.
|
|
105
112
|
const rawParams = (params as Record<string, unknown>)["params"] as
|
|
106
113
|
| Record<string, unknown>
|
|
107
114
|
| undefined;
|
|
108
115
|
const gatherAll =
|
|
109
116
|
((params as Record<string, unknown>)["gatherAll"] as boolean | undefined) ??
|
|
110
117
|
(rawParams?.gatherAll as boolean | undefined);
|
|
118
|
+
const fresh =
|
|
119
|
+
((params as Record<string, unknown>)["fresh"] as boolean | undefined) ??
|
|
120
|
+
(rawParams?.fresh as boolean | undefined);
|
|
111
121
|
const userParams = rawParams ? { ...rawParams } : undefined;
|
|
112
122
|
if (userParams && "gatherAll" in userParams) {
|
|
113
123
|
delete userParams.gatherAll;
|
|
114
124
|
}
|
|
125
|
+
if (userParams && "fresh" in userParams) {
|
|
126
|
+
delete userParams.fresh;
|
|
127
|
+
}
|
|
115
128
|
|
|
116
129
|
// 1. Resolve guides by domain — a domain may be claimed by more than
|
|
117
130
|
// one guide (one domain, multiple APIs). Search every match for the
|
|
@@ -183,6 +196,7 @@ export const apiFetchTool = defineTool({
|
|
|
183
196
|
const execOpts: ResolveOpOptions = { skipSsrfGuard: _bypassUrlSafety };
|
|
184
197
|
if (userParams) execOpts.userParams = userParams;
|
|
185
198
|
if (gatherAll !== undefined) execOpts.gatherAll = gatherAll;
|
|
199
|
+
if (fresh !== undefined) execOpts.fresh = fresh;
|
|
186
200
|
outcome = await resolveOpForExecution(guide, op, helperDirName, execOpts);
|
|
187
201
|
} catch (err) {
|
|
188
202
|
if (err instanceof HelperError) {
|
|
@@ -213,8 +227,9 @@ export const apiFetchTool = defineTool({
|
|
|
213
227
|
};
|
|
214
228
|
}
|
|
215
229
|
|
|
216
|
-
// Non-run outcomes: a session-disabled helper
|
|
217
|
-
// `requires` secret
|
|
230
|
+
// Non-run outcomes: a session-disabled helper, a fail-closed missing
|
|
231
|
+
// `requires` secret, or an unmintable OAuth2 token. All return before
|
|
232
|
+
// any request is made.
|
|
218
233
|
if (!outcome.ok) {
|
|
219
234
|
if (outcome.reason === "helper_disabled") {
|
|
220
235
|
return {
|
|
@@ -236,6 +251,21 @@ export const apiFetchTool = defineTool({
|
|
|
236
251
|
},
|
|
237
252
|
};
|
|
238
253
|
}
|
|
254
|
+
if (outcome.reason === "oauth_token_missing") {
|
|
255
|
+
return {
|
|
256
|
+
content: [
|
|
257
|
+
{
|
|
258
|
+
type: "text",
|
|
259
|
+
text: `🔑 ${guide.shortName}: ${outcome.message}`,
|
|
260
|
+
},
|
|
261
|
+
],
|
|
262
|
+
details: {
|
|
263
|
+
error: "oauth_token_missing",
|
|
264
|
+
domain,
|
|
265
|
+
operation,
|
|
266
|
+
},
|
|
267
|
+
};
|
|
268
|
+
}
|
|
239
269
|
const text =
|
|
240
270
|
`🔑 ${guide.shortName} requires a secret not yet provisioned: ` +
|
|
241
271
|
`${outcome.missing.join(", ")}.\n` +
|
|
@@ -266,8 +296,8 @@ export const apiFetchTool = defineTool({
|
|
|
266
296
|
text += `\n⚠ gatherAll ignored — ${operation} is not paginated (via: restGet).`;
|
|
267
297
|
}
|
|
268
298
|
if (authFooter) text += `\n${authFooter}`;
|
|
269
|
-
|
|
270
|
-
|
|
299
|
+
if (r.cached === true)
|
|
300
|
+
text += `\n⏱ cache-served (hit or 304-validated) — pass fresh: true to force a full refetch`;
|
|
271
301
|
return {
|
|
272
302
|
content: [{ type: "text", text }],
|
|
273
303
|
details: {
|
|
@@ -276,6 +306,7 @@ export const apiFetchTool = defineTool({
|
|
|
276
306
|
shortName: guide.shortName,
|
|
277
307
|
via: "restGet",
|
|
278
308
|
request: { method: "GET", url: r.url, params: r.params },
|
|
309
|
+
...(r.cached === true ? { cached: true } : {}),
|
|
279
310
|
// Output-channel audit: drop response headers that echo a known
|
|
280
311
|
// secret value (an auth-bearing server must not leak the key).
|
|
281
312
|
headers: scrubSecretHeaders(r.headers, outcome.authOpts.secretValues),
|
|
@@ -306,8 +337,6 @@ export const apiFetchTool = defineTool({
|
|
|
306
337
|
r.failedItems,
|
|
307
338
|
);
|
|
308
339
|
if (authFooter) text += `\n${authFooter}`;
|
|
309
|
-
const staleNote = staleSchemaLine(guide);
|
|
310
|
-
if (staleNote) text += `\n${staleNote}`;
|
|
311
340
|
return {
|
|
312
341
|
content: [{ type: "text", text }],
|
|
313
342
|
details: {
|
|
@@ -388,8 +417,6 @@ export const apiFetchTool = defineTool({
|
|
|
388
417
|
// Formatting helpers
|
|
389
418
|
// ═══════════════════════════════════════════════════════════════════
|
|
390
419
|
|
|
391
|
-
const INLINE_LIMIT = 4000;
|
|
392
|
-
|
|
393
420
|
/**
|
|
394
421
|
* Format a request URL line for result transparency.
|
|
395
422
|
*/
|
package/tools/api-guide.ts
CHANGED
|
@@ -19,14 +19,14 @@ import {
|
|
|
19
19
|
findGuidesByDomain,
|
|
20
20
|
getCatalogText,
|
|
21
21
|
} from "../core/guide-store.js";
|
|
22
|
+
import { TODAY } from "../core/parse-api-guide.js";
|
|
22
23
|
import {
|
|
23
24
|
formatGuideListings,
|
|
24
25
|
selectGuideByShortName,
|
|
25
26
|
shortNameErrorText,
|
|
26
|
-
|
|
27
|
-
staleSchemaLine,
|
|
28
|
-
} from "../core/parse-api-guide.js";
|
|
27
|
+
} from "../core/guide-catalog.js";
|
|
29
28
|
import { authStatusLine, canonicalStoreDomain } from "../core/auth.js";
|
|
29
|
+
import { INLINE_LIMIT } from "../core/response-spill.js";
|
|
30
30
|
import type { ApiGuide } from "../core/api-guide-types.js";
|
|
31
31
|
|
|
32
32
|
export const apiGuideTool = defineTool({
|
|
@@ -176,8 +176,6 @@ function renderGuideDetail(
|
|
|
176
176
|
lines.push(` Domains: ${guide.domains?.join(", ") ?? "—"}`);
|
|
177
177
|
lines.push(` Host: ${guide.apiHost}`);
|
|
178
178
|
lines.push(` Verified: ${guide.verified} · Updated: ${guide.updated}`);
|
|
179
|
-
const stale = staleSchemaLine(guide);
|
|
180
|
-
if (stale) lines.push(stale);
|
|
181
179
|
if (guide.docs) lines.push(` Docs: ${guide.docs}`);
|
|
182
180
|
lines.push(` Auth: ${guide.auth.kind}`);
|
|
183
181
|
// Auth status footer — shared with api-fetch. Metadata only (names,
|
|
@@ -216,6 +214,7 @@ function renderGuideDetail(
|
|
|
216
214
|
if (spec.required) parts.push("required");
|
|
217
215
|
if (spec.default !== undefined)
|
|
218
216
|
parts.push(`default ${JSON.stringify(spec.default)}`);
|
|
217
|
+
if (spec.listStyle) parts.push(`(listStyle: ${spec.listStyle})`);
|
|
219
218
|
return parts.join(" ");
|
|
220
219
|
});
|
|
221
220
|
lines.push(` params: ${rendered.join(", ")}`);
|
|
@@ -238,8 +237,19 @@ function renderGuideDetail(
|
|
|
238
237
|
if (pagCfg) {
|
|
239
238
|
const bits: string[] = [pagCfg.style];
|
|
240
239
|
if (pagCfg.pageParam) bits.push(pagCfg.pageParam);
|
|
241
|
-
if (pagCfg.pageSizeParam)
|
|
242
|
-
|
|
240
|
+
if (pagCfg.pageSizeParam) {
|
|
241
|
+
// offset-limit/page: the executor fabricates 50 when nothing
|
|
242
|
+
// resolves (and advances row offsets by it) — rendering the
|
|
243
|
+
// fallback is honest there. Cursor's terminal fallback is
|
|
244
|
+
// omit (server default applies) — only render a size that
|
|
245
|
+
// will actually be sent.
|
|
246
|
+
const renderedSize =
|
|
247
|
+
pagCfg.style === "cursor" && pagCfg.pageSize === undefined
|
|
248
|
+
? undefined
|
|
249
|
+
: String(pagCfg.pageSize ?? 50);
|
|
250
|
+
if (renderedSize !== undefined)
|
|
251
|
+
bits.push(`${pagCfg.pageSizeParam}=${renderedSize}`);
|
|
252
|
+
}
|
|
243
253
|
if (pagCfg.base !== undefined) bits.push(`base=${pagCfg.base}`);
|
|
244
254
|
lines.push(` pagination: ${bits.join(" ")}`);
|
|
245
255
|
}
|
|
@@ -256,15 +266,14 @@ function renderGuideDetail(
|
|
|
256
266
|
// budget api-fetch uses (INLINE_LIMIT) so a typical guide fits whole;
|
|
257
267
|
// longer prose spills with a pointer rather than flooding context.
|
|
258
268
|
if (guide.content) {
|
|
259
|
-
const PROSE_LIMIT = 4000;
|
|
260
269
|
const prose = guide.content.trim();
|
|
261
270
|
lines.push("");
|
|
262
271
|
lines.push("— Guide notes —");
|
|
263
|
-
if (prose.length <=
|
|
272
|
+
if (prose.length <= INLINE_LIMIT) {
|
|
264
273
|
lines.push(prose);
|
|
265
274
|
} else {
|
|
266
|
-
lines.push(prose.slice(0,
|
|
267
|
-
lines.push(`… (${prose.length -
|
|
275
|
+
lines.push(prose.slice(0, INLINE_LIMIT));
|
|
276
|
+
lines.push(`… (${prose.length - INLINE_LIMIT} more chars omitted)`);
|
|
268
277
|
}
|
|
269
278
|
}
|
|
270
279
|
|