@agent-score/commerce 2.0.2 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (116) hide show
  1. package/README.md +26 -11
  2. package/dist/_core-kI7FRAiZ.d.mts +10 -0
  3. package/dist/_core-kI7FRAiZ.d.ts +10 -0
  4. package/dist/challenge/index.d.mts +3 -3
  5. package/dist/challenge/index.d.ts +3 -3
  6. package/dist/challenge/index.js +21 -14
  7. package/dist/challenge/index.js.map +1 -1
  8. package/dist/challenge/index.mjs +21 -14
  9. package/dist/challenge/index.mjs.map +1 -1
  10. package/dist/{checkout-jNUIql6D.d.mts → checkout-BRw_caGr.d.mts} +13 -33
  11. package/dist/{checkout-DhSj_h94.d.ts → checkout-CuSNUJFX.d.ts} +13 -33
  12. package/dist/core.js +1 -1
  13. package/dist/core.js.map +1 -1
  14. package/dist/core.mjs +1 -1
  15. package/dist/core.mjs.map +1 -1
  16. package/dist/default_rails-C5gKZJMI.d.ts +198 -0
  17. package/dist/default_rails-XFCuRddA.d.mts +198 -0
  18. package/dist/discovery/index.d.mts +5 -5
  19. package/dist/discovery/index.d.ts +5 -5
  20. package/dist/discovery/index.js +14 -1
  21. package/dist/discovery/index.js.map +1 -1
  22. package/dist/discovery/index.mjs +14 -1
  23. package/dist/discovery/index.mjs.map +1 -1
  24. package/dist/identity/express.d.mts +7 -3
  25. package/dist/identity/express.d.ts +7 -3
  26. package/dist/identity/express.js +39 -96
  27. package/dist/identity/express.js.map +1 -1
  28. package/dist/identity/express.mjs +37 -87
  29. package/dist/identity/express.mjs.map +1 -1
  30. package/dist/identity/fastify.d.mts +4 -4
  31. package/dist/identity/fastify.d.ts +4 -4
  32. package/dist/identity/fastify.js +60 -96
  33. package/dist/identity/fastify.js.map +1 -1
  34. package/dist/identity/fastify.mjs +58 -87
  35. package/dist/identity/fastify.mjs.map +1 -1
  36. package/dist/identity/hono.d.mts +11 -3
  37. package/dist/identity/hono.d.ts +11 -3
  38. package/dist/identity/hono.js +39 -93
  39. package/dist/identity/hono.js.map +1 -1
  40. package/dist/identity/hono.mjs +37 -84
  41. package/dist/identity/hono.mjs.map +1 -1
  42. package/dist/identity/nextjs.d.mts +10 -3
  43. package/dist/identity/nextjs.d.ts +10 -3
  44. package/dist/identity/nextjs.js +49 -93
  45. package/dist/identity/nextjs.js.map +1 -1
  46. package/dist/identity/nextjs.mjs +46 -84
  47. package/dist/identity/nextjs.mjs.map +1 -1
  48. package/dist/identity/policy.js +22 -23317
  49. package/dist/identity/policy.js.map +1 -1
  50. package/dist/identity/policy.mjs +1 -23320
  51. package/dist/identity/policy.mjs.map +1 -1
  52. package/dist/identity/web.d.mts +9 -3
  53. package/dist/identity/web.d.ts +9 -3
  54. package/dist/identity/web.js +45 -93
  55. package/dist/identity/web.js.map +1 -1
  56. package/dist/identity/web.mjs +42 -84
  57. package/dist/identity/web.mjs.map +1 -1
  58. package/dist/index.d.mts +621 -90
  59. package/dist/index.d.ts +621 -90
  60. package/dist/index.js +1202 -328
  61. package/dist/index.js.map +1 -1
  62. package/dist/index.mjs +1188 -327
  63. package/dist/index.mjs.map +1 -1
  64. package/dist/middleware/express.d.mts +10 -0
  65. package/dist/middleware/express.d.ts +10 -0
  66. package/dist/middleware/express.js +128 -0
  67. package/dist/middleware/express.js.map +1 -0
  68. package/dist/middleware/express.mjs +91 -0
  69. package/dist/middleware/express.mjs.map +1 -0
  70. package/dist/middleware/fastify.d.mts +10 -0
  71. package/dist/middleware/fastify.d.ts +10 -0
  72. package/dist/middleware/fastify.js +127 -0
  73. package/dist/middleware/fastify.js.map +1 -0
  74. package/dist/middleware/fastify.mjs +90 -0
  75. package/dist/middleware/fastify.mjs.map +1 -0
  76. package/dist/middleware/hono.d.mts +10 -0
  77. package/dist/middleware/hono.d.ts +10 -0
  78. package/dist/middleware/hono.js +122 -0
  79. package/dist/middleware/hono.js.map +1 -0
  80. package/dist/middleware/hono.mjs +85 -0
  81. package/dist/middleware/hono.mjs.map +1 -0
  82. package/dist/middleware/nextjs.d.mts +22 -0
  83. package/dist/middleware/nextjs.d.ts +22 -0
  84. package/dist/middleware/nextjs.js +143 -0
  85. package/dist/middleware/nextjs.js.map +1 -0
  86. package/dist/middleware/nextjs.mjs +105 -0
  87. package/dist/middleware/nextjs.mjs.map +1 -0
  88. package/dist/middleware/web.d.mts +25 -0
  89. package/dist/middleware/web.d.ts +25 -0
  90. package/dist/middleware/web.js +128 -0
  91. package/dist/middleware/web.js.map +1 -0
  92. package/dist/middleware/web.mjs +91 -0
  93. package/dist/middleware/web.mjs.map +1 -0
  94. package/dist/payment/index.d.mts +32 -21
  95. package/dist/payment/index.d.ts +32 -21
  96. package/dist/payment/index.js +215 -12
  97. package/dist/payment/index.js.map +1 -1
  98. package/dist/payment/index.mjs +205 -12
  99. package/dist/payment/index.mjs.map +1 -1
  100. package/dist/{pricing-CxzwyiO6.d.mts → pricing-4n5Ota0D.d.mts} +14 -4
  101. package/dist/{pricing-CQ9DIFaw.d.ts → pricing-DHfH3ogG.d.ts} +14 -4
  102. package/dist/{rail_spec-XP0wKgJV.d.mts → rail_spec-D6qzh3J0.d.mts} +1 -1
  103. package/dist/{rail_spec-XP0wKgJV.d.ts → rail_spec-D6qzh3J0.d.ts} +1 -1
  104. package/dist/stripe-multichain/index.d.mts +150 -47
  105. package/dist/stripe-multichain/index.d.ts +150 -47
  106. package/dist/stripe-multichain/index.js +19799 -43
  107. package/dist/stripe-multichain/index.js.map +1 -1
  108. package/dist/stripe-multichain/index.mjs +19808 -28
  109. package/dist/stripe-multichain/index.mjs.map +1 -1
  110. package/dist/{x402_server-hgQzWQwB.d.mts → x402_server-Ciz2mls2.d.mts} +1 -1
  111. package/dist/{x402_server-hgQzWQwB.d.ts → x402_server-Ciz2mls2.d.ts} +1 -1
  112. package/package.json +43 -5
  113. package/dist/_response-BFYN3b6i.d.mts +0 -142
  114. package/dist/_response-_iPD5AIj.d.ts +0 -142
  115. package/dist/solana-Cds87OTu.d.mts +0 -67
  116. package/dist/solana-Cds87OTu.d.ts +0 -67
