@langfuse/client 4.0.0-beta.0 → 4.0.0-beta.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 +24 -0
- package/dist/index.cjs +434 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +633 -12
- package/dist/index.d.ts +633 -12
- package/dist/index.mjs +434 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -8,9 +8,44 @@ import {
|
|
|
8
8
|
|
|
9
9
|
// src/dataset/index.ts
|
|
10
10
|
var DatasetManager = class {
|
|
11
|
+
/**
|
|
12
|
+
* Creates a new DatasetManager instance.
|
|
13
|
+
*
|
|
14
|
+
* @param params - Configuration object containing the API client
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
11
17
|
constructor(params) {
|
|
12
18
|
this.apiClient = params.apiClient;
|
|
13
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Retrieves a dataset by name along with all its items.
|
|
22
|
+
*
|
|
23
|
+
* This method automatically handles pagination to fetch all dataset items
|
|
24
|
+
* and enhances each item with a `link` function for easy experiment tracking.
|
|
25
|
+
*
|
|
26
|
+
* @param name - The name of the dataset to retrieve
|
|
27
|
+
* @param options - Optional configuration for fetching
|
|
28
|
+
* @param options.fetchItemsPageSize - Number of items to fetch per page (default: 50)
|
|
29
|
+
*
|
|
30
|
+
* @returns Promise that resolves to the dataset with enhanced items
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const dataset = await langfuse.dataset.get("my-dataset");
|
|
35
|
+
*
|
|
36
|
+
* for (const item of dataset.items) {
|
|
37
|
+
* // Use the item data for your experiment
|
|
38
|
+
* const result = await processItem(item.input);
|
|
39
|
+
*
|
|
40
|
+
* // Link the result to the dataset item
|
|
41
|
+
* await item.link(
|
|
42
|
+
* { otelSpan: currentSpan },
|
|
43
|
+
* "experiment-run-1",
|
|
44
|
+
* { description: "Testing new model" }
|
|
45
|
+
* );
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
14
49
|
async get(name, options) {
|
|
15
50
|
var _a;
|
|
16
51
|
const dataset = await this.apiClient.datasets.get(name);
|
|
@@ -37,6 +72,13 @@ var DatasetManager = class {
|
|
|
37
72
|
};
|
|
38
73
|
return returnDataset;
|
|
39
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* Creates a link function for a specific dataset item.
|
|
77
|
+
*
|
|
78
|
+
* @param item - The dataset item to create a link function for
|
|
79
|
+
* @returns A function that can link the item to OpenTelemetry spans
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
40
82
|
createDatasetItemLinkFunction(item) {
|
|
41
83
|
const linkFunction = async (obj, runName, runArgs) => {
|
|
42
84
|
return await this.apiClient.datasetRunItems.create({
|
|
@@ -57,24 +99,27 @@ import {
|
|
|
57
99
|
uint8ArrayToBase64
|
|
58
100
|
} from "@langfuse/core";
|
|
59
101
|
var MediaManager = class _MediaManager {
|
|
102
|
+
/**
|
|
103
|
+
* Creates a new MediaManager instance.
|
|
104
|
+
*
|
|
105
|
+
* @param params - Configuration object containing the API client
|
|
106
|
+
* @internal
|
|
107
|
+
*/
|
|
60
108
|
constructor(params) {
|
|
61
109
|
this.apiClient = params.apiClient;
|
|
62
110
|
}
|
|
63
111
|
/**
|
|
64
|
-
* Replaces
|
|
112
|
+
* Replaces media reference strings in an object with base64 data URIs.
|
|
65
113
|
*
|
|
66
|
-
* This method recursively traverses an object
|
|
67
|
-
* in the format "@@@langfuseMedia:...@@@". When found, it fetches the actual media
|
|
68
|
-
* Langfuse
|
|
114
|
+
* This method recursively traverses an object looking for media reference strings
|
|
115
|
+
* in the format "@@@langfuseMedia:...@@@". When found, it fetches the actual media
|
|
116
|
+
* content from Langfuse and replaces the reference string with a base64 data URI.
|
|
69
117
|
*
|
|
70
|
-
* If fetching media content fails for a reference string, a warning is logged
|
|
118
|
+
* If fetching media content fails for a reference string, a warning is logged
|
|
119
|
+
* and the reference string is left unchanged.
|
|
71
120
|
*
|
|
72
121
|
* @param params - Configuration object
|
|
73
|
-
* @
|
|
74
|
-
* @param params.resolveWith - The representation of the media content to replace the media reference string with. Currently only "base64DataUri" is supported.
|
|
75
|
-
* @param params.maxDepth - Optional. Default is 10. The maximum depth to traverse the object.
|
|
76
|
-
*
|
|
77
|
-
* @returns A deep copy of the input object with all media references replaced with base64 data URIs where possible
|
|
122
|
+
* @returns A deep copy of the input object with media references resolved
|
|
78
123
|
*
|
|
79
124
|
* @example
|
|
80
125
|
* ```typescript
|
|
@@ -85,9 +130,9 @@ var MediaManager = class _MediaManager {
|
|
|
85
130
|
* }
|
|
86
131
|
* };
|
|
87
132
|
*
|
|
88
|
-
* const result = await
|
|
133
|
+
* const result = await langfuse.media.resolveReferences({
|
|
89
134
|
* obj,
|
|
90
|
-
*
|
|
135
|
+
* resolveWith: "base64DataUri"
|
|
91
136
|
* });
|
|
92
137
|
*
|
|
93
138
|
* // Result:
|
|
@@ -298,6 +343,14 @@ mustache.escape = function(text) {
|
|
|
298
343
|
return text;
|
|
299
344
|
};
|
|
300
345
|
var BasePromptClient = class {
|
|
346
|
+
/**
|
|
347
|
+
* Creates a new BasePromptClient instance.
|
|
348
|
+
*
|
|
349
|
+
* @param prompt - The base prompt data
|
|
350
|
+
* @param isFallback - Whether this is fallback content
|
|
351
|
+
* @param type - The prompt type
|
|
352
|
+
* @internal
|
|
353
|
+
*/
|
|
301
354
|
constructor(prompt, isFallback = false, type) {
|
|
302
355
|
this.name = prompt.name;
|
|
303
356
|
this.version = prompt.version;
|
|
@@ -365,14 +418,51 @@ var BasePromptClient = class {
|
|
|
365
418
|
}
|
|
366
419
|
};
|
|
367
420
|
var TextPromptClient = class extends BasePromptClient {
|
|
421
|
+
/**
|
|
422
|
+
* Creates a new TextPromptClient instance.
|
|
423
|
+
*
|
|
424
|
+
* @param prompt - The text prompt data
|
|
425
|
+
* @param isFallback - Whether this is fallback content
|
|
426
|
+
*/
|
|
368
427
|
constructor(prompt, isFallback = false) {
|
|
369
428
|
super(prompt, isFallback, "text");
|
|
370
429
|
this.promptResponse = prompt;
|
|
371
430
|
this.prompt = prompt.prompt;
|
|
372
431
|
}
|
|
432
|
+
/**
|
|
433
|
+
* Compiles the text prompt by substituting variables.
|
|
434
|
+
*
|
|
435
|
+
* Uses Mustache templating to replace {{variable}} placeholders with provided values.
|
|
436
|
+
*
|
|
437
|
+
* @param variables - Key-value pairs for variable substitution
|
|
438
|
+
* @param _placeholders - Ignored for text prompts
|
|
439
|
+
* @returns The compiled text with variables substituted
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* ```typescript
|
|
443
|
+
* const prompt = await langfuse.prompt.get("greeting", { type: "text" });
|
|
444
|
+
* const compiled = prompt.compile({ name: "Alice" });
|
|
445
|
+
* // If prompt is "Hello {{name}}!", result is "Hello Alice!"
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
373
448
|
compile(variables, _placeholders) {
|
|
374
449
|
return mustache.render(this.promptResponse.prompt, variables != null ? variables : {});
|
|
375
450
|
}
|
|
451
|
+
/**
|
|
452
|
+
* Converts the prompt to LangChain PromptTemplate format.
|
|
453
|
+
*
|
|
454
|
+
* Transforms Mustache-style {{variable}} syntax to LangChain's {variable} format.
|
|
455
|
+
*
|
|
456
|
+
* @param _options - Ignored for text prompts
|
|
457
|
+
* @returns The prompt string compatible with LangChain PromptTemplate
|
|
458
|
+
*
|
|
459
|
+
* @example
|
|
460
|
+
* ```typescript
|
|
461
|
+
* const prompt = await langfuse.prompt.get("greeting", { type: "text" });
|
|
462
|
+
* const langchainFormat = prompt.getLangchainPrompt();
|
|
463
|
+
* // Transforms "Hello {{name}}!" to "Hello {name}!"
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
376
466
|
getLangchainPrompt(_options) {
|
|
377
467
|
return this._transformToLangchainVariables(this.prompt);
|
|
378
468
|
}
|
|
@@ -390,6 +480,12 @@ var TextPromptClient = class extends BasePromptClient {
|
|
|
390
480
|
}
|
|
391
481
|
};
|
|
392
482
|
var ChatPromptClient = class _ChatPromptClient extends BasePromptClient {
|
|
483
|
+
/**
|
|
484
|
+
* Creates a new ChatPromptClient instance.
|
|
485
|
+
*
|
|
486
|
+
* @param prompt - The chat prompt data
|
|
487
|
+
* @param isFallback - Whether this is fallback content
|
|
488
|
+
*/
|
|
393
489
|
constructor(prompt, isFallback = false) {
|
|
394
490
|
const normalizedPrompt = _ChatPromptClient.normalizePrompt(prompt.prompt);
|
|
395
491
|
const typedPrompt = {
|
|
@@ -412,6 +508,26 @@ var ChatPromptClient = class _ChatPromptClient extends BasePromptClient {
|
|
|
412
508
|
}
|
|
413
509
|
});
|
|
414
510
|
}
|
|
511
|
+
/**
|
|
512
|
+
* Compiles the chat prompt by replacing placeholders and variables.
|
|
513
|
+
*
|
|
514
|
+
* First resolves placeholders with provided values, then applies variable substitution
|
|
515
|
+
* to message content using Mustache templating. Unresolved placeholders remain
|
|
516
|
+
* as placeholder objects in the output.
|
|
517
|
+
*
|
|
518
|
+
* @param variables - Key-value pairs for Mustache variable substitution in message content
|
|
519
|
+
* @param placeholders - Key-value pairs where keys are placeholder names and values are ChatMessage arrays
|
|
520
|
+
* @returns Array of ChatMessage objects and unresolved placeholder objects
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```typescript
|
|
524
|
+
* const prompt = await langfuse.prompt.get("conversation", { type: "chat" });
|
|
525
|
+
* const compiled = prompt.compile(
|
|
526
|
+
* { user_name: "Alice" },
|
|
527
|
+
* { examples: [{ role: "user", content: "Hello" }, { role: "assistant", content: "Hi!" }] }
|
|
528
|
+
* );
|
|
529
|
+
* ```
|
|
530
|
+
*/
|
|
415
531
|
compile(variables, placeholders) {
|
|
416
532
|
const messagesWithPlaceholdersReplaced = [];
|
|
417
533
|
const placeholderValues = placeholders != null ? placeholders : {};
|
|
@@ -452,6 +568,25 @@ var ChatPromptClient = class _ChatPromptClient extends BasePromptClient {
|
|
|
452
568
|
}
|
|
453
569
|
});
|
|
454
570
|
}
|
|
571
|
+
/**
|
|
572
|
+
* Converts the prompt to LangChain ChatPromptTemplate format.
|
|
573
|
+
*
|
|
574
|
+
* Resolves placeholders with provided values and converts unresolved ones
|
|
575
|
+
* to LangChain MessagesPlaceholder objects. Transforms variables from
|
|
576
|
+
* {{var}} to {var} format without rendering them.
|
|
577
|
+
*
|
|
578
|
+
* @param options - Configuration object
|
|
579
|
+
* @param options.placeholders - Key-value pairs for placeholder resolution
|
|
580
|
+
* @returns Array of ChatMessage objects and LangChain MessagesPlaceholder objects
|
|
581
|
+
*
|
|
582
|
+
* @example
|
|
583
|
+
* ```typescript
|
|
584
|
+
* const prompt = await langfuse.prompt.get("conversation", { type: "chat" });
|
|
585
|
+
* const langchainFormat = prompt.getLangchainPrompt({
|
|
586
|
+
* placeholders: { examples: [{ role: "user", content: "Hello" }] }
|
|
587
|
+
* });
|
|
588
|
+
* ```
|
|
589
|
+
*/
|
|
455
590
|
getLangchainPrompt(options) {
|
|
456
591
|
var _a;
|
|
457
592
|
const messagesWithPlaceholdersReplaced = [];
|
|
@@ -512,6 +647,12 @@ var ChatPromptClient = class _ChatPromptClient extends BasePromptClient {
|
|
|
512
647
|
|
|
513
648
|
// src/prompt/promptManager.ts
|
|
514
649
|
var PromptManager = class {
|
|
650
|
+
/**
|
|
651
|
+
* Creates a new PromptManager instance.
|
|
652
|
+
*
|
|
653
|
+
* @param params - Configuration object containing the API client
|
|
654
|
+
* @internal
|
|
655
|
+
*/
|
|
515
656
|
constructor(params) {
|
|
516
657
|
const { apiClient } = params;
|
|
517
658
|
this.apiClient = apiClient;
|
|
@@ -520,6 +661,35 @@ var PromptManager = class {
|
|
|
520
661
|
get logger() {
|
|
521
662
|
return getGlobalLogger3();
|
|
522
663
|
}
|
|
664
|
+
/**
|
|
665
|
+
* Creates a new prompt in Langfuse.
|
|
666
|
+
*
|
|
667
|
+
* Supports both text and chat prompts. Chat prompts can include placeholders
|
|
668
|
+
* for dynamic content insertion.
|
|
669
|
+
*
|
|
670
|
+
* @param body - The prompt data to create
|
|
671
|
+
* @returns Promise that resolves to the appropriate prompt client
|
|
672
|
+
*
|
|
673
|
+
* @example
|
|
674
|
+
* ```typescript
|
|
675
|
+
* // Create a text prompt
|
|
676
|
+
* const textPrompt = await langfuse.prompt.create({
|
|
677
|
+
* name: "greeting",
|
|
678
|
+
* prompt: "Hello {{name}}!",
|
|
679
|
+
* type: "text"
|
|
680
|
+
* });
|
|
681
|
+
*
|
|
682
|
+
* // Create a chat prompt
|
|
683
|
+
* const chatPrompt = await langfuse.prompt.create({
|
|
684
|
+
* name: "conversation",
|
|
685
|
+
* type: "chat",
|
|
686
|
+
* prompt: [
|
|
687
|
+
* { role: "system", content: "You are a helpful assistant." },
|
|
688
|
+
* { role: "user", content: "{{user_message}}" }
|
|
689
|
+
* ]
|
|
690
|
+
* });
|
|
691
|
+
* ```
|
|
692
|
+
*/
|
|
523
693
|
async create(body) {
|
|
524
694
|
var _a;
|
|
525
695
|
const requestBody = body.type === "chat" ? {
|
|
@@ -544,6 +714,25 @@ var PromptManager = class {
|
|
|
544
714
|
}
|
|
545
715
|
return new TextPromptClient(promptResponse);
|
|
546
716
|
}
|
|
717
|
+
/**
|
|
718
|
+
* Updates the labels of an existing prompt version.
|
|
719
|
+
*
|
|
720
|
+
* @param params - Update parameters
|
|
721
|
+
* @param params.name - Name of the prompt to update
|
|
722
|
+
* @param params.version - Version number of the prompt to update
|
|
723
|
+
* @param params.newLabels - New labels to apply to the prompt version
|
|
724
|
+
*
|
|
725
|
+
* @returns Promise that resolves to the updated prompt
|
|
726
|
+
*
|
|
727
|
+
* @example
|
|
728
|
+
* ```typescript
|
|
729
|
+
* const updatedPrompt = await langfuse.prompt.update({
|
|
730
|
+
* name: "my-prompt",
|
|
731
|
+
* version: 1,
|
|
732
|
+
* newLabels: ["production", "v2"]
|
|
733
|
+
* });
|
|
734
|
+
* ```
|
|
735
|
+
*/
|
|
547
736
|
async update(params) {
|
|
548
737
|
const { name, version, newLabels } = params;
|
|
549
738
|
const newPrompt = await this.apiClient.promptVersion.update(name, version, {
|
|
@@ -552,6 +741,40 @@ var PromptManager = class {
|
|
|
552
741
|
this.cache.invalidate(name);
|
|
553
742
|
return newPrompt;
|
|
554
743
|
}
|
|
744
|
+
/**
|
|
745
|
+
* Retrieves a prompt by name with intelligent caching.
|
|
746
|
+
*
|
|
747
|
+
* This method implements sophisticated caching behavior:
|
|
748
|
+
* - Fresh prompts are returned immediately from cache
|
|
749
|
+
* - Expired prompts are returned from cache while being refreshed in background
|
|
750
|
+
* - Cache misses trigger immediate fetch with optional fallback support
|
|
751
|
+
*
|
|
752
|
+
* @param name - Name of the prompt to retrieve
|
|
753
|
+
* @param options - Optional retrieval configuration
|
|
754
|
+
* @returns Promise that resolves to the appropriate prompt client
|
|
755
|
+
*
|
|
756
|
+
* @example
|
|
757
|
+
* ```typescript
|
|
758
|
+
* // Get latest version with caching
|
|
759
|
+
* const prompt = await langfuse.prompt.get("my-prompt");
|
|
760
|
+
*
|
|
761
|
+
* // Get specific version
|
|
762
|
+
* const v2Prompt = await langfuse.prompt.get("my-prompt", {
|
|
763
|
+
* version: 2
|
|
764
|
+
* });
|
|
765
|
+
*
|
|
766
|
+
* // Get with label filter
|
|
767
|
+
* const prodPrompt = await langfuse.prompt.get("my-prompt", {
|
|
768
|
+
* label: "production"
|
|
769
|
+
* });
|
|
770
|
+
*
|
|
771
|
+
* // Get with fallback
|
|
772
|
+
* const promptWithFallback = await langfuse.prompt.get("my-prompt", {
|
|
773
|
+
* type: "text",
|
|
774
|
+
* fallback: "Hello {{name}}!"
|
|
775
|
+
* });
|
|
776
|
+
* ```
|
|
777
|
+
*/
|
|
555
778
|
async get(name, options) {
|
|
556
779
|
var _a;
|
|
557
780
|
const cacheKey = this.cache.createKey({
|
|
@@ -673,6 +896,12 @@ import { trace } from "@opentelemetry/api";
|
|
|
673
896
|
var MAX_QUEUE_SIZE = 1e5;
|
|
674
897
|
var MAX_BATCH_SIZE = 100;
|
|
675
898
|
var ScoreManager = class {
|
|
899
|
+
/**
|
|
900
|
+
* Creates a new ScoreManager instance.
|
|
901
|
+
*
|
|
902
|
+
* @param params - Configuration object containing the API client
|
|
903
|
+
* @internal
|
|
904
|
+
*/
|
|
676
905
|
constructor(params) {
|
|
677
906
|
this.eventQueue = [];
|
|
678
907
|
this.flushPromise = null;
|
|
@@ -686,6 +915,25 @@ var ScoreManager = class {
|
|
|
686
915
|
get logger() {
|
|
687
916
|
return getGlobalLogger4();
|
|
688
917
|
}
|
|
918
|
+
/**
|
|
919
|
+
* Creates a new score event and adds it to the processing queue.
|
|
920
|
+
*
|
|
921
|
+
* Scores are queued and sent in batches for efficiency. The score will be
|
|
922
|
+
* automatically sent when the queue reaches the flush threshold or after
|
|
923
|
+
* the flush interval expires.
|
|
924
|
+
*
|
|
925
|
+
* @param data - The score data to create
|
|
926
|
+
*
|
|
927
|
+
* @example
|
|
928
|
+
* ```typescript
|
|
929
|
+
* langfuse.score.create({
|
|
930
|
+
* name: "quality",
|
|
931
|
+
* value: 0.85,
|
|
932
|
+
* traceId: "trace-123",
|
|
933
|
+
* comment: "High quality response"
|
|
934
|
+
* });
|
|
935
|
+
* ```
|
|
936
|
+
*/
|
|
689
937
|
create(data) {
|
|
690
938
|
var _a, _b;
|
|
691
939
|
const scoreData = {
|
|
@@ -714,6 +962,26 @@ var ScoreManager = class {
|
|
|
714
962
|
}, this.flushIntervalSeconds * 1e3);
|
|
715
963
|
}
|
|
716
964
|
}
|
|
965
|
+
/**
|
|
966
|
+
* Creates a score for a specific observation using its OpenTelemetry span.
|
|
967
|
+
*
|
|
968
|
+
* This method automatically extracts the trace ID and observation ID from
|
|
969
|
+
* the provided span context.
|
|
970
|
+
*
|
|
971
|
+
* @param observation - Object containing the OpenTelemetry span
|
|
972
|
+
* @param data - Score data (traceId and observationId will be auto-populated)
|
|
973
|
+
*
|
|
974
|
+
* @example
|
|
975
|
+
* ```typescript
|
|
976
|
+
* import { startSpan } from '@langfuse/tracing';
|
|
977
|
+
*
|
|
978
|
+
* const span = startSpan({ name: "my-operation" });
|
|
979
|
+
* langfuse.score.observation(
|
|
980
|
+
* { otelSpan: span },
|
|
981
|
+
* { name: "accuracy", value: 0.92 }
|
|
982
|
+
* );
|
|
983
|
+
* ```
|
|
984
|
+
*/
|
|
717
985
|
observation(observation, data) {
|
|
718
986
|
const { spanId, traceId } = observation.otelSpan.spanContext();
|
|
719
987
|
this.create({
|
|
@@ -722,6 +990,26 @@ var ScoreManager = class {
|
|
|
722
990
|
observationId: spanId
|
|
723
991
|
});
|
|
724
992
|
}
|
|
993
|
+
/**
|
|
994
|
+
* Creates a score for a trace using an OpenTelemetry span.
|
|
995
|
+
*
|
|
996
|
+
* This method automatically extracts the trace ID from the provided
|
|
997
|
+
* span context and creates a trace-level score.
|
|
998
|
+
*
|
|
999
|
+
* @param observation - Object containing the OpenTelemetry span
|
|
1000
|
+
* @param data - Score data (traceId will be auto-populated)
|
|
1001
|
+
*
|
|
1002
|
+
* @example
|
|
1003
|
+
* ```typescript
|
|
1004
|
+
* import { startSpan } from '@langfuse/tracing';
|
|
1005
|
+
*
|
|
1006
|
+
* const span = startSpan({ name: "my-operation" });
|
|
1007
|
+
* langfuse.score.trace(
|
|
1008
|
+
* { otelSpan: span },
|
|
1009
|
+
* { name: "overall_quality", value: 0.88 }
|
|
1010
|
+
* );
|
|
1011
|
+
* ```
|
|
1012
|
+
*/
|
|
725
1013
|
trace(observation, data) {
|
|
726
1014
|
const { traceId } = observation.otelSpan.spanContext();
|
|
727
1015
|
this.create({
|
|
@@ -729,6 +1017,28 @@ var ScoreManager = class {
|
|
|
729
1017
|
traceId
|
|
730
1018
|
});
|
|
731
1019
|
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Creates a score for the currently active observation.
|
|
1022
|
+
*
|
|
1023
|
+
* This method automatically detects the active OpenTelemetry span and
|
|
1024
|
+
* creates an observation-level score. If no active span is found,
|
|
1025
|
+
* a warning is logged and the operation is skipped.
|
|
1026
|
+
*
|
|
1027
|
+
* @param data - Score data (traceId and observationId will be auto-populated)
|
|
1028
|
+
*
|
|
1029
|
+
* @example
|
|
1030
|
+
* ```typescript
|
|
1031
|
+
* import { startActiveSpan } from '@langfuse/tracing';
|
|
1032
|
+
*
|
|
1033
|
+
* startActiveSpan({ name: "my-operation" }, (span) => {
|
|
1034
|
+
* // Inside the active span
|
|
1035
|
+
* langfuse.score.activeObservation({
|
|
1036
|
+
* name: "relevance",
|
|
1037
|
+
* value: 0.95
|
|
1038
|
+
* });
|
|
1039
|
+
* });
|
|
1040
|
+
* ```
|
|
1041
|
+
*/
|
|
732
1042
|
activeObservation(data) {
|
|
733
1043
|
const currentOtelSpan = trace.getActiveSpan();
|
|
734
1044
|
if (!currentOtelSpan) {
|
|
@@ -742,6 +1052,29 @@ var ScoreManager = class {
|
|
|
742
1052
|
observationId: spanId
|
|
743
1053
|
});
|
|
744
1054
|
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Creates a score for the currently active trace.
|
|
1057
|
+
*
|
|
1058
|
+
* This method automatically detects the active OpenTelemetry span and
|
|
1059
|
+
* creates a trace-level score. If no active span is found,
|
|
1060
|
+
* a warning is logged and the operation is skipped.
|
|
1061
|
+
*
|
|
1062
|
+
* @param data - Score data (traceId will be auto-populated)
|
|
1063
|
+
*
|
|
1064
|
+
* @example
|
|
1065
|
+
* ```typescript
|
|
1066
|
+
* import { startActiveSpan } from '@langfuse/tracing';
|
|
1067
|
+
*
|
|
1068
|
+
* startActiveSpan({ name: "my-operation" }, (span) => {
|
|
1069
|
+
* // Inside the active span
|
|
1070
|
+
* langfuse.score.activeTrace({
|
|
1071
|
+
* name: "user_satisfaction",
|
|
1072
|
+
* value: 4,
|
|
1073
|
+
* comment: "User rated 4 out of 5 stars"
|
|
1074
|
+
* });
|
|
1075
|
+
* });
|
|
1076
|
+
* ```
|
|
1077
|
+
*/
|
|
745
1078
|
activeTrace(data) {
|
|
746
1079
|
const currentOtelSpan = trace.getActiveSpan();
|
|
747
1080
|
if (!currentOtelSpan) {
|
|
@@ -781,10 +1114,38 @@ var ScoreManager = class {
|
|
|
781
1114
|
this.flushPromise = null;
|
|
782
1115
|
}
|
|
783
1116
|
}
|
|
1117
|
+
/**
|
|
1118
|
+
* Flushes all pending score events to the Langfuse API.
|
|
1119
|
+
*
|
|
1120
|
+
* This method ensures all queued scores are sent immediately rather than
|
|
1121
|
+
* waiting for the automatic flush interval or batch size threshold.
|
|
1122
|
+
*
|
|
1123
|
+
* @returns Promise that resolves when all pending scores have been sent
|
|
1124
|
+
*
|
|
1125
|
+
* @example
|
|
1126
|
+
* ```typescript
|
|
1127
|
+
* langfuse.score.create({ name: "quality", value: 0.8 });
|
|
1128
|
+
* await langfuse.score.flush(); // Ensures the score is sent immediately
|
|
1129
|
+
* ```
|
|
1130
|
+
*/
|
|
784
1131
|
async flush() {
|
|
785
1132
|
var _a;
|
|
786
1133
|
return (_a = this.flushPromise) != null ? _a : this.handleFlush();
|
|
787
1134
|
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Gracefully shuts down the score manager by flushing all pending scores.
|
|
1137
|
+
*
|
|
1138
|
+
* This method should be called before your application exits to ensure
|
|
1139
|
+
* all score data is sent to Langfuse.
|
|
1140
|
+
*
|
|
1141
|
+
* @returns Promise that resolves when shutdown is complete
|
|
1142
|
+
*
|
|
1143
|
+
* @example
|
|
1144
|
+
* ```typescript
|
|
1145
|
+
* // Before application exit
|
|
1146
|
+
* await langfuse.score.shutdown();
|
|
1147
|
+
* ```
|
|
1148
|
+
*/
|
|
788
1149
|
async shutdown() {
|
|
789
1150
|
await this.flush();
|
|
790
1151
|
}
|
|
@@ -792,6 +1153,26 @@ var ScoreManager = class {
|
|
|
792
1153
|
|
|
793
1154
|
// src/LangfuseClient.ts
|
|
794
1155
|
var LangfuseClient = class {
|
|
1156
|
+
/**
|
|
1157
|
+
* Creates a new LangfuseClient instance.
|
|
1158
|
+
*
|
|
1159
|
+
* @param params - Configuration parameters. If not provided, will use environment variables.
|
|
1160
|
+
*
|
|
1161
|
+
* @throws Will log warnings if required credentials are not provided
|
|
1162
|
+
*
|
|
1163
|
+
* @example
|
|
1164
|
+
* ```typescript
|
|
1165
|
+
* // With explicit configuration
|
|
1166
|
+
* const client = new LangfuseClient({
|
|
1167
|
+
* publicKey: "pk_...",
|
|
1168
|
+
* secretKey: "sk_...",
|
|
1169
|
+
* baseUrl: "https://your-instance.langfuse.com"
|
|
1170
|
+
* });
|
|
1171
|
+
*
|
|
1172
|
+
* // Using environment variables
|
|
1173
|
+
* const client = new LangfuseClient();
|
|
1174
|
+
* ```
|
|
1175
|
+
*/
|
|
795
1176
|
constructor(params) {
|
|
796
1177
|
this.projectId = null;
|
|
797
1178
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
@@ -850,12 +1231,53 @@ var LangfuseClient = class {
|
|
|
850
1231
|
this.fetchMedia = this.api.media.get;
|
|
851
1232
|
this.resolveMediaReferences = this.media.resolveReferences;
|
|
852
1233
|
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Flushes any pending score events to the Langfuse API.
|
|
1236
|
+
*
|
|
1237
|
+
* This method ensures all queued scores are sent immediately rather than
|
|
1238
|
+
* waiting for the automatic flush interval or batch size threshold.
|
|
1239
|
+
*
|
|
1240
|
+
* @returns Promise that resolves when all pending scores have been sent
|
|
1241
|
+
*
|
|
1242
|
+
* @example
|
|
1243
|
+
* ```typescript
|
|
1244
|
+
* langfuse.score.create({ name: "quality", value: 0.8 });
|
|
1245
|
+
* await langfuse.flush(); // Ensures the score is sent immediately
|
|
1246
|
+
* ```
|
|
1247
|
+
*/
|
|
853
1248
|
async flush() {
|
|
854
1249
|
return this.score.flush();
|
|
855
1250
|
}
|
|
1251
|
+
/**
|
|
1252
|
+
* Gracefully shuts down the client by flushing all pending data.
|
|
1253
|
+
*
|
|
1254
|
+
* This method should be called before your application exits to ensure
|
|
1255
|
+
* all data is sent to Langfuse.
|
|
1256
|
+
*
|
|
1257
|
+
* @returns Promise that resolves when shutdown is complete
|
|
1258
|
+
*
|
|
1259
|
+
* @example
|
|
1260
|
+
* ```typescript
|
|
1261
|
+
* // Before application exit
|
|
1262
|
+
* await langfuse.shutdown();
|
|
1263
|
+
* ```
|
|
1264
|
+
*/
|
|
856
1265
|
async shutdown() {
|
|
857
1266
|
return this.score.shutdown();
|
|
858
1267
|
}
|
|
1268
|
+
/**
|
|
1269
|
+
* Generates a URL to view a specific trace in the Langfuse UI.
|
|
1270
|
+
*
|
|
1271
|
+
* @param traceId - The ID of the trace to generate a URL for
|
|
1272
|
+
* @returns Promise that resolves to the trace URL
|
|
1273
|
+
*
|
|
1274
|
+
* @example
|
|
1275
|
+
* ```typescript
|
|
1276
|
+
* const traceId = "trace-123";
|
|
1277
|
+
* const url = await langfuse.getTraceUrl(traceId);
|
|
1278
|
+
* console.log(`View trace at: ${url}`);
|
|
1279
|
+
* ```
|
|
1280
|
+
*/
|
|
859
1281
|
async getTraceUrl(traceId) {
|
|
860
1282
|
let projectId = this.projectId;
|
|
861
1283
|
if (!projectId) {
|