Skip to content

Commit

Permalink
feat(langfuse-core): cleaner access to process.env and globalThis (
Browse files Browse the repository at this point in the history
  • Loading branch information
charlypoly authored Nov 13, 2023
1 parent c12f969 commit ea0c960
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
16 changes: 0 additions & 16 deletions langfuse-core/src/globalThis.d.ts

This file was deleted.

20 changes: 15 additions & 5 deletions langfuse-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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>("__deferRuntime");
if (deferRuntime) {
deferRuntime.langfuseTraces([
{
id: id,
name: body.name || "",
Expand Down
10 changes: 10 additions & 0 deletions langfuse-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,13 @@ type FixTypes<T> = Omit<
},
"externalId" | "traceIdType"
>;

export type DeferRuntime = {
langfuseTraces: (
traces: {
id: string;
name: string;
url: string;
}[]
) => void;
};
10 changes: 10 additions & 0 deletions langfuse-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@ export function safeSetTimeout(fn: () => void, timeout: number): any {
t?.unref && t?.unref();
return t;
}

export function getEnv<T = string>(key: string): T | undefined {
if (typeof process !== "undefined") {
return process.env[key] as T;
}
if (typeof globalThis !== "undefined") {
return (globalThis as any)[key];
}
return;
}

0 comments on commit ea0c960

Please sign in to comment.