@langfuse/tracing 4.0.0-beta.7 → 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 CHANGED
@@ -20,13 +20,18 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ LangfuseAgent: () => LangfuseAgent,
24
+ LangfuseChain: () => LangfuseChain,
25
+ LangfuseEmbedding: () => LangfuseEmbedding,
26
+ LangfuseEvaluator: () => LangfuseEvaluator,
23
27
  LangfuseEvent: () => LangfuseEvent,
24
28
  LangfuseGeneration: () => LangfuseGeneration,
29
+ LangfuseGuardrail: () => LangfuseGuardrail,
25
30
  LangfuseOtelSpanAttributes: () => import_core4.LangfuseOtelSpanAttributes,
31
+ LangfuseRetriever: () => LangfuseRetriever,
26
32
  LangfuseSpan: () => LangfuseSpan,
27
- createEvent: () => createEvent,
28
- createGenerationAttributes: () => createGenerationAttributes,
29
- createSpanAttributes: () => createSpanAttributes,
33
+ LangfuseTool: () => LangfuseTool,
34
+ createObservationAttributes: () => createObservationAttributes,
30
35
  createTraceAttributes: () => createTraceAttributes,
31
36
  createTraceId: () => createTraceId,
32
37
  getActiveSpanId: () => getActiveSpanId,
@@ -35,12 +40,9 @@ __export(index_exports, {
35
40
  getLangfuseTracerProvider: () => getLangfuseTracerProvider,
36
41
  observe: () => observe,
37
42
  setLangfuseTracerProvider: () => setLangfuseTracerProvider,
38
- startActiveGeneration: () => startActiveGeneration,
39
- startActiveSpan: () => startActiveSpan,
40
- startGeneration: () => startGeneration,
41
- startSpan: () => startSpan,
42
- updateActiveGeneration: () => updateActiveGeneration,
43
- updateActiveSpan: () => updateActiveSpan,
43
+ startActiveObservation: () => startActiveObservation,
44
+ startObservation: () => startObservation,
45
+ updateActiveObservation: () => updateActiveObservation,
44
46
  updateActiveTrace: () => updateActiveTrace
45
47
  });
46
48
  module.exports = __toCommonJS(index_exports);
@@ -79,43 +81,23 @@ function createTraceAttributes({
79
81
  Object.entries(attributes).filter(([_, v]) => v != null)
80
82
  );
81
83
  }
82
- function createSpanAttributes({
83
- metadata,
84
- input,
85
- output,
86
- level,
87
- statusMessage,
88
- version
89
- }) {
90
- const attributes = {
91
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_TYPE]: "span",
92
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_LEVEL]: level,
93
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]: statusMessage,
94
- [import_core.LangfuseOtelSpanAttributes.VERSION]: version,
95
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_INPUT]: _serialize(input),
96
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT]: _serialize(output),
97
- ..._flattenAndSerializeMetadata(metadata, "observation")
98
- };
99
- return Object.fromEntries(
100
- Object.entries(attributes).filter(([_, v]) => v != null)
101
- );
102
- }
103
- function createGenerationAttributes({
104
- completionStartTime,
105
- metadata,
106
- level,
107
- statusMessage,
108
- version,
109
- model,
110
- modelParameters,
111
- input,
112
- output,
113
- usageDetails,
114
- costDetails,
115
- prompt
116
- }) {
117
- const attributes = {
118
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_TYPE]: "generation",
84
+ function createObservationAttributes(type, attributes) {
85
+ const {
86
+ metadata,
87
+ input,
88
+ output,
89
+ level,
90
+ statusMessage,
91
+ version,
92
+ completionStartTime,
93
+ model,
94
+ modelParameters,
95
+ usageDetails,
96
+ costDetails,
97
+ prompt
98
+ } = attributes;
99
+ let otelAttributes = {
100
+ [import_core.LangfuseOtelSpanAttributes.OBSERVATION_TYPE]: type,
119
101
  [import_core.LangfuseOtelSpanAttributes.OBSERVATION_LEVEL]: level,
120
102
  [import_core.LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]: statusMessage,
121
103
  [import_core.LangfuseOtelSpanAttributes.VERSION]: version,
@@ -133,28 +115,7 @@ function createGenerationAttributes({
133
115
  ..._flattenAndSerializeMetadata(metadata, "observation")
134
116
  };
135
117
  return Object.fromEntries(
136
- Object.entries(attributes).filter(([_, v]) => v != null)
137
- );
138
- }
139
- function createEventAttributes({
140
- metadata,
141
- input,
142
- output,
143
- level,
144
- statusMessage,
145
- version
146
- }) {
147
- const attributes = {
148
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_TYPE]: "event",
149
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_LEVEL]: level,
150
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE]: statusMessage,
151
- [import_core.LangfuseOtelSpanAttributes.VERSION]: version,
152
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_INPUT]: _serialize(input),
153
- [import_core.LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT]: _serialize(output),
154
- ..._flattenAndSerializeMetadata(metadata, "observation")
155
- };
156
- return Object.fromEntries(
157
- Object.entries(attributes).filter(([_, v]) => v != null)
118
+ Object.entries(otelAttributes).filter(([_, v]) => v != null)
158
119
  );
