iobroker.assistant 1.1.0 → 1.2.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 +63 -1
- package/admin/custom/.vite/manifest.json +62 -62
- package/admin/custom/assets/Components-BSewCBfS.js +2 -0
- package/admin/custom/assets/{_virtual_mf-localSharedImportMap___mfe_internal__ConfigCustomAssistant__mf_owner__1-A4gakomR.js → _virtual_mf-localSharedImportMap___mfe_internal__ConfigCustomAssistant__mf_owner__1-C5NPM0Of.js} +1 -1
- package/admin/custom/assets/{_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__201375093093590__loadShare___mf_0_emotion_mf_1_styled__loadShare__.js-CwqmfMvn.js → _virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__163539842885036__loadShare___mf_0_emotion_mf_1_styled__loadShare__.js-IaosaymG.js} +1 -1
- package/admin/custom/assets/{_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__201375093093590__loadShare___mf_0_iobroker_mf_1_gui_mf_2_components__loadShare__.js-BMuLYe6f.js → _virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__163539842885036__loadShare___mf_0_iobroker_mf_1_gui_mf_2_components__loadShare__.js-Cdyjt5KT.js} +39 -39
- package/admin/custom/assets/{_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__201375093093590__loadShare__shared-BxjZmnpc.js → _virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__163539842885036__loadShare__shared-Dsw5zWS8.js} +1 -1
- package/admin/custom/assets/_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__163539842885036__prebuild___mf_0_iobroker_mf_1_json_mf_2_config__prebuild__-SgSagTOv.js +122 -0
- package/admin/custom/assets/{hostInit-4roC4hEj.js → hostInit-jJe8bIfV.js} +1 -1
- package/admin/custom/assets/{lib-CchzgTUs.js → lib-CL8mXvI6.js} +1 -1
- package/admin/custom/assets/{mf-entry-bootstrap-0-4806bbe4.js → mf-entry-bootstrap-0-ab227aa6.js} +2 -2
- package/admin/custom/assets/{pendingShares-DLlv8yFY.js → pendingShares-wrGtfliQ.js} +1 -1
- package/admin/custom/customComponents.js +2 -2
- package/admin/custom/i18n/de.json +40 -0
- package/admin/custom/i18n/en.json +40 -0
- package/admin/custom/i18n/es.json +40 -0
- package/admin/custom/i18n/fr.json +40 -0
- package/admin/custom/i18n/it.json +40 -0
- package/admin/custom/i18n/nl.json +40 -0
- package/admin/custom/i18n/pl.json +40 -0
- package/admin/custom/i18n/pt.json +40 -0
- package/admin/custom/i18n/ru.json +40 -0
- package/admin/custom/i18n/uk.json +40 -0
- package/admin/custom/i18n/zh-cn.json +40 -0
- 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 +6 -2
- package/admin/i18n/en.json +6 -2
- package/admin/i18n/es.json +6 -2
- package/admin/i18n/fr.json +6 -2
- package/admin/i18n/it.json +6 -2
- package/admin/i18n/nl.json +6 -2
- package/admin/i18n/pl.json +6 -2
- package/admin/i18n/pt.json +6 -2
- package/admin/i18n/ru.json +6 -2
- package/admin/i18n/uk.json +6 -2
- package/admin/i18n/zh-cn.json +6 -2
- package/admin/jsonConfig.json +29 -2
- package/build/lib/journal.js +298 -2
- package/build/lib/journal.js.map +1 -1
- package/build/lib/llm.js +105 -4
- package/build/lib/llm.js.map +1 -1
- package/build/lib/localLlm.js +47 -10
- package/build/lib/localLlm.js.map +1 -1
- package/build/lib/nlu.js +124 -5
- package/build/lib/nlu.js.map +1 -1
- package/build/lib/tools.js +3 -0
- package/build/lib/tools.js.map +1 -1
- package/build/lib/voice/esphome.js +81 -7
- package/build/lib/voice/esphome.js.map +1 -1
- package/build/lib/voice/esphomeProto.js.map +1 -1
- package/build/lib/voice/mediaServer.js +29 -1
- package/build/lib/voice/mediaServer.js.map +1 -1
- package/build/lib/voice/protocol.js.map +1 -1
- package/build/lib/voice/voiceServer.js +81 -7
- package/build/lib/voice/voiceServer.js.map +1 -1
- package/build/lib/voice/wakeWords.js +284 -0
- package/build/lib/voice/wakeWords.js.map +1 -0
- package/build/lib/voice/wyoming.js +12 -4
- package/build/lib/voice/wyoming.js.map +1 -1
- package/build/lib/weather.js +160 -0
- package/build/lib/weather.js.map +1 -1
- package/build/main.js +716 -74
- package/build/main.js.map +1 -1
- package/io-package.json +27 -25
- package/package.json +5 -5
- package/admin/custom/assets/Components-C6qcpaRC.js +0 -1
- package/admin/custom/assets/_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__201375093093590__prebuild___mf_0_iobroker_mf_1_json_mf_2_config__prebuild__-TM4l0OYm.js +0 -122
- /package/admin/custom/assets/{_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__201375093093590__loadShare___mf_0_emotion_mf_1_react__loadShare__.js-BkH7fzDw.js → _virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__163539842885036__loadShare___mf_0_emotion_mf_1_react__loadShare__.js-BkH7fzDw.js} +0 -0
- /package/admin/custom/assets/{_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__201375093093590__prebuild__react_mf_1_jsx_mf_2_runtime__prebuild__-M61rjtmp.js → _virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__163539842885036__prebuild__react_mf_1_jsx_mf_2_runtime__prebuild__-M61rjtmp.js} +0 -0
package/build/lib/localLlm.js
CHANGED
|
@@ -40,6 +40,7 @@ exports.installLocalLlm = installLocalLlm;
|
|
|
40
40
|
exports.isHandoff = isHandoff;
|
|
41
41
|
const node_child_process_1 = require("node:child_process");
|
|
42
42
|
const node_module_1 = require("node:module");
|
|
43
|
+
const os = __importStar(require("node:os"));
|
|
43
44
|
const node_url_1 = require("node:url");
|
|
44
45
|
const path = __importStar(require("node:path"));
|
|
45
46
|
const fs = __importStar(require("node:fs"));
|
|
@@ -117,9 +118,15 @@ class LocalLlm {
|
|
|
117
118
|
session = null;
|
|
118
119
|
model = null;
|
|
119
120
|
context = null;
|
|
121
|
+
/** A prompt is running — the session takes one at a time. */
|
|
122
|
+
busy = false;
|
|
120
123
|
constructor(opts) {
|
|
121
124
|
this.opts = opts;
|
|
122
125
|
}
|
|
126
|
+
/** A prompt is running, so {@link ask} would hand off at once. */
|
|
127
|
+
get running() {
|
|
128
|
+
return this.busy;
|
|
129
|
+
}
|
|
123
130
|
/** Full system prompt = the strict HANDOFF rule (device work must escalate to the tool-enabled cloud LLM). */
|
|
124
131
|
systemPrompt() {
|
|
125
132
|
// The local model has NO tools and cannot touch the smart home. Anything device-related MUST hand off,
|
|
@@ -159,27 +166,57 @@ class LocalLlm {
|
|
|
159
166
|
}
|
|
160
167
|
const llama = await getLlama();
|
|
161
168
|
this.model = await llama.loadModel({ modelPath });
|
|
162
|
-
|
|
169
|
+
// Leave one core to js-controller and the other adapters: llama.cpp takes all of them otherwise, and on
|
|
170
|
+
// a Raspberry Pi the whole host — the voice server included — stalls for the length of an answer.
|
|
171
|
+
const cores = typeof os.availableParallelism === 'function' ? os.availableParallelism() : os.cpus().length;
|
|
172
|
+
this.context = await this.model.createContext({ threads: Math.max(1, cores - 1) });
|
|
163
173
|
this.session = new LlamaChatSession({
|
|
164
174
|
contextSequence: this.context.getSequence(),
|
|
165
175
|
systemPrompt: this.systemPrompt(),
|
|
166
176
|
});
|
|
167
177
|
this.opts.log.info('Local model loaded.');
|
|
168
178
|
}
|
|
169
|
-
/**
|
|
170
|
-
|
|
171
|
-
|
|
179
|
+
/**
|
|
180
|
+
* Ask the local model. Returns its answer, or {@link HANDOFF} to escalate to the cloud LLM — also when
|
|
181
|
+
* another question is still being answered (waiting for it would only add its run time to this one).
|
|
182
|
+
* With `timeoutMs` the generation is aborted after that long and the call throws.
|
|
183
|
+
*/
|
|
184
|
+
async ask(question, timeoutMs = 0) {
|
|
185
|
+
if (!this.session || this.busy) {
|
|
172
186
|
return exports.HANDOFF;
|
|
173
187
|
}
|
|
174
|
-
|
|
188
|
+
this.busy = true;
|
|
189
|
+
const abort = new AbortController();
|
|
190
|
+
const timer = timeoutMs > 0 ? setTimeout(() => abort.abort(), timeoutMs) : null;
|
|
175
191
|
try {
|
|
176
|
-
|
|
192
|
+
// Reset per-question so prompts stay independent (best-effort across node-llama-cpp versions).
|
|
193
|
+
try {
|
|
194
|
+
this.session.setChatHistory?.([]);
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
/* ignore */
|
|
198
|
+
}
|
|
199
|
+
const answer = await this.session.prompt(question, {
|
|
200
|
+
maxTokens: this.opts.maxTokens || 256,
|
|
201
|
+
signal: abort.signal,
|
|
202
|
+
});
|
|
203
|
+
if (abort.signal.aborted) {
|
|
204
|
+
throw new Error('aborted');
|
|
205
|
+
}
|
|
206
|
+
return (answer || '').trim();
|
|
177
207
|
}
|
|
178
|
-
catch {
|
|
179
|
-
|
|
208
|
+
catch (e) {
|
|
209
|
+
if (abort.signal.aborted) {
|
|
210
|
+
throw new Error(`timed out after ${Math.round(timeoutMs / 1000)} s`);
|
|
211
|
+
}
|
|
212
|
+
throw e;
|
|
213
|
+
}
|
|
214
|
+
finally {
|
|
215
|
+
if (timer) {
|
|
216
|
+
clearTimeout(timer);
|
|
217
|
+
}
|
|
218
|
+
this.busy = false;
|
|
180
219
|
}
|
|
181
|
-
const answer = await this.session.prompt(question, { maxTokens: this.opts.maxTokens || 512 });
|
|
182
|
-
return (answer || '').trim();
|
|
183
220
|
}
|
|
184
221
|
async dispose() {
|
|
185
222
|
try {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localLlm.js","sourceRoot":"","sources":["../../src/lib/localLlm.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,kDAMC;AAGD,8CAMC;AAMD,0CAiBC;AA6GD,8BAOC;AA7MD,2DAA2C;AAC3C,6CAA4C;AAC5C,uCAAyC;AACzC,gDAAkC;AAClC,4CAA8B;AAE9B;;;;;;;;;;GAUG;AAEH,+EAA+E;AACl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KAAK,eAAO,IAAI,EAAE,CAAC,UAAU,CAAC,GAAG,eAAO,GAAG,CAAC,IAAI,EAAE,KAAK,IAAI,eAAO,GAAG,EAAE,CAAC;QAC1E,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,OAAO,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import { spawn } from 'node:child_process';\nimport { createRequire } from 'node:module';\nimport { pathToFileURL } from 'node:url';\nimport * as path from 'node:path';\nimport * as fs from 'node:fs';\n\n/**\n * Tier-1a local LLM via `node-llama-cpp` — installed **on demand** (not a package.json dependency, so\n * users who don't enable it never pay the large native download). Everything lives in the writable\n * instance data dir (`<iobroker-data>/assistant.<n>/`), which survives adapter upgrades; on a fresh\n * upgrade the module is simply re-installed on request.\n *\n * The model runs **tool-free**: it answers general questions and, when a request needs live ioBroker\n * state or it is unsure, it emits the {@link HANDOFF} sentinel so the orchestrator escalates to the\n * cloud LLM (which has the tools). Small models handle a \"say HANDOFF\" rule far more reliably than\n * real tool-calling.\n */\n\n/** Sentinel the local model returns to escalate a request to the cloud LLM. */\nexport const HANDOFF = 'HANDOFF';\n\n/** Default local model: small enough for arm64 (Raspi/NanoPi), tool-free chat, ~1 GB. */\nexport const DEFAULT_LOCAL_MODEL_URL =\n 'https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf';\n\nconst NPM = process.platform === 'win32' ? 'npm.cmd' : 'npm';\n\ntype Logger = Pick<ioBroker.Logger, 'info' | 'warn' | 'debug' | 'error'>;\n\nexport interface LocalLlmOptions {\n /** Writable instance data dir (from `adapter.getAbsoluteInstanceDataDir()`). */\n dataDir: string;\n /** Direct URL of a GGUF model file. */\n modelUrl: string;\n systemPrompt?: string;\n maxTokens?: number;\n log: Logger;\n}\n\n/** Directory the on-demand npm install targets (`<dataDir>/node_modules/node-llama-cpp`). */\nfunction moduleDir(dataDir: string): string {\n return path.join(dataDir, 'node_modules', 'node-llama-cpp');\n}\n\n/** Local file path a model URL is cached to. */\nfunction modelPathFor(dataDir: string, url: string): string {\n const file = url.split('/').pop() || 'model.gguf';\n return path.join(dataDir, 'models', file);\n}\n\n/** True once `node-llama-cpp` has been installed into the data dir. */\nexport function isLocalLlmInstalled(dataDir: string): boolean {\n try {\n return fs.existsSync(path.join(moduleDir(dataDir), 'package.json'));\n } catch {\n return false;\n }\n}\n\n/** True once the model file has been downloaded. */\nexport function isModelDownloaded(dataDir: string, url: string): boolean {\n try {\n return fs.existsSync(modelPathFor(dataDir, url));\n } catch {\n return false;\n }\n}\n\n/**\n * Install `node-llama-cpp` (with its prebuilt native binary) into the data dir via a spawned `npm\n * install`. Streams npm output to `onProgress`. Idempotent-ish: npm is a no-op if already present.\n */\nexport function installLocalLlm(dataDir: string, onProgress: (line: string) => void): Promise<void> {\n return new Promise((resolve, reject) => {\n fs.mkdirSync(dataDir, { recursive: true });\n // A local package.json makes npm install into <dataDir>/node_modules (and not walk up).\n const pkg = path.join(dataDir, 'package.json');\n if (!fs.existsSync(pkg)) {\n fs.writeFileSync(pkg, JSON.stringify({ name: 'assistant-local-llm', private: true, version: '1.0.0' }));\n }\n const child = spawn(NPM, ['install', 'node-llama-cpp@3', '--omit=dev', '--no-audit', '--no-fund'], {\n cwd: dataDir,\n shell: process.platform === 'win32', // npm.cmd needs a shell on Windows\n });\n child.stdout.on('data', d => onProgress(String(d).trim()));\n child.stderr.on('data', d => onProgress(String(d).trim()));\n child.on('error', reject);\n child.on('close', code => (code === 0 ? resolve() : reject(new Error(`npm install exited with code ${code}`))));\n });\n}\n\n/**\n * A loaded local model. `load()` imports the on-demand module, downloads the GGUF if missing, and\n * spins up a llama context. `ask()` returns the model's answer (or {@link HANDOFF}).\n */\nexport class LocalLlm {\n private session: any = null;\n private model: any = null;\n private context: any = null;\n\n constructor(private readonly opts: LocalLlmOptions) {}\n\n /** Full system prompt = the strict HANDOFF rule (device work must escalate to the tool-enabled cloud LLM). */\n private systemPrompt(): string {\n // The local model has NO tools and cannot touch the smart home. Anything device-related MUST hand off,\n // including control commands — otherwise a small model tends to \"helpfully\" refuse instead of escalating.\n return (\n `You are a smart-home assistant, but you have NO ability to read or control anything in the home. ` +\n `If the user wants to control or query ANYTHING in the home — e.g. turn on/off, switch, dim, ` +\n `open/close, start/stop, set, activate, or ask for the value/state/status of a light, socket, ` +\n `sensor, device, room, or the home — OR if you are unsure — you MUST reply with EXACTLY \"${HANDOFF}\" ` +\n `and nothing else. Do NOT apologise, do NOT explain, do NOT say you are an AI. ` +\n `Reply directly ONLY for general knowledge, small talk, or questions unrelated to the home.`\n );\n }\n\n async load(onProgress?: (line: string) => void): Promise<void> {\n const dir = moduleDir(this.opts.dataDir);\n if (!fs.existsSync(path.join(dir, 'package.json'))) {\n throw new Error('node-llama-cpp is not installed — run the install first');\n }\n // Resolve the package entry from the data dir and import it (v3 is ESM-only → dynamic import).\n const req = createRequire(path.join(this.opts.dataDir, 'package.json'));\n const entry = req.resolve('node-llama-cpp');\n const mod: any = await import(pathToFileURL(entry).href);\n const { getLlama, LlamaChatSession, createModelDownloader } = mod;\n\n const modelPath = modelPathFor(this.opts.dataDir, this.opts.modelUrl);\n if (!fs.existsSync(modelPath)) {\n this.opts.log.info(`Downloading local model → ${modelPath}`);\n fs.mkdirSync(path.dirname(modelPath), { recursive: true });\n const downloader = await createModelDownloader({\n modelUri: this.opts.modelUrl,\n dirPath: path.dirname(modelPath),\n fileName: path.basename(modelPath),\n onProgress: (p: { downloadedSize: number; totalSize: number }) => {\n if (p.totalSize) {\n onProgress?.(`download ${Math.round((100 * p.downloadedSize) / p.totalSize)}%`);\n }\n },\n });\n await downloader.download();\n }\n\n const llama = await getLlama();\n this.model = await llama.loadModel({ modelPath });\n this.context = await this.model.createContext();\n this.session = new LlamaChatSession({\n contextSequence: this.context.getSequence(),\n systemPrompt: this.systemPrompt(),\n });\n this.opts.log.info('Local model loaded.');\n }\n\n /** Ask the local model. Returns its answer, or {@link HANDOFF} to escalate to the cloud LLM. */\n async ask(question: string): Promise<string> {\n if (!this.session) {\n return HANDOFF;\n }\n // Reset per-question so prompts stay independent (best-effort across node-llama-cpp versions).\n try {\n this.session.setChatHistory?.([]);\n } catch {\n /* ignore */\n }\n const answer: string = await this.session.prompt(question, { maxTokens: this.opts.maxTokens || 512 });\n return (answer || '').trim();\n }\n\n async dispose(): Promise<void> {\n try {\n await this.context?.dispose?.();\n await this.model?.dispose?.();\n } catch {\n /* ignore */\n }\n this.session = this.context = this.model = null;\n }\n}\n\n/**\n * Refusal patterns (de/en/ru) a small tool-free model emits when it *should* have handed off — e.g.\n * \"I'm an AI and can't control real devices\". Treated as a hand-off so the tool-enabled cloud LLM runs.\n */\nconst REFUSAL = [\n /\\bhandoff\\b/i,\n // en\n /\\bi('?m| am)\\s+an?\\s+ai\\b/i,\n /\\bi\\s+(can('?t|not)|am\\s+unable\\s+to)\\b.*\\b(turn|switch|control|access|interact|device|light)\\b/i,\n // de\n /\\bich\\s+bin\\s+eine?\\s+(ki|künstliche)\\b/i,\n /\\bkann\\s+.*\\bnicht\\b.*\\b(schalten|steuern|einschalten|zugreifen|geräte?|licht)\\b/i,\n // ru (\\w/\\b are ASCII-only → use Cyrillic classes / plain substrings)\n /искусственн[а-яё]*\\s+интеллект/i,\n /не\\s+(могу|имею)/i,\n];\n\n/** True if the model's answer signals (or implies) a hand-off to the cloud LLM. */\nexport function isHandoff(answer: string): boolean {\n const trimmed = answer.trim();\n const up = trimmed.toUpperCase();\n if (up === HANDOFF || up.startsWith(`${HANDOFF} `) || up === `\"${HANDOFF}\"`) {\n return true;\n }\n return REFUSAL.some(re => re.test(trimmed));\n}\n"]}
|
|
1
|
+
{"version":3,"file":"localLlm.js","sourceRoot":"","sources":["../../src/lib/localLlm.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoDA,kDAM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AsE;IACtE,iCAAiC;IACjC,mBAAmB;CACtB,CAAC;AAEF,mFAAmF;AACnF,SAAgB,SAAS,CAAC,MAAc;IACpC,MAAM,OAAO,GAAG,MAAM,CAAC,IAAI,EAAE,CAAC;IAC9B,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IACjC,IAAI,EAAE,KAAK,eAAO,IAAI,EAAE,CAAC,UAAU,CAAC,GAAG,eAAO,GAAG,CAAC,IAAI,EAAE,KAAK,IAAI,eAAO,GAAG,EAAE,CAAC;QAC1E,OAAO,IAAI,CAAC;IAChB,CAAC;IACD,OAAO,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC;AAChD,CAAC","sourcesContent":["import { spawn } from 'node:child_process';\nimport { createRequire } from 'node:module';\nimport * as os from 'node:os';\nimport { pathToFileURL } from 'node:url';\nimport * as path from 'node:path';\nimport * as fs from 'node:fs';\n\n/**\n * Tier-1a local LLM via `node-llama-cpp` — installed **on demand** (not a package.json dependency, so\n * users who don't enable it never pay the large native download). Everything lives in the writable\n * instance data dir (`<iobroker-data>/assistant.<n>/`), which survives adapter upgrades; on a fresh\n * upgrade the module is simply re-installed on request.\n *\n * The model runs **tool-free**: it answers general questions and, when a request needs live ioBroker\n * state or it is unsure, it emits the {@link HANDOFF} sentinel so the orchestrator escalates to the\n * cloud LLM (which has the tools). Small models handle a \"say HANDOFF\" rule far more reliably than\n * real tool-calling.\n */\n\n/** Sentinel the local model returns to escalate a request to the cloud LLM. */\nexport const HANDOFF = 'HANDOFF';\n\n/** Default local model: small enough for arm64 (Raspi/NanoPi), tool-free chat, ~1 GB. */\nexport const DEFAULT_LOCAL_MODEL_URL =\n 'https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf';\n\nconst NPM = process.platform === 'win32' ? 'npm.cmd' : 'npm';\n\ntype Logger = Pick<ioBroker.Logger, 'info' | 'warn' | 'debug' | 'error'>;\n\nexport interface LocalLlmOptions {\n /** Writable instance data dir (from `adapter.getAbsoluteInstanceDataDir()`). */\n dataDir: string;\n /** Direct URL of a GGUF model file. */\n modelUrl: string;\n systemPrompt?: string;\n maxTokens?: number;\n log: Logger;\n}\n\n/** Directory the on-demand npm install targets (`<dataDir>/node_modules/node-llama-cpp`). */\nfunction moduleDir(dataDir: string): string {\n return path.join(dataDir, 'node_modules', 'node-llama-cpp');\n}\n\n/** Local file path a model URL is cached to. */\nfunction modelPathFor(dataDir: string, url: string): string {\n const file = url.split('/').pop() || 'model.gguf';\n return path.join(dataDir, 'models', file);\n}\n\n/** True once `node-llama-cpp` has been installed into the data dir. */\nexport function isLocalLlmInstalled(dataDir: string): boolean {\n try {\n return fs.existsSync(path.join(moduleDir(dataDir), 'package.json'));\n } catch {\n return false;\n }\n}\n\n/** True once the model file has been downloaded. */\nexport function isModelDownloaded(dataDir: string, url: string): boolean {\n try {\n return fs.existsSync(modelPathFor(dataDir, url));\n } catch {\n return false;\n }\n}\n\n/**\n * Install `node-llama-cpp` (with its prebuilt native binary) into the data dir via a spawned `npm\n * install`. Streams npm output to `onProgress`. Idempotent-ish: npm is a no-op if already present.\n */\nexport function installLocalLlm(dataDir: string, onProgress: (line: string) => void): Promise<void> {\n return new Promise((resolve, reject) => {\n fs.mkdirSync(dataDir, { recursive: true });\n // A local package.json makes npm install into <dataDir>/node_modules (and not walk up).\n const pkg = path.join(dataDir, 'package.json');\n if (!fs.existsSync(pkg)) {\n fs.writeFileSync(pkg, JSON.stringify({ name: 'assistant-local-llm', private: true, version: '1.0.0' }));\n }\n const child = spawn(NPM, ['install', 'node-llama-cpp@3', '--omit=dev', '--no-audit', '--no-fund'], {\n cwd: dataDir,\n shell: process.platform === 'win32', // npm.cmd needs a shell on Windows\n });\n child.stdout.on('data', d => onProgress(String(d).trim()));\n child.stderr.on('data', d => onProgress(String(d).trim()));\n child.on('error', reject);\n child.on('close', code => (code === 0 ? resolve() : reject(new Error(`npm install exited with code ${code}`))));\n });\n}\n\n/**\n * A loaded local model. `load()` imports the on-demand module, downloads the GGUF if missing, and\n * spins up a llama context. `ask()` returns the model's answer (or {@link HANDOFF}).\n */\nexport class LocalLlm {\n private session: any = null;\n private model: any = null;\n private context: any = null;\n /** A prompt is running — the session takes one at a time. */\n private busy = false;\n\n constructor(private readonly opts: LocalLlmOptions) {}\n\n /** A prompt is running, so {@link ask} would hand off at once. */\n get running(): boolean {\n return this.busy;\n }\n\n /** Full system prompt = the strict HANDOFF rule (device work must escalate to the tool-enabled cloud LLM). */\n systemPrompt(): string {\n // The local model has NO tools and cannot touch the smart home. Anything device-related MUST hand off,\n // including control commands — otherwise a small model tends to \"helpfully\" refuse instead of escalating.\n return (\n `You are a smart-home assistant, but you have NO ability to read or control anything in the home. ` +\n `If the user wants to control or query ANYTHING in the home — e.g. turn on/off, switch, dim, ` +\n `open/close, start/stop, set, activate, or ask for the value/state/status of a light, socket, ` +\n `sensor, device, room, or the home — OR if you are unsure — you MUST reply with EXACTLY \"${HANDOFF}\" ` +\n `and nothing else. Do NOT apologise, do NOT explain, do NOT say you are an AI. ` +\n `Reply directly ONLY for general knowledge, small talk, or questions unrelated to the home.`\n );\n }\n\n async load(onProgress?: (line: string) => void): Promise<void> {\n const dir = moduleDir(this.opts.dataDir);\n if (!fs.existsSync(path.join(dir, 'package.json'))) {\n throw new Error('node-llama-cpp is not installed — run the install first');\n }\n // Resolve the package entry from the data dir and import it (v3 is ESM-only → dynamic import).\n const req = createRequire(path.join(this.opts.dataDir, 'package.json'));\n const entry = req.resolve('node-llama-cpp');\n const mod: any = await import(pathToFileURL(entry).href);\n const { getLlama, LlamaChatSession, createModelDownloader } = mod;\n\n const modelPath = modelPathFor(this.opts.dataDir, this.opts.modelUrl);\n if (!fs.existsSync(modelPath)) {\n this.opts.log.info(`Downloading local model → ${modelPath}`);\n fs.mkdirSync(path.dirname(modelPath), { recursive: true });\n const downloader = await createModelDownloader({\n modelUri: this.opts.modelUrl,\n dirPath: path.dirname(modelPath),\n fileName: path.basename(modelPath),\n onProgress: (p: { downloadedSize: number; totalSize: number }) => {\n if (p.totalSize) {\n onProgress?.(`download ${Math.round((100 * p.downloadedSize) / p.totalSize)}%`);\n }\n },\n });\n await downloader.download();\n }\n\n const llama = await getLlama();\n this.model = await llama.loadModel({ modelPath });\n // Leave one core to js-controller and the other adapters: llama.cpp takes all of them otherwise, and on\n // a Raspberry Pi the whole host — the voice server included — stalls for the length of an answer.\n const cores = typeof os.availableParallelism === 'function' ? os.availableParallelism() : os.cpus().length;\n this.context = await this.model.createContext({ threads: Math.max(1, cores - 1) });\n this.session = new LlamaChatSession({\n contextSequence: this.context.getSequence(),\n systemPrompt: this.systemPrompt(),\n });\n this.opts.log.info('Local model loaded.');\n }\n\n /**\n * Ask the local model. Returns its answer, or {@link HANDOFF} to escalate to the cloud LLM — also when\n * another question is still being answered (waiting for it would only add its run time to this one).\n * With `timeoutMs` the generation is aborted after that long and the call throws.\n */\n async ask(question: string, timeoutMs = 0): Promise<string> {\n if (!this.session || this.busy) {\n return HANDOFF;\n }\n this.busy = true;\n const abort = new AbortController();\n const timer = timeoutMs > 0 ? setTimeout(() => abort.abort(), timeoutMs) : null;\n try {\n // Reset per-question so prompts stay independent (best-effort across node-llama-cpp versions).\n try {\n this.session.setChatHistory?.([]);\n } catch {\n /* ignore */\n }\n const answer: string = await this.session.prompt(question, {\n maxTokens: this.opts.maxTokens || 256,\n signal: abort.signal,\n });\n if (abort.signal.aborted) {\n throw new Error('aborted');\n }\n return (answer || '').trim();\n } catch (e) {\n if (abort.signal.aborted) {\n throw new Error(`timed out after ${Math.round(timeoutMs / 1000)} s`);\n }\n throw e;\n } finally {\n if (timer) {\n clearTimeout(timer);\n }\n this.busy = false;\n }\n }\n\n async dispose(): Promise<void> {\n try {\n await this.context?.dispose?.();\n await this.model?.dispose?.();\n } catch {\n /* ignore */\n }\n this.session = this.context = this.model = null;\n }\n}\n\n/**\n * Refusal patterns (de/en/ru) a small tool-free model emits when it *should* have handed off — e.g.\n * \"I'm an AI and can't control real devices\". Treated as a hand-off so the tool-enabled cloud LLM runs.\n */\nconst REFUSAL = [\n /\\bhandoff\\b/i,\n // en\n /\\bi('?m| am)\\s+an?\\s+ai\\b/i,\n /\\bi\\s+(can('?t|not)|am\\s+unable\\s+to)\\b.*\\b(turn|switch|control|access|interact|device|light)\\b/i,\n // de\n /\\bich\\s+bin\\s+eine?\\s+(ki|künstliche)\\b/i,\n /\\bkann\\s+.*\\bnicht\\b.*\\b(schalten|steuern|einschalten|zugreifen|geräte?|licht)\\b/i,\n // ru (\\w/\\b are ASCII-only → use Cyrillic classes / plain substrings)\n /искусственн[а-яё]*\\s+интеллект/i,\n /не\\s+(могу|имею)/i,\n];\n\n/** True if the model's answer signals (or implies) a hand-off to the cloud LLM. */\nexport function isHandoff(answer: string): boolean {\n const trimmed = answer.trim();\n const up = trimmed.toUpperCase();\n if (up === HANDOFF || up.startsWith(`${HANDOFF} `) || up === `\"${HANDOFF}\"`) {\n return true;\n }\n return REFUSAL.some(re => re.test(trimmed));\n}\n"]}
|
package/build/lib/nlu.js
CHANGED
|
@@ -188,6 +188,39 @@ const CATEGORY_WORDS = [
|
|
|
188
188
|
];
|
|
189
189
|
/** Words that ask about every room at once ("wie warm ist es überall"). */
|
|
190
190
|
const EVERYWHERE_WORDS = ['ueberall', 'alle', 'allen', 'everywhere', 'all', 'везде', 'всюду'];
|
|
191
|
+
/** Words (stemmed) that make a question a weather question on their own. */
|
|
192
|
+
const WEATHER_WORDS = ['wetter', 'wetterbericht', 'vorhersage', 'weather', 'forecast', 'погода', 'прогноз'];
|
|
193
|
+
/**
|
|
194
|
+
* Precipitation words, matched as whole tokens: stemming "regen" would also catch "Regelung", and nobody
|
|
195
|
+
* gives a smart home the order "rain" — these only ever start a question.
|
|
196
|
+
*/
|
|
197
|
+
const RAIN_TOKENS = new Set([
|
|
198
|
+
'regnet',
|
|
199
|
+
'regnen',
|
|
200
|
+
'regen',
|
|
201
|
+
'schneit',
|
|
202
|
+
'schnee',
|
|
203
|
+
'gewitter',
|
|
204
|
+
'rain',
|
|
205
|
+
'raining',
|
|
206
|
+
'snow',
|
|
207
|
+
'snowing',
|
|
208
|
+
'дождь',
|
|
209
|
+
'дождя',
|
|
210
|
+
'снег',
|
|
211
|
+
'гроза',
|
|
212
|
+
]);
|
|
213
|
+
/** A weather question about some other place or a later day is left to the LLM and its `get_weather` tool. */
|
|
214
|
+
const WEATHER_ELSEWHERE = new Set(['in', 'fuer', 'for', 'в', 'во', 'для']);
|
|
215
|
+
const WEATHER_LATER_RE = /(?<![\p{L}])(woche\p{L}*|wochenende|week\p{L}*|недел\p{L}*|выходн\p{L}*|montag|dienstag|mittwoch|donnerstag|freitag|samstag|sonntag|monday|tuesday|wednesday|thursday|friday|saturday|sunday|понедельник|вторник|среду|четверг|пятницу|субботу|воскресенье)(?![\p{L}])/u;
|
|
216
|
+
/** Words (stemmed) that name the playback volume. */
|
|
217
|
+
const VOLUME_WORDS = ['lautstaerke', 'volume', 'громкость'];
|
|
218
|
+
const LOUDER_TOKENS = new Set(['lauter', 'louder', 'громче', 'погромче']);
|
|
219
|
+
const QUIETER_TOKENS = new Set(['leiser', 'quieter', 'softer', 'тише', 'потише']);
|
|
220
|
+
const VOLUME_MAX_TOKENS = new Set(['maximum', 'max', 'voll', 'full', 'максимум', 'полную']);
|
|
221
|
+
const VOLUME_MIN_TOKENS = new Set(['minimum', 'min', 'минимум']);
|
|
222
|
+
/** Step of a bare "lauter"/"leiser", in percentage points. */
|
|
223
|
+
const VOLUME_STEP = 10;
|
|
191
224
|
/** Words (de/en/ru, stemmed) that mean "open". */
|
|
192
225
|
const OPEN_WORDS = ['offen', 'geoeffnet', 'geoffnet', 'auf', 'open', 'открыт', 'открыта', 'открыты', 'открытые'];
|
|
193
226
|
/** Words (de/en/ru, stemmed) that make a query an on/off question ("is the light on?"). */
|
|
@@ -345,9 +378,10 @@ const LABEL_MARKERS = new Set(['an', 'fuer', 'for', 'to', 'about', 'про']);
|
|
|
345
378
|
/**
|
|
346
379
|
* Marks a command as being about an alarm / wake-up ("Wecker", "weck mich", "alarm", "будильник"). Like
|
|
347
380
|
* {@link TIMER_RE}, "alarms?"/"wecker" carry no leading boundary so compounds ("Küchenwecker") and the
|
|
348
|
-
* plural still match; the verb-prefix forms ("weck…", "разбуд…") keep a boundary.
|
|
381
|
+
* plural still match; the verb-prefix forms ("weck…", "разбуд…") keep a boundary. "буди…" and not just
|
|
382
|
+
* "буд…": that would also catch "будет" (will be), and turn "будет ли дождь?" into an alarm query.
|
|
349
383
|
*/
|
|
350
|
-
const ALARM_RE = /(?:(?<![\p{L}])(weck\p{L}*|wake
|
|
384
|
+
const ALARM_RE = /(?:(?<![\p{L}])(weck\p{L}*|wake|буди\p{L}*|разбуд\p{L}*)|alarms?|wecker)(?![\p{L}])/iu;
|
|
351
385
|
/** Hour number words → digits (no fractions — those are handled by the clock regexes). de/en/ru, 1–12. */
|
|
352
386
|
const HOUR_WORDS = {
|
|
353
387
|
ein: '1',
|
|
@@ -624,9 +658,11 @@ class Nlu {
|
|
|
624
658
|
devices;
|
|
625
659
|
/** Synonym dictionary as normalized [from, to] pairs, applied to the input before matching. */
|
|
626
660
|
aliases;
|
|
627
|
-
|
|
661
|
+
opts;
|
|
662
|
+
constructor(rooms, devices, aliases = [], opts = {}) {
|
|
628
663
|
this.rooms = rooms.filter(Boolean);
|
|
629
664
|
this.devices = devices;
|
|
665
|
+
this.opts = opts;
|
|
630
666
|
this.aliases = aliases
|
|
631
667
|
.map(a => [normalize(a.from || ''), normalize(a.to || '')])
|
|
632
668
|
.filter(([from]) => from);
|
|
@@ -739,8 +775,11 @@ class Nlu {
|
|
|
739
775
|
const roomName = this.findRoom(p.joined);
|
|
740
776
|
const device = this.findDevice(p.joined, roomName);
|
|
741
777
|
if (!device) {
|
|
742
|
-
// No device named —
|
|
743
|
-
|
|
778
|
+
// No device named — so "lauter" is meant for the speaker that heard it, "wie wird das Wetter" goes to
|
|
779
|
+
// the weather adapter, and a question may still be about a kind of measurement. A named device
|
|
780
|
+
// always wins: "Fernseher lauter" is about the TV.
|
|
781
|
+
const features = this.extractFeatures(p);
|
|
782
|
+
return this.parseVolume(p) ?? this.parseWeatherQuery(p, features) ?? this.parseCategoryQuery(p, features);
|
|
744
783
|
}
|
|
745
784
|
let features = this.extractFeatures(p);
|
|
746
785
|
if (inherited && this.namesOnly(p, device, roomName)) {
|
|
@@ -915,6 +954,9 @@ class Nlu {
|
|
|
915
954
|
*/
|
|
916
955
|
parseTimeQuery(text) {
|
|
917
956
|
const has = (w) => wordInText(w, text);
|
|
957
|
+
if (WEATHER_WORDS.some(has)) {
|
|
958
|
+
return null; // "what is the weather the day after tomorrow" asks about the weather, not the date
|
|
959
|
+
}
|
|
918
960
|
const qualifier = () => has('heute') ||
|
|
919
961
|
has('welch') ||
|
|
920
962
|
has('what') ||
|
|
@@ -982,6 +1024,83 @@ class Nlu {
|
|
|
982
1024
|
confidence: 0.8,
|
|
983
1025
|
};
|
|
984
1026
|
}
|
|
1027
|
+
/**
|
|
1028
|
+
* "Wie wird das Wetter morgen?", "regnet es heute?", "wie warm wird es übermorgen?" (de/en/ru) → the
|
|
1029
|
+
* configured weather adapter, read and spoken without an LLM. Only today, tomorrow and the day after
|
|
1030
|
+
* — other days, other places ("Wetter in Berlin") and the plain "wie warm ist es" (the house sensors,
|
|
1031
|
+
* see {@link parseCategoryQuery}) stay with their own paths.
|
|
1032
|
+
*/
|
|
1033
|
+
parseWeatherQuery(p, features) {
|
|
1034
|
+
if (!this.opts.weather) {
|
|
1035
|
+
return null;
|
|
1036
|
+
}
|
|
1037
|
+
const t = p.joined;
|
|
1038
|
+
const tokens = p.tokens;
|
|
1039
|
+
const has = (w) => p.tokenSet.has(w);
|
|
1040
|
+
let when = 'now';
|
|
1041
|
+
if (has('uebermorgen') || t.includes('day after tomorrow') || has('послезавтра')) {
|
|
1042
|
+
when = 'dayAfter';
|
|
1043
|
+
}
|
|
1044
|
+
else if (has('heute') || has('today') || has('сегодня')) {
|
|
1045
|
+
when = 'today'; // also "heute Morgen"
|
|
1046
|
+
}
|
|
1047
|
+
else if (has('tomorrow') ||
|
|
1048
|
+
has('завтра') ||
|
|
1049
|
+
tokens.some((w, i) => w === 'morgen' && tokens[i - 1] !== 'guten')) {
|
|
1050
|
+
when = 'tomorrow';
|
|
1051
|
+
}
|
|
1052
|
+
const weatherWord = WEATHER_WORDS.some(w => wordInText(w, t));
|
|
1053
|
+
const rain = tokens.some(w => RAIN_TOKENS.has(w));
|
|
1054
|
+
const temperature = (CATEGORY_WORDS.find(c => c.category === 'temperature')?.words || []).some(w => wordInText(w, t));
|
|
1055
|
+
// A temperature question is about the forecast only when it names a day; without one it is about the house.
|
|
1056
|
+
if (!weatherWord && !rain && !(temperature && features.isQuery && when !== 'now')) {
|
|
1057
|
+
return null;
|
|
1058
|
+
}
|
|
1059
|
+
// The filler list drops prepositions from the tokens, so look for them in the full text.
|
|
1060
|
+
if (WEATHER_LATER_RE.test(t) || p.norm.split(/\s+/).some(w => WEATHER_ELSEWHERE.has(w))) {
|
|
1061
|
+
return null;
|
|
1062
|
+
}
|
|
1063
|
+
return { action: 'weatherQuery', when, confidence: 0.85 };
|
|
1064
|
+
}
|
|
1065
|
+
/**
|
|
1066
|
+
* "Lautstärke 5", "Lautstärke auf 40 Prozent", "lauter", "etwas leiser", "volume 7", "громче" → the
|
|
1067
|
+
* playback volume of the satellite that heard it (or of the named room's satellite). A bare number up
|
|
1068
|
+
* to 10 is a step on a 1–10 scale, anything above is percent. "Wie ist die Lautstärke" is not a command.
|
|
1069
|
+
*/
|
|
1070
|
+
parseVolume(p) {
|
|
1071
|
+
const louder = p.tokens.some(w => LOUDER_TOKENS.has(w));
|
|
1072
|
+
const quieter = p.tokens.some(w => QUIETER_TOKENS.has(w));
|
|
1073
|
+
const volumeWord = VOLUME_WORDS.some(w => wordInText(w, p.joined));
|
|
1074
|
+
if (!louder && !quieter && !volumeWord) {
|
|
1075
|
+
return null;
|
|
1076
|
+
}
|
|
1077
|
+
const percent = /(?:prozent|percent|процент\p{L}*|%)/u.test(p.norm);
|
|
1078
|
+
// "ein bisschen lauter" is no step count — only digits and the number words from two upwards count.
|
|
1079
|
+
const num = p.tokens
|
|
1080
|
+
.map(w => (/^\d+$/.test(w) ? w : ['ein', 'eine', 'einen'].includes(w) ? '' : NUMBER_WORDS[w] || ''))
|
|
1081
|
+
.find(w => /^\d+$/.test(w));
|
|
1082
|
+
const n = num === undefined ? null : parseInt(num, 10);
|
|
1083
|
+
const scaled = n === null ? null : n <= 10 && !percent ? n * 10 : n;
|
|
1084
|
+
const room = this.findRoom(p.joined) || undefined;
|
|
1085
|
+
if (louder !== quieter) {
|
|
1086
|
+
const step = scaled ?? VOLUME_STEP;
|
|
1087
|
+
return { action: 'volumeSet', delta: louder ? step : -step, room, confidence: 0.85 };
|
|
1088
|
+
}
|
|
1089
|
+
if (!volumeWord) {
|
|
1090
|
+
return null;
|
|
1091
|
+
}
|
|
1092
|
+
let level = scaled;
|
|
1093
|
+
if (p.tokens.some(w => VOLUME_MAX_TOKENS.has(w))) {
|
|
1094
|
+
level = 100;
|
|
1095
|
+
}
|
|
1096
|
+
else if (p.tokens.some(w => VOLUME_MIN_TOKENS.has(w))) {
|
|
1097
|
+
level = VOLUME_STEP;
|
|
1098
|
+
}
|
|
1099
|
+
if (level === null) {
|
|
1100
|
+
return null;
|
|
1101
|
+
}
|
|
1102
|
+
return { action: 'volumeSet', value: Math.min(100, Math.max(0, level)), room, confidence: 0.85 };
|
|
1103
|
+
}
|
|
985
1104
|
parseWindowsOpen(text) {
|
|
986
1105
|
if (!WINDOW_WORDS.some(w => wordInText(w, text)) || !OPEN_WORDS.some(w => wordInText(w, text))) {
|
|
987
1106
|
return null;
|