@belticlabs/agent-risk-sdk 0.6.0 → 0.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/dist/ai/index.d.ts +2 -2
- package/dist/ai/index.js +15 -18
- package/dist/chunk-FNU4CRJJ.js +547 -0
- package/dist/{chunk-IUWC6HT5.js → chunk-HI656UWG.js} +12 -14
- package/dist/{chunk-ZMPKY7AX.js → chunk-JGVVQUXQ.js} +1 -2
- package/dist/{chunk-77D74TWX.js → chunk-UXXSE643.js} +25 -22
- package/dist/{client-CgCjOrRP.d.ts → client-Xn9M58OJ.d.ts} +18 -12
- package/dist/{index-IfY4XCvJ.d.ts → index-DAVKGJs5.d.ts} +129 -139
- package/dist/index.d.ts +3 -3
- package/dist/index.js +351 -165
- package/dist/protocol/index.d.ts +51 -30
- package/dist/protocol/index.js +50 -529
- package/dist/{session-Dcof4UIn.d.ts → session-DAN29XoB.d.ts} +115 -43
- package/dist/x402/hono.d.ts +3 -4
- package/dist/x402/hono.js +3 -3
- package/dist/x402/index.d.ts +7 -22
- package/dist/x402/index.js +33 -24
- package/package.json +1 -1
- package/dist/adapter-AjCgj-KM.d.ts +0 -6
package/dist/ai/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { LanguageModelMiddleware, ToolSet } from 'ai';
|
|
2
|
-
import { S as Session } from '../session-
|
|
3
|
-
import '../index-
|
|
2
|
+
import { S as Session } from '../session-DAN29XoB.js';
|
|
3
|
+
import '../index-DAVKGJs5.js';
|
|
4
4
|
import 'zod';
|
|
5
5
|
|
|
6
6
|
declare function middleware(session: Session): LanguageModelMiddleware;
|
package/dist/ai/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
recordCall
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-JGVVQUXQ.js";
|
|
4
4
|
import {
|
|
5
5
|
toJson,
|
|
6
6
|
toJsonObject
|
|
7
7
|
} from "../chunk-FQDHFTVR.js";
|
|
8
8
|
import {
|
|
9
9
|
uuidv7
|
|
10
|
-
} from "../chunk-
|
|
10
|
+
} from "../chunk-UXXSE643.js";
|
|
11
|
+
import "../chunk-FNU4CRJJ.js";
|
|
11
12
|
|
|
12
13
|
// src/ai/index.ts
|
|
13
14
|
var PARAM_KEYS = [
|
|
@@ -28,10 +29,12 @@ function middleware(session) {
|
|
|
28
29
|
const stream = await session.stream();
|
|
29
30
|
if (!stream) return doGenerate();
|
|
30
31
|
return recordCall(
|
|
31
|
-
stream
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
stream.llmCall({
|
|
33
|
+
callId: uuidv7(),
|
|
34
|
+
provider: model.provider,
|
|
35
|
+
modelId: model.modelId,
|
|
36
|
+
params: pick(params)
|
|
37
|
+
}),
|
|
35
38
|
doGenerate,
|
|
36
39
|
(result) => ({
|
|
37
40
|
content: toJson(result.content),
|
|
@@ -44,14 +47,13 @@ function middleware(session) {
|
|
|
44
47
|
wrapStream: async ({ doStream, params, model }) => {
|
|
45
48
|
const evidence = await session.stream();
|
|
46
49
|
if (!evidence) return doStream();
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
await evidence.emit("llm_call.start", {
|
|
50
|
-
callId,
|
|
50
|
+
const span = evidence.llmCall({
|
|
51
|
+
callId: uuidv7(),
|
|
51
52
|
provider: model.provider,
|
|
52
53
|
modelId: model.modelId,
|
|
53
54
|
params: pick(params)
|
|
54
55
|
});
|
|
56
|
+
await span.opened;
|
|
55
57
|
const { stream, ...rest } = await doStream();
|
|
56
58
|
const parts = [];
|
|
57
59
|
let finish = {};
|
|
@@ -64,12 +66,10 @@ function middleware(session) {
|
|
|
64
66
|
controller.enqueue(part);
|
|
65
67
|
},
|
|
66
68
|
flush: async () => {
|
|
67
|
-
await
|
|
68
|
-
callId,
|
|
69
|
+
await span.end({
|
|
69
70
|
content: toJson(parts),
|
|
70
71
|
...finish.finishReason ? { finishReason: finish.finishReason } : {},
|
|
71
|
-
...finish.usage ? { usage: toJsonObject(finish.usage) } : {}
|
|
72
|
-
durationMs: Date.now() - started
|
|
72
|
+
...finish.usage ? { usage: toJsonObject(finish.usage) } : {}
|
|
73
73
|
});
|
|
74
74
|
}
|
|
75
75
|
});
|
|
@@ -90,10 +90,7 @@ function wrapTool(session, toolName, tool) {
|
|
|
90
90
|
const stream = await session.stream();
|
|
91
91
|
if (!stream) return original(input, options);
|
|
92
92
|
return recordCall(
|
|
93
|
-
stream,
|
|
94
|
-
"tool_call",
|
|
95
|
-
options.toolCallId,
|
|
96
|
-
{ toolName, input: toJson(input), transport: "local" },
|
|
93
|
+
stream.toolCall({ callId: options.toolCallId, toolName, input: toJson(input) }),
|
|
97
94
|
async () => {
|
|
98
95
|
const output = await original(input, options);
|
|
99
96
|
return output && typeof output === "object" && Symbol.asyncIterator in output ? collect(output) : output;
|
|
@@ -0,0 +1,547 @@
|
|
|
1
|
+
// ../protocol/src/evidence.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var WIRE_SOURCES = ["AGENT_TRACE", "INTERNAL_NETWORK"];
|
|
4
|
+
var ALL_SOURCES = ["AGENT_TRACE", "INTERNAL_NETWORK", "PLATFORM"];
|
|
5
|
+
var EvidenceSourceSchema = z.enum(WIRE_SOURCES);
|
|
6
|
+
var EvidenceSourceAllSchema = z.enum(ALL_SOURCES);
|
|
7
|
+
var SOURCE_ORDER = {
|
|
8
|
+
AGENT_TRACE: 0,
|
|
9
|
+
INTERNAL_NETWORK: 1,
|
|
10
|
+
PLATFORM: 2
|
|
11
|
+
};
|
|
12
|
+
function compareBySourceSeq(a, b) {
|
|
13
|
+
return SOURCE_ORDER[a.source] - SOURCE_ORDER[b.source] || a.seq - b.seq;
|
|
14
|
+
}
|
|
15
|
+
function chainKey(sessionId, source) {
|
|
16
|
+
return `${sessionId}:${source}`;
|
|
17
|
+
}
|
|
18
|
+
function compareByArrival(a, b) {
|
|
19
|
+
return a.ts.localeCompare(b.ts) || a.receivedAt.localeCompare(b.receivedAt) || a.sessionId.localeCompare(b.sessionId) || compareBySourceSeq(a, b);
|
|
20
|
+
}
|
|
21
|
+
function compareRootFirst(conversationId) {
|
|
22
|
+
return (a, b) => Number(b.id === conversationId) - Number(a.id === conversationId);
|
|
23
|
+
}
|
|
24
|
+
function withinHeads(heads) {
|
|
25
|
+
return (e) => {
|
|
26
|
+
const cap = heads[chainKey(e.sessionId, e.source)];
|
|
27
|
+
return cap !== void 0 && e.seq <= cap;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function compareBySessionSource(a, b) {
|
|
31
|
+
return a.sessionId < b.sessionId ? -1 : a.sessionId > b.sessionId ? 1 : SOURCE_ORDER[a.source] - SOURCE_ORDER[b.source];
|
|
32
|
+
}
|
|
33
|
+
var WIRE_KINDS = [
|
|
34
|
+
"session.open",
|
|
35
|
+
"session.close",
|
|
36
|
+
"intent.declared",
|
|
37
|
+
"llm_call.start",
|
|
38
|
+
"llm_call.end",
|
|
39
|
+
"tool_call.start",
|
|
40
|
+
"tool_call.end",
|
|
41
|
+
"payment.requested",
|
|
42
|
+
"payment.presented",
|
|
43
|
+
"gateway.decision",
|
|
44
|
+
"transport.gap"
|
|
45
|
+
];
|
|
46
|
+
var PLATFORM_KINDS = ["platform.observation", "platform.anomaly"];
|
|
47
|
+
var WireEvidenceKindSchema = z.enum(WIRE_KINDS);
|
|
48
|
+
var PlatformEvidenceKindSchema = z.enum(PLATFORM_KINDS);
|
|
49
|
+
var EvidenceKindSchema = z.enum([...WIRE_KINDS, ...PLATFORM_KINDS]);
|
|
50
|
+
var Hex64Schema = z.string().regex(/^[0-9a-f]{64}$/, "expected 64 lowercase hex chars");
|
|
51
|
+
var SigSchema = z.string().regex(/^ed25519:[A-Za-z0-9_-]{86}$/, "expected ed25519:<base64url>");
|
|
52
|
+
var SESSION_ID_PREFIX = "sess_";
|
|
53
|
+
var SESSION_ID_CHARS = 22;
|
|
54
|
+
var SessionIdSchema = z.string().regex(
|
|
55
|
+
new RegExp(`^${SESSION_ID_PREFIX}[0-9A-Za-z]{${SESSION_ID_CHARS}}$`),
|
|
56
|
+
`expected ${SESSION_ID_PREFIX} followed by ${SESSION_ID_CHARS} base62 chars`
|
|
57
|
+
);
|
|
58
|
+
var SeqSchema = z.number().int().min(0);
|
|
59
|
+
var TimestampSchema = z.iso.datetime({ offset: false });
|
|
60
|
+
var JsonValueSchema = z.lazy(
|
|
61
|
+
() => z.union([
|
|
62
|
+
z.null(),
|
|
63
|
+
z.boolean(),
|
|
64
|
+
z.number().finite(),
|
|
65
|
+
z.string(),
|
|
66
|
+
z.array(JsonValueSchema),
|
|
67
|
+
z.record(z.string(), JsonValueSchema)
|
|
68
|
+
])
|
|
69
|
+
);
|
|
70
|
+
var EvidenceEventSchema = z.strictObject({
|
|
71
|
+
sessionId: SessionIdSchema,
|
|
72
|
+
source: EvidenceSourceSchema,
|
|
73
|
+
seq: SeqSchema,
|
|
74
|
+
ts: TimestampSchema,
|
|
75
|
+
kind: WireEvidenceKindSchema,
|
|
76
|
+
payload: z.record(z.string(), JsonValueSchema),
|
|
77
|
+
prevHash: Hex64Schema,
|
|
78
|
+
sig: SigSchema.optional()
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// ../protocol/src/money.ts
|
|
82
|
+
import { z as z2 } from "zod";
|
|
83
|
+
var DecimalStringSchema = z2.string().regex(/^\d+(\.\d+)?$/, "expected a decimal string");
|
|
84
|
+
var AmountSchema = z2.strictObject({
|
|
85
|
+
/** Decimal string — never a float. For x402 this is the atomic amount (GAP-49). */
|
|
86
|
+
value: DecimalStringSchema,
|
|
87
|
+
/** ISO code, or `<network>/<asset>` for on-chain rails (GAP-49). */
|
|
88
|
+
currency: z2.string().min(1).max(128)
|
|
89
|
+
});
|
|
90
|
+
var ComparableSchema = z2.union([
|
|
91
|
+
DecimalStringSchema.transform((value) => ({ value, currency: null })),
|
|
92
|
+
z2.number().finite().transform(String).pipe(DecimalStringSchema).transform((value) => ({ value, currency: null })),
|
|
93
|
+
z2.object({ value: DecimalStringSchema, currency: z2.string().optional() }).transform((a) => ({ value: a.value, currency: a.currency ?? null }))
|
|
94
|
+
]);
|
|
95
|
+
var Money = class _Money {
|
|
96
|
+
constructor(value, currency) {
|
|
97
|
+
this.value = value;
|
|
98
|
+
this.currency = currency;
|
|
99
|
+
}
|
|
100
|
+
static of(amount) {
|
|
101
|
+
return new _Money(DecimalStringSchema.parse(amount.value), amount.currency);
|
|
102
|
+
}
|
|
103
|
+
/** A decimal with no currency: a bound in a rule, a running total. */
|
|
104
|
+
static bare(value) {
|
|
105
|
+
return new _Money(DecimalStringSchema.parse(value), null);
|
|
106
|
+
}
|
|
107
|
+
/** An x402 amount: atomic units of `asset` on `network`, its currency `<network>/<asset>` (GAP-49). */
|
|
108
|
+
static x402(network, asset, atomic) {
|
|
109
|
+
return new _Money(
|
|
110
|
+
DecimalStringSchema.parse(atomic.toString()),
|
|
111
|
+
_Money.x402Currency(network, asset)
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
static x402Currency(network, asset) {
|
|
115
|
+
return `${network}/${asset}`;
|
|
116
|
+
}
|
|
117
|
+
/** Anything a selector or a rule param may resolve to; null when it is not an amount. */
|
|
118
|
+
static parse(v) {
|
|
119
|
+
const r = ComparableSchema.safeParse(v);
|
|
120
|
+
return r.success ? new _Money(r.data.value, r.data.currency) : null;
|
|
121
|
+
}
|
|
122
|
+
comparable(other) {
|
|
123
|
+
return this.currency === null || other.currency === null || this.currency === other.currency;
|
|
124
|
+
}
|
|
125
|
+
compareTo(other) {
|
|
126
|
+
const [a, b] = _Money.scaled(this.value, other.value);
|
|
127
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
128
|
+
}
|
|
129
|
+
exceeds(other) {
|
|
130
|
+
return this.compareTo(other) > 0;
|
|
131
|
+
}
|
|
132
|
+
plus(other) {
|
|
133
|
+
const [a, b, scale] = _Money.scaled(this.value, other.value);
|
|
134
|
+
const digits = (a + b).toString().padStart(scale + 1, "0");
|
|
135
|
+
const int = digits.slice(0, digits.length - scale);
|
|
136
|
+
const frac = digits.slice(digits.length - scale);
|
|
137
|
+
return new _Money(scale ? `${int}.${frac}` : int, this.currency);
|
|
138
|
+
}
|
|
139
|
+
isZero() {
|
|
140
|
+
return this.toNumber() === 0;
|
|
141
|
+
}
|
|
142
|
+
/** Numeric view for ratios; precision loss is acceptable for a ratio. */
|
|
143
|
+
toNumber() {
|
|
144
|
+
return Number(this.value);
|
|
145
|
+
}
|
|
146
|
+
/** this / other, or null when `other` is zero. */
|
|
147
|
+
ratioTo(other) {
|
|
148
|
+
return other.isZero() ? null : this.toNumber() / other.toNumber();
|
|
149
|
+
}
|
|
150
|
+
toAmount() {
|
|
151
|
+
if (this.currency === null) throw new TypeError("an amount needs a currency");
|
|
152
|
+
return { value: this.value, currency: this.currency };
|
|
153
|
+
}
|
|
154
|
+
static scaled(a, b) {
|
|
155
|
+
const [ai, af = ""] = a.split(".");
|
|
156
|
+
const [bi, bf = ""] = b.split(".");
|
|
157
|
+
const scale = Math.max(af.length, bf.length);
|
|
158
|
+
return [
|
|
159
|
+
BigInt(`${ai}${af.padEnd(scale, "0")}`),
|
|
160
|
+
BigInt(`${bi}${bf.padEnd(scale, "0")}`),
|
|
161
|
+
scale
|
|
162
|
+
];
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
// ../protocol/src/reason-codes.ts
|
|
167
|
+
var INDICATOR_CODES = ["CHAIN_BROKEN", "EVIDENCE_MISMATCH", "SESSION_INVALID"];
|
|
168
|
+
var ANNOTATION_CODES = ["NO_AGENT_TRACE", "NO_POLICY"];
|
|
169
|
+
var REASON_CODE_PATTERN = /^[A-Z][A-Z0-9_]{1,63}$/;
|
|
170
|
+
|
|
171
|
+
// ../protocol/src/selector.ts
|
|
172
|
+
import { z as z3 } from "zod";
|
|
173
|
+
var SelectorSyntaxError = class extends Error {
|
|
174
|
+
constructor(input, at, detail) {
|
|
175
|
+
super(`invalid selector "${input}" at ${at}: ${detail}`);
|
|
176
|
+
this.input = input;
|
|
177
|
+
this.at = at;
|
|
178
|
+
this.name = "SelectorSyntaxError";
|
|
179
|
+
}
|
|
180
|
+
code = "SELECTOR_SYNTAX";
|
|
181
|
+
};
|
|
182
|
+
var IDENT = /^[A-Za-z_][A-Za-z0-9_-]*/;
|
|
183
|
+
var NAME = /^[A-Za-z_][A-Za-z0-9_.-]*/;
|
|
184
|
+
function parsePath(input, from) {
|
|
185
|
+
const path = [];
|
|
186
|
+
let i = from;
|
|
187
|
+
while (i < input.length) {
|
|
188
|
+
const ch = input[i];
|
|
189
|
+
if (ch === ".") {
|
|
190
|
+
const m = IDENT.exec(input.slice(i + 1));
|
|
191
|
+
if (!m) throw new SelectorSyntaxError(input, i + 1, 'expected identifier after "."');
|
|
192
|
+
path.push({ key: m[0] });
|
|
193
|
+
i += 1 + m[0].length;
|
|
194
|
+
} else if (ch === "[") {
|
|
195
|
+
const close = input.indexOf("]", i);
|
|
196
|
+
if (close < 0) throw new SelectorSyntaxError(input, i, 'unterminated "["');
|
|
197
|
+
const body = input.slice(i + 1, close);
|
|
198
|
+
if (!/^\d+$/.test(body))
|
|
199
|
+
throw new SelectorSyntaxError(input, i + 1, "expected integer index");
|
|
200
|
+
path.push({ index: Number(body) });
|
|
201
|
+
i = close + 1;
|
|
202
|
+
} else {
|
|
203
|
+
throw new SelectorSyntaxError(input, i, `unexpected "${ch}"`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return { path, end: i };
|
|
207
|
+
}
|
|
208
|
+
function parseSelector(input) {
|
|
209
|
+
if (typeof input !== "string" || input.length === 0) {
|
|
210
|
+
throw new SelectorSyntaxError(String(input), 0, "empty selector");
|
|
211
|
+
}
|
|
212
|
+
if (input.length > 512)
|
|
213
|
+
throw new SelectorSyntaxError(input.slice(0, 32), 512, "selector too long");
|
|
214
|
+
for (const root of ["payment", "intent", "session"]) {
|
|
215
|
+
if (input === root || input.startsWith(`${root}.`) || input.startsWith(`${root}[`)) {
|
|
216
|
+
return { root, path: parsePath(input, root.length).path };
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (input.startsWith("signal[name=")) {
|
|
220
|
+
const rest = input.slice("signal[name=".length);
|
|
221
|
+
const m = NAME.exec(rest);
|
|
222
|
+
if (!m) throw new SelectorSyntaxError(input, "signal[name=".length, "expected signal name");
|
|
223
|
+
const after = "signal[name=".length + m[0].length;
|
|
224
|
+
if (input[after] !== "]") throw new SelectorSyntaxError(input, after, 'expected "]"');
|
|
225
|
+
return { root: "signal", name: m[0], path: parsePath(input, after + 1).path };
|
|
226
|
+
}
|
|
227
|
+
if (input === "evidence[*]" || input.startsWith("evidence[*]")) {
|
|
228
|
+
return {
|
|
229
|
+
root: "evidence",
|
|
230
|
+
kind: "*",
|
|
231
|
+
all: true,
|
|
232
|
+
path: parsePath(input, "evidence[*]".length).path
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
if (input.startsWith("evidence[kind=")) {
|
|
236
|
+
let i = "evidence[kind=".length;
|
|
237
|
+
const km = NAME.exec(input.slice(i));
|
|
238
|
+
if (!km) throw new SelectorSyntaxError(input, i, "expected evidence kind");
|
|
239
|
+
const kind = km[0];
|
|
240
|
+
i += kind.length;
|
|
241
|
+
let source;
|
|
242
|
+
if (input.startsWith(",source=", i)) {
|
|
243
|
+
i += ",source=".length;
|
|
244
|
+
const sm = /^[A-Z_]+/.exec(input.slice(i));
|
|
245
|
+
if (!sm || !ALL_SOURCES.includes(sm[0])) {
|
|
246
|
+
throw new SelectorSyntaxError(input, i, `expected one of ${ALL_SOURCES.join("|")}`);
|
|
247
|
+
}
|
|
248
|
+
source = sm[0];
|
|
249
|
+
i += sm[0].length;
|
|
250
|
+
}
|
|
251
|
+
if (input[i] !== "]") throw new SelectorSyntaxError(input, i, 'expected "]"');
|
|
252
|
+
i += 1;
|
|
253
|
+
let all = false;
|
|
254
|
+
if (input.startsWith("[*]", i)) {
|
|
255
|
+
all = true;
|
|
256
|
+
i += 3;
|
|
257
|
+
}
|
|
258
|
+
const sel = { root: "evidence", kind, all, path: parsePath(input, i).path };
|
|
259
|
+
if (source) sel.source = source;
|
|
260
|
+
return sel;
|
|
261
|
+
}
|
|
262
|
+
throw new SelectorSyntaxError(
|
|
263
|
+
input,
|
|
264
|
+
0,
|
|
265
|
+
"unknown root (payment | intent | session | signal[name=\u2026] | evidence[\u2026])"
|
|
266
|
+
);
|
|
267
|
+
}
|
|
268
|
+
function isSelectorRef(value) {
|
|
269
|
+
return typeof value === "string" && value.startsWith("@") && value.length > 1;
|
|
270
|
+
}
|
|
271
|
+
function parseSelectorRef(ref) {
|
|
272
|
+
return parseSelector(ref.slice(1));
|
|
273
|
+
}
|
|
274
|
+
function walkPath(value, path) {
|
|
275
|
+
let cur = value;
|
|
276
|
+
for (const seg of path) {
|
|
277
|
+
if (cur === null || cur === void 0) return void 0;
|
|
278
|
+
if ("key" in seg) {
|
|
279
|
+
if (typeof cur !== "object" || Array.isArray(cur)) return void 0;
|
|
280
|
+
cur = cur[seg.key];
|
|
281
|
+
} else {
|
|
282
|
+
if (!Array.isArray(cur)) return void 0;
|
|
283
|
+
cur = cur[seg.index];
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
return cur;
|
|
287
|
+
}
|
|
288
|
+
var SelectorStringSchema = z3.string().refine(
|
|
289
|
+
(s) => {
|
|
290
|
+
try {
|
|
291
|
+
parseSelector(s);
|
|
292
|
+
return true;
|
|
293
|
+
} catch {
|
|
294
|
+
return false;
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
{ message: "invalid selector" }
|
|
298
|
+
);
|
|
299
|
+
|
|
300
|
+
// ../protocol/src/policy.ts
|
|
301
|
+
import { z as z4 } from "zod";
|
|
302
|
+
var PrimitiveSchema = z4.enum(["THRESHOLD", "MEMBERSHIP", "MATCH", "PRESENCE", "FRESHNESS"]);
|
|
303
|
+
var RuleVerdictSchema = z4.enum(["DENY", "REVIEW"]);
|
|
304
|
+
var RuleSchema = z4.strictObject({
|
|
305
|
+
id: z4.string().min(1).max(64),
|
|
306
|
+
primitive: PrimitiveSchema,
|
|
307
|
+
selector: SelectorStringSchema,
|
|
308
|
+
params: z4.record(z4.string(), JsonValueSchema),
|
|
309
|
+
onFail: RuleVerdictSchema,
|
|
310
|
+
/** What to do when the selector (or a `@ref`) resolves to nothing. Default: skip (GAP-44). */
|
|
311
|
+
onMissing: z4.enum(["skip", "DENY", "REVIEW"]).optional(),
|
|
312
|
+
/** Reason code surfaced to the caller when the rule fails (GAP-44). */
|
|
313
|
+
code: z4.string().regex(REASON_CODE_PATTERN)
|
|
314
|
+
});
|
|
315
|
+
var PolicyRulesSchema = z4.strictObject({
|
|
316
|
+
rules: z4.array(RuleSchema).max(200).refine((rules) => new Set(rules.map((r) => r.id)).size === rules.length, {
|
|
317
|
+
message: "rule ids must be unique"
|
|
318
|
+
})
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// ../protocol/src/api.ts
|
|
322
|
+
import { z as z5 } from "zod";
|
|
323
|
+
var DeclaredIntentSchema = z5.strictObject({
|
|
324
|
+
mandate: z5.string().min(1).max(4e3),
|
|
325
|
+
maxAmount: AmountSchema.optional(),
|
|
326
|
+
merchantAllowlist: z5.array(z5.string().min(1)).optional(),
|
|
327
|
+
validUntil: TimestampSchema
|
|
328
|
+
});
|
|
329
|
+
var CreateSessionInputSchema = z5.strictObject({
|
|
330
|
+
source: EvidenceSourceSchema,
|
|
331
|
+
/** Buyer-born sessions. `credential` is accepted and not kept: the DID is the credential (GAP-11). */
|
|
332
|
+
agent: z5.strictObject({ did: z5.string().min(1), credential: z5.string().min(1) }).optional(),
|
|
333
|
+
intent: DeclaredIntentSchema.optional(),
|
|
334
|
+
/** Any session of a conversation to continue: the new session joins it as a fresh chain (GAP-84). AGENT_TRACE only. */
|
|
335
|
+
resume: SessionIdSchema.optional()
|
|
336
|
+
}).describe("session input");
|
|
337
|
+
var MAX_BATCH_EVENTS = 500;
|
|
338
|
+
var EvidenceBatchInputSchema = z5.array(z5.unknown()).min(1).max(MAX_BATCH_EVENTS).describe("evidence batch");
|
|
339
|
+
var PaymentSummarySchema = z5.strictObject({
|
|
340
|
+
protocol: z5.string().min(1),
|
|
341
|
+
payee: z5.string().min(1),
|
|
342
|
+
amount: AmountSchema,
|
|
343
|
+
/** Not in the RFC; needed by `principal-links` and `EVIDENCE_MISMATCH` (GAP-06). */
|
|
344
|
+
payer: z5.string().min(1).optional()
|
|
345
|
+
});
|
|
346
|
+
var EvaluateInputSchema = z5.strictObject({
|
|
347
|
+
sessionId: SessionIdSchema,
|
|
348
|
+
payment: PaymentSummarySchema,
|
|
349
|
+
/** The caller's own id for the call under decision: the same id within one conversation reads the decision already taken (GAP-84). */
|
|
350
|
+
callId: z5.string().min(1).max(256).optional()
|
|
351
|
+
}).describe("evaluate input");
|
|
352
|
+
var CreatePolicyInputSchema = z5.strictObject({ rules: PolicyRulesSchema }).describe("policy input");
|
|
353
|
+
var ApiErrorSchema = z5.strictObject({
|
|
354
|
+
error: z5.strictObject({
|
|
355
|
+
code: z5.string().min(1),
|
|
356
|
+
message: z5.string().min(1),
|
|
357
|
+
details: JsonValueSchema.optional(),
|
|
358
|
+
request_id: z5.string().optional()
|
|
359
|
+
})
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
// ../protocol/src/payloads/index.ts
|
|
363
|
+
import { z as z6 } from "zod";
|
|
364
|
+
var Json = JsonValueSchema;
|
|
365
|
+
var JsonRecord = z6.record(z6.string(), Json);
|
|
366
|
+
var SessionOpenPayloadSchema = z6.looseObject({
|
|
367
|
+
runtime: z6.looseObject({
|
|
368
|
+
sdk: z6.string().min(1),
|
|
369
|
+
version: z6.string().min(1),
|
|
370
|
+
framework: z6.string().optional(),
|
|
371
|
+
model: z6.string().optional()
|
|
372
|
+
}),
|
|
373
|
+
/** Runtime / repo attestations read by the (staged) Attestation Assurance score. */
|
|
374
|
+
attestations: JsonRecord.optional()
|
|
375
|
+
});
|
|
376
|
+
var SESSION_CLOSE_REASONS = ["completed", "settled", "aborted", "expired"];
|
|
377
|
+
var SessionClosePayloadSchema = z6.looseObject({
|
|
378
|
+
reason: z6.enum(SESSION_CLOSE_REASONS),
|
|
379
|
+
transaction: z6.string().optional()
|
|
380
|
+
});
|
|
381
|
+
var IntentDeclaredPayloadSchema = DeclaredIntentSchema.loose();
|
|
382
|
+
var PromptDeltaSchema = z6.strictObject({
|
|
383
|
+
prior: z6.string().min(1),
|
|
384
|
+
shared: z6.number().int().min(0),
|
|
385
|
+
messages: z6.array(Json)
|
|
386
|
+
});
|
|
387
|
+
var LlmCallParamsSchema = z6.looseObject({
|
|
388
|
+
prompt: Json.optional(),
|
|
389
|
+
promptDelta: PromptDeltaSchema.optional()
|
|
390
|
+
}).refine((p) => p.prompt === void 0 || p.promptDelta === void 0, {
|
|
391
|
+
message: "params carries prompt or promptDelta, not both"
|
|
392
|
+
});
|
|
393
|
+
var LlmCallStartPayloadSchema = z6.looseObject({
|
|
394
|
+
callId: z6.string().min(1),
|
|
395
|
+
// GAP-07
|
|
396
|
+
provider: z6.string().min(1),
|
|
397
|
+
modelId: z6.string().min(1),
|
|
398
|
+
params: LlmCallParamsSchema
|
|
399
|
+
});
|
|
400
|
+
var LlmCallEndPayloadSchema = z6.looseObject({
|
|
401
|
+
callId: z6.string().min(1),
|
|
402
|
+
content: Json.optional(),
|
|
403
|
+
finishReason: z6.string().optional(),
|
|
404
|
+
usage: JsonRecord.optional(),
|
|
405
|
+
durationMs: z6.number().int().min(0),
|
|
406
|
+
error: z6.looseObject({ name: z6.string(), message: z6.string() }).optional()
|
|
407
|
+
});
|
|
408
|
+
var ToolCallStartPayloadSchema = z6.looseObject({
|
|
409
|
+
callId: z6.string().min(1),
|
|
410
|
+
// GAP-07
|
|
411
|
+
toolName: z6.string().min(1),
|
|
412
|
+
input: Json,
|
|
413
|
+
transport: z6.enum(["local", "mcp"]).optional(),
|
|
414
|
+
server: z6.string().optional()
|
|
415
|
+
});
|
|
416
|
+
var ToolCallEndPayloadSchema = z6.looseObject({
|
|
417
|
+
callId: z6.string().min(1),
|
|
418
|
+
output: Json.optional(),
|
|
419
|
+
error: z6.looseObject({ name: z6.string(), message: z6.string() }).optional(),
|
|
420
|
+
durationMs: z6.number().int().min(0)
|
|
421
|
+
});
|
|
422
|
+
var PAYMENT_ARTIFACTS = [
|
|
423
|
+
"http-402",
|
|
424
|
+
"mrtr-input-required",
|
|
425
|
+
"payment-signature",
|
|
426
|
+
"checkout-url"
|
|
427
|
+
];
|
|
428
|
+
var PaymentMomentPayloadSchema = z6.looseObject({
|
|
429
|
+
protocol: z6.string().min(1),
|
|
430
|
+
payee: z6.string().min(1),
|
|
431
|
+
amount: z6.looseObject({ value: z6.string().min(1), currency: z6.string().min(1) }),
|
|
432
|
+
payer: z6.string().optional(),
|
|
433
|
+
artifact: z6.enum(PAYMENT_ARTIFACTS),
|
|
434
|
+
raw: JsonRecord
|
|
435
|
+
});
|
|
436
|
+
var GatewayDecisionPayloadSchema = z6.looseObject({
|
|
437
|
+
gateway: z6.string().min(1),
|
|
438
|
+
call: z6.looseObject({ tool: z6.string().min(1), args: Json.optional() }),
|
|
439
|
+
decision: z6.enum(["ALLOW", "DENY", "REVIEW", "CHALLENGE"]),
|
|
440
|
+
reasonCodes: z6.array(z6.string()),
|
|
441
|
+
/** The gateway's own record of what it decided on — a black box to the platform. */
|
|
442
|
+
record: JsonRecord
|
|
443
|
+
});
|
|
444
|
+
var TransportGapPayloadSchema = z6.looseObject({
|
|
445
|
+
dropped: z6.number().int().min(1),
|
|
446
|
+
oversized: z6.number().int().min(0).optional(),
|
|
447
|
+
firstTs: z6.string(),
|
|
448
|
+
lastTs: z6.string()
|
|
449
|
+
});
|
|
450
|
+
var PlatformObservationPayloadSchema = z6.strictObject({
|
|
451
|
+
origin: z6.string().min(1),
|
|
452
|
+
params: Json,
|
|
453
|
+
response: Json
|
|
454
|
+
});
|
|
455
|
+
var ANOMALY_TYPES = [
|
|
456
|
+
"fork",
|
|
457
|
+
"seq_gap",
|
|
458
|
+
"prev_hash_mismatch",
|
|
459
|
+
"bad_sig",
|
|
460
|
+
"clock_skew",
|
|
461
|
+
"source_conflict"
|
|
462
|
+
];
|
|
463
|
+
var PlatformAnomalyPayloadSchema = z6.looseObject({
|
|
464
|
+
type: z6.enum(ANOMALY_TYPES),
|
|
465
|
+
source: EvidenceSourceAllSchema,
|
|
466
|
+
seq: z6.number().int().min(0).nullable(),
|
|
467
|
+
detail: JsonRecord
|
|
468
|
+
});
|
|
469
|
+
var REGISTRY = {
|
|
470
|
+
"session.open": SessionOpenPayloadSchema,
|
|
471
|
+
"session.close": SessionClosePayloadSchema,
|
|
472
|
+
"intent.declared": IntentDeclaredPayloadSchema,
|
|
473
|
+
"llm_call.start": LlmCallStartPayloadSchema,
|
|
474
|
+
"llm_call.end": LlmCallEndPayloadSchema,
|
|
475
|
+
"tool_call.start": ToolCallStartPayloadSchema,
|
|
476
|
+
"tool_call.end": ToolCallEndPayloadSchema,
|
|
477
|
+
"payment.requested": PaymentMomentPayloadSchema,
|
|
478
|
+
"payment.presented": PaymentMomentPayloadSchema,
|
|
479
|
+
"gateway.decision": GatewayDecisionPayloadSchema,
|
|
480
|
+
"transport.gap": TransportGapPayloadSchema,
|
|
481
|
+
"platform.observation": PlatformObservationPayloadSchema,
|
|
482
|
+
"platform.anomaly": PlatformAnomalyPayloadSchema
|
|
483
|
+
};
|
|
484
|
+
function payloadSchemaFor(kind) {
|
|
485
|
+
return REGISTRY[kind];
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// ../protocol/src/verdict.ts
|
|
489
|
+
var SEVERITY = { ALLOW: 0, REVIEW: 1, DENY: 2 };
|
|
490
|
+
var Verdict = class _Verdict {
|
|
491
|
+
constructor(value) {
|
|
492
|
+
this.value = value;
|
|
493
|
+
}
|
|
494
|
+
static ALLOW = new _Verdict("ALLOW");
|
|
495
|
+
static REVIEW = new _Verdict("REVIEW");
|
|
496
|
+
static DENY = new _Verdict("DENY");
|
|
497
|
+
static of(value) {
|
|
498
|
+
return value === "DENY" ? _Verdict.DENY : value === "REVIEW" ? _Verdict.REVIEW : _Verdict.ALLOW;
|
|
499
|
+
}
|
|
500
|
+
/** The more severe of the two. */
|
|
501
|
+
atLeast(other) {
|
|
502
|
+
const o = typeof other === "string" ? _Verdict.of(other) : other;
|
|
503
|
+
return SEVERITY[o.value] > SEVERITY[this.value] ? o : this;
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
export {
|
|
508
|
+
ALL_SOURCES,
|
|
509
|
+
EvidenceSourceSchema,
|
|
510
|
+
EvidenceSourceAllSchema,
|
|
511
|
+
SOURCE_ORDER,
|
|
512
|
+
compareBySourceSeq,
|
|
513
|
+
chainKey,
|
|
514
|
+
compareByArrival,
|
|
515
|
+
compareRootFirst,
|
|
516
|
+
withinHeads,
|
|
517
|
+
compareBySessionSource,
|
|
518
|
+
Hex64Schema,
|
|
519
|
+
SESSION_ID_PREFIX,
|
|
520
|
+
SESSION_ID_CHARS,
|
|
521
|
+
SessionIdSchema,
|
|
522
|
+
TimestampSchema,
|
|
523
|
+
JsonValueSchema,
|
|
524
|
+
EvidenceEventSchema,
|
|
525
|
+
DecimalStringSchema,
|
|
526
|
+
AmountSchema,
|
|
527
|
+
Money,
|
|
528
|
+
INDICATOR_CODES,
|
|
529
|
+
ANNOTATION_CODES,
|
|
530
|
+
REASON_CODE_PATTERN,
|
|
531
|
+
parseSelector,
|
|
532
|
+
isSelectorRef,
|
|
533
|
+
parseSelectorRef,
|
|
534
|
+
walkPath,
|
|
535
|
+
SelectorStringSchema,
|
|
536
|
+
PolicyRulesSchema,
|
|
537
|
+
DeclaredIntentSchema,
|
|
538
|
+
CreateSessionInputSchema,
|
|
539
|
+
MAX_BATCH_EVENTS,
|
|
540
|
+
EvidenceBatchInputSchema,
|
|
541
|
+
PaymentSummarySchema,
|
|
542
|
+
EvaluateInputSchema,
|
|
543
|
+
CreatePolicyInputSchema,
|
|
544
|
+
ApiErrorSchema,
|
|
545
|
+
payloadSchemaFor,
|
|
546
|
+
Verdict
|
|
547
|
+
};
|
|
@@ -6,7 +6,10 @@ import {
|
|
|
6
6
|
} from "./chunk-FQDHFTVR.js";
|
|
7
7
|
import {
|
|
8
8
|
payloadDigest
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-UXXSE643.js";
|
|
10
|
+
import {
|
|
11
|
+
Money
|
|
12
|
+
} from "./chunk-FNU4CRJJ.js";
|
|
10
13
|
|
|
11
14
|
// src/x402/binding.ts
|
|
12
15
|
var SESSION_EXTENSION = "beltic.sessionId";
|
|
@@ -17,9 +20,7 @@ function sessionIdOf(extensions) {
|
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
// src/x402/moments.ts
|
|
20
|
-
|
|
21
|
-
return `${network}/${asset}`;
|
|
22
|
-
}
|
|
23
|
+
import { z } from "zod";
|
|
23
24
|
function x402Summary(accepts, opts = {}) {
|
|
24
25
|
const payer = opts.payer?.toLowerCase();
|
|
25
26
|
return {
|
|
@@ -27,7 +28,7 @@ function x402Summary(accepts, opts = {}) {
|
|
|
27
28
|
payee: accepts?.payTo ?? "unknown",
|
|
28
29
|
amount: {
|
|
29
30
|
value: accepts?.amount ?? "0",
|
|
30
|
-
currency: accepts ? x402Currency(accepts.network ?? "unknown", accepts.asset ?? "unknown") : "unknown"
|
|
31
|
+
currency: accepts ? Money.x402Currency(accepts.network ?? "unknown", accepts.asset ?? "unknown") : "unknown"
|
|
31
32
|
},
|
|
32
33
|
...payer ? { payer } : {}
|
|
33
34
|
};
|
|
@@ -64,7 +65,7 @@ var x402Moments = {
|
|
|
64
65
|
return fromAccepts(
|
|
65
66
|
{
|
|
66
67
|
payTo: typeof accepts.payTo === "string" ? accepts.payTo : "dynamic",
|
|
67
|
-
amount:
|
|
68
|
+
amount: PriceSchema.parse(accepts.price),
|
|
68
69
|
network: accepts.network,
|
|
69
70
|
asset: extra?.asset ?? "route"
|
|
70
71
|
},
|
|
@@ -82,13 +83,11 @@ var x402Moments = {
|
|
|
82
83
|
);
|
|
83
84
|
}
|
|
84
85
|
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return "0";
|
|
91
|
-
}
|
|
86
|
+
var PriceSchema = z.union([
|
|
87
|
+
z.string().transform((s) => s.replace(/[^0-9.]/g, "") || "0"),
|
|
88
|
+
z.number().transform(String),
|
|
89
|
+
z.object({ amount: z.string() }).transform((p) => p.amount)
|
|
90
|
+
]).catch("0");
|
|
92
91
|
|
|
93
92
|
// src/x402/adapter.ts
|
|
94
93
|
function attachX402(beltic, server, http) {
|
|
@@ -137,7 +136,6 @@ function attachX402(beltic, server, http) {
|
|
|
137
136
|
export {
|
|
138
137
|
SESSION_EXTENSION,
|
|
139
138
|
SESSION_HEADER,
|
|
140
|
-
x402Currency,
|
|
141
139
|
x402Summary,
|
|
142
140
|
x402Moments,
|
|
143
141
|
attachX402
|