bitfab 0.9.2
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 +292 -0
- package/dist/chunk-VGALEXKQ.js +346 -0
- package/dist/chunk-VGALEXKQ.js.map +1 -0
- package/dist/chunk-WUJR72QY.js +1214 -0
- package/dist/chunk-WUJR72QY.js.map +1 -0
- package/dist/index.cjs +1731 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +568 -0
- package/dist/index.d.ts +568 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/node.cjs +1749 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.cts +2 -0
- package/dist/node.d.ts +2 -0
- package/dist/node.js +36 -0
- package/dist/node.js.map +1 -0
- package/dist/replay-DJ2HXTHQ.js +100 -0
- package/dist/replay-DJ2HXTHQ.js.map +1 -0
- package/package.json +88 -0
|
@@ -0,0 +1,1214 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BitfabError,
|
|
3
|
+
DEFAULT_SERVICE_URL,
|
|
4
|
+
HttpClient,
|
|
5
|
+
asyncStorageReady,
|
|
6
|
+
createAsyncLocalStorage,
|
|
7
|
+
getReplayContext,
|
|
8
|
+
isAsyncStorageInitDone,
|
|
9
|
+
serializeValue
|
|
10
|
+
} from "./chunk-VGALEXKQ.js";
|
|
11
|
+
|
|
12
|
+
// src/baml.ts
|
|
13
|
+
var cachedBaml = null;
|
|
14
|
+
async function loadBaml() {
|
|
15
|
+
if (cachedBaml) {
|
|
16
|
+
return cachedBaml;
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
cachedBaml = await import("@boundaryml/baml");
|
|
20
|
+
return cachedBaml;
|
|
21
|
+
} catch {
|
|
22
|
+
throw new Error(
|
|
23
|
+
"@boundaryml/baml is required for Bitfab.call(). Install it with: npm install @boundaryml/baml"
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function capitalize(str) {
|
|
28
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
29
|
+
}
|
|
30
|
+
function formatProvider(provider) {
|
|
31
|
+
const providerMap = {
|
|
32
|
+
openai: "OpenAI",
|
|
33
|
+
anthropic: "Anthropic",
|
|
34
|
+
google: "Google"
|
|
35
|
+
};
|
|
36
|
+
return providerMap[provider] ?? capitalize(provider);
|
|
37
|
+
}
|
|
38
|
+
function formatModel(model) {
|
|
39
|
+
return model.replace(/^gpt-/, "GPT").replace(/\./g, "_").replace(/-/g, "_");
|
|
40
|
+
}
|
|
41
|
+
function getClientName(provider, model) {
|
|
42
|
+
return `${formatProvider(provider)}_${formatModel(model)}`;
|
|
43
|
+
}
|
|
44
|
+
function generateClientDefinitions(providers) {
|
|
45
|
+
const definitions = [];
|
|
46
|
+
for (const providerDef of providers) {
|
|
47
|
+
for (const model of providerDef.models) {
|
|
48
|
+
const clientName = getClientName(providerDef.provider, model.model);
|
|
49
|
+
definitions.push(`client<llm> ${clientName} {
|
|
50
|
+
provider ${providerDef.provider}
|
|
51
|
+
options {
|
|
52
|
+
model "${model.model}"
|
|
53
|
+
api_key env.${providerDef.apiKeyEnv}
|
|
54
|
+
}
|
|
55
|
+
}`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return definitions.join("\n\n");
|
|
59
|
+
}
|
|
60
|
+
function withDefaultClients(bamlSource, providers) {
|
|
61
|
+
const hasDefaultClient = bamlSource.includes("client<llm> OpenAI_");
|
|
62
|
+
if (hasDefaultClient) {
|
|
63
|
+
return bamlSource;
|
|
64
|
+
}
|
|
65
|
+
const defaultClients = generateClientDefinitions(providers);
|
|
66
|
+
return `${defaultClients}
|
|
67
|
+
|
|
68
|
+
${bamlSource}`;
|
|
69
|
+
}
|
|
70
|
+
function extractFunctionName(bamlSource) {
|
|
71
|
+
const match = bamlSource.match(/function\s+(\w+)\s*\(/);
|
|
72
|
+
return match?.[1] ?? null;
|
|
73
|
+
}
|
|
74
|
+
function extractFunctionParameters(bamlSource) {
|
|
75
|
+
const functionMatch = bamlSource.match(/function\s+\w+\s*\(([^)]*)\)\s*->/);
|
|
76
|
+
if (!functionMatch) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
const paramsString = functionMatch[1].trim();
|
|
80
|
+
if (!paramsString) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
const params = [];
|
|
84
|
+
const paramParts = splitParameters(paramsString);
|
|
85
|
+
for (const part of paramParts) {
|
|
86
|
+
const trimmed = part.trim();
|
|
87
|
+
if (!trimmed) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const paramMatch = trimmed.match(/^(\w+)\s*:\s*(.+)$/);
|
|
91
|
+
if (paramMatch) {
|
|
92
|
+
const name = paramMatch[1];
|
|
93
|
+
let type = paramMatch[2].trim();
|
|
94
|
+
const isOptional = type.endsWith("?");
|
|
95
|
+
if (isOptional) {
|
|
96
|
+
type = type.slice(0, -1);
|
|
97
|
+
}
|
|
98
|
+
params.push({ name, type, isOptional });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return params;
|
|
102
|
+
}
|
|
103
|
+
function splitParameters(paramsString) {
|
|
104
|
+
const parts = [];
|
|
105
|
+
let current = "";
|
|
106
|
+
let depth = 0;
|
|
107
|
+
for (const char of paramsString) {
|
|
108
|
+
if (char === "<") {
|
|
109
|
+
depth++;
|
|
110
|
+
current += char;
|
|
111
|
+
} else if (char === ">") {
|
|
112
|
+
depth--;
|
|
113
|
+
current += char;
|
|
114
|
+
} else if (char === "," && depth === 0) {
|
|
115
|
+
parts.push(current);
|
|
116
|
+
current = "";
|
|
117
|
+
} else {
|
|
118
|
+
current += char;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (current.trim()) {
|
|
122
|
+
parts.push(current);
|
|
123
|
+
}
|
|
124
|
+
return parts;
|
|
125
|
+
}
|
|
126
|
+
function coerceToType(value, expectedType) {
|
|
127
|
+
if (expectedType === "string") {
|
|
128
|
+
return value;
|
|
129
|
+
}
|
|
130
|
+
if (expectedType === "int") {
|
|
131
|
+
const parsed = Number.parseInt(value, 10);
|
|
132
|
+
if (!Number.isNaN(parsed)) {
|
|
133
|
+
return parsed;
|
|
134
|
+
}
|
|
135
|
+
return value;
|
|
136
|
+
}
|
|
137
|
+
if (expectedType === "float") {
|
|
138
|
+
const parsed = Number.parseFloat(value);
|
|
139
|
+
if (!Number.isNaN(parsed)) {
|
|
140
|
+
return parsed;
|
|
141
|
+
}
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
if (expectedType === "bool") {
|
|
145
|
+
const lower = value.toLowerCase();
|
|
146
|
+
if (lower === "true") {
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
if (lower === "false") {
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
if (expectedType.endsWith("[]")) {
|
|
155
|
+
try {
|
|
156
|
+
const parsed = JSON.parse(value);
|
|
157
|
+
if (Array.isArray(parsed)) {
|
|
158
|
+
return parsed;
|
|
159
|
+
}
|
|
160
|
+
} catch {
|
|
161
|
+
}
|
|
162
|
+
return value;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
return JSON.parse(value);
|
|
166
|
+
} catch {
|
|
167
|
+
return value;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function coerceInputs(inputs, expectedTypes) {
|
|
171
|
+
const coerced = {};
|
|
172
|
+
for (const [key, value] of Object.entries(inputs)) {
|
|
173
|
+
if (typeof value === "string") {
|
|
174
|
+
const expectedType = expectedTypes.get(key);
|
|
175
|
+
if (expectedType) {
|
|
176
|
+
coerced[key] = coerceToType(value, expectedType);
|
|
177
|
+
} else {
|
|
178
|
+
coerced[key] = value;
|
|
179
|
+
}
|
|
180
|
+
} else {
|
|
181
|
+
coerced[key] = value;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return coerced;
|
|
185
|
+
}
|
|
186
|
+
function objToDict(obj, depth = 0, maxDepth = 5) {
|
|
187
|
+
if (depth > maxDepth) {
|
|
188
|
+
return `<max depth reached: ${typeof obj}>`;
|
|
189
|
+
}
|
|
190
|
+
if (obj === null || obj === void 0 || typeof obj === "string" || typeof obj === "number" || typeof obj === "boolean") {
|
|
191
|
+
return obj;
|
|
192
|
+
}
|
|
193
|
+
if (Array.isArray(obj)) {
|
|
194
|
+
return obj.map((item) => objToDict(item, depth + 1, maxDepth));
|
|
195
|
+
}
|
|
196
|
+
if (typeof obj === "object") {
|
|
197
|
+
const result = {};
|
|
198
|
+
if (obj.constructor && obj.constructor.name !== "Object") {
|
|
199
|
+
result.__type__ = obj.constructor.name;
|
|
200
|
+
}
|
|
201
|
+
for (const key of Object.keys(obj)) {
|
|
202
|
+
if (key.startsWith("_")) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
try {
|
|
206
|
+
const value = obj[key];
|
|
207
|
+
if (typeof value === "function") {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
result[key] = objToDict(value, depth + 1, maxDepth);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
result[key] = `<error: ${error instanceof Error ? error.message : String(error)}>`;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const proto = Object.getPrototypeOf(obj);
|
|
217
|
+
if (proto && proto !== Object.prototype) {
|
|
218
|
+
const descriptors = Object.getOwnPropertyDescriptors(proto);
|
|
219
|
+
for (const [key, descriptor] of Object.entries(descriptors)) {
|
|
220
|
+
if (key.startsWith("_") || key === "constructor" || key in result) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (descriptor.get) {
|
|
224
|
+
try {
|
|
225
|
+
const value = obj[key];
|
|
226
|
+
if (typeof value !== "function") {
|
|
227
|
+
result[key] = objToDict(value, depth + 1, maxDepth);
|
|
228
|
+
}
|
|
229
|
+
} catch {
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
} catch {
|
|
235
|
+
}
|
|
236
|
+
return result;
|
|
237
|
+
}
|
|
238
|
+
return String(obj);
|
|
239
|
+
}
|
|
240
|
+
function serializeCollector(collector) {
|
|
241
|
+
try {
|
|
242
|
+
return objToDict(collector, 0, 5);
|
|
243
|
+
} catch (_error) {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
var ALLOWED_ENV_KEYS = ["OPENAI_API_KEY"];
|
|
248
|
+
function filterEnvVars(envVars) {
|
|
249
|
+
const filtered = {};
|
|
250
|
+
for (const key of ALLOWED_ENV_KEYS) {
|
|
251
|
+
const value = envVars[key];
|
|
252
|
+
if (value) {
|
|
253
|
+
filtered[key] = value;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return filtered;
|
|
257
|
+
}
|
|
258
|
+
async function runFunctionWithBaml(bamlSource, inputs, providers, envVars) {
|
|
259
|
+
const { BamlRuntime, Collector } = await loadBaml();
|
|
260
|
+
const functionName = extractFunctionName(bamlSource);
|
|
261
|
+
if (!functionName) {
|
|
262
|
+
throw new Error("No function found in BAML source");
|
|
263
|
+
}
|
|
264
|
+
const fullSource = withDefaultClients(bamlSource, providers);
|
|
265
|
+
const filteredEnvVars = filterEnvVars(envVars);
|
|
266
|
+
const runtime = BamlRuntime.fromFiles(
|
|
267
|
+
"/tmp/baml_runtime",
|
|
268
|
+
{ "source.baml": fullSource },
|
|
269
|
+
filteredEnvVars
|
|
270
|
+
);
|
|
271
|
+
const ctx = runtime.createContextManager();
|
|
272
|
+
const collector = new Collector("bitfab-collector");
|
|
273
|
+
const params = extractFunctionParameters(bamlSource);
|
|
274
|
+
const expectedTypes = new Map(params.map((p) => [p.name, p.type]));
|
|
275
|
+
const args = coerceInputs(inputs, expectedTypes);
|
|
276
|
+
const functionResult = await runtime.callFunction(
|
|
277
|
+
functionName,
|
|
278
|
+
args,
|
|
279
|
+
ctx,
|
|
280
|
+
null,
|
|
281
|
+
// TypeBuilder
|
|
282
|
+
null,
|
|
283
|
+
// ClientRegistry
|
|
284
|
+
[collector],
|
|
285
|
+
// Collectors - capture execution data
|
|
286
|
+
{},
|
|
287
|
+
// Tags
|
|
288
|
+
filteredEnvVars
|
|
289
|
+
);
|
|
290
|
+
if (!functionResult.isOk()) {
|
|
291
|
+
throw new Error("BAML function execution failed");
|
|
292
|
+
}
|
|
293
|
+
const rawCollector = serializeCollector(collector);
|
|
294
|
+
return {
|
|
295
|
+
result: functionResult.parsed(false),
|
|
296
|
+
rawCollector
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/tracing.ts
|
|
301
|
+
var BitfabOpenAITracingProcessor = class {
|
|
302
|
+
/**
|
|
303
|
+
* Initialize the tracing processor.
|
|
304
|
+
*
|
|
305
|
+
* @param config - Configuration options
|
|
306
|
+
*/
|
|
307
|
+
constructor(config) {
|
|
308
|
+
this.activeTraces = {};
|
|
309
|
+
this.activeSpanMappings = {};
|
|
310
|
+
this.httpClient = new HttpClient({
|
|
311
|
+
apiKey: config.apiKey,
|
|
312
|
+
serviceUrl: config.serviceUrl ?? DEFAULT_SERVICE_URL,
|
|
313
|
+
timeout: config.timeout ?? 1e4
|
|
314
|
+
});
|
|
315
|
+
this.getActiveSpanContext = config.getActiveSpanContext ?? null;
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Called when a trace is started.
|
|
319
|
+
* If there's an active withSpan context, the trace ID is remapped to the
|
|
320
|
+
* outer trace and sent to pre-create the external_traces row on the server.
|
|
321
|
+
*/
|
|
322
|
+
async onTraceStart(trace) {
|
|
323
|
+
this.activeTraces[trace.traceId] = trace;
|
|
324
|
+
const activeContext = this.getActiveSpanContext?.();
|
|
325
|
+
if (activeContext) {
|
|
326
|
+
this.activeSpanMappings[trace.traceId] = activeContext;
|
|
327
|
+
}
|
|
328
|
+
this.sendTrace(trace, activeContext ? { id: activeContext.traceId } : {});
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Called when a trace is ended.
|
|
332
|
+
* If mapped to a withSpan trace, sends with remapped ID and completed=false
|
|
333
|
+
* since the parent withSpan handles completion.
|
|
334
|
+
*/
|
|
335
|
+
async onTraceEnd(trace) {
|
|
336
|
+
const mapping = this.activeSpanMappings[trace.traceId];
|
|
337
|
+
this.sendTrace(
|
|
338
|
+
trace,
|
|
339
|
+
mapping ? { id: mapping.traceId } : { completed: true }
|
|
340
|
+
);
|
|
341
|
+
delete this.activeSpanMappings[trace.traceId];
|
|
342
|
+
delete this.activeTraces[trace.traceId];
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Called when a span is started.
|
|
346
|
+
*/
|
|
347
|
+
// biome-ignore lint/suspicious/noExplicitAny: OpenAI Agents SDK uses any for span data
|
|
348
|
+
async onSpanStart(span) {
|
|
349
|
+
this.sendSpan(span);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Called when a span is ended.
|
|
353
|
+
*
|
|
354
|
+
* Send all spans to Bitfab for complete trace capture.
|
|
355
|
+
*/
|
|
356
|
+
// biome-ignore lint/suspicious/noExplicitAny: OpenAI Agents SDK uses any for span data
|
|
357
|
+
async onSpanEnd(span) {
|
|
358
|
+
this.sendSpan(span);
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Called when a trace is being flushed.
|
|
362
|
+
*/
|
|
363
|
+
async forceFlush() {
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Called when the trace processor is shutting down.
|
|
367
|
+
*/
|
|
368
|
+
async shutdown(_timeout) {
|
|
369
|
+
this.activeTraces = {};
|
|
370
|
+
this.activeSpanMappings = {};
|
|
371
|
+
}
|
|
372
|
+
/**
|
|
373
|
+
* Send trace to Bitfab API (fire-and-forget).
|
|
374
|
+
* When traceIdOverride is provided, the trace ID is remapped to link
|
|
375
|
+
* the OpenAI trace into an outer withSpan trace.
|
|
376
|
+
*/
|
|
377
|
+
sendTrace(trace, overrides = {}) {
|
|
378
|
+
try {
|
|
379
|
+
const { completed, ...traceOverrides } = overrides;
|
|
380
|
+
const traceData = trace.toJSON();
|
|
381
|
+
Object.assign(traceData, traceOverrides);
|
|
382
|
+
this.httpClient.sendExternalTrace({
|
|
383
|
+
type: "openai",
|
|
384
|
+
source: "typescript-sdk-openai-tracing",
|
|
385
|
+
externalTrace: traceData,
|
|
386
|
+
completed: completed ?? false
|
|
387
|
+
});
|
|
388
|
+
} catch {
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Export span to JSON object, collecting any errors.
|
|
393
|
+
*/
|
|
394
|
+
exportSpan(span) {
|
|
395
|
+
const errors = [];
|
|
396
|
+
let serializedSpan;
|
|
397
|
+
try {
|
|
398
|
+
const jsonResult = span.toJSON();
|
|
399
|
+
if (typeof jsonResult !== "object" || jsonResult === null) {
|
|
400
|
+
errors.push({
|
|
401
|
+
step: "span.toJSON()",
|
|
402
|
+
error: `Returned unexpected type: ${typeof jsonResult}`
|
|
403
|
+
});
|
|
404
|
+
serializedSpan = {};
|
|
405
|
+
} else {
|
|
406
|
+
serializedSpan = jsonResult;
|
|
407
|
+
}
|
|
408
|
+
} catch (error) {
|
|
409
|
+
errors.push({
|
|
410
|
+
step: "span.toJSON()",
|
|
411
|
+
error: error instanceof Error ? error.message : String(error)
|
|
412
|
+
});
|
|
413
|
+
serializedSpan = {};
|
|
414
|
+
}
|
|
415
|
+
if (!serializedSpan.span_data) {
|
|
416
|
+
serializedSpan.span_data = {};
|
|
417
|
+
}
|
|
418
|
+
return [serializedSpan, errors];
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Extract and add input/response to serialized span, updating errors list.
|
|
422
|
+
*/
|
|
423
|
+
extractSpanInputResponse(span, serializedSpan, errors) {
|
|
424
|
+
const spanData = serializedSpan.span_data;
|
|
425
|
+
try {
|
|
426
|
+
spanData.input = span.spanData?._input || [];
|
|
427
|
+
} catch (error) {
|
|
428
|
+
errors.push({
|
|
429
|
+
step: "access_input",
|
|
430
|
+
error: error instanceof Error ? error.message : String(error)
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
try {
|
|
434
|
+
spanData.response = span.spanData?._response || null;
|
|
435
|
+
} catch (error) {
|
|
436
|
+
errors.push({
|
|
437
|
+
step: "access_response",
|
|
438
|
+
error: error instanceof Error ? error.message : String(error)
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* If the span's trace is mapped to a withSpan trace, rewrite trace_id and parent_id.
|
|
444
|
+
*/
|
|
445
|
+
applySpanOverrides(serializedSpan, traceId) {
|
|
446
|
+
const mapping = this.activeSpanMappings[traceId];
|
|
447
|
+
if (mapping) {
|
|
448
|
+
serializedSpan.trace_id = mapping.traceId;
|
|
449
|
+
if (!serializedSpan.parent_id) {
|
|
450
|
+
serializedSpan.parent_id = mapping.spanId;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Build span payload for the external spans API.
|
|
456
|
+
*/
|
|
457
|
+
buildSpanPayload(serializedSpan, errors) {
|
|
458
|
+
const payload = {
|
|
459
|
+
type: "openai",
|
|
460
|
+
source: "typescript-sdk-openai-tracing",
|
|
461
|
+
sourceTraceId: serializedSpan.trace_id ?? "unknown",
|
|
462
|
+
rawSpan: serializedSpan
|
|
463
|
+
};
|
|
464
|
+
if (errors.length > 0) {
|
|
465
|
+
payload.errors = errors;
|
|
466
|
+
}
|
|
467
|
+
return payload;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Send span to Bitfab API (fire-and-forget).
|
|
471
|
+
* If the span belongs to a trace mapped to a withSpan trace, the trace_id
|
|
472
|
+
* and parent_id are rewritten to link the span into the withSpan tree.
|
|
473
|
+
*/
|
|
474
|
+
sendSpan(span) {
|
|
475
|
+
const errors = [];
|
|
476
|
+
const [serializedSpan, exportErrors] = this.exportSpan(span);
|
|
477
|
+
errors.push(...exportErrors);
|
|
478
|
+
this.extractSpanInputResponse(span, serializedSpan, errors);
|
|
479
|
+
this.applySpanOverrides(serializedSpan, span.traceId ?? "");
|
|
480
|
+
const payload = this.buildSpanPayload(serializedSpan, errors);
|
|
481
|
+
this.httpClient.sendExternalSpan(payload);
|
|
482
|
+
}
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
// src/client.ts
|
|
486
|
+
var activeTraceStates = /* @__PURE__ */ new Map();
|
|
487
|
+
var pendingSpanPromises = /* @__PURE__ */ new Map();
|
|
488
|
+
var asyncLocalStorage = null;
|
|
489
|
+
var asyncLocalStorageReady = asyncStorageReady.then(() => {
|
|
490
|
+
asyncLocalStorage = createAsyncLocalStorage();
|
|
491
|
+
});
|
|
492
|
+
var browserSpanStack = [];
|
|
493
|
+
function getSpanStack() {
|
|
494
|
+
if (asyncLocalStorage) {
|
|
495
|
+
return asyncLocalStorage.getStore() ?? [];
|
|
496
|
+
}
|
|
497
|
+
return browserSpanStack;
|
|
498
|
+
}
|
|
499
|
+
function runWithSpanStack(stack, fn) {
|
|
500
|
+
if (asyncLocalStorage) {
|
|
501
|
+
return asyncLocalStorage.run(stack, fn);
|
|
502
|
+
}
|
|
503
|
+
const previousStack = browserSpanStack;
|
|
504
|
+
browserSpanStack = stack;
|
|
505
|
+
try {
|
|
506
|
+
const result = fn();
|
|
507
|
+
if (result instanceof Promise) {
|
|
508
|
+
return result.finally(() => {
|
|
509
|
+
browserSpanStack = previousStack;
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
browserSpanStack = previousStack;
|
|
513
|
+
return result;
|
|
514
|
+
} catch (error) {
|
|
515
|
+
browserSpanStack = previousStack;
|
|
516
|
+
throw error;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
var cachedCollectorClass;
|
|
520
|
+
async function loadCollectorClass() {
|
|
521
|
+
if (cachedCollectorClass !== void 0) {
|
|
522
|
+
return cachedCollectorClass;
|
|
523
|
+
}
|
|
524
|
+
try {
|
|
525
|
+
const baml = await import("@boundaryml/baml");
|
|
526
|
+
cachedCollectorClass = baml.Collector;
|
|
527
|
+
return cachedCollectorClass;
|
|
528
|
+
} catch {
|
|
529
|
+
cachedCollectorClass = null;
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
function extractPromptFromCollector(collector) {
|
|
534
|
+
try {
|
|
535
|
+
const c = collector;
|
|
536
|
+
const calls = c?.last?.calls ?? [];
|
|
537
|
+
const selectedCall = calls.find((call) => call.selected) ?? calls[0];
|
|
538
|
+
if (!selectedCall?.httpRequest?.body) {
|
|
539
|
+
return null;
|
|
540
|
+
}
|
|
541
|
+
const body = selectedCall.httpRequest.body.json();
|
|
542
|
+
if (!body || typeof body !== "object") {
|
|
543
|
+
return null;
|
|
544
|
+
}
|
|
545
|
+
const messages = body.messages;
|
|
546
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
const rendered = messages.filter(
|
|
550
|
+
(msg) => typeof msg === "object" && msg !== null && "role" in msg && typeof msg.role === "string"
|
|
551
|
+
).map((msg) => ({
|
|
552
|
+
role: msg.role,
|
|
553
|
+
content: typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content)
|
|
554
|
+
}));
|
|
555
|
+
if (rendered.length > 0) {
|
|
556
|
+
return JSON.stringify(rendered);
|
|
557
|
+
}
|
|
558
|
+
return null;
|
|
559
|
+
} catch {
|
|
560
|
+
return null;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
function extractContextFromCollector(collector) {
|
|
564
|
+
try {
|
|
565
|
+
const c = collector;
|
|
566
|
+
const calls = c?.last?.calls ?? [];
|
|
567
|
+
const selectedCall = calls.find((call) => call.selected) ?? calls[0];
|
|
568
|
+
const usage = c?.usage;
|
|
569
|
+
const context = {};
|
|
570
|
+
if (selectedCall?.provider) {
|
|
571
|
+
context.provider = selectedCall.provider;
|
|
572
|
+
}
|
|
573
|
+
const body = selectedCall?.httpRequest?.body?.json();
|
|
574
|
+
if (body && typeof body === "object" && typeof body.model === "string") {
|
|
575
|
+
context.model = body.model;
|
|
576
|
+
} else {
|
|
577
|
+
const url = selectedCall?.httpRequest?.url;
|
|
578
|
+
if (url) {
|
|
579
|
+
const match = url.match(/\/models\/([^/:]+)/);
|
|
580
|
+
if (match?.[1]) {
|
|
581
|
+
context.model = match[1];
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
const inputTokens = usage?.inputTokens ?? selectedCall?.usage?.inputTokens ?? null;
|
|
586
|
+
const outputTokens = usage?.outputTokens ?? selectedCall?.usage?.outputTokens ?? null;
|
|
587
|
+
if (inputTokens !== null) {
|
|
588
|
+
context.inputTokens = inputTokens;
|
|
589
|
+
}
|
|
590
|
+
if (outputTokens !== null) {
|
|
591
|
+
context.outputTokens = outputTokens;
|
|
592
|
+
}
|
|
593
|
+
const durationMs = c?.last?.timing?.durationMs ?? null;
|
|
594
|
+
if (durationMs !== null) {
|
|
595
|
+
context.durationMs = durationMs;
|
|
596
|
+
}
|
|
597
|
+
return Object.keys(context).length > 0 ? context : null;
|
|
598
|
+
} catch {
|
|
599
|
+
return null;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
var noOpSpan = {
|
|
603
|
+
traceId: "",
|
|
604
|
+
addContext() {
|
|
605
|
+
},
|
|
606
|
+
setPrompt() {
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
var noOpTrace = {
|
|
610
|
+
setSessionId() {
|
|
611
|
+
},
|
|
612
|
+
setMetadata() {
|
|
613
|
+
},
|
|
614
|
+
addContext() {
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
function getCurrentSpan() {
|
|
618
|
+
const stack = getSpanStack();
|
|
619
|
+
const current = stack[stack.length - 1];
|
|
620
|
+
if (!current) {
|
|
621
|
+
return noOpSpan;
|
|
622
|
+
}
|
|
623
|
+
return {
|
|
624
|
+
traceId: current.traceId,
|
|
625
|
+
addContext(context) {
|
|
626
|
+
try {
|
|
627
|
+
if (typeof context !== "object" || context === null) {
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
current.contexts.push(context);
|
|
631
|
+
} catch {
|
|
632
|
+
}
|
|
633
|
+
},
|
|
634
|
+
setPrompt(prompt) {
|
|
635
|
+
try {
|
|
636
|
+
if (typeof prompt !== "string") {
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
current.prompt = prompt;
|
|
640
|
+
} catch {
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
}
|
|
645
|
+
function getCurrentTrace() {
|
|
646
|
+
const stack = getSpanStack();
|
|
647
|
+
const current = stack[stack.length - 1];
|
|
648
|
+
if (!current) {
|
|
649
|
+
return noOpTrace;
|
|
650
|
+
}
|
|
651
|
+
const traceId = current.traceId;
|
|
652
|
+
const getOrCreateTraceState = () => {
|
|
653
|
+
let traceState = activeTraceStates.get(traceId);
|
|
654
|
+
if (!traceState) {
|
|
655
|
+
traceState = {
|
|
656
|
+
traceId,
|
|
657
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
658
|
+
contexts: []
|
|
659
|
+
};
|
|
660
|
+
activeTraceStates.set(traceId, traceState);
|
|
661
|
+
}
|
|
662
|
+
return traceState;
|
|
663
|
+
};
|
|
664
|
+
return {
|
|
665
|
+
setSessionId(sessionId) {
|
|
666
|
+
try {
|
|
667
|
+
const traceState = getOrCreateTraceState();
|
|
668
|
+
traceState.sessionId = sessionId;
|
|
669
|
+
} catch {
|
|
670
|
+
}
|
|
671
|
+
},
|
|
672
|
+
setMetadata(metadata) {
|
|
673
|
+
try {
|
|
674
|
+
if (typeof metadata !== "object" || metadata === null) {
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
const traceState = getOrCreateTraceState();
|
|
678
|
+
traceState.metadata = { ...traceState.metadata, ...metadata };
|
|
679
|
+
} catch {
|
|
680
|
+
}
|
|
681
|
+
},
|
|
682
|
+
addContext(context) {
|
|
683
|
+
try {
|
|
684
|
+
if (typeof context !== "object" || context === null) {
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const traceState = getOrCreateTraceState();
|
|
688
|
+
traceState.contexts.push(context);
|
|
689
|
+
} catch {
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
var Bitfab = class {
|
|
695
|
+
/**
|
|
696
|
+
* Initialize the Bitfab client.
|
|
697
|
+
*
|
|
698
|
+
* @param config - Configuration options for the client
|
|
699
|
+
*/
|
|
700
|
+
constructor(config) {
|
|
701
|
+
this.apiKey = config.apiKey;
|
|
702
|
+
this.serviceUrl = config.serviceUrl ?? DEFAULT_SERVICE_URL;
|
|
703
|
+
this.timeout = config.timeout ?? 12e4;
|
|
704
|
+
this.envVars = config.envVars ?? {};
|
|
705
|
+
const enabled = config.enabled ?? true;
|
|
706
|
+
if (enabled && (!config.apiKey || config.apiKey.trim() === "")) {
|
|
707
|
+
console.warn(
|
|
708
|
+
"Bitfab: apiKey is empty \u2014 tracing is disabled. Provide a valid API key to enable tracing."
|
|
709
|
+
);
|
|
710
|
+
this.enabled = false;
|
|
711
|
+
} else {
|
|
712
|
+
this.enabled = enabled;
|
|
713
|
+
}
|
|
714
|
+
this.bamlClient = config.bamlClient ?? null;
|
|
715
|
+
this.httpClient = new HttpClient({
|
|
716
|
+
apiKey: this.apiKey,
|
|
717
|
+
serviceUrl: this.serviceUrl,
|
|
718
|
+
timeout: this.timeout
|
|
719
|
+
});
|
|
720
|
+
}
|
|
721
|
+
/**
|
|
722
|
+
* Fetch the function with its current version and BAML prompt from the server.
|
|
723
|
+
*
|
|
724
|
+
* @param methodName - The name of the method to fetch
|
|
725
|
+
* @returns The function with current version, BAML prompt, and provider definitions
|
|
726
|
+
* @throws {BitfabError} If the function is not found or an error occurs
|
|
727
|
+
*/
|
|
728
|
+
async fetchFunctionVersion(methodName) {
|
|
729
|
+
const result = await this.httpClient.lookupFunction(methodName);
|
|
730
|
+
if (result.id === null) {
|
|
731
|
+
throw new BitfabError(
|
|
732
|
+
`Function "${methodName}" not found. Create it at: ${this.serviceUrl}/functions`,
|
|
733
|
+
"/functions"
|
|
734
|
+
);
|
|
735
|
+
}
|
|
736
|
+
if (!result.prompt) {
|
|
737
|
+
throw new BitfabError(
|
|
738
|
+
`Function "${methodName}" has no prompt configured. Add one at: ${this.serviceUrl}/functions/${result.id}`,
|
|
739
|
+
`/functions/${result.id}`
|
|
740
|
+
);
|
|
741
|
+
}
|
|
742
|
+
return result;
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Call a method with the given named arguments via BAML execution.
|
|
746
|
+
*
|
|
747
|
+
* @param methodName - The name of the method to call
|
|
748
|
+
* @param inputs - Named arguments to pass to the method
|
|
749
|
+
* @returns The result of the BAML function execution
|
|
750
|
+
* @throws {BitfabError} If service_url is not set, or if an error occurs
|
|
751
|
+
*/
|
|
752
|
+
async call(methodName, inputs = {}) {
|
|
753
|
+
try {
|
|
754
|
+
const functionVersion = await this.fetchFunctionVersion(methodName);
|
|
755
|
+
const executionResult = await runFunctionWithBaml(
|
|
756
|
+
functionVersion.prompt,
|
|
757
|
+
inputs,
|
|
758
|
+
functionVersion.providers,
|
|
759
|
+
this.envVars
|
|
760
|
+
);
|
|
761
|
+
const resultStr = typeof executionResult.result === "string" ? executionResult.result : JSON.stringify(executionResult.result);
|
|
762
|
+
this.httpClient.sendInternalTrace(functionVersion.id, {
|
|
763
|
+
result: resultStr,
|
|
764
|
+
source: "typescript-sdk",
|
|
765
|
+
...Object.keys(inputs).length > 0 && { inputs },
|
|
766
|
+
...executionResult.rawCollector != null && {
|
|
767
|
+
rawCollector: executionResult.rawCollector
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
return executionResult.result;
|
|
771
|
+
} catch (error) {
|
|
772
|
+
if (error instanceof BitfabError) {
|
|
773
|
+
throw error;
|
|
774
|
+
}
|
|
775
|
+
if (error instanceof Error) {
|
|
776
|
+
throw new BitfabError(error.message);
|
|
777
|
+
}
|
|
778
|
+
throw new BitfabError("Unknown error occurred during local execution");
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Get a tracing processor for OpenAI Agents SDK integration.
|
|
783
|
+
*
|
|
784
|
+
* This processor automatically captures traces and spans from the OpenAI Agents SDK
|
|
785
|
+
* and sends them to Bitfab for monitoring and analysis.
|
|
786
|
+
*
|
|
787
|
+
* Example usage:
|
|
788
|
+
* ```typescript
|
|
789
|
+
* import { addTraceProcessor } from '@openai/agents';
|
|
790
|
+
*
|
|
791
|
+
* const client = new Bitfab({ apiKey: 'your-api-key' });
|
|
792
|
+
* const processor = client.getOpenAiTracingProcessor();
|
|
793
|
+
* addTraceProcessor(processor);
|
|
794
|
+
* ```
|
|
795
|
+
*
|
|
796
|
+
* @returns A BitfabOpenAITracingProcessor instance configured for this client
|
|
797
|
+
*/
|
|
798
|
+
getOpenAiTracingProcessor() {
|
|
799
|
+
return new BitfabOpenAITracingProcessor({
|
|
800
|
+
apiKey: this.apiKey,
|
|
801
|
+
serviceUrl: this.serviceUrl,
|
|
802
|
+
getActiveSpanContext: () => {
|
|
803
|
+
const stack = getSpanStack();
|
|
804
|
+
return stack[stack.length - 1] ?? null;
|
|
805
|
+
}
|
|
806
|
+
});
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Wrap a BAML client method to automatically capture prompt and LLM metadata.
|
|
810
|
+
*
|
|
811
|
+
* Creates a BAML Collector, calls the method through a tracked client,
|
|
812
|
+
* then extracts rendered messages and token usage — calling setPrompt()
|
|
813
|
+
* and addContext() on the current span automatically.
|
|
814
|
+
*
|
|
815
|
+
* The BAML client can be provided in the constructor or passed explicitly:
|
|
816
|
+
*
|
|
817
|
+
* ```typescript
|
|
818
|
+
* // Option 1: bamlClient in constructor (use wrapBAML with just the method)
|
|
819
|
+
* const client = new Bitfab({ apiKey: 'your-api-key', bamlClient: b });
|
|
820
|
+
* const traced = client.withSpan('classify', { type: 'llm' },
|
|
821
|
+
* client.wrapBAML(b.ClassifyText)
|
|
822
|
+
* );
|
|
823
|
+
*
|
|
824
|
+
* // Option 2: pass bamlClient at call site
|
|
825
|
+
* const client = new Bitfab({ apiKey: 'your-api-key' });
|
|
826
|
+
* const traced = client.withSpan('classify', { type: 'llm' },
|
|
827
|
+
* client.wrapBAML(b, b.ClassifyText)
|
|
828
|
+
* );
|
|
829
|
+
* ```
|
|
830
|
+
*
|
|
831
|
+
* @param methodOrClient - Either a BAML method (uses constructor bamlClient) or the BAML client instance
|
|
832
|
+
* @param maybeMethodOrOptions - The BAML method when the first argument is a client, or WrapBAMLOptions when the first argument is the method
|
|
833
|
+
* @param maybeOptions - WrapBAMLOptions when using the two-argument (client, method) form
|
|
834
|
+
* @returns An async function with the same signature that instruments the BAML call
|
|
835
|
+
*/
|
|
836
|
+
wrapBAML(methodOrClient, maybeMethodOrOptions, maybeOptions) {
|
|
837
|
+
let bamlClient;
|
|
838
|
+
let method;
|
|
839
|
+
let options;
|
|
840
|
+
if (typeof maybeMethodOrOptions === "function") {
|
|
841
|
+
bamlClient = methodOrClient;
|
|
842
|
+
method = maybeMethodOrOptions;
|
|
843
|
+
options = maybeOptions;
|
|
844
|
+
} else {
|
|
845
|
+
bamlClient = this.bamlClient;
|
|
846
|
+
method = methodOrClient;
|
|
847
|
+
options = maybeMethodOrOptions;
|
|
848
|
+
if (!bamlClient) {
|
|
849
|
+
throw new BitfabError(
|
|
850
|
+
"bamlClient is required for wrapBAML. Pass it in the constructor or as the first argument."
|
|
851
|
+
);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
const methodName = method.name;
|
|
855
|
+
if (!methodName) {
|
|
856
|
+
throw new BitfabError(
|
|
857
|
+
"wrapBAML requires a named function (e.g., b.ClassifyText)."
|
|
858
|
+
);
|
|
859
|
+
}
|
|
860
|
+
loadCollectorClass();
|
|
861
|
+
const wrappedFn = async (...args) => {
|
|
862
|
+
const CollectorClass = await loadCollectorClass();
|
|
863
|
+
if (!CollectorClass) {
|
|
864
|
+
wrappedFn.collector = null;
|
|
865
|
+
return await bamlClient[methodName](...args);
|
|
866
|
+
}
|
|
867
|
+
const collector = new CollectorClass("bitfab-baml-tracing");
|
|
868
|
+
const trackedClient = bamlClient.withOptions({ collector });
|
|
869
|
+
const trackedMethod = trackedClient[methodName];
|
|
870
|
+
const result = await trackedMethod.bind(
|
|
871
|
+
trackedClient
|
|
872
|
+
)(...args);
|
|
873
|
+
wrappedFn.collector = collector;
|
|
874
|
+
try {
|
|
875
|
+
const prompt = extractPromptFromCollector(collector);
|
|
876
|
+
if (prompt) {
|
|
877
|
+
getCurrentSpan().setPrompt(prompt);
|
|
878
|
+
}
|
|
879
|
+
const metadata = extractContextFromCollector(collector);
|
|
880
|
+
if (metadata) {
|
|
881
|
+
getCurrentSpan().addContext(metadata);
|
|
882
|
+
}
|
|
883
|
+
} catch {
|
|
884
|
+
}
|
|
885
|
+
try {
|
|
886
|
+
options?.onCollector?.(collector);
|
|
887
|
+
} catch {
|
|
888
|
+
}
|
|
889
|
+
return result;
|
|
890
|
+
};
|
|
891
|
+
wrappedFn.collector = null;
|
|
892
|
+
return wrappedFn;
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Wrap a function to automatically create a span for its inputs and outputs.
|
|
896
|
+
*
|
|
897
|
+
* The wrapped function behaves identically to the original, but sends
|
|
898
|
+
* span data to Bitfab in the background after each call.
|
|
899
|
+
*
|
|
900
|
+
* Example usage:
|
|
901
|
+
* ```typescript
|
|
902
|
+
* const client = new Bitfab({ apiKey: 'your-api-key' });
|
|
903
|
+
*
|
|
904
|
+
* async function processOrder(orderId: string, items: string[]): Promise<{ total: number }> {
|
|
905
|
+
* // ... process order
|
|
906
|
+
* return { total: 100 };
|
|
907
|
+
* }
|
|
908
|
+
*
|
|
909
|
+
* // Basic usage (defaults to "custom" span type)
|
|
910
|
+
* const tracedProcessOrder = client.withSpan('order-processing', processOrder);
|
|
911
|
+
*
|
|
912
|
+
* // With explicit span type
|
|
913
|
+
* const tracedProcessOrder = client.withSpan('order-processing', { type: 'function' }, processOrder);
|
|
914
|
+
*
|
|
915
|
+
* // Call the wrapped function normally
|
|
916
|
+
* const result = await tracedProcessOrder('order-123', ['item-1', 'item-2']);
|
|
917
|
+
* // Span is automatically sent to Bitfab
|
|
918
|
+
* ```
|
|
919
|
+
*
|
|
920
|
+
* @param traceFunctionKey - A string identifier for grouping spans (e.g., 'order-processing', 'user-auth')
|
|
921
|
+
* @param optionsOrFn - Either SpanOptions or the function to wrap
|
|
922
|
+
* @param maybeFn - The function to wrap if options were provided
|
|
923
|
+
* @returns A wrapped function with the same signature that creates spans for inputs and outputs
|
|
924
|
+
*/
|
|
925
|
+
withSpan(traceFunctionKey, optionsOrFn, maybeFn) {
|
|
926
|
+
if (!this.enabled) {
|
|
927
|
+
const fn2 = typeof optionsOrFn === "function" ? optionsOrFn : maybeFn;
|
|
928
|
+
return fn2;
|
|
929
|
+
}
|
|
930
|
+
const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
|
|
931
|
+
const fn = typeof optionsOrFn === "function" ? optionsOrFn : maybeFn;
|
|
932
|
+
const self = this;
|
|
933
|
+
const wrappedFn = function(...args) {
|
|
934
|
+
if (!asyncLocalStorage && !isAsyncStorageInitDone()) {
|
|
935
|
+
return asyncLocalStorageReady.then(
|
|
936
|
+
() => wrappedFn.apply(this, args)
|
|
937
|
+
);
|
|
938
|
+
}
|
|
939
|
+
const currentStack = getSpanStack();
|
|
940
|
+
const parentContext = currentStack[currentStack.length - 1];
|
|
941
|
+
const traceId = parentContext?.traceId ?? crypto.randomUUID();
|
|
942
|
+
const spanId = crypto.randomUUID();
|
|
943
|
+
const parentSpanId = parentContext?.spanId ?? null;
|
|
944
|
+
const isRootSpan = parentSpanId === null;
|
|
945
|
+
const newContext = { traceId, spanId, contexts: [] };
|
|
946
|
+
const newStack = [...currentStack, newContext];
|
|
947
|
+
const inputs = args;
|
|
948
|
+
const startedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
949
|
+
if (isRootSpan && !activeTraceStates.has(traceId)) {
|
|
950
|
+
activeTraceStates.set(traceId, {
|
|
951
|
+
traceId,
|
|
952
|
+
startedAt,
|
|
953
|
+
contexts: []
|
|
954
|
+
});
|
|
955
|
+
pendingSpanPromises.set(traceId, []);
|
|
956
|
+
}
|
|
957
|
+
const functionName = fn.name !== "" ? fn.name : void 0;
|
|
958
|
+
const baseSpanParams = {
|
|
959
|
+
traceFunctionKey,
|
|
960
|
+
functionName,
|
|
961
|
+
spanName: options.name ?? functionName ?? traceFunctionKey,
|
|
962
|
+
traceId,
|
|
963
|
+
spanId,
|
|
964
|
+
parentSpanId,
|
|
965
|
+
inputs,
|
|
966
|
+
startedAt,
|
|
967
|
+
spanType: options.type ?? "custom"
|
|
968
|
+
};
|
|
969
|
+
const sendSpan = async (params) => {
|
|
970
|
+
try {
|
|
971
|
+
const endedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
972
|
+
const replayCtx = getReplayContext();
|
|
973
|
+
const spanPromise = self.sendWrapperSpan({
|
|
974
|
+
...baseSpanParams,
|
|
975
|
+
...params,
|
|
976
|
+
contexts: newContext.contexts,
|
|
977
|
+
prompt: newContext.prompt,
|
|
978
|
+
endedAt,
|
|
979
|
+
...replayCtx?.testRunId && { testRunId: replayCtx.testRunId },
|
|
980
|
+
...replayCtx?.inputSourceSpanId && {
|
|
981
|
+
inputSourceSpanId: replayCtx.inputSourceSpanId
|
|
982
|
+
}
|
|
983
|
+
});
|
|
984
|
+
if (isRootSpan) {
|
|
985
|
+
const pending = pendingSpanPromises.get(traceId) ?? [];
|
|
986
|
+
pending.push(spanPromise);
|
|
987
|
+
await Promise.race([
|
|
988
|
+
Promise.allSettled(pending),
|
|
989
|
+
new Promise((resolve) => setTimeout(resolve, 5e3))
|
|
990
|
+
]);
|
|
991
|
+
pendingSpanPromises.delete(traceId);
|
|
992
|
+
const traceState = activeTraceStates.get(traceId);
|
|
993
|
+
self.sendTraceCompletion({
|
|
994
|
+
traceFunctionKey,
|
|
995
|
+
traceId,
|
|
996
|
+
startedAt: traceState?.startedAt ?? startedAt,
|
|
997
|
+
endedAt,
|
|
998
|
+
sessionId: traceState?.sessionId,
|
|
999
|
+
metadata: traceState?.metadata,
|
|
1000
|
+
contexts: traceState?.contexts ?? []
|
|
1001
|
+
});
|
|
1002
|
+
activeTraceStates.delete(traceId);
|
|
1003
|
+
} else {
|
|
1004
|
+
const pending = pendingSpanPromises.get(traceId);
|
|
1005
|
+
if (pending) {
|
|
1006
|
+
pending.push(spanPromise);
|
|
1007
|
+
} else {
|
|
1008
|
+
pendingSpanPromises.set(traceId, [spanPromise]);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
} catch {
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
const executeWithContext = () => {
|
|
1015
|
+
const result = fn(...args);
|
|
1016
|
+
if (result instanceof Promise) {
|
|
1017
|
+
return result.then((resolvedResult) => {
|
|
1018
|
+
void sendSpan({ result: resolvedResult });
|
|
1019
|
+
return resolvedResult;
|
|
1020
|
+
}).catch((error) => {
|
|
1021
|
+
void sendSpan({
|
|
1022
|
+
result: void 0,
|
|
1023
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1024
|
+
});
|
|
1025
|
+
throw error;
|
|
1026
|
+
});
|
|
1027
|
+
}
|
|
1028
|
+
void sendSpan({ result });
|
|
1029
|
+
return result;
|
|
1030
|
+
};
|
|
1031
|
+
return runWithSpanStack(newStack, executeWithContext);
|
|
1032
|
+
};
|
|
1033
|
+
return wrappedFn;
|
|
1034
|
+
}
|
|
1035
|
+
/**
|
|
1036
|
+
* Get a function wrapper for a specific trace function key.
|
|
1037
|
+
*
|
|
1038
|
+
* This provides a fluent API alternative to calling withSpan directly,
|
|
1039
|
+
* allowing you to bind the traceFunctionKey once and wrap multiple functions.
|
|
1040
|
+
*
|
|
1041
|
+
* Example usage:
|
|
1042
|
+
* ```typescript
|
|
1043
|
+
* const client = new Bitfab({ apiKey: 'your-api-key' });
|
|
1044
|
+
*
|
|
1045
|
+
* const orderFunc = client.getFunction('order-processing');
|
|
1046
|
+
* const tracedProcessOrder = orderFunc.withSpan(processOrder);
|
|
1047
|
+
* const tracedValidateOrder = orderFunc.withSpan(validateOrder);
|
|
1048
|
+
* ```
|
|
1049
|
+
*
|
|
1050
|
+
* @param traceFunctionKey - A string identifier for grouping spans
|
|
1051
|
+
* @returns A BitfabFunction instance for wrapping functions
|
|
1052
|
+
*/
|
|
1053
|
+
getFunction(traceFunctionKey) {
|
|
1054
|
+
return new BitfabFunction(this, traceFunctionKey);
|
|
1055
|
+
}
|
|
1056
|
+
/**
|
|
1057
|
+
* Send trace completion when a root span ends.
|
|
1058
|
+
* Internal method to record trace completion with end time.
|
|
1059
|
+
* Fire-and-forget - sends to externalTraces endpoint via httpClient.
|
|
1060
|
+
*/
|
|
1061
|
+
sendTraceCompletion(params) {
|
|
1062
|
+
const rawTrace = {
|
|
1063
|
+
id: params.traceId,
|
|
1064
|
+
started_at: params.startedAt,
|
|
1065
|
+
ended_at: params.endedAt,
|
|
1066
|
+
workflow_name: params.traceFunctionKey
|
|
1067
|
+
};
|
|
1068
|
+
if (params.metadata && Object.keys(params.metadata).length > 0) {
|
|
1069
|
+
rawTrace.metadata = params.metadata;
|
|
1070
|
+
}
|
|
1071
|
+
if (params.contexts && params.contexts.length > 0) {
|
|
1072
|
+
rawTrace.contexts = params.contexts;
|
|
1073
|
+
}
|
|
1074
|
+
this.httpClient.sendExternalTrace({
|
|
1075
|
+
type: "sdk-function",
|
|
1076
|
+
source: "typescript-sdk-function",
|
|
1077
|
+
traceFunctionKey: params.traceFunctionKey,
|
|
1078
|
+
externalTrace: rawTrace,
|
|
1079
|
+
completed: true,
|
|
1080
|
+
...params.sessionId && { sessionId: params.sessionId }
|
|
1081
|
+
});
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
* Send a wrapper span from function execution.
|
|
1085
|
+
* Internal method to record spans when using withSpan.
|
|
1086
|
+
* Fire-and-forget - sends to externalSpans endpoint via httpClient.
|
|
1087
|
+
*/
|
|
1088
|
+
sendWrapperSpan(params) {
|
|
1089
|
+
const serializedInputs = serializeValue(params.inputs);
|
|
1090
|
+
const serializedResult = serializeValue(params.result);
|
|
1091
|
+
const externalSpan = {
|
|
1092
|
+
id: params.spanId,
|
|
1093
|
+
trace_id: params.traceId,
|
|
1094
|
+
started_at: params.startedAt,
|
|
1095
|
+
ended_at: params.endedAt,
|
|
1096
|
+
span_data: {
|
|
1097
|
+
name: params.spanName,
|
|
1098
|
+
type: params.spanType,
|
|
1099
|
+
input: serializedInputs.json,
|
|
1100
|
+
output: serializedResult.json,
|
|
1101
|
+
// Include superjson meta for type preservation
|
|
1102
|
+
...serializedInputs.meta !== void 0 && {
|
|
1103
|
+
input_meta: serializedInputs.meta
|
|
1104
|
+
},
|
|
1105
|
+
...serializedResult.meta !== void 0 && {
|
|
1106
|
+
output_meta: serializedResult.meta
|
|
1107
|
+
},
|
|
1108
|
+
...params.functionName !== void 0 && {
|
|
1109
|
+
function_name: params.functionName
|
|
1110
|
+
},
|
|
1111
|
+
...params.error !== void 0 && { error: params.error },
|
|
1112
|
+
...params.contexts && params.contexts.length > 0 && {
|
|
1113
|
+
contexts: params.contexts
|
|
1114
|
+
},
|
|
1115
|
+
...params.prompt !== void 0 && { prompt: params.prompt }
|
|
1116
|
+
}
|
|
1117
|
+
};
|
|
1118
|
+
if (params.parentSpanId) {
|
|
1119
|
+
externalSpan.parent_id = params.parentSpanId;
|
|
1120
|
+
}
|
|
1121
|
+
if (params.inputSourceSpanId) {
|
|
1122
|
+
externalSpan.input_source_span_id = params.inputSourceSpanId;
|
|
1123
|
+
}
|
|
1124
|
+
return this.httpClient.sendExternalSpan({
|
|
1125
|
+
type: "sdk-function",
|
|
1126
|
+
source: "typescript-sdk-function",
|
|
1127
|
+
sourceTraceId: params.traceId,
|
|
1128
|
+
traceFunctionKey: params.traceFunctionKey,
|
|
1129
|
+
rawSpan: externalSpan,
|
|
1130
|
+
...params.testRunId && { testRunId: params.testRunId }
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Replay historical traces through a function and create a test run.
|
|
1135
|
+
*
|
|
1136
|
+
* Fetches the last N traces for the given trace function key, re-runs each
|
|
1137
|
+
* through the provided function, and returns comparison data.
|
|
1138
|
+
*
|
|
1139
|
+
* The function must have been wrapped with `withSpan` — replay injects
|
|
1140
|
+
* `testRunId` via async context so new spans are linked to the test run.
|
|
1141
|
+
*
|
|
1142
|
+
* @param traceFunctionKey - The trace function key to replay
|
|
1143
|
+
* @param fn - The function to replay (must be the return value of `withSpan`)
|
|
1144
|
+
* @param options - Optional replay options (limit, traceIds)
|
|
1145
|
+
* @returns ReplayResult with items, testRunId, and testRunUrl
|
|
1146
|
+
*/
|
|
1147
|
+
async replay(traceFunctionKey, fn, options) {
|
|
1148
|
+
const { replay: doReplay } = await import("./replay-DJ2HXTHQ.js");
|
|
1149
|
+
return doReplay(
|
|
1150
|
+
this.httpClient,
|
|
1151
|
+
this.serviceUrl,
|
|
1152
|
+
traceFunctionKey,
|
|
1153
|
+
fn,
|
|
1154
|
+
options
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
};
|
|
1158
|
+
var BitfabFunction = class {
|
|
1159
|
+
constructor(client, traceFunctionKey) {
|
|
1160
|
+
this.client = client;
|
|
1161
|
+
this.traceFunctionKey = traceFunctionKey;
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* Wrap a function to automatically create a span for its inputs and outputs.
|
|
1165
|
+
*
|
|
1166
|
+
* The wrapped function behaves identically to the original, but sends
|
|
1167
|
+
* span data to Bitfab in the background after each call.
|
|
1168
|
+
*
|
|
1169
|
+
* Example usage:
|
|
1170
|
+
* ```typescript
|
|
1171
|
+
* const orderFunc = client.getFunction('order-processing');
|
|
1172
|
+
*
|
|
1173
|
+
* // Basic usage (defaults to "custom" span type)
|
|
1174
|
+
* const tracedProcessOrder = orderFunc.withSpan(processOrder);
|
|
1175
|
+
*
|
|
1176
|
+
* // With explicit span type
|
|
1177
|
+
* const tracedProcessOrder = orderFunc.withSpan({ type: 'function' }, processOrder);
|
|
1178
|
+
* ```
|
|
1179
|
+
*
|
|
1180
|
+
* @param optionsOrFn - Either SpanOptions or the function to wrap
|
|
1181
|
+
* @param maybeFn - The function to wrap if options were provided
|
|
1182
|
+
* @returns A wrapped function with the same signature that creates spans
|
|
1183
|
+
*/
|
|
1184
|
+
withSpan(optionsOrFn, maybeFn) {
|
|
1185
|
+
const options = typeof optionsOrFn === "function" ? {} : optionsOrFn;
|
|
1186
|
+
const fn = typeof optionsOrFn === "function" ? optionsOrFn : maybeFn;
|
|
1187
|
+
return this.client.withSpan(this.traceFunctionKey, options, fn);
|
|
1188
|
+
}
|
|
1189
|
+
/**
|
|
1190
|
+
* Wrap a BAML client method to automatically capture prompt and LLM metadata.
|
|
1191
|
+
* Delegates to the parent client's wrapBAML method.
|
|
1192
|
+
*
|
|
1193
|
+
* @param methodOrClient - Either a BAML method (uses constructor bamlClient) or the BAML client instance
|
|
1194
|
+
* @param maybeMethodOrOptions - The BAML method when the first argument is a client, or WrapBAMLOptions when the first argument is the method
|
|
1195
|
+
* @param maybeOptions - WrapBAMLOptions when using the two-argument (client, method) form
|
|
1196
|
+
* @returns An async function with the same signature that instruments the BAML call
|
|
1197
|
+
*/
|
|
1198
|
+
wrapBAML(methodOrClient, maybeMethodOrOptions, maybeOptions) {
|
|
1199
|
+
return this.client.wrapBAML(
|
|
1200
|
+
methodOrClient,
|
|
1201
|
+
maybeMethodOrOptions,
|
|
1202
|
+
maybeOptions
|
|
1203
|
+
);
|
|
1204
|
+
}
|
|
1205
|
+
};
|
|
1206
|
+
|
|
1207
|
+
export {
|
|
1208
|
+
BitfabOpenAITracingProcessor,
|
|
1209
|
+
getCurrentSpan,
|
|
1210
|
+
getCurrentTrace,
|
|
1211
|
+
Bitfab,
|
|
1212
|
+
BitfabFunction
|
|
1213
|
+
};
|
|
1214
|
+
//# sourceMappingURL=chunk-WUJR72QY.js.map
|