Skip to content

Commit

Permalink
Simplify Posting of Data To Webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
birksy89 committed May 10, 2024
1 parent 754b040 commit 10874e6
Showing 1 changed file with 8 additions and 56 deletions.
64 changes: 8 additions & 56 deletions packages/server/src/utils/addChatMesage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { ChatMessage } from '../database/entities/ChatMessage'
import { IChatMessage, IReactFlowObject } from '../Interface'
import { IChatMessage } from '../Interface'
import { getRunningExpressApp } from '../utils/getRunningExpressApp'
import chatflows from '../services/chatflows'
import { InternalFlowiseError } from '../errors/internalFlowiseError'
import { StatusCodes } from 'http-status-codes'
import { Assistant } from '../database/entities/Assistant'
import axios from 'axios'

/**
Expand All @@ -23,59 +19,15 @@ export const utilAddChatMessage = async (chatMessage: Partial<IChatMessage>): Pr

// When a chat message is created, we want to post it to the CXCortex Console
try {
if (!chatMessage.chatflowid) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
'Error: chatMessagesController.createChatMessage - chatflowid not provided!'
)
}
const chatflow = await chatflows.getChatflowById(chatMessage.chatflowid)
if (!chatflow) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, 'Error: chatMessagesController.createChatMessage - Cannot find chatflow!')
}

const flowData = chatflow.flowData
const parsedFlowData: IReactFlowObject = JSON.parse(flowData)
const nodes = parsedFlowData.nodes

const openAIAssistant = nodes.find((node) => node.data.type === 'OpenAIAssistant')

if (openAIAssistant) {
const internalAssistantId = openAIAssistant.data.inputs?.selectedAssistant as string | undefined

if (internalAssistantId) {
const assistant = await appServer.AppDataSource.getRepository(Assistant).findOneBy({
id: internalAssistantId
})

if (!assistant) {
throw new InternalFlowiseError(StatusCodes.NOT_FOUND, `Assistant ${internalAssistantId} not found`)
}

const openAIAssistantId = JSON.parse(assistant.details)?.id as string | undefined

const cxcortexURL = `${process.env.CXCORTEX_CONSOLE_URL}/api/webhook/flowise`

if (!cxcortexURL) {
throw new InternalFlowiseError(
StatusCodes.PRECONDITION_FAILED,
'Error: chatMessagesController.createChatMessage - CXCortex Console URL not provided!'
)
}
const cxcortexURL = `${process.env.CXCORTEX_CONSOLE_URL}/api/webhook/flowise`

// Perform the post to the CXCortex Console
const response = await axios.post(cxcortexURL, {
assistant_id: openAIAssistantId,
thread_id: chatMessage.sessionId
})
// Perform the post to the CXCortex Console
const response = await axios.post(cxcortexURL, {
...dbResponse
})

if (response.status !== 200) {
throw new InternalFlowiseError(
StatusCodes.INTERNAL_SERVER_ERROR,
'Error: chatMessagesController.createChatMessage - Failed to post to CXCortex Console'
)
}
}
if (response.status !== 200) {
console.error(`Error posting chat message to CXCortex Console: ${response.statusText}`)
}
} catch (error) {
console.error(error)
Expand Down

0 comments on commit 10874e6

Please sign in to comment.