@relyon/kit 0.0.0-stage → 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.
Files changed (225) hide show
  1. package/AGENTS.md +75 -0
  2. package/CHANGELOG.md +1053 -0
  3. package/LICENSE +21 -0
  4. package/QUICKSTART.md +239 -0
  5. package/README.md +117 -2
  6. package/dist/adapters/express.d.ts +39 -0
  7. package/dist/adapters/express.js +106 -0
  8. package/dist/adapters/hono.d.ts +16 -0
  9. package/dist/adapters/hono.js +15 -0
  10. package/dist/adapters/next.d.ts +29 -0
  11. package/dist/adapters/next.js +38 -0
  12. package/dist/agent-card.d.ts +14 -0
  13. package/dist/agent-card.js +58 -0
  14. package/dist/agent-docs.d.ts +36 -0
  15. package/dist/agent-docs.js +78 -0
  16. package/dist/agent-hooks.d.ts +66 -0
  17. package/dist/agent-hooks.js +280 -0
  18. package/dist/agents.d.ts +24 -0
  19. package/dist/agents.js +120 -0
  20. package/dist/api-key.d.ts +6 -0
  21. package/dist/api-key.js +33 -0
  22. package/dist/cache.d.ts +46 -0
  23. package/dist/cache.js +86 -0
  24. package/dist/cart-tokens.d.ts +31 -0
  25. package/dist/cart-tokens.js +58 -0
  26. package/dist/cart-transfer.d.ts +95 -0
  27. package/dist/cart-transfer.js +387 -0
  28. package/dist/catalog-enrichment.d.ts +6 -0
  29. package/dist/catalog-enrichment.js +71 -0
  30. package/dist/catalog-reads.d.ts +126 -0
  31. package/dist/catalog-reads.js +104 -0
  32. package/dist/catalog.d.ts +78 -0
  33. package/dist/catalog.js +288 -0
  34. package/dist/charge-claims.d.ts +11 -0
  35. package/dist/charge-claims.js +47 -0
  36. package/dist/check.d.ts +44 -0
  37. package/dist/check.js +152 -0
  38. package/dist/checklist-manifest.d.ts +72 -0
  39. package/dist/checklist-manifest.js +341 -0
  40. package/dist/checkout.d.ts +222 -0
  41. package/dist/checkout.js +353 -0
  42. package/dist/cli.d.ts +2 -0
  43. package/dist/cli.js +727 -0
  44. package/dist/confirmation.d.ts +78 -0
  45. package/dist/confirmation.js +124 -0
  46. package/dist/context.d.ts +81 -0
  47. package/dist/context.js +10 -0
  48. package/dist/discovery.d.ts +151 -0
  49. package/dist/discovery.js +346 -0
  50. package/dist/doctor-cart-transfer.d.ts +33 -0
  51. package/dist/doctor-cart-transfer.js +186 -0
  52. package/dist/doctor-catalog.d.ts +8 -0
  53. package/dist/doctor-catalog.js +47 -0
  54. package/dist/doctor-evidence.d.ts +22 -0
  55. package/dist/doctor-evidence.js +57 -0
  56. package/dist/doctor-merchant.d.ts +48 -0
  57. package/dist/doctor-merchant.js +74 -0
  58. package/dist/doctor-source.d.ts +5 -0
  59. package/dist/doctor-source.js +142 -0
  60. package/dist/doctor-variants.d.ts +70 -0
  61. package/dist/doctor-variants.js +102 -0
  62. package/dist/doctor.d.ts +215 -0
  63. package/dist/doctor.js +1689 -0
  64. package/dist/events.d.ts +95 -0
  65. package/dist/events.js +200 -0
  66. package/dist/feed-mapping.d.ts +114 -0
  67. package/dist/feed-mapping.js +152 -0
  68. package/dist/feed-values.d.ts +13 -0
  69. package/dist/feed-values.js +40 -0
  70. package/dist/feeds.d.ts +104 -0
  71. package/dist/feeds.js +366 -0
  72. package/dist/identify.d.ts +102 -0
  73. package/dist/identify.js +124 -0
  74. package/dist/identity.d.ts +37 -0
  75. package/dist/identity.js +54 -0
  76. package/dist/index.d.ts +42 -0
  77. package/dist/index.js +41 -0
  78. package/dist/init.d.ts +157 -0
  79. package/dist/init.js +912 -0
  80. package/dist/kit-config.d.ts +19 -0
  81. package/dist/kit-config.js +43 -0
  82. package/dist/kit-environment.d.ts +20 -0
  83. package/dist/kit-environment.js +32 -0
  84. package/dist/kit-updates.d.ts +47 -0
  85. package/dist/kit-updates.js +77 -0
  86. package/dist/kit-version-project.d.ts +18 -0
  87. package/dist/kit-version-project.js +214 -0
  88. package/dist/kit-version.d.ts +37 -0
  89. package/dist/kit-version.js +162 -0
  90. package/dist/kit.d.ts +516 -0
  91. package/dist/kit.js +1309 -0
  92. package/dist/layers/pages.d.ts +3 -0
  93. package/dist/layers/pages.js +5 -0
  94. package/dist/layers/payments.d.ts +7 -0
  95. package/dist/layers/payments.js +9 -0
  96. package/dist/layers/products.d.ts +10 -0
  97. package/dist/layers/products.js +13 -0
  98. package/dist/live.d.ts +13 -0
  99. package/dist/live.js +52 -0
  100. package/dist/mcp.d.ts +270 -0
  101. package/dist/mcp.js +312 -0
  102. package/dist/next-steps.d.ts +101 -0
  103. package/dist/next-steps.js +257 -0
  104. package/dist/order-pending.d.ts +57 -0
  105. package/dist/order-pending.js +127 -0
  106. package/dist/orders.d.ts +12 -0
  107. package/dist/orders.js +21 -0
  108. package/dist/payments-init.d.ts +19 -0
  109. package/dist/payments-init.js +70 -0
  110. package/dist/payments-routes.d.ts +54 -0
  111. package/dist/payments-routes.js +123 -0
  112. package/dist/payments.d.ts +165 -0
  113. package/dist/payments.js +192 -0
  114. package/dist/pnpm-workspace.d.ts +58 -0
  115. package/dist/pnpm-workspace.js +250 -0
  116. package/dist/policies.d.ts +111 -0
  117. package/dist/policies.js +317 -0
  118. package/dist/preview-hosts.d.ts +64 -0
  119. package/dist/preview-hosts.js +175 -0
  120. package/dist/private-config.d.ts +169 -0
  121. package/dist/private-config.js +820 -0
  122. package/dist/progress.d.ts +54 -0
  123. package/dist/progress.js +153 -0
  124. package/dist/protocol-inputs.d.ts +22 -0
  125. package/dist/protocol-inputs.js +151 -0
  126. package/dist/public-origin.d.ts +50 -0
  127. package/dist/public-origin.js +180 -0
  128. package/dist/refusals.d.ts +86 -0
  129. package/dist/refusals.js +157 -0
  130. package/dist/relay.d.ts +107 -0
  131. package/dist/relay.js +655 -0
  132. package/dist/release-notes.d.ts +2 -0
  133. package/dist/release-notes.js +290 -0
  134. package/dist/relyon-cart-tokens.d.ts +101 -0
  135. package/dist/relyon-cart-tokens.js +521 -0
  136. package/dist/relyon.d.ts +141 -0
  137. package/dist/relyon.js +238 -0
  138. package/dist/robots.d.ts +27 -0
  139. package/dist/robots.js +127 -0
  140. package/dist/rules.d.ts +144 -0
  141. package/dist/rules.js +347 -0
  142. package/dist/sdk/generated/public-api.d.ts +4816 -0
  143. package/dist/sdk/generated/public-api.js +1 -0
  144. package/dist/sdk/index.d.ts +24 -0
  145. package/dist/sdk/index.js +34 -0
  146. package/dist/sdk/signatures.d.ts +17 -0
  147. package/dist/sdk/signatures.js +33 -0
  148. package/dist/setup.d.ts +37 -0
  149. package/dist/setup.js +94 -0
  150. package/dist/signatures.d.ts +112 -0
  151. package/dist/signatures.js +443 -0
  152. package/dist/state.d.ts +185 -0
  153. package/dist/state.js +96 -0
  154. package/dist/store-payments.d.ts +48 -0
  155. package/dist/store-payments.js +143 -0
  156. package/dist/structured-fields.d.ts +26 -0
  157. package/dist/structured-fields.js +200 -0
  158. package/dist/templates.d.ts +19 -0
  159. package/dist/templates.js +193 -0
  160. package/dist/test-checkout-command.d.ts +32 -0
  161. package/dist/test-checkout-command.js +161 -0
  162. package/dist/test-checkout.d.ts +88 -0
  163. package/dist/test-checkout.js +292 -0
  164. package/dist/types.d.ts +103 -0
  165. package/dist/types.js +18 -0
  166. package/dist/ucp-versions.d.ts +57 -0
  167. package/dist/ucp-versions.js +129 -0
  168. package/dist/util.d.ts +23 -0
  169. package/dist/util.js +68 -0
  170. package/dist/variants.d.ts +67 -0
  171. package/dist/variants.js +146 -0
  172. package/dist/version.d.ts +9 -0
  173. package/dist/version.js +10 -0
  174. package/dist/waiting.d.ts +43 -0
  175. package/dist/waiting.js +57 -0
  176. package/dist/webhook-command.d.ts +48 -0
  177. package/dist/webhook-command.js +367 -0
  178. package/dist/webhook-registration.d.ts +47 -0
  179. package/dist/webhook-registration.js +69 -0
  180. package/dist/webmcp-schema.d.ts +14 -0
  181. package/dist/webmcp-schema.js +140 -0
  182. package/dist/webmcp-script.d.ts +3 -0
  183. package/dist/webmcp-script.js +176 -0
  184. package/dist/webmcp-store.d.ts +102 -0
  185. package/dist/webmcp-store.js +116 -0
  186. package/dist/webmcp.d.ts +242 -0
  187. package/dist/webmcp.js +866 -0
  188. package/dist/wire-names.d.ts +85 -0
  189. package/dist/wire-names.js +109 -0
  190. package/package.json +118 -4
  191. package/release-notes.json +290 -0
  192. package/scripts/install-notice.cjs +7 -0
  193. package/skills/relyon-agents/SKILL.md +327 -0
  194. package/skills/relyon-catalog/SKILL.md +499 -0
  195. package/skills/relyon-catalog/references/example-store.md +218 -0
  196. package/skills/relyon-checkout/SKILL.md +603 -0
  197. package/skills/relyon-checkout/references/checkout-flow.md +257 -0
  198. package/skills/relyon-checkout/references/checkout-page.md +315 -0
  199. package/skills/relyon-go-live/SKILL.md +344 -0
  200. package/skills/relyon-orders/SKILL.md +191 -0
  201. package/skills/relyon-orders/references/webhook-receiver.md +113 -0
  202. package/skills/relyon-payments/SKILL.md +117 -0
  203. package/skills/relyon-setup/SKILL.md +1061 -0
  204. package/skills/relyon-setup/references/QUICKSTART.md +239 -0
  205. package/skills/relyon-setup/references/durable-state.md +209 -0
  206. package/skills/relyon-setup/references/express-server.md +388 -0
  207. package/skills/relyon-setup/references/next-app-router.md +251 -0
  208. package/templates/common/STORE-README.md +67 -0
  209. package/templates/common/gitignore +8 -0
  210. package/templates/common/scripts/check-pricing.ts +22 -0
  211. package/templates/common/scripts/serve.mjs +13 -0
  212. package/templates/common/server/catalog.ts +79 -0
  213. package/templates/common/server/checkout-flow.d.ts +34 -0
  214. package/templates/common/server/pages.ts +184 -0
  215. package/templates/common/server/storage.ts +114 -0
  216. package/templates/common/server/store.ts +67 -0
  217. package/templates/express/server/express.ts +20 -0
  218. package/templates/express/tsconfig.build.json +6 -0
  219. package/templates/express/tsconfig.json +9 -0
  220. package/templates/next/app/[[...path]]/route.ts +24 -0
  221. package/templates/next/next.config.mjs +19 -0
  222. package/templates/next/tsconfig.json +12 -0
  223. package/templates/payments/express.ts +39 -0
  224. package/templates/payments/next.ts +38 -0
  225. package/templates/payments/node.ts +39 -0
