Filter by feedback type in view messages dialog (#2869)

* Update get chat messages and stats filter to work with feedback type filter

* Add feedback type filter to view messages dialog

* Fix issues with feedback type filter
This commit is contained in:
Ilango
2024-07-26 20:17:58 +05:30
committed by GitHub
parent 1c730323e2
commit e5018d2743
6 changed files with 175 additions and 18 deletions
+22 -3
View File
@@ -1,8 +1,9 @@
import { MoreThanOrEqual, LessThanOrEqual } from 'typeorm'
import { chatType } from '../Interface'
import { ChatMessageRatingType, chatType } from '../Interface'
import { ChatMessage } from '../database/entities/ChatMessage'
import { ChatMessageFeedback } from '../database/entities/ChatMessageFeedback'
import { getRunningExpressApp } from '../utils/getRunningExpressApp'
/**
* Method that get chat messages.
* @param {string} chatflowid
@@ -14,6 +15,7 @@ import { getRunningExpressApp } from '../utils/getRunningExpressApp'
* @param {string} startDate
* @param {string} endDate
* @param {boolean} feedback
* @param {ChatMessageRatingType[]} feedbackTypes
*/
export const utilGetChatMessage = async (
chatflowid: string,
@@ -25,7 +27,8 @@ export const utilGetChatMessage = async (
startDate?: string,
endDate?: string,
messageId?: string,
feedback?: boolean
feedback?: boolean,
feedbackTypes?: ChatMessageRatingType[]
): Promise<ChatMessage[]> => {
const appServer = getRunningExpressApp()
const setDateToStartOrEndOfDay = (dateTimeStr: string, setHours: 'start' | 'end') => {
@@ -79,7 +82,23 @@ export const utilGetChatMessage = async (
// sort
query.orderBy('chat_message.createdDate', sortOrder === 'DESC' ? 'DESC' : 'ASC')
const messages = await query.getMany()
const messages = (await query.getMany()) as Array<ChatMessage & { feedback: ChatMessageFeedback }>
if (feedbackTypes && feedbackTypes.length > 0) {
// just applying a filter to the messages array will only return the messages that have feedback,
// but we also want the message before the feedback message which is the user message.
const indicesToKeep = new Set()
messages.forEach((message, index) => {
if (message.role === 'apiMessage' && message.feedback && feedbackTypes.includes(message.feedback.rating)) {
if (index > 0) indicesToKeep.add(index - 1)
indicesToKeep.add(index)
}
})
return messages.filter((_, index) => indicesToKeep.has(index))
}
return messages
}