@agent-score/commerce 2.0.0 → 2.0.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.
@@ -552,6 +552,47 @@ type SolanaChargeMethod = {
552
552
  * Fine for agent-driven flows; manual signing flows still have plenty of margin.
553
553
  */
554
554
  declare function wrapSolanaChargeWithFinalizedBlockhash(baseMethod: SolanaChargeMethod, rpcUrl: string): SolanaChargeMethod;
555
+ /**
556
+ * Result shape of `composeMppxRequest`. mppx's `mppx.compose(...)(request)`
557
+ * resolves to one of two variants — type-narrowed here so consumers can
558
+ * `if (result.status === 200) { result.withReceipt(...) }` without an
559
+ * `as any` cast.
560
+ */
561
+ type MppxComposeResult = {
562
+ status: 200;
563
+ /** Wraps a Response with the `Payment-Receipt` header attached. */
564
+ withReceipt: (response: Response) => Response;
565
+ [k: string]: unknown;
566
+ } | {
567
+ status: 402;
568
+ /** The 402 challenge Response mppx emitted (carries WWW-Authenticate). */
569
+ challenge: Response;
570
+ [k: string]: unknown;
571
+ };
572
+ /**
573
+ * Run `mppx.compose(...intents)(request)` with a typed return. Replaces the
574
+ * `(mppx as any).compose(...intents)(request)` cast every hand-rolled
575
+ * `composeMppx` hook ends up writing.
576
+ *
577
+ * @example
578
+ * ```ts
579
+ * const result = await composeMppxRequest(mppx, [
580
+ * ['tempo/charge', { amount, currency, decimals, recipient }],
581
+ * ['stripe/charge', { amount, currency: 'usd', decimals: 2 }],
582
+ * ], ctx.request.raw);
583
+ * if (result.status === 402) return { status: 402, headers: mppxChallengeHeaders(result) };
584
+ * return { status: 200, raw: result };
585
+ * ```
586
+ */
587
+ declare function composeMppxRequest(mppx: unknown, intents: readonly unknown[], request: Request): Promise<MppxComposeResult>;
588
+ /**
589
+ * Extract the 402 challenge response's headers as a plain `Record<string, string>`,
590
+ * the shape `MppxComposeOutcome.headers` accepts. Wraps the one-liner every
591
+ * hand-rolled compose hook writes.
592
+ */
593
+ declare function mppxChallengeHeaders(result: {
594
+ challenge: Response;
595
+ }): Record<string, string>;
555
596
 
556
597
  /**
557
598
  * Detect which payment-protocol family the inbound request carries.
@@ -838,4 +879,4 @@ declare function lazyMppxServer(opts: {
838
879
  secretKey: string;
839
880
  }): () => Promise<unknown>;
840
881
 
841
- export { type ClassifiedX402Error, type MppxRailSpec, type NetworkFamily, type PaymentHeadersRail, type PaymentHeadersResult, type ProcessX402SettleResult, type RailDefinition, type RailName, SETTLEMENT_OVERRIDES_HEADER, type SettlementHandlers, type SettlementOverrides, type SettlementPayloadLike, SignerNetwork, SolanaMppRailSpec, StripeRailSpec, TempoRailSpec, TempoSessionRailSpec, USDC, type VerifyX402RequestResult, X402BaseRailSpec, X402Server, type X402ServerLike, X402_SUPPORTED_BASE_NETWORKS, type ZeroSettleRail, type ZeroSettleResult, buildIdempotencyKey, buildPaymentDirective, buildPaymentHeaders, buildPaymentRequestBlob, classifyOrchestrationError, classifyX402SettleResult, createMppxServer, detectRailFromHeaders, dispatchSettlementByNetwork, lazyMppxServer, lazyX402Server, lookupRail, networkFamily, networks, paymentDirective, processX402Settle, rails, registerX402SchemesV1V2, settlementOverrideHeader, validateX402NetworkConfig, verifyX402Request, wrapSolanaChargeWithFinalizedBlockhash, zeroAmountCarveOut };
882
+ export { type ClassifiedX402Error, type MppxComposeResult, type MppxRailSpec, type NetworkFamily, type PaymentHeadersRail, type PaymentHeadersResult, type ProcessX402SettleResult, type RailDefinition, type RailName, SETTLEMENT_OVERRIDES_HEADER, type SettlementHandlers, type SettlementOverrides, type SettlementPayloadLike, SignerNetwork, SolanaMppRailSpec, StripeRailSpec, TempoRailSpec, TempoSessionRailSpec, USDC, type VerifyX402RequestResult, X402BaseRailSpec, X402Server, type X402ServerLike, X402_SUPPORTED_BASE_NETWORKS, type ZeroSettleRail, type ZeroSettleResult, buildIdempotencyKey, buildPaymentDirective, buildPaymentHeaders, buildPaymentRequestBlob, classifyOrchestrationError, classifyX402SettleResult, composeMppxRequest, createMppxServer, detectRailFromHeaders, dispatchSettlementByNetwork, lazyMppxServer, lazyX402Server, lookupRail, mppxChallengeHeaders, networkFamily, networks, paymentDirective, processX402Settle, rails, registerX402SchemesV1V2, settlementOverrideHeader, validateX402NetworkConfig, verifyX402Request, wrapSolanaChargeWithFinalizedBlockhash, zeroAmountCarveOut };
@@ -552,6 +552,47 @@ type SolanaChargeMethod = {
552
552
  * Fine for agent-driven flows; manual signing flows still have plenty of margin.
553
553
  */
554
554
  declare function wrapSolanaChargeWithFinalizedBlockhash(baseMethod: SolanaChargeMethod, rpcUrl: string): SolanaChargeMethod;
555
+ /**
556
+ * Result shape of `composeMppxRequest`. mppx's `mppx.compose(...)(request)`
557
+ * resolves to one of two variants — type-narrowed here so consumers can
558
+ * `if (result.status === 200) { result.withReceipt(...) }` without an
559
+ * `as any` cast.
560
+ */
561
+ type MppxComposeResult = {
562
+ status: 200;
563
+ /** Wraps a Response with the `Payment-Receipt` header attached. */
564
+ withReceipt: (response: Response) => Response;
565
+ [k: string]: unknown;
566
+ } | {
567
+ status: 402;
568
+ /** The 402 challenge Response mppx emitted (carries WWW-Authenticate). */
569
+ challenge: Response;
570
+ [k: string]: unknown;
571
+ };
572
+ /**
573
+ * Run `mppx.compose(...intents)(request)` with a typed return. Replaces the
574
+ * `(mppx as any).compose(...intents)(request)` cast every hand-rolled
575
+ * `composeMppx` hook ends up writing.
576
+ *
577
+ * @example
578
+ * ```ts
579
+ * const result = await composeMppxRequest(mppx, [
580
+ * ['tempo/charge', { amount, currency, decimals, recipient }],
581
+ * ['stripe/charge', { amount, currency: 'usd', decimals: 2 }],
582
+ * ], ctx.request.raw);
583
+ * if (result.status === 402) return { status: 402, headers: mppxChallengeHeaders(result) };
584
+ * return { status: 200, raw: result };
585
+ * ```
586
+ */
587
+ declare function composeMppxRequest(mppx: unknown, intents: readonly unknown[], request: Request): Promise<MppxComposeResult>;
588
+ /**
589
+ * Extract the 402 challenge response's headers as a plain `Record<string, string>`,
590
+ * the shape `MppxComposeOutcome.headers` accepts. Wraps the one-liner every
591
+ * hand-rolled compose hook writes.
592
+ */
593
+ declare function mppxChallengeHeaders(result: {
594
+ challenge: Response;
595
+ }): Record<string, string>;
555
596
 
556
597
  /**
557
598
  * Detect which payment-protocol family the inbound request carries.
@@ -838,4 +879,4 @@ declare function lazyMppxServer(opts: {
838
879
  secretKey: string;
839
880
  }): () => Promise<unknown>;
840
881
 
841
- export { type ClassifiedX402Error, type MppxRailSpec, type NetworkFamily, type PaymentHeadersRail, type PaymentHeadersResult, type ProcessX402SettleResult, type RailDefinition, type RailName, SETTLEMENT_OVERRIDES_HEADER, type SettlementHandlers, type SettlementOverrides, type SettlementPayloadLike, SignerNetwork, SolanaMppRailSpec, StripeRailSpec, TempoRailSpec, TempoSessionRailSpec, USDC, type VerifyX402RequestResult, X402BaseRailSpec, X402Server, type X402ServerLike, X402_SUPPORTED_BASE_NETWORKS, type ZeroSettleRail, type ZeroSettleResult, buildIdempotencyKey, buildPaymentDirective, buildPaymentHeaders, buildPaymentRequestBlob, classifyOrchestrationError, classifyX402SettleResult, createMppxServer, detectRailFromHeaders, dispatchSettlementByNetwork, lazyMppxServer, lazyX402Server, lookupRail, networkFamily, networks, paymentDirective, processX402Settle, rails, registerX402SchemesV1V2, settlementOverrideHeader, validateX402NetworkConfig, verifyX402Request, wrapSolanaChargeWithFinalizedBlockhash, zeroAmountCarveOut };
882
+ export { type ClassifiedX402Error, type MppxComposeResult, type MppxRailSpec, type NetworkFamily, type PaymentHeadersRail, type PaymentHeadersResult, type ProcessX402SettleResult, type RailDefinition, type RailName, SETTLEMENT_OVERRIDES_HEADER, type SettlementHandlers, type SettlementOverrides, type SettlementPayloadLike, SignerNetwork, SolanaMppRailSpec, StripeRailSpec, TempoRailSpec, TempoSessionRailSpec, USDC, type VerifyX402RequestResult, X402BaseRailSpec, X402Server, type X402ServerLike, X402_SUPPORTED_BASE_NETWORKS, type ZeroSettleRail, type ZeroSettleResult, buildIdempotencyKey, buildPaymentDirective, buildPaymentHeaders, buildPaymentRequestBlob, classifyOrchestrationError, classifyX402SettleResult, composeMppxRequest, createMppxServer, detectRailFromHeaders, dispatchSettlementByNetwork, lazyMppxServer, lazyX402Server, lookupRail, mppxChallengeHeaders, networkFamily, networks, paymentDirective, processX402Settle, rails, registerX402SchemesV1V2, settlementOverrideHeader, validateX402NetworkConfig, verifyX402Request, wrapSolanaChargeWithFinalizedBlockhash, zeroAmountCarveOut };
@@ -32,6 +32,7 @@ __export(payment_exports, {
32
32
  buildX402AcceptsFor402: () => buildX402AcceptsFor402,
33
33
  classifyOrchestrationError: () => classifyOrchestrationError,
34
34
  classifyX402SettleResult: () => classifyX402SettleResult,
35
+ composeMppxRequest: () => composeMppxRequest,
35
36
  createMppxServer: () => createMppxServer,
36
37
  createX402Server: () => createX402Server,
37
38
  detectRailFromHeaders: () => detectRailFromHeaders,
@@ -44,6 +45,7 @@ __export(payment_exports, {
44
45
  lazyX402Server: () => lazyX402Server,
45
46
  loadSolanaFeePayer: () => loadSolanaFeePayer,
46
47
  lookupRail: () => lookupRail,
48
+ mppxChallengeHeaders: () => mppxChallengeHeaders,
47
49
  networkFamily: () => networkFamily,
48
50
  networks: () => networks,
49
51
  paymentDirective: () => paymentDirective,
@@ -807,6 +809,21 @@ function wrapSolanaChargeWithFinalizedBlockhash(baseMethod, rpcUrl) {
807
809
  }
808
810
  };
809
811
  }
812
+ async function composeMppxRequest(mppx, intents, request) {
813
+ if (!mppx || typeof mppx !== "object" || !("compose" in mppx)) {
814
+ throw new Error("composeMppxRequest: argument is not an mppx server instance");
815
+ }
816
+ const compose = mppx.compose;
817
+ if (typeof compose !== "function") {
818
+ throw new Error("composeMppxRequest: mppx.compose is not a function");
819
+ }
820
+ const typedCompose = compose;
821
+ const handler = typedCompose.apply(mppx, [...intents]);
822
+ return handler(request);
823
+ }
824
+ function mppxChallengeHeaders(result) {
825
+ return Object.fromEntries(result.challenge.headers);
826
+ }
810
827
 
811
828
  // src/payment/dispatch.ts
812
829
  function detectRailFromHeaders(headers) {
@@ -1232,6 +1249,7 @@ async function loadSolanaFeePayer(opts) {
1232
1249
  buildX402AcceptsFor402,
1233
1250
  classifyOrchestrationError,
1234
1251
  classifyX402SettleResult,
1252
+ composeMppxRequest,
1235
1253
  createMppxServer,
1236
1254
  createX402Server,
1237
1255
  detectRailFromHeaders,
@@ -1244,6 +1262,7 @@ async function loadSolanaFeePayer(opts) {
1244
1262
  lazyX402Server,
1245
1263
  loadSolanaFeePayer,
1246
1264
  lookupRail,
1265
+ mppxChallengeHeaders,
1247
1266
  networkFamily,
1248
1267
  networks,
1249
1268
  paymentDirective,