@langfuse/tracing 4.0.0-beta.6 → 4.0.0-beta.8
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/index.cjs +325 -236
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1408 -427
- package/dist/index.d.ts +1408 -427
- package/dist/index.mjs +314 -227
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -38,43 +38,23 @@ function createTraceAttributes({
|
|
|
38
38
|
Object.entries(attributes).filter(([_, v]) => v != null)
|
|
39
39
|
);
|
|
40
40
|
}
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return Object.fromEntries(
|
|
59
|
-
Object.entries(attributes).filter(([_, v]) => v != null)
|
|
60
|
-
);
|
|
61
|
-
}
|
|
62
|
-
function createGenerationAttributes({
|
|
63
|
-
completionStartTime,
|
|
64
|
-
metadata,
|
|
65
|
-
level,
|
|
66
|
-
statusMessage,
|
|
67
|
-
version,
|
|
68
|
-
model,
|
|
69
|
-
modelParameters,
|
|
70
|
-
input,
|
|
71
|
-
output,
|
|
72
|
-
usageDetails,
|
|
73
|
-
costDetails,
|
|
74
|
-
prompt
|
|
75
|
-
}) {
|
|
76
|
-
const attributes = {
|
|
77
|
-
[LangfuseOtelSpanAttributes.OBSERVATION_TYPE]: "generation",
|
|
41
|
+
function createObservationAttributes(type, attributes) {
|
|
42
|
+
const {
|
|
43
|
+
metadata,
|
|
44
|
+
input,
|
|
45
|
+
output,
|
|
46
|
+
level,
|
|
47
|
+
statusMessage,
|
|
48
|
+
version,
|
|
49
|
+
completionStartTime,
|
|
50
|
+
model,
|
|
51
|
+
modelParameters,
|
|
52
|
+
usageDetails,
|
|
53
|
+
costDetails,
|
|
54
|
+
prompt
|
|
55
|
+
} = attributes;
|
|
56
|
+
let otelAttributes = {
|
|
57
|
+
[LangfuseOtelSpanAttributes.OBSERVATION_TYPE]: type,
|
|
78
58
|
[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL]: level,
|
|
79
59
|
[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]: statusMessage,
|
|
80
60
|
[LangfuseOtelSpanAttributes.VERSION]: version,
|
|
@@ -92,28 +72,7 @@ function createGenerationAttributes({
|
|
|
92
72
|
..._flattenAndSerializeMetadata(metadata, "observation")
|
|
93
73
|
};
|
|
94
74
|
return Object.fromEntries(
|
|
95
|
-
Object.entries(
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
function createEventAttributes({
|
|
99
|
-
metadata,
|
|
100
|
-
input,
|
|
101
|
-
output,
|
|
102
|
-
level,
|
|
103
|
-
statusMessage,
|
|
104
|
-
version
|
|
105
|
-
}) {
|
|
106
|
-
const attributes = {
|
|
107
|
-
[LangfuseOtelSpanAttributes.OBSERVATION_TYPE]: "event",
|
|
108
|
-
[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL]: level,
|
|
109
|
-
[LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]: statusMessage,
|
|
110
|
-
[LangfuseOtelSpanAttributes.VERSION]: version,
|
|
111
|
-
[LangfuseOtelSpanAttributes.OBSERVATION_INPUT]: _serialize(input),
|
|
112
|
-
[LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT]: _serialize(output),
|
|
113
|
-
..._flattenAndSerializeMetadata(metadata, "observation")
|
|
114
|
-
};
|
|
115
|
-
return Object.fromEntries(
|
|
116
|
-
Object.entries(attributes).filter(([_, v]) => v != null)
|
|
75
|
+
Object.entries(otelAttributes).filter(([_, v]) => v != null)
|
|
117
76
|
);
|
|
118
77
|
}
|
|
119
78
|
function _serialize(obj) {
|
|
@@ -204,11 +163,17 @@ function getLangfuseTracer() {
|
|
|
204
163
|
}
|
|
205
164
|
|
|
206
165
|
// src/spanWrapper.ts
|
|
207
|
-
var
|
|
166
|
+
var LangfuseBaseObservation = class {
|
|
208
167
|
constructor(params) {
|
|
209
168
|
this.otelSpan = params.otelSpan;
|
|
210
169
|
this.id = params.otelSpan.spanContext().spanId;
|
|
211
170
|
this.traceId = params.otelSpan.spanContext().traceId;
|
|
171
|
+
this.type = params.type;
|
|
172
|
+
if (params.attributes) {
|
|
173
|
+
this.otelSpan.setAttributes(
|
|
174
|
+
createObservationAttributes(params.type, params.attributes)
|
|
175
|
+
);
|
|
176
|
+
}
|
|
212
177
|
}
|
|
213
178
|
/** Gets the Langfuse OpenTelemetry tracer instance */
|
|
214
179
|
get tracer() {
|
|
@@ -222,6 +187,11 @@ var LangfuseSpanWrapper = class {
|
|
|
222
187
|
end(endTime) {
|
|
223
188
|
this.otelSpan.end(endTime);
|
|
224
189
|
}
|
|
190
|
+
updateOtelSpanAttributes(attributes) {
|
|
191
|
+
this.otelSpan.setAttributes(
|
|
192
|
+
createObservationAttributes(this.type, attributes)
|
|
193
|
+
);
|
|
194
|
+
}
|
|
225
195
|
/**
|
|
226
196
|
* Updates the parent trace with new attributes.
|
|
227
197
|
*
|
|
@@ -235,13 +205,18 @@ var LangfuseSpanWrapper = class {
|
|
|
235
205
|
this.otelSpan.setAttributes(createTraceAttributes(attributes));
|
|
236
206
|
return this;
|
|
237
207
|
}
|
|
208
|
+
startObservation(name, attributes, options) {
|
|
209
|
+
const { asType = "span" } = options || {};
|
|
210
|
+
return startObservation(name, attributes, {
|
|
211
|
+
asType,
|
|
212
|
+
// typecast necessary as ts cannot narrow the type correctly
|
|
213
|
+
parentSpanContext: this.otelSpan.spanContext()
|
|
214
|
+
});
|
|
215
|
+
}
|
|
238
216
|
};
|
|
239
|
-
var LangfuseSpan = class extends
|
|
217
|
+
var LangfuseSpan = class extends LangfuseBaseObservation {
|
|
240
218
|
constructor(params) {
|
|
241
|
-
super(params);
|
|
242
|
-
if (params.attributes) {
|
|
243
|
-
this.otelSpan.setAttributes(createSpanAttributes(params.attributes));
|
|
244
|
-
}
|
|
219
|
+
super({ ...params, type: "span" });
|
|
245
220
|
}
|
|
246
221
|
/**
|
|
247
222
|
* Updates this span with new attributes.
|
|
@@ -259,134 +234,178 @@ var LangfuseSpan = class extends LangfuseSpanWrapper {
|
|
|
259
234
|
* ```
|
|
260
235
|
*/
|
|
261
236
|
update(attributes) {
|
|
262
|
-
|
|
237
|
+
super.updateOtelSpanAttributes(attributes);
|
|
263
238
|
return this;
|
|
264
239
|
}
|
|
240
|
+
};
|
|
241
|
+
var LangfuseAgent = class extends LangfuseBaseObservation {
|
|
242
|
+
constructor(params) {
|
|
243
|
+
super({ ...params, type: "agent" });
|
|
244
|
+
}
|
|
265
245
|
/**
|
|
266
|
-
*
|
|
246
|
+
* Updates this agent observation with new attributes.
|
|
267
247
|
*
|
|
268
|
-
* @param
|
|
269
|
-
* @
|
|
270
|
-
* @returns The new child span
|
|
248
|
+
* @param attributes - Agent attributes to set
|
|
249
|
+
* @returns This agent for method chaining
|
|
271
250
|
*
|
|
272
251
|
* @example
|
|
273
252
|
* ```typescript
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
253
|
+
* agent.update({
|
|
254
|
+
* output: {
|
|
255
|
+
* taskCompleted: true,
|
|
256
|
+
* iterationsUsed: 5,
|
|
257
|
+
* toolsInvoked: ['web-search', 'calculator', 'summarizer'],
|
|
258
|
+
* finalResult: 'Research completed with high confidence'
|
|
259
|
+
* },
|
|
260
|
+
* metadata: {
|
|
261
|
+
* efficiency: 0.85,
|
|
262
|
+
* qualityScore: 0.92,
|
|
263
|
+
* resourcesConsumed: { tokens: 15000, apiCalls: 12 }
|
|
264
|
+
* }
|
|
277
265
|
* });
|
|
278
266
|
* ```
|
|
279
267
|
*/
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
268
|
+
update(attributes) {
|
|
269
|
+
super.updateOtelSpanAttributes(attributes);
|
|
270
|
+
return this;
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
var LangfuseTool = class extends LangfuseBaseObservation {
|
|
274
|
+
constructor(params) {
|
|
275
|
+
super({ ...params, type: "tool" });
|
|
284
276
|
}
|
|
285
277
|
/**
|
|
286
|
-
*
|
|
278
|
+
* Updates this tool observation with new attributes.
|
|
287
279
|
*
|
|
288
|
-
* @param
|
|
289
|
-
* @
|
|
290
|
-
* @returns The new child generation
|
|
280
|
+
* @param attributes - Tool attributes to set
|
|
281
|
+
* @returns This tool for method chaining
|
|
291
282
|
*
|
|
292
283
|
* @example
|
|
293
284
|
* ```typescript
|
|
294
|
-
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
297
|
-
*
|
|
285
|
+
* tool.update({
|
|
286
|
+
* output: {
|
|
287
|
+
* result: searchResults,
|
|
288
|
+
* count: searchResults.length,
|
|
289
|
+
* relevanceScore: 0.89,
|
|
290
|
+
* executionTime: 1250
|
|
291
|
+
* },
|
|
292
|
+
* metadata: {
|
|
293
|
+
* cacheHit: false,
|
|
294
|
+
* apiCost: 0.025,
|
|
295
|
+
* rateLimitRemaining: 950
|
|
296
|
+
* }
|
|
298
297
|
* });
|
|
299
298
|
* ```
|
|
300
299
|
*/
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
300
|
+
update(attributes) {
|
|
301
|
+
super.updateOtelSpanAttributes(attributes);
|
|
302
|
+
return this;
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
var LangfuseChain = class extends LangfuseBaseObservation {
|
|
306
|
+
constructor(params) {
|
|
307
|
+
super({ ...params, type: "chain" });
|
|
305
308
|
}
|
|
306
309
|
/**
|
|
307
|
-
*
|
|
310
|
+
* Updates this chain observation with new attributes.
|
|
308
311
|
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
311
|
-
* @param name - Name of the event
|
|
312
|
-
* @param attributes - Optional event attributes
|
|
313
|
-
* @returns The created event (already ended)
|
|
312
|
+
* @param attributes - Chain attributes to set
|
|
313
|
+
* @returns This chain for method chaining
|
|
314
314
|
*
|
|
315
315
|
* @example
|
|
316
316
|
* ```typescript
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
317
|
+
* chain.update({
|
|
318
|
+
* output: {
|
|
319
|
+
* stepsCompleted: 5,
|
|
320
|
+
* stepsSuccessful: 4,
|
|
321
|
+
* finalResult: processedData,
|
|
322
|
+
* pipelineEfficiency: 0.87
|
|
323
|
+
* },
|
|
324
|
+
* metadata: {
|
|
325
|
+
* bottleneckStep: 'data-validation',
|
|
326
|
+
* parallelizationOpportunities: ['step-2', 'step-3'],
|
|
327
|
+
* optimizationSuggestions: ['cache-intermediate-results']
|
|
328
|
+
* }
|
|
320
329
|
* });
|
|
321
330
|
* ```
|
|
322
331
|
*/
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
});
|
|
332
|
+
update(attributes) {
|
|
333
|
+
super.updateOtelSpanAttributes(attributes);
|
|
334
|
+
return this;
|
|
327
335
|
}
|
|
328
336
|
};
|
|
329
|
-
var
|
|
337
|
+
var LangfuseRetriever = class extends LangfuseBaseObservation {
|
|
330
338
|
constructor(params) {
|
|
331
|
-
super(params);
|
|
332
|
-
if (params.attributes) {
|
|
333
|
-
this.otelSpan.setAttributes(
|
|
334
|
-
createGenerationAttributes(params.attributes)
|
|
335
|
-
);
|
|
336
|
-
}
|
|
339
|
+
super({ ...params, type: "retriever" });
|
|
337
340
|
}
|
|
338
341
|
/**
|
|
339
|
-
* Updates this
|
|
340
|
-
*
|
|
341
|
-
* @param attributes - Generation attributes to set
|
|
342
|
-
* @returns This generation for method chaining
|
|
342
|
+
* Updates this retriever observation with new attributes.
|
|
343
343
|
*
|
|
344
|
-
* @
|
|
345
|
-
*
|
|
346
|
-
* generation.update({
|
|
347
|
-
* output: { role: 'assistant', content: 'Hello there!' },
|
|
348
|
-
* usageDetails: {
|
|
349
|
-
* promptTokens: 10,
|
|
350
|
-
* completionTokens: 15,
|
|
351
|
-
* totalTokens: 25
|
|
352
|
-
* },
|
|
353
|
-
* costDetails: { totalCost: 0.001 }
|
|
354
|
-
* });
|
|
355
|
-
* ```
|
|
344
|
+
* @param attributes - Retriever attributes to set
|
|
345
|
+
* @returns This retriever for method chaining
|
|
356
346
|
*/
|
|
357
347
|
update(attributes) {
|
|
358
|
-
|
|
348
|
+
super.updateOtelSpanAttributes(attributes);
|
|
359
349
|
return this;
|
|
360
350
|
}
|
|
351
|
+
};
|
|
352
|
+
var LangfuseEvaluator = class extends LangfuseBaseObservation {
|
|
353
|
+
constructor(params) {
|
|
354
|
+
super({ ...params, type: "evaluator" });
|
|
355
|
+
}
|
|
361
356
|
/**
|
|
362
|
-
*
|
|
357
|
+
* Updates this evaluator observation with new attributes.
|
|
363
358
|
*
|
|
364
|
-
*
|
|
359
|
+
* @param attributes - Evaluator attributes to set
|
|
360
|
+
* @returns This evaluator for method chaining
|
|
361
|
+
*/
|
|
362
|
+
update(attributes) {
|
|
363
|
+
super.updateOtelSpanAttributes(attributes);
|
|
364
|
+
return this;
|
|
365
|
+
}
|
|
366
|
+
};
|
|
367
|
+
var LangfuseGuardrail = class extends LangfuseBaseObservation {
|
|
368
|
+
constructor(params) {
|
|
369
|
+
super({ ...params, type: "guardrail" });
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Updates this guardrail observation with new attributes.
|
|
365
373
|
*
|
|
366
|
-
* @param
|
|
367
|
-
* @
|
|
368
|
-
|
|
374
|
+
* @param attributes - Guardrail attributes to set
|
|
375
|
+
* @returns This guardrail for method chaining
|
|
376
|
+
*/
|
|
377
|
+
update(attributes) {
|
|
378
|
+
super.updateOtelSpanAttributes(attributes);
|
|
379
|
+
return this;
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
var LangfuseGeneration = class extends LangfuseBaseObservation {
|
|
383
|
+
constructor(params) {
|
|
384
|
+
super({ ...params, type: "generation" });
|
|
385
|
+
}
|
|
386
|
+
update(attributes) {
|
|
387
|
+
this.updateOtelSpanAttributes(attributes);
|
|
388
|
+
return this;
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
var LangfuseEmbedding = class extends LangfuseBaseObservation {
|
|
392
|
+
constructor(params) {
|
|
393
|
+
super({ ...params, type: "embedding" });
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Updates this embedding observation with new attributes.
|
|
369
397
|
*
|
|
370
|
-
* @
|
|
371
|
-
*
|
|
372
|
-
* generation.createEvent('token-limit-reached', {
|
|
373
|
-
* level: 'WARNING',
|
|
374
|
-
* metadata: { requestedTokens: 2000, maxTokens: 1500 }
|
|
375
|
-
* });
|
|
376
|
-
* ```
|
|
398
|
+
* @param attributes - Embedding attributes to set
|
|
399
|
+
* @returns This embedding for method chaining
|
|
377
400
|
*/
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
});
|
|
401
|
+
update(attributes) {
|
|
402
|
+
this.updateOtelSpanAttributes(attributes);
|
|
403
|
+
return this;
|
|
382
404
|
}
|
|
383
405
|
};
|
|
384
|
-
var LangfuseEvent = class extends
|
|
406
|
+
var LangfuseEvent = class extends LangfuseBaseObservation {
|
|
385
407
|
constructor(params) {
|
|
386
|
-
super(params);
|
|
387
|
-
if (params.attributes) {
|
|
388
|
-
this.otelSpan.setAttributes(createEventAttributes(params.attributes));
|
|
389
|
-
}
|
|
408
|
+
super({ ...params, type: "event" });
|
|
390
409
|
this.otelSpan.end(params.timestamp);
|
|
391
410
|
}
|
|
392
411
|
};
|
|
@@ -424,73 +443,145 @@ function wrapPromise(promise, span, endOnExit) {
|
|
|
424
443
|
}
|
|
425
444
|
);
|
|
426
445
|
}
|
|
427
|
-
function
|
|
428
|
-
const otelSpan = createOtelSpan({
|
|
429
|
-
name,
|
|
430
|
-
...options
|
|
431
|
-
});
|
|
432
|
-
return new LangfuseSpan({ otelSpan, attributes });
|
|
433
|
-
}
|
|
434
|
-
function startGeneration(name, attributes, options) {
|
|
435
|
-
const otelSpan = createOtelSpan({
|
|
436
|
-
name,
|
|
437
|
-
...options
|
|
438
|
-
});
|
|
439
|
-
return new LangfuseGeneration({ otelSpan, attributes });
|
|
440
|
-
}
|
|
441
|
-
function createEvent(name, attributes, options) {
|
|
446
|
+
function startObservation(name, attributes, options) {
|
|
442
447
|
var _a;
|
|
443
|
-
const
|
|
448
|
+
const { asType = "span", ...observationOptions } = options || {};
|
|
444
449
|
const otelSpan = createOtelSpan({
|
|
445
450
|
name,
|
|
446
|
-
...
|
|
447
|
-
startTime: timestamp
|
|
451
|
+
...observationOptions
|
|
448
452
|
});
|
|
449
|
-
|
|
453
|
+
switch (asType) {
|
|
454
|
+
case "generation":
|
|
455
|
+
return new LangfuseGeneration({
|
|
456
|
+
otelSpan,
|
|
457
|
+
attributes
|
|
458
|
+
});
|
|
459
|
+
case "embedding":
|
|
460
|
+
return new LangfuseEmbedding({
|
|
461
|
+
otelSpan,
|
|
462
|
+
attributes
|
|
463
|
+
});
|
|
464
|
+
case "agent":
|
|
465
|
+
return new LangfuseAgent({
|
|
466
|
+
otelSpan,
|
|
467
|
+
attributes
|
|
468
|
+
});
|
|
469
|
+
case "tool":
|
|
470
|
+
return new LangfuseTool({
|
|
471
|
+
otelSpan,
|
|
472
|
+
attributes
|
|
473
|
+
});
|
|
474
|
+
case "chain":
|
|
475
|
+
return new LangfuseChain({
|
|
476
|
+
otelSpan,
|
|
477
|
+
attributes
|
|
478
|
+
});
|
|
479
|
+
case "retriever":
|
|
480
|
+
return new LangfuseRetriever({
|
|
481
|
+
otelSpan,
|
|
482
|
+
attributes
|
|
483
|
+
});
|
|
484
|
+
case "evaluator":
|
|
485
|
+
return new LangfuseEvaluator({
|
|
486
|
+
otelSpan,
|
|
487
|
+
attributes
|
|
488
|
+
});
|
|
489
|
+
case "guardrail":
|
|
490
|
+
return new LangfuseGuardrail({
|
|
491
|
+
otelSpan,
|
|
492
|
+
attributes
|
|
493
|
+
});
|
|
494
|
+
case "event": {
|
|
495
|
+
const timestamp = (_a = observationOptions == null ? void 0 : observationOptions.startTime) != null ? _a : /* @__PURE__ */ new Date();
|
|
496
|
+
return new LangfuseEvent({
|
|
497
|
+
otelSpan,
|
|
498
|
+
attributes,
|
|
499
|
+
timestamp
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
case "span":
|
|
503
|
+
default:
|
|
504
|
+
return new LangfuseSpan({
|
|
505
|
+
otelSpan,
|
|
506
|
+
attributes
|
|
507
|
+
});
|
|
508
|
+
}
|
|
450
509
|
}
|
|
451
|
-
function
|
|
510
|
+
function startActiveObservation(name, fn, options) {
|
|
452
511
|
var _a;
|
|
512
|
+
const { asType = "span", ...observationOptions } = options || {};
|
|
453
513
|
return getLangfuseTracer().startActiveSpan(
|
|
454
514
|
name,
|
|
455
|
-
{ startTime:
|
|
456
|
-
(_a = createParentContext(
|
|
515
|
+
{ startTime: observationOptions == null ? void 0 : observationOptions.startTime },
|
|
516
|
+
(_a = createParentContext(observationOptions == null ? void 0 : observationOptions.parentSpanContext)) != null ? _a : context.active(),
|
|
457
517
|
(span) => {
|
|
518
|
+
var _a2;
|
|
458
519
|
try {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
520
|
+
let observation;
|
|
521
|
+
switch (asType) {
|
|
522
|
+
case "generation":
|
|
523
|
+
observation = new LangfuseGeneration({
|
|
524
|
+
otelSpan: span
|
|
525
|
+
});
|
|
526
|
+
break;
|
|
527
|
+
case "embedding":
|
|
528
|
+
observation = new LangfuseEmbedding({
|
|
529
|
+
otelSpan: span
|
|
530
|
+
});
|
|
531
|
+
break;
|
|
532
|
+
case "agent":
|
|
533
|
+
observation = new LangfuseAgent({
|
|
534
|
+
otelSpan: span
|
|
535
|
+
});
|
|
536
|
+
break;
|
|
537
|
+
case "tool":
|
|
538
|
+
observation = new LangfuseTool({
|
|
539
|
+
otelSpan: span
|
|
540
|
+
});
|
|
541
|
+
break;
|
|
542
|
+
case "chain":
|
|
543
|
+
observation = new LangfuseChain({
|
|
544
|
+
otelSpan: span
|
|
545
|
+
});
|
|
546
|
+
break;
|
|
547
|
+
case "retriever":
|
|
548
|
+
observation = new LangfuseRetriever({
|
|
549
|
+
otelSpan: span
|
|
550
|
+
});
|
|
551
|
+
break;
|
|
552
|
+
case "evaluator":
|
|
553
|
+
observation = new LangfuseEvaluator({
|
|
554
|
+
otelSpan: span
|
|
555
|
+
});
|
|
556
|
+
break;
|
|
557
|
+
case "guardrail":
|
|
558
|
+
observation = new LangfuseGuardrail({
|
|
559
|
+
otelSpan: span
|
|
560
|
+
});
|
|
561
|
+
break;
|
|
562
|
+
case "event": {
|
|
563
|
+
const timestamp = (_a2 = observationOptions == null ? void 0 : observationOptions.startTime) != null ? _a2 : /* @__PURE__ */ new Date();
|
|
564
|
+
observation = new LangfuseEvent({
|
|
565
|
+
otelSpan: span,
|
|
566
|
+
timestamp
|
|
567
|
+
});
|
|
568
|
+
break;
|
|
465
569
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
message: err instanceof Error ? err.message : "Unknown error"
|
|
472
|
-
});
|
|
473
|
-
if ((options == null ? void 0 : options.endOnExit) !== false) {
|
|
474
|
-
span.end();
|
|
570
|
+
case "span":
|
|
571
|
+
default:
|
|
572
|
+
observation = new LangfuseSpan({
|
|
573
|
+
otelSpan: span
|
|
574
|
+
});
|
|
475
575
|
}
|
|
476
|
-
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
);
|
|
480
|
-
}
|
|
481
|
-
function startActiveGeneration(name, fn, options) {
|
|
482
|
-
var _a;
|
|
483
|
-
return getLangfuseTracer().startActiveSpan(
|
|
484
|
-
name,
|
|
485
|
-
{ startTime: options == null ? void 0 : options.startTime },
|
|
486
|
-
(_a = createParentContext(options == null ? void 0 : options.parentSpanContext)) != null ? _a : context.active(),
|
|
487
|
-
(span) => {
|
|
488
|
-
try {
|
|
489
|
-
const result = fn(new LangfuseGeneration({ otelSpan: span }));
|
|
576
|
+
const result = fn(observation);
|
|
490
577
|
if (result instanceof Promise) {
|
|
491
|
-
return wrapPromise(
|
|
578
|
+
return wrapPromise(
|
|
579
|
+
result,
|
|
580
|
+
span,
|
|
581
|
+
observationOptions == null ? void 0 : observationOptions.endOnExit
|
|
582
|
+
);
|
|
492
583
|
} else {
|
|
493
|
-
if ((
|
|
584
|
+
if ((observationOptions == null ? void 0 : observationOptions.endOnExit) !== false) {
|
|
494
585
|
span.end();
|
|
495
586
|
}
|
|
496
587
|
return result;
|
|
@@ -500,7 +591,7 @@ function startActiveGeneration(name, fn, options) {
|
|
|
500
591
|
code: SpanStatusCode.ERROR,
|
|
501
592
|
message: err instanceof Error ? err.message : "Unknown error"
|
|
502
593
|
});
|
|
503
|
-
if ((
|
|
594
|
+
if ((observationOptions == null ? void 0 : observationOptions.endOnExit) !== false) {
|
|
504
595
|
span.end();
|
|
505
596
|
}
|
|
506
597
|
throw err;
|
|
@@ -518,7 +609,7 @@ function updateActiveTrace(attributes) {
|
|
|
518
609
|
}
|
|
519
610
|
span.setAttributes(createTraceAttributes(attributes));
|
|
520
611
|
}
|
|
521
|
-
function
|
|
612
|
+
function updateActiveObservation(currentType, attributes) {
|
|
522
613
|
const span = trace2.getActiveSpan();
|
|
523
614
|
if (!span) {
|
|
524
615
|
getGlobalLogger2().warn(
|
|
@@ -526,17 +617,7 @@ function updateActiveSpan(attributes) {
|
|
|
526
617
|
);
|
|
527
618
|
return;
|
|
528
619
|
}
|
|
529
|
-
span.setAttributes(
|
|
530
|
-
}
|
|
531
|
-
function updateActiveGeneration(attributes) {
|
|
532
|
-
const span = trace2.getActiveSpan();
|
|
533
|
-
if (!span) {
|
|
534
|
-
getGlobalLogger2().warn(
|
|
535
|
-
"No active OTEL span in context. Skipping generation update."
|
|
536
|
-
);
|
|
537
|
-
return;
|
|
538
|
-
}
|
|
539
|
-
span.setAttributes(createGenerationAttributes(attributes));
|
|
620
|
+
span.setAttributes(createObservationAttributes(currentType, attributes));
|
|
540
621
|
}
|
|
541
622
|
function observe(fn, options = {}) {
|
|
542
623
|
const {
|
|
@@ -548,11 +629,15 @@ function observe(fn, options = {}) {
|
|
|
548
629
|
} = options;
|
|
549
630
|
const wrappedFunction = function(...args) {
|
|
550
631
|
const inputData = captureInput ? _captureArguments(args) : void 0;
|
|
551
|
-
const observation =
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
632
|
+
const observation = startObservation(
|
|
633
|
+
name,
|
|
634
|
+
inputData ? { input: inputData } : {},
|
|
635
|
+
{
|
|
636
|
+
asType,
|
|
637
|
+
// typecast necessary as ts cannot narrow down type
|
|
638
|
+
parentSpanContext
|
|
639
|
+
}
|
|
640
|
+
);
|
|
556
641
|
const activeContext = trace2.setSpan(context.active(), observation.otelSpan);
|
|
557
642
|
try {
|
|
558
643
|
const result = context.with(activeContext, () => fn.apply(this, args));
|
|
@@ -642,13 +727,18 @@ function getActiveSpanId() {
|
|
|
642
727
|
return (_a = trace2.getActiveSpan()) == null ? void 0 : _a.spanContext().spanId;
|
|
643
728
|
}
|
|
644
729
|
export {
|
|
730
|
+
LangfuseAgent,
|
|
731
|
+
LangfuseChain,
|
|
732
|
+
LangfuseEmbedding,
|
|
733
|
+
LangfuseEvaluator,
|
|
645
734
|
LangfuseEvent,
|
|
646
735
|
LangfuseGeneration,
|
|
736
|
+
LangfuseGuardrail,
|
|
647
737
|
LangfuseOtelSpanAttributes2 as LangfuseOtelSpanAttributes,
|
|
738
|
+
LangfuseRetriever,
|
|
648
739
|
LangfuseSpan,
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
createSpanAttributes,
|
|
740
|
+
LangfuseTool,
|
|
741
|
+
createObservationAttributes,
|
|
652
742
|
createTraceAttributes,
|
|
653
743
|
createTraceId,
|
|
654
744
|
getActiveSpanId,
|
|
@@ -657,12 +747,9 @@ export {
|
|
|
657
747
|
getLangfuseTracerProvider,
|
|
658
748
|
observe,
|
|
659
749
|
setLangfuseTracerProvider,
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
startSpan,
|
|
664
|
-
updateActiveGeneration,
|
|
665
|
-
updateActiveSpan,
|
|
750
|
+
startActiveObservation,
|
|
751
|
+
startObservation,
|
|
752
|
+
updateActiveObservation,
|
|
666
753
|
updateActiveTrace
|
|
667
754
|
};
|
|
668
755
|
//# sourceMappingURL=index.mjs.map
|