@belticlabs/agent-risk-sdk 0.1.1 → 0.3.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-BEpzr2R3.d.ts +29 -0
- package/dist/ai/index.d.ts +5 -5
- package/dist/ai/index.js +48 -36
- package/dist/chunk-4MG6VNAU.js +276 -0
- package/dist/chunk-FAQ442YH.js +24 -0
- package/dist/{chunk-U5Z5Z2BQ.js → chunk-LM4NIYE5.js} +26 -20
- package/dist/chunk-NJVO2WIV.js +17 -0
- package/dist/{chunk-W2OY7QXV.js → chunk-OKC6VMFH.js} +15 -26
- package/dist/{chunk-SFGM7KOG.js → chunk-X3W2Z5GC.js} +3 -0
- package/dist/index.d.ts +19 -9
- package/dist/index.js +354 -161
- package/dist/mcp/index.d.ts +3 -5
- package/dist/mcp/index.js +42 -34
- package/dist/middleware-9gI0ou2i.d.ts +15 -0
- package/dist/protocol/index.d.ts +2 -3
- package/dist/seller/anti-fraud-gateway.d.ts +11 -4
- package/dist/seller/anti-fraud-gateway.js +13 -14
- package/dist/session-DsBWEP8d.d.ts +485 -0
- package/dist/{index-Bjs3BPPU.d.ts → verdict-6vCyoAHE.d.ts} +25 -1
- package/dist/x402/express.d.ts +4 -6
- package/dist/x402/express.js +8 -7
- package/dist/x402/hono.d.ts +4 -6
- package/dist/x402/hono.js +8 -7
- package/dist/x402/index.d.ts +80 -19
- package/dist/x402/index.js +109 -35
- package/package.json +1 -1
- package/dist/chunk-5TWO73OD.js +0 -35
- package/dist/chunk-VM7MK43J.js +0 -13
- package/dist/client-NxpD_384.d.ts +0 -64
- package/dist/middleware-D-ejQIoE.d.ts +0 -40
- package/dist/session-B2mfurae.d.ts +0 -196
- package/dist/verdict-BAahb5po.d.ts +0 -26
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { D as Decision } from './verdict-6vCyoAHE.js';
|
|
2
|
+
import { x402ResourceServer, x402HTTPResourceServer } from '@x402/core/server';
|
|
3
|
+
import { B as Beltic, n as SessionBorn, a as Session } from './session-DsBWEP8d.js';
|
|
4
|
+
|
|
5
|
+
interface CorrelationContext {
|
|
6
|
+
path: string;
|
|
7
|
+
method: string;
|
|
8
|
+
header: (name: string) => string | undefined;
|
|
9
|
+
}
|
|
10
|
+
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
|
+
/** Called with every decision; the default logs nothing. */
|
|
14
|
+
onDecision?: ((d: {
|
|
15
|
+
sessionId: string;
|
|
16
|
+
decision: Decision;
|
|
17
|
+
reasonCodes: string[];
|
|
18
|
+
born: SessionBorn;
|
|
19
|
+
/** The verdict the buyer says it obtained before presenting (GAP-80), unverified. */
|
|
20
|
+
buyerDecisionId: string | null;
|
|
21
|
+
}) => void) | undefined;
|
|
22
|
+
}
|
|
23
|
+
interface AttachedX402 {
|
|
24
|
+
/** Sessions resolved at verify time, keyed by payment digest — for tests and settle hooks. */
|
|
25
|
+
readonly inFlight: ReadonlyMap<string, Session>;
|
|
26
|
+
}
|
|
27
|
+
declare function attachX402(beltic: Beltic, server: x402ResourceServer, http?: x402HTTPResourceServer, opts?: AttachOptions): AttachedX402;
|
|
28
|
+
|
|
29
|
+
export { type AttachOptions as A, type CorrelationContext as C, attachX402 as a, type AttachedX402 as b };
|
package/dist/ai/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { P as PaymentMomentPayload } from '../
|
|
1
|
+
import { P as PaymentMomentPayload } from '../verdict-6vCyoAHE.js';
|
|
2
2
|
import { LanguageModelMiddleware, Tool, ToolSet } from 'ai';
|
|
3
|
-
import { S as
|
|
3
|
+
import { S as SessionSource } from '../session-DsBWEP8d.js';
|
|
4
4
|
import 'zod';
|
|
5
5
|
|
|
6
6
|
interface PaymentMoments {
|
|
@@ -12,8 +12,8 @@ interface WrapToolOptions<I = unknown, O = unknown> {
|
|
|
12
12
|
/** When the tool performs a purchase, map its input/output to the payment moments it produced. */
|
|
13
13
|
payment?: (input: I, output: O) => PaymentMoments | null;
|
|
14
14
|
}
|
|
15
|
-
declare function middleware(
|
|
16
|
-
declare function wrapTool<T extends Tool>(
|
|
17
|
-
declare function wrapTools<T extends ToolSet>(
|
|
15
|
+
declare function middleware(source: SessionSource): LanguageModelMiddleware;
|
|
16
|
+
declare function wrapTool<T extends Tool>(source: SessionSource, tool: T, opts?: WrapToolOptions): T;
|
|
17
|
+
declare function wrapTools<T extends ToolSet>(source: SessionSource, tools: T, opts?: Record<string, WrapToolOptions>): T;
|
|
18
18
|
|
|
19
19
|
export { type PaymentMoments, type WrapToolOptions, middleware, wrapTool, wrapTools };
|
package/dist/ai/index.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import {
|
|
2
|
+
Sessions,
|
|
2
3
|
recordCall
|
|
3
|
-
} from "../chunk-
|
|
4
|
+
} from "../chunk-4MG6VNAU.js";
|
|
4
5
|
import {
|
|
5
6
|
uuidv7
|
|
6
|
-
} from "../chunk-
|
|
7
|
+
} from "../chunk-X3W2Z5GC.js";
|
|
8
|
+
import "../chunk-46QN2KEZ.js";
|
|
7
9
|
import {
|
|
8
10
|
toJson,
|
|
9
11
|
toJsonObject
|
|
10
12
|
} from "../chunk-FQDHFTVR.js";
|
|
11
|
-
import "../chunk-46QN2KEZ.js";
|
|
12
13
|
|
|
13
14
|
// src/ai/index.ts
|
|
14
15
|
var PARAM_KEYS = [
|
|
@@ -23,22 +24,29 @@ var PARAM_KEYS = [
|
|
|
23
24
|
"responseFormat",
|
|
24
25
|
"stopSequences"
|
|
25
26
|
];
|
|
26
|
-
function middleware(
|
|
27
|
+
function middleware(source) {
|
|
28
|
+
if (!source) return {};
|
|
27
29
|
return {
|
|
28
|
-
wrapGenerate: ({ doGenerate, params, model }) =>
|
|
29
|
-
session
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
wrapGenerate: async ({ doGenerate, params, model }) => {
|
|
31
|
+
const session = await Sessions.resolve(source);
|
|
32
|
+
if (!session) return doGenerate();
|
|
33
|
+
return recordCall(
|
|
34
|
+
session,
|
|
35
|
+
"llm_call",
|
|
36
|
+
uuidv7(),
|
|
37
|
+
{ provider: model.provider, modelId: model.modelId, params: pick(params) },
|
|
38
|
+
doGenerate,
|
|
39
|
+
(result) => ({
|
|
40
|
+
content: toJson(result.content),
|
|
41
|
+
finishReason: result.finishReason.unified,
|
|
42
|
+
usage: toJsonObject(result.usage)
|
|
43
|
+
})
|
|
44
|
+
);
|
|
45
|
+
},
|
|
40
46
|
// A stream ends when it drains, not when doStream resolves — so its end is emitted at flush.
|
|
41
47
|
wrapStream: async ({ doStream, params, model }) => {
|
|
48
|
+
const session = await Sessions.resolve(source);
|
|
49
|
+
if (!session) return doStream();
|
|
42
50
|
const callId = uuidv7();
|
|
43
51
|
const started = Date.now();
|
|
44
52
|
await session.emit("llm_call.start", {
|
|
@@ -72,32 +80,36 @@ function middleware(session) {
|
|
|
72
80
|
}
|
|
73
81
|
};
|
|
74
82
|
}
|
|
75
|
-
function wrapTool(
|
|
76
|
-
if (!tool.execute) return tool;
|
|
83
|
+
function wrapTool(source, tool, opts = {}) {
|
|
84
|
+
if (!source || !tool.execute) return tool;
|
|
77
85
|
const original = tool.execute;
|
|
78
86
|
const toolName = opts.name ?? tool.name ?? "tool";
|
|
79
|
-
const execute = (input, options) =>
|
|
80
|
-
session
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
87
|
+
const execute = async (input, options) => {
|
|
88
|
+
const session = await Sessions.resolve(source);
|
|
89
|
+
if (!session) return original(input, options);
|
|
90
|
+
return recordCall(
|
|
91
|
+
session,
|
|
92
|
+
"tool_call",
|
|
93
|
+
options?.toolCallId ?? uuidv7(),
|
|
94
|
+
{ toolName, input: toJson(input), transport: "local" },
|
|
95
|
+
async () => {
|
|
96
|
+
const output = await original(input, options);
|
|
97
|
+
return output && typeof output === "object" && Symbol.asyncIterator in output ? collect(output) : output;
|
|
98
|
+
},
|
|
99
|
+
async (output) => {
|
|
100
|
+
const moments = opts.payment?.(input, output) ?? null;
|
|
101
|
+
if (moments?.requested) await session.emit("payment.requested", moments.requested);
|
|
102
|
+
if (moments?.presented) await session.emit("payment.presented", moments.presented);
|
|
103
|
+
return { output: toJson(output) };
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
};
|
|
95
107
|
return { ...tool, execute };
|
|
96
108
|
}
|
|
97
|
-
function wrapTools(
|
|
109
|
+
function wrapTools(source, tools, opts = {}) {
|
|
98
110
|
const out = {};
|
|
99
111
|
for (const [name, tool] of Object.entries(tools))
|
|
100
|
-
out[name] = wrapTool(
|
|
112
|
+
out[name] = wrapTool(source, tool, { name, ...opts[name] });
|
|
101
113
|
return out;
|
|
102
114
|
}
|
|
103
115
|
function pick(params) {
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Chain
|
|
3
|
+
} from "./chunk-X3W2Z5GC.js";
|
|
4
|
+
|
|
5
|
+
// src/core/disabled-error.ts
|
|
6
|
+
var BelticDisabledError = class extends Error {
|
|
7
|
+
code = "SDK_DISABLED";
|
|
8
|
+
constructor(entry) {
|
|
9
|
+
super(`${entry} needs a configured client: this Beltic is disabled (no BELTIC_* configured)`);
|
|
10
|
+
this.name = "BelticDisabledError";
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// src/core/record.ts
|
|
15
|
+
function errorOf(err) {
|
|
16
|
+
const e = err;
|
|
17
|
+
return { name: String(e?.name ?? "Error"), message: String(e?.message ?? err) };
|
|
18
|
+
}
|
|
19
|
+
function openCall(session, kind, callId, start) {
|
|
20
|
+
const started = Date.now();
|
|
21
|
+
const opened = session.emit(`${kind}.start`, { callId, ...start });
|
|
22
|
+
const close = async (outcome) => {
|
|
23
|
+
await opened;
|
|
24
|
+
return session.emit(
|
|
25
|
+
`${kind}.end`,
|
|
26
|
+
{ callId, ...outcome, durationMs: Date.now() - started }
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
callId,
|
|
31
|
+
opened,
|
|
32
|
+
end: (outcome = {}) => close(outcome),
|
|
33
|
+
fail: (error, outcome = {}) => close({ error: errorOf(error), ...outcome })
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
async function recordCall(session, kind, callId, start, run, end = () => ({})) {
|
|
37
|
+
const span = openCall(session, kind, callId, start);
|
|
38
|
+
await span.opened;
|
|
39
|
+
try {
|
|
40
|
+
const result = await run();
|
|
41
|
+
await span.end(await end(result));
|
|
42
|
+
return result;
|
|
43
|
+
} catch (err) {
|
|
44
|
+
await span.fail(err);
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/core/session.ts
|
|
50
|
+
var Session = class {
|
|
51
|
+
constructor(deps, id, source, expiresAt, born) {
|
|
52
|
+
this.deps = deps;
|
|
53
|
+
this.id = id;
|
|
54
|
+
this.source = source;
|
|
55
|
+
this.expiresAt = expiresAt;
|
|
56
|
+
this.born = born;
|
|
57
|
+
this.chain = Chain.genesis(id, source);
|
|
58
|
+
this.now = deps.now ?? (() => /* @__PURE__ */ new Date());
|
|
59
|
+
}
|
|
60
|
+
chain;
|
|
61
|
+
building = Promise.resolve();
|
|
62
|
+
dropped = 0;
|
|
63
|
+
droppedFirstTs = null;
|
|
64
|
+
droppedLastTs = null;
|
|
65
|
+
closed = false;
|
|
66
|
+
now;
|
|
67
|
+
get head() {
|
|
68
|
+
return this.chain.head;
|
|
69
|
+
}
|
|
70
|
+
get droppedCount() {
|
|
71
|
+
return this.dropped;
|
|
72
|
+
}
|
|
73
|
+
get isClosed() {
|
|
74
|
+
return this.closed;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Resolves once the event is sequenced and buffered — not once it is
|
|
78
|
+
* acknowledged. `false` when the event was dropped, or (fail-open) when
|
|
79
|
+
* the chain can no longer take it.
|
|
80
|
+
*/
|
|
81
|
+
async emit(kind, payload) {
|
|
82
|
+
if (!this.deps.failOpen) return this.chainEvent(kind, payload);
|
|
83
|
+
try {
|
|
84
|
+
return await this.chainEvent(kind, payload);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
this.deps.onError?.(err);
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** The tool call whose `execute` the host runs itself; see `ToolCallSpan`. */
|
|
91
|
+
toolCall(call) {
|
|
92
|
+
const { callId, ...start } = call;
|
|
93
|
+
return openCall(this, "tool_call", callId, { transport: "local", ...start });
|
|
94
|
+
}
|
|
95
|
+
async chainEvent(kind, payload) {
|
|
96
|
+
const halted = this.deps.transport.haltedError(this.id, this.source);
|
|
97
|
+
if (halted) throw halted;
|
|
98
|
+
const ts = this.now().toISOString();
|
|
99
|
+
if (!this.deps.transport.hasRoom()) {
|
|
100
|
+
this.dropped++;
|
|
101
|
+
this.droppedFirstTs ??= ts;
|
|
102
|
+
this.droppedLastTs = ts;
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
if (this.dropped > 0) {
|
|
106
|
+
this.deps.transport.enqueue(
|
|
107
|
+
await this.next(
|
|
108
|
+
"transport.gap",
|
|
109
|
+
{ dropped: this.dropped, firstTs: this.droppedFirstTs, lastTs: this.droppedLastTs },
|
|
110
|
+
ts
|
|
111
|
+
)
|
|
112
|
+
);
|
|
113
|
+
this.dropped = 0;
|
|
114
|
+
this.droppedFirstTs = this.droppedLastTs = null;
|
|
115
|
+
}
|
|
116
|
+
const body = payload;
|
|
117
|
+
this.deps.transport.enqueue(
|
|
118
|
+
await this.next(kind, this.deps.redact ? this.deps.redact(kind, body) : body, ts)
|
|
119
|
+
);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
async close(reason = "completed", extra = {}) {
|
|
123
|
+
if (this.closed) return;
|
|
124
|
+
this.closed = true;
|
|
125
|
+
await this.emit("session.close", { reason, ...extra });
|
|
126
|
+
await this.flush();
|
|
127
|
+
this.deps.onClosed?.(this);
|
|
128
|
+
}
|
|
129
|
+
/** Read-your-writes: the platform must hold the evidence before anyone judges it (GAP-16/66). */
|
|
130
|
+
flush() {
|
|
131
|
+
return this.deps.transport.flush();
|
|
132
|
+
}
|
|
133
|
+
/** Serialized: two concurrent emits get consecutive seqs, never the same one. */
|
|
134
|
+
next(kind, payload, ts) {
|
|
135
|
+
const run = this.building.then(async () => {
|
|
136
|
+
const built = await this.chain.append({ ts, kind, payload }, this.deps.signer);
|
|
137
|
+
this.chain = built.chain;
|
|
138
|
+
return built.event;
|
|
139
|
+
});
|
|
140
|
+
this.building = run.catch(() => void 0);
|
|
141
|
+
return run;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
var DEFAULT_OPEN_RETRY_MS = 6e4;
|
|
145
|
+
var Sessions = class {
|
|
146
|
+
constructor(deps) {
|
|
147
|
+
this.deps = deps;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* One session object per (session, source) per process: a chain's head
|
|
151
|
+
* lives in it, so two objects for the same chain would both start at
|
|
152
|
+
* seq 0 and fork it. Closed sessions are forgotten; a process restart
|
|
153
|
+
* mid-session still loses the head (GAP-67).
|
|
154
|
+
*/
|
|
155
|
+
attached = /* @__PURE__ */ new Map();
|
|
156
|
+
/** Buyer sessions by the host's own key (GAP-71). */
|
|
157
|
+
opened = /* @__PURE__ */ new Map();
|
|
158
|
+
retryAt = 0;
|
|
159
|
+
/** The session behind a source: itself, or the one the run opens (null when there is none). */
|
|
160
|
+
static resolve(source) {
|
|
161
|
+
if (!source) return Promise.resolve(null);
|
|
162
|
+
return source instanceof Session ? Promise.resolve(source) : source.session();
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Buyer half: the evidence session for a key of the host's own (its
|
|
166
|
+
* session, run or conversation id), opened on first use and reused
|
|
167
|
+
* after. A halted chain is reopened as a fresh session that continues
|
|
168
|
+
* the same key; a closed key is forgotten. When the platform refuses to
|
|
169
|
+
* open one, a fail-open client resolves null — the host runs without
|
|
170
|
+
* evidence — until `openRetryMs` has passed (GAP-71); otherwise the
|
|
171
|
+
* refusal is thrown and the next call tries again. The identity is
|
|
172
|
+
* required either way: that is configuration.
|
|
173
|
+
*/
|
|
174
|
+
open(key, input = {}) {
|
|
175
|
+
if (this.deps.enabled === false) return Promise.resolve(null);
|
|
176
|
+
const prior = this.opened.get(key) ?? Promise.resolve(null);
|
|
177
|
+
const next = prior.catch(() => null).then(
|
|
178
|
+
(session) => session && !session.isClosed && !this.deps.transport.haltedError(session.id, session.source) ? session : this.openFresh(input, () => this.forget(key, next))
|
|
179
|
+
);
|
|
180
|
+
this.opened.set(key, next);
|
|
181
|
+
next.catch(() => this.forget(key, next));
|
|
182
|
+
return next;
|
|
183
|
+
}
|
|
184
|
+
forget(key, entry) {
|
|
185
|
+
if (this.opened.get(key) === entry) this.opened.delete(key);
|
|
186
|
+
}
|
|
187
|
+
async openFresh(input, onClosed) {
|
|
188
|
+
this.identityFor("open");
|
|
189
|
+
const create = async () => this.create(typeof input === "function" ? await input() : input, onClosed);
|
|
190
|
+
if (!this.deps.failOpen) return create();
|
|
191
|
+
if (Date.now() < this.retryAt) return null;
|
|
192
|
+
try {
|
|
193
|
+
const session = await create();
|
|
194
|
+
this.retryAt = 0;
|
|
195
|
+
return session;
|
|
196
|
+
} catch (err) {
|
|
197
|
+
this.retryAt = Date.now() + (this.deps.openRetryMs ?? DEFAULT_OPEN_RETRY_MS);
|
|
198
|
+
this.deps.onError?.(err);
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/** Buyer half: create an AGENT_TRACE session bound to the agent identity, then announce it on the chain. */
|
|
203
|
+
start(input = {}) {
|
|
204
|
+
if (this.deps.enabled === false)
|
|
205
|
+
return Promise.reject(new BelticDisabledError("sessions.start"));
|
|
206
|
+
return this.create(input);
|
|
207
|
+
}
|
|
208
|
+
identityFor(entry) {
|
|
209
|
+
const identity = this.deps.identity;
|
|
210
|
+
if (!identity)
|
|
211
|
+
throw new Error(`sessions.${entry} needs an agent identity (createBeltic({ identity }))`);
|
|
212
|
+
return identity;
|
|
213
|
+
}
|
|
214
|
+
async create(input, onClosed) {
|
|
215
|
+
const identity = this.identityFor("start");
|
|
216
|
+
const body = {
|
|
217
|
+
source: "AGENT_TRACE",
|
|
218
|
+
agent: { did: identity.did, credential: identity.credential ?? identity.did },
|
|
219
|
+
...input.intent ? { intent: input.intent } : {}
|
|
220
|
+
};
|
|
221
|
+
const out = await this.deps.api.post("/v1/sessions", body);
|
|
222
|
+
const session = this.attach(out.sessionId, "AGENT_TRACE", out.expiresAt, "buyer", onClosed);
|
|
223
|
+
await session.emit("session.open", {
|
|
224
|
+
runtime: {
|
|
225
|
+
sdk: "@belticlabs/agent-risk-sdk",
|
|
226
|
+
version: this.deps.sdkVersion,
|
|
227
|
+
...input.runtime
|
|
228
|
+
},
|
|
229
|
+
...input.attestations ? { attestations: input.attestations } : {}
|
|
230
|
+
});
|
|
231
|
+
if (input.intent) await session.emit("intent.declared", input.intent);
|
|
232
|
+
return session;
|
|
233
|
+
}
|
|
234
|
+
/** Seller half: emit INTERNAL_NETWORK evidence into a session the buyer bound, or open a seller-born one. */
|
|
235
|
+
async ensure(sessionId) {
|
|
236
|
+
if (this.deps.enabled === false) throw new BelticDisabledError("sessions.ensure");
|
|
237
|
+
if (sessionId) return this.attach(sessionId, "INTERNAL_NETWORK", null, "buyer");
|
|
238
|
+
const out = await this.deps.api.post("/v1/sessions", {
|
|
239
|
+
source: "INTERNAL_NETWORK"
|
|
240
|
+
});
|
|
241
|
+
return this.attach(out.sessionId, "INTERNAL_NETWORK", out.expiresAt, "seller");
|
|
242
|
+
}
|
|
243
|
+
attach(id, source, expiresAt, born, onClosed) {
|
|
244
|
+
const key = `${id}:${source}`;
|
|
245
|
+
const existing = this.attached.get(key);
|
|
246
|
+
if (existing) return existing;
|
|
247
|
+
const session = new Session(
|
|
248
|
+
{
|
|
249
|
+
transport: this.deps.transport,
|
|
250
|
+
signer: source === "AGENT_TRACE" ? this.deps.identity?.signer : void 0,
|
|
251
|
+
redact: this.deps.redact,
|
|
252
|
+
now: this.deps.now,
|
|
253
|
+
failOpen: this.deps.failOpen,
|
|
254
|
+
onError: this.deps.onError,
|
|
255
|
+
onClosed: () => {
|
|
256
|
+
this.attached.delete(key);
|
|
257
|
+
onClosed?.();
|
|
258
|
+
}
|
|
259
|
+
},
|
|
260
|
+
id,
|
|
261
|
+
source,
|
|
262
|
+
expiresAt,
|
|
263
|
+
born
|
|
264
|
+
);
|
|
265
|
+
this.attached.set(key, session);
|
|
266
|
+
return session;
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
export {
|
|
271
|
+
BelticDisabledError,
|
|
272
|
+
recordCall,
|
|
273
|
+
Session,
|
|
274
|
+
DEFAULT_OPEN_RETRY_MS,
|
|
275
|
+
Sessions
|
|
276
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/x402/binding.ts
|
|
2
|
+
var SESSION_EXTENSION = "beltic.sessionId";
|
|
3
|
+
var SESSION_HEADER = "Beltic-Session-Id";
|
|
4
|
+
var DECISION_EXTENSION = "beltic.decisionId";
|
|
5
|
+
var DECISION_HEADER = "Beltic-Decision-Id";
|
|
6
|
+
function sessionIdOf(extensions) {
|
|
7
|
+
return stringAt(extensions, SESSION_EXTENSION);
|
|
8
|
+
}
|
|
9
|
+
function decisionIdOf(extensions) {
|
|
10
|
+
return stringAt(extensions, DECISION_EXTENSION);
|
|
11
|
+
}
|
|
12
|
+
function stringAt(extensions, key) {
|
|
13
|
+
const v = extensions?.[key];
|
|
14
|
+
return typeof v === "string" && v.length > 0 ? v : null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
SESSION_EXTENSION,
|
|
19
|
+
SESSION_HEADER,
|
|
20
|
+
DECISION_EXTENSION,
|
|
21
|
+
DECISION_HEADER,
|
|
22
|
+
sessionIdOf,
|
|
23
|
+
decisionIdOf
|
|
24
|
+
};
|
|
@@ -6,20 +6,23 @@ import {
|
|
|
6
6
|
function x402Currency(network, asset) {
|
|
7
7
|
return `${network}/${asset}`;
|
|
8
8
|
}
|
|
9
|
-
function
|
|
9
|
+
function x402Summary(accepts, opts = {}) {
|
|
10
|
+
const payer = opts.payer?.toLowerCase();
|
|
10
11
|
return {
|
|
11
12
|
protocol: "x402",
|
|
12
|
-
payee:
|
|
13
|
+
payee: accepts?.payTo ?? "unknown",
|
|
13
14
|
amount: {
|
|
14
|
-
value:
|
|
15
|
-
currency:
|
|
15
|
+
value: accepts?.amount ?? accepts?.maxAmountRequired ?? "0",
|
|
16
|
+
currency: accepts ? x402Currency(accepts.network ?? "unknown", accepts.asset ?? "unknown") : "unknown"
|
|
16
17
|
},
|
|
17
|
-
|
|
18
|
-
raw
|
|
18
|
+
...payer ? { payer } : {}
|
|
19
19
|
};
|
|
20
20
|
}
|
|
21
|
+
function fromAccepts(a, artifact, raw, payer) {
|
|
22
|
+
return { ...x402Summary(a, { payer }), artifact, raw };
|
|
23
|
+
}
|
|
21
24
|
function payerOf(payload) {
|
|
22
|
-
const p = payload.payload;
|
|
25
|
+
const p = payload.payload ?? {};
|
|
23
26
|
const auth = p.authorization;
|
|
24
27
|
const candidates = [
|
|
25
28
|
auth?.from,
|
|
@@ -32,15 +35,13 @@ function payerOf(payload) {
|
|
|
32
35
|
return typeof hit === "string" ? hit.toLowerCase() : void 0;
|
|
33
36
|
}
|
|
34
37
|
var x402Moments = {
|
|
35
|
-
/** The 402 challenge as the buyer saw it. */
|
|
38
|
+
/** The 402 challenge as the buyer saw it, v2 header or v1 body. */
|
|
36
39
|
required(required) {
|
|
37
|
-
|
|
38
|
-
return fromAccepts(r.accepts[0], "http-402", toJsonObject(r));
|
|
40
|
+
return fromAccepts(required.accepts?.[0], "http-402", toJsonObject(required));
|
|
39
41
|
},
|
|
40
42
|
/** The requirements the seller's resource server resolved for a request. */
|
|
41
43
|
requirements(req) {
|
|
42
|
-
|
|
43
|
-
return fromAccepts(r, "http-402", toJsonObject(r));
|
|
44
|
+
return fromAccepts(req, "http-402", toJsonObject(req));
|
|
44
45
|
},
|
|
45
46
|
/** A route's static `accepts` config, before any payment header exists. */
|
|
46
47
|
route(route, raw) {
|
|
@@ -61,14 +62,18 @@ var x402Moments = {
|
|
|
61
62
|
ask(first, raw) {
|
|
62
63
|
return fromAccepts(first, "mrtr-input-required", raw);
|
|
63
64
|
},
|
|
64
|
-
/**
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
65
|
+
/**
|
|
66
|
+
* The signed payment the buyer presented. A v2 payload carries the
|
|
67
|
+
* requirement it accepted; a v1 payload does not, so the caller passes
|
|
68
|
+
* the `accepts` entry it answered.
|
|
69
|
+
*/
|
|
70
|
+
payload(payload, accepts) {
|
|
71
|
+
return fromAccepts(
|
|
72
|
+
accepts ?? payload.accepted,
|
|
73
|
+
"payment-signature",
|
|
74
|
+
toJsonObject(payload),
|
|
75
|
+
payerOf(payload)
|
|
76
|
+
);
|
|
72
77
|
}
|
|
73
78
|
};
|
|
74
79
|
function priceValue(price) {
|
|
@@ -81,6 +86,7 @@ function priceValue(price) {
|
|
|
81
86
|
|
|
82
87
|
export {
|
|
83
88
|
x402Currency,
|
|
89
|
+
x402Summary,
|
|
84
90
|
payerOf,
|
|
85
91
|
x402Moments
|
|
86
92
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
attachX402
|
|
3
|
+
} from "./chunk-OKC6VMFH.js";
|
|
4
|
+
|
|
5
|
+
// src/x402/middleware.ts
|
|
6
|
+
import {
|
|
7
|
+
x402HTTPResourceServer
|
|
8
|
+
} from "@x402/core/server";
|
|
9
|
+
function guardedPaymentMiddleware(fromHTTPServer, beltic, routes, server, opts = {}) {
|
|
10
|
+
const http = new x402HTTPResourceServer(server, routes);
|
|
11
|
+
attachX402(beltic, server, http, opts);
|
|
12
|
+
return fromHTTPServer(http, opts.paywall);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export {
|
|
16
|
+
guardedPaymentMiddleware
|
|
17
|
+
};
|
|
@@ -1,19 +1,14 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SESSION_HEADER,
|
|
3
|
+
decisionIdOf,
|
|
3
4
|
sessionIdOf
|
|
4
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-FAQ442YH.js";
|
|
5
6
|
import {
|
|
6
7
|
payloadDigest
|
|
7
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-X3W2Z5GC.js";
|
|
8
9
|
import {
|
|
9
10
|
x402Moments
|
|
10
|
-
} from "./chunk-
|
|
11
|
-
import {
|
|
12
|
-
summaryOf
|
|
13
|
-
} from "./chunk-7G5EHNVW.js";
|
|
14
|
-
import {
|
|
15
|
-
Verdict
|
|
16
|
-
} from "./chunk-46QN2KEZ.js";
|
|
11
|
+
} from "./chunk-LM4NIYE5.js";
|
|
17
12
|
|
|
18
13
|
// src/x402/adapter.ts
|
|
19
14
|
function attachX402(beltic, server, http, opts = {}) {
|
|
@@ -56,16 +51,21 @@ function attachX402(beltic, server, http, opts = {}) {
|
|
|
56
51
|
const presented = x402Moments.payload(payload);
|
|
57
52
|
await session.emit("payment.presented", presented);
|
|
58
53
|
inFlight.set(payloadDigest(payload), session);
|
|
59
|
-
const out = await beltic.evaluate(session.id,
|
|
54
|
+
const out = await beltic.evaluate(session.id, presented);
|
|
55
|
+
if (!out) return;
|
|
60
56
|
opts.onDecision?.({
|
|
61
57
|
sessionId: session.id,
|
|
62
58
|
decision: out.decision,
|
|
63
59
|
reasonCodes: out.reasonCodes,
|
|
64
|
-
born: session.born
|
|
60
|
+
born: session.born,
|
|
61
|
+
buyerDecisionId: decisionIdOf(payload.extensions)
|
|
65
62
|
});
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
63
|
+
if (out.verdict.blocks(beltic.onReview)) {
|
|
64
|
+
return {
|
|
65
|
+
abort: true,
|
|
66
|
+
reason: `BELTIC_${out.verdict.value}`,
|
|
67
|
+
message: out.reasonCodes.join(",")
|
|
68
|
+
};
|
|
69
69
|
}
|
|
70
70
|
return;
|
|
71
71
|
});
|
|
@@ -79,17 +79,6 @@ function attachX402(beltic, server, http, opts = {}) {
|
|
|
79
79
|
return { inFlight };
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
// src/x402/middleware.ts
|
|
83
|
-
import {
|
|
84
|
-
x402HTTPResourceServer
|
|
85
|
-
} from "@x402/core/server";
|
|
86
|
-
function guardedPaymentMiddleware(fromHTTPServer, beltic, routes, server, opts = {}) {
|
|
87
|
-
const http = new x402HTTPResourceServer(server, routes);
|
|
88
|
-
attachX402(beltic, server, http, opts);
|
|
89
|
-
return fromHTTPServer(http, opts.paywall);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
82
|
export {
|
|
93
|
-
attachX402
|
|
94
|
-
guardedPaymentMiddleware
|
|
83
|
+
attachX402
|
|
95
84
|
};
|