node-red-contrib-knx-ultimate 7.1.0-beta.2 → 7.1.2

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 (52) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +5 -1
  3. package/nodes/knxUltimateAI.html +1820 -0
  4. package/nodes/knxUltimateAI.js +16797 -0
  5. package/nodes/knxUltimateAIHomeAssistant.html +40 -0
  6. package/nodes/knxUltimateAIHomeAssistant.js +152 -0
  7. package/nodes/knxUltimateHueController.html +1 -1
  8. package/nodes/knxUltimateMatterBridge.html +2 -2
  9. package/nodes/knxUltimateMatterControllerDevice.html +2 -2
  10. package/nodes/locales/de/knxUltimateAI.html +5 -0
  11. package/nodes/locales/de/knxUltimateAI.json +231 -0
  12. package/nodes/locales/de/knxUltimateUtility.json +8 -0
  13. package/nodes/locales/en/knxUltimateAI.html +5 -0
  14. package/nodes/locales/en/knxUltimateAI.json +234 -0
  15. package/nodes/locales/en/knxUltimateUtility.json +8 -0
  16. package/nodes/locales/es/knxUltimateAI.html +5 -0
  17. package/nodes/locales/es/knxUltimateAI.json +231 -0
  18. package/nodes/locales/es/knxUltimateUtility.json +9 -1
  19. package/nodes/locales/fr/knxUltimateAI.html +5 -0
  20. package/nodes/locales/fr/knxUltimateAI.json +231 -0
  21. package/nodes/locales/fr/knxUltimateUtility.json +8 -0
  22. package/nodes/locales/it/knxUltimateAI.html +5 -0
  23. package/nodes/locales/it/knxUltimateAI.json +234 -0
  24. package/nodes/locales/it/knxUltimateUtility.json +8 -0
  25. package/nodes/locales/zh-CN/knxUltimateAI.html +5 -0
  26. package/nodes/locales/zh-CN/knxUltimateAI.json +231 -0
  27. package/nodes/locales/zh-CN/knxUltimateUtility.json +8 -0
  28. package/nodes/plugins/knxUltimate-cerebrum-runtime-plugin.js +79 -0
  29. package/nodes/plugins/knxUltimate-migration-notice-plugin.html +19 -0
  30. package/nodes/plugins/knxUltimateAI-vue/assets/app.css +1 -0
  31. package/nodes/plugins/knxUltimateAI-vue/assets/app.js +13 -0
  32. package/nodes/plugins/knxUltimateAI-vue/assets/chunk-html2canvas.esm.js +5 -0
  33. package/nodes/plugins/knxUltimateAI-vue/assets/chunk-index.es.js +5 -0
  34. package/nodes/plugins/knxUltimateAI-vue/assets/chunk-jspdf.es.min.js +79 -0
  35. package/nodes/plugins/knxUltimateAI-vue/assets/chunk-purify.es.js +3 -0
  36. package/nodes/plugins/knxUltimateAI-vue/index.html +13 -0
  37. package/nodes/plugins/knxUltimateViewer-vue/assets/app.js +2 -2
  38. package/nodes/utils/knxAiCamera.js +302 -0
  39. package/nodes/utils/knxAiCatalogRetrieval.js +349 -0
  40. package/nodes/utils/knxAiCerebrum.js +406 -0
  41. package/nodes/utils/knxAiChatContext.js +496 -0
  42. package/nodes/utils/knxAiEventHistory.js +521 -0
  43. package/nodes/utils/knxAiHomeMemory.js +1021 -0
  44. package/nodes/utils/knxAiScheduler.js +445 -0
  45. package/nodes/utils/knxAiSemanticContext.js +662 -0
  46. package/nodes/utils/knxAiTelegramVoice.js +545 -0
  47. package/nodes/utils/knxAiWebAccess.js +631 -0
  48. package/package.json +17 -9
  49. package/resources/KNXAIChatAdapterMappings.js +416 -0
  50. package/resources/hueControllerMigrationDialog.js +3 -0
  51. package/resources/knxUtilityMigration.js +3 -0
  52. package/resources/legacyMigrationNotice.js +143 -0