@@ -0,0 +1,86 @@
1
+ export type RefusalProtocol = "ucp" | "acp" | "mcp" | "plain";
2
+ /** Why the kit itself answered instead of relaying. */
3
+ export type RefusalCode = "agent_refused" | "channel_paused" | "service_unavailable" | "protocol_not_available" | "not_found" | "invalid_request" | "internal_error";
4
+ /** JSON-RPC error codes, following the Commerce MCP conventions (never the SDK's own -32002/-32021/-32022/-32042). */
5
+ export declare const MCP_ERROR_CODES: Record<string, number>;
6
+ /**
7
+ * The note on a refusal for a step that names an existing checkout (get,
8
+ * update, complete or cancel with a checkout ID): the kit can't tell from the
9
+ * step alone whether that checkout was paid, so it never says nothing was
10
+ * charged. Only `create` keeps that wording.
11
+ */
12
+ export declare const EXISTING_CHECKOUT_NOTE = "Read this checkout for its status before trying again; don't start a new checkout.";
13
+ /** MCP's version of `EXISTING_CHECKOUT_NOTE`. */
14
+ export declare const EXISTING_CHECKOUT_NOTE_MCP = "Call get_checkout for its status before trying again; don't start a new checkout.";
15
+ /**
16
+ * The kit's own message for a refusal: charge-neutral (never "nothing was
17
+ * charged") when the step names an existing checkout, otherwise the default.
18
+ */
19
+ export declare function refusalMessage(code: string, existingCheckout: boolean, protocol?: RefusalProtocol): string;
20
+ /**
21
+ * An explicit refusal message (a store's `StoreRefusal`, a rule's text) for
22
+ * a step that names an existing checkout: every sentence claiming nothing was
23
+ * charged (see `NOTHING_CHARGED_CLAIM`) is removed and the charge-neutral note
24
+ * appended. Other messages are kept. If a claim can't be cut out cleanly the
25
+ * kit's own charge-neutral wording replaces the whole message.
26
+ */
27
+ export declare function existingCheckoutMessage(message: string, protocol?: RefusalProtocol): string;
28
+ export interface RefusalOptions {
29
+ message?: string;
30
+ status?: number;
31
+ /** For MCP: the JSON-RPC request ID being answered. */
32
+ id?: unknown;
33
+ /** Extra `data` for MCP errors. */
34
+ data?: Record<string, unknown>;
35
+ /**
36
+ * The step names an existing checkout (a checkout ID): the default message
37
+ * is charge-neutral (see `EXISTING_CHECKOUT_NOTE`).
38
+ */
39
+ existingCheckout?: boolean;
40
+ }
41
+ /**
42
+ * A refusal in the protocol's own error format:
43
+ * UCP `{code, content}` with the HTTP status, ACP `{type, code, message}`,
44
+ * and MCP an in-band JSON-RPC error (HTTP 200) with `data.error`.
45
+ */
46
+ export declare function refusal(protocol: RefusalProtocol, code: string, options?: RefusalOptions): Response;
47
+ /** The stable code of the kit's unknown-payment-outcome answer. */
48
+ export declare const PAYMENT_OUTCOME_UNKNOWN = "payment_outcome_unknown";
49
+ /** HTTP status of the unknown-outcome answer on UCP and ACP (MCP answers in-band with HTTP 200). */
50
+ export declare const PAYMENT_OUTCOME_UNKNOWN_STATUS = 503;
51
+ /** Retry-After (seconds) on the unknown-outcome answer; MCP carries it as `data.retry_after`. */
52
+ export declare const PAYMENT_OUTCOME_UNKNOWN_RETRY_AFTER = 30;
53
+ /** Response header carrying `unknown` or `pending` on the UCP and ACP unknown-outcome answers. */
54
+ export declare const PAYMENT_STATUS_HEADER = "payment-status";
55
+ export interface PaymentOutcomeUnknownOptions {
56
+ /** `pending` when Relyon reported the payment as pending or processing; otherwise `unknown`. */
57
+ paymentStatus: "unknown" | "pending";
58
+ /** UCP/ACP: the store's own URL where the agent reads this checkout. */
59
+ statusUrl?: string;
60
+ /** MCP: the checkout to read with `get_checkout`. */
61
+ checkoutId?: string;
62
+ /** MCP: the JSON-RPC request ID being answered. */
63
+ id?: unknown;
64
+ }
65
+ /**
66
+ * The kit's answer when an agent's `complete` may have charged and the kit
67
+ * can't tell: UCP `{code, content}` (plus `payment_status`, `status_url`) and
68
+ * ACP's schema-exact Error `{type, code, message}`, both HTTP 503 with
69
+ * Retry-After, a `Payment-Status` header and a `Link: <url>; rel="status"`
70
+ * to the store's own checkout read; MCP an in-band JSON-RPC error (HTTP 200)
71
+ * whose `data` names `payment_status` and the `get_checkout` tool. The code
72
+ * is `payment_outcome_unknown` everywhere: UCP's error_code and ACP's Error
73
+ * code are free strings. Never says that nothing was charged.
74
+ */
75
+ export declare function paymentOutcomeUnknown(protocol: "ucp" | "acp" | "mcp", options: PaymentOutcomeUnknownOptions): Response;
76
+ export declare function mcpErrorBody(id: unknown, code: string, message: string, data?: Record<string, unknown>): {
77
+ jsonrpc: "2.0";
78
+ id: string | number | null;
79
+ error: {
80
+ code: number;
81
+ message: string;
82
+ data: {
83
+ error: string;
84
+ };
85
+ };
86
+ };
@@ -0,0 +1,157 @@
1
+ import { claimsNothingCharged, withoutNothingChargedClaims } from "./charge-claims.js";
2
+ import { jsonResponse } from "./util.js";
3
+ const STATUS = {
4
+ agent_refused: 403,
5
+ channel_paused: 403,
6
+ service_unavailable: 503,
7
+ protocol_not_available: 501,
8
+ not_found: 404,
9
+ invalid_request: 400,
10
+ internal_error: 500,
11
+ };
12
+ /** JSON-RPC error codes, following the Commerce MCP conventions (never the SDK's own -32002/-32021/-32022/-32042). */
13
+ export const MCP_ERROR_CODES = {
14
+ invalid_request: -32602,
15
+ not_found: -32004,
16
+ agent_refused: -32010,
17
+ channel_paused: -32016,
18
+ cart_changed: -32035,
19
+ rate_limited: -32029,
20
+ service_unavailable: -32041,
21
+ payment_outcome_unknown: -32044,
22
+ order_pending: -32045,
23
+ protocol_not_available: -32050,
24
+ processor_not_configured: -32051,
25
+ sandbox_test_only: -32052,
26
+ live_not_available: -32053,
27
+ internal_error: -32603,
28
+ };
29
+ const MESSAGES = {
30
+ agent_refused: "This store does not accept requests from this agent.",
31
+ channel_paused: "Agent checkout is paused for this store. Nothing was charged.",
32
+ service_unavailable: "Checkout is temporarily unavailable. Nothing was charged; try again shortly.",
33
+ protocol_not_available: "Checkout over this protocol is not available yet. Nothing was charged.",
34
+ not_found: "The requested resource was not found.",
35
+ invalid_request: "The request is not valid.",
36
+ internal_error: "The request could not be processed. Nothing was charged; try again.",
37
+ };
38
+ /**
39
+ * The note on a refusal for a step that names an existing checkout (get,
40
+ * update, complete or cancel with a checkout ID): the kit can't tell from the
41
+ * step alone whether that checkout was paid, so it never says nothing was
42
+ * charged. Only `create` keeps that wording.
43
+ */
44
+ export const EXISTING_CHECKOUT_NOTE = "Read this checkout for its status before trying again; don't start a new checkout.";
45
+ /** MCP's version of `EXISTING_CHECKOUT_NOTE`. */
46
+ export const EXISTING_CHECKOUT_NOTE_MCP = "Call get_checkout for its status before trying again; don't start a new checkout.";
47
+ /** Charge-neutral messages for a step that names an existing checkout; the note is appended. */
48
+ const EXISTING_CHECKOUT_MESSAGES = {
49
+ channel_paused: "Agent checkout is paused for this store.",
50
+ service_unavailable: "Checkout is temporarily unavailable; try again shortly.",
51
+ protocol_not_available: "Checkout over this protocol is not available yet.",
52
+ internal_error: "This checkout step could not be answered.",
53
+ };
54
+ /**
55
+ * The kit's own message for a refusal: charge-neutral (never "nothing was
56
+ * charged") when the step names an existing checkout, otherwise the default.
57
+ */
58
+ export function refusalMessage(code, existingCheckout, protocol = "ucp") {
59
+ const known = code in STATUS ? code : undefined;
60
+ if (!existingCheckout)
61
+ return known ? MESSAGES[known] : MESSAGES.internal_error;
62
+ const base = EXISTING_CHECKOUT_MESSAGES[code] ?? (known ? MESSAGES[known] : EXISTING_CHECKOUT_MESSAGES.internal_error);
63
+ return `${base} ${protocol === "mcp" ? EXISTING_CHECKOUT_NOTE_MCP : EXISTING_CHECKOUT_NOTE}`;
64
+ }
65
+ /**
66
+ * An explicit refusal message (a store's `StoreRefusal`, a rule's text) for
67
+ * a step that names an existing checkout: every sentence claiming nothing was
68
+ * charged (see `NOTHING_CHARGED_CLAIM`) is removed and the charge-neutral note
69
+ * appended. Other messages are kept. If a claim can't be cut out cleanly the
70
+ * kit's own charge-neutral wording replaces the whole message.
71
+ */
72
+ export function existingCheckoutMessage(message, protocol = "ucp") {
73
+ if (!claimsNothingCharged(message))
74
+ return message;
75
+ const base = withoutNothingChargedClaims(message) || EXISTING_CHECKOUT_MESSAGES.internal_error;
76
+ return `${base} ${protocol === "mcp" ? EXISTING_CHECKOUT_NOTE_MCP : EXISTING_CHECKOUT_NOTE}`;
77
+ }
78
+ /**
79
+ * A refusal in the protocol's own error format:
80
+ * UCP `{code, content}` with the HTTP status, ACP `{type, code, message}`,
81
+ * and MCP an in-band JSON-RPC error (HTTP 200) with `data.error`.
82
+ */
83
+ export function refusal(protocol, code, options = {}) {
84
+ const known = code in STATUS ? code : undefined;
85
+ const status = options.status ?? (known ? STATUS[known] : 500);
86
+ const message = options.message === undefined
87
+ ? refusalMessage(code, options.existingCheckout === true, protocol)
88
+ : options.existingCheckout === true ? existingCheckoutMessage(options.message, protocol) : options.message;
89
+ const retry = status === 503 ? { "retry-after": "30" } : {};
90
+ switch (protocol) {
91
+ case "ucp":
92
+ return jsonResponse(status, { code, content: message }, retry);
93
+ case "acp": {
94
+ const type = status === 503 ? "service_unavailable" : status >= 500 ? "processing_error" : "invalid_request";
95
+ return jsonResponse(status, { type, code, message }, retry);
96
+ }
97
+ case "mcp":
98
+ return jsonResponse(200, mcpErrorBody(options.id, code, message, options.data));
99
+ case "plain":
100
+ return new Response(`${message}\n`, { status, headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "no-store", ...retry } });
101
+ }
102
+ }
103
+ /** The stable code of the kit's unknown-payment-outcome answer. */
104
+ export const PAYMENT_OUTCOME_UNKNOWN = "payment_outcome_unknown";
105
+ /** HTTP status of the unknown-outcome answer on UCP and ACP (MCP answers in-band with HTTP 200). */
106
+ export const PAYMENT_OUTCOME_UNKNOWN_STATUS = 503;
107
+ /** Retry-After (seconds) on the unknown-outcome answer; MCP carries it as `data.retry_after`. */
108
+ export const PAYMENT_OUTCOME_UNKNOWN_RETRY_AFTER = 30;
109
+ /** Response header carrying `unknown` or `pending` on the UCP and ACP unknown-outcome answers. */
110
+ export const PAYMENT_STATUS_HEADER = "payment-status";
111
+ const UNKNOWN_OUTCOME_LEAD = "The payment outcome is unknown: a charge may have gone through. Don't assume the checkout was paid or unpaid.";
112
+ const UNKNOWN_OUTCOME_TAIL = "Don't start a new checkout or change the payment until the status is known.";
113
+ /**
114
+ * The kit's answer when an agent's `complete` may have charged and the kit
115
+ * can't tell: UCP `{code, content}` (plus `payment_status`, `status_url`) and
116
+ * ACP's schema-exact Error `{type, code, message}`, both HTTP 503 with
117
+ * Retry-After, a `Payment-Status` header and a `Link: <url>; rel="status"`
118
+ * to the store's own checkout read; MCP an in-band JSON-RPC error (HTTP 200)
119
+ * whose `data` names `payment_status` and the `get_checkout` tool. The code
120
+ * is `payment_outcome_unknown` everywhere: UCP's error_code and ACP's Error
121
+ * code are free strings. Never says that nothing was charged.
122
+ */
123
+ export function paymentOutcomeUnknown(protocol, options) {
124
+ const status = PAYMENT_OUTCOME_UNKNOWN_STATUS;
125
+ const retryAfter = PAYMENT_OUTCOME_UNKNOWN_RETRY_AFTER;
126
+ if (protocol === "mcp") {
127
+ const checkoutId = options.checkoutId ?? "";
128
+ const message = `${UNKNOWN_OUTCOME_LEAD} Payment status: ${options.paymentStatus}. Call get_checkout with checkout_id "${checkoutId}" to see the payment status before doing anything else. ${UNKNOWN_OUTCOME_TAIL}`;
129
+ return jsonResponse(200, mcpErrorBody(options.id, PAYMENT_OUTCOME_UNKNOWN, message, {
130
+ payment_status: options.paymentStatus,
131
+ checkout_id: checkoutId,
132
+ check: { tool: "get_checkout", arguments: { checkout_id: checkoutId } },
133
+ retry_after: retryAfter,
134
+ }));
135
+ }
136
+ const statusUrl = options.statusUrl ?? "";
137
+ const message = `${UNKNOWN_OUTCOME_LEAD} Payment status: ${options.paymentStatus}. Read the checkout at ${statusUrl} to see the payment status,`
138
+ + ` or resend this exact request (same body and Idempotency-Key) to get the original outcome. ${UNKNOWN_OUTCOME_TAIL}`;
139
+ // Machine-readable on both protocols as headers (RFC 8631 `status` link);
140
+ // ACP's Error object allows no extra body fields.
141
+ const headers = {
142
+ "retry-after": String(retryAfter),
143
+ [PAYMENT_STATUS_HEADER]: options.paymentStatus,
144
+ link: `<${statusUrl}>; rel="status"`,
145
+ };
146
+ return protocol === "ucp"
147
+ ? jsonResponse(status, { code: PAYMENT_OUTCOME_UNKNOWN, content: message, payment_status: options.paymentStatus, status_url: statusUrl }, headers)
148
+ : jsonResponse(status, { type: "processing_error", code: PAYMENT_OUTCOME_UNKNOWN, message }, headers);
149
+ }
150
+ export function mcpErrorBody(id, code, message, data = {}) {
151
+ const validId = typeof id === "string" || (typeof id === "number" && Number.isFinite(id)) ? id : null;
152
+ return {
153
+ jsonrpc: "2.0",
154
+ id: validId,
155
+ error: { code: MCP_ERROR_CODES[code] ?? MCP_ERROR_CODES.internal_error, message, data: { error: code, ...data } },
156
+ };
157
+ }
@@ -0,0 +1,107 @@
1
+ import type { KitContext } from "./context.js";
2
+ import { type InputPatch } from "./protocol-inputs.js";
3
+ import type { CheckoutOrderPending } from "./state.js";
4
+ import type { AgentProtocol, CheckoutOperation, PaymentLike } from "./types.js";
5
+ /** One agent checkout step as the store received it. */
6
+ export interface RelayStep {
7
+ protocol: AgentProtocol;
8
+ operation: CheckoutOperation;
9
+ /** The protocol checkout ID from the path (or tool arguments); null for `create`. */
10
+ sessionId: string | null;
11
+ request: Request;
12
+ /** The raw request body as received (UTF-8); "" when none. */
13
+ rawBody: string;
14
+ /** What this step changes in the cart inputs (create/update only). */
15
+ patch: InputPatch | null;
16
+ }
17
+ /** Relyon's answer, read tolerantly: contract names first, today's server names as a fallback. */
18
+ export interface RelayedResult {
19
+ decision: "allowed" | "refused";
20
+ reason: string | null;
21
+ agentCheckoutId: string | null;
22
+ status: number;
23
+ headers: Record<string, string>;
24
+ body: string;
25
+ payment: PaymentLike | null;
26
+ /** Cart fields Relyon locked for `complete` (server extension; not in the contract). */
27
+ locked: Record<string, unknown> | null;
28
+ }
29
+ /** What the kit knows about a payment whose outcome it can't tell. */
30
+ export type UnknownPaymentStatus = "unknown" | "pending";
31
+ export type RelayOutcome = {
32
+ kind: "relayed";
33
+ response: Response;
34
+ result: RelayedResult;
35
+ orderCreated: boolean;
36
+ }
37
+ /** The kit answered itself; nothing was charged (Relyon never ran the step, or refused it before any charge). */
38
+ | {
39
+ kind: "refused";
40
+ code: string;
41
+ status?: number;
42
+ message?: string;
43
+ }
44
+ /**
45
+ * `complete` may have reached Relyon and its answer wasn't usable: a charge
46
+ * may have happened. Never answered as "nothing was charged".
47
+ */
48
+ | {
49
+ kind: "outcome_unknown";
50
+ paymentStatus: UnknownPaymentStatus;
51
+ reason: string;
52
+ }
53
+ /**
54
+ * The payment succeeded and the store's order isn't created yet (on
55
+ * `complete`, or a read of a checkout that is still order pending). The
56
+ * caller answers in its protocol's order-pending format, built from
57
+ * Relyon's answer (`result`); never `completed`, never "nothing was charged".
58
+ */
59
+ | {
60
+ kind: "order_pending";
61
+ result: RelayedResult;
62
+ sessionId: string | null;
63
+ paymentId: string;
64
+ };
65
+ /**
66
+ * The Idempotency-Key the kit sends Relyon for one step. With the agent's own
67
+ * key it is derived from that key. Without one, `complete` gets a key derived
68
+ * from the exact request (protocol, operation, checkout, URL, body), so
69
+ * resending the same `complete` reconciles the original attempt and a
70
+ * different request (a new instrument after a decline) gets its own key.
71
+ * The other operations keep a fresh key per call.
72
+ */
73
+ export declare function relayIdempotencyKey(step: Pick<RelayStep, "protocol" | "operation" | "sessionId" | "rawBody">, url: string, agentKey: string | undefined): string;
74
+ /**
75
+ * Relays one agent checkout step: rebuilds the agent's exact request on the
76
+ * store's public https URL, prices the cart with the store's `priceCart`,
77
+ * calls `handleAgentCheckout`, and returns Relyon's status, headers and body
78
+ * unchanged. A paid `complete` creates the store's order once; until the
79
+ * order exists the step is order pending (and so is a read of the checkout),
80
+ * never `completed`. When Relyon
81
+ * can't give a usable answer the step is refused: it never runs without Relyon.
82
+ * For `complete` that answer is an unknown payment outcome instead, unless
83
+ * Relyon refused it before any charge.
84
+ */
85
+ export declare function relayStep(ctx: KitContext, step: RelayStep): Promise<RelayOutcome>;
86
+ /**
87
+ * The order-pending marker of an existing checkout (the payment succeeded
88
+ * and the store's order isn't created yet), or null when there is none or
89
+ * the order exists. Read before the kit answers any refusal of its own on a
90
+ * step that names the checkout, so a charged checkout never hears "nothing
91
+ * was charged". Counts markers the state store refused to save; never
92
+ * throws (a state store failure reads as no marker unless this process holds
93
+ * one, and the caller then answers charge-neutrally).
94
+ */
95
+ export declare function orderPendingFor(ctx: KitContext, protocol: AgentProtocol, sessionId: string): Promise<CheckoutOrderPending | null>;
96
+ /**
97
+ * Marks (or clears) the agent checkout a store-page payment paid (a hand-off
98
+ * cart) as order pending, so the agent's own steps on it answer order
99
+ * pending while the store's order isn't created. Clears only the marker of
100
+ * this payment. Never throws: a failed save keeps the marker in this process.
101
+ */
102
+ export declare function markHandoffOrderPending(ctx: KitContext, agentCheckoutId: string, paymentId: string, pending: boolean): Promise<void>;
103
+ /** Reads an AgentCheckoutResult; null (treated as malformed) when it doesn't have what the kit relies on. */
104
+ export declare function readResult(data: unknown): RelayedResult | null;
105
+ /** The code of the kit's answer to an agent's `complete` on a checkout paid on the store's own page. */
106
+ export declare const PAID_ON_STORE = "checkout_paid_on_store";
107
+ export declare const PAID_ON_STORE_MESSAGE: string;