@@ -565,6 +565,9 @@ async function verifyX402Request({
565
565
  return { ok: true, payload, signedNetwork, signedPayTo };
566
566
  }
567
567
 
568
+ // src/payment/mppx_server.ts
569
+ import { AsyncLocalStorage } from "async_hooks";
570
+
568
571
  // src/stripe-multichain/mppx_stripe.ts
569
572
  async function createMppxStripe({
570
573
  profileId,
@@ -599,7 +602,8 @@ function isSolanaMppRailSpec(s) {
599
602
  return s.network?.startsWith("solana:") ?? false;
600
603
  }
601
604
  function solanaNetworkFromCAIP2(caip2) {
602
- if (caip2 === networks.solana.devnet.caip2) return "devnet";
605
+ if (caip2 === "devnet" || caip2 === networks.solana.devnet.caip2) return "devnet";
606
+ if (caip2 === "localnet") return "localnet";
603
607
  return "mainnet-beta";
604
608
  }
605
609
  function solanaDefaultRpcUrl(network) {
@@ -742,6 +746,31 @@ function wrapSolanaChargeWithFinalizedBlockhash(baseMethod, rpcUrl) {
742
746
  }
743
747
  };
744
748
  }
749
+ var mppxCapture = new AsyncLocalStorage();
750
+ var consoleErrorPatched = false;
751
+ function ensureConsoleErrorPatch() {
752
+ if (consoleErrorPatched) return;
753
+ consoleErrorPatched = true;
754
+ const original = console.error.bind(console);
755
+ console.error = function captureMppxInternal(...args) {
756
+ if (args[0] === "mppx: internal verification error" && args[1] !== void 0) {
757
+ const ctx = mppxCapture.getStore();
758
+ if (ctx) {
759
+ const e = args[1];
760
+ const reason = typeof e?.shortMessage === "string" ? e.shortMessage : typeof e?.message === "string" ? e.message : String(args[1]);
761
+ const details = e?.details;
762
+ ctx.reason = typeof details === "string" && details.length > 0 ? `${reason} (${details})` : reason;
763
+ }
764
+ }
765
+ return original(...args);
766
+ };
767
+ }
768
+ async function runWithMppxFailureCapture(fn) {
769
+ ensureConsoleErrorPatch();
770
+ const ctx = { reason: null };
771
+ const result = await mppxCapture.run(ctx, fn);
772
+ return { result, failureReason: ctx.reason };
773
+ }
745
774
  async function composeMppxRequest(mppx, intents, request) {
746
775
  if (!mppx || typeof mppx !== "object" || !("compose" in mppx)) {
747
776
  throw new Error("composeMppxRequest: argument is not an mppx server instance");
@@ -750,6 +779,7 @@ async function composeMppxRequest(mppx, intents, request) {
750
779
  if (typeof compose !== "function") {
751
780
  throw new Error("composeMppxRequest: mppx.compose is not a function");
752
781
  }
782
+ ensureConsoleErrorPatch();
753
783
  const typedCompose = compose;
754
784
  const handler = typedCompose.apply(mppx, [...intents]);
755
785
  return handler(request);
@@ -758,6 +788,22 @@ function mppxChallengeHeaders(result) {
758
788
  return Object.fromEntries(result.challenge.headers);
759
789
  }
760
790
 
791
+ // src/errors.ts
792
+ var CheckoutValidationError = class extends Error {
793
+ code;
794
+ action;
795
+ status;
796
+ extra;
797
+ constructor(opts) {
798
+ super(opts.message);
799
+ this.name = "CheckoutValidationError";
800
+ this.code = opts.code;
801
+ this.action = opts.action ?? "fix_request";
802
+ this.status = opts.status ?? 400;
803
+ this.extra = opts.extra;
804
+ }
805
+ };
806
+
761
807
  // src/payment/dispatch.ts
762
808
  function detectRailFromHeaders(headers) {
763
809
  const get = (name) => {
@@ -786,17 +832,32 @@ async function dispatchSettlementByNetwork(payload, handlers) {
786
832
  const network = payload.accepted.network;
787
833
  if (network.startsWith("eip155:")) {
788
834
  if (!handlers.evm) {
789
- throw new Error(`No EVM settlement handler registered (network: ${network})`);
835
+ throw new CheckoutValidationError({
836
+ code: "payment_provider_unavailable",
837
+ message: `No EVM settlement handler registered (network: ${network})`,
838
+ action: "retry_later",
839
+ status: 503
840
+ });
790
841
  }
791
842
  return handlers.evm(payload);
792
843
  }
793
844
  if (network.startsWith("solana:")) {
794
845
  if (!handlers.svm) {
795
- throw new Error(`No Solana settlement handler registered (network: ${network})`);
846
+ throw new CheckoutValidationError({
847
+ code: "payment_provider_unavailable",
848
+ message: `No Solana settlement handler registered (network: ${network})`,
849
+ action: "retry_later",
850
+ status: 503
851
+ });
796
852
  }
797
853
  return handlers.svm(payload);
798
854
  }
799
- throw new Error(`Unrecognized network in settlement payload: ${network}`);
855
+ throw new CheckoutValidationError({
856
+ code: "payment_provider_unavailable",
857
+ message: `Unrecognized network in settlement payload: ${network}`,
858
+ action: "retry_later",
859
+ status: 503
860
+ });
800
861
  }
801
862
 
802
863
  // src/payment/wwwauthenticate.ts
@@ -886,6 +947,13 @@ function clampKey(key) {
886
947
  return key;
887
948
  }
888
949
 
950
+ // src/_headers.ts
951
+ function normalizeHeadersToLowercase(headers) {
952
+ const out = {};
953
+ for (const [k, v] of Object.entries(headers)) out[k.toLowerCase()] = v;
954
+ return out;
955
+ }
956
+
889
957
  // src/signer.ts
890
958
  var TOKEN_PROGRAM = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
891
959
  var TOKEN_2022_PROGRAM = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb";
@@ -968,13 +1036,8 @@ async function extractPaymentSignerFromAuth(authHeader, x402PaymentHeader) {
968
1036
  function readX402PaymentHeader(request) {
969
1037
  return request.headers.get("payment-signature") ?? request.headers.get("x-payment") ?? void 0;
970
1038
  }
971
- function lowerHeaders(headers) {
972
- const out = {};
973
- for (const [k, v] of Object.entries(headers)) out[k.toLowerCase()] = v;
974
- return out;
975
- }
976
1039
  async function extractSignerForPrecheck(headers) {
977
- const lower = lowerHeaders(headers);
1040
+ const lower = normalizeHeadersToLowercase(headers);
978
1041
  const x402 = lower["payment-signature"] ?? lower["x-payment"];
979
1042
  if (x402) {
980
1043
  const signer = await extractPaymentSignerFromAuth(void 0, x402);
@@ -1031,8 +1094,8 @@ function usdToAtomic(usd, opts) {
1031
1094
  }
1032
1095
  return result;
1033
1096
  }
1034
- function formatUsdCents(cents) {
1035
- return (cents / 100).toFixed(2);
1097
+ function formatUsdCents(cents, decimals = 2) {
1098
+ return (cents / 100).toFixed(decimals);
1036
1099
  }
1037
1100
 
1038
1101
  // src/payment/zero-settle.ts
@@ -1168,13 +1231,136 @@ async function loadSolanaFeePayer(opts) {
1168
1231
  }
1169
1232
  return kit.createKeyPairSignerFromPrivateKeyBytes(bytes);
1170
1233
  }
1234
+
1235
+ // src/payment/constants.ts
1236
+ var STRIPE_MIN_CHARGE_USD = 0.5;
1237
+
1238
+ // src/payment/compose_rails.ts
1239
+ var warnedStripeBelowMinimum = false;
1240
+ function buildMppxComposeRails(opts) {
1241
+ const rails2 = [];
1242
+ if (opts.tempoRecipient) {
1243
+ rails2.push(["tempo/charge", {
1244
+ amount: opts.amountUsd,
1245
+ currency: opts.tempoTokenAddress ?? USDC.tempo.mainnet.address,
1246
+ decimals: 6,
1247
+ recipient: opts.tempoRecipient
1248
+ }]);
1249
+ }
1250
+ if (opts.solanaRecipient) {
1251
+ const atomic = usdToAtomic(opts.amountUsd, { decimals: 6 });
1252
+ rails2.push(["solana/charge", {
1253
+ amount: atomic.toString(),
1254
+ currency: opts.solanaTokenMint ?? USDC.solana.mainnet.mint,
1255
+ decimals: 6,
1256
+ recipient: opts.solanaRecipient,
1257
+ network: opts.solanaNetwork ?? "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"
1258
+ }]);
1259
+ }
1260
+ if (opts.includeStripe !== false) {
1261
+ const amountUsdNumeric = Number(opts.amountUsd);
1262
+ if (Number.isFinite(amountUsdNumeric) && amountUsdNumeric < STRIPE_MIN_CHARGE_USD) {
1263
+ if (!warnedStripeBelowMinimum) {
1264
+ warnedStripeBelowMinimum = true;
1265
+ console.warn(
1266
+ `[buildMppxComposeRails] Dropping stripe/charge rail: amountUsd=${opts.amountUsd} is below Stripe's $${STRIPE_MIN_CHARGE_USD.toFixed(2)} USD minimum. Stripe's fixed ~$0.30 fee makes sub-50-cent charges unprofitable (and many accounts reject PI creation with amount_too_small below this floor). Pass includeStripe: false to suppress this warning.`
1267
+ );
1268
+ }
1269
+ } else {
1270
+ rails2.push(["stripe/charge", { amount: opts.amountUsd, currency: "usd", decimals: 2 }]);
1271
+ }
1272
+ }
1273
+ return rails2;
1274
+ }
1275
+
1276
+ // src/payment/default_rails.ts
1277
+ function buildDefaultCheckoutRails(opts) {
1278
+ const out = {};
1279
+ if (opts.tempo) {
1280
+ out.tempo = { recipient: "", ...RAIL_SPEC_DEFAULTS.tempo, ...opts.tempo };
1281
+ }
1282
+ if (opts.x402Base) {
1283
+ const merged = { recipient: "", ...RAIL_SPEC_DEFAULTS.x402Base, ...opts.x402Base };
1284
+ if (merged.network === networks.base.sepolia.caip2) {
1285
+ if (opts.x402Base.chainId === void 0) merged.chainId = networks.base.sepolia.chainId;
1286
+ if (opts.x402Base.token === void 0) merged.token = USDC.base.sepolia.address;
1287
+ } else if (merged.network === networks.base.mainnet.caip2) {
1288
+ if (opts.x402Base.chainId === void 0) merged.chainId = networks.base.mainnet.chainId;
1289
+ if (opts.x402Base.token === void 0) merged.token = USDC.base.mainnet.address;
1290
+ }
1291
+ out.x402_base = merged;
1292
+ }
1293
+ if (opts.solanaMpp) {
1294
+ const merged = { recipient: "", ...RAIL_SPEC_DEFAULTS.solanaMpp, ...opts.solanaMpp };
1295
+ const isDevnet = merged.network === "devnet" || merged.network === networks.solana.devnet.caip2;
1296
+ if (isDevnet && opts.solanaMpp.token === void 0) {
1297
+ merged.token = USDC.solana.devnet.mint;
1298
+ }
1299
+ out.solana_mpp = merged;
1300
+ }
1301
+ if (opts.stripe) {
1302
+ out.stripe = { ...RAIL_SPEC_DEFAULTS.stripe, ...opts.stripe };
1303
+ }
1304
+ return out;
1305
+ }
1306
+
1307
+ // src/payment/payment_header.ts
1308
+ function toTitleCase(name) {
1309
+ return name.replace(/(^|-)([a-z])/g, (_m, sep, c) => sep + c.toUpperCase());
1310
+ }
1311
+ function readHeader(headers, name) {
1312
+ if (typeof headers.get === "function") {
1313
+ return headers.get(name);
1314
+ }
1315
+ const rec = headers;
1316
+ const v = rec[name] ?? rec[name.toLowerCase()] ?? rec[toTitleCase(name)];
1317
+ if (typeof v === "string") return v;
1318
+ if (Array.isArray(v) && typeof v[0] === "string") return v[0];
1319
+ return null;
1320
+ }
1321
+ function asHeaders(input) {
1322
+ return typeof input.headers === "object" && input instanceof Request ? input.headers : input;
1323
+ }
1324
+ function hasPaymentHeader(input) {
1325
+ const headers = asHeaders(input);
1326
+ return Boolean(
1327
+ readHeader(headers, "payment-signature") || readHeader(headers, "x-payment") || readHeader(headers, "authorization")?.startsWith("Payment ")
1328
+ );
1329
+ }
1330
+ function hasX402Header(input) {
1331
+ const headers = asHeaders(input);
1332
+ return Boolean(readHeader(headers, "payment-signature") || readHeader(headers, "x-payment"));
1333
+ }
1334
+ function hasMppxHeader(input) {
1335
+ const headers = asHeaders(input);
1336
+ return Boolean(readHeader(headers, "authorization")?.startsWith("Payment "));
1337
+ }
1338
+
1339
+ // src/payment/network_kind.ts
1340
+ function readNetwork(input) {
1341
+ if (typeof input === "string") return input;
1342
+ if (input && typeof input === "object") {
1343
+ const network = input.network;
1344
+ return typeof network === "string" ? network : "";
1345
+ }
1346
+ return "";
1347
+ }
1348
+ function isEvmNetwork(input) {
1349
+ return readNetwork(input).startsWith("eip155:");
1350
+ }
1351
+ function isSolanaNetwork(input) {
1352
+ return readNetwork(input).startsWith("solana:");
1353
+ }
1171
1354
  export {
1172
1355
  RAIL_SPEC_DEFAULTS,
1173
1356
  SETTLEMENT_OVERRIDES_HEADER,
1174
1357
  USDC,
1175
1358
  X402_SUPPORTED_BASE_NETWORKS,
1176
1359
  aliasAmountFields,
1360
+ asHeaders,
1361
+ buildDefaultCheckoutRails,
1177
1362
  buildIdempotencyKey,
1363
+ buildMppxComposeRails,
1178
1364
  buildPaymentDirective,
1179
1365
  buildPaymentHeaders,
1180
1366
  buildPaymentRequestBlob,
@@ -1190,6 +1376,11 @@ export {
1190
1376
  extractPaymentSignerFromAuth,
1191
1377
  extractSignerForPrecheck,
1192
1378
  formatUsdCents,
1379
+ hasMppxHeader,
1380
+ hasPaymentHeader,
1381
+ hasX402Header,
1382
+ isEvmNetwork,
1383
+ isSolanaNetwork,
1193
1384
  lazyMppxServer,
1194
1385
  lazyX402Server,
1195
1386
  loadSolanaFeePayer,
@@ -1201,9 +1392,11 @@ export {
1201
1392
  paymentRequiredHeader,
1202
1393
  processX402Settle,
1203
1394
  rails,
1395
+ readHeader,
1204
1396
  readX402PaymentHeader,
1205
1397
  registerX402SchemesV1V2,
1206
1398
  resolveRecipient,
1399
+ runWithMppxFailureCapture,
1207
1400
  settlementOverrideHeader,
1208
1401
  usdToAtomic,
1209
1402
  validateX402NetworkConfig,