@dvmkit/sdk 0.1.2-rc.7 → 0.1.4-rc.7
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/README.md +121 -8
- package/dist/chunk-BIFLRKMO.js +87 -0
- package/dist/chunk-BQ2NMWKE.js +160 -0
- package/dist/{chunk-LDTWX7JW.js → chunk-BTZY7VPH.js} +13 -1
- package/dist/{chunk-FJDCFHW5.js → chunk-C6JHBLMW.js} +3 -81
- package/dist/{chunk-EXHBXA4U.js → chunk-DBCLBYHP.js} +13 -1
- package/dist/chunk-E4EVGPDX.js +391 -0
- package/dist/chunk-EDDYHZ6W.js +1010 -0
- package/dist/{chunk-2ABMGUDS.js → chunk-EVBK675R.js} +88 -10
- package/dist/{chunk-P4RUVDU7.js → chunk-H2MEFVH6.js} +12 -141
- package/dist/chunk-KXZUCCEY.js +142 -0
- package/dist/{chunk-LWUR4CGG.js → chunk-MLRCSJYX.js} +11 -3
- package/dist/{chunk-N4VTG3KH.js → chunk-QK3VJNCK.js} +930 -3011
- package/dist/chunk-RW5LP57K.js +44 -0
- package/dist/{chunk-6JZIX5WW.js → chunk-SSSZUVWM.js} +178 -8
- package/dist/{chunk-TKA6ZP4M.js → chunk-U6M3ATSG.js} +56 -426
- package/dist/chunk-VRQDX5P4.js +1742 -0
- package/dist/{chunk-JGGI65I3.js → chunk-Z4BNLUZF.js} +1 -150
- package/dist/{credit-ledger-ED6JXKVD.js → credit-ledger-2DFQHNLB.js} +2 -2
- package/dist/{credit-menu-BM4qCD5U.d.ts → credit-menu-C1ezIFlJ.d.ts} +1699 -1941
- package/dist/{fx-C-liI3oY.d.ts → fx-C6dl2LVI.d.ts} +1 -1
- package/dist/index.d.ts +7 -6
- package/dist/index.js +8 -4
- package/dist/internal/caller.d.ts +10441 -0
- package/dist/internal/caller.js +10078 -0
- package/dist/internal/index.d.ts +2 -10816
- package/dist/internal/index.js +2 -10130
- package/dist/internal/server.d.ts +404 -0
- package/dist/internal/server.js +202 -0
- package/dist/job-store-DHnW4Cg_.d.ts +591 -0
- package/dist/lightning-backend-Ci1nogk_.d.ts +367 -0
- package/dist/{memory-credit-ledger-XJ5VQEVP.js → memory-credit-ledger-OP24Z2KO.js} +3 -3
- package/dist/{postgres-job-store-J5F4GUWU.js → postgres-job-store-3RAXMNSY.js} +1 -1
- package/dist/{revenue-reporter-JIKUPXOK.js → revenue-reporter-ASZ7SHHH.js} +1 -1
- package/dist/server/index.d.ts +41 -11
- package/dist/server/index.js +93 -69
- package/dist/{job-store-C53VQ5uu.d.ts → step-cache-BLPZNizw.d.ts} +176 -585
- package/dist/{tempo-session-store-DALMRIWN.js → tempo-session-store-2JNOKJGX.js} +2 -2
- package/dist/testing/index.d.ts +25 -3
- package/dist/testing/index.js +36 -3
- package/dist/{usd-gLcJB1ps.d.ts → usd-BgOfZlk6.d.ts} +1 -1
- package/dist/wallet-CJC8lwxx.d.ts +29 -0
- package/dist/{x402-FTG2GRAQ.js → x402-T2C5MX3T.js} +6 -3
- package/package.json +11 -5
- package/dist/{chunk-RU7SXHLO.js → chunk-UP2F5RRT.js} +3 -3
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import {
|
|
2
|
+
initCallerLoggers
|
|
3
|
+
} from "./chunk-66HGCPBU.js";
|
|
4
|
+
|
|
5
|
+
// src/sdk/step-cache.ts
|
|
6
|
+
var StepCache = class _StepCache {
|
|
7
|
+
cache = /* @__PURE__ */ new Map();
|
|
8
|
+
/** Check if a step result is cached. */
|
|
9
|
+
has(id) {
|
|
10
|
+
return this.cache.has(id);
|
|
11
|
+
}
|
|
12
|
+
/** Get a cached step result. Throws if not present. */
|
|
13
|
+
get(id) {
|
|
14
|
+
return this.getRecord(id).value;
|
|
15
|
+
}
|
|
16
|
+
/** Get a cached step record, including replay metadata. Throws if not present. */
|
|
17
|
+
getRecord(id) {
|
|
18
|
+
const record = this.cache.get(id);
|
|
19
|
+
if (record === void 0) throw new Error(`StepCache: no cached result for step "${id}"`);
|
|
20
|
+
return record;
|
|
21
|
+
}
|
|
22
|
+
/** Cache a step result. */
|
|
23
|
+
set(id, value, costs) {
|
|
24
|
+
this.cache.set(id, {
|
|
25
|
+
id,
|
|
26
|
+
value,
|
|
27
|
+
...costs !== void 0 && costs.length > 0 ? { costs: costs.map(({ amount, currency }) => ({ amount, currency })) } : {}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/** Export all cached steps for persistence. */
|
|
31
|
+
serialize() {
|
|
32
|
+
return [...this.cache.values()];
|
|
33
|
+
}
|
|
34
|
+
/** Restore a StepCache from persisted records. */
|
|
35
|
+
static deserialize(records) {
|
|
36
|
+
const cache = new _StepCache();
|
|
37
|
+
for (const { id, value, costs } of records) {
|
|
38
|
+
cache.set(id, value, costs);
|
|
39
|
+
}
|
|
40
|
+
return cache;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
// src/sdk/logger.ts
|
|
45
|
+
function createConsoleLogger(jobId) {
|
|
46
|
+
const fmt = (message, data) => {
|
|
47
|
+
const parts = [`[${jobId}] ${message}`];
|
|
48
|
+
if (data) parts.push(data);
|
|
49
|
+
return parts;
|
|
50
|
+
};
|
|
51
|
+
return {
|
|
52
|
+
debug(message, data) {
|
|
53
|
+
console.debug(...fmt(message, data));
|
|
54
|
+
},
|
|
55
|
+
info(message, data) {
|
|
56
|
+
console.info(...fmt(message, data));
|
|
57
|
+
},
|
|
58
|
+
warn(message, data) {
|
|
59
|
+
console.warn(...fmt(message, data));
|
|
60
|
+
},
|
|
61
|
+
error(message, data) {
|
|
62
|
+
console.error(...fmt(message, data));
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function createStdoutLogger(context) {
|
|
67
|
+
const emit = (sink, level) => (message, data) => {
|
|
68
|
+
sink(JSON.stringify({ level, msg: message, ...context, ...data }));
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
debug: emit(console.debug.bind(console), "debug"),
|
|
72
|
+
info: emit(console.info.bind(console), "info"),
|
|
73
|
+
warn: emit(console.warn.bind(console), "warn"),
|
|
74
|
+
error: emit(console.error.bind(console), "error")
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function createNoopLogger() {
|
|
78
|
+
const noop2 = () => {
|
|
79
|
+
};
|
|
80
|
+
return { debug: noop2, info: noop2, warn: noop2, error: noop2 };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/observability/context.ts
|
|
84
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
85
|
+
import { randomBytes, randomUUID } from "crypto";
|
|
86
|
+
var storage = new AsyncLocalStorage();
|
|
87
|
+
var defaultService = "unknown";
|
|
88
|
+
function setDefaultService(service) {
|
|
89
|
+
defaultService = service;
|
|
90
|
+
}
|
|
91
|
+
function randomTraceId() {
|
|
92
|
+
const buf = randomBytes(16);
|
|
93
|
+
const now = Date.now();
|
|
94
|
+
buf.writeUInt16BE(Math.floor(now / 4294967296), 0);
|
|
95
|
+
buf.writeUInt32BE(now % 4294967296, 2);
|
|
96
|
+
buf[6] = buf[6] & 15 | 112;
|
|
97
|
+
buf[8] = buf[8] & 63 | 128;
|
|
98
|
+
return buf.toString("hex");
|
|
99
|
+
}
|
|
100
|
+
function randomSpanId() {
|
|
101
|
+
return randomUUID().replace(/-/g, "").slice(0, 16);
|
|
102
|
+
}
|
|
103
|
+
function withTraceContext(ctx, fn) {
|
|
104
|
+
return storage.run(ctx, fn);
|
|
105
|
+
}
|
|
106
|
+
function currentContext(service) {
|
|
107
|
+
const ctx = storage.getStore();
|
|
108
|
+
if (ctx) return ctx;
|
|
109
|
+
return {
|
|
110
|
+
traceId: randomTraceId(),
|
|
111
|
+
spanId: null,
|
|
112
|
+
dimensions: {},
|
|
113
|
+
service: service ?? defaultService
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function pinDimension(key, value) {
|
|
117
|
+
const ctx = storage.getStore();
|
|
118
|
+
if (ctx) {
|
|
119
|
+
ctx.dimensions = { ...ctx.dimensions, [key]: value };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function rawContext() {
|
|
123
|
+
return storage.getStore();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// src/observability/with-span.ts
|
|
127
|
+
import { hostname } from "os";
|
|
128
|
+
|
|
129
|
+
// src/observability/stdout-sink.ts
|
|
130
|
+
var SENSITIVE_KEY_RE = /secret|token|password|private|bearer|seed|preimage|api[_-]?key|signing[_-]?key|priv[_-]?key/i;
|
|
131
|
+
var isTTY = typeof process.stdout.isTTY === "boolean" && process.stdout.isTTY;
|
|
132
|
+
var RED = isTTY ? "\x1B[31m" : "";
|
|
133
|
+
var YELLOW = isTTY ? "\x1B[33m" : "";
|
|
134
|
+
var RESET = isTTY ? "\x1B[0m" : "";
|
|
135
|
+
function formatSpanLine(span) {
|
|
136
|
+
const ts = new Date(span.started_at_ms).toISOString();
|
|
137
|
+
const levelStr = colorLevel(span.level);
|
|
138
|
+
const parts = [`[${ts}]`, span.dataset, `${levelStr}:`, span.name];
|
|
139
|
+
if (span.trace_id) {
|
|
140
|
+
parts.push(`trace=${span.trace_id}`);
|
|
141
|
+
}
|
|
142
|
+
for (const [k, v] of Object.entries(span.dimensions)) {
|
|
143
|
+
parts.push(formatKV(k, v));
|
|
144
|
+
}
|
|
145
|
+
for (const [k, v] of Object.entries(span.attributes)) {
|
|
146
|
+
parts.push(formatKV(k, v));
|
|
147
|
+
}
|
|
148
|
+
const durationMs = span.ended_at_ms - span.started_at_ms;
|
|
149
|
+
if (durationMs > 0) {
|
|
150
|
+
parts.push(`duration=${durationMs}ms`);
|
|
151
|
+
}
|
|
152
|
+
if (span.error_message) {
|
|
153
|
+
parts.push(`error=${truncate(span.error_message, 120)}`);
|
|
154
|
+
}
|
|
155
|
+
return parts.join(" ");
|
|
156
|
+
}
|
|
157
|
+
function writeSpanLine(span) {
|
|
158
|
+
const line = formatSpanLine(span);
|
|
159
|
+
process.stdout.write(line + "\n");
|
|
160
|
+
}
|
|
161
|
+
function colorLevel(level) {
|
|
162
|
+
if (level === "error") return `${RED}error${RESET}`;
|
|
163
|
+
if (level === "warn") return `${YELLOW}warn${RESET}`;
|
|
164
|
+
return level;
|
|
165
|
+
}
|
|
166
|
+
function formatKV(key, value) {
|
|
167
|
+
if (SENSITIVE_KEY_RE.test(key)) return `${key}=[REDACTED]`;
|
|
168
|
+
return `${key}=${truncate(stringify(value), 80)}`;
|
|
169
|
+
}
|
|
170
|
+
function stringify(value) {
|
|
171
|
+
if (typeof value === "string") return value;
|
|
172
|
+
if (value === void 0) return "undefined";
|
|
173
|
+
if (value === null) return "null";
|
|
174
|
+
if (typeof value === "function") return "[function]";
|
|
175
|
+
if (typeof value === "symbol") return value.toString();
|
|
176
|
+
if (typeof value === "bigint") return value.toString();
|
|
177
|
+
return JSON.stringify(value);
|
|
178
|
+
}
|
|
179
|
+
function truncate(s, max) {
|
|
180
|
+
if (s.length <= max) return s;
|
|
181
|
+
return s.slice(0, max - 3) + "...";
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// src/observability/with-span.ts
|
|
185
|
+
var emitter = null;
|
|
186
|
+
function setEmitter(fn) {
|
|
187
|
+
emitter = fn;
|
|
188
|
+
}
|
|
189
|
+
var cachedHost = hostname();
|
|
190
|
+
var serviceVersion = process.env.GIT_SHA ?? process.env.FLY_IMAGE_REF ?? "dev";
|
|
191
|
+
async function withSpan(opts, fn) {
|
|
192
|
+
const spanId = randomSpanId();
|
|
193
|
+
const existingCtx = rawContext();
|
|
194
|
+
const ctx = existingCtx ?? currentContext();
|
|
195
|
+
const startMs = opts.startMs ?? Date.now();
|
|
196
|
+
let status = "ok";
|
|
197
|
+
let level = opts.level;
|
|
198
|
+
let errorMessage = null;
|
|
199
|
+
let errorStack = null;
|
|
200
|
+
if (opts.err) {
|
|
201
|
+
status = "error";
|
|
202
|
+
errorMessage = opts.err.message;
|
|
203
|
+
errorStack = opts.err.stack ?? null;
|
|
204
|
+
}
|
|
205
|
+
const childCtx = {
|
|
206
|
+
traceId: ctx.traceId,
|
|
207
|
+
spanId,
|
|
208
|
+
dimensions: { ...ctx.dimensions, ...opts.dims },
|
|
209
|
+
service: ctx.service,
|
|
210
|
+
tracestate: ctx.tracestate
|
|
211
|
+
};
|
|
212
|
+
const emitRow = () => {
|
|
213
|
+
const endMs = opts.endMs ?? Date.now();
|
|
214
|
+
const row = {
|
|
215
|
+
span_id: spanId,
|
|
216
|
+
trace_id: ctx.traceId,
|
|
217
|
+
parent_span_id: ctx.spanId,
|
|
218
|
+
dataset: opts.dataset,
|
|
219
|
+
name: opts.name,
|
|
220
|
+
level: level ?? (status === "error" ? "error" : "info"),
|
|
221
|
+
status,
|
|
222
|
+
started_at_ms: startMs,
|
|
223
|
+
ended_at_ms: endMs,
|
|
224
|
+
service: ctx.service,
|
|
225
|
+
service_version: serviceVersion,
|
|
226
|
+
host: cachedHost,
|
|
227
|
+
pid: process.pid,
|
|
228
|
+
dimensions: childCtx.dimensions,
|
|
229
|
+
attributes: opts.attrs ?? {},
|
|
230
|
+
error_message: errorMessage,
|
|
231
|
+
error_stack: errorStack,
|
|
232
|
+
sample_rate: 1
|
|
233
|
+
};
|
|
234
|
+
writeSpanLine(row);
|
|
235
|
+
if (emitter) emitter(row);
|
|
236
|
+
};
|
|
237
|
+
try {
|
|
238
|
+
const result = await withTraceContext(childCtx, () => fn());
|
|
239
|
+
if (opts.inspect) {
|
|
240
|
+
const overrides = opts.inspect(result);
|
|
241
|
+
if (overrides) {
|
|
242
|
+
if (overrides.status) status = overrides.status;
|
|
243
|
+
if (overrides.level) level = overrides.level;
|
|
244
|
+
if (overrides.errorMessage !== void 0) errorMessage = overrides.errorMessage;
|
|
245
|
+
if (overrides.attrs) Object.assign(opts.attrs ??= {}, overrides.attrs);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
emitRow();
|
|
249
|
+
return result;
|
|
250
|
+
} catch (err) {
|
|
251
|
+
status = "error";
|
|
252
|
+
if (err instanceof Error) {
|
|
253
|
+
errorMessage = err.message;
|
|
254
|
+
errorStack = err.stack ?? null;
|
|
255
|
+
} else {
|
|
256
|
+
errorMessage = String(err);
|
|
257
|
+
}
|
|
258
|
+
if (opts.onError) {
|
|
259
|
+
const overrides = opts.onError(err);
|
|
260
|
+
if (overrides) Object.assign(opts.attrs ??= {}, overrides);
|
|
261
|
+
}
|
|
262
|
+
emitRow();
|
|
263
|
+
throw err;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// src/observability/datasets.ts
|
|
268
|
+
var SPANS_DATASETS = [
|
|
269
|
+
{ name: "platform", retentionDays: 90 },
|
|
270
|
+
{ name: "cashu", retentionDays: 90 },
|
|
271
|
+
{ name: "lightning", retentionDays: 90 },
|
|
272
|
+
{ name: "stripe", retentionDays: 90 },
|
|
273
|
+
{ name: "x402", retentionDays: 90 },
|
|
274
|
+
{ name: "tempo", retentionDays: 90 },
|
|
275
|
+
{ name: "isolate", retentionDays: 90 },
|
|
276
|
+
{ name: "audit", retentionDays: 365 },
|
|
277
|
+
{ name: "cron", retentionDays: 30 }
|
|
278
|
+
];
|
|
279
|
+
var DATASET_NAMES = new Set(SPANS_DATASETS.map((d) => d.name));
|
|
280
|
+
|
|
281
|
+
// src/observability/loggers.ts
|
|
282
|
+
var noop = () => {
|
|
283
|
+
};
|
|
284
|
+
var loggers = buildNoopLoggers();
|
|
285
|
+
function initLoggers(writer, service) {
|
|
286
|
+
setEmitter((row) => {
|
|
287
|
+
writer.enqueue(row);
|
|
288
|
+
});
|
|
289
|
+
setDefaultService(service);
|
|
290
|
+
const result = {};
|
|
291
|
+
for (const ds of SPANS_DATASETS) {
|
|
292
|
+
result[ds.name] = createLogger(ds.name, {});
|
|
293
|
+
}
|
|
294
|
+
loggers = result;
|
|
295
|
+
initCallerLoggers(result);
|
|
296
|
+
return result;
|
|
297
|
+
}
|
|
298
|
+
function createLogger(dataset, bakedDims) {
|
|
299
|
+
const logger = {
|
|
300
|
+
info(name, attrs) {
|
|
301
|
+
void withSpan({ dataset, name, level: "info", attrs, dims: bakedDims }, noop).catch(noop);
|
|
302
|
+
},
|
|
303
|
+
warn(name, attrs) {
|
|
304
|
+
void withSpan({ dataset, name, level: "warn", attrs, dims: bakedDims }, noop).catch(noop);
|
|
305
|
+
},
|
|
306
|
+
error(name, attrs) {
|
|
307
|
+
const err = attrs?.err instanceof Error ? attrs.err : void 0;
|
|
308
|
+
const cleanAttrs = attrs ? { ...attrs } : void 0;
|
|
309
|
+
if (cleanAttrs) delete cleanAttrs.err;
|
|
310
|
+
void withSpan(
|
|
311
|
+
{ dataset, name, level: "error", attrs: cleanAttrs, dims: bakedDims, err },
|
|
312
|
+
noop
|
|
313
|
+
).catch(noop);
|
|
314
|
+
},
|
|
315
|
+
async span(name, attrs, fn, opts) {
|
|
316
|
+
return withSpan({ dataset, name, attrs, dims: bakedDims, onError: opts?.onError }, fn);
|
|
317
|
+
},
|
|
318
|
+
timer(name, attrs) {
|
|
319
|
+
const startMs = Date.now();
|
|
320
|
+
return {
|
|
321
|
+
[Symbol.dispose]() {
|
|
322
|
+
const endMs = Date.now();
|
|
323
|
+
void withSpan({ dataset, name, attrs, dims: bakedDims, startMs, endMs }, noop).catch(
|
|
324
|
+
noop
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
},
|
|
329
|
+
set(dims) {
|
|
330
|
+
for (const [k, v] of Object.entries(dims)) {
|
|
331
|
+
pinDimension(k, v);
|
|
332
|
+
}
|
|
333
|
+
},
|
|
334
|
+
extend(dims) {
|
|
335
|
+
return createLogger(dataset, { ...bakedDims, ...dims });
|
|
336
|
+
},
|
|
337
|
+
tracked(name, fn) {
|
|
338
|
+
return ((...args) => logger.span(name, {}, () => fn(...args)));
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
return logger;
|
|
342
|
+
}
|
|
343
|
+
function buildNoopLoggers() {
|
|
344
|
+
const noopLogger = {
|
|
345
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
346
|
+
info() {
|
|
347
|
+
},
|
|
348
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
349
|
+
warn() {
|
|
350
|
+
},
|
|
351
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
352
|
+
error() {
|
|
353
|
+
},
|
|
354
|
+
async span(_name, _attrs, fn, _opts) {
|
|
355
|
+
return fn();
|
|
356
|
+
},
|
|
357
|
+
timer() {
|
|
358
|
+
return { [Symbol.dispose]() {
|
|
359
|
+
} };
|
|
360
|
+
},
|
|
361
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
362
|
+
set() {
|
|
363
|
+
},
|
|
364
|
+
extend() {
|
|
365
|
+
return noopLogger;
|
|
366
|
+
},
|
|
367
|
+
tracked(_name, fn) {
|
|
368
|
+
return fn;
|
|
369
|
+
}
|
|
370
|
+
};
|
|
371
|
+
const result = {};
|
|
372
|
+
for (const ds of SPANS_DATASETS) {
|
|
373
|
+
result[ds.name] = noopLogger;
|
|
374
|
+
}
|
|
375
|
+
return result;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export {
|
|
379
|
+
StepCache,
|
|
380
|
+
createConsoleLogger,
|
|
381
|
+
createStdoutLogger,
|
|
382
|
+
createNoopLogger,
|
|
383
|
+
randomTraceId,
|
|
384
|
+
withTraceContext,
|
|
385
|
+
currentContext,
|
|
386
|
+
pinDimension,
|
|
387
|
+
setEmitter,
|
|
388
|
+
withSpan,
|
|
389
|
+
loggers,
|
|
390
|
+
initLoggers
|
|
391
|
+
};
|