Initialize repo

This commit is contained in:
Patryk Gensch
2026-11-15 15:39:55 +01:00
commit af37f7966d
27 changed files with 5678 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
node_modules/
.DS_Store
build/
dist/
+141
View File
@@ -0,0 +1,141 @@
# Tłumacz napisów SRT
Lokalna aplikacja webowa, która tłumaczy pliki `.srt` modelami LLM uruchomionymi u Ciebie:
**Ollama**, **LM Studio** albo dowolny serwer **OpenAI-compatible** (llama.cpp `llama-server`, vLLM, KoboldCpp, Jan…).
```bash
npm install # tylko @lmstudio/sdk (ładowanie modeli z KV-cache quant); bez niego też działa
npm start # → http://127.0.0.1:8765
npm test
```
Wymaga Node.js ≥ 20. Serwer nie ma innych zależności. `PORT` i `HOST` można ustawić zmiennymi środowiskowymi.
## Jak to działa — czemu ramki czasowe się nie rozjadą
1. Parser SRT zapisuje linie czasu **dosłownie**. Model ich w ogóle nie widzi: dostaje tylko tekst z numerami (`[17] …`).
2. Napisy idą partiami (domyślnie 25) razem z kilkoma poprzednimi (już przetłumaczonymi) i następnymi liniami jako kontekstem.
3. **Tryb JSON (domyślny):** odpowiedź jest ograniczona schematem JSON (Ollama `format`, LM Studio/OpenAI `response_format: json_schema`)
z dokładnie tymi kluczami, w tej kolejności. Klucz zawiera początek oryginału, np. `"17|The partner's office…"`. Gramatyka każe modelowi
napisać go tuż przed tłumaczeniem, więc każda wartość jest przypięta do swojego napisu. To usuwa klasyczny błąd, w którym od jakiegoś
miejsca wszystko jest przesunięte o jedną linię.
4. Wynik jest sprawdzany: czy nie brakuje id, czy odpowiedź nie została ucięta, czy model nie skopiował oryginału, nie scalił dwóch linii
i nie powtarza tłumaczeń. Jeśli coś jest nie tak, partia jest dzielona na pół, a pojedyncze problematyczne linie idą osobno.
Zdanie scalone z dwóch napisów (jedna linia pełna, druga pusta) jest dzielone automatycznie, proporcjonalnie do długości oryginałów.
5. Formatowanie obejmujące cały napis (`{\an8}`, `<i>…</i>`) jest zdejmowane przed tłumaczeniem i potem przywracane. Linie bez liter
(`♪ ♪`) są kopiowane bez pytania modelu. Za długie linie są zawijane (domyślnie do 42 znaków, maks. 2 linie).
Inne tryby: **numerowane linie** (dla serwerów bez structured output; odporny parser bierze ostatni kompletny blok odpowiedzi)
i **po jednej linii**. Jeśli model „myśli na głos” i w trybie linii ucina odpowiedzi, zadanie samo przełącza się na JSON.
**Piosenki (linie z ♪/♫ albo w starym stylu `# … #`)** mają trzy tryby: *tłumacz jak resztę*, *zostaw w oryginale*
albo *usuń z napisów*. W dwóch ostatnich linie piosenek w ogóle nie trafiają do modelu (szybciej, mniej tokenów) i nie są używane
jako kontekst. Usuwanie dzieje się przy eksporcie: napis z samą piosenką znika, a w napisie mieszanym („- ♪ Sto lat ♪ / - Przestań!”)
usuwane są tylko linie piosenki. Pozostałe napisy zachowują dokładnie swoje czasy i są numerowane od nowa. Wieloliniowa zwrotka
otwarta i zamknięta nutą w jednym napisie jest rozpoznawana w całości. Opisy dźwięków typu `[music playing]` nie są traktowane
jako piosenki. Zmiana trybu nie wymaga ponownego tłumaczenia.
W tabeli wyników tłumaczenia można edytować ręcznie, a każdą linię przetłumaczyć ponownie (↻). Podejrzane linie są oznaczane:
za krótkie lub za długie względem oryginału, bez zmian, >2 linie, za długie linie, za szybkie do przeczytania (>21 znaków/s).
Praca zapisuje się w przeglądarce, a „Dokończ brakujące” wznawia przerwane tłumaczenie. Eksport: samo tłumaczenie albo
napisy dwujęzyczne, LF/CRLF, opcjonalny BOM.
## Kontekst, KV-cache i pamięć
| | Długość kontekstu | Kwantyzacja KV-cache | Szacunek pamięci |
|---|---|---|---|
| **LM Studio** | przy ładowaniu modelu (przycisk „Załaduj model” albo automatycznie na starcie) | ✅ K i V osobno (`q8_0`, `q4_0`, …) przez `@lmstudio/sdk` | estymator LM Studio + własny |
| **Ollama** | `num_ctx` w każdym zapytaniu (Ollama sama przeładuje model) | ustawienie serwera: `OLLAMA_FLASH_ATTENTION=1`, `OLLAMA_KV_CACHE_TYPE=q8_0` | własny + **rzeczywiste** użycie z `/api/ps` po załadowaniu |
| **OpenAI-compat.** | przy starcie serwera (`llama-server -c 16384 -fa on -ctk q8_0 -ctv q8_0`) | argumenty serwera | własny (do planowania) |
Własny szacunek liczy KV-cache warstwa po warstwie na podstawie metadanych GGUF: z Ollamy przez `/api/show`, a dla LM Studio
z nagłówka pliku `.gguf` na dysku. Uwzględnia GQA, warstwy z przesuwnym oknem (Gemma), współdzielony KV-cache (Gemma 3n/4),
modele hybrydowe z warstwami rekurencyjnymi (Qwen3.5/Qwen3-Next), MLA (DeepSeek) i typ kwantyzacji K/V. Na Apple Silicon budżet
to `recommendedMaxWorkingSetSize` z Metal (ta sama wartość, której używają Ollama i llama.cpp; odczytywana raz przez `swift`,
a gdy go nie ma, szacowana na ~74% RAM), albo `iogpu.wired_limit_mb`, jeśli go podniesiono. Na NVIDII to wynik `nvidia-smi`. Bufory obliczeniowe są przybliżone, więc dla LM Studio wiarygodniejszy jest jego własny estymator.
Kwantyzacja V-cache w llama.cpp wymaga Flash Attention. `q8_0` praktycznie nie pogarsza jakości i zmniejsza KV-cache prawie o połowę.
## Pobieranie modeli z aplikacji
W karcie „2. Model” → „Pobierz nowy model” wpisujesz nazwę i widzisz pasek postępu z prędkością i czasem do końca.
Pobieranie trwa na serwerze, więc odświeżenie lub zamknięcie karty go nie przerywa (po powrocie pasek się
podpina). Podpowiedzi tylko wpisują nazwę, a pobieranie startuje dopiero przyciskiem. Modele, które nie zmieszczą
się w pamięci GPU, są oznaczone, a te już pobrane mają ✓.
- **Ollama:** nazwy z biblioteki (`translategemma:12b`) albo dowolne repozytorium GGUF z Hugging Face
(`hf.co/speakleash/Bielik-11B-v3.0-Instruct-GGUF:Q4_K_M`). Postęp przychodzi z `/api/pull`. Przerwanie zatrzymuje
pobieranie, a ponowne pobranie wznawia je od miejsca przerwania.
- **LM Studio:** nazwy z katalogu (`google/gemma-3-12b`) albo link do Hugging Face plus kwantyzacja
(`POST /api/v1/models/download` i odpytywanie statusu). API LM Studio nie ma anulowania, więc pobieranie
wstrzymuje się w panelu Downloads w samym LM Studio.
- Silnik OpenAI-compatible nie ma ujednoliconego pobierania.
## Modele
**Zwykłe LLM-y** (profil „Uniwersalny”) tłumaczą partiami z kontekstem: Gemma, Qwen, Llama, Mistral, Aya Expanse, a dla polskiego
warto też spróbować Bielika (SpeakLeash). Rozumowanie („thinking”) jest domyślnie wyłączane (`think: false` w Ollamie,
`reasoning_effort: "none"` w LM Studio), bo przy tłumaczeniu tylko spowalnia.
**Dedykowane modele tłumaczeniowe** są rozpoznawane po nazwie (profil można też wybrać ręcznie). Dostają swój oficjalny szablon
promptu i tłumaczą po jednym segmencie. Kwestie dialogowe (`- …`) idą linia po linii.
| Profil | Przykłady | Uwagi |
|---|---|---|
| TranslateGemma | `ollama pull translategemma:4b` / `:12b` / `:27b` | wymaga języka źródłowego (aplikacja go wykrywa) |
| Hunyuan-MT / HY-MT | `tencent/HY-MT1.5-7B` (GGUF od społeczności) | zalecane parametry ustawia przycisk „Zastosuj parametry zalecane” |
| Seed-X | `ByteDance-Seed/Seed-X-PPO-7B` | |
| Tower | `Unbabel/Tower-Plus-9B`, TowerInstruct | |
Modele MT tłumaczą zdania bez szerszego kontekstu, więc przy rodzaju gramatycznym i zwrotach (ty/Pan) radzą sobie gorzej niż
dobry LLM z kontekstem. Pole „Uwagi dla tłumacza” (płeć postaci, ton, słowniczek) trafia tylko do profilu uniwersalnego.
## API (dla skryptów)
`POST /api/srt/parse` (surowe bajty pliku) → napisy · `POST /api/jobs` → id zadania · `GET /api/jobs/:id/events` (SSE) ·
`GET /api/jobs/:id` · `POST /api/jobs/:id/cancel` · `POST /api/srt/build` → plik SRT ·
`POST /api/engine/{status,models,model-info,estimate,load,unload,loaded}`.
## Struktura
```
server.js serwer HTTP + API (bez frameworka)
src/srt.js parser/serializer SRT, wykrywanie kodowania, tagi, zawijanie
src/translator.js zadanie tłumaczenia: partie, walidacja, podziały, naprawy
src/prompts.js prompty, schemat JSON, szablony modeli MT, parser linii
src/vram.js szacowanie pamięci z metadanych GGUF, pamięć systemu
src/gguf.js czytnik nagłówka GGUF
src/engines/ ollama.js, lmstudio.js, openai.js
public/ interfejs (HTML/CSS/JS bez bundlera)
samples/ przykładowe napisy
```
## Wyniki testów (MacBook M4, 16 GB)
| Model | Silnik | Tryb | 40 linii | Uwagi |
|---|---|---|---|---|
| gemma-4-e4b Q4_K_M | LM Studio | JSON, partie po 15 | ~40 s | najlepsza jakość z testowanych; potrafi „myśleć na głos” w trybie linii, więc najlepiej JSON |
| translategemma:4b | Ollama | oficjalny szablon MT | ~25 s | szybka (~40 tok/s); bez kontekstu zgaduje rodzaj i ton, czasem przegaduje |
| translategemma:4b | Ollama | JSON z kontekstem | ~34 s | trafniejsza w kontekście, ale daje warianty „byłeś/aś” (oznaczane w tabeli) |
| qwen3.5-9b Q4_K_M | LM Studio | JSON | — | wolniejsza (~13 tok/s); 12 linii w ~15 s |
Szacunek KV-cache porównany z logami llama.cpp (TranslateGemma 4B, 32k): f16 — 814 MiB (szacunek 0.79 GiB),
q8_0 — 432 MiB (szacunek 0.42 GiB). Uwaga: Ollama 0.40 w `/api/ps` raportuje mniej, niż faktycznie zajmuje
(pomija część KV-cache), dlatego aplikacja pokazuje tę wartość jako „Ollama raportuje”, z ostrzeżeniem, gdy jest niespójna.
## Plik wykonywalny (bez instalowania Node.js)
```bash
npm run build:win # → dist/srt-translator-win-x64.zip (srt-translator.exe + JAK-URUCHOMIC.txt)
npm run build:mac # → dist/srt-translator-darwin-arm64.zip
```
Skrypt `scripts/build-exe.mjs` łączy serwer i wszystkie moduły w jeden plik (esbuild), a interfejs (`public/`) dodaje
jako zasoby. Potem wstrzykuje to do oficjalnej binarki Node.js dla systemu docelowego (Node.js Single Executable
Application). Binarki są pobierane raz z nodejs.org do `build/cache` i sprawdzane sumą SHA-256. Node z Homebrew ma
wyłączone SEA, więc do samego budowania skrypt używa wtedy oficjalnego Node dla bieżącego systemu. Wersja Node
uruchamiającego skrypt musi zgadzać się z wersją binarki docelowej.
Gotowy program uruchamia serwer, otwiera przeglądarkę i, jeśli 8765 jest zajęty, sam bierze następny wolny port.
Pliki nie są podpisane, więc Windows SmartScreen („Więcej informacji” → „Uruchom mimo to”) i macOS Gatekeeper
(prawy klik → Otwórz) pytają przy pierwszym uruchomieniu.
+645
View File
@@ -0,0 +1,645 @@
{
"name": "translate-srt",
"version": "0.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "translate-srt",
"version": "0.1.0",
"dependencies": {
"@lmstudio/sdk": "^2.1.0"
},
"devDependencies": {
"esbuild": "^0.28.2"
},
"engines": {
"node": ">=20"
}
},
"node_modules/@esbuild/aix-ppc64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz",
"integrity": "sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==",
"cpu": [
"ppc64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"aix"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/android-arm": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.28.2.tgz",
"integrity": "sha512-kXXoiPVVGQcnIYGOeaovwOURpniDBpSq4A03qkQ+BMQqtGG6HYap3xne9C1O1yo4TR3qxlCX5IqqmX6fFo2Lqg==",
"cpu": [
"arm"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"android"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/android-arm64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.28.2.tgz",
"integrity": "sha512-5YfKeeI8qWfBZIX+u2xZC3Zlb3Os/gLS2sbEKM+I4ZOcsWmHS2WLysCcQZDAFRslDUU5Oiq44gf6PYN1vGwG5A==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"android"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/android-x64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.28.2.tgz",
"integrity": "sha512-O387ite7SzUyCcy3JQX4P4bLtEA7bLLkx+esve5JHnyYfNTxcVpXZo9jhdB0lTKN44gztELTdU7nS8Nr16Fs1Q==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"android"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/darwin-arm64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.28.2.tgz",
"integrity": "sha512-n4KqkOQrraxHJcgjM1RvwbigfQKIKJVpM7xp+KsxiyUSrRdIXnt73VhrPAx0fV44hgfmIVKjxMN9J1t5jySVkw==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/darwin-x64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.28.2.tgz",
"integrity": "sha512-uq6suIWYP37qzGddBKPw5QEQPi6HiLGsO7UmkpfyaYNQ3D+rN6w6WfwH+nuqcGXWvawGwxOEroO4YGnFh95azw==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/freebsd-arm64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.28.2.tgz",
"integrity": "sha512-n+I0BTSRIoy+d6RPKnEVwql5UwBJolytvY4mAOIEJorKlqgPII8ix6slVVrfZ5Tnj7glIZvloylbB/EJPMWEXw==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"freebsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/freebsd-x64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.28.2.tgz",
"integrity": "sha512-78XJTJkvPs0kz2w61301PJjXl4g7q3JqiYMZ/M/yVI73EHBrCRTgkhu9oqG7vPqq+a/yadEW8aD+agKlk5xrmg==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"freebsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-arm": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.28.2.tgz",
"integrity": "sha512-XlDnu2q5yoqems+xay6wSAcg9DDD7K9RLKZEBOMZm3ckNpJBvOX20tSfby8KfrrhINDyv9V2YVZKY/SpoGJI8w==",
"cpu": [
"arm"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-arm64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.28.2.tgz",
"integrity": "sha512-pW4AC0P3it8c7do9MVM4p51FzHzdM/TZrerurgRcHJ2WTa1VQ1CIq18xncfpBJw4ojkiZZrKW2yIBWBP92j6Ug==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-ia32": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.28.2.tgz",
"integrity": "sha512-CYbnj78HsIeA+DhgUKgFCfvNsTHFhMMrinUrMZpDXJXKN8T3XViTZ/+wtHeVxEWY8ewSzTFN+nRmSwO2tZaLUQ==",
"cpu": [
"ia32"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-loong64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.28.2.tgz",
"integrity": "sha512-buwkd8nsph4R+ajRvw0qM5Hja/TXQow3ptzWO2EbG/cqcIkHloRrdlBtQlshyYGTNFvfkfJ5tpPLVkY4DtsPfQ==",
"cpu": [
"loong64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-mips64el": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.28.2.tgz",
"integrity": "sha512-ZVykbDyk7519VwiNb9Lcj9m8XM6v5V9uKPvrEMkkEedVewf+0itkhahp4HDpgERXhwLRpWFypsGbG/J8s0QjJA==",
"cpu": [
"mips64el"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-ppc64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.28.2.tgz",
"integrity": "sha512-CAXl+Dtd9UUuJd8pKKdwh6MLm3MUMiqMPmhZ3tTSXPqfyQ3vDl6R5hZdZ/kYojK4ofXtdfSv1tFq8XzWx3heNQ==",
"cpu": [
"ppc64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-riscv64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.28.2.tgz",
"integrity": "sha512-GeXCej4IQtU1B+QlDV8W/RRvbzI3O/Stss+/bCXv4lZls5WGRtu2a+3JkA3i4qIUlMXpcHebWpF8AkJhATowuA==",
"cpu": [
"riscv64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-s390x": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.28.2.tgz",
"integrity": "sha512-3H1weTYZPxt/WOhByszQZybS9w5lKzUn1FDMsgEChbHWQwHYQQRfBxgCcZvPhjHfKyJjIievvMmEUawJrdY9Dg==",
"cpu": [
"s390x"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/linux-x64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.28.2.tgz",
"integrity": "sha512-4xTZr1FUmSoQW4XIWmit3tzQrUTZM+N3P0XV8xROKYF50XfI7xeO90+1bZvNwxIufQ9hDQVRJH5YhgPVF8A/HQ==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/netbsd-arm64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.28.2.tgz",
"integrity": "sha512-sSATRjPeDBg3pdgHoQfoYBob11Kk1FGa9lui5RIHZCoCkJa9QKlvl3/vKz2usCmYYjs7ymJR/2Nnsqe+Hjt5nw==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"netbsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/netbsd-x64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.28.2.tgz",
"integrity": "sha512-lqnzCV+mM0gIADaKihiCg6ifgfU2L3h5E33rNQBN1Y4MaVGnzryzmvvf7UHxprpQdE8hpqLolJ9Rl+SkIRDpyw==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"netbsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/openbsd-arm64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.28.2.tgz",
"integrity": "sha512-AL2qJILH7lNjrDmCQDvdxMfAUIv8KMNZOvrwAQ8i8//ntL9FflhOyMJ8OZSMBb8/AWXe3/5v5S20y3zCoZWKoQ==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"openbsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/openbsd-x64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.28.2.tgz",
"integrity": "sha512-QtiuPytchRyC4rwUKhexJdQKvDuZ6hWloi3igqPQNUJCS1/v9EiO3UTOXR6A3FoMo4fnAKbWJdqaIwhOzh8qEw==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"openbsd"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/openharmony-arm64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.28.2.tgz",
"integrity": "sha512-WkhYDmpTjLvGlScA1rwjRUmhl4k8oXR3cIbtqWmELgU/dFeHHlEllxDvdWcNJV9rbzCexB5vz8gtNewWLgCT7Q==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"openharmony"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/sunos-x64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.28.2.tgz",
"integrity": "sha512-GPMSkTOtMnv2U2F8gxe4Io6qmVs+YKyp832Etqqxr0hFngmXQ3rzwytelm3GIn7T4VviRUlf3sOgBOiTdvaf7g==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"sunos"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/win32-arm64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.28.2.tgz",
"integrity": "sha512-PIhhEkE9uPBleRBrQEJpUn7MBnibZzbGzYWPmY3x+YoVg/95zbjB4CxPPOQ8l5tYYM4mMaCthF8/1DIfBQQyWQ==",
"cpu": [
"arm64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/win32-ia32": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.28.2.tgz",
"integrity": "sha512-YmJbfTlvU7Sdn9BB+4PRES4oB6pxgS37MAONj+hBr/cpXS1aBPKXxNnDbu+QCWPj0o9dgyxeq79g6c5P8KeuYA==",
"cpu": [
"ia32"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@esbuild/win32-x64": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.28.2.tgz",
"integrity": "sha512-5ebpxr3nWMzrL/rnUI755Jkuee0bHL/Gq0WTF9lvcpv73wAp5eu8MfBUgWK9bhWvZjj7yX8etf/8tI8Ney695g==",
"cpu": [
"x64"
],
"dev": true,
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">=18"
}
},
"node_modules/@lmstudio/lms-isomorphic": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/@lmstudio/lms-isomorphic/-/lms-isomorphic-0.5.0.tgz",
"integrity": "sha512-PunyIQL0vsiOlNHiLJUEcNnwOKqUmCFcNrTk7GwH0v2NCzKR6PctFpTtviHLFQyrXyS4D6aLThzqbC4YCBiuQg==",
"license": "Apache-2.0",
"dependencies": {
"ws": "^8.16.0"
}
},
"node_modules/@lmstudio/sdk": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/@lmstudio/sdk/-/sdk-2.1.0.tgz",
"integrity": "sha512-+SiuksZWTnSoZT8f7Mfdn19lLj1SXxHj9s1eLsDHsGHO4XKkSQKUf/bPdEXSym+9Ns4uK+rQh1yaA33Rt0ZteA==",
"license": "MIT",
"dependencies": {
"@lmstudio/lms-isomorphic": "^0.5.0",
"chalk": "^4.1.2",
"jsonschema": "^1.5.0",
"zod": "^3.22.4",
"zod-to-json-schema": "^3.22.5"
}
},
"node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"license": "MIT",
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"license": "MIT",
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"license": "MIT",
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"license": "MIT"
},
"node_modules/esbuild": {
"version": "0.28.2",
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.28.2.tgz",
"integrity": "sha512-HKVLS8dvII+xoKW9kmqxbRKrnWEXfJJr/FZhhJmiqIB0e053QNYFqOBouTMO/k5sID4MvCiUCvv8b9M4h32wIA==",
"dev": true,
"hasInstallScript": true,
"license": "MIT",
"bin": {
"esbuild": "bin/esbuild"
},
"engines": {
"node": ">=18"
},
"optionalDependencies": {
"@esbuild/aix-ppc64": "0.28.2",
"@esbuild/android-arm": "0.28.2",
"@esbuild/android-arm64": "0.28.2",
"@esbuild/android-x64": "0.28.2",
"@esbuild/darwin-arm64": "0.28.2",
"@esbuild/darwin-x64": "0.28.2",
"@esbuild/freebsd-arm64": "0.28.2",
"@esbuild/freebsd-x64": "0.28.2",
"@esbuild/linux-arm": "0.28.2",
"@esbuild/linux-arm64": "0.28.2",
"@esbuild/linux-ia32": "0.28.2",
"@esbuild/linux-loong64": "0.28.2",
"@esbuild/linux-mips64el": "0.28.2",
"@esbuild/linux-ppc64": "0.28.2",
"@esbuild/linux-riscv64": "0.28.2",
"@esbuild/linux-s390x": "0.28.2",
"@esbuild/linux-x64": "0.28.2",
"@esbuild/netbsd-arm64": "0.28.2",
"@esbuild/netbsd-x64": "0.28.2",
"@esbuild/openbsd-arm64": "0.28.2",
"@esbuild/openbsd-x64": "0.28.2",
"@esbuild/openharmony-arm64": "0.28.2",
"@esbuild/sunos-x64": "0.28.2",
"@esbuild/win32-arm64": "0.28.2",
"@esbuild/win32-ia32": "0.28.2",
"@esbuild/win32-x64": "0.28.2"
}
},
"node_modules/has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"license": "MIT",
"engines": {
"node": ">=8"
}
},
"node_modules/jsonschema": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/jsonschema/-/jsonschema-1.5.0.tgz",
"integrity": "sha512-K+A9hhqbn0f3pJX17Q/7H6yQfD/5OXgdrR5UE12gMXCiN9D5Xq2o5mddV2QEcX/bjla99ASsAAQUyMCCRWAEhw==",
"license": "MIT",
"engines": {
"node": "*"
}
},
"node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"license": "MIT",
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/ws": {
"version": "8.23.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.23.0.tgz",
"integrity": "sha512-wCkmX8QMOrKjgO8HW2WyDbTSGeqkocxgDzS9iR79rEAexty6/2qzilx3Ag8Tqf9ykop3irLKle0MtVn5qjVnoQ==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
},
"node_modules/zod": {
"version": "3.25.76",
"resolved": "https://registry.npmjs.org/zod/-/zod-3.25.76.tgz",
"integrity": "sha512-gzUt/qt81nXsFGKIFcC3YnfEAx5NkunCfnDlvuBSSFS02bcXu4Lmea0AFIUwbLWxWPx3d9p8S5QoaujKcNQxcQ==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/colinhacks"
}
},
"node_modules/zod-to-json-schema": {
"version": "3.25.2",
"resolved": "https://registry.npmjs.org/zod-to-json-schema/-/zod-to-json-schema-3.25.2.tgz",
"integrity": "sha512-O/PgfnpT1xKSDeQYSCfRI5Gy3hPf91mKVDuYLUHZJMiDFptvP41MSnWofm8dnCm0256ZNfZIM7DSzuSMAFnjHA==",
"license": "ISC",
"peerDependencies": {
"zod": "^3.25.28 || ^4"
}
}
}
}
+22
View File
@@ -0,0 +1,22 @@
{
"name": "translate-srt",
"version": "0.1.0",
"private": true,
"type": "module",
"description": "Web app translating SRT subtitles with local LLMs (Ollama, LM Studio, OpenAI-compatible)",
"scripts": {
"start": "node server.js",
"test": "node --test test/",
"build:win": "node scripts/build-exe.mjs win-x64",
"build:mac": "node scripts/build-exe.mjs darwin-arm64"
},
"engines": {
"node": ">=20"
},
"dependencies": {
"@lmstudio/sdk": "^2.1.0"
},
"devDependencies": {
"esbuild": "^0.28.2"
}
}
+1075
View File
File diff suppressed because it is too large Load Diff
+213
View File
@@ -0,0 +1,213 @@
<!doctype html>
<html lang="pl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tłumacz napisów SRT</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><rect width='32' height='32' rx='7' fill='%234f46e5'/><rect x='6' y='18' width='20' height='3' rx='1.5' fill='white'/><rect x='9' y='23' width='14' height='3' rx='1.5' fill='white'/></svg>">
<link rel="stylesheet" href="style.css">
</head>
<body>
<header class="topbar">
<div class="brand"><span class="logo"></span>Tłumacz napisów <b>SRT</b></div>
<div class="engine-pill" id="enginePill"><span class="dot"></span><span id="enginePillText">niepołączony</span></div>
</header>
<main class="layout">
<!-- ===================== LEFT: configuration ===================== -->
<aside class="side">
<section class="card" id="engineCard">
<h2>1. Silnik</h2>
<div class="seg" id="engineSeg">
<button data-kind="ollama">Ollama</button>
<button data-kind="lmstudio">LM Studio</button>
<button data-kind="openai">OpenAI-compat.</button>
</div>
<div class="row">
<input id="baseUrl" type="text" spellcheck="false" placeholder="http://127.0.0.1:11434">
<button id="connectBtn" class="btn">Połącz</button>
</div>
<div class="only-openai">
<label class="field"><span>Klucz API (opcjonalnie)</span><input id="apiKey" type="password" autocomplete="off"></label>
<label class="check"><input id="structuredOutput" type="checkbox" checked> Wymuszaj JSON (response_format json_schema)</label>
<label class="field"><span>Dodatkowe pola żądania (JSON)</span><textarea id="extraBody" rows="2" spellcheck="false" placeholder='{"chat_template_kwargs": {"enable_thinking": false}}'></textarea></label>
</div>
<p class="hint" id="engineHint"></p>
</section>
<section class="card" id="modelCard">
<h2>2. Model</h2>
<div class="row">
<select id="modelSelect" disabled><option>— najpierw połącz —</option></select>
<button id="refreshModels" class="btn icon" title="Odśwież listę">↻</button>
</div>
<div id="modelDetails" class="model-details"></div>
<label class="field"><span>Profil promptu</span>
<select id="profile"></select>
</label>
<p class="hint" id="profileNote"></p>
<details class="pull not-openai" id="pullBox">
<summary>Pobierz nowy model</summary>
<div class="pull-body">
<div class="row">
<input id="pullName" type="text" spellcheck="false" autocomplete="off">
<button id="pullBtn" class="btn">Pobierz</button>
</div>
<label class="field only-lmstudio"><span>Kwantyzacja <span class="muted">(tylko dla linków Hugging Face)</span></span><input id="pullQuant" type="text" spellcheck="false" placeholder="np. Q4_K_M"></label>
<div class="chips" id="pullChips"></div>
<p class="hint" id="pullHint"></p>
<div class="pull-list" id="pullList"></div>
</div>
</details>
</section>
<section class="card" id="memCard">
<h2>3. Kontekst i pamięć</h2>
<label class="field"><span>Długość kontekstu <b id="ctxLabel"></b></span>
<div class="row">
<input id="ctxRange" type="range" min="0" max="10" step="1">
<input id="contextLength" type="number" min="512" step="256" class="num">
</div>
</label>
<div class="grid2">
<label class="field"><span>KV-cache K</span><select id="kvCacheK"></select></label>
<label class="field"><span>KV-cache V</span><select id="kvCacheV"></select></label>
</div>
<label class="check"><input id="flashAttention" type="checkbox" checked> Flash Attention <span class="muted only-server">(ustawienie serwera)</span></label>
<div class="grid2">
<label class="field only-lmstudio"><span>Offload na GPU</span>
<select id="gpuRatio"><option value="">auto</option><option value="1">max (100%)</option><option value="0.75">75%</option><option value="0.5">50%</option><option value="0.25">25%</option><option value="0">wyłączony (CPU)</option></select></label>
<label class="field only-ollama"><span>Warstwy na GPU (num_gpu)</span><input id="numGpu" type="number" min="-1" placeholder="auto"></label>
<label class="field not-openai"><span>Batch ewaluacji</span><input id="evalBatchSize" type="number" min="32" step="32" placeholder="auto"></label>
</div>
<label class="check only-lmstudio"><input id="unloadOthers" type="checkbox" checked> Przy ładowaniu zwolnij inne modele (więcej wolnej pamięci)</label>
<p class="hint only-server" id="kvServerHint"></p>
<div class="vram" id="vram">
<div class="vram-head"><span>Szacowane użycie pamięci</span><b id="vramTotal">—</b></div>
<div class="vram-bar"><div class="seg-w"></div><div class="seg-kv"></div><div class="seg-c"></div><div class="limit"></div></div>
<div class="vram-legend">
<span><i class="sw w"></i>wagi <b id="vW">—</b></span>
<span><i class="sw kv"></i>KV-cache <b id="vKV">—</b></span>
<span><i class="sw c"></i>bufory <b id="vC">—</b></span>
</div>
<div class="vram-lines" id="vramLines"></div>
</div>
<div class="row load-row not-openai">
<button id="loadBtn" class="btn">Załaduj model</button>
<button id="unloadBtn" class="btn ghost">Zwolnij</button>
</div>
<div id="loadedInfo" class="hint"></div>
</section>
<section class="card" id="genCard">
<h2>4. Generowanie</h2>
<div class="grid3">
<label class="field"><span>Temperatura</span><input id="temperature" type="number" step="0.05" min="0" max="2" value="0.3"></label>
<label class="field"><span>top_p</span><input id="topP" type="number" step="0.05" min="0" max="1" placeholder="—"></label>
<label class="field"><span>top_k</span><input id="topK" type="number" step="1" min="0" placeholder="—"></label>
<label class="field"><span>min_p</span><input id="minP" type="number" step="0.01" min="0" max="1" placeholder="—"></label>
<label class="field"><span>Kara za powt.</span><input id="repeatPenalty" type="number" step="0.01" min="0.8" max="2" placeholder="—"></label>
<label class="field"><span>Seed</span><input id="seed" type="number" placeholder="—"></label>
</div>
<label class="check"><input id="disableThinking" type="checkbox" checked> Wyłącz „myślenie” (reasoning) — szybciej, a do tłumaczenia zwykle zbędne</label>
<button id="applyRecommended" class="btn ghost small" hidden>Zastosuj parametry zalecane dla modelu</button>
</section>
<section class="card" id="trCard">
<h2>5. Tłumaczenie</h2>
<div class="grid2">
<label class="field"><span>Z języka</span><select id="source"></select></label>
<label class="field"><span>Na język</span><select id="target"></select></label>
</div>
<div class="grid2">
<label class="field"><span>Tryb</span>
<select id="mode">
<option value="auto">auto</option>
<option value="json">partie · JSON (zalecany)</option>
<option value="lines">partie · numerowane linie</option>
<option value="single">po jednej linii</option>
</select></label>
<label class="field"><span>Linii w partii</span><input id="batchSize" type="number" min="1" max="200" value="25"></label>
<label class="field"><span>Kontekst przed</span><input id="contextBefore" type="number" min="0" max="50" value="6"></label>
<label class="field"><span>Kontekst po</span><input id="contextAfter" type="number" min="0" max="20" value="3"></label>
<label class="field"><span>Maks. znaków w linii</span><input id="maxLineLength" type="number" min="0" max="120" value="42" title="0 = nie zawijaj"></label>
<label class="field"><span>Równoległość</span><input id="concurrency" type="number" min="1" max="8" value="1" title="Liczba jednoczesnych zapytań"></label>
</div>
<label class="field"><span>Piosenki (linie z ♪) <span class="muted" id="songCount"></span></span>
<select id="songs">
<option value="translate">tłumacz jak resztę</option>
<option value="keep">zostaw w oryginale</option>
<option value="remove">usuń z napisów</option>
</select></label>
<label class="field"><span>Uwagi dla tłumacza <span class="muted">(kontekst, imiona, płeć postaci, ton, słowniczek)</span></span>
<textarea id="notes" rows="3" placeholder="Np. Serial kryminalny. Morgan — mężczyzna, Sarah — kobieta. Zwracają się do siebie na „ty”. „Precinct” = „komisariat”."></textarea></label>
</section>
</aside>
<!-- ===================== RIGHT: file + results ===================== -->
<section class="content">
<div id="drop" class="drop">
<input id="fileInput" type="file" accept=".srt,text/plain" hidden>
<div class="drop-inner">
<div class="drop-icon"></div>
<p><b>Upuść plik .srt</b> albo <button class="linkbtn" id="pickFile">wybierz z dysku</button></p>
<p class="muted">Kodowanie wykrywane automatycznie (UTF-8, Windows-1250, …). Znaczniki czasu nie są wysyłane do modelu.</p>
</div>
</div>
<div id="fileBar" class="filebar" hidden>
<div class="fileinfo">
<b id="fileName"></b>
<span id="fileStats" class="muted"></span>
<label class="inline">kodowanie
<select id="encoding">
<option value="auto">auto</option><option value="utf-8">UTF-8</option><option value="windows-1250">Windows-1250</option>
<option value="windows-1252">Windows-1252</option><option value="iso-8859-2">ISO-8859-2</option><option value="windows-1251">Windows-1251</option>
</select></label>
<button class="linkbtn" id="changeFile">zmień plik</button>
</div>
<div class="actions">
<button id="startBtn" class="btn primary">Tłumacz</button>
<button id="resumeBtn" class="btn" hidden>Dokończ brakujące</button>
<button id="stopBtn" class="btn danger" hidden>Zatrzymaj</button>
</div>
</div>
<div id="progressBox" class="progress" hidden>
<div class="bar"><div id="progressFill"></div></div>
<div class="pstats" id="progressStats"></div>
</div>
<div id="tableTools" class="tabletools" hidden>
<div class="filters">
<label class="check"><input type="checkbox" id="onlyFlagged"> tylko oznaczone <span id="flagCount" class="badge warn"></span></label>
<input id="search" type="search" placeholder="Szukaj…">
</div>
<div class="export">
<select id="exportFormat"><option value="translated">tylko tłumaczenie</option><option value="bilingual">dwujęzyczne</option></select>
<select id="exportEol"><option value="orig">końce linii jak w oryginale</option><option value="lf">LF</option><option value="crlf">CRLF (Windows)</option></select>
<label class="check"><input type="checkbox" id="exportBom"> BOM</label>
<button id="downloadBtn" class="btn primary">Pobierz SRT</button>
</div>
</div>
<div id="tableWrap" class="tablewrap" hidden>
<table class="cues">
<thead><tr><th class="c-id">#</th><th class="c-time">czas</th><th>oryginał</th><th>tłumaczenie <span class="muted">(kliknij, by edytować)</span></th><th class="c-act"></th></tr></thead>
<tbody id="cueBody"></tbody>
</table>
</div>
<details class="logbox" id="logBox" hidden>
<summary>Dziennik <span id="logCount" class="muted"></span></summary>
<div id="log"></div>
</details>
</section>
</main>
<div id="toast" class="toast" hidden></div>
<script type="module" src="app.js"></script>
</body>
</html>
+245
View File
@@ -0,0 +1,245 @@
:root {
--bg: #f5f6f8;
--panel: #ffffff;
--text: #1c1f26;
--muted: #6b7280;
--line: #e4e7ec;
--line-strong: #d0d5dd;
--accent: #4f46e5;
--accent-soft: #eef0ff;
--accent-text: #ffffff;
--ok: #12a150;
--warn: #c27c0e;
--warn-soft: #fff6e5;
--err: #d92d20;
--err-soft: #fdecea;
--w: #6366f1;
--kv: #f59e0b;
--c: #94a3b8;
--row-hover: #fafbfc;
--shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
--radius: 10px;
--mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
color-scheme: light;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0f1115;
--panel: #171a21;
--text: #e6e8ec;
--muted: #8b93a1;
--line: #262b35;
--line-strong: #343a46;
--accent: #818cf8;
--accent-soft: #1f2240;
--accent-text: #0f1115;
--ok: #3ccf7f;
--warn: #f0b33c;
--warn-soft: #2b2312;
--err: #ff6b5e;
--err-soft: #2e1714;
--w: #818cf8;
--kv: #fbbf24;
--c: #64748b;
--row-hover: #1b1f27;
--shadow: none;
color-scheme: dark;
}
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
background: var(--bg);
color: var(--text);
font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.topbar {
position: sticky; top: 0; z-index: 10;
display: flex; align-items: center; justify-content: space-between;
padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 16px; }
.logo { width: 22px; height: 22px; border-radius: 6px; background: var(--accent); position: relative; }
.logo::before, .logo::after { content: ""; position: absolute; left: 4px; right: 4px; height: 3px; border-radius: 2px; background: var(--accent-text); }
.logo::before { bottom: 8px; } .logo::after { bottom: 4px; left: 6px; right: 6px; }
.engine-pill { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); padding: 4px 10px; border: 1px solid var(--line); border-radius: 999px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line-strong); }
.engine-pill.ok .dot { background: var(--ok); }
.engine-pill.err .dot { background: var(--err); }
.layout { display: grid; grid-template-columns: 380px minmax(0, 1fr); gap: 16px; padding: 16px 20px 40px; align-items: start; }
.side { display: flex; flex-direction: column; gap: 12px; position: sticky; top: 64px; max-height: calc(100vh - 80px); overflow-y: auto; padding-right: 4px; scrollbar-width: thin; }
.content { min-width: 0; display: flex; flex-direction: column; gap: 12px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 10px; }
.card h2 { margin: 0; font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; }
.row > input[type=text], .row > select { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); min-width: 0; }
.field > span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.field b { color: var(--text); font-weight: 600; }
.check { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; cursor: pointer; }
.check input { margin: 2px 0 0; accent-color: var(--accent); }
.muted { color: var(--muted); }
.hint { margin: 0; font-size: 12px; color: var(--muted); }
.hint:empty { display: none; }
.hint code, .model-details code { font-family: var(--mono); font-size: 11.5px; background: var(--bg); padding: 1px 4px; border-radius: 4px; word-break: break-all; }
input[type=text], input[type=password], input[type=number], input[type=search], select, textarea {
width: 100%; padding: 7px 9px; border: 1px solid var(--line-strong); border-radius: 7px; background: var(--panel); outline: none; min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; }
.num { width: 96px !important; flex: none; }
input[type=range] { flex: 1; accent-color: var(--accent); }
.btn { padding: 7px 14px; border-radius: 7px; border: 1px solid var(--line-strong); background: var(--panel); cursor: pointer; white-space: nowrap; font-weight: 500; }
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); }
.btn.danger { color: var(--err); border-color: var(--err); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 12px; align-self: flex-start; }
.btn.icon { padding: 7px 10px; }
.btn.busy { position: relative; color: transparent !important; }
.btn.busy::after { content: ""; position: absolute; inset: 0; margin: auto; width: 14px; height: 14px; border: 2px solid var(--muted); border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.linkbtn { background: none; border: none; padding: 0; color: var(--accent); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--line-strong); border-radius: 8px; overflow: hidden; }
.seg button { border: none; background: transparent; padding: 7px 4px; cursor: pointer; font-size: 13px; }
.seg button + button { border-left: 1px solid var(--line-strong); }
.seg button.active { background: var(--accent); color: var(--accent-text); font-weight: 600; }
body[data-engine=ollama] .only-lmstudio, body[data-engine=ollama] .only-openai,
body[data-engine=lmstudio] .only-ollama, body[data-engine=lmstudio] .only-openai, body[data-engine=lmstudio] .only-server,
body[data-engine=openai] .only-lmstudio, body[data-engine=openai] .only-ollama, body[data-engine=openai] .not-openai { display: none !important; }
.model-details { font-size: 12px; color: var(--muted); display: flex; flex-wrap: wrap; gap: 4px 10px; }
.model-details:empty { display: none; }
.badge { display: inline-block; padding: 0 6px; border-radius: 999px; font-size: 11px; font-weight: 600; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.badge.ok { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.err { background: var(--err-soft); color: var(--err); }
.badge:empty { display: none; }
.vram { border: 1px solid var(--line); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; background: var(--bg); }
.vram-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.vram-head b { color: var(--text); font-size: 13px; }
.vram-bar { position: relative; height: 12px; border-radius: 6px; background: var(--line); display: flex; overflow: visible; }
.vram-bar > div[class^=seg] { height: 100%; transition: width .25s; }
.vram-bar .seg-w { background: var(--w); border-radius: 6px 0 0 6px; }
.vram-bar .seg-kv { background: var(--kv); }
.vram-bar .seg-c { background: var(--c); border-radius: 0 6px 6px 0; }
.vram-bar .limit { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--err); border-radius: 1px; display: none; }
.vram.over .vram-head b { color: var(--err); }
.vram-legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.vram-legend b { color: var(--text); font-weight: 600; }
.sw { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; vertical-align: 0; }
.sw.w { background: var(--w); } .sw.kv { background: var(--kv); } .sw.c { background: var(--c); }
.vram-lines { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 3px; }
.vram-lines b { color: var(--text); }
.vram-lines .over { color: var(--err); font-weight: 600; }
.drop { border: 2px dashed var(--line-strong); border-radius: var(--radius); background: var(--panel); padding: 48px 20px; text-align: center; transition: border-color .15s, background .15s; cursor: pointer; }
.drop.drag { border-color: var(--accent); background: var(--accent-soft); }
.drop p { margin: 6px 0; }
.drop-icon { width: 44px; height: 52px; margin: 0 auto 10px; border-radius: 6px; border: 2px solid var(--muted); position: relative; opacity: .7; }
.drop-icon::before, .drop-icon::after { content: ""; position: absolute; left: 8px; right: 8px; height: 3px; border-radius: 2px; background: var(--muted); }
.drop-icon::before { bottom: 16px; } .drop-icon::after { bottom: 9px; right: 14px; }
.filebar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; box-shadow: var(--shadow); }
.fileinfo { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; min-width: 0; }
.fileinfo b { word-break: break-all; }
.inline { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.inline select { width: auto; padding: 3px 6px; }
.actions { display: flex; gap: 8px; }
.progress { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; display: flex; flex-direction: column; gap: 8px; }
.progress .bar { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; }
#progressFill { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.progress.done #progressFill { background: var(--ok); }
.progress.error #progressFill { background: var(--err); }
.pstats { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 12px; color: var(--muted); }
.pstats b { color: var(--text); }
.tabletools { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; align-items: center; }
.filters, .export { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.filters input[type=search] { width: 200px; }
.export select { width: auto; }
.tablewrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; box-shadow: var(--shadow); max-height: calc(100vh - 250px); }
table.cues { width: 100%; border-collapse: collapse; table-layout: fixed; }
.cues thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--line); }
.cues td { padding: 7px 10px; border-bottom: 1px solid var(--line); vertical-align: top; white-space: pre-wrap; word-wrap: break-word; }
.cues tr:hover td { background: var(--row-hover); }
.c-id { width: 52px; } .c-time { width: 104px; } .c-act { width: 40px; }
td.id { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
td.time { color: var(--muted); font-family: var(--mono); font-size: 11px; line-height: 1.5; }
td.tr { outline: none; border-left: 2px solid transparent; }
td.tr:focus { background: var(--accent-soft); border-left-color: var(--accent); }
td.tr.pending { color: var(--muted); font-style: italic; }
td.tr.edited { border-left-color: var(--ok); }
tr.active td { background: var(--accent-soft); }
tr.flagged td.tr { background: var(--warn-soft); }
tr.removed td.src, tr.removed td.tr { text-decoration: line-through; opacity: .5; }
.badge.song { background: color-mix(in srgb, var(--muted) 18%, transparent); color: var(--muted); }
tr.err td.tr { background: var(--err-soft); }
.flags { display: block; margin-top: 4px; }
.flags .badge { margin-right: 4px; font-weight: 500; }
.rowbtn { border: none; background: none; cursor: pointer; color: var(--muted); padding: 2px 6px; border-radius: 5px; font-size: 15px; }
.rowbtn:hover { color: var(--accent); background: var(--accent-soft); }
.rowbtn.busy { animation: spin 1s linear infinite; }
.logbox { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 14px; }
.logbox summary { cursor: pointer; font-size: 13px; font-weight: 500; }
#log { font-family: var(--mono); font-size: 11.5px; max-height: 240px; overflow: auto; margin-top: 8px; }
#log div { padding: 1px 0; }
#log .warn { color: var(--warn); }
#log .error { color: var(--err); }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-size: 13px; z-index: 100; max-width: min(640px, 90vw); box-shadow: 0 8px 24px rgba(0,0,0,.2); }
.toast.err { background: var(--err); color: #fff; }
@media (max-width: 900px) {
.layout { grid-template-columns: 1fr; padding: 12px; }
.side { position: static; max-height: none; overflow: visible; }
.c-time { width: 0; } td.time, th.c-time { display: none; }
.tablewrap { max-height: none; }
}
/* model downloads */
.pull { border-top: 1px solid var(--line); padding-top: 8px; }
.pull summary { cursor: pointer; font-size: 13px; font-weight: 500; }
.pull-body { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.pull-body .row > input { flex: 1; min-width: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--line-strong); background: var(--panel); border-radius: 999px; padding: 3px 10px; font-size: 12px; cursor: pointer; }
.chip:hover:not(:disabled) { border-color: var(--accent); }
.chip:disabled { opacity: .55; cursor: default; }
.chip.warn { border-color: color-mix(in srgb, var(--warn) 60%, transparent); }
.pull-list { display: flex; flex-direction: column; gap: 8px; }
.pull-list:empty { display: none; }
.pull-item { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: var(--bg); display: flex; flex-direction: column; gap: 6px; }
.pull-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.pull-name { font-size: 12px; word-break: break-all; }
.pull-item .bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
.pull-item .bar > div { height: 100%; width: 0; background: var(--accent); transition: width .3s; }
.pull-item.done .bar > div { background: var(--ok); }
.pull-item.error .bar > div { background: var(--err); width: 100% !important; }
.pull-item.cancelled .bar > div { background: var(--muted); }
.pull-item.indeterminate .bar > div { width: 30% !important; animation: slide 1.2s ease-in-out infinite; }
@keyframes slide { from { margin-left: -30%; } to { margin-left: 100%; } }
.pull-stats { font-size: 12px; color: var(--muted); }
.pull-actions { display: flex; gap: 6px; justify-content: flex-end; }
.pull-actions:empty { display: none; }
.pull-item.paused .bar > div { background: var(--warn); }
.pull-item.error .pull-stats { color: var(--err); }
+52
View File
@@ -0,0 +1,52 @@
1
00:00:01,000 --> 00:00:03,200
Where were you last night?
2
00:00:03,400 --> 00:00:06,100
I told you,
I was at my sister's place.
3
00:00:06,300 --> 00:00:08,000
<i>She's lying. I can always tell.</i>
4
00:00:08,200 --> 00:00:10,500
- You don't believe me?
- Should I?
5
00:00:10,700 --> 00:00:12,000
♪ ♪
6
00:00:12,200 --> 00:00:15,900
{\an8}Detective Morgan wants to see you
in his office. Now.
7
00:00:16,100 --> 00:00:18,400
Tell him I'll be there
in five minutes.
8
00:00:18,600 --> 00:00:21,000
He said that an hour ago.
9
00:00:21,200 --> 00:00:23,500
[door slams]
10
00:00:23,700 --> 00:00:26,800
Look, whatever you think happened,
it's not what it looks like.
11
00:00:27,000 --> 00:00:29,300
It never is, is it?
12
00:00:29,500 --> 00:00:32,000
Grab your coat. We're leaving.
+159
View File
@@ -0,0 +1,159 @@
1
00:00:01,000 --> 00:00:03,960
Morning, Sarah. You're in early.
2
00:00:04,210 --> 00:00:08,435
Couldn't sleep. The Hendricks case is driving me crazy.
3
00:00:08,685 --> 00:00:11,480
Still nothing on the husband?
4
00:00:11,730 --> 00:00:15,955
His alibi checks out. Bowling league, twelve witnesses.
5
00:00:16,205 --> 00:00:20,320
Twelve witnesses who'd lie for a free round of beers.
6
00:00:20,570 --> 00:00:23,365
That's cynical, even for you.
7
00:00:23,615 --> 00:00:26,905
I've been doing this for twenty years.
8
00:00:27,155 --> 00:00:30,940
Cynical is just experience with a bad attitude.
9
00:00:31,190 --> 00:00:34,370
Okay, so if it wasn't the husband...
10
00:00:34,620 --> 00:00:37,965
Then who had a reason to want her gone?
11
00:00:38,215 --> 00:00:42,605
The business partner. He's been dodging my calls all week.
12
00:00:42,855 --> 00:00:45,265
Let's pay him a visit.
13
00:00:45,515 --> 00:00:47,045
- Now?
14
00:00:47,295 --> 00:00:50,750
- Unless you have somewhere better to be.
15
00:00:51,000 --> 00:00:54,455
Give me five minutes to finish my coffee.
16
00:00:54,705 --> 00:00:56,730
You've got two.
17
00:00:56,980 --> 00:01:01,040
<i>The partner's office was on the twelfth floor</i>
18
00:01:01,290 --> 00:01:05,075
<i>of a building that had seen better days.</i>
19
00:01:05,325 --> 00:01:09,605
Mr. Callahan? Detective Reyes. This is Detective Morgan.
20
00:01:09,855 --> 00:01:13,530
I already told your people everything I know.
21
00:01:13,780 --> 00:01:16,960
Then it won't hurt to tell us again.
22
00:01:17,210 --> 00:01:20,830
Laura and I built this company from nothing.
23
00:01:21,080 --> 00:01:23,875
Why would I want to hurt her?
24
00:01:24,125 --> 00:01:28,295
Insurance policies have a way of changing friendships.
25
00:01:28,545 --> 00:01:32,770
That's ridiculous. I didn't even know about the policy.
26
00:01:33,020 --> 00:01:35,925
Your signature is on page four.
27
00:01:36,175 --> 00:01:38,200
[phone ringing]
28
00:01:38,450 --> 00:01:40,750
I need to take this.
29
00:01:41,000 --> 00:01:43,410
Of course. We'll wait.
30
00:01:43,660 --> 00:01:45,465
He's lying.
31
00:01:45,715 --> 00:01:48,345
Obviously. But about what?
32
00:01:48,595 --> 00:01:52,050
Did you see his hands? They were shaking.
33
00:01:52,300 --> 00:01:55,205
People get nervous around cops.
34
00:01:55,455 --> 00:01:58,690
Not like that. That man is terrified.
35
00:01:58,940 --> 00:02:00,470
Of us?
36
00:02:00,720 --> 00:02:04,615
No. Of whoever is on the other end of that phone.
37
00:02:04,865 --> 00:02:08,705
I'm sorry, Detectives. Something urgent came up.
38
00:02:08,955 --> 00:02:12,740
I'm afraid we'll have to continue another time.
39
00:02:12,990 --> 00:02:15,895
Don't leave town, Mr. Callahan.
40
00:02:16,145 --> 00:02:18,500
I wasn't planning to.
+30
View File
@@ -0,0 +1,30 @@
1
00:00:01,000 --> 00:00:03,000
♪ ♪
2
00:00:03,500 --> 00:00:07,000
<i>♪ I've been waiting
all my life
for you ♪</i>
3
00:00:07,200 --> 00:00:09,500
- ♪ Happy birthday to you ♪
- Okay, stop singing!
4
00:00:09,800 --> 00:00:12,000
Who taught you to sing like that?
5
00:00:12,200 --> 00:00:14,000
# Jingle bells, jingle bells #
6
00:00:14,300 --> 00:00:17,000
My grandmother. She was a jazz singer.
7
00:00:17,200 --> 00:00:19,500
[music playing]
+24
View File
@@ -0,0 +1,24 @@
TLUMACZ NAPISOW SRT - jak uruchomic
====================================
Nie trzeba niczego instalowac poza programem z modelem jezykowym (Ollama albo LM Studio).
1. Zainstaluj Ollame (https://ollama.com) albo LM Studio (https://lmstudio.ai)
i pobierz model, np. w Ollamie (w terminalu / PowerShellu):
ollama pull translategemma:4b
(wiekszy i lepszy: translategemma:12b - potrzebuje ok. 8 GB pamieci karty graficznej)
2. Uruchom srt-translator.exe (dwuklik).
- Windows moze pokazac "System Windows ochronil ten komputer" (plik nie jest podpisany):
kliknij "Wiecej informacji" -> "Uruchom mimo to".
- Otworzy sie czarne okno (to serwer aplikacji - nie zamykaj go)
i przegladarka z adresem http://127.0.0.1:8765
- Jesli przegladarka sie nie otworzy, wpisz ten adres recznie.
3. W aplikacji: wybierz silnik (Ollama / LM Studio), kliknij "Polacz", wybierz model,
wrzuc plik .srt i kliknij "Tlumacz". Na koniec "Pobierz SRT".
LM Studio: w zakladce "Developer" wlacz serwer (Start Server).
Zamkniecie czarnego okna wylacza aplikacje. Nic nie jest wysylane do internetu -
tlumaczenie dzieje sie na Twoim komputerze.
+125
View File
@@ -0,0 +1,125 @@
#!/usr/bin/env node
// Builds a single-file executable (Node.js SEA) that needs no Node.js on the target machine.
// node scripts/build-exe.mjs win-x64 (also: darwin-arm64, darwin-x64, linux-x64)
// The official Node.js binary for the target is downloaded from nodejs.org once (SHA-256 verified)
// and cached in build/cache. Its version must match the Node.js running this script.
import { build } from 'esbuild';
import { execFileSync } from 'node:child_process';
import { createHash } from 'node:crypto';
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync, statSync, rmSync, copyFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const BUILD = path.join(ROOT, 'build');
const CACHE = path.join(BUILD, 'cache');
const DIST = path.join(ROOT, 'dist');
const VERSION = process.versions.node;
const target = process.argv[2] || 'win-x64';
const TARGETS = {
'win-x64': { archive: `node-v${VERSION}-win-x64.zip`, inner: `node-v${VERSION}-win-x64/node.exe`, exe: 'srt-translator.exe' },
'darwin-arm64': { archive: `node-v${VERSION}-darwin-arm64.tar.xz`, inner: `node-v${VERSION}-darwin-arm64/bin/node`, exe: 'srt-translator' },
'darwin-x64': { archive: `node-v${VERSION}-darwin-x64.tar.xz`, inner: `node-v${VERSION}-darwin-x64/bin/node`, exe: 'srt-translator' },
'linux-x64': { archive: `node-v${VERSION}-linux-x64.tar.xz`, inner: `node-v${VERSION}-linux-x64/bin/node`, exe: 'srt-translator' },
};
const t = TARGETS[target];
if (!t) throw new Error(`Unknown target ${target}. Use one of: ${Object.keys(TARGETS).join(', ')}`);
const log = (m) => console.log(`[build] ${m}`);
mkdirSync(CACHE, { recursive: true });
mkdirSync(DIST, { recursive: true });
// 1) Bundle the server and all modules (incl. @lmstudio/sdk) into one CommonJS file.
const bundle = path.join(BUILD, 'app.cjs');
await build({
entryPoints: [path.join(ROOT, 'server.js')],
bundle: true,
platform: 'node',
format: 'cjs',
target: `node${VERSION.split('.')[0]}`,
outfile: bundle,
external: ['bufferutil', 'utf-8-validate'], // optional native add-ons of `ws`
logLevel: 'warning',
logOverride: { 'empty-import-meta': 'silent' }, // handled in server.js
legalComments: 'none',
});
log(`bundle: ${path.relative(ROOT, bundle)} (${(statSync(bundle).size / 1024).toFixed(0)} KB)`);
// 2) Official Node.js binaries (cached, checksum-verified): one to inject into (target) and one
// to run the build if this Node has SEA disabled (Homebrew builds do).
const base = `https://nodejs.org/dist/v${VERSION}`;
const sumsPath = path.join(CACHE, `SHASUMS256-v${VERSION}.txt`);
async function download(url, dest) {
log(`downloading ${url}`);
const res = await fetch(url);
if (!res.ok) throw new Error(`${url}: HTTP ${res.status}`);
writeFileSync(dest, Buffer.from(await res.arrayBuffer()));
}
async function ensureNode(name) {
const spec = TARGETS[name];
if (!spec) throw new Error(`No official Node.js build known for ${name}`);
if (!existsSync(sumsPath)) {
const legacy = path.join(CACHE, 'SHASUMS256.txt');
if (existsSync(legacy) && readFileSync(legacy, 'utf8').includes(`v${VERSION}`)) copyFileSync(legacy, sumsPath);
else await download(`${base}/SHASUMS256.txt`, sumsPath);
}
const archivePath = path.join(CACHE, spec.archive);
if (!existsSync(archivePath)) await download(`${base}/${spec.archive}`, archivePath);
const expected = readFileSync(sumsPath, 'utf8').split('\n').find((l) => l.endsWith(` ${spec.archive}`))?.split(/\s+/)[0];
const actual = createHash('sha256').update(readFileSync(archivePath)).digest('hex');
if (!expected || expected !== actual) throw new Error(`Checksum mismatch for ${spec.archive}: expected ${expected}, got ${actual}`);
const dir = path.join(CACHE, `node-${VERSION}-${name}`);
const bin = path.join(dir, spec.inner);
if (!existsSync(bin)) {
mkdirSync(dir, { recursive: true });
execFileSync('tar', ['-xf', archivePath, '-C', dir, spec.inner], { stdio: 'inherit' }); // bsdtar also reads .zip
}
log(`node ${VERSION} for ${name}: checksum OK`);
return bin;
}
const nodeBin = await ensureNode(target);
function seaEnabled(bin) {
try {
execFileSync(bin, ['--experimental-sea-config', path.join(BUILD, 'nonexistent.json')], { stdio: 'pipe' });
} catch (e) {
return !/Single executable application is disabled/.test(String(e.stderr) + String(e.stdout));
}
return true;
}
let builder = process.execPath;
if (!seaEnabled(builder)) {
const host = `${process.platform === 'win32' ? 'win' : process.platform}-${process.arch}`;
log(`this Node has SEA disabled (${builder}) — using the official Node.js for ${host} to build`);
builder = await ensureNode(host);
}
// 3) Single executable: bundle + UI files as assets, injected into the target's node binary.
const assets = Object.fromEntries(readdirSync(path.join(ROOT, 'public')).map((f) => [f, path.join(ROOT, 'public', f)]));
const outDir = path.join(DIST, target);
rmSync(outDir, { recursive: true, force: true });
mkdirSync(outDir, { recursive: true });
const output = path.join(outDir, t.exe);
const seaConfig = path.join(BUILD, `sea-config-${target}.json`);
writeFileSync(seaConfig, JSON.stringify({
main: bundle,
mainFormat: 'commonjs',
executable: nodeBin,
output,
disableExperimentalSEAWarning: true,
useSnapshot: false, // must be off for cross-platform builds
useCodeCache: false,
assets,
}, null, 2));
execFileSync(builder, ['--build-sea', seaConfig], { stdio: 'inherit' });
if (target.startsWith('darwin') && process.platform === 'darwin') execFileSync('codesign', ['--sign', '-', '--force', output], { stdio: 'inherit' });
log(`executable: ${path.relative(ROOT, output)} (${(statSync(output).size / 2 ** 20).toFixed(1)} MB)`);
// 4) Short instructions for whoever gets the file, then a zip to send around.
const howto = path.join(outDir, 'JAK-URUCHOMIC.txt');
writeFileSync(howto, readFileSync(path.join(ROOT, 'scripts', 'JAK-URUCHOMIC.txt'), 'utf8').replace(/\n/g, target.startsWith('win') ? '\r\n' : '\n'));
const zip = path.join(DIST, `srt-translator-${target}.zip`);
rmSync(zip, { force: true });
execFileSync('zip', ['-j', '-q', '-9', zip, output, howto]);
log(`zip: ${path.relative(ROOT, zip)} (${(statSync(zip).size / 2 ** 20).toFixed(1)} MB)`);
+316
View File
@@ -0,0 +1,316 @@
#!/usr/bin/env node
// SRT Translator — local web app. Zero-dependency HTTP server (+ optional @lmstudio/sdk).
import http from 'node:http';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { spawn } from 'node:child_process';
import { isSea, getAsset } from 'node:sea';
import { parseSrt, serializeSrt, decodeSubtitleBuffer, songKind, stripSongs } from './src/srt.js';
import { detectLanguage } from './src/langdetect.js';
import { LANGUAGES } from './src/languages.js';
import { PROFILES, detectProfile } from './src/prompts.js';
import { getEngine, ENGINE_DEFAULTS } from './src/engines/index.js';
import { estimateMemory, maxContextFor, systemMemory, archParams, KV_TYPES } from './src/vram.js';
import { TranslationJob } from './src/translator.js';
import { PullJob } from './src/pulls.js';
// In the bundled single executable (CommonJS) import.meta is empty and the UI comes from SEA assets.
const SEA = isSea();
const __dirname = typeof import.meta?.url === 'string' ? path.dirname(fileURLToPath(import.meta.url)) : process.cwd();
const PUBLIC = path.join(__dirname, 'public');
const PORT = Number(process.env.PORT || 8765);
const HOST = process.env.HOST || '127.0.0.1';
const MAX_BODY = 25 * 2 ** 20;
const jobs = new Map();
const pulls = new Map();
const MIME = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.svg': 'image/svg+xml', '.ico': 'image/x-icon' };
function send(res, status, data, headers = {}) {
const body = typeof data === 'string' || Buffer.isBuffer(data) ? data : JSON.stringify(data);
res.writeHead(status, { 'content-type': typeof data === 'string' || Buffer.isBuffer(data) ? 'text/plain; charset=utf-8' : 'application/json; charset=utf-8', 'cache-control': 'no-store', ...headers });
res.end(body);
}
async function readBody(req) {
const chunks = [];
let size = 0;
for await (const c of req) {
size += c.length;
if (size > MAX_BODY) throw Object.assign(new Error('Za duże żądanie'), { status: 413 });
chunks.push(c);
}
return Buffer.concat(chunks);
}
async function readJson(req) {
const b = await readBody(req);
if (!b.length) return {};
try { return JSON.parse(b.toString('utf8')); } catch { throw Object.assign(new Error('Niepoprawny JSON'), { status: 400 }); }
}
// ---------------------------------------------------------------------------
const routes = {
'GET /api/meta': async () => ({
languages: LANGUAGES,
profiles: Object.fromEntries(Object.entries(PROFILES).map(([k, p]) => [k, { label: p.label, modes: p.modes, defaultMode: p.defaultMode, needsSource: !!p.needsSource, recommended: p.recommended || null, note: p.note || null }])),
engines: ENGINE_DEFAULTS,
kvTypes: Object.keys(KV_TYPES).filter((k) => k !== 'bf16'),
system: await systemMemory(),
}),
'GET /api/system': async () => systemMemory(),
'POST /api/srt/parse': async (req, url) => {
const buf = await readBody(req);
const { text, encoding } = decodeSubtitleBuffer(buf, url.searchParams.get('encoding') || 'auto');
const { cues, eol } = parseSrt(text);
if (!cues.length) throw Object.assign(new Error('Nie znaleziono napisów — czy to na pewno plik SRT?'), { status: 400 });
const all = cues.map((c) => c.text).join('\n');
for (const c of cues) c.song = songKind(c.text);
return {
encoding, eol: eol === '\r\n' ? 'crlf' : 'lf', cues,
detectedLanguage: detectLanguage(all),
stats: { cues: cues.length, chars: all.length, durationMs: cues[cues.length - 1].endMs },
};
},
'POST /api/srt/build': async (req) => {
const { cues, translations = {}, eol = 'lf', format = 'translated', bom = false, songs = 'translate' } = await readJson(req);
const remove = songs === 'remove';
const getText = (c) => {
let src = c.text, t = translations[c.id];
if (remove) { src = stripSongs(src); t = t ? stripSongs(t) : t; }
if (format === 'bilingual') return t ? (src ? `${src}\n${t}` : t) : src;
return t || src;
};
// Removing songs drops whole cues (lyrics only) and lyric lines from mixed cues; the
// remaining cues keep their exact timings and are renumbered.
const kept = remove ? cues.filter((c) => songKind(c.text) !== 'full' && getText(c).trim()) : cues;
const srt = serializeSrt(kept, { eol: eol === 'crlf' ? '\r\n' : '\n', getText });
return { srt: (bom ? '\uFEFF' : '') + srt, cues: kept.length, removed: cues.length - kept.length };
},
'POST /api/engine/status': async (req) => {
const { engine } = await readJson(req);
const e = getEngine(engine);
const st = await e.status();
return { ...st, capabilities: e.capabilities() };
},
'POST /api/engine/models': async (req) => {
const { engine } = await readJson(req);
const models = await getEngine(engine).listModels();
return { models: models.map((m) => ({ ...m, profile: detectProfile(m.id) })) };
},
'POST /api/engine/model-info': async (req) => {
const { engine, model } = await readJson(req);
const info = await getEngine(engine).modelInfo(model);
const p = info.meta ? archParams(info.meta) : null;
return {
...info,
meta: undefined,
hasMeta: !!info.meta,
arch: p ? { name: p.arch, layers: p.nLayer, headsKv: p.nHeadKv, keyLen: p.keyLen, valLen: p.valLen, slidingWindow: p.slidingWindow, sharedKvLayers: p.sharedKvLayers, fullAttnInterval: p.fullAttnInterval, vocab: p.vocab, kvLoraRank: p.kvLoraRank } : null,
profile: detectProfile(model),
};
},
'POST /api/engine/estimate': async (req) => {
const { engine, model, settings = {}, sizeBytes } = await readJson(req);
const e = getEngine(engine);
const sys = await systemMemory();
let info = null;
try { info = await e.modelInfo(model); } catch { /* estimate without metadata */ }
const opts = {
modelBytes: Number(sizeBytes) || 0,
nCtx: Number(settings.contextLength) || 4096,
kType: settings.kvCacheK || 'f16',
vType: settings.kvCacheV || 'f16',
flashAttention: settings.flashAttention !== false,
nUbatch: 512,
nSeq: 1,
gpuRatio: settings.gpuRatio === '' || settings.gpuRatio === undefined ? 1 : Number(settings.gpuRatio),
};
const own = estimateMemory(info?.meta, opts);
const budget = sys.gpus[0]?.total ? sys.gpus[0].total - (sys.gpus[0].unified ? 0 : sys.gpus[0].used || 0) : null;
const fit = budget && info?.meta ? maxContextFor(info.meta, { ...opts, maxCtx: info.maxContext || 131072 }, budget * 0.95) : null;
let engineEst = null, engineErr = null;
if (e.estimate) {
try { engineEst = await e.estimate(model, settings); } catch (err) { engineErr = err.message; }
}
return { own, engine: engineEst, engineError: engineErr, system: sys, budget, maxContextFit: fit, maxContext: info?.maxContext || null };
},
'POST /api/engine/loaded': async (req) => {
const { engine } = await readJson(req);
return { loaded: await getEngine(engine).loaded() };
},
'POST /api/engine/load': async (req) => {
const { engine, model, settings } = await readJson(req);
const e = getEngine(engine);
if (!e.load) throw Object.assign(new Error('Ten silnik nie obsługuje ładowania modeli z poziomu aplikacji.'), { status: 400 });
return e.load(model, settings);
},
'POST /api/engine/unload': async (req) => {
const { engine, instanceId } = await readJson(req);
const e = getEngine(engine);
if (!e.unload) throw Object.assign(new Error('Ten silnik nie obsługuje zwalniania modeli.'), { status: 400 });
return e.unload(instanceId);
},
'POST /api/pulls': async (req) => {
const { engine, model, quantization } = await readJson(req);
const name = String(model || '').trim();
if (!name) throw Object.assign(new Error('Podaj nazwę modelu.'), { status: 400 });
const e = getEngine(engine);
if (!e.pull) throw Object.assign(new Error('Ten silnik nie obsługuje pobierania modeli z aplikacji.'), { status: 400 });
for (const [id, p] of pulls) if ((p.isTerminal() || p.state === 'error') && Date.now() - p.finishedAt > 3600e3) pulls.delete(id);
// The same model already has a download → continue that one (one tile in the UI), don't start a second.
const existing = [...pulls.values()].find((p) => !p.isTerminal() && p.engine === e && p.model === name);
if (existing) {
existing.resume();
return existing.snapshot();
}
const job = new PullJob({ engine: e, engineKind: e.kind, model: name, quantization });
pulls.set(job.id, job);
job.start();
return job.snapshot();
},
'GET /api/pulls': async () => ({ pulls: [...pulls.values()].map((p) => p.snapshot()) }),
'POST /api/jobs': async (req) => {
const { engine, model, settings, options, cues, existing, onlyIds } = await readJson(req);
if (!model) throw Object.assign(new Error('Wybierz model.'), { status: 400 });
if (!Array.isArray(cues) || !cues.length) throw Object.assign(new Error('Brak napisów.'), { status: 400 });
for (const [id, j] of jobs) if (j.state !== 'running' && Date.now() - (j.stats.finishedAt || 0) > 3600e3) jobs.delete(id);
const job = new TranslationJob({ cues, engine: getEngine(engine), model, settings, options, existing, onlyIds });
jobs.set(job.id, job);
job.run();
return { id: job.id, profile: job.profile, mode: job.mode };
},
};
function pullRoute(req, res, url) {
const m = /^\/api\/pulls\/([\w-]+)(\/events|\/cancel|\/pause|\/resume)$/.exec(url.pathname);
if (!m) return false;
const job = pulls.get(m[1]);
if (!job) { send(res, 404, { error: 'Nie ma takiego pobierania' }); return true; }
if (req.method === 'POST' && ['/cancel', '/pause', '/resume'].includes(m[2])) {
const action = { '/cancel': () => job.cancel(), '/pause': () => job.pause(), '/resume': () => job.resume() }[m[2]];
Promise.resolve(action()).then(() => send(res, 200, job.snapshot()), (e) => send(res, 500, { error: e.message }));
return true;
}
if (m[2] === '/events' && req.method === 'GET') {
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' });
const ev = (type, data) => res.write(`event: ${type}\ndata: ${JSON.stringify(data)}\n\n`);
ev('state', job.snapshot());
if (job.isTerminal()) { res.end(); return true; }
const onProgress = (s) => ev('progress', s);
const onState = (s) => ev('state', s);
const ping = setInterval(() => res.write(': ping\n\n'), 15000);
job.on('progress', onProgress); job.on('state', onState);
req.on('close', () => { clearInterval(ping); job.off('progress', onProgress); job.off('state', onState); });
return true;
}
return false;
}
function jobRoute(req, res, url) {
const m = /^\/api\/jobs\/([\w-]+)(\/events|\/cancel)?$/.exec(url.pathname);
if (!m) return false;
const job = jobs.get(m[1]);
if (!job) { send(res, 404, { error: 'Nie ma takiego zadania' }); return true; }
if (!m[2] && req.method === 'GET') { send(res, 200, job.snapshot()); return true; }
if (m[2] === '/cancel' && req.method === 'POST') { job.cancel(); send(res, 200, { ok: true }); return true; }
if (m[2] === '/events' && req.method === 'GET') {
res.writeHead(200, { 'content-type': 'text/event-stream', 'cache-control': 'no-store', connection: 'keep-alive' });
const ev = (type, data) => res.write(`event: ${type}\ndata: ${JSON.stringify(data)}\n\n`);
ev('snapshot', job.snapshot());
// Cue updates are coalesced so long files don't flood the browser.
let pendingCues = [];
const flush = () => { if (pendingCues.length) { ev('cues', pendingCues); pendingCues = []; } };
const timer = setInterval(flush, 250);
const ping = setInterval(() => res.write(': ping\n\n'), 15000);
const onCue = (c) => pendingCues.push(c);
const onLog = (l) => ev('log', l);
const onProgress = (s) => ev('progress', s);
const onState = (s) => { flush(); ev('state', { state: s, error: job.error || null, stats: job.stats }); };
job.on('cue', onCue); job.on('log', onLog); job.on('progress', onProgress); job.on('state', onState);
req.on('close', () => {
clearInterval(timer); clearInterval(ping);
job.off('cue', onCue); job.off('log', onLog); job.off('progress', onProgress); job.off('state', onState);
});
return true;
}
return false;
}
async function serveStatic(res, pathname) {
const rel = pathname === '/' ? 'index.html' : pathname.replace(/^\/+/, '');
if (SEA) {
try {
const data = Buffer.from(getAsset(rel));
res.writeHead(200, { 'content-type': MIME[path.extname(rel)] || 'application/octet-stream', 'cache-control': 'no-cache' });
return res.end(data);
} catch {
return send(res, 404, 'Not found');
}
}
const file = path.normalize(path.join(PUBLIC, rel));
if (!file.startsWith(PUBLIC)) return send(res, 403, 'Forbidden');
try {
const data = await readFile(file);
res.writeHead(200, { 'content-type': MIME[path.extname(file)] || 'application/octet-stream', 'cache-control': 'no-cache' });
res.end(data);
} catch {
send(res, 404, 'Not found');
}
}
const server = http.createServer(async (req, res) => {
const url = new URL(req.url, `http://${req.headers.host || 'localhost'}`);
try {
if (url.pathname.startsWith('/api/jobs/') && jobRoute(req, res, url)) return;
if (url.pathname.startsWith('/api/pulls/') && pullRoute(req, res, url)) return;
const handler = routes[`${req.method} ${url.pathname}`];
if (handler) return send(res, 200, await handler(req, url));
if (req.method === 'GET' && !url.pathname.startsWith('/api/')) return serveStatic(res, url.pathname);
send(res, 404, { error: 'Not found' });
} catch (e) {
send(res, e.status && e.status < 500 ? e.status : 502, { error: e.message });
}
});
function openBrowser(url) {
const [cmd, args] = process.platform === 'win32' ? ['cmd', ['/c', 'start', '', url]]
: process.platform === 'darwin' ? ['open', [url]] : ['xdg-open', [url]];
try { spawn(cmd, args, { detached: true, stdio: 'ignore', windowsHide: true }).unref(); } catch { /* user can open it manually */ }
}
// If the port is taken (e.g. the app is already running), try the next few.
let port = PORT;
server.on('error', (e) => {
if (e.code === 'EADDRINUSE' && port < PORT + 10) {
port++;
server.listen(port, HOST);
} else {
console.error(`Nie udalo sie uruchomic serwera: ${e.message}`);
process.exit(1);
}
});
server.on('listening', () => {
const url = `http://${HOST === '0.0.0.0' ? 'localhost' : HOST}:${port}`;
// Plain ASCII: the classic Windows console mangles Polish letters.
console.log(`SRT Translator dziala: ${url}`);
if (SEA) console.log('Nie zamykaj tego okna - to serwer aplikacji. Zamkniecie okna wylacza aplikacje.');
if ((SEA || process.env.OPEN_BROWSER === '1') && process.env.NO_BROWSER !== '1') openBrowser(url);
});
server.listen(port, HOST);
+48
View File
@@ -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);
}
}
+28
View File
@@ -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;
}
+336
View File
@@ -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));
}
+240
View File
@@ -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}`;
}
+96
View File
@@ -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
View File
@@ -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();
}
}
+54
View File
@@ -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;
}
+54
View File
@@ -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
View File
@@ -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
View File
@@ -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
View File
@@ -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'));
}
+519
View File
@@ -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
View File
@@ -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;
}
+255
View File
@@ -0,0 +1,255 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parseSrt, serializeSrt, decodeSubtitleBuffer, protectFormatting, rewrap, hasTranslatableText, cleanModelText, songKind, stripSongs } from '../src/srt.js';
import { parseNumberedLines, jsonSchemaFor, pickJsonValue, detectProfile, mtMessages } from '../src/prompts.js';
import { detectLanguage } from '../src/langdetect.js';
import { estimateMemory, parseWindowsRegistryGpus } from '../src/vram.js';
import { normalizeLmsModelRef } from '../src/engines/lmstudio.js';
import { explainPullError } from '../src/engines/ollama.js';
import { PullJob } from '../src/pulls.js';
import { looksLikeReasoning, repairMerges } from '../src/translator.js';
test('parseSrt keeps timing lines verbatim and handles messy input', () => {
const src = '1\r\n00:00:01,000 --> 00:00:02,500\r\nHello\r\nthere\r\n\r\n\r\n2\r\n00:00:03,000 --> 00:00:04,000 X1:10 X2:20\r\n<i>Bye</i>\r\n3\r\n00:00:05.5 --> 00:00:06,000\r\n42\r\n';
const { cues, eol } = parseSrt(src);
assert.equal(eol, '\r\n');
assert.equal(cues.length, 3);
assert.equal(cues[0].text, 'Hello\nthere');
assert.equal(cues[1].timing, '00:00:03,000 --> 00:00:04,000 X1:10 X2:20');
// index "3" glued to cue 2 without a blank line is recognised as the next index
assert.equal(cues[1].text, '<i>Bye</i>');
assert.equal(cues[2].text, '42'); // numeric dialogue line is kept
assert.equal(cues[2].startMs, 5500);
});
test('serializeSrt renumbers and round-trips timings', () => {
const { cues } = parseSrt('5\n00:00:01,000 --> 00:00:02,000\nA\n\n9\n00:00:02,000 --> 00:00:03,000\nB\n');
const out = serializeSrt(cues, { getText: (c) => c.text.toLowerCase() });
assert.equal(out, '1\n00:00:01,000 --> 00:00:02,000\na\n\n2\n00:00:02,000 --> 00:00:03,000\nb\n');
});
test('decodeSubtitleBuffer detects windows-1250 Polish', () => {
const text = 'Nie wiem, czy już tu był.\nMożesz mi pomóc? Żółć gęślą jaźń.\nTo się nie uda, że też musiałeś.';
// encode to cp1250 manually for the Polish letters used
const map = { 'ą': 0xb9, 'ć': 0xe6, 'ę': 0xea, 'ł': 0xb3, 'ń': 0xf1, 'ó': 0xf3, 'ś': 0x9c, 'ź': 0x9f, 'ż': 0xbf, 'Ż': 0xaf };
const bytes = Buffer.from([...text].map((ch) => map[ch] ?? ch.charCodeAt(0)));
const r = decodeSubtitleBuffer(bytes);
assert.equal(r.encoding, 'windows-1250');
assert.equal(r.text, text);
assert.equal(decodeSubtitleBuffer(Buffer.from('zażółć', 'utf8')).encoding, 'utf-8');
});
test('protectFormatting strips and restores wrapping tags', () => {
let p = protectFormatting('{\\an8}<i>Hello\nworld</i>');
assert.equal(p.text, 'Hello\nworld');
assert.equal(p.restore('Cześć\nświecie'), '{\\an8}<i>Cześć\nświecie</i>');
p = protectFormatting('<i>One</i>\n<i>Two</i>');
assert.equal(p.text, 'One\nTwo');
assert.equal(p.restore('Jeden\nDwa'), '<i>Jeden</i>\n<i>Dwa</i>');
p = protectFormatting('Say <i>this</i> now');
assert.equal(p.text, 'Say <i>this</i> now');
assert.equal(hasTranslatableText('♪ ♪'), false);
assert.equal(hasTranslatableText('<i>♪ la la ♪</i>'), true);
});
test('rewrap balances long lines and keeps dialogue', () => {
assert.equal(rewrap('Krótki tekst.', 42), 'Krótki tekst.');
const r = rewrap('Słuchaj, nieważne co myślisz, że się stało, to nie jest tak, jak wygląda.', 42);
const lines = r.split('\n');
assert.equal(lines.length, 2);
assert.ok(lines.every((l) => l.length <= 42), r);
assert.equal(rewrap('- Nie wierzysz mi, naprawdę, nawet odrobinę?\n- A powinienem?', 30), '- Nie wierzysz mi, naprawdę, nawet odrobinę?\n- A powinienem?');
// no one-letter word left at a line end
for (const t of ['Detektyw Morgan chce spotkać się z Tobą w swoim gabinecie. Prosimy.', 'Detektyw Morgan chce spotkać się z Tobą w swoim gabinecie. Prosimy, jak najszybciej.']) {
const s = rewrap(t, 42);
assert.ok(!/(^|\s)\p{L}$/u.test(s.split('\n')[0]), s);
}
});
test('cleanModelText removes think blocks and fences', () => {
assert.equal(cleanModelText('<think>hmm</think>\n```json\n{"1":"a"}\n```'), '{"1":"a"}');
});
test('parseNumberedLines takes the final answer block, ignores drafts', () => {
const t = 'The user wants...\n1. Produce natural Polish.\n[1] Where? -> Gdzie?\n[2] Told -> Mówiłem\nFinal check.[1] Gdzie byłaś?\n[2] Mówiłam ci, <br> że u siostry.\n[3] Nigdy.';
assert.deepEqual([...parseNumberedLines(t, [1, 2, 3])], [[1, 'Gdzie byłaś?'], [2, 'Mówiłam ci,\nże u siostry.'], [3, 'Nigdy.']]);
assert.deepEqual([...parseNumberedLines('[1] a\n[3] c', [1, 2, 3])], [[1, 'a'], [3, 'c']]);
});
test('jsonSchemaFor anchors keys with the start of each source line', () => {
const s = jsonSchemaFor([{ id: 4, text: '<i>The partner\'s office was on the twelfth floor</i>' }, { id: 7, text: '"Quoted" \\ text' }]);
assert.deepEqual(s.required, ["4|The partner's office…", '7|Quoted text']);
assert.equal(s.additionalProperties, false);
assert.equal(pickJsonValue({ '4|The partner': 'x' }, { id: 4, text: 'The partner left' }), 'x');
assert.equal(pickJsonValue({ 4: 'y' }, { id: 4, text: 'Anything' }), 'y');
});
test('profiles and MT templates', () => {
assert.equal(detectProfile('translategemma:12b'), 'translategemma');
assert.equal(detectProfile('hf.co/tencent/HY-MT1.5-7B-GGUF'), 'hunyuan');
assert.equal(detectProfile('qwen3:8b'), 'generic');
const [m] = mtMessages('translategemma', { text: 'Hello', source: 'en', target: 'pl' });
assert.match(m.content, /^You are a professional English \(en\) to Polish \(pl\) translator\./);
assert.match(m.content, /Polish:\n\n\nHello$/);
});
test('detectLanguage', () => {
assert.equal(detectLanguage("Where were you last night? I told you, I was at my sister's place. You don't know what it is."), 'en');
assert.equal(detectLanguage('Nie wiem, co się stało. To jest tylko żart, ale już nie mogę.'), 'pl');
assert.equal(detectLanguage('これは日本語のテキストです。'), 'ja');
});
test('repairMerges splits a sentence merged into one entry', () => {
const batch = [
{ id: 17, text: "The partner's office was on the twelfth floor" },
{ id: 18, text: 'of a building that had seen better days.' },
{ id: 19, text: 'Mr. Callahan?' },
];
const map = new Map([[17, 'Biuro partnera było na dwunastym piętrze budynku, który widział lepsze czasy.'], [18, ''], [19, 'Panie Callahan?']]);
assert.deepEqual(repairMerges(batch, map), [[17, 18]]);
assert.equal(map.get(17), 'Biuro partnera było na dwunastym piętrze');
assert.equal(map.get(18), 'budynku, który widział lepsze czasy.');
// A legitimately short translation next to an empty one is not "repaired".
const m2 = new Map([[1, 'Tak.'], [2, '']]);
assert.deepEqual(repairMerges([{ id: 1, text: 'Yes.' }, { id: 2, text: 'Something else entirely here.' }], m2), []);
});
test('looksLikeReasoning', () => {
assert.equal(looksLikeReasoning('The user wants me to translate...', 'Hi'), true);
assert.equal(looksLikeReasoning('Cześć, jak się masz?', 'Hi, how are you?'), false);
});
const GEMMA4 = {
'general.architecture': 'gemma4', 'gemma4.block_count': 42, 'gemma4.embedding_length': 2560,
'gemma4.attention.head_count': 8, 'gemma4.attention.head_count_kv': 2, 'gemma4.attention.key_length': 512,
'gemma4.attention.value_length': 512, 'gemma4.attention.key_length_swa': 256, 'gemma4.attention.value_length_swa': 256,
'gemma4.attention.sliding_window': 512, 'gemma4.attention.shared_kv_layers': 18,
'gemma4.attention.sliding_window_pattern': Array.from({ length: 42 }, (_, i) => i % 6 !== 5),
'tokenizer.ggml.tokens': { length: 262144 },
};
test('estimateMemory: SWA, shared KV, KV quantization', () => {
const f16 = estimateMemory(GEMMA4, { modelBytes: 5e9, nCtx: 32768, kType: 'f16', vType: 'f16', flashAttention: true });
assert.deepEqual(f16.layers, { full: 4, swa: 20, recurrent: 0, shared: 18 });
// 4 global layers × 32768 × 2 heads × (512+512) × 2 B + 20 SWA layers × 1024 × 2 × 512 × 2 B
assert.equal(f16.kv, 4 * 32768 * 2 * 1024 * 2 + 20 * 1024 * 2 * 512 * 2);
const q8 = estimateMemory(GEMMA4, { modelBytes: 5e9, nCtx: 32768, kType: 'q8_0', vType: 'q8_0', flashAttention: true });
assert.ok(Math.abs(q8.kv / f16.kv - 34 / 64) < 1e-9);
const hybrid = estimateMemory({
'general.architecture': 'qwen35', 'qwen35.block_count': 32, 'qwen35.embedding_length': 4096, 'qwen35.attention.head_count': 16,
'qwen35.attention.head_count_kv': 4, 'qwen35.attention.key_length': 256, 'qwen35.attention.value_length': 256,
'qwen35.full_attention_interval': 4, 'qwen35.ssm.inner_size': 4096, 'qwen35.ssm.state_size': 128, 'qwen35.ssm.conv_kernel': 4, 'qwen35.ssm.group_count': 16,
}, { modelBytes: 5e9, nCtx: 8192 });
assert.equal(hybrid.layers.full, 8);
assert.equal(hybrid.layers.recurrent, 24);
assert.equal(hybrid.kv, 8 * 8192 * 4 * 512 * 2);
});
test('song detection and removal', () => {
assert.equal(songKind('♪ ♪'), 'full');
assert.equal(songKind("<i>♪ I've been waiting\nall my life\nfor you ♪</i>"), 'full');
assert.equal(songKind('# Jingle bells #'), 'full');
assert.equal(songKind('- ♪ Happy birthday ♪\n- Stop singing!'), 'partial');
assert.equal(songKind('[music playing]'), null);
assert.equal(songKind('I paid #1 price'), null);
// An unclosed note doesn't swallow the following dialogue line.
assert.equal(stripSongs('♪ Happy birthday\n- Thanks, guys!'), 'Thanks, guys!');
assert.equal(stripSongs('- ♪ La la ♪\n- Stop!'), 'Stop!');
assert.equal(stripSongs('<i>- ♪ La la ♪\n- Stop!</i>'), '<i>Stop!</i>');
assert.equal(stripSongs('♪ only lyrics ♪'), '');
assert.equal(stripSongs('Plain line'), 'Plain line');
});
test('parseWindowsRegistryGpus reads VRAM and names from reg query output', () => {
const qw = [
'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}\\0000',
' HardwareInformation.qwMemorySize REG_QWORD 0x400000000',
'',
'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}\\0001',
' HardwareInformation.qwMemorySize REG_QWORD 0x8000000',
'',
'End of search: 2 match(es) found.',
].join('\r\n');
const desc = [
'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}\\0000',
' DriverDesc REG_SZ AMD Radeon RX 7800 XT',
'',
'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Class\\{4d36e968-e325-11ce-bfc1-08002be10318}\\0001',
' DriverDesc REG_SZ Intel(R) UHD Graphics',
].join('\r\n');
assert.deepEqual(parseWindowsRegistryGpus(qw, desc), [{ name: 'AMD Radeon RX 7800 XT', total: 16 * 2 ** 30 }]);
});
test('normalizeLmsModelRef accepts the usual ways of naming a model', () => {
assert.deepEqual(normalizeLmsModelRef('google/gemma-3-12b'), { model: 'google/gemma-3-12b', quantization: undefined });
assert.deepEqual(normalizeLmsModelRef('hf.co/speakleash/Bielik-11B-v3.0-Instruct-GGUF:Q4_K_M'), { model: 'https://huggingface.co/speakleash/Bielik-11B-v3.0-Instruct-GGUF', quantization: 'Q4_K_M' });
assert.deepEqual(normalizeLmsModelRef('bartowski/EuroLLM-9B-Instruct-GGUF', 'Q5_K_M'), { model: 'https://huggingface.co/bartowski/EuroLLM-9B-Instruct-GGUF', quantization: 'Q5_K_M' });
assert.deepEqual(normalizeLmsModelRef('https://huggingface.co/tencent/Hy-MT2-7B-GGUF/'), { model: 'https://huggingface.co/tencent/Hy-MT2-7B-GGUF', quantization: undefined });
// quantization only applies to Hugging Face links
assert.deepEqual(normalizeLmsModelRef('qwen/qwen3-8b', 'Q4_K_M'), { model: 'qwen/qwen3-8b', quantization: undefined });
});
function fakePullEngine(steps = 10, delay = 60) {
return {
kind: 'ollama',
capabilities: () => ({}),
calls: [],
async pull(model, { onProgress, signal }) {
// Resumes from wherever the previous attempt stopped, like Ollama does.
let i = this.resumeAt || 0;
this.calls.push(i);
for (; i <= steps; i++) {
if (signal.aborted) { this.resumeAt = i; throw new Error('aborted'); }
onProgress({ phase: 'downloading', total: steps * 100, completed: i * 100, digests: ['sha256:' + 'a'.repeat(64)] });
await new Promise((r) => setTimeout(r, delay));
}
return { ok: true };
},
};
}
test('PullJob: one job through pause → resume → done, with speed and ETA', async () => {
const eng = fakePullEngine(10, 150);
const job = new PullJob({ engine: eng, engineKind: 'ollama', model: 'x' });
const seen = [];
job.on('progress', (s) => seen.push(s.progress));
job.start();
await new Promise((r) => setTimeout(r, 250));
assert.equal(await job.pause(), true);
assert.equal(job.state, 'paused');
assert.match(job.note, /Wznów/);
const pausedAt = job.progress.completed;
assert.equal(job.resume(), true);
await job.runPromise;
assert.equal(job.state, 'done');
assert.ok(eng.calls[1] > 0, 'second attempt resumed, not restarted');
assert.ok(job.progress.completed >= pausedAt);
assert.ok(seen.some((p) => p.bytesPerSec > 0 && p.etaSec != null), 'speed/ETA computed');
});
test('PullJob: cancel deletes only this download\'s partial files', async () => {
const { mkdtemp, writeFile, readdir } = await import('node:fs/promises');
const os = await import('node:os');
const path = await import('node:path');
const { OllamaEngine } = await import('../src/engines/ollama.js');
const dir = await mkdtemp(path.join(os.tmpdir(), 'blobs-'));
const mine = 'a'.repeat(64), other = 'b'.repeat(64);
for (const f of [`sha256-${mine}-partial`, `sha256-${mine}-partial-0`, `sha256-${mine}-partial-1`, `sha256-${other}-partial`, `sha256-${mine}`, `sha256-${other}`]) await writeFile(path.join(dir, f), 'xxxx');
const ollama = new OllamaEngine({});
const eng = { ...fakePullEngine(50, 30), discardPartial: (d) => ollama.discardPartial(d, dir) };
const job = new PullJob({ engine: eng, engineKind: 'ollama', model: 'x' });
job.start();
await new Promise((r) => setTimeout(r, 150));
assert.equal(await job.cancel(), true);
assert.equal(job.state, 'cancelled');
assert.match(job.note, /usunięto/);
// the finished blob with the same digest and another download's partial stay untouched
assert.deepEqual((await readdir(dir)).sort(), [`sha256-${mine}`, `sha256-${other}`, `sha256-${other}-partial`].sort());
assert.equal(job.resume(), false, 'a cancelled download cannot be resumed');
});
test('explainPullError turns Ollama pull errors into hints', () => {
assert.match(explainPullError('pull model manifest: file does not exist', 'foo:1b'), /bibliotece Ollamy/);
assert.match(explainPullError('pull model manifest: realm host "huggingface.co" does not match original host "hf.co"', 'hf.co/x/y-GGUF:Q4_K_M'), /Hugging Face/);
assert.equal(explainPullError('something else', 'foo'), 'Ollama: something else');
});