(
{
state,
llm,
interrupt,
agent,
name,
abortControllerSignal,
nodeData,
input,
options
}: {
state: ISeqAgentsState
llm: BaseChatModel
interrupt: boolean
agent: AgentExecutor | RunnableSequence
name: string
abortControllerSignal: AbortController
nodeData: INodeData
input: string
options: ICommonObject
},
config: RunnableConfig
)
| 761 | } |
| 762 | |
| 763 | async function agentNode( |
| 764 | { |
| 765 | state, |
| 766 | llm, |
| 767 | interrupt, |
| 768 | agent, |
| 769 | name, |
| 770 | abortControllerSignal, |
| 771 | nodeData, |
| 772 | input, |
| 773 | options |
| 774 | }: { |
| 775 | state: ISeqAgentsState |
| 776 | llm: BaseChatModel |
| 777 | interrupt: boolean |
| 778 | agent: AgentExecutor | RunnableSequence |
| 779 | name: string |
| 780 | abortControllerSignal: AbortController |
| 781 | nodeData: INodeData |
| 782 | input: string |
| 783 | options: ICommonObject |
| 784 | }, |
| 785 | config: RunnableConfig |
| 786 | ) { |
| 787 | try { |
| 788 | if (abortControllerSignal.signal.aborted) { |
| 789 | throw new Error('Aborted!') |
| 790 | } |
| 791 | |
| 792 | const historySelection = (nodeData.inputs?.conversationHistorySelection || 'all_messages') as ConversationHistorySelection |
| 793 | // @ts-ignore |
| 794 | state.messages = filterConversationHistory(historySelection, input, state) |
| 795 | // @ts-ignore |
| 796 | state.messages = restructureMessages(llm, state) |
| 797 | |
| 798 | let result = await agent.invoke({ ...state, signal: abortControllerSignal.signal }, config) |
| 799 | |
| 800 | if (interrupt) { |
| 801 | const messages = state.messages as unknown as BaseMessage[] |
| 802 | const lastMessage = messages.length ? messages[messages.length - 1] : null |
| 803 | |
| 804 | // If the last message is a tool message and is an interrupted message, format output into standard agent output |
| 805 | if (lastMessage && lastMessage._getType() === 'tool' && lastMessage.additional_kwargs?.nodeId === nodeData.id) { |
| 806 | let formattedAgentResult: { |
| 807 | output?: string |
| 808 | usedTools?: IUsedTool[] |
| 809 | sourceDocuments?: IDocument[] |
| 810 | artifacts?: ICommonObject[] |
| 811 | } = {} |
| 812 | formattedAgentResult.output = result.content |
| 813 | if (lastMessage.additional_kwargs?.usedTools) { |
| 814 | formattedAgentResult.usedTools = lastMessage.additional_kwargs.usedTools as IUsedTool[] |
| 815 | } |
| 816 | if (lastMessage.additional_kwargs?.sourceDocuments) { |
| 817 | formattedAgentResult.sourceDocuments = lastMessage.additional_kwargs.sourceDocuments as IDocument[] |
| 818 | } |
| 819 | if (lastMessage.additional_kwargs?.artifacts) { |
| 820 | formattedAgentResult.artifacts = lastMessage.additional_kwargs.artifacts as ICommonObject[] |
no test coverage detected