Files
SubMiner/src/main/assemblies/subtitle-generation.ts
T
sudacode f89f78b861 refactor(main): move plugins, session actions and subtitle generation into assemblies (#327)
* refactor(main): build overlay geometry and pointer interaction in the overlay assembly

Inject Electron's screen (and the stats-window promotion) into the overlay geometry and
visible-overlay interaction runtimes so they load under bun test, then construct both in
createOverlayAssembly together with the overlay visibility composer and the mpv subtitle
suppression handlers. Replace the three regex tests that pinned the moved Linux input and
shape code with behaviour tests on the runtimes.

* refactor(main): move the autoplay-ready gate into the playback assembly

The autoplay-ready gate, its post-warm subtitle prime, the current-media tokenization gate and
managed local subtitle selection all read playback state, and playback signals through the
gate, so createPlaybackAssembly now builds them. The overlay reaches the gate through a getter.

Replace the last four regex guards with behaviour tests: the gate holds the release until the
visible overlay has measured the line and ignores untokenized lines during warmup (playback),
cached autoplay priming never paints raw first and the sidebar media path is tagged only after
prefetch loads (priming runtime), and dashboard mining overrides the Hachidori Anki server only
when the proxy is on (stats server runtime). main-wiring.test.ts is now empty and removed.

* refactor(main): move the plugin host, panel and windows into a plugins assembly

* refactor(main): move session action dispatch and renderer mpv commands into an assembly

createSessionActionsAssembly owns the session action dispatcher, the secondary-subtitle mode cycle, queueing the clipboard's video, and the mpv commands the overlay renderer sends. The runtime option cycle both paths used is now one function.

* refactor(main): move Whisper subtitle generation into an assembly

* refactor(main): resolve subtitle annotation options in a runtime module

The runtime-option-over-config resolution that the texthooker page and the MeCab startup decision share moves out of main.ts into createAnnotationOptions.

* test(main): cover a non-boolean runtime annotation option value

* docs: describe the plugins, session actions and subtitle generation assemblies

Update ownership for overlay geometry and pointer interaction, the autoplay-ready gate, and annotation options; point the overlay priming entry points at their new modules; the main-wiring regex debt is gone.

* build: export MpvClientEvents so declaration emit can name the plugins assembly's return type
2026-11-04 21:54:49 -08:00

95 lines
3.8 KiB
TypeScript

import path from 'node:path';
import type { IpcMain } from 'electron';
import { toJellyfinOriginalStreamUrl, type createOverlayManager } from '../../core/services';
import type { ResolvedConfig } from '../../types';
import { IPC_CHANNELS } from '../../shared/ipc/contracts';
import { registerSubtitleGenerationIpc } from '../runtime/subtitle-generation-ipc';
import { openSubtitleGenerationModal } from '../runtime/subtitle-generation-open';
import { createSubtitleGenerationRuntime } from '../runtime/subtitle-generation-runtime';
import type { AppState } from '../state';
import type { JellyfinAssembly } from './jellyfin';
import type { OverlayModalsAssembly } from './overlay-modals';
import type { YoutubeAssembly } from './youtube';
export interface SubtitleGenerationAssemblyDeps {
appState: Pick<AppState, 'mpvClient'>;
getConfig: () => ResolvedConfig;
/** Managed Whisper models live next to the config file. */
getModelDirectory: () => string;
overlayManager: Pick<ReturnType<typeof createOverlayManager>, 'getMainWindow' | 'getModalWindow'>;
overlayModals: Pick<OverlayModalsAssembly, 'createOverlayHostedModalOpenDeps'>;
youtube: {
whisperSubtitles: Pick<YoutubeAssembly['whisperSubtitles'], 'generate'>;
flow: Pick<YoutubeAssembly['flow'], 'createSubtitleTempDir'>;
playback: Pick<YoutubeAssembly['playback'], 'getYoutubeSourceUrlForStream'>;
};
jellyfin: Pick<JellyfinAssembly, 'getActivePlayback'>;
ipcMain: Pick<IpcMain, 'handle'>;
}
export type SubtitleGenerationAssembly = ReturnType<typeof createSubtitleGenerationAssembly>;
/**
* Owns Whisper subtitle generation for the playing media (local files, YouTube and Jellyfin
* streams): the generation runtime, its overlay modal and its renderer IPC.
*/
export function createSubtitleGenerationAssembly(deps: SubtitleGenerationAssemblyDeps) {
const { appState, overlayManager, youtube } = deps;
function getOverlayWindows() {
return [overlayManager.getMainWindow(), overlayManager.getModalWindow()];
}
const runtime = createSubtitleGenerationRuntime({
getConfig: () => deps.getConfig().subtitleGeneration,
getModelDirectory: () => deps.getModelDirectory(),
generateYoutube: async ({ url, config, signal, onProgress }) =>
youtube.whisperSubtitles.generate({
url,
config,
signal,
onProgress,
outputPath: path.join(await youtube.flow.createSubtitleTempDir(), 'youtube-whisper.ja.srt'),
}),
resolveYoutubeSourceUrl: (mediaPath) =>
youtube.playback.getYoutubeSourceUrlForStream(mediaPath),
resolveJellyfinMedia: (mediaPath) => {
const playback = deps.jellyfin.getActivePlayback();
const sourceUrl = playback && toJellyfinOriginalStreamUrl(mediaPath, playback.itemId);
return sourceUrl
? {
sourceUrl,
transcoded: playback.playMethod === 'Transcode',
audioStreamIndex: playback.audioStreamIndex ?? null,
}
: null;
},
getMpvClient: () => appState.mpvClient,
onProgress: (progress) => {
for (const window of getOverlayWindows()) {
if (window && !window.isDestroyed())
window.webContents.send(IPC_CHANNELS.event.subtitleGenerationProgress, progress);
}
},
});
function openModal(): Promise<boolean> {
return openSubtitleGenerationModal(deps.overlayModals.createOverlayHostedModalOpenDeps());
}
/** Registers the generation IPC; only the overlay windows may call it. */
function registerIpc(): void {
registerSubtitleGenerationIpc({
ipc: deps.ipcMain,
isAllowedSender: (sender) =>
getOverlayWindows().some(
(window) => window && !window.isDestroyed() && window.webContents === sender,
),
runtime,
openModal,
});
}
return { runtime, openModal, registerIpc };
}