Files
DeepTutor/web/lib/quiz-judge.ts
Ruslan 4c6498b72a feat(i18n): add Ukrainian as a full interface and output language
Adds `uk` alongside `en` and `zh`: a complete `web/locales/uk/app.json`
(4009 keys), a Ukrainian CLI label set (102 keys), a Ukrainian quiz-judge
prompt, and the language directive that tells the model to answer in it.

Most of the diff is not the translation — it is the six independent
places that each decided, on their own, which languages exist. Every one
of them collapsed an unknown code to English *silently*, so picking
Ukrainian in Settings produced an English UI, English AI feedback and an
English chat reply, with nothing logged anywhere:

  * `services/settings/interface_settings._normalize_language` — the
    canonical normalizer; `interface.json` stored `uk`, the API returned
    `en`, and it looked like the setting had failed to save.
  * `agents/loop/prompt_blocks` — conflated two different things in one
    attribute: which prompt ASSETS exist (yaml ships `en`/`zh` only) and
    which language to ANSWER in (any). Split into `language` and
    `output_language`; the directive now takes the latter.
  * `api/routers/quiz_judge` — the supported set is now derived from
    `_JUDGE_SYSTEM_PROMPTS`, so a language cannot be advertised without a
    prompt or shipped without being advertised.
  * `web/i18n/init` — `normalizeLanguage`/`ensureLanguage`, plus a new
    exported `APP_LANGUAGES` that every picker maps over. The Settings
    toggle inlined `["en", "zh"]`, so a new locale was invisible there
    even once it existed.
  * `web/context/app-shell-storage` — held a second, private copy of the
    `AppLanguage` union; re-exported from `i18n/init` instead.
  * `runtime/banner` and `services/config/settings_spec` — the CLI wizard
    labels and the Settings language list.

Tool hints stay a per-language map from the API where only `en` is
guaranteed, so `ToolsSettingsSection` falls back instead of indexing a
missing key. Backend prompt yaml is still `en`/`zh`; Ukrainian uses the
English scaffolding with a Ukrainian output directive, which is what the
directive is for.

`tests/i18n/test_ukrainian_locale.py` pins each seam — locale key parity,
placeholder integrity, judge/prompt/normalizer coverage, and that the
Settings toggle maps over `APP_LANGUAGES` rather than a literal — so the
next language fails in CI rather than quietly in the UI.
2026-09-09 22:16:00 +03:00

158 lines
3.7 KiB
TypeScript

import { wsUrl } from "@/lib/api";
export interface QuizJudgeImage {
/** Base64 of the freshly-picked image (no ``data:`` prefix). */
base64: string | null;
/** AttachmentStore URL for a previously persisted image. */
url: string | null;
filename: string;
mime_type: string;
}
export interface QuizJudgeRequest {
question: string;
question_type: string;
options: Record<string, string> | null;
correct_answer: string;
explanation: string;
user_answer: string;
/** Multi-image list — backend builds a multimodal user message from this. */
user_answer_images: QuizJudgeImage[];
language: string;
}
export interface QuizJudgeHandle {
close: () => void;
cancel: () => void;
}
export interface QuizJudgeHandlers {
onStart?: () => void;
onChunk: (chunk: string) => void;
onDone: (finalText: string) => void;
onError: (message: string) => void;
}
export function startQuizJudge(
payload: QuizJudgeRequest,
handlers: QuizJudgeHandlers,
): QuizJudgeHandle {
const ws = new WebSocket(wsUrl("/ws/questions/judge"));
let buffer = "";
let finished = false;
let idleTimer: ReturnType<typeof setTimeout> | undefined;
const clearIdleTimer = () => {
if (idleTimer) clearTimeout(idleTimer);
idleTimer = undefined;
};
const armIdleTimer = () => {
clearIdleTimer();
idleTimer = setTimeout(() => {
finalize("error", "AI judge timed out. Please try again.");
}, 2 * 60_000);
};
const finalize = (kind: "done" | "error", message?: string) => {
if (finished) return;
finished = true;
clearIdleTimer();
if (kind === "done") {
handlers.onDone(buffer);
} else {
handlers.onError(message ?? "AI judge failed.");
}
try {
ws.close();
} catch {
/* ignore */
}
};
ws.onopen = () => {
armIdleTimer();
try {
ws.send(JSON.stringify(payload));
} catch (err) {
finalize("error", err instanceof Error ? err.message : String(err));
}
};
ws.onmessage = (ev) => {
armIdleTimer();
let frame: { type?: string; content?: string };
try {
frame = JSON.parse(ev.data);
} catch {
return;
}
if (frame.type === "started") {
handlers.onStart?.();
return;
}
if (frame.type === "text" && typeof frame.content === "string") {
buffer += frame.content;
handlers.onChunk(frame.content);
return;
}
if (frame.type === "done") {
finalize("done");
return;
}
if (frame.type === "error") {
finalize("error", frame.content || "AI judge failed.");
}
};
ws.onerror = () => {
finalize("error", "AI judge connection error.");
};
ws.onclose = () => {
if (!finished) {
finalize("error", "AI judge connection closed unexpectedly.");
}
};
return {
close: () => {
if (!finished) {
finished = true;
}
clearIdleTimer();
try {
ws.close();
} catch {
/* ignore */
}
},
cancel: () => finalize("error", "AI judgment stopped."),
};
}
export function readFileAsBase64(
file: File,
): Promise<{ base64: string; mime: string; name: string }> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const result = reader.result;
if (typeof result !== "string") {
reject(new Error("Failed to read file"));
return;
}
const comma = result.indexOf(",");
const base64 = comma >= 0 ? result.slice(comma + 1) : result;
resolve({
base64,
mime: file.type || "image/png",
name: file.name || "answer.png",
});
};
reader.onerror = () =>
reject(reader.error ?? new Error("Failed to read file"));
reader.readAsDataURL(file);
});
}