mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 17:01:00 +03:00
add retrieve internal chat message
This commit is contained in:
@@ -64,6 +64,7 @@
|
|||||||
"socket.io": "^4.6.1",
|
"socket.io": "^4.6.1",
|
||||||
"sqlite3": "^5.1.6",
|
"sqlite3": "^5.1.6",
|
||||||
"typeorm": "^0.3.6",
|
"typeorm": "^0.3.6",
|
||||||
|
"uuid": "^9.0.1",
|
||||||
"winston": "^3.9.0"
|
"winston": "^3.9.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import basicAuth from 'express-basic-auth'
|
|||||||
import { Server } from 'socket.io'
|
import { Server } from 'socket.io'
|
||||||
import logger from './utils/logger'
|
import logger from './utils/logger'
|
||||||
import { expressRequestLogger } from './utils/logger'
|
import { expressRequestLogger } from './utils/logger'
|
||||||
|
import { v4 as uuidv4 } from 'uuid'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IChatFlow,
|
IChatFlow,
|
||||||
@@ -391,14 +392,13 @@ export class App {
|
|||||||
|
|
||||||
// Get all chatmessages from chatflowid
|
// Get all chatmessages from chatflowid
|
||||||
this.app.get('/api/v1/chatmessage/:id', async (req: Request, res: Response) => {
|
this.app.get('/api/v1/chatmessage/:id', async (req: Request, res: Response) => {
|
||||||
const chatmessages = await this.AppDataSource.getRepository(ChatMessage).find({
|
const chatmessages = await this.getChatMessage(req.params.id, undefined)
|
||||||
where: {
|
return res.json(chatmessages)
|
||||||
chatflowid: req.params.id
|
})
|
||||||
},
|
|
||||||
order: {
|
// Get internal chatmessages from chatflowid
|
||||||
createdDate: 'ASC'
|
this.app.get('/api/v1/internal-chatmessage/:id', async (req: Request, res: Response) => {
|
||||||
}
|
const chatmessages = await this.getChatMessage(req.params.id, chatType.INTERNAL)
|
||||||
})
|
|
||||||
return res.json(chatmessages)
|
return res.json(chatmessages)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -815,10 +815,31 @@ export class App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add Chat Message
|
* Method that get chat messages.
|
||||||
* @param {any} chatMessage
|
*
|
||||||
|
* @param chatflowid -
|
||||||
|
* @param chatType -
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
async addChatMessage(chatMessage: any) {
|
async getChatMessage(chatflowid: string, chatType: chatType | undefined): Promise<ChatMessage[]> {
|
||||||
|
return await this.AppDataSource.getRepository(ChatMessage).find({
|
||||||
|
where: {
|
||||||
|
chatflowid: chatflowid,
|
||||||
|
chatType: chatType
|
||||||
|
},
|
||||||
|
order: {
|
||||||
|
createdDate: 'ASC'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that add chat messages.
|
||||||
|
*
|
||||||
|
* @param chatMessage -
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
async addChatMessage(chatMessage: any): Promise<ChatMessage> {
|
||||||
const newChatMessage = new ChatMessage()
|
const newChatMessage = new ChatMessage()
|
||||||
Object.assign(newChatMessage, chatMessage)
|
Object.assign(newChatMessage, chatMessage)
|
||||||
|
|
||||||
@@ -826,6 +847,20 @@ export class App {
|
|||||||
return await this.AppDataSource.getRepository(ChatMessage).save(chatmessage)
|
return await this.AppDataSource.getRepository(ChatMessage).save(chatmessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that find memory label.
|
||||||
|
*
|
||||||
|
* @param nodes -
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
findMemoryLabel(nodes: any[]): string | undefined {
|
||||||
|
const memoryNode = nodes.find((node) => {
|
||||||
|
return node.data.category === 'Memory'
|
||||||
|
})
|
||||||
|
|
||||||
|
return memoryNode ? memoryNode.data.label : undefined
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process Prediction
|
* Process Prediction
|
||||||
* @param {Request} req
|
* @param {Request} req
|
||||||
@@ -845,20 +880,13 @@ export class App {
|
|||||||
})
|
})
|
||||||
if (!chatflow) return res.status(404).send(`Chatflow ${chatflowid} not found`)
|
if (!chatflow) return res.status(404).send(`Chatflow ${chatflowid} not found`)
|
||||||
|
|
||||||
let chatId = await getChatId(chatflow.id)
|
const chatId = incomingInput.chatId ?? uuidv4()
|
||||||
if (!chatId) chatId = chatflowid
|
const userMessageDateTime = new Date()
|
||||||
|
|
||||||
if (!isInternal) {
|
if (!isInternal) {
|
||||||
await this.validateKey(req, res, chatflow)
|
await this.validateKey(req, res, chatflow)
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.addChatMessage({
|
|
||||||
role: 'userMessage',
|
|
||||||
content: incomingInput.question,
|
|
||||||
chatflowid: chatflowid,
|
|
||||||
chatType: isInternal ? chatType.INTERNAL : chatType.EXTERNAL
|
|
||||||
})
|
|
||||||
|
|
||||||
let isStreamValid = false
|
let isStreamValid = false
|
||||||
|
|
||||||
const files = (req.files as any[]) || []
|
const files = (req.files as any[]) || []
|
||||||
@@ -986,9 +1014,12 @@ export class App {
|
|||||||
|
|
||||||
logger.debug(`[server]: Running ${nodeToExecuteData.label} (${nodeToExecuteData.id})`)
|
logger.debug(`[server]: Running ${nodeToExecuteData.label} (${nodeToExecuteData.id})`)
|
||||||
|
|
||||||
if (nodeToExecuteData.instance) checkMemorySessionId(nodeToExecuteData.instance, chatId)
|
let sessionId = undefined
|
||||||
|
let memoryLabel = undefined
|
||||||
|
if (nodeToExecuteData.instance) sessionId = checkMemorySessionId(nodeToExecuteData.instance, chatId)
|
||||||
|
if (sessionId) memoryLabel = this.findMemoryLabel(nodes)
|
||||||
|
|
||||||
const result = isStreamValid
|
let result = isStreamValid
|
||||||
? await nodeInstance.run(nodeToExecuteData, incomingInput.question, {
|
? await nodeInstance.run(nodeToExecuteData, incomingInput.question, {
|
||||||
chatHistory: incomingInput.history,
|
chatHistory: incomingInput.history,
|
||||||
socketIO,
|
socketIO,
|
||||||
@@ -1006,16 +1037,33 @@ export class App {
|
|||||||
analytic: chatflow.analytic
|
analytic: chatflow.analytic
|
||||||
})
|
})
|
||||||
|
|
||||||
|
result = typeof result === 'string' ? { text: result } : result
|
||||||
|
|
||||||
|
await this.addChatMessage({
|
||||||
|
role: 'userMessage',
|
||||||
|
content: incomingInput.question,
|
||||||
|
chatflowid: chatflowid,
|
||||||
|
chatType: isInternal ? chatType.INTERNAL : chatType.EXTERNAL,
|
||||||
|
chatId: chatId,
|
||||||
|
memoryType: memoryLabel ? memoryLabel : undefined,
|
||||||
|
sessionId: sessionId ? sessionId : undefined,
|
||||||
|
createdDate: userMessageDateTime
|
||||||
|
})
|
||||||
|
|
||||||
const apiMessage: any = {
|
const apiMessage: any = {
|
||||||
role: 'apiMessage',
|
role: 'apiMessage',
|
||||||
content: typeof result === 'string' ? result : result.text,
|
content: result.text,
|
||||||
chatflowid: chatflowid,
|
chatflowid: chatflowid,
|
||||||
chatType: isInternal ? chatType.INTERNAL : chatType.EXTERNAL
|
chatType: isInternal ? chatType.INTERNAL : chatType.EXTERNAL,
|
||||||
|
chatId: chatId,
|
||||||
|
memoryType: memoryLabel ? memoryLabel : undefined,
|
||||||
|
sessionId: sessionId ? sessionId : undefined
|
||||||
}
|
}
|
||||||
if (result?.sourceDocuments) apiMessage.sourceDocuments = JSON.stringify(result.sourceDocuments)
|
if (result?.sourceDocuments) apiMessage.sourceDocuments = JSON.stringify(result.sourceDocuments)
|
||||||
await this.addChatMessage(apiMessage)
|
await this.addChatMessage(apiMessage)
|
||||||
|
|
||||||
logger.debug(`[server]: Finished running ${nodeToExecuteData.label} (${nodeToExecuteData.id})`)
|
logger.debug(`[server]: Finished running ${nodeToExecuteData.label} (${nodeToExecuteData.id})`)
|
||||||
|
result.chatId = chatId
|
||||||
return res.json(result)
|
return res.json(result)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
logger.error('[server]: Error:', e)
|
logger.error('[server]: Error:', e)
|
||||||
|
|||||||
@@ -922,8 +922,9 @@ export const redactCredentialWithPasswordType = (
|
|||||||
* @param {any} instance
|
* @param {any} instance
|
||||||
* @param {string} chatId
|
* @param {string} chatId
|
||||||
*/
|
*/
|
||||||
export const checkMemorySessionId = (instance: any, chatId: string) => {
|
export const checkMemorySessionId = (instance: any, chatId: string): string => {
|
||||||
if (instance.memory && instance.memory.isSessionIdUsingChatMessageId && chatId) {
|
if (instance.memory && instance.memory.isSessionIdUsingChatMessageId && chatId) {
|
||||||
instance.memory.sessionId = chatId
|
instance.memory.sessionId = chatId
|
||||||
}
|
}
|
||||||
|
return instance.memory.sessionId
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import client from './client'
|
import client from './client'
|
||||||
|
|
||||||
const getChatmessageFromChatflow = (id) => client.get(`/chatmessage/${id}`)
|
const getChatmessageFromChatflow = (id) => client.get(`/internal-chatmessage/${id}`)
|
||||||
|
|
||||||
const deleteChatmessage = (id) => client.delete(`/chatmessage/${id}`)
|
const deleteChatmessage = (id) => client.delete(`/chatmessage/${id}`)
|
||||||
|
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
|
|||||||
const [isChatFlowAvailableToStream, setIsChatFlowAvailableToStream] = useState(false)
|
const [isChatFlowAvailableToStream, setIsChatFlowAvailableToStream] = useState(false)
|
||||||
const [sourceDialogOpen, setSourceDialogOpen] = useState(false)
|
const [sourceDialogOpen, setSourceDialogOpen] = useState(false)
|
||||||
const [sourceDialogProps, setSourceDialogProps] = useState({})
|
const [sourceDialogProps, setSourceDialogProps] = useState({})
|
||||||
|
const [chatId, setChatId] = useState(undefined)
|
||||||
|
|
||||||
const inputRef = useRef(null)
|
const inputRef = useRef(null)
|
||||||
const getChatmessageApi = useApi(chatmessageApi.getChatmessageFromChatflow)
|
const getChatmessageApi = useApi(chatmessageApi.getChatmessageFromChatflow)
|
||||||
@@ -148,7 +149,8 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
|
|||||||
try {
|
try {
|
||||||
const params = {
|
const params = {
|
||||||
question: userInput,
|
question: userInput,
|
||||||
history: messages.filter((msg) => msg.message !== 'Hi there! How can I help?')
|
history: messages.filter((msg) => msg.message !== 'Hi there! How can I help?'),
|
||||||
|
chatId
|
||||||
}
|
}
|
||||||
if (isChatFlowAvailableToStream) params.socketIOClientId = socketIOClientId
|
if (isChatFlowAvailableToStream) params.socketIOClientId = socketIOClientId
|
||||||
|
|
||||||
@@ -159,18 +161,16 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
|
|||||||
|
|
||||||
data = handleVectaraMetadata(data)
|
data = handleVectaraMetadata(data)
|
||||||
|
|
||||||
if (typeof data === 'object' && data.text && data.sourceDocuments) {
|
if (!chatId) {
|
||||||
if (!isChatFlowAvailableToStream) {
|
setChatId(data.chatId)
|
||||||
setMessages((prevMessages) => [
|
|
||||||
...prevMessages,
|
|
||||||
{ message: data.text, sourceDocuments: data.sourceDocuments, type: 'apiMessage' }
|
|
||||||
])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!isChatFlowAvailableToStream) {
|
|
||||||
setMessages((prevMessages) => [...prevMessages, { message: data, type: 'apiMessage' }])
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (!isChatFlowAvailableToStream) {
|
||||||
|
setMessages((prevMessages) => [
|
||||||
|
...prevMessages,
|
||||||
|
{ message: data.text, sourceDocuments: data?.sourceDocuments, type: 'apiMessage' }
|
||||||
|
])
|
||||||
|
}
|
||||||
|
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
setUserInput('')
|
setUserInput('')
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
@@ -201,15 +201,15 @@ export const ChatMessage = ({ open, chatflowid, isDialog }) => {
|
|||||||
// Get chatmessages successful
|
// Get chatmessages successful
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (getChatmessageApi.data) {
|
if (getChatmessageApi.data) {
|
||||||
const loadedMessages = []
|
setChatId(getChatmessageApi.data[0]?.chatId)
|
||||||
for (const message of getChatmessageApi.data) {
|
const loadedMessages = getChatmessageApi.data.map((message) => {
|
||||||
const obj = {
|
const obj = {
|
||||||
message: message.content,
|
message: message.content,
|
||||||
type: message.role
|
type: message.role
|
||||||
}
|
}
|
||||||
if (message.sourceDocuments) obj.sourceDocuments = JSON.parse(message.sourceDocuments)
|
if (message.sourceDocuments) obj.sourceDocuments = JSON.parse(message.sourceDocuments)
|
||||||
loadedMessages.push(obj)
|
return obj
|
||||||
}
|
})
|
||||||
setMessages((prevMessages) => [...prevMessages, ...loadedMessages])
|
setMessages((prevMessages) => [...prevMessages, ...loadedMessages])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user