@langfuse/client 4.0.0-alpha.2 → 4.0.0-alpha.3

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.d.ts CHANGED
@@ -1,6 +1,308 @@
1
- export * from "./LangfuseClient.js";
2
- export * from "./prompt/index.js";
3
- export * from "./score/index.js";
4
- export * from "./dataset/index.js";
5
- export * from "./media/index.js";
6
- //# sourceMappingURL=index.d.ts.map
1
+ import * as _langfuse_core from '@langfuse/core';
2
+ import { LangfuseAPIClient, Dataset, DatasetItem, DatasetRunItem, ParsedMediaReference, CreatePromptRequest, ChatMessage, ChatMessageWithPlaceholders, PlaceholderMessage, BasePrompt, Prompt, ScoreBody } from '@langfuse/core';
3
+ import { Span } from '@opentelemetry/api';
4
+
5
+ type LinkDatasetItemFunction = (obj: {
6
+ otelSpan: Span;
7
+ }, runName: string, runArgs?: {
8
+ description?: string;
9
+ metadata?: any;
10
+ }) => Promise<DatasetRunItem>;
11
+ declare class DatasetManager {
12
+ private apiClient;
13
+ constructor(params: {
14
+ apiClient: LangfuseAPIClient;
15
+ });
16
+ get(name: string, options?: {
17
+ fetchItemsPageSize: number;
18
+ }): Promise<Dataset & {
19
+ items: (DatasetItem & {
20
+ link: LinkDatasetItemFunction;
21
+ })[];
22
+ }>;
23
+ private createDatasetItemLinkFunction;
24
+ }
25
+
26
+ type LangfuseMediaResolveMediaReferencesParams<T> = {
27
+ obj: T;
28
+ resolveWith: "base64DataUri";
29
+ maxDepth?: number;
30
+ };
31
+ declare class MediaManager {
32
+ private apiClient;
33
+ constructor(params: {
34
+ apiClient: LangfuseAPIClient;
35
+ });
36
+ /**
37
+ * Replaces the media reference strings in an object with base64 data URIs for the media content.
38
+ *
39
+ * This method recursively traverses an object (up to a maximum depth of 10) looking for media reference strings
40
+ * in the format "@@@langfuseMedia:...@@@". When found, it fetches the actual media content using the provided
41
+ * Langfuse client and replaces the reference string with a base64 data URI.
42
+ *
43
+ * If fetching media content fails for a reference string, a warning is logged and the reference string is left unchanged.
44
+ *
45
+ * @param params - Configuration object
46
+ * @param params.obj - The object to process. Can be a primitive value, array, or nested object
47
+ * @param params.resolveWith - The representation of the media content to replace the media reference string with. Currently only "base64DataUri" is supported.
48
+ * @param params.maxDepth - Optional. Default is 10. The maximum depth to traverse the object.
49
+ *
50
+ * @returns A deep copy of the input object with all media references replaced with base64 data URIs where possible
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * const obj = {
55
+ * image: "@@@langfuseMedia:type=image/jpeg|id=123|source=bytes@@@",
56
+ * nested: {
57
+ * pdf: "@@@langfuseMedia:type=application/pdf|id=456|source=bytes@@@"
58
+ * }
59
+ * };
60
+ *
61
+ * const result = await LangfuseMedia.resolveMediaReferences({
62
+ * obj,
63
+ * langfuseClient
64
+ * });
65
+ *
66
+ * // Result:
67
+ * // {
68
+ * // image: "data:image/jpeg;base64,/9j/4AAQSkZJRg...",
69
+ * // nested: {
70
+ * // pdf: "data:application/pdf;base64,JVBERi0xLjcK..."
71
+ * // }
72
+ * // }
73
+ * ```
74
+ */
75
+ resolveReferences<T>(params: LangfuseMediaResolveMediaReferencesParams<T>): Promise<T>;
76
+ /**
77
+ * Parses a media reference string into a ParsedMediaReference.
78
+ *
79
+ * Example reference string:
80
+ * "@@@langfuseMedia:type=image/jpeg|id=some-uuid|source=base64DataUri@@@"
81
+ *
82
+ * @param referenceString - The reference string to parse.
83
+ * @returns An object with the mediaId, source, and contentType.
84
+ *
85
+ * @throws Error if the reference string is invalid or missing required fields.
86
+ */
87
+ static parseReferenceString(referenceString: string): ParsedMediaReference;
88
+ }
89
+
90
+ declare enum ChatMessageType {
91
+ ChatMessage = "chatmessage",
92
+ Placeholder = "placeholder"
93
+ }
94
+ type ChatMessageOrPlaceholder = ChatMessage | ({
95
+ type: ChatMessageType.Placeholder;
96
+ } & PlaceholderMessage);
97
+ type LangchainMessagesPlaceholder = {
98
+ variableName: string;
99
+ optional?: boolean;
100
+ };
101
+ type CreateChatPromptBodyWithPlaceholders = {
102
+ type: "chat";
103
+ } & Omit<CreatePromptRequest.Chat, "type" | "prompt"> & {
104
+ prompt: (ChatMessage | ChatMessageWithPlaceholders)[];
105
+ };
106
+
107
+ declare abstract class BasePromptClient {
108
+ readonly name: string;
109
+ readonly version: number;
110
+ readonly config: unknown;
111
+ readonly labels: string[];
112
+ readonly tags: string[];
113
+ readonly isFallback: boolean;
114
+ readonly type: "text" | "chat";
115
+ readonly commitMessage: string | null | undefined;
116
+ constructor(prompt: BasePrompt, isFallback: boolean | undefined, type: "text" | "chat");
117
+ abstract get prompt(): string | ChatMessageWithPlaceholders[];
118
+ abstract set prompt(value: string | ChatMessageWithPlaceholders[]);
119
+ abstract compile(variables?: Record<string, string>, placeholders?: Record<string, any>): string | ChatMessage[] | (ChatMessageOrPlaceholder | any)[];
120
+ abstract getLangchainPrompt(options?: {
121
+ placeholders?: Record<string, any>;
122
+ }): string | ChatMessage[] | ChatMessageOrPlaceholder[] | (ChatMessage | LangchainMessagesPlaceholder | any)[];
123
+ protected _transformToLangchainVariables(content: string): string;
124
+ /**
125
+ * Escapes every curly brace that is part of a JSON object by doubling it.
126
+ *
127
+ * A curly brace is considered “JSON-related” when, after skipping any immediate
128
+ * whitespace, the next non-whitespace character is a single (') or double (") quote.
129
+ *
130
+ * Braces that are already doubled (e.g. `{{variable}}` placeholders) are left untouched.
131
+ *
132
+ * @param text - Input string that may contain JSON snippets.
133
+ * @returns The string with JSON-related braces doubled.
134
+ */
135
+ protected escapeJsonForLangchain(text: string): string;
136
+ abstract toJSON(): string;
137
+ }
138
+ declare class TextPromptClient extends BasePromptClient {
139
+ readonly promptResponse: Prompt.Text;
140
+ readonly prompt: string;
141
+ constructor(prompt: Prompt.Text, isFallback?: boolean);
142
+ compile(variables?: Record<string, string>, _placeholders?: Record<string, any>): string;
143
+ getLangchainPrompt(_options?: {
144
+ placeholders?: Record<string, any>;
145
+ }): string;
146
+ toJSON(): string;
147
+ }
148
+ declare class ChatPromptClient extends BasePromptClient {
149
+ readonly promptResponse: Prompt.Chat;
150
+ readonly prompt: ChatMessageWithPlaceholders[];
151
+ constructor(prompt: Prompt.Chat, isFallback?: boolean);
152
+ private static normalizePrompt;
153
+ compile(variables?: Record<string, string>, placeholders?: Record<string, any>): (ChatMessageOrPlaceholder | any)[];
154
+ getLangchainPrompt(options?: {
155
+ placeholders?: Record<string, any>;
156
+ }): (ChatMessage | LangchainMessagesPlaceholder | any)[];
157
+ toJSON(): string;
158
+ }
159
+
160
+ declare class PromptManager {
161
+ private cache;
162
+ private apiClient;
163
+ constructor(params: {
164
+ apiClient: LangfuseAPIClient;
165
+ });
166
+ get logger(): _langfuse_core.Logger;
167
+ create(body: CreateChatPromptBodyWithPlaceholders): Promise<ChatPromptClient>;
168
+ create(body: Omit<CreatePromptRequest.Text, "type"> & {
169
+ type?: "text";
170
+ }): Promise<TextPromptClient>;
171
+ create(body: CreatePromptRequest.Chat): Promise<ChatPromptClient>;
172
+ update(params: {
173
+ name: string;
174
+ version: number;
175
+ newLabels: string[];
176
+ }): Promise<Prompt>;
177
+ get(name: string, options?: {
178
+ version?: number;
179
+ label?: string;
180
+ cacheTtlSeconds?: number;
181
+ fallback?: string;
182
+ maxRetries?: number;
183
+ type?: "text";
184
+ fetchTimeoutMs?: number;
185
+ }): Promise<TextPromptClient>;
186
+ get(name: string, options?: {
187
+ version?: number;
188
+ label?: string;
189
+ cacheTtlSeconds?: number;
190
+ fallback?: ChatMessage[];
191
+ maxRetries?: number;
192
+ type: "chat";
193
+ fetchTimeoutMs?: number;
194
+ }): Promise<ChatPromptClient>;
195
+ private fetchPromptAndUpdateCache;
196
+ }
197
+
198
+ declare class ScoreManager {
199
+ private apiClient;
200
+ private eventQueue;
201
+ private flushPromise;
202
+ private flushTimer;
203
+ private flushAtCount;
204
+ private flushIntervalSeconds;
205
+ constructor(params: {
206
+ apiClient: LangfuseAPIClient;
207
+ });
208
+ get logger(): _langfuse_core.Logger;
209
+ create(data: ScoreBody): void;
210
+ observation(observation: {
211
+ otelSpan: Span;
212
+ }, data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">): void;
213
+ trace(observation: {
214
+ otelSpan: Span;
215
+ }, data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">): void;
216
+ activeObservation(data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">): void;
217
+ activeTrace(data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">): void;
218
+ private handleFlush;
219
+ flush(): Promise<void>;
220
+ shutdown(): Promise<void>;
221
+ }
222
+
223
+ interface LangfuseClientParams {
224
+ publicKey?: string;
225
+ secretKey?: string;
226
+ baseUrl?: string;
227
+ timeout?: number;
228
+ additionalHeaders?: Record<string, string>;
229
+ }
230
+ declare class LangfuseClient {
231
+ api: LangfuseAPIClient;
232
+ prompt: PromptManager;
233
+ dataset: DatasetManager;
234
+ score: ScoreManager;
235
+ media: MediaManager;
236
+ private baseUrl;
237
+ private projectId;
238
+ /**
239
+ * @deprecated Use prompt.get instead
240
+ */
241
+ getPrompt: typeof PromptManager.prototype.get;
242
+ /**
243
+ * @deprecated Use prompt.create instead
244
+ */
245
+ createPrompt: typeof PromptManager.prototype.create;
246
+ /**
247
+ * @deprecated Use prompt.update instead
248
+ */
249
+ updatePrompt: typeof PromptManager.prototype.update;
250
+ /**
251
+ * @deprecated Use dataset.get instead
252
+ */
253
+ getDataset: typeof DatasetManager.prototype.get;
254
+ /**
255
+ * @deprecated Use api.trace.get instead
256
+ */
257
+ fetchTrace: typeof LangfuseAPIClient.prototype.trace.get;
258
+ /**
259
+ * @deprecated Use api.trace.list instead
260
+ */
261
+ fetchTraces: typeof LangfuseAPIClient.prototype.trace.list;
262
+ /**
263
+ * @deprecated Use api.observations.get instead
264
+ */
265
+ fetchObservation: typeof LangfuseAPIClient.prototype.observations.get;
266
+ /**
267
+ * @deprecated Use api.observations.list instead
268
+ */
269
+ fetchObservations: typeof LangfuseAPIClient.prototype.observations.getMany;
270
+ /**
271
+ * @deprecated Use api.sessions.get instead
272
+ */
273
+ fetchSessions: typeof LangfuseAPIClient.prototype.sessions.get;
274
+ /**
275
+ * @deprecated Use api.datasets.getRun instead
276
+ */
277
+ getDatasetRun: typeof LangfuseAPIClient.prototype.datasets.getRun;
278
+ /**
279
+ * @deprecated Use api.datasets.getRuns instead
280
+ */
281
+ getDatasetRuns: typeof LangfuseAPIClient.prototype.datasets.getRuns;
282
+ /**
283
+ * @deprecated Use api.datasets.create instead
284
+ */
285
+ createDataset: typeof LangfuseAPIClient.prototype.datasets.create;
286
+ /**
287
+ * @deprecated Use api.datasetItems.get instead
288
+ */
289
+ getDatasetItem: typeof LangfuseAPIClient.prototype.datasetItems.get;
290
+ /**
291
+ * @deprecated Use api.datasetItems.create instead
292
+ */
293
+ createDatasetItem: typeof LangfuseAPIClient.prototype.datasetItems.create;
294
+ /**
295
+ * @deprecated Use api.media.get instead
296
+ */
297
+ fetchMedia: typeof LangfuseAPIClient.prototype.media.get;
298
+ /**
299
+ * @deprecated Use media.resolveReferences instead
300
+ */
301
+ resolveMediaReferences: typeof MediaManager.prototype.resolveReferences;
302
+ constructor(params?: LangfuseClientParams);
303
+ flush(): Promise<void>;
304
+ shutdown(): Promise<void>;
305
+ getTraceUrl(traceId: string): Promise<string>;
306
+ }
307
+
308
+ export { type ChatMessageOrPlaceholder, ChatMessageType, ChatPromptClient, type CreateChatPromptBodyWithPlaceholders, DatasetManager, type LangchainMessagesPlaceholder, LangfuseClient, type LangfuseClientParams, type LangfuseMediaResolveMediaReferencesParams, type LinkDatasetItemFunction, MediaManager, PromptManager, ScoreManager, TextPromptClient };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langfuse/client",
3
- "version": "4.0.0-alpha.2",
3
+ "version": "4.0.0-alpha.3",
4
4
  "description": "Langfuse API client for universal JavaScript environments",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -21,7 +21,7 @@
21
21
  ],
22
22
  "dependencies": {
23
23
  "mustache": "^4.2.0",
24
- "@langfuse/core": "^4.0.0-alpha.2"
24
+ "@langfuse/core": "^4.0.0-alpha.3"
25
25
  },
26
26
  "peerDependencies": {
27
27
  "@opentelemetry/api": "^1.9.0"
@@ -1,90 +0,0 @@
1
- import { LangfuseAPIClient } from "@langfuse/core";
2
- import { DatasetManager } from "./dataset/index.js";
3
- import { MediaManager } from "./media/index.js";
4
- import { PromptManager } from "./prompt/index.js";
5
- import { ScoreManager } from "./score/index.js";
6
- export interface LangfuseClientParams {
7
- publicKey?: string;
8
- secretKey?: string;
9
- baseUrl?: string;
10
- timeout?: number;
11
- additionalHeaders?: Record<string, string>;
12
- }
13
- export declare class LangfuseClient {
14
- api: LangfuseAPIClient;
15
- prompt: PromptManager;
16
- dataset: DatasetManager;
17
- score: ScoreManager;
18
- media: MediaManager;
19
- private baseUrl;
20
- private projectId;
21
- /**
22
- * @deprecated Use prompt.get instead
23
- */
24
- getPrompt: typeof PromptManager.prototype.get;
25
- /**
26
- * @deprecated Use prompt.create instead
27
- */
28
- createPrompt: typeof PromptManager.prototype.create;
29
- /**
30
- * @deprecated Use prompt.update instead
31
- */
32
- updatePrompt: typeof PromptManager.prototype.update;
33
- /**
34
- * @deprecated Use dataset.get instead
35
- */
36
- getDataset: typeof DatasetManager.prototype.get;
37
- /**
38
- * @deprecated Use api.trace.get instead
39
- */
40
- fetchTrace: typeof LangfuseAPIClient.prototype.trace.get;
41
- /**
42
- * @deprecated Use api.trace.list instead
43
- */
44
- fetchTraces: typeof LangfuseAPIClient.prototype.trace.list;
45
- /**
46
- * @deprecated Use api.observations.get instead
47
- */
48
- fetchObservation: typeof LangfuseAPIClient.prototype.observations.get;
49
- /**
50
- * @deprecated Use api.observations.list instead
51
- */
52
- fetchObservations: typeof LangfuseAPIClient.prototype.observations.getMany;
53
- /**
54
- * @deprecated Use api.sessions.get instead
55
- */
56
- fetchSessions: typeof LangfuseAPIClient.prototype.sessions.get;
57
- /**
58
- * @deprecated Use api.datasets.getRun instead
59
- */
60
- getDatasetRun: typeof LangfuseAPIClient.prototype.datasets.getRun;
61
- /**
62
- * @deprecated Use api.datasets.getRuns instead
63
- */
64
- getDatasetRuns: typeof LangfuseAPIClient.prototype.datasets.getRuns;
65
- /**
66
- * @deprecated Use api.datasets.create instead
67
- */
68
- createDataset: typeof LangfuseAPIClient.prototype.datasets.create;
69
- /**
70
- * @deprecated Use api.datasetItems.get instead
71
- */
72
- getDatasetItem: typeof LangfuseAPIClient.prototype.datasetItems.get;
73
- /**
74
- * @deprecated Use api.datasetItems.create instead
75
- */
76
- createDatasetItem: typeof LangfuseAPIClient.prototype.datasetItems.create;
77
- /**
78
- * @deprecated Use api.media.get instead
79
- */
80
- fetchMedia: typeof LangfuseAPIClient.prototype.media.get;
81
- /**
82
- * @deprecated Use media.resolveReferences instead
83
- */
84
- resolveMediaReferences: typeof MediaManager.prototype.resolveReferences;
85
- constructor(params?: LangfuseClientParams);
86
- flush(): Promise<void>;
87
- shutdown(): Promise<void>;
88
- getTraceUrl(traceId: string): Promise<string>;
89
- }
90
- //# sourceMappingURL=LangfuseClient.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"LangfuseClient.d.ts","sourceRoot":"","sources":["../src/LangfuseClient.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAIlB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAEhD,MAAM,WAAW,oBAAoB;IACnC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC5C;AAED,qBAAa,cAAc;IAClB,GAAG,EAAE,iBAAiB,CAAC;IACvB,MAAM,EAAE,aAAa,CAAC;IACtB,OAAO,EAAE,cAAc,CAAC;IACxB,KAAK,EAAE,YAAY,CAAC;IACpB,KAAK,EAAE,YAAY,CAAC;IAE3B,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,SAAS,CAAuB;IAExC;;OAEG;IACI,SAAS,EAAE,OAAO,aAAa,CAAC,SAAS,CAAC,GAAG,CAAC;IACrD;;OAEG;IACI,YAAY,EAAE,OAAO,aAAa,CAAC,SAAS,CAAC,MAAM,CAAC;IAC3D;;OAEG;IACI,YAAY,EAAE,OAAO,aAAa,CAAC,SAAS,CAAC,MAAM,CAAC;IAC3D;;OAEG;IACI,UAAU,EAAE,OAAO,cAAc,CAAC,SAAS,CAAC,GAAG,CAAC;IACvD;;OAEG;IACI,UAAU,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC;IAChE;;OAEG;IACI,WAAW,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC;IAClE;;OAEG;IACI,gBAAgB,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC;IAC7E;;OAEG;IACI,iBAAiB,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,YAAY,CAAC,OAAO,CAAC;IAClF;;OAEG;IACI,aAAa,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC;IACtE;;OAEG;IACI,aAAa,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC;IACzE;;OAEG;IACI,cAAc,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,QAAQ,CAAC,OAAO,CAAC;IAC3E;;OAEG;IACI,aAAa,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC;IACzE;;OAEG;IACI,cAAc,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,YAAY,CAAC,GAAG,CAAC;IAC3E;;OAEG;IACI,iBAAiB,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,YAAY,CAAC,MAAM,CAAC;IACjF;;OAEG;IACI,UAAU,EAAE,OAAO,iBAAiB,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC;IAChE;;OAEG;IACI,sBAAsB,EAAE,OAAO,YAAY,CAAC,SAAS,CAAC,iBAAiB,CAAC;gBAEnE,MAAM,CAAC,EAAE,oBAAoB;IAiE5B,KAAK;IAIL,QAAQ;IAIR,WAAW,CAAC,OAAO,EAAE,MAAM;CAYzC"}
@@ -1,74 +0,0 @@
1
- import { LangfuseAPIClient, LANGFUSE_SDK_VERSION, getGlobalLogger, getEnv, } from "@langfuse/core";
2
- import { DatasetManager } from "./dataset/index.js";
3
- import { MediaManager } from "./media/index.js";
4
- import { PromptManager } from "./prompt/index.js";
5
- import { ScoreManager } from "./score/index.js";
6
- export class LangfuseClient {
7
- constructor(params) {
8
- var _a, _b, _c, _d, _e, _f, _g;
9
- this.projectId = null;
10
- const logger = getGlobalLogger();
11
- const publicKey = (_a = params === null || params === void 0 ? void 0 : params.publicKey) !== null && _a !== void 0 ? _a : getEnv("LANGFUSE_PUBLIC_KEY");
12
- const secretKey = (_b = params === null || params === void 0 ? void 0 : params.secretKey) !== null && _b !== void 0 ? _b : getEnv("LANGFUSE_SECRET_KEY");
13
- this.baseUrl =
14
- (_e = (_d = (_c = params === null || params === void 0 ? void 0 : params.baseUrl) !== null && _c !== void 0 ? _c : getEnv("LANGFUSE_BASE_URL")) !== null && _d !== void 0 ? _d : getEnv("LANGFUSE_BASEURL")) !== null && _e !== void 0 ? _e : "https://cloud.langfuse.com";
15
- if (!publicKey) {
16
- logger.warn("No public key provided in constructor or as LANGFUSE_PUBLIC_KEY env var. Client operations will fail.");
17
- }
18
- if (!secretKey) {
19
- logger.warn("No secret key provided in constructor or as LANGFUSE_SECRET_KEY env var. Client operations will fail.");
20
- }
21
- const timeoutSeconds = (_f = params === null || params === void 0 ? void 0 : params.timeout) !== null && _f !== void 0 ? _f : Number((_g = getEnv("LANGFUSE_TIMEOUT")) !== null && _g !== void 0 ? _g : 5);
22
- this.api = new LangfuseAPIClient({
23
- baseUrl: this.baseUrl,
24
- username: publicKey,
25
- password: secretKey,
26
- xLangfusePublicKey: publicKey,
27
- xLangfuseSdkVersion: LANGFUSE_SDK_VERSION,
28
- xLangfuseSdkName: "javascript",
29
- environment: "", // noop as baseUrl is set
30
- headers: params === null || params === void 0 ? void 0 : params.additionalHeaders,
31
- });
32
- logger.debug("Initialized LangfuseClient with params:", {
33
- publicKey,
34
- baseUrl: this.baseUrl,
35
- timeoutSeconds,
36
- });
37
- this.prompt = new PromptManager({ apiClient: this.api });
38
- this.dataset = new DatasetManager({ apiClient: this.api });
39
- this.score = new ScoreManager({ apiClient: this.api });
40
- this.media = new MediaManager({ apiClient: this.api });
41
- // Keep v3 compat by exposing old interface
42
- this.getPrompt = this.prompt.get.bind(this.prompt); // keep correct this context for cache access
43
- this.createPrompt = this.prompt.create.bind(this.prompt);
44
- this.updatePrompt = this.prompt.update.bind(this.prompt);
45
- this.getDataset = this.dataset.get;
46
- this.fetchTrace = this.api.trace.get;
47
- this.fetchTraces = this.api.trace.list;
48
- this.fetchObservation = this.api.observations.get;
49
- this.fetchObservations = this.api.observations.getMany;
50
- this.fetchSessions = this.api.sessions.get;
51
- this.getDatasetRun = this.api.datasets.getRun;
52
- this.getDatasetRuns = this.api.datasets.getRuns;
53
- this.createDataset = this.api.datasets.create;
54
- this.getDatasetItem = this.api.datasetItems.get;
55
- this.createDatasetItem = this.api.datasetItems.create;
56
- this.fetchMedia = this.api.media.get;
57
- this.resolveMediaReferences = this.media.resolveReferences;
58
- }
59
- async flush() {
60
- return this.score.flush();
61
- }
62
- async shutdown() {
63
- return this.score.shutdown();
64
- }
65
- async getTraceUrl(traceId) {
66
- let projectId = this.projectId;
67
- if (!projectId) {
68
- projectId = (await this.api.projects.get()).data[0].id;
69
- this.projectId = projectId;
70
- }
71
- const traceUrl = `${this.baseUrl}/project/${projectId}/traces/${traceId}`;
72
- return traceUrl;
73
- }
74
- }
@@ -1,23 +0,0 @@
1
- import { LangfuseAPIClient, Dataset, DatasetRunItem, DatasetItem } from "@langfuse/core";
2
- import { Span } from "@opentelemetry/api";
3
- export type LinkDatasetItemFunction = (obj: {
4
- otelSpan: Span;
5
- }, runName: string, runArgs?: {
6
- description?: string;
7
- metadata?: any;
8
- }) => Promise<DatasetRunItem>;
9
- export declare class DatasetManager {
10
- private apiClient;
11
- constructor(params: {
12
- apiClient: LangfuseAPIClient;
13
- });
14
- get(name: string, options?: {
15
- fetchItemsPageSize: number;
16
- }): Promise<Dataset & {
17
- items: (DatasetItem & {
18
- link: LinkDatasetItemFunction;
19
- })[];
20
- }>;
21
- private createDatasetItemLinkFunction;
22
- }
23
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/dataset/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,OAAO,EACP,cAAc,EACd,WAAW,EACZ,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAE1C,MAAM,MAAM,uBAAuB,GAAG,CACpC,GAAG,EAAE;IAAE,QAAQ,EAAE,IAAI,CAAA;CAAE,EACvB,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;IACR,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,GAAG,CAAC;CAChB,KACE,OAAO,CAAC,cAAc,CAAC,CAAC;AAE7B,qBAAa,cAAc;IACzB,OAAO,CAAC,SAAS,CAAoB;gBAEzB,MAAM,EAAE;QAAE,SAAS,EAAE,iBAAiB,CAAA;KAAE;IAI9C,GAAG,CACP,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE;QACR,kBAAkB,EAAE,MAAM,CAAC;KAC5B,GACA,OAAO,CACR,OAAO,GAAG;QACR,KAAK,EAAE,CAAC,WAAW,GAAG;YAAE,IAAI,EAAE,uBAAuB,CAAA;SAAE,CAAC,EAAE,CAAC;KAC5D,CACF;IAiCD,OAAO,CAAC,6BAA6B;CAsBtC"}
@@ -1,43 +0,0 @@
1
- export class DatasetManager {
2
- constructor(params) {
3
- this.apiClient = params.apiClient;
4
- }
5
- async get(name, options) {
6
- var _a;
7
- const dataset = await this.apiClient.datasets.get(name);
8
- const items = [];
9
- let page = 1;
10
- while (true) {
11
- const itemsResponse = await this.apiClient.datasetItems.list({
12
- datasetName: name,
13
- limit: (_a = options === null || options === void 0 ? void 0 : options.fetchItemsPageSize) !== null && _a !== void 0 ? _a : 50,
14
- page,
15
- });
16
- items.push(...itemsResponse.data);
17
- if (itemsResponse.meta.totalPages <= page) {
18
- break;
19
- }
20
- page++;
21
- }
22
- const returnDataset = {
23
- ...dataset,
24
- items: items.map((item) => ({
25
- ...item,
26
- link: this.createDatasetItemLinkFunction(item),
27
- })),
28
- };
29
- return returnDataset;
30
- }
31
- createDatasetItemLinkFunction(item) {
32
- const linkFunction = async (obj, runName, runArgs) => {
33
- return await this.apiClient.datasetRunItems.create({
34
- runName,
35
- datasetItemId: item.id,
36
- traceId: obj.otelSpan.spanContext().traceId,
37
- runDescription: runArgs === null || runArgs === void 0 ? void 0 : runArgs.description,
38
- metadata: runArgs === null || runArgs === void 0 ? void 0 : runArgs.metadata,
39
- });
40
- };
41
- return linkFunction;
42
- }
43
- }
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC"}
package/dist/index.js DELETED
@@ -1,5 +0,0 @@
1
- export * from "./LangfuseClient.js";
2
- export * from "./prompt/index.js";
3
- export * from "./score/index.js";
4
- export * from "./dataset/index.js";
5
- export * from "./media/index.js";
@@ -1,65 +0,0 @@
1
- import { LangfuseAPIClient, ParsedMediaReference } from "@langfuse/core";
2
- export type LangfuseMediaResolveMediaReferencesParams<T> = {
3
- obj: T;
4
- resolveWith: "base64DataUri";
5
- maxDepth?: number;
6
- };
7
- export declare class MediaManager {
8
- private apiClient;
9
- constructor(params: {
10
- apiClient: LangfuseAPIClient;
11
- });
12
- /**
13
- * Replaces the media reference strings in an object with base64 data URIs for the media content.
14
- *
15
- * This method recursively traverses an object (up to a maximum depth of 10) looking for media reference strings
16
- * in the format "@@@langfuseMedia:...@@@". When found, it fetches the actual media content using the provided
17
- * Langfuse client and replaces the reference string with a base64 data URI.
18
- *
19
- * If fetching media content fails for a reference string, a warning is logged and the reference string is left unchanged.
20
- *
21
- * @param params - Configuration object
22
- * @param params.obj - The object to process. Can be a primitive value, array, or nested object
23
- * @param params.resolveWith - The representation of the media content to replace the media reference string with. Currently only "base64DataUri" is supported.
24
- * @param params.maxDepth - Optional. Default is 10. The maximum depth to traverse the object.
25
- *
26
- * @returns A deep copy of the input object with all media references replaced with base64 data URIs where possible
27
- *
28
- * @example
29
- * ```typescript
30
- * const obj = {
31
- * image: "@@@langfuseMedia:type=image/jpeg|id=123|source=bytes@@@",
32
- * nested: {
33
- * pdf: "@@@langfuseMedia:type=application/pdf|id=456|source=bytes@@@"
34
- * }
35
- * };
36
- *
37
- * const result = await LangfuseMedia.resolveMediaReferences({
38
- * obj,
39
- * langfuseClient
40
- * });
41
- *
42
- * // Result:
43
- * // {
44
- * // image: "data:image/jpeg;base64,/9j/4AAQSkZJRg...",
45
- * // nested: {
46
- * // pdf: "data:application/pdf;base64,JVBERi0xLjcK..."
47
- * // }
48
- * // }
49
- * ```
50
- */
51
- resolveReferences<T>(params: LangfuseMediaResolveMediaReferencesParams<T>): Promise<T>;
52
- /**
53
- * Parses a media reference string into a ParsedMediaReference.
54
- *
55
- * Example reference string:
56
- * "@@@langfuseMedia:type=image/jpeg|id=some-uuid|source=base64DataUri@@@"
57
- *
58
- * @param referenceString - The reference string to parse.
59
- * @returns An object with the mediaId, source, and contentType.
60
- *
61
- * @throws Error if the reference string is invalid or missing required fields.
62
- */
63
- static parseReferenceString(referenceString: string): ParsedMediaReference;
64
- }
65
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/media/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EAIrB,MAAM,gBAAgB,CAAC;AAExB,MAAM,MAAM,yCAAyC,CAAC,CAAC,IAAI;IACzD,GAAG,EAAE,CAAC,CAAC;IACP,WAAW,EAAE,eAAe,CAAC;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,qBAAa,YAAY;IACvB,OAAO,CAAC,SAAS,CAAoB;gBAEzB,MAAM,EAAE;QAAE,SAAS,EAAE,iBAAiB,CAAA;KAAE;IAIpD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAsCG;IACU,iBAAiB,CAAC,CAAC,EAC9B,MAAM,EAAE,yCAAyC,CAAC,CAAC,CAAC,GACnD,OAAO,CAAC,CAAC,CAAC;IA4Fb;;;;;;;;;;OAUG;WACW,oBAAoB,CAChC,eAAe,EAAE,MAAM,GACtB,oBAAoB;CAoCxB"}