@agent-score/commerce 1.0.2 → 1.1.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 (52) hide show
  1. package/README.md +29 -2
  2. package/dist/challenge/index.js +58 -0
  3. package/dist/challenge/index.js.map +1 -1
  4. package/dist/challenge/index.mjs +65 -0
  5. package/dist/challenge/index.mjs.map +1 -1
  6. package/dist/core.d.mts +28 -1
  7. package/dist/core.d.ts +28 -1
  8. package/dist/core.js +237 -157
  9. package/dist/core.js.map +1 -1
  10. package/dist/core.mjs +246 -157
  11. package/dist/core.mjs.map +1 -1
  12. package/dist/discovery/index.js.map +1 -1
  13. package/dist/discovery/index.mjs.map +1 -1
  14. package/dist/identity/express.d.mts +18 -2
  15. package/dist/identity/express.d.ts +18 -2
  16. package/dist/identity/express.js +227 -164
  17. package/dist/identity/express.js.map +1 -1
  18. package/dist/identity/express.mjs +232 -164
  19. package/dist/identity/express.mjs.map +1 -1
  20. package/dist/identity/fastify.d.mts +17 -2
  21. package/dist/identity/fastify.d.ts +17 -2
  22. package/dist/identity/fastify.js +227 -164
  23. package/dist/identity/fastify.js.map +1 -1
  24. package/dist/identity/fastify.mjs +232 -164
  25. package/dist/identity/fastify.mjs.map +1 -1
  26. package/dist/identity/hono.d.mts +22 -2
  27. package/dist/identity/hono.d.ts +22 -2
  28. package/dist/identity/hono.js +227 -164
  29. package/dist/identity/hono.js.map +1 -1
  30. package/dist/identity/hono.mjs +232 -164
  31. package/dist/identity/hono.mjs.map +1 -1
  32. package/dist/identity/nextjs.d.mts +8 -1
  33. package/dist/identity/nextjs.d.ts +8 -1
  34. package/dist/identity/nextjs.js +213 -166
  35. package/dist/identity/nextjs.js.map +1 -1
  36. package/dist/identity/nextjs.mjs +220 -166
  37. package/dist/identity/nextjs.mjs.map +1 -1
  38. package/dist/identity/web.d.mts +15 -1
  39. package/dist/identity/web.d.ts +15 -1
  40. package/dist/identity/web.js +213 -166
  41. package/dist/identity/web.js.map +1 -1
  42. package/dist/identity/web.mjs +220 -166
  43. package/dist/identity/web.mjs.map +1 -1
  44. package/dist/index.js +120 -101
  45. package/dist/index.js.map +1 -1
  46. package/dist/index.mjs +127 -101
  47. package/dist/index.mjs.map +1 -1
  48. package/dist/payment/index.js.map +1 -1
  49. package/dist/payment/index.mjs.map +1 -1
  50. package/dist/stripe-multichain/index.js.map +1 -1
  51. package/dist/stripe-multichain/index.mjs.map +1 -1
  52. package/package.json +3 -3
package/README.md CHANGED
@@ -26,7 +26,7 @@ npm install hono mppx @x402/core @x402/evm @x402/svm stripe # whatever your st
26
26
  | `/identity/{hono,express,fastify,nextjs,web}` | Trust gate middleware: KYC, sanctions, age, jurisdiction. `agentscoreGate(...)`, `getAgentScoreData(c)`, `captureWallet(...)`, `verifyWalletSignerMatch(...)`. Plus shared denial helpers: `denialReasonStatus`, `denialReasonToBody`, `buildSignerMismatchBody`, `buildContactSupportNextSteps`, `verificationAgentInstructions`, `isFixableDenial`, `FIXABLE_DENIAL_REASONS`. |
