@agent-score/commerce 1.0.0 → 1.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.
Files changed (49) hide show
  1. package/dist/{_response-DmziuJz6.d.mts → _response-DS-LR590.d.mts} +13 -8
  2. package/dist/{_response-rbK0zM7y.d.ts → _response-RpEB7-vl.d.ts} +13 -8
  3. package/dist/challenge/index.js.map +1 -1
  4. package/dist/challenge/index.mjs.map +1 -1
  5. package/dist/core.js +86 -67
  6. package/dist/core.js.map +1 -1
  7. package/dist/core.mjs +86 -67
  8. package/dist/core.mjs.map +1 -1
  9. package/dist/discovery/index.js +3 -3
  10. package/dist/discovery/index.js.map +1 -1
  11. package/dist/discovery/index.mjs +3 -3
  12. package/dist/discovery/index.mjs.map +1 -1
  13. package/dist/identity/express.d.mts +1 -1
  14. package/dist/identity/express.d.ts +1 -1
  15. package/dist/identity/express.js +120 -71
  16. package/dist/identity/express.js.map +1 -1
  17. package/dist/identity/express.mjs +120 -71
  18. package/dist/identity/express.mjs.map +1 -1
  19. package/dist/identity/fastify.d.mts +1 -1
  20. package/dist/identity/fastify.d.ts +1 -1
  21. package/dist/identity/fastify.js +120 -71
  22. package/dist/identity/fastify.js.map +1 -1
  23. package/dist/identity/fastify.mjs +120 -71
  24. package/dist/identity/fastify.mjs.map +1 -1
  25. package/dist/identity/hono.d.mts +1 -1
  26. package/dist/identity/hono.d.ts +1 -1
  27. package/dist/identity/hono.js +120 -71
  28. package/dist/identity/hono.js.map +1 -1
  29. package/dist/identity/hono.mjs +120 -71
  30. package/dist/identity/hono.mjs.map +1 -1
  31. package/dist/identity/nextjs.d.mts +1 -1
  32. package/dist/identity/nextjs.d.ts +1 -1
  33. package/dist/identity/nextjs.js +120 -71
  34. package/dist/identity/nextjs.js.map +1 -1
  35. package/dist/identity/nextjs.mjs +120 -71
  36. package/dist/identity/nextjs.mjs.map +1 -1
  37. package/dist/identity/web.d.mts +1 -1
  38. package/dist/identity/web.d.ts +1 -1
  39. package/dist/identity/web.js +120 -71
  40. package/dist/identity/web.js.map +1 -1
  41. package/dist/identity/web.mjs +120 -71
  42. package/dist/identity/web.mjs.map +1 -1
  43. package/dist/index.d.mts +1 -1
  44. package/dist/index.d.ts +1 -1
  45. package/dist/index.js +122 -81
  46. package/dist/index.js.map +1 -1
  47. package/dist/index.mjs +122 -81
  48. package/dist/index.mjs.map +1 -1
  49. package/package.json +3 -3
@@ -38,11 +38,10 @@ module.exports = __toCommonJS(nextjs_exports);
38
38
  var FIXABLE_DENIAL_REASONS = /* @__PURE__ */ new Set([
39
39
  "kyc_required",
40
40
  "kyc_pending",
41
- "kyc_failed",
42
- "jurisdiction_restricted"
41
+ "kyc_failed"
43
42
  ]);
44
43
  function isFixableDenial(reasons) {
45
- if (!reasons || reasons.length === 0) return true;
44
+ if (!reasons || reasons.length === 0) return false;
46
45
  return reasons.every((r) => FIXABLE_DENIAL_REASONS.has(r));
47
46
  }
