Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(langfuse-core): cleaner access to process.env and globalThis #43

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading