pi-lean-host 0.2.1 → 0.3.1
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 +62 -89
- package/core/api-guide-types.ts +9 -0
- package/core/api-toggle.ts +23 -1
- package/core/delete-command.ts +190 -0
- package/core/guide-picker.ts +104 -0
- package/core/helpers-command.ts +2 -4
- package/core/helpers.ts +28 -0
- package/core/parse-api-guide.ts +451 -24
- package/core/resolve-op.ts +222 -0
- package/core/transport.ts +1 -1
- package/core/verify-command.ts +399 -0
- package/package.json +5 -1
- package/tools/api-fetch.ts +149 -229
- package/tools/api-guide.ts +33 -38
- package/tools/api-learn.ts +432 -131
- package/tools/api-probe.ts +146 -36
package/tools/api-probe.ts
CHANGED
|
@@ -31,11 +31,7 @@ import {
|
|
|
31
31
|
resolveSecretQueryParams,
|
|
32
32
|
scrubSecretValues,
|
|
33
33
|
} from "../core/auth.js";
|
|
34
|
-
import {
|
|
35
|
-
listDomains,
|
|
36
|
-
listNames,
|
|
37
|
-
provisionedDomainsSuffix,
|
|
38
|
-
} from "../core/secrets-store.js";
|
|
34
|
+
import { listDomains, listNames } from "../core/secrets-store.js";
|
|
39
35
|
import { findGuidesByDomain } from "../core/guide-store.js";
|
|
40
36
|
import { isApiLearnEnabled } from "../core/api-toggle.js";
|
|
41
37
|
import { appendFooter, contentText } from "./utils.js";
|
|
@@ -87,6 +83,7 @@ export interface ProbeOptions {
|
|
|
87
83
|
* Values resolve from the secrets store and never enter the transcript.
|
|
88
84
|
*/
|
|
89
85
|
auth?: {
|
|
86
|
+
/** Maps header name → secret name, same direction as the guide schema. */
|
|
90
87
|
secretRefs?: Record<string, string>;
|
|
91
88
|
secretQueryRefs?: Record<string, string>;
|
|
92
89
|
/**
|
|
@@ -96,7 +93,7 @@ export interface ProbeOptions {
|
|
|
96
93
|
*/
|
|
97
94
|
headerPrefixes?: Record<string, string>;
|
|
98
95
|
};
|
|
99
|
-
/** Domain for secrets-store lookups; defaults to apiHost's hostname. */
|
|
96
|
+
/** Domain for secrets-store lookups; defaults to apiHost's hostname (or its provisioned parent domain). */
|
|
100
97
|
domain?: string;
|
|
101
98
|
}
|
|
102
99
|
|
|
@@ -224,6 +221,9 @@ interface ProbeAuthCtx {
|
|
|
224
221
|
secretQueryParamNames: Set<string>;
|
|
225
222
|
secretValues: string[];
|
|
226
223
|
missingNames: string[];
|
|
224
|
+
/** headerPrefixes declared but no secretRefs to apply them to — the
|
|
225
|
+
* prefixes would be silently dropped. Surfaced as a note, not an error. */
|
|
226
|
+
misconfiguredPrefixes: boolean;
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
/**
|
|
@@ -237,6 +237,16 @@ function resolveProbeAuth(
|
|
|
237
237
|
auth: NonNullable<ProbeOptions["auth"]> | undefined,
|
|
238
238
|
domain: string,
|
|
239
239
|
): ProbeAuthCtx {
|
|
240
|
+
// Computed before the hasRefs check so the flag survives both paths: a
|
|
241
|
+
// headerPrefixes-only block (hasRefs false → early return) and a
|
|
242
|
+
// secretQueryRefs block with prefixes but no secretRefs (hasRefs true →
|
|
243
|
+
// normal path, where resolveSecretHeaders would silently drop the
|
|
244
|
+
// prefixes). Query secrets never take prefixes, so secretQueryRefs is
|
|
245
|
+
// deliberately excluded from the flag.
|
|
246
|
+
const misconfiguredPrefixes =
|
|
247
|
+
!!auth &&
|
|
248
|
+
Object.keys(auth.headerPrefixes ?? {}).length > 0 &&
|
|
249
|
+
Object.keys(auth.secretRefs ?? {}).length === 0;
|
|
240
250
|
const hasRefs =
|
|
241
251
|
!!auth &&
|
|
242
252
|
(Object.keys(auth.secretRefs ?? {}).length > 0 ||
|
|
@@ -250,6 +260,7 @@ function resolveProbeAuth(
|
|
|
250
260
|
secretQueryParamNames: new Set(),
|
|
251
261
|
secretValues: [],
|
|
252
262
|
missingNames: [],
|
|
263
|
+
misconfiguredPrefixes,
|
|
253
264
|
};
|
|
254
265
|
}
|
|
255
266
|
const headerRes = resolveSecretHeaders(
|
|
@@ -286,16 +297,78 @@ function resolveProbeAuth(
|
|
|
286
297
|
...queryRes.absentRequired,
|
|
287
298
|
...queryRes.absentOptional,
|
|
288
299
|
],
|
|
300
|
+
misconfiguredPrefixes,
|
|
289
301
|
};
|
|
290
302
|
}
|
|
291
303
|
|
|
292
|
-
/**
|
|
304
|
+
/** Root-cause note for a headerPrefixes-only auth block (no secretRefs to
|
|
305
|
+
* apply the prefixes to). `:`/`;` internal separators keep the combined
|
|
306
|
+
* error note from stacking three ` — ` joins when it composes with the
|
|
307
|
+
* 401/403 wording. */
|
|
308
|
+
const MISCONFIGURED_PREFIXES_NOTE =
|
|
309
|
+
"headerPrefixes ignored: no secretRefs to apply them to; put the secret name in auth.secretRefs";
|
|
310
|
+
|
|
311
|
+
/** Extracts the server's own human-readable reason from a 403 body — the
|
|
312
|
+
* note says what the API actually means instead of a synthesized
|
|
313
|
+
* classification. A 403 is "authenticated but forbidden", not a
|
|
314
|
+
* header/secret problem, so surfacing the server's words beats both the
|
|
315
|
+
* bare status and a "verify header" false signal. Returns undefined when
|
|
316
|
+
* the body isn't JSON or carries no message field; the caller falls back
|
|
317
|
+
* to the bare status.
|
|
318
|
+
* ponytail: secret-scrub invariant — this MUST parse the scrubbed `raw`
|
|
319
|
+
* slice, never the raw `res.body`, or a key echoed in the body would leak
|
|
320
|
+
* into agent context. */
|
|
321
|
+
function serverMessage(raw: string): string | undefined {
|
|
322
|
+
let data: unknown;
|
|
323
|
+
try {
|
|
324
|
+
data = JSON.parse(raw);
|
|
325
|
+
} catch {
|
|
326
|
+
return undefined;
|
|
327
|
+
}
|
|
328
|
+
if (!isObj(data)) return undefined;
|
|
329
|
+
const d = data as Record<string, unknown>;
|
|
330
|
+
const status = isObj(d["status"])
|
|
331
|
+
? (d["status"] as Record<string, unknown>)
|
|
332
|
+
: undefined;
|
|
333
|
+
const candidates: unknown[] = [
|
|
334
|
+
d["message"],
|
|
335
|
+
d["error"],
|
|
336
|
+
d["error_message"],
|
|
337
|
+
d["detail"],
|
|
338
|
+
status?.["error_message"],
|
|
339
|
+
];
|
|
340
|
+
const msg = candidates.find(
|
|
341
|
+
(c) => typeof c === "string" && c.trim().length > 0,
|
|
342
|
+
);
|
|
343
|
+
return typeof msg === "string" ? msg.trim().slice(0, 200) : undefined;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** First missing secret name as a one-line note (names only, never values).
|
|
347
|
+
* Prescriptive: names the other provisioned domains and tells the author to
|
|
348
|
+
* pass `domain:` — the probe's domain is inferred from apiHost, so a miss is
|
|
349
|
+
* usually a domain-mismatch, not a missing secret. */
|
|
293
350
|
function missNote(authCtx: ProbeAuthCtx, domain: string): string {
|
|
294
351
|
if (authCtx.missingNames.length === 0) return "";
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
352
|
+
const others = listDomains().filter((d) => d !== domain);
|
|
353
|
+
const tail =
|
|
354
|
+
others.length > 0
|
|
355
|
+
? ` — provisioned domains: ${others.join(", ")}; pass domain: <one> to use its secret`
|
|
356
|
+
: "";
|
|
357
|
+
return `secret "${authCtx.missingNames[0]}" not found in store for domain "${domain}"${tail}`;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** Resolve the secrets-store domain for a probe: the apiHost hostname when it
|
|
361
|
+
* is itself provisioned, else the longest provisioned parent domain
|
|
362
|
+
* (pro-api.coinmarketcap.com → coinmarketcap.com), else the hostname as-is.
|
|
363
|
+
* ponytail: parent-suffix match against the store, not a public-suffix list —
|
|
364
|
+
* no dep, and the store is the source of truth for where secrets live. */
|
|
365
|
+
export function resolveProbeStoreDomain(hostname: string): string {
|
|
366
|
+
const domains = listDomains();
|
|
367
|
+
if (domains.includes(hostname)) return hostname;
|
|
368
|
+
const parent = domains
|
|
369
|
+
.filter((d) => hostname.endsWith(`.${d}`))
|
|
370
|
+
.sort((a, b) => b.length - a.length)[0];
|
|
371
|
+
return parent ?? hostname;
|
|
299
372
|
}
|
|
300
373
|
|
|
301
374
|
/** Hostname of an apiHost URL (falls back to the raw string). */
|
|
@@ -326,7 +399,7 @@ export async function probe(
|
|
|
326
399
|
): Promise<ProbeResult> {
|
|
327
400
|
const accept = opts.accept ?? "application/json";
|
|
328
401
|
const walkVersions = opts.walkVersions ?? true;
|
|
329
|
-
const domain = opts.domain ?? hostnameOf(apiHost);
|
|
402
|
+
const domain = opts.domain ?? resolveProbeStoreDomain(hostnameOf(apiHost));
|
|
330
403
|
const authCtx = resolveProbeAuth(opts.auth, domain);
|
|
331
404
|
|
|
332
405
|
// Base case only carries the apiHost version prefix; a walk-hit draft
|
|
@@ -457,18 +530,36 @@ async function fetchOne(
|
|
|
457
530
|
);
|
|
458
531
|
|
|
459
532
|
if (res.status >= 400) {
|
|
460
|
-
|
|
533
|
+
// 401 or 403 — both mean the request was rejected on auth grounds
|
|
534
|
+
// (gated endpoint, or bad/missing credentials), distinct from other
|
|
535
|
+
// 4xx/5xx statuses. The name is deliberate: it covers 403 too.
|
|
536
|
+
const isAuthError = res.status === 401 || res.status === 403;
|
|
461
537
|
const miss = missNote(authCtx, domain);
|
|
462
538
|
let note: string;
|
|
463
|
-
if (authCtx.hasAuthBlock) {
|
|
464
|
-
//
|
|
465
|
-
//
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
note = is401
|
|
470
|
-
? `${res.status} — requires authentication? (guide is auth:none)`
|
|
539
|
+
if (!authCtx.hasAuthBlock) {
|
|
540
|
+
// No auth injected — a 401/403 means the endpoint is gated and the
|
|
541
|
+
// probe sent nothing. Pre-guide authoring tool: no guide in scope,
|
|
542
|
+
// so no stale "(guide is auth:none)" wording.
|
|
543
|
+
note = isAuthError
|
|
544
|
+
? `${res.status} — endpoint requires auth; configure auth injection (auth.secretRefs)`
|
|
471
545
|
: `${res.status}`;
|
|
546
|
+
} else if (miss) {
|
|
547
|
+
// Auth injected but a required secret was missing — the miss names it.
|
|
548
|
+
note = `${res.status}${isAuthError ? " — auth rejected;" : ""} ${miss}`;
|
|
549
|
+
} else {
|
|
550
|
+
// Auth injected, nothing missing. A 403 means authenticated but
|
|
551
|
+
// forbidden — "verify header name" is a 401 signal, not a 403 one.
|
|
552
|
+
// Surface the server's own reason (from the scrubbed `raw`, never
|
|
553
|
+
// `res.body`) when we can parse it; fall back to the bare status.
|
|
554
|
+
const msg = res.status === 403 ? serverMessage(raw) : undefined;
|
|
555
|
+
note = msg
|
|
556
|
+
? `${res.status} — ${msg} (auth configured correctly; not a header/secret problem)`
|
|
557
|
+
: res.status === 401
|
|
558
|
+
? `${res.status} — auth injected but rejected; verify header name and secret value`
|
|
559
|
+
: `${res.status}`;
|
|
560
|
+
}
|
|
561
|
+
if (authCtx.misconfiguredPrefixes) {
|
|
562
|
+
note += ` — ${MISCONFIGURED_PREFIXES_NOTE}`;
|
|
472
563
|
}
|
|
473
564
|
return {
|
|
474
565
|
url,
|
|
@@ -501,15 +592,20 @@ async function fetchOne(
|
|
|
501
592
|
|
|
502
593
|
const shape = summarize(data);
|
|
503
594
|
const miss = missNote(authCtx, domain);
|
|
595
|
+
const draft = emitDraft(path, params, shape, prefix);
|
|
596
|
+
let note: string | undefined = miss;
|
|
597
|
+
if (authCtx.misconfiguredPrefixes) {
|
|
598
|
+
note = [note, MISCONFIGURED_PREFIXES_NOTE].filter(Boolean).join(" — ");
|
|
599
|
+
}
|
|
504
600
|
return {
|
|
505
601
|
url,
|
|
506
602
|
finalUrl,
|
|
507
603
|
status: res.status,
|
|
508
604
|
ok: true,
|
|
509
605
|
shape,
|
|
510
|
-
draft
|
|
606
|
+
draft,
|
|
511
607
|
raw,
|
|
512
|
-
...(
|
|
608
|
+
...(note ? { note } : {}),
|
|
513
609
|
};
|
|
514
610
|
}
|
|
515
611
|
|
|
@@ -571,23 +667,33 @@ export function emitDraft(
|
|
|
571
667
|
lines.push(
|
|
572
668
|
" # unverified — pagination params are guessed from response keys; confirm the API accepts them",
|
|
573
669
|
);
|
|
574
|
-
const
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
670
|
+
const markers = shape.paginationMarkers;
|
|
671
|
+
const isPageStyle = markers.includes("page") || markers.includes("per_page");
|
|
672
|
+
const style = isPageStyle ? "page" : "offset-limit";
|
|
673
|
+
// `start` marker ⇒ 1-based row offset (CMC, GBIF) — emit base: 1 so the
|
|
674
|
+
// first page starts at 1, not the 0 default. `offset` ⇒ 0-based, no base.
|
|
675
|
+
let pageParam: string;
|
|
676
|
+
if (isPageStyle) pageParam = "page";
|
|
677
|
+
else if (markers.includes("start")) pageParam = "start";
|
|
678
|
+
else pageParam = "offset";
|
|
679
|
+
const pageSizeParam = isPageStyle ? "per_page" : "limit";
|
|
680
|
+
if (style === "offset-limit") {
|
|
681
|
+
lines.push(
|
|
682
|
+
" # offset-limit advances the page param by pageSize each page (row-offset semantics); base seeds the start (1 for `start` params, 0 for `offset`)",
|
|
683
|
+
);
|
|
684
|
+
}
|
|
581
685
|
lines.push(" pagination:");
|
|
582
686
|
lines.push(` style: ${style}`);
|
|
583
687
|
lines.push(` itemsPath: ${shape.suggestedItemsPath}`);
|
|
584
688
|
lines.push(` pageParam: ${pageParam}`);
|
|
585
689
|
lines.push(` pageSizeParam: ${pageSizeParam}`);
|
|
586
690
|
lines.push(" pageSize: 30");
|
|
691
|
+
if (pageParam === "start") lines.push(" base: 1");
|
|
587
692
|
}
|
|
588
693
|
if (shape.suggestedVia === "restGet" && shape.suggestedItemsPath !== "") {
|
|
589
694
|
lines.push(
|
|
590
|
-
" # array response with no pagination markers — if the API documents paging, prefer paginate
|
|
695
|
+
" # array response with no pagination markers — if the API documents paging, prefer paginate:",
|
|
696
|
+
" # offset-limit advances the page param by pageSize each page (use base: 1 for 1-based `start` APIs like CMC); page increments the page param by 1.",
|
|
591
697
|
);
|
|
592
698
|
}
|
|
593
699
|
if (queryParamKeys.length > 0) {
|
|
@@ -614,7 +720,6 @@ function suggestName(path: string): string {
|
|
|
614
720
|
.replace(/[^a-zA-Z0-9]/g, "");
|
|
615
721
|
}
|
|
616
722
|
|
|
617
|
-
// ═══════════════════════════════════════════════════════════════════
|
|
618
723
|
// Tool definition
|
|
619
724
|
// ═══════════════════════════════════════════════════════════════════
|
|
620
725
|
|
|
@@ -637,7 +742,7 @@ export const apiProbeTool = defineTool({
|
|
|
637
742
|
"version — then probe that version explicitly. Do not default the version from " +
|
|
638
743
|
"memory: a stale version that still returns 200 is not detected as old (the " +
|
|
639
744
|
"backward walk only fires on 404). The probe recovers an over-claimed version " +
|
|
640
|
-
"but cannot detect a stale-but-working one.",
|
|
745
|
+
"but cannot detect a stale-but-working one. ",
|
|
641
746
|
|
|
642
747
|
parameters: Type.Object({
|
|
643
748
|
apiHost: Type.Optional(
|
|
@@ -678,8 +783,10 @@ export const apiProbeTool = defineTool({
|
|
|
678
783
|
},
|
|
679
784
|
{
|
|
680
785
|
description:
|
|
681
|
-
"Store-backed auth injection for probing auth-gated endpoints (authoring loop).
|
|
786
|
+
"Store-backed auth injection for probing auth-gated endpoints (authoring loop). Accepts only secretRefs, secretQueryRefs, and headerPrefixes. Values resolve from the secrets store and never enter the transcript; a store miss fetches unauthenticated and reports the miss in the note.",
|
|
682
787
|
// Tight: unknown keys (e.g. a stray `domain`) are rejected before execute runs.
|
|
788
|
+
// The description above names the allowed fields explicitly — keep it in
|
|
789
|
+
// sync when adding/renaming a field here, or the prose lies to agents.
|
|
683
790
|
additionalProperties: false,
|
|
684
791
|
},
|
|
685
792
|
),
|
|
@@ -687,7 +794,7 @@ export const apiProbeTool = defineTool({
|
|
|
687
794
|
domain: Type.Optional(
|
|
688
795
|
Type.String({
|
|
689
796
|
description:
|
|
690
|
-
"Domain for secrets-store lookups; defaults to apiHost's hostname.",
|
|
797
|
+
"Domain for secrets-store lookups; defaults to apiHost's hostname (or its provisioned parent domain).",
|
|
691
798
|
}),
|
|
692
799
|
),
|
|
693
800
|
listSecrets: Type.Optional(
|
|
@@ -711,6 +818,7 @@ export const apiProbeTool = defineTool({
|
|
|
711
818
|
auth?: {
|
|
712
819
|
secretRefs?: Record<string, string>;
|
|
713
820
|
secretQueryRefs?: Record<string, string>;
|
|
821
|
+
headerPrefixes?: Record<string, string>;
|
|
714
822
|
};
|
|
715
823
|
domain?: string;
|
|
716
824
|
listSecrets?: boolean;
|
|
@@ -740,7 +848,9 @@ export const apiProbeTool = defineTool({
|
|
|
740
848
|
const unscoped = domain ? undefined : unscopedStoreDomains();
|
|
741
849
|
const blocks: string[] = [];
|
|
742
850
|
if (unscoped !== undefined) blocks.push(formatUnscopedDomains(unscoped));
|
|
743
|
-
const target =
|
|
851
|
+
const target =
|
|
852
|
+
domain ??
|
|
853
|
+
(apiHost ? resolveProbeStoreDomain(hostnameOf(apiHost)) : undefined);
|
|
744
854
|
if (target !== undefined) {
|
|
745
855
|
const secrets = listDomainSecrets(target);
|
|
746
856
|
blocks.push(formatSecretsResult(secrets));
|