27
27
  | `/payment` | `networks`, `USDC`, `rails` registries; `paymentDirective`, `buildPaymentDirective`, `wwwAuthenticateHeader`, `paymentRequiredHeader`, `aliasAmountFields` (v1↔v2 amount field shim — emits both `amount` and `maxAmountRequired` so v1-only x402 parsers like Coinbase awal can read v2 bodies), `settlementOverrideHeader`, `dispatchSettlementByNetwork`, `extractPaymentSigner` (returns `{address, network}`); `createX402Server`, `createMppxServer`; drop-in x402 helpers: `validateX402NetworkConfig` (boot-time guard), `verifyX402Request` (parse + validate inbound X-Payment), `processX402Settle` (verify-then-settle with one call). |
28
28
  | `/discovery` | `isDiscoveryProbeRequest`, `buildDiscoveryProbeResponse` (with optional `x402Sample` for x402-aware crawlers — `awal x402 details` etc.), `sampleX402AcceptForNetwork` (USDC sample-accept builder for known CAIP-2 networks), `buildWellKnownMpp`, `buildLlmsTxt` + `llmsTxtIdentitySection` + `llmsTxtPaymentSection` (compact + verbose modes), `agentscoreOpenApiSnippets`, `createBazaarDiscovery`, `noindexNonDiscoveryPaths` (Hono middleware that emits `X-Robots-Tag: noindex` on every path except the agent-discovery surfaces — defaults cover `/openapi.json`, `/llms.txt`, `/.well-known/{mpp.json,agent-card.json,ucp}`, `/favicon.{png,ico}`; pure helpers `isDiscoveryPath` + `defaultDiscoveryPaths` for non-Hono frameworks). |
