@belticlabs/agent-risk-sdk 0.1.0 → 0.2.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/adapter-CF-cUYSA.d.ts +28 -0
- package/dist/ai/index.d.ts +6 -6
- package/dist/ai/index.js +4 -3
- package/dist/{chunk-GCKCAKHA.js → chunk-46QN2KEZ.js} +199 -33
- package/dist/chunk-GSH5APZW.js +39 -0
- package/dist/{chunk-U5Z5Z2BQ.js → chunk-LM4NIYE5.js} +26 -20
- package/dist/{chunk-YVMJ5CZX.js → chunk-SO6HPRJT.js} +10 -23
- package/dist/chunk-U7HM37CB.js +17 -0
- package/dist/client-C-mV_3A0.d.ts +91 -0
- package/dist/index-Bjs3BPPU.d.ts +547 -0
- package/dist/index.d.ts +10 -8
- package/dist/index.js +117 -16
- package/dist/mcp/index.d.ts +6 -5
- package/dist/mcp/index.js +11 -8
- package/dist/middleware-BwAYpBTl.d.ts +15 -0
- package/dist/protocol/index.d.ts +158 -0
- package/dist/protocol/index.js +152 -0
- package/dist/seller/anti-fraud-gateway.d.ts +14 -4
- package/dist/seller/anti-fraud-gateway.js +13 -14
- package/dist/{session-BMNB1N1g.d.ts → session-5TClPLI4.d.ts} +70 -4
- package/dist/verdict-BAahb5po.d.ts +26 -0
- package/dist/x402/express.d.ts +7 -6
- package/dist/x402/express.js +6 -5
- package/dist/x402/hono.d.ts +7 -6
- package/dist/x402/hono.js +6 -5
- package/dist/x402/index.d.ts +95 -19
- package/dist/x402/index.js +83 -32
- package/package.json +5 -1
- package/dist/chunk-5TWO73OD.js +0 -35
- package/dist/client-CVx9LgJC.d.ts +0 -63
- package/dist/middleware-_DSwvNIx.d.ts +0 -40
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
+
openCall,
|
|
2
3
|
recordCall
|
|
3
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-GSH5APZW.js";
|
|
4
5
|
import {
|
|
5
6
|
Chain,
|
|
6
7
|
didKeyFromEd25519,
|
|
@@ -8,11 +9,13 @@ import {
|
|
|
8
9
|
memorySigner,
|
|
9
10
|
toHex
|
|
10
11
|
} from "./chunk-SFGM7KOG.js";
|
|
12
|
+
import {
|
|
13
|
+
Verdict
|
|
14
|
+
} from "./chunk-46QN2KEZ.js";
|
|
11
15
|
import {
|
|
12
16
|
presentedFrom,
|
|
13
17
|
summaryOf
|
|
14
18
|
} from "./chunk-7G5EHNVW.js";
|
|
15
|
-
import "./chunk-GCKCAKHA.js";
|
|
16
19
|
|
|
17
20
|
// src/core/api-client.ts
|
|
18
21
|
var BelticApiError = class extends Error {
|
|
@@ -136,8 +139,26 @@ var Session = class {
|
|
|
136
139
|
get droppedCount() {
|
|
137
140
|
return this.dropped;
|
|
138
141
|
}
|
|
139
|
-
/**
|
|
142
|
+
/**
|
|
143
|
+
* Resolves once the event is sequenced and buffered — not once it is
|
|
144
|
+
* acknowledged. `false` when the event was dropped, or (fail-open) when
|
|
145
|
+
* the chain can no longer take it.
|
|
146
|
+
*/
|
|
140
147
|
async emit(kind, payload) {
|
|
148
|
+
if (!this.deps.failOpen) return this.chainEvent(kind, payload);
|
|
149
|
+
try {
|
|
150
|
+
return await this.chainEvent(kind, payload);
|
|
151
|
+
} catch (err) {
|
|
152
|
+
this.deps.onError?.(err);
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/** The tool call whose `execute` the host runs itself; see `ToolCallSpan`. */
|
|
157
|
+
toolCall(call) {
|
|
158
|
+
const { callId, ...start } = call;
|
|
159
|
+
return openCall(this, "tool_call", callId, { transport: "local", ...start });
|
|
160
|
+
}
|
|
161
|
+
async chainEvent(kind, payload) {
|
|
141
162
|
const halted = this.deps.transport.haltedError(this.id, this.source);
|
|
142
163
|
if (halted) throw halted;
|
|
143
164
|
const ts = this.now().toISOString();
|
|
@@ -186,6 +207,7 @@ var Session = class {
|
|
|
186
207
|
return run;
|
|
187
208
|
}
|
|
188
209
|
};
|
|
210
|
+
var DEFAULT_OPEN_RETRY_MS = 6e4;
|
|
189
211
|
var Sessions = class {
|
|
190
212
|
constructor(deps) {
|
|
191
213
|
this.deps = deps;
|
|
@@ -197,18 +219,65 @@ var Sessions = class {
|
|
|
197
219
|
* mid-session still loses the head (GAP-67).
|
|
198
220
|
*/
|
|
199
221
|
attached = /* @__PURE__ */ new Map();
|
|
222
|
+
/** Buyer sessions by the host's own key (GAP-71). */
|
|
223
|
+
opened = /* @__PURE__ */ new Map();
|
|
224
|
+
retryAt = 0;
|
|
225
|
+
/**
|
|
226
|
+
* Buyer half: the evidence session for a key of the host's own (its
|
|
227
|
+
* session, run or conversation id), opened on first use and reused
|
|
228
|
+
* after. A halted chain is reopened as a fresh session that continues
|
|
229
|
+
* the same key; a closed key is forgotten. When the platform refuses to
|
|
230
|
+
* open one, a fail-open client resolves null — the host runs without
|
|
231
|
+
* evidence — until `openRetryMs` has passed (GAP-71); otherwise the
|
|
232
|
+
* refusal is thrown and the next call tries again. The identity is
|
|
233
|
+
* required either way: that is configuration.
|
|
234
|
+
*/
|
|
235
|
+
open(key, input = {}) {
|
|
236
|
+
const prior = this.opened.get(key) ?? Promise.resolve(null);
|
|
237
|
+
const next = prior.catch(() => null).then(
|
|
238
|
+
(session) => session && !this.deps.transport.haltedError(session.id, session.source) ? session : this.openFresh(input, () => this.forget(key, next))
|
|
239
|
+
);
|
|
240
|
+
this.opened.set(key, next);
|
|
241
|
+
next.catch(() => this.forget(key, next));
|
|
242
|
+
return next;
|
|
243
|
+
}
|
|
244
|
+
forget(key, entry) {
|
|
245
|
+
if (this.opened.get(key) === entry) this.opened.delete(key);
|
|
246
|
+
}
|
|
247
|
+
async openFresh(input, onClosed) {
|
|
248
|
+
this.identityFor("open");
|
|
249
|
+
const create = async () => this.create(typeof input === "function" ? await input() : input, onClosed);
|
|
250
|
+
if (!this.deps.failOpen) return create();
|
|
251
|
+
if (Date.now() < this.retryAt) return null;
|
|
252
|
+
try {
|
|
253
|
+
const session = await create();
|
|
254
|
+
this.retryAt = 0;
|
|
255
|
+
return session;
|
|
256
|
+
} catch (err) {
|
|
257
|
+
this.retryAt = Date.now() + (this.deps.openRetryMs ?? DEFAULT_OPEN_RETRY_MS);
|
|
258
|
+
this.deps.onError?.(err);
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
200
262
|
/** Buyer half: create an AGENT_TRACE session bound to the agent identity, then announce it on the chain. */
|
|
201
|
-
|
|
263
|
+
start(input = {}) {
|
|
264
|
+
return this.create(input);
|
|
265
|
+
}
|
|
266
|
+
identityFor(entry) {
|
|
202
267
|
const identity = this.deps.identity;
|
|
203
268
|
if (!identity)
|
|
204
|
-
throw new Error(
|
|
269
|
+
throw new Error(`sessions.${entry} needs an agent identity (createBeltic({ identity }))`);
|
|
270
|
+
return identity;
|
|
271
|
+
}
|
|
272
|
+
async create(input, onClosed) {
|
|
273
|
+
const identity = this.identityFor("start");
|
|
205
274
|
const body = {
|
|
206
275
|
source: "AGENT_TRACE",
|
|
207
276
|
agent: { did: identity.did, credential: identity.credential ?? identity.did },
|
|
208
277
|
...input.intent ? { intent: input.intent } : {}
|
|
209
278
|
};
|
|
210
279
|
const out = await this.deps.api.post("/v1/sessions", body);
|
|
211
|
-
const session = this.attach(out.sessionId, "AGENT_TRACE", out.expiresAt, "buyer");
|
|
280
|
+
const session = this.attach(out.sessionId, "AGENT_TRACE", out.expiresAt, "buyer", onClosed);
|
|
212
281
|
await session.emit("session.open", {
|
|
213
282
|
runtime: {
|
|
214
283
|
sdk: "@belticlabs/agent-risk-sdk",
|
|
@@ -228,7 +297,7 @@ var Sessions = class {
|
|
|
228
297
|
});
|
|
229
298
|
return this.attach(out.sessionId, "INTERNAL_NETWORK", out.expiresAt, "seller");
|
|
230
299
|
}
|
|
231
|
-
attach(id, source, expiresAt, born) {
|
|
300
|
+
attach(id, source, expiresAt, born, onClosed) {
|
|
232
301
|
const key = `${id}:${source}`;
|
|
233
302
|
const existing = this.attached.get(key);
|
|
234
303
|
if (existing) return existing;
|
|
@@ -238,7 +307,12 @@ var Sessions = class {
|
|
|
238
307
|
signer: source === "AGENT_TRACE" ? this.deps.identity?.signer : void 0,
|
|
239
308
|
redact: this.deps.redact,
|
|
240
309
|
now: this.deps.now,
|
|
241
|
-
|
|
310
|
+
failOpen: this.deps.failOpen,
|
|
311
|
+
onError: this.deps.onError,
|
|
312
|
+
onClosed: () => {
|
|
313
|
+
this.attached.delete(key);
|
|
314
|
+
onClosed?.();
|
|
315
|
+
}
|
|
242
316
|
},
|
|
243
317
|
id,
|
|
244
318
|
source,
|
|
@@ -412,7 +486,7 @@ var Transport = class {
|
|
|
412
486
|
};
|
|
413
487
|
|
|
414
488
|
// src/client.ts
|
|
415
|
-
var SDK_VERSION = "0.
|
|
489
|
+
var SDK_VERSION = "0.2.0";
|
|
416
490
|
var Beltic = class {
|
|
417
491
|
api;
|
|
418
492
|
transport;
|
|
@@ -420,28 +494,54 @@ var Beltic = class {
|
|
|
420
494
|
identity;
|
|
421
495
|
correlation;
|
|
422
496
|
onReview;
|
|
497
|
+
failOpen;
|
|
498
|
+
onError;
|
|
423
499
|
constructor(opts) {
|
|
500
|
+
this.failOpen = opts.failOpen ?? false;
|
|
501
|
+
this.onError = opts.onError ?? ((err) => console.error("[beltic]", err));
|
|
424
502
|
this.api = new ApiClient({ ...opts, userAgent: `@belticlabs/agent-risk-sdk/${SDK_VERSION}` });
|
|
425
|
-
this.transport = new Transport(this.api,
|
|
503
|
+
this.transport = new Transport(this.api, {
|
|
504
|
+
onError: this.onError,
|
|
505
|
+
onChainHalted: this.onError,
|
|
506
|
+
...opts.transport
|
|
507
|
+
});
|
|
426
508
|
this.sessions = new Sessions({
|
|
427
509
|
api: this.api,
|
|
428
510
|
transport: this.transport,
|
|
429
511
|
sdkVersion: SDK_VERSION,
|
|
430
512
|
identity: opts.identity,
|
|
431
513
|
redact: opts.redact,
|
|
432
|
-
now: opts.now
|
|
514
|
+
now: opts.now,
|
|
515
|
+
failOpen: this.failOpen,
|
|
516
|
+
onError: this.onError,
|
|
517
|
+
openRetryMs: opts.openRetryMs
|
|
433
518
|
});
|
|
434
519
|
this.identity = opts.identity;
|
|
435
520
|
this.correlation = opts.correlation ?? new MemoryCorrelationStore();
|
|
436
521
|
this.onReview = opts.onReview ?? "abort";
|
|
437
522
|
}
|
|
438
|
-
/**
|
|
523
|
+
/**
|
|
524
|
+
* The platform's verdict on a payment — the seller's before it verifies,
|
|
525
|
+
* the buyer's before it presents. Read-your-writes: the buffered evidence
|
|
526
|
+
* is flushed first so the platform judges what the caller already saw
|
|
527
|
+
* (GAP-16). A recorded moment is accepted as is: only its comparable core
|
|
528
|
+
* (payee, amount, payer) is sent. `null` only under `failOpen`, when the
|
|
529
|
+
* platform could not be asked.
|
|
530
|
+
*/
|
|
439
531
|
async evaluate(sessionId, payment) {
|
|
532
|
+
const input = { sessionId, payment: summaryOf(payment) };
|
|
533
|
+
if (!this.failOpen) return this.decide(input);
|
|
534
|
+
try {
|
|
535
|
+
return await this.decide(input);
|
|
536
|
+
} catch (err) {
|
|
537
|
+
this.onError(err);
|
|
538
|
+
return null;
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
async decide(input) {
|
|
440
542
|
await this.transport.flush();
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
payment
|
|
444
|
-
});
|
|
543
|
+
const out = await this.api.post("/v1/evaluate", input);
|
|
544
|
+
return { ...out, verdict: Verdict.of(out.decision) };
|
|
445
545
|
}
|
|
446
546
|
flush() {
|
|
447
547
|
return this.transport.flush();
|
|
@@ -482,6 +582,7 @@ export {
|
|
|
482
582
|
Beltic,
|
|
483
583
|
BelticApiError,
|
|
484
584
|
ChainRejectedError,
|
|
585
|
+
DEFAULT_OPEN_RETRY_MS,
|
|
485
586
|
DEFAULT_TRANSPORT,
|
|
486
587
|
MemoryCorrelationStore,
|
|
487
588
|
SDK_VERSION,
|
package/dist/mcp/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { PaymentSummary, PaymentMomentPayload } from '
|
|
2
|
-
import { B as Beltic } from '../client-
|
|
3
|
-
import { S as Session } from '../session-
|
|
4
|
-
import '
|
|
1
|
+
import { a as PaymentSummary, P as PaymentMomentPayload } from '../index-Bjs3BPPU.js';
|
|
2
|
+
import { B as Beltic } from '../client-C-mV_3A0.js';
|
|
3
|
+
import { S as Session } from '../session-5TClPLI4.js';
|
|
4
|
+
import 'zod';
|
|
5
|
+
import '../verdict-BAahb5po.js';
|
|
5
6
|
|
|
6
7
|
interface CallToolParams {
|
|
7
8
|
name: string;
|
|
@@ -12,7 +13,7 @@ interface CallToolParams {
|
|
|
12
13
|
interface McpClientLike {
|
|
13
14
|
callTool(params: CallToolParams, ...rest: unknown[]): Promise<unknown>;
|
|
14
15
|
}
|
|
15
|
-
declare function wrapClient<C extends McpClientLike>(session: Session, client: C, opts?: {
|
|
16
|
+
declare function wrapClient<C extends McpClientLike>(session: Session | null | undefined, client: C, opts?: {
|
|
16
17
|
server?: string;
|
|
17
18
|
}): C;
|
|
18
19
|
/**
|
package/dist/mcp/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
recordCall
|
|
3
|
-
} from "../chunk-
|
|
3
|
+
} from "../chunk-GSH5APZW.js";
|
|
4
4
|
import {
|
|
5
5
|
SESSION_EXTENSION
|
|
6
6
|
} from "../chunk-VM7MK43J.js";
|
|
@@ -8,21 +8,22 @@ import {
|
|
|
8
8
|
uuidv7
|
|
9
9
|
} from "../chunk-SFGM7KOG.js";
|
|
10
10
|
import {
|
|
11
|
-
|
|
12
|
-
} from "../chunk-
|
|
11
|
+
Verdict
|
|
12
|
+
} from "../chunk-46QN2KEZ.js";
|
|
13
13
|
import {
|
|
14
14
|
presentedFrom
|
|
15
15
|
} from "../chunk-7G5EHNVW.js";
|
|
16
|
+
import {
|
|
17
|
+
x402Moments
|
|
18
|
+
} from "../chunk-LM4NIYE5.js";
|
|
16
19
|
import {
|
|
17
20
|
toJson,
|
|
18
21
|
toJsonObject
|
|
19
22
|
} from "../chunk-FQDHFTVR.js";
|
|
20
|
-
import {
|
|
21
|
-
Verdict
|
|
22
|
-
} from "../chunk-GCKCAKHA.js";
|
|
23
23
|
|
|
24
24
|
// src/mcp/index.ts
|
|
25
25
|
function wrapClient(session, client, opts = {}) {
|
|
26
|
+
if (!session) return client;
|
|
26
27
|
const original = client.callTool.bind(client);
|
|
27
28
|
const callTool = (params, ...rest) => recordCall(
|
|
28
29
|
session,
|
|
@@ -85,8 +86,10 @@ function withBeltic(beltic, handler, opts) {
|
|
|
85
86
|
presentedFrom(payment, { tool: opts.toolName, args: toJson(args) })
|
|
86
87
|
);
|
|
87
88
|
const out = await beltic.evaluate(session.id, payment);
|
|
88
|
-
|
|
89
|
-
|
|
89
|
+
if (out) {
|
|
90
|
+
verdict = out.verdict;
|
|
91
|
+
reasonCodes = out.reasonCodes;
|
|
92
|
+
}
|
|
90
93
|
}
|
|
91
94
|
await session.emit("gateway.decision", {
|
|
92
95
|
gateway,
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { PaywallConfig } from '@x402/core/server';
|
|
2
|
+
import { A as AttachOptions } from './adapter-CF-cUYSA.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The seller half as one middleware: the framework's `@x402/*` payment
|
|
6
|
+
* middleware over an `x402HTTPResourceServer` with the Beltic hooks
|
|
7
|
+
* attached. `@belticlabs/agent-risk-sdk/hono` and `@belticlabs/agent-risk-sdk/express` differ only in
|
|
8
|
+
* which `paymentMiddlewareFromHTTPServer` they hand in.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
type GuardedMiddlewareOptions = AttachOptions & {
|
|
12
|
+
paywall?: PaywallConfig | undefined;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type { GuardedMiddlewareOptions as G };
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { b as JsonValue, E as EvidenceSourceAll } from '../index-Bjs3BPPU.js';
|
|
2
|
+
export { A as ALL_SOURCES, c as ANOMALY_TYPES, d as Amount, e as AmountSchema, f as AnchorEntry, g as AnchorEntrySchema, h as AnchorsOutput, i as AnchorsOutputSchema, j as ApiError, k as ApiErrorSchema, C as ChainHead, l as ChainHeadSchema, m as CreatePolicyInput, n as CreatePolicyInputSchema, o as CreatePolicyOutput, p as CreatePolicyOutputSchema, q as CreateSessionInput, r as CreateSessionInputSchema, s as CreateSessionOutput, t as CreateSessionOutputSchema, D as Decision, u as DecisionSchema, v as DeclaredIntent, w as DeclaredIntentSchema, x as DigestedEnvelope, y as EvaluateInput, z as EvaluateInputSchema, B as EvaluateOutput, F as EvaluateOutputSchema, G as EventResult, H as EventResultSchema, I as EventResultStatus, K as EventResultStatusSchema, L as EvidenceAck, M as EvidenceAckSchema, N as EvidenceBatchInput, O as EvidenceBatchInputSchema, Q as EvidenceEnvelope, R as EvidenceEvent, S as EvidenceEventSchema, T as EvidenceKind, U as EvidenceKindSchema, V as EvidenceSource, W as EvidenceSourceAllSchema, X as EvidenceSourceSchema, Y as GatewayDecisionPayload, Z as GatewayDecisionPayloadSchema, _ as Hex64, $ as Hex64Schema, a0 as IntentDeclaredPayload, a1 as IntentDeclaredPayloadSchema, J as JsonObject, a2 as JsonValueSchema, a3 as LlmCallEndPayload, a4 as LlmCallEndPayloadSchema, a5 as LlmCallStartPayload, a6 as LlmCallStartPayloadSchema, a7 as MAX_BATCH_EVENTS, a8 as PAYMENT_ARTIFACTS, a9 as PLATFORM_KINDS, aa as PayloadByKind, P as PaymentMomentPayload, ab as PaymentMomentPayloadSchema, a as PaymentSummary, ac as PaymentSummarySchema, ad as PlatformAnomalyPayload, ae as PlatformAnomalyPayloadSchema, af as PlatformEvidenceKind, ag as PlatformEvidenceKindSchema, ah as PlatformObservationPayload, ai as PlatformObservationPayloadSchema, aj as SESSION_CLOSE_REASONS, ak as SOURCE_ORDER, al as SeqSchema, am as SessionClosePayload, an as SessionClosePayloadSchema, ao as SessionIdSchema, ap as SessionOpenPayload, aq as SessionOpenPayloadSchema, ar as Sig, as as SigSchema, at as TimestampSchema, au as ToolCallEndPayload, av as ToolCallEndPayloadSchema, aw as ToolCallStartPayload, ax as ToolCallStartPayloadSchema, ay as TransportGapPayload, az as TransportGapPayloadSchema, aA as WIRE_KINDS, aB as WIRE_SOURCES, aC as WireEvidenceKind, aD as WireEvidenceKindSchema, aE as compareBySessionSource, aF as compareBySourceSeq, aG as isPlatformKind, aH as isWireKind, aI as payloadSchemaFor } from '../index-Bjs3BPPU.js';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
export { O as OnReview, V as Verdict } from '../verdict-BAahb5po.js';
|
|
5
|
+
|
|
6
|
+
declare const PRIMITIVES: readonly ["THRESHOLD", "MEMBERSHIP", "MATCH", "PRESENCE", "FRESHNESS"];
|
|
7
|
+
declare const PrimitiveSchema: z.ZodEnum<{
|
|
8
|
+
THRESHOLD: "THRESHOLD";
|
|
9
|
+
MEMBERSHIP: "MEMBERSHIP";
|
|
10
|
+
MATCH: "MATCH";
|
|
11
|
+
PRESENCE: "PRESENCE";
|
|
12
|
+
FRESHNESS: "FRESHNESS";
|
|
13
|
+
}>;
|
|
14
|
+
type Primitive = z.infer<typeof PrimitiveSchema>;
|
|
15
|
+
declare const RuleVerdictSchema: z.ZodEnum<{
|
|
16
|
+
DENY: "DENY";
|
|
17
|
+
REVIEW: "REVIEW";
|
|
18
|
+
}>;
|
|
19
|
+
type RuleVerdict = z.infer<typeof RuleVerdictSchema>;
|
|
20
|
+
/**
|
|
21
|
+
* Params are JSON; a string value of the form `@<selector>` is resolved
|
|
22
|
+
* against the session view before the primitive runs (GAP-24).
|
|
23
|
+
*/
|
|
24
|
+
declare const RuleSchema: z.ZodObject<{
|
|
25
|
+
id: z.ZodString;
|
|
26
|
+
primitive: z.ZodEnum<{
|
|
27
|
+
THRESHOLD: "THRESHOLD";
|
|
28
|
+
MEMBERSHIP: "MEMBERSHIP";
|
|
29
|
+
MATCH: "MATCH";
|
|
30
|
+
PRESENCE: "PRESENCE";
|
|
31
|
+
FRESHNESS: "FRESHNESS";
|
|
32
|
+
}>;
|
|
33
|
+
selector: z.ZodString;
|
|
34
|
+
params: z.ZodRecord<z.ZodString, z.ZodType<JsonValue, unknown, z.core.$ZodTypeInternals<JsonValue, unknown>>>;
|
|
35
|
+
onFail: z.ZodEnum<{
|
|
36
|
+
DENY: "DENY";
|
|
37
|
+
REVIEW: "REVIEW";
|
|
38
|
+
}>;
|
|
39
|
+
onMissing: z.ZodOptional<z.ZodEnum<{
|
|
40
|
+
DENY: "DENY";
|
|
41
|
+
REVIEW: "REVIEW";
|
|
42
|
+
skip: "skip";
|
|
43
|
+
}>>;
|
|
44
|
+
code: z.ZodString;
|
|
45
|
+
}, z.core.$strict>;
|
|
46
|
+
type Rule = z.infer<typeof RuleSchema>;
|
|
47
|
+
declare const PolicyRulesSchema: z.ZodObject<{
|
|
48
|
+
rules: z.ZodArray<z.ZodObject<{
|
|
49
|
+
id: z.ZodString;
|
|
50
|
+
primitive: z.ZodEnum<{
|
|
51
|
+
THRESHOLD: "THRESHOLD";
|
|
52
|
+
MEMBERSHIP: "MEMBERSHIP";
|
|
53
|
+
MATCH: "MATCH";
|
|
54
|
+
PRESENCE: "PRESENCE";
|
|
55
|
+
FRESHNESS: "FRESHNESS";
|
|
56
|
+
}>;
|
|
57
|
+
selector: z.ZodString;
|
|
58
|
+
params: z.ZodRecord<z.ZodString, z.ZodType<JsonValue, unknown, z.core.$ZodTypeInternals<JsonValue, unknown>>>;
|
|
59
|
+
onFail: z.ZodEnum<{
|
|
60
|
+
DENY: "DENY";
|
|
61
|
+
REVIEW: "REVIEW";
|
|
62
|
+
}>;
|
|
63
|
+
onMissing: z.ZodOptional<z.ZodEnum<{
|
|
64
|
+
DENY: "DENY";
|
|
65
|
+
REVIEW: "REVIEW";
|
|
66
|
+
skip: "skip";
|
|
67
|
+
}>>;
|
|
68
|
+
code: z.ZodString;
|
|
69
|
+
}, z.core.$strict>>;
|
|
70
|
+
scoreThresholds: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodNumber>>;
|
|
71
|
+
}, z.core.$strict>;
|
|
72
|
+
type PolicyRules = z.infer<typeof PolicyRulesSchema>;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Reason codes are the only thing that crosses the verdict boundary besides
|
|
76
|
+
* the verdict itself (Fraud Engine RFC › API: "the seller receives a verdict
|
|
77
|
+
* and reasons — never the record"). Three families, kept separate so the
|
|
78
|
+
* type system knows which layer may emit which.
|
|
79
|
+
*/
|
|
80
|
+
/** Static platform policy — may DENY alone (Fraud Engine RFC › Fraud indicators). */
|
|
81
|
+
declare const INDICATOR_CODES: readonly ["CHAIN_BROKEN", "EVIDENCE_MISMATCH", "CREDENTIAL_REVOKED", "SESSION_INVALID", "PRINCIPAL_FLAGGED"];
|
|
82
|
+
type IndicatorCode = (typeof INDICATOR_CODES)[number];
|
|
83
|
+
/**
|
|
84
|
+
* Annotations describe the floor the decision was made on. They never change
|
|
85
|
+
* a verdict by themselves — `NO_AGENT_TRACE` is "thinner evidence", priced by
|
|
86
|
+
* the owner's rules, not a denial (GAP-43).
|
|
87
|
+
*/
|
|
88
|
+
declare const ANNOTATION_CODES: readonly ["NO_AGENT_TRACE", "NO_POLICY"];
|
|
89
|
+
type AnnotationCode = (typeof ANNOTATION_CODES)[number];
|
|
90
|
+
/** Why the Collector refused or downgraded an event (per-event ack, GAP-17). */
|
|
91
|
+
declare const COLLECTOR_CODES: readonly ["SCHEMA_INVALID", "SESSION_UNKNOWN", "SESSION_CLOSED", "SESSION_EXPIRED", "SOURCE_FORBIDDEN", "KIND_FORBIDDEN", "SEQ_GAP", "PREV_HASH_MISMATCH", "SIG_MISSING", "SIG_INVALID", "PAYLOAD_INVALID", "EVENT_TOO_LARGE"];
|
|
92
|
+
type CollectorCode = (typeof COLLECTOR_CODES)[number];
|
|
93
|
+
/** Codes the platform default policy uses; owners may define their own (GAP-44). */
|
|
94
|
+
declare const DEFAULT_RULE_CODES: readonly ["CAP_EXCEEDED", "PAYEE_NOT_ALLOWED", "NO_INTENT", "INTENT_EXPIRED"];
|
|
95
|
+
type DefaultRuleCode = (typeof DEFAULT_RULE_CODES)[number];
|
|
96
|
+
type ReasonCode = IndicatorCode | AnnotationCode | DefaultRuleCode | (string & {});
|
|
97
|
+
declare const REASON_CODE_PATTERN: RegExp;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Selector language (GAP-24; answers Fraud Engine RFC › Open Questions ›
|
|
101
|
+
* "Selector language"). One grammar addresses everything a rule or a
|
|
102
|
+
* detector may read from the session's materialized view:
|
|
103
|
+
*
|
|
104
|
+
* selector := root path?
|
|
105
|
+
* root := 'payment' | 'intent' | 'session'
|
|
106
|
+
* | 'signal[name=' NAME ']'
|
|
107
|
+
* | 'evidence[kind=' KIND (',source=' SOURCE)? ']' ('[*]')?
|
|
108
|
+
* | 'evidence[*]'
|
|
109
|
+
* path := ('.' IDENT | '[' INT ']')*
|
|
110
|
+
*
|
|
111
|
+
* payment.* the payment under evaluation (EvaluateInput)
|
|
112
|
+
* intent.* the session's declared intent
|
|
113
|
+
* session.<field> id · status · agentDid · hasAgentTrace · expiresAt · sourceBorn
|
|
114
|
+
* signal[name=N] latest value of signal N (a detector's namespace, dotted)
|
|
115
|
+
* evidence[kind=K] latest payload of kind K (any source unless source= given)
|
|
116
|
+
* evidence[kind=K][*] every payload of kind K, in (source, seq) order
|
|
117
|
+
* evidence[*] every envelope (chain checks)
|
|
118
|
+
*
|
|
119
|
+
* Inside rule params, a string `@<selector>` is a reference resolved the
|
|
120
|
+
* same way. The grammar is deliberately not JSONPath: it names four roots
|
|
121
|
+
* and nothing else, so a policy cannot address what the RFC says the
|
|
122
|
+
* engine must not expose.
|
|
123
|
+
*/
|
|
124
|
+
|
|
125
|
+
type PathSegment = {
|
|
126
|
+
key: string;
|
|
127
|
+
} | {
|
|
128
|
+
index: number;
|
|
129
|
+
};
|
|
130
|
+
type Selector = {
|
|
131
|
+
root: 'payment' | 'intent' | 'session';
|
|
132
|
+
path: PathSegment[];
|
|
133
|
+
} | {
|
|
134
|
+
root: 'signal';
|
|
135
|
+
name: string;
|
|
136
|
+
path: PathSegment[];
|
|
137
|
+
} | {
|
|
138
|
+
root: 'evidence';
|
|
139
|
+
kind: string | '*';
|
|
140
|
+
source?: EvidenceSourceAll;
|
|
141
|
+
all: boolean;
|
|
142
|
+
path: PathSegment[];
|
|
143
|
+
};
|
|
144
|
+
declare class SelectorSyntaxError extends Error {
|
|
145
|
+
readonly input: string;
|
|
146
|
+
readonly at: number;
|
|
147
|
+
readonly code = "SELECTOR_SYNTAX";
|
|
148
|
+
constructor(input: string, at: number, detail: string);
|
|
149
|
+
}
|
|
150
|
+
declare function parseSelector(input: string): Selector;
|
|
151
|
+
declare function formatSelector(sel: Selector): string;
|
|
152
|
+
declare function isSelectorRef(value: unknown): value is `@${string}`;
|
|
153
|
+
declare function parseSelectorRef(ref: string): Selector;
|
|
154
|
+
/** Walk a `path` over a value; `undefined` when any step is missing. */
|
|
155
|
+
declare function walkPath(value: unknown, path: readonly PathSegment[]): unknown;
|
|
156
|
+
declare const SelectorStringSchema: z.ZodString;
|
|
157
|
+
|
|
158
|
+
export { ANNOTATION_CODES, type AnnotationCode, COLLECTOR_CODES, type CollectorCode, DEFAULT_RULE_CODES, type DefaultRuleCode, EvidenceSourceAll, INDICATOR_CODES, type IndicatorCode, JsonValue, PRIMITIVES, type PathSegment, type PolicyRules, PolicyRulesSchema, type Primitive, PrimitiveSchema, REASON_CODE_PATTERN, type ReasonCode, type Rule, RuleSchema, type RuleVerdict, RuleVerdictSchema, type Selector, SelectorStringSchema, SelectorSyntaxError, formatSelector, isSelectorRef, parseSelector, parseSelectorRef, walkPath };
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ALL_SOURCES,
|
|
3
|
+
ANNOTATION_CODES,
|
|
4
|
+
ANOMALY_TYPES,
|
|
5
|
+
AmountSchema,
|
|
6
|
+
AnchorEntrySchema,
|
|
7
|
+
AnchorsOutputSchema,
|
|
8
|
+
ApiErrorSchema,
|
|
9
|
+
COLLECTOR_CODES,
|
|
10
|
+
ChainHeadSchema,
|
|
11
|
+
CreatePolicyInputSchema,
|
|
12
|
+
CreatePolicyOutputSchema,
|
|
13
|
+
CreateSessionInputSchema,
|
|
14
|
+
CreateSessionOutputSchema,
|
|
15
|
+
DEFAULT_RULE_CODES,
|
|
16
|
+
DecisionSchema,
|
|
17
|
+
DeclaredIntentSchema,
|
|
18
|
+
EvaluateInputSchema,
|
|
19
|
+
EvaluateOutputSchema,
|
|
20
|
+
EventResultSchema,
|
|
21
|
+
EventResultStatusSchema,
|
|
22
|
+
EvidenceAckSchema,
|
|
23
|
+
EvidenceBatchInputSchema,
|
|
24
|
+
EvidenceEventSchema,
|
|
25
|
+
EvidenceKindSchema,
|
|
26
|
+
EvidenceSourceAllSchema,
|
|
27
|
+
EvidenceSourceSchema,
|
|
28
|
+
GatewayDecisionPayloadSchema,
|
|
29
|
+
Hex64Schema,
|
|
30
|
+
INDICATOR_CODES,
|
|
31
|
+
IntentDeclaredPayloadSchema,
|
|
32
|
+
JsonValueSchema,
|
|
33
|
+
LlmCallEndPayloadSchema,
|
|
34
|
+
LlmCallStartPayloadSchema,
|
|
35
|
+
MAX_BATCH_EVENTS,
|
|
36
|
+
PAYMENT_ARTIFACTS,
|
|
37
|
+
PLATFORM_KINDS,
|
|
38
|
+
PRIMITIVES,
|
|
39
|
+
PaymentMomentPayloadSchema,
|
|
40
|
+
PaymentSummarySchema,
|
|
41
|
+
PlatformAnomalyPayloadSchema,
|
|
42
|
+
PlatformEvidenceKindSchema,
|
|
43
|
+
PlatformObservationPayloadSchema,
|
|
44
|
+
PolicyRulesSchema,
|
|
45
|
+
PrimitiveSchema,
|
|
46
|
+
REASON_CODE_PATTERN,
|
|
47
|
+
RuleSchema,
|
|
48
|
+
RuleVerdictSchema,
|
|
49
|
+
SESSION_CLOSE_REASONS,
|
|
50
|
+
SOURCE_ORDER,
|
|
51
|
+
SelectorStringSchema,
|
|
52
|
+
SelectorSyntaxError,
|
|
53
|
+
SeqSchema,
|
|
54
|
+
SessionClosePayloadSchema,
|
|
55
|
+
SessionIdSchema,
|
|
56
|
+
SessionOpenPayloadSchema,
|
|
57
|
+
SigSchema,
|
|
58
|
+
TimestampSchema,
|
|
59
|
+
ToolCallEndPayloadSchema,
|
|
60
|
+
ToolCallStartPayloadSchema,
|
|
61
|
+
TransportGapPayloadSchema,
|
|
62
|
+
Verdict,
|
|
63
|
+
WIRE_KINDS,
|
|
64
|
+
WIRE_SOURCES,
|
|
65
|
+
WireEvidenceKindSchema,
|
|
66
|
+
compareBySessionSource,
|
|
67
|
+
compareBySourceSeq,
|
|
68
|
+
formatSelector,
|
|
69
|
+
isPlatformKind,
|
|
70
|
+
isSelectorRef,
|
|
71
|
+
isWireKind,
|
|
72
|
+
parseSelector,
|
|
73
|
+
parseSelectorRef,
|
|
74
|
+
payloadSchemaFor,
|
|
75
|
+
walkPath
|
|
76
|
+
} from "../chunk-46QN2KEZ.js";
|
|
77
|
+
export {
|
|
78
|
+
ALL_SOURCES,
|
|
79
|
+
ANNOTATION_CODES,
|
|
80
|
+
ANOMALY_TYPES,
|
|
81
|
+
AmountSchema,
|
|
82
|
+
AnchorEntrySchema,
|
|
83
|
+
AnchorsOutputSchema,
|
|
84
|
+
ApiErrorSchema,
|
|
85
|
+
COLLECTOR_CODES,
|
|
86
|
+
ChainHeadSchema,
|
|
87
|
+
CreatePolicyInputSchema,
|
|
88
|
+
CreatePolicyOutputSchema,
|
|
89
|
+
CreateSessionInputSchema,
|
|
90
|
+
CreateSessionOutputSchema,
|
|
91
|
+
DEFAULT_RULE_CODES,
|
|
92
|
+
DecisionSchema,
|
|
93
|
+
DeclaredIntentSchema,
|
|
94
|
+
EvaluateInputSchema,
|
|
95
|
+
EvaluateOutputSchema,
|
|
96
|
+
EventResultSchema,
|
|
97
|
+
EventResultStatusSchema,
|
|
98
|
+
EvidenceAckSchema,
|
|
99
|
+
EvidenceBatchInputSchema,
|
|
100
|
+
EvidenceEventSchema,
|
|
101
|
+
EvidenceKindSchema,
|
|
102
|
+
EvidenceSourceAllSchema,
|
|
103
|
+
EvidenceSourceSchema,
|
|
104
|
+
GatewayDecisionPayloadSchema,
|
|
105
|
+
Hex64Schema,
|
|
106
|
+
INDICATOR_CODES,
|
|
107
|
+
IntentDeclaredPayloadSchema,
|
|
108
|
+
JsonValueSchema,
|
|
109
|
+
LlmCallEndPayloadSchema,
|
|
110
|
+
LlmCallStartPayloadSchema,
|
|
111
|
+
MAX_BATCH_EVENTS,
|
|
112
|
+
PAYMENT_ARTIFACTS,
|
|
113
|
+
PLATFORM_KINDS,
|
|
114
|
+
PRIMITIVES,
|
|
115
|
+
PaymentMomentPayloadSchema,
|
|
116
|
+
PaymentSummarySchema,
|
|
117
|
+
PlatformAnomalyPayloadSchema,
|
|
118
|
+
PlatformEvidenceKindSchema,
|
|
119
|
+
PlatformObservationPayloadSchema,
|
|
120
|
+
PolicyRulesSchema,
|
|
121
|
+
PrimitiveSchema,
|
|
122
|
+
REASON_CODE_PATTERN,
|
|
123
|
+
RuleSchema,
|
|
124
|
+
RuleVerdictSchema,
|
|
125
|
+
SESSION_CLOSE_REASONS,
|
|
126
|
+
SOURCE_ORDER,
|
|
127
|
+
SelectorStringSchema,
|
|
128
|
+
SelectorSyntaxError,
|
|
129
|
+
SeqSchema,
|
|
130
|
+
SessionClosePayloadSchema,
|
|
131
|
+
SessionIdSchema,
|
|
132
|
+
SessionOpenPayloadSchema,
|
|
133
|
+
SigSchema,
|
|
134
|
+
TimestampSchema,
|
|
135
|
+
ToolCallEndPayloadSchema,
|
|
136
|
+
ToolCallStartPayloadSchema,
|
|
137
|
+
TransportGapPayloadSchema,
|
|
138
|
+
Verdict,
|
|
139
|
+
WIRE_KINDS,
|
|
140
|
+
WIRE_SOURCES,
|
|
141
|
+
WireEvidenceKindSchema,
|
|
142
|
+
compareBySessionSource,
|
|
143
|
+
compareBySourceSeq,
|
|
144
|
+
formatSelector,
|
|
145
|
+
isPlatformKind,
|
|
146
|
+
isSelectorRef,
|
|
147
|
+
isWireKind,
|
|
148
|
+
parseSelector,
|
|
149
|
+
parseSelectorRef,
|
|
150
|
+
payloadSchemaFor,
|
|
151
|
+
walkPath
|
|
152
|
+
};
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import { B as Beltic } from '../client-
|
|
2
|
-
import '
|
|
3
|
-
import '
|
|
4
|
-
import '
|
|
1
|
+
import { B as Beltic } from '../client-C-mV_3A0.js';
|
|
2
|
+
import '../index-Bjs3BPPU.js';
|
|
3
|
+
import 'zod';
|
|
4
|
+
import '../verdict-BAahb5po.js';
|
|
5
|
+
import '../session-5TClPLI4.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Adapter for the anti-fraud port already in Beltic's `platform` monorepo
|
|
9
|
+
* (`x402-core/gateways/anti-fraud.gateway.interface.ts`), GAP-37. That
|
|
10
|
+
* port is binary and session-less; the mapping is lossy by construction:
|
|
11
|
+
* `REVIEW` becomes a deny unless `onReview` says otherwise, and reason
|
|
12
|
+
* codes travel joined in `reason`. The structural type is copied so this
|
|
13
|
+
* module needs no dependency on `platform`.
|
|
14
|
+
*/
|
|
5
15
|
|
|
6
16
|
interface PaymentRiskContext {
|
|
7
17
|
wallet: string;
|