mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 13:59:25 +08:00
* feat: separate UI and model response languages * refactor(settings): keep one owner for the interface.json language rules The legacy migration — a file written before the UI/response split inherits its single language into both fields — had been duplicated into both readers of interface.json, as had the theme/language defaults. Two copies of a migration rule drift. Move the rule into resolve_languages() in the module that owns the file, have the router compose its superset of defaults on top of the service's, and cover the agreement with a test that reads one legacy file through both paths. The helper also drops the separate key-presence check: _normalize_language already falls back to its default for missing, null and unrecognized values alike. * fix: keep the language bootstrap single and local-choice-first Merging dev surfaced three collisions this branch had not seen: - The new bootstrap fetched /settings/ui unconditionally and overwrote writeStoredLanguage on every mount, undoing #730's rule that a choice made in this browser is the more specific signal and must win. Folded it into the existing guarded effect so one fetch serves both language fields and neither can clobber a local selection. - apiFetch/apiUrl ended up imported twice in AppShellContext, which git merged without a conflict and tsc rejects outright. - Three tests still stubbed get_ui_language at call sites that now ask for get_response_language: the question router's stand-in module and two mastery_path patches (renamed the zh case to say response language). * fix(api): include response_language in the pre-session UI read The bootstrap adopts both languages from this one endpoint, so leaving response_language out of the projection meant a fresh browser always inherited the interface locale and never saw the saved output-language preference. It belongs with the other two: all three are the visitor's own presentation choices, none describe the deployment. --------- Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
310 lines
9.1 KiB
TypeScript
310 lines
9.1 KiB
TypeScript
"use client";
|
|
|
|
export type AppLanguage = "en" | "zh";
|
|
|
|
export const ACTIVE_SESSION_STORAGE_KEY = "deeptutor.activeSessionId.tab";
|
|
export const LANGUAGE_STORAGE_KEY = "deeptutor-language";
|
|
export const RESPONSE_LANGUAGE_STORAGE_KEY = "deeptutor-response-language";
|
|
export const SIDEBAR_COLLAPSED_STORAGE_KEY = "deeptutor.sidebarCollapsed";
|
|
export const CHAT_RESPONSE_TIMEOUT_STORAGE_KEY =
|
|
"deeptutor.chatResponseTimeout";
|
|
export const CODE_BLOCK_THEME_STORAGE_KEY = "deeptutor.code-block-theme";
|
|
export const CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY =
|
|
"deeptutor.code-block-show-line-numbers";
|
|
export const CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY =
|
|
"deeptutor.code-block-wrap-long-lines";
|
|
|
|
// Mirror of the per-user ``chat_response_timeout`` UI preference. Cached in
|
|
// localStorage so the chat watchdog (a separate provider from Settings) can
|
|
// read it synchronously without its own fetch. Kept in sync on settings load.
|
|
export const DEFAULT_CHAT_RESPONSE_TIMEOUT_SECONDS = 180;
|
|
export const MIN_CHAT_RESPONSE_TIMEOUT_SECONDS = 30;
|
|
export const MAX_CHAT_RESPONSE_TIMEOUT_SECONDS = 1800;
|
|
|
|
export function clampChatResponseTimeout(seconds: number): number {
|
|
if (!Number.isFinite(seconds)) return DEFAULT_CHAT_RESPONSE_TIMEOUT_SECONDS;
|
|
return Math.min(
|
|
MAX_CHAT_RESPONSE_TIMEOUT_SECONDS,
|
|
Math.max(MIN_CHAT_RESPONSE_TIMEOUT_SECONDS, Math.round(seconds)),
|
|
);
|
|
}
|
|
|
|
export function readStoredChatResponseTimeout(): number {
|
|
if (typeof window === "undefined")
|
|
return DEFAULT_CHAT_RESPONSE_TIMEOUT_SECONDS;
|
|
try {
|
|
const raw = window.localStorage.getItem(CHAT_RESPONSE_TIMEOUT_STORAGE_KEY);
|
|
const parsed = raw ? Number.parseInt(raw, 10) : NaN;
|
|
return Number.isFinite(parsed) && parsed > 0
|
|
? clampChatResponseTimeout(parsed)
|
|
: DEFAULT_CHAT_RESPONSE_TIMEOUT_SECONDS;
|
|
} catch {
|
|
return DEFAULT_CHAT_RESPONSE_TIMEOUT_SECONDS;
|
|
}
|
|
}
|
|
|
|
export function writeStoredChatResponseTimeout(seconds: number): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(
|
|
CHAT_RESPONSE_TIMEOUT_STORAGE_KEY,
|
|
String(clampChatResponseTimeout(seconds)),
|
|
);
|
|
} catch {
|
|
// localStorage may be unavailable
|
|
}
|
|
}
|
|
|
|
export const ACTIVE_SESSION_EVENT = "deeptutor:active-session";
|
|
export const LANGUAGE_EVENT = "deeptutor:language";
|
|
export const RESPONSE_LANGUAGE_EVENT = "deeptutor:response-language";
|
|
export const SIDEBAR_COLLAPSED_EVENT = "deeptutor:sidebar-collapsed";
|
|
export const CODE_BLOCK_SETTINGS_EVENT = "deeptutor:code-block-settings";
|
|
|
|
export function normalizeLanguage(
|
|
value: string | null | undefined,
|
|
): AppLanguage {
|
|
return value === "zh" ? "zh" : "en";
|
|
}
|
|
|
|
export function resolveResponseLanguage(
|
|
value: string | null | undefined,
|
|
legacyLanguage: string | null | undefined = "en",
|
|
): AppLanguage {
|
|
return value === "zh" || value === "en"
|
|
? value
|
|
: normalizeLanguage(legacyLanguage);
|
|
}
|
|
|
|
export function readStoredLanguage(): AppLanguage {
|
|
if (typeof window === "undefined") return "en";
|
|
try {
|
|
return normalizeLanguage(window.localStorage.getItem(LANGUAGE_STORAGE_KEY));
|
|
} catch {
|
|
return "en";
|
|
}
|
|
}
|
|
|
|
/** Whether this browser has ever recorded a choice.
|
|
*
|
|
* ``readStoredLanguage`` cannot answer this: it normalizes a missing value to
|
|
* "en", which is indistinguishable from an explicit English selection. The
|
|
* bootstrap needs the difference — it may only consult the server-side
|
|
* preference when the browser has no choice of its own to honour.
|
|
*/
|
|
export function hasStoredLanguage(): boolean {
|
|
if (typeof window === "undefined") return false;
|
|
try {
|
|
return window.localStorage.getItem(LANGUAGE_STORAGE_KEY) !== null;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
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 readStoredResponseLanguage(): AppLanguage {
|
|
if (typeof window === "undefined") return "en";
|
|
try {
|
|
return resolveResponseLanguage(
|
|
window.localStorage.getItem(RESPONSE_LANGUAGE_STORAGE_KEY),
|
|
window.localStorage.getItem(LANGUAGE_STORAGE_KEY),
|
|
);
|
|
} catch {
|
|
return "en";
|
|
}
|
|
}
|
|
|
|
export function writeStoredResponseLanguage(language: AppLanguage): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(RESPONSE_LANGUAGE_STORAGE_KEY, language);
|
|
window.dispatchEvent(
|
|
new CustomEvent(RESPONSE_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
|
|
}
|
|
}
|
|
|
|
export function readStoredSidebarCollapsed(): boolean {
|
|
if (typeof window === "undefined") return false;
|
|
try {
|
|
return window.localStorage.getItem(SIDEBAR_COLLAPSED_STORAGE_KEY) === "1";
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function writeStoredSidebarCollapsed(collapsed: boolean): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(
|
|
SIDEBAR_COLLAPSED_STORAGE_KEY,
|
|
collapsed ? "1" : "0",
|
|
);
|
|
window.dispatchEvent(
|
|
new CustomEvent(SIDEBAR_COLLAPSED_EVENT, {
|
|
detail: { collapsed },
|
|
}),
|
|
);
|
|
} catch {
|
|
// localStorage may be unavailable
|
|
}
|
|
}
|
|
|
|
// Code block settings defaults
|
|
export const DEFAULT_CODE_BLOCK_THEME = "oneDark";
|
|
export const DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS = false;
|
|
export const DEFAULT_CODE_BLOCK_WRAP_LONG_LINES = false;
|
|
|
|
export function normalizeCodeBlockTheme(
|
|
value: string | null | undefined,
|
|
): string {
|
|
if (!value || value.trim() === "") return DEFAULT_CODE_BLOCK_THEME;
|
|
return value.trim();
|
|
}
|
|
|
|
export function readStoredCodeBlockTheme(): string {
|
|
if (typeof window === "undefined") return DEFAULT_CODE_BLOCK_THEME;
|
|
try {
|
|
const raw = window.localStorage.getItem(CODE_BLOCK_THEME_STORAGE_KEY);
|
|
return normalizeCodeBlockTheme(raw);
|
|
} catch {
|
|
return DEFAULT_CODE_BLOCK_THEME;
|
|
}
|
|
}
|
|
|
|
export function writeStoredCodeBlockTheme(theme: string): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(
|
|
CODE_BLOCK_THEME_STORAGE_KEY,
|
|
normalizeCodeBlockTheme(theme),
|
|
);
|
|
window.dispatchEvent(
|
|
new CustomEvent(CODE_BLOCK_SETTINGS_EVENT, {
|
|
detail: { codeBlockTheme: normalizeCodeBlockTheme(theme) },
|
|
}),
|
|
);
|
|
} catch {
|
|
// localStorage may be unavailable
|
|
}
|
|
}
|
|
|
|
export function normalizeCodeBlockShowLineNumbers(
|
|
value: string | null | undefined,
|
|
): boolean {
|
|
if (value === "true") return true;
|
|
if (value === "false") return false;
|
|
return DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS;
|
|
}
|
|
|
|
export function readStoredCodeBlockShowLineNumbers(): boolean {
|
|
if (typeof window === "undefined")
|
|
return DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS;
|
|
try {
|
|
const raw = window.localStorage.getItem(
|
|
CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,
|
|
);
|
|
return normalizeCodeBlockShowLineNumbers(raw);
|
|
} catch {
|
|
return DEFAULT_CODE_BLOCK_SHOW_LINE_NUMBERS;
|
|
}
|
|
}
|
|
|
|
export function writeStoredCodeBlockShowLineNumbers(show: boolean): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(
|
|
CODE_BLOCK_SHOW_LINE_NUMBERS_STORAGE_KEY,
|
|
String(show),
|
|
);
|
|
window.dispatchEvent(
|
|
new CustomEvent(CODE_BLOCK_SETTINGS_EVENT, {
|
|
detail: { codeBlockShowLineNumbers: show },
|
|
}),
|
|
);
|
|
} catch {
|
|
// localStorage may be unavailable
|
|
}
|
|
}
|
|
|
|
export function normalizeCodeBlockWrapLongLines(
|
|
value: string | null | undefined,
|
|
): boolean {
|
|
if (value === "true") return true;
|
|
if (value === "false") return false;
|
|
return DEFAULT_CODE_BLOCK_WRAP_LONG_LINES;
|
|
}
|
|
|
|
export function readStoredCodeBlockWrapLongLines(): boolean {
|
|
if (typeof window === "undefined") return DEFAULT_CODE_BLOCK_WRAP_LONG_LINES;
|
|
try {
|
|
const raw = window.localStorage.getItem(
|
|
CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,
|
|
);
|
|
return normalizeCodeBlockWrapLongLines(raw);
|
|
} catch {
|
|
return DEFAULT_CODE_BLOCK_WRAP_LONG_LINES;
|
|
}
|
|
}
|
|
|
|
export function writeStoredCodeBlockWrapLongLines(wrap: boolean): void {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(
|
|
CODE_BLOCK_WRAP_LONG_LINES_STORAGE_KEY,
|
|
String(wrap),
|
|
);
|
|
window.dispatchEvent(
|
|
new CustomEvent(CODE_BLOCK_SETTINGS_EVENT, {
|
|
detail: { codeBlockWrapLongLines: wrap },
|
|
}),
|
|
);
|
|
} catch {
|
|
// localStorage may be unavailable
|
|
}
|
|
}
|