@vorim/sdk 3.19.0 → 3.22.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.
Files changed (109) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/README.md +73 -1
  3. package/dist/index.cjs +261 -9
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.cts +140 -3
  6. package/dist/index.d.ts +140 -3
  7. package/dist/index.js +253 -9
  8. package/dist/index.js.map +1 -1
  9. package/dist/integrations/anthropic.d.cts +2 -1
  10. package/dist/integrations/anthropic.d.ts +2 -1
  11. package/dist/integrations/ap2.d.cts +2 -1
  12. package/dist/integrations/ap2.d.ts +2 -1
  13. package/dist/integrations/aws-iam.d.cts +2 -1
  14. package/dist/integrations/aws-iam.d.ts +2 -1
  15. package/dist/integrations/bank-rails.d.cts +2 -1
  16. package/dist/integrations/bank-rails.d.ts +2 -1
  17. package/dist/integrations/circle.d.cts +2 -1
  18. package/dist/integrations/circle.d.ts +2 -1
  19. package/dist/integrations/cloudflare.d.cts +2 -1
  20. package/dist/integrations/cloudflare.d.ts +2 -1
  21. package/dist/integrations/coinbase-cdp.d.cts +2 -1
  22. package/dist/integrations/coinbase-cdp.d.ts +2 -1
  23. package/dist/integrations/commerce-agents.cjs +467 -0
  24. package/dist/integrations/commerce-agents.cjs.map +1 -0
  25. package/dist/integrations/commerce-agents.d.cts +200 -0
  26. package/dist/integrations/commerce-agents.d.ts +200 -0
  27. package/dist/integrations/commerce-agents.js +429 -0
  28. package/dist/integrations/commerce-agents.js.map +1 -0
  29. package/dist/integrations/crewai.d.cts +2 -1
  30. package/dist/integrations/crewai.d.ts +2 -1
  31. package/dist/integrations/entra.d.cts +2 -1
  32. package/dist/integrations/entra.d.ts +2 -1
  33. package/dist/integrations/fhir.d.cts +2 -1
  34. package/dist/integrations/fhir.d.ts +2 -1
  35. package/dist/integrations/grok.d.cts +2 -1
  36. package/dist/integrations/grok.d.ts +2 -1
  37. package/dist/integrations/guidewire.d.cts +2 -1
  38. package/dist/integrations/guidewire.d.ts +2 -1
  39. package/dist/integrations/hermes.d.cts +2 -1
  40. package/dist/integrations/hermes.d.ts +2 -1
  41. package/dist/integrations/jira.d.cts +2 -1
  42. package/dist/integrations/jira.d.ts +2 -1
  43. package/dist/integrations/langchain.d.cts +2 -1
  44. package/dist/integrations/langchain.d.ts +2 -1
  45. package/dist/integrations/langgraph.d.cts +2 -1
  46. package/dist/integrations/langgraph.d.ts +2 -1
  47. package/dist/integrations/legal.d.cts +2 -1
  48. package/dist/integrations/legal.d.ts +2 -1
  49. package/dist/integrations/life-sciences.d.cts +2 -1
  50. package/dist/integrations/life-sciences.d.ts +2 -1
  51. package/dist/integrations/linear.d.cts +2 -1
  52. package/dist/integrations/linear.d.ts +2 -1
  53. package/dist/integrations/lithic.d.cts +2 -1
  54. package/dist/integrations/lithic.d.ts +2 -1
  55. package/dist/integrations/llamaindex.d.cts +2 -1
  56. package/dist/integrations/llamaindex.d.ts +2 -1
  57. package/dist/integrations/marqeta.d.cts +2 -1
  58. package/dist/integrations/marqeta.d.ts +2 -1
  59. package/dist/integrations/modern-treasury.d.cts +2 -1
  60. package/dist/integrations/modern-treasury.d.ts +2 -1
  61. package/dist/integrations/muse.cjs +437 -0
  62. package/dist/integrations/muse.cjs.map +1 -0
  63. package/dist/integrations/muse.d.cts +72 -0
  64. package/dist/integrations/muse.d.ts +72 -0
  65. package/dist/integrations/muse.js +394 -0
  66. package/dist/integrations/muse.js.map +1 -0
  67. package/dist/integrations/nekuda.d.cts +2 -1
  68. package/dist/integrations/nekuda.d.ts +2 -1
  69. package/dist/integrations/okta.d.cts +2 -1
  70. package/dist/integrations/okta.d.ts +2 -1
  71. package/dist/integrations/openai.d.cts +2 -1
  72. package/dist/integrations/openai.d.ts +2 -1
  73. package/dist/integrations/openrouter.d.cts +2 -1
  74. package/dist/integrations/openrouter.d.ts +2 -1
  75. package/dist/integrations/paypal-agent.d.cts +2 -1
  76. package/dist/integrations/paypal-agent.d.ts +2 -1
  77. package/dist/integrations/perplexity.d.cts +2 -1
  78. package/dist/integrations/perplexity.d.ts +2 -1
  79. package/dist/integrations/plaid.d.cts +2 -1
  80. package/dist/integrations/plaid.d.ts +2 -1
  81. package/dist/integrations/ramp.d.cts +2 -1
  82. package/dist/integrations/ramp.d.ts +2 -1
  83. package/dist/integrations/shopify.d.cts +2 -1
  84. package/dist/integrations/shopify.d.ts +2 -1
  85. package/dist/integrations/siem.d.cts +1 -1
  86. package/dist/integrations/siem.d.ts +1 -1
  87. package/dist/integrations/skyfire.d.cts +2 -1
  88. package/dist/integrations/skyfire.d.ts +2 -1
  89. package/dist/integrations/stripe-acp.d.cts +2 -1
  90. package/dist/integrations/stripe-acp.d.ts +2 -1
  91. package/dist/integrations/stripe-issuing.d.cts +2 -1
  92. package/dist/integrations/stripe-issuing.d.ts +2 -1
  93. package/dist/integrations/truanon.d.cts +2 -1
  94. package/dist/integrations/truanon.d.ts +2 -1
  95. package/dist/integrations/typesafe.cjs +268 -0
  96. package/dist/integrations/typesafe.cjs.map +1 -0
  97. package/dist/integrations/typesafe.d.cts +145 -0
  98. package/dist/integrations/typesafe.d.ts +145 -0
  99. package/dist/integrations/typesafe.js +221 -0
  100. package/dist/integrations/typesafe.js.map +1 -0
  101. package/dist/integrations/ucp.d.cts +2 -1
  102. package/dist/integrations/ucp.d.ts +2 -1
  103. package/dist/integrations/vercel-ai.d.cts +2 -1
  104. package/dist/integrations/vercel-ai.d.ts +2 -1
  105. package/dist/integrations/x402.d.cts +2 -1
  106. package/dist/integrations/x402.d.ts +2 -1
  107. package/dist/{types-BmeluKHI.d.cts → types-BrDQAJdl.d.cts} +36 -1
  108. package/dist/{types-BmeluKHI.d.ts → types-BrDQAJdl.d.ts} +36 -1
  109. package/package.json +31 -1
