Update generate tts endpoint and its usage in internal chat
This commit is contained in:
parent
2e33a00667
commit
95a63fa609
|
|
@ -1,4 +1,5 @@
|
||||||
import { Request, Response, NextFunction } from 'express'
|
import { Request, Response, NextFunction } from 'express'
|
||||||
|
import chatflowsService from '../../services/chatflows'
|
||||||
import textToSpeechService from '../../services/text-to-speech'
|
import textToSpeechService from '../../services/text-to-speech'
|
||||||
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
|
import { InternalFlowiseError } from '../../errors/internalFlowiseError'
|
||||||
import { StatusCodes } from 'http-status-codes'
|
import { StatusCodes } from 'http-status-codes'
|
||||||
|
|
@ -8,7 +9,16 @@ import { databaseEntities } from '../../utils'
|
||||||
|
|
||||||
const generateTextToSpeech = async (req: Request, res: Response) => {
|
const generateTextToSpeech = async (req: Request, res: Response) => {
|
||||||
try {
|
try {
|
||||||
const { chatMessageId, text, provider, credentialId, voice, model } = req.body
|
const {
|
||||||
|
chatId,
|
||||||
|
chatflowId,
|
||||||
|
chatMessageId,
|
||||||
|
text,
|
||||||
|
provider: bodyProvider,
|
||||||
|
credentialId: bodyCredentialId,
|
||||||
|
voice: bodyVoice,
|
||||||
|
model: bodyModel
|
||||||
|
} = req.body
|
||||||
|
|
||||||
if (!text) {
|
if (!text) {
|
||||||
throw new InternalFlowiseError(
|
throw new InternalFlowiseError(
|
||||||
|
|
@ -17,6 +27,35 @@ const generateTextToSpeech = async (req: Request, res: Response) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let provider: string, credentialId: string, voice: string, model: string
|
||||||
|
|
||||||
|
if (chatflowId) {
|
||||||
|
// Get TTS config from chatflow
|
||||||
|
const chatflow = await chatflowsService.getChatflowById(chatflowId)
|
||||||
|
const ttsConfig = JSON.parse(chatflow.textToSpeech)
|
||||||
|
|
||||||
|
// Extract the first provider config (assuming single provider per chatflow)
|
||||||
|
const providerKey = Object.keys(ttsConfig)[0]
|
||||||
|
if (!providerKey) {
|
||||||
|
throw new InternalFlowiseError(
|
||||||
|
StatusCodes.BAD_REQUEST,
|
||||||
|
`Error: textToSpeechController.generateTextToSpeech - no TTS provider configured in chatflow!`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const providerConfig = ttsConfig[providerKey]
|
||||||
|
provider = providerKey
|
||||||
|
credentialId = providerConfig.credentialId
|
||||||
|
voice = providerConfig.voice
|
||||||
|
model = providerConfig.model
|
||||||
|
} else {
|
||||||
|
// Use TTS config from request body
|
||||||
|
provider = bodyProvider
|
||||||
|
credentialId = bodyCredentialId
|
||||||
|
voice = bodyVoice
|
||||||
|
model = bodyModel
|
||||||
|
}
|
||||||
|
|
||||||
if (!provider) {
|
if (!provider) {
|
||||||
throw new InternalFlowiseError(
|
throw new InternalFlowiseError(
|
||||||
StatusCodes.BAD_REQUEST,
|
StatusCodes.BAD_REQUEST,
|
||||||
|
|
@ -40,8 +79,8 @@ const generateTextToSpeech = async (req: Request, res: Response) => {
|
||||||
const appServer = getRunningExpressApp()
|
const appServer = getRunningExpressApp()
|
||||||
const options = {
|
const options = {
|
||||||
orgId: '',
|
orgId: '',
|
||||||
chatflowid: '',
|
chatflowid: chatflowId || '',
|
||||||
chatId: '',
|
chatId: chatId || '',
|
||||||
appDataSource: appServer.AppDataSource,
|
appDataSource: appServer.AppDataSource,
|
||||||
databaseEntities: databaseEntities
|
databaseEntities: databaseEntities
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -357,7 +357,18 @@ const getSinglePublicChatbotConfig = async (chatflowId: string): Promise<any> =>
|
||||||
if (dbResponse.chatbotConfig || uploadsConfig) {
|
if (dbResponse.chatbotConfig || uploadsConfig) {
|
||||||
try {
|
try {
|
||||||
const parsedConfig = dbResponse.chatbotConfig ? JSON.parse(dbResponse.chatbotConfig) : {}
|
const parsedConfig = dbResponse.chatbotConfig ? JSON.parse(dbResponse.chatbotConfig) : {}
|
||||||
return { ...parsedConfig, uploads: uploadsConfig, flowData: dbResponse.flowData }
|
const ttsConfig =
|
||||||
|
typeof dbResponse.textToSpeech === 'string' ? JSON.parse(dbResponse.textToSpeech) : dbResponse.textToSpeech
|
||||||
|
|
||||||
|
let isTTSEnabled = false
|
||||||
|
if (ttsConfig) {
|
||||||
|
Object.keys(ttsConfig).forEach((provider) => {
|
||||||
|
if (ttsConfig?.[provider]?.status) {
|
||||||
|
isTTSEnabled = true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return { ...parsedConfig, uploads: uploadsConfig, flowData: dbResponse.flowData, isTTSEnabled }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error parsing Chatbot Config for Chatflow ${chatflowId}`)
|
throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, `Error parsing Chatbot Config for Chatflow ${chatflowId}`)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1342,7 +1342,7 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
|
||||||
let isEnabled = false
|
let isEnabled = false
|
||||||
if (ttsConfig) {
|
if (ttsConfig) {
|
||||||
Object.keys(ttsConfig).forEach((provider) => {
|
Object.keys(ttsConfig).forEach((provider) => {
|
||||||
if (ttsConfig[provider] && ttsConfig[provider].status && ttsConfig[provider].credentialId) {
|
if (ttsConfig?.[provider]?.status) {
|
||||||
isEnabled = true
|
isEnabled = true
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
@ -1625,37 +1625,6 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
|
||||||
|
|
||||||
handleTTSStart({ chatMessageId: messageId, format: 'mp3' })
|
handleTTSStart({ chatMessageId: messageId, format: 'mp3' })
|
||||||
try {
|
try {
|
||||||
let ttsConfig = null
|
|
||||||
if (getChatflowConfig?.data?.textToSpeech) {
|
|
||||||
try {
|
|
||||||
ttsConfig =
|
|
||||||
typeof getChatflowConfig.data.textToSpeech === 'string'
|
|
||||||
? JSON.parse(getChatflowConfig.data.textToSpeech)
|
|
||||||
: getChatflowConfig.data.textToSpeech
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error parsing TTS config:', error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let activeProvider = null
|
|
||||||
let providerConfig = null
|
|
||||||
if (ttsConfig) {
|
|
||||||
Object.keys(ttsConfig).forEach((provider) => {
|
|
||||||
if (ttsConfig?.[provider]?.status) {
|
|
||||||
activeProvider = provider
|
|
||||||
providerConfig = ttsConfig[provider]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!activeProvider || !providerConfig || !providerConfig.credentialId) {
|
|
||||||
enqueueSnackbar({
|
|
||||||
message: 'Text-to-speech is not configured for this chatflow',
|
|
||||||
options: { variant: 'warning' }
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const abortController = new AbortController()
|
const abortController = new AbortController()
|
||||||
setTtsStreamingState((prev) => ({ ...prev, abortController }))
|
setTtsStreamingState((prev) => ({ ...prev, abortController }))
|
||||||
|
|
||||||
|
|
@ -1668,13 +1637,10 @@ const ChatMessage = ({ open, chatflowid, isAgentCanvas, isDialog, previews, setP
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
signal: abortController.signal,
|
signal: abortController.signal,
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
chatflowId: chatflowid,
|
||||||
chatId: chatId,
|
chatId: chatId,
|
||||||
chatMessageId: messageId,
|
chatMessageId: messageId,
|
||||||
text: messageText,
|
text: messageText
|
||||||
provider: activeProvider,
|
|
||||||
credentialId: providerConfig.credentialId,
|
|
||||||
voice: providerConfig.voice,
|
|
||||||
model: providerConfig.model
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue