mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 06:49:54 +08:00
- Replace litellm with native openai + anthropic SDKs across both services and tutorbot layers - Add 5 new providers: mistral, stepfun, xiaomi_mimo, qianfan, ovms - Add `backend` field to ProviderSpec for SDK routing - New TutorBot OpenAICompatProvider (openai SDK) and AnthropicProvider (anthropic SDK), replacing LiteLLMProvider - Rewrite services/llm/executors.py to use openai SDK directly - Remove litellm from factory.py routing, token_tracker, provider_cmd - Auto-fallback to streaming for tool-call format errors (fixes #265) - Settings UI: provider dropdown with auto base-url fill - Clean up litellm from requirements/cli.txt and pyproject.toml Made-with: Cursor
188 lines
5.0 KiB
TypeScript
188 lines
5.0 KiB
TypeScript
"use client";
|
|
|
|
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
useState,
|
|
} from "react";
|
|
import {
|
|
getStoredTheme,
|
|
getSystemTheme,
|
|
setTheme as applyThemePreference,
|
|
subscribeToThemeChanges,
|
|
type Theme,
|
|
} from "@/lib/theme";
|
|
|
|
export type AppLanguage = "en" | "zh";
|
|
|
|
export const ACTIVE_SESSION_STORAGE_KEY = "deeptutor.activeSessionId.tab";
|
|
export const LANGUAGE_STORAGE_KEY = "deeptutor-language";
|
|
|
|
const ACTIVE_SESSION_EVENT = "deeptutor:active-session";
|
|
const LANGUAGE_EVENT = "deeptutor:language";
|
|
|
|
function normalizeLanguage(value: string | null | undefined): AppLanguage {
|
|
return value === "zh" ? "zh" : "en";
|
|
}
|
|
|
|
export function readStoredLanguage(): AppLanguage {
|
|
if (typeof window === "undefined") return "en";
|
|
try {
|
|
return normalizeLanguage(window.localStorage.getItem(LANGUAGE_STORAGE_KEY));
|
|
} catch {
|
|
return "en";
|
|
}
|
|
}
|
|
|
|
export function writeStoredLanguage(language: AppLanguage): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(LANGUAGE_STORAGE_KEY, language);
|
|
window.dispatchEvent(
|
|
new CustomEvent(LANGUAGE_EVENT, {
|
|
detail: { language },
|
|
}),
|
|
);
|
|
} catch {
|
|
// localStorage may be unavailable
|
|
}
|
|
}
|
|
|
|
export function readStoredActiveSessionId(): string | null {
|
|
if (typeof window === "undefined") return null;
|
|
try {
|
|
return window.sessionStorage.getItem(ACTIVE_SESSION_STORAGE_KEY);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function writeStoredActiveSessionId(sessionId: string | null): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
if (sessionId) {
|
|
window.sessionStorage.setItem(ACTIVE_SESSION_STORAGE_KEY, sessionId);
|
|
} else {
|
|
window.sessionStorage.removeItem(ACTIVE_SESSION_STORAGE_KEY);
|
|
}
|
|
window.dispatchEvent(
|
|
new CustomEvent(ACTIVE_SESSION_EVENT, {
|
|
detail: { sessionId },
|
|
}),
|
|
);
|
|
} catch {
|
|
// sessionStorage may be unavailable
|
|
}
|
|
}
|
|
|
|
interface AppShellContextValue {
|
|
theme: Theme;
|
|
setTheme: (theme: Theme) => void;
|
|
language: AppLanguage;
|
|
setLanguage: (language: AppLanguage) => void;
|
|
activeSessionId: string | null;
|
|
setActiveSessionId: (sessionId: string | null) => void;
|
|
}
|
|
|
|
const AppShellContext = createContext<AppShellContextValue | null>(null);
|
|
|
|
export function AppShellProvider({
|
|
children,
|
|
}: {
|
|
children: React.ReactNode;
|
|
}) {
|
|
const [theme, setThemeState] = useState<Theme>(() => {
|
|
return getStoredTheme() ?? getSystemTheme();
|
|
});
|
|
// Always start with "en" to match SSR; hydrate from localStorage after mount
|
|
const [language, setLanguageState] = useState<AppLanguage>("en");
|
|
const [activeSessionId, setActiveSessionIdState] = useState<string | null>(() =>
|
|
readStoredActiveSessionId(),
|
|
);
|
|
|
|
useEffect(() => {
|
|
setLanguageState(readStoredLanguage());
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
return subscribeToThemeChanges((nextTheme) => {
|
|
setThemeState(nextTheme);
|
|
});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === "undefined") return;
|
|
|
|
const onStorage = (event: StorageEvent) => {
|
|
if (event.key === LANGUAGE_STORAGE_KEY) {
|
|
setLanguageState(normalizeLanguage(event.newValue));
|
|
}
|
|
if (event.key === ACTIVE_SESSION_STORAGE_KEY) {
|
|
setActiveSessionIdState(event.newValue);
|
|
}
|
|
};
|
|
|
|
const onLanguage = (event: Event) => {
|
|
const detail = (event as CustomEvent<{ language?: AppLanguage }>).detail;
|
|
setLanguageState(normalizeLanguage(detail?.language));
|
|
};
|
|
|
|
const onActiveSession = (event: Event) => {
|
|
const detail = (event as CustomEvent<{ sessionId?: string | null }>).detail;
|
|
setActiveSessionIdState(detail?.sessionId ?? null);
|
|
};
|
|
|
|
window.addEventListener("storage", onStorage);
|
|
window.addEventListener(LANGUAGE_EVENT, onLanguage);
|
|
window.addEventListener(ACTIVE_SESSION_EVENT, onActiveSession);
|
|
|
|
return () => {
|
|
window.removeEventListener("storage", onStorage);
|
|
window.removeEventListener(LANGUAGE_EVENT, onLanguage);
|
|
window.removeEventListener(ACTIVE_SESSION_EVENT, onActiveSession);
|
|
};
|
|
}, []);
|
|
|
|
const setTheme = useCallback((nextTheme: Theme) => {
|
|
applyThemePreference(nextTheme);
|
|
setThemeState(nextTheme);
|
|
}, []);
|
|
|
|
const setLanguage = useCallback((nextLanguage: AppLanguage) => {
|
|
writeStoredLanguage(nextLanguage);
|
|
setLanguageState(nextLanguage);
|
|
}, []);
|
|
|
|
const setActiveSessionId = useCallback((sessionId: string | null) => {
|
|
writeStoredActiveSessionId(sessionId);
|
|
setActiveSessionIdState(sessionId);
|
|
}, []);
|
|
|
|
const value = useMemo<AppShellContextValue>(
|
|
() => ({
|
|
theme,
|
|
setTheme,
|
|
language,
|
|
setLanguage,
|
|
activeSessionId,
|
|
setActiveSessionId,
|
|
}),
|
|
[activeSessionId, language, setActiveSessionId, setLanguage, setTheme, theme],
|
|
);
|
|
|
|
return (
|
|
<AppShellContext.Provider value={value}>{children}</AppShellContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useAppShell() {
|
|
const context = useContext(AppShellContext);
|
|
if (!context) {
|
|
throw new Error("useAppShell must be used inside AppShellProvider");
|
|
}
|
|
return context;
|
|
}
|