@@ -1,6 +1,7 @@
1
- import { P as PermissionScope, A as AgentRegistrationResult } from '../types-BmeluKHI.cjs';
1
+ import { P as PermissionScope, A as AgentRegistrationResult } from '../types-BrDQAJdl.cjs';
2
2
  import { VorimSDK, ReplayInputs } from '../index.cjs';
3
3
  import { E as EscalationPolicy, T as TransportFailurePolicy } from '../_runtime-gate-DOJUKJBu.cjs';
4
+ import '@vorim/shared-types';
4
5
 
5
6
  interface AnthropicTool {
6
7
  name: string;
@@ -1,6 +1,7 @@
1
- import { P as PermissionScope, A as AgentRegistrationResult } from '../types-BmeluKHI.js';
1
+ import { P as PermissionScope, A as AgentRegistrationResult } from '../types-BrDQAJdl.js';
2
2
  import { VorimSDK, ReplayInputs } from '../index.js';
3
3
  import { E as EscalationPolicy, T as TransportFailurePolicy } from '../_runtime-gate-DOJUKJBu.js';
4
+ import '@vorim/shared-types';
4
5
 
5
6
  interface AnthropicTool {
6
7
  name: string;
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.cjs';
2
- import '../types-BmeluKHI.cjs';
2
+ import '../types-BrDQAJdl.cjs';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  type AP2MandateType = 'checkout' | 'payment';
5
6
  interface AP2Config {
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.js';
2
- import '../types-BmeluKHI.js';
2
+ import '../types-BrDQAJdl.js';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  type AP2MandateType = 'checkout' | 'payment';
5
6
  interface AP2Config {
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.cjs';
2
- import { P as PermissionScope } from '../types-BmeluKHI.cjs';
2
+ import { P as PermissionScope } from '../types-BrDQAJdl.cjs';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  /** The AWS IAM / STS operations this guard classifies (real API call names). */
5
6
  type AwsIamOperation = 'CreateUser' | 'CreateRole' | 'AttachRolePolicy' | 'PutRolePolicy' | 'CreateAccessKey' | 'AssumeRole' | 'read';
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.js';
2
- import { P as PermissionScope } from '../types-BmeluKHI.js';
2
+ import { P as PermissionScope } from '../types-BrDQAJdl.js';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  /** The AWS IAM / STS operations this guard classifies (real API call names). */
5
6
  type AwsIamOperation = 'CreateUser' | 'CreateRole' | 'AttachRolePolicy' | 'PutRolePolicy' | 'CreateAccessKey' | 'AssumeRole' | 'read';
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.cjs';
2
- import '../types-BmeluKHI.cjs';
2
+ import '../types-BrDQAJdl.cjs';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  type BankRail = 'ach' | 'wire' | 'rtp' | 'book';
5
6
  type BankProvider = 'increase' | 'column' | 'generic';
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.js';
2
- import '../types-BmeluKHI.js';
2
+ import '../types-BrDQAJdl.js';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  type BankRail = 'ach' | 'wire' | 'rtp' | 'book';
5
6
  type BankProvider = 'increase' | 'column' | 'generic';
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.cjs';
2
- import '../types-BmeluKHI.cjs';
2
+ import '../types-BrDQAJdl.cjs';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  interface CircleConfig {
5
6
  apiKey: string;
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.js';
2
- import '../types-BmeluKHI.js';
2
+ import '../types-BrDQAJdl.js';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  interface CircleConfig {
5
6
  apiKey: string;
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.cjs';
2
- import { P as PermissionScope } from '../types-BmeluKHI.cjs';
2
+ import { P as PermissionScope } from '../types-BrDQAJdl.cjs';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  interface CloudflareGuardConfig {
5
6
  /** The acting agent's Vorim id (agid_...). */
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.js';
2
- import { P as PermissionScope } from '../types-BmeluKHI.js';
2
+ import { P as PermissionScope } from '../types-BrDQAJdl.js';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  interface CloudflareGuardConfig {
5
6
  /** The acting agent's Vorim id (agid_...). */
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.cjs';
2
- import { P as PermissionScope } from '../types-BmeluKHI.cjs';
2
+ import { P as PermissionScope } from '../types-BrDQAJdl.cjs';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  type CdpOperation = 'sendTransaction' | 'transfer' | 'signMessage';
5
6
  interface CoinbaseCdpConfig {
@@ -1,5 +1,6 @@
1
1
  import { VorimSDK } from '../index.js';
2
- import { P as PermissionScope } from '../types-BmeluKHI.js';
2
+ import { P as PermissionScope } from '../types-BrDQAJdl.js';
3
+ import '@vorim/shared-types';
3
4
 
4
5
  type CdpOperation = 'sendTransaction' | 'transfer' | 'signMessage';
5
6
  interface CoinbaseCdpConfig {
@@ -0,0 +1,467 @@
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/commerce-agents.ts
21
+ var commerce_agents_exports = {};
22
+ __export(commerce_agents_exports, {
23
+ APPROVE_CHANGE: () => APPROVE_CHANGE,
24
+ CommerceDeniedError: () => CommerceDeniedError,
25
+ HUMAN_APPROVAL_OPERATIONS: () => HUMAN_APPROVAL_OPERATIONS,
26
+ MERCHANT_OPERATION_SCOPE: () => MERCHANT_OPERATION_SCOPE,
27
+ SHOPPING_OPERATION_SCOPE: () => SHOPPING_OPERATION_SCOPE,
28
+ UNGUARDED: () => UNGUARDED,
29
+ VorimCommerceGuard: () => VorimCommerceGuard,
30
+ createVorimCommerceGuard: () => createVorimCommerceGuard,
31
+ default: () => commerce_agents_default,
32
+ describeCommerceCall: () => describeCommerceCall,
33
+ guardMerchantBackend: () => guardMerchantBackend,
34
+ guardStorefrontBackend: () => guardStorefrontBackend,
35
+ normalizeOperation: () => normalizeOperation
36
+ });
37
+ module.exports = __toCommonJS(commerce_agents_exports);
38
+
39
+ // src/integrations/_runtime-gate.ts
40
+ function isAllowVerdict(decision) {
41
+ return decision === "allow" || decision === "modify";
42
+ }
43
+ async function runtimeGate(vorim, agentId, actionTarget, scope, payload, opts = {}) {
44
+ const onEscalation = opts.onEscalation ?? "wait";
45
+ const onTransportFailure = opts.onTransportFailure ?? "deny";
46
+ const d = await vorim.beforeAction(
47
+ { agentId, actionType: "tool_call", actionTarget, requiredScope: scope, payload },
48
+ { throwOnDeny: false }
49
+ );
50
+ if (d.isFallback || d.decision === "fallback") {
51
+ if (onTransportFailure === "allow") {
52
+ return { allowed: true, reason: d.reason ?? "control plane unreachable (fail-open)", decisionId: d.decisionId || void 0 };
53
+ }
54
+ return { allowed: false, reason: d.reason ?? "control plane unreachable (failed closed)", decisionId: d.decisionId || void 0 };
55
+ }
56
+ if (d.decision !== "escalate") {
57
+ return { allowed: isAllowVerdict(d.decision), reason: d.reason, decisionId: d.decisionId || void 0 };
58
+ }
59
+ if (onEscalation === "allow") {
60
+ return { allowed: true, reason: d.reason ?? "escalated (auto-allowed)", decisionId: d.decisionId || void 0 };
61
+ }
62
+ if (onEscalation === "deny") {
63
+ return { allowed: false, reason: d.reason ?? "escalated (auto-denied \u2014 awaiting human)", decisionId: d.decisionId || void 0 };
64
+ }
65
+ if (!d.decisionId) {
66
+ return { allowed: false, reason: "escalated but no decisionId to poll", decisionId: void 0 };
67
+ }
68
+ try {
69
+ const resolved = await vorim.waitForDecisionResolution(d.decisionId, { timeoutMs: opts.escalationTimeoutMs });
70
+ return { allowed: isAllowVerdict(resolved.decision), reason: resolved.reason, decisionId: resolved.decisionId || d.decisionId };
71
+ } catch {
72
+ return { allowed: false, reason: "escalation not resolved before timeout", decisionId: d.decisionId };
73
+ }
74
+ }
75
+
76
+ // src/integrations/commerce-agents.ts
77
+ var SHOPPING_OPERATION_SCOPE = {
78
+ // Catalogue and customer reads.
79
+ search_products: "agent:read",
80
+ get_product_details: "agent:read",
81
+ get_cart: "agent:read",
82
+ get_preferences: "agent:read",
83
+ get_account_context: "agent:read",
84
+ get_orders: "agent:read",
85
+ get_order: "agent:read",
86
+ search_policies: "agent:read",
87
+ get_disclosure: "agent:read",
88
+ get_fulfillment_options: "agent:read",
89
+ // Cart writes — reversible, but they change what the customer is about to buy.
90
+ add_to_cart: "agent:write",
91
+ update_cart_item: "agent:write",
92
+ remove_from_cart: "agent:write",
93
+ // The money moment. No money moves here (the host completes checkout), but
94
+ // this is the call that produces a payable cart, so it takes agent:transact.
95
+ checkout_handoff: "agent:transact"
96
+ };
97
+ var MERCHANT_OPERATION_SCOPE = {
98
+ // Analytics and catalogue reads.
99
+ get_business_snapshot: "agent:read",
100
+ query_metrics: "agent:read",
101
+ get_campaign_performance: "agent:read",
102
+ search_listings: "agent:read",
103
+ get_listing: "agent:read",
104
+ get_inventory_alerts: "agent:read",
105
+ get_order_issues: "agent:read",
106
+ get_pricing_context: "agent:read",
107
+ get_pending_changes: "agent:read",
108
+ get_analysis_schema: "agent:read",
109
+ get_merchant_context: "agent:read",
110
+ // The analysis delegate's read-only SQL. Read-only by contract in the
111
+ // blueprint; it still runs model-authored SQL, so hold it to a higher trust
112
+ // floor if your warehouse has rows the shopper-facing agent should not see.
113
+ execute_analysis_query: "agent:read",
114
+ // Staged writes: nothing is live yet, and every one of them is discardable.
115
+ stage_listing_update: "agent:write",
116
+ stage_price_update: "agent:write",
117
+ stage_inventory_action: "agent:write",
118
+ stage_promotion: "agent:write",
119
+ stage_campaign: "agent:write",
120
+ discard_change: "agent:write",
121
+ // The live-store write. Irreversible from the agent's side, so it is held to
122
+ // agent:elevate and escalated to a human under runtime control.
123
+ apply_change: "agent:elevate"
124
+ };
125
+ var HUMAN_APPROVAL_OPERATIONS = ["checkout_handoff", "apply_change"];
126
+ var APPROVE_CHANGE = "approve_change";
127
+ var CommerceDeniedError = class extends Error {
128
+ receipt;
129
+ constructor(message, receipt) {
130
+ super(message);
131
+ this.name = "CommerceDeniedError";
132
+ this.receipt = receipt;
133
+ }
134
+ };
135
+ var RESOURCE_KEYS = [
136
+ "change_id",
137
+ "changeId",
138
+ "product_id",
139
+ "productId",
140
+ "order_id",
141
+ "orderId",
142
+ "listing_id",
143
+ "listingId",
144
+ "campaign_id",
145
+ "campaignId",
146
+ "promotion_id",
147
+ "promotionId"
148
+ ];
149
+ var ID_AFTER_SESSION = /* @__PURE__ */ new Set([
150
+ "get_product_details",
151
+ "add_to_cart",
152
+ "update_cart_item",
153
+ "remove_from_cart",
154
+ "get_order",
155
+ "get_disclosure",
156
+ "get_listing",
157
+ "stage_listing_update",
158
+ "apply_change",
159
+ "discard_change"
160
+ ]);
161
+ function normalizeOperation(operation) {
162
+ return operation.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
163
+ }
164
+ function isSessionContext(value) {
165
+ if (!value || typeof value !== "object") return false;
166
+ const v = value;
167
+ return "session_id" in v || "sessionId" in v;
168
+ }
169
+ async function sha256Hex(input) {
170
+ try {
171
+ const bytes = new TextEncoder().encode(input);
172
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
173
+ return Array.from(new Uint8Array(digest)).map((b) => b.toString(16).padStart(2, "0")).join("");
174
+ } catch {
175
+ return void 0;
176
+ }
177
+ }
178
+ async function describeCommerceCall(operation, args) {
179
+ const action = { operation: normalizeOperation(operation) };
180
+ const rest = [];
181
+ for (const arg of args) {
182
+ if (isSessionContext(arg) && !action.sessionId) {
183
+ const session = arg;
184
+ action.sessionId = session.session_id ?? session.sessionId;
185
+ action.humanId = session.user_id ?? session.userId ?? session.operator;
186
+ continue;
187
+ }
188
+ rest.push(arg);
189
+ }
190
+ if (ID_AFTER_SESSION.has(action.operation) && typeof rest[0] === "string" && rest[0]) {
191
+ action.resourceId = rest[0];
192
+ }
193
+ for (const arg of rest) {
194
+ if (arg && typeof arg === "object" && !Array.isArray(arg)) {
195
+ const record = arg;
196
+ for (const key of RESOURCE_KEYS) {
197
+ const value = record[key];
198
+ if (!action.resourceId && typeof value === "string" && value) action.resourceId = value;
199
+ }
200
+ }
201
+ }
202
+ if (rest.length) {
203
+ try {
204
+ action.payload = { args: JSON.parse(JSON.stringify(rest)) };
205
+ action.inputHash = await sha256Hex(JSON.stringify(action.payload));
206
+ } catch {
207
+ action.payload = void 0;
208
+ }
209
+ }
210
+ return action;
211
+ }
212
+ var VorimCommerceGuard = class {
213
+ sdk;
214
+ agentId;
215
+ role;
216
+ humanId;
217
+ storeId;
218
+ minTrustScore;
219
+ onError;
220
+ unknownScope;
221
+ useRuntimeControl;
222
+ onEscalation;
223
+ escalationTimeoutMs;
224
+ onTransportFailure;
225
+ onDenied;
226
+ constructor(sdk, config) {
227
+ if (!config?.agentId) throw new Error("[vorim] commerce-agents integration requires an agentId.");
228
+ if (config.role !== "shopping" && config.role !== "merchant") {
229
+ throw new Error("[vorim] commerce-agents role must be 'shopping' or 'merchant'.");
230
+ }
231
+ this.sdk = sdk;
232
+ this.agentId = config.agentId;
233
+ this.role = config.role;
234
+ this.humanId = config.humanId;
235
+ this.storeId = config.storeId;
236
+ this.minTrustScore = config.minTrustScore ?? 0;
237
+ this.onError = config.onError ?? "closed";
238
+ this.unknownScope = config.unknownScope ?? "agent:write";
239
+ this.useRuntimeControl = config.useRuntimeControl ?? false;
240
+ this.onEscalation = config.onEscalation ?? "wait";
241
+ this.escalationTimeoutMs = config.escalationTimeoutMs;
242
+ this.onTransportFailure = config.onTransportFailure ?? "deny";
243
+ this.onDenied = config.onDenied;
244
+ }
245
+ /** The scope map for this guard's role. */
246
+ get scopes() {
247
+ return this.role === "shopping" ? SHOPPING_OPERATION_SCOPE : MERCHANT_OPERATION_SCOPE;
248
+ }
249
+ /** Whether an operation is one this guard's role knows how to classify. */
250
+ knows(operation) {
251
+ return normalizeOperation(operation) in this.scopes;
252
+ }
253
+ /** The Vorim scope an operation requires; unknown methods take unknownScope. */
254
+ scopeFor(operation) {
255
+ const name = normalizeOperation(operation);
256
+ if (name === APPROVE_CHANGE) return "agent:elevate";
257
+ return this.scopes[name] ?? this.unknownScope;
258
+ }
259
+ /**
260
+ * Check whether the acting agent may run this operation, before it runs.
261
+ * Static path: trust floor, then permission scope. Runtime path: a
262
+ * beforeAction decision, with `escalate` resolved per onEscalation.
263
+ */
264
+ async authorize(action) {
265
+ const scope = this.scopeFor(action.operation);
266
+ try {
267
+ if (this.useRuntimeControl) {
268
+ const target = `commerce:${this.role}:${normalizeOperation(action.operation)}${action.resourceId ? " " + action.resourceId : ""}`;
269
+ const gate = await runtimeGate(this.sdk, this.agentId, target, scope, action.payload, {
270
+ onEscalation: this.onEscalation,
271
+ escalationTimeoutMs: this.escalationTimeoutMs,
272
+ onTransportFailure: this.onTransportFailure
273
+ });
274
+ return { authorized: gate.allowed, reason: gate.reason, decisionId: gate.decisionId };
275
+ }
276
+ if (this.minTrustScore > 0) {
277
+ const trust = await this.sdk.verify(this.agentId);
278
+ if ((trust?.trust_score ?? 0) < this.minTrustScore) {
279
+ return { authorized: false, reason: `trust score below ${this.minTrustScore}` };
280
+ }
281
+ }
282
+ const perm = await this.sdk.check(this.agentId, scope);
283
+ return perm.allowed ? { authorized: true } : { authorized: false, reason: perm.reason ?? `missing scope ${scope}` };
284
+ } catch (e) {
285
+ return this.onError === "open" ? { authorized: true, reason: "vorim unreachable, failed open" } : { authorized: false, reason: `vorim unreachable: ${e?.message ?? "error"}` };
286
+ }
287
+ }
288
+ /**
289
+ * Emit a source-signed audit event and return whether it was recorded. The
290
+ * event names the agent, the store, the session and the person the agent acts
291
+ * for, so a cart write or a live price move is attributable and independently
292
+ * verifiable offline.
293
+ */
294
+ async record(action, result, reason, decisionId) {
295
+ const operation = normalizeOperation(action.operation);
296
+ try {
297
+ await this.sdk.emit(
298
+ {
299
+ agent_id: this.agentId,
300
+ event_type: "tool_call",
301
+ action: `commerce:${this.role}:${operation}${action.resourceId ? " " + action.resourceId : ""}`,
302
+ resource: action.resourceId || this.storeId,
303
+ permission: this.scopeFor(operation),
304
+ result,
305
+ input_hash: action.inputHash,
306
+ metadata: {
307
+ integration: "commerce-agents",
308
+ role: this.role,
309
+ store: this.storeId,
310
+ acts_for_human: action.humanId ?? this.humanId,
311
+ session_id: action.sessionId,
312
+ operation,
313
+ ...reason ? { reason } : {},
314
+ ...decisionId ? { decision_id: decisionId } : {}
315
+ }
316
+ },
317
+ { sign: true }
318
+ // source-signed: the agent's key signs the record
319
+ );
320
+ return true;
321
+ } catch {
322
+ return false;
323
+ }
324
+ }
325
+ /**
326
+ * The full flow for one backend call: authorize, run it if allowed, sign a
327
+ * receipt either way. If not authorized, `run` is never called — the
328
+ * catalogue is never queried, the cart is never written, nothing goes live.
329
+ * An error from `run` is recorded and rethrown with the receipt attached as
330
+ * `vorimReceipt`, so the executor's failure ladder still sees the real
331
+ * failure.
332
+ */
333
+ async guard(action, run) {
334
+ const auth = await this.authorize(action);
335
+ if (!auth.authorized) {
336
+ const recorded = await this.record(action, "denied", auth.reason, auth.decisionId);
337
+ return {
338
+ receipt: this.receipt(action, false, "denied", auth.reason, recorded, auth.decisionId)
339
+ };
340
+ }
341
+ try {
342
+ const value = await run();
343
+ const recorded = await this.record(action, "success", void 0, auth.decisionId);
344
+ return {
345
+ receipt: this.receipt(action, true, "success", void 0, recorded, auth.decisionId),
346
+ value
347
+ };
348
+ } catch (e) {
349
+ const recorded = await this.record(action, "error", e?.message, auth.decisionId);
350
+ throw Object.assign(e ?? new Error("commerce backend call failed"), {
351
+ vorimReceipt: this.receipt(action, true, "error", e?.message, recorded, auth.decisionId)
352
+ });
353
+ }
354
+ }
355
+ /**
356
+ * Ask a human to approve a staged merchant change, and mark it approved on the
357
+ * session state when they do.
358
+ *
359
+ * This is the bridge to MerchantAgentConfig.require_host_approval:
360
+ * commerce-agents applies a change only for ids in
361
+ * MerchantSessionState.approved_change_ids, and this fills that set from a
362
+ * signed human decision rather than from host code that trusted the
363
+ * conversation. With useRuntimeControl off there is nobody to escalate to, so
364
+ * it falls back to the static scope check — which proves the agent *may* apply
365
+ * changes, not that a person approved this one.
366
+ */
367
+ async approveChange(changeId, opts = {}) {
368
+ const payload = { change_id: changeId, ...opts.payload ?? {} };
369
+ const action = {
370
+ operation: APPROVE_CHANGE,
371
+ resourceId: changeId,
372
+ sessionId: opts.sessionId,
373
+ payload,
374
+ inputHash: await sha256Hex(JSON.stringify(payload))
375
+ };
376
+ const { receipt } = await this.guard(action, async () => void 0);
377
+ if (receipt.authorized && opts.state) {
378
+ const approved = opts.state.approved_change_ids ?? opts.state.approvedChangeIds;
379
+ if (!approved) {
380
+ throw new TypeError(
381
+ "[vorim] approveChange expected a MerchantSessionState with approved_change_ids."
382
+ );
383
+ }
384
+ approved.add(changeId);
385
+ }
386
+ return receipt;
387
+ }
388
+ /** The error a guarded backend method throws on a denial. */
389
+ denialError(receipt) {
390
+ if (this.onDenied) return this.onDenied(receipt);
391
+ return new CommerceDeniedError(
392
+ `${receipt.operation} was not authorized for ${receipt.agentId} (${receipt.scope}): ${receipt.reason ?? "denied"}`,
393
+ receipt
394
+ );
395
+ }
396
+ receipt(action, authorized, result, reason, recorded, decisionId) {
397
+ return {
398
+ authorized,
399
+ agentId: this.agentId,
400
+ role: this.role,
401
+ operation: normalizeOperation(action.operation),
402
+ result,
403
+ scope: this.scopeFor(action.operation),
404
+ recorded,
405
+ storeId: this.storeId,
406
+ humanId: action.humanId ?? this.humanId,
407
+ sessionId: action.sessionId,
408
+ resourceId: action.resourceId,
409
+ inputHash: action.inputHash,
410
+ reason,
411
+ decisionId
412
+ };
413
+ }
414
+ };
415
+ var UNGUARDED = /* @__PURE__ */ Symbol.for("vorim.commerce.inner");
416
+ function guardBackend(backend, guard, options = {}) {
417
+ const guardUnknown = options.guardUnknown ?? true;
418
+ return new Proxy(backend, {
419
+ get(target, property, receiver) {
420
+ if (property === UNGUARDED) return target;
421
+ const value = Reflect.get(target, property, receiver);
422
+ if (typeof property !== "string" || typeof value !== "function") return value;
423
+ if (!guard.knows(property) && !guardUnknown) return value;
424
+ return async (...args) => {
425
+ const action = await describeCommerceCall(property, args);
426
+ const { receipt, value: result } = await guard.guard(
427
+ action,
428
+ async () => value.apply(target, args)
429
+ );
430
+ if (!receipt.authorized) throw guard.denialError(receipt);
431
+ return result;
432
+ };
433
+ }
434
+ });
435
+ }
436
+ function guardStorefrontBackend(backend, guard, options) {
437
+ if (guard.role !== "shopping") {
438
+ throw new Error("[vorim] guardStorefrontBackend needs a guard built with role: 'shopping'.");
439
+ }
440
+ return guardBackend(backend, guard, options);
441
+ }
442
+ function guardMerchantBackend(backend, guard, options) {
443
+ if (guard.role !== "merchant") {
444
+ throw new Error("[vorim] guardMerchantBackend needs a guard built with role: 'merchant'.");
445
+ }
446
+ return guardBackend(backend, guard, options);
447
+ }
448
+ function createVorimCommerceGuard(sdk, config) {
449
+ return new VorimCommerceGuard(sdk, config);
450
+ }
451
+ var commerce_agents_default = createVorimCommerceGuard;
452
+ // Annotate the CommonJS export names for ESM import in node:
453
+ 0 && (module.exports = {
454
+ APPROVE_CHANGE,
455
+ CommerceDeniedError,
456
+ HUMAN_APPROVAL_OPERATIONS,
457
+ MERCHANT_OPERATION_SCOPE,
458
+ SHOPPING_OPERATION_SCOPE,
459
+ UNGUARDED,
460
+ VorimCommerceGuard,
461
+ createVorimCommerceGuard,
462
+ describeCommerceCall,
463
+ guardMerchantBackend,
464
+ guardStorefrontBackend,
465
+ normalizeOperation
466
+ });
467
+ //# sourceMappingURL=commerce-agents.cjs.map