29
- | `/challenge` | `build402Body`, `buildAcceptedMethods`, `buildIdentityMetadata`, `buildHowToPay`, `buildAgentInstructions`, `buildPricingBlock`, `firstEncounterAgentMemory`, `OrderReceipt`; `respond402` — drop-in 402 emit that preserves mppx's `WWW-Authenticate` and layers x402's `PAYMENT-REQUIRED`. `buildValidationError` — structured 4xx body builder (`{error: {code, message}, required_fields?, example_body?, next_steps?, ...extra}`) so vendors compose body shapes by name instead of inlining at every validation site. |
29
+ | `/challenge` | `build402Body`, `buildAcceptedMethods`, `buildIdentityMetadata`, `buildHowToPay`, `buildAgentInstructions` (auto-emits per-rail `compatible_clients` — smoke-verified CLIs the agent should use; vendor override supported), `buildPricingBlock`, `firstEncounterAgentMemory`, `OrderReceipt`; `respond402` — drop-in 402 emit that preserves mppx's `WWW-Authenticate` and layers x402's `PAYMENT-REQUIRED`. `buildValidationError` — structured 4xx body builder (`{error: {code, message}, required_fields?, example_body?, next_steps?, ...extra}`) so vendors compose body shapes by name instead of inlining at every validation site. |
30
30
  | `/stripe-multichain` | `createMultichainPaymentIntent`, `getDepositAddress`, `simulateCryptoDeposit`, `createMppxStripe`; `createPiCache` (TTL'd PI / deposit-address cache, Redis-backed when `redisUrl` set, in-memory otherwise), `simulateDepositIfTestMode` (gates on `sk_test_` and looks up the PI for you), `STRIPE_TEST_TX_HASH_SUCCESS` / `STRIPE_TEST_TX_HASH_FAILED` constants. Peer dep on `stripe`. |
31
31
  | `/api` | Everything from `@agent-score/sdk` re-exported in one place: `AgentScore` + `AgentScoreError`, `AGENTSCORE_TEST_ADDRESSES` + `isAgentScoreTestAddress`. **Don't add `@agent-score/sdk` as a separate dep** — the two can drift versions and cause subtle type mismatches. |
32
32
 
@@ -278,9 +278,36 @@ app.post("/purchase", async (c) => {
278
278
  });
279
279
  ```
280
280
 
281
+ ## Fail-open behavior
282
+
283
+ By default AgentScore Gate fails closed: any AgentScore-side infrastructure failure (HTTP 429, 5xx, network timeout) returns 503 to the buyer. Set `failOpen: true` on `agentscoreGate({...})` to opt in to graceful degradation:
284
+
285
+ ```ts
286
+ import { agentscoreGate, getGateDegradedState } from '@agent-score/commerce/identity/hono';
287
+
288
+ const gate = agentscoreGate({ apiKey: process.env.AGENTSCORE_API_KEY!, failOpen: true });
289
+
290
+ app.use('/purchase', gate);
291
+
292
+ app.post('/purchase', async (c) => {
293
+ const { degraded, infraReason } = getGateDegradedState(c);
294
+ if (degraded) {
295
+ // Compliance was NOT enforced this request — log/alert/refund-async/etc.
296
+ console.warn(`[gate] degraded: ${infraReason}`);
297
+ }
298
+ // ...rest of handler
299
+ });
300
+ ```
301
+
302
+ When `failOpen: true` AND the failure is infra-shape, the gate carries `degraded: true` + `infraReason: 'quota_exceeded' | 'api_error' | 'network_timeout'` so merchants can log/alert without parsing console output. **Compliance denials (sanctions, age, jurisdiction, signer-mismatch) still deny regardless of `failOpen`** — `failOpen` only covers "AgentScore couldn't tell us," never "AgentScore said no."
303
+
304
+ For regulated commerce (alcohol, age-gated, sanctioned-jurisdiction-relevant) keep the default `failOpen: false` — outage is the correct posture; bypassing compliance on infra failure is a compliance gap. For low-stakes commerce or high-uptime SLAs, opt in and use the `degraded` flag as the audit trail.
305
+
306
+ The `getGateDegradedState` helper is exported by every framework adapter (Hono, Express, Fastify, Next.js, Web Fetch). For `withAgentScoreGate` (Next.js / Web Fetch), the `degraded` + `infraReason` fields land directly on the `gate` object passed to your handler.
307
+
281
308
  ## Examples
282
309
 
283
- The [examples/](./examples) directory has 6 runnable single-file Hono apps covering common merchant scenarios — copy-paste templates, not frameworks. See [examples/README.md](./examples/README.md) for the full table.
310
+ The [examples/](./examples) directory has 7 runnable single-file Hono apps covering common merchant scenarios — copy-paste templates, not frameworks. See [examples/README.md](./examples/README.md) for the full table.
284
311
 
285
312
  ## Vendor profile examples
286
313
 
@@ -215,6 +215,64 @@ function buildAgentInstructions(input) {
215
215
  };
216
216
  }
217
217
 
218
+ // src/core.ts
219
+ var import_sdk = require("@agent-score/sdk");
220
+
221
+ // src/_response.ts
222
+ var WALLET_NOT_TRUSTED_INSTRUCTIONS = JSON.stringify({
223
+ action: "contact_support",
224
+ steps: [
225
+ "The wallet's operator failed an UNFIXABLE compliance check (sanctions, age, or jurisdiction). `reasons` lists which: `sanctions_flagged` / `age_insufficient` / `jurisdiction_restricted`. KYC re-verification won't change the outcome \u2014 the policy denial is structural.",
226
+ "Surface the denial to the user with the merchant's support contact. Do not retry the same merchant request; do not hand the user a verify_url (verification won't fix this code path).",
227
+ "Fixable compliance reasons (`kyc_required`, `kyc_pending`, `kyc_failed`) do NOT land on this code \u2014 the gate auto-mints a verification session for those and returns `identity_verification_required` with poll endpoints, same shape as `missing_identity`. `jurisdiction_restricted` IS in the unfixable bucket because the API only emits it after KYC is verified (the user's KYC'd country is in the blocked list \u2014 re-doing KYC won't change the country)."
228
+ ],
229
+ user_message: "This purchase is denied by the merchant's compliance policy and cannot be resolved by re-verifying. Contact the merchant's support if you believe this is in error."
230
+ });
231
+ var PAYMENT_REQUIRED_INSTRUCTIONS = JSON.stringify({
232
+ action: "contact_merchant",
233
+ steps: [
234
+ "The merchant's AgentScore account does not have the assess endpoint enabled, so agent identity cannot be evaluated. This is a merchant-side configuration gap \u2014 there is no agent-side recovery.",
235
+ "Contact the merchant (their support channel \u2014 typically listed in /llms.txt or the OpenAPI servers metadata) so they can resolve the configuration on their side."
236
+ ],
237
+ user_message: "This merchant's identity gate is misconfigured. Contact the merchant \u2014 there's nothing to fix on the agent side."
238
+ });
239
+ var IDENTITY_VERIFICATION_REQUIRED_FALLBACK_INSTRUCTIONS = JSON.stringify({
240
+ action: "deliver_verify_url_and_poll",
241
+ steps: [
242
+ "Share verify_url with the user \u2014 they complete identity verification on AgentScore.",
243
+ "If session_id + poll_secret are present in the body, poll poll_url every 5 seconds with header `X-Poll-Secret: <poll_secret>` until status=verified. The poll returns a one-time operator_token.",
244
+ "Retry the original request with header `X-Operator-Token: <opc_...>`."
245
+ ],
246
+ user_message: "Identity verification is required. Visit verify_url, then poll poll_url for the operator token and retry."
247
+ });
248
+ var API_ERROR_INSTRUCTIONS = JSON.stringify({
249
+ action: "retry_with_backoff",
250
+ steps: [
251
+ "Verification is temporarily unavailable. Retry the request after 5\u201330 seconds with exponential backoff.",
252
+ "This is NOT a compliance denial \u2014 the user does not need to re-verify their identity. Send the same identity headers (X-Wallet-Address or X-Operator-Token) on retry.",
253
+ "If the request continues to fail after 3+ retries (~60 seconds total), surface the error to the user with the merchant's support contact."
254
+ ],
255
+ user_message: "Verification is temporarily unavailable. Please try again in a moment \u2014 this is a transient issue, not a problem with your account."
256
+ });
257
+ var QUOTA_EXCEEDED_INSTRUCTIONS = JSON.stringify({
258
+ action: "contact_merchant",
259
+ steps: [
260
+ "AgentScore identity verification is unavailable for this merchant. This is a merchant-side issue and is NOT recoverable via retry.",
261
+ "Do not retry: the same 503 will be returned until the merchant resolves the issue on their side.",
262
+ "Surface to the user with the merchant's support contact. The merchant (not the agent) needs to act."
263
+ ],
264
+ user_message: "This merchant's identity verification is temporarily unavailable. Try again later, or contact the merchant directly."
265
+ });
266
+ var TOKEN_EXPIRED_FALLBACK_INSTRUCTIONS = JSON.stringify({
267
+ action: "deliver_verify_url_and_poll",
268
+ steps: [
269
+ "The operator token is expired or revoked. AgentScore auto-mints a fresh verification session \u2014 complete it to receive a new opc_...",
270
+ "Share verify_url with the user, then poll poll_url every 5 seconds with header `X-Poll-Secret: <poll_secret>` until status=verified. The poll returns a fresh one-time operator_token.",
271
+ "Retry the original request with header `X-Operator-Token: <new_opc_...>`."
272
+ ],
273
+ user_message: "Operator token is expired or revoked. A new verification session has been minted \u2014 visit verify_url to refresh."
274
+ });
275
+
218
276
  // src/core.ts
219
277
  var CANONICAL_AGENTSCORE_API = "https://api.agentscore.sh";
220
278
  var WALLET_SIGNER_MISMATCH_INSTRUCTIONS = JSON.stringify({