feat: run manual translations in the background
The translate endpoint now checks the request, answers 202 with the run, and translates as its own task. A slow local model takes minutes, and the old handler was dropped as soon as the page closed, losing the work. The run reports on the file's subtitle status until it lands or fails, and the panel polls that instead of holding the request open. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
ca717d9486
commit
1a38ecf161
+79
-8
@@ -167,6 +167,7 @@ import {
|
||||
subtitleSummary,
|
||||
subtitleSummaryLabel,
|
||||
summaryWidth,
|
||||
type Translation,
|
||||
translateSubtitle,
|
||||
translationSources,
|
||||
wantedSubtitles,
|
||||
@@ -192,6 +193,8 @@ import {
|
||||
} from "./ui";
|
||||
|
||||
const POLL_MS = 15_000;
|
||||
/** How often an open title re-reads a running translation's status. */
|
||||
const TRANSLATION_POLL_MS = 5000;
|
||||
|
||||
/** §9.8: the live download snapshot, refetched on the app's one poll cadence
|
||||
* (POLL_MS) rather than a second timer. Read by row builders at paint time. */
|
||||
@@ -1059,7 +1062,11 @@ function downloadChip(
|
||||
const downloadSays = new WeakMap<HTMLElement, (text: string | null) => void>();
|
||||
|
||||
function fillDownloadSlot(slot: HTMLElement) {
|
||||
const download = findDownload(latestDownloads, slot.dataset.dlKind ?? "", Number(slot.dataset.dlId));
|
||||
const download = findDownload(
|
||||
latestDownloads,
|
||||
slot.dataset.dlKind ?? "",
|
||||
Number(slot.dataset.dlId),
|
||||
);
|
||||
if (download === null) {
|
||||
slot.replaceChildren();
|
||||
return;
|
||||
@@ -1503,6 +1510,9 @@ function subtitleSection(status: SubtitleStatus, refresh: () => void): SubtitleS
|
||||
let subtitles = status.subtitles;
|
||||
let missing = status.missing;
|
||||
let tracks = status.embedded_tracks;
|
||||
let runs = status.translations;
|
||||
/** Pending re-read while a translation runs; one per panel at most. */
|
||||
let pollTimer: number | undefined;
|
||||
/** Set by an extraction so the sidecar it just wrote stays picked across
|
||||
* the repaint that follows. */
|
||||
let pickAfterRepaint: number | null = null;
|
||||
@@ -1547,7 +1557,13 @@ function subtitleSection(status: SubtitleStatus, refresh: () => void): SubtitleS
|
||||
*/
|
||||
function paintChips() {
|
||||
const summary = subtitleSummary(
|
||||
{ media_file_id: mediaFileId, subtitles, missing, embedded_tracks: tracks },
|
||||
{
|
||||
media_file_id: mediaFileId,
|
||||
subtitles,
|
||||
missing,
|
||||
embedded_tracks: tracks,
|
||||
translations: runs,
|
||||
},
|
||||
wanted,
|
||||
);
|
||||
// the codes read as one string — the chip's own flex gap would otherwise
|
||||
@@ -1747,7 +1763,57 @@ function subtitleSection(status: SubtitleStatus, refresh: () => void): SubtitleS
|
||||
targetLanguages.bar(picked?.language ?? null);
|
||||
// a track is a source only once it has been written out, so until then
|
||||
// the button has nothing to translate from
|
||||
translateButton.disabled = engine.select.disabled || picked === null || "index" in picked;
|
||||
translateButton.disabled =
|
||||
engine.select.disabled || picked === null || "index" in picked || running().length > 0;
|
||||
}
|
||||
|
||||
function running(): Translation[] {
|
||||
return runs.filter((run) => run.state === "running");
|
||||
}
|
||||
|
||||
/**
|
||||
* Say where the file's translations stand, from the status rather than
|
||||
* from the click, so a run started before the page was left still shows.
|
||||
* `before` is the previous status's runs: a language that was running and
|
||||
* is now gone has landed.
|
||||
*/
|
||||
function paintRuns(before: Translation[]) {
|
||||
const now = running();
|
||||
const failed = runs.find((run) => run.state === "failed");
|
||||
const landed = before.find(
|
||||
(run) =>
|
||||
run.state === "running" &&
|
||||
!runs.some((next) => next.language === run.language) &&
|
||||
subtitles.some((subtitle) => subtitle.language === run.language),
|
||||
);
|
||||
if (now.length > 0) {
|
||||
translateNote.hidden = false;
|
||||
say(translateNote, `translating into ${now.map((run) => run.language).join(", ")}…`);
|
||||
} else if (failed !== undefined) {
|
||||
translateNote.hidden = false;
|
||||
say(
|
||||
translateNote,
|
||||
`translation into ${failed.language} failed — ${failed.detail ?? "no reason given"}`,
|
||||
"fault",
|
||||
);
|
||||
} else if (landed !== undefined) {
|
||||
translateNote.hidden = false;
|
||||
say(
|
||||
translateNote,
|
||||
`written next to the video as a machine translation into ${landed.language}`,
|
||||
);
|
||||
}
|
||||
window.clearTimeout(pollTimer);
|
||||
pollTimer = undefined;
|
||||
// a run takes minutes; re-read on a slow beat until it settles, and stop
|
||||
// once the panel has left the page
|
||||
if (now.length > 0) {
|
||||
pollTimer = window.setTimeout(() => {
|
||||
if (toggle.isConnected) {
|
||||
refresh();
|
||||
}
|
||||
}, TRANSLATION_POLL_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1949,20 +2015,21 @@ function subtitleSection(status: SubtitleStatus, refresh: () => void): SubtitleS
|
||||
const sourceId = picked.id;
|
||||
translateButton.disabled = true;
|
||||
translateNote.hidden = false;
|
||||
say(translateNote, `translating into ${target}…`);
|
||||
say(translateNote, `starting a translation into ${target}…`);
|
||||
void translateSubtitle(mediaFileId, {
|
||||
source_subtitle_id: sourceId,
|
||||
target_language: target,
|
||||
engine: engine.select.value,
|
||||
}).then((outcome) => {
|
||||
translateButton.disabled = false;
|
||||
translateNote.hidden = false;
|
||||
if (outcome.kind === "error") {
|
||||
translateButton.disabled = false;
|
||||
say(translateNote, `translation failed — ${outcome.detail}`, "fault");
|
||||
return;
|
||||
}
|
||||
say(translateNote, `written next to the video as a machine translation into ${target}`);
|
||||
refresh();
|
||||
const before = runs;
|
||||
runs = [...runs.filter((run) => run.language !== outcome.run.language), outcome.run];
|
||||
paintSources();
|
||||
paintRuns(before);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1975,6 +2042,7 @@ function subtitleSection(status: SubtitleStatus, refresh: () => void): SubtitleS
|
||||
});
|
||||
|
||||
paintChips();
|
||||
paintRuns([]);
|
||||
// the wanted set decides which languages the chip may name at all, so it
|
||||
// is asked for on sight rather than when the panel opens; the request is
|
||||
// shared by every row on the page
|
||||
@@ -1989,8 +2057,11 @@ function subtitleSection(status: SubtitleStatus, refresh: () => void): SubtitleS
|
||||
subtitles = next.subtitles;
|
||||
missing = next.missing;
|
||||
tracks = next.embedded_tracks;
|
||||
const before = runs;
|
||||
runs = next.translations;
|
||||
paintChips();
|
||||
paintSources();
|
||||
paintRuns(before);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
+26
-3
@@ -53,6 +53,20 @@ export interface SubtitleStatus {
|
||||
missing: MissingSubtitle[];
|
||||
/** Empty for a file probed before #189, which recorded no codec. */
|
||||
embedded_tracks: EmbeddedTrack[];
|
||||
/** Manual translations still running, or failed after the click returned. */
|
||||
translations: Translation[];
|
||||
}
|
||||
|
||||
/**
|
||||
* A manual translation the daemon runs on its own (§15). A finished one is
|
||||
* not listed: its sidecar is on `subtitles` instead.
|
||||
*/
|
||||
export interface Translation {
|
||||
language: string;
|
||||
engine: string;
|
||||
state: "running" | "failed";
|
||||
/** Set only when `state` is `failed`. */
|
||||
detail: string | null;
|
||||
}
|
||||
|
||||
/** The series-wide bulk sibling of `SubtitleStatus` (one call, not one per episode). */
|
||||
@@ -258,11 +272,20 @@ export async function grabSubtitle(
|
||||
}
|
||||
}
|
||||
|
||||
/** Translate one subtitle already on the file into another language (§15). */
|
||||
export type TranslateOutcome =
|
||||
| { kind: "started"; run: Translation }
|
||||
| { kind: "error"; detail: string };
|
||||
|
||||
/**
|
||||
* Start translating one subtitle already on the file into another language
|
||||
* (§15). The daemon answers before the translation is done; the file's
|
||||
* subtitle status reports the run from then on, so leaving the page loses
|
||||
* nothing.
|
||||
*/
|
||||
export async function translateSubtitle(
|
||||
mediaFileId: number,
|
||||
input: { source_subtitle_id: number; target_language: string; engine: string },
|
||||
): Promise<SubtitleWriteOutcome> {
|
||||
): Promise<TranslateOutcome> {
|
||||
try {
|
||||
const response = await fetch(`/api/media-files/${mediaFileId}/subtitles/translate`, {
|
||||
method: "POST",
|
||||
@@ -272,7 +295,7 @@ export async function translateSubtitle(
|
||||
if (!response.ok) {
|
||||
return { kind: "error", detail: await errorDetail(response) };
|
||||
}
|
||||
return { kind: "done", subtitle: (await response.json()) as Subtitle };
|
||||
return { kind: "started", run: (await response.json()) as Translation };
|
||||
} catch {
|
||||
return { kind: "error", detail: "daemon unreachable" };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user