iobroker.assistant 1.1.0 → 1.3.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.
Files changed (81) hide show
  1. package/README.md +74 -1
  2. package/admin/custom/.vite/manifest.json +62 -62
  3. package/admin/custom/assets/Components-x2Dfv8qE.js +2 -0
  4. package/admin/custom/assets/{_virtual_mf-localSharedImportMap___mfe_internal__ConfigCustomAssistant__mf_owner__1-A4gakomR.js → _virtual_mf-localSharedImportMap___mfe_internal__ConfigCustomAssistant__mf_owner__1-B4YbqhBb.js} +1 -1
  5. 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
  6. 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-o4EU_Ut5.js} +39 -39
  7. 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--be2hWbV.js} +1 -1
  8. package/admin/custom/assets/_virtual_mf___mfe_internal__ConfigCustomAssistant__mf_owner__163539842885036__prebuild___mf_0_iobroker_mf_1_json_mf_2_config__prebuild__-BsTz_EWE.js +122 -0
  9. package/admin/custom/assets/{hostInit-4roC4hEj.js → hostInit-Blh7a-Qe.js} +1 -1
  10. package/admin/custom/assets/{lib-CchzgTUs.js → lib-DBbNtolG.js} +1 -1
  11. package/admin/custom/assets/{mf-entry-bootstrap-0-4806bbe4.js → mf-entry-bootstrap-0-7abd30d3.js} +2 -2
  12. package/admin/custom/assets/{pendingShares-DLlv8yFY.js → pendingShares-U0D1MYK4.js} +1 -1
  13. package/admin/custom/customComponents.js +2 -2
  14. package/admin/custom/i18n/de.json +56 -0
  15. package/admin/custom/i18n/en.json +56 -0
  16. package/admin/custom/i18n/es.json +56 -0
  17. package/admin/custom/i18n/fr.json +56 -0
  18. package/admin/custom/i18n/it.json +56 -0
  19. package/admin/custom/i18n/nl.json +56 -0
  20. package/admin/custom/i18n/pl.json +56 -0
  21. package/admin/custom/i18n/pt.json +56 -0
  22. package/admin/custom/i18n/ru.json +56 -0
  23. package/admin/custom/i18n/uk.json +56 -0
  24. package/admin/custom/i18n/zh-cn.json +56 -0
  25. package/admin/custom/index.html +2 -2
  26. package/admin/custom/mf-manifest.json +1 -1
  27. package/admin/custom/mf-stats.json +1 -1
  28. package/admin/i18n/de.json +31 -4
  29. package/admin/i18n/en.json +31 -4
  30. package/admin/i18n/es.json +31 -4
  31. package/admin/i18n/fr.json +31 -4
  32. package/admin/i18n/it.json +31 -4
  33. package/admin/i18n/nl.json +31 -4
  34. package/admin/i18n/pl.json +31 -4
  35. package/admin/i18n/pt.json +31 -4
  36. package/admin/i18n/ru.json +31 -4
  37. package/admin/i18n/uk.json +31 -4
  38. package/admin/i18n/zh-cn.json +31 -4
  39. package/admin/jsonConfig.json +226 -15
  40. package/build/lib/credentials.js +11 -5
  41. package/build/lib/credentials.js.map +1 -1
  42. package/build/lib/journal.js +298 -2
  43. package/build/lib/journal.js.map +1 -1
  44. package/build/lib/llm.js +122 -7
  45. package/build/lib/llm.js.map +1 -1
  46. package/build/lib/localLlm.js +47 -10
  47. package/build/lib/localLlm.js.map +1 -1
  48. package/build/lib/nlu.js +372 -13
  49. package/build/lib/nlu.js.map +1 -1
  50. package/build/lib/piAgent.js +89 -0
  51. package/build/lib/piAgent.js.map +1 -0
  52. package/build/lib/systemAi.js +124 -0
  53. package/build/lib/systemAi.js.map +1 -0
  54. package/build/lib/tools.js +3 -0
  55. package/build/lib/tools.js.map +1 -1
  56. package/build/lib/voice/engines.js +31 -3
  57. package/build/lib/voice/engines.js.map +1 -1
  58. package/build/lib/voice/esphome.js +81 -7
  59. package/build/lib/voice/esphome.js.map +1 -1
  60. package/build/lib/voice/esphomeProto.js.map +1 -1
  61. package/build/lib/voice/mediaServer.js +29 -1
  62. package/build/lib/voice/mediaServer.js.map +1 -1
  63. package/build/lib/voice/openrouter.js +220 -0
  64. package/build/lib/voice/openrouter.js.map +1 -0
  65. package/build/lib/voice/protocol.js.map +1 -1
  66. package/build/lib/voice/voiceServer.js +81 -7
  67. package/build/lib/voice/voiceServer.js.map +1 -1
  68. package/build/lib/voice/wakeWords.js +284 -0
  69. package/build/lib/voice/wakeWords.js.map +1 -0
  70. package/build/lib/voice/wyoming.js +12 -4
  71. package/build/lib/voice/wyoming.js.map +1 -1
  72. package/build/lib/weather.js +160 -0
  73. package/build/lib/weather.js.map +1 -1
  74. package/build/main.js +1282 -126
  75. package/build/main.js.map +1 -1
  76. package/io-package.json +53 -41
  77. package/package.json +5 -5
  78. package/admin/custom/assets/Components-C6qcpaRC.js +0 -1
  79. 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
  80. /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
  81. /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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"piAgent.js","sourceRoot":"","sources":["../../src/lib/piAgent.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;GAYG;;;AA8BH,kDAaC;AAGD,wCAEC;AA9CD,8FAA8F;AACjF,QAAA,yBAAyB,GAAG,KAAK,CAAC;AAE/C,wFAAwF;AAC3E,QAAA,oBAAoB,GAAG,EAAE,CAAC;AAiBvC;;;;;;GAMG;AACH,SAAgB,mBAAmB,CAAC,QAAkB,EAAE,QAAkB;IACtE,MAAM,MAAM,GAAG,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7C,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IACvC,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,OAAO,KAAK,CAAC,MAAM,EAAE,CAAC;QAClB,MAAM,IAAI,GAAG,CAAC,GAAG,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAChD,IAAI,IAAI,CAAC,MAAM,IAAI,iCAAyB,EAAE,CAAC;YAC3C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;QAC7B,CAAC;QACD,KAAK,CAAC,GAAG,EAAE,CAAC;QACZ,OAAO,EAAE,CAAC;IACd,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,iCAAyB,CAAC,EAAE,OAAO,EAAE,CAAC;AACtF,CAAC;AAED,0FAA0F;AAC1F,SAAgB,cAAc,CAAC,MAAc;IACzC,OAAO,CAAC,MAAM,IAAI,SAAS,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,KAAK,CAAC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,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC7F,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,CAAC,QAAQ,KAAK,KAAK,EAAE,CAAC,CAAC;QAClD,CAAC;QACD,MAAM,MAAM,GAAG,OAAO,MAAM,EAAE,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC9E,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,CAAC,QAAQ,iBAAiB,CAAC,CAAC;QACvD,CAAC;QACD,OAAO,MAAM,CAAC;IAClB,CAAC;CACJ;AA1CD,wCA0CC","sourcesContent":["/**\n * The adapter `pi-agent` as the cloud tier (\"harness\"): instead of running its own tool loop, the assistant\n * hands the question to a pi-agent instance via `sendTo(<instance>, 'ask', …)`. pi-agent stores every model\n * and tool call as a checkpoint before it runs (an interrupted request continues after a restart, a write is\n * never repeated), enforces its own write permissions and confirmations, and can use the system-wide AI\n * settings of admin — none of which the built-in `LlmAgent` does.\n *\n * Each request source (satellite, `chat`, `telegram:<user>`) gets a conversation of its own in pi-agent\n * (`conversation`), so two rooms do not see each other's follow-ups. The source-independent part of the\n * prompt (style, follow-up marker, memory, device list) goes along as the `instructions` of that\n * conversation; what changes every turn (time, weather, presence) is prepended to the question.\n * pi-agent 0.3.0 and older ignore both fields and answer everything in their root conversation.\n */\n\n/** Longest instructions pi-agent accepts for a conversation; anything beyond is cut there. */\nexport const PI_AGENT_MAX_INSTRUCTIONS = 20000;\n\n/** Default for `piAgentTimeoutSec`: a tool loop with several reads can take a while. */\nexport const PI_AGENT_TIMEOUT_SEC = 60;\n\n/** The part of the adapter this module needs (kept small so tests can fake it). */\nexport interface PiAgentHost {\n sendToAsync(instance: string, command: string, message: unknown, options?: { timeout?: number }): Promise<unknown>;\n getForeignStateAsync(id: string): Promise<ioBroker.State | null | undefined>;\n}\n\nexport interface PiAgentRequest {\n /** The text pi-agent answers: context lines + the question. */\n text: string;\n /** Conversation of the caller, e.g. the satellite name. */\n conversation: string;\n /** Instructions of that conversation. */\n instructions: string;\n}\n\n/**\n * Join instruction blocks, leaving out optional ones from the end until the result fits into pi-agent's\n * limit — pi-agent would cut the text in the middle, and a half device list is worse than none.\n *\n * @param required blocks that always go along (style, follow-up marker)\n * @param optional blocks in order of importance (memory before the device list)\n */\nexport function composeInstructions(required: string[], optional: string[]): { text: string; dropped: number } {\n const blocks = [...required.filter(Boolean)];\n const extra = optional.filter(Boolean);\n let dropped = 0;\n while (extra.length) {\n const text = [...blocks, ...extra].join('\\n\\n');\n if (text.length <= PI_AGENT_MAX_INSTRUCTIONS) {\n return { text, dropped };\n }\n extra.pop();\n dropped++;\n }\n return { text: blocks.join('\\n\\n').slice(0, PI_AGENT_MAX_INSTRUCTIONS), dropped };\n}\n\n/** Conversation name for a request source; pi-agent uses \"/\" as separator in its keys. */\nexport function conversationOf(source: string): string {\n return (source || 'default').replace(/\\//g, '_').slice(0, 100);\n}\n\nexport class PiAgentHarness {\n private seq = 0;\n\n public constructor(\n private readonly host: PiAgentHost,\n /** e.g. `pi-agent.0` */\n public readonly instance: string,\n private readonly timeoutMs: number = PI_AGENT_TIMEOUT_SEC * 1000,\n ) {}\n\n /**\n * Ask pi-agent. Fails fast when the instance is not running (otherwise the caller would wait for the whole\n * timeout), and turns pi-agent's `{error}` into an exception so the caller can fall back to the local model.\n */\n public async ask(req: PiAgentRequest): Promise<string> {\n const alive = await this.host.getForeignStateAsync(`system.adapter.${this.instance}.alive`);\n if (!alive?.val) {\n throw new Error(`${this.instance} is not running`);\n }\n // Unique per request: pi-agent answers a repeated requestId only once.\n const requestId = `assistant-${Date.now().toString(36)}-${(++this.seq).toString(36)}`;\n const result = (await this.host.sendToAsync(\n this.instance,\n 'ask',\n {\n text: req.text,\n conversation: conversationOf(req.conversation),\n instructions: req.instructions,\n requestId,\n },\n { timeout: this.timeoutMs },\n )) as { answer?: unknown; error?: unknown } | null | undefined;\n if (result?.error) {\n const error = typeof result.error === 'string' ? result.error : JSON.stringify(result.error);\n throw new Error(`${this.instance}: ${error}`);\n }\n const answer = typeof result?.answer === 'string' ? result.answer.trim() : '';\n if (!answer) {\n throw new Error(`${this.instance} gave no answer`);\n }\n return answer;\n }\n}\n"]}
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SYSTEM_AI_ID = void 0;
4
+ exports.systemAiSelection = systemAiSelection;
5
+ exports.systemAiFingerprint = systemAiFingerprint;
6
+ exports.usesSystemAi = usesSystemAi;
7
+ exports.readSystemAi = readSystemAi;
8
+ exports.resolveSystemLlm = resolveSystemLlm;
9
+ exports.resolveLlm = resolveLlm;
10
+ const credentials_1 = require("./credentials");
11
+ const llm_1 = require("./llm");
12
+ /** ID of the object with the system-wide AI settings */
13
+ exports.SYSTEM_AI_ID = 'system.ai';
14
+ /** Fields of `system.ai` the assistant uses — a change of the others (like admin's chat button) needs no restart */
15
+ const USED_FIELDS = ['provider', 'model', 'credentialId', 'baseUrl'];
16
+ /** Where to set them up, for the log */
17
+ const WHERE = 'admin → System settings → tab "AI"';
18
+ function text(value) {
19
+ return typeof value === 'string' || typeof value === 'number' ? String(value).trim() : '';
20
+ }
21
+ /**
22
+ * Provider, model, credential and address of `system.ai`; `null` if it names no provider the assistant knows.
23
+ *
24
+ * @param native the `native` of `system.ai`
25
+ */
26
+ function systemAiSelection(native) {
27
+ const provider = text(native?.provider);
28
+ if (!provider || !Object.prototype.hasOwnProperty.call(llm_1.PROVIDER_PRESETS, provider)) {
29
+ return null;
30
+ }
31
+ return {
32
+ provider: provider,
33
+ model: text(native?.model),
34
+ credentialId: text(native?.credentialId),
35
+ baseUrl: provider === 'custom' ? text(native?.baseUrl) : '',
36
+ };
37
+ }
38
+ /**
39
+ * What of `system.ai` the assistant depends on; a change of it restarts the instance.
40
+ *
41
+ * @param native the `native` of `system.ai`
42
+ */
43
+ function systemAiFingerprint(native) {
44
+ return JSON.stringify(USED_FIELDS.map(field => text(native?.[field])));
45
+ }
46
+ /**
47
+ * Whether the instance takes the cloud LLM from `system.ai`: provider `system`, or a provider that needs a key
48
+ * without one (neither in the adapter nor a credential) — the custom endpoint often works without a key.
49
+ *
50
+ * @param cfg the configuration of the instance
51
+ */
52
+ function usesSystemAi(cfg) {
53
+ const provider = cfg.provider || 'openai';
54
+ if (provider === 'system') {
55
+ return true;
56
+ }
57
+ if (provider === 'none' || provider === 'pi-agent' || provider === 'custom') {
58
+ return false;
59
+ }
60
+ return cfg.credentialType === 'manager' ? !text(cfg.credentialIdApiKey) : !text(cfg.apiKey);
61
+ }
62
+ /**
63
+ * Read `system.ai`; a missing object (or one the adapter may not read) gives `undefined`.
64
+ *
65
+ * @param adapter the adapter
66
+ */
67
+ async function readSystemAi(adapter) {
68
+ try {
69
+ const obj = await adapter.getForeignObjectAsync(exports.SYSTEM_AI_ID);
70
+ return obj?.native;
71
+ }
72
+ catch {
73
+ return undefined;
74
+ }
75
+ }
76
+ /**
77
+ * The cloud LLM of `system.ai`, or why there is none.
78
+ *
79
+ * @param adapter the adapter (reads the credential)
80
+ * @param native the `native` of `system.ai`
81
+ * @param explicit the provider `system` was chosen (otherwise the instance only has no key of its own)
82
+ */
83
+ async function resolveSystemLlm(adapter, native, explicit) {
84
+ const sel = systemAiSelection(native);
85
+ if (!sel) {
86
+ return {
87
+ error: explicit
88
+ ? `The system-wide AI settings have no provider — set one up in ${WHERE} or choose a provider here.`
89
+ : `No API key configured — enter one in the adapter settings or set up the system-wide AI settings in ${WHERE}.`,
90
+ };
91
+ }
92
+ if (sel.provider === 'custom' && !sel.baseUrl) {
93
+ return { error: `The system-wide AI settings use an OpenAI-compatible server without address (${WHERE}).` };
94
+ }
95
+ const apiKey = (0, llm_1.effectiveApiKey)(sel.provider, await (0, credentials_1.readCentralKey)(adapter, sel.credentialId));
96
+ if (!apiKey) {
97
+ return { error: `The system-wide AI settings have no API key for ${sel.provider} (${WHERE}).` };
98
+ }
99
+ const preset = (0, llm_1.resolveProvider)(sel.provider, sel.baseUrl);
100
+ return {
101
+ provider: sel.provider,
102
+ apiKey,
103
+ model: sel.model || preset.defaultModel,
104
+ baseUrl: preset.baseUrl,
105
+ system: true,
106
+ };
107
+ }
108
+ /**
109
+ * The cloud LLM of the instance: its own provider and key, or those of `system.ai` (see {@link usesSystemAi}).
110
+ * Not for the providers `none` and `pi-agent`, which have no LLM of their own.
111
+ *
112
+ * @param adapter the adapter
113
+ * @param cfg the configuration of the instance
114
+ */
115
+ async function resolveLlm(adapter, cfg) {
116
+ if (usesSystemAi(cfg)) {
117
+ return resolveSystemLlm(adapter, await readSystemAi(adapter), cfg.provider === 'system');
118
+ }
119
+ const provider = (cfg.provider || 'openai');
120
+ const apiKey = (0, llm_1.effectiveApiKey)(provider, await (0, credentials_1.resolveApiKey)(adapter, cfg));
121
+ const preset = (0, llm_1.resolveProvider)(provider, cfg.baseUrl);
122
+ return { provider, apiKey, model: cfg.model || preset.defaultModel, baseUrl: preset.baseUrl, system: false };
123
+ }
124
+ //# sourceMappingURL=systemAi.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"systemAi.js","sourceRoot":"","sources":["../../src/lib/systemAi.ts"],"names":[],"mappings":";;;AAmDA,8CAWC;AAOD,kDAEC;AAQD,oCAWC;AAOD,oCAOC;AASD,4CA4BC;AASD,gCAWC;AAtJD,+CAA8D;AAC9D,+BAA0F;AAE1F,wDAAwD;AAC3C,QAAA,YAAY,GAAG,WAAW,CAAC;AAExC,oHAAoH;AACpH,MAAM,WAAW,GAAG,CAAC,UAAU,EAAE,OAAO,EAAE,cAAc,EAAE,SAAS,CAAU,CAAC;AAE9E,wCAAwC;AACxC,MAAM,KAAK,GAAG,oCAAoC,CAAC;AAqBnD,SAAS,IAAI,CAAC,KAAc;IACxB,OAAO,OAAO,KAAK,KAAK,QAAQ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EAAC,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;IAC1D,OAAO;QACH,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,MAAM;QACN,KAAK,EAAE,GAAG,CAAC,KAAK,IAAI,MAAM,CAAC,YAAY;QACvC,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,MAAM,EAAE,IAAI;KACf,CAAC;AACN,CAAC;AAED;;;;;;GAMG;AACI,KAAK,UAAU,UAAU,CAC5B,OAAyB,EACzB,GAAkB;IAElB,IAAI,YAAY,CAAC,GAAG,CAAC,EAAE,CAAC;QACpB,OAAO,gBAAgB,CAAC,OAAO,EAAE,MAAM,YAAY,CAAC,OAAO,CAAC,EAAE,GAAG,CAAC,QAAQ,KAAK,QAAQ,CAAC,CAAC;IAC7F,CAAC;IACD,MAAM,QAAQ,GAAG,CAAC,GAAG,CAAC,QAAQ,IAAI,QAAQ,CAAa,CAAC;IACxD,MAAM,MAAM,GAAG,IAAA,qBAAe,EAAC,QAAQ,EAAE,MAAM,IAAA,2BAAa,EAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;IAC5E,MAAM,MAAM,GAAG,IAAA,qBAAe,EAAC,QAAQ,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;IACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,IAAI,MAAM,CAAC,YAAY,EAAE,OAAO,EAAE,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AACjH,CAAC","sourcesContent":["/**\n * The system-wide AI settings of admin (`system.ai`, System settings → tab \"AI\") as the source of the\n * cloud LLM: provider, model, key — always an entry of the central credential store — and the address of\n * a custom endpoint.\n *\n * Used with the provider `system`, and by an instance that has no key of its own: an assistant nobody\n * configured takes what was set up once for the whole system (the same rule as `@iobroker/ai-core`'s\n * `usesSystemAiSettings`). The maximum answer length stays the assistant's own (`maxTokens`), because a\n * spoken answer needs far fewer tokens than the admin chat.\n */\nimport type { AdapterConfig } from '../types';\nimport { readCentralKey, resolveApiKey } from './credentials';\nimport { PROVIDER_PRESETS, effectiveApiKey, resolveProvider, type Provider } from './llm';\n\n/** ID of the object with the system-wide AI settings */\nexport const SYSTEM_AI_ID = 'system.ai';\n\n/** Fields of `system.ai` the assistant uses — a change of the others (like admin's chat button) needs no restart */\nconst USED_FIELDS = ['provider', 'model', 'credentialId', 'baseUrl'] as const;\n\n/** Where to set them up, for the log */\nconst WHERE = 'admin → System settings → tab \"AI\"';\n\n/** Provider, model, credential and address out of `system.ai` */\nexport interface SystemAiSelection {\n provider: Provider;\n model: string;\n credentialId: string;\n /** Only for `custom` */\n baseUrl: string;\n}\n\n/** The cloud LLM the instance works with, whether configured here or in `system.ai` */\nexport interface ResolvedLlm {\n provider: Provider;\n apiKey: string;\n model: string;\n baseUrl: string;\n /** Taken from `system.ai` */\n system: boolean;\n}\n\nfunction text(value: unknown): string {\n return typeof value === 'string' || typeof value === 'number' ? String(value).trim() : '';\n}\n\n/**\n * Provider, model, credential and address of `system.ai`; `null` if it names no provider the assistant knows.\n *\n * @param native the `native` of `system.ai`\n */\nexport function systemAiSelection(native: Record<string, unknown> | null | undefined): SystemAiSelection | null {\n const provider = text(native?.provider);\n if (!provider || !Object.prototype.hasOwnProperty.call(PROVIDER_PRESETS, provider)) {\n return null;\n }\n return {\n provider: provider as Provider,\n model: text(native?.model),\n credentialId: text(native?.credentialId),\n baseUrl: provider === 'custom' ? text(native?.baseUrl) : '',\n };\n}\n\n/**\n * What of `system.ai` the assistant depends on; a change of it restarts the instance.\n *\n * @param native the `native` of `system.ai`\n */\nexport function systemAiFingerprint(native: Record<string, unknown> | null | undefined): string {\n return JSON.stringify(USED_FIELDS.map(field => text(native?.[field])));\n}\n\n/**\n * Whether the instance takes the cloud LLM from `system.ai`: provider `system`, or a provider that needs a key\n * without one (neither in the adapter nor a credential) — the custom endpoint often works without a key.\n *\n * @param cfg the configuration of the instance\n */\nexport function usesSystemAi(\n cfg: Pick<AdapterConfig, 'provider' | 'credentialType' | 'apiKey' | 'credentialIdApiKey'>,\n): boolean {\n const provider = cfg.provider || 'openai';\n if (provider === 'system') {\n return true;\n }\n if (provider === 'none' || provider === 'pi-agent' || provider === 'custom') {\n return false;\n }\n return cfg.credentialType === 'manager' ? !text(cfg.credentialIdApiKey) : !text(cfg.apiKey);\n}\n\n/**\n * Read `system.ai`; a missing object (or one the adapter may not read) gives `undefined`.\n *\n * @param adapter the adapter\n */\nexport async function readSystemAi(adapter: ioBroker.Adapter): Promise<Record<string, unknown> | undefined> {\n try {\n const obj = await adapter.getForeignObjectAsync(SYSTEM_AI_ID);\n return obj?.native;\n } catch {\n return undefined;\n }\n}\n\n/**\n * The cloud LLM of `system.ai`, or why there is none.\n *\n * @param adapter the adapter (reads the credential)\n * @param native the `native` of `system.ai`\n * @param explicit the provider `system` was chosen (otherwise the instance only has no key of its own)\n */\nexport async function resolveSystemLlm(\n adapter: ioBroker.Adapter,\n native: Record<string, unknown> | null | undefined,\n explicit: boolean,\n): Promise<ResolvedLlm | { error: string }> {\n const sel = systemAiSelection(native);\n if (!sel) {\n return {\n error: explicit\n ? `The system-wide AI settings have no provider — set one up in ${WHERE} or choose a provider here.`\n : `No API key configured — enter one in the adapter settings or set up the system-wide AI settings in ${WHERE}.`,\n };\n }\n if (sel.provider === 'custom' && !sel.baseUrl) {\n return { error: `The system-wide AI settings use an OpenAI-compatible server without address (${WHERE}).` };\n }\n const apiKey = effectiveApiKey(sel.provider, await readCentralKey(adapter, sel.credentialId));\n if (!apiKey) {\n return { error: `The system-wide AI settings have no API key for ${sel.provider} (${WHERE}).` };\n }\n const preset = resolveProvider(sel.provider, sel.baseUrl);\n return {\n provider: sel.provider,\n apiKey,\n model: sel.model || preset.defaultModel,\n baseUrl: preset.baseUrl,\n system: true,\n };\n}\n\n/**\n * The cloud LLM of the instance: its own provider and key, or those of `system.ai` (see {@link usesSystemAi}).\n * Not for the providers `none` and `pi-agent`, which have no LLM of their own.\n *\n * @param adapter the adapter\n * @param cfg the configuration of the instance\n */\nexport async function resolveLlm(\n adapter: ioBroker.Adapter,\n cfg: AdapterConfig,\n): Promise<ResolvedLlm | { error: string }> {\n if (usesSystemAi(cfg)) {\n return resolveSystemLlm(adapter, await readSystemAi(adapter), cfg.provider === 'system');\n }\n const provider = (cfg.provider || 'openai') as Provider;\n const apiKey = effectiveApiKey(provider, await resolveApiKey(adapter, cfg));\n const preset = resolveProvider(provider, cfg.baseUrl);\n return { provider, apiKey, model: cfg.model || preset.defaultModel, baseUrl: preset.baseUrl, system: false };\n}\n"]}
@@ -81,8 +81,11 @@ class ListCache {
81
81
  this.store.set(key, { at: now, val });
82
82
  return val;
83
83
  }
