pi-lean-host 0.0.1 → 0.1.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 +777 -30
- package/core/api-guide-types.ts +244 -0
- package/core/api-toggle.ts +313 -0
- package/core/guide-loader.ts +80 -0
- package/core/guide-store.ts +125 -0
- package/core/helpers-command.ts +110 -0
- package/core/helpers.ts +801 -0
- package/core/local-helpers.ts +246 -0
- package/core/parse-api-guide.ts +1218 -0
- package/core/path-template.ts +50 -0
- package/core/portal-projection.ts +69 -0
- package/core/response-spill.ts +185 -0
- package/core/ssrf-guard.ts +99 -0
- package/core/transport.ts +451 -0
- package/core/verify-ship-manifest.ts +118 -0
- package/index.ts +46 -0
- package/package.json +42 -13
- package/tools/api-fetch.ts +578 -0
- package/tools/api-guide.ts +320 -0
- package/tools/api-learn.ts +348 -0
- package/tools/api-probe.ts +500 -0
- package/tools/index.ts +7 -0
- package/tools/utils.ts +34 -0
- package/AGENTS.md +0 -22
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* api-probe tool definition.
|
|
3
|
+
*
|
|
4
|
+
* Shape-discovery for the authoring loop: fetch an exploratory path against a
|
|
5
|
+
* domain's apiHost, summarize the JSON shape, suggest `via` / `itemsPath`,
|
|
6
|
+
* echo a representative record id, and emit a draft YAML op block to paste
|
|
7
|
+
* into `guide.md`. The author confirms — it never writes the guide.
|
|
8
|
+
*
|
|
9
|
+
* Pre-guide: call it with a bare URL + templated path. For an endpoint that
|
|
10
|
+
* already has a guide, get the `apiHost` from `api-guide({domain})` first.
|
|
11
|
+
*
|
|
12
|
+
* The suggestions (via/itemsPath/draft) are advisory — the operation must
|
|
13
|
+
* still be traceable to a real endpoint ("cite the source, don't invent
|
|
14
|
+
* endpoints"). This tool surfaces evidence, not authority.
|
|
15
|
+
*
|
|
16
|
+
* Reuses the same `transport.ts` pipeline as api-fetch (UA, charset, 429
|
|
17
|
+
* retry, ETag cache) — the sanctioned way to reach even WAF'd hosts.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { defineTool } from "@earendil-works/pi-coding-agent";
|
|
21
|
+
import { Type } from "@earendil-works/pi-ai";
|
|
22
|
+
import { Text } from "@earendil-works/pi-tui";
|
|
23
|
+
import { fetchUrl } from "../core/transport.js";
|
|
24
|
+
import {
|
|
25
|
+
extractPathTokens,
|
|
26
|
+
fillPathTemplate,
|
|
27
|
+
joinUrl,
|
|
28
|
+
} from "../core/path-template.js";
|
|
29
|
+
import { contentText, renderExpandedText } from "./utils.js";
|
|
30
|
+
|
|
31
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
32
|
+
// Types
|
|
33
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
34
|
+
|
|
35
|
+
export interface ShapeSummary {
|
|
36
|
+
topLevel: "object" | "array" | "number" | "string" | "boolean" | "null";
|
|
37
|
+
isArray: boolean;
|
|
38
|
+
/** Top-level keys when an object (capped at 20). */
|
|
39
|
+
keys: string[];
|
|
40
|
+
/** Length when the body (or the items array) is an array. */
|
|
41
|
+
arrayLen: number;
|
|
42
|
+
/** Suggested executor. `paginate` for any list; `restGet` for singles. */
|
|
43
|
+
suggestedVia: "restGet" | "paginate";
|
|
44
|
+
/** `itemsPath` for `paginate` — `$` for a bare top-level array. Empty for restGet. */
|
|
45
|
+
suggestedItemsPath: string;
|
|
46
|
+
/** Body keys that look like pagination signals. */
|
|
47
|
+
paginationMarkers: string[];
|
|
48
|
+
/** First record's id-ish field, for reuse as a detail-lookup path param. */
|
|
49
|
+
representativeId?: { field: string; value: string | number };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface ProbeResult {
|
|
53
|
+
url: string;
|
|
54
|
+
/** Final URL after redirects (equals `url` when no hop occurred). */
|
|
55
|
+
finalUrl: string;
|
|
56
|
+
status: number;
|
|
57
|
+
ok: boolean;
|
|
58
|
+
shape: ShapeSummary | null;
|
|
59
|
+
/** Draft YAML operation block (empty on non-2xx / non-JSON). */
|
|
60
|
+
draft: string;
|
|
61
|
+
/** Truncated raw body for eyeballing. */
|
|
62
|
+
raw: string;
|
|
63
|
+
/** Human note (auth-required, 404, version-prefix hit, non-JSON, …). */
|
|
64
|
+
note?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ProbeOptions {
|
|
68
|
+
/** Accept header (default application/json). */
|
|
69
|
+
accept?: string;
|
|
70
|
+
/** On 404, auto-try /v1/ and /v2/ prefixes. Default true. */
|
|
71
|
+
tryPrefixes?: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
75
|
+
// Pure shape logic — the non-trivial part, exported for testing
|
|
76
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
77
|
+
|
|
78
|
+
const PAGINATION_KEYS = new Set([
|
|
79
|
+
"offset",
|
|
80
|
+
"limit",
|
|
81
|
+
"start",
|
|
82
|
+
"endOfRecords",
|
|
83
|
+
"page",
|
|
84
|
+
"per_page",
|
|
85
|
+
"continue",
|
|
86
|
+
"cursor",
|
|
87
|
+
"next",
|
|
88
|
+
"nextLink",
|
|
89
|
+
"next_page_url",
|
|
90
|
+
"total_count",
|
|
91
|
+
"count",
|
|
92
|
+
"total",
|
|
93
|
+
"numFound",
|
|
94
|
+
]);
|
|
95
|
+
const ARRAY_VALUE_KEYS = new Set([
|
|
96
|
+
"results",
|
|
97
|
+
"items",
|
|
98
|
+
"search",
|
|
99
|
+
"docs",
|
|
100
|
+
"data",
|
|
101
|
+
"records",
|
|
102
|
+
"features",
|
|
103
|
+
"releases",
|
|
104
|
+
"commits",
|
|
105
|
+
"issues",
|
|
106
|
+
"events",
|
|
107
|
+
]);
|
|
108
|
+
const ID_FIELDS = [
|
|
109
|
+
"id",
|
|
110
|
+
"key",
|
|
111
|
+
"sha",
|
|
112
|
+
"usageKey",
|
|
113
|
+
"node_id",
|
|
114
|
+
"number",
|
|
115
|
+
"uuid",
|
|
116
|
+
"_id",
|
|
117
|
+
];
|
|
118
|
+
|
|
119
|
+
export function summarize(data: unknown): ShapeSummary {
|
|
120
|
+
const topLevel = topLevelOf(data);
|
|
121
|
+
const keys = isObj(data) ? Object.keys(data as object).slice(0, 20) : [];
|
|
122
|
+
const paginationMarkers = keys.filter((k) => PAGINATION_KEYS.has(k));
|
|
123
|
+
|
|
124
|
+
// Find the items array: either the body itself, or the first array-valued
|
|
125
|
+
// key (preferring known envelope keys, then any array key).
|
|
126
|
+
let itemsPath = "";
|
|
127
|
+
let items: unknown[] | null = null;
|
|
128
|
+
if (Array.isArray(data)) {
|
|
129
|
+
itemsPath = "$";
|
|
130
|
+
items = data;
|
|
131
|
+
} else if (isObj(data)) {
|
|
132
|
+
const entries = Object.entries(data as Record<string, unknown>);
|
|
133
|
+
const hit =
|
|
134
|
+
entries.find(([k, v]) => ARRAY_VALUE_KEYS.has(k) && Array.isArray(v)) ??
|
|
135
|
+
entries.find(([, v]) => Array.isArray(v));
|
|
136
|
+
if (hit) {
|
|
137
|
+
itemsPath = hit[0];
|
|
138
|
+
items = hit[1] as unknown[];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const arrayLen = items?.length ?? 0;
|
|
143
|
+
const suggestedVia = items !== null ? "paginate" : "restGet";
|
|
144
|
+
|
|
145
|
+
const summary: ShapeSummary = {
|
|
146
|
+
topLevel,
|
|
147
|
+
isArray: Array.isArray(data),
|
|
148
|
+
keys,
|
|
149
|
+
arrayLen,
|
|
150
|
+
suggestedVia,
|
|
151
|
+
suggestedItemsPath: suggestedVia === "paginate" ? itemsPath : "",
|
|
152
|
+
paginationMarkers,
|
|
153
|
+
};
|
|
154
|
+
const representativeId = pickRepresentativeId(items);
|
|
155
|
+
if (representativeId) summary.representativeId = representativeId;
|
|
156
|
+
return summary;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function topLevelOf(data: unknown): ShapeSummary["topLevel"] {
|
|
160
|
+
if (data === null) return "null";
|
|
161
|
+
if (Array.isArray(data)) return "array";
|
|
162
|
+
return typeof data as ShapeSummary["topLevel"];
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function isObj(data: unknown): data is object {
|
|
166
|
+
return typeof data === "object" && data !== null && !Array.isArray(data);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function pickRepresentativeId(
|
|
170
|
+
items: unknown[] | null,
|
|
171
|
+
): { field: string; value: string | number } | undefined {
|
|
172
|
+
const rec = items?.[0];
|
|
173
|
+
if (!isObj(rec)) return undefined;
|
|
174
|
+
const obj = rec as Record<string, unknown>;
|
|
175
|
+
for (const f of ID_FIELDS) {
|
|
176
|
+
const v = obj[f];
|
|
177
|
+
if (typeof v === "string" || typeof v === "number")
|
|
178
|
+
return { field: f, value: v };
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
184
|
+
// IO wrapper — fetches via the shared transport and formats the answer
|
|
185
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
186
|
+
|
|
187
|
+
export async function probe(
|
|
188
|
+
apiHost: string,
|
|
189
|
+
path: string,
|
|
190
|
+
params: Record<string, unknown> = {},
|
|
191
|
+
opts: ProbeOptions = {},
|
|
192
|
+
): Promise<ProbeResult> {
|
|
193
|
+
const accept = opts.accept ?? "application/json";
|
|
194
|
+
const tryPrefixes = opts.tryPrefixes ?? true;
|
|
195
|
+
|
|
196
|
+
const base = await fetchOne(apiHost, path, params, accept);
|
|
197
|
+
if (base.status === 404 && tryPrefixes && !/^\/v\d+\//.test(path)) {
|
|
198
|
+
for (const p of [`/v1${path}`, `/v2${path}`]) {
|
|
199
|
+
const tried = await fetchOne(apiHost, p, params, accept);
|
|
200
|
+
if (tried.status !== 404) {
|
|
201
|
+
tried.note = `404 on ${path}; /v*/ prefix hit → ${p}`;
|
|
202
|
+
return tried;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return base;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function fetchOne(
|
|
210
|
+
apiHost: string,
|
|
211
|
+
path: string,
|
|
212
|
+
params: Record<string, unknown>,
|
|
213
|
+
accept: string,
|
|
214
|
+
): Promise<ProbeResult> {
|
|
215
|
+
const url = buildUrl(apiHost, path, params);
|
|
216
|
+
const res = await fetchUrl(url, { headers: { accept }, fresh: true });
|
|
217
|
+
const raw = res.body.slice(0, 800);
|
|
218
|
+
const finalUrl = res.finalUrl ?? url;
|
|
219
|
+
|
|
220
|
+
if (res.status >= 400) {
|
|
221
|
+
const note =
|
|
222
|
+
res.status === 401 || res.status === 403
|
|
223
|
+
? `${res.status} — requires authentication? (guide is auth:none)`
|
|
224
|
+
: `${res.status}`;
|
|
225
|
+
return {
|
|
226
|
+
url,
|
|
227
|
+
finalUrl,
|
|
228
|
+
status: res.status,
|
|
229
|
+
ok: false,
|
|
230
|
+
shape: null,
|
|
231
|
+
draft: "",
|
|
232
|
+
raw,
|
|
233
|
+
note,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
let data: unknown;
|
|
238
|
+
try {
|
|
239
|
+
data = JSON.parse(res.body);
|
|
240
|
+
} catch {
|
|
241
|
+
return {
|
|
242
|
+
url,
|
|
243
|
+
finalUrl,
|
|
244
|
+
status: res.status,
|
|
245
|
+
ok: true,
|
|
246
|
+
shape: null,
|
|
247
|
+
draft: "",
|
|
248
|
+
raw,
|
|
249
|
+
note: "non-JSON body (set opts.accept for XML/HTML, or use a different path)",
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const shape = summarize(data);
|
|
254
|
+
return {
|
|
255
|
+
url,
|
|
256
|
+
finalUrl,
|
|
257
|
+
status: res.status,
|
|
258
|
+
ok: true,
|
|
259
|
+
shape,
|
|
260
|
+
draft: emitDraft(path, params, shape),
|
|
261
|
+
raw,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function buildUrl(
|
|
266
|
+
apiHost: string,
|
|
267
|
+
path: string,
|
|
268
|
+
params: Record<string, unknown>,
|
|
269
|
+
): string {
|
|
270
|
+
// Substitute {token} → params[token] BEFORE building the URL, so a
|
|
271
|
+
// templated path fetches real values instead of literal %7Bowner%7D.
|
|
272
|
+
// Missing tokens stay literal ({token}) — fine for probing an unfilled path.
|
|
273
|
+
const pathTokens = new Set(extractPathTokens(path));
|
|
274
|
+
const substituted = fillPathTemplate(path, params);
|
|
275
|
+
const qs = new URLSearchParams(
|
|
276
|
+
Object.fromEntries(
|
|
277
|
+
Object.entries(params)
|
|
278
|
+
.filter(([k, v]) => v !== undefined && !pathTokens.has(k))
|
|
279
|
+
.map(([k, v]) => [k, String(v)]),
|
|
280
|
+
),
|
|
281
|
+
).toString();
|
|
282
|
+
return joinUrl(apiHost, substituted, qs);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
286
|
+
// Draft YAML emission (suggests; the author confirms)
|
|
287
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
288
|
+
|
|
289
|
+
export function emitDraft(
|
|
290
|
+
path: string,
|
|
291
|
+
params: Record<string, unknown>,
|
|
292
|
+
shape: ShapeSummary,
|
|
293
|
+
): string {
|
|
294
|
+
const name = suggestName(path);
|
|
295
|
+
const pathTokens = extractPathTokens(path);
|
|
296
|
+
const queryParamKeys = Object.keys(params).filter(
|
|
297
|
+
(k) => !pathTokens.includes(k),
|
|
298
|
+
);
|
|
299
|
+
const lines: string[] = [
|
|
300
|
+
` - name: ${name}`,
|
|
301
|
+
` via: ${shape.suggestedVia}`,
|
|
302
|
+
` path: ${path}`,
|
|
303
|
+
` accept: json`,
|
|
304
|
+
];
|
|
305
|
+
|
|
306
|
+
if (shape.suggestedVia === "paginate") {
|
|
307
|
+
lines.push(
|
|
308
|
+
" # unverified — pagination params are guessed from response keys; confirm the API accepts them",
|
|
309
|
+
);
|
|
310
|
+
const style =
|
|
311
|
+
shape.paginationMarkers.includes("page") ||
|
|
312
|
+
shape.paginationMarkers.includes("per_page")
|
|
313
|
+
? "page"
|
|
314
|
+
: "offset-limit";
|
|
315
|
+
const pageParam = style === "page" ? "page" : "offset";
|
|
316
|
+
const pageSizeParam = style === "page" ? "per_page" : "limit";
|
|
317
|
+
lines.push(" pagination:");
|
|
318
|
+
lines.push(` style: ${style}`);
|
|
319
|
+
lines.push(` itemsPath: ${shape.suggestedItemsPath}`);
|
|
320
|
+
lines.push(` pageParam: ${pageParam}`);
|
|
321
|
+
lines.push(` pageSizeParam: ${pageSizeParam}`);
|
|
322
|
+
lines.push(" pageSize: 30");
|
|
323
|
+
}
|
|
324
|
+
if (queryParamKeys.length > 0) {
|
|
325
|
+
lines.push(" params:");
|
|
326
|
+
for (const k of queryParamKeys) {
|
|
327
|
+
lines.push(` ${k}:`);
|
|
328
|
+
lines.push(` description: TODO — ${k}.`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (shape.representativeId) {
|
|
332
|
+
lines.push(
|
|
333
|
+
` # representative id: ${shape.representativeId.field}=${shape.representativeId.value}`,
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
return lines.join("\n");
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function suggestName(path: string): string {
|
|
340
|
+
const segs = path.split("/").filter((s) => s && !s.startsWith("{"));
|
|
341
|
+
const last = segs[segs.length - 1] ?? "op";
|
|
342
|
+
// kebab/snake → camelCase placeholder; author renames.
|
|
343
|
+
return last
|
|
344
|
+
.replace(/[-_]([a-z])/g, (_, c) => c.toUpperCase())
|
|
345
|
+
.replace(/[^a-zA-Z0-9]/g, "");
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
349
|
+
// Tool definition
|
|
350
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
351
|
+
|
|
352
|
+
export const apiProbeTool = defineTool({
|
|
353
|
+
name: "api-probe",
|
|
354
|
+
label: "API Probe",
|
|
355
|
+
description:
|
|
356
|
+
"Discover the shape of a not-yet-guided API endpoint before authoring a guide. " +
|
|
357
|
+
"Fetches a templated path over the real transport, summarizes the JSON shape, " +
|
|
358
|
+
"suggests via/itemsPath/pagination, echoes a representative record id, and emits " +
|
|
359
|
+
"a draft YAML operation block to paste into a guide recipe. " +
|
|
360
|
+
"It only suggests — it never writes the guide, and the operation must still be " +
|
|
361
|
+
"traceable to your plan source. Pre-guide: pass apiHost + path. After a guide " +
|
|
362
|
+
"exists, use api-guide({domain}) to get apiHost, or api-fetch to execute.",
|
|
363
|
+
|
|
364
|
+
parameters: Type.Object({
|
|
365
|
+
apiHost: Type.String({
|
|
366
|
+
description:
|
|
367
|
+
"Base URL including version prefix, e.g. 'https://api.github.com'.",
|
|
368
|
+
}),
|
|
369
|
+
path: Type.String({
|
|
370
|
+
description:
|
|
371
|
+
"Templated path, e.g. '/repos/{owner}/{repo}/branches'. {token} placeholders are filled from params.",
|
|
372
|
+
}),
|
|
373
|
+
params: Type.Optional(
|
|
374
|
+
Type.Record(Type.String(), Type.Unknown(), {
|
|
375
|
+
description:
|
|
376
|
+
"Values for {token} path placeholders and query parameters. Path tokens are not re-declared as query params.",
|
|
377
|
+
}),
|
|
378
|
+
),
|
|
379
|
+
tryPrefixes: Type.Optional(
|
|
380
|
+
Type.Boolean({
|
|
381
|
+
description: "On 404, auto-try /v1/ and /v2/ prefixes. Default true.",
|
|
382
|
+
}),
|
|
383
|
+
),
|
|
384
|
+
}),
|
|
385
|
+
|
|
386
|
+
async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
|
|
387
|
+
const { apiHost, path, tryPrefixes } = params as {
|
|
388
|
+
apiHost: string;
|
|
389
|
+
path: string;
|
|
390
|
+
params?: Record<string, unknown>;
|
|
391
|
+
tryPrefixes?: boolean;
|
|
392
|
+
};
|
|
393
|
+
const userParams = (params as Record<string, unknown>)["params"] as
|
|
394
|
+
| Record<string, unknown>
|
|
395
|
+
| undefined;
|
|
396
|
+
|
|
397
|
+
try {
|
|
398
|
+
const result = await probe(apiHost, path, userParams ?? {}, {
|
|
399
|
+
...(tryPrefixes !== undefined ? { tryPrefixes } : {}),
|
|
400
|
+
});
|
|
401
|
+
return {
|
|
402
|
+
content: [{ type: "text", text: formatProbeResult(result) }],
|
|
403
|
+
details: {
|
|
404
|
+
url: result.url,
|
|
405
|
+
finalUrl: result.finalUrl,
|
|
406
|
+
status: result.status,
|
|
407
|
+
ok: result.ok,
|
|
408
|
+
shape: result.shape,
|
|
409
|
+
note: result.note,
|
|
410
|
+
draft: result.draft,
|
|
411
|
+
raw: result.raw,
|
|
412
|
+
},
|
|
413
|
+
};
|
|
414
|
+
} catch (err) {
|
|
415
|
+
return {
|
|
416
|
+
content: [
|
|
417
|
+
{
|
|
418
|
+
type: "text",
|
|
419
|
+
text: `api-probe failed for '${apiHost}${path}': ${
|
|
420
|
+
err instanceof Error ? err.message : String(err)
|
|
421
|
+
}`,
|
|
422
|
+
},
|
|
423
|
+
],
|
|
424
|
+
details: { error: "unexpected", message: String(err) },
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
},
|
|
428
|
+
|
|
429
|
+
renderCall(args, theme, _context) {
|
|
430
|
+
const parts: string[] = [theme.fg("toolTitle", theme.bold("api-probe "))];
|
|
431
|
+
if (args.apiHost) parts.push(theme.fg("accent", `"${args.apiHost}"`));
|
|
432
|
+
if (args.path) parts.push(theme.fg("dim", `› ${args.path}`));
|
|
433
|
+
return new Text(parts.join(" "), 0, 0);
|
|
434
|
+
},
|
|
435
|
+
|
|
436
|
+
renderResult(result, { expanded, isPartial }, theme, _context) {
|
|
437
|
+
if (isPartial) return new Text(theme.fg("warning", "Probing…"), 0, 0);
|
|
438
|
+
const d = result.details as Record<string, unknown> | undefined;
|
|
439
|
+
if (d?.error) {
|
|
440
|
+
return new Text(theme.fg("error", `⚠ ${contentText(result, "?")}`), 0, 0);
|
|
441
|
+
}
|
|
442
|
+
const status = d?.status as number | undefined;
|
|
443
|
+
const shape = d?.shape as ShapeSummary | null | undefined;
|
|
444
|
+
|
|
445
|
+
let text = theme.fg("accent", theme.bold("🔬 api-probe"));
|
|
446
|
+
text += ` — ${(d?.url as string) ?? "?"} · ${status ?? "?"}`;
|
|
447
|
+
if (shape) {
|
|
448
|
+
const items =
|
|
449
|
+
shape.suggestedVia === "paginate"
|
|
450
|
+
? ` · itemsPath: ${shape.suggestedItemsPath}`
|
|
451
|
+
: "";
|
|
452
|
+
text += `\n${theme.fg("dim", `${shape.suggestedVia}${items}`)}`;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const content = contentText(result);
|
|
456
|
+
if (expanded) {
|
|
457
|
+
text += "\n";
|
|
458
|
+
text = renderExpandedText(text, theme, content, 1000);
|
|
459
|
+
} else {
|
|
460
|
+
text += `\n${theme.fg("muted", `${content.length} chars (expand)`)}`;
|
|
461
|
+
}
|
|
462
|
+
return new Text(text, 0, 0);
|
|
463
|
+
},
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
467
|
+
// Result formatting
|
|
468
|
+
// ═══════════════════════════════════════════════════════════════════
|
|
469
|
+
|
|
470
|
+
function formatProbeResult(r: ProbeResult): string {
|
|
471
|
+
const lines: string[] = [];
|
|
472
|
+
lines.push(`🔬 api-probe — ${r.url}`);
|
|
473
|
+
lines.push(` status: ${r.status}`);
|
|
474
|
+
if (r.finalUrl !== r.url) lines.push(` final url: ${r.finalUrl}`);
|
|
475
|
+
if (r.note) lines.push(` note: ${r.note}`);
|
|
476
|
+
if (r.shape) {
|
|
477
|
+
const s = r.shape;
|
|
478
|
+
lines.push(` shape: ${s.topLevel}`);
|
|
479
|
+
if (s.keys.length > 0) lines.push(` keys: ${s.keys.join(", ")}`);
|
|
480
|
+
lines.push(` via: ${s.suggestedVia}`);
|
|
481
|
+
if (s.suggestedItemsPath)
|
|
482
|
+
lines.push(` itemsPath: ${s.suggestedItemsPath}`);
|
|
483
|
+
if (s.arrayLen > 0) lines.push(` arrayLen: ${s.arrayLen}`);
|
|
484
|
+
if (s.paginationMarkers.length > 0)
|
|
485
|
+
lines.push(` pagination markers: ${s.paginationMarkers.join(", ")}`);
|
|
486
|
+
if (s.representativeId)
|
|
487
|
+
lines.push(
|
|
488
|
+
` representative id: ${s.representativeId.field}=${s.representativeId.value}`,
|
|
489
|
+
);
|
|
490
|
+
}
|
|
491
|
+
if (r.draft) {
|
|
492
|
+
lines.push(` draft:`);
|
|
493
|
+
lines.push(r.draft.replace(/^/gm, " "));
|
|
494
|
+
}
|
|
495
|
+
if (r.raw) {
|
|
496
|
+
lines.push(` raw (truncated):`);
|
|
497
|
+
lines.push(r.raw);
|
|
498
|
+
}
|
|
499
|
+
return lines.join("\n");
|
|
500
|
+
}
|
package/tools/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool definitions barrel — exports all 4 tool definitions for registration.
|
|
3
|
+
*/
|
|
4
|
+
export { apiGuideTool } from "./api-guide.js";
|
|
5
|
+
export { apiFetchTool } from "./api-fetch.js";
|
|
6
|
+
export { apiLearnTool } from "./api-learn.js";
|
|
7
|
+
export { apiProbeTool } from "./api-probe.js";
|
package/tools/utils.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AgentToolResult,
|
|
3
|
+
ThemeColor,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Extract the text of the first text content block of a tool result.
|
|
8
|
+
* Returns the fallback (default "") when there is none.
|
|
9
|
+
*/
|
|
10
|
+
export function contentText(
|
|
11
|
+
result: AgentToolResult<unknown>,
|
|
12
|
+
fallback = "",
|
|
13
|
+
): string {
|
|
14
|
+
const c = result.content?.[0];
|
|
15
|
+
return c && c.type === "text" ? c.text : fallback;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Append a dim-styled content preview to an in-progress result string,
|
|
20
|
+
* with a "more chars" suffix when the content exceeds the given limit.
|
|
21
|
+
*/
|
|
22
|
+
export function renderExpandedText(
|
|
23
|
+
text: string,
|
|
24
|
+
theme: { fg: (c: ThemeColor, t: string) => string },
|
|
25
|
+
content: string,
|
|
26
|
+
limit: number,
|
|
27
|
+
): string {
|
|
28
|
+
const preview = content.replace(/\n{3,}/g, "\n\n").slice(0, limit);
|
|
29
|
+
if (!preview) return text;
|
|
30
|
+
text += `\n${theme.fg("dim", preview)}`;
|
|
31
|
+
if (content.length > limit)
|
|
32
|
+
text += `\n${theme.fg("muted", `… ${content.length - limit} more chars`)}`;
|
|
33
|
+
return text;
|
|
34
|
+
}
|
package/AGENTS.md
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# AGENTS.md — pi-lean-host (package)
|
|
2
|
-
|
|
3
|
-
> Benchmarking harness for the [pi-lean-dimension](../../AGENTS.md) monorepo.
|
|
4
|
-
>
|
|
5
|
-
> **This file is a stub.** For the suite overview, install matrix, dev
|
|
6
|
-
> commands, registered tools/commands summary, testing strategy, and
|
|
7
|
-
> TypeScript quirks, see [`../../AGENTS.md`](../../AGENTS.md). For portal
|
|
8
|
-
> internals (the `BrowserPlugin` interface, router dispatch, profile/cookie
|
|
9
|
-
> persistence, snapshot cache, etc.), see
|
|
10
|
-
> [`../pi-lean-portal/AGENTS.md`](../pi-lean-portal/AGENTS.md).
|
|
11
|
-
|
|
12
|
-
## What this package is
|
|
13
|
-
|
|
14
|
-
**pi-lean-host** is the evaluation harness for `BrowserPlugin` backends.
|
|
15
|
-
It runs behavioral evaluation (MiniWoB, WebArena, etc.) against any
|
|
16
|
-
plugin implementation via BrowserGym, while staying agnostic to the plugin
|
|
17
|
-
internals. See [`browsergym-migration-plan.md`](../../browsergym-migration-plan.md)
|
|
18
|
-
for the full plan.
|
|
19
|
-
|
|
20
|
-
## Status
|
|
21
|
-
|
|
22
|
-
Placeholder — see the README.
|