Bugfix/update get chat messages chattype (#3881)

update get chat messages chattype
This commit is contained in:
Henry Heng 2025-01-16 14:53:36 +00:00 committed by GitHub
parent 62d5d1e8ef
commit c89be26024
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 55 additions and 82 deletions

View File

@ -1,25 +0,0 @@
import { INodeParams, INodeCredential } from '../src/Interface'
class OpenAPIAuth implements INodeCredential {
label: string
name: string
version: number
description: string
inputs: INodeParams[]
constructor() {
this.label = 'OpenAPI Auth Token'
this.name = 'openAPIAuth'
this.version = 1.0
this.inputs = [
{
label: 'OpenAPI Token',
name: 'openAPIToken',
type: 'password',
description: 'Auth Token. For example: Bearer <TOKEN>'
}
]
}
}
module.exports = { credClass: OpenAPIAuth }

View File

@ -4,7 +4,7 @@ import chatflowsService from '../../services/chatflows'
import chatMessagesService from '../../services/chat-messages' import chatMessagesService from '../../services/chat-messages'
import { aMonthAgo, clearSessionMemory } from '../../utils' import { aMonthAgo, clearSessionMemory } from '../../utils'
import { getRunningExpressApp } from '../../utils/getRunningExpressApp' import { getRunningExpressApp } from '../../utils/getRunningExpressApp'
import { Between, DeleteResult, FindOptionsWhere } from 'typeorm' import { Between, DeleteResult, FindOptionsWhere, In } from 'typeorm'
import { ChatMessage } from '../../database/entities/ChatMessage' import { ChatMessage } from '../../database/entities/ChatMessage'
import { InternalFlowiseError } from '../../errors/internalFlowiseError' import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes' import { StatusCodes } from 'http-status-codes'
@ -49,19 +49,17 @@ const createChatMessage = async (req: Request, res: Response, next: NextFunction
const getAllChatMessages = async (req: Request, res: Response, next: NextFunction) => { const getAllChatMessages = async (req: Request, res: Response, next: NextFunction) => {
try { try {
let chatTypeFilter = req.query?.chatType as ChatType | undefined const _chatTypes = req.query?.chatType as string | undefined
if (chatTypeFilter) { let chatTypes: ChatType[] | undefined
if (_chatTypes) {
try { try {
const chatTypeFilterArray = JSON.parse(chatTypeFilter) if (Array.isArray(_chatTypes)) {
if (chatTypeFilterArray.includes(ChatType.EXTERNAL) && chatTypeFilterArray.includes(ChatType.INTERNAL)) { chatTypes = _chatTypes
chatTypeFilter = undefined } else {
} else if (chatTypeFilterArray.includes(ChatType.EXTERNAL)) { chatTypes = JSON.parse(_chatTypes)
chatTypeFilter = ChatType.EXTERNAL
} else if (chatTypeFilterArray.includes(ChatType.INTERNAL)) {
chatTypeFilter = ChatType.INTERNAL
} }
} catch (e) { } catch (e) {
return res.status(500).send(e) chatTypes = [_chatTypes as ChatType]
} }
} }
const sortOrder = req.query?.order as string | undefined const sortOrder = req.query?.order as string | undefined
@ -84,7 +82,7 @@ const getAllChatMessages = async (req: Request, res: Response, next: NextFunctio
} }
const apiResponse = await chatMessagesService.getAllChatMessages( const apiResponse = await chatMessagesService.getAllChatMessages(
req.params.id, req.params.id,
chatTypeFilter, chatTypes,
sortOrder, sortOrder,
chatId, chatId,
memoryType, memoryType,
@ -118,7 +116,7 @@ const getAllInternalChatMessages = async (req: Request, res: Response, next: Nex
} }
const apiResponse = await chatMessagesService.getAllInternalChatMessages( const apiResponse = await chatMessagesService.getAllInternalChatMessages(
req.params.id, req.params.id,
ChatType.INTERNAL, [ChatType.INTERNAL],
sortOrder, sortOrder,
chatId, chatId,
memoryType, memoryType,
@ -155,7 +153,19 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
const chatId = req.query?.chatId as string const chatId = req.query?.chatId as string
const memoryType = req.query?.memoryType as string | undefined const memoryType = req.query?.memoryType as string | undefined
const sessionId = req.query?.sessionId as string | undefined const sessionId = req.query?.sessionId as string | undefined
const _chatType = req.query?.chatType as string | undefined const _chatTypes = req.query?.chatType as string | undefined
let chatTypes: ChatType[] | undefined
if (_chatTypes) {
try {
if (Array.isArray(_chatTypes)) {
chatTypes = _chatTypes
} else {
chatTypes = JSON.parse(_chatTypes)
}
} catch (e) {
chatTypes = [_chatTypes as ChatType]
}
}
const startDate = req.query?.startDate as string | undefined const startDate = req.query?.startDate as string | undefined
const endDate = req.query?.endDate as string | undefined const endDate = req.query?.endDate as string | undefined
const isClearFromViewMessageDialog = req.query?.isClearFromViewMessageDialog as string | undefined const isClearFromViewMessageDialog = req.query?.isClearFromViewMessageDialog as string | undefined
@ -169,7 +179,7 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
const hardDelete = req.query?.hardDelete as boolean | undefined const hardDelete = req.query?.hardDelete as boolean | undefined
const messages = await utilGetChatMessage({ const messages = await utilGetChatMessage({
chatflowid, chatflowid,
chatType: _chatType as ChatType | undefined, chatTypes,
startDate, startDate,
endDate, endDate,
feedback: isFeedback, feedback: isFeedback,
@ -236,7 +246,9 @@ const removeAllChatMessages = async (req: Request, res: Response, next: NextFunc
if (chatId) deleteOptions.chatId = chatId if (chatId) deleteOptions.chatId = chatId
if (memoryType) deleteOptions.memoryType = memoryType if (memoryType) deleteOptions.memoryType = memoryType
if (sessionId) deleteOptions.sessionId = sessionId if (sessionId) deleteOptions.sessionId = sessionId
if (_chatType) deleteOptions.chatType = _chatType if (chatTypes && chatTypes.length > 0) {
deleteOptions.chatType = In(chatTypes)
}
if (startDate && endDate) { if (startDate && endDate) {
const fromDate = new Date(startDate) const fromDate = new Date(startDate)
const toDate = new Date(endDate) const toDate = new Date(endDate)

View File

@ -3,7 +3,6 @@ import { Request, Response, NextFunction } from 'express'
import statsService from '../../services/stats' import statsService from '../../services/stats'
import { ChatMessageRatingType, ChatType } from '../../Interface' import { ChatMessageRatingType, ChatType } from '../../Interface'
import { InternalFlowiseError } from '../../errors/internalFlowiseError' import { InternalFlowiseError } from '../../errors/internalFlowiseError'
import { getErrorMessage } from '../../errors/utils'
const getChatflowStats = async (req: Request, res: Response, next: NextFunction) => { const getChatflowStats = async (req: Request, res: Response, next: NextFunction) => {
try { try {
@ -11,27 +10,22 @@ const getChatflowStats = async (req: Request, res: Response, next: NextFunction)
throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: statsController.getChatflowStats - id not provided!`) throw new InternalFlowiseError(StatusCodes.PRECONDITION_FAILED, `Error: statsController.getChatflowStats - id not provided!`)
} }
const chatflowid = req.params.id const chatflowid = req.params.id
let chatTypeFilter = req.query?.chatType as ChatType | undefined const _chatTypes = req.query?.chatType as string | undefined
let chatTypes: ChatType[] | undefined
if (_chatTypes) {
try {
if (Array.isArray(_chatTypes)) {
chatTypes = _chatTypes
} else {
chatTypes = JSON.parse(_chatTypes)
}
} catch (e) {
chatTypes = [_chatTypes as ChatType]
}
}
const startDate = req.query?.startDate as string | undefined const startDate = req.query?.startDate as string | undefined
const endDate = req.query?.endDate as string | undefined const endDate = req.query?.endDate as string | undefined
let feedbackTypeFilters = req.query?.feedbackType as ChatMessageRatingType[] | undefined let feedbackTypeFilters = req.query?.feedbackType as ChatMessageRatingType[] | undefined
if (chatTypeFilter) {
try {
const chatTypeFilterArray = JSON.parse(chatTypeFilter)
if (chatTypeFilterArray.includes(ChatType.EXTERNAL) && chatTypeFilterArray.includes(ChatType.INTERNAL)) {
chatTypeFilter = undefined
} else if (chatTypeFilterArray.includes(ChatType.EXTERNAL)) {
chatTypeFilter = ChatType.EXTERNAL
} else if (chatTypeFilterArray.includes(ChatType.INTERNAL)) {
chatTypeFilter = ChatType.INTERNAL
}
} catch (e) {
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
`Error: statsController.getChatflowStats - ${getErrorMessage(e)}`
)
}
}
if (feedbackTypeFilters) { if (feedbackTypeFilters) {
try { try {
const feedbackTypeFilterArray = JSON.parse(JSON.stringify(feedbackTypeFilters)) const feedbackTypeFilterArray = JSON.parse(JSON.stringify(feedbackTypeFilters))
@ -51,15 +45,7 @@ const getChatflowStats = async (req: Request, res: Response, next: NextFunction)
return res.status(500).send(e) return res.status(500).send(e)
} }
} }
const apiResponse = await statsService.getChatflowStats( const apiResponse = await statsService.getChatflowStats(chatflowid, chatTypes, startDate, endDate, '', true, feedbackTypeFilters)
chatflowid,
chatTypeFilter,
startDate,
endDate,
'',
true,
feedbackTypeFilters
)
return res.json(apiResponse) return res.json(apiResponse)
} catch (error) { } catch (error) {
next(error) next(error)

View File

@ -27,7 +27,7 @@ const createChatMessage = async (chatMessage: Partial<IChatMessage>) => {
// Get all chatmessages from chatflowid // Get all chatmessages from chatflowid
const getAllChatMessages = async ( const getAllChatMessages = async (
chatflowId: string, chatflowId: string,
chatTypeFilter: ChatType | undefined, chatTypes: ChatType[] | undefined,
sortOrder: string = 'ASC', sortOrder: string = 'ASC',
chatId?: string, chatId?: string,
memoryType?: string, memoryType?: string,
@ -41,7 +41,7 @@ const getAllChatMessages = async (
try { try {
const dbResponse = await utilGetChatMessage({ const dbResponse = await utilGetChatMessage({
chatflowid: chatflowId, chatflowid: chatflowId,
chatType: chatTypeFilter, chatTypes,
sortOrder, sortOrder,
chatId, chatId,
memoryType, memoryType,
@ -64,7 +64,7 @@ const getAllChatMessages = async (
// Get internal chatmessages from chatflowid // Get internal chatmessages from chatflowid
const getAllInternalChatMessages = async ( const getAllInternalChatMessages = async (
chatflowId: string, chatflowId: string,
chatTypeFilter: ChatType | undefined, chatTypes: ChatType[] | undefined,
sortOrder: string = 'ASC', sortOrder: string = 'ASC',
chatId?: string, chatId?: string,
memoryType?: string, memoryType?: string,
@ -78,7 +78,7 @@ const getAllInternalChatMessages = async (
try { try {
const dbResponse = await utilGetChatMessage({ const dbResponse = await utilGetChatMessage({
chatflowid: chatflowId, chatflowid: chatflowId,
chatType: chatTypeFilter, chatTypes,
sortOrder, sortOrder,
chatId, chatId,
memoryType, memoryType,

View File

@ -9,7 +9,7 @@ import { getErrorMessage } from '../../errors/utils'
// get stats for showing in chatflow // get stats for showing in chatflow
const getChatflowStats = async ( const getChatflowStats = async (
chatflowid: string, chatflowid: string,
chatTypeFilter: ChatType | undefined, chatTypes: ChatType[] | undefined,
startDate?: string, startDate?: string,
endDate?: string, endDate?: string,
messageId?: string, messageId?: string,
@ -19,7 +19,7 @@ const getChatflowStats = async (
try { try {
const chatmessages = (await utilGetChatMessage({ const chatmessages = (await utilGetChatMessage({
chatflowid, chatflowid,
chatType: chatTypeFilter, chatTypes,
startDate, startDate,
endDate, endDate,
messageId, messageId,

View File

@ -1,4 +1,4 @@
import { MoreThanOrEqual, LessThanOrEqual, Between } from 'typeorm' import { MoreThanOrEqual, LessThanOrEqual, Between, In } from 'typeorm'
import { ChatMessageRatingType, ChatType } from '../Interface' import { ChatMessageRatingType, ChatType } from '../Interface'
import { ChatMessage } from '../database/entities/ChatMessage' import { ChatMessage } from '../database/entities/ChatMessage'
import { ChatMessageFeedback } from '../database/entities/ChatMessageFeedback' import { ChatMessageFeedback } from '../database/entities/ChatMessageFeedback'
@ -8,7 +8,7 @@ import { aMonthAgo } from '.'
/** /**
* Method that get chat messages. * Method that get chat messages.
* @param {string} chatflowid * @param {string} chatflowid
* @param {ChatType} chatType * @param {ChatType[]} chatTypes
* @param {string} sortOrder * @param {string} sortOrder
* @param {string} chatId * @param {string} chatId
* @param {string} memoryType * @param {string} memoryType
@ -20,7 +20,7 @@ import { aMonthAgo } from '.'
*/ */
interface GetChatMessageParams { interface GetChatMessageParams {
chatflowid: string chatflowid: string
chatType?: ChatType chatTypes?: ChatType[]
sortOrder?: string sortOrder?: string
chatId?: string chatId?: string
memoryType?: string memoryType?: string
@ -34,7 +34,7 @@ interface GetChatMessageParams {
export const utilGetChatMessage = async ({ export const utilGetChatMessage = async ({
chatflowid, chatflowid,
chatType, chatTypes,
sortOrder = 'ASC', sortOrder = 'ASC',
chatId, chatId,
memoryType, memoryType,
@ -56,8 +56,8 @@ export const utilGetChatMessage = async ({
.where('chat_message.chatflowid = :chatflowid', { chatflowid }) .where('chat_message.chatflowid = :chatflowid', { chatflowid })
// based on which parameters are available add `andWhere` clauses to the query // based on which parameters are available add `andWhere` clauses to the query
if (chatType) { if (chatTypes && chatTypes.length > 0) {
query.andWhere('chat_message.chatType = :chatType', { chatType }) query.andWhere('chat_message.chatType IN (:...chatTypes)', { chatTypes })
} }
if (chatId) { if (chatId) {
query.andWhere('chat_message.chatId = :chatId', { chatId }) query.andWhere('chat_message.chatId = :chatId', { chatId })
@ -114,7 +114,7 @@ export const utilGetChatMessage = async ({
return await appServer.AppDataSource.getRepository(ChatMessage).find({ return await appServer.AppDataSource.getRepository(ChatMessage).find({
where: { where: {
chatflowid, chatflowid,
chatType, chatType: chatTypes?.length ? In(chatTypes) : undefined,
chatId, chatId,
memoryType: memoryType ?? undefined, memoryType: memoryType ?? undefined,
sessionId: sessionId ?? undefined, sessionId: sessionId ?? undefined,