pi-lean-host 0.3.1 โ†’ 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.
@@ -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
- formatGuideListings,
35
- staleSchemaLine,
36
- } from "../core/parse-api-guide.js";
37
- import { spillResponse, formatSpillNotice } from "../core/response-spill.js";
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
 
@@ -62,7 +63,8 @@ export const apiFetchTool = defineTool({
62
63
  description:
63
64
  "Fetch structured data from a REST API using a recipe-based guide. " +
64
65
  "Requires an API guide for the target domain. " +
65
- "Call api-guide({domain}) to see available operations, or api-learn({domain, recipeFile}) to author a new guide.",
66
+ "Call api-guide({domain}) to see available operations, or api-learn({domain, dir}) to author a new guide.\n\n" +
67
+ "Authoring: pass params per call to test an op (verify.json feeds /api verify, not api-fetch).",
66
68
 
67
69
  parameters: Type.Object({
68
70
  domain: Type.String({
@@ -84,6 +86,12 @@ export const apiFetchTool = defineTool({
84
86
  "When true, paginate to gather all items up to the guide's gatherAllMax ceiling.",
85
87
  }),
86
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
+ ),
87
95
  }),
88
96
 
89
97
  async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
@@ -95,22 +103,28 @@ export const apiFetchTool = defineTool({
95
103
  gatherAll?: boolean;
96
104
  };
97
105
 
98
- // `gatherAll` may be passed at the tool's top level (the documented
99
- // position) or nested inside `params` (the natural place, since `params`
100
- // holds the operation arguments). Resolve both, preferring the top level,
101
- // and strip `gatherAll` out of the forwarded params so it can't leak onto
102
- // a `passthrough` query string or reach a domain helper. This must happen
103
- // 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.
104
112
  const rawParams = (params as Record<string, unknown>)["params"] as
105
113
  | Record<string, unknown>
106
114
  | undefined;
107
115
  const gatherAll =
108
116
  ((params as Record<string, unknown>)["gatherAll"] as boolean | undefined) ??
109
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);
110
121
  const userParams = rawParams ? { ...rawParams } : undefined;
111
122
  if (userParams && "gatherAll" in userParams) {
112
123
  delete userParams.gatherAll;
113
124
  }
125
+ if (userParams && "fresh" in userParams) {
126
+ delete userParams.fresh;
127
+ }
114
128
 
115
129
  // 1. Resolve guides by domain โ€” a domain may be claimed by more than
116
130
  // one guide (one domain, multiple APIs). Search every match for the
@@ -182,6 +196,7 @@ export const apiFetchTool = defineTool({
182
196
  const execOpts: ResolveOpOptions = { skipSsrfGuard: _bypassUrlSafety };
183
197
  if (userParams) execOpts.userParams = userParams;
184
198
  if (gatherAll !== undefined) execOpts.gatherAll = gatherAll;
199
+ if (fresh !== undefined) execOpts.fresh = fresh;
185
200
  outcome = await resolveOpForExecution(guide, op, helperDirName, execOpts);
186
201
  } catch (err) {
187
202
  if (err instanceof HelperError) {
@@ -212,8 +227,9 @@ export const apiFetchTool = defineTool({
212
227
  };
213
228
  }
214
229
 
215
- // Non-run outcomes: a session-disabled helper or a fail-closed missing
216
- // `requires` secret. Both return before any request is made.
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.
217
233
  if (!outcome.ok) {
218
234
  if (outcome.reason === "helper_disabled") {
219
235
  return {
@@ -235,6 +251,21 @@ export const apiFetchTool = defineTool({
235
251
  },
236
252
  };
237
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
+ }
238
269
  const text =
239
270
  `๐Ÿ”‘ ${guide.shortName} requires a secret not yet provisioned: ` +
240
271
  `${outcome.missing.join(", ")}.\n` +
@@ -265,8 +296,8 @@ export const apiFetchTool = defineTool({
265
296
  text += `\nโš  gatherAll ignored โ€” ${operation} is not paginated (via: restGet).`;
266
297
  }
267
298
  if (authFooter) text += `\n${authFooter}`;
268
- const staleNote = staleSchemaLine(guide);
269
- if (staleNote) text += `\n${staleNote}`;
299
+ if (r.cached === true)
300
+ text += `\nโฑ cache-served (hit or 304-validated) โ€” pass fresh: true to force a full refetch`;
270
301
  return {
271
302
  content: [{ type: "text", text }],
272
303
  details: {
@@ -275,6 +306,7 @@ export const apiFetchTool = defineTool({
275
306
  shortName: guide.shortName,
276
307
  via: "restGet",
277
308
  request: { method: "GET", url: r.url, params: r.params },
309
+ ...(r.cached === true ? { cached: true } : {}),
278
310
  // Output-channel audit: drop response headers that echo a known
279
311
  // secret value (an auth-bearing server must not leak the key).
280
312
  headers: scrubSecretHeaders(r.headers, outcome.authOpts.secretValues),
@@ -305,8 +337,6 @@ export const apiFetchTool = defineTool({
305
337
  r.failedItems,
306
338
  );
307
339
  if (authFooter) text += `\n${authFooter}`;
308
- const staleNote = staleSchemaLine(guide);
309
- if (staleNote) text += `\n${staleNote}`;
310
340
  return {
311
341
  content: [{ type: "text", text }],
312
342
  details: {
@@ -387,8 +417,6 @@ export const apiFetchTool = defineTool({
387
417
  // Formatting helpers
388
418
  // โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
389
419
 
390
- const INLINE_LIMIT = 4000;
391
-
392
420
  /**
393
421
  * Format a request URL line for result transparency.
394
422
  */
@@ -519,7 +547,7 @@ function formatAmbiguousOperation(
519
547
  `Call api-guide({domain: "${domain}", guide: "${first.guide.shortName}"}) to read each guide's full recipe,`,
520
548
  );
521
549
  lines.push(
522
- `then re-author one guide via api-learn({domain: "${first.dirName}", recipeFile: โ€ฆ}) with the colliding operation renamed so the names no longer clash.`,
550
+ `then re-author one guide via api-learn({dir: โ€ฆ}) with the colliding operation renamed so the names no longer clash.`,
523
551
  );
524
552
  lines.push(`Note: api-learn rewrites a whole recipe, not a single operation.`);
525
553
  return lines.join("\n");
@@ -579,7 +607,7 @@ function formatHelperError(
579
607
  if (err.url) lines.push(formatRequestLine("GET", err.url));
580
608
  lines.push("");
581
609
  lines.push(
582
- `Call api-guide({domain: "${domain}"}) to review the guide, or api-learn({domain: "${domain}", recipeFile: โ€ฆ}) to fix it.`,
610
+ `Call api-guide({domain: "${domain}"}) to review the guide, or api-learn({domain: "${domain}", dir: โ€ฆ}) to fix it.`,
583
611
  );
584
612
  return lines.join("\n");
585
613
  }
@@ -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
- TODAY,
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({
@@ -77,7 +77,7 @@ export const apiGuideTool = defineTool({
77
77
  text:
78
78
  `No API guide for '${domain}'.` +
79
79
  (known.length > 0 ? ` Known guides: ${known.join(", ")}.` : "") +
80
- "\n\nCall api-guide with no params to list all guides, or api-learn({domain, recipeFile}) to author one.",
80
+ "\n\nCall api-guide with no params to list all guides, or api-learn({domain, dir}) to author one.",
81
81
  },
82
82
  ],
83
83
  details: {},
@@ -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
- bits.push(`${pagCfg.pageSizeParam}=${pagCfg.pageSize ?? 50}`);
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 <= PROSE_LIMIT) {
272
+ if (prose.length <= INLINE_LIMIT) {
264
273
  lines.push(prose);
265
274
  } else {
266
- lines.push(prose.slice(0, PROSE_LIMIT));
267
- lines.push(`โ€ฆ (${prose.length - PROSE_LIMIT} more chars omitted)`);
275
+ lines.push(prose.slice(0, INLINE_LIMIT));
276
+ lines.push(`โ€ฆ (${prose.length - INLINE_LIMIT} more chars omitted)`);
268
277
  }
269
278
  }
270
279