Initialize repo
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
export class EngineError extends Error {
|
||||
constructor(message, { status, body } = {}) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.body = body;
|
||||
}
|
||||
}
|
||||
|
||||
export function joinUrl(base, path) {
|
||||
return base.replace(/\/+$/, '') + path;
|
||||
}
|
||||
|
||||
/** fetch + JSON with timeout and external abort signal. */
|
||||
export async function requestJson(url, { method = 'GET', body, headers = {}, timeoutMs = 30000, signal } = {}) {
|
||||
const ctrl = new AbortController();
|
||||
const onAbort = () => ctrl.abort(signal.reason);
|
||||
if (signal) {
|
||||
if (signal.aborted) throw signal.reason ?? new Error('aborted');
|
||||
signal.addEventListener('abort', onAbort, { once: true });
|
||||
}
|
||||
const timer = timeoutMs ? setTimeout(() => ctrl.abort(new Error(`Timeout po ${Math.round(timeoutMs / 1000)} s`)), timeoutMs) : null;
|
||||
try {
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(url, {
|
||||
method,
|
||||
headers: { ...(body !== undefined ? { 'content-type': 'application/json' } : {}), ...headers },
|
||||
body: body !== undefined ? JSON.stringify(body) : undefined,
|
||||
signal: ctrl.signal,
|
||||
});
|
||||
} catch (e) {
|
||||
if (ctrl.signal.aborted) throw ctrl.signal.reason instanceof Error ? ctrl.signal.reason : new Error(String(ctrl.signal.reason));
|
||||
const cause = e.cause?.code || e.cause?.message || e.message;
|
||||
throw new EngineError(`Nie można połączyć się z ${url} (${cause})`);
|
||||
}
|
||||
const text = await res.text();
|
||||
let json;
|
||||
try { json = text ? JSON.parse(text) : null; } catch { json = null; }
|
||||
if (!res.ok) {
|
||||
const msg = json?.error?.message || json?.error || text.slice(0, 300) || res.statusText;
|
||||
throw new EngineError(`${res.status}: ${typeof msg === 'string' ? msg : JSON.stringify(msg)}`, { status: res.status, body: json ?? text });
|
||||
}
|
||||
return json ?? text;
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
if (signal) signal.removeEventListener('abort', onAbort);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { OllamaEngine } from './ollama.js';
|
||||
import { LMStudioEngine } from './lmstudio.js';
|
||||
import { OpenAICompatEngine } from './openai.js';
|
||||
|
||||
export const ENGINE_DEFAULTS = {
|
||||
ollama: { label: 'Ollama', baseUrl: 'http://127.0.0.1:11434', notes: new OllamaEngine().capabilities().notes },
|
||||
lmstudio: { label: 'LM Studio', baseUrl: 'http://127.0.0.1:1234', notes: [] },
|
||||
openai: { label: 'OpenAI-compatible (llama.cpp, vLLM, …)', baseUrl: 'http://127.0.0.1:8080', notes: new OpenAICompatEngine().capabilities().notes },
|
||||
};
|
||||
|
||||
// Engines keep caches (model info, SDK connection), so reuse instances per config.
|
||||
const instances = new Map();
|
||||
|
||||
export function getEngine(cfg = {}) {
|
||||
const kind = cfg.kind || 'ollama';
|
||||
const baseUrl = cfg.baseUrl || ENGINE_DEFAULTS[kind]?.baseUrl;
|
||||
const key = JSON.stringify([kind, baseUrl, cfg.apiKey || '', cfg.extraBody || null, cfg.structuredOutput ?? true]);
|
||||
if (instances.has(key)) return instances.get(key);
|
||||
let engine;
|
||||
switch (kind) {
|
||||
case 'ollama': engine = new OllamaEngine({ baseUrl }); break;
|
||||
case 'lmstudio': engine = new LMStudioEngine({ baseUrl }); break;
|
||||
case 'openai': engine = new OpenAICompatEngine({ baseUrl, apiKey: cfg.apiKey, extraBody: cfg.extraBody, structuredOutput: cfg.structuredOutput }); break;
|
||||
default: throw new Error(`Nieznany silnik: ${kind}`);
|
||||
}
|
||||
instances.set(key, engine);
|
||||
return engine;
|
||||
}
|
||||
@@ -0,0 +1,336 @@
|
||||
import { readFile, readdir, stat } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { readGgufMetadata } from '../gguf.js';
|
||||
import { requestJson, joinUrl, EngineError } from './http.js';
|
||||
|
||||
let sdkPromise = null;
|
||||
function loadSdk() {
|
||||
sdkPromise ??= import('@lmstudio/sdk').catch(() => null);
|
||||
return sdkPromise;
|
||||
}
|
||||
|
||||
const silentLogger = { info() {}, warn() {}, debug() {}, error() {} };
|
||||
|
||||
/**
|
||||
* LM Studio: REST v1 (/api/v1/models) for listing, OpenAI-compatible endpoint for chat
|
||||
* (supports json_schema), @lmstudio/sdk for loading with full config (KV-cache
|
||||
* quantization, flash attention...) and LM Studio's own memory estimator.
|
||||
*/
|
||||
export class LMStudioEngine {
|
||||
kind = 'lmstudio';
|
||||
|
||||
constructor({ baseUrl } = {}) {
|
||||
this.baseUrl = baseUrl || 'http://127.0.0.1:1234';
|
||||
this.client = null;
|
||||
this.metaCache = new Map();
|
||||
this.modelsCache = null;
|
||||
}
|
||||
|
||||
url(p) { return joinUrl(this.baseUrl, p); }
|
||||
|
||||
isLocal() {
|
||||
try { return ['localhost', '127.0.0.1', '::1', '[::1]'].includes(new URL(this.baseUrl).hostname); } catch { return false; }
|
||||
}
|
||||
|
||||
async sdk() {
|
||||
const mod = await loadSdk();
|
||||
if (!mod) return null;
|
||||
if (!this.client) {
|
||||
const ws = this.baseUrl.replace(/^http/, 'ws');
|
||||
this.client = new mod.LMStudioClient({ baseUrl: ws, logger: silentLogger });
|
||||
}
|
||||
return this.client;
|
||||
}
|
||||
|
||||
capabilities() {
|
||||
return {
|
||||
loadControl: true,
|
||||
pull: true,
|
||||
pullCancel: false, // the REST API has no cancel; pausing/cancelling is done in LM Studio's Downloads panel
|
||||
kvCacheQuantization: 'load', // set per model load via SDK
|
||||
estimate: 'engine',
|
||||
notes: [],
|
||||
};
|
||||
}
|
||||
|
||||
async status() {
|
||||
await requestJson(this.url('/api/v1/models'), { timeoutMs: 4000 });
|
||||
let version = null, sdk = false;
|
||||
try {
|
||||
const c = await this.sdk();
|
||||
if (c) { sdk = true; version = (await withTimeout(c.system.getLMStudioVersion(), 4000)).version; }
|
||||
} catch { /* SDK optional */ }
|
||||
return { ok: true, version, sdk };
|
||||
}
|
||||
|
||||
async rawModels() {
|
||||
const r = await requestJson(this.url('/api/v1/models'), { timeoutMs: 8000 });
|
||||
this.modelsCache = r?.models || [];
|
||||
return this.modelsCache;
|
||||
}
|
||||
|
||||
/** Real load configs of loaded instances (REST doesn't report KV-cache types). */
|
||||
async loadedConfigs() {
|
||||
const out = new Map();
|
||||
try {
|
||||
const c = await this.sdk();
|
||||
if (!c) return out;
|
||||
const handles = await withTimeout(c.llm.listLoaded(), 4000);
|
||||
for (const h of handles) {
|
||||
try { out.set(h.identifier, summarizeApplied(await withTimeout(h.getLoadConfig(), 4000))); } catch { /* ignore */ }
|
||||
}
|
||||
} catch { /* SDK optional */ }
|
||||
return out;
|
||||
}
|
||||
|
||||
async listModels() {
|
||||
const models = await this.rawModels();
|
||||
const anyLoaded = models.some((m) => m.loaded_instances?.length);
|
||||
const cfgs = anyLoaded ? await this.loadedConfigs() : new Map();
|
||||
return models.filter((m) => m.type === 'llm').map((m) => ({
|
||||
id: m.key,
|
||||
name: m.display_name || m.key,
|
||||
sizeBytes: m.size_bytes,
|
||||
params: m.params_string,
|
||||
quant: m.quantization?.name,
|
||||
family: m.architecture,
|
||||
format: m.format,
|
||||
maxContext: m.max_context_length,
|
||||
reasoning: m.capabilities?.reasoning || null,
|
||||
vision: !!m.capabilities?.vision,
|
||||
loaded: (m.loaded_instances || []).map((i) => ({ id: i.id, contextLength: i.config?.context_length, config: i.config, applied: cfgs.get(i.id) || null })),
|
||||
}));
|
||||
}
|
||||
|
||||
async findModel(key) {
|
||||
const models = this.modelsCache || (await this.rawModels());
|
||||
return models.find((m) => m.key === key) || (await this.rawModels()).find((m) => m.key === key);
|
||||
}
|
||||
|
||||
/** Locate the GGUF file on disk (only when LM Studio runs on this machine). */
|
||||
async ggufPath(key) {
|
||||
if (!this.isLocal()) return null;
|
||||
const home = path.join(os.homedir(), '.lmstudio');
|
||||
let modelsDir = path.join(home, 'models');
|
||||
try { modelsDir = JSON.parse(await readFile(path.join(home, 'settings.json'), 'utf8')).downloadsFolder || modelsDir; } catch { /* default */ }
|
||||
const m = await this.findModel(key);
|
||||
const quant = (m?.quantization?.name || '').toLowerCase();
|
||||
const dirs = [path.join(modelsDir, key)];
|
||||
try {
|
||||
const manifest = JSON.parse(await readFile(path.join(home, 'hub', 'models', key, 'manifest.json'), 'utf8'));
|
||||
for (const dep of manifest.dependencies || []) {
|
||||
for (const s of dep.sources || []) if (s.user && s.repo) dirs.push(path.join(modelsDir, s.user, s.repo));
|
||||
}
|
||||
} catch { /* not a hub model */ }
|
||||
for (const d of dirs) {
|
||||
try {
|
||||
const st = await stat(d);
|
||||
if (st.isFile() && d.endsWith('.gguf')) return d;
|
||||
const files = (await readdir(d)).filter((f) => f.endsWith('.gguf') && !/mmproj/i.test(f));
|
||||
if (!files.length) continue;
|
||||
const pick = files.find((f) => f.toLowerCase().includes(quant) && /-0*1-of-|^(?!.*-of-)/.test(f)) || files.sort()[0];
|
||||
return path.join(d, pick);
|
||||
} catch { /* try next */ }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async modelInfo(key) {
|
||||
const m = await this.findModel(key);
|
||||
if (!m) throw new EngineError(`Nie znaleziono modelu ${key}`);
|
||||
let meta = this.metaCache.get(key) || null;
|
||||
if (!meta) {
|
||||
try {
|
||||
const p = await this.ggufPath(key);
|
||||
if (p) { meta = await readGgufMetadata(p); this.metaCache.set(key, meta); }
|
||||
} catch { /* metadata optional */ }
|
||||
}
|
||||
const reasoning = m.capabilities?.reasoning;
|
||||
return {
|
||||
meta,
|
||||
arch: m.architecture,
|
||||
maxContext: m.max_context_length,
|
||||
capabilities: {
|
||||
thinking: !!reasoning,
|
||||
canDisableThinking: !!reasoning?.allowed_options?.includes('off'),
|
||||
vision: !!m.capabilities?.vision,
|
||||
},
|
||||
defaults: {},
|
||||
};
|
||||
}
|
||||
|
||||
loadConfig(s = {}) {
|
||||
const cfg = {};
|
||||
if (s.contextLength) cfg.contextLength = Number(s.contextLength);
|
||||
if (s.flashAttention !== undefined) cfg.flashAttention = !!s.flashAttention;
|
||||
const kq = s.kvCacheK && s.kvCacheK !== 'f16' ? s.kvCacheK : false;
|
||||
const vq = s.kvCacheV && s.kvCacheV !== 'f16' ? s.kvCacheV : false;
|
||||
cfg.llamaKCacheQuantizationType = kq;
|
||||
cfg.llamaVCacheQuantizationType = vq;
|
||||
if (s.gpuRatio !== undefined && s.gpuRatio !== '' && s.gpuRatio !== null) cfg.gpu = { ratio: Number(s.gpuRatio) };
|
||||
if (s.evalBatchSize) cfg.evalBatchSize = Number(s.evalBatchSize);
|
||||
if (s.offloadKvToGpu !== undefined) cfg.offloadKVCacheToGpu = !!s.offloadKvToGpu;
|
||||
if (s.parallel) cfg.maxParallelPredictions = Number(s.parallel);
|
||||
return cfg;
|
||||
}
|
||||
|
||||
async estimate(key, settings) {
|
||||
const c = await this.sdk();
|
||||
if (!c) return null;
|
||||
const r = await withTimeout(c.llm.estimateResourcesUsage(key, this.loadConfig(settings)), 15000);
|
||||
return {
|
||||
model: r.memory.modelVramBytes ?? r.memory.modelBytes,
|
||||
context: r.memory.contextVramBytes ?? r.memory.contextBytes,
|
||||
total: r.memory.totalVramBytes ?? r.memory.totalBytes,
|
||||
totalRam: r.memory.totalBytes,
|
||||
confidence: r.memory.confidence,
|
||||
passesGuardrails: r.passesGuardrails,
|
||||
};
|
||||
}
|
||||
|
||||
async loaded() {
|
||||
const models = await this.rawModels();
|
||||
return models.flatMap((m) => (m.loaded_instances || []).map((i) => ({ id: i.id, model: m.key, contextLength: i.config?.context_length, config: i.config })));
|
||||
}
|
||||
|
||||
/** (Re)loads the model with the given settings. Existing instances of the model are unloaded first. */
|
||||
async load(key, settings = {}) {
|
||||
// Unload this model's instances (and optionally all other models, to free memory).
|
||||
const instances = (await this.loaded()).filter((i) => i.model === key || settings.unloadOthers);
|
||||
for (const i of instances) await this.unload(i.id);
|
||||
const c = await this.sdk();
|
||||
const cfg = this.loadConfig(settings);
|
||||
if (c) {
|
||||
const handle = await withTimeout(c.llm.load(key, { identifier: key, config: cfg }), 600000);
|
||||
const applied = await handle.getLoadConfig().catch(() => null);
|
||||
return { ok: true, instanceId: handle.identifier, applied: summarizeApplied(applied), via: 'sdk' };
|
||||
}
|
||||
// REST fallback (no KV-cache quantization available here).
|
||||
const body = { model: key, echo_load_config: true };
|
||||
if (cfg.contextLength) body.context_length = cfg.contextLength;
|
||||
if (cfg.flashAttention !== undefined) body.flash_attention = cfg.flashAttention;
|
||||
if (cfg.evalBatchSize) body.eval_batch_size = cfg.evalBatchSize;
|
||||
if (cfg.offloadKVCacheToGpu !== undefined) body.offload_kv_cache_to_gpu = cfg.offloadKVCacheToGpu;
|
||||
const r = await requestJson(this.url('/api/v1/models/load'), { method: 'POST', body, timeoutMs: 600000 });
|
||||
return { ok: true, instanceId: r.instance_id, applied: r.load_config, via: 'rest', warning: (cfg.llamaKCacheQuantizationType || cfg.llamaVCacheQuantizationType) ? 'Brak @lmstudio/sdk — kwantyzacja KV-cache pominięta (uruchom npm install).' : undefined };
|
||||
}
|
||||
|
||||
async unload(instanceId) {
|
||||
await requestJson(this.url('/api/v1/models/unload'), { method: 'POST', body: { instance_id: instanceId }, timeoutMs: 60000 });
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a model from LM Studio's catalog ("google/gemma-3-12b") or Hugging Face
|
||||
* ("https://huggingface.co/user/repo", "hf.co/user/repo:Q4_K_M", "user/repo-GGUF").
|
||||
* POST /api/v1/models/download starts a job; we poll /api/v1/models/download/status/:job_id.
|
||||
*/
|
||||
async pull(model, { onProgress = () => {}, signal, quantization } = {}) {
|
||||
const { model: m, quantization: q } = normalizeLmsModelRef(model, quantization);
|
||||
const body = { model: m };
|
||||
if (q) body.quantization = q;
|
||||
let start;
|
||||
try {
|
||||
start = await requestJson(this.url('/api/v1/models/download'), { method: 'POST', body, timeoutMs: 120000, signal });
|
||||
} catch (e) {
|
||||
if (e.status === 404) throw new EngineError(`LM Studio nie znalazł takiego modelu — podaj nazwę z katalogu (lmstudio.ai/models) albo link do repozytorium GGUF na Hugging Face. (${e.message})`, { status: 404 });
|
||||
throw e;
|
||||
}
|
||||
if (start.status === 'already_downloaded') {
|
||||
onProgress({ phase: 'done', status: 'already_downloaded', total: 1, completed: 1 });
|
||||
return { ok: true, already: true, model: m };
|
||||
}
|
||||
if (start.status === 'failed' || !start.job_id) throw new EngineError('LM Studio nie rozpoczął pobierania.');
|
||||
onProgress({ phase: 'downloading', status: start.status, total: start.total_size_bytes || 0, completed: 0 });
|
||||
for (;;) {
|
||||
await sleep(1000, signal);
|
||||
const st = await requestJson(this.url(`/api/v1/models/download/status/${encodeURIComponent(start.job_id)}`), { timeoutMs: 15000, signal });
|
||||
const etaSec = st.estimated_completion ? Math.max(0, (Date.parse(st.estimated_completion) - Date.now()) / 1000) : null;
|
||||
onProgress({
|
||||
phase: st.status === 'completed' ? 'done' : st.status, status: st.status,
|
||||
total: st.total_size_bytes || start.total_size_bytes || 0, completed: st.downloaded_bytes || 0,
|
||||
bytesPerSec: st.bytes_per_second ?? null, etaSec,
|
||||
});
|
||||
if (st.status === 'completed') return { ok: true, model: m };
|
||||
if (st.status === 'failed') throw new EngineError('LM Studio: pobieranie nie powiodło się (szczegóły w panelu Downloads w LM Studio).');
|
||||
}
|
||||
}
|
||||
|
||||
async chat({ model, messages, schema, settings = {}, maxTokens, signal }) {
|
||||
const m = await this.findModel(model).catch(() => null);
|
||||
const inst = m?.loaded_instances?.[0]?.id || model;
|
||||
const body = { model: inst, messages, stream: false };
|
||||
if (settings.temperature !== undefined && settings.temperature !== '' && settings.temperature !== null) body.temperature = Number(settings.temperature);
|
||||
if (settings.topP) body.top_p = Number(settings.topP);
|
||||
if (settings.topK) body.top_k = Number(settings.topK);
|
||||
if (settings.minP) body.min_p = Number(settings.minP);
|
||||
if (settings.repeatPenalty) body.repeat_penalty = Number(settings.repeatPenalty);
|
||||
if (settings.seed !== undefined && settings.seed !== '' && settings.seed !== null) body.seed = Number(settings.seed);
|
||||
if (maxTokens) body.max_tokens = maxTokens;
|
||||
if (schema) body.response_format = { type: 'json_schema', json_schema: { name: 'subtitles', strict: true, schema } };
|
||||
const reasoning = m?.capabilities?.reasoning;
|
||||
if (reasoning && settings.disableThinking !== false) {
|
||||
// Without this, constrained JSON ends up in reasoning_content on thinking models.
|
||||
if (reasoning.allowed_options?.includes('off')) body.reasoning_effort = 'none';
|
||||
else if (reasoning.allowed_options?.includes('low')) body.reasoning_effort = 'low';
|
||||
}
|
||||
const t0 = Date.now();
|
||||
const r = await requestJson(this.url('/v1/chat/completions'), { method: 'POST', body, timeoutMs: settings.requestTimeoutMs || 600000, signal });
|
||||
const choice = r.choices?.[0] || {};
|
||||
let text = choice.message?.content ?? '';
|
||||
if (!String(text).trim() && choice.message?.reasoning_content) text = choice.message.reasoning_content;
|
||||
const ms = Date.now() - t0;
|
||||
return {
|
||||
text,
|
||||
finishReason: choice.finish_reason === 'length' ? 'length' : 'stop',
|
||||
promptTokens: r.usage?.prompt_tokens,
|
||||
completionTokens: r.usage?.completion_tokens,
|
||||
ms,
|
||||
tokPerSec: r.stats?.tokens_per_second ?? (r.usage?.completion_tokens ? r.usage.completion_tokens / (ms / 1000) : null),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/** Turns the forms people paste (hf.co/…, user/repo-GGUF, repo:Q4_K_M) into what LM Studio expects. */
|
||||
export function normalizeLmsModelRef(input, quantization) {
|
||||
let m = String(input || '').trim();
|
||||
let q = quantization ? String(quantization).trim() : '';
|
||||
if (/^hf\.co\//i.test(m)) m = `https://huggingface.co/${m.slice(6)}`;
|
||||
else if (/^huggingface\.co\//i.test(m)) m = `https://${m}`;
|
||||
else if (!/^https?:\/\//i.test(m) && /^[\w.-]+\/[\w.-]+-gguf(:[\w.-]+)?$/i.test(m)) m = `https://huggingface.co/${m}`;
|
||||
const tag = /^(https:\/\/huggingface\.co\/[^:]+):([\w.-]+)$/i.exec(m);
|
||||
if (tag) { m = tag[1]; q ||= tag[2]; }
|
||||
m = m.replace(/\/+$/, '');
|
||||
// quantization is only accepted for Hugging Face links
|
||||
return { model: m, quantization: /^https:\/\/huggingface\.co\//i.test(m) && q ? q : undefined };
|
||||
}
|
||||
|
||||
function summarizeApplied(cfg) {
|
||||
if (!cfg) return null;
|
||||
return {
|
||||
context_length: cfg.contextLength,
|
||||
flash_attention: cfg.flashAttention,
|
||||
k_cache: cfg.llamaKCacheQuantizationType || 'f16',
|
||||
v_cache: cfg.llamaVCacheQuantizationType || 'f16',
|
||||
gpu_ratio: cfg.gpu?.ratio,
|
||||
eval_batch_size: cfg.evalBatchSize,
|
||||
parallel: cfg.maxParallelPredictions,
|
||||
unified_kv: cfg.useUnifiedKvCache,
|
||||
};
|
||||
}
|
||||
|
||||
/** setTimeout that can be aborted; cleans its listener up so long polls don't pile them up. */
|
||||
function sleep(ms, signal) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (signal?.aborted) return reject(signal.reason ?? new Error('aborted'));
|
||||
const onAbort = () => { clearTimeout(t); reject(signal.reason ?? new Error('aborted')); };
|
||||
const t = setTimeout(() => { signal?.removeEventListener('abort', onAbort); resolve(); }, ms);
|
||||
signal?.addEventListener('abort', onAbort, { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
function withTimeout(promise, ms) {
|
||||
let t;
|
||||
return Promise.race([promise, new Promise((_, rej) => { t = setTimeout(() => rej(new Error(`Timeout ${ms} ms`)), ms); })]).finally(() => clearTimeout(t));
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
import { readdir, stat, unlink } from 'node:fs/promises';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { requestJson, joinUrl, EngineError } from './http.js';
|
||||
|
||||
/** Ollama native API (https://docs.ollama.com/api). */
|
||||
export class OllamaEngine {
|
||||
kind = 'ollama';
|
||||
|
||||
constructor({ baseUrl } = {}) {
|
||||
this.baseUrl = baseUrl || 'http://127.0.0.1:11434';
|
||||
this.showCache = new Map();
|
||||
}
|
||||
|
||||
url(p) { return joinUrl(this.baseUrl, p); }
|
||||
|
||||
capabilities() {
|
||||
return {
|
||||
loadControl: false, // Ollama loads on demand; num_ctx is sent per request
|
||||
pull: true,
|
||||
kvCacheQuantization: 'server-env',
|
||||
estimate: 'own',
|
||||
notes: [
|
||||
'Kwantyzacja KV-cache w Ollamie to ustawienie serwera: OLLAMA_FLASH_ATTENTION=1 oraz OLLAMA_KV_CACHE_TYPE=q8_0 (lub q4_0) i restart Ollamy.',
|
||||
...(process.platform === 'win32' ? [
|
||||
'Windows (PowerShell/cmd): setx OLLAMA_FLASH_ATTENTION 1 oraz setx OLLAMA_KV_CACHE_TYPE q8_0 — potem zamknij Ollamę w zasobniku systemowym i uruchom ją ponownie.',
|
||||
] : process.platform === 'darwin' ? [
|
||||
'Z terminala: OLLAMA_FLASH_ATTENTION=1 OLLAMA_KV_CACHE_TYPE=q8_0 ollama serve',
|
||||
'macOS (aplikacja): launchctl setenv OLLAMA_FLASH_ATTENTION 1; launchctl setenv OLLAMA_KV_CACHE_TYPE q8_0 — potem zrestartuj Ollamę.',
|
||||
] : [
|
||||
'Linux (systemd): sudo systemctl edit ollama → [Service] Environment="OLLAMA_FLASH_ATTENTION=1" Environment="OLLAMA_KV_CACHE_TYPE=q8_0", potem sudo systemctl restart ollama',
|
||||
]),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
async status() {
|
||||
const v = await requestJson(this.url('/api/version'), { timeoutMs: 4000 });
|
||||
return { ok: true, version: v?.version };
|
||||
}
|
||||
|
||||
async listModels() {
|
||||
const [tags, ps] = await Promise.all([
|
||||
requestJson(this.url('/api/tags'), { timeoutMs: 8000 }),
|
||||
requestJson(this.url('/api/ps'), { timeoutMs: 8000 }).catch(() => ({ models: [] })),
|
||||
]);
|
||||
const loaded = new Map((ps?.models || []).map((m) => [m.name, m]));
|
||||
return (tags?.models || [])
|
||||
.filter((m) => !/embed/i.test(m.name) && !/bert/i.test(m.details?.family || ''))
|
||||
.map((m) => {
|
||||
const l = loaded.get(m.name);
|
||||
return {
|
||||
id: m.name,
|
||||
name: m.name,
|
||||
sizeBytes: m.size,
|
||||
params: m.details?.parameter_size,
|
||||
quant: m.details?.quantization_level,
|
||||
family: m.details?.family,
|
||||
format: m.details?.format,
|
||||
loaded: l ? [{ id: m.name, contextLength: l.context_length, sizeVram: l.size_vram, size: l.size, expiresAt: l.expires_at }] : [],
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async show(model) {
|
||||
if (this.showCache.has(model)) return this.showCache.get(model);
|
||||
const r = await requestJson(this.url('/api/show'), { method: 'POST', body: { model }, timeoutMs: 15000 });
|
||||
this.showCache.set(model, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
async modelInfo(model) {
|
||||
const r = await this.show(model);
|
||||
const meta = r.model_info || {};
|
||||
const arch = meta['general.architecture'];
|
||||
const params = {};
|
||||
for (const line of (r.parameters || '').split('\n')) {
|
||||
const m = /^(\S+)\s+(.+)$/.exec(line.trim());
|
||||
if (m) params[m[1]] = m[2].replace(/^"|"$/g, '');
|
||||
}
|
||||
const caps = r.capabilities || [];
|
||||
return {
|
||||
meta,
|
||||
arch,
|
||||
maxContext: meta[`${arch}.context_length`],
|
||||
capabilities: { thinking: caps.includes('thinking'), vision: caps.includes('vision'), tools: caps.includes('tools'), completion: caps.includes('completion') },
|
||||
defaults: params,
|
||||
template: r.template,
|
||||
};
|
||||
}
|
||||
|
||||
async loaded() {
|
||||
const ps = await requestJson(this.url('/api/ps'), { timeoutMs: 8000 });
|
||||
return (ps?.models || []).map((m) => ({ id: m.name, model: m.name, contextLength: m.context_length, sizeVram: m.size_vram, size: m.size, expiresAt: m.expires_at }));
|
||||
}
|
||||
|
||||
ollamaOptions(s = {}) {
|
||||
const o = {};
|
||||
if (s.contextLength) o.num_ctx = Number(s.contextLength);
|
||||
if (s.temperature !== undefined && s.temperature !== null && s.temperature !== '') o.temperature = Number(s.temperature);
|
||||
if (s.topP) o.top_p = Number(s.topP);
|
||||
if (s.topK) o.top_k = Number(s.topK);
|
||||
if (s.minP) o.min_p = Number(s.minP);
|
||||
if (s.repeatPenalty) o.repeat_penalty = Number(s.repeatPenalty);
|
||||
if (s.seed !== undefined && s.seed !== '' && s.seed !== null) o.seed = Number(s.seed);
|
||||
if (s.numGpu !== undefined && s.numGpu !== '' && s.numGpu !== null) o.num_gpu = Number(s.numGpu);
|
||||
if (s.evalBatchSize) o.num_batch = Number(s.evalBatchSize);
|
||||
return o;
|
||||
}
|
||||
|
||||
/** Loads (or reloads with a new num_ctx) by sending an empty request. */
|
||||
async load(model, settings = {}) {
|
||||
await requestJson(this.url('/api/generate'), {
|
||||
method: 'POST',
|
||||
body: { model, prompt: '', stream: false, keep_alive: settings.keepAlive || '30m', options: this.ollamaOptions(settings) },
|
||||
timeoutMs: 600000,
|
||||
});
|
||||
const l = (await this.loaded()).find((m) => m.id === model);
|
||||
return { ok: true, instance: l || null };
|
||||
}
|
||||
|
||||
async unload(model) {
|
||||
await requestJson(this.url('/api/generate'), { method: 'POST', body: { model, keep_alive: 0 }, timeoutMs: 60000 });
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
isLocal() {
|
||||
try { return ['localhost', '127.0.0.1', '::1', '[::1]'].includes(new URL(this.baseUrl).hostname); } catch { return false; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes unfinished layer files (sha256-<digest>-partial*) left by a cancelled pull. Only
|
||||
* partial files of the given digests are touched — finished blobs may be shared by other models.
|
||||
* Works when Ollama runs on this machine with the default (or OLLAMA_MODELS) folder.
|
||||
* @returns {Promise<{removed:number, bytes:number}|null>} null if the folder isn't reachable
|
||||
*/
|
||||
async discardPartial(digests, blobsDir) {
|
||||
if (!blobsDir && !this.isLocal()) return null;
|
||||
const dir = blobsDir || (process.env.OLLAMA_MODELS ? path.join(process.env.OLLAMA_MODELS, 'blobs') : path.join(os.homedir(), '.ollama', 'models', 'blobs'));
|
||||
let files;
|
||||
try { files = await readdir(dir); } catch { return null; }
|
||||
const prefixes = digests.filter((d) => /^sha256:[0-9a-f]{64}$/.test(d)).map((d) => `${d.replace(':', '-')}-partial`);
|
||||
let removed = 0, bytes = 0;
|
||||
for (const f of files) {
|
||||
if (!prefixes.some((p) => f.startsWith(p))) continue;
|
||||
const full = path.join(dir, f);
|
||||
try { bytes += (await stat(full)).size; await unlink(full); removed++; } catch { /* in use or gone */ }
|
||||
}
|
||||
return { removed, bytes };
|
||||
}
|
||||
|
||||
/**
|
||||
* Downloads a model (library name like "translategemma:12b" or "hf.co/user/repo:Q4_K_M").
|
||||
* /api/pull streams NDJSON with per-layer {digest,total,completed}; aborting the request stops
|
||||
* the download, and a later pull resumes from the partial files.
|
||||
*/
|
||||
async pull(model, { onProgress = () => {}, signal } = {}) {
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(this.url('/api/pull'), { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ model, stream: true }), signal });
|
||||
} catch (e) {
|
||||
if (signal?.aborted) throw e;
|
||||
throw new EngineError(`Nie można połączyć się z Ollamą (${e.cause?.code || e.message})`);
|
||||
}
|
||||
if (!res.ok) {
|
||||
const t = await res.text();
|
||||
let msg = t;
|
||||
try { msg = JSON.parse(t).error || t; } catch { /* plain text */ }
|
||||
throw new EngineError(explainPullError(msg, model), { status: res.status });
|
||||
}
|
||||
const layers = new Map();
|
||||
const reader = res.body.getReader();
|
||||
const dec = new TextDecoder();
|
||||
let buf = '';
|
||||
for (;;) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
buf += dec.decode(value, { stream: true });
|
||||
let nl;
|
||||
while ((nl = buf.indexOf('\n')) >= 0) {
|
||||
const line = buf.slice(0, nl).trim();
|
||||
buf = buf.slice(nl + 1);
|
||||
if (!line) continue;
|
||||
let ev;
|
||||
try { ev = JSON.parse(line); } catch { continue; }
|
||||
if (ev.error) throw new EngineError(explainPullError(ev.error, model));
|
||||
if (ev.digest && ev.total) layers.set(ev.digest, { total: ev.total, completed: ev.completed || 0 });
|
||||
let total = 0, completed = 0;
|
||||
for (const l of layers.values()) { total += l.total; completed += l.completed; }
|
||||
const s = ev.status || '';
|
||||
const phase = s === 'success' ? 'done' : /^pulling [0-9a-f]/.test(s) ? 'downloading' : /verifying/.test(s) ? 'verifying' : /writing|removing/.test(s) ? 'finalizing' : 'starting';
|
||||
onProgress({ phase, status: s, total, completed, digests: [...layers.keys()] });
|
||||
if (s === 'success') return { ok: true, model };
|
||||
}
|
||||
}
|
||||
throw new EngineError('Pobieranie przerwane — Ollama zamknęła połączenie.');
|
||||
}
|
||||
|
||||
async chat({ model, messages, schema, settings = {}, maxTokens, signal }) {
|
||||
let info = null;
|
||||
try { info = await this.modelInfo(model); } catch { /* ignore */ }
|
||||
const body = {
|
||||
model,
|
||||
messages,
|
||||
stream: false,
|
||||
keep_alive: settings.keepAlive || '30m',
|
||||
options: { ...this.ollamaOptions(settings), ...(maxTokens ? { num_predict: maxTokens } : {}) },
|
||||
};
|
||||
if (schema) body.format = schema;
|
||||
if (info?.capabilities.thinking && settings.disableThinking !== false) {
|
||||
// gpt-oss can't fully disable thinking; "low" is the minimum.
|
||||
body.think = /gpt-?oss/i.test(model) ? 'low' : false;
|
||||
}
|
||||
const t0 = Date.now();
|
||||
const r = await requestJson(this.url('/api/chat'), { method: 'POST', body, timeoutMs: settings.requestTimeoutMs || 600000, signal });
|
||||
let text = r.message?.content ?? '';
|
||||
if (!text.trim() && r.message?.thinking) text = r.message.thinking; // constrained output landed in thinking
|
||||
return {
|
||||
text,
|
||||
finishReason: r.done_reason === 'length' ? 'length' : 'stop',
|
||||
promptTokens: r.prompt_eval_count,
|
||||
completionTokens: r.eval_count,
|
||||
ms: Date.now() - t0,
|
||||
tokPerSec: r.eval_count && r.eval_duration ? r.eval_count / (r.eval_duration / 1e9) : null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/** Ollama's pull errors are terse; say what they usually mean. */
|
||||
export function explainPullError(msg, model) {
|
||||
const m = String(msg);
|
||||
if (/file does not exist|not found/i.test(m) && !/^hf\.co\//i.test(model)) {
|
||||
return `Nie ma takiego modelu w bibliotece Ollamy — sprawdź nazwę i tag (np. translategemma:12b) na ollama.com/search. (${m})`;
|
||||
}
|
||||
if (/realm host|unauthorized|401|403|does not exist/i.test(m) && /^hf\.co\//i.test(model)) {
|
||||
return `Hugging Face nie udostępnia takiego repozytorium — sprawdź nazwę (wielkość liter ma znaczenie) i kwantyzację po dwukropku, albo czy repozytorium nie jest prywatne lub nie wymaga akceptacji licencji. (${m})`;
|
||||
}
|
||||
if (/no space left/i.test(m)) return `Brak miejsca na dysku. (${m})`;
|
||||
return `Ollama: ${m}`;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
import { requestJson, joinUrl } from './http.js';
|
||||
|
||||
/**
|
||||
* Any OpenAI-compatible server: llama.cpp llama-server, vLLM, KoboldCpp, Jan, LocalAI,
|
||||
* or a cloud API. Load-time parameters (context, KV cache) are fixed at server start.
|
||||
*/
|
||||
export class OpenAICompatEngine {
|
||||
kind = 'openai';
|
||||
|
||||
constructor({ baseUrl, apiKey, extraBody, structuredOutput } = {}) {
|
||||
this.baseUrl = (baseUrl || 'http://127.0.0.1:8080').replace(/\/v1\/?$/, '');
|
||||
this.apiKey = apiKey || '';
|
||||
this.extraBody = extraBody || null;
|
||||
this.structuredOutput = structuredOutput !== false;
|
||||
}
|
||||
|
||||
url(p) { return joinUrl(this.baseUrl, p); }
|
||||
headers() { return this.apiKey ? { authorization: `Bearer ${this.apiKey}` } : {}; }
|
||||
|
||||
capabilities() {
|
||||
return {
|
||||
loadControl: false,
|
||||
kvCacheQuantization: 'server-args',
|
||||
estimate: 'own',
|
||||
notes: [
|
||||
'Kontekst i KV-cache ustawia się przy starcie serwera, np. llama-server: -c 16384 -fa on -ctk q8_0 -ctv q8_0',
|
||||
'vLLM: --max-model-len 16384 --kv-cache-dtype fp8',
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
async props() {
|
||||
// llama-server exposes /props with the real n_ctx; others 404.
|
||||
try {
|
||||
const p = await requestJson(this.url('/props'), { headers: this.headers(), timeoutMs: 3000 });
|
||||
return p && typeof p === 'object' && p.default_generation_settings ? p : null;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
async status() {
|
||||
await requestJson(this.url('/v1/models'), { headers: this.headers(), timeoutMs: 5000 });
|
||||
const props = await this.props();
|
||||
return { ok: true, version: props?.build_info || null, server: props ? 'llama-server' : null };
|
||||
}
|
||||
|
||||
async listModels() {
|
||||
const r = await requestJson(this.url('/v1/models'), { headers: this.headers(), timeoutMs: 10000 });
|
||||
const props = await this.props();
|
||||
const nCtx = props?.default_generation_settings?.n_ctx || props?.n_ctx;
|
||||
return (r?.data || []).filter((m) => !/embed/i.test(m.id)).map((m) => ({
|
||||
id: m.id,
|
||||
name: m.id,
|
||||
sizeBytes: m.meta?.size,
|
||||
params: m.meta?.n_params ? `${(m.meta.n_params / 1e9).toFixed(1)}B` : undefined,
|
||||
maxContext: m.meta?.n_ctx_train || m.max_model_len,
|
||||
loaded: nCtx || m.max_model_len ? [{ id: m.id, contextLength: nCtx || m.max_model_len }] : [],
|
||||
}));
|
||||
}
|
||||
|
||||
async modelInfo(id) {
|
||||
const models = await this.listModels();
|
||||
const m = models.find((x) => x.id === id);
|
||||
return { meta: null, maxContext: m?.maxContext, capabilities: {}, defaults: {}, serverContext: m?.loaded?.[0]?.contextLength };
|
||||
}
|
||||
|
||||
async loaded() {
|
||||
return (await this.listModels()).flatMap((m) => m.loaded.map((l) => ({ ...l, model: m.id })));
|
||||
}
|
||||
|
||||
async chat({ model, messages, schema, settings = {}, maxTokens, signal }) {
|
||||
const body = { model, messages, stream: false };
|
||||
if (settings.temperature !== undefined && settings.temperature !== '' && settings.temperature !== null) body.temperature = Number(settings.temperature);
|
||||
if (settings.topP) body.top_p = Number(settings.topP);
|
||||
if (settings.topK) body.top_k = Number(settings.topK);
|
||||
if (settings.minP) body.min_p = Number(settings.minP);
|
||||
if (settings.repeatPenalty) body.repeat_penalty = Number(settings.repeatPenalty);
|
||||
if (settings.seed !== undefined && settings.seed !== '' && settings.seed !== null) body.seed = Number(settings.seed);
|
||||
if (maxTokens) body.max_tokens = maxTokens;
|
||||
if (schema && this.structuredOutput) body.response_format = { type: 'json_schema', json_schema: { name: 'subtitles', strict: true, schema } };
|
||||
if (this.extraBody) Object.assign(body, this.extraBody);
|
||||
const t0 = Date.now();
|
||||
const r = await requestJson(this.url('/v1/chat/completions'), { method: 'POST', body, headers: this.headers(), timeoutMs: settings.requestTimeoutMs || 600000, signal });
|
||||
const choice = r.choices?.[0] || {};
|
||||
let text = choice.message?.content ?? '';
|
||||
if (!String(text).trim()) text = choice.message?.reasoning_content || choice.message?.reasoning || '';
|
||||
const ms = Date.now() - t0;
|
||||
return {
|
||||
text,
|
||||
finishReason: choice.finish_reason === 'length' ? 'length' : 'stop',
|
||||
promptTokens: r.usage?.prompt_tokens,
|
||||
completionTokens: r.usage?.completion_tokens,
|
||||
ms,
|
||||
tokPerSec: r.timings?.predicted_per_second ?? (r.usage?.completion_tokens ? r.usage.completion_tokens / (ms / 1000) : null),
|
||||
};
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
// Minimal GGUF header reader: pulls key/value metadata without loading tensors.
|
||||
// Spec: https://github.com/ggml-org/ggml/blob/master/docs/gguf.md
|
||||
import { open } from 'node:fs/promises';
|
||||
|
||||
const T = { U8: 0, I8: 1, U16: 2, I16: 3, U32: 4, I32: 5, F32: 6, BOOL: 7, STRING: 8, ARRAY: 9, U64: 10, I64: 11, F64: 12 };
|
||||
const SCALAR_SIZE = { 0: 1, 1: 1, 2: 2, 3: 2, 4: 4, 5: 4, 6: 4, 7: 1, 10: 8, 11: 8, 12: 8 };
|
||||
|
||||
// Arrays longer than this (token lists, merges...) are skipped; only their length is kept.
|
||||
const MAX_ARRAY_KEEP = 4096;
|
||||
|
||||
class Reader {
|
||||
constructor(fh) {
|
||||
this.fh = fh;
|
||||
this.pos = 0;
|
||||
this.buf = Buffer.alloc(0);
|
||||
this.bufStart = 0;
|
||||
}
|
||||
async ensure(n) {
|
||||
const off = this.pos - this.bufStart;
|
||||
if (off >= 0 && off + n <= this.buf.length) return;
|
||||
const size = Math.max(n, 1 << 20);
|
||||
const buf = Buffer.alloc(size);
|
||||
const { bytesRead } = await this.fh.read(buf, 0, size, this.pos);
|
||||
if (bytesRead < n) throw new Error('Unexpected end of GGUF file');
|
||||
this.buf = buf.subarray(0, bytesRead);
|
||||
this.bufStart = this.pos;
|
||||
}
|
||||
async u32() { await this.ensure(4); const v = this.buf.readUInt32LE(this.pos - this.bufStart); this.pos += 4; return v; }
|
||||
async u64() { await this.ensure(8); const v = this.buf.readBigUInt64LE(this.pos - this.bufStart); this.pos += 8; return Number(v); }
|
||||
async str() {
|
||||
const len = await this.u64();
|
||||
if (len > 1 << 24) throw new Error('GGUF string too long');
|
||||
await this.ensure(len);
|
||||
const o = this.pos - this.bufStart;
|
||||
const s = this.buf.toString('utf8', o, o + len);
|
||||
this.pos += len;
|
||||
return s;
|
||||
}
|
||||
async scalar(type) {
|
||||
const size = SCALAR_SIZE[type];
|
||||
await this.ensure(size);
|
||||
const b = this.buf, o = this.pos - this.bufStart;
|
||||
this.pos += size;
|
||||
switch (type) {
|
||||
case T.U8: return b.readUInt8(o);
|
||||
case T.I8: return b.readInt8(o);
|
||||
case T.U16: return b.readUInt16LE(o);
|
||||
case T.I16: return b.readInt16LE(o);
|
||||
case T.U32: return b.readUInt32LE(o);
|
||||
case T.I32: return b.readInt32LE(o);
|
||||
case T.F32: return b.readFloatLE(o);
|
||||
case T.BOOL: return b.readUInt8(o) !== 0;
|
||||
case T.U64: return Number(b.readBigUInt64LE(o));
|
||||
case T.I64: return Number(b.readBigInt64LE(o));
|
||||
case T.F64: return b.readDoubleLE(o);
|
||||
default: throw new Error(`Unknown GGUF scalar type ${type}`);
|
||||
}
|
||||
}
|
||||
async value(type) {
|
||||
if (type === T.STRING) return this.str();
|
||||
if (type !== T.ARRAY) return this.scalar(type);
|
||||
const itemType = await this.u32();
|
||||
const count = await this.u64();
|
||||
if (count > MAX_ARRAY_KEEP) {
|
||||
// Skip the content, keep a marker with the length.
|
||||
if (itemType === T.STRING) {
|
||||
for (let i = 0; i < count; i++) { const len = await this.u64(); this.pos += len; }
|
||||
} else if (SCALAR_SIZE[itemType]) {
|
||||
this.pos += SCALAR_SIZE[itemType] * count;
|
||||
} else {
|
||||
throw new Error('Nested GGUF arrays are not supported');
|
||||
}
|
||||
return { skippedArray: true, length: count };
|
||||
}
|
||||
const out = new Array(count);
|
||||
for (let i = 0; i < count; i++) out[i] = await this.value(itemType);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads GGUF metadata. Returns a flat { key: value } object (like Ollama's model_info). */
|
||||
export async function readGgufMetadata(path) {
|
||||
const fh = await open(path, 'r');
|
||||
try {
|
||||
const r = new Reader(fh);
|
||||
await r.ensure(4);
|
||||
const magic = r.buf.toString('ascii', 0, 4);
|
||||
if (magic !== 'GGUF') throw new Error('Not a GGUF file');
|
||||
r.pos = 4;
|
||||
const version = await r.u32();
|
||||
if (version < 2) throw new Error(`Unsupported GGUF version ${version}`);
|
||||
const tensorCount = await r.u64();
|
||||
const kvCount = await r.u64();
|
||||
const meta = {};
|
||||
for (let i = 0; i < kvCount; i++) {
|
||||
const key = await r.str();
|
||||
const type = await r.u32();
|
||||
meta[key] = await r.value(type);
|
||||
}
|
||||
meta['gguf.version'] = version;
|
||||
meta['gguf.tensor_count'] = tensorCount;
|
||||
return meta;
|
||||
} finally {
|
||||
await fh.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// Tiny heuristic language detector (stopwords + scripts). Good enough to prefill
|
||||
// the source language for dedicated MT models that require it.
|
||||
|
||||
const STOPWORDS = {
|
||||
en: 'the you and to is it that of what in me this my have don i\'m we your not be are was do know just no for on can with all he so',
|
||||
pl: 'nie się to że jest co na mnie jak ale tak już tu mi czy za do jestem być tylko ty może wiem go jej był dobrze',
|
||||
de: 'ich nicht die das du ist und sie der es was zu wir ein mir mich mit den hier auf ja aber dich so habe',
|
||||
fr: 'je de pas le la est vous que et les tu un ce il à ne une on c\'est des en mais suis qui moi',
|
||||
es: 'que de no la el es y en lo a un por me se qué los te una mi con las estoy pero eso esta',
|
||||
it: 'non che di è la il un per mi una sono ti ma lo cosa si io ho le con questo ci da',
|
||||
pt: 'que não de é o a um eu para se me você e do uma da com isso está os por ele mas',
|
||||
nl: 'de ik het je niet een is dat en van wat we in te zijn ze hij maar met op heb',
|
||||
cs: 'je to se na že jsem ne co v a ale tak jsi mi už jak by tady ty proč',
|
||||
sv: 'jag det är du inte att och en som på vi har han vad med för kan',
|
||||
ru: 'не что я ты это в и на он с мне как вы да так но мы у',
|
||||
uk: 'що не я ти це в і на він з мені як ви так але ми є',
|
||||
tr: 'bir bu ne ve ben sen mi için çok de da o ama değil var',
|
||||
};
|
||||
|
||||
const SETS = Object.fromEntries(Object.entries(STOPWORDS).map(([k, v]) => [k, new Set(v.split(' '))]));
|
||||
|
||||
export function detectLanguage(text) {
|
||||
const sample = text.slice(0, 20000);
|
||||
const count = (re) => (sample.match(re) || []).length;
|
||||
const letters = count(/\p{L}/gu) || 1;
|
||||
const scripts = [
|
||||
['ja', count(/[\p{Script=Hiragana}\p{Script=Katakana}]/gu)],
|
||||
['ko', count(/\p{Script=Hangul}/gu)],
|
||||
['zh', count(/\p{Script=Han}/gu)],
|
||||
['ar', count(/\p{Script=Arabic}/gu)],
|
||||
['he', count(/\p{Script=Hebrew}/gu)],
|
||||
['el', count(/\p{Script=Greek}/gu)],
|
||||
['th', count(/\p{Script=Thai}/gu)],
|
||||
['hi', count(/\p{Script=Devanagari}/gu)],
|
||||
];
|
||||
// Kana beats Han (Japanese uses both).
|
||||
if (scripts[0][1] / letters > 0.05) return 'ja';
|
||||
for (const [code, n] of scripts.slice(1)) if (n / letters > 0.3) return code;
|
||||
|
||||
const words = sample.toLowerCase().match(/[\p{L}']+/gu) || [];
|
||||
if (!words.length) return null;
|
||||
const scores = {};
|
||||
for (const [lang, set] of Object.entries(SETS)) {
|
||||
let s = 0;
|
||||
for (const w of words) if (set.has(w)) s++;
|
||||
scores[lang] = s / words.length;
|
||||
}
|
||||
// Cyrillic: decide between ru/uk by distinctive letters.
|
||||
if (count(/\p{Script=Cyrillic}/gu) / letters > 0.3) {
|
||||
return count(/[іїєґ]/giu) > count(/[ыэъё]/giu) ? 'uk' : 'ru';
|
||||
}
|
||||
const best = Object.entries(scores).sort((a, b) => b[1] - a[1])[0];
|
||||
return best && best[1] > 0.04 ? best[0] : null;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// code, English name (used in prompts), Polish name (UI)
|
||||
export const LANGUAGES = [
|
||||
['pl', 'Polish', 'polski'],
|
||||
['en', 'English', 'angielski'],
|
||||
['de', 'German', 'niemiecki'],
|
||||
['fr', 'French', 'francuski'],
|
||||
['es', 'Spanish', 'hiszpański'],
|
||||
['it', 'Italian', 'włoski'],
|
||||
['pt', 'Portuguese', 'portugalski'],
|
||||
['pt-BR', 'Brazilian Portuguese', 'portugalski (Brazylia)'],
|
||||
['nl', 'Dutch', 'niderlandzki'],
|
||||
['sv', 'Swedish', 'szwedzki'],
|
||||
['no', 'Norwegian', 'norweski'],
|
||||
['da', 'Danish', 'duński'],
|
||||
['fi', 'Finnish', 'fiński'],
|
||||
['cs', 'Czech', 'czeski'],
|
||||
['sk', 'Slovak', 'słowacki'],
|
||||
['uk', 'Ukrainian', 'ukraiński'],
|
||||
['ru', 'Russian', 'rosyjski'],
|
||||
['be', 'Belarusian', 'białoruski'],
|
||||
['lt', 'Lithuanian', 'litewski'],
|
||||
['lv', 'Latvian', 'łotewski'],
|
||||
['et', 'Estonian', 'estoński'],
|
||||
['hu', 'Hungarian', 'węgierski'],
|
||||
['ro', 'Romanian', 'rumuński'],
|
||||
['bg', 'Bulgarian', 'bułgarski'],
|
||||
['hr', 'Croatian', 'chorwacki'],
|
||||
['sr', 'Serbian', 'serbski'],
|
||||
['sl', 'Slovenian', 'słoweński'],
|
||||
['el', 'Greek', 'grecki'],
|
||||
['tr', 'Turkish', 'turecki'],
|
||||
['ar', 'Arabic', 'arabski'],
|
||||
['he', 'Hebrew', 'hebrajski'],
|
||||
['fa', 'Persian', 'perski'],
|
||||
['hi', 'Hindi', 'hindi'],
|
||||
['th', 'Thai', 'tajski'],
|
||||
['vi', 'Vietnamese', 'wietnamski'],
|
||||
['id', 'Indonesian', 'indonezyjski'],
|
||||
['ms', 'Malay', 'malajski'],
|
||||
['zh', 'Simplified Chinese', 'chiński (uproszczony)'],
|
||||
['zh-TW', 'Traditional Chinese', 'chiński (tradycyjny)'],
|
||||
['ja', 'Japanese', 'japoński'],
|
||||
['ko', 'Korean', 'koreański'],
|
||||
].map(([code, name, namePl]) => ({ code, name, namePl }));
|
||||
|
||||
export function langByCode(code) {
|
||||
return LANGUAGES.find((l) => l.code === code) || null;
|
||||
}
|
||||
|
||||
/** Language display name for prompts; accepts a code or a free-form name. */
|
||||
export function langName(codeOrName) {
|
||||
if (!codeOrName) return null;
|
||||
return langByCode(codeOrName)?.name || codeOrName;
|
||||
}
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
// Prompt profiles. "generic" = instruction-following chat LLM (batched, JSON or numbered
|
||||
// lines). Dedicated MT models translate one segment per request with their own template.
|
||||
import { langName } from './languages.js';
|
||||
|
||||
const ZH_NAMES = {
|
||||
en: '英语', pl: '波兰语', de: '德语', fr: '法语', es: '西班牙语', it: '意大利语', pt: '葡萄牙语', 'pt-BR': '葡萄牙语',
|
||||
ru: '俄语', uk: '乌克兰语', cs: '捷克语', nl: '荷兰语', tr: '土耳其语', ar: '阿拉伯语', ja: '日语', ko: '韩语',
|
||||
vi: '越南语', th: '泰语', id: '印尼语', ms: '马来语', zh: '中文', 'zh-TW': '繁体中文', hi: '印地语', fa: '波斯语', he: '希伯来语',
|
||||
};
|
||||
|
||||
export const PROFILES = {
|
||||
generic: {
|
||||
label: 'Uniwersalny LLM (czat)',
|
||||
modes: ['json', 'lines', 'single'],
|
||||
defaultMode: 'json',
|
||||
},
|
||||
translategemma: {
|
||||
label: 'TranslateGemma (Google)',
|
||||
match: /translate-?gemma/i,
|
||||
modes: ['single'],
|
||||
defaultMode: 'single',
|
||||
needsSource: true,
|
||||
recommended: { temperature: 0 },
|
||||
note: 'Oficjalny szablon promptu od Google.',
|
||||
},
|
||||
hunyuan: {
|
||||
label: 'Hunyuan-MT / HY-MT (Tencent)',
|
||||
match: /hunyuan-?mt|hy-?mt|hymt/i,
|
||||
modes: ['single'],
|
||||
defaultMode: 'single',
|
||||
recommended: { temperature: 0.7, top_p: 0.6, top_k: 20, repeat_penalty: 1.05 },
|
||||
note: 'Szablon z karty modelu; parametry zalecane przez Tencent.',
|
||||
},
|
||||
seedx: {
|
||||
label: 'Seed-X (ByteDance)',
|
||||
match: /seed-?x/i,
|
||||
modes: ['single'],
|
||||
defaultMode: 'single',
|
||||
needsSource: true,
|
||||
recommended: { temperature: 0 },
|
||||
},
|
||||
tower: {
|
||||
label: 'Tower (Unbabel)',
|
||||
match: /tower(instruct|-?plus|\+)?/i,
|
||||
modes: ['single'],
|
||||
defaultMode: 'single',
|
||||
needsSource: true,
|
||||
recommended: { temperature: 0 },
|
||||
},
|
||||
};
|
||||
|
||||
export function detectProfile(modelName = '') {
|
||||
for (const [key, p] of Object.entries(PROFILES)) if (p.match && p.match.test(modelName)) return key;
|
||||
return 'generic';
|
||||
}
|
||||
|
||||
function codeFor(code) {
|
||||
return (code || '').split('-')[0];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Generic LLM prompts.
|
||||
|
||||
export function genericSystemPrompt({ source, target, notes }) {
|
||||
const tgt = langName(target);
|
||||
const src = source && source !== 'auto' ? langName(source) : null;
|
||||
return [
|
||||
`You are a professional subtitle translator. Translate subtitles ${src ? `from ${src} ` : ''}into ${tgt}.`,
|
||||
'',
|
||||
'Rules:',
|
||||
`- Produce natural, idiomatic ${tgt} as it is spoken in films and TV. Keep meaning, tone, register and humour.`,
|
||||
'- Subtitles must be quick to read: be concise, do not pad or explain.',
|
||||
'- Use the surrounding subtitles to resolve context (who is speaking, gender, formality, sentences continuing across entries).',
|
||||
'- Translate every entry separately: never merge, split, skip or reorder entries.',
|
||||
'- A sentence often continues over several entries: translate each part in its own entry, splitting the translated sentence at a similar point.',
|
||||
'- Keep line breaks inside an entry where sensible, keep dialogue dashes ("- ") at line starts.',
|
||||
'- Keep tags like <i>, <b>, <font ...> and symbols like ♪ exactly as in the source.',
|
||||
`- Keep names as they are unless they have an established ${tgt} form. No notes, no transliterations.`,
|
||||
'- Never give alternative forms separated by a slash (e.g. gender variants like "was/were"): pick the single most likely form from context.',
|
||||
'- If an entry is already in the target language or untranslatable (e.g. a name or a sound), copy it.',
|
||||
notes ? `\nAdditional context from the user (follow it):\n${notes.trim()}` : '',
|
||||
].join('\n').trim();
|
||||
}
|
||||
|
||||
const fmtCtx = (items, withTranslation) =>
|
||||
items.map((c) => (withTranslation && c.translation
|
||||
? `[${c.id}] ${oneLine(c.text)} => ${oneLine(c.translation)}`
|
||||
: `[${c.id}] ${oneLine(c.text)}`)).join('\n');
|
||||
|
||||
export const oneLine = (s) => s.replace(/\n/g, ' <br> ');
|
||||
|
||||
/** JSON mode: input is an object {id: text}; output is constrained by a JSON schema with exactly these keys. */
|
||||
export function genericJsonUserPrompt({ batch, before, after }) {
|
||||
const parts = [];
|
||||
if (before.length) parts.push('Previous subtitles (context only — already translated, do not output them):\n' + fmtCtx(before, true));
|
||||
if (after.length) parts.push('Next subtitles (context only, do not output them):\n' + fmtCtx(after, false));
|
||||
// Input as numbered lines (not JSON): small models tend to copy a JSON input verbatim.
|
||||
parts.push(
|
||||
`Translate these ${batch.length} subtitles. "<br>" marks a line break inside a subtitle.\n` +
|
||||
'Answer with a JSON object. Each key is a label: the subtitle id and its first words (e.g. "12|Where were you…"). ' +
|
||||
'Its value is the translation of that WHOLE subtitle, complete, exactly as listed below (use \\n for line breaks).\n\n' +
|
||||
batch.map((c) => `[${c.id}] ${oneLine(c.text)}`).join('\n'),
|
||||
);
|
||||
return parts.join('\n\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON keys carry the start of the source ("17|The partner's office"). The grammar forces the
|
||||
* model to write that key right before each translation, which anchors every value to its
|
||||
* subtitle and stops the classic "everything shifted by one line" failure.
|
||||
*/
|
||||
export function jsonKey(c) {
|
||||
const words = c.text.replace(/<[^>]+>|\{\\[^}]*\}/g, '').replace(/["\\\u0000-\u001f]+/g, ' ').split(/\s+/).filter(Boolean);
|
||||
let snip = '';
|
||||
for (const w of words) {
|
||||
if ((snip + ' ' + w).trim().length > 22) break;
|
||||
snip = (snip + ' ' + w).trim();
|
||||
if (snip.split(' ').length >= 3) break;
|
||||
}
|
||||
if (!snip && words.length) snip = words[0].slice(0, 22);
|
||||
// The ellipsis marks the key as a label, so the model translates the whole subtitle, not the snippet.
|
||||
return `${c.id}|${snip}${words.join(' ').length > snip.length ? '…' : ''}`;
|
||||
}
|
||||
|
||||
export function jsonSchemaFor(batch) {
|
||||
const properties = {};
|
||||
for (const c of batch) properties[jsonKey(c)] = { type: 'string' };
|
||||
return { type: 'object', properties, required: batch.map(jsonKey), additionalProperties: false };
|
||||
}
|
||||
|
||||
/** Looks a translation up by anchored key, tolerating models that dropped the snippet. */
|
||||
export function pickJsonValue(obj, c) {
|
||||
if (typeof obj[jsonKey(c)] === 'string') return obj[jsonKey(c)];
|
||||
if (typeof obj[String(c.id)] === 'string') return obj[String(c.id)];
|
||||
const k = Object.keys(obj).find((x) => x.startsWith(`${c.id}|`) || x.startsWith(`[${c.id}]`));
|
||||
return k !== undefined && typeof obj[k] === 'string' ? obj[k] : undefined;
|
||||
}
|
||||
|
||||
/** Numbered-lines mode: "[12] text" in, "[12] translation" out. Line breaks become <br>. */
|
||||
export function genericLinesUserPrompt({ batch, before, after }) {
|
||||
const parts = [];
|
||||
if (before.length) parts.push('Previous subtitles (context only — already translated, do not output them):\n' + fmtCtx(before, true));
|
||||
if (after.length) parts.push('Next subtitles (context only, do not output them):\n' + fmtCtx(after, false));
|
||||
parts.push(
|
||||
`Translate the ${batch.length} subtitles below. Output exactly ${batch.length} lines, one per subtitle, ` +
|
||||
'each starting with its [id] exactly as in the input. "<br>" marks a line break — keep it. ' +
|
||||
'Output only these lines: no analysis, no drafts, no comments.\n\n' +
|
||||
batch.map((c) => `[${c.id}] ${oneLine(c.text)}`).join('\n'),
|
||||
);
|
||||
return parts.join('\n\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses "[id] text" output. Some models "think aloud" in plain content (restating the
|
||||
* input, drafting "src -> translation" plans) before the answer, so we take the LAST run
|
||||
* of markers that contains all expected ids in order and doesn't look like a draft.
|
||||
*/
|
||||
export function parseNumberedLines(text, expectedIds) {
|
||||
const marks = [];
|
||||
const re = /\[(\d+)\]/g;
|
||||
let m;
|
||||
while ((m = re.exec(text))) marks.push({ id: Number(m[1]), start: m.index, end: m.index + m[0].length });
|
||||
const ids = expectedIds?.length ? expectedIds : [...new Set(marks.map((x) => x.id))];
|
||||
const clean = (s) => s.replace(/\s*<br\s*\/?>\s*/gi, '\n').trim();
|
||||
const starts = marks.map((x, i) => (x.id === ids[0] ? i : -1)).filter((i) => i >= 0).reverse();
|
||||
for (const fi of starts) {
|
||||
const seq = [];
|
||||
let j = fi;
|
||||
for (const id of ids) {
|
||||
while (j < marks.length && marks[j].id !== id) j++;
|
||||
if (j >= marks.length) break;
|
||||
seq.push(marks[j++]);
|
||||
}
|
||||
if (seq.length !== ids.length) continue;
|
||||
const out = new Map();
|
||||
let draft = false;
|
||||
for (let k = 0; k < seq.length; k++) {
|
||||
let seg = text.slice(seq[k].end, k + 1 < seq.length ? seq[k + 1].start : text.length);
|
||||
if (k + 1 === seq.length) seg = seg.replace(/^\s+/, '').split('\n')[0];
|
||||
seg = seg.trim();
|
||||
if (/\s(->|=>|→)\s/.test(seg)) { draft = true; break; }
|
||||
out.set(seq[k].id, clean(seg));
|
||||
}
|
||||
if (!draft) return out;
|
||||
}
|
||||
// Partial result (lets the caller report which id is missing).
|
||||
const out = new Map();
|
||||
for (const line of text.split('\n')) {
|
||||
const lm = /^\s*\[(\d+)\]\s?(.*)$/.exec(line);
|
||||
if (lm && !/\s(->|=>|→)\s/.test(lm[2])) out.set(Number(lm[1]), clean(lm[2]));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function genericSingleMessages({ text, before, source, target, notes }) {
|
||||
const system = genericSystemPrompt({ source, target, notes });
|
||||
const ctx = before.length ? 'Previous subtitles (context only):\n' + fmtCtx(before, true) + '\n\n' : '';
|
||||
return [
|
||||
{ role: 'system', content: system },
|
||||
{ role: 'user', content: `${ctx}Translate this subtitle into ${langName(target)}. Output only the translation:\n\n${text}` },
|
||||
];
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dedicated MT model templates (one segment per request, user message only).
|
||||
|
||||
export function mtMessages(profile, { text, before, source, target }) {
|
||||
const tgt = langName(target);
|
||||
const srcCode = source && source !== 'auto' ? source : 'en';
|
||||
const src = langName(srcCode);
|
||||
let content;
|
||||
switch (profile) {
|
||||
case 'translategemma':
|
||||
content =
|
||||
`You are a professional ${src} (${srcCode}) to ${tgt} (${target}) translator. ` +
|
||||
`Your goal is to accurately convey the meaning and nuances of the original ${src} text ` +
|
||||
`while adhering to ${tgt} grammar, vocabulary, and cultural sensitivities.\n` +
|
||||
`Produce only the ${tgt} translation, without any additional explanations or commentary. ` +
|
||||
`Please translate the following ${src} text into ${tgt}:\n\n\n${text}`;
|
||||
break;
|
||||
case 'hunyuan': {
|
||||
const zh = codeFor(srcCode) === 'zh' || codeFor(target) === 'zh';
|
||||
if (before?.length) {
|
||||
const ctx = before.map((c) => c.text.replace(/\n/g, ' ')).join('\n');
|
||||
content = `${ctx}\n参考上面的信息,把下面的文本翻译成${ZH_NAMES[target] || tgt},注意不需要翻译上文,也不要额外解释:\n${text}`;
|
||||
} else if (zh) {
|
||||
content = `将以下文本翻译为${ZH_NAMES[target] || tgt},注意只需要输出翻译后的结果,不要额外解释:\n\n${text}`;
|
||||
} else {
|
||||
content = `Translate the following segment into ${tgt}, without additional explanation.\n\n${text}`;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 'seedx':
|
||||
content = `Translate the following ${src} sentence into ${tgt}:\n${text} <${codeFor(target)}>`;
|
||||
break;
|
||||
case 'tower':
|
||||
content = `Translate the following text from ${src} into ${tgt}.\n${src}: ${text}\n${tgt}:`;
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unknown MT profile ${profile}`);
|
||||
}
|
||||
return [{ role: 'user', content }];
|
||||
}
|
||||
|
||||
/** Rough token estimate when no tokenizer is available (refined at runtime from real usage). */
|
||||
export function estimateTokens(text, charsPerToken = 3.2) {
|
||||
return Math.ceil(text.length / charsPerToken);
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
// Model downloads run on the server, so they keep going when the browser tab is closed;
|
||||
// the UI just (re)attaches to their progress over SSE.
|
||||
//
|
||||
// One PullJob = one tile in the UI for its whole life:
|
||||
// running ⇄ paused (pause keeps the partial files, resume continues from them)
|
||||
// running → done | error | cancelled (error can be retried = resumed)
|
||||
// Cancel stops the download and deletes the partial files of this download.
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
|
||||
const fmtMB = (b) => `${Math.round(b / 2 ** 20)} MB`;
|
||||
|
||||
export class PullJob extends EventEmitter {
|
||||
constructor({ engine, engineKind, model, quantization }) {
|
||||
super();
|
||||
this.setMaxListeners(50);
|
||||
this.id = randomUUID();
|
||||
this.engine = engine;
|
||||
this.engineKind = engineKind;
|
||||
this.model = model;
|
||||
this.quantization = quantization || undefined;
|
||||
this.state = 'idle';
|
||||
this.error = null;
|
||||
this.note = null;
|
||||
this.startedAt = Date.now();
|
||||
this.finishedAt = null;
|
||||
this.progress = { phase: 'starting', status: '', completed: 0, total: 0, bytesPerSec: null, etaSec: null };
|
||||
this.digests = new Set();
|
||||
this.abort = null;
|
||||
this.runPromise = null;
|
||||
}
|
||||
|
||||
get controls() {
|
||||
const caps = this.engine.capabilities?.() || {};
|
||||
return { pause: caps.pullCancel !== false, cancel: caps.pullCancel !== false };
|
||||
}
|
||||
|
||||
snapshot() {
|
||||
return {
|
||||
id: this.id, engineKind: this.engineKind, model: this.model, state: this.state, error: this.error, note: this.note,
|
||||
progress: this.progress, startedAt: this.startedAt, finishedAt: this.finishedAt, controls: this.controls,
|
||||
};
|
||||
}
|
||||
|
||||
isTerminal() { return this.state === 'done' || this.state === 'cancelled'; }
|
||||
|
||||
/** Starts (or restarts after pause/error) the download. */
|
||||
start() {
|
||||
if (this.state === 'running' || this.isTerminal()) return false;
|
||||
this.state = 'running';
|
||||
this.error = null;
|
||||
this.note = null;
|
||||
this.finishedAt = null;
|
||||
this.samples = [];
|
||||
this.lastEmit = 0;
|
||||
this.progress = { ...this.progress, phase: 'starting', bytesPerSec: null, etaSec: null };
|
||||
this.abort = new AbortController();
|
||||
this.emit('state', this.snapshot());
|
||||
this.runPromise = this.run(this.abort);
|
||||
return true;
|
||||
}
|
||||
|
||||
async run(ctrl) {
|
||||
try {
|
||||
const r = await this.engine.pull(this.model, { quantization: this.quantization, signal: ctrl.signal, onProgress: (p) => this.update(p) });
|
||||
if (ctrl.signal.aborted) return;
|
||||
this.state = 'done';
|
||||
this.result = r;
|
||||
if (r?.already) this.note = 'Ten model jest już pobrany.';
|
||||
this.progress = { ...this.progress, phase: 'done', completed: this.progress.total || this.progress.completed, bytesPerSec: null, etaSec: null };
|
||||
this.finishedAt = Date.now();
|
||||
this.emit('state', this.snapshot());
|
||||
} catch (e) {
|
||||
if (ctrl.signal.aborted) return; // pause()/cancel() set the state themselves
|
||||
this.state = 'error';
|
||||
this.error = e.message;
|
||||
this.finishedAt = Date.now();
|
||||
this.emit('state', this.snapshot());
|
||||
}
|
||||
}
|
||||
|
||||
update(p) {
|
||||
if (this.state !== 'running') return;
|
||||
const now = Date.now();
|
||||
const prevPhase = this.progress.phase;
|
||||
if (p.digests) for (const d of p.digests) this.digests.add(d);
|
||||
const rest = { ...p };
|
||||
delete rest.digests;
|
||||
const pr = { ...this.progress, ...rest };
|
||||
if (p.bytesPerSec == null && pr.total) {
|
||||
// Speed over the last ~5 s (Ollama doesn't report it). Samples restart on resume.
|
||||
this.samples.push([now, pr.completed]);
|
||||
while (this.samples.length > 2 && now - this.samples[0][0] > 5000) this.samples.shift();
|
||||
const [t0, c0] = this.samples[0];
|
||||
pr.bytesPerSec = now - t0 > 800 && pr.completed >= c0 ? ((pr.completed - c0) * 1000) / (now - t0) : null;
|
||||
}
|
||||
if (p.etaSec == null) pr.etaSec = pr.bytesPerSec > 0 && pr.total ? (pr.total - pr.completed) / pr.bytesPerSec : null;
|
||||
this.progress = pr;
|
||||
// At most ~4 updates per second, but never drop a phase change.
|
||||
if (now - this.lastEmit > 250 || pr.phase !== prevPhase || pr.phase === 'done') {
|
||||
this.lastEmit = now;
|
||||
this.emit('progress', this.snapshot());
|
||||
}
|
||||
}
|
||||
|
||||
/** Stops downloading but keeps what was downloaded, so resume() continues from there. */
|
||||
async pause() {
|
||||
if (this.state !== 'running' || !this.controls.pause) return false;
|
||||
this.state = 'paused';
|
||||
this.progress = { ...this.progress, bytesPerSec: null, etaSec: null };
|
||||
this.note = this.progress.total
|
||||
? `Wstrzymano przy ${fmtMB(this.progress.completed)} z ${fmtMB(this.progress.total)}. „Wznów” kontynuuje od tego miejsca.`
|
||||
: 'Wstrzymano. „Wznów” kontynuuje od tego miejsca.';
|
||||
this.abort.abort(new Error('Wstrzymano'));
|
||||
await this.runPromise?.catch(() => {});
|
||||
this.emit('state', this.snapshot());
|
||||
return true;
|
||||
}
|
||||
|
||||
resume() { return this.state === 'paused' || this.state === 'error' ? this.start() : false; }
|
||||
|
||||
/** Stops the download (if running) and deletes this download's unfinished files. */
|
||||
async cancel() {
|
||||
if (this.isTerminal() || !this.controls.cancel) return false;
|
||||
const wasRunning = this.state === 'running';
|
||||
this.state = 'cancelled';
|
||||
this.finishedAt = Date.now();
|
||||
if (wasRunning) {
|
||||
this.abort.abort(new Error('Anulowano'));
|
||||
await this.runPromise?.catch(() => {});
|
||||
await new Promise((r) => setTimeout(r, 400)); // let the engine close its files
|
||||
}
|
||||
let cleaned = null;
|
||||
if (this.engine.discardPartial && this.digests.size) {
|
||||
try { cleaned = await this.engine.discardPartial([...this.digests]); } catch { cleaned = null; }
|
||||
}
|
||||
// Ollama preallocates partial files to the full layer size, so report what was actually downloaded.
|
||||
this.note = cleaned
|
||||
? `Anulowano${cleaned.removed ? ` — usunięto niedokończone pliki (${fmtMB(this.progress.completed || 0)} pobranych danych).` : '.'}`
|
||||
: 'Anulowano. Niedokończonych plików nie dało się usunąć z tego komputera (Ollama działa gdzie indziej lub w innym folderze).';
|
||||
this.emit('state', this.snapshot());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
// SRT parsing/serialization. Timing lines are kept verbatim, so translation can never
|
||||
// shift timestamps: the LLM only ever sees and returns text keyed by cue id.
|
||||
|
||||
const TIMING_RE = /^\s*(\d{1,2}:\d{1,2}:\d{1,2}(?:[,.]\d{1,3})?)\s*-->\s*(\d{1,2}:\d{1,2}:\d{1,2}(?:[,.]\d{1,3})?)(.*)$/;
|
||||
|
||||
export function timeToMs(t) {
|
||||
const m = /^(\d+):(\d+):(\d+)(?:[,.](\d+))?$/.exec(t.trim());
|
||||
if (!m) return 0;
|
||||
const ms = m[4] ? Number(m[4].padEnd(3, '0').slice(0, 3)) : 0;
|
||||
return ((Number(m[1]) * 60 + Number(m[2])) * 60 + Number(m[3])) * 1000 + ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses SRT text. Uses timing lines as anchors (more tolerant than relying on blank
|
||||
* lines): everything between two timing lines belongs to the earlier cue, except the
|
||||
* numeric index line right before the next timing line.
|
||||
* @returns {{cues: {id:number,timing:string,start:string,end:string,startMs:number,endMs:number,text:string}[], eol:string}}
|
||||
*/
|
||||
export function parseSrt(input) {
|
||||
const text = input.replace(/^/, '');
|
||||
const eol = /\r\n/.test(text) ? '\r\n' : '\n';
|
||||
const lines = text.split(/\r\n|\r|\n/);
|
||||
const anchors = [];
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
const m = TIMING_RE.exec(lines[i]);
|
||||
if (m) anchors.push({ line: i, m });
|
||||
}
|
||||
const cues = [];
|
||||
let prevIndex = anchors.length && /^\s*\d+\s*$/.test(lines[anchors[0].line - 1] || '') ? Number(lines[anchors[0].line - 1]) : null;
|
||||
for (let a = 0; a < anchors.length; a++) {
|
||||
const { line, m } = anchors[a];
|
||||
let endLine = a + 1 < anchors.length ? anchors[a + 1].line : lines.length;
|
||||
// Drop the next cue's index number (and blanks) from the tail.
|
||||
let body = lines.slice(line + 1, endLine);
|
||||
while (body.length && body[body.length - 1].trim() === '') body.pop();
|
||||
let nextIndex = null;
|
||||
if (a + 1 < anchors.length && body.length && /^\s*\d+\s*$/.test(body[body.length - 1])) {
|
||||
const n = Number(body[body.length - 1]);
|
||||
const beforeIdx = body.length >= 2 ? body[body.length - 2] : '';
|
||||
// It's the next cue's index if separated by a blank line, the only line, or the expected number
|
||||
// (files with missing blank lines are common; a numeric-only dialogue line isn't).
|
||||
if (body.length === 1 || beforeIdx.trim() === '' || (prevIndex !== null && n === prevIndex + 1)) {
|
||||
body.pop();
|
||||
nextIndex = n;
|
||||
}
|
||||
}
|
||||
prevIndex = nextIndex ?? (prevIndex !== null ? prevIndex + 1 : null);
|
||||
body = body.map((l) => l.replace(/\s+$/, '')).filter((l) => l.trim() !== '');
|
||||
const extra = m[3].trim();
|
||||
cues.push({
|
||||
id: cues.length + 1,
|
||||
start: m[1],
|
||||
end: m[2],
|
||||
timing: `${m[1]} --> ${m[2]}${extra ? ' ' + extra : ''}`,
|
||||
startMs: timeToMs(m[1]),
|
||||
endMs: timeToMs(m[2]),
|
||||
text: body.join('\n'),
|
||||
});
|
||||
}
|
||||
return { cues, eol };
|
||||
}
|
||||
|
||||
/** Serializes cues; `getText(cue)` returns the text to write (default: cue.text). */
|
||||
export function serializeSrt(cues, { eol = '\n', getText = (c) => c.text } = {}) {
|
||||
const out = [];
|
||||
cues.forEach((c, i) => {
|
||||
const t = (getText(c) ?? '').split('\n').filter((l) => l.trim() !== '').join(eol);
|
||||
out.push(`${i + 1}${eol}${c.timing}${eol}${t}${eol}`);
|
||||
});
|
||||
return out.join(eol);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Encoding detection (Polish subtitles in the wild are often windows-1250).
|
||||
|
||||
const CANDIDATES = ['windows-1250', 'windows-1252', 'windows-1251', 'iso-8859-2'];
|
||||
const DISTINCTIVE = new Set(('się że już być był była może jeszcze więc też cię mną żeby będzie tę ją ów ' +
|
||||
'qué está más también así él sí aquí allí día después ' +
|
||||
'à déjà été être très où ça même voilà ' +
|
||||
'è perché però più già ' +
|
||||
'für über können müssen schön würde wäre ' +
|
||||
'že není máš jsem být když tě mě řekl ' +
|
||||
'что это как так мне вы да нет все ' +
|
||||
'não você está então também até').split(' '));
|
||||
|
||||
function scoreDecoding(text) {
|
||||
const words = text.toLowerCase().match(/[\p{L}']+/gu) || [];
|
||||
let hits = 0;
|
||||
for (const w of words) if (DISTINCTIVE.has(w)) hits++;
|
||||
// Symbols/control chars glued to letters are a sign of a wrong code page.
|
||||
const garbage = (text.match(/[\p{L}][\u0080-¿\u0080-\u009F†-›][\p{L}]?/gu) || []).length;
|
||||
return hits - garbage * 3;
|
||||
}
|
||||
|
||||
export function decodeSubtitleBuffer(buf, forced) {
|
||||
if (forced && forced !== 'auto') {
|
||||
return { text: new TextDecoder(forced).decode(buf), encoding: forced };
|
||||
}
|
||||
if (buf[0] === 0xef && buf[1] === 0xbb && buf[2] === 0xbf) return { text: new TextDecoder('utf-8').decode(buf.subarray(3)), encoding: 'utf-8-bom' };
|
||||
if (buf[0] === 0xff && buf[1] === 0xfe) return { text: new TextDecoder('utf-16le').decode(buf.subarray(2)), encoding: 'utf-16le' };
|
||||
if (buf[0] === 0xfe && buf[1] === 0xff) return { text: new TextDecoder('utf-16be').decode(buf.subarray(2)), encoding: 'utf-16be' };
|
||||
try {
|
||||
return { text: new TextDecoder('utf-8', { fatal: true }).decode(buf), encoding: 'utf-8' };
|
||||
} catch { /* not UTF-8 */ }
|
||||
let best = null;
|
||||
for (const enc of CANDIDATES) {
|
||||
let text;
|
||||
try { text = new TextDecoder(enc).decode(buf); } catch { continue; }
|
||||
const score = scoreDecoding(text);
|
||||
if (!best || score > best.score) best = { text, encoding: enc, score };
|
||||
}
|
||||
return { text: best.text, encoding: best.encoding };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Formatting helpers.
|
||||
|
||||
const WRAP_TAGS = ['i', 'b', 'u', 'font'];
|
||||
|
||||
function stripWrap(s) {
|
||||
for (const tag of WRAP_TAGS) {
|
||||
const re = new RegExp(`^(<${tag}(?:\\s[^>]*)?>)([\\s\\S]*)(</${tag}>)$`, 'i');
|
||||
const m = re.exec(s);
|
||||
if (m && !new RegExp(`</?${tag}[\\s>]`, 'i').test(m[2])) return { open: m[1], inner: m[2], close: m[3] };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pulls out formatting that wraps the whole cue (ASS {\an8}, <i>…</i> around the
|
||||
* whole cue or around every line) so the model sees clean text. `restore` puts it back.
|
||||
*/
|
||||
export function protectFormatting(text) {
|
||||
let prefix = '';
|
||||
const ass = /^((?:\{\\[^}]*\})+)/.exec(text);
|
||||
if (ass) { prefix = ass[1]; text = text.slice(prefix.length); }
|
||||
|
||||
const wholeWraps = [];
|
||||
for (let k = 0; k < 3; k++) {
|
||||
const w = stripWrap(text.trim());
|
||||
if (!w) break;
|
||||
wholeWraps.push(w);
|
||||
text = w.inner;
|
||||
}
|
||||
let lineWrap = null;
|
||||
const lines = text.split('\n');
|
||||
if (lines.length > 1) {
|
||||
const ws = lines.map((l) => stripWrap(l.trim()));
|
||||
if (ws.every((w) => w && w.open === ws[0].open)) {
|
||||
lineWrap = ws[0];
|
||||
text = ws.map((w) => w.inner).join('\n');
|
||||
}
|
||||
}
|
||||
const restore = (t) => {
|
||||
let out = t;
|
||||
if (lineWrap) out = out.split('\n').map((l) => `${lineWrap.open}${l}${lineWrap.close}`).join('\n');
|
||||
for (let k = wholeWraps.length - 1; k >= 0; k--) out = `${wholeWraps[k].open}${out}${wholeWraps[k].close}`;
|
||||
return prefix + out;
|
||||
};
|
||||
return { text, restore };
|
||||
}
|
||||
|
||||
export function hasTranslatableText(text) {
|
||||
return /\p{L}/u.test(text.replace(/<[^>]+>|\{\\[^}]*\}/g, ''));
|
||||
}
|
||||
|
||||
export function isDialogue(text) {
|
||||
const lines = text.split('\n');
|
||||
return lines.length > 1 && lines.filter((l) => /^\s*[-–—]/.test(l)).length >= 2;
|
||||
}
|
||||
|
||||
const visibleLength = (s) => s.replace(/<[^>]+>|\{\\[^}]*\}/g, '').length;
|
||||
|
||||
/**
|
||||
* Re-wraps a translated cue so lines are not longer than maxLen (max 2 lines, split near
|
||||
* the middle, preferring punctuation). Dialogue lines and short text are left alone.
|
||||
*/
|
||||
export function rewrap(text, maxLen = 42) {
|
||||
const t = text.trim();
|
||||
if (!maxLen || maxLen <= 0) return t;
|
||||
const lines = t.split('\n').map((l) => l.trim()).filter(Boolean);
|
||||
if (lines.every((l) => visibleLength(l) <= maxLen)) return lines.join('\n');
|
||||
if (isDialogue(lines.join('\n'))) return lines.join('\n');
|
||||
const flat = lines.join(' ').replace(/\s+/g, ' ');
|
||||
if (visibleLength(flat) <= maxLen) return flat;
|
||||
const mid = flat.length / 2;
|
||||
let best = -1, bestScore = Infinity;
|
||||
for (let i = 1; i < flat.length - 1; i++) {
|
||||
if (flat[i] !== ' ') continue;
|
||||
const prev = flat[i - 1];
|
||||
const bonus = /[,.;:!?…]/.test(prev) ? 0.35 : 0;
|
||||
const l1 = visibleLength(flat.slice(0, i)), l2 = visibleLength(flat.slice(i + 1));
|
||||
let score = Math.abs(i - mid) / flat.length - bonus;
|
||||
// Don't leave a one-letter word ("w", "z", "i", "a") hanging at the end of a line.
|
||||
if (/(^|\s)\p{L}$/u.test(flat.slice(0, i))) score += 0.6;
|
||||
// Over-length is penalised in proportion: a line 1–5 chars too long beats a hanging "w".
|
||||
const over = Math.max(0, l1 - maxLen) + Math.max(0, l2 - maxLen);
|
||||
if (over) score += 0.2 + (over / maxLen) * 3;
|
||||
if (score < bestScore) { bestScore = score; best = i; }
|
||||
}
|
||||
if (best < 0) return flat;
|
||||
return `${flat.slice(0, best)}\n${flat.slice(best + 1)}`;
|
||||
}
|
||||
|
||||
/** Removes stray stuff models like to add: code fences, surrounding quotes, <think> blocks. */
|
||||
export function cleanModelText(s) {
|
||||
let t = String(s ?? '');
|
||||
t = t.replace(/<think>[\s\S]*?<\/think>/gi, '').replace(/^[\s\S]*?<\/think>/i, '');
|
||||
t = t.replace(/^```[a-z]*\n?|```$/gim, '');
|
||||
return t.trim();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Song lyrics (♪ … ♪). Used to keep them untranslated or drop them completely.
|
||||
|
||||
const NOTE_CH = '♪♫♬♩';
|
||||
const NOTE_RE = new RegExp(`[${NOTE_CH}]`);
|
||||
const OPEN_RE = new RegExp(`^[-–—]?\\s*([${NOTE_CH}]|#(\\s|$))`);
|
||||
const CLOSE_RE = new RegExp(`([${NOTE_CH}]|(^|\\s)#)\\s*$`);
|
||||
const plainLine = (l) => l.replace(/<[^>]+>|\{\\[^}]*\}/g, '').trim();
|
||||
|
||||
/**
|
||||
* Marks lyric lines of a cue: lines with a music note (or the old "# lyrics #" style), plus
|
||||
* lines between an opening note and a closing note within the same cue
|
||||
* ("♪ I've been waiting / all my life / for you ♪"). Dialogue lines ("- …") in between
|
||||
* are not swallowed.
|
||||
* @returns {boolean[]}
|
||||
*/
|
||||
export function songLineMask(text) {
|
||||
const lines = String(text ?? '').split('\n').map(plainLine);
|
||||
const mask = lines.map((l) => NOTE_RE.test(l) || /^#(\s|$)/.test(l) || /\s#$/.test(l));
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
if (!mask[i] || !OPEN_RE.test(lines[i])) continue;
|
||||
const rest = lines[i].replace(OPEN_RE, '');
|
||||
if (CLOSE_RE.test(rest) && rest.trim()) continue; // opens and closes on the same line
|
||||
let j = i + 1;
|
||||
while (j < lines.length && !mask[j]) j++;
|
||||
if (j < lines.length && CLOSE_RE.test(lines[j])) {
|
||||
for (let k = i + 1; k < j; k++) if (!/^[-–—]/.test(lines[k])) mask[k] = true;
|
||||
}
|
||||
}
|
||||
return mask;
|
||||
}
|
||||
|
||||
/** 'full' — the whole cue is lyrics/music, 'partial' — some lines, null — none. */
|
||||
export function songKind(text) {
|
||||
const mask = songLineMask(text);
|
||||
const nonEmpty = String(text ?? '').split('\n').map((l, i) => [plainLine(l), mask[i]]).filter(([l]) => l);
|
||||
if (!nonEmpty.length || !nonEmpty.some(([, m]) => m)) return null;
|
||||
return nonEmpty.every(([, m]) => m) ? 'full' : 'partial';
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits a cue into leading lyric lines, the rest, and trailing lyric lines. Formatting that
|
||||
* wraps the whole cue is taken off first so cutting lines never leaves a dangling </i>.
|
||||
*/
|
||||
export function splitSongLines(text) {
|
||||
const prot = protectFormatting(String(text ?? ''));
|
||||
const lines = prot.text.split('\n');
|
||||
const mask = songLineMask(prot.text);
|
||||
let a = 0, b = lines.length;
|
||||
while (a < b && mask[a]) a++;
|
||||
while (b > a && mask[b - 1]) b--;
|
||||
return { lead: lines.slice(0, a), body: lines.slice(a, b), trail: lines.slice(b), mask, restore: prot.restore };
|
||||
}
|
||||
|
||||
/** Removes lyric lines; returns '' if nothing else is left. */
|
||||
export function stripSongs(text) {
|
||||
const { mask, restore } = splitSongLines(text);
|
||||
const lines = protectFormatting(String(text ?? '')).text.split('\n');
|
||||
const kept = lines.filter((l, i) => !mask[i] && plainLine(l));
|
||||
if (!kept.length) return '';
|
||||
// A dialogue cue reduced to one line shouldn't keep its leading dash.
|
||||
if (kept.length === 1 && lines.length > 1) kept[0] = kept[0].replace(/^(\s*(?:<[^>]+>)*)[-–—]\s*/, '$1');
|
||||
return restore(kept.join('\n'));
|
||||
}
|
||||
@@ -0,0 +1,519 @@
|
||||
// Translation job: batches cues, keeps them aligned by id, validates output and
|
||||
// degrades gracefully (split batch → numbered lines → one cue at a time).
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { protectFormatting, hasTranslatableText, rewrap, cleanModelText, isDialogue, songKind, splitSongLines } from './srt.js';
|
||||
import {
|
||||
PROFILES, detectProfile, genericSystemPrompt, genericJsonUserPrompt, jsonSchemaFor, pickJsonValue,
|
||||
genericLinesUserPrompt, parseNumberedLines, genericSingleMessages, mtMessages, estimateTokens,
|
||||
} from './prompts.js';
|
||||
import { langName } from './languages.js';
|
||||
|
||||
const MAX_LOG = 500;
|
||||
|
||||
export class TranslationJob extends EventEmitter {
|
||||
/**
|
||||
* @param {object} p
|
||||
* @param {{id:number,text:string}[]} p.cues
|
||||
* @param {object} p.engine engine instance
|
||||
* @param {string} p.model
|
||||
* @param {object} p.settings model/sampling settings
|
||||
* @param {object} p.options translation options
|
||||
* @param {Record<string,string>} [p.existing] already translated cues (id -> text)
|
||||
* @param {number[]} [p.onlyIds] translate only these cues (others are context)
|
||||
*/
|
||||
constructor({ cues, engine, model, settings = {}, options = {}, existing = {}, onlyIds = null }) {
|
||||
super();
|
||||
this.setMaxListeners(50);
|
||||
this.id = randomUUID();
|
||||
this.cues = cues;
|
||||
this.byId = new Map(cues.map((c, i) => [c.id, { ...c, index: i }]));
|
||||
this.engine = engine;
|
||||
this.model = model;
|
||||
this.settings = settings;
|
||||
this.options = {
|
||||
source: 'auto', target: 'pl', mode: 'auto', profile: 'auto', batchSize: 25, contextBefore: 6, contextAfter: 3,
|
||||
notes: '', maxLineLength: 42, concurrency: 1, autoLoad: true, songs: 'translate', ...options,
|
||||
};
|
||||
// songs: 'translate' | 'keep' (original lyrics) | 'remove' (dropped at export). For keep/remove
|
||||
// lyric lines are never sent to the model.
|
||||
this.skipSongs = this.options.songs === 'keep' || this.options.songs === 'remove';
|
||||
this.songCues = new Set(this.skipSongs ? cues.filter((c) => songKind(c.text) === 'full').map((c) => c.id) : []);
|
||||
this.state = 'pending';
|
||||
this.results = new Map(); // id -> {text, status}
|
||||
for (const [id, t] of Object.entries(existing || {})) {
|
||||
if (t != null && t !== '') this.results.set(Number(id), { text: t, status: 'existing' });
|
||||
}
|
||||
this.onlyIds = onlyIds ? new Set(onlyIds.map(Number)) : null;
|
||||
this.abort = new AbortController();
|
||||
this.log = [];
|
||||
this.stats = {
|
||||
total: 0, done: 0, copied: 0, songs: 0, failed: 0, requests: 0, retries: 0, splits: 0, fallbacks: 0,
|
||||
promptTokens: 0, completionTokens: 0, startedAt: null, finishedAt: null, tokPerSec: null, charsPerToken: 3.2,
|
||||
};
|
||||
this.effectiveContext = null;
|
||||
this.profile = this.options.profile === 'auto' ? detectProfile(model) : this.options.profile;
|
||||
const prof = PROFILES[this.profile] || PROFILES.generic;
|
||||
this.mode = this.options.mode === 'auto' || !prof.modes.includes(this.options.mode) ? prof.defaultMode : this.options.mode;
|
||||
this.canJson = !(engine.kind === 'openai' && engine.structuredOutput === false);
|
||||
if (this.mode === 'json' && !this.canJson) this.mode = 'lines';
|
||||
this.updated = new Map();
|
||||
}
|
||||
|
||||
info(msg) { this.pushLog('info', msg); }
|
||||
warn(msg) { this.pushLog('warn', msg); }
|
||||
pushLog(level, msg) {
|
||||
const e = { t: Date.now(), level, msg };
|
||||
this.log.push(e);
|
||||
if (this.log.length > MAX_LOG) this.log.shift();
|
||||
this.emit('log', e);
|
||||
}
|
||||
|
||||
cancel() {
|
||||
if (this.state === 'running' || this.state === 'pending') {
|
||||
this.state = 'cancelled';
|
||||
this.abort.abort(new Error('Anulowano'));
|
||||
this.info('Anulowano.');
|
||||
this.emit('state', this.state);
|
||||
}
|
||||
}
|
||||
|
||||
snapshot() {
|
||||
return {
|
||||
id: this.id,
|
||||
state: this.state,
|
||||
model: this.model,
|
||||
profile: this.profile,
|
||||
mode: this.mode,
|
||||
effectiveContext: this.effectiveContext,
|
||||
stats: this.stats,
|
||||
error: this.error || null,
|
||||
log: this.log.slice(-200),
|
||||
results: Object.fromEntries([...this.results].map(([id, r]) => [id, r])),
|
||||
};
|
||||
}
|
||||
|
||||
setResult(id, text, status) {
|
||||
this.results.set(id, { text, status });
|
||||
if (status !== 'existing') {
|
||||
this.stats.done++;
|
||||
if (status === 'error') this.stats.failed++;
|
||||
if (status === 'copied' || status === 'song') this.stats.copied++;
|
||||
if (status === 'song') this.stats.songs++;
|
||||
}
|
||||
this.emit('cue', { id, text, status });
|
||||
}
|
||||
|
||||
async resolveContext() {
|
||||
const s = this.settings;
|
||||
let ctx = Number(s.contextLength) || null;
|
||||
if (this.engine.kind === 'lmstudio') {
|
||||
let inst = (await this.engine.loaded()).find((i) => i.model === this.model);
|
||||
if (!inst && this.options.autoLoad) {
|
||||
this.info(`Ładuję model w LM Studio (kontekst ${s.contextLength || 'domyślny'}, KV ${s.kvCacheK || 'f16'}/${s.kvCacheV || 'f16'})…`);
|
||||
const r = await this.engine.load(this.model, s);
|
||||
if (r.warning) this.warn(r.warning);
|
||||
inst = (await this.engine.loaded()).find((i) => i.model === this.model);
|
||||
}
|
||||
if (inst?.contextLength) {
|
||||
if (ctx && inst.contextLength !== ctx) this.warn(`Model jest załadowany z kontekstem ${inst.contextLength} (nie ${ctx}) — używam ${inst.contextLength}. Przeładuj model, aby zmienić.`);
|
||||
ctx = inst.contextLength;
|
||||
}
|
||||
} else if (this.engine.kind === 'openai') {
|
||||
const info = await this.engine.modelInfo(this.model).catch(() => ({}));
|
||||
if (info.serverContext) ctx = info.serverContext;
|
||||
}
|
||||
this.effectiveContext = ctx || 4096;
|
||||
// Several parallel requests share one unified KV cache in LM Studio / llama-server.
|
||||
this.perRequestContext = Math.floor(this.effectiveContext / Math.max(1, this.engine.kind === 'ollama' ? 1 : this.options.concurrency));
|
||||
}
|
||||
|
||||
async run() {
|
||||
this.state = 'running';
|
||||
this.stats.startedAt = Date.now();
|
||||
this.emit('state', this.state);
|
||||
try {
|
||||
await this.resolveContext();
|
||||
this.info(`Model: ${this.model} · profil: ${PROFILES[this.profile]?.label || this.profile} · tryb: ${this.mode} · kontekst: ${this.effectiveContext} tok.`);
|
||||
if (PROFILES[this.profile]?.needsSource && (!this.options.source || this.options.source === 'auto')) {
|
||||
this.warn('Ten model wymaga języka źródłowego — zakładam angielski. Ustaw język źródłowy, jeśli jest inny.');
|
||||
}
|
||||
|
||||
const pending = [];
|
||||
for (const c of this.cues) {
|
||||
if (this.onlyIds && !this.onlyIds.has(c.id)) continue;
|
||||
if (!this.onlyIds && this.results.has(c.id)) continue; // resume: keep existing
|
||||
if (this.songCues.has(c.id)) {
|
||||
this.stats.total++;
|
||||
this.setResult(c.id, c.text, 'song');
|
||||
continue;
|
||||
}
|
||||
let item;
|
||||
if (this.skipSongs && songKind(c.text) === 'partial') {
|
||||
// Only the non-lyric lines go to the model; lyric lines are put back untouched.
|
||||
const sp = splitSongLines(c.text);
|
||||
const prot = protectFormatting(sp.body.join('\n'));
|
||||
item = { text: prot.text, restore: (t) => sp.restore([...sp.lead, prot.restore(t), ...sp.trail].join('\n')) };
|
||||
} else {
|
||||
const prot = protectFormatting(c.text);
|
||||
item = { text: prot.text, restore: prot.restore };
|
||||
}
|
||||
if (!hasTranslatableText(item.text)) {
|
||||
this.stats.total++;
|
||||
this.setResult(c.id, c.text, 'copied');
|
||||
continue;
|
||||
}
|
||||
pending.push({ id: c.id, ...item, index: this.byId.get(c.id).index });
|
||||
}
|
||||
if (this.stats.songs) this.info(`Pominięto ${this.stats.songs} napisów z piosenkami (${this.options.songs === 'remove' ? 'zostaną usunięte przy eksporcie' : 'zostają w oryginale'}).`);
|
||||
this.stats.total += pending.length;
|
||||
this.emit('progress', this.stats);
|
||||
if (!pending.length) this.info('Nic do tłumaczenia.');
|
||||
|
||||
const units = this.mode === 'single' ? pending.map((p) => [p]) : this.makeBatches(pending);
|
||||
let next = 0;
|
||||
const worker = async () => {
|
||||
while (next < units.length && this.state === 'running') {
|
||||
const unit = units[next++];
|
||||
await this.translateUnit(unit, this.mode);
|
||||
this.emit('progress', this.stats);
|
||||
}
|
||||
};
|
||||
const n = Math.max(1, Math.min(8, Number(this.options.concurrency) || 1));
|
||||
await Promise.all(Array.from({ length: n }, worker));
|
||||
|
||||
if (this.state === 'running') {
|
||||
this.state = 'done';
|
||||
const secs = ((Date.now() - this.stats.startedAt) / 1000).toFixed(1);
|
||||
this.info(`Gotowe: ${this.stats.done}/${this.stats.total} w ${secs} s, ${this.stats.requests} zapytań, błędów: ${this.stats.failed}.`);
|
||||
}
|
||||
} catch (e) {
|
||||
if (this.state !== 'cancelled') {
|
||||
this.state = 'error';
|
||||
this.error = e.message;
|
||||
this.pushLog('error', e.message);
|
||||
}
|
||||
} finally {
|
||||
this.stats.finishedAt = Date.now();
|
||||
this.emit('progress', this.stats);
|
||||
this.emit('state', this.state);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- token budgeting ---------------------------------------------------
|
||||
|
||||
tokens(text) { return estimateTokens(text, this.stats.charsPerToken); }
|
||||
|
||||
systemPrompt() {
|
||||
return genericSystemPrompt({ source: this.options.source, target: this.options.target, notes: this.options.notes });
|
||||
}
|
||||
|
||||
/** Splits pending cues into batches that fit into the context window. */
|
||||
makeBatches(pending) {
|
||||
const ctx = this.perRequestContext;
|
||||
const fixed = this.tokens(this.systemPrompt()) + 120 // instructions + chat template
|
||||
+ (this.options.contextBefore + this.options.contextAfter) * 40; // context lines (src + translation)
|
||||
const expansion = 1.6; // output tokens per input token (target languages often need more tokens)
|
||||
const budget = Math.max(200, (ctx * 0.9 - fixed) / (1 + expansion));
|
||||
const maxN = Math.max(1, Number(this.options.batchSize) || 25);
|
||||
const batches = [];
|
||||
let cur = [], curTok = 0;
|
||||
for (const p of pending) {
|
||||
const t = this.tokens(p.text) + 8;
|
||||
if (cur.length && (cur.length >= maxN || curTok + t > budget)) { batches.push(cur); cur = []; curTok = 0; }
|
||||
cur.push(p);
|
||||
curTok += t;
|
||||
}
|
||||
if (cur.length) batches.push(cur);
|
||||
const avg = batches.length ? (pending.length / batches.length).toFixed(1) : 0;
|
||||
this.info(`${pending.length} linii w ${batches.length} partiach (śr. ${avg}/partię, budżet wejścia ≈ ${Math.round(budget)} tok.).`);
|
||||
if (Number(this.options.batchSize) > 1 && avg < Number(this.options.batchSize) / 2 && batches.length > 1) {
|
||||
this.warn('Kontekst ogranicza wielkość partii — zwiększ długość kontekstu, by tłumaczyć szybciej.');
|
||||
}
|
||||
return batches;
|
||||
}
|
||||
|
||||
contextFor(batch) {
|
||||
const first = batch[0].index, last = batch[batch.length - 1].index;
|
||||
// Skipped song cues are left out of the context too (they won't be in the output).
|
||||
const usable = (c) => !this.songCues.has(c.id);
|
||||
const before = this.cues.slice(Math.max(0, first - this.options.contextBefore * 2), first).filter(usable)
|
||||
.slice(-this.options.contextBefore)
|
||||
.map((c) => ({ id: c.id, text: c.text, translation: this.results.get(c.id)?.text }));
|
||||
const ids = new Set(batch.map((b) => b.id));
|
||||
const after = this.cues.slice(last + 1, last + 1 + this.options.contextAfter * 2)
|
||||
.filter((c) => !ids.has(c.id) && usable(c)).slice(0, this.options.contextAfter).map((c) => ({ id: c.id, text: c.text }));
|
||||
return { before, after };
|
||||
}
|
||||
|
||||
// ---- calling the model --------------------------------------------------
|
||||
|
||||
async call(messages, { schema, maxTokens }) {
|
||||
const promptChars = messages.reduce((n, m) => n + m.content.length, 0);
|
||||
const room = this.perRequestContext - this.tokens(messages.map((m) => m.content).join('')) - 32;
|
||||
const limit = Math.max(64, Math.min(maxTokens, room));
|
||||
let lastErr;
|
||||
for (let attempt = 0; attempt < 3; attempt++) {
|
||||
if (this.state !== 'running') throw new Error('Anulowano');
|
||||
try {
|
||||
this.stats.requests++;
|
||||
const r = await this.engine.chat({ model: this.model, messages, schema, settings: this.settings, maxTokens: limit, signal: this.abort.signal });
|
||||
if (r.promptTokens) {
|
||||
this.stats.promptTokens += r.promptTokens;
|
||||
// Learn the real chars/token ratio of this model+language to size batches better.
|
||||
const ratio = promptChars / r.promptTokens;
|
||||
if (ratio > 1 && ratio < 8) this.stats.charsPerToken = this.stats.charsPerToken * 0.7 + ratio * 0.3;
|
||||
}
|
||||
if (r.completionTokens) this.stats.completionTokens += r.completionTokens;
|
||||
if (r.tokPerSec) this.stats.tokPerSec = r.tokPerSec;
|
||||
return r;
|
||||
} catch (e) {
|
||||
if (this.state !== 'running') throw e;
|
||||
lastErr = e;
|
||||
if (e.status && e.status >= 400 && e.status < 500 && e.status !== 429) throw e; // bad request: retrying won't help
|
||||
this.stats.retries++;
|
||||
this.warn(`Błąd zapytania (${e.message}) — ponawiam (${attempt + 1}/3)…`);
|
||||
await new Promise((r) => setTimeout(r, 1000 * (attempt + 1)));
|
||||
}
|
||||
}
|
||||
throw lastErr;
|
||||
}
|
||||
|
||||
async translateUnit(unit, mode) {
|
||||
if (this.state !== 'running') return;
|
||||
try {
|
||||
if (mode === 'single') return await this.translateSingle(unit[0]);
|
||||
const out = mode === 'json' ? await this.batchJson(unit) : await this.batchLines(unit);
|
||||
if (out.ok) {
|
||||
const repaired = repairMerges(unit, out.map);
|
||||
for (const [a, b] of repaired) this.info(`Linie ${a}/${b}: model połączył zdanie w jedną linię — podzielono automatycznie.`);
|
||||
const retry = [];
|
||||
for (const p of unit) {
|
||||
const v = out.map.get(p.id);
|
||||
if ((out.echoed?.has(p.id) || isBlank(v, p.text)) && unit.length > 1) retry.push(p);
|
||||
else this.finish(p, v);
|
||||
}
|
||||
// Entries the model copied or left empty get a second chance one at a time.
|
||||
for (const p of retry) await this.translateUnit([p], 'single');
|
||||
return;
|
||||
}
|
||||
if (mode === 'lines' && out.truncated && this.canJson) {
|
||||
// Typical for models that "think aloud" in plain text: grammar-constrained JSON stops that.
|
||||
if (this.mode === 'lines') this.warn('Model generuje za dużo tekstu w trybie linii — przełączam na tryb JSON.');
|
||||
this.mode = 'json';
|
||||
return await this.translateUnit(unit, 'json');
|
||||
}
|
||||
if (unit.length > 1) {
|
||||
this.stats.splits++;
|
||||
this.warn(`Partia ${unit[0].id}–${unit[unit.length - 1].id}: ${out.reason} — dzielę na pół.`);
|
||||
const mid = Math.ceil(unit.length / 2);
|
||||
await this.translateUnit(unit.slice(0, mid), mode);
|
||||
await this.translateUnit(unit.slice(mid), mode);
|
||||
return;
|
||||
}
|
||||
this.stats.fallbacks++;
|
||||
this.warn(`Linia ${unit[0].id}: ${out.reason} — tłumaczę pojedynczo.`);
|
||||
await this.translateSingle(unit[0]);
|
||||
} catch (e) {
|
||||
if (this.state !== 'running') return;
|
||||
const range = unit.length === 1 ? `Linia ${unit[0].id}` : `Linie ${unit[0].id}–${unit[unit.length - 1].id}`;
|
||||
this.pushLog('error', `${range}: ${e.message}`);
|
||||
for (const p of unit) if (!this.results.has(p.id) || this.results.get(p.id).status === 'existing') this.setResult(p.id, '', 'error');
|
||||
// Nothing works at all → stop early instead of burning through the whole file.
|
||||
const attempted = this.stats.done - this.stats.copied; // copied includes skipped songs
|
||||
if (this.stats.failed >= 3 && this.stats.failed === attempted && !this.onlyIds) {
|
||||
this.error = `Pierwsze ${attempted} linii zakończyło się błędem (${e.message}). Sprawdź model, profil promptu i tryb — np. modele „myślące” najlepiej działają w trybie JSON.`;
|
||||
this.state = 'error';
|
||||
this.pushLog('error', this.error);
|
||||
this.abort.abort(new Error('Przerwano'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
outputBudget(batch) {
|
||||
const inTok = batch.reduce((n, p) => n + this.tokens(p.text) + 8, 0);
|
||||
return Math.ceil(inTok * 2.5 + 64);
|
||||
}
|
||||
|
||||
async batchJson(batch) {
|
||||
const { before, after } = this.contextFor(batch);
|
||||
const messages = [
|
||||
{ role: 'system', content: this.systemPrompt() },
|
||||
{ role: 'user', content: genericJsonUserPrompt({ batch, before, after }) },
|
||||
];
|
||||
const r = await this.call(messages, { schema: jsonSchemaFor(batch), maxTokens: this.outputBudget(batch) });
|
||||
if (r.finishReason === 'length') return { ok: false, reason: 'odpowiedź ucięta (limit tokenów)' };
|
||||
const obj = parseJsonLoose(r.text);
|
||||
if (!obj) return { ok: false, reason: 'niepoprawny JSON' };
|
||||
const map = new Map();
|
||||
for (const p of batch) {
|
||||
const v = pickJsonValue(obj, p);
|
||||
if (typeof v !== 'string') return { ok: false, reason: `brak id ${p.id}` };
|
||||
map.set(p.id, v);
|
||||
}
|
||||
return this.sanityCheck(batch, map);
|
||||
}
|
||||
|
||||
async batchLines(batch) {
|
||||
const { before, after } = this.contextFor(batch);
|
||||
const messages = [
|
||||
{ role: 'system', content: this.systemPrompt() },
|
||||
{ role: 'user', content: genericLinesUserPrompt({ batch, before, after }) },
|
||||
];
|
||||
const r = await this.call(messages, { maxTokens: this.outputBudget(batch) });
|
||||
if (r.finishReason === 'length') return { ok: false, truncated: true, reason: 'odpowiedź ucięta (limit tokenów)' };
|
||||
const parsed = parseNumberedLines(cleanModelText(r.text), batch.map((p) => p.id));
|
||||
const map = new Map();
|
||||
for (const p of batch) {
|
||||
if (!parsed.has(p.id)) return { ok: false, reason: `brak linii [${p.id}]` };
|
||||
map.set(p.id, parsed.get(p.id));
|
||||
}
|
||||
return this.sanityCheck(batch, map);
|
||||
}
|
||||
|
||||
/** Catches the typical failure: the model shifted lines or echoed the source. */
|
||||
sanityCheck(batch, map) {
|
||||
const vals = batch.map((p) => (map.get(p.id) || '').trim());
|
||||
const empty = batch.filter((p, i) => isBlank(vals[i], p.text) && p.text.trim());
|
||||
if (empty.length > Math.max(0, batch.length * 0.2)) return { ok: false, reason: `${empty.length} pustych tłumaczeń` };
|
||||
// Merged entries: the next translation repeated inside the previous one.
|
||||
const norm2 = (s) => s.toLowerCase().replace(/[^\p{L}\p{N}]+/gu, ' ').trim();
|
||||
for (let i = 0; i + 1 < vals.length; i++) {
|
||||
const a = norm2(vals[i]), b = norm2(vals[i + 1]);
|
||||
if (b.length > 12 && a.length > b.length && a.includes(b)) return { ok: false, reason: `linie ${batch[i].id}/${batch[i + 1].id} scalone` };
|
||||
}
|
||||
if (batch.length >= 4) {
|
||||
let dup = 0;
|
||||
for (let i = 1; i < vals.length; i++) if (vals[i] && vals[i] === vals[i - 1] && batch[i].text !== batch[i - 1].text) dup++;
|
||||
if (dup >= 2) return { ok: false, reason: 'powtórzone tłumaczenia (przesunięcie linii?)' };
|
||||
}
|
||||
// Copied source text ("echo"): only meaningful for real sentences in a different language.
|
||||
const echoed = new Set();
|
||||
if (this.options.source !== this.options.target) {
|
||||
const norm = (s) => s.replace(/<[^>]+>|\s+/g, ' ').trim().toLowerCase();
|
||||
batch.forEach((p, i) => {
|
||||
if ((p.text.match(/\p{L}{2,}/gu) || []).length >= 3 && norm(vals[i]) === norm(p.text)) echoed.add(p.id);
|
||||
});
|
||||
if (batch.length > 1 && echoed.size > batch.length / 2) return { ok: false, reason: 'model zwrócił tekst bez tłumaczenia' };
|
||||
}
|
||||
return { ok: true, map, echoed };
|
||||
}
|
||||
|
||||
async translateSingle(p) {
|
||||
if (this.state !== 'running') return;
|
||||
const generic = this.profile === 'generic';
|
||||
if (generic && this.canJson) {
|
||||
// One-entry JSON keeps the context and stops models from chatting instead of translating.
|
||||
const res = await this.batchJson([p]);
|
||||
if (res.ok) return this.finish(p, res.map.get(p.id));
|
||||
this.warn(`Linia ${p.id}: ${res.reason} — próbuję zwykłym tekstem.`);
|
||||
}
|
||||
// MT models expect plain sentences: dialogue cues are translated line by line, others joined.
|
||||
const parts = !generic && isDialogue(p.text) ? p.text.split('\n') : [generic ? p.text : p.text.replace(/\n/g, ' ')];
|
||||
const out = [];
|
||||
for (const part of parts) {
|
||||
const dash = /^\s*([-–—]\s*)/.exec(part);
|
||||
const body = dash ? part.slice(dash[0].length) : part;
|
||||
const { before } = this.contextFor([p]);
|
||||
const messages = generic
|
||||
? genericSingleMessages({ text: body, before: before.slice(-3), source: this.options.source, target: this.options.target, notes: this.options.notes })
|
||||
: mtMessages(this.profile, { text: body, before: this.options.mtContext ? before.slice(-2) : [], source: this.options.source, target: this.options.target });
|
||||
const r = await this.call(messages, { maxTokens: Math.ceil(this.tokens(body) * 3 + 64) });
|
||||
if (r.finishReason === 'length') throw new Error('odpowiedź ucięta — model generuje zbyt dużo tekstu');
|
||||
let t = cleanModelText(r.text);
|
||||
t = stripMtArtifacts(t, this.options.target);
|
||||
if (looksLikeReasoning(t, body)) throw new Error('model opisuje zadanie zamiast tłumaczyć (użyj trybu JSON lub innego modelu)');
|
||||
out.push((dash ? dash[1] : '') + t);
|
||||
}
|
||||
this.finish(p, out.join('\n'));
|
||||
}
|
||||
|
||||
finish(p, raw) {
|
||||
let t = cleanModelText(raw).replace(/\\n/g, '\n').replace(/\s*<br\s*\/?>\s*/gi, '\n');
|
||||
t = t.split('\n').map((l) => l.trim()).filter(Boolean).join('\n');
|
||||
if (!t || isBlank(t, p.text)) {
|
||||
if (t) this.warn(`Linia ${p.id}: tłumaczenie bez treści („${t}”) — oznaczono jako błąd.`);
|
||||
this.setResult(p.id, '', 'error');
|
||||
return;
|
||||
}
|
||||
// Models like to add line breaks of their own; more lines than the source → re-flow.
|
||||
if (t.split('\n').length > p.text.split('\n').length && !isDialogue(t)) t = t.replace(/\n/g, ' ');
|
||||
if (this.options.maxLineLength) t = rewrap(t, Number(this.options.maxLineLength));
|
||||
this.setResult(p.id, p.restore(t), 'ok');
|
||||
}
|
||||
}
|
||||
|
||||
function parseJsonLoose(text) {
|
||||
const t = cleanModelText(text);
|
||||
try { return JSON.parse(t); } catch { /* try to cut out the object */ }
|
||||
const a = t.indexOf('{'), b = t.lastIndexOf('}');
|
||||
if (a >= 0 && b > a) {
|
||||
try { return JSON.parse(t.slice(a, b + 1)); } catch { /* give up */ }
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Empty, or only symbols while the source has words (e.g. "[door slams]" → "[]"). */
|
||||
function isBlank(v, src) {
|
||||
const t = (v || '').trim();
|
||||
return !t || (hasTranslatableText(src) && !hasTranslatableText(t));
|
||||
}
|
||||
|
||||
const vis = (s) => (s || '').replace(/<[^>]+>|\{\\[^}]*\}/g, '').replace(/\s+/g, ' ').trim().length;
|
||||
|
||||
/** Splits `text` near `frac` of its length at a word boundary, preferring punctuation. */
|
||||
export function splitAt(text, frac) {
|
||||
const t = text.replace(/\s*\n\s*/g, ' ').trim();
|
||||
const target = t.length * frac;
|
||||
let best = -1, bestScore = Infinity;
|
||||
for (let i = 1; i < t.length - 1; i++) {
|
||||
if (t[i] !== ' ') continue;
|
||||
const prev = t[i - 1];
|
||||
const score = Math.abs(i - target) / t.length - (/[.!?…]/.test(prev) ? 0.1 : /[,;:]/.test(prev) ? 0.03 : 0);
|
||||
if (score < bestScore) { bestScore = score; best = i; }
|
||||
}
|
||||
return best < 0 ? null : [t.slice(0, best).trim(), t.slice(best + 1).trim()];
|
||||
}
|
||||
|
||||
/**
|
||||
* A sentence spanning two subtitles often comes back fully in one entry and empty in the
|
||||
* other. If the lengths say so, split the translation in proportion to the source parts.
|
||||
* Mutates `map`; returns the repaired id pairs.
|
||||
*/
|
||||
export function repairMerges(batch, map) {
|
||||
const fixed = [];
|
||||
for (let i = 0; i < batch.length; i++) {
|
||||
const p = batch[i];
|
||||
if ((map.get(p.id) || '').trim() || !vis(p.text)) continue;
|
||||
for (const [a, b] of [[batch[i - 1], p], [p, batch[i + 1]]]) {
|
||||
if (!a || !b) continue;
|
||||
const full = (map.get(a === p ? b.id : a.id) || '').trim();
|
||||
const owner = a === p ? b : a;
|
||||
if (!full) continue;
|
||||
const sa = vis(a.text), sb = vis(b.text), f = vis(full);
|
||||
const alone = f / vis(owner.text), together = f / (sa + sb);
|
||||
if (alone < 1.35 || together < 0.55 || together > 1.8) continue;
|
||||
const parts = splitAt(full, sa / (sa + sb));
|
||||
if (!parts) continue;
|
||||
map.set(a.id, parts[0]);
|
||||
map.set(b.id, parts[1]);
|
||||
fixed.push([a.id, b.id]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return fixed;
|
||||
}
|
||||
|
||||
/** Detects "thinking aloud" returned instead of a translation. */
|
||||
export function looksLikeReasoning(t, src) {
|
||||
if (/^(the user|user wants|okay[,.]|ok[,.]|alright|let me|let's|i need to|i will|i'll |we need|first,|thinking|analysis|translation:|here is|here's)/i.test(t)) return true;
|
||||
return t.length > Math.max(160, src.length * 4);
|
||||
}
|
||||
|
||||
/** MT models sometimes prefix "Polish:" or wrap in quotes. */
|
||||
function stripMtArtifacts(t, target) {
|
||||
const name = langName(target);
|
||||
let s = t.replace(new RegExp(`^${name}\\s*:\\s*`, 'i'), '');
|
||||
if (/^".*"$/s.test(s) && !/^".*".*"$/s.test(s.slice(1))) s = s.slice(1, -1);
|
||||
return s.trim();
|
||||
}
|
||||
+223
@@ -0,0 +1,223 @@
|
||||
// Memory estimate for llama.cpp-style runtimes (Ollama, LM Studio GGUF, llama-server).
|
||||
// Input: GGUF metadata (Ollama /api/show model_info or parsed from the file).
|
||||
import { execFile } from 'node:child_process';
|
||||
import os from 'node:os';
|
||||
import { promisify } from 'node:util';
|
||||
|
||||
const run = promisify(execFile);
|
||||
|
||||
// bytes per element, block formats: (block bytes)/(block elements)
|
||||
export const KV_TYPES = {
|
||||
f32: 4, f16: 2, bf16: 2, q8_0: 34 / 32, q5_1: 24 / 32, q5_0: 22 / 32, q4_1: 20 / 32, q4_0: 18 / 32, iq4_nl: 18 / 32,
|
||||
};
|
||||
|
||||
// Default "every n-th layer is global" patterns for archs that don't store them in GGUF.
|
||||
const DEFAULT_SWA_PATTERN = { gemma2: 2, gemma3: 6, cohere2: 4, llama4: 4, 'gpt-oss': 2, gptoss: 2, exaone4: 4 };
|
||||
|
||||
const at = (v, i) => (Array.isArray(v) ? v[i] : v);
|
||||
const VOCAB_BY_ARCH = { gemma2: 256000, gemma3: 262144, gemma3n: 262144, gemma4: 262144, llama: 128256, qwen2: 151936, qwen3: 151936, qwen3moe: 151936, qwen35: 248320, mistral3: 131072 };
|
||||
|
||||
export function archParams(meta) {
|
||||
const arch = meta['general.architecture'];
|
||||
const g = (k) => meta[`${arch}.${k}`];
|
||||
const nLayer = g('block_count');
|
||||
if (!nLayer) return null;
|
||||
const nHead = g('attention.head_count');
|
||||
const nEmbd = g('embedding_length');
|
||||
const headDim = nEmbd && at(nHead, 0) ? nEmbd / at(nHead, 0) : 128;
|
||||
const vocabArr = meta['tokenizer.ggml.tokens'];
|
||||
return {
|
||||
arch,
|
||||
nLayer,
|
||||
nHead,
|
||||
nHeadKv: g('attention.head_count_kv') ?? nHead,
|
||||
nEmbd,
|
||||
keyLen: g('attention.key_length') ?? headDim,
|
||||
valLen: g('attention.value_length') ?? g('attention.key_length') ?? headDim,
|
||||
keyLenSwa: g('attention.key_length_swa'),
|
||||
valLenSwa: g('attention.value_length_swa'),
|
||||
slidingWindow: g('attention.sliding_window') || 0,
|
||||
swaPattern: g('attention.sliding_window_pattern') ?? DEFAULT_SWA_PATTERN[arch],
|
||||
sharedKvLayers: g('attention.shared_kv_layers') || 0,
|
||||
fullAttnInterval: g('full_attention_interval'),
|
||||
kvLoraRank: g('attention.kv_lora_rank'),
|
||||
ropeDim: g('rope.dimension_count'),
|
||||
ssmInner: g('ssm.inner_size'),
|
||||
ssmState: g('ssm.state_size'),
|
||||
ssmConv: g('ssm.conv_kernel'),
|
||||
ssmGroups: g('ssm.group_count') || 1,
|
||||
contextLength: g('context_length'),
|
||||
// Ollama returns token arrays empty/null unless verbose; fall back to vocab_size / family.
|
||||
vocab: vocabArr?.length > 0 ? vocabArr.length : (g('vocab_size') || VOCAB_BY_ARCH[arch] || 128000),
|
||||
recurrentOnly: /^(mamba|mamba2|rwkv\d*|rwkv\d*qwen)$/.test(arch),
|
||||
};
|
||||
}
|
||||
|
||||
function layerKind(p, il) {
|
||||
if (p.recurrentOnly) return 'recurrent';
|
||||
if (p.sharedKvLayers && il >= p.nLayer - p.sharedKvLayers) return 'shared';
|
||||
if (p.fullAttnInterval && (il + 1) % p.fullAttnInterval !== 0) return 'recurrent';
|
||||
if (at(p.nHeadKv, il) === 0) return 'recurrent'; // e.g. LFM2 conv layers
|
||||
if (p.slidingWindow) {
|
||||
const pat = p.swaPattern;
|
||||
if (Array.isArray(pat)) return pat[il] ? 'swa' : 'full';
|
||||
if (typeof pat === 'number' && pat > 1) return il % pat < pat - 1 ? 'swa' : 'full';
|
||||
// sliding window everywhere (old Mistral style) if no pattern known
|
||||
return 'swa';
|
||||
}
|
||||
return 'full';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} meta GGUF metadata
|
||||
* @param {{modelBytes:number, nCtx:number, kType?:string, vType?:string, flashAttention?:boolean, nUbatch?:number, nSeq?:number, gpuRatio?:number}} o
|
||||
*/
|
||||
export function estimateMemory(meta, o) {
|
||||
const p = meta ? archParams(meta) : null;
|
||||
const nCtx = o.nCtx || 4096;
|
||||
const nUbatch = o.nUbatch || 512;
|
||||
const nSeq = o.nSeq || 1;
|
||||
const kB = KV_TYPES[o.kType || 'f16'] ?? 2;
|
||||
const vB = KV_TYPES[o.vType || o.kType || 'f16'] ?? 2;
|
||||
const weights = o.modelBytes || 0;
|
||||
const res = { weights, kv: 0, recurrent: 0, compute: 0, total: 0, layers: { full: 0, swa: 0, recurrent: 0, shared: 0 }, approximate: !p, notes: [] };
|
||||
if (!p) {
|
||||
// No metadata: crude guess (≈ 0.12 MB / token for a 7-8B GQA model at f16).
|
||||
res.kv = nCtx * 0.125 * 2 ** 20 * (kB + vB) / 4;
|
||||
res.compute = 0.5 * 2 ** 30;
|
||||
res.total = weights + res.kv + res.compute;
|
||||
res.notes.push('Brak metadanych architektury — bardzo zgrubne oszacowanie.');
|
||||
return res;
|
||||
}
|
||||
let kv = 0;
|
||||
for (let il = 0; il < p.nLayer; il++) {
|
||||
const kind = layerKind(p, il);
|
||||
res.layers[kind]++;
|
||||
if (kind === 'shared' || kind === 'recurrent') continue;
|
||||
const swa = kind === 'swa';
|
||||
const ctx = swa ? Math.min(nCtx, p.slidingWindow * nSeq + nUbatch) : nCtx;
|
||||
if (p.kvLoraRank) {
|
||||
// MLA (DeepSeek-style): compressed latent + rope part per token.
|
||||
kv += ctx * (p.kvLoraRank + (p.ropeDim || 64)) * kB;
|
||||
continue;
|
||||
}
|
||||
const hkv = at(p.nHeadKv, il);
|
||||
const kl = (swa && p.keyLenSwa) || p.keyLen;
|
||||
const vl = (swa && p.valLenSwa) || p.valLen;
|
||||
kv += ctx * hkv * (kl * kB + vl * vB);
|
||||
}
|
||||
res.kv = kv;
|
||||
if (res.layers.recurrent && p.ssmInner && p.ssmState) {
|
||||
const conv = ((p.ssmConv || 4) - 1) * (p.ssmInner + 2 * p.ssmGroups * p.ssmState) * 4;
|
||||
const state = p.ssmInner * p.ssmState * 4;
|
||||
res.recurrent = res.layers.recurrent * (conv + state) * nSeq;
|
||||
}
|
||||
const nHead = at(p.nHead, 0) || 32;
|
||||
// Calibrated on llama.cpp logs (gemma3 4B, 32k ctx, FA: 195 MiB GPU + 54 MiB CPU compute buffers).
|
||||
const logits = p.vocab * nUbatch * 4 * 0.35;
|
||||
const graph = (p.nEmbd || 4096) * nUbatch * 40;
|
||||
const kq = o.flashAttention ? 0 : nHead * nCtx * nUbatch * 4;
|
||||
res.compute = Math.max(192 * 2 ** 20, logits + graph) + kq;
|
||||
const gpu = o.gpuRatio ?? 1;
|
||||
res.total = weights + res.kv + res.recurrent + res.compute;
|
||||
res.gpu = weights * gpu + (res.kv + res.recurrent) * (gpu > 0 ? 1 : 0) + res.compute * (gpu > 0 ? 1 : 0);
|
||||
res.perTokenKv = nCtx ? kv / nCtx : 0;
|
||||
if (res.layers.swa) res.notes.push(`${res.layers.swa} warstw z przesuwnym oknem (${p.slidingWindow} tok.) — mały KV-cache.`);
|
||||
if (res.layers.shared) res.notes.push(`${res.layers.shared} warstw współdzieli KV-cache.`);
|
||||
if (res.layers.recurrent) res.notes.push(`${res.layers.recurrent} warstw rekurencyjnych (stały rozmiar stanu, bez KV).`);
|
||||
if (p.kvLoraRank) res.notes.push('MLA: skompresowany KV-cache.');
|
||||
if (vB < 2 && !o.flashAttention) res.notes.push('Kwantyzacja V-cache w llama.cpp wymaga Flash Attention.');
|
||||
return res;
|
||||
}
|
||||
|
||||
/** Find the largest context that fits into `budgetBytes`. */
|
||||
export function maxContextFor(meta, o, budgetBytes) {
|
||||
let lo = 512, hi = Math.max(512, o.maxCtx || 131072);
|
||||
if (estimateMemory(meta, { ...o, nCtx: lo }).total > budgetBytes) return 0;
|
||||
while (hi - lo > 256) {
|
||||
const mid = Math.floor((lo + hi) / 2);
|
||||
if (estimateMemory(meta, { ...o, nCtx: mid }).total <= budgetBytes) lo = mid; else hi = mid;
|
||||
}
|
||||
return Math.floor(lo / 256) * 256;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// What memory does this machine have for models?
|
||||
|
||||
// Metal's recommendedMaxWorkingSetSize — the real GPU budget on Apple Silicon (what Ollama and
|
||||
// llama.cpp use). Asked once via a tiny Swift snippet; falls back to a heuristic.
|
||||
let metalLimitPromise = null;
|
||||
function metalLimit() {
|
||||
metalLimitPromise ??= run('swift', ['-e', 'import Metal; print(MTLCreateSystemDefaultDevice()!.recommendedMaxWorkingSetSize)'], { timeout: 30000 })
|
||||
.then(({ stdout }) => Number(stdout.trim()) || null)
|
||||
.catch(() => null);
|
||||
return metalLimitPromise;
|
||||
}
|
||||
if (process.platform === 'darwin' && os.arch() === 'arm64') metalLimit();
|
||||
|
||||
/**
|
||||
* Windows without nvidia-smi (AMD, Intel): the display driver stores the real VRAM size as a
|
||||
* 64-bit value in the registry (WMI's AdapterRAM is capped at 4 GB).
|
||||
*/
|
||||
export function parseWindowsRegistryGpus(qwOut, descOut) {
|
||||
const blocks = (out, re) => {
|
||||
const map = new Map();
|
||||
let key = null;
|
||||
for (const line of out.split(/\r?\n/)) {
|
||||
if (/^HKEY_/.test(line)) key = line.trim();
|
||||
else if (key) { const m = re.exec(line); if (m) map.set(key, m[1]); }
|
||||
}
|
||||
return map;
|
||||
};
|
||||
const mem = blocks(qwOut, /HardwareInformation\.qwMemorySize\s+REG_QWORD\s+(0x[0-9a-f]+)/i);
|
||||
const desc = blocks(descOut, /DriverDesc\s+REG_SZ\s+(.+)$/i);
|
||||
const gpus = [];
|
||||
for (const [key, hex] of mem) {
|
||||
const total = Number(BigInt(hex));
|
||||
if (total > 256 * 2 ** 20) gpus.push({ name: (desc.get(key) || 'GPU').trim(), total });
|
||||
}
|
||||
return gpus.sort((a, b) => b.total - a.total);
|
||||
}
|
||||
|
||||
async function windowsGpus() {
|
||||
const cls = 'HKLM\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}';
|
||||
try {
|
||||
const opts = { timeout: 5000, windowsHide: true };
|
||||
const [qw, desc] = await Promise.all([
|
||||
run('reg', ['query', cls, '/s', '/v', 'HardwareInformation.qwMemorySize'], opts),
|
||||
run('reg', ['query', cls, '/s', '/v', 'DriverDesc'], opts),
|
||||
]);
|
||||
return parseWindowsRegistryGpus(qw.stdout, desc.stdout);
|
||||
} catch { return []; }
|
||||
}
|
||||
|
||||
let sysCache = null;
|
||||
export async function systemMemory() {
|
||||
if (sysCache && Date.now() - sysCache.at < 10000) return sysCache.value;
|
||||
const value = { platform: process.platform, totalRam: os.totalmem(), freeRam: os.freemem(), gpus: [] };
|
||||
try {
|
||||
if (process.platform === 'darwin' && os.arch() === 'arm64') {
|
||||
// Apple Silicon: unified memory; the GPU may use up to Metal's working-set limit
|
||||
// (or iogpu.wired_limit_mb if the user raised it).
|
||||
let wired = 0;
|
||||
try { wired = Number((await run('sysctl', ['-n', 'iogpu.wired_limit_mb'])).stdout.trim()) * 2 ** 20; } catch { /* ignore */ }
|
||||
const metal = await Promise.race([metalLimit(), new Promise((r) => setTimeout(() => r(null), 5000))]);
|
||||
const guess = value.totalRam * (value.totalRam <= 8 * 2 ** 30 ? 0.66 : 0.74);
|
||||
let chip = 'Apple Silicon';
|
||||
try { chip = (await run('sysctl', ['-n', 'machdep.cpu.brand_string'])).stdout.trim(); } catch { /* ignore */ }
|
||||
const total = wired > 0 ? wired : metal || guess;
|
||||
value.gpus.push({ name: `${chip} (pamięć zunifikowana)`, total, unified: true, limitSource: wired > 0 ? 'iogpu.wired_limit_mb' : metal ? 'Metal' : 'szacunek' });
|
||||
} else {
|
||||
try {
|
||||
const { stdout } = await run('nvidia-smi', ['--query-gpu=name,memory.total,memory.used', '--format=csv,noheader,nounits'], { timeout: 4000, windowsHide: true });
|
||||
for (const line of stdout.trim().split('\n')) {
|
||||
const [name, total, used] = line.split(',').map((s) => s.trim());
|
||||
if (name && Number(total)) value.gpus.push({ name, total: Number(total) * 2 ** 20, used: Number(used) * 2 ** 20 });
|
||||
}
|
||||
} catch { /* no NVIDIA driver */ }
|
||||
if (!value.gpus.length && process.platform === 'win32') value.gpus.push(...(await windowsGpus()));
|
||||
}
|
||||
} catch { /* no GPU info */ }
|
||||
sysCache = { at: Date.now(), value };
|
||||
return value;
|
||||
}
|
||||
Reference in New Issue
Block a user