diff --git a/langfuse-core/src/globalThis.d.ts b/langfuse-core/src/globalThis.d.ts deleted file mode 100644 index 17d0033e..00000000 --- a/langfuse-core/src/globalThis.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -export {}; - -declare interface DeferLangFuseTrace { - id: string; - name: string; - url: string; -} - -declare global { - namespace globalThis { - // eslint-disable-next-line no-var - var __deferRuntime: { - langfuseTraces: (traces: DeferLangFuseTrace[]) => void; - }; - } -} diff --git a/langfuse-core/src/index.ts b/langfuse-core/src/index.ts index 3d61732e..2a17f697 100644 --- a/langfuse-core/src/index.ts +++ b/langfuse-core/src/index.ts @@ -23,8 +23,17 @@ import { type CreateLangfuseDatasetItemResponse, type GetLangfuseDatasetRunResponse, type GetLangfuseDatasetRunParams, + type DeferRuntime, } from "./types"; -import { assert, generateUUID, removeTrailingSlash, retriable, type RetriableOptions, safeSetTimeout } from "./utils"; +import { + assert, + generateUUID, + removeTrailingSlash, + retriable, + type RetriableOptions, + safeSetTimeout, + getEnv, +} from "./utils"; export * as utils from "./utils"; import { SimpleEventEmitter } from "./eventemitter"; import { getCommonReleaseEnvs } from "./release-env"; @@ -85,7 +94,7 @@ abstract class LangfuseCoreStateless { this.baseUrl = removeTrailingSlash(options?.baseUrl || "https://cloud.langfuse.com"); this.flushAt = options?.flushAt ? Math.max(options?.flushAt, 1) : 1; this.flushInterval = options?.flushInterval ?? 10000; - this.release = options?.release ?? process.env.LANGFUSE_RELEASE ?? getCommonReleaseEnvs() ?? undefined; + this.release = options?.release ?? getEnv("LANGFUSE_RELEASE") ?? getCommonReleaseEnvs() ?? undefined; this._retryOptions = { retryCount: options?.fetchRetryCount ?? 3, @@ -453,10 +462,11 @@ export abstract class LangfuseCore extends LangfuseCoreStateless { trace(body?: CreateLangfuseTraceBody): LangfuseTraceClient { const id = this.traceStateless(body ?? {}); const t = new LangfuseTraceClient(this, id); - if (process.env.DEFER && body) { + if (getEnv("DEFER") && body) { try { - if (globalThis.__deferRuntime) { - __deferRuntime.langfuseTraces([ + const deferRuntime = getEnv("__deferRuntime"); + if (deferRuntime) { + deferRuntime.langfuseTraces([ { id: id, name: body.name || "", diff --git a/langfuse-core/src/types.ts b/langfuse-core/src/types.ts index 9149891d..a6958e3c 100644 --- a/langfuse-core/src/types.ts +++ b/langfuse-core/src/types.ts @@ -141,3 +141,13 @@ type FixTypes = Omit< }, "externalId" | "traceIdType" >; + +export type DeferRuntime = { + langfuseTraces: ( + traces: { + id: string; + name: string; + url: string; + }[] + ) => void; +}; diff --git a/langfuse-core/src/utils.ts b/langfuse-core/src/utils.ts index 13389fdc..a3a15594 100644 --- a/langfuse-core/src/utils.ts +++ b/langfuse-core/src/utils.ts @@ -81,3 +81,13 @@ export function safeSetTimeout(fn: () => void, timeout: number): any { t?.unref && t?.unref(); return t; } + +export function getEnv(key: string): T | undefined { + if (typeof process !== "undefined") { + return process.env[key] as T; + } + if (typeof globalThis !== "undefined") { + return (globalThis as any)[key]; + } + return; +}