@vorim/sdk 3.6.3 → 3.7.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.
- package/CHANGELOG.md +21 -0
- package/dist/index.cjs +7 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -5
- package/dist/index.d.ts +8 -5
- package/dist/index.js +7 -2
- package/dist/index.js.map +1 -1
- package/dist/integrations/siem.cjs +1 -1
- package/dist/integrations/siem.cjs.map +1 -1
- package/dist/integrations/siem.js +1 -1
- package/dist/integrations/siem.js.map +1 -1
- package/dist/integrations/ucp.cjs +224 -0
- package/dist/integrations/ucp.cjs.map +1 -0
- package/dist/integrations/ucp.d.cts +102 -0
- package/dist/integrations/ucp.d.ts +102 -0
- package/dist/integrations/ucp.js +198 -0
- package/dist/integrations/ucp.js.map +1 -0
- package/package.json +11 -1
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/integrations/ucp.ts
|
|
21
|
+
var ucp_exports = {};
|
|
22
|
+
__export(ucp_exports, {
|
|
23
|
+
VorimUCP: () => VorimUCP,
|
|
24
|
+
createVorimUCP: () => createVorimUCP
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(ucp_exports);
|
|
27
|
+
var VorimUCP = class {
|
|
28
|
+
sdk;
|
|
29
|
+
minTrustScore;
|
|
30
|
+
requiredScope;
|
|
31
|
+
constructor(sdk, config) {
|
|
32
|
+
this.sdk = sdk;
|
|
33
|
+
this.minTrustScore = config?.minTrustScore ?? 50;
|
|
34
|
+
this.requiredScope = config?.requiredScope ?? "agent:transact";
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Verify an agent is authorized to initiate a UCP purchase: it holds the
|
|
38
|
+
* transact scope, is active, and meets the trust-score threshold. Every
|
|
39
|
+
* decision (allow or deny) is recorded as a signed audit event.
|
|
40
|
+
*/
|
|
41
|
+
async authorizePurchase(req) {
|
|
42
|
+
const threshold = req.minTrustScore ?? this.minTrustScore;
|
|
43
|
+
try {
|
|
44
|
+
const perm = await this.sdk.check(req.agentId, this.requiredScope);
|
|
45
|
+
if (!perm.allowed) {
|
|
46
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "denied", req, { reason: `Missing ${this.requiredScope}` });
|
|
47
|
+
return {
|
|
48
|
+
authorized: false,
|
|
49
|
+
agentId: req.agentId,
|
|
50
|
+
trustScore: 0,
|
|
51
|
+
status: "unknown",
|
|
52
|
+
reason: `Agent does not have ${this.requiredScope} permission`,
|
|
53
|
+
scopeVerified: false
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
} catch (e) {
|
|
57
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "denied", req, { reason: e.message });
|
|
58
|
+
return {
|
|
59
|
+
authorized: false,
|
|
60
|
+
agentId: req.agentId,
|
|
61
|
+
trustScore: 0,
|
|
62
|
+
status: "unknown",
|
|
63
|
+
reason: `Permission check failed: ${e.message}`,
|
|
64
|
+
scopeVerified: false
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
let score = 0;
|
|
68
|
+
let status = "unknown";
|
|
69
|
+
try {
|
|
70
|
+
const trust = await this.sdk.verify(req.agentId);
|
|
71
|
+
score = trust.trust_score ?? 0;
|
|
72
|
+
status = trust.status ?? "unknown";
|
|
73
|
+
} catch {
|
|
74
|
+
}
|
|
75
|
+
if (status !== "active") {
|
|
76
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "denied", req, { reason: `Status: ${status}` });
|
|
77
|
+
return {
|
|
78
|
+
authorized: false,
|
|
79
|
+
agentId: req.agentId,
|
|
80
|
+
trustScore: score,
|
|
81
|
+
status,
|
|
82
|
+
reason: `Agent is ${status}, not active`,
|
|
83
|
+
scopeVerified: true
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
if (score < threshold) {
|
|
87
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "denied", req, { reason: `Score ${score} < ${threshold}` });
|
|
88
|
+
return {
|
|
89
|
+
authorized: false,
|
|
90
|
+
agentId: req.agentId,
|
|
91
|
+
trustScore: score,
|
|
92
|
+
status,
|
|
93
|
+
reason: `Trust score ${score} is below minimum ${threshold}`,
|
|
94
|
+
scopeVerified: true
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "success", req, { trust_score: score });
|
|
98
|
+
return {
|
|
99
|
+
authorized: true,
|
|
100
|
+
agentId: req.agentId,
|
|
101
|
+
trustScore: score,
|
|
102
|
+
status,
|
|
103
|
+
scopeVerified: true
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Record the UCP commerce lifecycle as signed, offline-verifiable audit
|
|
108
|
+
* events. The id passed is the UCP checkout-session / order reference the
|
|
109
|
+
* caller already holds (UCP spec 2026-04-08) — Vorim does not parse UCP
|
|
110
|
+
* payloads, it attests to them.
|
|
111
|
+
*/
|
|
112
|
+
async logCartCreated(agentId, ucpOrderId, merchant) {
|
|
113
|
+
await this.logEvent(agentId, "ucp.cart.created", "success", { agentId, merchant }, { ucp_order_id: ucpOrderId });
|
|
114
|
+
}
|
|
115
|
+
async logOrderPlaced(agentId, ucpOrderId, merchant, amount) {
|
|
116
|
+
await this.logEvent(agentId, "ucp.order.placed", "success", { agentId, merchant, amount }, { ucp_order_id: ucpOrderId });
|
|
117
|
+
}
|
|
118
|
+
async logOrderFulfilled(agentId, ucpOrderId, merchant) {
|
|
119
|
+
await this.logEvent(agentId, "ucp.order.fulfilled", "success", { agentId, merchant }, { ucp_order_id: ucpOrderId });
|
|
120
|
+
}
|
|
121
|
+
async logOrderCanceled(agentId, ucpOrderId, reason) {
|
|
122
|
+
await this.logEvent(agentId, "ucp.order.canceled", "success", { agentId }, { ucp_order_id: ucpOrderId, cancel_reason: reason });
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Record the outcome of a UCP checkout-session completion, keyed to the real
|
|
126
|
+
* UCP status vocabulary. The spec's `complete` response carries a status of
|
|
127
|
+
* `completed` | `incomplete` | `requires_escalation` | `error`; this maps it
|
|
128
|
+
* to the audit `result` so an outside party can verify the agent's purchase
|
|
129
|
+
* outcome. `requires_escalation` is recorded as a denial pending human
|
|
130
|
+
* approval — the natural hand-off point for Vorim runtime control.
|
|
131
|
+
*/
|
|
132
|
+
async logCheckoutSession(agentId, sessionId, status, opts) {
|
|
133
|
+
const result = status === "completed" ? "success" : status === "error" ? "error" : "denied";
|
|
134
|
+
await this.logEvent(
|
|
135
|
+
agentId,
|
|
136
|
+
"ucp.checkout_session.complete",
|
|
137
|
+
result,
|
|
138
|
+
{ agentId, merchant: opts?.merchant, amount: opts?.amount },
|
|
139
|
+
{ ucp_order_id: sessionId, ucp_status: status }
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Record the AP2 payment mandate (UCP's `dev.ucp.shopping.ap2_mandate`
|
|
144
|
+
* extension) for an autonomous purchase. The `checkout_mandate` is a signed
|
|
145
|
+
* JWT proof that the agent was authorised to pay; capturing it in the Vorim
|
|
146
|
+
* audit links UCP's own cryptographic payment proof to Vorim's signed,
|
|
147
|
+
* independently-verifiable record of the action.
|
|
148
|
+
*/
|
|
149
|
+
async logAp2Mandate(agentId, sessionId, checkoutMandate, opts) {
|
|
150
|
+
await this.logEvent(
|
|
151
|
+
agentId,
|
|
152
|
+
"ucp.ap2.mandate",
|
|
153
|
+
"success",
|
|
154
|
+
{ agentId, merchant: opts?.merchant, amount: opts?.amount },
|
|
155
|
+
{ ucp_order_id: sessionId, ap2_checkout_mandate: checkoutMandate }
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Express/Connect middleware that gates a UCP checkout endpoint on agent
|
|
160
|
+
* authorization. Expects the agent id in the `x-vorim-agent-id` header
|
|
161
|
+
* (configurable) and the merchant/amount in the request body.
|
|
162
|
+
*/
|
|
163
|
+
middleware(options) {
|
|
164
|
+
const header = options?.agentIdHeader ?? "x-vorim-agent-id";
|
|
165
|
+
const threshold = options?.minTrustScore ?? this.minTrustScore;
|
|
166
|
+
return async (req, res, next) => {
|
|
167
|
+
const agentId = req.headers?.[header] || req.headers?.[header.toLowerCase()] || "";
|
|
168
|
+
if (!agentId) {
|
|
169
|
+
res.status(401).json({
|
|
170
|
+
error: { code: "MISSING_AGENT_ID", message: `Agent ID required in ${header} header` }
|
|
171
|
+
});
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const auth = await this.authorizePurchase({
|
|
175
|
+
agentId,
|
|
176
|
+
merchant: req.body?.merchant || req.body?.seller || "",
|
|
177
|
+
amount: req.body?.amount || 0,
|
|
178
|
+
currency: req.body?.currency || "usd",
|
|
179
|
+
minTrustScore: threshold
|
|
180
|
+
});
|
|
181
|
+
if (!auth.authorized) {
|
|
182
|
+
res.status(403).json({
|
|
183
|
+
error: {
|
|
184
|
+
code: "UNAUTHORIZED_AGENT",
|
|
185
|
+
message: auth.reason || "Agent not authorized for UCP transactions",
|
|
186
|
+
trust_score: auth.trustScore,
|
|
187
|
+
status: auth.status
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
req.vorimAuthorization = auth;
|
|
193
|
+
next();
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
async logEvent(agentId, action, result, req, metadata) {
|
|
197
|
+
try {
|
|
198
|
+
await this.sdk.emit({
|
|
199
|
+
agent_id: agentId,
|
|
200
|
+
event_type: "api_request",
|
|
201
|
+
action,
|
|
202
|
+
result,
|
|
203
|
+
resource: req?.merchant || "",
|
|
204
|
+
metadata: {
|
|
205
|
+
protocol: "ucp",
|
|
206
|
+
merchant: req?.merchant || "",
|
|
207
|
+
amount: req?.amount || 0,
|
|
208
|
+
currency: req?.currency || "",
|
|
209
|
+
...metadata
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
} catch {
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
function createVorimUCP(sdk, config) {
|
|
217
|
+
return new VorimUCP(sdk, config);
|
|
218
|
+
}
|
|
219
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
220
|
+
0 && (module.exports = {
|
|
221
|
+
VorimUCP,
|
|
222
|
+
createVorimUCP
|
|
223
|
+
});
|
|
224
|
+
//# sourceMappingURL=ucp.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/ucp.ts"],"sourcesContent":["// ============================================================================\n// Vorim AI — Universal Commerce Protocol (UCP) Integration\n//\n// UCP is Google's open agentic-commerce protocol (announced Jan 2026, NRF). It\n// standardises how an AI agent transacts with a merchant across the shopping\n// journey — discovery, checkout, payment (via AP2), and order management — over\n// REST / JSON-RPC, with A2A and MCP bindings.\n//\n// What UCP does NOT answer is the question Vorim exists to answer: *which* agent\n// is this, is it *allowed* to spend on this merchant for this amount, and can you\n// produce a tamper-evident record of what it bought that a counterparty can\n// verify independently. This integration adds exactly that layer around the UCP\n// commerce lifecycle.\n//\n// Design note: this wrapper is deliberately agnostic about UCP's internal wire\n// payloads. It operates on the commerce facts Vorim needs — agent id, merchant,\n// amount, currency, and an opaque UCP order/checkout reference — so it stays\n// correct as the UCP schema evolves. It does not invent UCP field names; the\n// caller passes the UCP order id it already holds. Where it does touch\n// UCP-specific vocabulary (the checkout-session status values and the\n// `ap2_mandate` extension's `checkout_mandate`), it matches the UCP spec,\n// version 2026-04-08. Spec: https://ucp.dev/latest/specification/overview/\n//\n// Usage:\n// import createVorim from '@vorim/sdk';\n// import { createVorimUCP } from '@vorim/sdk/integrations/ucp';\n//\n// const vorim = createVorim({ apiKey: 'agid_sk_...' });\n// const ucp = createVorimUCP(vorim, { minTrustScore: 60 });\n//\n// // Before an agent initiates a UCP checkout:\n// const auth = await ucp.authorizePurchase({\n// agentId: 'agid_abc123',\n// merchant: 'acme-store',\n// amount: 4999,\n// currency: 'usd',\n// });\n// if (!auth.authorized) throw new Error(auth.reason);\n//\n// // ...run the UCP checkout, then record the signed audit trail:\n// await ucp.logOrderPlaced('agid_abc123', ucpOrderId, 'acme-store', 4999);\n// await ucp.logOrderFulfilled('agid_abc123', ucpOrderId);\n// ============================================================================\n\nimport type { VorimSDK } from '../index.js';\n\nexport interface UCPConfig {\n apiKey: string;\n baseUrl?: string;\n minTrustScore?: number;\n requiredScope?: string;\n}\n\nexport interface PurchaseAuthorizationRequest {\n agentId: string;\n /** UCP merchant / seller identifier the agent is transacting with. */\n merchant?: string;\n /** Amount in the smallest currency unit (e.g. cents), as UCP/AP2 uses. */\n amount?: number;\n currency?: string;\n minTrustScore?: number;\n}\n\nexport interface PurchaseAuthorization {\n authorized: boolean;\n agentId: string;\n trustScore: number;\n status: string;\n reason?: string;\n scopeVerified: boolean;\n}\n\nexport class VorimUCP {\n private sdk: VorimSDK;\n private minTrustScore: number;\n private requiredScope: string;\n\n constructor(sdk: VorimSDK, config?: Partial<UCPConfig>) {\n this.sdk = sdk;\n this.minTrustScore = config?.minTrustScore ?? 50;\n this.requiredScope = config?.requiredScope ?? 'agent:transact';\n }\n\n /**\n * Verify an agent is authorized to initiate a UCP purchase: it holds the\n * transact scope, is active, and meets the trust-score threshold. Every\n * decision (allow or deny) is recorded as a signed audit event.\n */\n async authorizePurchase(req: PurchaseAuthorizationRequest): Promise<PurchaseAuthorization> {\n const threshold = req.minTrustScore ?? this.minTrustScore;\n\n // 1. Permission: agent must hold the transact scope.\n try {\n const perm = await this.sdk.check(req.agentId, this.requiredScope as any);\n if (!(perm as any).allowed) {\n await this.logEvent(req.agentId, 'ucp.purchase.authorize', 'denied', req, { reason: `Missing ${this.requiredScope}` });\n return {\n authorized: false,\n agentId: req.agentId,\n trustScore: 0,\n status: 'unknown',\n reason: `Agent does not have ${this.requiredScope} permission`,\n scopeVerified: false,\n };\n }\n } catch (e: any) {\n await this.logEvent(req.agentId, 'ucp.purchase.authorize', 'denied', req, { reason: e.message });\n return {\n authorized: false,\n agentId: req.agentId,\n trustScore: 0,\n status: 'unknown',\n reason: `Permission check failed: ${e.message}`,\n scopeVerified: false,\n };\n }\n\n // 2. Trust: agent must be active and above the score threshold.\n let score = 0;\n let status = 'unknown';\n try {\n const trust = await this.sdk.verify(req.agentId);\n score = (trust as any).trust_score ?? 0;\n status = (trust as any).status ?? 'unknown';\n } catch {\n // Trust verification failed; treated as not-active below.\n }\n\n if (status !== 'active') {\n await this.logEvent(req.agentId, 'ucp.purchase.authorize', 'denied', req, { reason: `Status: ${status}` });\n return {\n authorized: false,\n agentId: req.agentId,\n trustScore: score,\n status,\n reason: `Agent is ${status}, not active`,\n scopeVerified: true,\n };\n }\n\n if (score < threshold) {\n await this.logEvent(req.agentId, 'ucp.purchase.authorize', 'denied', req, { reason: `Score ${score} < ${threshold}` });\n return {\n authorized: false,\n agentId: req.agentId,\n trustScore: score,\n status,\n reason: `Trust score ${score} is below minimum ${threshold}`,\n scopeVerified: true,\n };\n }\n\n await this.logEvent(req.agentId, 'ucp.purchase.authorize', 'success', req, { trust_score: score });\n\n return {\n authorized: true,\n agentId: req.agentId,\n trustScore: score,\n status,\n scopeVerified: true,\n };\n }\n\n /**\n * Record the UCP commerce lifecycle as signed, offline-verifiable audit\n * events. The id passed is the UCP checkout-session / order reference the\n * caller already holds (UCP spec 2026-04-08) — Vorim does not parse UCP\n * payloads, it attests to them.\n */\n async logCartCreated(agentId: string, ucpOrderId: string, merchant?: string): Promise<void> {\n await this.logEvent(agentId, 'ucp.cart.created', 'success', { agentId, merchant }, { ucp_order_id: ucpOrderId });\n }\n\n async logOrderPlaced(agentId: string, ucpOrderId: string, merchant?: string, amount?: number): Promise<void> {\n await this.logEvent(agentId, 'ucp.order.placed', 'success', { agentId, merchant, amount }, { ucp_order_id: ucpOrderId });\n }\n\n async logOrderFulfilled(agentId: string, ucpOrderId: string, merchant?: string): Promise<void> {\n await this.logEvent(agentId, 'ucp.order.fulfilled', 'success', { agentId, merchant }, { ucp_order_id: ucpOrderId });\n }\n\n async logOrderCanceled(agentId: string, ucpOrderId: string, reason?: string): Promise<void> {\n await this.logEvent(agentId, 'ucp.order.canceled', 'success', { agentId }, { ucp_order_id: ucpOrderId, cancel_reason: reason });\n }\n\n /**\n * Record the outcome of a UCP checkout-session completion, keyed to the real\n * UCP status vocabulary. The spec's `complete` response carries a status of\n * `completed` | `incomplete` | `requires_escalation` | `error`; this maps it\n * to the audit `result` so an outside party can verify the agent's purchase\n * outcome. `requires_escalation` is recorded as a denial pending human\n * approval — the natural hand-off point for Vorim runtime control.\n */\n async logCheckoutSession(\n agentId: string,\n sessionId: string,\n status: 'completed' | 'incomplete' | 'requires_escalation' | 'error',\n opts?: { merchant?: string; amount?: number },\n ): Promise<void> {\n const result: 'success' | 'denied' | 'error' =\n status === 'completed' ? 'success' : status === 'error' ? 'error' : 'denied';\n await this.logEvent(\n agentId,\n 'ucp.checkout_session.complete',\n result,\n { agentId, merchant: opts?.merchant, amount: opts?.amount },\n { ucp_order_id: sessionId, ucp_status: status },\n );\n }\n\n /**\n * Record the AP2 payment mandate (UCP's `dev.ucp.shopping.ap2_mandate`\n * extension) for an autonomous purchase. The `checkout_mandate` is a signed\n * JWT proof that the agent was authorised to pay; capturing it in the Vorim\n * audit links UCP's own cryptographic payment proof to Vorim's signed,\n * independently-verifiable record of the action.\n */\n async logAp2Mandate(\n agentId: string,\n sessionId: string,\n checkoutMandate: string,\n opts?: { merchant?: string; amount?: number },\n ): Promise<void> {\n await this.logEvent(\n agentId,\n 'ucp.ap2.mandate',\n 'success',\n { agentId, merchant: opts?.merchant, amount: opts?.amount },\n { ucp_order_id: sessionId, ap2_checkout_mandate: checkoutMandate },\n );\n }\n\n /**\n * Express/Connect middleware that gates a UCP checkout endpoint on agent\n * authorization. Expects the agent id in the `x-vorim-agent-id` header\n * (configurable) and the merchant/amount in the request body.\n */\n middleware(options?: { agentIdHeader?: string; minTrustScore?: number }) {\n const header = options?.agentIdHeader ?? 'x-vorim-agent-id';\n const threshold = options?.minTrustScore ?? this.minTrustScore;\n\n return async (req: any, res: any, next: any) => {\n const agentId = req.headers?.[header] || req.headers?.[header.toLowerCase()] || '';\n\n if (!agentId) {\n res.status(401).json({\n error: { code: 'MISSING_AGENT_ID', message: `Agent ID required in ${header} header` },\n });\n return;\n }\n\n const auth = await this.authorizePurchase({\n agentId,\n merchant: req.body?.merchant || req.body?.seller || '',\n amount: req.body?.amount || 0,\n currency: req.body?.currency || 'usd',\n minTrustScore: threshold,\n });\n\n if (!auth.authorized) {\n res.status(403).json({\n error: {\n code: 'UNAUTHORIZED_AGENT',\n message: auth.reason || 'Agent not authorized for UCP transactions',\n trust_score: auth.trustScore,\n status: auth.status,\n },\n });\n return;\n }\n\n req.vorimAuthorization = auth;\n next();\n };\n }\n\n private async logEvent(\n agentId: string,\n action: string,\n result: 'success' | 'denied' | 'error',\n req?: any,\n metadata?: Record<string, any>,\n ): Promise<void> {\n try {\n // Canonical audit contract: event_type from the AuditEventType enum\n // ('api_request' for a commerce action) and `result` (not `outcome`)\n // from the AuditResult enum.\n await this.sdk.emit({\n agent_id: agentId,\n event_type: 'api_request',\n action,\n result,\n resource: req?.merchant || '',\n metadata: {\n protocol: 'ucp',\n merchant: req?.merchant || '',\n amount: req?.amount || 0,\n currency: req?.currency || '',\n ...metadata,\n },\n });\n } catch {\n // Never block the commerce flow on an audit failure.\n }\n }\n}\n\n/**\n * Create a VorimUCP instance.\n *\n * @example\n * ```typescript\n * import createVorim from '@vorim/sdk';\n * import { createVorimUCP } from '@vorim/sdk/integrations/ucp';\n *\n * const vorim = createVorim({ apiKey: 'agid_sk_...' });\n * const ucp = createVorimUCP(vorim, { minTrustScore: 70 });\n *\n * // Verify before a UCP checkout\n * const auth = await ucp.authorizePurchase({ agentId: 'agid_abc123', merchant: 'shop', amount: 4999 });\n *\n * // Express middleware on your UCP checkout route\n * app.post('/ucp/checkout', ucp.middleware(), createCheckoutHandler);\n * ```\n */\nexport function createVorimUCP(sdk: VorimSDK, config?: Partial<UCPConfig>): VorimUCP {\n return new VorimUCP(sdk, config);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAwEO,IAAM,WAAN,MAAe;AAAA,EACZ;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,KAAe,QAA6B;AACtD,SAAK,MAAM;AACX,SAAK,gBAAgB,QAAQ,iBAAiB;AAC9C,SAAK,gBAAgB,QAAQ,iBAAiB;AAAA,EAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,kBAAkB,KAAmE;AACzF,UAAM,YAAY,IAAI,iBAAiB,KAAK;AAG5C,QAAI;AACF,YAAM,OAAO,MAAM,KAAK,IAAI,MAAM,IAAI,SAAS,KAAK,aAAoB;AACxE,UAAI,CAAE,KAAa,SAAS;AAC1B,cAAM,KAAK,SAAS,IAAI,SAAS,0BAA0B,UAAU,KAAK,EAAE,QAAQ,WAAW,KAAK,aAAa,GAAG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Z;AAAA,QACA;AAAA,QACA,UAAU,KAAK,YAAY;AAAA,QAC3B,UAAU;AAAA,UACR,UAAU;AAAA,UACV,UAAU,KAAK,YAAY;AAAA,UAC3B,QAAQ,KAAK,UAAU;AAAA,UACvB,UAAU,KAAK,YAAY;AAAA,UAC3B,GAAG;AAAA,QACL;AAAA,MACF,CAAC;AAAA,IACH,QAAQ;AAAA,IAER;AAAA,EACF;AACF;AAoBO,SAAS,eAAe,KAAe,QAAuC;AACnF,SAAO,IAAI,SAAS,KAAK,MAAM;AACjC;","names":[]}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { VorimSDK } from '../index.cjs';
|
|
2
|
+
import '../types-B22WnXEW.cjs';
|
|
3
|
+
|
|
4
|
+
interface UCPConfig {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
minTrustScore?: number;
|
|
8
|
+
requiredScope?: string;
|
|
9
|
+
}
|
|
10
|
+
interface PurchaseAuthorizationRequest {
|
|
11
|
+
agentId: string;
|
|
12
|
+
/** UCP merchant / seller identifier the agent is transacting with. */
|
|
13
|
+
merchant?: string;
|
|
14
|
+
/** Amount in the smallest currency unit (e.g. cents), as UCP/AP2 uses. */
|
|
15
|
+
amount?: number;
|
|
16
|
+
currency?: string;
|
|
17
|
+
minTrustScore?: number;
|
|
18
|
+
}
|
|
19
|
+
interface PurchaseAuthorization {
|
|
20
|
+
authorized: boolean;
|
|
21
|
+
agentId: string;
|
|
22
|
+
trustScore: number;
|
|
23
|
+
status: string;
|
|
24
|
+
reason?: string;
|
|
25
|
+
scopeVerified: boolean;
|
|
26
|
+
}
|
|
27
|
+
declare class VorimUCP {
|
|
28
|
+
private sdk;
|
|
29
|
+
private minTrustScore;
|
|
30
|
+
private requiredScope;
|
|
31
|
+
constructor(sdk: VorimSDK, config?: Partial<UCPConfig>);
|
|
32
|
+
/**
|
|
33
|
+
* Verify an agent is authorized to initiate a UCP purchase: it holds the
|
|
34
|
+
* transact scope, is active, and meets the trust-score threshold. Every
|
|
35
|
+
* decision (allow or deny) is recorded as a signed audit event.
|
|
36
|
+
*/
|
|
37
|
+
authorizePurchase(req: PurchaseAuthorizationRequest): Promise<PurchaseAuthorization>;
|
|
38
|
+
/**
|
|
39
|
+
* Record the UCP commerce lifecycle as signed, offline-verifiable audit
|
|
40
|
+
* events. The id passed is the UCP checkout-session / order reference the
|
|
41
|
+
* caller already holds (UCP spec 2026-04-08) — Vorim does not parse UCP
|
|
42
|
+
* payloads, it attests to them.
|
|
43
|
+
*/
|
|
44
|
+
logCartCreated(agentId: string, ucpOrderId: string, merchant?: string): Promise<void>;
|
|
45
|
+
logOrderPlaced(agentId: string, ucpOrderId: string, merchant?: string, amount?: number): Promise<void>;
|
|
46
|
+
logOrderFulfilled(agentId: string, ucpOrderId: string, merchant?: string): Promise<void>;
|
|
47
|
+
logOrderCanceled(agentId: string, ucpOrderId: string, reason?: string): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Record the outcome of a UCP checkout-session completion, keyed to the real
|
|
50
|
+
* UCP status vocabulary. The spec's `complete` response carries a status of
|
|
51
|
+
* `completed` | `incomplete` | `requires_escalation` | `error`; this maps it
|
|
52
|
+
* to the audit `result` so an outside party can verify the agent's purchase
|
|
53
|
+
* outcome. `requires_escalation` is recorded as a denial pending human
|
|
54
|
+
* approval — the natural hand-off point for Vorim runtime control.
|
|
55
|
+
*/
|
|
56
|
+
logCheckoutSession(agentId: string, sessionId: string, status: 'completed' | 'incomplete' | 'requires_escalation' | 'error', opts?: {
|
|
57
|
+
merchant?: string;
|
|
58
|
+
amount?: number;
|
|
59
|
+
}): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Record the AP2 payment mandate (UCP's `dev.ucp.shopping.ap2_mandate`
|
|
62
|
+
* extension) for an autonomous purchase. The `checkout_mandate` is a signed
|
|
63
|
+
* JWT proof that the agent was authorised to pay; capturing it in the Vorim
|
|
64
|
+
* audit links UCP's own cryptographic payment proof to Vorim's signed,
|
|
65
|
+
* independently-verifiable record of the action.
|
|
66
|
+
*/
|
|
67
|
+
logAp2Mandate(agentId: string, sessionId: string, checkoutMandate: string, opts?: {
|
|
68
|
+
merchant?: string;
|
|
69
|
+
amount?: number;
|
|
70
|
+
}): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Express/Connect middleware that gates a UCP checkout endpoint on agent
|
|
73
|
+
* authorization. Expects the agent id in the `x-vorim-agent-id` header
|
|
74
|
+
* (configurable) and the merchant/amount in the request body.
|
|
75
|
+
*/
|
|
76
|
+
middleware(options?: {
|
|
77
|
+
agentIdHeader?: string;
|
|
78
|
+
minTrustScore?: number;
|
|
79
|
+
}): (req: any, res: any, next: any) => Promise<void>;
|
|
80
|
+
private logEvent;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Create a VorimUCP instance.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import createVorim from '@vorim/sdk';
|
|
88
|
+
* import { createVorimUCP } from '@vorim/sdk/integrations/ucp';
|
|
89
|
+
*
|
|
90
|
+
* const vorim = createVorim({ apiKey: 'agid_sk_...' });
|
|
91
|
+
* const ucp = createVorimUCP(vorim, { minTrustScore: 70 });
|
|
92
|
+
*
|
|
93
|
+
* // Verify before a UCP checkout
|
|
94
|
+
* const auth = await ucp.authorizePurchase({ agentId: 'agid_abc123', merchant: 'shop', amount: 4999 });
|
|
95
|
+
*
|
|
96
|
+
* // Express middleware on your UCP checkout route
|
|
97
|
+
* app.post('/ucp/checkout', ucp.middleware(), createCheckoutHandler);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function createVorimUCP(sdk: VorimSDK, config?: Partial<UCPConfig>): VorimUCP;
|
|
101
|
+
|
|
102
|
+
export { type PurchaseAuthorization, type PurchaseAuthorizationRequest, type UCPConfig, VorimUCP, createVorimUCP };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { VorimSDK } from '../index.js';
|
|
2
|
+
import '../types-B22WnXEW.js';
|
|
3
|
+
|
|
4
|
+
interface UCPConfig {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
baseUrl?: string;
|
|
7
|
+
minTrustScore?: number;
|
|
8
|
+
requiredScope?: string;
|
|
9
|
+
}
|
|
10
|
+
interface PurchaseAuthorizationRequest {
|
|
11
|
+
agentId: string;
|
|
12
|
+
/** UCP merchant / seller identifier the agent is transacting with. */
|
|
13
|
+
merchant?: string;
|
|
14
|
+
/** Amount in the smallest currency unit (e.g. cents), as UCP/AP2 uses. */
|
|
15
|
+
amount?: number;
|
|
16
|
+
currency?: string;
|
|
17
|
+
minTrustScore?: number;
|
|
18
|
+
}
|
|
19
|
+
interface PurchaseAuthorization {
|
|
20
|
+
authorized: boolean;
|
|
21
|
+
agentId: string;
|
|
22
|
+
trustScore: number;
|
|
23
|
+
status: string;
|
|
24
|
+
reason?: string;
|
|
25
|
+
scopeVerified: boolean;
|
|
26
|
+
}
|
|
27
|
+
declare class VorimUCP {
|
|
28
|
+
private sdk;
|
|
29
|
+
private minTrustScore;
|
|
30
|
+
private requiredScope;
|
|
31
|
+
constructor(sdk: VorimSDK, config?: Partial<UCPConfig>);
|
|
32
|
+
/**
|
|
33
|
+
* Verify an agent is authorized to initiate a UCP purchase: it holds the
|
|
34
|
+
* transact scope, is active, and meets the trust-score threshold. Every
|
|
35
|
+
* decision (allow or deny) is recorded as a signed audit event.
|
|
36
|
+
*/
|
|
37
|
+
authorizePurchase(req: PurchaseAuthorizationRequest): Promise<PurchaseAuthorization>;
|
|
38
|
+
/**
|
|
39
|
+
* Record the UCP commerce lifecycle as signed, offline-verifiable audit
|
|
40
|
+
* events. The id passed is the UCP checkout-session / order reference the
|
|
41
|
+
* caller already holds (UCP spec 2026-04-08) — Vorim does not parse UCP
|
|
42
|
+
* payloads, it attests to them.
|
|
43
|
+
*/
|
|
44
|
+
logCartCreated(agentId: string, ucpOrderId: string, merchant?: string): Promise<void>;
|
|
45
|
+
logOrderPlaced(agentId: string, ucpOrderId: string, merchant?: string, amount?: number): Promise<void>;
|
|
46
|
+
logOrderFulfilled(agentId: string, ucpOrderId: string, merchant?: string): Promise<void>;
|
|
47
|
+
logOrderCanceled(agentId: string, ucpOrderId: string, reason?: string): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Record the outcome of a UCP checkout-session completion, keyed to the real
|
|
50
|
+
* UCP status vocabulary. The spec's `complete` response carries a status of
|
|
51
|
+
* `completed` | `incomplete` | `requires_escalation` | `error`; this maps it
|
|
52
|
+
* to the audit `result` so an outside party can verify the agent's purchase
|
|
53
|
+
* outcome. `requires_escalation` is recorded as a denial pending human
|
|
54
|
+
* approval — the natural hand-off point for Vorim runtime control.
|
|
55
|
+
*/
|
|
56
|
+
logCheckoutSession(agentId: string, sessionId: string, status: 'completed' | 'incomplete' | 'requires_escalation' | 'error', opts?: {
|
|
57
|
+
merchant?: string;
|
|
58
|
+
amount?: number;
|
|
59
|
+
}): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Record the AP2 payment mandate (UCP's `dev.ucp.shopping.ap2_mandate`
|
|
62
|
+
* extension) for an autonomous purchase. The `checkout_mandate` is a signed
|
|
63
|
+
* JWT proof that the agent was authorised to pay; capturing it in the Vorim
|
|
64
|
+
* audit links UCP's own cryptographic payment proof to Vorim's signed,
|
|
65
|
+
* independently-verifiable record of the action.
|
|
66
|
+
*/
|
|
67
|
+
logAp2Mandate(agentId: string, sessionId: string, checkoutMandate: string, opts?: {
|
|
68
|
+
merchant?: string;
|
|
69
|
+
amount?: number;
|
|
70
|
+
}): Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Express/Connect middleware that gates a UCP checkout endpoint on agent
|
|
73
|
+
* authorization. Expects the agent id in the `x-vorim-agent-id` header
|
|
74
|
+
* (configurable) and the merchant/amount in the request body.
|
|
75
|
+
*/
|
|
76
|
+
middleware(options?: {
|
|
77
|
+
agentIdHeader?: string;
|
|
78
|
+
minTrustScore?: number;
|
|
79
|
+
}): (req: any, res: any, next: any) => Promise<void>;
|
|
80
|
+
private logEvent;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Create a VorimUCP instance.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* import createVorim from '@vorim/sdk';
|
|
88
|
+
* import { createVorimUCP } from '@vorim/sdk/integrations/ucp';
|
|
89
|
+
*
|
|
90
|
+
* const vorim = createVorim({ apiKey: 'agid_sk_...' });
|
|
91
|
+
* const ucp = createVorimUCP(vorim, { minTrustScore: 70 });
|
|
92
|
+
*
|
|
93
|
+
* // Verify before a UCP checkout
|
|
94
|
+
* const auth = await ucp.authorizePurchase({ agentId: 'agid_abc123', merchant: 'shop', amount: 4999 });
|
|
95
|
+
*
|
|
96
|
+
* // Express middleware on your UCP checkout route
|
|
97
|
+
* app.post('/ucp/checkout', ucp.middleware(), createCheckoutHandler);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
declare function createVorimUCP(sdk: VorimSDK, config?: Partial<UCPConfig>): VorimUCP;
|
|
101
|
+
|
|
102
|
+
export { type PurchaseAuthorization, type PurchaseAuthorizationRequest, type UCPConfig, VorimUCP, createVorimUCP };
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// src/integrations/ucp.ts
|
|
2
|
+
var VorimUCP = class {
|
|
3
|
+
sdk;
|
|
4
|
+
minTrustScore;
|
|
5
|
+
requiredScope;
|
|
6
|
+
constructor(sdk, config) {
|
|
7
|
+
this.sdk = sdk;
|
|
8
|
+
this.minTrustScore = config?.minTrustScore ?? 50;
|
|
9
|
+
this.requiredScope = config?.requiredScope ?? "agent:transact";
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Verify an agent is authorized to initiate a UCP purchase: it holds the
|
|
13
|
+
* transact scope, is active, and meets the trust-score threshold. Every
|
|
14
|
+
* decision (allow or deny) is recorded as a signed audit event.
|
|
15
|
+
*/
|
|
16
|
+
async authorizePurchase(req) {
|
|
17
|
+
const threshold = req.minTrustScore ?? this.minTrustScore;
|
|
18
|
+
try {
|
|
19
|
+
const perm = await this.sdk.check(req.agentId, this.requiredScope);
|
|
20
|
+
if (!perm.allowed) {
|
|
21
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "denied", req, { reason: `Missing ${this.requiredScope}` });
|
|
22
|
+
return {
|
|
23
|
+
authorized: false,
|
|
24
|
+
agentId: req.agentId,
|
|
25
|
+
trustScore: 0,
|
|
26
|
+
status: "unknown",
|
|
27
|
+
reason: `Agent does not have ${this.requiredScope} permission`,
|
|
28
|
+
scopeVerified: false
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
} catch (e) {
|
|
32
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "denied", req, { reason: e.message });
|
|
33
|
+
return {
|
|
34
|
+
authorized: false,
|
|
35
|
+
agentId: req.agentId,
|
|
36
|
+
trustScore: 0,
|
|
37
|
+
status: "unknown",
|
|
38
|
+
reason: `Permission check failed: ${e.message}`,
|
|
39
|
+
scopeVerified: false
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
let score = 0;
|
|
43
|
+
let status = "unknown";
|
|
44
|
+
try {
|
|
45
|
+
const trust = await this.sdk.verify(req.agentId);
|
|
46
|
+
score = trust.trust_score ?? 0;
|
|
47
|
+
status = trust.status ?? "unknown";
|
|
48
|
+
} catch {
|
|
49
|
+
}
|
|
50
|
+
if (status !== "active") {
|
|
51
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "denied", req, { reason: `Status: ${status}` });
|
|
52
|
+
return {
|
|
53
|
+
authorized: false,
|
|
54
|
+
agentId: req.agentId,
|
|
55
|
+
trustScore: score,
|
|
56
|
+
status,
|
|
57
|
+
reason: `Agent is ${status}, not active`,
|
|
58
|
+
scopeVerified: true
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (score < threshold) {
|
|
62
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "denied", req, { reason: `Score ${score} < ${threshold}` });
|
|
63
|
+
return {
|
|
64
|
+
authorized: false,
|
|
65
|
+
agentId: req.agentId,
|
|
66
|
+
trustScore: score,
|
|
67
|
+
status,
|
|
68
|
+
reason: `Trust score ${score} is below minimum ${threshold}`,
|
|
69
|
+
scopeVerified: true
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
await this.logEvent(req.agentId, "ucp.purchase.authorize", "success", req, { trust_score: score });
|
|
73
|
+
return {
|
|
74
|
+
authorized: true,
|
|
75
|
+
agentId: req.agentId,
|
|
76
|
+
trustScore: score,
|
|
77
|
+
status,
|
|
78
|
+
scopeVerified: true
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Record the UCP commerce lifecycle as signed, offline-verifiable audit
|
|
83
|
+
* events. The id passed is the UCP checkout-session / order reference the
|
|
84
|
+
* caller already holds (UCP spec 2026-04-08) — Vorim does not parse UCP
|
|
85
|
+
* payloads, it attests to them.
|
|
86
|
+
*/
|
|
87
|
+
async logCartCreated(agentId, ucpOrderId, merchant) {
|
|
88
|
+
await this.logEvent(agentId, "ucp.cart.created", "success", { agentId, merchant }, { ucp_order_id: ucpOrderId });
|
|
89
|
+
}
|
|
90
|
+
async logOrderPlaced(agentId, ucpOrderId, merchant, amount) {
|
|
91
|
+
await this.logEvent(agentId, "ucp.order.placed", "success", { agentId, merchant, amount }, { ucp_order_id: ucpOrderId });
|
|
92
|
+
}
|
|
93
|
+
async logOrderFulfilled(agentId, ucpOrderId, merchant) {
|
|
94
|
+
await this.logEvent(agentId, "ucp.order.fulfilled", "success", { agentId, merchant }, { ucp_order_id: ucpOrderId });
|
|
95
|
+
}
|
|
96
|
+
async logOrderCanceled(agentId, ucpOrderId, reason) {
|
|
97
|
+
await this.logEvent(agentId, "ucp.order.canceled", "success", { agentId }, { ucp_order_id: ucpOrderId, cancel_reason: reason });
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Record the outcome of a UCP checkout-session completion, keyed to the real
|
|
101
|
+
* UCP status vocabulary. The spec's `complete` response carries a status of
|
|
102
|
+
* `completed` | `incomplete` | `requires_escalation` | `error`; this maps it
|
|
103
|
+
* to the audit `result` so an outside party can verify the agent's purchase
|
|
104
|
+
* outcome. `requires_escalation` is recorded as a denial pending human
|
|
105
|
+
* approval — the natural hand-off point for Vorim runtime control.
|
|
106
|
+
*/
|
|
107
|
+
async logCheckoutSession(agentId, sessionId, status, opts) {
|
|
108
|
+
const result = status === "completed" ? "success" : status === "error" ? "error" : "denied";
|
|
109
|
+
await this.logEvent(
|
|
110
|
+
agentId,
|
|
111
|
+
"ucp.checkout_session.complete",
|
|
112
|
+
result,
|
|
113
|
+
{ agentId, merchant: opts?.merchant, amount: opts?.amount },
|
|
114
|
+
{ ucp_order_id: sessionId, ucp_status: status }
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Record the AP2 payment mandate (UCP's `dev.ucp.shopping.ap2_mandate`
|
|
119
|
+
* extension) for an autonomous purchase. The `checkout_mandate` is a signed
|
|
120
|
+
* JWT proof that the agent was authorised to pay; capturing it in the Vorim
|
|
121
|
+
* audit links UCP's own cryptographic payment proof to Vorim's signed,
|
|
122
|
+
* independently-verifiable record of the action.
|
|
123
|
+
*/
|
|
124
|
+
async logAp2Mandate(agentId, sessionId, checkoutMandate, opts) {
|
|
125
|
+
await this.logEvent(
|
|
126
|
+
agentId,
|
|
127
|
+
"ucp.ap2.mandate",
|
|
128
|
+
"success",
|
|
129
|
+
{ agentId, merchant: opts?.merchant, amount: opts?.amount },
|
|
130
|
+
{ ucp_order_id: sessionId, ap2_checkout_mandate: checkoutMandate }
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Express/Connect middleware that gates a UCP checkout endpoint on agent
|
|
135
|
+
* authorization. Expects the agent id in the `x-vorim-agent-id` header
|
|
136
|
+
* (configurable) and the merchant/amount in the request body.
|
|
137
|
+
*/
|
|
138
|
+
middleware(options) {
|
|
139
|
+
const header = options?.agentIdHeader ?? "x-vorim-agent-id";
|
|
140
|
+
const threshold = options?.minTrustScore ?? this.minTrustScore;
|
|
141
|
+
return async (req, res, next) => {
|
|
142
|
+
const agentId = req.headers?.[header] || req.headers?.[header.toLowerCase()] || "";
|
|
143
|
+
if (!agentId) {
|
|
144
|
+
res.status(401).json({
|
|
145
|
+
error: { code: "MISSING_AGENT_ID", message: `Agent ID required in ${header} header` }
|
|
146
|
+
});
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const auth = await this.authorizePurchase({
|
|
150
|
+
agentId,
|
|
151
|
+
merchant: req.body?.merchant || req.body?.seller || "",
|
|
152
|
+
amount: req.body?.amount || 0,
|
|
153
|
+
currency: req.body?.currency || "usd",
|
|
154
|
+
minTrustScore: threshold
|
|
155
|
+
});
|
|
156
|
+
if (!auth.authorized) {
|
|
157
|
+
res.status(403).json({
|
|
158
|
+
error: {
|
|
159
|
+
code: "UNAUTHORIZED_AGENT",
|
|
160
|
+
message: auth.reason || "Agent not authorized for UCP transactions",
|
|
161
|
+
trust_score: auth.trustScore,
|
|
162
|
+
status: auth.status
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
req.vorimAuthorization = auth;
|
|
168
|
+
next();
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
async logEvent(agentId, action, result, req, metadata) {
|
|
172
|
+
try {
|
|
173
|
+
await this.sdk.emit({
|
|
174
|
+
agent_id: agentId,
|
|
175
|
+
event_type: "api_request",
|
|
176
|
+
action,
|
|
177
|
+
result,
|
|
178
|
+
resource: req?.merchant || "",
|
|
179
|
+
metadata: {
|
|
180
|
+
protocol: "ucp",
|
|
181
|
+
merchant: req?.merchant || "",
|
|
182
|
+
amount: req?.amount || 0,
|
|
183
|
+
currency: req?.currency || "",
|
|
184
|
+
...metadata
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
} catch {
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
function createVorimUCP(sdk, config) {
|
|
192
|
+
return new VorimUCP(sdk, config);
|
|
193
|
+
}
|
|
194
|
+
export {
|
|
195
|
+
VorimUCP,
|
|
196
|
+
createVorimUCP
|
|
197
|
+
};
|
|
198
|
+
//# sourceMappingURL=ucp.js.map
|