159
120
  }
160
121
  function _serialize(obj) {
@@ -241,11 +202,17 @@ function getLangfuseTracer() {
241
202
  }
242
203
 
243
204
  // src/spanWrapper.ts
244
- var LangfuseSpanWrapper = class {
205
+ var LangfuseBaseObservation = class {
245
206
  constructor(params) {
246
207
  this.otelSpan = params.otelSpan;
247
208
  this.id = params.otelSpan.spanContext().spanId;
248
209
  this.traceId = params.otelSpan.spanContext().traceId;
210
+ this.type = params.type;
211
+ if (params.attributes) {
212
+ this.otelSpan.setAttributes(
213
+ createObservationAttributes(params.type, params.attributes)
214
+ );
215
+ }
249
216
  }
250
217
  /** Gets the Langfuse OpenTelemetry tracer instance */
251
218
  get tracer() {
@@ -259,6 +226,11 @@ var LangfuseSpanWrapper = class {
259
226
  end(endTime) {
260
227
  this.otelSpan.end(endTime);
261
228
  }
229
+ updateOtelSpanAttributes(attributes) {
230
+ this.otelSpan.setAttributes(
231
+ createObservationAttributes(this.type, attributes)
232
+ );
233
+ }
262
234
  /**
263
235
  * Updates the parent trace with new attributes.
264
236
  *
@@ -272,13 +244,18 @@ var LangfuseSpanWrapper = class {
272
244
  this.otelSpan.setAttributes(createTraceAttributes(attributes));
273
245
  return this;
274
246
  }
247
+ startObservation(name, attributes, options) {
248
+ const { asType = "span" } = options || {};
249
+ return startObservation(name, attributes, {
250
+ asType,
251
+ // typecast necessary as ts cannot narrow the type correctly
252
+ parentSpanContext: this.otelSpan.spanContext()
253
+ });
254
+ }
275
255
  };
276
- var LangfuseSpan = class extends LangfuseSpanWrapper {
256
+ var LangfuseSpan = class extends LangfuseBaseObservation {
277
257
  constructor(params) {
278
- super(params);
279
- if (params.attributes) {
280
- this.otelSpan.setAttributes(createSpanAttributes(params.attributes));
281
- }
258
+ super({ ...params, type: "span" });
282
259
  }
283
260
  /**
284
261
  * Updates this span with new attributes.
@@ -296,134 +273,178 @@ var LangfuseSpan = class extends LangfuseSpanWrapper {
296
273
  * ```
297
274
  */
298
275
  update(attributes) {
299
- this.otelSpan.setAttributes(createSpanAttributes(attributes));
276
+ super.updateOtelSpanAttributes(attributes);
300
277
  return this;
301
278
  }
279
+ };
280
+ var LangfuseAgent = class extends LangfuseBaseObservation {
281
+ constructor(params) {
282
+ super({ ...params, type: "agent" });
283
+ }
302
284
  /**
303
- * Starts a new child span within this span.
285
+ * Updates this agent observation with new attributes.
304
286
  *
305
- * @param name - Name of the child span
306
- * @param attributes - Optional attributes for the child span
307
- * @returns The new child span
287
+ * @param attributes - Agent attributes to set
288
+ * @returns This agent for method chaining
308
289
  *
309
290
  * @example
310
291
  * ```typescript
311
- * const childSpan = parentSpan.startSpan('database-query', {
312
- * input: { query: 'SELECT * FROM users' },
313
- * metadata: { database: 'primary' }
292
+ * agent.update({
293
+ * output: {
294
+ * taskCompleted: true,
295
+ * iterationsUsed: 5,
296
+ * toolsInvoked: ['web-search', 'calculator', 'summarizer'],
297
+ * finalResult: 'Research completed with high confidence'
298
+ * },
299
+ * metadata: {
300
+ * efficiency: 0.85,
301
+ * qualityScore: 0.92,
302
+ * resourcesConsumed: { tokens: 15000, apiCalls: 12 }
303
+ * }
314
304
  * });
315
305
  * ```
316
306
  */
317
- startSpan(name, attributes) {
318
- return startSpan(name, attributes, {
319
- parentSpanContext: this.otelSpan.spanContext()
320
- });
307
+ update(attributes) {
308
+ super.updateOtelSpanAttributes(attributes);
309
+ return this;
310
+ }
311
+ };
312
+ var LangfuseTool = class extends LangfuseBaseObservation {
313
+ constructor(params) {
314
+ super({ ...params, type: "tool" });
321
315
  }
322
316
  /**
323
- * Starts a new child generation within this span.
317
+ * Updates this tool observation with new attributes.
324
318
  *
325
- * @param name - Name of the generation (typically the model name)
326
- * @param attributes - Optional generation-specific attributes
327
- * @returns The new child generation
319
+ * @param attributes - Tool attributes to set
320
+ * @returns This tool for method chaining
328
321
  *
329
322
  * @example
330
323
  * ```typescript
331
- * const generation = parentSpan.startGeneration('gpt-4', {
332
- * input: [{ role: 'user', content: 'Hello!' }],
333
- * model: 'gpt-4',
334
- * modelParameters: { temperature: 0.7 }
324
+ * tool.update({
325
+ * output: {
326
+ * result: searchResults,
327
+ * count: searchResults.length,
328
+ * relevanceScore: 0.89,
329
+ * executionTime: 1250
330
+ * },
331
+ * metadata: {
332
+ * cacheHit: false,
333
+ * apiCost: 0.025,
334
+ * rateLimitRemaining: 950
335
+ * }
335
336
  * });
336
337
  * ```
337
338
  */
338
- startGeneration(name, attributes) {
339
- return startGeneration(name, attributes, {
340
- parentSpanContext: this.otelSpan.spanContext()
341
- });
339
+ update(attributes) {
340
+ super.updateOtelSpanAttributes(attributes);
341
+ return this;
342
+ }
343
+ };
344
+ var LangfuseChain = class extends LangfuseBaseObservation {
345
+ constructor(params) {
346
+ super({ ...params, type: "chain" });
342
347
  }
343
348
  /**
344
- * Creates a new event within this span.
349
+ * Updates this chain observation with new attributes.
345
350
  *
346
- * Events are point-in-time occurrences and are automatically ended.
347
- *
348
- * @param name - Name of the event
349
- * @param attributes - Optional event attributes
350
- * @returns The created event (already ended)
351
+ * @param attributes - Chain attributes to set
352
+ * @returns This chain for method chaining
351
353
  *
352
354
  * @example
353
355
  * ```typescript
354
- * parentSpan.createEvent('user-action', {
355
- * input: { action: 'click', button: 'submit' },
356
- * metadata: { userId: '123' }
356
+ * chain.update({
357
+ * output: {
358
+ * stepsCompleted: 5,
359
+ * stepsSuccessful: 4,
360
+ * finalResult: processedData,
361
+ * pipelineEfficiency: 0.87
362
+ * },
363
+ * metadata: {
364
+ * bottleneckStep: 'data-validation',
365
+ * parallelizationOpportunities: ['step-2', 'step-3'],
366
+ * optimizationSuggestions: ['cache-intermediate-results']
367
+ * }
357
368
  * });
358
369
  * ```
359
370
  */
360
- createEvent(name, attributes) {
361
- return createEvent(name, attributes, {
362
- parentSpanContext: this.otelSpan.spanContext()
363
- });
371
+ update(attributes) {
372
+ super.updateOtelSpanAttributes(attributes);
373
+ return this;
364
374
  }
365
375
  };
366
- var LangfuseGeneration = class extends LangfuseSpanWrapper {
376
+ var LangfuseRetriever = class extends LangfuseBaseObservation {
367
377
  constructor(params) {
368
- super(params);
369
- if (params.attributes) {
370
- this.otelSpan.setAttributes(
371
- createGenerationAttributes(params.attributes)
372
- );
373
- }
378
+ super({ ...params, type: "retriever" });
374
379
  }
375
380
  /**
376
- * Updates this generation with new attributes.
377
- *
378
- * @param attributes - Generation attributes to set
379
- * @returns This generation for method chaining
381
+ * Updates this retriever observation with new attributes.
380
382
  *
381
- * @example
382
- * ```typescript
383
- * generation.update({
384
- * output: { role: 'assistant', content: 'Hello there!' },
385
- * usageDetails: {
386
- * promptTokens: 10,
387
- * completionTokens: 15,
388
- * totalTokens: 25
389
- * },
390
- * costDetails: { totalCost: 0.001 }
391
- * });
392
- * ```
383
+ * @param attributes - Retriever attributes to set
384
+ * @returns This retriever for method chaining
393
385
  */
394
386
  update(attributes) {
395
- this.otelSpan.setAttributes(createGenerationAttributes(attributes));
387
+ super.updateOtelSpanAttributes(attributes);
396
388
  return this;
397
389
  }
390
+ };
391
+ var LangfuseEvaluator = class extends LangfuseBaseObservation {
392
+ constructor(params) {
393
+ super({ ...params, type: "evaluator" });
394
+ }
398
395
  /**
399
- * Creates a new event within this generation.
396
+ * Updates this evaluator observation with new attributes.
400
397
  *
401
- * Events are point-in-time occurrences and are automatically ended.
398
+ * @param attributes - Evaluator attributes to set
399
+ * @returns This evaluator for method chaining
400
+ */
401
+ update(attributes) {
402
+ super.updateOtelSpanAttributes(attributes);
403
+ return this;
404
+ }
405
+ };
406
+ var LangfuseGuardrail = class extends LangfuseBaseObservation {
407
+ constructor(params) {
408
+ super({ ...params, type: "guardrail" });
409
+ }
410
+ /**
411
+ * Updates this guardrail observation with new attributes.
402
412
  *
403
- * @param name - Name of the event
404
- * @param attributes - Optional event attributes
405
- * @returns The created event (already ended)
413
+ * @param attributes - Guardrail attributes to set
414
+ * @returns This guardrail for method chaining
415
+ */
416
+ update(attributes) {
417
+ super.updateOtelSpanAttributes(attributes);
418
+ return this;
419
+ }
420
+ };
421
+ var LangfuseGeneration = class extends LangfuseBaseObservation {
422
+ constructor(params) {
423
+ super({ ...params, type: "generation" });
424
+ }
425
+ update(attributes) {
426
+ this.updateOtelSpanAttributes(attributes);
427
+ return this;
428
+ }
429
+ };
430
+ var LangfuseEmbedding = class extends LangfuseBaseObservation {
431
+ constructor(params) {
432
+ super({ ...params, type: "embedding" });
433
+ }
434
+ /**
435
+ * Updates this embedding observation with new attributes.
406
436
  *
407
- * @example
408
- * ```typescript
409
- * generation.createEvent('token-limit-reached', {
410
- * level: 'WARNING',
411
- * metadata: { requestedTokens: 2000, maxTokens: 1500 }
412
- * });
413
- * ```
437
+ * @param attributes - Embedding attributes to set
438
+ * @returns This embedding for method chaining
414
439
  */
415
- createEvent(name, attributes) {
416
- return createEvent(name, attributes, {
417
- parentSpanContext: this.otelSpan.spanContext()
418
- });
440
+ update(attributes) {
441
+ this.updateOtelSpanAttributes(attributes);
442
+ return this;
419
443
  }
420
444
  };
421
- var LangfuseEvent = class extends LangfuseSpanWrapper {
445
+ var LangfuseEvent = class extends LangfuseBaseObservation {
422
446
  constructor(params) {
423
- super(params);
424
- if (params.attributes) {
425
- this.otelSpan.setAttributes(createEventAttributes(params.attributes));
426
- }
447
+ super({ ...params, type: "event" });
427
448
  this.otelSpan.end(params.timestamp);
428
449
  }
429
450
  };
@@ -461,73 +482,145 @@ function wrapPromise(promise, span, endOnExit) {
461
482
  }
462
483
  );
463
484
  }
464
- function startSpan(name, attributes, options) {
465
- const otelSpan = createOtelSpan({
466
- name,
467
- ...options
468
- });
469
- return new LangfuseSpan({ otelSpan, attributes });
470
- }
471
- function startGeneration(name, attributes, options) {
472
- const otelSpan = createOtelSpan({
473
- name,
474
- ...options
475
- });
476
- return new LangfuseGeneration({ otelSpan, attributes });
477
- }
478
- function createEvent(name, attributes, options) {
485
+ function startObservation(name, attributes, options) {
479
486
  var _a;
480
- const timestamp = (_a = options == null ? void 0 : options.startTime) != null ? _a : /* @__PURE__ */ new Date();
487
+ const { asType = "span", ...observationOptions } = options || {};
481
488
  const otelSpan = createOtelSpan({
482
489
  name,
483
- ...options,
484
- startTime: timestamp
490
+ ...observationOptions
485
491
  });
486
- return new LangfuseEvent({ otelSpan, attributes, timestamp });
492
+ switch (asType) {
493
+ case "generation":
494
+ return new LangfuseGeneration({
495
+ otelSpan,
496
+ attributes
497
+ });
498
+ case "embedding":
499
+ return new LangfuseEmbedding({
500
+ otelSpan,
501
+ attributes
502
+ });
503
+ case "agent":
504
+ return new LangfuseAgent({
505
+ otelSpan,
506
+ attributes
507
+ });
508
+ case "tool":
509
+ return new LangfuseTool({
510
+ otelSpan,
511
+ attributes
512
+ });
513
+ case "chain":
514
+ return new LangfuseChain({
515
+ otelSpan,
516
+ attributes
517
+ });
518
+ case "retriever":
519
+ return new LangfuseRetriever({
520
+ otelSpan,
521
+ attributes
522
+ });
523
+ case "evaluator":
524
+ return new LangfuseEvaluator({
525
+ otelSpan,
526
+ attributes
527
+ });
528
+ case "guardrail":
529
+ return new LangfuseGuardrail({
530
+ otelSpan,
531
+ attributes
532
+ });
533
+ case "event": {
534
+ const timestamp = (_a = observationOptions == null ? void 0 : observationOptions.startTime) != null ? _a : /* @__PURE__ */ new Date();
535
+ return new LangfuseEvent({
536
+ otelSpan,
537
+ attributes,
538
+ timestamp
539
+ });
540
+ }
541
+ case "span":
542
+ default:
543
+ return new LangfuseSpan({
544
+ otelSpan,
545
+ attributes
546
+ });
547
+ }
487
548
  }
488
- function startActiveSpan(name, fn, options) {
549
+ function startActiveObservation(name, fn, options) {
489
550
  var _a;
551
+ const { asType = "span", ...observationOptions } = options || {};
490
552
  return getLangfuseTracer().startActiveSpan(
491
553
  name,
492
- { startTime: options == null ? void 0 : options.startTime },
493
- (_a = createParentContext(options == null ? void 0 : options.parentSpanContext)) != null ? _a : import_api2.context.active(),
554
+ { startTime: observationOptions == null ? void 0 : observationOptions.startTime },
555
+ (_a = createParentContext(observationOptions == null ? void 0 : observationOptions.parentSpanContext)) != null ? _a : import_api2.context.active(),
494
556
  (span) => {
557
+ var _a2;
495
558
  try {
496
- const result = fn(new LangfuseSpan({ otelSpan: span }));
497
- if (result instanceof Promise) {
498
- return wrapPromise(result, span, options == null ? void 0 : options.endOnExit);
499
- } else {
500
- if ((options == null ? void 0 : options.endOnExit) !== false) {
501
- span.end();
559
+ let observation;
560
+ switch (asType) {
561
+ case "generation":
562
+ observation = new LangfuseGeneration({
563
+ otelSpan: span
564
+ });
565
+ break;
566
+ case "embedding":
567
+ observation = new LangfuseEmbedding({
568
+ otelSpan: span
569
+ });
570
+ break;
571
+ case "agent":
572
+ observation = new LangfuseAgent({
573
+ otelSpan: span
574
+ });
575
+ break;
576
+ case "tool":
577
+ observation = new LangfuseTool({
578
+ otelSpan: span
579
+ });
580
+ break;
581
+ case "chain":
582
+ observation = new LangfuseChain({
583
+ otelSpan: span
584
+ });
585
+ break;
586
+ case "retriever":
587
+ observation = new LangfuseRetriever({
588
+ otelSpan: span
589
+ });
590
+ break;
591
+ case "evaluator":
592
+ observation = new LangfuseEvaluator({
593
+ otelSpan: span
594
+ });
595
+ break;
596
+ case "guardrail":
597
+ observation = new LangfuseGuardrail({
598
+ otelSpan: span
599
+ });
600
+ break;
601
+ case "event": {
602
+ const timestamp = (_a2 = observationOptions == null ? void 0 : observationOptions.startTime) != null ? _a2 : /* @__PURE__ */ new Date();
603
+ observation = new LangfuseEvent({
604
+ otelSpan: span,
605
+ timestamp
606
+ });
607
+ break;
502
608
  }
503
- return result;
504
- }
505
- } catch (err) {
506
- span.setStatus({
507
- code: import_api2.SpanStatusCode.ERROR,
508
- message: err instanceof Error ? err.message : "Unknown error"
509
- });
510
- if ((options == null ? void 0 : options.endOnExit) !== false) {
511
- span.end();
609
+ case "span":
610
+ default:
611
+ observation = new LangfuseSpan({
612
+ otelSpan: span
613
+ });
512
614
  }
513
- throw err;
514
- }
515
- }
516
- );
517
- }
518
- function startActiveGeneration(name, fn, options) {
519
- var _a;
520
- return getLangfuseTracer().startActiveSpan(
521
- name,
522
- { startTime: options == null ? void 0 : options.startTime },
523
- (_a = createParentContext(options == null ? void 0 : options.parentSpanContext)) != null ? _a : import_api2.context.active(),
524
- (span) => {
525
- try {
526
- const result = fn(new LangfuseGeneration({ otelSpan: span }));
615
+ const result = fn(observation);
527
616
  if (result instanceof Promise) {
528
- return wrapPromise(result, span, options == null ? void 0 : options.endOnExit);
617
+ return wrapPromise(
618
+ result,
619
+ span,
620
+ observationOptions == null ? void 0 : observationOptions.endOnExit
621
+ );
529
622
  } else {
530
- if ((options == null ? void 0 : options.endOnExit) !== false) {
623
+ if ((observationOptions == null ? void 0 : observationOptions.endOnExit) !== false) {
531
624
  span.end();
532
625
  }
533
626
  return result;
@@ -537,7 +630,7 @@ function startActiveGeneration(name, fn, options) {
537
630
  code: import_api2.SpanStatusCode.ERROR,
538
631
  message: err instanceof Error ? err.message : "Unknown error"
539
632
  });
540
- if ((options == null ? void 0 : options.endOnExit) !== false) {
633
+ if ((observationOptions == null ? void 0 : observationOptions.endOnExit) !== false) {
541
634
  span.end();
542
635
  }
543
636
  throw err;
@@ -555,7 +648,7 @@ function updateActiveTrace(attributes) {
555
648
  }
556
649
  span.setAttributes(createTraceAttributes(attributes));
557
650
  }
558
- function updateActiveSpan(attributes) {
651
+ function updateActiveObservation(currentType, attributes) {
559
652
  const span = import_api2.trace.getActiveSpan();
560
653
  if (!span) {
561
654
  (0, import_core3.getGlobalLogger)().warn(
@@ -563,17 +656,7 @@ function updateActiveSpan(attributes) {
563
656
  );
564
657
  return;
565
658
  }
566
- span.setAttributes(createSpanAttributes(attributes));
567
- }
568
- function updateActiveGeneration(attributes) {
569
- const span = import_api2.trace.getActiveSpan();
570
- if (!span) {
571
- (0, import_core3.getGlobalLogger)().warn(
572
- "No active OTEL span in context. Skipping generation update."
573
- );
574
- return;
575
- }
576
- span.setAttributes(createGenerationAttributes(attributes));
659
+ span.setAttributes(createObservationAttributes(currentType, attributes));
577
660
  }
578
661
  function observe(fn, options = {}) {
579
662
  const {
@@ -585,11 +668,15 @@ function observe(fn, options = {}) {
585
668
  } = options;
586
669
  const wrappedFunction = function(...args) {
587
670
  const inputData = captureInput ? _captureArguments(args) : void 0;
588
- const observation = asType === "generation" ? startGeneration(name, inputData ? { input: inputData } : {}, {
589
- parentSpanContext
590
- }) : startSpan(name, inputData ? { input: inputData } : {}, {
591
- parentSpanContext
592
- });
671
+ const observation = startObservation(
672
+ name,
673
+ inputData ? { input: inputData } : {},
674
+ {
675
+ asType,
676
+ // typecast necessary as ts cannot narrow down type
677
+ parentSpanContext
678
+ }
679
+ );
593
680
  const activeContext = import_api2.trace.setSpan(import_api2.context.active(), observation.otelSpan);
594
681
  try {
595
682
  const result = import_api2.context.with(activeContext, () => fn.apply(this, args));
@@ -680,13 +767,18 @@ function getActiveSpanId() {
680
767
  }
681
768
  // Annotate the CommonJS export names for ESM import in node:
682
769
  0 && (module.exports = {
770
+ LangfuseAgent,
771
+ LangfuseChain,
772
+ LangfuseEmbedding,
773
+ LangfuseEvaluator,
683
774
  LangfuseEvent,
684
775
  LangfuseGeneration,
776
+ LangfuseGuardrail,
685
777
  LangfuseOtelSpanAttributes,
778
+ LangfuseRetriever,
686
779
  LangfuseSpan,
687
- createEvent,
688
- createGenerationAttributes,
689
- createSpanAttributes,
780
+ LangfuseTool,
781
+ createObservationAttributes,
690
782
  createTraceAttributes,
691
783
  createTraceId,
692
784
  getActiveSpanId,
@@ -695,12 +787,9 @@ function getActiveSpanId() {
695
787
  getLangfuseTracerProvider,
696
788
  observe,
697
789
  setLangfuseTracerProvider,
698
- startActiveGeneration,
699
- startActiveSpan,
700
- startGeneration,
701
- startSpan,
702
- updateActiveGeneration,
703
- updateActiveSpan,
790
+ startActiveObservation,
791
+ startObservation,
792
+ updateActiveObservation,
704
793
  updateActiveTrace
705
794
  });
706
795
  //# sourceMappingURL=index.cjs.map