iobroker.assistant 1.5.0 → 1.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -0
- package/admin/custom/.vite/manifest.json +18 -18
- package/admin/custom/assets/Components-DGifJGYx.js +2 -0
- package/admin/custom/assets/{_virtual_mf-localSharedImportMap___mfe_internal__ConfigCustomAssistant__mf_owner__1-fxARoTRS.js → _virtual_mf-localSharedImportMap___mfe_internal__ConfigCustomAssistant__mf_owner__1-Ckpr0MHE.js} +1 -1
- package/admin/custom/assets/{_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__212234797318001__loadShare___mf_0_iobroker_mf_1_gui_mf_2_components__loadShare__.js-CRG5UC4m.js → _virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__212234797318001__loadShare___mf_0_iobroker_mf_1_gui_mf_2_components__loadShare__.js-BanHP4Nt.js} +64 -64
- package/admin/custom/assets/{_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__212234797318001__loadShare__shared-CpHul7u9.js → _virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__212234797318001__loadShare__shared-BnAYSFON.js} +1 -1
- package/admin/custom/assets/_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__212234797318001__prebuild___mf_0_iobroker_mf_1_json_mf_2_config__prebuild__-JBzVK-6R.js +122 -0
- package/admin/custom/assets/{hostInit-y7sTzQxx.js → hostInit-6JlKIkio.js} +1 -1
- package/admin/custom/assets/{lib-DSjTTMgF.js → lib-HGRsyW2B.js} +1 -1
- package/admin/custom/assets/{mf-entry-bootstrap-0-b04cc744.js → mf-entry-bootstrap-0-95048639.js} +2 -2
- package/admin/custom/assets/{pendingShares-DX6xYXgn.js → pendingShares-BGhHdsEv.js} +1 -1
- package/admin/custom/customComponents.js +2 -2
- package/admin/custom/i18n/de.json +57 -1
- package/admin/custom/i18n/en.json +57 -1
- package/admin/custom/i18n/es.json +57 -1
- package/admin/custom/i18n/fr.json +57 -1
- package/admin/custom/i18n/it.json +57 -1
- package/admin/custom/i18n/nl.json +57 -1
- package/admin/custom/i18n/pl.json +57 -1
- package/admin/custom/i18n/pt.json +57 -1
- package/admin/custom/i18n/ru.json +57 -1
- package/admin/custom/i18n/uk.json +57 -1
- package/admin/custom/i18n/zh-cn.json +57 -1
- package/admin/custom/index.html +2 -2
- package/admin/custom/mf-manifest.json +1 -1
- package/admin/custom/mf-stats.json +1 -1
- package/admin/i18n/de.json +23 -3
- package/admin/i18n/en.json +23 -3
- package/admin/i18n/es.json +23 -3
- package/admin/i18n/fr.json +23 -3
- package/admin/i18n/it.json +23 -3
- package/admin/i18n/nl.json +23 -3
- package/admin/i18n/pl.json +23 -3
- package/admin/i18n/pt.json +23 -3
- package/admin/i18n/ru.json +23 -3
- package/admin/i18n/uk.json +23 -3
- package/admin/i18n/zh-cn.json +23 -3
- package/admin/jsonConfig.json +796 -618
- package/build/lib/llm.js +267 -4
- package/build/lib/llm.js.map +1 -1
- package/build/lib/llmBenchmark.js +139 -0
- package/build/lib/llmBenchmark.js.map +1 -0
- package/build/lib/llmModels.js +678 -0
- package/build/lib/llmModels.js.map +1 -0
- package/build/lib/localLlm.js +56 -37
- package/build/lib/localLlm.js.map +1 -1
- package/build/lib/piAgent.js +33 -0
- package/build/lib/piAgent.js.map +1 -1
- package/build/lib/systemAi.js +17 -2
- package/build/lib/systemAi.js.map +1 -1
- package/build/lib/voice/aws.js +9 -2
- package/build/lib/voice/aws.js.map +1 -1
- package/build/lib/voice/azure.js +6 -2
- package/build/lib/voice/azure.js.map +1 -1
- package/build/lib/voice/engines.js +34 -2
- package/build/lib/voice/engines.js.map +1 -1
- package/build/lib/voice/openrouter.js +5 -3
- package/build/lib/voice/openrouter.js.map +1 -1
- package/build/lib/voice/piper.js +6 -1
- package/build/lib/voice/piper.js.map +1 -1
- package/build/lib/voice/speechLanguage.js +217 -0
- package/build/lib/voice/speechLanguage.js.map +1 -0
- package/build/main.js +283 -19
- package/build/main.js.map +1 -1
- package/io-package.json +30 -27
- package/package.json +1 -1
- package/admin/custom/assets/Components-BQL6oX_k.js +0 -2
- package/admin/custom/assets/_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__212234797318001__prebuild___mf_0_iobroker_mf_1_json_mf_2_config__prebuild__-2mXnqS7c.js +0 -122
|
@@ -0,0 +1,678 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.ModelManager = exports.DISK_RESERVE_BYTES = exports.DEFAULT_PRESET_ID = exports.MODEL_PRESETS = void 0;
|
|
37
|
+
exports.summarizeStats = summarizeStats;
|
|
38
|
+
exports.normalizeModelUrl = normalizeModelUrl;
|
|
39
|
+
exports.modelFileName = modelFileName;
|
|
40
|
+
exports.modelDisplayName = modelDisplayName;
|
|
41
|
+
exports.presetForFile = presetForFile;
|
|
42
|
+
exports.formatBytes = formatBytes;
|
|
43
|
+
const node_fs_1 = require("node:fs");
|
|
44
|
+
const path = __importStar(require("node:path"));
|
|
45
|
+
const node_stream_1 = require("node:stream");
|
|
46
|
+
const promises_1 = require("node:stream/promises");
|
|
47
|
+
const HF = 'https://huggingface.co';
|
|
48
|
+
/**
|
|
49
|
+
* Presets. URLs and sizes checked against Hugging Face on 2026-11-10; languages from the model cards.
|
|
50
|
+
* Qwen2.5-1.5B is the former default ({@link DEFAULT_PRESET_ID}).
|
|
51
|
+
*/
|
|
52
|
+
exports.MODEL_PRESETS = [
|
|
53
|
+
{
|
|
54
|
+
id: 'mnn-pocket-0.3b',
|
|
55
|
+
name: 'MNN-Pocket 0.3B (Q4_1)',
|
|
56
|
+
file: 'MNN-Pocket-0.3B-Instruct-Q4_1.gguf',
|
|
57
|
+
url: `${HF}/taobao-mnn/MNN-Pocket-0.3B-GGUF/resolve/main/MNN-Pocket-0.3B-Instruct-Q4_1.gguf`,
|
|
58
|
+
sizeBytes: 191_896_672,
|
|
59
|
+
params: '0.3B',
|
|
60
|
+
languages: ['en', 'zh'],
|
|
61
|
+
speed: 'very fast',
|
|
62
|
+
description: {
|
|
63
|
+
en: 'Smallest and fastest. Trained for English and Chinese only: German or Russian questions are understood badly.',
|
|
64
|
+
de: 'Am kleinsten und schnellsten. Nur auf Englisch und Chinesisch trainiert: deutsche oder russische Fragen versteht es schlecht.',
|
|
65
|
+
ru: 'Самая маленькая и быстрая. Обучена только на английском и китайском: немецкие и русские вопросы понимает плохо.',
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
id: 'qwen3-0.6b',
|
|
70
|
+
name: 'Qwen3 0.6B (Q4_K_M)',
|
|
71
|
+
file: 'Qwen3-0.6B-Q4_K_M.gguf',
|
|
72
|
+
url: `${HF}/unsloth/Qwen3-0.6B-GGUF/resolve/main/Qwen3-0.6B-Q4_K_M.gguf`,
|
|
73
|
+
sizeBytes: 396_705_472,
|
|
74
|
+
params: '0.6B',
|
|
75
|
+
languages: ['multi'],
|
|
76
|
+
speed: 'very fast',
|
|
77
|
+
thinking: true,
|
|
78
|
+
description: {
|
|
79
|
+
en: 'Very small, many languages including German and Russian. Its "thinking" is switched off for speed. Good for short facts, weak at anything longer.',
|
|
80
|
+
de: 'Sehr klein, viele Sprachen inklusive Deutsch und Russisch. Das „Nachdenken“ wird für Tempo abgeschaltet. Gut für kurze Fakten, schwach bei allem Längeren.',
|
|
81
|
+
ru: 'Очень маленькая, много языков, включая немецкий и русский. «Размышления» отключаются ради скорости. Хороша для коротких фактов, слаба во всём, что длиннее.',
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: 'qwen2.5-0.5b',
|
|
86
|
+
name: 'Qwen2.5 0.5B (Q4_K_M)',
|
|
87
|
+
file: 'qwen2.5-0.5b-instruct-q4_k_m.gguf',
|
|
88
|
+
url: `${HF}/Qwen/Qwen2.5-0.5B-Instruct-GGUF/resolve/main/qwen2.5-0.5b-instruct-q4_k_m.gguf`,
|
|
89
|
+
sizeBytes: 491_400_032,
|
|
90
|
+
params: '0.5B',
|
|
91
|
+
languages: ['multi'],
|
|
92
|
+
speed: 'very fast',
|
|
93
|
+
description: {
|
|
94
|
+
en: 'Very small, multilingual. Answers quickly but often wrong; hands device questions over reliably.',
|
|
95
|
+
de: 'Sehr klein, mehrsprachig. Antwortet schnell, aber oft falsch; gibt Gerätefragen zuverlässig ab.',
|
|
96
|
+
ru: 'Очень маленькая, многоязычная. Отвечает быстро, но часто ошибается; вопросы про устройства надёжно передаёт дальше.',
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
id: 'gemma3-1b',
|
|
101
|
+
name: 'Gemma 3 1B (Q4_K_M)',
|
|
102
|
+
file: 'google_gemma-3-1b-it-Q4_K_M.gguf',
|
|
103
|
+
url: `${HF}/bartowski/google_gemma-3-1b-it-GGUF/resolve/main/google_gemma-3-1b-it-Q4_K_M.gguf`,
|
|
104
|
+
sizeBytes: 806_058_496,
|
|
105
|
+
params: '1B',
|
|
106
|
+
languages: ['multi'],
|
|
107
|
+
speed: 'fast',
|
|
108
|
+
description: {
|
|
109
|
+
en: 'Google, small and multilingual. A good balance on a Raspberry Pi 5.',
|
|
110
|
+
de: 'Von Google, klein und mehrsprachig. Guter Kompromiss auf einem Raspberry Pi 5.',
|
|
111
|
+
ru: 'От Google, маленькая и многоязычная. Хороший компромисс для Raspberry Pi 5.',
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
id: 'llama3.2-1b',
|
|
116
|
+
name: 'Llama 3.2 1B (Q4_K_M)',
|
|
117
|
+
file: 'Llama-3.2-1B-Instruct-Q4_K_M.gguf',
|
|
118
|
+
url: `${HF}/bartowski/Llama-3.2-1B-Instruct-GGUF/resolve/main/Llama-3.2-1B-Instruct-Q4_K_M.gguf`,
|
|
119
|
+
sizeBytes: 807_694_464,
|
|
120
|
+
params: '1B',
|
|
121
|
+
languages: ['en', 'de', 'fr', 'it', 'pt', 'hi', 'es', 'th'],
|
|
122
|
+
speed: 'fast',
|
|
123
|
+
description: {
|
|
124
|
+
en: 'Meta. Officially English, German and six more languages, not Russian.',
|
|
125
|
+
de: 'Von Meta. Offiziell Englisch, Deutsch und sechs weitere Sprachen, kein Russisch.',
|
|
126
|
+
ru: 'От Meta. Официально английский, немецкий и ещё шесть языков, русского нет.',
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
id: 'qwen3-1.7b',
|
|
131
|
+
name: 'Qwen3 1.7B (Q4_K_M)',
|
|
132
|
+
file: 'Qwen3-1.7B-Q4_K_M.gguf',
|
|
133
|
+
url: `${HF}/unsloth/Qwen3-1.7B-GGUF/resolve/main/Qwen3-1.7B-Q4_K_M.gguf`,
|
|
134
|
+
sizeBytes: 1_107_409_472,
|
|
135
|
+
params: '1.7B',
|
|
136
|
+
languages: ['multi'],
|
|
137
|
+
speed: 'medium',
|
|
138
|
+
thinking: true,
|
|
139
|
+
description: {
|
|
140
|
+
en: 'Multilingual, noticeably better than the small ones. "Thinking" is switched off for speed.',
|
|
141
|
+
de: 'Mehrsprachig, merklich besser als die kleinen. Das „Nachdenken“ wird für Tempo abgeschaltet.',
|
|
142
|
+
ru: 'Многоязычная, заметно лучше маленьких. «Размышления» отключаются ради скорости.',
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
id: 'qwen2.5-1.5b',
|
|
147
|
+
name: 'Qwen2.5 1.5B (Q4_K_M)',
|
|
148
|
+
file: 'qwen2.5-1.5b-instruct-q4_k_m.gguf',
|
|
149
|
+
url: `${HF}/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf`,
|
|
150
|
+
sizeBytes: 1_117_320_736,
|
|
151
|
+
params: '1.5B',
|
|
152
|
+
languages: ['multi'],
|
|
153
|
+
speed: 'medium',
|
|
154
|
+
description: {
|
|
155
|
+
en: 'The previous default. Multilingual, solid for general questions.',
|
|
156
|
+
de: 'Der bisherige Standard. Mehrsprachig, solide bei allgemeinen Fragen.',
|
|
157
|
+
ru: 'Прежняя модель по умолчанию. Многоязычная, надёжна в общих вопросах.',
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
id: 'llama3.2-3b',
|
|
162
|
+
name: 'Llama 3.2 3B (Q4_K_M)',
|
|
163
|
+
file: 'Llama-3.2-3B-Instruct-Q4_K_M.gguf',
|
|
164
|
+
url: `${HF}/bartowski/Llama-3.2-3B-Instruct-GGUF/resolve/main/Llama-3.2-3B-Instruct-Q4_K_M.gguf`,
|
|
165
|
+
sizeBytes: 2_019_377_696,
|
|
166
|
+
params: '3B',
|
|
167
|
+
languages: ['en', 'de', 'fr', 'it', 'pt', 'hi', 'es', 'th'],
|
|
168
|
+
speed: 'slow',
|
|
169
|
+
description: {
|
|
170
|
+
en: 'Meta, better answers, but slow on a Raspberry Pi. No Russian.',
|
|
171
|
+
de: 'Von Meta, bessere Antworten, aber langsam auf einem Raspberry Pi. Kein Russisch.',
|
|
172
|
+
ru: 'От Meta, ответы лучше, но на Raspberry Pi медленно. Русского нет.',
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
id: 'qwen2.5-3b',
|
|
177
|
+
name: 'Qwen2.5 3B (Q4_K_M)',
|
|
178
|
+
file: 'qwen2.5-3b-instruct-q4_k_m.gguf',
|
|
179
|
+
url: `${HF}/Qwen/Qwen2.5-3B-Instruct-GGUF/resolve/main/qwen2.5-3b-instruct-q4_k_m.gguf`,
|
|
180
|
+
sizeBytes: 2_104_932_768,
|
|
181
|
+
params: '3B',
|
|
182
|
+
languages: ['multi'],
|
|
183
|
+
speed: 'slow',
|
|
184
|
+
description: {
|
|
185
|
+
en: 'Multilingual, better answers, but slow on a Raspberry Pi. Needs about 3 GB RAM.',
|
|
186
|
+
de: 'Mehrsprachig, bessere Antworten, aber langsam auf einem Raspberry Pi. Braucht etwa 3 GB RAM.',
|
|
187
|
+
ru: 'Многоязычная, ответы лучше, но на Raspberry Pi медленно. Нужно около 3 ГБ ОЗУ.',
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
id: 'gemma3-4b',
|
|
192
|
+
name: 'Gemma 3 4B (Q4_K_M)',
|
|
193
|
+
file: 'google_gemma-3-4b-it-Q4_K_M.gguf',
|
|
194
|
+
url: `${HF}/bartowski/google_gemma-3-4b-it-GGUF/resolve/main/google_gemma-3-4b-it-Q4_K_M.gguf`,
|
|
195
|
+
sizeBytes: 2_489_758_112,
|
|
196
|
+
params: '4B',
|
|
197
|
+
languages: ['multi'],
|
|
198
|
+
speed: 'slow',
|
|
199
|
+
description: {
|
|
200
|
+
en: 'Google, the best of the presets, but only sensible on a PC or a strong mini PC.',
|
|
201
|
+
de: 'Von Google, die beste der Vorlagen, aber nur auf einem PC oder starken Mini-PC sinnvoll.',
|
|
202
|
+
ru: 'От Google, лучшая из предустановок, но разумна только на ПК или мощном мини-ПК.',
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
];
|
|
206
|
+
exports.DEFAULT_PRESET_ID = 'qwen2.5-1.5b';
|
|
207
|
+
/** Space kept free after a download, so the host does not run out of disk. */
|
|
208
|
+
exports.DISK_RESERVE_BYTES = 200 * 1024 * 1024;
|
|
209
|
+
const META_FILE = 'models.json';
|
|
210
|
+
const STATS_FILE = 'local-llm-stats.json';
|
|
211
|
+
const GGUF_MAGIC = 'GGUF';
|
|
212
|
+
function emptyStats() {
|
|
213
|
+
return {
|
|
214
|
+
asked: 0,
|
|
215
|
+
answered: 0,
|
|
216
|
+
handoffs: 0,
|
|
217
|
+
busy: 0,
|
|
218
|
+
timeouts: 0,
|
|
219
|
+
errors: 0,
|
|
220
|
+
answerMs: 0,
|
|
221
|
+
tokens: 0,
|
|
222
|
+
genMs: 0,
|
|
223
|
+
firstTokenMs: 0,
|
|
224
|
+
firstTokenRuns: 0,
|
|
225
|
+
promptTokens: 0,
|
|
226
|
+
loads: 0,
|
|
227
|
+
lastLoadMs: 0,
|
|
228
|
+
lastUsed: 0,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/** Averages for display; nulls where there is nothing to average. */
|
|
232
|
+
function summarizeStats(s) {
|
|
233
|
+
const st = { ...emptyStats(), ...(s || {}) };
|
|
234
|
+
const handled = st.asked - st.busy;
|
|
235
|
+
return {
|
|
236
|
+
...st,
|
|
237
|
+
failureRate: handled > 0 ? (st.timeouts + st.errors) / handled : null,
|
|
238
|
+
tokensPerSec: st.genMs > 0 ? (st.tokens / st.genMs) * 1000 : null,
|
|
239
|
+
avgFirstTokenMs: st.firstTokenRuns > 0 ? st.firstTokenMs / st.firstTokenRuns : null,
|
|
240
|
+
avgAnswerMs: st.answered > 0 ? st.answerMs / st.answered : null,
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Normalizes what a user pastes: `hf:org/repo/file.gguf` and Hugging Face `/blob/` page links become direct
|
|
245
|
+
* download links. Returns '' for anything that is not http(s).
|
|
246
|
+
*/
|
|
247
|
+
function normalizeModelUrl(input) {
|
|
248
|
+
let url = (input || '').trim();
|
|
249
|
+
const hf = /^hf:\/*([^/]+)\/([^/]+)\/(.+)$/i.exec(url);
|
|
250
|
+
if (hf) {
|
|
251
|
+
url = `${HF}/${hf[1]}/${hf[2]}/resolve/main/${hf[3]}`;
|
|
252
|
+
}
|
|
253
|
+
if (!/^https?:\/\//i.test(url)) {
|
|
254
|
+
return '';
|
|
255
|
+
}
|
|
256
|
+
// https://huggingface.co/org/repo/blob/main/x.gguf → …/resolve/main/x.gguf
|
|
257
|
+
return url.replace(/^(https?:\/\/(?:www\.)?huggingface\.co\/[^/]+\/[^/]+)\/blob\//i, '$1/resolve/');
|
|
258
|
+
}
|
|
259
|
+
/** File name a model URL is stored as; '' when the URL does not end in a `.gguf` file. */
|
|
260
|
+
function modelFileName(url) {
|
|
261
|
+
const last = (url.split('?')[0].split('#')[0].split('/').pop() || '').trim();
|
|
262
|
+
let name = '';
|
|
263
|
+
try {
|
|
264
|
+
name = decodeURIComponent(last);
|
|
265
|
+
}
|
|
266
|
+
catch {
|
|
267
|
+
name = last;
|
|
268
|
+
}
|
|
269
|
+
name = name.replace(/[^\w.\-+]/g, '_');
|
|
270
|
+
return /\.gguf$/i.test(name) ? name : '';
|
|
271
|
+
}
|
|
272
|
+
/** Display name of a model file ("qwen2.5-1.5b-instruct-q4_k_m"). */
|
|
273
|
+
function modelDisplayName(file) {
|
|
274
|
+
const preset = exports.MODEL_PRESETS.find(p => p.file === file);
|
|
275
|
+
return preset ? preset.name : file.replace(/\.gguf$/i, '');
|
|
276
|
+
}
|
|
277
|
+
function presetForFile(file) {
|
|
278
|
+
return exports.MODEL_PRESETS.find(p => p.file === file);
|
|
279
|
+
}
|
|
280
|
+
/** A file name that is safe to join to the model directory. */
|
|
281
|
+
function safeFile(file) {
|
|
282
|
+
const base = path.basename(String(file || ''));
|
|
283
|
+
if (!base || base !== file || !/\.gguf$/i.test(base)) {
|
|
284
|
+
throw new Error(`invalid model file name "${file}"`);
|
|
285
|
+
}
|
|
286
|
+
return base;
|
|
287
|
+
}
|
|
288
|
+
/** The nearest existing directory at or above `dir` (statfs needs one). */
|
|
289
|
+
async function existingParent(dir) {
|
|
290
|
+
let cur = path.resolve(dir);
|
|
291
|
+
for (;;) {
|
|
292
|
+
try {
|
|
293
|
+
const st = await node_fs_1.promises.stat(cur);
|
|
294
|
+
if (st.isDirectory()) {
|
|
295
|
+
return cur;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
catch {
|
|
299
|
+
/* go up */
|
|
300
|
+
}
|
|
301
|
+
const up = path.dirname(cur);
|
|
302
|
+
if (up === cur) {
|
|
303
|
+
return cur;
|
|
304
|
+
}
|
|
305
|
+
cur = up;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
class ModelManager {
|
|
309
|
+
dataDir;
|
|
310
|
+
log;
|
|
311
|
+
stats = {};
|
|
312
|
+
statsLoaded = false;
|
|
313
|
+
saveTimer = null;
|
|
314
|
+
downloads = new Map();
|
|
315
|
+
/** Failed downloads stay listed for a minute so the GUI can show the error. */
|
|
316
|
+
failed = new Map();
|
|
317
|
+
constructor(dataDir, log) {
|
|
318
|
+
this.dataDir = dataDir;
|
|
319
|
+
this.log = log;
|
|
320
|
+
}
|
|
321
|
+
/** The model directory: configured absolute path, or `<dataDir>/models`. */
|
|
322
|
+
resolveDir(configured) {
|
|
323
|
+
const dir = (configured || '').trim();
|
|
324
|
+
if (!dir) {
|
|
325
|
+
return path.join(this.dataDir, 'models');
|
|
326
|
+
}
|
|
327
|
+
if (!path.isAbsolute(dir)) {
|
|
328
|
+
throw new Error(`the model directory must be an absolute path: "${dir}"`);
|
|
329
|
+
}
|
|
330
|
+
return path.resolve(dir);
|
|
331
|
+
}
|
|
332
|
+
/** Full path of a model file. */
|
|
333
|
+
modelPath(dir, file) {
|
|
334
|
+
return path.join(dir, safeFile(file));
|
|
335
|
+
}
|
|
336
|
+
// ---- Directory ---------------------------------------------------------------------------------------
|
|
337
|
+
async readMeta(dir) {
|
|
338
|
+
try {
|
|
339
|
+
const data = JSON.parse(await node_fs_1.promises.readFile(path.join(dir, META_FILE), 'utf8'));
|
|
340
|
+
return data && typeof data === 'object' ? data : {};
|
|
341
|
+
}
|
|
342
|
+
catch {
|
|
343
|
+
return {};
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
async writeMeta(dir, meta) {
|
|
347
|
+
await node_fs_1.promises.mkdir(dir, { recursive: true });
|
|
348
|
+
await node_fs_1.promises.writeFile(path.join(dir, META_FILE), JSON.stringify(meta, null, 2));
|
|
349
|
+
}
|
|
350
|
+
/** Remembers where a file came from, e.g. for a model the legacy loader downloaded. */
|
|
351
|
+
async rememberSource(dir, file, url) {
|
|
352
|
+
const meta = await this.readMeta(dir);
|
|
353
|
+
if (!meta[file]?.url) {
|
|
354
|
+
meta[file] = { ...meta[file], url, presetId: presetForFile(file)?.id, addedAt: Date.now() };
|
|
355
|
+
await this.writeMeta(dir, meta);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
/** The GGUF files in `dir` (not the `.part` files of running downloads). */
|
|
359
|
+
async list(dir) {
|
|
360
|
+
await this.loadStats();
|
|
361
|
+
let names = [];
|
|
362
|
+
try {
|
|
363
|
+
names = await node_fs_1.promises.readdir(dir);
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
return [];
|
|
367
|
+
}
|
|
368
|
+
const meta = await this.readMeta(dir);
|
|
369
|
+
const out = [];
|
|
370
|
+
for (const file of names) {
|
|
371
|
+
if (!/\.gguf$/i.test(file)) {
|
|
372
|
+
continue;
|
|
373
|
+
}
|
|
374
|
+
try {
|
|
375
|
+
const st = await node_fs_1.promises.stat(path.join(dir, file));
|
|
376
|
+
if (!st.isFile()) {
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
const m = meta[file] || {};
|
|
380
|
+
out.push({
|
|
381
|
+
file,
|
|
382
|
+
name: modelDisplayName(file),
|
|
383
|
+
sizeBytes: st.size,
|
|
384
|
+
modifiedAt: st.mtimeMs,
|
|
385
|
+
url: m.url || presetForFile(file)?.url,
|
|
386
|
+
presetId: m.presetId || presetForFile(file)?.id,
|
|
387
|
+
addedAt: m.addedAt,
|
|
388
|
+
stats: summarizeStats(this.stats[file]),
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
/* vanished meanwhile */
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
return out.sort((a, b) => a.sizeBytes - b.sizeBytes);
|
|
396
|
+
}
|
|
397
|
+
async disk(dir) {
|
|
398
|
+
try {
|
|
399
|
+
const st = await node_fs_1.promises.statfs(await existingParent(dir));
|
|
400
|
+
return { free: st.bavail * st.bsize, total: st.blocks * st.bsize };
|
|
401
|
+
}
|
|
402
|
+
catch {
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
/** Deletes a model file and its source entry. Never the active model or one that is downloading. */
|
|
407
|
+
async delete(dir, file, activeFile) {
|
|
408
|
+
const name = safeFile(file);
|
|
409
|
+
if (name === activeFile) {
|
|
410
|
+
throw new Error('the active model cannot be deleted; select another model first');
|
|
411
|
+
}
|
|
412
|
+
if (this.downloads.has(name)) {
|
|
413
|
+
throw new Error('the model is still downloading; cancel the download first');
|
|
414
|
+
}
|
|
415
|
+
await node_fs_1.promises.rm(path.join(dir, name), { force: true });
|
|
416
|
+
const meta = await this.readMeta(dir);
|
|
417
|
+
if (meta[name]) {
|
|
418
|
+
delete meta[name];
|
|
419
|
+
await this.writeMeta(dir, meta);
|
|
420
|
+
}
|
|
421
|
+
this.log.info(`Local model ${name} deleted from ${dir}`);
|
|
422
|
+
}
|
|
423
|
+
// ---- Downloads ---------------------------------------------------------------------------------------
|
|
424
|
+
/** Running downloads plus failed ones of the last minute. */
|
|
425
|
+
listDownloads() {
|
|
426
|
+
const now = Date.now();
|
|
427
|
+
for (const [key, f] of this.failed) {
|
|
428
|
+
if (now - f.failedAt > 60_000) {
|
|
429
|
+
this.failed.delete(key);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
return [
|
|
433
|
+
...[...this.downloads.values()].map(d => ({ ...d.info })),
|
|
434
|
+
...[...this.failed.values()].map(f => f.info),
|
|
435
|
+
];
|
|
436
|
+
}
|
|
437
|
+
isDownloading(file) {
|
|
438
|
+
return this.downloads.has(file);
|
|
439
|
+
}
|
|
440
|
+
/**
|
|
441
|
+
* Starts a download in the background and returns at once. Checks first: valid `.gguf` link, not
|
|
442
|
+
* already there, enough free space (size from a HEAD request; a server without a size is not checked).
|
|
443
|
+
* `onDone` runs after success or failure.
|
|
444
|
+
*/
|
|
445
|
+
async startDownload(input, dir, onDone, onProgress) {
|
|
446
|
+
const url = normalizeModelUrl(input);
|
|
447
|
+
if (!url) {
|
|
448
|
+
throw new Error('not a http(s) link or hf:org/repo/file.gguf');
|
|
449
|
+
}
|
|
450
|
+
const file = modelFileName(url);
|
|
451
|
+
if (!file) {
|
|
452
|
+
throw new Error('the link must point to a .gguf file');
|
|
453
|
+
}
|
|
454
|
+
if (this.downloads.has(file)) {
|
|
455
|
+
throw new Error(`${file} is already downloading`);
|
|
456
|
+
}
|
|
457
|
+
if ((0, node_fs_1.existsSync)(path.join(dir, file))) {
|
|
458
|
+
throw new Error(`${file} is already in ${dir}`);
|
|
459
|
+
}
|
|
460
|
+
const preset = exports.MODEL_PRESETS.find(p => p.url === url || p.file === file);
|
|
461
|
+
let total = preset?.sizeBytes || 0;
|
|
462
|
+
try {
|
|
463
|
+
const head = await fetch(url, { method: 'HEAD', redirect: 'follow' });
|
|
464
|
+
if (!head.ok) {
|
|
465
|
+
throw new Error(`HTTP ${head.status}`);
|
|
466
|
+
}
|
|
467
|
+
total = Number(head.headers.get('content-length')) || total;
|
|
468
|
+
}
|
|
469
|
+
catch (e) {
|
|
470
|
+
throw new Error(`cannot reach ${url}: ${e.message}`);
|
|
471
|
+
}
|
|
472
|
+
await node_fs_1.promises.mkdir(dir, { recursive: true });
|
|
473
|
+
const disk = await this.disk(dir);
|
|
474
|
+
if (disk && total && total + exports.DISK_RESERVE_BYTES > disk.free) {
|
|
475
|
+
throw new Error(`not enough free space in ${dir}: ${formatBytes(total)} needed plus ${formatBytes(exports.DISK_RESERVE_BYTES)} reserve, ${formatBytes(disk.free)} free`);
|
|
476
|
+
}
|
|
477
|
+
this.failed.delete(file);
|
|
478
|
+
const info = { file, url, dir, presetId: preset?.id, received: 0, total, startedAt: Date.now() };
|
|
479
|
+
const abort = new AbortController();
|
|
480
|
+
this.downloads.set(file, { info, abort });
|
|
481
|
+
this.log.info(`Downloading local model ${file} (${total ? formatBytes(total) : 'unknown size'}) to ${dir}`);
|
|
482
|
+
void this.runDownload(info, abort.signal, onProgress)
|
|
483
|
+
.then(async () => {
|
|
484
|
+
const meta = await this.readMeta(dir);
|
|
485
|
+
meta[file] = { url, presetId: preset?.id, addedAt: Date.now() };
|
|
486
|
+
await this.writeMeta(dir, meta);
|
|
487
|
+
this.log.info(`Local model ${file} downloaded`);
|
|
488
|
+
onDone?.(file);
|
|
489
|
+
})
|
|
490
|
+
.catch(e => {
|
|
491
|
+
const msg = abort.signal.aborted ? 'cancelled' : e.message;
|
|
492
|
+
if (!abort.signal.aborted) {
|
|
493
|
+
this.log.warn(`Download of local model ${file} failed: ${msg}`);
|
|
494
|
+
}
|
|
495
|
+
this.failed.set(file, { info: { ...info, error: msg }, failedAt: Date.now() });
|
|
496
|
+
onDone?.(file, new Error(msg));
|
|
497
|
+
})
|
|
498
|
+
.finally(() => this.downloads.delete(file));
|
|
499
|
+
return { ...info };
|
|
500
|
+
}
|
|
501
|
+
cancelDownload(file) {
|
|
502
|
+
const d = this.downloads.get(file);
|
|
503
|
+
if (!d) {
|
|
504
|
+
return false;
|
|
505
|
+
}
|
|
506
|
+
d.abort.abort();
|
|
507
|
+
return true;
|
|
508
|
+
}
|
|
509
|
+
/** Download to `<file>.part`, check the GGUF magic, then rename. */
|
|
510
|
+
async runDownload(info, signal, onProgress) {
|
|
511
|
+
const target = path.join(info.dir, info.file);
|
|
512
|
+
const part = `${target}.part`;
|
|
513
|
+
try {
|
|
514
|
+
const res = await fetch(info.url, { redirect: 'follow', signal });
|
|
515
|
+
if (!res.ok || !res.body) {
|
|
516
|
+
throw new Error(`HTTP ${res.status}`);
|
|
517
|
+
}
|
|
518
|
+
info.total = Number(res.headers.get('content-length')) || info.total;
|
|
519
|
+
let lastReport = 0;
|
|
520
|
+
const counter = new node_stream_1.Transform({
|
|
521
|
+
transform(chunk, _enc, cb) {
|
|
522
|
+
info.received += chunk.length;
|
|
523
|
+
const now = Date.now();
|
|
524
|
+
if (onProgress && now - lastReport > 1000) {
|
|
525
|
+
lastReport = now;
|
|
526
|
+
onProgress({ ...info });
|
|
527
|
+
}
|
|
528
|
+
cb(null, chunk);
|
|
529
|
+
},
|
|
530
|
+
});
|
|
531
|
+
await (0, promises_1.pipeline)(node_stream_1.Readable.fromWeb(res.body), counter, (0, node_fs_1.createWriteStream)(part), { signal });
|
|
532
|
+
const fh = await node_fs_1.promises.open(part, 'r');
|
|
533
|
+
const magic = Buffer.alloc(4);
|
|
534
|
+
try {
|
|
535
|
+
await fh.read(magic, 0, 4, 0);
|
|
536
|
+
}
|
|
537
|
+
finally {
|
|
538
|
+
await fh.close();
|
|
539
|
+
}
|
|
540
|
+
if (magic.toString('latin1') !== GGUF_MAGIC) {
|
|
541
|
+
throw new Error('the downloaded file is not a GGUF model (wrong link?)');
|
|
542
|
+
}
|
|
543
|
+
await node_fs_1.promises.rename(part, target);
|
|
544
|
+
onProgress?.({ ...info, received: info.total || info.received });
|
|
545
|
+
}
|
|
546
|
+
catch (e) {
|
|
547
|
+
await node_fs_1.promises.rm(part, { force: true }).catch(() => undefined);
|
|
548
|
+
throw e;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/** Cancels all downloads (adapter stop). */
|
|
552
|
+
cancelAll() {
|
|
553
|
+
for (const d of this.downloads.values()) {
|
|
554
|
+
d.abort.abort();
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
// ---- Statistics --------------------------------------------------------------------------------------
|
|
558
|
+
async loadStats() {
|
|
559
|
+
if (this.statsLoaded) {
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
this.statsLoaded = true;
|
|
563
|
+
try {
|
|
564
|
+
const data = JSON.parse(await node_fs_1.promises.readFile(path.join(this.dataDir, STATS_FILE), 'utf8'));
|
|
565
|
+
if (data && typeof data === 'object') {
|
|
566
|
+
this.stats = data;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
catch {
|
|
570
|
+
/* first start */
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
scheduleSave() {
|
|
574
|
+
if (this.saveTimer) {
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
this.saveTimer = setTimeout(() => {
|
|
578
|
+
this.saveTimer = null;
|
|
579
|
+
void this.flush();
|
|
580
|
+
}, 5000);
|
|
581
|
+
}
|
|
582
|
+
async flush() {
|
|
583
|
+
if (this.saveTimer) {
|
|
584
|
+
clearTimeout(this.saveTimer);
|
|
585
|
+
this.saveTimer = null;
|
|
586
|
+
}
|
|
587
|
+
if (!this.statsLoaded) {
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
try {
|
|
591
|
+
await node_fs_1.promises.mkdir(this.dataDir, { recursive: true });
|
|
592
|
+
await node_fs_1.promises.writeFile(path.join(this.dataDir, STATS_FILE), JSON.stringify(this.stats));
|
|
593
|
+
}
|
|
594
|
+
catch (e) {
|
|
595
|
+
this.log.debug(`Cannot save local model statistics: ${e.message}`);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
async entry(file) {
|
|
599
|
+
await this.loadStats();
|
|
600
|
+
const s = (this.stats[file] = { ...emptyStats(), ...(this.stats[file] || {}) });
|
|
601
|
+
return s;
|
|
602
|
+
}
|
|
603
|
+
async recordRun(file, sample) {
|
|
604
|
+
const s = await this.entry(file);
|
|
605
|
+
s.asked++;
|
|
606
|
+
s.lastUsed = Date.now();
|
|
607
|
+
switch (sample.outcome) {
|
|
608
|
+
case 'answered':
|
|
609
|
+
s.answered++;
|
|
610
|
+
s.answerMs += sample.ms;
|
|
611
|
+
break;
|
|
612
|
+
case 'handoff':
|
|
613
|
+
s.handoffs++;
|
|
614
|
+
break;
|
|
615
|
+
case 'busy':
|
|
616
|
+
s.busy++;
|
|
617
|
+
break;
|
|
618
|
+
case 'timeout':
|
|
619
|
+
s.timeouts++;
|
|
620
|
+
break;
|
|
621
|
+
case 'error':
|
|
622
|
+
s.errors++;
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
if (sample.tokens && sample.genMs && sample.genMs > 0) {
|
|
626
|
+
s.tokens += sample.tokens;
|
|
627
|
+
s.genMs += sample.genMs;
|
|
628
|
+
}
|
|
629
|
+
if (sample.firstTokenMs !== undefined && sample.firstTokenMs >= 0) {
|
|
630
|
+
s.firstTokenMs += sample.firstTokenMs;
|
|
631
|
+
s.firstTokenRuns++;
|
|
632
|
+
}
|
|
633
|
+
if (sample.promptTokens) {
|
|
634
|
+
s.promptTokens += sample.promptTokens;
|
|
635
|
+
}
|
|
636
|
+
this.scheduleSave();
|
|
637
|
+
}
|
|
638
|
+
async recordLoad(file, ms) {
|
|
639
|
+
const s = await this.entry(file);
|
|
640
|
+
s.loads++;
|
|
641
|
+
s.lastLoadMs = ms;
|
|
642
|
+
this.scheduleSave();
|
|
643
|
+
}
|
|
644
|
+
async recordBenchmark(file, result) {
|
|
645
|
+
const s = await this.entry(file);
|
|
646
|
+
s.benchmark = result;
|
|
647
|
+
await this.flush();
|
|
648
|
+
}
|
|
649
|
+
async getStats(file) {
|
|
650
|
+
await this.loadStats();
|
|
651
|
+
return summarizeStats(this.stats[file]);
|
|
652
|
+
}
|
|
653
|
+
/** Resets the statistics of one model, or of all when `file` is empty. The benchmark result stays. */
|
|
654
|
+
async resetStats(file) {
|
|
655
|
+
await this.loadStats();
|
|
656
|
+
const keep = (f) => ({ ...emptyStats(), benchmark: this.stats[f]?.benchmark });
|
|
657
|
+
if (file) {
|
|
658
|
+
this.stats[file] = keep(file);
|
|
659
|
+
}
|
|
660
|
+
else {
|
|
661
|
+
for (const f of Object.keys(this.stats)) {
|
|
662
|
+
this.stats[f] = keep(f);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
await this.flush();
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
exports.ModelManager = ModelManager;
|
|
669
|
+
function formatBytes(n) {
|
|
670
|
+
if (n >= 1024 ** 3) {
|
|
671
|
+
return `${(n / 1024 ** 3).toFixed(2)} GB`;
|
|
672
|
+
}
|
|
673
|
+
if (n >= 1024 ** 2) {
|
|
674
|
+
return `${Math.round(n / 1024 ** 2)} MB`;
|
|
675
|
+
}
|
|
676
|
+
return `${Math.round(n / 1024)} KB`;
|
|
677
|
+
}
|
|
678
|
+
//# sourceMappingURL=llmModels.js.map
|