@belticlabs/agent-risk-sdk 0.3.0 → 0.5.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.
@@ -1,15 +1,8 @@
1
- import { D as Decision } from './verdict-6vCyoAHE.js';
1
+ import { D as Decision } from './verdict-DMnbFuS5.js';
2
2
  import { x402ResourceServer, x402HTTPResourceServer } from '@x402/core/server';
3
- import { B as Beltic, n as SessionBorn, a as Session } from './session-DsBWEP8d.js';
3
+ import { B as Beltic, j as SessionBorn, i as Session } from './session-D9E-efc0.js';
4
4
 
5
- interface CorrelationContext {
6
- path: string;
7
- method: string;
8
- header: (name: string) => string | undefined;
9
- }
10
5
  interface AttachOptions {
11
- /** A key the merchant will see again at verify time, for delegated flows (GAP-31). */
12
- correlate?: ((ctx: CorrelationContext) => string | null) | undefined;
13
6
  /** Called with every decision; the default logs nothing. */
14
7
  onDecision?: ((d: {
15
8
  sessionId: string;
@@ -26,4 +19,4 @@ interface AttachedX402 {
26
19
  }
27
20
  declare function attachX402(beltic: Beltic, server: x402ResourceServer, http?: x402HTTPResourceServer, opts?: AttachOptions): AttachedX402;
28
21
 
29
- export { type AttachOptions as A, type CorrelationContext as C, attachX402 as a, type AttachedX402 as b };
22
+ export { type AttachOptions as A, attachX402 as a, type AttachedX402 as b };
@@ -1,6 +1,6 @@
1
- import { P as PaymentMomentPayload } from '../verdict-6vCyoAHE.js';
1
+ import { P as PaymentMomentPayload } from '../verdict-DMnbFuS5.js';
2
2
  import { LanguageModelMiddleware, Tool, ToolSet } from 'ai';
3
- import { S as SessionSource } from '../session-DsBWEP8d.js';
3
+ import { S as SessionSource } from '../session-D9E-efc0.js';
4
4
  import 'zod';
5
5
 
6
6
  interface PaymentMoments {
package/dist/ai/index.js CHANGED
@@ -1,15 +1,14 @@
1
1
  import {
2
2
  Sessions,
3
3
  recordCall
4
- } from "../chunk-4MG6VNAU.js";
5
- import {
6
- uuidv7
7
- } from "../chunk-X3W2Z5GC.js";
8
- import "../chunk-46QN2KEZ.js";
4
+ } from "../chunk-JSE6JQJC.js";
9
5
  import {
10
6
  toJson,
11
7
  toJsonObject
12
8
  } from "../chunk-FQDHFTVR.js";
9
+ import {
10
+ uuidv7
11
+ } from "../chunk-X3W2Z5GC.js";
13
12
 
14
13
  // src/ai/index.ts
15
14
  var PARAM_KEYS = [
@@ -25,7 +24,6 @@ var PARAM_KEYS = [
25
24
  "stopSequences"
26
25
  ];
27
26
  function middleware(source) {
28
- if (!source) return {};
29
27
  return {
30
28
  wrapGenerate: async ({ doGenerate, params, model }) => {
31
29
  const session = await Sessions.resolve(source);
@@ -81,7 +79,7 @@ function middleware(source) {
81
79
  };
82
80
  }
83
81
  function wrapTool(source, tool, opts = {}) {
84
- if (!source || !tool.execute) return tool;
82
+ if (!tool.execute) return tool;
85
83
  const original = tool.execute;
86
84
  const toolName = opts.name ?? tool.name ?? "tool";
87
85
  const execute = async (input, options) => {
@@ -90,7 +88,7 @@ function wrapTool(source, tool, opts = {}) {
90
88
  return recordCall(
91
89
  session,
92
90
  "tool_call",
93
- options?.toolCallId ?? uuidv7(),
91
+ options.toolCallId,
94
92
  { toolName, input: toJson(input), transport: "local" },
95
93
  async () => {
96
94
  const output = await original(input, options);
@@ -326,13 +326,6 @@ var EvaluateOutputSchema = z4.strictObject({
326
326
  /** Additive (GAP-21). */
327
327
  decisionId: z4.uuid()
328
328
  });
329
- var AnchorEntrySchema = z4.strictObject({
330
- epoch: z4.string().min(1),
331
- root: Hex64Schema,
332
- sig: z4.string().min(1),
333
- chainRef: z4.string().optional()
334
- });
335
- var AnchorsOutputSchema = z4.strictObject({ entries: z4.array(AnchorEntrySchema) });
336
329
  var CreatePolicyInputSchema = z4.strictObject({ rules: PolicyRulesSchema });
337
330
  var CreatePolicyOutputSchema = z4.strictObject({
338
331
  policyId: z4.uuid(),
@@ -542,8 +535,6 @@ export {
542
535
  EvaluateInputSchema,
543
536
  DecisionSchema,
544
537
  EvaluateOutputSchema,
545
- AnchorEntrySchema,
546
- AnchorsOutputSchema,
547
538
  CreatePolicyInputSchema,
548
539
  CreatePolicyOutputSchema,
549
540
  ApiErrorSchema,
@@ -0,0 +1,163 @@
1
+ import {
2
+ toJsonObject
3
+ } from "./chunk-FQDHFTVR.js";
4
+ import {
5
+ payloadDigest
6
+ } from "./chunk-X3W2Z5GC.js";
7
+
8
+ // src/x402/binding.ts
9
+ var SESSION_EXTENSION = "beltic.sessionId";
10
+ var SESSION_HEADER = "Beltic-Session-Id";
11
+ var DECISION_EXTENSION = "beltic.decisionId";
12
+ function sessionIdOf(extensions) {
13
+ return stringAt(extensions, SESSION_EXTENSION);
14
+ }
15
+ function decisionIdOf(extensions) {
16
+ return stringAt(extensions, DECISION_EXTENSION);
17
+ }
18
+ function stringAt(extensions, key) {
19
+ const v = extensions?.[key];
20
+ return typeof v === "string" && v.length > 0 ? v : null;
21
+ }
22
+
23
+ // src/x402/moments.ts
24
+ function x402Currency(network, asset) {
25
+ return `${network}/${asset}`;
26
+ }
27
+ function x402Summary(accepts, opts = {}) {
28
+ const payer = opts.payer?.toLowerCase();
29
+ return {
30
+ protocol: "x402",
31
+ payee: accepts?.payTo ?? "unknown",
32
+ amount: {
33
+ value: accepts?.amount ?? "0",
34
+ currency: accepts ? x402Currency(accepts.network ?? "unknown", accepts.asset ?? "unknown") : "unknown"
35
+ },
36
+ ...payer ? { payer } : {}
37
+ };
38
+ }
39
+ function fromAccepts(a, artifact, raw, payer) {
40
+ return { ...x402Summary(a, { payer }), artifact, raw };
41
+ }
42
+ function payerOf(payload) {
43
+ const p = payload.payload ?? {};
44
+ const auth = p.authorization;
45
+ const candidates = [
46
+ auth?.from,
47
+ p.from,
48
+ p.payer,
49
+ p.signer,
50
+ p.permit?.owner
51
+ ];
52
+ const hit = candidates.find((c) => typeof c === "string" && c.length > 0);
53
+ return typeof hit === "string" ? hit.toLowerCase() : void 0;
54
+ }
55
+ var x402Moments = {
56
+ /** The 402 challenge as the buyer saw it, from the `PAYMENT-REQUIRED` header. */
57
+ required(required) {
58
+ return fromAccepts(required.accepts?.[0], "http-402", toJsonObject(required));
59
+ },
60
+ /** The requirements the seller's resource server resolved for a request. */
61
+ requirements(req) {
62
+ return fromAccepts(req, "http-402", toJsonObject(req));
63
+ },
64
+ /** A route's static `accepts` config, before any payment header exists. */
65
+ route(route, raw) {
66
+ const accepts = route ?? {};
67
+ const extra = accepts.extra;
68
+ return fromAccepts(
69
+ {
70
+ payTo: typeof accepts.payTo === "string" ? accepts.payTo : "dynamic",
71
+ amount: priceValue(accepts.price),
72
+ network: accepts.network,
73
+ asset: extra?.asset ?? "route"
74
+ },
75
+ "http-402",
76
+ raw
77
+ );
78
+ },
79
+ /** The signed payment the buyer presented, with the requirement it accepted. */
80
+ payload(payload) {
81
+ return fromAccepts(
82
+ payload.accepted,
83
+ "payment-signature",
84
+ toJsonObject(payload),
85
+ payerOf(payload)
86
+ );
87
+ }
88
+ };
89
+ function priceValue(price) {
90
+ if (typeof price === "string") return price.replace(/[^0-9.]/g, "") || "0";
91
+ if (typeof price === "number") return String(price);
92
+ if (price && typeof price === "object" && typeof price.amount === "string")
93
+ return price.amount;
94
+ return "0";
95
+ }
96
+
97
+ // src/x402/adapter.ts
98
+ function attachX402(beltic, server, http, opts = {}) {
99
+ const inFlight = /* @__PURE__ */ new Map();
100
+ http?.onProtectedRequest(async (ctx, route) => {
101
+ if (ctx.paymentHeader) return;
102
+ const bound = ctx.adapter.getHeader(SESSION_HEADER) ?? ctx.adapter.getHeader(SESSION_HEADER.toLowerCase());
103
+ if (!bound) return;
104
+ const session = await beltic.sessions.ensure(bound);
105
+ const accepts = Array.isArray(route.accepts) ? route.accepts[0] : route.accepts;
106
+ await session.emit(
107
+ "payment.requested",
108
+ x402Moments.route(accepts, {
109
+ path: ctx.path,
110
+ method: ctx.method,
111
+ route: JSON.parse(JSON.stringify(route))
112
+ })
113
+ );
114
+ });
115
+ server.onBeforeVerify(async (ctx) => {
116
+ const payload = ctx.paymentPayload;
117
+ const requirements = ctx.requirements;
118
+ const session = await beltic.sessions.ensure(sessionIdOf(payload.extensions));
119
+ if (session.born === "seller")
120
+ await session.emit("payment.requested", x402Moments.requirements(requirements));
121
+ const presented = x402Moments.payload(payload);
122
+ await session.emit("payment.presented", presented);
123
+ inFlight.set(payloadDigest(payload), session);
124
+ const out = await beltic.evaluate(session.id, presented);
125
+ if (!out) return;
126
+ opts.onDecision?.({
127
+ sessionId: session.id,
128
+ decision: out.decision,
129
+ reasonCodes: out.reasonCodes,
130
+ born: session.born,
131
+ buyerDecisionId: decisionIdOf(payload.extensions)
132
+ });
133
+ if (out.verdict.blocks(beltic.onReview)) {
134
+ return {
135
+ abort: true,
136
+ reason: `BELTIC_${out.verdict.value}`,
137
+ message: out.reasonCodes.join(",")
138
+ };
139
+ }
140
+ return;
141
+ });
142
+ server.onAfterSettle(async (ctx) => {
143
+ const key = payloadDigest(ctx.paymentPayload);
144
+ const session = inFlight.get(key);
145
+ if (!session) return;
146
+ inFlight.delete(key);
147
+ if (ctx.result.success) await session.close("settled", { transaction: ctx.result.transaction });
148
+ });
149
+ return { inFlight };
150
+ }
151
+
152
+ export {
153
+ SESSION_EXTENSION,
154
+ SESSION_HEADER,
155
+ DECISION_EXTENSION,
156
+ sessionIdOf,
157
+ decisionIdOf,
158
+ x402Currency,
159
+ x402Summary,
160
+ payerOf,
161
+ x402Moments,
162
+ attachX402
163
+ };