@agentcash/discovery 1.2.0 → 1.3.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/index.js CHANGED
@@ -13842,7 +13842,8 @@ var WellKnownParsedSchema = external_exports.object({
13842
13842
  routes: external_exports.array(
13843
13843
  external_exports.object({
13844
13844
  path: external_exports.string(),
13845
- method: external_exports.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"])
13845
+ method: external_exports.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"]),
13846
+ price: external_exports.string().optional()
13846
13847
  })
13847
13848
  ),
13848
13849
  instructions: external_exports.string().optional()
@@ -13935,9 +13936,9 @@ function toAbsoluteUrl(origin, value) {
13935
13936
 
13936
13937
  // src/core/source/fetch.ts
13937
13938
  import { ResultAsync } from "neverthrow";
13938
- function toFetchError(err) {
13939
- const cause = err instanceof DOMException && (err.name === "TimeoutError" || err.name === "AbortError") ? "timeout" : "network";
13940
- return { cause, message: String(err) };
13939
+ function toFetchError(err2) {
13940
+ const cause = err2 instanceof DOMException && (err2.name === "TimeoutError" || err2.name === "AbortError") ? "timeout" : "network";
13941
+ return { cause, message: String(err2) };
13941
13942
  }
13942
13943
  function fetchSafe(url2, init) {
13943
13944
  return ResultAsync.fromPromise(fetch(url2, init), toFetchError);
@@ -14003,6 +14004,9 @@ function getOpenAPI(origin, headers, signal, specificationOverrideUrl) {
14003
14004
  }
14004
14005
 
14005
14006
  // src/core/source/wellknown/index.ts
14007
+ import { ok, err, ResultAsync as ResultAsync5 } from "neverthrow";
14008
+
14009
+ // src/core/source/wellknown/x402.ts
14006
14010
  import { okAsync as okAsync2, ResultAsync as ResultAsync3 } from "neverthrow";
14007
14011
  function toWellKnownParsed(origin, doc) {
14008
14012
  const routes = doc.resources.flatMap((entry) => {
@@ -14029,12 +14033,17 @@ async function parseBody2(response, origin, url2) {
14029
14033
  if (!doc.success) return null;
14030
14034
  const parsed = WellKnownParsedSchema.safeParse(toWellKnownParsed(origin, doc.data));
14031
14035
  if (!parsed.success) return null;
14032
- return { raw: payload, ...parsed.data, fetchedUrl: url2 };
14036
+ return {
14037
+ raw: payload,
14038
+ ...parsed.data,
14039
+ protocol: "x402",
14040
+ fetchedUrl: url2
14041
+ };
14033
14042
  } catch {
14034
14043
  return null;
14035
14044
  }
14036
14045
  }
14037
- function getWellKnown(origin, headers, signal) {
14046
+ function getX402WellKnown(origin, headers, signal) {
14038
14047
  const url2 = `${origin}/.well-known/x402`;
14039
14048
  return fetchSafe(url2, {
14040
14049
  method: "GET",
@@ -14046,6 +14055,127 @@ function getWellKnown(origin, headers, signal) {
14046
14055
  });
14047
14056
  }
14048
14057
 
14058
+ // src/core/source/wellknown/mpp.ts
14059
+ import { okAsync as okAsync3, ResultAsync as ResultAsync4 } from "neverthrow";
14060
+ var MppEndpointSchema = external_exports.object({
14061
+ method: external_exports.string(),
14062
+ path: external_exports.string(),
14063
+ description: external_exports.string().optional(),
14064
+ payment: external_exports.object({
14065
+ intent: external_exports.string().optional(),
14066
+ method: external_exports.string().optional(),
14067
+ amount: external_exports.string().optional(),
14068
+ currency: external_exports.string().optional()
14069
+ }).optional()
14070
+ });
14071
+ var MppWellKnownDocSchema = external_exports.object({
14072
+ version: external_exports.number().optional(),
14073
+ name: external_exports.string().optional(),
14074
+ description: external_exports.string().optional(),
14075
+ categories: external_exports.array(external_exports.string()).optional(),
14076
+ methods: external_exports.record(external_exports.string(), external_exports.unknown()).optional(),
14077
+ endpoints: external_exports.array(MppEndpointSchema).default([]),
14078
+ docs: external_exports.object({
14079
+ homepage: external_exports.string().optional(),
14080
+ apiReference: external_exports.string().optional()
14081
+ }).optional()
14082
+ });
14083
+ var MPP_DECIMALS = 6;
14084
+ function formatMppAmount(raw) {
14085
+ if (!raw) return void 0;
14086
+ const n = Number(raw);
14087
+ if (!Number.isFinite(n)) return void 0;
14088
+ return `$${(n / 10 ** MPP_DECIMALS).toFixed(MPP_DECIMALS)}`;
14089
+ }
14090
+ function toWellKnownParsed2(doc) {
14091
+ const routes = doc.endpoints.flatMap((entry) => {
14092
+ const method = parseMethod(entry.method);
14093
+ if (!method) return [];
14094
+ const path = normalizePath(entry.path);
14095
+ if (!path) return [];
14096
+ const price = formatMppAmount(entry.payment?.amount);
14097
+ return [{ path, method, ...price ? { price } : {} }];
14098
+ });
14099
+ return {
14100
+ routes,
14101
+ ...doc.description ? { instructions: doc.description } : {}
14102
+ };
14103
+ }
14104
+ async function parseBody3(response, url2) {
14105
+ try {
14106
+ const payload = await response.json();
14107
+ const doc = MppWellKnownDocSchema.safeParse(payload);
14108
+ if (!doc.success) return null;
14109
+ const parsed = WellKnownParsedSchema.safeParse(toWellKnownParsed2(doc.data));
14110
+ if (!parsed.success) return null;
14111
+ return {
14112
+ raw: payload,
14113
+ ...parsed.data,
14114
+ ...doc.data.name ? { title: doc.data.name } : {},
14115
+ ...doc.data.description ? { description: doc.data.description } : {},
14116
+ protocol: "mpp",
14117
+ fetchedUrl: url2
14118
+ };
14119
+ } catch {
14120
+ return null;
14121
+ }
14122
+ }
14123
+ function getMppWellKnown(origin, headers, signal) {
14124
+ const url2 = `${origin}/.well-known/mpp`;
14125
+ return fetchSafe(url2, {
14126
+ method: "GET",
14127
+ headers: { Accept: "application/json", ...headers },
14128
+ signal
14129
+ }).andThen((response) => {
14130
+ if (!response.ok) return okAsync3(null);
14131
+ return ResultAsync4.fromSafePromise(parseBody3(response, url2));
14132
+ });
14133
+ }
14134
+
14135
+ // src/core/source/wellknown/index.ts
14136
+ function mergeError(a, b) {
14137
+ return {
14138
+ cause: a.cause === "network" || b.cause === "network" ? "network" : "timeout",
14139
+ message: `x402: ${a.message} | mpp: ${b.message}`
14140
+ };
14141
+ }
14142
+ function merge2(x402, mpp) {
14143
+ const seen = /* @__PURE__ */ new Set();
14144
+ const routes = [...x402.routes, ...mpp.routes].filter((r) => {
14145
+ const key = `${r.method} ${r.path}`;
14146
+ if (seen.has(key)) return false;
14147
+ seen.add(key);
14148
+ return true;
14149
+ });
14150
+ return {
14151
+ raw: { ...mpp.raw, ...x402.raw },
14152
+ routes,
14153
+ protocol: "x402+mpp",
14154
+ // Prefer x402 instructions; fall back to mpp
14155
+ ...x402.instructions || mpp.instructions ? { instructions: x402.instructions ?? mpp.instructions } : {},
14156
+ fetchedUrl: x402.fetchedUrl
14157
+ };
14158
+ }
14159
+ function getWellKnown(origin, headers, signal) {
14160
+ return new ResultAsync5(
14161
+ Promise.all([
14162
+ getX402WellKnown(origin, headers, signal),
14163
+ getMppWellKnown(origin, headers, signal)
14164
+ ]).then(([x402Result, mppResult]) => {
14165
+ const x402 = x402Result.isOk() ? x402Result.value : null;
14166
+ const mpp = mppResult.isOk() ? mppResult.value : null;
14167
+ if (x402 && mpp) return ok(merge2(x402, mpp));
14168
+ if (x402) return ok(x402);
14169
+ if (mpp) return ok(mpp);
14170
+ if (x402Result.isErr() && mppResult.isErr())
14171
+ return err(mergeError(x402Result.error, mppResult.error));
14172
+ if (x402Result.isErr()) return err(x402Result.error);
14173
+ if (mppResult.isErr()) return err(mppResult.error);
14174
+ return ok(null);
14175
+ })
14176
+ );
14177
+ }
14178
+
14049
14179
  // src/core/layers/l2.ts
14050
14180
  function formatPrice(pricing) {
14051
14181
  if (pricing.pricingMode === "fixed") return `$${pricing.price}`;
@@ -14071,15 +14201,27 @@ function checkL2ForOpenAPI(openApi) {
14071
14201
  source: "openapi"
14072
14202
  };
14073
14203
  }
14204
+ var WELL_KNOWN_PROTOCOLS = {
14205
+ x402: ["x402"],
14206
+ mpp: ["mpp"],
14207
+ "x402+mpp": ["x402", "mpp"]
14208
+ };
14074
14209
  function checkL2ForWellknown(wellKnown) {
14210
+ const protocols = WELL_KNOWN_PROTOCOLS[wellKnown.protocol];
14075
14211
  const routes = wellKnown.routes.map((route) => ({
14076
14212
  path: route.path,
14077
14213
  method: route.method,
14078
14214
  summary: `${route.method} ${route.path}`,
14079
14215
  authMode: "paid",
14080
- protocols: ["x402"]
14216
+ protocols,
14217
+ ...route.price ? { price: route.price } : {}
14081
14218
  }));
14082
- return { routes, source: "well-known/x402" };
14219
+ return {
14220
+ ...wellKnown.title ? { title: wellKnown.title } : {},
14221
+ ...wellKnown.description ? { description: wellKnown.description } : {},
14222
+ routes,
14223
+ source: `well-known/${wellKnown.protocol}`
14224
+ };
14083
14225
  }
14084
14226
 
14085
14227
  // src/core/layers/l4.ts
@@ -14091,7 +14233,7 @@ function checkL4ForOpenAPI(openApi) {
14091
14233
  }
14092
14234
  function checkL4ForWellknown(wellKnown) {
14093
14235
  if (wellKnown.instructions) {
14094
- return { guidance: wellKnown.instructions, source: "well-known/x402" };
14236
+ return { guidance: wellKnown.instructions, source: `well-known/${wellKnown.protocol}` };
14095
14237
  }
14096
14238
  return null;
14097
14239
  }
@@ -14164,14 +14306,20 @@ async function discoverOriginSchema(options) {
14164
14306
  const base = {
14165
14307
  found: true,
14166
14308
  origin,
14167
- source: "well-known/x402",
14309
+ source: l2.source,
14310
+ ...l2.title ? {
14311
+ info: {
14312
+ title: l2.title,
14313
+ ...l2.description ? { description: l2.description } : {}
14314
+ }
14315
+ } : {},
14168
14316
  endpoints: l2.routes
14169
14317
  };
14170
14318
  return withGuidance(base, l4, guidanceMode);
14171
14319
  }
14172
14320
 
14173
14321
  // src/core/source/probe/index.ts
14174
- import { ResultAsync as ResultAsync4 } from "neverthrow";
14322
+ import { ResultAsync as ResultAsync6 } from "neverthrow";
14175
14323
 
14176
14324
  // src/core/protocols/x402/v1/schema.ts
14177
14325
  function extractSchemas(accepts) {
@@ -14621,8 +14769,8 @@ function probeMethod(url2, method, path, headers, signal, inputBody) {
14621
14769
  ...hasBody ? { body: JSON.stringify(inputBody) } : {},
14622
14770
  signal
14623
14771
  }).andThen((response) => {
14624
- if (!isUsableStatus(response.status)) return ResultAsync4.fromSafePromise(Promise.resolve(null));
14625
- return ResultAsync4.fromSafePromise(
14772
+ if (!isUsableStatus(response.status)) return ResultAsync6.fromSafePromise(Promise.resolve(null));
14773
+ return ResultAsync6.fromSafePromise(
14626
14774
  (async () => {
14627
14775
  let authHint = response.status === 402 ? "paid" : "unprotected";
14628
14776
  let paymentRequiredBody;
@@ -14653,7 +14801,7 @@ function probeMethod(url2, method, path, headers, signal, inputBody) {
14653
14801
  }
14654
14802
  function getProbe(url2, headers, signal, inputBody) {
14655
14803
  const path = normalizePath(new URL(url2).pathname || "/");
14656
- return ResultAsync4.fromSafePromise(
14804
+ return ResultAsync6.fromSafePromise(
14657
14805
  Promise.all(
14658
14806
  [...HTTP_METHODS].map(
14659
14807
  (method) => probeMethod(url2, method, path, headers, signal, inputBody).match(
@@ -14666,6 +14814,20 @@ function getProbe(url2, headers, signal, inputBody) {
14666
14814
  }
14667
14815
 
14668
14816
  // src/core/protocols/mpp/index.ts
14817
+ import { Result } from "neverthrow";
14818
+ function parseBase64Json(encoded) {
14819
+ return Result.fromThrowable(
14820
+ () => {
14821
+ const decoded = typeof Buffer !== "undefined" ? Buffer.from(encoded, "base64").toString("utf8") : atob(encoded);
14822
+ const parsed = JSON.parse(decoded);
14823
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
14824
+ throw new Error("not an object");
14825
+ }
14826
+ return parsed;
14827
+ },
14828
+ (e) => e
14829
+ )();
14830
+ }
14669
14831
  var TEMPO_DEFAULT_CHAIN_ID = 4217;
14670
14832
  function parseAuthParams(segment) {
14671
14833
  const params = {};
@@ -14688,14 +14850,9 @@ function extractPaymentOptions4(wwwAuthenticate) {
14688
14850
  const description = params["description"];
14689
14851
  const requestStr = params["request"];
14690
14852
  if (!paymentMethod || !intent || !realm || !requestStr) continue;
14691
- let request;
14692
- try {
14693
- const parsed = JSON.parse(requestStr);
14694
- if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) continue;
14695
- request = parsed;
14696
- } catch {
14697
- continue;
14698
- }
14853
+ const requestResult = parseBase64Json(requestStr);
14854
+ if (requestResult.isErr()) continue;
14855
+ const request = requestResult.value;
14699
14856
  const asset = typeof request["currency"] === "string" ? request["currency"] : void 0;
14700
14857
  const amountRaw = request["amount"];
14701
14858
  const amount = typeof amountRaw === "string" ? amountRaw : typeof amountRaw === "number" ? String(amountRaw) : void 0;
@@ -15683,6 +15840,7 @@ export {
15683
15840
  getL3,
15684
15841
  getL3ForOpenAPI,
15685
15842
  getL3ForProbe,
15843
+ getMppWellKnown,
15686
15844
  getOpenAPI,
15687
15845
  getProbe,
15688
15846
  getWarningsFor402Body,
@@ -15693,5 +15851,6 @@ export {
15693
15851
  getWarningsForOpenAPI,
15694
15852
  getWarningsForWellKnown,
15695
15853
  getWellKnown,
15854
+ getX402WellKnown,
15696
15855
  validatePaymentRequiredDetailed
15697
15856
  };
package/dist/schemas.cjs CHANGED
@@ -13864,7 +13864,8 @@ var WellKnownParsedSchema = external_exports.object({
13864
13864
  routes: external_exports.array(
13865
13865
  external_exports.object({
13866
13866
  path: external_exports.string(),
13867
- method: external_exports.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"])
13867
+ method: external_exports.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"]),
13868
+ price: external_exports.string().optional()
13868
13869
  })
13869
13870
  ),
13870
13871
  instructions: external_exports.string().optional()
@@ -517,6 +517,7 @@ declare const WellKnownParsedSchema: z.ZodObject<{
517
517
  OPTIONS: "OPTIONS";
518
518
  TRACE: "TRACE";
519
519
  }>;
520
+ price: z.ZodOptional<z.ZodString>;
520
521
  }, z.core.$strip>>;
521
522
  instructions: z.ZodOptional<z.ZodString>;
522
523
  }, z.core.$strip>;
package/dist/schemas.d.ts CHANGED
@@ -517,6 +517,7 @@ declare const WellKnownParsedSchema: z.ZodObject<{
517
517
  OPTIONS: "OPTIONS";
518
518
  TRACE: "TRACE";
519
519
  }>;
520
+ price: z.ZodOptional<z.ZodString>;
520
521
  }, z.core.$strip>>;
521
522
  instructions: z.ZodOptional<z.ZodString>;
522
523
  }, z.core.$strip>;
package/dist/schemas.js CHANGED
@@ -13839,7 +13839,8 @@ var WellKnownParsedSchema = external_exports.object({
13839
13839
  routes: external_exports.array(
13840
13840
  external_exports.object({
13841
13841
  path: external_exports.string(),
13842
- method: external_exports.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"])
13842
+ method: external_exports.enum(["GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE"]),
13843
+ price: external_exports.string().optional()
13843
13844
  })
13844
13845
  ),
13845
13846
  instructions: external_exports.string().optional()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentcash/discovery",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Canonical OpenAPI-first discovery runtime for the agentcash ecosystem",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",