@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.
- 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/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/package.json +3 -3
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
var FIXABLE_DENIAL_REASONS = /* @__PURE__ */ new Set([
|
|
3
3
|
"kyc_required",
|
|
4
4
|
"kyc_pending",
|
|
5
|
-
"kyc_failed"
|
|
6
|
-
"jurisdiction_restricted"
|
|
5
|
+
"kyc_failed"
|
|
7
6
|
]);
|
|
8
7
|
function isFixableDenial(reasons) {
|
|
9
|
-
if (!reasons || reasons.length === 0) return
|
|
8
|
+
if (!reasons || reasons.length === 0) return false;
|
|
10
9
|
return reasons.every((r) => FIXABLE_DENIAL_REASONS.has(r));
|
|
11
10
|
}
|
|
12
11
|
function denialReasonStatus(reason) {
|
|
@@ -79,6 +78,47 @@ function verificationAgentInstructions(input = {}) {
|
|
|
79
78
|
}
|
|
80
79
|
|
|
81
80
|
// src/_response.ts
|
|
81
|
+
var WALLET_NOT_TRUSTED_INSTRUCTIONS = JSON.stringify({
|
|
82
|
+
action: "contact_support",
|
|
83
|
+
steps: [
|
|
84
|
+
"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.",
|
|
85
|
+
"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).",
|
|
86
|
+
"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)."
|
|
87
|
+
],
|
|
88
|
+
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."
|
|
89
|
+
});
|
|
90
|
+
var PAYMENT_REQUIRED_INSTRUCTIONS = JSON.stringify({
|
|
91
|
+
action: "contact_merchant",
|
|
92
|
+
steps: [
|
|
93
|
+
"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.",
|
|
94
|
+
"Contact the merchant (their support channel \u2014 typically listed in /llms.txt or the OpenAPI servers metadata) and request they upgrade their AgentScore plan."
|
|
95
|
+
],
|
|
96
|
+
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."
|
|
97
|
+
});
|
|
98
|
+
var IDENTITY_VERIFICATION_REQUIRED_FALLBACK_INSTRUCTIONS = JSON.stringify({
|
|
99
|
+
action: "deliver_verify_url_and_poll",
|
|
100
|
+
steps: [
|
|
101
|
+
"Share verify_url with the user \u2014 they complete identity verification on AgentScore.",
|
|
102
|
+
"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.",
|
|
103
|
+
"Retry the original request with header `X-Operator-Token: <opc_...>`."
|
|
104
|
+
],
|
|
105
|
+
user_message: "Identity verification is required. Visit verify_url, then poll poll_url for the operator token and retry."
|
|
106
|
+
});
|
|
107
|
+
var TOKEN_EXPIRED_FALLBACK_INSTRUCTIONS = JSON.stringify({
|
|
108
|
+
action: "deliver_verify_url_and_poll",
|
|
109
|
+
steps: [
|
|
110
|
+
"The operator token is expired or revoked. AgentScore auto-mints a fresh verification session \u2014 complete it to receive a new opc_...",
|
|
111
|
+
"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.",
|
|
112
|
+
"Retry the original request with header `X-Operator-Token: <new_opc_...>`."
|
|
113
|
+
],
|
|
114
|
+
user_message: "Operator token is expired or revoked. A new verification session has been minted \u2014 visit verify_url to refresh."
|
|
115
|
+
});
|
|
116
|
+
var DEFAULT_AGENT_INSTRUCTIONS = {
|
|
117
|
+
wallet_not_trusted: WALLET_NOT_TRUSTED_INSTRUCTIONS,
|
|
118
|
+
payment_required: PAYMENT_REQUIRED_INSTRUCTIONS,
|
|
119
|
+
identity_verification_required: IDENTITY_VERIFICATION_REQUIRED_FALLBACK_INSTRUCTIONS,
|
|
120
|
+
token_expired: TOKEN_EXPIRED_FALLBACK_INSTRUCTIONS
|
|
121
|
+
};
|
|
82
122
|
var DEFAULT_MESSAGES = {
|
|
83
123
|
missing_identity: "No identity provided. Send X-Wallet-Address (wallet) or X-Operator-Token (credential).",
|
|
84
124
|
identity_verification_required: "Identity verification is required to access this resource. Visit verify_url to complete KYC.",
|
|
@@ -115,7 +155,8 @@ function denialReasonToBody(reason) {
|
|
|
115
155
|
if (reason.session_id) body.session_id = reason.session_id;
|
|
116
156
|
if (reason.poll_secret) body.poll_secret = reason.poll_secret;
|
|
117
157
|
if (reason.poll_url) body.poll_url = reason.poll_url;
|
|
118
|
-
|
|
158
|
+
const instructions = reason.agent_instructions ?? DEFAULT_AGENT_INSTRUCTIONS[reason.code];
|
|
159
|
+
if (instructions) body.agent_instructions = instructions;
|
|
119
160
|
if (reason.agent_memory) body.agent_memory = reason.agent_memory;
|
|
120
161
|
if (reason.claimed_operator) body.claimed_operator = reason.claimed_operator;
|
|
121
162
|
if (reason.code === "wallet_signer_mismatch") body.actual_signer_operator = reason.actual_signer_operator ?? null;
|
|
@@ -265,80 +306,80 @@ function createAgentScoreCore(options) {
|
|
|
265
306
|
} = options;
|
|
266
307
|
const baseUrl = stripTrailingSlashes(rawBaseUrl);
|
|
267
308
|
const agentMemoryHint = buildAgentMemoryHint();
|
|
268
|
-
const defaultUa = `@agent-score/commerce@${"1.0.
|
|
309
|
+
const defaultUa = `@agent-score/commerce@${"1.0.1"}`;
|
|
269
310
|
const userAgentHeader = userAgent ? `${userAgent} (${defaultUa})` : defaultUa;
|
|
270
311
|
const API_TIMEOUT_MS = 1e4;
|
|
271
312
|
const cache = new TTLCache(cacheSeconds * 1e3);
|
|
272
|
-
async function
|
|
273
|
-
if (!
|
|
274
|
-
|
|
275
|
-
|
|
313
|
+
async function tryMintSessionDenial(ctx) {
|
|
314
|
+
if (!createSessionOnMissing) return void 0;
|
|
315
|
+
try {
|
|
316
|
+
const sessionBody = {};
|
|
317
|
+
if (createSessionOnMissing.context != null) sessionBody.context = createSessionOnMissing.context;
|
|
318
|
+
if (createSessionOnMissing.productName != null) sessionBody.product_name = createSessionOnMissing.productName;
|
|
319
|
+
if (createSessionOnMissing.getSessionOptions && ctx !== void 0) {
|
|
276
320
|
try {
|
|
277
|
-
const
|
|
278
|
-
if (
|
|
279
|
-
if (
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
expires_at: data.expires_at
|
|
315
|
-
};
|
|
316
|
-
const result = await createSessionOnMissing.onBeforeSession(ctx, sessionMeta);
|
|
317
|
-
if (result && typeof result === "object") extra = result;
|
|
318
|
-
} catch (err) {
|
|
319
|
-
console.warn("[gate] createSessionOnMissing.onBeforeSession hook failed:", err instanceof Error ? err.message : err);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
const apiNextSteps = data.next_steps;
|
|
323
|
-
return {
|
|
324
|
-
kind: "deny",
|
|
325
|
-
reason: {
|
|
326
|
-
code: "identity_verification_required",
|
|
327
|
-
verify_url: data.verify_url,
|
|
328
|
-
session_id: data.session_id,
|
|
329
|
-
poll_secret: data.poll_secret,
|
|
330
|
-
poll_url: data.poll_url,
|
|
331
|
-
agent_instructions: apiNextSteps ? JSON.stringify(apiNextSteps) : void 0,
|
|
332
|
-
agent_memory: agentMemoryHint,
|
|
333
|
-
...extra && { extra }
|
|
334
|
-
}
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
}
|
|
321
|
+
const dynamic = await createSessionOnMissing.getSessionOptions(ctx);
|
|
322
|
+
if (dynamic?.context != null) sessionBody.context = dynamic.context;
|
|
323
|
+
if (dynamic?.productName != null) sessionBody.product_name = dynamic.productName;
|
|
324
|
+
} catch (err) {
|
|
325
|
+
console.warn("[gate] createSessionOnMissing.getSessionOptions hook failed:", err instanceof Error ? err.message : err);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const sessionBaseUrl = stripTrailingSlashes(createSessionOnMissing.baseUrl ?? "https://api.agentscore.sh");
|
|
329
|
+
const sessionRes = await fetch(`${sessionBaseUrl}/v1/sessions`, {
|
|
330
|
+
method: "POST",
|
|
331
|
+
headers: {
|
|
332
|
+
"X-API-Key": createSessionOnMissing.apiKey,
|
|
333
|
+
"Content-Type": "application/json",
|
|
334
|
+
Accept: "application/json",
|
|
335
|
+
"User-Agent": userAgentHeader
|
|
336
|
+
},
|
|
337
|
+
body: JSON.stringify(sessionBody),
|
|
338
|
+
signal: AbortSignal.timeout(API_TIMEOUT_MS)
|
|
339
|
+
});
|
|
340
|
+
if (!sessionRes.ok) return void 0;
|
|
341
|
+
const data = await sessionRes.json();
|
|
342
|
+
if (typeof data.session_id !== "string" || typeof data.poll_secret !== "string" || typeof data.verify_url !== "string") {
|
|
343
|
+
console.warn("[gate] /v1/sessions returned 200 without required fields \u2014 falling back to bare denial");
|
|
344
|
+
return void 0;
|
|
345
|
+
}
|
|
346
|
+
let extra;
|
|
347
|
+
if (createSessionOnMissing.onBeforeSession && ctx !== void 0) {
|
|
348
|
+
try {
|
|
349
|
+
const sessionMeta = {
|
|
350
|
+
session_id: data.session_id,
|
|
351
|
+
verify_url: data.verify_url,
|
|
352
|
+
poll_secret: data.poll_secret,
|
|
353
|
+
poll_url: data.poll_url,
|
|
354
|
+
expires_at: data.expires_at
|
|
355
|
+
};
|
|
356
|
+
const result = await createSessionOnMissing.onBeforeSession(ctx, sessionMeta);
|
|
357
|
+
if (result && typeof result === "object") extra = result;
|
|
338
358
|
} catch (err) {
|
|
339
|
-
console.warn("[gate] createSessionOnMissing
|
|
359
|
+
console.warn("[gate] createSessionOnMissing.onBeforeSession hook failed:", err instanceof Error ? err.message : err);
|
|
340
360
|
}
|
|
341
361
|
}
|
|
362
|
+
const apiNextSteps = data.next_steps;
|
|
363
|
+
return {
|
|
364
|
+
code: "identity_verification_required",
|
|
365
|
+
verify_url: data.verify_url,
|
|
366
|
+
session_id: data.session_id,
|
|
367
|
+
poll_secret: data.poll_secret,
|
|
368
|
+
poll_url: data.poll_url,
|
|
369
|
+
agent_instructions: apiNextSteps ? JSON.stringify(apiNextSteps) : void 0,
|
|
370
|
+
agent_memory: agentMemoryHint,
|
|
371
|
+
...extra && { extra }
|
|
372
|
+
};
|
|
373
|
+
} catch (err) {
|
|
374
|
+
console.warn("[gate] createSessionOnMissing path failed \u2014 falling back to bare denial:", err instanceof Error ? err.message : err);
|
|
375
|
+
return void 0;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
async function evaluate(identity, ctx) {
|
|
379
|
+
if (!identity || !identity.address && !identity.operatorToken) {
|
|
380
|
+
if (failOpen) return { kind: "allow" };
|
|
381
|
+
const sessionReason = await tryMintSessionDenial(ctx);
|
|
382
|
+
if (sessionReason) return { kind: "deny", reason: sessionReason };
|
|
342
383
|
const missingIdentityInstructions = JSON.stringify({
|
|
343
384
|
action: "probe_identity_then_session",
|
|
344
385
|
steps: [
|
|
@@ -363,6 +404,10 @@ function createAgentScoreCore(options) {
|
|
|
363
404
|
if (cached.allow) {
|
|
364
405
|
return { kind: "allow", data: cached.raw };
|
|
365
406
|
}
|
|
407
|
+
if (isFixableDenial(cached.reasons)) {
|
|
408
|
+
const sessionReason = await tryMintSessionDenial(ctx);
|
|
409
|
+
if (sessionReason) return { kind: "deny", reason: sessionReason };
|
|
410
|
+
}
|
|
366
411
|
return {
|
|
367
412
|
kind: "deny",
|
|
368
413
|
reason: {
|
|
@@ -460,6 +505,10 @@ function createAgentScoreCore(options) {
|
|
|
460
505
|
if (allow) {
|
|
461
506
|
return { kind: "allow", data };
|
|
462
507
|
}
|
|
508
|
+
if (isFixableDenial(decisionReasons)) {
|
|
509
|
+
const sessionReason = await tryMintSessionDenial(ctx);
|
|
510
|
+
if (sessionReason) return { kind: "deny", reason: sessionReason };
|
|
511
|
+
}
|
|
463
512
|
return {
|
|
464
513
|
kind: "deny",
|
|
465
514
|
reason: {
|