From f3f66ee9248e832cc9e76fa12e70113731ca33f5 Mon Sep 17 00:00:00 2001 From: Henry Li Date: Mon, 19 Jan 2026 11:23:40 +0800 Subject: [PATCH] feat: add NEXT_PUBLIC_BACKEND_BASE_URL --- frontend/.env.example | 3 +++ frontend/src/core/api/api-client.ts | 10 +++------- frontend/src/core/artifacts/utils.ts | 3 ++- frontend/src/core/config/index.ts | 5 +++++ frontend/src/env.js | 8 ++++++-- 5 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 frontend/src/core/config/index.ts diff --git a/frontend/.env.example b/frontend/.env.example index 3f5fac5..f1d671f 100644 --- a/frontend/.env.example +++ b/frontend/.env.example @@ -9,6 +9,9 @@ # When adding additional environment variables, the schema in "/src/env.js" # should be updated accordingly. +# The base URL of DeerFlow's backend service +NEXT_PUBLIC_BACKEND_BASE_URL="http://localhost:8000" + # Better Auth ## Better Auth - Google OAuth BETTER_AUTH_GOOGLE_CLIENT_ID="" diff --git a/frontend/src/core/api/api-client.ts b/frontend/src/core/api/api-client.ts index e8197a9..0672d10 100644 --- a/frontend/src/core/api/api-client.ts +++ b/frontend/src/core/api/api-client.ts @@ -2,16 +2,12 @@ import { Client as LangGraphClient } from "@langchain/langgraph-sdk/client"; +import { getBackendBaseURL } from "../config"; + let _singleton: LangGraphClient | null = null; export function getAPIClient(): LangGraphClient { - let url: URL | null = null; - if (typeof window === "undefined") { - url = new URL("/api/langgraph", "http://localhost:3000"); - } else { - url = new URL("/api/langgraph", window.location.origin); - } _singleton ??= new LangGraphClient({ - apiUrl: "http://localhost:2024", + apiUrl: getBackendBaseURL(), }); return _singleton; } diff --git a/frontend/src/core/artifacts/utils.ts b/frontend/src/core/artifacts/utils.ts index 8f1eccd..5dc03e3 100644 --- a/frontend/src/core/artifacts/utils.ts +++ b/frontend/src/core/artifacts/utils.ts @@ -1,3 +1,4 @@ +import { getBackendBaseURL } from "../config"; import type { AgentThread } from "../threads"; export function urlOfArtifact({ @@ -9,7 +10,7 @@ export function urlOfArtifact({ threadId: string; download?: boolean; }) { - return `http://localhost:8000/api/threads/${threadId}/artifacts${filepath}${download ? "?download=true" : ""}`; + return `${getBackendBaseURL()}/api/threads/${threadId}/artifacts${filepath}${download ? "?download=true" : ""}`; } export function extractArtifactsFromThread(thread: AgentThread) { diff --git a/frontend/src/core/config/index.ts b/frontend/src/core/config/index.ts new file mode 100644 index 0000000..a9cfba0 --- /dev/null +++ b/frontend/src/core/config/index.ts @@ -0,0 +1,5 @@ +import { env } from "@/env"; + +export function getBackendBaseURL() { + return env.NEXT_PUBLIC_BACKEND_BASE_URL ?? "http://localhost:8000"; +} diff --git a/frontend/src/env.js b/frontend/src/env.js index 52a4bbe..ebff7e1 100644 --- a/frontend/src/env.js +++ b/frontend/src/env.js @@ -24,7 +24,10 @@ export const env = createEnv({ * `NEXT_PUBLIC_`. */ client: { - // NEXT_PUBLIC_CLIENTVAR: z.string(), + NEXT_PUBLIC_BACKEND_BASE_URL: z + .string() + .optional() + .default("http://localhost:8000/api"), }, /** @@ -37,7 +40,8 @@ export const env = createEnv({ BETTER_AUTH_GITHUB_CLIENT_SECRET: process.env.BETTER_AUTH_GITHUB_CLIENT_SECRET, NODE_ENV: process.env.NODE_ENV, - // NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR, + + NEXT_PUBLIC_BACKEND_BASE_URL: process.env.NEXT_PUBLIC_BACKEND_BASE_URL, }, /** * Run `build` or `dev` with `SKIP_ENV_VALIDATION` to skip env validation. This is especially