@agentcash/discovery 0.1.3 → 1.0.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/dist/cli.cjs CHANGED
@@ -24,625 +24,107 @@ __export(cli_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(cli_exports);
26
26
 
27
- // src/flags.ts
28
- var DEFAULT_COMPAT_MODE = "on";
29
- var STRICT_ESCALATION_CODES = [
30
- "LEGACY_WELL_KNOWN_USED",
31
- "LEGACY_DNS_USED",
32
- "LEGACY_DNS_PLAIN_URL",
33
- "LEGACY_MISSING_METHOD",
34
- "LEGACY_INSTRUCTIONS_USED",
35
- "LEGACY_OWNERSHIP_PROOFS_USED",
36
- "INTEROP_MPP_USED"
37
- ];
27
+ // src/core/source/openapi/index.ts
28
+ var import_neverthrow2 = require("neverthrow");
38
29
 
39
- // src/cli/args.ts
40
- var DEFAULT_TIMEOUT_MS = 5e3;
41
- function parseCompatibilityMode(value) {
42
- if (value === "on" || value === "off" || value === "strict") {
43
- return value;
44
- }
45
- return null;
46
- }
47
- function parseTimeoutMs(value) {
48
- const parsed = Number(value);
49
- if (!Number.isFinite(parsed) || parsed <= 0 || !Number.isInteger(parsed)) {
50
- return null;
51
- }
52
- return parsed;
53
- }
54
- function parseClient(value) {
55
- if (value === "claude-code" || value === "skill-cli" || value === "generic-mcp" || value === "generic") {
56
- return value;
57
- }
58
- return null;
59
- }
60
- function parseContextWindowTokens(value) {
61
- const parsed = Number(value);
62
- if (!Number.isFinite(parsed) || parsed <= 0 || !Number.isInteger(parsed)) {
63
- return null;
64
- }
65
- return parsed;
66
- }
67
- function parseCliArgs(argv) {
68
- let target;
69
- let verbose = false;
70
- let json = false;
71
- let probe = false;
72
- let timeoutMs = DEFAULT_TIMEOUT_MS;
73
- let compatMode = DEFAULT_COMPAT_MODE;
74
- let color = true;
75
- let noTruncate = false;
76
- let harness = false;
77
- let client = "claude-code";
78
- let contextWindowTokens;
79
- for (let i = 0; i < argv.length; i += 1) {
80
- const arg = argv[i];
81
- if (arg === "--help" || arg === "-h") {
82
- return { kind: "help" };
83
- }
84
- if (arg === "--verbose" || arg === "-v") {
85
- verbose = true;
86
- continue;
87
- }
88
- if (arg === "--json") {
89
- json = true;
90
- continue;
91
- }
92
- if (arg === "--probe") {
93
- probe = true;
94
- continue;
95
- }
96
- if (arg === "--no-color") {
97
- color = false;
98
- continue;
99
- }
100
- if (arg === "--no-truncate") {
101
- noTruncate = true;
102
- continue;
103
- }
104
- if (arg === "--harness") {
105
- harness = true;
106
- continue;
107
- }
108
- if (arg === "--compat") {
109
- const value = argv[i + 1];
110
- if (!value) {
111
- return { kind: "error", message: "Missing value for --compat." };
112
- }
113
- const parsed = parseCompatibilityMode(value);
114
- if (!parsed) {
115
- return {
116
- kind: "error",
117
- message: `Invalid --compat value '${value}'. Expected: on | off | strict.`
118
- };
119
- }
120
- compatMode = parsed;
121
- i += 1;
122
- continue;
123
- }
124
- if (arg === "--timeout-ms") {
125
- const value = argv[i + 1];
126
- if (!value) {
127
- return { kind: "error", message: "Missing value for --timeout-ms." };
128
- }
129
- const parsed = parseTimeoutMs(value);
130
- if (!parsed) {
131
- return {
132
- kind: "error",
133
- message: `Invalid --timeout-ms value '${value}'. Expected a positive integer.`
134
- };
135
- }
136
- timeoutMs = parsed;
137
- i += 1;
138
- continue;
139
- }
140
- if (arg === "--client") {
141
- const value = argv[i + 1];
142
- if (!value) {
143
- return { kind: "error", message: "Missing value for --client." };
144
- }
145
- const parsed = parseClient(value);
146
- if (!parsed) {
147
- return {
148
- kind: "error",
149
- message: "Invalid --client value '" + value + "'. Expected: claude-code | skill-cli | generic-mcp | generic."
150
- };
151
- }
152
- client = parsed;
153
- i += 1;
154
- continue;
155
- }
156
- if (arg === "--context-window-tokens") {
157
- const value = argv[i + 1];
158
- if (!value) {
159
- return { kind: "error", message: "Missing value for --context-window-tokens." };
160
- }
161
- const parsed = parseContextWindowTokens(value);
162
- if (!parsed) {
163
- return {
164
- kind: "error",
165
- message: `Invalid --context-window-tokens value '${value}'. Expected a positive integer.`
166
- };
167
- }
168
- contextWindowTokens = parsed;
169
- i += 1;
170
- continue;
171
- }
172
- if (arg.startsWith("-")) {
173
- return { kind: "error", message: `Unknown flag '${arg}'.` };
174
- }
175
- if (target) {
176
- return {
177
- kind: "error",
178
- message: `Unexpected extra positional argument '${arg}'. Only one domain/URL is supported.`
179
- };
180
- }
181
- target = arg;
182
- }
183
- if (!target) {
184
- return { kind: "error", message: "Missing required <domain> argument." };
185
- }
186
- return {
187
- kind: "ok",
188
- options: {
189
- target,
190
- verbose,
191
- json,
192
- compatMode,
193
- probe,
194
- timeoutMs,
195
- color,
196
- noTruncate,
197
- harness,
198
- client,
199
- ...contextWindowTokens ? { contextWindowTokens } : {}
200
- }
201
- };
202
- }
203
- function renderHelp() {
204
- return [
205
- "Usage: npx @agentcash/discovery <domain-or-url> [options]",
206
- "",
207
- "Options:",
208
- " -v, --verbose Show full compatibility matrix",
209
- " --json Emit JSON output",
210
- " --compat <mode> Compatibility mode: on | off | strict (default: on)",
211
- " --probe Re-run with probe candidates from discovered paths",
212
- " --harness Render L0-L5 context harness audit view",
213
- " --client <id> Harness client: claude-code | skill-cli | generic-mcp | generic",
214
- " --context-window-tokens <n>",
215
- " Override client context window for harness budget checks",
216
- " --timeout-ms <ms> Per-request timeout in milliseconds (default: 5000)",
217
- " --no-color Disable ANSI colors",
218
- " --no-truncate Disable output truncation in verbose tables",
219
- " -h, --help Show help"
220
- ].join("\n");
221
- }
222
-
223
- // src/cli/render.ts
224
- var import_table = require("table");
225
- function createPalette(enabled) {
226
- const wrap = (code) => (value) => enabled ? `\x1B[${code}m${value}\x1B[0m` : value;
227
- return {
228
- dim: wrap(2),
229
- bold: wrap(1),
230
- red: wrap(31),
231
- green: wrap(32),
232
- yellow: wrap(33),
233
- cyan: wrap(36)
234
- };
235
- }
236
- function summarizeWarnings(warnings) {
237
- const errors = warnings.filter((entry) => entry.severity === "error").length;
238
- const warns = warnings.filter((entry) => entry.severity === "warn").length;
239
- const infos = warnings.filter((entry) => entry.severity === "info").length;
240
- const codeCounts = warnings.filter((entry) => entry.code !== "PROBE_STAGE_SKIPPED").reduce(
241
- (acc, entry) => {
242
- acc[entry.code] = (acc[entry.code] ?? 0) + 1;
243
- return acc;
244
- },
245
- {}
246
- );
247
- const codes = Object.entries(codeCounts).sort((a, b) => b[1] - a[1]).map(([code, count]) => ({ code, count }));
248
- return {
249
- total: warnings.length,
250
- errors,
251
- warns,
252
- infos,
253
- codes
254
- };
255
- }
256
- function asYesNo(value) {
257
- return value ? "yes" : "no";
258
- }
259
- function truncate(value, max, noTruncate) {
260
- if (noTruncate || value.length <= max) {
261
- return value;
262
- }
263
- if (max <= 1) return value.slice(0, max);
264
- return `${value.slice(0, Math.max(1, max - 1))}...`;
265
- }
266
- function formatPricing(resource) {
267
- if (resource.authHint !== "paid") return "-";
268
- if (resource.pricing) {
269
- if (resource.pricing.pricingMode === "fixed") {
270
- return resource.pricing.price ? `fixed ${resource.pricing.price}` : "fixed";
271
- }
272
- if (resource.pricing.pricingMode === "range") {
273
- return `range ${resource.pricing.minPrice ?? "?"}-${resource.pricing.maxPrice ?? "?"}`;
274
- }
275
- return "quote";
276
- }
277
- return resource.priceHint ? `hint ${resource.priceHint}` : "-";
278
- }
279
- function statusBadge(ok, palette) {
280
- return ok ? palette.green("[PASS]") : palette.red("[FAIL]");
281
- }
282
- function riskBadge(summary, palette) {
283
- if (summary.errors > 0) return palette.red("HIGH");
284
- if (summary.warns > 0) return palette.yellow("MEDIUM");
285
- return palette.green("LOW");
286
- }
287
- function sectionHeader(title, palette) {
288
- return `${palette.bold(title)}
289
- ${palette.dim("-".repeat(title.length))}`;
290
- }
291
- function introBlock(run, options, palette) {
292
- const summary = summarizeWarnings(run.result.warnings);
293
- const topCodes = summary.codes.slice(0, 3).map(({ code, count }) => `${code} (${count})`).join(", ");
294
- const lines = [
295
- `${palette.bold("AGENTCASH DISCOVERY AUDIT")} ${statusBadge(run.result.resources.length > 0, palette)}`,
296
- `${palette.dim("=".repeat(74))}`,
297
- `Target ${options.target}`,
298
- `Origin ${run.result.origin}`,
299
- `Stage ${run.result.selectedStage ?? "none"} Compat: ${run.result.compatMode}`,
300
- `Resources ${run.result.resources.length} Duration: ${run.durationMs}ms`,
301
- `Risk ${riskBadge(summary, palette)} Upgrade suggested: ${run.result.upgradeSuggested ? "yes" : "no"}`,
302
- `Warnings error=${summary.errors} warn=${summary.warns} info=${summary.infos}`,
303
- `Top signals ${topCodes || "-"}`
304
- ];
305
- return lines.join("\n");
306
- }
307
- function outcomeLine(run) {
308
- const selectedStage = run.result.selectedStage ?? "none";
309
- if (run.result.resources.length === 0) {
310
- return "No usable discovery metadata was found.";
311
- }
312
- if (selectedStage === "openapi" || selectedStage === "override") {
313
- return `Canonical discovery succeeded via ${selectedStage}.`;
314
- }
315
- return `Discovery succeeded, but only via legacy stage ${selectedStage}.`;
316
- }
317
- function actionItems(run, options) {
318
- const target = options.target;
319
- if (run.result.resources.length === 0) {
320
- return [
321
- `Run deeper diagnostics: npx @agentcash/discovery ${target} -v --probe --compat on`,
322
- `Check that ${target} serves /openapi.json or /.well-known/x402`,
323
- "If endpoint exists, verify it returns parseable JSON and non-empty resources"
324
- ];
325
- }
326
- if (run.result.selectedStage === "openapi" || run.result.selectedStage === "override") {
327
- return [
328
- `Run strict mode hardening: npx @agentcash/discovery ${target} --compat strict -v`,
329
- "Reduce warning count to near-zero and keep pricing/auth hints explicit"
330
- ];
331
- }
332
- return [
333
- "Publish canonical OpenAPI discovery metadata with x-agentcash extensions",
334
- `Validate migration path: npx @agentcash/discovery ${target} --compat strict -v --probe`,
335
- "Keep legacy well-known path only during sunset window"
336
- ];
337
- }
338
- function buildTable(data, maxColWidths, noTruncate) {
339
- const rendered = data.map(
340
- (row) => row.map((cell, idx) => {
341
- const width = maxColWidths[idx] ?? 120;
342
- return noTruncate ? cell : truncate(cell, width, false);
30
+ // src/schemas.ts
31
+ var import_zod = require("zod");
32
+ var OpenApiPaymentInfoSchema = import_zod.z.object({
33
+ pricingMode: import_zod.z.enum(["fixed", "range", "quote"]),
34
+ price: import_zod.z.string().optional(),
35
+ minPrice: import_zod.z.string().optional(),
36
+ maxPrice: import_zod.z.string().optional(),
37
+ protocols: import_zod.z.array(import_zod.z.string()).optional()
38
+ });
39
+ var OpenApiOperationSchema = import_zod.z.object({
40
+ operationId: import_zod.z.string().optional(),
41
+ summary: import_zod.z.string().optional(),
42
+ description: import_zod.z.string().optional(),
43
+ tags: import_zod.z.array(import_zod.z.string()).optional(),
44
+ security: import_zod.z.array(import_zod.z.record(import_zod.z.string(), import_zod.z.array(import_zod.z.string()))).optional(),
45
+ parameters: import_zod.z.array(
46
+ import_zod.z.object({
47
+ in: import_zod.z.string(),
48
+ name: import_zod.z.string(),
49
+ schema: import_zod.z.unknown().optional(),
50
+ required: import_zod.z.boolean().optional()
343
51
  })
344
- );
345
- return (0, import_table.table)(rendered, {
346
- border: (0, import_table.getBorderCharacters)("ramac"),
347
- columns: maxColWidths.reduce(
348
- (acc, width, idx) => {
349
- acc[idx] = noTruncate ? { wrapWord: false, alignment: "left" } : { width, truncate: width, wrapWord: false, alignment: "left" };
350
- return acc;
351
- },
352
- {}
353
- ),
354
- drawHorizontalLine: (index, size) => index === 0 || index === 1 || index === size,
355
- columnDefault: {
356
- paddingLeft: 1,
357
- paddingRight: 1
358
- }
359
- }).trimEnd();
360
- }
361
- function stageMatrixRows(result) {
362
- const header = ["stage", "valid", "resources", "duration", "selected", "warning codes"];
363
- const rows = result.trace.map((entry) => {
364
- const codes = [...new Set(entry.warnings.map((warning2) => warning2.code))].join(", ");
365
- return [
366
- entry.stage,
367
- asYesNo(entry.valid),
368
- String(entry.resourceCount),
369
- `${entry.durationMs}ms`,
370
- result.selectedStage === entry.stage ? "yes" : "-",
371
- codes || "-"
372
- ];
373
- });
374
- return [header, ...rows];
375
- }
376
- function resourceMatrixRows(result) {
377
- const sorted = [...result.resources].sort((a, b) => {
378
- if (a.path !== b.path) return a.path.localeCompare(b.path);
379
- if (a.method !== b.method) return a.method.localeCompare(b.method);
380
- return a.origin.localeCompare(b.origin);
381
- });
382
- const header = ["method", "path", "auth", "pricing", "source", "verified", "notes"];
383
- const rows = sorted.map((resource) => [
384
- resource.method,
385
- resource.path,
386
- resource.authHint ?? "-",
387
- formatPricing(resource),
388
- resource.source,
389
- asYesNo(resource.verified),
390
- resource.authHint === "siwx" ? "siwx" : "-"
391
- ]);
392
- return [header, ...rows];
393
- }
394
- function warningRows(summary, result) {
395
- const hintByCode = /* @__PURE__ */ new Map();
396
- for (const warning2 of result.warnings) {
397
- if (!hintByCode.has(warning2.code)) {
398
- hintByCode.set(warning2.code, warning2.hint ?? warning2.message);
399
- }
400
- }
401
- const header = ["severity", "code", "count", "hint/message"];
402
- const rows = summary.codes.slice(0, 8).map(({ code, count }) => {
403
- const sample = hintByCode.get(code) ?? "-";
404
- const severity = code.includes("NO_DISCOVERY") ? "error" : code.includes("LEGACY") ? "warn" : "info";
405
- return [severity, code, String(count), sample];
406
- });
407
- return [header, ...rows];
408
- }
409
- function harnessBudgetTable(report) {
410
- return [
411
- ["metric", "value"],
412
- ["client", `${report.client.id} (${report.client.surface})`],
413
- ["context window tokens", String(report.budget.contextWindowTokens)],
414
- ["zero-hop budget tokens", String(report.budget.zeroHopBudgetTokens)],
415
- ["estimated L0+L1 tokens", String(report.budget.estimatedZeroHopTokens)],
416
- ["within budget", report.budget.withinBudget ? "yes" : "no"]
417
- ];
418
- }
419
- function harnessLayerSummaryRows(report) {
420
- return [
421
- ["layer", "what", "primary command", "status"],
422
- [
423
- "L0",
424
- "trigger layer",
425
- report.levels.l0.installCommand,
426
- `${report.levels.l0.intentTriggers.length} triggers`
427
- ],
428
- [
429
- "L1",
430
- "installed domain index",
431
- report.levels.l1.fanoutCommands[0] ?? "-",
432
- `${report.levels.l1.domainClass}; stage=${report.levels.l1.selectedDiscoveryStage ?? "none"}`
433
- ],
434
- [
435
- "L2",
436
- "domain resources",
437
- report.levels.l2.command,
438
- `${report.levels.l2.resourceCount} resources`
439
- ],
440
- [
441
- "L3",
442
- "resource details",
443
- report.levels.l3.command,
444
- `${report.levels.l3.pricedResourceCount} priced`
445
- ],
446
- ["L4", "domain guidance", report.levels.l4.llmsTxtUrl ?? "-", report.levels.l4.guidanceStatus],
447
- ["L5", "cross-domain composition", "-", report.levels.l5.status]
448
- ];
449
- }
450
- function harnessL2PreviewRows(report, noTruncate) {
451
- const header = ["method", "path", "auth", "source"];
452
- const rows = report.levels.l2.resources.slice(0, 15).map((resource) => [
453
- resource.method,
454
- truncate(resource.path, 54, noTruncate),
455
- resource.authMode ?? "-",
456
- resource.source
457
- ]);
458
- return [header, ...rows];
459
- }
460
- function renderHarnessSummary(report, options, palette) {
461
- const lines = [];
462
- lines.push(
463
- `${palette.bold("L0-L5 Context Harness")} ${statusBadge(report.levels.l2.resourceCount > 0, palette)}`
464
- );
465
- lines.push(`${palette.dim("=".repeat(74))}`);
466
- lines.push(`Client ${report.client.label}`);
467
- lines.push(`Domain ${report.levels.l1.domain}`);
468
- lines.push(`L2 resources ${report.levels.l2.resourceCount}`);
469
- lines.push(
470
- `Budget ${report.budget.estimatedZeroHopTokens}/${report.budget.zeroHopBudgetTokens} tokens (L0+L1)`
471
- );
472
- lines.push("");
473
- lines.push("Layer map:");
474
- lines.push("1. L0 trigger surface -> install and route to agentcash");
475
- lines.push(`2. L1 domain index -> ${report.levels.l1.fanoutCommands[0]}`);
476
- lines.push(`3. L2 resources -> ${report.levels.l2.command}`);
477
- lines.push(`4. L3 details -> ${report.levels.l3.command}`);
478
- lines.push(
479
- `5. L4 guidance -> ${report.levels.l4.guidanceStatus}${report.levels.l4.llmsTxtUrl ? ` (${report.levels.l4.llmsTxtUrl})` : ""}`
480
- );
481
- lines.push("6. L5 cross-domain -> out of scope in v1");
482
- lines.push("");
483
- lines.push(
484
- `Next: npx @agentcash/discovery ${options.target} --harness -v --client ${report.client.id}`
485
- );
486
- return lines.join("\n");
487
- }
488
- function renderHarnessVerbose(report, options, palette) {
489
- const lines = [];
490
- lines.push(
491
- `${palette.bold("L0-L5 Context Harness (Verbose)")} ${statusBadge(report.levels.l2.resourceCount > 0, palette)}`
492
- );
493
- lines.push(`${palette.dim("=".repeat(74))}`);
494
- lines.push(`Target ${report.target}`);
495
- lines.push(`Origin ${report.origin}`);
496
- lines.push(`Client ${report.client.label}`);
497
- lines.push("");
498
- lines.push(sectionHeader("Budget check", palette));
499
- lines.push(buildTable(harnessBudgetTable(report), [30, 42], options.noTruncate));
500
- lines.push("");
501
- lines.push(sectionHeader("Layer summary", palette));
502
- lines.push(buildTable(harnessLayerSummaryRows(report), [8, 24, 44, 28], options.noTruncate));
503
- lines.push("");
504
- lines.push(sectionHeader("L2 resource preview", palette));
505
- if (report.levels.l2.resources.length === 0) {
506
- lines.push(palette.dim("(no discovered resources)"));
507
- } else {
508
- lines.push(
509
- buildTable(
510
- harnessL2PreviewRows(report, options.noTruncate),
511
- [10, 54, 12, 20],
512
- options.noTruncate
513
- )
514
- );
515
- if (report.levels.l2.resources.length > 15) {
516
- lines.push(
517
- palette.dim(
518
- `Showing 15/${report.levels.l2.resources.length} resources. Use --json to inspect full L2/L3 payload.`
519
- )
520
- );
521
- }
522
- }
523
- lines.push("");
524
- lines.push(sectionHeader("L4 guidance", palette));
525
- lines.push(`status: ${report.levels.l4.guidanceStatus}`);
526
- lines.push(`llms.txt url: ${report.levels.l4.llmsTxtUrl ?? "-"}`);
527
- lines.push(`llms.txt tokens: ${report.levels.l4.llmsTxtTokenEstimate}`);
528
- lines.push(`preview: ${report.levels.l4.guidancePreview ?? "-"}`);
529
- lines.push("");
530
- lines.push(sectionHeader("Diagnostics", palette));
531
- lines.push(
532
- `warnings: ${report.diagnostics.warningCount} (errors=${report.diagnostics.errorWarningCount})`
533
- );
534
- lines.push(`selected stage: ${report.diagnostics.selectedStage ?? "none"}`);
535
- lines.push(`upgrade suggested: ${report.diagnostics.upgradeSuggested ? "yes" : "no"}`);
536
- lines.push(
537
- `upgrade reasons: ${report.diagnostics.upgradeReasons.length > 0 ? report.diagnostics.upgradeReasons.join(", ") : "-"}`
538
- );
539
- return lines.join("\n");
540
- }
541
- function renderSummary(run, options) {
542
- const palette = createPalette(options.color);
543
- if (options.harness && run.harness) {
544
- return renderHarnessSummary(run.harness, options, palette);
545
- }
546
- const body = [];
547
- body.push(introBlock(run, options, palette));
548
- body.push("");
549
- body.push(`Outcome: ${outcomeLine(run)}`);
550
- const items = actionItems(run, options);
551
- body.push("Next:");
552
- for (let i = 0; i < items.length; i += 1) {
553
- body.push(`${i + 1}. ${items[i]}`);
554
- }
555
- if (options.probe) {
556
- body.push(`Probe candidates considered: ${run.probeCandidateCount}`);
557
- }
558
- return body.join("\n");
52
+ ).optional(),
53
+ requestBody: import_zod.z.object({
54
+ required: import_zod.z.boolean().optional(),
55
+ content: import_zod.z.record(import_zod.z.string(), import_zod.z.object({ schema: import_zod.z.unknown().optional() }))
56
+ }).optional(),
57
+ responses: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional(),
58
+ "x-payment-info": OpenApiPaymentInfoSchema.optional()
59
+ });
60
+ var OpenApiPathItemSchema = import_zod.z.object({
61
+ get: OpenApiOperationSchema.optional(),
62
+ post: OpenApiOperationSchema.optional(),
63
+ put: OpenApiOperationSchema.optional(),
64
+ delete: OpenApiOperationSchema.optional(),
65
+ patch: OpenApiOperationSchema.optional(),
66
+ head: OpenApiOperationSchema.optional(),
67
+ options: OpenApiOperationSchema.optional(),
68
+ trace: OpenApiOperationSchema.optional()
69
+ });
70
+ var OpenApiDocSchema = import_zod.z.object({
71
+ // TODO(zdql): We should inherit a canonical OpenAPI schema and then extend with our types.
72
+ openapi: import_zod.z.string(),
73
+ info: import_zod.z.object({
74
+ title: import_zod.z.string(),
75
+ version: import_zod.z.string(),
76
+ description: import_zod.z.string().optional(),
77
+ guidance: import_zod.z.string().optional()
78
+ }),
79
+ servers: import_zod.z.array(import_zod.z.object({ url: import_zod.z.string() })).optional(),
80
+ tags: import_zod.z.array(import_zod.z.object({ name: import_zod.z.string() })).optional(),
81
+ components: import_zod.z.object({ securitySchemes: import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional() }).optional(),
82
+ "x-discovery": import_zod.z.record(import_zod.z.string(), import_zod.z.unknown()).optional(),
83
+ paths: import_zod.z.record(import_zod.z.string(), OpenApiPathItemSchema)
84
+ });
85
+ var WellKnownDocSchema = import_zod.z.object({
86
+ version: import_zod.z.number().optional(),
87
+ resources: import_zod.z.array(import_zod.z.string()).default([]),
88
+ mppResources: import_zod.z.array(import_zod.z.string()).optional(),
89
+ // isMmmEnabled
90
+ description: import_zod.z.string().optional(),
91
+ ownershipProofs: import_zod.z.array(import_zod.z.string()).optional(),
92
+ instructions: import_zod.z.string().optional()
93
+ });
94
+ var WellKnownParsedSchema = import_zod.z.object({
95
+ routes: import_zod.z.array(
96
+ import_zod.z.object({
97
+ path: import_zod.z.string(),
98
+ method: import_zod.z.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"])
99
+ })
100
+ ),
101
+ instructions: import_zod.z.string().optional()
102
+ });
103
+
104
+ // src/core/source/openapi/utils.ts
105
+ function isRecord(value) {
106
+ return value !== null && typeof value === "object" && !Array.isArray(value);
559
107
  }
560
- function renderVerbose(run, options) {
561
- const palette = createPalette(options.color);
562
- if (options.harness && run.harness) {
563
- return renderHarnessVerbose(run.harness, options, palette);
564
- }
565
- const summary = summarizeWarnings(run.result.warnings);
566
- const noTruncate = options.noTruncate;
567
- const body = [];
568
- body.push(introBlock(run, options, palette));
569
- body.push("");
570
- body.push(sectionHeader("What this means", palette));
571
- body.push(outcomeLine(run));
572
- body.push("");
573
- body.push(sectionHeader("Recommended actions", palette));
574
- const items = actionItems(run, options);
575
- for (let i = 0; i < items.length; i += 1) {
576
- body.push(`${i + 1}. ${items[i]}`);
577
- }
578
- body.push("");
579
- body.push(sectionHeader("Compatibility matrix", palette));
580
- body.push(buildTable(stageMatrixRows(run.result), [18, 8, 10, 10, 10, 56], noTruncate));
581
- body.push("");
582
- body.push(sectionHeader("Resource matrix", palette));
583
- if (run.result.resources.length === 0) {
584
- body.push(palette.dim("(no discovered resources)"));
585
- } else {
586
- const allRows = resourceMatrixRows(run.result);
587
- const previewRows = allRows.slice(0, 16);
588
- body.push(buildTable(previewRows, [8, 44, 10, 24, 18, 10, 10], noTruncate));
589
- if (allRows.length > previewRows.length) {
590
- body.push(
591
- palette.dim(
592
- `Showing ${previewRows.length - 1}/${allRows.length - 1} resources. Use --no-truncate or --json for complete output.`
593
- )
594
- );
595
- }
596
- }
597
- body.push("");
598
- body.push(sectionHeader("Warning breakdown", palette));
599
- if (summary.codes.length === 0) {
600
- body.push(palette.dim("(no warnings)"));
601
- } else {
602
- body.push(buildTable(warningRows(summary, run.result), [10, 34, 8, 62], noTruncate));
603
- }
604
- if (run.result.upgradeReasons.length > 0) {
605
- body.push("");
606
- body.push(sectionHeader("Upgrade guidance", palette));
607
- for (const reason of run.result.upgradeReasons) {
608
- body.push(`- ${reason}`);
609
- }
610
- }
611
- return body.join("\n");
612
- }
613
- function renderJson(run, options) {
614
- return JSON.stringify(
615
- {
616
- ok: run.result.resources.length > 0,
617
- target: options.target,
618
- origin: run.result.origin,
619
- compatMode: run.result.compatMode,
620
- selectedStage: run.result.selectedStage ?? null,
621
- resources: run.result.resources,
622
- trace: run.result.trace,
623
- warnings: run.result.warnings,
624
- upgradeSuggested: run.result.upgradeSuggested,
625
- upgradeReasons: run.result.upgradeReasons,
626
- meta: {
627
- durationMs: run.durationMs,
628
- probeEnabled: options.probe,
629
- probeCandidateCount: run.probeCandidateCount,
630
- timeoutMs: options.timeoutMs
631
- },
632
- ...run.harness ? { harness: run.harness } : {}
633
- },
634
- null,
635
- 2
636
- );
108
+ function hasSecurity(operation, scheme) {
109
+ return operation.security?.some((s) => scheme in s) ?? false;
110
+ }
111
+ function has402Response(operation) {
112
+ return Boolean(operation.responses?.["402"]);
113
+ }
114
+ function inferAuthMode(operation) {
115
+ const hasXPaymentInfo = Boolean(operation["x-payment-info"]);
116
+ const hasPayment = hasXPaymentInfo || has402Response(operation);
117
+ const hasApiKey = hasSecurity(operation, "apiKey");
118
+ const hasSiwx = hasSecurity(operation, "siwx");
119
+ if (hasPayment && hasApiKey) return "apiKey+paid";
120
+ if (hasXPaymentInfo) return "paid";
121
+ if (hasPayment) return hasSiwx ? "siwx" : "paid";
122
+ if (hasApiKey) return "apiKey";
123
+ if (hasSiwx) return "siwx";
124
+ return void 0;
637
125
  }
638
126
 
639
- // src/mmm-enabled.ts
640
- var isMmmEnabled = () => "0.1.3".includes("-mmm");
641
-
642
- // src/core/constants.ts
643
- var OPENAPI_PATH_CANDIDATES = ["/openapi.json", "/.well-known/openapi.json"];
644
- var WELL_KNOWN_MPP_PATH = "/.well-known/mpp";
645
- var LLMS_TOKEN_WARNING_THRESHOLD = 2500;
127
+ // src/core/lib/constants.ts
646
128
  var HTTP_METHODS = /* @__PURE__ */ new Set([
647
129
  "GET",
648
130
  "POST",
@@ -653,10 +135,9 @@ var HTTP_METHODS = /* @__PURE__ */ new Set([
653
135
  "OPTIONS",
654
136
  "TRACE"
655
137
  ]);
656
- var DEFAULT_PROBE_METHODS = ["GET", "POST"];
657
138
  var DEFAULT_MISSING_METHOD = "POST";
658
139
 
659
- // src/core/url.ts
140
+ // src/core/lib/url.ts
660
141
  function normalizeOrigin(target) {
661
142
  const trimmed = target.trim();
662
143
  const withProtocol = /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;
@@ -674,9 +155,6 @@ function normalizePath(pathname) {
674
155
  const normalized = prefixed.replace(/\/+/g, "/");
675
156
  return normalized !== "/" ? normalized.replace(/\/$/, "") : "/";
676
157
  }
677
- function toResourceKey(origin, method, path) {
678
- return `${origin} ${method} ${normalizePath(path)}`;
679
- }
680
158
  function parseMethod(value) {
681
159
  if (!value) return void 0;
682
160
  const upper = value.toUpperCase();
@@ -691,895 +169,504 @@ function toAbsoluteUrl(origin, value) {
691
169
  }
692
170
  }
693
171
 
694
- // src/core/warnings.ts
695
- function warning(code, severity, message, options) {
696
- return {
697
- code,
698
- severity,
699
- message,
700
- ...options?.hint ? { hint: options.hint } : {},
701
- ...options?.stage ? { stage: options.stage } : {},
702
- ...options?.resourceKey ? { resourceKey: options.resourceKey } : {}
703
- };
704
- }
705
- function applyStrictEscalation(warnings, strict) {
706
- if (!strict) return warnings;
707
- return warnings.map((entry) => {
708
- if (!STRICT_ESCALATION_CODES.includes(entry.code)) {
709
- return entry;
710
- }
711
- if (entry.severity === "error") return entry;
712
- return {
713
- ...entry,
714
- severity: "error",
715
- message: `${entry.message} (strict mode escalated)`
716
- };
717
- });
172
+ // src/core/source/fetch.ts
173
+ var import_neverthrow = require("neverthrow");
174
+ function toFetchError(err) {
175
+ const cause = err instanceof DOMException && (err.name === "TimeoutError" || err.name === "AbortError") ? "timeout" : "network";
176
+ return { cause, message: String(err) };
718
177
  }
719
- function dedupeWarnings(warnings) {
720
- const seen = /* @__PURE__ */ new Set();
721
- const output = [];
722
- for (const item of warnings) {
723
- const key = `${item.code}|${item.severity}|${item.stage ?? ""}|${item.resourceKey ?? ""}|${item.message}`;
724
- if (seen.has(key)) continue;
725
- seen.add(key);
726
- output.push(item);
727
- }
728
- return output;
178
+ function fetchSafe(url, init) {
179
+ return import_neverthrow.ResultAsync.fromPromise(fetch(url, init), toFetchError);
729
180
  }
730
181
 
731
- // src/core/normalize.ts
732
- function createResource(input, confidence, trustTier) {
733
- const normalizedOrigin = normalizeOrigin(input.origin);
734
- const normalizedPath = normalizePath(input.path);
735
- return {
736
- resourceKey: toResourceKey(normalizedOrigin, input.method, normalizedPath),
737
- origin: normalizedOrigin,
738
- method: input.method,
739
- path: normalizedPath,
740
- source: input.source,
741
- verified: false,
742
- ...input.protocolHints?.length ? { protocolHints: [...new Set(input.protocolHints)] } : {},
743
- ...input.priceHint ? { priceHint: input.priceHint } : {},
744
- ...input.pricing ? { pricing: input.pricing } : {},
745
- ...input.authHint ? { authHint: input.authHint } : {},
746
- ...input.summary ? { summary: input.summary } : {},
747
- confidence,
748
- trustTier,
749
- ...input.links ? { links: input.links } : {}
750
- };
751
- }
182
+ // src/mmm-enabled.ts
183
+ var isMmmEnabled = () => "1.0.0".includes("-mmm");
752
184
 
753
- // src/compat/legacy-x402scan/wellKnown.ts
754
- function isRecord(value) {
755
- return value !== null && typeof value === "object" && !Array.isArray(value);
756
- }
757
- function parseLegacyResourceEntry(entry) {
758
- const trimmed = entry.trim();
759
- if (!trimmed) return null;
760
- const parts = trimmed.split(/\s+/);
761
- if (parts.length >= 2) {
762
- const maybeMethod = parseMethod(parts[0]);
763
- if (maybeMethod) {
764
- const target = parts.slice(1).join(" ");
765
- return { method: maybeMethod, target };
766
- }
767
- }
768
- if (trimmed.startsWith("/") || /^https?:\/\//i.test(trimmed)) {
769
- return { target: trimmed };
770
- }
771
- return null;
772
- }
773
- function parseWellKnownPayload(payload, origin, sourceUrl) {
774
- const warnings = [
775
- warning(
776
- "LEGACY_WELL_KNOWN_USED",
777
- "warn",
778
- "Using legacy /.well-known/x402 compatibility path. Migrate to OpenAPI-first.",
779
- {
780
- stage: "well-known/x402"
781
- }
782
- )
783
- ];
784
- if (!isRecord(payload)) {
785
- warnings.push(
786
- warning("PARSE_FAILED", "error", "Legacy well-known payload is not an object", {
787
- stage: "well-known/x402"
788
- })
789
- );
790
- return { resources: [], warnings, raw: payload };
791
- }
792
- const resourcesRaw = Array.isArray(payload.resources) ? payload.resources.filter((entry) => typeof entry === "string") : [];
793
- if (resourcesRaw.length === 0) {
794
- warnings.push(
795
- warning("STAGE_EMPTY", "warn", "Legacy well-known has no valid resources array", {
796
- stage: "well-known/x402"
797
- })
798
- );
799
- }
800
- const instructions = typeof payload.instructions === "string" ? payload.instructions : void 0;
801
- const ownershipProofs = Array.isArray(payload.ownershipProofs) ? payload.ownershipProofs.filter((entry) => typeof entry === "string") : [];
802
- if (instructions) {
803
- warnings.push(
804
- warning(
805
- "LEGACY_INSTRUCTIONS_USED",
806
- "warn",
807
- "Using /.well-known/x402.instructions as compatibility guidance fallback. Prefer llms.txt.",
808
- {
809
- stage: "well-known/x402",
810
- hint: "Move guidance to llms.txt and reference via x-agentcash-guidance.llmsTxtUrl."
811
- }
812
- )
813
- );
814
- }
815
- if (ownershipProofs.length > 0) {
816
- warnings.push(
817
- warning(
818
- "LEGACY_OWNERSHIP_PROOFS_USED",
819
- "warn",
820
- "Using /.well-known/x402.ownershipProofs compatibility field. Prefer OpenAPI provenance extension.",
821
- {
822
- stage: "well-known/x402",
823
- hint: "Move ownership proofs to x-agentcash-provenance.ownershipProofs in OpenAPI."
824
- }
825
- )
826
- );
827
- }
828
- const resources = [];
829
- for (const rawEntry of resourcesRaw) {
830
- const parsed = parseLegacyResourceEntry(rawEntry);
831
- if (!parsed) {
832
- warnings.push(
833
- warning("PARSE_FAILED", "warn", `Invalid legacy resource entry: ${rawEntry}`, {
834
- stage: "well-known/x402"
835
- })
836
- );
837
- continue;
838
- }
839
- const absolute = toAbsoluteUrl(origin, parsed.target);
840
- if (!absolute) {
841
- warnings.push(
842
- warning("PARSE_FAILED", "warn", `Invalid legacy resource URL: ${rawEntry}`, {
843
- stage: "well-known/x402"
844
- })
845
- );
846
- continue;
847
- }
848
- const method = parsed.method ?? DEFAULT_MISSING_METHOD;
849
- if (!parsed.method) {
850
- warnings.push(
851
- warning(
852
- "LEGACY_MISSING_METHOD",
853
- "warn",
854
- `Legacy resource '${rawEntry}' missing method. Defaulting to ${DEFAULT_MISSING_METHOD}.`,
855
- {
856
- stage: "well-known/x402"
857
- }
858
- )
185
+ // src/core/source/openapi/index.ts
186
+ var OpenApiParsedSchema = OpenApiDocSchema.transform((doc) => {
187
+ const routes = [];
188
+ for (const [rawPath, pathItem] of Object.entries(doc.paths)) {
189
+ for (const httpMethod of [...HTTP_METHODS]) {
190
+ const operation = pathItem[httpMethod.toLowerCase()];
191
+ if (!operation) continue;
192
+ const authMode = inferAuthMode(operation) ?? void 0;
193
+ if (!authMode) continue;
194
+ const p = operation["x-payment-info"];
195
+ const protocols = (p?.protocols ?? []).filter(
196
+ (proto) => proto !== "mpp" || isMmmEnabled()
859
197
  );
198
+ if ((authMode === "paid" || authMode === "siwx") && !has402Response(operation)) continue;
199
+ if (authMode === "paid" && protocols.length === 0) continue;
200
+ const pricing = authMode === "paid" && p ? {
201
+ pricingMode: p.pricingMode,
202
+ ...p.price ? { price: p.price } : {},
203
+ ...p.minPrice ? { minPrice: p.minPrice } : {},
204
+ ...p.maxPrice ? { maxPrice: p.maxPrice } : {}
205
+ } : void 0;
206
+ const summary = operation.summary ?? operation.description;
207
+ routes.push({
208
+ path: normalizePath(rawPath),
209
+ method: httpMethod.toUpperCase(),
210
+ ...summary ? { summary } : {},
211
+ authMode,
212
+ ...protocols.length ? { protocols } : {},
213
+ ...pricing ? { pricing } : {}
214
+ });
860
215
  }
861
- const path = normalizePath(absolute.pathname);
862
- resources.push(
863
- createResource(
864
- {
865
- origin: absolute.origin,
866
- method,
867
- path,
868
- source: "well-known/x402",
869
- summary: `${method} ${path}`,
870
- authHint: "paid",
871
- protocolHints: ["x402"],
872
- links: {
873
- wellKnownUrl: sourceUrl,
874
- discoveryUrl: sourceUrl
875
- }
876
- },
877
- 0.65,
878
- ownershipProofs.length > 0 ? "ownership_verified" : "origin_hosted"
879
- )
880
- );
881
216
  }
882
217
  return {
883
- resources,
884
- warnings,
885
- raw: payload
218
+ info: {
219
+ title: doc.info.title,
220
+ ...doc.info.description ? { description: doc.info.description } : {},
221
+ version: doc.info.version
222
+ },
223
+ routes,
224
+ ...doc.info.guidance ? { guidance: doc.info.guidance } : {}
886
225
  };
887
- }
888
- async function runWellKnownX402Stage(options) {
889
- const stageUrl = options.url ?? `${options.origin}/.well-known/x402`;
226
+ });
227
+ async function parseBody(response, url) {
890
228
  try {
891
- const response = await options.fetcher(stageUrl, {
892
- method: "GET",
893
- headers: { Accept: "application/json", ...options.headers },
894
- signal: options.signal
895
- });
896
- if (!response.ok) {
897
- if (response.status === 404) {
898
- return {
899
- stage: "well-known/x402",
900
- valid: false,
901
- resources: [],
902
- warnings: [],
903
- links: { wellKnownUrl: stageUrl }
904
- };
905
- }
906
- return {
907
- stage: "well-known/x402",
908
- valid: false,
909
- resources: [],
910
- warnings: [
911
- warning("FETCH_FAILED", "warn", `Legacy well-known fetch failed (${response.status})`, {
912
- stage: "well-known/x402"
913
- })
914
- ],
915
- links: { wellKnownUrl: stageUrl }
916
- };
917
- }
918
229
  const payload = await response.json();
919
- const parsed = parseWellKnownPayload(payload, options.origin, stageUrl);
920
- return {
921
- stage: "well-known/x402",
922
- valid: parsed.resources.length > 0,
923
- resources: parsed.resources,
924
- warnings: parsed.warnings,
925
- links: { wellKnownUrl: stageUrl },
926
- ...options.includeRaw ? { raw: parsed.raw } : {}
927
- };
928
- } catch (error) {
929
- return {
930
- stage: "well-known/x402",
931
- valid: false,
932
- resources: [],
933
- warnings: [
934
- warning(
935
- "FETCH_FAILED",
936
- "warn",
937
- `Legacy well-known fetch exception: ${error instanceof Error ? error.message : String(error)}`,
938
- {
939
- stage: "well-known/x402"
940
- }
941
- )
942
- ],
943
- links: { wellKnownUrl: stageUrl }
944
- };
230
+ const parsed = OpenApiParsedSchema.safeParse(payload);
231
+ if (!parsed.success) return null;
232
+ return { raw: payload, ...parsed.data, fetchedUrl: url };
233
+ } catch {
234
+ return null;
945
235
  }
946
236
  }
947
-
948
- // src/compat/legacy-x402scan/dns.ts
949
- function parseDnsRecord(record) {
950
- const trimmed = record.trim();
951
- if (!trimmed) return null;
952
- if (/^https?:\/\//i.test(trimmed)) {
953
- return { url: trimmed, legacyPlainUrl: true };
954
- }
955
- const parts = trimmed.split(";").map((entry) => entry.trim()).filter(Boolean);
956
- const keyValues = /* @__PURE__ */ new Map();
957
- for (const part of parts) {
958
- const separator = part.indexOf("=");
959
- if (separator <= 0) continue;
960
- const key = part.slice(0, separator).trim().toLowerCase();
961
- const value = part.slice(separator + 1).trim();
962
- if (key && value) keyValues.set(key, value);
963
- }
964
- if (keyValues.get("v") !== "x4021") return null;
965
- const url = keyValues.get("url");
966
- if (!url || !/^https?:\/\//i.test(url)) return null;
967
- return { url, legacyPlainUrl: false };
237
+ function getOpenAPI(origin, headers, signal, specificationOverrideUrl) {
238
+ const url = specificationOverrideUrl ?? `${origin}/openapi.json`;
239
+ return fetchSafe(url, {
240
+ method: "GET",
241
+ headers: { Accept: "application/json", ...headers },
242
+ signal
243
+ }).andThen((response) => {
244
+ if (!response.ok) return (0, import_neverthrow2.okAsync)(null);
245
+ return import_neverthrow2.ResultAsync.fromSafePromise(parseBody(response, url));
246
+ });
968
247
  }
969
- async function runDnsStage(options) {
970
- if (!options.txtResolver) {
971
- return {
972
- stage: "dns/_x402",
973
- valid: false,
974
- resources: [],
975
- warnings: []
976
- };
977
- }
978
- const origin = normalizeOrigin(options.origin);
979
- const hostname = new URL(origin).hostname;
980
- const fqdn = `_x402.${hostname}`;
981
- let records;
982
- try {
983
- records = await options.txtResolver(fqdn);
984
- } catch (error) {
985
- return {
986
- stage: "dns/_x402",
987
- valid: false,
988
- resources: [],
989
- warnings: [
990
- warning(
991
- "FETCH_FAILED",
992
- "warn",
993
- `DNS TXT lookup failed for ${fqdn}: ${error instanceof Error ? error.message : String(error)}`,
994
- { stage: "dns/_x402" }
995
- )
996
- ]
997
- };
998
- }
999
- if (records.length === 0) {
1000
- return {
1001
- stage: "dns/_x402",
1002
- valid: false,
1003
- resources: [],
1004
- warnings: []
1005
- };
1006
- }
1007
- const warnings = [
1008
- warning(
1009
- "LEGACY_DNS_USED",
1010
- "warn",
1011
- "Using DNS _x402 compatibility path. Migrate to OpenAPI-first discovery.",
248
+
249
+ // src/core/source/wellknown/index.ts
250
+ var import_neverthrow3 = require("neverthrow");
251
+ function toWellKnownParsed(origin, doc) {
252
+ const routes = doc.resources.flatMap((entry) => {
253
+ const trimmed = entry.trim();
254
+ if (!trimmed) return [];
255
+ const parts = trimmed.split(/\s+/);
256
+ const maybeMethod = parts.length >= 2 ? parseMethod(parts[0]) : void 0;
257
+ const target = maybeMethod ? parts.slice(1).join(" ") : trimmed;
258
+ const absolute = toAbsoluteUrl(origin, target);
259
+ if (!absolute) return [];
260
+ return [
1012
261
  {
1013
- stage: "dns/_x402"
262
+ path: normalizePath(absolute.pathname),
263
+ method: maybeMethod ?? DEFAULT_MISSING_METHOD
1014
264
  }
1015
- )
1016
- ];
1017
- const urls = [];
1018
- for (const record of records) {
1019
- const parsed = parseDnsRecord(record);
1020
- if (!parsed) {
1021
- warnings.push(
1022
- warning("PARSE_FAILED", "warn", `Invalid DNS _x402 TXT record: ${record}`, {
1023
- stage: "dns/_x402"
1024
- })
1025
- );
1026
- continue;
1027
- }
1028
- urls.push(parsed.url);
1029
- if (parsed.legacyPlainUrl) {
1030
- warnings.push(
1031
- warning("LEGACY_DNS_PLAIN_URL", "warn", `Legacy plain URL TXT format used: ${record}`, {
1032
- stage: "dns/_x402",
1033
- hint: "Use v=x4021;url=<https-url> format."
1034
- })
1035
- );
1036
- }
1037
- }
1038
- if (urls.length === 0) {
1039
- return {
1040
- stage: "dns/_x402",
1041
- valid: false,
1042
- resources: [],
1043
- warnings,
1044
- ...options.includeRaw ? { raw: { dnsRecords: records } } : {}
1045
- };
1046
- }
1047
- const mergedWarnings = [...warnings];
1048
- const mergedResources = [];
1049
- const rawDocuments = [];
1050
- for (const url of urls) {
1051
- const stageResult = await runWellKnownX402Stage({
1052
- origin,
1053
- url,
1054
- fetcher: options.fetcher,
1055
- headers: options.headers,
1056
- signal: options.signal,
1057
- includeRaw: options.includeRaw
1058
- });
1059
- mergedResources.push(...stageResult.resources);
1060
- mergedWarnings.push(...stageResult.warnings);
1061
- if (options.includeRaw && stageResult.raw !== void 0) {
1062
- rawDocuments.push({ url, document: stageResult.raw });
1063
- }
1064
- }
1065
- const deduped = /* @__PURE__ */ new Map();
1066
- for (const resource of mergedResources) {
1067
- if (!deduped.has(resource.resourceKey)) {
1068
- deduped.set(resource.resourceKey, {
1069
- ...resource,
1070
- source: "dns/_x402",
1071
- links: {
1072
- ...resource.links,
1073
- discoveryUrl: resource.links?.discoveryUrl
1074
- }
1075
- });
1076
- }
1077
- }
1078
- return {
1079
- stage: "dns/_x402",
1080
- valid: deduped.size > 0,
1081
- resources: [...deduped.values()],
1082
- warnings: mergedWarnings,
1083
- ...options.includeRaw ? { raw: { dnsRecords: records, documents: rawDocuments } } : {}
1084
- };
1085
- }
1086
-
1087
- // src/compat/interop-mpp/wellKnownMpp.ts
1088
- function isRecord2(value) {
1089
- return value !== null && typeof value === "object" && !Array.isArray(value);
265
+ ];
266
+ });
267
+ return { routes, ...doc.instructions ? { instructions: doc.instructions } : {} };
1090
268
  }
1091
- async function runInteropMppStage(options) {
1092
- const url = `${options.origin}${WELL_KNOWN_MPP_PATH}`;
269
+ async function parseBody2(response, origin, url) {
1093
270
  try {
1094
- const response = await options.fetcher(url, {
1095
- method: "GET",
1096
- headers: { Accept: "application/json", ...options.headers },
1097
- signal: options.signal
1098
- });
1099
- if (!response.ok) {
1100
- return {
1101
- stage: "interop/mpp",
1102
- valid: false,
1103
- resources: [],
1104
- warnings: []
1105
- };
1106
- }
1107
271
  const payload = await response.json();
1108
- if (!isRecord2(payload)) {
1109
- return {
1110
- stage: "interop/mpp",
1111
- valid: false,
1112
- resources: [],
1113
- warnings: [
1114
- warning("PARSE_FAILED", "warn", "Interop /.well-known/mpp payload is not an object", {
1115
- stage: "interop/mpp"
1116
- })
1117
- ],
1118
- ...options.includeRaw ? { raw: payload } : {}
1119
- };
1120
- }
1121
- const warnings = [
1122
- warning(
1123
- "INTEROP_MPP_USED",
1124
- "info",
1125
- "Using /.well-known/mpp interop adapter. This is additive and non-canonical.",
1126
- {
1127
- stage: "interop/mpp",
1128
- hint: "Use for registry indexing only, not canonical runtime routing."
1129
- }
1130
- )
1131
- ];
1132
- const resources = [];
1133
- const fromStringResources = Array.isArray(payload.resources) ? payload.resources.filter((entry) => typeof entry === "string") : [];
1134
- for (const entry of fromStringResources) {
1135
- const parts = entry.trim().split(/\s+/);
1136
- let method = parseMethod(parts[0]);
1137
- let path = parts.join(" ");
1138
- if (method) {
1139
- path = parts.slice(1).join(" ");
1140
- } else {
1141
- method = "POST";
1142
- }
1143
- const normalizedPath = normalizePath(path);
1144
- resources.push(
1145
- createResource(
1146
- {
1147
- origin: options.origin,
1148
- method,
1149
- path: normalizedPath,
1150
- source: "interop/mpp",
1151
- summary: `${method} ${normalizedPath}`,
1152
- authHint: "paid",
1153
- protocolHints: ["mpp"],
1154
- links: { discoveryUrl: url }
1155
- },
1156
- 0.45,
1157
- "unverified"
1158
- )
1159
- );
1160
- }
1161
- const fromServiceObjects = Array.isArray(payload.services) ? payload.services.filter((entry) => isRecord2(entry)) : [];
1162
- for (const service of fromServiceObjects) {
1163
- const path = typeof service.path === "string" ? service.path : void 0;
1164
- const method = parseMethod(typeof service.method === "string" ? service.method : void 0) ?? "POST";
1165
- if (!path) continue;
1166
- const normalizedPath = normalizePath(path);
1167
- resources.push(
1168
- createResource(
1169
- {
1170
- origin: options.origin,
1171
- method,
1172
- path: normalizedPath,
1173
- source: "interop/mpp",
1174
- summary: typeof service.summary === "string" ? service.summary : `${method} ${normalizedPath}`,
1175
- authHint: "paid",
1176
- protocolHints: ["mpp"],
1177
- links: { discoveryUrl: url }
1178
- },
1179
- 0.45,
1180
- "unverified"
1181
- )
1182
- );
1183
- }
1184
- const deduped = /* @__PURE__ */ new Map();
1185
- for (const resource of resources) {
1186
- if (!deduped.has(resource.resourceKey)) deduped.set(resource.resourceKey, resource);
1187
- }
1188
- return {
1189
- stage: "interop/mpp",
1190
- valid: deduped.size > 0,
1191
- resources: [...deduped.values()],
1192
- warnings,
1193
- ...options.includeRaw ? { raw: payload } : {}
1194
- };
1195
- } catch (error) {
1196
- return {
1197
- stage: "interop/mpp",
1198
- valid: false,
1199
- resources: [],
1200
- warnings: [
1201
- warning(
1202
- "FETCH_FAILED",
1203
- "warn",
1204
- `Interop /.well-known/mpp fetch failed: ${error instanceof Error ? error.message : String(error)}`,
1205
- {
1206
- stage: "interop/mpp"
1207
- }
1208
- )
1209
- ]
1210
- };
272
+ const doc = WellKnownDocSchema.safeParse(payload);
273
+ if (!doc.success) return null;
274
+ const parsed = WellKnownParsedSchema.safeParse(toWellKnownParsed(origin, doc.data));
275
+ if (!parsed.success) return null;
276
+ return { raw: payload, ...parsed.data, fetchedUrl: url };
277
+ } catch {
278
+ return null;
1211
279
  }
1212
280
  }
1213
-
1214
- // src/core/token.ts
1215
- function estimateTokenCount(text) {
1216
- return Math.ceil(text.length / 4);
281
+ function getWellKnown(origin, headers, signal) {
282
+ const url = `${origin}/.well-known/x402`;
283
+ return fetchSafe(url, {
284
+ method: "GET",
285
+ headers: { Accept: "application/json", ...headers },
286
+ signal
287
+ }).andThen((response) => {
288
+ if (!response.ok) return (0, import_neverthrow3.okAsync)(null);
289
+ return import_neverthrow3.ResultAsync.fromSafePromise(parseBody2(response, origin, url));
290
+ });
1217
291
  }
1218
292
 
1219
- // src/core/openapi.ts
1220
- function isRecord3(value) {
1221
- return value !== null && typeof value === "object" && !Array.isArray(value);
293
+ // src/core/layers/l2.ts
294
+ function formatPrice(pricing) {
295
+ if (pricing.pricingMode === "fixed") return `$${pricing.price}`;
296
+ if (pricing.pricingMode === "range") return `$${pricing.minPrice}-$${pricing.maxPrice}`;
297
+ return pricing.maxPrice ? `up to $${pricing.maxPrice}` : "quote";
298
+ }
299
+ function checkL2ForOpenAPI(openApi) {
300
+ const routes = openApi.routes.map((route) => ({
301
+ path: route.path,
302
+ method: route.method,
303
+ summary: route.summary ?? `${route.method} ${route.path}`,
304
+ ...route.authMode ? { authMode: route.authMode } : {},
305
+ ...route.pricing ? { price: formatPrice(route.pricing) } : {},
306
+ ...route.protocols?.length ? { protocols: route.protocols } : {}
307
+ }));
308
+ return {
309
+ ...openApi.info.title ? { title: openApi.info.title } : {},
310
+ ...openApi.info.description ? { description: openApi.info.description } : {},
311
+ routes,
312
+ source: "openapi"
313
+ };
1222
314
  }
1223
- function asString(value) {
1224
- return typeof value === "string" && value.length > 0 ? value : void 0;
315
+ function checkL2ForWellknown(wellKnown) {
316
+ const routes = wellKnown.routes.map((route) => ({
317
+ path: route.path,
318
+ method: route.method,
319
+ summary: `${route.method} ${route.path}`,
320
+ authMode: "paid",
321
+ protocols: ["x402"]
322
+ }));
323
+ return { routes, source: "well-known/x402" };
1225
324
  }
1226
- function parsePriceValue(value) {
1227
- if (typeof value === "string" && value.length > 0) return value;
1228
- if (typeof value === "number" && Number.isFinite(value)) return String(value);
1229
- return void 0;
325
+
326
+ // src/core/layers/l4.ts
327
+ function checkL4ForOpenAPI(openApi) {
328
+ if (openApi.guidance) {
329
+ return { guidance: openApi.guidance, source: "openapi" };
330
+ }
331
+ return null;
1230
332
  }
1231
- function require402Response(operation) {
1232
- const responses = operation.responses;
1233
- if (!isRecord3(responses)) return false;
1234
- return Boolean(responses["402"]);
333
+ function checkL4ForWellknown(wellKnown) {
334
+ if (wellKnown.instructions) {
335
+ return { guidance: wellKnown.instructions, source: "well-known/x402" };
336
+ }
337
+ return null;
1235
338
  }
1236
- function parseProtocols(paymentInfo) {
1237
- const protocols = paymentInfo.protocols;
1238
- if (!Array.isArray(protocols)) return [];
1239
- return protocols.filter(
1240
- (entry) => typeof entry === "string" && entry.length > 0
1241
- );
339
+
340
+ // src/audit/codes.ts
341
+ var AUDIT_CODES = {
342
+ // ─── Source presence ─────────────────────────────────────────────────────────
343
+ OPENAPI_NOT_FOUND: "OPENAPI_NOT_FOUND",
344
+ WELLKNOWN_NOT_FOUND: "WELLKNOWN_NOT_FOUND",
345
+ // ─── OpenAPI quality ─────────────────────────────────────────────────────────
346
+ OPENAPI_NO_ROUTES: "OPENAPI_NO_ROUTES",
347
+ // ─── L2 route-list checks ────────────────────────────────────────────────────
348
+ L2_NO_ROUTES: "L2_NO_ROUTES",
349
+ L2_ROUTE_COUNT_HIGH: "L2_ROUTE_COUNT_HIGH",
350
+ L2_AUTH_MODE_MISSING: "L2_AUTH_MODE_MISSING",
351
+ L2_PRICE_MISSING_ON_PAID: "L2_PRICE_MISSING_ON_PAID",
352
+ L2_PROTOCOLS_MISSING_ON_PAID: "L2_PROTOCOLS_MISSING_ON_PAID",
353
+ // ─── L3 endpoint advisory checks ─────────────────────────────────────────────
354
+ L3_NOT_FOUND: "L3_NOT_FOUND",
355
+ L3_INPUT_SCHEMA_MISSING: "L3_INPUT_SCHEMA_MISSING",
356
+ L3_AUTH_MODE_MISSING: "L3_AUTH_MODE_MISSING",
357
+ L3_PROTOCOLS_MISSING_ON_PAID: "L3_PROTOCOLS_MISSING_ON_PAID",
358
+ // ─── L4 guidance checks ──────────────────────────────────────────────────────
359
+ L4_GUIDANCE_MISSING: "L4_GUIDANCE_MISSING",
360
+ L4_GUIDANCE_TOO_LONG: "L4_GUIDANCE_TOO_LONG"
361
+ };
362
+
363
+ // src/audit/warnings.ts
364
+ var GUIDANCE_TOO_LONG_CHARS = 4e3;
365
+ var ROUTE_COUNT_HIGH = 40;
366
+ function getWarningsForOpenAPI(openApi) {
367
+ if (openApi === null) {
368
+ return [
369
+ {
370
+ code: AUDIT_CODES.OPENAPI_NOT_FOUND,
371
+ severity: "info",
372
+ message: "No OpenAPI specification found at this origin.",
373
+ hint: "Expose an OpenAPI spec (e.g. /openapi.json) for richer discovery."
374
+ }
375
+ ];
376
+ }
377
+ const warnings = [];
378
+ if (openApi.routes.length === 0) {
379
+ warnings.push({
380
+ code: AUDIT_CODES.OPENAPI_NO_ROUTES,
381
+ severity: "warn",
382
+ message: "OpenAPI spec found but contains no route definitions.",
383
+ hint: "Add paths to your OpenAPI spec so agents can discover endpoints.",
384
+ path: "paths"
385
+ });
386
+ }
387
+ return warnings;
1242
388
  }
1243
- function parseAuthMode(operation) {
1244
- const auth = operation["x-agentcash-auth"];
1245
- if (!isRecord3(auth)) return void 0;
1246
- const mode = auth.mode;
1247
- if (mode === "paid" || mode === "siwx" || mode === "apiKey" || mode === "unprotected") {
1248
- return mode;
389
+ function getWarningsForWellKnown(wellKnown) {
390
+ if (wellKnown === null) {
391
+ return [
392
+ {
393
+ code: AUDIT_CODES.WELLKNOWN_NOT_FOUND,
394
+ severity: "info",
395
+ message: "No /.well-known/x402 resource found at this origin.",
396
+ hint: "Expose /.well-known/x402 as a fallback for agents that cannot read OpenAPI."
397
+ }
398
+ ];
1249
399
  }
1250
- return void 0;
400
+ return [];
1251
401
  }
1252
- async function runOpenApiStage(options) {
402
+ function getWarningsForL2(l2) {
1253
403
  const warnings = [];
1254
- const resources = [];
1255
- let fetchedUrl;
1256
- let document;
1257
- for (const path of OPENAPI_PATH_CANDIDATES) {
1258
- const url = `${options.origin}${path}`;
1259
- try {
1260
- const response = await options.fetcher(url, {
1261
- method: "GET",
1262
- headers: { Accept: "application/json", ...options.headers },
1263
- signal: options.signal
404
+ if (l2.routes.length === 0) {
405
+ warnings.push({
406
+ code: AUDIT_CODES.L2_NO_ROUTES,
407
+ severity: "warn",
408
+ message: "No routes found from any discovery source."
409
+ });
410
+ return warnings;
411
+ }
412
+ if (l2.routes.length > ROUTE_COUNT_HIGH) {
413
+ warnings.push({
414
+ code: AUDIT_CODES.L2_ROUTE_COUNT_HIGH,
415
+ severity: "warn",
416
+ message: `High route count (${l2.routes.length}) may exceed agent token budgets for zero-hop injection.`,
417
+ hint: "Consider grouping routes or reducing the advertised surface."
418
+ });
419
+ }
420
+ for (const route of l2.routes) {
421
+ const loc = `${route.method} ${route.path}`;
422
+ if (!route.authMode) {
423
+ warnings.push({
424
+ code: AUDIT_CODES.L2_AUTH_MODE_MISSING,
425
+ severity: "warn",
426
+ message: `Route ${loc} is missing an auth mode declaration.`,
427
+ hint: "Set x-payment-info.authMode (or securitySchemes) so agents know if payment is required.",
428
+ path: route.path
1264
429
  });
1265
- if (!response.ok) {
1266
- if (response.status !== 404) {
1267
- warnings.push(
1268
- warning("FETCH_FAILED", "warn", `OpenAPI fetch failed (${response.status}) at ${url}`, {
1269
- stage: "openapi"
1270
- })
1271
- );
1272
- }
1273
- continue;
430
+ }
431
+ if (route.authMode === "paid") {
432
+ if (!route.price) {
433
+ warnings.push({
434
+ code: AUDIT_CODES.L2_PRICE_MISSING_ON_PAID,
435
+ severity: "warn",
436
+ message: `Paid route ${loc} has no price hint.`,
437
+ hint: "Add x-payment-info.price (or minPrice/maxPrice) so agents can budget before calling.",
438
+ path: route.path
439
+ });
1274
440
  }
1275
- const payload = await response.json();
1276
- if (!isRecord3(payload)) {
1277
- warnings.push(
1278
- warning("PARSE_FAILED", "error", `OpenAPI payload at ${url} is not a JSON object`, {
1279
- stage: "openapi"
1280
- })
1281
- );
1282
- continue;
441
+ if (!route.protocols?.length) {
442
+ warnings.push({
443
+ code: AUDIT_CODES.L2_PROTOCOLS_MISSING_ON_PAID,
444
+ severity: "info",
445
+ message: `Paid route ${loc} does not declare supported payment protocols.`,
446
+ hint: "Add x-payment-info.protocols (e.g. ['x402']) to signal which payment flows are accepted.",
447
+ path: route.path
448
+ });
1283
449
  }
1284
- document = payload;
1285
- fetchedUrl = url;
1286
- break;
1287
- } catch (error) {
1288
- warnings.push(
1289
- warning(
1290
- "FETCH_FAILED",
1291
- "warn",
1292
- `OpenAPI fetch exception at ${url}: ${error instanceof Error ? error.message : String(error)}`,
1293
- { stage: "openapi" }
1294
- )
1295
- );
1296
450
  }
1297
451
  }
1298
- if (!document || !fetchedUrl) {
1299
- return {
1300
- stage: "openapi",
1301
- valid: false,
1302
- resources: [],
1303
- warnings
1304
- };
452
+ return warnings;
453
+ }
454
+ function getWarningsForL3(l3) {
455
+ if (l3 === null) {
456
+ return [
457
+ {
458
+ code: AUDIT_CODES.L3_NOT_FOUND,
459
+ severity: "info",
460
+ message: "No spec data found for this endpoint.",
461
+ hint: "Ensure the path is defined in your OpenAPI spec or reachable via probe."
462
+ }
463
+ ];
1305
464
  }
1306
- const hasTopLevel = typeof document.openapi === "string" && isRecord3(document.info) && typeof document.info.title === "string" && typeof document.info.version === "string" && isRecord3(document.paths);
1307
- if (!hasTopLevel) {
1308
- warnings.push(
1309
- warning("OPENAPI_TOP_LEVEL_INVALID", "error", "OpenAPI required fields are missing", {
1310
- stage: "openapi"
1311
- })
1312
- );
1313
- return {
1314
- stage: "openapi",
1315
- valid: false,
1316
- resources: [],
1317
- warnings,
1318
- links: { openapiUrl: fetchedUrl },
1319
- ...options.includeRaw ? { raw: document } : {}
1320
- };
465
+ const warnings = [];
466
+ if (!l3.authMode) {
467
+ warnings.push({
468
+ code: AUDIT_CODES.L3_AUTH_MODE_MISSING,
469
+ severity: "warn",
470
+ message: "Endpoint has no auth mode in the spec.",
471
+ hint: "Declare auth mode via security schemes or x-payment-info so agents can plan payment."
472
+ });
1321
473
  }
1322
- const paths = document.paths;
1323
- const provenance = isRecord3(document["x-agentcash-provenance"]) ? document["x-agentcash-provenance"] : void 0;
1324
- const ownershipProofs = Array.isArray(provenance?.ownershipProofs) ? provenance.ownershipProofs.filter((entry) => typeof entry === "string") : [];
1325
- const guidance = isRecord3(document["x-agentcash-guidance"]) ? document["x-agentcash-guidance"] : void 0;
1326
- const llmsTxtUrl = asString(guidance?.llmsTxtUrl);
1327
- if (ownershipProofs.length > 0) {
1328
- warnings.push(
1329
- warning("OPENAPI_OWNERSHIP_PROOFS_PRESENT", "info", "OpenAPI ownership proofs detected", {
1330
- stage: "openapi"
1331
- })
1332
- );
474
+ if (l3.authMode === "paid" && !l3.inputSchema) {
475
+ warnings.push({
476
+ code: AUDIT_CODES.L3_INPUT_SCHEMA_MISSING,
477
+ severity: "warn",
478
+ message: "Paid endpoint is missing an input schema.",
479
+ hint: "Add a requestBody or parameters schema so agents can construct valid payloads."
480
+ });
1333
481
  }
1334
- for (const [rawPath, rawPathItem] of Object.entries(paths)) {
1335
- if (!isRecord3(rawPathItem)) {
1336
- warnings.push(
1337
- warning("OPENAPI_OPERATION_INVALID", "warn", `Path item ${rawPath} is not an object`, {
1338
- stage: "openapi"
1339
- })
1340
- );
1341
- continue;
1342
- }
1343
- for (const [rawMethod, rawOperation] of Object.entries(rawPathItem)) {
1344
- const method = parseMethod(rawMethod);
1345
- if (!method) continue;
1346
- if (!isRecord3(rawOperation)) {
1347
- warnings.push(
1348
- warning(
1349
- "OPENAPI_OPERATION_INVALID",
1350
- "warn",
1351
- `${rawMethod.toUpperCase()} ${rawPath} is not an object`,
1352
- {
1353
- stage: "openapi"
1354
- }
1355
- )
1356
- );
1357
- continue;
1358
- }
1359
- const operation = rawOperation;
1360
- const summary = asString(operation.summary) ?? asString(operation.description);
1361
- if (!summary) {
1362
- warnings.push(
1363
- warning(
1364
- "OPENAPI_SUMMARY_MISSING",
1365
- "warn",
1366
- `${method} ${rawPath} missing summary/description, using fallback summary`,
1367
- { stage: "openapi" }
1368
- )
1369
- );
1370
- }
1371
- const authMode = parseAuthMode(operation);
1372
- if (!authMode) {
1373
- warnings.push(
1374
- warning(
1375
- "OPENAPI_AUTH_MODE_MISSING",
1376
- "error",
1377
- `${method} ${rawPath} missing x-agentcash-auth.mode`,
1378
- {
1379
- stage: "openapi"
1380
- }
1381
- )
1382
- );
1383
- continue;
1384
- }
1385
- if ((authMode === "paid" || authMode === "siwx") && !require402Response(operation)) {
1386
- warnings.push(
1387
- warning(
1388
- "OPENAPI_402_MISSING",
1389
- "error",
1390
- `${method} ${rawPath} requires 402 response for authMode=${authMode}`,
1391
- { stage: "openapi" }
1392
- )
1393
- );
1394
- continue;
1395
- }
1396
- const paymentInfo = isRecord3(operation["x-payment-info"]) ? operation["x-payment-info"] : void 0;
1397
- const protocols = paymentInfo ? parseProtocols(paymentInfo) : [];
1398
- if (authMode === "paid" && protocols.length === 0) {
1399
- warnings.push(
1400
- warning(
1401
- "OPENAPI_PAID_PROTOCOLS_MISSING",
1402
- "error",
1403
- `${method} ${rawPath} must define x-payment-info.protocols when authMode=paid`,
1404
- { stage: "openapi" }
1405
- )
1406
- );
1407
- continue;
1408
- }
1409
- let pricing;
1410
- let priceHint;
1411
- if (paymentInfo && authMode === "paid") {
1412
- const pricingModeRaw = asString(paymentInfo.pricingMode);
1413
- const price = parsePriceValue(paymentInfo.price);
1414
- const minPrice = parsePriceValue(paymentInfo.minPrice);
1415
- const maxPrice = parsePriceValue(paymentInfo.maxPrice);
1416
- const inferredPricingMode = pricingModeRaw ?? (price ? "fixed" : minPrice && maxPrice ? "range" : "quote");
1417
- if (inferredPricingMode !== "fixed" && inferredPricingMode !== "range" && inferredPricingMode !== "quote") {
1418
- warnings.push(
1419
- warning(
1420
- "OPENAPI_PRICING_INVALID",
1421
- "error",
1422
- `${method} ${rawPath} has invalid pricingMode`,
1423
- {
1424
- stage: "openapi"
1425
- }
1426
- )
1427
- );
1428
- continue;
1429
- }
1430
- if (inferredPricingMode === "fixed" && !price) {
1431
- warnings.push(
1432
- warning(
1433
- "OPENAPI_PRICING_INVALID",
1434
- "error",
1435
- `${method} ${rawPath} fixed pricing requires price`,
1436
- {
1437
- stage: "openapi"
1438
- }
1439
- )
1440
- );
1441
- continue;
1442
- }
1443
- if (inferredPricingMode === "range") {
1444
- if (!minPrice || !maxPrice) {
1445
- warnings.push(
1446
- warning(
1447
- "OPENAPI_PRICING_INVALID",
1448
- "error",
1449
- `${method} ${rawPath} range pricing requires minPrice and maxPrice`,
1450
- { stage: "openapi" }
1451
- )
1452
- );
1453
- continue;
1454
- }
1455
- const min = Number(minPrice);
1456
- const max = Number(maxPrice);
1457
- if (!Number.isFinite(min) || !Number.isFinite(max) || min > max) {
1458
- warnings.push(
1459
- warning(
1460
- "OPENAPI_PRICING_INVALID",
1461
- "error",
1462
- `${method} ${rawPath} range pricing requires numeric minPrice <= maxPrice`,
1463
- { stage: "openapi" }
1464
- )
1465
- );
1466
- continue;
1467
- }
1468
- }
1469
- pricing = {
1470
- pricingMode: inferredPricingMode,
1471
- ...price ? { price } : {},
1472
- ...minPrice ? { minPrice } : {},
1473
- ...maxPrice ? { maxPrice } : {}
1474
- };
1475
- priceHint = inferredPricingMode === "fixed" ? price : inferredPricingMode === "range" ? `${minPrice}-${maxPrice}` : maxPrice;
482
+ if (l3.authMode === "paid" && !l3.protocols?.length) {
483
+ warnings.push({
484
+ code: AUDIT_CODES.L3_PROTOCOLS_MISSING_ON_PAID,
485
+ severity: "info",
486
+ message: "Paid endpoint does not declare supported payment protocols.",
487
+ hint: "Add x-payment-info.protocols (e.g. ['x402']) to the operation."
488
+ });
489
+ }
490
+ return warnings;
491
+ }
492
+ function getWarningsForL4(l4) {
493
+ if (l4 === null) {
494
+ return [
495
+ {
496
+ code: AUDIT_CODES.L4_GUIDANCE_MISSING,
497
+ severity: "info",
498
+ message: "No guidance text found (llms.txt or OpenAPI info.guidance).",
499
+ hint: "Add an info.guidance field to your OpenAPI spec or expose /llms.txt for agent-readable instructions."
1476
500
  }
1477
- const resource = createResource(
1478
- {
1479
- origin: options.origin,
1480
- method,
1481
- path: normalizePath(rawPath),
1482
- source: "openapi",
1483
- summary: summary ?? `${method} ${normalizePath(rawPath)}`,
1484
- authHint: authMode,
1485
- protocolHints: protocols,
1486
- ...priceHint ? { priceHint } : {},
1487
- ...pricing ? { pricing } : {},
1488
- links: {
1489
- openapiUrl: fetchedUrl,
1490
- ...llmsTxtUrl ? { llmsTxtUrl } : {}
1491
- }
1492
- },
1493
- 0.95,
1494
- ownershipProofs.length > 0 ? "ownership_verified" : "origin_hosted"
1495
- );
1496
- resources.push(resource);
1497
- }
501
+ ];
1498
502
  }
1499
- if (llmsTxtUrl) {
1500
- try {
1501
- const llmsResponse = await options.fetcher(llmsTxtUrl, {
1502
- method: "GET",
1503
- headers: { Accept: "text/plain", ...options.headers },
1504
- signal: options.signal
1505
- });
1506
- if (llmsResponse.ok) {
1507
- const llmsText = await llmsResponse.text();
1508
- const tokenCount = estimateTokenCount(llmsText);
1509
- if (tokenCount > LLMS_TOKEN_WARNING_THRESHOLD) {
1510
- warnings.push(
1511
- warning(
1512
- "LLMSTXT_TOO_LARGE",
1513
- "warn",
1514
- `llms.txt estimated ${tokenCount} tokens (threshold ${LLMS_TOKEN_WARNING_THRESHOLD})`,
1515
- {
1516
- stage: "openapi",
1517
- hint: "Keep llms.txt concise and domain-level only."
1518
- }
1519
- )
1520
- );
1521
- }
1522
- if (options.includeRaw) {
1523
- return {
1524
- stage: "openapi",
1525
- valid: resources.length > 0,
1526
- resources,
1527
- warnings,
1528
- links: { openapiUrl: fetchedUrl, llmsTxtUrl },
1529
- raw: {
1530
- openapi: document,
1531
- llmsTxt: llmsText
1532
- }
1533
- };
1534
- }
1535
- } else {
1536
- warnings.push(
1537
- warning(
1538
- "LLMSTXT_FETCH_FAILED",
1539
- "warn",
1540
- `llms.txt fetch failed (${llmsResponse.status})`,
1541
- {
1542
- stage: "openapi"
1543
- }
1544
- )
1545
- );
503
+ if (l4.guidance.length > GUIDANCE_TOO_LONG_CHARS) {
504
+ return [
505
+ {
506
+ code: AUDIT_CODES.L4_GUIDANCE_TOO_LONG,
507
+ severity: "warn",
508
+ message: `Guidance text is ${l4.guidance.length} characters, which may exceed zero-hop token budgets.`,
509
+ hint: "Trim guidance to under ~4000 characters for reliable zero-hop context injection."
1546
510
  }
1547
- } catch (error) {
1548
- warnings.push(
1549
- warning(
1550
- "LLMSTXT_FETCH_FAILED",
1551
- "warn",
1552
- `llms.txt fetch failed: ${error instanceof Error ? error.message : String(error)}`,
1553
- { stage: "openapi" }
1554
- )
1555
- );
1556
- }
511
+ ];
1557
512
  }
513
+ return [];
514
+ }
515
+
516
+ // src/core/source/probe/index.ts
517
+ var import_neverthrow4 = require("neverthrow");
518
+
519
+ // src/core/protocols/x402/v1/schema.ts
520
+ function extractSchemas(accepts) {
521
+ const first = accepts[0];
522
+ if (!isRecord(first)) return {};
523
+ const outputSchema = isRecord(first.outputSchema) ? first.outputSchema : void 0;
1558
524
  return {
1559
- stage: "openapi",
1560
- valid: resources.length > 0,
1561
- resources,
1562
- warnings,
1563
- links: {
1564
- openapiUrl: fetchedUrl,
1565
- ...llmsTxtUrl ? { llmsTxtUrl } : {}
1566
- },
1567
- ...options.includeRaw ? { raw: { openapi: document } } : {}
525
+ ...outputSchema && isRecord(outputSchema.input) ? { inputSchema: outputSchema.input } : {},
526
+ ...outputSchema && isRecord(outputSchema.output) ? { outputSchema: outputSchema.output } : {}
1568
527
  };
1569
528
  }
1570
529
 
1571
- // src/core/probe.ts
1572
- function detectAuthHintFrom402Payload(payload) {
1573
- if (payload && typeof payload === "object") {
1574
- const asRecord = payload;
1575
- const extensions = asRecord.extensions;
1576
- if (extensions && typeof extensions === "object") {
1577
- const extRecord = extensions;
1578
- if (extRecord["sign-in-with-x"]) return "siwx";
1579
- }
530
+ // src/core/protocols/x402/shared.ts
531
+ function asNonEmptyString(value) {
532
+ if (typeof value !== "string") return void 0;
533
+ const trimmed = value.trim();
534
+ return trimmed.length > 0 ? trimmed : void 0;
535
+ }
536
+ function asPositiveNumber(value) {
537
+ return typeof value === "number" && Number.isFinite(value) && value > 0 ? value : void 0;
538
+ }
539
+
540
+ // src/core/protocols/x402/v1/payment-options.ts
541
+ function extractPaymentOptions(accepts) {
542
+ return accepts.flatMap((accept) => {
543
+ if (!isRecord(accept)) return [];
544
+ const network = asNonEmptyString(accept.network);
545
+ const asset = asNonEmptyString(accept.asset);
546
+ const maxAmountRequired = asNonEmptyString(accept.maxAmountRequired);
547
+ if (!network || !asset || !maxAmountRequired) return [];
548
+ const scheme = asNonEmptyString(accept.scheme);
549
+ const payTo = asNonEmptyString(accept.payTo);
550
+ const maxTimeoutSeconds = asPositiveNumber(accept.maxTimeoutSeconds);
551
+ return [
552
+ {
553
+ protocol: "x402",
554
+ version: 1,
555
+ network,
556
+ asset,
557
+ maxAmountRequired,
558
+ ...scheme ? { scheme } : {},
559
+ ...payTo ? { payTo } : {},
560
+ ...maxTimeoutSeconds ? { maxTimeoutSeconds } : {}
561
+ }
562
+ ];
563
+ });
564
+ }
565
+
566
+ // src/core/protocols/x402/v2/schema.ts
567
+ function extractSchemas2(payload) {
568
+ if (!isRecord(payload)) return {};
569
+ const extensions = isRecord(payload.extensions) ? payload.extensions : void 0;
570
+ const bazaar = extensions && isRecord(extensions.bazaar) ? extensions.bazaar : void 0;
571
+ const schema = bazaar && isRecord(bazaar.schema) ? bazaar.schema : void 0;
572
+ if (!schema) return {};
573
+ const schemaProps = isRecord(schema.properties) ? schema.properties : void 0;
574
+ if (!schemaProps) return {};
575
+ const inputProps = isRecord(schemaProps.input) ? schemaProps.input : void 0;
576
+ const inputProperties = inputProps && isRecord(inputProps.properties) ? inputProps.properties : void 0;
577
+ const inputSchema = (inputProperties && isRecord(inputProperties.body) ? inputProperties.body : void 0) ?? (inputProperties && isRecord(inputProperties.queryParams) ? inputProperties.queryParams : void 0);
578
+ const outputProps = isRecord(schemaProps.output) ? schemaProps.output : void 0;
579
+ const outputProperties = outputProps && isRecord(outputProps.properties) ? outputProps.properties : void 0;
580
+ const outputSchema = outputProperties && isRecord(outputProperties.example) ? outputProperties.example : void 0;
581
+ return {
582
+ ...inputSchema ? { inputSchema } : {},
583
+ ...outputSchema ? { outputSchema } : {}
584
+ };
585
+ }
586
+
587
+ // src/core/protocols/x402/v2/payment-options.ts
588
+ function extractPaymentOptions2(accepts) {
589
+ return accepts.flatMap((accept) => {
590
+ if (!isRecord(accept)) return [];
591
+ const network = asNonEmptyString(accept.network);
592
+ const asset = asNonEmptyString(accept.asset);
593
+ const amount = asNonEmptyString(accept.amount);
594
+ if (!network || !asset || !amount) return [];
595
+ const scheme = asNonEmptyString(accept.scheme);
596
+ const payTo = asNonEmptyString(accept.payTo);
597
+ const maxTimeoutSeconds = asPositiveNumber(accept.maxTimeoutSeconds);
598
+ return [
599
+ {
600
+ protocol: "x402",
601
+ version: 2,
602
+ network,
603
+ asset,
604
+ amount,
605
+ ...scheme ? { scheme } : {},
606
+ ...payTo ? { payTo } : {},
607
+ ...maxTimeoutSeconds ? { maxTimeoutSeconds } : {}
608
+ }
609
+ ];
610
+ });
611
+ }
612
+
613
+ // src/core/protocols/x402/index.ts
614
+ function parseVersion(payload) {
615
+ if (!isRecord(payload)) return void 0;
616
+ const v = payload.x402Version;
617
+ if (v === 1 || v === 2) return v;
618
+ return void 0;
619
+ }
620
+ function detectAuthHint(payload) {
621
+ if (isRecord(payload) && isRecord(payload.extensions)) {
622
+ if (payload.extensions["api-key"]) return "apiKey+paid";
623
+ if (payload.extensions["sign-in-with-x"]) return "siwx";
1580
624
  }
1581
625
  return "paid";
1582
626
  }
627
+ function extractPaymentOptions3(payload) {
628
+ if (!isRecord(payload)) return [];
629
+ const version = parseVersion(payload);
630
+ const accepts = Array.isArray(payload.accepts) ? payload.accepts : [];
631
+ if (version === 1) return extractPaymentOptions(accepts);
632
+ if (version === 2) return extractPaymentOptions2(accepts);
633
+ return [];
634
+ }
635
+ function extractSchemas3(payload) {
636
+ if (!isRecord(payload)) return {};
637
+ const version = parseVersion(payload);
638
+ if (version === 2) return extractSchemas2(payload);
639
+ if (version === 1) {
640
+ const accepts = Array.isArray(payload.accepts) ? payload.accepts : [];
641
+ return extractSchemas(accepts);
642
+ }
643
+ return {};
644
+ }
645
+ function parseInputSchema(payload) {
646
+ return extractSchemas3(payload).inputSchema;
647
+ }
648
+ function parseOutputSchema(payload) {
649
+ return extractSchemas3(payload).outputSchema;
650
+ }
651
+
652
+ // src/core/protocols/x402/v1/parse-payment-required.ts
653
+ async function parsePaymentRequiredBody(response) {
654
+ const payload = await response.clone().json();
655
+ if (!isRecord(payload) || payload.x402Version !== 1) return null;
656
+ return payload;
657
+ }
658
+
659
+ // src/core/protocols/x402/v2/parse-payment-required.ts
660
+ function parsePaymentRequiredBody2(headerValue) {
661
+ const payload = JSON.parse(atob(headerValue));
662
+ if (!isRecord(payload) || payload.x402Version !== 2) return null;
663
+ return payload;
664
+ }
665
+
666
+ // src/core/source/probe/index.ts
667
+ function isUsableStatus(status) {
668
+ return status === 402 || status >= 200 && status < 300;
669
+ }
1583
670
  function detectProtocols(response) {
1584
671
  const protocols = /* @__PURE__ */ new Set();
1585
672
  const directHeader = response.headers.get("x-payment-protocol");
@@ -1594,767 +681,548 @@ function detectProtocols(response) {
1594
681
  if (isMmmEnabled() && authHeader.includes("mpp")) protocols.add("mpp");
1595
682
  return [...protocols];
1596
683
  }
1597
- async function runProbeStage(options) {
1598
- const candidates = options.probeCandidates ?? [];
1599
- if (candidates.length === 0) {
1600
- return {
1601
- stage: "probe",
1602
- valid: false,
1603
- resources: [],
1604
- warnings: [
1605
- warning(
1606
- "PROBE_STAGE_SKIPPED",
1607
- "info",
1608
- "Probe stage skipped because no probe candidates were provided.",
1609
- { stage: "probe" }
1610
- )
1611
- ]
1612
- };
684
+ function buildProbeUrl(url, method, inputBody) {
685
+ if (!inputBody || method !== "GET" && method !== "HEAD") return url;
686
+ const u = new URL(url);
687
+ for (const [key, value] of Object.entries(inputBody)) {
688
+ u.searchParams.set(key, String(value));
1613
689
  }
1614
- const resources = [];
1615
- const warnings = [];
1616
- for (const candidate of candidates) {
1617
- const path = normalizePath(candidate.path);
1618
- const methods = candidate.methods?.length ? candidate.methods : DEFAULT_PROBE_METHODS;
1619
- for (const method of methods) {
1620
- const url = `${options.origin}${path}`;
1621
- try {
1622
- const response = await options.fetcher(url, {
1623
- method,
1624
- headers: {
1625
- Accept: "application/json, text/plain;q=0.9, */*;q=0.8",
1626
- ...options.headers
1627
- },
1628
- signal: options.signal
1629
- });
1630
- if (response.status !== 402 && (response.status < 200 || response.status >= 300)) {
1631
- continue;
1632
- }
690
+ return u.toString();
691
+ }
692
+ function probeMethod(url, method, path, headers, signal, inputBody) {
693
+ const hasBody = inputBody !== void 0 && method !== "GET" && method !== "HEAD";
694
+ const probeUrl = buildProbeUrl(url, method, inputBody);
695
+ return fetchSafe(probeUrl, {
696
+ method,
697
+ headers: {
698
+ Accept: "application/json, text/plain;q=0.9, */*;q=0.8",
699
+ ...hasBody ? { "Content-Type": "application/json" } : {},
700
+ ...headers
701
+ },
702
+ ...hasBody ? { body: JSON.stringify(inputBody) } : {},
703
+ signal
704
+ }).andThen((response) => {
705
+ if (!isUsableStatus(response.status)) return import_neverthrow4.ResultAsync.fromSafePromise(Promise.resolve(null));
706
+ return import_neverthrow4.ResultAsync.fromSafePromise(
707
+ (async () => {
1633
708
  let authHint = response.status === 402 ? "paid" : "unprotected";
709
+ let paymentRequiredBody;
1634
710
  if (response.status === 402) {
1635
711
  try {
1636
- const payload = await response.clone().json();
1637
- authHint = detectAuthHintFrom402Payload(payload);
712
+ const headerValue = response.headers.get("payment-required");
713
+ const parsed = headerValue !== null ? parsePaymentRequiredBody2(headerValue) : await parsePaymentRequiredBody(response);
714
+ if (parsed !== null) {
715
+ paymentRequiredBody = parsed;
716
+ authHint = detectAuthHint(paymentRequiredBody);
717
+ }
1638
718
  } catch {
1639
719
  }
1640
720
  }
1641
- const protocolHints = detectProtocols(response);
1642
- resources.push(
1643
- createResource(
1644
- {
1645
- origin: options.origin,
1646
- method,
1647
- path,
1648
- source: "probe",
1649
- summary: `${method} ${path}`,
1650
- authHint,
1651
- ...protocolHints.length ? { protocolHints } : {}
1652
- },
1653
- 0.6,
1654
- "runtime_verified"
1655
- )
1656
- );
1657
- } catch (error) {
1658
- warnings.push(
1659
- warning(
1660
- "FETCH_FAILED",
1661
- "info",
1662
- `Probe ${method} ${path} failed: ${error instanceof Error ? error.message : String(error)}`,
1663
- { stage: "probe" }
1664
- )
1665
- );
1666
- }
1667
- }
1668
- }
1669
- return {
1670
- stage: "probe",
1671
- valid: resources.length > 0,
1672
- resources,
1673
- warnings
1674
- };
721
+ const protocols = detectProtocols(response);
722
+ const wwwAuthenticate = response.headers.get("www-authenticate") ?? void 0;
723
+ return {
724
+ path,
725
+ method,
726
+ authHint,
727
+ ...protocols.length ? { protocols } : {},
728
+ ...paymentRequiredBody !== void 0 ? { paymentRequiredBody } : {},
729
+ ...wwwAuthenticate ? { wwwAuthenticate } : {}
730
+ };
731
+ })()
732
+ );
733
+ });
1675
734
  }
1676
-
1677
- // src/core/upgrade.ts
1678
- var UPGRADE_WARNING_CODES = [
1679
- "LEGACY_WELL_KNOWN_USED",
1680
- "LEGACY_DNS_USED",
1681
- "LEGACY_DNS_PLAIN_URL",
1682
- "LEGACY_INSTRUCTIONS_USED",
1683
- "LEGACY_OWNERSHIP_PROOFS_USED",
1684
- "OPENAPI_AUTH_MODE_MISSING",
1685
- "OPENAPI_TOP_LEVEL_INVALID"
1686
- ];
1687
- var UPGRADE_WARNING_CODE_SET = new Set(UPGRADE_WARNING_CODES);
1688
- function computeUpgradeSignal(warnings) {
1689
- const reasons = warnings.map((entry) => entry.code).filter((code, index, list) => list.indexOf(code) === index).filter((code) => UPGRADE_WARNING_CODE_SET.has(code));
1690
- return {
1691
- upgradeSuggested: reasons.length > 0,
1692
- upgradeReasons: reasons
1693
- };
735
+ function getProbe(url, headers, signal, inputBody) {
736
+ const path = normalizePath(new URL(url).pathname || "/");
737
+ return import_neverthrow4.ResultAsync.fromSafePromise(
738
+ Promise.all(
739
+ [...HTTP_METHODS].map(
740
+ (method) => probeMethod(url, method, path, headers, signal, inputBody).match(
741
+ (result) => result,
742
+ () => null
743
+ )
744
+ )
745
+ ).then((results) => results.filter((r) => r !== null))
746
+ );
1694
747
  }
1695
748
 
1696
- // src/core/discovery.ts
1697
- function mergeResources(target, incoming, resourceWarnings) {
1698
- const warnings = [];
1699
- for (const resource of incoming) {
1700
- const existing = target.get(resource.resourceKey);
1701
- if (!existing) {
1702
- target.set(resource.resourceKey, resource);
1703
- continue;
1704
- }
1705
- const conflict = existing.authHint !== resource.authHint || existing.priceHint !== resource.priceHint || JSON.stringify(existing.protocolHints ?? []) !== JSON.stringify(resource.protocolHints ?? []);
1706
- if (conflict) {
1707
- const conflictWarning = warning(
1708
- "CROSS_SOURCE_CONFLICT",
1709
- "warn",
1710
- `Resource conflict for ${resource.resourceKey}; keeping higher-precedence source ${existing.source} over ${resource.source}.`,
1711
- {
1712
- resourceKey: resource.resourceKey
1713
- }
1714
- );
1715
- warnings.push(conflictWarning);
1716
- resourceWarnings[resource.resourceKey] = [
1717
- ...resourceWarnings[resource.resourceKey] ?? [],
1718
- conflictWarning
1719
- ];
1720
- continue;
1721
- }
1722
- target.set(resource.resourceKey, {
1723
- ...existing,
1724
- summary: existing.summary ?? resource.summary,
1725
- links: { ...resource.links, ...existing.links },
1726
- confidence: Math.max(existing.confidence ?? 0, resource.confidence ?? 0)
1727
- });
1728
- }
1729
- return warnings;
1730
- }
1731
- function toTrace(stageResult, startedAt) {
1732
- return {
1733
- stage: stageResult.stage,
1734
- attempted: true,
1735
- valid: stageResult.valid,
1736
- resourceCount: stageResult.resources.length,
1737
- durationMs: Date.now() - startedAt,
1738
- warnings: stageResult.warnings,
1739
- ...stageResult.links ? { links: stageResult.links } : {}
1740
- };
1741
- }
1742
- async function runOverrideStage(options) {
1743
- const warnings = [];
1744
- for (const overrideUrl of options.overrideUrls) {
1745
- const normalized = overrideUrl.trim();
1746
- if (!normalized) continue;
749
+ // src/core/protocols/mpp/index.ts
750
+ var TEMPO_DEFAULT_CHAIN_ID = 4217;
751
+ function parseAuthParams(segment) {
752
+ const params = {};
753
+ const re = /(\w+)=(?:"([^"]*)"|'([^']*)')/g;
754
+ let match;
755
+ while ((match = re.exec(segment)) !== null) {
756
+ params[match[1]] = match[2] ?? match[3] ?? "";
757
+ }
758
+ return params;
759
+ }
760
+ function extractPaymentOptions4(wwwAuthenticate) {
761
+ if (!isMmmEnabled() || !wwwAuthenticate) return [];
762
+ const options = [];
763
+ for (const segment of wwwAuthenticate.split(/,\s*(?=Payment\s)/i)) {
764
+ const stripped = segment.replace(/^Payment\s+/i, "").trim();
765
+ const params = parseAuthParams(stripped);
766
+ const paymentMethod = params["method"];
767
+ const intent = params["intent"];
768
+ const realm = params["realm"];
769
+ const description = params["description"];
770
+ const requestStr = params["request"];
771
+ if (!paymentMethod || !intent || !realm || !requestStr) continue;
772
+ let request;
1747
773
  try {
1748
- const response = await options.fetcher(normalized, {
1749
- method: "GET",
1750
- headers: { Accept: "application/json, text/plain;q=0.9", ...options.headers },
1751
- signal: options.signal
1752
- });
1753
- if (!response.ok) {
1754
- warnings.push(
1755
- warning(
1756
- "FETCH_FAILED",
1757
- "warn",
1758
- `Override fetch failed (${response.status}) at ${normalized}`,
1759
- {
1760
- stage: "override"
1761
- }
1762
- )
1763
- );
1764
- continue;
1765
- }
1766
- const bodyText = await response.text();
1767
- let parsedJson;
1768
- try {
1769
- parsedJson = JSON.parse(bodyText);
1770
- } catch {
1771
- parsedJson = void 0;
1772
- }
1773
- if (parsedJson && typeof parsedJson === "object" && !Array.isArray(parsedJson) && "openapi" in parsedJson && "paths" in parsedJson) {
1774
- const openapiResult = await runOpenApiStage({
1775
- origin: options.origin,
1776
- fetcher: async (input, init) => {
1777
- const inputString = String(input);
1778
- if (inputString === `${options.origin}/openapi.json` || inputString === `${options.origin}/.well-known/openapi.json`) {
1779
- return new Response(bodyText, {
1780
- status: 200,
1781
- headers: { "content-type": "application/json" }
1782
- });
1783
- }
1784
- return options.fetcher(input, init);
1785
- },
1786
- headers: options.headers,
1787
- signal: options.signal,
1788
- includeRaw: options.includeRaw
1789
- });
1790
- return {
1791
- ...openapiResult,
1792
- stage: "override",
1793
- warnings: [
1794
- warning("OVERRIDE_USED", "info", `Using explicit override source ${normalized}`, {
1795
- stage: "override"
1796
- }),
1797
- ...openapiResult.warnings
1798
- ]
1799
- };
1800
- }
1801
- if (parsedJson && typeof parsedJson === "object" && !Array.isArray(parsedJson)) {
1802
- const parsedWellKnown = parseWellKnownPayload(parsedJson, options.origin, normalized);
1803
- if (parsedWellKnown.resources.length > 0) {
1804
- return {
1805
- stage: "override",
1806
- valid: true,
1807
- resources: parsedWellKnown.resources,
1808
- warnings: [
1809
- warning("OVERRIDE_USED", "info", `Using explicit override source ${normalized}`, {
1810
- stage: "override"
1811
- }),
1812
- ...parsedWellKnown.warnings
1813
- ],
1814
- links: { discoveryUrl: normalized },
1815
- ...options.includeRaw ? { raw: parsedWellKnown.raw } : {}
1816
- };
1817
- }
1818
- }
1819
- } catch (error) {
1820
- warnings.push(
1821
- warning(
1822
- "FETCH_FAILED",
1823
- "warn",
1824
- `Override fetch exception at ${normalized}: ${error instanceof Error ? error.message : String(error)}`,
1825
- { stage: "override" }
1826
- )
1827
- );
774
+ const parsed = JSON.parse(requestStr);
775
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) continue;
776
+ request = parsed;
777
+ } catch {
1828
778
  continue;
1829
779
  }
1830
- const fallbackWellKnownResult = await runWellKnownX402Stage({
1831
- origin: options.origin,
1832
- url: normalized,
1833
- fetcher: options.fetcher,
1834
- headers: options.headers,
1835
- signal: options.signal,
1836
- includeRaw: options.includeRaw
780
+ const asset = typeof request["currency"] === "string" ? request["currency"] : void 0;
781
+ const amountRaw = request["amount"];
782
+ const amount = typeof amountRaw === "string" ? amountRaw : typeof amountRaw === "number" ? String(amountRaw) : void 0;
783
+ const decimals = typeof request["decimals"] === "number" ? request["decimals"] : void 0;
784
+ const payTo = typeof request["recipient"] === "string" ? request["recipient"] : void 0;
785
+ const methodDetails = request["methodDetails"];
786
+ const chainId = methodDetails !== null && typeof methodDetails === "object" && !Array.isArray(methodDetails) && typeof methodDetails["chainId"] === "number" ? methodDetails["chainId"] : TEMPO_DEFAULT_CHAIN_ID;
787
+ if (!asset || !amount) continue;
788
+ options.push({
789
+ protocol: "mpp",
790
+ // isMmmEnabled
791
+ paymentMethod,
792
+ intent,
793
+ realm,
794
+ network: `tempo:${String(chainId)}`,
795
+ // isMmmEnabled
796
+ asset,
797
+ amount,
798
+ ...decimals != null ? { decimals } : {},
799
+ ...payTo ? { payTo } : {},
800
+ ...description ? { description } : {}
1837
801
  });
1838
- if (fallbackWellKnownResult.valid) {
1839
- return {
1840
- ...fallbackWellKnownResult,
1841
- stage: "override",
1842
- warnings: [
1843
- warning("OVERRIDE_USED", "info", `Using explicit override source ${normalized}`, {
1844
- stage: "override"
1845
- }),
1846
- ...fallbackWellKnownResult.warnings
1847
- ]
1848
- };
1849
- }
1850
- warnings.push(...fallbackWellKnownResult.warnings);
1851
802
  }
1852
- return {
1853
- stage: "override",
1854
- valid: false,
1855
- resources: [],
1856
- warnings
1857
- };
803
+ return options;
1858
804
  }
1859
- async function runDiscovery({
1860
- detailed,
1861
- options
1862
- }) {
1863
- const fetcher = options.fetcher ?? fetch;
1864
- const compatMode = options.compatMode ?? DEFAULT_COMPAT_MODE;
1865
- const strictCompat = compatMode === "strict";
1866
- const rawView = detailed ? options.rawView ?? "none" : "none";
1867
- const includeRaw = rawView === "full";
1868
- const includeInteropMpp = detailed && Boolean(options.includeInteropMpp);
1869
- const origin = normalizeOrigin(options.target);
1870
- const warnings = [];
1871
- const trace = [];
1872
- const resourceWarnings = {};
1873
- const rawSources = {};
1874
- const merged = /* @__PURE__ */ new Map();
1875
- if (compatMode !== "off") {
1876
- warnings.push(
1877
- warning(
1878
- "COMPAT_MODE_ENABLED",
1879
- compatMode === "strict" ? "warn" : "info",
1880
- `Compatibility mode is '${compatMode}'. Legacy adapters are active.`
1881
- )
1882
- );
1883
- }
1884
- const stages = [];
1885
- if (options.overrideUrls && options.overrideUrls.length > 0) {
1886
- stages.push(
1887
- () => runOverrideStage({
1888
- origin,
1889
- overrideUrls: options.overrideUrls ?? [],
1890
- fetcher,
1891
- headers: options.headers,
1892
- signal: options.signal,
1893
- includeRaw
1894
- })
1895
- );
1896
- }
1897
- stages.push(
1898
- () => runOpenApiStage({
1899
- origin,
1900
- fetcher,
1901
- headers: options.headers,
1902
- signal: options.signal,
1903
- includeRaw
1904
- })
1905
- );
1906
- if (compatMode !== "off") {
1907
- stages.push(
1908
- () => runWellKnownX402Stage({
1909
- origin,
1910
- fetcher,
1911
- headers: options.headers,
1912
- signal: options.signal,
1913
- includeRaw
1914
- })
1915
- );
1916
- stages.push(
1917
- () => runDnsStage({
1918
- origin,
1919
- fetcher,
1920
- txtResolver: options.txtResolver,
1921
- headers: options.headers,
1922
- signal: options.signal,
1923
- includeRaw
1924
- })
1925
- );
1926
- if (includeInteropMpp && isMmmEnabled()) {
1927
- stages.push(
1928
- () => runInteropMppStage({
1929
- origin,
1930
- fetcher,
1931
- headers: options.headers,
1932
- signal: options.signal,
1933
- includeRaw
1934
- })
1935
- );
805
+
806
+ // src/core/layers/l3.ts
807
+ function findMatchingOpenApiPath(paths, targetPath) {
808
+ const exact = paths[targetPath];
809
+ if (isRecord(exact)) return { matchedPath: targetPath, pathItem: exact };
810
+ for (const [specPath, entry] of Object.entries(paths)) {
811
+ if (!isRecord(entry)) continue;
812
+ const pattern = specPath.replace(/\{[^}]+\}/g, "[^/]+");
813
+ const regex = new RegExp(`^${pattern}$`);
814
+ if (regex.test(targetPath)) {
815
+ return { matchedPath: specPath, pathItem: entry };
1936
816
  }
1937
817
  }
1938
- stages.push(
1939
- () => runProbeStage({
1940
- origin,
1941
- fetcher,
1942
- headers: options.headers,
1943
- signal: options.signal,
1944
- probeCandidates: options.probeCandidates
1945
- })
1946
- );
1947
- let selectedStage;
1948
- for (const runStage of stages) {
1949
- const startedAt = Date.now();
1950
- const stageResult = await runStage();
1951
- stageResult.warnings = applyStrictEscalation(stageResult.warnings, strictCompat);
1952
- trace.push(toTrace(stageResult, startedAt));
1953
- warnings.push(...stageResult.warnings);
1954
- if (stageResult.resources.length > 0) {
1955
- for (const stageWarning of stageResult.warnings) {
1956
- if (stageWarning.resourceKey) {
1957
- resourceWarnings[stageWarning.resourceKey] = [
1958
- ...resourceWarnings[stageWarning.resourceKey] ?? [],
1959
- stageWarning
1960
- ];
1961
- }
818
+ return null;
819
+ }
820
+ function resolveRef(document, ref, seen) {
821
+ if (!ref.startsWith("#/")) return void 0;
822
+ if (seen.has(ref)) return { $circular: ref };
823
+ seen.add(ref);
824
+ const parts = ref.slice(2).split("/");
825
+ let current = document;
826
+ for (const part of parts) {
827
+ if (!isRecord(current)) return void 0;
828
+ current = current[part];
829
+ if (current === void 0) return void 0;
830
+ }
831
+ if (isRecord(current)) return resolveRefs(document, current, seen);
832
+ return current;
833
+ }
834
+ function resolveRefs(document, obj, seen, depth = 0) {
835
+ if (depth > 4) return obj;
836
+ const resolved = {};
837
+ for (const [key, value] of Object.entries(obj)) {
838
+ if (key === "$ref" && typeof value === "string") {
839
+ const deref = resolveRef(document, value, seen);
840
+ if (isRecord(deref)) {
841
+ Object.assign(resolved, deref);
842
+ } else {
843
+ resolved[key] = value;
1962
844
  }
845
+ continue;
1963
846
  }
1964
- if (!stageResult.valid) {
847
+ if (isRecord(value)) {
848
+ resolved[key] = resolveRefs(document, value, seen, depth + 1);
1965
849
  continue;
1966
850
  }
1967
- const mergeWarnings = mergeResources(merged, stageResult.resources, resourceWarnings);
1968
- warnings.push(...mergeWarnings);
1969
- if (includeRaw && stageResult.raw !== void 0) {
1970
- if (stageResult.stage === "openapi" || stageResult.stage === "override") {
1971
- const rawObject = stageResult.raw;
1972
- if (rawObject.openapi !== void 0) rawSources.openapi = rawObject.openapi;
1973
- if (typeof rawObject.llmsTxt === "string") rawSources.llmsTxt = rawObject.llmsTxt;
1974
- }
1975
- if (stageResult.stage === "well-known/x402" || stageResult.stage === "override") {
1976
- rawSources.wellKnownX402 = [...rawSources.wellKnownX402 ?? [], stageResult.raw];
1977
- }
1978
- if (stageResult.stage === "dns/_x402") {
1979
- const rawObject = stageResult.raw;
1980
- if (Array.isArray(rawObject.dnsRecords)) {
1981
- rawSources.dnsRecords = rawObject.dnsRecords;
1982
- }
1983
- }
1984
- if (stageResult.stage === "interop/mpp") {
1985
- rawSources.interopMpp = stageResult.raw;
1986
- }
851
+ if (Array.isArray(value)) {
852
+ resolved[key] = value.map(
853
+ (item) => isRecord(item) ? resolveRefs(document, item, seen, depth + 1) : item
854
+ );
855
+ continue;
1987
856
  }
1988
- if (!detailed) {
1989
- selectedStage = selectedStage ?? stageResult.stage;
1990
- break;
857
+ resolved[key] = value;
858
+ }
859
+ return resolved;
860
+ }
861
+ function extractRequestBodySchema(operationSchema) {
862
+ const requestBody = operationSchema.requestBody;
863
+ if (!isRecord(requestBody)) return void 0;
864
+ const content = requestBody.content;
865
+ if (!isRecord(content)) return void 0;
866
+ const jsonMediaType = content["application/json"];
867
+ if (isRecord(jsonMediaType) && isRecord(jsonMediaType.schema)) {
868
+ return jsonMediaType.schema;
869
+ }
870
+ for (const mediaType of Object.values(content)) {
871
+ if (isRecord(mediaType) && isRecord(mediaType.schema)) {
872
+ return mediaType.schema;
1991
873
  }
1992
874
  }
1993
- if (merged.size === 0) {
1994
- warnings.push(
1995
- warning(
1996
- "NO_DISCOVERY_SOURCES",
1997
- "error",
1998
- "No discovery stage returned first-valid-non-empty results."
1999
- )
2000
- );
2001
- }
2002
- const dedupedWarnings = dedupeWarnings(warnings);
2003
- const upgradeSignal = computeUpgradeSignal(dedupedWarnings);
2004
- const resources = [...merged.values()];
2005
- if (!detailed) {
2006
- return {
2007
- origin,
2008
- resources,
2009
- warnings: dedupedWarnings,
2010
- compatMode,
2011
- ...upgradeSignal,
2012
- ...selectedStage ? { selectedStage } : {}
2013
- };
2014
- }
875
+ return void 0;
876
+ }
877
+ function extractParameters(operationSchema) {
878
+ const params = operationSchema.parameters;
879
+ if (!Array.isArray(params)) return [];
880
+ return params.filter((value) => isRecord(value));
881
+ }
882
+ function extractInputSchema(operationSchema) {
883
+ const requestBody = extractRequestBodySchema(operationSchema);
884
+ const parameters = extractParameters(operationSchema);
885
+ if (!requestBody && parameters.length === 0) return void 0;
886
+ if (requestBody && parameters.length === 0) return requestBody;
2015
887
  return {
2016
- origin,
2017
- resources,
2018
- warnings: dedupedWarnings,
2019
- compatMode,
2020
- ...upgradeSignal,
2021
- selectedStage: trace.find((entry) => entry.valid)?.stage,
2022
- trace,
2023
- resourceWarnings,
2024
- ...includeRaw ? { rawSources } : {}
888
+ ...requestBody ? { requestBody } : {},
889
+ ...parameters.length > 0 ? { parameters } : {}
2025
890
  };
2026
891
  }
2027
-
2028
- // src/validation/payment-required.ts
2029
- var import_schemas = require("@x402/core/schemas");
2030
-
2031
- // src/harness.ts
2032
- var INTENT_TRIGGERS = ["x402", "mpp", "pay for", "micropayment", "agentic commerce"];
2033
- var CLIENT_PROFILES = {
2034
- "claude-code": {
2035
- id: "claude-code",
2036
- label: "Claude Code MCP Harness",
2037
- surface: "mcp",
2038
- defaultContextWindowTokens: 2e5,
2039
- zeroHopBudgetPercent: 0.1,
2040
- notes: "Targets environments where MCP context can use roughly 10% of total context."
2041
- },
2042
- "skill-cli": {
2043
- id: "skill-cli",
2044
- label: "Skill + CLI Harness",
2045
- surface: "skill-cli",
2046
- defaultContextWindowTokens: 2e5,
2047
- zeroHopBudgetPercent: 0.02,
2048
- notes: "Targets constrained skill contexts with title/description-heavy routing."
2049
- },
2050
- "generic-mcp": {
2051
- id: "generic-mcp",
2052
- label: "Generic MCP Harness",
2053
- surface: "mcp",
2054
- defaultContextWindowTokens: 128e3,
2055
- zeroHopBudgetPercent: 0.05,
2056
- notes: "Conservative MCP profile when specific client budgets are unknown."
2057
- },
2058
- generic: {
2059
- id: "generic",
2060
- label: "Generic Agent Harness",
2061
- surface: "generic",
2062
- defaultContextWindowTokens: 128e3,
2063
- zeroHopBudgetPercent: 0.03,
2064
- notes: "Fallback profile for unknown clients."
892
+ function parseOperationPrice(operation) {
893
+ const paymentInfo = operation["x-payment-info"];
894
+ if (!isRecord(paymentInfo)) return void 0;
895
+ function toFiniteNumber(value) {
896
+ if (typeof value === "number" && Number.isFinite(value)) return value;
897
+ if (typeof value === "string" && value.trim().length > 0) {
898
+ const parsed = Number(value);
899
+ if (Number.isFinite(parsed)) return parsed;
900
+ }
901
+ return void 0;
2065
902
  }
2066
- };
2067
- function isFirstPartyDomain(hostname) {
2068
- const normalized = hostname.toLowerCase();
2069
- return normalized.endsWith(".dev") && normalized.startsWith("stable");
903
+ const fixed = toFiniteNumber(paymentInfo.price);
904
+ if (fixed != null) return `$${String(fixed)}`;
905
+ const min = toFiniteNumber(paymentInfo.minPrice);
906
+ const max = toFiniteNumber(paymentInfo.maxPrice);
907
+ if (min != null && max != null) return `$${String(min)}-$${String(max)}`;
908
+ return void 0;
2070
909
  }
2071
- function safeHostname(origin) {
2072
- try {
2073
- return new URL(origin).hostname;
2074
- } catch {
2075
- return origin.replace(/^https?:\/\//, "").split("/")[0] ?? origin;
2076
- }
910
+ function parseOperationProtocols(operation) {
911
+ const paymentInfo = operation["x-payment-info"];
912
+ if (!isRecord(paymentInfo) || !Array.isArray(paymentInfo.protocols)) return void 0;
913
+ const protocols = paymentInfo.protocols.filter(
914
+ (protocol) => typeof protocol === "string" && protocol.length > 0 && (protocol !== "mpp" || isMmmEnabled())
915
+ );
916
+ return protocols.length > 0 ? protocols : void 0;
917
+ }
918
+ function getL3ForOpenAPI(openApi, path, method) {
919
+ const document = openApi.raw;
920
+ const paths = isRecord(document.paths) ? document.paths : void 0;
921
+ if (!paths) return null;
922
+ const matched = findMatchingOpenApiPath(paths, path);
923
+ if (!matched) return null;
924
+ const operation = matched.pathItem[method.toLowerCase()];
925
+ if (!isRecord(operation)) return null;
926
+ const resolvedOperation = resolveRefs(document, operation, /* @__PURE__ */ new Set());
927
+ const summary = typeof resolvedOperation.summary === "string" ? resolvedOperation.summary : typeof resolvedOperation.description === "string" ? resolvedOperation.description : void 0;
928
+ return {
929
+ source: "openapi",
930
+ ...summary ? { summary } : {},
931
+ authMode: inferAuthMode(resolvedOperation) ?? void 0,
932
+ estimatedPrice: parseOperationPrice(resolvedOperation),
933
+ protocols: parseOperationProtocols(resolvedOperation),
934
+ inputSchema: extractInputSchema(resolvedOperation),
935
+ outputSchema: resolvedOperation
936
+ };
2077
937
  }
2078
- function previewText(text) {
2079
- if (!text) return null;
2080
- const compact = text.replace(/\s+/g, " ").trim();
2081
- if (!compact) return null;
2082
- return compact.length > 220 ? `${compact.slice(0, 217)}...` : compact;
2083
- }
2084
- function toAuthModeCount(resources) {
2085
- const counts = {
2086
- paid: 0,
2087
- siwx: 0,
2088
- apiKey: 0,
2089
- unprotected: 0,
2090
- unknown: 0
938
+ function getL3ForProbe(probe, path, method) {
939
+ const probeResult = probe.find((r) => r.path === path && r.method === method);
940
+ if (!probeResult) return null;
941
+ const inputSchema = probeResult.paymentRequiredBody ? parseInputSchema(probeResult.paymentRequiredBody) : void 0;
942
+ const outputSchema = probeResult.paymentRequiredBody ? parseOutputSchema(probeResult.paymentRequiredBody) : void 0;
943
+ const paymentOptions = [
944
+ ...probeResult.paymentRequiredBody ? extractPaymentOptions3(probeResult.paymentRequiredBody) : [],
945
+ ...isMmmEnabled() ? extractPaymentOptions4(probeResult.wwwAuthenticate) : []
946
+ // isMmmEnabled
947
+ ];
948
+ return {
949
+ source: "probe",
950
+ authMode: probeResult.authHint,
951
+ ...probeResult.protocols?.length ? { protocols: probeResult.protocols } : {},
952
+ ...inputSchema ? { inputSchema } : {},
953
+ ...outputSchema ? { outputSchema } : {},
954
+ ...paymentOptions.length ? { paymentOptions } : {}
2091
955
  };
2092
- for (const resource of resources) {
2093
- const mode = resource.authHint;
2094
- if (mode === "paid" || mode === "siwx" || mode === "apiKey" || mode === "unprotected") {
2095
- counts[mode] += 1;
2096
- } else {
2097
- counts.unknown += 1;
2098
- }
2099
- }
2100
- return counts;
2101
956
  }
2102
- function toSourceCount(resources) {
2103
- return resources.reduce(
2104
- (acc, resource) => {
2105
- acc[resource.source] = (acc[resource.source] ?? 0) + 1;
2106
- return acc;
2107
- },
2108
- {}
2109
- );
957
+
958
+ // src/runtime/check-endpoint.ts
959
+ function getAdvisoriesForOpenAPI(openApi, path) {
960
+ return [...HTTP_METHODS].flatMap((method) => {
961
+ const l3 = getL3ForOpenAPI(openApi, path, method);
962
+ return l3 ? [{ method, ...l3 }] : [];
963
+ });
2110
964
  }
2111
- function toL2Entries(resources) {
2112
- return [...resources].sort((a, b) => {
2113
- if (a.path !== b.path) return a.path.localeCompare(b.path);
2114
- if (a.method !== b.method) return a.method.localeCompare(b.method);
2115
- return a.resourceKey.localeCompare(b.resourceKey);
2116
- }).map((resource) => ({
2117
- resourceKey: resource.resourceKey,
2118
- method: resource.method,
2119
- path: resource.path,
2120
- source: resource.source,
2121
- authMode: resource.authHint ?? null
2122
- }));
965
+ function getAdvisoriesForProbe(probe, path) {
966
+ return [...HTTP_METHODS].flatMap((method) => {
967
+ const l3 = getL3ForProbe(probe, path, method);
968
+ return l3 ? [{ method, ...l3 }] : [];
969
+ });
2123
970
  }
2124
- function getLlmsTxtInfo(result) {
2125
- const llmsTxtUrl = result.trace.find((entry) => entry.links?.llmsTxtUrl)?.links?.llmsTxtUrl ?? result.resources.find((resource) => resource.links?.llmsTxtUrl)?.links?.llmsTxtUrl ?? null;
2126
- const llmsTxt = result.rawSources?.llmsTxt;
2127
- if (llmsTxt) {
971
+ async function checkEndpointSchema(options) {
972
+ const endpoint = new URL(options.url);
973
+ const origin = normalizeOrigin(endpoint.origin);
974
+ const path = normalizePath(endpoint.pathname || "/");
975
+ if (options.sampleInputBody !== void 0) {
976
+ const probeResult2 = await getProbe(
977
+ options.url,
978
+ options.headers,
979
+ options.signal,
980
+ options.sampleInputBody
981
+ );
982
+ if (probeResult2.isErr()) {
983
+ return {
984
+ found: false,
985
+ origin,
986
+ path,
987
+ cause: probeResult2.error.cause,
988
+ message: probeResult2.error.message
989
+ };
990
+ }
991
+ const advisories2 = getAdvisoriesForProbe(probeResult2.value, path);
992
+ if (advisories2.length > 0) return { found: true, origin, path, advisories: advisories2 };
993
+ return { found: false, origin, path, cause: "not_found" };
994
+ }
995
+ const openApiResult = await getOpenAPI(origin, options.headers, options.signal);
996
+ const openApi = openApiResult.isOk() ? openApiResult.value : null;
997
+ if (openApi) {
998
+ const advisories2 = getAdvisoriesForOpenAPI(openApi, path);
999
+ if (advisories2.length > 0) return { found: true, origin, path, advisories: advisories2 };
1000
+ return { found: false, origin, path, cause: "not_found" };
1001
+ }
1002
+ const probeResult = await getProbe(options.url, options.headers, options.signal);
1003
+ if (probeResult.isErr()) {
2128
1004
  return {
2129
- llmsTxtUrl,
2130
- llmsTxtTokenEstimate: estimateTokenCount(llmsTxt),
2131
- guidancePreview: previewText(llmsTxt),
2132
- guidanceStatus: "present"
1005
+ found: false,
1006
+ origin,
1007
+ path,
1008
+ cause: probeResult.error.cause,
1009
+ message: probeResult.error.message
2133
1010
  };
2134
1011
  }
2135
- if (llmsTxtUrl) {
2136
- const failed = result.warnings.some((warning2) => warning2.code === "LLMSTXT_FETCH_FAILED");
2137
- return {
2138
- llmsTxtUrl,
2139
- llmsTxtTokenEstimate: 0,
2140
- guidancePreview: null,
2141
- guidanceStatus: failed ? "advertised_but_unfetched" : "missing"
2142
- };
1012
+ const advisories = getAdvisoriesForProbe(probeResult.value, path);
1013
+ if (advisories.length > 0) return { found: true, origin, path, advisories };
1014
+ return { found: false, origin, path, cause: "not_found" };
1015
+ }
1016
+
1017
+ // src/cli.ts
1018
+ function parseArgs(args) {
1019
+ const flags = { json: false, verbose: false };
1020
+ const positional = [];
1021
+ for (const arg of args) {
1022
+ if (arg === "--json") flags.json = true;
1023
+ else if (arg === "-v" || arg === "--verbose") flags.verbose = true;
1024
+ else positional.push(arg);
2143
1025
  }
2144
- return {
2145
- llmsTxtUrl: null,
2146
- llmsTxtTokenEstimate: 0,
2147
- guidancePreview: null,
2148
- guidanceStatus: "not_advertised"
2149
- };
1026
+ const [first, second] = positional;
1027
+ if (first && (first.startsWith("http://") || first.startsWith("https://"))) {
1028
+ return { command: "discover", target: first, flags };
1029
+ }
1030
+ return { command: first ?? null, target: second ?? null, flags };
2150
1031
  }
2151
- function getHarnessClientProfile(client = "claude-code") {
2152
- return CLIENT_PROFILES[client] ?? CLIENT_PROFILES.generic;
2153
- }
2154
- function buildContextHarnessReport(options) {
2155
- const client = getHarnessClientProfile(options.client);
2156
- const contextWindowTokens = options.contextWindowTokens ?? client.defaultContextWindowTokens;
2157
- const zeroHopBudgetTokens = Math.floor(contextWindowTokens * client.zeroHopBudgetPercent);
2158
- const hostname = safeHostname(options.result.origin);
2159
- const domainClass = isFirstPartyDomain(hostname) ? "first-party" : "ugc";
2160
- const l0Summary = "Route payment-capable intents to agentcash and use install/discover commands for progressive disclosure.";
2161
- const l1Summary = "Expose installed domain routing hints and trigger fan-out into L2/L3 commands when user intent matches domain capabilities.";
2162
- const l0EstimatedTokens = estimateTokenCount(
2163
- `${INTENT_TRIGGERS.join(", ")} npx agentcash install npx agentcash install-ext`
2164
- );
2165
- const l1EstimatedTokens = estimateTokenCount(
2166
- `${hostname} ${l1Summary} npx agentcash discover ${hostname} npx agentcash discover ${hostname} --verbose`
2167
- );
2168
- const llmsInfo = getLlmsTxtInfo(options.result);
1032
+ function routeToResource(route) {
2169
1033
  return {
2170
- target: options.target,
2171
- origin: options.result.origin,
2172
- client,
2173
- budget: {
2174
- contextWindowTokens,
2175
- zeroHopBudgetTokens,
2176
- estimatedZeroHopTokens: l0EstimatedTokens + l1EstimatedTokens,
2177
- withinBudget: l0EstimatedTokens + l1EstimatedTokens <= zeroHopBudgetTokens
2178
- },
2179
- levels: {
2180
- l0: {
2181
- layer: "L0",
2182
- intentTriggers: INTENT_TRIGGERS,
2183
- installCommand: "npx agentcash install",
2184
- deliverySurfaces: ["MCP", "Skill+CLI"],
2185
- summary: l0Summary,
2186
- estimatedTokens: l0EstimatedTokens
2187
- },
2188
- l1: {
2189
- layer: "L1",
2190
- domain: hostname,
2191
- domainClass,
2192
- selectedDiscoveryStage: options.result.selectedStage ?? null,
2193
- summary: l1Summary,
2194
- fanoutCommands: [
2195
- `npx agentcash discover ${hostname}`,
2196
- `npx agentcash discover ${hostname} --verbose`
2197
- ],
2198
- estimatedTokens: l1EstimatedTokens
2199
- },
2200
- l2: {
2201
- layer: "L2",
2202
- command: `npx agentcash discover ${hostname}`,
2203
- tokenLight: true,
2204
- resourceCount: options.result.resources.length,
2205
- resources: toL2Entries(options.result.resources)
2206
- },
2207
- l3: {
2208
- layer: "L3",
2209
- command: `npx agentcash discover ${hostname} --verbose`,
2210
- detailLevel: "endpoint-schema-and-metadata",
2211
- countsByAuthMode: toAuthModeCount(options.result.resources),
2212
- countsBySource: toSourceCount(options.result.resources),
2213
- pricedResourceCount: options.result.resources.filter(
2214
- (resource) => resource.authHint === "paid"
2215
- ).length
2216
- },
2217
- l4: {
2218
- layer: "L4",
2219
- guidanceSource: llmsInfo.llmsTxtUrl ? "llms.txt" : "none",
2220
- llmsTxtUrl: llmsInfo.llmsTxtUrl,
2221
- llmsTxtTokenEstimate: llmsInfo.llmsTxtTokenEstimate,
2222
- guidancePreview: llmsInfo.guidancePreview,
2223
- guidanceStatus: llmsInfo.guidanceStatus
2224
- },
2225
- l5: {
2226
- layer: "L5",
2227
- status: "out_of_scope",
2228
- note: "Cross-domain composition is intentionally out of scope for discovery v1."
2229
- }
2230
- },
2231
- diagnostics: {
2232
- warningCount: options.result.warnings.length,
2233
- errorWarningCount: options.result.warnings.filter((warning2) => warning2.severity === "error").length,
2234
- selectedStage: options.result.selectedStage ?? null,
2235
- upgradeSuggested: options.result.upgradeSuggested,
2236
- upgradeReasons: options.result.upgradeReasons
2237
- }
1034
+ resourceKey: `${route.method} ${route.path}`,
1035
+ method: route.method,
1036
+ path: route.path,
1037
+ summary: route.summary,
1038
+ ...route.authMode ? { authHint: route.authMode } : {},
1039
+ ...route.price ? { priceHint: route.price } : {},
1040
+ ...route.protocols?.length ? { protocols: route.protocols } : {}
2238
1041
  };
2239
1042
  }
2240
-
2241
- // src/index.ts
2242
- async function discoverDetailed(options) {
2243
- return await runDiscovery({ detailed: true, options });
2244
- }
2245
-
2246
- // src/cli/run.ts
2247
- var CLI_USER_AGENT = "@agentcash/discovery-cli/0.1";
2248
- function buildProbeCandidates(resources) {
2249
- const methodsByPath = /* @__PURE__ */ new Map();
2250
- for (const resource of resources) {
2251
- const existing = methodsByPath.get(resource.path) ?? /* @__PURE__ */ new Set();
2252
- existing.add(resource.method);
2253
- methodsByPath.set(resource.path, existing);
2254
- }
2255
- return [...methodsByPath.entries()].sort((a, b) => a[0].localeCompare(b[0])).map(([path, methods]) => ({ path, methods: [...methods].sort() }));
2256
- }
2257
- function createTimeoutFetcher(timeoutMs, fetchImpl) {
2258
- return async (input, init = {}) => {
2259
- const controller = new AbortController();
2260
- const onAbort = () => controller.abort();
2261
- const timer = setTimeout(() => controller.abort(), timeoutMs);
2262
- if (init.signal) {
2263
- if (init.signal.aborted) {
2264
- clearTimeout(timer);
2265
- throw new Error("Request aborted");
1043
+ async function main(args) {
1044
+ const { command, target, flags } = parseArgs(args);
1045
+ if (command === "discover" && target) return runDiscover(target, flags);
1046
+ if (command === "check" && target) return runCheck(target, flags);
1047
+ console.log("Usage:");
1048
+ console.log(" discovery <origin> Discover all endpoints at an origin");
1049
+ console.log(" discovery discover <origin> Discover all endpoints at an origin");
1050
+ console.log(" discovery check <url> Inspect a specific endpoint URL");
1051
+ console.log("");
1052
+ console.log("Flags:");
1053
+ console.log(" --json Machine-readable JSON output");
1054
+ console.log(" -v Verbose output (includes guidance text and warning hints)");
1055
+ return 1;
1056
+ }
1057
+ async function runDiscover(target, flags) {
1058
+ const origin = normalizeOrigin(target);
1059
+ if (!flags.json) console.log(`
1060
+ Discovering ${origin}...
1061
+ `);
1062
+ const [openApiResult, wellKnownResult] = await Promise.all([
1063
+ getOpenAPI(origin),
1064
+ getWellKnown(origin)
1065
+ ]);
1066
+ const openApi = openApiResult.isOk() ? openApiResult.value : null;
1067
+ const wellKnown = wellKnownResult.isOk() ? wellKnownResult.value : null;
1068
+ const warnings = [
1069
+ ...getWarningsForOpenAPI(openApi),
1070
+ ...getWarningsForWellKnown(wellKnown)
1071
+ ];
1072
+ if (openApi) {
1073
+ const l2 = checkL2ForOpenAPI(openApi);
1074
+ const l4 = checkL4ForOpenAPI(openApi);
1075
+ warnings.push(...getWarningsForL2(l2), ...getWarningsForL4(l4));
1076
+ if (flags.json) {
1077
+ const meta = {
1078
+ origin,
1079
+ specUrl: openApi.fetchedUrl,
1080
+ ...l2.title ? { title: l2.title } : {},
1081
+ ...l2.description ? { description: l2.description } : {}
1082
+ };
1083
+ if (l4) {
1084
+ meta.guidanceTokens = Math.ceil(l4.guidance.length / 4);
1085
+ if (flags.verbose) meta.guidance = l4.guidance;
2266
1086
  }
2267
- init.signal.addEventListener("abort", onAbort, { once: true });
1087
+ console.log(
1088
+ JSON.stringify(
1089
+ {
1090
+ ok: true,
1091
+ selectedStage: "openapi",
1092
+ resources: l2.routes.map(routeToResource),
1093
+ warnings,
1094
+ trace: [],
1095
+ meta
1096
+ },
1097
+ null,
1098
+ 2
1099
+ )
1100
+ );
1101
+ return 0;
2268
1102
  }
2269
- try {
2270
- return await fetchImpl(input, {
2271
- ...init,
2272
- signal: controller.signal
2273
- });
2274
- } finally {
2275
- clearTimeout(timer);
2276
- init.signal?.removeEventListener("abort", onAbort);
1103
+ console.log(`Source: openapi`);
1104
+ console.log(`Spec: ${openApi.fetchedUrl}`);
1105
+ if (l2.title) console.log(`API: ${l2.title}`);
1106
+ console.log(`Routes: ${l2.routes.length}
1107
+ `);
1108
+ for (const route of l2.routes) {
1109
+ const auth = route.authMode ? ` ${route.authMode}` : "";
1110
+ const price = route.price ? ` ${route.price}` : "";
1111
+ const protocols = route.protocols?.length ? ` [${route.protocols.join(", ")}]` : "";
1112
+ console.log(` ${route.method.padEnd(7)} ${route.path}${auth}${price}${protocols}`);
2277
1113
  }
2278
- };
2279
- }
2280
- async function runAudit(options, deps = {}) {
2281
- const discoverDetailedFn = deps.discoverDetailedFn ?? discoverDetailed;
2282
- const fetchImpl = deps.fetchImpl ?? fetch;
2283
- const fetcher = createTimeoutFetcher(options.timeoutMs, fetchImpl);
2284
- const baseOptions = {
2285
- target: options.target,
2286
- compatMode: options.compatMode,
2287
- fetcher,
2288
- headers: {
2289
- "user-agent": CLI_USER_AGENT
2290
- },
2291
- rawView: options.harness ? "full" : "none"
2292
- };
2293
- const startedAt = Date.now();
2294
- let result = await discoverDetailedFn(baseOptions);
2295
- let probeCandidateCount = 0;
2296
- if (options.probe) {
2297
- const probeCandidates = buildProbeCandidates(result.resources);
2298
- probeCandidateCount = probeCandidates.length;
2299
- if (probeCandidates.length > 0) {
2300
- result = await discoverDetailedFn({
2301
- ...baseOptions,
2302
- probeCandidates
2303
- });
1114
+ if (l4) {
1115
+ const tokens = Math.ceil(l4.guidance.length / 4);
1116
+ console.log(`
1117
+ Guidance: ${tokens} tokens`);
1118
+ if (flags.verbose) console.log(`
1119
+ ${l4.guidance}`);
2304
1120
  }
2305
- }
2306
- return {
2307
- result,
2308
- durationMs: Date.now() - startedAt,
2309
- probeCandidateCount,
2310
- ...options.harness ? {
2311
- harness: buildContextHarnessReport({
2312
- target: options.target,
2313
- result,
2314
- client: options.client,
2315
- contextWindowTokens: options.contextWindowTokens
2316
- })
2317
- } : {}
2318
- };
2319
- }
2320
-
2321
- // src/cli.ts
2322
- function defaultStdout(message) {
2323
- process.stdout.write(`${message}
1121
+ } else if (wellKnown) {
1122
+ const l2 = checkL2ForWellknown(wellKnown);
1123
+ const l4 = checkL4ForWellknown(wellKnown);
1124
+ warnings.push(...getWarningsForL2(l2), ...getWarningsForL4(l4));
1125
+ if (flags.json) {
1126
+ const meta = { origin, specUrl: wellKnown.fetchedUrl };
1127
+ if (l4 && flags.verbose) meta.guidance = l4.guidance;
1128
+ console.log(
1129
+ JSON.stringify(
1130
+ {
1131
+ ok: true,
1132
+ selectedStage: "well-known/x402",
1133
+ resources: l2.routes.map(routeToResource),
1134
+ warnings,
1135
+ trace: [],
1136
+ meta
1137
+ },
1138
+ null,
1139
+ 2
1140
+ )
1141
+ );
1142
+ return 0;
1143
+ }
1144
+ console.log(`Source: well-known/x402`);
1145
+ console.log(`Spec: ${wellKnown.fetchedUrl}`);
1146
+ console.log(`Routes: ${l2.routes.length}
2324
1147
  `);
1148
+ for (const route of l2.routes) {
1149
+ console.log(` ${route.method.padEnd(7)} ${route.path} paid [x402]`);
1150
+ }
1151
+ } else {
1152
+ if (flags.json) {
1153
+ console.log(
1154
+ JSON.stringify(
1155
+ { ok: false, selectedStage: null, resources: [], warnings, trace: [], meta: { origin } },
1156
+ null,
1157
+ 2
1158
+ )
1159
+ );
1160
+ return 0;
1161
+ }
1162
+ console.log("Not found \u2014 no OpenAPI spec or /.well-known/x402 at this origin.");
1163
+ }
1164
+ printWarnings(warnings, flags.verbose);
1165
+ return 0;
2325
1166
  }
2326
- function defaultStderr(message) {
2327
- process.stderr.write(`${message}
1167
+ async function runCheck(url, flags) {
1168
+ if (!flags.json) console.log(`
1169
+ Checking ${url}...
2328
1170
  `);
2329
- }
2330
- async function main(argv = process.argv.slice(2), deps = {}) {
2331
- const stdout = deps.stdout ?? defaultStdout;
2332
- const stderr = deps.stderr ?? defaultStderr;
2333
- const parsed = parseCliArgs(argv);
2334
- if (parsed.kind === "help") {
2335
- stdout(renderHelp());
2336
- return 0;
2337
- }
2338
- if (parsed.kind === "error") {
2339
- stderr(parsed.message);
2340
- stderr("");
2341
- stderr(renderHelp());
2342
- return 2;
1171
+ const result = await checkEndpointSchema({ url });
1172
+ if (!result.found) {
1173
+ const warnings2 = getWarningsForL3(null);
1174
+ if (flags.json) {
1175
+ console.log(JSON.stringify({ url, found: false, warnings: warnings2 }, null, 2));
1176
+ return 1;
1177
+ }
1178
+ console.log("Not found \u2014 no spec data for this endpoint.");
1179
+ printWarnings(warnings2, flags.verbose);
1180
+ return 1;
2343
1181
  }
2344
- try {
2345
- const run = await runAudit(parsed.options, deps);
2346
- if (parsed.options.json) {
2347
- stdout(renderJson(run, parsed.options));
2348
- } else if (parsed.options.verbose) {
2349
- stdout(renderVerbose(run, parsed.options));
2350
- } else {
2351
- stdout(renderSummary(run, parsed.options));
1182
+ const warnings = [];
1183
+ if (flags.json) {
1184
+ for (const advisory of result.advisories) {
1185
+ warnings.push(...getWarningsForL3(advisory));
2352
1186
  }
2353
- return run.result.resources.length > 0 ? 0 : 1;
2354
- } catch (error) {
2355
- const message = error instanceof Error ? error.message : String(error);
2356
- stderr(`Discovery audit failed: ${message}`);
2357
- return 2;
1187
+ console.log(
1188
+ JSON.stringify(
1189
+ {
1190
+ url,
1191
+ found: true,
1192
+ origin: result.origin,
1193
+ path: result.path,
1194
+ advisories: result.advisories,
1195
+ warnings
1196
+ },
1197
+ null,
1198
+ 2
1199
+ )
1200
+ );
1201
+ return 0;
1202
+ }
1203
+ console.log(`Origin: ${result.origin}`);
1204
+ console.log(`Path: ${result.path}
1205
+ `);
1206
+ for (const advisory of result.advisories) {
1207
+ const auth = advisory.authMode ? ` ${advisory.authMode}` : "";
1208
+ const price = advisory.estimatedPrice ? ` ${advisory.estimatedPrice}` : "";
1209
+ const protocols = advisory.protocols?.length ? ` [${advisory.protocols.join(", ")}]` : "";
1210
+ console.log(` ${advisory.method.padEnd(7)}${auth}${price}${protocols}`);
1211
+ warnings.push(...getWarningsForL3(advisory));
1212
+ }
1213
+ printWarnings(warnings, flags.verbose);
1214
+ return 0;
1215
+ }
1216
+ function printWarnings(warnings, verbose) {
1217
+ if (warnings.length === 0) return;
1218
+ console.log(`
1219
+ Warnings (${warnings.length}):`);
1220
+ for (const w of warnings) {
1221
+ const loc = w.path ? ` (${w.path})` : "";
1222
+ const hint = verbose && w.hint ? `
1223
+ Hint: ${w.hint}` : "";
1224
+ console.log(` [${w.severity.padEnd(4)}] ${w.code}${loc}
1225
+ ${w.message}${hint}`);
2358
1226
  }
2359
1227
  }
2360
1228
  // Annotate the CommonJS export names for ESM import in node: