Skip to content

Bugfix/Prevent empty user message when using STT #4280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions packages/components/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as path from 'path'
import { JSDOM } from 'jsdom'
import { z } from 'zod'
import { DataSource } from 'typeorm'
import { ICommonObject, IDatabaseEntity, IDocument, IMessage, INodeData, IVariable, MessageContentImageUrl } from './Interface'
import { ICommonObject, IDatabaseEntity, IFileUpload, IMessage, INodeData, IVariable, MessageContentImageUrl } from './Interface'
import { AES, enc } from 'crypto-js'
import { omit } from 'lodash'
import { AIMessage, HumanMessage, BaseMessage } from '@langchain/core/messages'
Expand Down Expand Up @@ -718,10 +718,10 @@ export const mapChatMessageToBaseMessage = async (chatmessages: any[] = []): Pro
// example: [{"type":"stored-file","name":"0_DiXc4ZklSTo3M8J4.jpg","mime":"image/jpeg"}]
try {
let messageWithFileUploads = ''
const uploads = JSON.parse(message.fileUploads)
const uploads: IFileUpload[] = JSON.parse(message.fileUploads)
const imageContents: MessageContentImageUrl[] = []
for (const upload of uploads) {
if (upload.type === 'stored-file' && upload.mime.startsWith('image')) {
if (upload.type === 'stored-file' && upload.mime.startsWith('image/')) {
const fileData = await getFileFromStorage(upload.name, message.chatflowid, message.chatId)
// as the image is stored in the server, read the file and convert it to base64
const bf = 'data:' + upload.mime + ';base64,' + fileData.toString('base64')
Expand All @@ -732,7 +732,7 @@ export const mapChatMessageToBaseMessage = async (chatmessages: any[] = []): Pro
url: bf
}
})
} else if (upload.type === 'url' && upload.mime.startsWith('image')) {
} else if (upload.type === 'url' && upload.mime.startsWith('image') && upload.data) {
imageContents.push({
type: 'image_url',
image_url: {
Expand All @@ -748,14 +748,15 @@ export const mapChatMessageToBaseMessage = async (chatmessages: any[] = []): Pro
chatflowid: message.chatflowid,
chatId: message.chatId
}
let fileInputFieldFromMimeType = 'txtFile'
fileInputFieldFromMimeType = mapMimeTypeToInputField(upload.mime)
const nodeData = {
inputs: {
txtFile: `FILE-STORAGE::${JSON.stringify([upload.name])}`
[fileInputFieldFromMimeType]: `FILE-STORAGE::${JSON.stringify([upload.name])}`
}
}
const documents: IDocument[] = await fileLoaderNodeInstance.init(nodeData, '', options)
const pageContents = documents.map((doc) => doc.pageContent).join('\n')
messageWithFileUploads += `<doc name='${upload.name}'>${pageContents}</doc>\n\n`
const documents: string = await fileLoaderNodeInstance.init(nodeData, '', options)
messageWithFileUploads += `<doc name='${upload.name}'>${documents}</doc>\n\n`
}
}
const messageContent = messageWithFileUploads ? `${messageWithFileUploads}\n\n${message.content}` : message.content
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/utils/buildChatflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export const executeFlow = async ({
...incomingInput
}

const question = incomingInput.question || '' // Ensure question is never undefined
let question = incomingInput.question || '' // Ensure question is never undefined
let overrideConfig = incomingInput.overrideConfig ?? {}
const uploads = incomingInput.uploads
const prependMessages = incomingInput.history ?? []
Expand Down Expand Up @@ -308,6 +308,7 @@ export const executeFlow = async ({
logger.debug(`Speech to text result: ${speechToTextResult}`)
if (speechToTextResult) {
incomingInput.question = speechToTextResult
question = speechToTextResult
}
}
}
Expand Down