48
47
  function denialReasonStatus(reason) {
@@ -115,6 +114,47 @@ function verificationAgentInstructions(input = {}) {
115
114
  }
116
115
 
117
116
  // src/_response.ts
117
+ var WALLET_NOT_TRUSTED_INSTRUCTIONS = JSON.stringify({
118
+ action: "contact_support",
119
+ steps: [
120
+ "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.",
121
+ "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).",
122
+ "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)."
123
+ ],
124
+ 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."
125
+ });
126
+ var PAYMENT_REQUIRED_INSTRUCTIONS = JSON.stringify({
127
+ action: "contact_merchant",
128
+ steps: [
129
+ "The merchant's AgentScore tier does not include the assess feature, so agent identity cannot be evaluated. This is a merchant-side configuration gap \u2014 there is no agent-side recovery.",
130
+ "Contact the merchant (their support channel \u2014 typically listed in /llms.txt or the OpenAPI servers metadata) and request they upgrade their AgentScore plan."
131
+ ],
132
+ user_message: "This merchant's identity gate is misconfigured (AgentScore tier doesn't support assess). Contact the merchant \u2014 there's nothing to fix on the agent side."
133
+ });
134
+ var IDENTITY_VERIFICATION_REQUIRED_FALLBACK_INSTRUCTIONS = JSON.stringify({
135
+ action: "deliver_verify_url_and_poll",
136
+ steps: [
137
+ "Share verify_url with the user \u2014 they complete identity verification on AgentScore.",
138
+ "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.",
139
+ "Retry the original request with header `X-Operator-Token: <opc_...>`."
140
+ ],
141
+ user_message: "Identity verification is required. Visit verify_url, then poll poll_url for the operator token and retry."
142
+ });
143
+ var TOKEN_EXPIRED_FALLBACK_INSTRUCTIONS = JSON.stringify({
144
+ action: "deliver_verify_url_and_poll",
145
+ steps: [
146
+ "The operator token is expired or revoked. AgentScore auto-mints a fresh verification session \u2014 complete it to receive a new opc_...",
147
+ "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.",
148
+ "Retry the original request with header `X-Operator-Token: <new_opc_...>`."
149
+ ],
150
+ user_message: "Operator token is expired or revoked. A new verification session has been minted \u2014 visit verify_url to refresh."
151
+ });
152
+ var DEFAULT_AGENT_INSTRUCTIONS = {
153
+ wallet_not_trusted: WALLET_NOT_TRUSTED_INSTRUCTIONS,
154
+ payment_required: PAYMENT_REQUIRED_INSTRUCTIONS,
155
+ identity_verification_required: IDENTITY_VERIFICATION_REQUIRED_FALLBACK_INSTRUCTIONS,
156
+ token_expired: TOKEN_EXPIRED_FALLBACK_INSTRUCTIONS
157
+ };
118
158
  var DEFAULT_MESSAGES = {
119
159
  missing_identity: "No identity provided. Send X-Wallet-Address (wallet) or X-Operator-Token (credential).",
120
160
  identity_verification_required: "Identity verification is required to access this resource. Visit verify_url to complete KYC.",
@@ -151,7 +191,8 @@ function denialReasonToBody(reason) {
151
191
  if (reason.session_id) body.session_id = reason.session_id;
152
192
  if (reason.poll_secret) body.poll_secret = reason.poll_secret;
153
193
  if (reason.poll_url) body.poll_url = reason.poll_url;
154
- if (reason.agent_instructions) body.agent_instructions = reason.agent_instructions;
194
+ const instructions = reason.agent_instructions ?? DEFAULT_AGENT_INSTRUCTIONS[reason.code];
195
+ if (instructions) body.agent_instructions = instructions;
155
196
  if (reason.agent_memory) body.agent_memory = reason.agent_memory;
156
197
  if (reason.claimed_operator) body.claimed_operator = reason.claimed_operator;
157
198
  if (reason.code === "wallet_signer_mismatch") body.actual_signer_operator = reason.actual_signer_operator ?? null;
@@ -301,80 +342,80 @@ function createAgentScoreCore(options) {
301
342
  } = options;
302
343
  const baseUrl = stripTrailingSlashes(rawBaseUrl);
303
344
  const agentMemoryHint = buildAgentMemoryHint();
304
- const defaultUa = `@agent-score/commerce@${"1.0.0"}`;
345
+ const defaultUa = `@agent-score/commerce@${"1.0.1"}`;
305
346
  const userAgentHeader = userAgent ? `${userAgent} (${defaultUa})` : defaultUa;
306
347
  const API_TIMEOUT_MS = 1e4;
307
348
  const cache = new TTLCache(cacheSeconds * 1e3);
308
- async function evaluate(identity, ctx) {
309
- if (!identity || !identity.address && !identity.operatorToken) {
310
- if (failOpen) return { kind: "allow" };
311
- if (createSessionOnMissing) {
349
+ async function tryMintSessionDenial(ctx) {
350
+ if (!createSessionOnMissing) return void 0;
351
+ try {
352
+ const sessionBody = {};
353
+ if (createSessionOnMissing.context != null) sessionBody.context = createSessionOnMissing.context;
354
+ if (createSessionOnMissing.productName != null) sessionBody.product_name = createSessionOnMissing.productName;
355
+ if (createSessionOnMissing.getSessionOptions && ctx !== void 0) {
312
356
  try {
313
- const sessionBody = {};
314
- if (createSessionOnMissing.context != null) sessionBody.context = createSessionOnMissing.context;
315
- if (createSessionOnMissing.productName != null) sessionBody.product_name = createSessionOnMissing.productName;
316
- if (createSessionOnMissing.getSessionOptions && ctx !== void 0) {
317
- try {
318
- const dynamic = await createSessionOnMissing.getSessionOptions(ctx);
319
- if (dynamic?.context != null) sessionBody.context = dynamic.context;
320
- if (dynamic?.productName != null) sessionBody.product_name = dynamic.productName;
321
- } catch (err) {
322
- console.warn("[gate] createSessionOnMissing.getSessionOptions hook failed:", err instanceof Error ? err.message : err);
323
- }
324
- }
325
- const sessionBaseUrl = stripTrailingSlashes(createSessionOnMissing.baseUrl ?? "https://api.agentscore.sh");
326
- const sessionRes = await fetch(`${sessionBaseUrl}/v1/sessions`, {
327
- method: "POST",
328
- headers: {
329
- "X-API-Key": createSessionOnMissing.apiKey,
330
- "Content-Type": "application/json",
331
- Accept: "application/json",
332
- "User-Agent": userAgentHeader
333
- },
334
- body: JSON.stringify(sessionBody),
335
- signal: AbortSignal.timeout(API_TIMEOUT_MS)
336
- });
337
- if (sessionRes.ok) {
338
- const data = await sessionRes.json();
339
- if (typeof data.session_id !== "string" || typeof data.poll_secret !== "string" || typeof data.verify_url !== "string") {
340
- console.warn("[gate] /v1/sessions returned 200 without required fields \u2014 falling back to bare missing_identity");
341
- } else {
342
- let extra;
343
- if (createSessionOnMissing.onBeforeSession && ctx !== void 0) {
344
- try {
345
- const sessionMeta = {
346
- session_id: data.session_id,
347
- verify_url: data.verify_url,
348
- poll_secret: data.poll_secret,
349
- poll_url: data.poll_url,
350
- expires_at: data.expires_at
351
- };
352
- const result = await createSessionOnMissing.onBeforeSession(ctx, sessionMeta);
353
- if (result && typeof result === "object") extra = result;
354
- } catch (err) {
355
- console.warn("[gate] createSessionOnMissing.onBeforeSession hook failed:", err instanceof Error ? err.message : err);
356
- }
357
- }
358
- const apiNextSteps = data.next_steps;
359
- return {
360
- kind: "deny",
361
- reason: {
362
- code: "identity_verification_required",
363
- verify_url: data.verify_url,
364
- session_id: data.session_id,
365
- poll_secret: data.poll_secret,
366
- poll_url: data.poll_url,
367
- agent_instructions: apiNextSteps ? JSON.stringify(apiNextSteps) : void 0,
368
- agent_memory: agentMemoryHint,
369
- ...extra && { extra }
370
- }
371
- };
372
- }
373
- }
357
+ const dynamic = await createSessionOnMissing.getSessionOptions(ctx);
358
+ if (dynamic?.context != null) sessionBody.context = dynamic.context;
359
+ if (dynamic?.productName != null) sessionBody.product_name = dynamic.productName;
360
+ } catch (err) {
361
+ console.warn("[gate] createSessionOnMissing.getSessionOptions hook failed:", err instanceof Error ? err.message : err);
362
+ }
363
+ }
364
+ const sessionBaseUrl = stripTrailingSlashes(createSessionOnMissing.baseUrl ?? "https://api.agentscore.sh");
365
+ const sessionRes = await fetch(`${sessionBaseUrl}/v1/sessions`, {
366
+ method: "POST",
367
+ headers: {
368
+ "X-API-Key": createSessionOnMissing.apiKey,
369
+ "Content-Type": "application/json",
370
+ Accept: "application/json",
371
+ "User-Agent": userAgentHeader
372
+ },
373
+ body: JSON.stringify(sessionBody),
374
+ signal: AbortSignal.timeout(API_TIMEOUT_MS)
375
+ });
376
+ if (!sessionRes.ok) return void 0;
377
+ const data = await sessionRes.json();
378
+ if (typeof data.session_id !== "string" || typeof data.poll_secret !== "string" || typeof data.verify_url !== "string") {
379
+ console.warn("[gate] /v1/sessions returned 200 without required fields \u2014 falling back to bare denial");
380
+ return void 0;
381
+ }
382
+ let extra;
383
+ if (createSessionOnMissing.onBeforeSession && ctx !== void 0) {
384
+ try {
385
+ const sessionMeta = {
386
+ session_id: data.session_id,
387
+ verify_url: data.verify_url,
388
+ poll_secret: data.poll_secret,
389
+ poll_url: data.poll_url,
390
+ expires_at: data.expires_at
391
+ };
392
+ const result = await createSessionOnMissing.onBeforeSession(ctx, sessionMeta);
393
+ if (result && typeof result === "object") extra = result;
374
394
  } catch (err) {
375
- console.warn("[gate] createSessionOnMissing path failed \u2014 falling back to bare missing_identity:", err instanceof Error ? err.message : err);
395
+ console.warn("[gate] createSessionOnMissing.onBeforeSession hook failed:", err instanceof Error ? err.message : err);
376
396
  }
377
397
  }
398
+ const apiNextSteps = data.next_steps;
399
+ return {
400
+ code: "identity_verification_required",
401
+ verify_url: data.verify_url,
402
+ session_id: data.session_id,
403
+ poll_secret: data.poll_secret,
404
+ poll_url: data.poll_url,
405
+ agent_instructions: apiNextSteps ? JSON.stringify(apiNextSteps) : void 0,
406
+ agent_memory: agentMemoryHint,
407
+ ...extra && { extra }
408
+ };
409
+ } catch (err) {
410
+ console.warn("[gate] createSessionOnMissing path failed \u2014 falling back to bare denial:", err instanceof Error ? err.message : err);
411
+ return void 0;
412
+ }
413
+ }
414
+ async function evaluate(identity, ctx) {
415
+ if (!identity || !identity.address && !identity.operatorToken) {
416
+ if (failOpen) return { kind: "allow" };
417
+ const sessionReason = await tryMintSessionDenial(ctx);
418
+ if (sessionReason) return { kind: "deny", reason: sessionReason };
378
419
  const missingIdentityInstructions = JSON.stringify({
379
420
  action: "probe_identity_then_session",
380
421
  steps: [
@@ -399,6 +440,10 @@ function createAgentScoreCore(options) {
399
440
  if (cached.allow) {
400
441
  return { kind: "allow", data: cached.raw };
401
442
  }
443
+ if (isFixableDenial(cached.reasons)) {
444
+ const sessionReason = await tryMintSessionDenial(ctx);
445
+ if (sessionReason) return { kind: "deny", reason: sessionReason };
446
+ }
402
447
  return {
403
448
  kind: "deny",
404
449
  reason: {
@@ -496,6 +541,10 @@ function createAgentScoreCore(options) {
496
541
  if (allow) {
497
542
  return { kind: "allow", data };
498
543
  }
544
+ if (isFixableDenial(decisionReasons)) {
545
+ const sessionReason = await tryMintSessionDenial(ctx);
546
+ if (sessionReason) return { kind: "deny", reason: sessionReason };
547
+ }
499
548
  return {
500
549
  kind: "deny",
501
550
  reason: {