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