feat: re-implement configuration at client-side

This commit is contained in:
Henry Li
2025-06-11 17:40:12 +08:00
parent 6693d844a7
commit 2ffacd9294
6 changed files with 47 additions and 20 deletions

View File

@@ -8,6 +8,7 @@ import { Geist } from "next/font/google";
import Script from "next/script";
import { ThemeProviderWrapper } from "~/components/deer-flow/theme-provider-wrapper";
import { loadConfig } from "~/core/api/config";
import { env } from "~/env";
import { Toaster } from "../components/deer-flow/toaster";
@@ -24,12 +25,14 @@ const geist = Geist({
variable: "--font-geist-sans",
});
export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{ children: React.ReactNode }>) {
const conf = await loadConfig();
return (
<html lang="en" className={`${geist.variable}`} suppressHydrationWarning>
<head>
<script>{`window.__deerflowConfig = ${JSON.stringify(conf)}`}</script>
{/* Define isSpace function globally to fix markdown-it issues with Next.js + Turbopack
https://github.com/markdown-it/markdown-it/issues/1082#issuecomment-2749656365 */}
<Script id="markdown-it-fix" strategy="beforeInteractive">

View File

@@ -0,0 +1,25 @@
import { type DeerFlowConfig } from "../config/types";
import { resolveServiceURL } from "./resolve-service-url";
declare global {
interface Window {
__deerflowConfig: DeerFlowConfig;
}
}
export async function loadConfig() {
const res = await fetch(resolveServiceURL("./config"));
const config = await res.json();
return config;
}
export function getConfig(): DeerFlowConfig {
if (
typeof window === "undefined" ||
typeof window.__deerflowConfig === "undefined"
) {
throw new Error("Config not loaded");
}
return window.__deerflowConfig;
}

View File

@@ -8,7 +8,7 @@ import { env } from "~/env";
import { useReplay } from "../replay";
import { fetchReplayTitle } from "./chat";
import { getRAGConfig } from "./rag";
import { getConfig } from "./config";
export function useReplayMetadata() {
const { isReplay } = useReplay();
@@ -52,15 +52,8 @@ export function useRAGProvider() {
setLoading(false);
return;
}
getRAGConfig()
.then(setProvider)
.catch((e) => {
setProvider(null);
console.error("Failed to get RAG provider", e);
})
.finally(() => {
setLoading(false);
});
setProvider(getConfig().rag.provider);
setLoading(false);
}, []);
return { provider, loading };

View File

@@ -10,15 +10,7 @@ export function queryRAGResources(query: string) {
.then((res) => {
return res.resources as Array<Resource>;
})
.catch((err) => {
.catch(() => {
return [];
});
}
export function getRAGConfig() {
return fetch(resolveServiceURL(`rag/config`), {
method: "GET",
})
.then((res) => res.json())
.then((res) => res.provider);
}

View File

@@ -0,0 +1 @@
export * from "./types";

View File

@@ -0,0 +1,13 @@
export interface ModelConfig {
basic: string[];
reasoning: string[];
}
export interface RagConfig {
provider: string;
}
export interface DeerFlowConfig {
rag: RagConfig;
models: ModelConfig;
}