84
+ /** Called after every {@link clear}, so caches built on top of these listings can drop theirs too. */
85
+ onClear = null;
84
86
  clear() {
85
87
  this.store.clear();
88
+ this.onClear?.();
86
89
  }
87
90
  }
88
91
  exports.ListCache = ListCache;
@@ -1 +1 @@
1
- {"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/lib/tools.ts"],"names":[],"mappings":";;;AAmCA,wCAIC;AAOD,8BAEC;AAsFD,sCAwBC;AAsKD,sCAwDC;AA1VD,8DAA8D;AAC9D,SAAgB,cAAc,CAAC,GAAmB;IAC9C,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC;SACnC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC;SACpB,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,SAAgB,SAAS,CAAC,QAAkB;IACxC,OAAO,CAAC,GAAG,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AACzC,CAAC;AAcD,0EAA0E;AAC1E,MAAM,aAAa,GAAiC;IAChD,UAAU,EAAE,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AAC,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;IAC5G,CAAC;IAED,iGAAiG;IACjG,iGAAiG;IACjG,KAAK,CAAC,IAAI,CAAC;QACP,IAAI,EAAE,sBAAsB;QAC5B,WAAW,EACP,gSAAgS;QACpS,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE,oBAAoB,EAAE,KAAK,EAAE;QAC3E,GAAG,EAAE,GAAoB,EAAE;YACvB,KAAK,CAAC,KAAK,EAAE,CAAC;YACd,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;QAClF,CAAC;KACJ,CAAC,CAAC;IAEH,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AACpC,CAAC","sourcesContent":["import type { InProcessMcp } from '@iobroker/mcp-server';\n\n/** An LLM-callable tool: name, description, JSON-schema parameters, and a runner. */\nexport interface Tool {\n name: string;\n description: string;\n /** JSON schema for the tool arguments. */\n parameters: Record<string, unknown>;\n run(args: Record<string, unknown>): Promise<unknown>;\n}\n\n/** Per-device read/write override, keyed by {@link deviceKey}. Absent → governed by the coarse toggles. */\nexport type DeviceAcl = Record<string, { read: boolean; write: boolean }>;\n\n/** The access toggles from the adapter config that gate which MCP tools the LLM may use. */\nexport interface ToolAccess {\n allowWriteStates: boolean;\n allowObjectChange: boolean;\n readObjects: 'devices' | 'all';\n allowReadLogs: boolean;\n allowWriteLogs: boolean;\n allowHistory: boolean;\n allowFiles: boolean;\n allowSystemInfo: boolean;\n deviceAcl: DeviceAcl;\n}\n\n/** One `list_devices` device entry (as far as we care: its controls' state ids). */\nexport interface McpDeviceEntry {\n deviceName?: string;\n deviceType?: string;\n controls?: Record<string, { stateId?: string; writable?: boolean }>;\n}\n\n/** All control state ids of a `list_devices` device entry. */\nexport function deviceStateIds(dev: McpDeviceEntry): string[] {\n return Object.values(dev.controls || {})\n .map(c => c?.stateId)\n .filter((x): x is string => !!x);\n}\n\n/**\n * Stable per-device key used by both the backend write-guard and the admin ACL editor. Keyed by the\n * device's primary (lexicographically first) control state id — unique per device and independent of the\n * (often non-unique) type-detector name, so two \"SET\" controls no longer collide on one ACL entry.\n */\nexport function deviceKey(stateIds: string[]): string {\n return [...stateIds].sort()[0] || '';\n}\n\ntype ToolCategory =\n | 'stateRead'\n | 'stateWrite'\n | 'devices'\n | 'objectReadAll'\n | 'logRead'\n | 'logWrite'\n | 'history'\n | 'files'\n | 'system'\n | 'objectChange';\n\n/** Maps every known `@iobroker/mcp-server` tool to an access category. */\nconst TOOL_CATEGORY: Record<string, ToolCategory> = {\n get_states: 'stateRead',\n list_devices: 'devices',\n list_rooms: 'devices',\n list_functions: 'devices',\n set_state: 'stateWrite',\n set_states: 'stateWrite',\n get_object: 'objectReadAll',\n search_objects: 'objectReadAll',\n get_logs: 'logRead',\n write_log: 'logWrite',\n history_query: 'history',\n read_file: 'files',\n list_files: 'files',\n file_exists: 'files',\n system_info: 'system',\n list_hosts: 'system',\n list_instances: 'system',\n list_adapters: 'system',\n search_adapter_repository: 'system',\n ping_host: 'system',\n set_object: 'objectChange',\n delete_object: 'objectChange',\n create_state: 'objectChange',\n create_scene: 'objectChange',\n write_file: 'objectChange',\n delete_file: 'objectChange',\n rename_file: 'objectChange',\n mkdir: 'objectChange',\n};\n\n/**\n * Read-only, system-wide-scan tools whose results are stable enough to serve from a short-TTL cache.\n * Each of these runs the ioBroker type-detector over *all* objects (`list_devices` alone does five\n * full `getObjectView` scans), so repeat calls inside one conversation are the main avoidable cost.\n */\nconst CACHEABLE_TOOLS = new Set(['list_devices', 'list_rooms', 'list_functions']);\n\n/** Default lifetime of a cached device/room/function listing (ms). */\nexport const LIST_CACHE_TTL_MS = 30_000;\n\n/**\n * Tiny time-based cache: memoizes a tool result per (tool, args) for {@link ttlMs}. Not invalidated on\n * object changes — a freshly added device appears after at most one TTL, which is fine for an assistant.\n * Call {@link ListCache.clear} to force a refresh (e.g. from an `objectChange` subscription).\n */\nexport class ListCache {\n private readonly store = new Map<string, { at: number; val: string }>();\n\n constructor(\n private readonly ttlMs: number = LIST_CACHE_TTL_MS,\n private readonly now: () => number = Date.now,\n ) {}\n\n async run(key: string, produce: () => Promise<string>): Promise<string> {\n const hit = this.store.get(key);\n const now = this.now();\n if (hit && now - hit.at < this.ttlMs) {\n return hit.val;\n }\n const val = await produce();\n this.store.set(key, { at: now, val });\n return val;\n }\n\n clear(): void {\n this.store.clear();\n }\n}\n\n/** Decide whether a tool is allowed by the current access configuration. Unknown tools are denied. */\nexport function isToolAllowed(name: string, access: ToolAccess): boolean {\n switch (TOOL_CATEGORY[name]) {\n case 'stateRead':\n case 'devices':\n return true; // reading device/state values is always allowed\n case 'stateWrite':\n return access.allowWriteStates;\n case 'objectReadAll':\n return access.readObjects === 'all';\n case 'logRead':\n return access.allowReadLogs;\n case 'logWrite':\n return access.allowWriteLogs;\n case 'history':\n return access.allowHistory;\n case 'files':\n return access.allowFiles;\n case 'system':\n return access.allowSystemInfo;\n case 'objectChange':\n return access.allowObjectChange;\n default:\n return false; // unknown/unmapped tool → deny (safe default for an access list)\n }\n}\n\n/** Device types that default to write-disabled (must be explicitly allowed) for safety, e.g. door locks. */\nconst DEFAULT_WRITE_FALSE_TYPES = new Set(['lock']);\n\n/**\n * Build the stateId → deviceKey map and the set of keys that default to write-disabled (e.g. locks),\n * from the MCP `list_devices` tool (best-effort; empty on any error).\n */\nasync function loadStateDeviceMap(\n mcp: InProcessMcp,\n): Promise<{ stateToKey: Map<string, string>; defaultWriteFalse: Set<string> }> {\n const stateToKey = new Map<string, string>();\n const defaultWriteFalse = new Set<string>();\n try {\n const res = await mcp.callTool('list_devices', {});\n const parsed = JSON.parse(res.text) as {\n data?: { rooms?: { devicesInRoom?: McpDeviceEntry[] }[] };\n };\n for (const room of parsed.data?.rooms || []) {\n for (const dev of room.devicesInRoom || []) {\n const ids = deviceStateIds(dev);\n const key = deviceKey(ids);\n // Every control of the device shares the device's key, so the write-guard resolves any\n // of its states to the same ACL entry.\n for (const id of ids) {\n stateToKey.set(id, key);\n }\n if (DEFAULT_WRITE_FALSE_TYPES.has(String(dev.deviceType ?? ''))) {\n defaultWriteFalse.add(key);\n }\n }\n }\n } catch {\n /* no device map → no per-device enforcement */\n }\n return { stateToKey, defaultWriteFalse };\n}\n\n/** JSON shape of a `list_devices` result, for post-processing the device names the LLM sees. */\ninterface ListDevicesResult {\n data?: { rooms?: { devicesInRoom?: McpDeviceEntry[] }[] };\n}\n\n/** Device types dropped from `list_devices` so the LLM never treats them as controllable devices. */\nconst HIDDEN_LLM_TYPES = new Set(['button']);\n\n/**\n * Post-process a `list_devices` result for the LLM: drop hidden types (write-only buttons) and devices\n * the user set to read-disabled (`acl[key].read === false`), and — when a resolver is given — rewrite\n * each `deviceName` to the friendly name (smartName → parent name). Original text on any parse problem.\n */\nasync function postProcessListDevices(\n text: string,\n acl: DeviceAcl,\n resolveName?: (stateId: string, fallback: string) => Promise<string>,\n): Promise<string> {\n let parsed: ListDevicesResult;\n try {\n parsed = JSON.parse(text) as ListDevicesResult;\n } catch {\n return text;\n }\n const rooms = parsed.data?.rooms;\n if (!Array.isArray(rooms)) {\n return text;\n }\n for (const room of rooms) {\n const kept = (room.devicesInRoom || []).filter(d => {\n if (HIDDEN_LLM_TYPES.has(String(d.deviceType ?? ''))) {\n return false;\n }\n const key = deviceKey(deviceStateIds(d));\n return !(key && acl[key]?.read === false); // hide read-disabled devices from the LLM\n });\n const trimmed: McpDeviceEntry[] = [];\n for (const dev of kept) {\n const ids = deviceStateIds(dev);\n const name =\n resolveName && ids.length\n ? (await resolveName(deviceKey(ids), String(dev.deviceName ?? ''))) || dev.deviceName\n : dev.deviceName;\n // Keep only what the LLM needs to act (name/type + controlType→{stateId,writable}); drop the\n // verbose role/unit/min/max/ioBrokerValueType/friendlyDeviceNames to cut tokens & latency.\n const controls: Record<string, { stateId?: string; writable?: boolean }> = {};\n for (const [ct, c] of Object.entries(dev.controls || {})) {\n controls[ct] = { stateId: c?.stateId, writable: c?.writable };\n }\n trimmed.push({ deviceName: name, deviceType: dev.deviceType, controls });\n }\n room.devicesInRoom = trimmed;\n }\n return JSON.stringify(parsed);\n}\n\n/** Wrap `get_states` so it drops values of devices the user set to read-disabled. */\nfunction guardRead(\n baseRun: (args: Record<string, unknown>) => Promise<unknown>,\n stateToKey: Map<string, string>,\n acl: DeviceAcl,\n): (args: Record<string, unknown>) => Promise<unknown> {\n return async (args: Record<string, unknown>): Promise<unknown> => {\n const text = (await baseRun(args)) as string;\n let parsed: { data?: { states?: { id?: string }[] } };\n try {\n parsed = JSON.parse(text) as { data?: { states?: { id?: string }[] } };\n } catch {\n return text;\n }\n const states = parsed.data?.states;\n if (Array.isArray(states) && parsed.data) {\n parsed.data.states = states.filter(s => {\n const key = s?.id ? stateToKey.get(s.id) : undefined;\n return !(key && acl[key]?.read === false);\n });\n }\n return JSON.stringify(parsed);\n };\n}\n\n/**\n * Wrap a state-writing tool so it refuses writes to states of devices the user marked read-only.\n * Write is allowed when the explicit ACL entry says so; without an entry, the device type's default\n * applies (write-disabled for {@link DEFAULT_WRITE_FALSE_TYPES} like locks, allowed otherwise).\n */\nfunction guardWrite(\n name: string,\n baseRun: (args: Record<string, unknown>) => Promise<unknown>,\n stateToKey: Map<string, string>,\n acl: DeviceAcl,\n defaultWriteFalse: Set<string>,\n): (args: Record<string, unknown>) => Promise<unknown> {\n return async (args: Record<string, unknown>): Promise<unknown> => {\n const ids =\n name === 'set_states'\n ? ((args.states as { id?: string }[] | undefined) || []).map(s => s?.id).filter((x): x is string => !!x)\n : typeof args.id === 'string'\n ? [args.id]\n : [];\n for (const id of ids) {\n const key = stateToKey.get(id);\n if (!key) {\n continue;\n }\n const entry = acl[key];\n const writeAllowed = entry ? entry.write !== false : !defaultWriteFalse.has(key);\n if (!writeAllowed) {\n return JSON.stringify({\n ok: false,\n error: `Writing \"${id}\" is not allowed: device \"${key}\" is read-only in the assistant settings.`,\n });\n }\n }\n return baseRun(args);\n };\n}\n\n/**\n * Build the LLM tool set from the in-process ioBroker MCP server, filtered by the access config and\n * enforcing the per-device write ACL.\n *\n * All tools come from `@iobroker/mcp-server` — nothing is duplicated here. Gating layers:\n * 1. `createInProcessMcp({ allowSetState, allowObjectChange })` — which tools exist at all.\n * 2. `isToolAllowed()` — fine-grained category toggles (logs/history/files/object-read scope/…).\n * 3. `guardWrite()` — per-device write ACL (`access.deviceAcl`), e.g. keep a lock read-only.\n */\nexport async function buildMcpTools(\n mcp: InProcessMcp,\n access: ToolAccess,\n cache: ListCache = new ListCache(),\n resolveName?: (stateId: string, fallback: string) => Promise<string>,\n): Promise<{ tools: Tool[]; denied: string[]; cache: ListCache }> {\n const infos = await mcp.listTools();\n const acl = access.deviceAcl || {};\n // Write guard: whenever writing is enabled (explicit ACL + type defaults like locks).\n const enforceWrite = access.allowWriteStates;\n // Read guard: whenever any device is set read-disabled (hide it from list_devices + get_states).\n const enforceRead = Object.values(acl).some(a => a && a.read === false);\n const { stateToKey, defaultWriteFalse } =\n enforceWrite || enforceRead\n ? await loadStateDeviceMap(mcp)\n : { stateToKey: new Map<string, string>(), defaultWriteFalse: new Set<string>() };\n\n const tools: Tool[] = [];\n const denied: string[] = [];\n for (const info of infos) {\n if (!isToolAllowed(info.name, access)) {\n denied.push(info.name);\n continue;\n }\n const callTool = async (args: Record<string, unknown>): Promise<string> => {\n const text = (await mcp.callTool(info.name, args)).text;\n // For the LLM: drop buttons + read-disabled devices, and use friendly names.\n return info.name === 'list_devices' ? postProcessListDevices(text, acl, resolveName) : text;\n };\n // Serve stable, expensive read-only listings from the short-TTL cache; everything else runs live.\n const baseRun: (args: Record<string, unknown>) => Promise<unknown> = CACHEABLE_TOOLS.has(info.name)\n ? (args): Promise<string> => cache.run(`${info.name}:${JSON.stringify(args ?? {})}`, () => callTool(args))\n : callTool;\n let run = baseRun;\n if (enforceWrite && (info.name === 'set_state' || info.name === 'set_states')) {\n run = guardWrite(info.name, baseRun, stateToKey, acl, defaultWriteFalse);\n } else if (enforceRead && info.name === 'get_states') {\n run = guardRead(baseRun, stateToKey, acl);\n }\n tools.push({ name: info.name, description: info.description || '', parameters: info.inputSchema, run });\n }\n\n // Synthetic (non-MCP) tool: lets the model bust the device/room cache and re-scan. Useful when a\n // device the user mentions is missing from list_devices (stale cache) or was just added/renamed.\n tools.push({\n name: 'refresh_device_cache',\n description:\n 'Clear the cached device, room and function listings so the next list_devices, list_rooms or list_functions call performs a fresh scan. Call this when a device or room the user mentions is not in the list, or when the user says a device was just added, renamed or reassigned, then retry.',\n parameters: { type: 'object', properties: {}, additionalProperties: false },\n run: (): Promise<string> => {\n cache.clear();\n return Promise.resolve(JSON.stringify({ ok: true, data: { cleared: true } }));\n },\n });\n\n return { tools, denied, cache };\n}\n"]}
1
+ {"version":3,"file":"tools.js","sourceRoot":"","sources":["../../src/lib/tools.ts"],"names":[],"mappings":";;;AAmCA,wCAIC;AAOD,8BAEC;AA0FD,sCAwBC;AAsKD,sCAwDC;AA9VD,8DAA8D;AAC9D,SAAgB,cAAc,CAAC,GAAmB;IAC9C,OAAO,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,QAAQ,IAAI,EAAE,CAAC;SACnC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC;SACpB,MAAM,CAAC,CAAC,CAAC,EAAe,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,SAAgB,SAAS,CAAC,QAAkB;IACxC,OAAO,CAAC,GAAG,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AACzC,CAAC;AAcD,0EAA0E;AAC1E,MAAM,aAAa,GAAiC;IAChD,UAAU,EAAE,WAAW;IACvB,YAAY,EAAE,SAAS;IACvB,UAAU,EA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,WAAW,IAAI,EAAE,EAAE,UAAU,EAAE,IAAI,CAAC,WAAW,EAAE,GAAG,EAAE,CAAC,CAAC;IAC5G,CAAC;IAED,iGAAiG;IACjG,iGAAiG;IACjG,KAAK,CAAC,IAAI,CAAC;QACP,IAAI,EAAE,sBAAsB;QAC5B,WAAW,EACP,gSAAgS;QACpS,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,EAAE,oBAAoB,EAAE,KAAK,EAAE;QAC3E,GAAG,EAAE,GAAoB,EAAE;YACvB,KAAK,CAAC,KAAK,EAAE,CAAC;YACd,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC;QAClF,CAAC;KACJ,CAAC,CAAC;IAEH,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;AACpC,CAAC","sourcesContent":["import type { InProcessMcp } from '@iobroker/mcp-server';\n\n/** An LLM-callable tool: name, description, JSON-schema parameters, and a runner. */\nexport interface Tool {\n name: string;\n description: string;\n /** JSON schema for the tool arguments. */\n parameters: Record<string, unknown>;\n run(args: Record<string, unknown>): Promise<unknown>;\n}\n\n/** Per-device read/write override, keyed by {@link deviceKey}. Absent → governed by the coarse toggles. */\nexport type DeviceAcl = Record<string, { read: boolean; write: boolean }>;\n\n/** The access toggles from the adapter config that gate which MCP tools the LLM may use. */\nexport interface ToolAccess {\n allowWriteStates: boolean;\n allowObjectChange: boolean;\n readObjects: 'devices' | 'all';\n allowReadLogs: boolean;\n allowWriteLogs: boolean;\n allowHistory: boolean;\n allowFiles: boolean;\n allowSystemInfo: boolean;\n deviceAcl: DeviceAcl;\n}\n\n/** One `list_devices` device entry (as far as we care: its controls' state ids). */\nexport interface McpDeviceEntry {\n deviceName?: string;\n deviceType?: string;\n controls?: Record<string, { stateId?: string; writable?: boolean }>;\n}\n\n/** All control state ids of a `list_devices` device entry. */\nexport function deviceStateIds(dev: McpDeviceEntry): string[] {\n return Object.values(dev.controls || {})\n .map(c => c?.stateId)\n .filter((x): x is string => !!x);\n}\n\n/**\n * Stable per-device key used by both the backend write-guard and the admin ACL editor. Keyed by the\n * device's primary (lexicographically first) control state id — unique per device and independent of the\n * (often non-unique) type-detector name, so two \"SET\" controls no longer collide on one ACL entry.\n */\nexport function deviceKey(stateIds: string[]): string {\n return [...stateIds].sort()[0] || '';\n}\n\ntype ToolCategory =\n | 'stateRead'\n | 'stateWrite'\n | 'devices'\n | 'objectReadAll'\n | 'logRead'\n | 'logWrite'\n | 'history'\n | 'files'\n | 'system'\n | 'objectChange';\n\n/** Maps every known `@iobroker/mcp-server` tool to an access category. */\nconst TOOL_CATEGORY: Record<string, ToolCategory> = {\n get_states: 'stateRead',\n list_devices: 'devices',\n list_rooms: 'devices',\n list_functions: 'devices',\n set_state: 'stateWrite',\n set_states: 'stateWrite',\n get_object: 'objectReadAll',\n search_objects: 'objectReadAll',\n get_logs: 'logRead',\n write_log: 'logWrite',\n history_query: 'history',\n read_file: 'files',\n list_files: 'files',\n file_exists: 'files',\n system_info: 'system',\n list_hosts: 'system',\n list_instances: 'system',\n list_adapters: 'system',\n search_adapter_repository: 'system',\n ping_host: 'system',\n set_object: 'objectChange',\n delete_object: 'objectChange',\n create_state: 'objectChange',\n create_scene: 'objectChange',\n write_file: 'objectChange',\n delete_file: 'objectChange',\n rename_file: 'objectChange',\n mkdir: 'objectChange',\n};\n\n/**\n * Read-only, system-wide-scan tools whose results are stable enough to serve from a short-TTL cache.\n * Each of these runs the ioBroker type-detector over *all* objects (`list_devices` alone does five\n * full `getObjectView` scans), so repeat calls inside one conversation are the main avoidable cost.\n */\nconst CACHEABLE_TOOLS = new Set(['list_devices', 'list_rooms', 'list_functions']);\n\n/** Default lifetime of a cached device/room/function listing (ms). */\nexport const LIST_CACHE_TTL_MS = 30_000;\n\n/**\n * Tiny time-based cache: memoizes a tool result per (tool, args) for {@link ttlMs}. Not invalidated on\n * object changes — a freshly added device appears after at most one TTL, which is fine for an assistant.\n * Call {@link ListCache.clear} to force a refresh (e.g. from an `objectChange` subscription).\n */\nexport class ListCache {\n private readonly store = new Map<string, { at: number; val: string }>();\n\n constructor(\n private readonly ttlMs: number = LIST_CACHE_TTL_MS,\n private readonly now: () => number = Date.now,\n ) {}\n\n async run(key: string, produce: () => Promise<string>): Promise<string> {\n const hit = this.store.get(key);\n const now = this.now();\n if (hit && now - hit.at < this.ttlMs) {\n return hit.val;\n }\n const val = await produce();\n this.store.set(key, { at: now, val });\n return val;\n }\n\n /** Called after every {@link clear}, so caches built on top of these listings can drop theirs too. */\n onClear: (() => void) | null = null;\n\n clear(): void {\n this.store.clear();\n this.onClear?.();\n }\n}\n\n/** Decide whether a tool is allowed by the current access configuration. Unknown tools are denied. */\nexport function isToolAllowed(name: string, access: ToolAccess): boolean {\n switch (TOOL_CATEGORY[name]) {\n case 'stateRead':\n case 'devices':\n return true; // reading device/state values is always allowed\n case 'stateWrite':\n return access.allowWriteStates;\n case 'objectReadAll':\n return access.readObjects === 'all';\n case 'logRead':\n return access.allowReadLogs;\n case 'logWrite':\n return access.allowWriteLogs;\n case 'history':\n return access.allowHistory;\n case 'files':\n return access.allowFiles;\n case 'system':\n return access.allowSystemInfo;\n case 'objectChange':\n return access.allowObjectChange;\n default:\n return false; // unknown/unmapped tool → deny (safe default for an access list)\n }\n}\n\n/** Device types that default to write-disabled (must be explicitly allowed) for safety, e.g. door locks. */\nconst DEFAULT_WRITE_FALSE_TYPES = new Set(['lock']);\n\n/**\n * Build the stateId → deviceKey map and the set of keys that default to write-disabled (e.g. locks),\n * from the MCP `list_devices` tool (best-effort; empty on any error).\n */\nasync function loadStateDeviceMap(\n mcp: InProcessMcp,\n): Promise<{ stateToKey: Map<string, string>; defaultWriteFalse: Set<string> }> {\n const stateToKey = new Map<string, string>();\n const defaultWriteFalse = new Set<string>();\n try {\n const res = await mcp.callTool('list_devices', {});\n const parsed = JSON.parse(res.text) as {\n data?: { rooms?: { devicesInRoom?: McpDeviceEntry[] }[] };\n };\n for (const room of parsed.data?.rooms || []) {\n for (const dev of room.devicesInRoom || []) {\n const ids = deviceStateIds(dev);\n const key = deviceKey(ids);\n // Every control of the device shares the device's key, so the write-guard resolves any\n // of its states to the same ACL entry.\n for (const id of ids) {\n stateToKey.set(id, key);\n }\n if (DEFAULT_WRITE_FALSE_TYPES.has(String(dev.deviceType ?? ''))) {\n defaultWriteFalse.add(key);\n }\n }\n }\n } catch {\n /* no device map → no per-device enforcement */\n }\n return { stateToKey, defaultWriteFalse };\n}\n\n/** JSON shape of a `list_devices` result, for post-processing the device names the LLM sees. */\ninterface ListDevicesResult {\n data?: { rooms?: { devicesInRoom?: McpDeviceEntry[] }[] };\n}\n\n/** Device types dropped from `list_devices` so the LLM never treats them as controllable devices. */\nconst HIDDEN_LLM_TYPES = new Set(['button']);\n\n/**\n * Post-process a `list_devices` result for the LLM: drop hidden types (write-only buttons) and devices\n * the user set to read-disabled (`acl[key].read === false`), and — when a resolver is given — rewrite\n * each `deviceName` to the friendly name (smartName → parent name). Original text on any parse problem.\n */\nasync function postProcessListDevices(\n text: string,\n acl: DeviceAcl,\n resolveName?: (stateId: string, fallback: string) => Promise<string>,\n): Promise<string> {\n let parsed: ListDevicesResult;\n try {\n parsed = JSON.parse(text) as ListDevicesResult;\n } catch {\n return text;\n }\n const rooms = parsed.data?.rooms;\n if (!Array.isArray(rooms)) {\n return text;\n }\n for (const room of rooms) {\n const kept = (room.devicesInRoom || []).filter(d => {\n if (HIDDEN_LLM_TYPES.has(String(d.deviceType ?? ''))) {\n return false;\n }\n const key = deviceKey(deviceStateIds(d));\n return !(key && acl[key]?.read === false); // hide read-disabled devices from the LLM\n });\n const trimmed: McpDeviceEntry[] = [];\n for (const dev of kept) {\n const ids = deviceStateIds(dev);\n const name =\n resolveName && ids.length\n ? (await resolveName(deviceKey(ids), String(dev.deviceName ?? ''))) || dev.deviceName\n : dev.deviceName;\n // Keep only what the LLM needs to act (name/type + controlType→{stateId,writable}); drop the\n // verbose role/unit/min/max/ioBrokerValueType/friendlyDeviceNames to cut tokens & latency.\n const controls: Record<string, { stateId?: string; writable?: boolean }> = {};\n for (const [ct, c] of Object.entries(dev.controls || {})) {\n controls[ct] = { stateId: c?.stateId, writable: c?.writable };\n }\n trimmed.push({ deviceName: name, deviceType: dev.deviceType, controls });\n }\n room.devicesInRoom = trimmed;\n }\n return JSON.stringify(parsed);\n}\n\n/** Wrap `get_states` so it drops values of devices the user set to read-disabled. */\nfunction guardRead(\n baseRun: (args: Record<string, unknown>) => Promise<unknown>,\n stateToKey: Map<string, string>,\n acl: DeviceAcl,\n): (args: Record<string, unknown>) => Promise<unknown> {\n return async (args: Record<string, unknown>): Promise<unknown> => {\n const text = (await baseRun(args)) as string;\n let parsed: { data?: { states?: { id?: string }[] } };\n try {\n parsed = JSON.parse(text) as { data?: { states?: { id?: string }[] } };\n } catch {\n return text;\n }\n const states = parsed.data?.states;\n if (Array.isArray(states) && parsed.data) {\n parsed.data.states = states.filter(s => {\n const key = s?.id ? stateToKey.get(s.id) : undefined;\n return !(key && acl[key]?.read === false);\n });\n }\n return JSON.stringify(parsed);\n };\n}\n\n/**\n * Wrap a state-writing tool so it refuses writes to states of devices the user marked read-only.\n * Write is allowed when the explicit ACL entry says so; without an entry, the device type's default\n * applies (write-disabled for {@link DEFAULT_WRITE_FALSE_TYPES} like locks, allowed otherwise).\n */\nfunction guardWrite(\n name: string,\n baseRun: (args: Record<string, unknown>) => Promise<unknown>,\n stateToKey: Map<string, string>,\n acl: DeviceAcl,\n defaultWriteFalse: Set<string>,\n): (args: Record<string, unknown>) => Promise<unknown> {\n return async (args: Record<string, unknown>): Promise<unknown> => {\n const ids =\n name === 'set_states'\n ? ((args.states as { id?: string }[] | undefined) || []).map(s => s?.id).filter((x): x is string => !!x)\n : typeof args.id === 'string'\n ? [args.id]\n : [];\n for (const id of ids) {\n const key = stateToKey.get(id);\n if (!key) {\n continue;\n }\n const entry = acl[key];\n const writeAllowed = entry ? entry.write !== false : !defaultWriteFalse.has(key);\n if (!writeAllowed) {\n return JSON.stringify({\n ok: false,\n error: `Writing \"${id}\" is not allowed: device \"${key}\" is read-only in the assistant settings.`,\n });\n }\n }\n return baseRun(args);\n };\n}\n\n/**\n * Build the LLM tool set from the in-process ioBroker MCP server, filtered by the access config and\n * enforcing the per-device write ACL.\n *\n * All tools come from `@iobroker/mcp-server` — nothing is duplicated here. Gating layers:\n * 1. `createInProcessMcp({ allowSetState, allowObjectChange })` — which tools exist at all.\n * 2. `isToolAllowed()` — fine-grained category toggles (logs/history/files/object-read scope/…).\n * 3. `guardWrite()` — per-device write ACL (`access.deviceAcl`), e.g. keep a lock read-only.\n */\nexport async function buildMcpTools(\n mcp: InProcessMcp,\n access: ToolAccess,\n cache: ListCache = new ListCache(),\n resolveName?: (stateId: string, fallback: string) => Promise<string>,\n): Promise<{ tools: Tool[]; denied: string[]; cache: ListCache }> {\n const infos = await mcp.listTools();\n const acl = access.deviceAcl || {};\n // Write guard: whenever writing is enabled (explicit ACL + type defaults like locks).\n const enforceWrite = access.allowWriteStates;\n // Read guard: whenever any device is set read-disabled (hide it from list_devices + get_states).\n const enforceRead = Object.values(acl).some(a => a && a.read === false);\n const { stateToKey, defaultWriteFalse } =\n enforceWrite || enforceRead\n ? await loadStateDeviceMap(mcp)\n : { stateToKey: new Map<string, string>(), defaultWriteFalse: new Set<string>() };\n\n const tools: Tool[] = [];\n const denied: string[] = [];\n for (const info of infos) {\n if (!isToolAllowed(info.name, access)) {\n denied.push(info.name);\n continue;\n }\n const callTool = async (args: Record<string, unknown>): Promise<string> => {\n const text = (await mcp.callTool(info.name, args)).text;\n // For the LLM: drop buttons + read-disabled devices, and use friendly names.\n return info.name === 'list_devices' ? postProcessListDevices(text, acl, resolveName) : text;\n };\n // Serve stable, expensive read-only listings from the short-TTL cache; everything else runs live.\n const baseRun: (args: Record<string, unknown>) => Promise<unknown> = CACHEABLE_TOOLS.has(info.name)\n ? (args): Promise<string> => cache.run(`${info.name}:${JSON.stringify(args ?? {})}`, () => callTool(args))\n : callTool;\n let run = baseRun;\n if (enforceWrite && (info.name === 'set_state' || info.name === 'set_states')) {\n run = guardWrite(info.name, baseRun, stateToKey, acl, defaultWriteFalse);\n } else if (enforceRead && info.name === 'get_states') {\n run = guardRead(baseRun, stateToKey, acl);\n }\n tools.push({ name: info.name, description: info.description || '', parameters: info.inputSchema, run });\n }\n\n // Synthetic (non-MCP) tool: lets the model bust the device/room cache and re-scan. Useful when a\n // device the user mentions is missing from list_devices (stale cache) or was just added/renamed.\n tools.push({\n name: 'refresh_device_cache',\n description:\n 'Clear the cached device, room and function listings so the next list_devices, list_rooms or list_functions call performs a fresh scan. Call this when a device or room the user mentions is not in the list, or when the user says a device was just added, renamed or reassigned, then retry.',\n parameters: { type: 'object', properties: {}, additionalProperties: false },\n run: (): Promise<string> => {\n cache.clear();\n return Promise.resolve(JSON.stringify({ ok: true, data: { cleared: true } }));\n },\n });\n\n return { tools, denied, cache };\n}\n"]}
@@ -37,6 +37,7 @@ exports.createSttEngine = createSttEngine;
37
37
  exports.createTtsEngine = createTtsEngine;
