diff --git a/packages/components/src/Interface.ts b/packages/components/src/Interface.ts index 5b4c4d23d9a..817801053db 100644 --- a/packages/components/src/Interface.ts +++ b/packages/components/src/Interface.ts @@ -153,6 +153,8 @@ export interface INodeCredential { export interface IMessage { message: string type: MessageType + role?: MessageType + content?: string } export interface IUsedTool { diff --git a/packages/components/src/utils.ts b/packages/components/src/utils.ts index 6f5b7c5c744..57ac493c814 100644 --- a/packages/components/src/utils.ts +++ b/packages/components/src/utils.ts @@ -587,10 +587,10 @@ export const mapChatMessageToBaseMessage = (chatmessages: any[] = []): BaseMessa const chatHistory = [] for (const message of chatmessages) { - if (message.role === 'apiMessage') { - chatHistory.push(new AIMessage(message.content)) - } else if (message.role === 'userMessage') { - chatHistory.push(new HumanMessage(message.content)) + if (message.role === 'apiMessage' || message.type === 'apiMessage') { + chatHistory.push(new AIMessage(message.content || '')) + } else if (message.role === 'userMessage' || message.role === 'userMessage') { + chatHistory.push(new HumanMessage(message.content || '')) } } return chatHistory diff --git a/packages/server/src/Interface.ts b/packages/server/src/Interface.ts index 8d975666245..803d044df2f 100644 --- a/packages/server/src/Interface.ts +++ b/packages/server/src/Interface.ts @@ -209,6 +209,8 @@ export interface IDepthQueue { export interface IMessage { message: string type: MessageType + role?: MessageType + content?: string } export interface IncomingInput { diff --git a/packages/server/src/utils/buildAgentGraph.ts b/packages/server/src/utils/buildAgentGraph.ts index e6a61a4cbf6..c4ef963ca67 100644 --- a/packages/server/src/utils/buildAgentGraph.ts +++ b/packages/server/src/utils/buildAgentGraph.ts @@ -181,6 +181,7 @@ export const buildAgentGraph = async ( options, startingNodeIds, incomingInput.question, + incomingInput.history, chatHistory, incomingInput?.overrideConfig, sessionId || chatId, @@ -196,6 +197,7 @@ export const buildAgentGraph = async ( appServer.nodesPool.componentNodes, options, incomingInput.question, + incomingInput.history, chatHistory, incomingInput?.overrideConfig, sessionId || chatId, @@ -447,6 +449,7 @@ const compileMultiAgentsGraph = async ( options: ICommonObject, startingNodeIds: string[], question: string, + prependHistoryMessages: IMessage[] = [], chatHistory: IMessage[] = [], overrideConfig?: ICommonObject, threadId?: string, @@ -570,10 +573,22 @@ const compileMultiAgentsGraph = async ( const callbacks = await additionalCallbacks(flowNodeData, options) const config = { configurable: { thread_id: threadId } } + let prependMessages = [] + // Only append in the first message + if (prependHistoryMessages.length === chatHistory.length) { + for (const message of prependHistoryMessages) { + if (message.role === 'apiMessage' || message.type === 'apiMessage') { + prependMessages.push(new AIMessage({ content: message.message || message.content || '' })) + } else if (message.role === 'userMessage' || message.type === 'userMessage') { + prependMessages.push(new HumanMessage({ content: message.message || message.content || '' })) + } + } + } + // Return stream result as we should only have 1 supervisor return await graph.stream( { - messages: [new HumanMessage({ content: question })] + messages: [...prependMessages, new HumanMessage({ content: question })] }, { recursionLimit: supervisorResult?.recursionLimit ?? 100, callbacks: [loggerHandler, ...callbacks], configurable: config } ) @@ -605,6 +620,7 @@ const compileSeqAgentsGraph = async ( componentNodes: IComponentNodes, options: ICommonObject, question: string, + prependHistoryMessages: IMessage[] = [], chatHistory: IMessage[] = [], overrideConfig?: ICommonObject, threadId?: string, @@ -952,8 +968,20 @@ const compileSeqAgentsGraph = async ( const callbacks = await additionalCallbacks(flowNodeData as any, options) const config = { configurable: { thread_id: threadId }, bindModel } + let prependMessages = [] + // Only append in the first message + if (prependHistoryMessages.length === chatHistory.length) { + for (const message of prependHistoryMessages) { + if (message.role === 'apiMessage' || message.type === 'apiMessage') { + prependMessages.push(new AIMessage({ content: message.message || message.content || '' })) + } else if (message.role === 'userMessage' || message.type === 'userMessage') { + prependMessages.push(new HumanMessage({ content: message.message || message.content || '' })) + } + } + } + let humanMsg: { messages: HumanMessage[] | ToolMessage[] } | null = { - messages: [new HumanMessage({ content: question })] + messages: [...prependMessages, new HumanMessage({ content: question })] } if (action && action.mapping && question === action.mapping.approve) {