Files
srt-translator/server.js
T
2026-11-15 15:39:55 +01:00

317 lines
15 KiB
JavaScript

#!/usr/bin/env node
// SRT Translator — local web app. Zero-dependency HTTP server (+ optional @lmstudio/sdk).
import http from 'node:http';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { spawn } from 'node:child_process';
import { isSea, getAsset } from 'node:sea';
import { parseSrt, serializeSrt, decodeSubtitleBuffer, songKind, stripSongs } from './src/srt.js';
import { detectLanguage } from './src/langdetect.js';
import { LANGUAGES } from './src/languages.js';
import { PROFILES, detectProfile } from './src/prompts.js';
import { getEngine, ENGINE_DEFAULTS } from './src/engines/index.js';
import { estimateMemory, maxContextFor, systemMemory, archParams, KV_TYPES } from './src/vram.js';
import { TranslationJob } from './src/translator.js';
import { PullJob } from './src/pulls.js';
// In the bundled single executable (CommonJS) import.meta is empty and the UI comes from SEA assets.
const SEA = isSea();
const __dirname = typeof import.meta?.url === 'string' ? path.dirname(fileURLToPath(import.meta.url)) : process.cwd();
const PUBLIC = path.join(__dirname, 'public');
const PORT = Number(process.env.PORT || 8765);
const HOST = process.env.HOST || '127.0.0.1';
const MAX_BODY = 25 * 2 ** 20;
const jobs = new Map();
const pulls = new Map();
const MIME = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.svg': 'image/svg+xml', '.ico': 'image/x-icon' };
function send(res, status, data, headers = {}) {
const body = typeof data === 'string' || Buffer.isBuffer(data) ? data : JSON.stringify(data);
res.writeHead(status, { 'content-type': typeof data === 'string' || Buffer.isBuffer(data) ? 'text/plain; charset=utf-8' : 'application/json; charset=utf-8', 'cache-control': 'no-store', ...headers });
res.end(body);
}
async function readBody(req) {
const chunks = [];
let size = 0;
for await (const c of req) {
size += c.length;
if (size > MAX_BODY) throw Object.assign(new Error('Za duże żądanie'), { status: 413 });
chunks.push(c);
}
return Buffer.concat(chunks);
}
async function readJson(req) {
const b = await readBody(req);
if (!b.length) return {};
try { return JSON.parse(b.toString('utf8')); } catch { throw Object.assign(new Error('Niepoprawny JSON'), { status: 400 }); }
}
// ---------------------------------------------------------------------------
const routes = {
'GET /api/meta': async () => ({
languages: LANGUAGES,
profiles: Object.fromEntries(Object.entries(PROFILES).map(([k, p]) => [k, { label: p.label, modes: p.modes, defaultMode: p.defaultMode, needsSource: !!p.needsSource, recommended: p.recommended || null, note: p.note || null }])),
engines: ENGINE_DEFAULTS,
kvTypes: Object.keys(KV_TYPES).filter((k) => k !== 'bf16'),
system: await systemMemory(),
}),
'GET /api/system': async () => systemMemory(),
'POST /api/srt/parse': async (req, url) => {
const buf = await readBody(req);
const { text, encoding } = decodeSubtitleBuffer(buf, url.searchParams.get('encoding') || 'auto');
const { cues, eol } = parseSrt(text);
if (!cues.length) throw Object.assign(new Error('Nie znaleziono napisów — czy to na pewno plik SRT?'), { status: 400 });
const all = cues.map((c) => c.text).join('\n');
for (const c of cues) c.song = songKind(c.text);
return {
encoding, eol: eol === '\r\n' ? 'crlf' : 'lf', cues,
detectedLanguage: detectLanguage(all),
stats: { cues: cues.length, chars: all.length, durationMs: cues[cues.length - 1].endMs },
};
},
'POST /api/srt/build': async (req) => {
const { cues, translations = {}, eol = 'lf', format = 'translated', bom = false, songs = 'translate' } = await readJson(req);
const remove = songs === 'remove';
const getText = (c) => {
let src = c.text, t = translations[c.id];
if (remove) { src = stripSongs(src); t = t ? stripSongs(t) : t; }
if (format === 'bilingual') return t ? (src ? `${src}\n${t}` : t) : src;
return t || src;
};
// Removing songs drops whole cues (lyrics only) and lyric lines from mixed cues; the
// remaining cues keep their exact timings and are renumbered.
const kept = remove ? cues.filter((c) => songKind(c.text) !== 'full' && getText(c).trim()) : cues;
const srt = serializeSrt(kept, { eol: eol === 'crlf' ? '\r\n' : '\n', getText });
return { srt: (bom ? '\uFEFF' : '') + srt, cues: kept.length, removed: cues.length - kept.length };
},
'POST /api/engine/status': async (req) => {
const { engine } = await readJson(req);
const e = getEngine(engine);
const st = await e.status();
return { ...st, capabilities: e.capabilities() };
},
'POST /api/engine/models': async (req) => {
const { engine } = await readJson(req);
const models = await getEngine(engine).listModels();
return { models: models.map((m) => ({ ...m, profile: detectProfile(m.id) })) };
},
'POST /api/engine/model-info': async (req) => {
const { engine, model } = await readJson(req);
const info = await getEngine(engine).modelInfo(model);
const p = info.meta ? archParams(info.meta) : null;
return {
...info,
meta: undefined,
hasMeta: !!info.meta,
arch: p ? { name: p.arch, layers: p.nLayer, headsKv: p.nHeadKv, keyLen: p.keyLen, valLen: p.valLen, slidingWindow: p.slidingWindow, sharedKvLayers: p.sharedKvLayers, fullAttnInterval: p.fullAttnInterval, vocab: p.vocab, kvLoraRank: p.kvLoraRank } : null,
profile: detectProfile(model),
};
},
'POST /api/engine/estimate': async (req) => {
const { engine, model, settings = {}, sizeBytes } = await readJson(req);
const e = getEngine(engine);
const sys = await systemMemory();
let info = null;
try { info = await e.modelInfo(model); } catch { /* estimate without metadata */ }
const opts = {
modelBytes: Number(sizeBytes) || 0,
nCtx: Number(settings.contextLength) || 4096,
kType: settings.kvCacheK || 'f16',
vType: settings.kvCacheV || 'f16',
flashAttention: settings.flashAttention !== false,
nUbatch: 512,
nSeq: 1,
gpuRatio: settings.gpuRatio === '' || settings.gpuRatio === undefined ? 1 : Number(settings.gpuRatio),
};
const own = estimateMemory(info?.meta, opts);
const budget = sys.gpus[0]?.total ? sys.gpus[0].total - (sys.gpus[0].unified ? 0 : sys.gpus[0].used || 0) : null;
const fit = budget && info?.meta ? maxContextFor(info.meta, { ...opts, maxCtx: info.maxContext || 131072 }, budget * 0.95) : null;
let engineEst = null, engineErr = null;
if (e.estimate) {
try { engineEst = await e.estimate(model, settings); } catch (err) { engineErr = err.message; }
}
return { own, engine: engineEst, engineError: engineErr, system: sys, budget, maxContextFit: fit, maxContext: info?.maxContext || null };
},
'POST /api/engine/loaded': async (req) => {
const { engine } = await readJson(req);
return { loaded: await getEngine(engine).loaded() };
},
'POST /api/engine/load': async (req) => {
const { engine, model, settings } = await readJson(req);
const e = getEngine(engine);
if (!e.load) throw Object.assign(new Error('Ten silnik nie obsługuje ładowania modeli z poziomu aplikacji.'), { status: 400 });
return e.load(model, settings);
},
'POST /api/engine/unload': async (req) => {
const { engine, instanceId } = await readJson(req);
const e = getEngine(engine);
if (!e.unload) throw Object.assign(new Error('Ten silnik nie obsługuje zwalniania modeli.'), { status: 400 });
return e.unload(instanceId);
},
'POST /api/pulls': async (req) => {
const { engine, model, quantization } = await readJson(req);
const name = String(model || '').trim();
if (!name) throw Object.assign(new Error('Podaj nazwę modelu.'), { status: 400 });
const e = getEngine(engine);
if (!e.pull) throw Object.assign(new Error('Ten silnik nie obsługuje pobierania modeli z aplikacji.'), { status: 400 });
for (const [id, p] of pulls) if ((p.isTerminal() || p.state === 'error') && Date.now() - p.finishedAt > 3600e3) pulls.delete(id);
// The same model already has a download → continue that one (one tile in the UI), don't start a second.
const existing = [...pulls.values()].find((p) => !p.isTerminal() && p.engine === e && p.model === name);
if (existing) {
existing.resume();
return existing.snapshot();
}
const job = new PullJob({ engine: e, engineKind: e.kind, model: name, quantization });
pulls.set(job.id, job);
job.start();
return job.snapshot();
},
'GET /api/pulls': async () => ({ pulls: [...pulls.values()].map((p) => p.snapshot()) }),
'POST /api/jobs': async (req) => {
const { engine, model, settings, options, cues, existing, onlyIds } = await readJson(req);
if (!model) throw Object.assign(new Error('Wybierz model.'), { status: 400 });
if (!Array.isArray(cues) || !cues.length) throw Object.assign(new Error('Brak napisów.'), { status: 400 });
for (const [id, j] of jobs) if (j.state !== 'running' && Date.now() - (j.stats.finishedAt || 0) > 3600e3) jobs.delete(id);
const job = new TranslationJob({ cues, engine: getEngine(engine), model, settings, options, existing, onlyIds });
jobs.set(job.id, job);
job.run();
return { id: job.id, profile: job.profile, mode: job.mode };
},
};
function pullRoute(req, res, url) {
const m = /^\/api\/pulls\/([\w-]+)(\/events|\/cancel|\/pause|\/resume)$/.exec(url.pathname);
if (!m) return false;
const job = pulls.get(m[1]);
if (!job) { send(res, 404, { error: 'Nie ma takiego pobierania' }); return true; }
if (req.method === 'POST' && ['/cancel', '/pause', '/resume'].includes(m[2])) {
const action = { '/cancel': () => job.cancel(), '/pause': () => job.pause(), '/resume': () => job.resume() }[m[2]];
Promise.resolve(action()).then(() => send(res, 200, job.snapshot()), (e) => send(res, 500, { error: e.message }));
return true;
}
if (m[2] === '/events' && req.method === 'GET') {
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' });
const ev = (type, data) => res.write(`event: ${type}\ndata: ${JSON.stringify(data)}\n\n`);
ev('state', job.snapshot());
if (job.isTerminal()) { res.end(); return true; }
const onProgress = (s) => ev('progress', s);
const onState = (s) => ev('state', s);
const ping = setInterval(() => res.write(': ping\n\n'), 15000);
job.on('progress', onProgress); job.on('state', onState);
req.on('close', () => { clearInterval(ping); job.off('progress', onProgress); job.off('state', onState); });
return true;
}
return false;
}
function jobRoute(req, res, url) {
const m = /^\/api\/jobs\/([\w-]+)(\/events|\/cancel)?$/.exec(url.pathname);
if (!m) return false;
const job = jobs.get(m[1]);
if (!job) { send(res, 404, { error: 'Nie ma takiego zadania' }); return true; }
if (!m[2] && req.method === 'GET') { send(res, 200, job.snapshot()); return true; }
if (m[2] === '/cancel' && req.method === 'POST') { job.cancel(); send(res, 200, { ok: true }); return true; }
if (m[2] === '/events' && req.method === 'GET') {
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' });
const ev = (type, data) => res.write(`event: ${type}\ndata: ${JSON.stringify(data)}\n\n`);
ev('snapshot', job.snapshot());
// Cue updates are coalesced so long files don't flood the browser.
let pendingCues = [];
const flush = () => { if (pendingCues.length) { ev('cues', pendingCues); pendingCues = []; } };
const timer = setInterval(flush, 250);
const ping = setInterval(() => res.write(': ping\n\n'), 15000);
const onCue = (c) => pendingCues.push(c);
const onLog = (l) => ev('log', l);
const onProgress = (s) => ev('progress', s);
const onState = (s) => { flush(); ev('state', { state: s, error: job.error || null, stats: job.stats }); };
job.on('cue', onCue); job.on('log', onLog); job.on('progress', onProgress); job.on('state', onState);
req.on('close', () => {
clearInterval(timer); clearInterval(ping);
job.off('cue', onCue); job.off('log', onLog); job.off('progress', onProgress); job.off('state', onState);
});
return true;
}
return false;
}
async function serveStatic(res, pathname) {
const rel = pathname === '/' ? 'index.html' : pathname.replace(/^\/+/, '');
if (SEA) {
try {
const data = Buffer.from(getAsset(rel));
res.writeHead(200, { 'content-type': MIME[path.extname(rel)] || 'application/octet-stream', 'cache-control': 'no-cache' });
return res.end(data);
} catch {
return send(res, 404, 'Not found');
}
}
const file = path.normalize(path.join(PUBLIC, rel));
if (!file.startsWith(PUBLIC)) return send(res, 403, 'Forbidden');
try {
const data = await readFile(file);
res.writeHead(200, { 'content-type': MIME[path.extname(file)] || 'application/octet-stream', 'cache-control': 'no-cache' });
res.end(data);
} catch {
send(res, 404, 'Not found');
}
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
try {
if (url.pathname.startsWith('/api/jobs/') && jobRoute(req, res, url)) return;
if (url.pathname.startsWith('/api/pulls/') && pullRoute(req, res, url)) return;
const handler = routes[`${req.method} ${url.pathname}`];
if (handler) return send(res, 200, await handler(req, url));
if (req.method === 'GET' && !url.pathname.startsWith('/api/')) return serveStatic(res, url.pathname);
send(res, 404, { error: 'Not found' });
} catch (e) {
send(res, e.status && e.status < 500 ? e.status : 502, { error: e.message });
}
});
function openBrowser(url) {
const [cmd, args] = process.platform === 'win32' ? ['cmd', ['/c', 'start', '', url]]
: process.platform === 'darwin' ? ['open', [url]] : ['xdg-open', [url]];
try { spawn(cmd, args, { detached: true, stdio: 'ignore', windowsHide: true }).unref(); } catch { /* user can open it manually */ }
}
// If the port is taken (e.g. the app is already running), try the next few.
let port = PORT;
server.on('error', (e) => {
if (e.code === 'EADDRINUSE' && port < PORT + 10) {
port++;
server.listen(port, HOST);
} else {
console.error(`Nie udalo sie uruchomic serwera: ${e.message}`);
process.exit(1);
}
});
server.on('listening', () => {
const url = `http://${HOST === '0.0.0.0' ? 'localhost' : HOST}:${port}`;
// Plain ASCII: the classic Windows console mangles Polish letters.
console.log(`SRT Translator dziala: ${url}`);
if (SEA) console.log('Nie zamykaj tego okna - to serwer aplikacji. Zamkniecie okna wylacza aplikacje.');
if ((SEA || process.env.OPEN_BROWSER === '1') && process.env.NO_BROWSER !== '1') openBrowser(url);
});
server.listen(port, HOST);