38
38
  exports.listVoices = listVoices;
39
39
  exports.listSttModels = listSttModels;
40
+ exports.listTtsModels = listTtsModels;
40
41
  /**
41
42
  * Factory that builds the configured STT/TTS engine. Cloud engines (OpenAI/Azure/AWS) use resolved
42
43
  * credentials; local engines (Vosk/Piper) download their models on demand into the data dir.
@@ -48,6 +49,7 @@ const azure_1 = require("./azure");
48
49
  const aws_1 = require("./aws");
49
50
  const vosk_1 = require("./vosk");
50
51
  const piper_1 = require("./piper");
52
+ const openrouter_1 = require("./openrouter");
51
53
  const path = __importStar(require("node:path"));
52
54
  const ttsCache_1 = require("./ttsCache");
53
55
  const fallback_1 = require("./fallback");
@@ -67,6 +69,11 @@ function requireOpenAi(creds) {
67
69
  throw new Error('OpenAI speech needs an API key');
68
70
  }
69
71
  }
72
+ function requireOpenRouter(creds) {
73
+ if (!creds.openrouterKey) {
74
+ throw new Error('OpenRouter speech needs an API key');
75
+ }
76
+ }
70
77
  function createSttEngine(provider, ctx) {
71
78
  const primary = { name: provider, engine: buildSttEngine(provider, ctx) };
72
79
  const fallback = ctx.sttFallback && ctx.sttFallback !== provider ? ctx.sttFallback : '';
@@ -91,6 +98,9 @@ function buildSttEngine(provider, ctx) {
91
98
  return new aws_1.AwsStt(ctx.creds.aws);
92
99
  case 'vosk':
93
100
  return new vosk_1.VoskStt(ctx.dataDir, ctx.voskModel, ctx.log);
101
+ case 'openrouter':
102
+ requireOpenRouter(ctx.creds);
103
+ return new openrouter_1.OpenRouterStt(ctx.creds.openrouterKey, ctx.openrouterSttModel);
94
104
  case 'piper':
95
105
  throw new Error('Piper is a text-to-speech engine, not speech-to-text');
96
106
  default:
@@ -126,7 +136,9 @@ function cachedTts(provider, ctx) {
126
136
  ? ctx.voices.aws
127
137
  : provider === 'piper'
128
138
  ? ctx.voices.piper
129
- : ctx.voices.openai;
139
+ : provider === 'openrouter'
140
+ ? `${ctx.openrouterTtsModel || openrouter_1.DEFAULT_OPENROUTER_TTS_MODEL}-${ctx.voices.openrouter}`
141
+ : ctx.voices.openai;
130
142
  const dir = path.join(ctx.dataDir, 'ttscache', `${provider}-${(voice || 'default').replace(/[^\w.-]/g, '_')}`);
131
143
  return new ttsCache_1.CachedTts(buildTtsEngine(provider, ctx), { dir, log: ctx.log });
132
144
  }
@@ -142,6 +154,9 @@ function buildTtsEngine(provider, ctx) {
142
154
  return new piper_1.PiperTts(ctx.dataDir, ctx.voices.piper, ctx.log);
143
155
  case 'vosk':
144
156
  throw new Error('Vosk is a speech-to-text engine, not text-to-speech');
157
+ case 'openrouter':
158
+ requireOpenRouter(ctx.creds);
159
+ return new openrouter_1.OpenRouterTts(ctx.creds.openrouterKey, ctx.openrouterTtsModel, ctx.voices.openrouter, ctx.decodeAudio);
145
160
  default:
146
161
  requireOpenAi(ctx.creds);
147
162
  return new tts_1.OpenAiTts(ctx.creds.openaiKey, (ctx.voices.openai || 'alloy'), ctx.ttsModel || undefined);
@@ -160,12 +175,25 @@ async function listVoices(provider, ctx, language) {
160
175
  return (0, aws_1.listPollyVoices)(ctx.creds.aws, (0, lang_1.isoToLocale)(language));
161
176
  case 'piper':
162
177
  return (0, piper_1.listPiperVoices)();
178
+ case 'openrouter': {
179
+ // public list, no key needed: the voices of the chosen model
180
+ const model = ctx.openrouterTtsModel || openrouter_1.DEFAULT_OPENROUTER_TTS_MODEL;
181
+ const models = await (0, openrouter_1.listOpenRouterSpeechModels)('speech');
182
+ return (0, openrouter_1.voicesForLanguage)(models.find(m => m.id === model)?.voices || [], language);
183
+ }
163
184
  default:
164
185
  return OPENAI_VOICES;
165
186
  }
166
187
  }
167
- /** Suggested STT model names for the settings dropdown. Only Vosk has a local model catalogue. */
168
- function listSttModels(provider, language) {
188
+ /** Suggested STT model names for the settings dropdown: the local Vosk catalogue, or the models of OpenRouter. */
189
+ async function listSttModels(provider, language) {
190
+ if (provider === 'openrouter') {
191
+ return (await (0, openrouter_1.listOpenRouterSpeechModels)('transcription')).map(m => m.id);
192
+ }
169
193
  return provider === 'vosk' ? (0, vosk_1.listVoskModels)(language) : [];
170
194
  }
195
+ /** TTS model names for the settings dropdown — only OpenRouter has a choice worth listing. */
196
+ async function listTtsModels(provider) {
197
+ return provider === 'openrouter' ? (await (0, openrouter_1.listOpenRouterSpeechModels)('speech')).map(m => m.id) : [];
198
+ }
171
199
  //# sourceMappingURL=engines.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"engines.js","sourceRoot":"","sources":["../../../src/lib/voice/engines.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,0CAYC;AAoBD,0CAeC;AA6CD,gCAaC;AAGD,sCAEC;AAtLD;;;;GAIG;AACH,+BAAkD;AAClD,+BAAoE;AACpE,mCAA8D;AAC9D,+BAAuE;AACvE,iCAAi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Q,EAAE,CAAC;QACf,KAAK,OAAO;YACR,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACxB,OAAO,IAAA,uBAAe,EAAC,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,IAAA,kBAAW,EAAC,QAAQ,CAAC,CAAC,CAAC;QAC7F,KAAK,KAAK;YACN,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;YACtB,OAAO,IAAA,qBAAe,EAAC,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,IAAA,kBAAW,EAAC,QAAQ,CAAC,CAAC,CAAC;QACjE,KAAK,OAAO;YACR,OAAO,IAAA,uBAAe,GAAE,CAAC;QAC7B;YACI,OAAO,aAAa,CAAC;IAC7B,CAAC;AACL,CAAC;AAED,kGAAkG;AAClG,SAAgB,aAAa,CAAC,QAAwB,EAAE,QAAgB;IACpE,OAAO,QAAQ,KAAK,MAAM,CAAC,CAAC,CAAC,IAAA,qBAAc,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AAC/D,CAAC","sourcesContent":["/**\n * Factory that builds the configured STT/TTS engine. Cloud engines (OpenAI/Azure/AWS) use resolved\n * credentials; local engines (Vosk/Piper) download their models on demand into the data dir.\n * Adding a provider = one new engine class + one `case` here.\n */\nimport { OpenAiStt, type SttEngine } from './stt';\nimport { OpenAiTts, type OpenAiVoice, type TtsEngine } from './tts';\nimport { AzureStt, AzureTts, listAzureVoices } from './azure';\nimport { AwsStt, AwsTts, listPollyVoices, type AwsCreds } from './aws';\nimport { VoskStt, listVoskModels } from './vosk';\nimport { PiperTts, listPiperVoices } from './piper';\nimport * as path from 'node:path';\n\nimport { CachedTts } from './ttsCache';\nimport { FallbackStt, FallbackTts, type NamedEngine } from './fallback';\nimport { isoToLocale } from './lang';\nimport type { VoiceLogger } from './download';\n\nexport type SpeechProvider = 'openai' | 'azure' | 'aws' | 'vosk' | 'piper';\n\n/** All speech credentials, resolved from config (or the central credential store). */\nexport interface VoiceCredentials {\n openaiKey: string;\n azureKey: string;\n azureRegion: string;\n aws: AwsCreds;\n}\n\n/** Provider-specific voice ids (each provider names voices differently). */\nexport interface VoiceNames {\n openai: string; // alloy/echo/…\n azure: string; // e.g. de-DE-KatjaNeural\n aws: string; // e.g. Vicki\n piper: string; // e.g. de_DE-thorsten-medium\n}\n\n/** Everything the factory needs to build any engine. */\nexport interface EngineContext {\n creds: VoiceCredentials;\n voices: VoiceNames;\n /** Optional second engine, used when the primary one fails ('' = none). */\n sttFallback?: SpeechProvider | '';\n ttsFallback?: SpeechProvider | '';\n /** Writable instance data dir (for the local engines' downloads). */\n dataDir: string;\n log: VoiceLogger;\n /** Local model overrides ('' → default per language). */\n voskModel: string;\n /** OpenAI STT model ('' → 'whisper-1'; e.g. 'gpt-4o-transcribe'). */\n sttModel: string;\n /** OpenAI TTS model ('' → 'tts-1'; e.g. 'gpt-4o-mini-tts'). */\n ttsModel: string;\n}\n\nfunction requireAzure(creds: VoiceCredentials): void {\n if (!creds.azureKey || !creds.azureRegion) {\n throw new Error('Azure speech needs a subscription key and region');\n }\n}\n\nfunction requireAws(creds: VoiceCredentials): void {\n if (!creds.aws.accessKeyId || !creds.aws.secretAccessKey || !creds.aws.region) {\n throw new Error('AWS speech needs an access key id, secret access key and region');\n }\n}\n\nfunction requireOpenAi(creds: VoiceCredentials): void {\n if (!creds.openaiKey) {\n throw new Error('OpenAI speech needs an API key');\n }\n}\n\nexport function createSttEngine(provider: SpeechProvider, ctx: EngineContext): SttEngine {\n const primary: NamedEngine<SttEngine> = { name: provider, engine: buildSttEngine(provider, ctx) };\n const fallback = ctx.sttFallback && ctx.sttFallback !== provider ? ctx.sttFallback : '';\n if (!fallback) {\n return primary.engine;\n }\n try {\n return new FallbackStt([primary, { name: fallback, engine: buildSttEngine(fallback, ctx) }], ctx.log);\n } catch (e) {\n ctx.log.warn(`Speech-to-text fallback '${fallback}' unavailable: ${(e as Error).message}`);\n return primary.engine;\n }\n}\n\nfunction buildSttEngine(provider: SpeechProvider, ctx: EngineContext): SttEngine {\n switch (provider) {\n case 'azure':\n requireAzure(ctx.creds);\n return new AzureStt(ctx.creds.azureKey, ctx.creds.azureRegion);\n case 'aws':\n requireAws(ctx.creds);\n return new AwsStt(ctx.creds.aws);\n case 'vosk':\n return new VoskStt(ctx.dataDir, ctx.voskModel, ctx.log);\n case 'piper':\n throw new Error('Piper is a text-to-speech engine, not speech-to-text');\n default:\n requireOpenAi(ctx.creds);\n return new OpenAiStt(ctx.creds.openaiKey, ctx.sttModel || undefined);\n }\n}\n\nexport function createTtsEngine(provider: SpeechProvider, ctx: EngineContext): TtsEngine {\n // Each engine gets the shared behaviour (disk cache, length limit, SSML) and its own cache directory,\n // so a fallback engine's voice can never be served later while the primary one is healthy again.\n const primary: NamedEngine<TtsEngine> = { name: provider, engine: cachedTts(provider, ctx) };\n const fallback = ctx.ttsFallback && ctx.ttsFallback !== provider ? ctx.ttsFallback : '';\n if (!fallback) {\n return primary.engine;\n }\n try {\n return new FallbackTts([primary, { name: fallback, engine: cachedTts(fallback, ctx) }], ctx.log);\n } catch (e) {\n // A fallback that cannot even be built (missing key, wrong provider) must not take the main one down.\n ctx.log.warn(`Text-to-speech fallback '${fallback}' unavailable: ${(e as Error).message}`);\n return primary.engine;\n }\n}\n\n/**\n * One provider binding plus the shared cache/length/SSML layer. One cache directory per provider+voice —\n * a voice change must not serve audio in the old voice.\n */\nfunction cachedTts(provider: SpeechProvider, ctx: EngineContext): TtsEngine {\n const voice =\n provider === 'azure'\n ? ctx.voices.azure\n : provider === 'aws'\n ? ctx.voices.aws\n : provider === 'piper'\n ? ctx.voices.piper\n : ctx.voices.openai;\n const dir = path.join(ctx.dataDir, 'ttscache', `${provider}-${(voice || 'default').replace(/[^\\w.-]/g, '_')}`);\n return new CachedTts(buildTtsEngine(provider, ctx), { dir, log: ctx.log });\n}\n\nfunction buildTtsEngine(provider: SpeechProvider, ctx: EngineContext): TtsEngine {\n switch (provider) {\n case 'azure':\n requireAzure(ctx.creds);\n return new AzureTts(ctx.creds.azureKey, ctx.creds.azureRegion, ctx.voices.azure);\n case 'aws':\n requireAws(ctx.creds);\n return new AwsTts(ctx.creds.aws, ctx.voices.aws);\n case 'piper':\n return new PiperTts(ctx.dataDir, ctx.voices.piper, ctx.log);\n case 'vosk':\n throw new Error('Vosk is a speech-to-text engine, not text-to-speech');\n default:\n requireOpenAi(ctx.creds);\n return new OpenAiTts(\n ctx.creds.openaiKey,\n (ctx.voices.openai || 'alloy') as OpenAiVoice,\n ctx.ttsModel || undefined,\n );\n }\n}\n\n/** OpenAI TTS voices are a fixed set (no list API). */\nconst OPENAI_VOICES = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'];\n\n/** List the available TTS voices for a provider (for the settings voice dropdown). */\nexport async function listVoices(provider: SpeechProvider, ctx: EngineContext, language: string): Promise<string[]> {\n switch (provider) {\n case 'azure':\n requireAzure(ctx.creds);\n return listAzureVoices(ctx.creds.azureKey, ctx.creds.azureRegion, isoToLocale(language));\n case 'aws':\n requireAws(ctx.creds);\n return listPollyVoices(ctx.creds.aws, isoToLocale(language));\n case 'piper':\n return listPiperVoices();\n default:\n return OPENAI_VOICES;\n }\n}\n\n/** Suggested STT model names for the settings dropdown. Only Vosk has a local model catalogue. */\nexport function listSttModels(provider: SpeechProvider, language: string): string[] {\n return provider === 'vosk' ? listVoskModels(language) : [];\n}\n"]}
1
+ {"version":3,"file":"engines.js","sourceRoot":"","sources":["../../../src/lib/voice/engines.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6FA,0CAYC;AAuBD,0CAeC;AAuDD,gCAmBC;AAGD,sCAKC;AAGD,sCAEC;AAtOD;;;;GAIG;AACH,+BAAkD;AAClD,+BAAoE;AACpE,mCAA8D;AAC9D,+BAAuE;AACvE,iCAAiD;AACjD,mCAAoD;AACpD,6CAOsB;AACtB,gDAAkC;AAElC,yCAAu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rE,MAAM,MAAM,GAAG,MAAM,IAAA,uCAA0B,EAAC,QAAQ,CAAC,CAAC;YAC1D,OAAO,IAAA,8BAAiB,EAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,KAAK,KAAK,CAAC,EAAE,MAAM,IAAI,EAAE,EAAE,QAAQ,CAAC,CAAC;QACvF,CAAC;QACD;YACI,OAAO,aAAa,CAAC;IAC7B,CAAC;AACL,CAAC;AAED,kHAAkH;AAC3G,KAAK,UAAU,aAAa,CAAC,QAAwB,EAAE,QAAgB;IAC1E,IAAI,QAAQ,KAAK,YAAY,EAAE,CAAC;QAC5B,OAAO,CAAC,MAAM,IAAA,uCAA0B,EAAC,eAAe,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;IAC9E,CAAC;IACD,OAAO,QAAQ,KAAK,MAAM,CAAC,CAAC,CAAC,IAAA,qBAAc,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AAC/D,CAAC;AAED,8FAA8F;AACvF,KAAK,UAAU,aAAa,CAAC,QAAwB;IACxD,OAAO,QAAQ,KAAK,YAAY,CAAC,CAAC,CAAC,CAAC,MAAM,IAAA,uCAA0B,EAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACxG,CAAC","sourcesContent":["/**\n * Factory that builds the configured STT/TTS engine. Cloud engines (OpenAI/Azure/AWS) use resolved\n * credentials; local engines (Vosk/Piper) download their models on demand into the data dir.\n * Adding a provider = one new engine class + one `case` here.\n */\nimport { OpenAiStt, type SttEngine } from './stt';\nimport { OpenAiTts, type OpenAiVoice, type TtsEngine } from './tts';\nimport { AzureStt, AzureTts, listAzureVoices } from './azure';\nimport { AwsStt, AwsTts, listPollyVoices, type AwsCreds } from './aws';\nimport { VoskStt, listVoskModels } from './vosk';\nimport { PiperTts, listPiperVoices } from './piper';\nimport {\n OpenRouterStt,\n OpenRouterTts,\n listOpenRouterSpeechModels,\n voicesForLanguage,\n DEFAULT_OPENROUTER_TTS_MODEL,\n type AudioDecoder,\n} from './openrouter';\nimport * as path from 'node:path';\n\nimport { CachedTts } from './ttsCache';\nimport { FallbackStt, FallbackTts, type NamedEngine } from './fallback';\nimport { isoToLocale } from './lang';\nimport type { VoiceLogger } from './download';\n\nexport type SpeechProvider = 'openai' | 'openrouter' | 'azure' | 'aws' | 'vosk' | 'piper';\n\n/** All speech credentials, resolved from config (or the central credential store). */\nexport interface VoiceCredentials {\n openaiKey: string;\n openrouterKey: string;\n azureKey: string;\n azureRegion: string;\n aws: AwsCreds;\n}\n\n/** Provider-specific voice ids (each provider names voices differently). */\nexport interface VoiceNames {\n openai: string; // alloy/echo/…\n azure: string; // e.g. de-DE-KatjaNeural\n aws: string; // e.g. Vicki\n piper: string; // e.g. de_DE-thorsten-medium\n openrouter: string; // depends on the model, e.g. Kore, george\n}\n\n/** Everything the factory needs to build any engine. */\nexport interface EngineContext {\n creds: VoiceCredentials;\n voices: VoiceNames;\n /** Optional second engine, used when the primary one fails ('' = none). */\n sttFallback?: SpeechProvider | '';\n ttsFallback?: SpeechProvider | '';\n /** Writable instance data dir (for the local engines' downloads). */\n dataDir: string;\n log: VoiceLogger;\n /** Local model overrides ('' → default per language). */\n voskModel: string;\n /** OpenAI STT model ('' → 'whisper-1'; e.g. 'gpt-4o-transcribe'). */\n sttModel: string;\n /** OpenAI TTS model ('' → 'tts-1'; e.g. 'gpt-4o-mini-tts'). */\n ttsModel: string;\n /** OpenRouter models ('' → the defaults of openrouter.ts) */\n openrouterSttModel: string;\n openrouterTtsModel: string;\n /** Decoder for compressed speech (OpenRouter models that only return mp3); absent → those models fail */\n decodeAudio?: AudioDecoder;\n}\n\nfunction requireAzure(creds: VoiceCredentials): void {\n if (!creds.azureKey || !creds.azureRegion) {\n throw new Error('Azure speech needs a subscription key and region');\n }\n}\n\nfunction requireAws(creds: VoiceCredentials): void {\n if (!creds.aws.accessKeyId || !creds.aws.secretAccessKey || !creds.aws.region) {\n throw new Error('AWS speech needs an access key id, secret access key and region');\n }\n}\n\nfunction requireOpenAi(creds: VoiceCredentials): void {\n if (!creds.openaiKey) {\n throw new Error('OpenAI speech needs an API key');\n }\n}\n\nfunction requireOpenRouter(creds: VoiceCredentials): void {\n if (!creds.openrouterKey) {\n throw new Error('OpenRouter speech needs an API key');\n }\n}\n\nexport function createSttEngine(provider: SpeechProvider, ctx: EngineContext): SttEngine {\n const primary: NamedEngine<SttEngine> = { name: provider, engine: buildSttEngine(provider, ctx) };\n const fallback = ctx.sttFallback && ctx.sttFallback !== provider ? ctx.sttFallback : '';\n if (!fallback) {\n return primary.engine;\n }\n try {\n return new FallbackStt([primary, { name: fallback, engine: buildSttEngine(fallback, ctx) }], ctx.log);\n } catch (e) {\n ctx.log.warn(`Speech-to-text fallback '${fallback}' unavailable: ${(e as Error).message}`);\n return primary.engine;\n }\n}\n\nfunction buildSttEngine(provider: SpeechProvider, ctx: EngineContext): SttEngine {\n switch (provider) {\n case 'azure':\n requireAzure(ctx.creds);\n return new AzureStt(ctx.creds.azureKey, ctx.creds.azureRegion);\n case 'aws':\n requireAws(ctx.creds);\n return new AwsStt(ctx.creds.aws);\n case 'vosk':\n return new VoskStt(ctx.dataDir, ctx.voskModel, ctx.log);\n case 'openrouter':\n requireOpenRouter(ctx.creds);\n return new OpenRouterStt(ctx.creds.openrouterKey, ctx.openrouterSttModel);\n case 'piper':\n throw new Error('Piper is a text-to-speech engine, not speech-to-text');\n default:\n requireOpenAi(ctx.creds);\n return new OpenAiStt(ctx.creds.openaiKey, ctx.sttModel || undefined);\n }\n}\n\nexport function createTtsEngine(provider: SpeechProvider, ctx: EngineContext): TtsEngine {\n // Each engine gets the shared behaviour (disk cache, length limit, SSML) and its own cache directory,\n // so a fallback engine's voice can never be served later while the primary one is healthy again.\n const primary: NamedEngine<TtsEngine> = { name: provider, engine: cachedTts(provider, ctx) };\n const fallback = ctx.ttsFallback && ctx.ttsFallback !== provider ? ctx.ttsFallback : '';\n if (!fallback) {\n return primary.engine;\n }\n try {\n return new FallbackTts([primary, { name: fallback, engine: cachedTts(fallback, ctx) }], ctx.log);\n } catch (e) {\n // A fallback that cannot even be built (missing key, wrong provider) must not take the main one down.\n ctx.log.warn(`Text-to-speech fallback '${fallback}' unavailable: ${(e as Error).message}`);\n return primary.engine;\n }\n}\n\n/**\n * One provider binding plus the shared cache/length/SSML layer. One cache directory per provider+voice —\n * a voice change must not serve audio in the old voice.\n */\nfunction cachedTts(provider: SpeechProvider, ctx: EngineContext): TtsEngine {\n const voice =\n provider === 'azure'\n ? ctx.voices.azure\n : provider === 'aws'\n ? ctx.voices.aws\n : provider === 'piper'\n ? ctx.voices.piper\n : provider === 'openrouter'\n ? `${ctx.openrouterTtsModel || DEFAULT_OPENROUTER_TTS_MODEL}-${ctx.voices.openrouter}`\n : ctx.voices.openai;\n const dir = path.join(ctx.dataDir, 'ttscache', `${provider}-${(voice || 'default').replace(/[^\\w.-]/g, '_')}`);\n return new CachedTts(buildTtsEngine(provider, ctx), { dir, log: ctx.log });\n}\n\nfunction buildTtsEngine(provider: SpeechProvider, ctx: EngineContext): TtsEngine {\n switch (provider) {\n case 'azure':\n requireAzure(ctx.creds);\n return new AzureTts(ctx.creds.azureKey, ctx.creds.azureRegion, ctx.voices.azure);\n case 'aws':\n requireAws(ctx.creds);\n return new AwsTts(ctx.creds.aws, ctx.voices.aws);\n case 'piper':\n return new PiperTts(ctx.dataDir, ctx.voices.piper, ctx.log);\n case 'vosk':\n throw new Error('Vosk is a speech-to-text engine, not text-to-speech');\n case 'openrouter':\n requireOpenRouter(ctx.creds);\n return new OpenRouterTts(\n ctx.creds.openrouterKey,\n ctx.openrouterTtsModel,\n ctx.voices.openrouter,\n ctx.decodeAudio,\n );\n default:\n requireOpenAi(ctx.creds);\n return new OpenAiTts(\n ctx.creds.openaiKey,\n (ctx.voices.openai || 'alloy') as OpenAiVoice,\n ctx.ttsModel || undefined,\n );\n }\n}\n\n/** OpenAI TTS voices are a fixed set (no list API). */\nconst OPENAI_VOICES = ['alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer'];\n\n/** List the available TTS voices for a provider (for the settings voice dropdown). */\nexport async function listVoices(provider: SpeechProvider, ctx: EngineContext, language: string): Promise<string[]> {\n switch (provider) {\n case 'azure':\n requireAzure(ctx.creds);\n return listAzureVoices(ctx.creds.azureKey, ctx.creds.azureRegion, isoToLocale(language));\n case 'aws':\n requireAws(ctx.creds);\n return listPollyVoices(ctx.creds.aws, isoToLocale(language));\n case 'piper':\n return listPiperVoices();\n case 'openrouter': {\n // public list, no key needed: the voices of the chosen model\n const model = ctx.openrouterTtsModel || DEFAULT_OPENROUTER_TTS_MODEL;\n const models = await listOpenRouterSpeechModels('speech');\n return voicesForLanguage(models.find(m => m.id === model)?.voices || [], language);\n }\n default:\n return OPENAI_VOICES;\n }\n}\n\n/** Suggested STT model names for the settings dropdown: the local Vosk catalogue, or the models of OpenRouter. */\nexport async function listSttModels(provider: SpeechProvider, language: string): Promise<string[]> {\n if (provider === 'openrouter') {\n return (await listOpenRouterSpeechModels('transcription')).map(m => m.id);\n }\n return provider === 'vosk' ? listVoskModels(language) : [];\n}\n\n/** TTS model names for the settings dropdown — only OpenRouter has a choice worth listing. */\nexport async function listTtsModels(provider: SpeechProvider): Promise<string[]> {\n return provider === 'openrouter' ? (await listOpenRouterSpeechModels('speech')).map(m => m.id) : [];\n}\n"]}
@@ -160,6 +160,15 @@ class EsphomeSatellites {
160
160
  target.setWakeWords(ids);
161
161
  return true;
162
162
  }
163
+ /**
164
+ * Offer the current custom wake words to every connected satellite again — after one was uploaded
165
+ * or deleted. Each device answers with its new configuration (→ `onWakeWords`).
166
+ */
167
+ refreshWakeWords() {
168
+ const online = this.connections.filter(c => c.online);
169
+ online.forEach(c => c.requestWakeWordConfig());
170
+ return online.length;
171
+ }
163
172
  /** Everything one satellite exposes besides the voice pipeline (gain, noise suppression, …). */
164
173
  entities(device) {
165
174
  return this.connections.find(c => c.online && c.deviceName === device)?.entities.list() ?? [];
@@ -208,6 +217,12 @@ class EsphomeConnection {
208
217
  online = false;
209
218
  /** Last wake-word configuration the device reported; null until the handshake answers. */
210
219
  wakeWordConfig = null;
220
+ /**
221
+ * One entry per configuration request in flight, in send order (the device answers each one, in
222
+ * order): the ids a preceding `SetConfiguration` asked for, or null. The read-back is the only
223
+ * failure signal the device gives.
224
+ */
225
+ configChecks = [];
211
226
  /** Everything else the device exposes — rebuilt from scratch on every reconnect. */
212
227
  entities = new esphomeEntities_1.EntityRegistry();
213
228
  host;
@@ -261,6 +276,7 @@ class EsphomeConnection {
261
276
  // The device re-announces everything after a reconnect, and the keys are not stable across
262
277
  // reboots, so starting from a clean slate is the only safe option.
263
278
  this.entities.clear();
279
+ this.configChecks = [];
264
280
  const socket = net.createConnection({ host: this.host, port: this.port }, () => {
265
281
  this.opts.log.debug(`ESPHome ${this.host}:${this.port} connected`);
266
282
  this.handshake();
@@ -379,7 +395,7 @@ class EsphomeConnection {
379
395
  subscribe.setSubscribe(true);
380
396
  subscribe.setFlags(SUBSCRIBE_API_AUDIO);
381
397
  this.send(subscribe);
382
- this.send(new esphomeProto_1.pb.VoiceAssistantConfigurationRequest());
398
+ this.requestWakeWordConfig();
383
399
  this.pingTimer = setInterval(() => {
384
400
  if (++this.missedPings > 2) {
385
401
  this.opts.log.info(`ESPHome ${this.deviceName}: no reply to keepalive — reconnecting`);
@@ -413,18 +429,32 @@ class EsphomeConnection {
413
429
  }
414
430
  case 'VoiceAssistantConfigurationResponse': {
415
431
  const config = message;
432
+ // A custom wake word the device already downloaded is listed twice: once from its own
433
+ // model folder and once from our offer.
434
+ const seen = new Set();
416
435
  this.wakeWordConfig = {
417
436
  active: config.getActiveWakeWordsList(),
418
- available: config.getAvailableWakeWordsList().map(w => ({
437
+ available: config
438
+ .getAvailableWakeWordsList()
439
+ .map(w => ({
419
440
  id: w.getId(),
420
441
  phrase: w.getWakeWord(),
421
442
  languages: w.getTrainedLanguagesList(),
422
- })),
443
+ }))
444
+ .filter(w => !seen.has(w.id) && !!seen.add(w.id)),
423
445
  max: config.getMaxActiveWakeWords(),
424
446
  };
425
447
  const active = this.wakeWordConfig.active.join(', ') || '(none active)';
426
448
  const available = this.wakeWordConfig.available.map(w => w.id);
427
449
  this.opts.log.info(`ESPHome ${this.deviceName}: wake words ${active}${available.length ? ` (available: ${available.join(', ')})` : ''}`);
450
+ const requested = this.configChecks.shift();
451
+ const missing = requested?.filter(id => !this.wakeWordConfig?.active.includes(id)) ?? [];
452
+ if (missing.length) {
453
+ // The device answers a model it cannot fetch or load with silence, not an error.
454
+ this.opts.log.warn(`ESPHome ${this.deviceName}: could not activate wake word(s) ${missing.join(', ')}. ` +
455
+ `For an uploaded wake word the device must reach ${this.mediaUrl('/')} and the model ` +
456
+ `must be a microWakeWord .tflite.`);
457
+ }
428
458
  this.opts.onWakeWords?.(this.deviceName, this.room, this.wakeWordConfig);
429
459
  return;
430
460
  }
@@ -611,15 +641,27 @@ class EsphomeConnection {
611
641
  room: this.room,
612
642
  wakeWord: capture.wakeWord,
613
643
  sttMs,
644
+ audioMs: Math.round(seconds * 1000),
645
+ vad: {
646
+ peak: Math.round(capture.detector.peak),
647
+ noiseFloor: Math.round(capture.detector.noiseFloor),
648
+ startLevel: Math.round(capture.detector.startLevel),
649
+ endLevel: Math.round(capture.detector.endLevel),
650
+ },
614
651
  })).trim();
615
- const followUp = !!answer && this.opts.expectsFollowUp?.(answer) === true;
652
+ const followUp = !!answer && this.opts.expectsFollowUp?.(answer, this.deviceName) === true;
616
653
  this.event(EVENT.INTENT_END, followUp ? { continue_conversation: '1' } : undefined);
617
654
  if (!answer) {
618
655
  this.event(EVENT.RUN_END);
619
656
  this.status('idle');
620
657
  return;
621
658
  }
659
+ const ttsStart = Date.now();
622
660
  const { pcm: ttsPcm, sampleRate } = await this.opts.tts.synthesize(answer, this.opts.language);
661
+ this.opts.onSpoken?.(this.deviceName, {
662
+ ms: Date.now() - ttsStart,
663
+ audioMs: (ttsPcm.length / 2 / sampleRate) * 1000,
664
+ });
623
665
  const url = this.publish(ttsPcm, sampleRate);
624
666
  this.status('speaking');
625
667
  this.event(EVENT.TTS_START, { text: answer });
@@ -664,8 +706,37 @@ class EsphomeConnection {
664
706
  message.setActiveWakeWordsList(valid);
665
707
  this.send(message);
666
708
  this.opts.log.info(`ESPHome ${this.deviceName}: wake words set to ${valid.join(', ') || '(none)'}`);
667
- // Read back, so the reported state is the device's and not our guess.
668
- this.send(new esphomeProto_1.pb.VoiceAssistantConfigurationRequest());
709
+ // Read back, so the reported state is the device's and not our guess. The device resolves a
710
+ // custom wake word (and downloads it) while handling the SetConfiguration, so this answer
711
+ // already tells whether that worked.
712
+ this.requestWakeWordConfig(valid);
713
+ }
714
+ /**
715
+ * Ask the device for its wake-word configuration. The request always carries the current custom
716
+ * wake words: the firmware replaces its list of offered models with every request, so one sent
717
+ * without them would make an uploaded wake word vanish from the device until the next offer.
718
+ *
719
+ * @param check ids a SetConfiguration just asked for — warned about if the answer lacks them
720
+ */
721
+ requestWakeWordConfig(check = null) {
722
+ const request = new esphomeProto_1.pb.VoiceAssistantConfigurationRequest();
723
+ const offers = this.opts.externalWakeWords?.() ?? [];
724
+ request.setExternalWakeWordsList(offers.map(offer => {
725
+ const w = new esphomeProto_1.pb.VoiceAssistantExternalWakeWord();
726
+ w.setId(offer.id);
727
+ w.setWakeWord(offer.phrase);
728
+ w.setTrainedLanguagesList(offer.languages);
729
+ w.setModelType('micro');
730
+ w.setModelSize(offer.size);
731
+ w.setModelHash(offer.sha256);
732
+ w.setUrl(this.mediaUrl(offer.path));
733
+ return w;
734
+ }));
735
+ // A device that never answers must not let this grow without bound.
736
+ if (this.configChecks.length < 16) {
737
+ this.configChecks.push(check);
738
+ }
739
+ this.send(request);
669
740
  }
670
741
  /** Drop a half-finished capture (device went away, or told us to stop). */
671
742
  abortCapture() {
@@ -705,7 +776,10 @@ class EsphomeConnection {
705
776
  * addresses, and the local end of *this* socket is by definition one it can talk to.
706
777
  */
707
778
  publish(pcm, sampleRate) {
708
- const path = this.opts.media.publish((0, stt_1.pcmToWav)(pcm, sampleRate), 'audio/wav');
779
+ return this.mediaUrl(this.opts.media.publish((0, stt_1.pcmToWav)(pcm, sampleRate), 'audio/wav'));
780
+ }
781
+ /** Absolute URL of a path on the media server, with a host this device can reach. */
782
+ mediaUrl(path) {
709
783
  const local = (this.socket?.localAddress || '').replace(/^::ffff:/, '');
710
784
  const host = this.opts.mediaHost?.trim() || local || '127.0.0.1';
711
785
  // An IPv6 literal has to be bracketed in a URL.