@@ -0,0 +1,545 @@
1
+ const path = require('path')
2
+
3
+ const KNX_AI_TELEGRAM_VOICE_MAX_BYTES = 20 * 1024 * 1024
4
+ const KNX_AI_TELEGRAM_VOICE_MAX_DURATION_SECONDS = 5 * 60
5
+ const KNX_AI_VOICE_API_TIMEOUT_MS = 120000
6
+ const KNX_AI_VOICE_TRANSCRIPTION_MODEL = 'gpt-4o-mini-transcribe'
7
+ const KNX_AI_VOICE_SPEECH_MODEL = 'gpt-4o-mini-tts'
8
+ const KNX_AI_VOICE_SPEECH_VOICE = 'alloy'
9
+ const KNX_AI_VOICE_SPEECH_MAX_CHARS = 4096
10
+ const KNX_AI_VOICE_DEFAULT_BASE_URL = 'https://api.openai.com/v1'
11
+
12
+ const normalizeKnxAiLlmProvider = (value) => {
13
+ const normalized = String(value || '').trim().toLowerCase().replace(/[ -]+/g, '_')
14
+ if (!normalized || normalized === 'openai' || normalized === 'openai_compatible') return 'openai_compat'
15
+ return normalized
16
+ }
17
+
18
+ const isKnxAiOpenAiCompatibleChatProvider = (value) => {
19
+ const provider = normalizeKnxAiLlmProvider(value)
20
+ return provider === 'openai_compat'
21
+ }
22
+
23
+ const resolveKnxAiVoiceServiceConfig = ({
24
+ chatProvider,
25
+ chatBaseUrl,
26
+ chatApiKey
27
+ } = {}) => {
28
+ const provider = normalizeKnxAiLlmProvider(chatProvider)
29
+ const chatCompatible = isKnxAiOpenAiCompatibleChatProvider(provider)
30
+
31
+ return {
32
+ apiKey: chatCompatible ? String(chatApiKey || '').trim() : '',
33
+ baseUrl: chatCompatible ? (String(chatBaseUrl || '').trim() || KNX_AI_VOICE_DEFAULT_BASE_URL) : '',
34
+ chatCompatible,
35
+ chatProvider: provider,
36
+ source: chatCompatible ? 'chat' : 'unconfigured',
37
+ speechModel: KNX_AI_VOICE_SPEECH_MODEL,
38
+ speechVoice: KNX_AI_VOICE_SPEECH_VOICE,
39
+ transcriptionModel: KNX_AI_VOICE_TRANSCRIPTION_MODEL
40
+ }
41
+ }
42
+
43
+ const isOfficialOpenAiVoiceUrl = (value) => {
44
+ try {
45
+ const parsed = new URL(String(value || '').trim())
46
+ const hostname = parsed.hostname.toLowerCase()
47
+ return parsed.protocol === 'https:' && (hostname === 'api.openai.com' || hostname.endsWith('.api.openai.com'))
48
+ } catch (error) {
49
+ return false
50
+ }
51
+ }
52
+
53
+ const normalizeVoiceMediaType = (value) => {
54
+ const normalized = String(value || '').split(';')[0].trim().toLowerCase()
55
+ return normalized.startsWith('audio/') ? normalized : 'audio/ogg'
56
+ }
57
+
58
+ const extensionForVoiceMediaType = (mediaType) => {
59
+ const normalized = normalizeVoiceMediaType(mediaType)
60
+ if (normalized === 'audio/mpeg' || normalized === 'audio/mp3') return '.mp3'
61
+ if (normalized === 'audio/mp4' || normalized === 'audio/m4a' || normalized === 'audio/x-m4a') return '.m4a'
62
+ if (normalized === 'audio/wav' || normalized === 'audio/x-wav') return '.wav'
63
+ if (normalized === 'audio/webm') return '.webm'
64
+ return '.ogg'
65
+ }
66
+
67
+ const sanitizeVoiceFilename = ({ filename, mediaType, fallback = 'telegram-voice' } = {}) => {
68
+ const source = path.basename(String(filename || '').trim()).replace(/[^a-zA-Z0-9._-]/g, '-')
69
+ if (source) return source.slice(0, 240)
70
+ return `${fallback}${extensionForVoiceMediaType(mediaType)}`
71
+ }
72
+
73
+ const getAiVoiceDisclosure = (language) => {
74
+ const normalized = String(language || '').trim().toLowerCase().split(/[-_]/)[0]
75
+ const labels = {
76
+ de: 'KI-generierte Stimme',
77
+ en: 'AI-generated voice',
78
+ es: 'Voz generada por IA',
79
+ fr: 'Voix générée par l’IA',
80
+ it: 'Voce generata dall’IA',
81
+ zh: 'AI 生成的语音'
82
+ }
83
+ return labels[normalized] || labels.en
84
+ }
85
+
86
+ const deriveOpenAiCompatibleAudioUrl = (baseUrl, resource = 'transcriptions') => {
87
+ const resourceName = String(resource || '').trim().toLowerCase()
88
+ if (resourceName !== 'transcriptions' && resourceName !== 'speech') {
89
+ throw new Error(`Unsupported OpenAI audio resource '${resourceName}'`)
90
+ }
91
+ let parsed
92
+ try {
93
+ parsed = new URL(String(baseUrl || 'https://api.openai.com/v1/chat/completions').trim())
94
+ } catch (error) {
95
+ throw new Error('Invalid OpenAI-compatible base URL for voice processing')
96
+ }
97
+ if (parsed.username || parsed.password) throw new Error('OpenAI-compatible voice URL must not contain credentials')
98
+ const pathname = parsed.pathname.replace(/\/+$/, '') || '/v1'
99
+ const terminalPaths = ['/chat/completions', '/completions', '/responses']
100
+ const terminal = terminalPaths.find(item => pathname.endsWith(item))
101
+ let apiRoot = terminal ? pathname.slice(0, -terminal.length) : pathname
102
+ if (!terminal && !/\/v1$/i.test(apiRoot)) {
103
+ const versionIndex = apiRoot.toLowerCase().lastIndexOf('/v1/')
104
+ if (versionIndex >= 0) apiRoot = apiRoot.slice(0, versionIndex + 3)
105
+ }
106
+ parsed.pathname = `${apiRoot.replace(/\/+$/, '')}/audio/${resourceName}`
107
+ parsed.hash = ''
108
+ return parsed.toString()
109
+ }
110
+
111
+ const findTelegramVoiceMetadata = (message) => {
112
+ const source = message && typeof message === 'object' ? message : {}
113
+ const original = source.originalMessage && typeof source.originalMessage === 'object'
114
+ ? source.originalMessage
115
+ : {}
116
+ if (original.voice && typeof original.voice === 'object') return original.voice
117
+ if (original.message && original.message.voice && typeof original.message.voice === 'object') return original.message.voice
118
+ return {}
119
+ }
120
+
121
+ const resolveTelegramVoiceAllowedOrigin = (message) => {
122
+ const details = message && message.telegramBot && typeof message.telegramBot === 'object'
123
+ ? message.telegramBot
124
+ : {}
125
+ const candidate = String(details.baseApiUrl || details.baseapiurl || details.baseApiURL || '').trim()
126
+ if (!candidate) return 'https://api.telegram.org'
127
+ try { return new URL(candidate).origin } catch (error) { return 'https://api.telegram.org' }
128
+ }
129
+
130
+ const applyKnxAiTelegramVoiceInputPresetFallback = ({ preset, message } = {}) => {
131
+ const normalizedPreset = String(preset || '')
132
+ if (!['windkh-telegrambot', 'redbot-telegram'].includes(normalizedPreset) || !message || typeof message !== 'object') return null
133
+ const telegram = message.payload && typeof message.payload === 'object' ? message.payload : null
134
+ if (!telegram) return null
135
+ const chatId = telegram.chatId
136
+ if (chatId === undefined || chatId === null || chatId === '') return null
137
+ const original = message.originalMessage && typeof message.originalMessage === 'object'
138
+ ? message.originalMessage
139
+ : {}
140
+ const voice = findTelegramVoiceMetadata(message)
141
+
142
+ if (normalizedPreset === 'redbot-telegram') {
143
+ const redbotType = String(telegram.type || '').trim().toLowerCase()
144
+ if ((redbotType !== 'audio' && redbotType !== 'voice') || !Buffer.isBuffer(telegram.content)) return null
145
+ const mediaType = normalizeVoiceMediaType(voice.mime_type || telegram.mimeType)
146
+ message.sessionId = String(chatId)
147
+ message.language = (original.from && original.from.language_code) ||
148
+ (original.message && original.message.from && original.message.from.language_code) ||
149
+ telegram.language ||
150
+ message.language ||
151
+ ''
152
+ message.topic = 'ask'
153
+ delete message.prompt
154
+ message.knxAi = Object.assign({}, message.knxAi, {
155
+ sessionId: String(chatId),
156
+ voiceInput: {
157
+ source: 'telegram',
158
+ originalType: 'voice',
159
+ transport: 'redbot-buffer',
160
+ data: telegram.content,
161
+ fileId: String(voice.file_id || '').trim(),
162
+ mediaType,
163
+ filename: sanitizeVoiceFilename({ filename: telegram.filename || voice.file_name, mediaType }),
164
+ durationSeconds: Math.max(0, Number(voice.duration || telegram.duration) || 0),
165
+ fileSize: Math.max(0, Number(voice.file_size || telegram.fileSize) || telegram.content.length)
166
+ }
167
+ })
168
+ return message
169
+ }
170
+
171
+ if (telegram.type !== 'voice') return null
172
+ const mediaType = normalizeVoiceMediaType(voice.mime_type || telegram.contentType || telegram.mimeType)
173
+ const fileId = String(telegram.content || voice.file_id || '').trim()
174
+ const weblink = String(telegram.weblink || message.weblink || '').trim()
175
+ if (!fileId && !weblink) return null
176
+
177
+ message.sessionId = String(chatId)
178
+ message.language = (original.from && original.from.language_code) ||
179
+ (original.message && original.message.from && original.message.from.language_code) ||
180
+ message.language ||
181
+ ''
182
+ message.topic = 'ask'
183
+ delete message.prompt
184
+ message.knxAi = Object.assign({}, message.knxAi, {
185
+ sessionId: String(chatId),
186
+ voiceInput: {
187
+ source: 'telegram',
188
+ originalType: 'voice',
189
+ fileId,
190
+ weblink,
191
+ allowedOrigin: resolveTelegramVoiceAllowedOrigin(message),
192
+ mediaType,
193
+ filename: sanitizeVoiceFilename({ filename: telegram.filename || voice.file_name, mediaType }),
194
+ durationSeconds: Math.max(0, Number(voice.duration || telegram.duration) || 0),
195
+ fileSize: Math.max(0, Number(voice.file_size || telegram.fileSize) || 0)
196
+ }
197
+ })
198
+ return message
199
+ }
200
+
201
+ const isKnxAiTelegramVoiceInput = (message) => {
202
+ const voiceInput = message && message.knxAi && message.knxAi.voiceInput
203
+ return !!(voiceInput && voiceInput.source === 'telegram' && voiceInput.originalType === 'voice')
204
+ }
205
+
206
+ const redactKnxAiTelegramVoiceLocations = (message) => {
207
+ if (!message || typeof message !== 'object') return message
208
+ if (message.payload && typeof message.payload === 'object') {
209
+ message.payload = Object.assign({}, message.payload)
210
+ if (Buffer.isBuffer(message.payload.content)) message.payload.content = ''
211
+ delete message.payload.weblink
212
+ delete message.payload.path
213
+ }
214
+ delete message.weblink
215
+ delete message.path
216
+ if (message.knxAi && message.knxAi.voiceInput && typeof message.knxAi.voiceInput === 'object') {
217
+ const voiceInput = Object.assign({}, message.knxAi.voiceInput)
218
+ delete voiceInput.data
219
+ delete voiceInput.weblink
220
+ delete voiceInput.path
221
+ message.knxAi = Object.assign({}, message.knxAi, { voiceInput })
222
+ }
223
+ return message
224
+ }
225
+
226
+ const responseHeader = (response, name) => {
227
+ if (!response || !response.headers) return ''
228
+ if (typeof response.headers.get === 'function') return String(response.headers.get(name) || '')
229
+ const normalizedName = String(name || '').toLowerCase()
230
+ const entry = Object.entries(response.headers).find(([key]) => String(key).toLowerCase() === normalizedName)
231
+ return entry ? String(entry[1] || '') : ''
232
+ }
233
+
234
+ const readBoundedResponseBuffer = async (response, maxBytes) => {
235
+ const limit = Math.max(1, Number(maxBytes) || KNX_AI_TELEGRAM_VOICE_MAX_BYTES)
236
+ const declaredLength = Number(responseHeader(response, 'content-length'))
237
+ if (Number.isFinite(declaredLength) && declaredLength > limit) {
238
+ throw new Error(`Voice audio exceeds the ${Math.round(limit / (1024 * 1024))} MB limit`)
239
+ }
240
+ if (response && response.body && typeof response.body.getReader === 'function') {
241
+ const reader = response.body.getReader()
242
+ const chunks = []
243
+ let total = 0
244
+ try {
245
+ while (true) {
246
+ const part = await reader.read()
247
+ if (part.done) break
248
+ const chunk = Buffer.from(part.value)
249
+ total += chunk.length
250
+ if (total > limit) {
251
+ try { await reader.cancel() } catch (error) { /* ignore */ }
252
+ throw new Error(`Voice audio exceeds the ${Math.round(limit / (1024 * 1024))} MB limit`)
253
+ }
254
+ chunks.push(chunk)
255
+ }
256
+ } finally {
257
+ try { reader.releaseLock() } catch (error) { /* ignore */ }
258
+ }
259
+ return Buffer.concat(chunks, total)
260
+ }
261
+ if (!response || typeof response.arrayBuffer !== 'function') throw new Error('Voice download returned no audio data')
262
+ const data = Buffer.from(await response.arrayBuffer())
263
+ if (data.length > limit) throw new Error(`Voice audio exceeds the ${Math.round(limit / (1024 * 1024))} MB limit`)
264
+ return data
265
+ }
266
+
267
+ const extractAudioApiError = (text) => {
268
+ const raw = String(text || '').trim()
269
+ if (!raw) return ''
270
+ try {
271
+ const json = JSON.parse(raw)
272
+ const candidate = json && json.error && json.error.message
273
+ ? json.error.message
274
+ : json && (json.message || json.detail || json.error)
275
+ if (candidate) return String(candidate).replace(/\s+/g, ' ').trim().slice(0, 1200)
276
+ } catch (error) { /* use response text */ }
277
+ return raw.replace(/\s+/g, ' ').slice(0, 1200)
278
+ }
279
+
280
+ const fetchKnxAiTelegramVoice = async ({
281
+ voiceInput,
282
+ fetchImpl = globalThis.fetch,
283
+ timeoutMs = KNX_AI_VOICE_API_TIMEOUT_MS,
284
+ maxBytes = KNX_AI_TELEGRAM_VOICE_MAX_BYTES
285
+ } = {}) => {
286
+ const source = voiceInput && typeof voiceInput === 'object' ? voiceInput : {}
287
+ const declaredSize = Math.max(0, Number(source.fileSize) || 0)
288
+ if (declaredSize > maxBytes) throw new Error(`Voice audio exceeds the ${Math.round(maxBytes / (1024 * 1024))} MB limit`)
289
+ const declaredDuration = Math.max(0, Number(source.durationSeconds) || 0)
290
+ if (declaredDuration > KNX_AI_TELEGRAM_VOICE_MAX_DURATION_SECONDS) {
291
+ throw new Error(`Voice message exceeds the ${Math.round(KNX_AI_TELEGRAM_VOICE_MAX_DURATION_SECONDS / 60)}-minute limit`)
292
+ }
293
+ const mediaType = normalizeVoiceMediaType(source.mediaType)
294
+ const filename = sanitizeVoiceFilename({ filename: source.filename, mediaType })
295
+ if (Buffer.isBuffer(source.data)) {
296
+ if (!source.data.length) throw new Error('Telegram returned an empty voice message')
297
+ if (source.data.length > maxBytes) throw new Error(`Voice audio exceeds the ${Math.round(maxBytes / (1024 * 1024))} MB limit`)
298
+ return {
299
+ data: Buffer.from(source.data),
300
+ mediaType,
301
+ filename,
302
+ source: String(source.transport || 'telegram-buffer')
303
+ }
304
+ }
305
+ const weblink = String(source.weblink || '').trim()
306
+ if (weblink) {
307
+ let parsed
308
+ try {
309
+ parsed = new URL(weblink)
310
+ } catch (error) {
311
+ throw new Error('Telegram returned an invalid voice download link')
312
+ }
313
+ if (parsed.protocol !== 'https:' && parsed.protocol !== 'http:') {
314
+ throw new Error('Telegram voice download link must use HTTP or HTTPS')
315
+ }
316
+ if (parsed.username || parsed.password) throw new Error('Telegram voice download link must not contain credentials')
317
+ const allowedOrigin = String(source.allowedOrigin || 'https://api.telegram.org').trim()
318
+ if (parsed.origin !== allowedOrigin) throw new Error('Telegram voice download link has an unexpected origin')
319
+ if (typeof fetchImpl !== 'function') throw new Error('Voice download is unavailable in this Node.js runtime')
320
+ const controller = new AbortController()
321
+ const timer = setTimeout(() => controller.abort(), Math.max(1000, Number(timeoutMs) || KNX_AI_VOICE_API_TIMEOUT_MS))
322
+ try {
323
+ let response
324
+ try {
325
+ response = await fetchImpl(parsed.toString(), { signal: controller.signal, redirect: 'manual' })
326
+ } catch (error) {
327
+ if (error && error.name === 'AbortError') throw new Error('Telegram voice download timed out')
328
+ throw new Error('Telegram voice download failed (network error)')
329
+ }
330
+ if (!response || !response.ok) {
331
+ const status = response && response.status ? `HTTP ${response.status}` : 'network error'
332
+ throw new Error(`Telegram voice download failed (${status})`)
333
+ }
334
+ const data = await readBoundedResponseBuffer(response, maxBytes)
335
+ if (!data.length) throw new Error('Telegram returned an empty voice message')
336
+ return {
337
+ data,
338
+ mediaType: normalizeVoiceMediaType(responseHeader(response, 'content-type') || mediaType),
339
+ filename,
340
+ source: 'telegram-weblink'
341
+ }
342
+ } finally {
343
+ clearTimeout(timer)
344
+ }
345
+ }
346
+
347
+ throw new Error('Telegram did not provide a downloadable voice link')
348
+ }
349
+
350
+ const postKnxAiVoiceTranscription = async ({
351
+ url,
352
+ apiKey,
353
+ audio,
354
+ model = KNX_AI_VOICE_TRANSCRIPTION_MODEL,
355
+ language = '',
356
+ fetchImpl = globalThis.fetch,
357
+ timeoutMs = KNX_AI_VOICE_API_TIMEOUT_MS
358
+ } = {}) => {
359
+ if (!audio || !Buffer.isBuffer(audio.data) || !audio.data.length) throw new Error('Missing voice audio to transcribe')
360
+ if (typeof fetchImpl !== 'function' || typeof globalThis.FormData !== 'function' || typeof globalThis.Blob !== 'function') {
361
+ throw new Error('Voice transcription requires Node.js FormData support')
362
+ }
363
+ const form = new globalThis.FormData()
364
+ form.append('file', new globalThis.Blob([audio.data], { type: normalizeVoiceMediaType(audio.mediaType) }), sanitizeVoiceFilename(audio))
365
+ form.append('model', String(model || KNX_AI_VOICE_TRANSCRIPTION_MODEL))
366
+ const normalizedLanguage = String(language || '').trim().toLowerCase().split(/[-_]/)[0]
367
+ if (/^[a-z]{2,3}$/.test(normalizedLanguage)) form.append('language', normalizedLanguage)
368
+ const controller = new AbortController()
369
+ const timer = setTimeout(() => controller.abort(), Math.max(1000, Number(timeoutMs) || KNX_AI_VOICE_API_TIMEOUT_MS))
370
+ try {
371
+ let response
372
+ try {
373
+ response = await fetchImpl(url, {
374
+ method: 'POST',
375
+ headers: apiKey ? { authorization: `Bearer ${apiKey}` } : {},
376
+ body: form,
377
+ signal: controller.signal
378
+ })
379
+ } catch (error) {
380
+ if (error && error.name === 'AbortError') throw new Error('Voice transcription timed out')
381
+ throw new Error('Voice transcription failed (network error)')
382
+ }
383
+ const text = await response.text()
384
+ if (!response.ok) {
385
+ const detail = extractAudioApiError(text)
386
+ throw new Error(detail ? `Voice transcription failed (HTTP ${response.status}: ${detail})` : `Voice transcription failed (HTTP ${response.status})`)
387
+ }
388
+ let json
389
+ try { json = JSON.parse(text) } catch (error) { json = null }
390
+ const transcript = String(json && json.text !== undefined ? json.text : text).trim()
391
+ if (!transcript) throw new Error('Voice transcription returned no text')
392
+ return { text: transcript, model: String(model || KNX_AI_VOICE_TRANSCRIPTION_MODEL) }
393
+ } finally {
394
+ clearTimeout(timer)
395
+ }
396
+ }
397
+
398
+ const postKnxAiVoiceSpeech = async ({
399
+ url,
400
+ apiKey,
401
+ text,
402
+ model = KNX_AI_VOICE_SPEECH_MODEL,
403
+ voice = KNX_AI_VOICE_SPEECH_VOICE,
404
+ fetchImpl = globalThis.fetch,
405
+ timeoutMs = KNX_AI_VOICE_API_TIMEOUT_MS,
406
+ maxBytes = KNX_AI_TELEGRAM_VOICE_MAX_BYTES
407
+ } = {}) => {
408
+ const input = String(text === undefined || text === null ? '' : text).trim()
409
+ if (!input) throw new Error('Missing text for voice reply')
410
+ if (input.length > KNX_AI_VOICE_SPEECH_MAX_CHARS) {
411
+ throw new Error(`Voice reply exceeds the ${KNX_AI_VOICE_SPEECH_MAX_CHARS}-character speech limit`)
412
+ }
413
+ if (typeof fetchImpl !== 'function') throw new Error('Voice synthesis is unavailable in this Node.js runtime')
414
+ const controller = new AbortController()
415
+ const timer = setTimeout(() => controller.abort(), Math.max(1000, Number(timeoutMs) || KNX_AI_VOICE_API_TIMEOUT_MS))
416
+ try {
417
+ let response
418
+ try {
419
+ response = await fetchImpl(url, {
420
+ method: 'POST',
421
+ headers: Object.assign(
422
+ { 'content-type': 'application/json' },
423
+ apiKey ? { authorization: `Bearer ${apiKey}` } : {}
424
+ ),
425
+ body: JSON.stringify({
426
+ model: String(model || KNX_AI_VOICE_SPEECH_MODEL),
427
+ voice: String(voice || KNX_AI_VOICE_SPEECH_VOICE),
428
+ input,
429
+ response_format: 'opus'
430
+ }),
431
+ signal: controller.signal
432
+ })
433
+ } catch (error) {
434
+ if (error && error.name === 'AbortError') throw new Error('Voice synthesis timed out')
435
+ throw new Error('Voice synthesis failed (network error)')
436
+ }
437
+ if (!response.ok) {
438
+ const detail = extractAudioApiError(await response.text())
439
+ throw new Error(detail ? `Voice synthesis failed (HTTP ${response.status}: ${detail})` : `Voice synthesis failed (HTTP ${response.status})`)
440
+ }
441
+ const data = await readBoundedResponseBuffer(response, maxBytes)
442
+ if (!data.length) throw new Error('Voice synthesis returned no audio')
443
+ return {
444
+ data,
445
+ mediaType: 'audio/ogg',
446
+ filename: 'knx-ai-reply.ogg',
447
+ model: String(model || KNX_AI_VOICE_SPEECH_MODEL),
448
+ voice: String(voice || KNX_AI_VOICE_SPEECH_VOICE)
449
+ }
450
+ } finally {
451
+ clearTimeout(timer)
452
+ }
453
+ }
454
+
455
+ const applyKnxAiTelegramVoiceOutputPresetFallback = ({ preset, message, inputMessage } = {}) => {
456
+ const normalizedPreset = String(preset || '')
457
+ if (!['windkh-telegrambot', 'redbot-telegram'].includes(normalizedPreset) || !message || typeof message !== 'object') return message
458
+ const audio = message.knxAi && message.knxAi.audio
459
+ if (!audio || !Buffer.isBuffer(audio.data) || !audio.data.length) return message
460
+ const currentPayload = message.payload && typeof message.payload === 'object' ? message.payload : null
461
+ if (currentPayload && (currentPayload.type === 'voice' || currentPayload.type === 'audio' || currentPayload.type === 'photo')) return message
462
+ if (normalizedPreset === 'redbot-telegram' && currentPayload && currentPayload.type === 'inline-buttons') return message
463
+ const confirmation = message.knxAi && message.knxAi.confirmationRequest
464
+ if (normalizedPreset === 'redbot-telegram' && confirmation && confirmation.required === true) return message
465
+ const source = inputMessage && typeof inputMessage === 'object'
466
+ ? inputMessage
467
+ : message.inputMessage && typeof message.inputMessage === 'object'
468
+ ? message.inputMessage
469
+ : message
470
+ const sourcePayload = source.payload && typeof source.payload === 'object' ? source.payload : {}
471
+ const chatId = currentPayload && currentPayload.chatId !== undefined
472
+ ? currentPayload.chatId
473
+ : sourcePayload.chatId !== undefined
474
+ ? sourcePayload.chatId
475
+ : source.chatId
476
+ if (chatId === undefined || chatId === null || chatId === '') return message
477
+ let caption = currentPayload ? currentPayload.content : message.payload
478
+ if (caption && typeof caption === 'object') caption = caption.error || caption.message || ''
479
+ const language = message.knxAi && message.knxAi.language
480
+ ? message.knxAi.language
481
+ : source.language
482
+ caption = [getAiVoiceDisclosure(language), String(caption === undefined || caption === null ? '' : caption)]
483
+ .filter(Boolean)
484
+ .join('\n')
485
+ .slice(0, 1024)
486
+
487
+ if (normalizedPreset === 'redbot-telegram') {
488
+ const transport = currentPayload && currentPayload.transport
489
+ ? currentPayload.transport
490
+ : sourcePayload.transport || 'telegram'
491
+ const userId = currentPayload && currentPayload.userId !== undefined
492
+ ? currentPayload.userId
493
+ : sourcePayload.userId
494
+ message.payload = {
495
+ transport,
496
+ chatId,
497
+ type: 'audio',
498
+ inbound: false,
499
+ content: audio.data,
500
+ filename: sanitizeVoiceFilename({ filename: audio.filename, mediaType: audio.mediaType, fallback: 'knx-ai-reply' }),
501
+ mimeType: normalizeVoiceMediaType(audio.mediaType),
502
+ caption
503
+ }
504
+ if (userId !== undefined) message.payload.userId = userId
505
+ return message
506
+ }
507
+
508
+ const options = Object.assign({}, currentPayload && currentPayload.options ? currentPayload.options : {})
509
+ if (caption) options.caption = caption
510
+ message.payload = {
511
+ chatId,
512
+ type: 'voice',
513
+ content: audio.data,
514
+ options,
515
+ fileOptions: {
516
+ filename: sanitizeVoiceFilename({ filename: audio.filename, mediaType: audio.mediaType, fallback: 'knx-ai-reply' }),
517
+ contentType: normalizeVoiceMediaType(audio.mediaType)
518
+ }
519
+ }
520
+ return message
521
+ }
522
+
523
+ module.exports = {
524
+ KNX_AI_TELEGRAM_VOICE_MAX_BYTES,
525
+ KNX_AI_TELEGRAM_VOICE_MAX_DURATION_SECONDS,
526
+ KNX_AI_VOICE_API_TIMEOUT_MS,
527
+ KNX_AI_VOICE_DEFAULT_BASE_URL,
528
+ KNX_AI_VOICE_SPEECH_MAX_CHARS,
529
+ KNX_AI_VOICE_SPEECH_MODEL,
530
+ KNX_AI_VOICE_SPEECH_VOICE,
531
+ KNX_AI_VOICE_TRANSCRIPTION_MODEL,
532
+ applyKnxAiTelegramVoiceInputPresetFallback,
533
+ applyKnxAiTelegramVoiceOutputPresetFallback,
534
+ deriveOpenAiCompatibleAudioUrl,
535
+ fetchKnxAiTelegramVoice,
536
+ isKnxAiOpenAiCompatibleChatProvider,
537
+ isKnxAiTelegramVoiceInput,
538
+ isOfficialOpenAiVoiceUrl,
539
+ normalizeKnxAiLlmProvider,
540
+ postKnxAiVoiceSpeech,
541
+ postKnxAiVoiceTranscription,
542
+ readBoundedResponseBuffer,
543
+ redactKnxAiTelegramVoiceLocations,
544
+ resolveKnxAiVoiceServiceConfig
545
+ }