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