Skip to content

Commit 95a63fa

Browse files
committed
Update generate tts endpoint and its usage in internal chat
1 parent 2e33a00 commit 95a63fa

File tree

3 files changed

+57
-41
lines changed

3 files changed

+57
-41
lines changed

packages/server/src/controllers/text-to-speech/index.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Request, Response, NextFunction } from 'express'
2+
import chatflowsService from '../../services/chatflows'
23
import textToSpeechService from '../../services/text-to-speech'
34
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
45
import { StatusCodes } from 'http-status-codes'
@@ -8,7 +9,16 @@ import { databaseEntities } from '../../utils'
89

910
const generateTextToSpeech = async (req: Request, res: Response) => {
1011
try {
11-
const { chatMessageId, text, provider, credentialId, voice, model } = req.body
12+
const {
13+
chatId,
14+
chatflowId,
15+
chatMessageId,
16+
text,
17+
provider: bodyProvider,
18+
credentialId: bodyCredentialId,
19+
voice: bodyVoice,
20+
model: bodyModel
21+
} = req.body
1222

1323
if (!text) {
1424
throw new InternalFlowiseError(
@@ -17,6 +27,35 @@ const generateTextToSpeech = async (req: Request, res: Response) => {
1727
)
1828
}
1929

30+
let provider: string, credentialId: string, voice: string, model: string
31+
32+
if (chatflowId) {
33+
// Get TTS config from chatflow
34+
const chatflow = await chatflowsService.getChatflowById(chatflowId)
35+
const ttsConfig = JSON.parse(chatflow.textToSpeech)
36+
37+
// Extract the first provider config (assuming single provider per chatflow)
38+
const providerKey = Object.keys(ttsConfig)[0]
39+
if (!providerKey) {
40+
throw new InternalFlowiseError(
41+
StatusCodes.BAD_REQUEST,
42+
`Error: textToSpeechController.generateTextToSpeech - no TTS provider configured in chatflow!`
43+
)
44+
}
45+
46+
const providerConfig = ttsConfig[providerKey]
47+
provider = providerKey
48+
credentialId = providerConfig.credentialId
49+
voice = providerConfig.voice
50+
model = providerConfig.model
51+
} else {
52+
// Use TTS config from request body
53+
provider = bodyProvider
54+
credentialId = bodyCredentialId
55+
voice = bodyVoice
56+
model = bodyModel
57+
}
58+
2059
if (!provider) {
2160
throw new InternalFlowiseError(
2261
StatusCodes.BAD_REQUEST,
@@ -40,8 +79,8 @@ const generateTextToSpeech = async (req: Request, res: Response) => {
4079
const appServer = getRunningExpressApp()
4180
const options = {
4281
orgId: '',
43-
chatflowid: '',
44-
chatId: '',
82+
chatflowid: chatflowId || '',
83+
chatId: chatId || '',
4584
appDataSource: appServer.AppDataSource,
4685
databaseEntities: databaseEntities
4786
}

packages/server/src/services/chatflows/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,18 @@ const getSinglePublicChatbotConfig = async (chatflowId: string): Promise<any> =>
357357
if (dbResponse.chatbotConfig || uploadsConfig) {
358358
try {
359359
const parsedConfig = dbResponse.chatbotConfig ? JSON.parse(dbResponse.chatbotConfig) : {}
360-
return { ...parsedConfig, uploads: uploadsConfig, flowData: dbResponse.flowData }
360+
const ttsConfig =
361+
typeof dbResponse.textToSpeech === 'string' ? JSON.parse(dbResponse.textToSpeech) : dbResponse.textToSpeech
362+
363+
let isTTSEnabled = false
364+
if (ttsConfig) {
365+
Object.keys(ttsConfig).forEach((provider) => {
366+
if (ttsConfig?.[provider]?.status) {
367+
isTTSEnabled = true
368+
}
369+
})
370+
}
371+
return { ...parsedConfig, uploads: uploadsConfig, flowData: dbResponse.flowData, isTTSEnabled }
361372
} catch (e) {
362373
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error parsing Chatbot Config for Chatflow ${chatflowId}`)
363374
}

packages/ui/src/views/chatmessage/ChatMessage.jsx

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
13421342
let isEnabled = false
13431343
if (ttsConfig) {
13441344
Object.keys(ttsConfig).forEach((provider) => {
1345-
if (ttsConfig[provider] && ttsConfig[provider].status && ttsConfig[provider].credentialId) {
1345+
if (ttsConfig?.[provider]?.status) {
13461346
isEnabled = true
13471347
}
13481348
})
@@ -1625,37 +1625,6 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
16251625

16261626
handleTTSStart({ chatMessageId: messageId, format: 'mp3' })
16271627
try {
1628-
let ttsConfig = null
1629-
if (getChatflowConfig?.data?.textToSpeech) {
1630-
try {
1631-
ttsConfig =
1632-
typeof getChatflowConfig.data.textToSpeech === 'string'
1633-
? JSON.parse(getChatflowConfig.data.textToSpeech)
1634-
: getChatflowConfig.data.textToSpeech
1635-
} catch (error) {
1636-
console.error('Error parsing TTS config:', error)
1637-
}
1638-
}
1639-
1640-
let activeProvider = null
1641-
let providerConfig = null
1642-
if (ttsConfig) {
1643-
Object.keys(ttsConfig).forEach((provider) => {
1644-
if (ttsConfig?.[provider]?.status) {
1645-
activeProvider = provider
1646-
providerConfig = ttsConfig[provider]
1647-
}
1648-
})
1649-
}
1650-
1651-
if (!activeProvider || !providerConfig || !providerConfig.credentialId) {
1652-
enqueueSnackbar({
1653-
message: 'Text-to-speech is not configured for this chatflow',
1654-
options: { variant: 'warning' }
1655-
})
1656-
return
1657-
}
1658-
16591628
const abortController = new AbortController()
16601629
setTtsStreamingState((prev) => ({ ...prev, abortController }))
16611630

@@ -1668,13 +1637,10 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
16681637
credentials: 'include',
16691638
signal: abortController.signal,
16701639
body: JSON.stringify({
1640+
chatflowId: chatflowid,
16711641
chatId: chatId,
16721642
chatMessageId: messageId,
1673-
text: messageText,
1674-
provider: activeProvider,
1675-
credentialId: providerConfig.credentialId,
1676-
voice: providerConfig.voice,
1677-
model: providerConfig.model
1643+
text: messageText
16781644
})
16791645
})
16801646

0 commit comments

Comments
 (0)