@n8n/n8n-nodes-langchain 1.99.1 → 1.100.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/dist/nodes/agents/Agent/V2/AgentV2.node.js +51 -34
  2. package/dist/nodes/agents/Agent/V2/AgentV2.node.js.map +1 -1
  3. package/dist/nodes/agents/Agent/agents/ToolsAgent/V1/execute.js.map +1 -1
  4. package/dist/nodes/agents/Agent/agents/ToolsAgent/V2/execute.js +57 -28
  5. package/dist/nodes/agents/Agent/agents/ToolsAgent/V2/execute.js.map +1 -1
  6. package/dist/nodes/agents/Agent/agents/ToolsAgent/common.js +12 -2
  7. package/dist/nodes/agents/Agent/agents/ToolsAgent/common.js.map +1 -1
  8. package/dist/nodes/chains/ChainLLM/methods/chainExecutor.js +13 -4
  9. package/dist/nodes/chains/ChainLLM/methods/chainExecutor.js.map +1 -1
  10. package/dist/nodes/chains/ChainLLM/methods/config.js +32 -0
  11. package/dist/nodes/chains/ChainLLM/methods/config.js.map +1 -1
  12. package/dist/nodes/chains/ChainLLM/methods/processItem.js +38 -6
  13. package/dist/nodes/chains/ChainLLM/methods/processItem.js.map +1 -1
  14. package/dist/nodes/chains/ChainLLM/methods/types.js.map +1 -1
  15. package/dist/nodes/llms/LmChatGoogleGemini/LmChatGoogleGemini.node.js +1 -1
  16. package/dist/nodes/llms/LmChatGoogleGemini/LmChatGoogleGemini.node.js.map +1 -1
  17. package/dist/nodes/llms/N8nLlmTracing.js +2 -6
  18. package/dist/nodes/llms/N8nLlmTracing.js.map +1 -1
  19. package/dist/nodes/text_splitters/TextSplitterTokenSplitter/TokenTextSplitter.js +43 -14
  20. package/dist/nodes/text_splitters/TextSplitterTokenSplitter/TokenTextSplitter.js.map +1 -1
  21. package/dist/nodes/tools/ToolWorkflow/v2/utils/WorkflowToolService.js +74 -41
  22. package/dist/nodes/tools/ToolWorkflow/v2/utils/WorkflowToolService.js.map +1 -1
  23. package/dist/types/nodes.json +7 -7
  24. package/dist/utils/descriptions.js +1 -1
  25. package/dist/utils/descriptions.js.map +1 -1
  26. package/dist/utils/helpers.js +29 -0
  27. package/dist/utils/helpers.js.map +1 -1
  28. package/dist/utils/output_parsers/N8nOutputParser.js +2 -2
  29. package/dist/utils/output_parsers/N8nOutputParser.js.map +1 -1
  30. package/dist/utils/tokenizer/token-estimator.js +119 -0
  31. package/dist/utils/tokenizer/token-estimator.js.map +1 -0
  32. package/package.json +12 -10
@@ -25,24 +25,13 @@ var import_n8n_workflow = require("n8n-workflow");
25
25
  var import_descriptions = require("../../../../utils/descriptions");
26
26
  var import_description = require("../agents/ToolsAgent/V2/description");
27
27
  var import_execute = require("../agents/ToolsAgent/V2/execute");
28
- function getInputs(hasOutputParser) {
28
+ function getInputs(hasOutputParser, needsFallback) {
29
29
  const getInputData = (inputs) => {
30
- const displayNames = {
31
- ai_languageModel: "Model",
32
- ai_memory: "Memory",
33
- ai_tool: "Tool",
34
- ai_outputParser: "Output Parser"
35
- };
36
- return inputs.map(({ type, filter }) => {
37
- const isModelType = type === "ai_languageModel";
38
- let displayName = type in displayNames ? displayNames[type] : void 0;
39
- if (isModelType) {
40
- displayName = "Chat Model";
41
- }
30
+ return inputs.map(({ type, filter, displayName, required }) => {
42
31
  const input = {
43
32
  type,
44
33
  displayName,
45
- required: isModelType,
34
+ required,
46
35
  maxConnections: ["ai_languageModel", "ai_memory", "ai_outputParser"].includes(
47
36
  type
48
37
  ) ? 1 : void 0
@@ -56,39 +45,49 @@ function getInputs(hasOutputParser) {
56
45
  let specialInputs = [
57
46
  {
58
47
  type: "ai_languageModel",
48
+ displayName: "Chat Model",
49
+ required: true,
59
50
  filter: {
60
- nodes: [
61
- "@n8n/n8n-nodes-langchain.lmChatAnthropic",
62
- "@n8n/n8n-nodes-langchain.lmChatAzureOpenAi",
63
- "@n8n/n8n-nodes-langchain.lmChatAwsBedrock",
64
- "@n8n/n8n-nodes-langchain.lmChatMistralCloud",
65
- "@n8n/n8n-nodes-langchain.lmChatOllama",
66
- "@n8n/n8n-nodes-langchain.lmChatOpenAi",
67
- "@n8n/n8n-nodes-langchain.lmChatGroq",
68
- "@n8n/n8n-nodes-langchain.lmChatGoogleVertex",
69
- "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
70
- "@n8n/n8n-nodes-langchain.lmChatDeepSeek",
71
- "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
72
- "@n8n/n8n-nodes-langchain.lmChatXAiGrok",
73
- "@n8n/n8n-nodes-langchain.code",
74
- "@n8n/n8n-nodes-langchain.modelSelector"
51
+ excludedNodes: [
52
+ "@n8n/n8n-nodes-langchain.lmCohere",
53
+ "@n8n/n8n-nodes-langchain.lmOllama",
54
+ "n8n/n8n-nodes-langchain.lmOpenAi",
55
+ "@n8n/n8n-nodes-langchain.lmOpenHuggingFaceInference"
75
56
  ]
76
57
  }
77
58
  },
78
59
  {
60
+ type: "ai_languageModel",
61
+ displayName: "Fallback Model",
62
+ required: true,
63
+ filter: {
64
+ excludedNodes: [
65
+ "@n8n/n8n-nodes-langchain.lmCohere",
66
+ "@n8n/n8n-nodes-langchain.lmOllama",
67
+ "n8n/n8n-nodes-langchain.lmOpenAi",
68
+ "@n8n/n8n-nodes-langchain.lmOpenHuggingFaceInference"
69
+ ]
70
+ }
71
+ },
72
+ {
73
+ displayName: "Memory",
79
74
  type: "ai_memory"
80
75
  },
81
76
  {
82
- type: "ai_tool",
83
- required: true
77
+ displayName: "Tool",
78
+ type: "ai_tool"
84
79
  },
85
80
  {
81
+ displayName: "Output Parser",
86
82
  type: "ai_outputParser"
87
83
  }
88
84
  ];
89
85
  if (hasOutputParser === false) {
90
86
  specialInputs = specialInputs.filter((input) => input.type !== "ai_outputParser");
91
87
  }
88
+ if (needsFallback === false) {
89
+ specialInputs = specialInputs.filter((input) => input.displayName !== "Fallback Model");
90
+ }
92
91
  return ["main", ...getInputData(specialInputs)];
93
92
  }
94
93
  class AgentV2 {
@@ -101,10 +100,10 @@ class AgentV2 {
101
100
  color: "#404040"
102
101
  },
103
102
  inputs: `={{
104
- ((hasOutputParser) => {
103
+ ((hasOutputParser, needsFallback) => {
105
104
  ${getInputs.toString()};
106
- return getInputs(hasOutputParser)
107
- })($parameter.hasOutputParser === undefined || $parameter.hasOutputParser === true)
105
+ return getInputs(hasOutputParser, needsFallback)
106
+ })($parameter.hasOutputParser === undefined || $parameter.hasOutputParser === true, $parameter.needsFallback === undefined || $parameter.needsFallback === true)
108
107
  }}`,
109
108
  outputs: [import_n8n_workflow.NodeConnectionTypes.Main],
110
109
  properties: [
@@ -149,6 +148,24 @@ class AgentV2 {
149
148
  }
150
149
  }
151
150
  },
151
+ {
152
+ displayName: "Enable Fallback Model",
153
+ name: "needsFallback",
154
+ type: "boolean",
155
+ default: false,
156
+ noDataExpression: true
157
+ },
158
+ {
159
+ displayName: "Connect an additional language model on the canvas to use it as a fallback if the main model fails",
160
+ name: "fallbackNotice",
161
+ type: "notice",
162
+ default: "",
163
+ displayOptions: {
164
+ show: {
165
+ needsFallback: [true]
166
+ }
167
+ }
168
+ },
152
169
  ...import_description.toolsAgentProperties
153
170
  ]
154
171
  };
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../nodes/agents/Agent/V2/AgentV2.node.ts"],"sourcesContent":["import { NodeConnectionTypes } from 'n8n-workflow';\nimport type {\n\tINodeInputConfiguration,\n\tINodeInputFilter,\n\tIExecuteFunctions,\n\tINodeExecutionData,\n\tINodeType,\n\tINodeTypeDescription,\n\tNodeConnectionType,\n\tINodeTypeBaseDescription,\n} from 'n8n-workflow';\n\nimport { promptTypeOptions, textFromPreviousNode, textInput } from '@utils/descriptions';\n\nimport { toolsAgentProperties } from '../agents/ToolsAgent/V2/description';\nimport { toolsAgentExecute } from '../agents/ToolsAgent/V2/execute';\n\n// Function used in the inputs expression to figure out which inputs to\n// display based on the agent type\nfunction getInputs(hasOutputParser?: boolean): Array<NodeConnectionType | INodeInputConfiguration> {\n\tinterface SpecialInput {\n\t\ttype: NodeConnectionType;\n\t\tfilter?: INodeInputFilter;\n\t\trequired?: boolean;\n\t}\n\n\tconst getInputData = (\n\t\tinputs: SpecialInput[],\n\t): Array<NodeConnectionType | INodeInputConfiguration> => {\n\t\tconst displayNames: { [key: string]: string } = {\n\t\t\tai_languageModel: 'Model',\n\t\t\tai_memory: 'Memory',\n\t\t\tai_tool: 'Tool',\n\t\t\tai_outputParser: 'Output Parser',\n\t\t};\n\n\t\treturn inputs.map(({ type, filter }) => {\n\t\t\tconst isModelType = type === 'ai_languageModel';\n\t\t\tlet displayName = type in displayNames ? displayNames[type] : undefined;\n\t\t\tif (isModelType) {\n\t\t\t\tdisplayName = 'Chat Model';\n\t\t\t}\n\t\t\tconst input: INodeInputConfiguration = {\n\t\t\t\ttype,\n\t\t\t\tdisplayName,\n\t\t\t\trequired: isModelType,\n\t\t\t\tmaxConnections: ['ai_languageModel', 'ai_memory', 'ai_outputParser'].includes(\n\t\t\t\t\ttype as NodeConnectionType,\n\t\t\t\t)\n\t\t\t\t\t? 1\n\t\t\t\t\t: undefined,\n\t\t\t};\n\n\t\t\tif (filter) {\n\t\t\t\tinput.filter = filter;\n\t\t\t}\n\n\t\t\treturn input;\n\t\t});\n\t};\n\n\tlet specialInputs: SpecialInput[] = [\n\t\t{\n\t\t\ttype: 'ai_languageModel',\n\t\t\tfilter: {\n\t\t\t\tnodes: [\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatAnthropic',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatAzureOpenAi',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatAwsBedrock',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatMistralCloud',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatOllama',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatOpenAi',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatGroq',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatGoogleVertex',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatGoogleGemini',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatDeepSeek',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatOpenRouter',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmChatXAiGrok',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.code',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.modelSelector',\n\t\t\t\t],\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\ttype: 'ai_memory',\n\t\t},\n\t\t{\n\t\t\ttype: 'ai_tool',\n\t\t\trequired: true,\n\t\t},\n\t\t{\n\t\t\ttype: 'ai_outputParser',\n\t\t},\n\t];\n\n\tif (hasOutputParser === false) {\n\t\tspecialInputs = specialInputs.filter((input) => input.type !== 'ai_outputParser');\n\t}\n\treturn ['main', ...getInputData(specialInputs)];\n}\n\nexport class AgentV2 implements INodeType {\n\tdescription: INodeTypeDescription;\n\n\tconstructor(baseDescription: INodeTypeBaseDescription) {\n\t\tthis.description = {\n\t\t\t...baseDescription,\n\t\t\tversion: 2,\n\t\t\tdefaults: {\n\t\t\t\tname: 'AI Agent',\n\t\t\t\tcolor: '#404040',\n\t\t\t},\n\t\t\tinputs: `={{\n\t\t\t\t((hasOutputParser) => {\n\t\t\t\t\t${getInputs.toString()};\n\t\t\t\t\treturn getInputs(hasOutputParser)\n\t\t\t\t})($parameter.hasOutputParser === undefined || $parameter.hasOutputParser === true)\n\t\t\t}}`,\n\t\t\toutputs: [NodeConnectionTypes.Main],\n\t\t\tproperties: [\n\t\t\t\t{\n\t\t\t\t\tdisplayName:\n\t\t\t\t\t\t'Tip: Get a feel for agents with our quick <a href=\"https://docs.n8n.io/advanced-ai/intro-tutorial/\" target=\"_blank\">tutorial</a> or see an <a href=\"/workflows/templates/1954\" target=\"_blank\">example</a> of how this node works',\n\t\t\t\t\tname: 'aiAgentStarterCallout',\n\t\t\t\t\ttype: 'callout',\n\t\t\t\t\tdefault: '',\n\t\t\t\t},\n\t\t\t\tpromptTypeOptions,\n\t\t\t\t{\n\t\t\t\t\t...textFromPreviousNode,\n\t\t\t\t\tdisplayOptions: {\n\t\t\t\t\t\tshow: {\n\t\t\t\t\t\t\tpromptType: ['auto'],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t...textInput,\n\t\t\t\t\tdisplayOptions: {\n\t\t\t\t\t\tshow: {\n\t\t\t\t\t\t\tpromptType: ['define'],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tdisplayName: 'Require Specific Output Format',\n\t\t\t\t\tname: 'hasOutputParser',\n\t\t\t\t\ttype: 'boolean',\n\t\t\t\t\tdefault: false,\n\t\t\t\t\tnoDataExpression: true,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tdisplayName: `Connect an <a data-action='openSelectiveNodeCreator' data-action-parameter-connectiontype='${NodeConnectionTypes.AiOutputParser}'>output parser</a> on the canvas to specify the output format you require`,\n\t\t\t\t\tname: 'notice',\n\t\t\t\t\ttype: 'notice',\n\t\t\t\t\tdefault: '',\n\t\t\t\t\tdisplayOptions: {\n\t\t\t\t\t\tshow: {\n\t\t\t\t\t\t\thasOutputParser: [true],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t...toolsAgentProperties,\n\t\t\t],\n\t\t};\n\t}\n\n\tasync execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {\n\t\treturn await toolsAgentExecute.call(this);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAAoC;AAYpC,0BAAmE;AAEnE,yBAAqC;AACrC,qBAAkC;AAIlC,SAAS,UAAU,iBAAgF;AAOlG,QAAM,eAAe,CACpB,WACyD;AACzD,UAAM,eAA0C;AAAA,MAC/C,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Q;AAAA;AAAA,OAEJ,UAAU,SAAS,CAAC;AAAA;AAAA;AAAA;AAAA,MAIxB,SAAS,CAAC,wCAAoB,IAAI;AAAA,MAClC,YAAY;AAAA,QACX;AAAA,UACC,aACC;AAAA,UACD,MAAM;AAAA,UACN,MAAM;AAAA,UACN,SAAS;AAAA,QACV;AAAA,QACA;AAAA,QACA;AAAA,UACC,GAAG;AAAA,UACH,gBAAgB;AAAA,YACf,MAAM;AAAA,cACL,YAAY,CAAC,MAAM;AAAA,YACpB;AAAA,UACD;AAAA,QACD;AAAA,QACA;AAAA,UACC,GAAG;AAAA,UACH,gBAAgB;AAAA,YACf,MAAM;AAAA,cACL,YAAY,CAAC,QAAQ;AAAA,YACtB;AAAA,UACD;AAAA,QACD;AAAA,QACA;AAAA,UACC,aAAa;AAAA,UACb,MAAM;AAAA,UACN,MAAM;AAAA,UACN,SAAS;AAAA,UACT,kBAAkB;AAAA,QACnB;AAAA,QACA;AAAA,UACC,aAAa,8FAA8F,wCAAoB,cAAc;AAAA,UAC7I,MAAM;AAAA,UACN,MAAM;AAAA,UACN,SAAS;AAAA,UACT,gBAAgB;AAAA,YACf,MAAM;AAAA,cACL,iBAAiB,CAAC,IAAI;AAAA,YACvB;AAAA,UACD;AAAA,QACD;AAAA,QACA,GAAG;AAAA,MACJ;AAAA,IACD;AAAA,EACD;AAAA,EAEA,MAAM,UAAkE;AACvE,WAAO,MAAM,iCAAkB,KAAK,IAAI;AAAA,EACzC;AACD;","names":[]}
1
+ {"version":3,"sources":["../../../../../nodes/agents/Agent/V2/AgentV2.node.ts"],"sourcesContent":["import { NodeConnectionTypes } from 'n8n-workflow';\nimport type {\n\tINodeInputConfiguration,\n\tINodeInputFilter,\n\tIExecuteFunctions,\n\tINodeExecutionData,\n\tINodeType,\n\tINodeTypeDescription,\n\tNodeConnectionType,\n\tINodeTypeBaseDescription,\n} from 'n8n-workflow';\n\nimport { promptTypeOptions, textFromPreviousNode, textInput } from '@utils/descriptions';\n\nimport { toolsAgentProperties } from '../agents/ToolsAgent/V2/description';\nimport { toolsAgentExecute } from '../agents/ToolsAgent/V2/execute';\n\n// Function used in the inputs expression to figure out which inputs to\n// display based on the agent type\nfunction getInputs(\n\thasOutputParser?: boolean,\n\tneedsFallback?: boolean,\n): Array<NodeConnectionType | INodeInputConfiguration> {\n\tinterface SpecialInput {\n\t\ttype: NodeConnectionType;\n\t\tfilter?: INodeInputFilter;\n\t\tdisplayName: string;\n\t\trequired?: boolean;\n\t}\n\n\tconst getInputData = (\n\t\tinputs: SpecialInput[],\n\t): Array<NodeConnectionType | INodeInputConfiguration> => {\n\t\treturn inputs.map(({ type, filter, displayName, required }) => {\n\t\t\tconst input: INodeInputConfiguration = {\n\t\t\t\ttype,\n\t\t\t\tdisplayName,\n\t\t\t\trequired,\n\t\t\t\tmaxConnections: ['ai_languageModel', 'ai_memory', 'ai_outputParser'].includes(\n\t\t\t\t\ttype as NodeConnectionType,\n\t\t\t\t)\n\t\t\t\t\t? 1\n\t\t\t\t\t: undefined,\n\t\t\t};\n\n\t\t\tif (filter) {\n\t\t\t\tinput.filter = filter;\n\t\t\t}\n\n\t\t\treturn input;\n\t\t});\n\t};\n\n\tlet specialInputs: SpecialInput[] = [\n\t\t{\n\t\t\ttype: 'ai_languageModel',\n\t\t\tdisplayName: 'Chat Model',\n\t\t\trequired: true,\n\t\t\tfilter: {\n\t\t\t\texcludedNodes: [\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmCohere',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmOllama',\n\t\t\t\t\t'n8n/n8n-nodes-langchain.lmOpenAi',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmOpenHuggingFaceInference',\n\t\t\t\t],\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\ttype: 'ai_languageModel',\n\t\t\tdisplayName: 'Fallback Model',\n\t\t\trequired: true,\n\t\t\tfilter: {\n\t\t\t\texcludedNodes: [\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmCohere',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmOllama',\n\t\t\t\t\t'n8n/n8n-nodes-langchain.lmOpenAi',\n\t\t\t\t\t'@n8n/n8n-nodes-langchain.lmOpenHuggingFaceInference',\n\t\t\t\t],\n\t\t\t},\n\t\t},\n\t\t{\n\t\t\tdisplayName: 'Memory',\n\t\t\ttype: 'ai_memory',\n\t\t},\n\t\t{\n\t\t\tdisplayName: 'Tool',\n\t\t\ttype: 'ai_tool',\n\t\t},\n\t\t{\n\t\t\tdisplayName: 'Output Parser',\n\t\t\ttype: 'ai_outputParser',\n\t\t},\n\t];\n\n\tif (hasOutputParser === false) {\n\t\tspecialInputs = specialInputs.filter((input) => input.type !== 'ai_outputParser');\n\t}\n\tif (needsFallback === false) {\n\t\tspecialInputs = specialInputs.filter((input) => input.displayName !== 'Fallback Model');\n\t}\n\treturn ['main', ...getInputData(specialInputs)];\n}\n\nexport class AgentV2 implements INodeType {\n\tdescription: INodeTypeDescription;\n\n\tconstructor(baseDescription: INodeTypeBaseDescription) {\n\t\tthis.description = {\n\t\t\t...baseDescription,\n\t\t\tversion: 2,\n\t\t\tdefaults: {\n\t\t\t\tname: 'AI Agent',\n\t\t\t\tcolor: '#404040',\n\t\t\t},\n\t\t\tinputs: `={{\n\t\t\t\t((hasOutputParser, needsFallback) => {\n\t\t\t\t\t${getInputs.toString()};\n\t\t\t\t\treturn getInputs(hasOutputParser, needsFallback)\n\t\t\t\t})($parameter.hasOutputParser === undefined || $parameter.hasOutputParser === true, $parameter.needsFallback === undefined || $parameter.needsFallback === true)\n\t\t\t}}`,\n\t\t\toutputs: [NodeConnectionTypes.Main],\n\t\t\tproperties: [\n\t\t\t\t{\n\t\t\t\t\tdisplayName:\n\t\t\t\t\t\t'Tip: Get a feel for agents with our quick <a href=\"https://docs.n8n.io/advanced-ai/intro-tutorial/\" target=\"_blank\">tutorial</a> or see an <a href=\"/workflows/templates/1954\" target=\"_blank\">example</a> of how this node works',\n\t\t\t\t\tname: 'aiAgentStarterCallout',\n\t\t\t\t\ttype: 'callout',\n\t\t\t\t\tdefault: '',\n\t\t\t\t},\n\t\t\t\tpromptTypeOptions,\n\t\t\t\t{\n\t\t\t\t\t...textFromPreviousNode,\n\t\t\t\t\tdisplayOptions: {\n\t\t\t\t\t\tshow: {\n\t\t\t\t\t\t\tpromptType: ['auto'],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\t...textInput,\n\t\t\t\t\tdisplayOptions: {\n\t\t\t\t\t\tshow: {\n\t\t\t\t\t\t\tpromptType: ['define'],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tdisplayName: 'Require Specific Output Format',\n\t\t\t\t\tname: 'hasOutputParser',\n\t\t\t\t\ttype: 'boolean',\n\t\t\t\t\tdefault: false,\n\t\t\t\t\tnoDataExpression: true,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tdisplayName: `Connect an <a data-action='openSelectiveNodeCreator' data-action-parameter-connectiontype='${NodeConnectionTypes.AiOutputParser}'>output parser</a> on the canvas to specify the output format you require`,\n\t\t\t\t\tname: 'notice',\n\t\t\t\t\ttype: 'notice',\n\t\t\t\t\tdefault: '',\n\t\t\t\t\tdisplayOptions: {\n\t\t\t\t\t\tshow: {\n\t\t\t\t\t\t\thasOutputParser: [true],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tdisplayName: 'Enable Fallback Model',\n\t\t\t\t\tname: 'needsFallback',\n\t\t\t\t\ttype: 'boolean',\n\t\t\t\t\tdefault: false,\n\t\t\t\t\tnoDataExpression: true,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tdisplayName:\n\t\t\t\t\t\t'Connect an additional language model on the canvas to use it as a fallback if the main model fails',\n\t\t\t\t\tname: 'fallbackNotice',\n\t\t\t\t\ttype: 'notice',\n\t\t\t\t\tdefault: '',\n\t\t\t\t\tdisplayOptions: {\n\t\t\t\t\t\tshow: {\n\t\t\t\t\t\t\tneedsFallback: [true],\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t...toolsAgentProperties,\n\t\t\t],\n\t\t};\n\t}\n\n\tasync execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {\n\t\treturn await toolsAgentExecute.call(this);\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAAoC;AAYpC,0BAAmE;AAEnE,yBAAqC;AACrC,qBAAkC;AAIlC,SAAS,UACR,iBACA,eACsD;AAQtD,QAAM,eAAe,CACpB,WACyD;AACzD,WAAO,OAAO,IAAI,CAAC,EAAE,MAAM,QAAQ,aAAa,SAAS,MAAM;AAC9D,YAAM,QAAiC;AAAA,QACtC;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB,CAAC,oBAAoB,aAAa,iBAAiB,EAAE;AAAA,UACpE;AAAA,QACD,IACG,IACA;AAAA,MACJ;AAEA,UAAI,QAAQ;AACX,cAAM,SAAS;AAAA,MAChB;AAEA,aAAO;AAAA,IACR,CAAC;AAAA,EACF;AAEA,MAAI,gBAAgC;AAAA,IACnC;AAAA,MACC,MAAM;AAAA,MACN,aAAa;AAAA,MACb,UAAU;AAAA,MACV,QAAQ;AAAA,QACP,eAAe;AAAA,UACd;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,IACA;AAAA,MACC,MAAM;AAAA,MACN,aAAa;AAAA,MACb,UAAU;AAAA,MACV,QAAQ;AAAA,QACP,eAAe;AAAA,UACd;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACD;AAAA,MACD;AAAA,IACD;AAAA,IACA;AAAA,MACC,aAAa;AAAA,MACb,MAAM;AAAA,IACP;AAAA,IACA;AAAA,MACC,aAAa;AAAA,MACb,MAA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,cACL,iBAAiB,CAAC,IAAI;AAAA,YACvB;AAAA,UACD;AAAA,QACD;AAAA,QACA;AAAA,UACC,aAAa;AAAA,UACb,MAAM;AAAA,UACN,MAAM;AAAA,UACN,SAAS;AAAA,UACT,kBAAkB;AAAA,QACnB;AAAA,QACA;AAAA,UACC,aACC;AAAA,UACD,MAAM;AAAA,UACN,MAAM;AAAA,UACN,SAAS;AAAA,UACT,gBAAgB;AAAA,YACf,MAAM;AAAA,cACL,eAAe,CAAC,IAAI;AAAA,YACrB;AAAA,UACD;AAAA,QACD;AAAA,QACA,GAAG;AAAA,MACJ;AAAA,IACD;AAAA,EACD;AAAA,EAEA,MAAM,UAAkE;AACvE,WAAO,MAAM,iCAAkB,KAAK,IAAI;AAAA,EACzC;AACD;","names":[]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../nodes/agents/Agent/agents/ToolsAgent/V1/execute.ts"],"sourcesContent":["import { RunnableSequence } from '@langchain/core/runnables';\nimport { AgentExecutor, createToolCallingAgent } from 'langchain/agents';\nimport omit from 'lodash/omit';\nimport { jsonParse, NodeOperationError } from 'n8n-workflow';\nimport type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';\n\nimport { getPromptInputByType } from '@utils/helpers';\nimport { getOptionalOutputParser } from '@utils/output_parsers/N8nOutputParser';\n\nimport {\n\tfixEmptyContentMessage,\n\tgetAgentStepsParser,\n\tgetChatModel,\n\tgetOptionalMemory,\n\tgetTools,\n\tprepareMessages,\n\tpreparePrompt,\n} from '../common';\nimport { SYSTEM_MESSAGE } from '../prompt';\n\n/* -----------------------------------------------------------\n Main Executor Function\n----------------------------------------------------------- */\n/**\n * The main executor method for the Tools Agent.\n *\n * This function retrieves necessary components (model, memory, tools), prepares the prompt,\n * creates the agent, and processes each input item. The error handling for each item is also\n * managed here based on the node's continueOnFail setting.\n *\n * @returns The array of execution data for all processed items\n */\nexport async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {\n\tthis.logger.debug('Executing Tools Agent');\n\n\tconst returnData: INodeExecutionData[] = [];\n\tconst items = this.getInputData();\n\tconst outputParser = await getOptionalOutputParser(this);\n\tconst tools = await getTools(this, outputParser);\n\n\tfor (let itemIndex = 0; itemIndex < items.length; itemIndex++) {\n\t\ttry {\n\t\t\tconst model = await getChatModel(this);\n\t\t\tconst memory = await getOptionalMemory(this);\n\n\t\t\tconst input = getPromptInputByType({\n\t\t\t\tctx: this,\n\t\t\t\ti: itemIndex,\n\t\t\t\tinputKey: 'text',\n\t\t\t\tpromptTypeKey: 'promptType',\n\t\t\t});\n\t\t\tif (input === undefined) {\n\t\t\t\tthrow new NodeOperationError(this.getNode(), 'The “text” parameter is empty.');\n\t\t\t}\n\n\t\t\tconst options = this.getNodeParameter('options', itemIndex, {}) as {\n\t\t\t\tsystemMessage?: string;\n\t\t\t\tmaxIterations?: number;\n\t\t\t\treturnIntermediateSteps?: boolean;\n\t\t\t\tpassthroughBinaryImages?: boolean;\n\t\t\t};\n\n\t\t\t// Prepare the prompt messages and prompt template.\n\t\t\tconst messages = await prepareMessages(this, itemIndex, {\n\t\t\t\tsystemMessage: options.systemMessage,\n\t\t\t\tpassthroughBinaryImages: options.passthroughBinaryImages ?? true,\n\t\t\t\toutputParser,\n\t\t\t});\n\t\t\tconst prompt = preparePrompt(messages);\n\n\t\t\t// Create the base agent that calls tools.\n\t\t\tconst agent = createToolCallingAgent({\n\t\t\t\tllm: model,\n\t\t\t\ttools,\n\t\t\t\tprompt,\n\t\t\t\tstreamRunnable: false,\n\t\t\t});\n\t\t\tagent.streamRunnable = false;\n\t\t\t// Wrap the agent with parsers and fixes.\n\t\t\tconst runnableAgent = RunnableSequence.from([\n\t\t\t\tagent,\n\t\t\t\tgetAgentStepsParser(outputParser, memory),\n\t\t\t\tfixEmptyContentMessage,\n\t\t\t]);\n\t\t\tconst executor = AgentExecutor.fromAgentAndTools({\n\t\t\t\tagent: runnableAgent,\n\t\t\t\tmemory,\n\t\t\t\ttools,\n\t\t\t\treturnIntermediateSteps: options.returnIntermediateSteps === true,\n\t\t\t\tmaxIterations: options.maxIterations ?? 10,\n\t\t\t});\n\n\t\t\t// Invoke the executor with the given input and system message.\n\t\t\tconst response = await executor.invoke(\n\t\t\t\t{\n\t\t\t\t\tinput,\n\t\t\t\t\tsystem_message: options.systemMessage ?? SYSTEM_MESSAGE,\n\t\t\t\t\tformatting_instructions:\n\t\t\t\t\t\t'IMPORTANT: For your response to user, you MUST use the `format_final_json_response` tool with your complete answer formatted according to the required schema. Do not attempt to format the JSON manually - always use this tool. Your response will be rejected if it is not properly formatted through this tool. Only use this tool once you are ready to provide your final answer.',\n\t\t\t\t},\n\t\t\t\t{ signal: this.getExecutionCancelSignal() },\n\t\t\t);\n\n\t\t\t// If memory and outputParser are connected, parse the output.\n\t\t\tif (memory && outputParser) {\n\t\t\t\tconst parsedOutput = jsonParse<{ output: Record<string, unknown> }>(\n\t\t\t\t\tresponse.output as string,\n\t\t\t\t);\n\t\t\t\tresponse.output = parsedOutput?.output ?? parsedOutput;\n\t\t\t}\n\n\t\t\t// Omit internal keys before returning the result.\n\t\t\tconst itemResult = {\n\t\t\t\tjson: omit(\n\t\t\t\t\tresponse,\n\t\t\t\t\t'system_message',\n\t\t\t\t\t'formatting_instructions',\n\t\t\t\t\t'input',\n\t\t\t\t\t'chat_history',\n\t\t\t\t\t'agent_scratchpad',\n\t\t\t\t),\n\t\t\t};\n\n\t\t\treturnData.push(itemResult);\n\t\t} catch (error) {\n\t\t\tif (this.continueOnFail()) {\n\t\t\t\treturnData.push({\n\t\t\t\t\tjson: { error: error.message },\n\t\t\t\t\tpairedItem: { item: itemIndex },\n\t\t\t\t});\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthrow error;\n\t\t}\n\t}\n\n\treturn [returnData];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAAiC;AACjC,oBAAsD;AACtD,kBAAiB;AACjB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GD,YAAM,WAAW,MAAM,SAAS;AAAA,QAC/B;AAAA,UACC;AAAA,UACA,gBAAgB,QAAQ,iBAAiB;AAAA,UACzC,yBACC;AAAA,QACF;AAAA,QACA,EAAE,QAAQ,KAAK,yBAAyB,EAAE;AAAA,MAC3C;AAGA,UAAI,UAAU,cAAc;AAC3B,cAAM,mBAAe;AAAA,UACpB,SAAS;AAAA,QACV;AACA,iBAAS,SAAS,cAAc,UAAU;AAAA,MAC3C;AAGA,YAAM,aAAa;AAAA,QAClB,UAAM,YAAAA;AAAA,UACL;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACD;AAAA,MACD;AAEA,iBAAW,KAAK,UAAU;AAAA,IAC3B,SAAS,OAAO;AACf,UAAI,KAAK,eAAe,GAAG;AAC1B,mBAAW,KAAK;AAAA,UACf,MAAM,EAAE,OAAO,MAAM,QAAQ;AAAA,UAC7B,YAAY,EAAE,MAAM,UAAU;AAAA,QAC/B,CAAC;AACD;AAAA,MACD;AACA,YAAM;AAAA,IACP;AAAA,EACD;AAEA,SAAO,CAAC,UAAU;AACnB;","names":["omit"]}
1
+ {"version":3,"sources":["../../../../../../../nodes/agents/Agent/agents/ToolsAgent/V1/execute.ts"],"sourcesContent":["import type { BaseLanguageModel } from '@langchain/core/language_models/base';\nimport { RunnableSequence } from '@langchain/core/runnables';\nimport { AgentExecutor, createToolCallingAgent } from 'langchain/agents';\nimport omit from 'lodash/omit';\nimport { jsonParse, NodeOperationError } from 'n8n-workflow';\nimport type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';\n\nimport { getPromptInputByType } from '@utils/helpers';\nimport { getOptionalOutputParser } from '@utils/output_parsers/N8nOutputParser';\n\nimport {\n\tfixEmptyContentMessage,\n\tgetAgentStepsParser,\n\tgetChatModel,\n\tgetOptionalMemory,\n\tgetTools,\n\tprepareMessages,\n\tpreparePrompt,\n} from '../common';\nimport { SYSTEM_MESSAGE } from '../prompt';\n\n/* -----------------------------------------------------------\n Main Executor Function\n----------------------------------------------------------- */\n/**\n * The main executor method for the Tools Agent.\n *\n * This function retrieves necessary components (model, memory, tools), prepares the prompt,\n * creates the agent, and processes each input item. The error handling for each item is also\n * managed here based on the node's continueOnFail setting.\n *\n * @returns The array of execution data for all processed items\n */\nexport async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {\n\tthis.logger.debug('Executing Tools Agent');\n\n\tconst returnData: INodeExecutionData[] = [];\n\tconst items = this.getInputData();\n\tconst outputParser = await getOptionalOutputParser(this);\n\tconst tools = await getTools(this, outputParser);\n\n\tfor (let itemIndex = 0; itemIndex < items.length; itemIndex++) {\n\t\ttry {\n\t\t\tconst model = (await getChatModel(this)) as BaseLanguageModel;\n\t\t\tconst memory = await getOptionalMemory(this);\n\n\t\t\tconst input = getPromptInputByType({\n\t\t\t\tctx: this,\n\t\t\t\ti: itemIndex,\n\t\t\t\tinputKey: 'text',\n\t\t\t\tpromptTypeKey: 'promptType',\n\t\t\t});\n\t\t\tif (input === undefined) {\n\t\t\t\tthrow new NodeOperationError(this.getNode(), 'The “text” parameter is empty.');\n\t\t\t}\n\n\t\t\tconst options = this.getNodeParameter('options', itemIndex, {}) as {\n\t\t\t\tsystemMessage?: string;\n\t\t\t\tmaxIterations?: number;\n\t\t\t\treturnIntermediateSteps?: boolean;\n\t\t\t\tpassthroughBinaryImages?: boolean;\n\t\t\t};\n\n\t\t\t// Prepare the prompt messages and prompt template.\n\t\t\tconst messages = await prepareMessages(this, itemIndex, {\n\t\t\t\tsystemMessage: options.systemMessage,\n\t\t\t\tpassthroughBinaryImages: options.passthroughBinaryImages ?? true,\n\t\t\t\toutputParser,\n\t\t\t});\n\t\t\tconst prompt = preparePrompt(messages);\n\n\t\t\t// Create the base agent that calls tools.\n\t\t\tconst agent = createToolCallingAgent({\n\t\t\t\tllm: model,\n\t\t\t\ttools,\n\t\t\t\tprompt,\n\t\t\t\tstreamRunnable: false,\n\t\t\t});\n\t\t\tagent.streamRunnable = false;\n\t\t\t// Wrap the agent with parsers and fixes.\n\t\t\tconst runnableAgent = RunnableSequence.from([\n\t\t\t\tagent,\n\t\t\t\tgetAgentStepsParser(outputParser, memory),\n\t\t\t\tfixEmptyContentMessage,\n\t\t\t]);\n\t\t\tconst executor = AgentExecutor.fromAgentAndTools({\n\t\t\t\tagent: runnableAgent,\n\t\t\t\tmemory,\n\t\t\t\ttools,\n\t\t\t\treturnIntermediateSteps: options.returnIntermediateSteps === true,\n\t\t\t\tmaxIterations: options.maxIterations ?? 10,\n\t\t\t});\n\n\t\t\t// Invoke the executor with the given input and system message.\n\t\t\tconst response = await executor.invoke(\n\t\t\t\t{\n\t\t\t\t\tinput,\n\t\t\t\t\tsystem_message: options.systemMessage ?? SYSTEM_MESSAGE,\n\t\t\t\t\tformatting_instructions:\n\t\t\t\t\t\t'IMPORTANT: For your response to user, you MUST use the `format_final_json_response` tool with your complete answer formatted according to the required schema. Do not attempt to format the JSON manually - always use this tool. Your response will be rejected if it is not properly formatted through this tool. Only use this tool once you are ready to provide your final answer.',\n\t\t\t\t},\n\t\t\t\t{ signal: this.getExecutionCancelSignal() },\n\t\t\t);\n\n\t\t\t// If memory and outputParser are connected, parse the output.\n\t\t\tif (memory && outputParser) {\n\t\t\t\tconst parsedOutput = jsonParse<{ output: Record<string, unknown> }>(\n\t\t\t\t\tresponse.output as string,\n\t\t\t\t);\n\t\t\t\tresponse.output = parsedOutput?.output ?? parsedOutput;\n\t\t\t}\n\n\t\t\t// Omit internal keys before returning the result.\n\t\t\tconst itemResult = {\n\t\t\t\tjson: omit(\n\t\t\t\t\tresponse,\n\t\t\t\t\t'system_message',\n\t\t\t\t\t'formatting_instructions',\n\t\t\t\t\t'input',\n\t\t\t\t\t'chat_history',\n\t\t\t\t\t'agent_scratchpad',\n\t\t\t\t),\n\t\t\t};\n\n\t\t\treturnData.push(itemResult);\n\t\t} catch (error) {\n\t\t\tif (this.continueOnFail()) {\n\t\t\t\treturnData.push({\n\t\t\t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M,gBAAgB,kCAAiB,KAAK;AAAA,QAC3C;AAAA,YACA,mCAAoB,cAAc,MAAM;AAAA,QACxC;AAAA,MACD,CAAC;AACD,YAAM,WAAW,4BAAc,kBAAkB;AAAA,QAChD,OAAO;AAAA,QACP;AAAA,QACA;AAAA,QACA,yBAAyB,QAAQ,4BAA4B;AAAA,QAC7D,eAAe,QAAQ,iBAAiB;AAAA,MACzC,CAAC;AAGD,YAAM,WAAW,MAAM,SAAS;AAAA,QAC/B;AAAA,UACC;AAAA,UACA,gBAAgB,QAAQ,iBAAiB;AAAA,UACzC,yBACC;AAAA,QACF;AAAA,QACA,EAAE,QAAQ,KAAK,yBAAyB,EAAE;AAAA,MAC3C;AAGA,UAAI,UAAU,cAAc;AAC3B,cAAM,mBAAe;AAAA,UACpB,SAAS;AAAA,QACV;AACA,iBAAS,SAAS,cAAc,UAAU;AAAA,MAC3C;AAGA,YAAM,aAAa;AAAA,QAClB,UAAM,YAAAA;AAAA,UACL;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACD;AAAA,MACD;AAEA,iBAAW,KAAK,UAAU;AAAA,IAC3B,SAAS,OAAO;AACf,UAAI,KAAK,eAAe,GAAG;AAC1B,mBAAW,KAAK;AAAA,UACf,MAAM,EAAE,OAAO,MAAM,QAAQ;AAAA,UAC7B,YAAY,EAAE,MAAM,UAAU;AAAA,QAC/B,CAAC;AACD;AAAA,MACD;AACA,YAAM;AAAA,IACP;AAAA,EACD;AAEA,SAAO,CAAC,UAAU;AACnB;","names":["omit"]}
@@ -35,24 +35,61 @@ var import_runnables = require("@langchain/core/runnables");
35
35
  var import_agents = require("langchain/agents");
36
36
  var import_omit = __toESM(require("lodash/omit"));
37
37
  var import_n8n_workflow = require("n8n-workflow");
38
+ var import_node_assert = __toESM(require("node:assert"));
38
39
  var import_helpers = require("../../../../../../utils/helpers");
39
40
  var import_N8nOutputParser = require("../../../../../../utils/output_parsers/N8nOutputParser");
40
41
  var import_common = require("../common");
41
42
  var import_prompt = require("../prompt");
43
+ function createAgentExecutor(model, tools, prompt, options, outputParser, memory, fallbackModel) {
44
+ const agent = (0, import_agents.createToolCallingAgent)({
45
+ llm: model,
46
+ tools,
47
+ prompt,
48
+ streamRunnable: false
49
+ });
50
+ let fallbackAgent;
51
+ if (fallbackModel) {
52
+ fallbackAgent = (0, import_agents.createToolCallingAgent)({
53
+ llm: fallbackModel,
54
+ tools,
55
+ prompt,
56
+ streamRunnable: false
57
+ });
58
+ }
59
+ const runnableAgent = import_runnables.RunnableSequence.from([
60
+ fallbackAgent ? agent.withFallbacks([fallbackAgent]) : agent,
61
+ (0, import_common.getAgentStepsParser)(outputParser, memory),
62
+ import_common.fixEmptyContentMessage
63
+ ]);
64
+ return import_agents.AgentExecutor.fromAgentAndTools({
65
+ agent: runnableAgent,
66
+ memory,
67
+ tools,
68
+ returnIntermediateSteps: options.returnIntermediateSteps === true,
69
+ maxIterations: options.maxIterations ?? 10
70
+ });
71
+ }
42
72
  async function toolsAgentExecute() {
43
73
  this.logger.debug("Executing Tools Agent V2");
44
74
  const returnData = [];
45
75
  const items = this.getInputData();
46
- const outputParser = await (0, import_N8nOutputParser.getOptionalOutputParser)(this);
47
- const tools = await (0, import_common.getTools)(this, outputParser);
48
76
  const batchSize = this.getNodeParameter("options.batching.batchSize", 0, 1);
49
77
  const delayBetweenBatches = this.getNodeParameter(
50
78
  "options.batching.delayBetweenBatches",
51
79
  0,
52
80
  0
53
81
  );
82
+ const needsFallback = this.getNodeParameter("needsFallback", 0, false);
54
83
  const memory = await (0, import_common.getOptionalMemory)(this);
55
- const model = await (0, import_common.getChatModel)(this);
84
+ const model = await (0, import_common.getChatModel)(this, 0);
85
+ (0, import_node_assert.default)(model, "Please connect a model to the Chat Model input");
86
+ const fallbackModel = needsFallback ? await (0, import_common.getChatModel)(this, 1) : null;
87
+ if (needsFallback && !fallbackModel) {
88
+ throw new import_n8n_workflow.NodeOperationError(
89
+ this.getNode(),
90
+ "Please connect a model to the Fallback Model input or disable the fallback option"
91
+ );
92
+ }
56
93
  for (let i = 0; i < items.length; i += batchSize) {
57
94
  const batch = items.slice(i, i + batchSize);
58
95
  const batchPromises = batch.map(async (_item, batchItemIndex) => {
@@ -64,44 +101,36 @@ async function toolsAgentExecute() {
64
101
  promptTypeKey: "promptType"
65
102
  });
66
103
  if (input === void 0) {
67
- throw new import_n8n_workflow.NodeOperationError(this.getNode(), "The \u201Ctext\u201D parameter is empty.");
104
+ throw new import_n8n_workflow.NodeOperationError(this.getNode(), 'The "text" parameter is empty.');
68
105
  }
106
+ const outputParser2 = await (0, import_N8nOutputParser.getOptionalOutputParser)(this, itemIndex);
107
+ const tools = await (0, import_common.getTools)(this, outputParser2);
69
108
  const options = this.getNodeParameter("options", itemIndex, {});
70
109
  const messages = await (0, import_common.prepareMessages)(this, itemIndex, {
71
110
  systemMessage: options.systemMessage,
72
111
  passthroughBinaryImages: options.passthroughBinaryImages ?? true,
73
- outputParser
112
+ outputParser: outputParser2
74
113
  });
75
114
  const prompt = (0, import_common.preparePrompt)(messages);
76
- const agent = (0, import_agents.createToolCallingAgent)({
77
- llm: model,
115
+ const executor = createAgentExecutor(
116
+ model,
78
117
  tools,
79
118
  prompt,
80
- streamRunnable: false
81
- });
82
- agent.streamRunnable = false;
83
- const runnableAgent = import_runnables.RunnableSequence.from([
84
- agent,
85
- (0, import_common.getAgentStepsParser)(outputParser, memory),
86
- import_common.fixEmptyContentMessage
87
- ]);
88
- const executor = import_agents.AgentExecutor.fromAgentAndTools({
89
- agent: runnableAgent,
119
+ options,
120
+ outputParser2,
90
121
  memory,
91
- tools,
92
- returnIntermediateSteps: options.returnIntermediateSteps === true,
93
- maxIterations: options.maxIterations ?? 10
94
- });
95
- return await executor.invoke(
96
- {
97
- input,
98
- system_message: options.systemMessage ?? import_prompt.SYSTEM_MESSAGE,
99
- formatting_instructions: "IMPORTANT: For your response to user, you MUST use the `format_final_json_response` tool with your complete answer formatted according to the required schema. Do not attempt to format the JSON manually - always use this tool. Your response will be rejected if it is not properly formatted through this tool. Only use this tool once you are ready to provide your final answer."
100
- },
101
- { signal: this.getExecutionCancelSignal() }
122
+ fallbackModel
102
123
  );
124
+ const invokeParams = {
125
+ input,
126
+ system_message: options.systemMessage ?? import_prompt.SYSTEM_MESSAGE,
127
+ formatting_instructions: "IMPORTANT: For your response to user, you MUST use the `format_final_json_response` tool with your complete answer formatted according to the required schema. Do not attempt to format the JSON manually - always use this tool. Your response will be rejected if it is not properly formatted through this tool. Only use this tool once you are ready to provide your final answer."
128
+ };
129
+ const executeOptions = { signal: this.getExecutionCancelSignal() };
130
+ return await executor.invoke(invokeParams, executeOptions);
103
131
  });
104
132
  const batchResults = await Promise.allSettled(batchPromises);
133
+ const outputParser = await (0, import_N8nOutputParser.getOptionalOutputParser)(this, 0);
105
134
  batchResults.forEach((result, index) => {
106
135
  const itemIndex = i + index;
107
136
  if (result.status === "rejected") {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../nodes/agents/Agent/agents/ToolsAgent/V2/execute.ts"],"sourcesContent":["import type { ChatPromptTemplate } from '@langchain/core/prompts';\nimport { RunnableSequence } from '@langchain/core/runnables';\nimport { AgentExecutor, createToolCallingAgent } from 'langchain/agents';\nimport omit from 'lodash/omit';\nimport { jsonParse, NodeOperationError, sleep } from 'n8n-workflow';\nimport type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';\n\nimport { getPromptInputByType } from '@utils/helpers';\nimport { getOptionalOutputParser } from '@utils/output_parsers/N8nOutputParser';\n\nimport {\n\tfixEmptyContentMessage,\n\tgetAgentStepsParser,\n\tgetChatModel,\n\tgetOptionalMemory,\n\tgetTools,\n\tprepareMessages,\n\tpreparePrompt,\n} from '../common';\nimport { SYSTEM_MESSAGE } from '../prompt';\n\n/* -----------------------------------------------------------\n Main Executor Function\n----------------------------------------------------------- */\n/**\n * The main executor method for the Tools Agent.\n *\n * This function retrieves necessary components (model, memory, tools), prepares the prompt,\n * creates the agent, and processes each input item. The error handling for each item is also\n * managed here based on the node's continueOnFail setting.\n *\n * @returns The array of execution data for all processed items\n */\nexport async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {\n\tthis.logger.debug('Executing Tools Agent V2');\n\n\tconst returnData: INodeExecutionData[] = [];\n\tconst items = this.getInputData();\n\tconst outputParser = await getOptionalOutputParser(this);\n\tconst tools = await getTools(this, outputParser);\n\tconst batchSize = this.getNodeParameter('options.batching.batchSize', 0, 1) as number;\n\tconst delayBetweenBatches = this.getNodeParameter(\n\t\t'options.batching.delayBetweenBatches',\n\t\t0,\n\t\t0,\n\t) as number;\n\tconst memory = await getOptionalMemory(this);\n\tconst model = await getChatModel(this);\n\n\tfor (let i = 0; i < items.length; i += batchSize) {\n\t\tconst batch = items.slice(i, i + batchSize);\n\t\tconst batchPromises = batch.map(async (_item, batchItemIndex) => {\n\t\t\tconst itemIndex = i + batchItemIndex;\n\n\t\t\tconst input = getPromptInputByType({\n\t\t\t\tctx: this,\n\t\t\t\ti: itemIndex,\n\t\t\t\tinputKey: 'text',\n\t\t\t\tpromptTypeKey: 'promptType',\n\t\t\t});\n\t\t\tif (input === undefined) {\n\t\t\t\tthrow new NodeOperationError(this.getNode(), 'The “text” parameter is empty.');\n\t\t\t}\n\n\t\t\tconst options = this.getNodeParameter('options', itemIndex, {}) as {\n\t\t\t\tsystemMessage?: string;\n\t\t\t\tmaxIterations?: number;\n\t\t\t\treturnIntermediateSteps?: boolean;\n\t\t\t\tpassthroughBinaryImages?: boolean;\n\t\t\t};\n\n\t\t\t// Prepare the prompt messages and prompt template.\n\t\t\tconst messages = await prepareMessages(this, itemIndex, {\n\t\t\t\tsystemMessage: options.systemMessage,\n\t\t\t\tpassthroughBinaryImages: options.passthroughBinaryImages ?? true,\n\t\t\t\toutputParser,\n\t\t\t});\n\t\t\tconst prompt: ChatPromptTemplate = preparePrompt(messages);\n\n\t\t\t// Create the base agent that calls tools.\n\t\t\tconst agent = createToolCallingAgent({\n\t\t\t\tllm: model,\n\t\t\t\ttools,\n\t\t\t\tprompt,\n\t\t\t\tstreamRunnable: false,\n\t\t\t});\n\t\t\tagent.streamRunnable = false;\n\t\t\t// Wrap the agent with parsers and fixes.\n\t\t\tconst runnableAgent = RunnableSequence.from([\n\t\t\t\tagent,\n\t\t\t\tgetAgentStepsParser(outputParser, memory),\n\t\t\t\tfixEmptyContentMessage,\n\t\t\t]);\n\t\t\tconst executor = AgentExecutor.fromAgentAndTools({\n\t\t\t\tagent: runnableAgent,\n\t\t\t\tmemory,\n\t\t\t\ttools,\n\t\t\t\treturnIntermediateSteps: options.returnIntermediateSteps === true,\n\t\t\t\tmaxIterations: options.maxIterations ?? 10,\n\t\t\t});\n\n\t\t\t// Invoke the executor with the given input and system message.\n\t\t\treturn await executor.invoke(\n\t\t\t\t{\n\t\t\t\t\tinput,\n\t\t\t\t\tsystem_message: options.systemMessage ?? SYSTEM_MESSAGE,\n\t\t\t\t\tformatting_instructions:\n\t\t\t\t\t\t'IMPORTANT: For your response to user, you MUST use the `format_final_json_response` tool with your complete answer formatted according to the required schema. Do not attempt to format the JSON manually - always use this tool. Your response will be rejected if it is not properly formatted through this tool. Only use this tool once you are ready to provide your final answer.',\n\t\t\t\t},\n\t\t\t\t{ signal: this.getExecutionCancelSignal() },\n\t\t\t);\n\t\t});\n\n\t\tconst batchResults = await Promise.allSettled(batchPromises);\n\n\t\tbatchResults.forEach((result, index) => {\n\t\t\tconst itemIndex = i + index;\n\t\t\tif (result.status === 'rejected') {\n\t\t\t\tconst error = result.reason as Error;\n\t\t\t\tif (this.continueOnFail()) {\n\t\t\t\t\treturnData.push({\n\t\t\t\t\t\tjson: { error: error.message },\n\t\t\t\t\t\tpairedItem: { item: itemIndex },\n\t\t\t\t\t});\n\t\t\t\t\treturn;\n\t\t\t\t} else {\n\t\t\t\t\tthrow new NodeOperationError(this.getNode(), error);\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst response = result.value;\n\t\t\t// If memory and outputParser are connected, parse the output.\n\t\t\tif (memory && outputParser) {\n\t\t\t\tconst parsedOutput = jsonParse<{ output: Record<string, unknown> }>(\n\t\t\t\t\tresponse.output as string,\n\t\t\t\t);\n\t\t\t\tresponse.output = parsedOutput?.output ?? parsedOutput;\n\t\t\t}\n\n\t\t\t// Omit internal keys before returning the result.\n\t\t\tconst itemResult = {\n\t\t\t\tjson: omit(\n\t\t\t\t\tresponse,\n\t\t\t\t\t'system_message',\n\t\t\t\t\t'formatting_instructions',\n\t\t\t\t\t'input',\n\t\t\t\t\t'chat_history',\n\t\t\t\t\t'agent_scratchpad',\n\t\t\t\t),\n\t\t\t\tpairedItem: { item: itemIndex },\n\t\t\t};\n\n\t\t\treturnData.push(itemResult);\n\t\t});\n\n\t\tif (i + batchSize < items.length && delayBetweenBatches > 0) {\n\t\t\tawait sleep(delayBetweenBatches);\n\t\t}\n\t}\n\n\treturn [returnData];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,uBAAiC;AACjC,oBAAsD;AACtD,kBAAiB;AACjB,0BAAqD;AAGrD,qBAAqC;AACrC,6BAAwC;AAExC,oBAQO;AACP,oBAA+B;AAc/B,eAAsB,oBAA4E;AACjG,OAAK,OAAO,MAAM,0BAA0B;AAE5C,QAAM,aAAmC,CAAC;AAC1C,QAAM,QAAQ,KAAK,aAAa;AAChC,QAAM,eAAe,UAAM,gDAAwB,IAAI;AACvD,QAAM,QAAQ,UAAM,wBAAS,MAAM,YAAY;AAC/C,QAAM,YAAY,KAAK,iBAAiB,8BAA8B,GAAG,CAAC;AAC1E,QAAM,sBAAsB,KAAK;AAAA,IAChC;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACA,QAAM,SAAS,UAAM,iCAAkB,IAAI;AAC3C,QAAM,QAAQ,UAAM,4BAAa,IAAI;AAErC,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK,WAAW;AACjD,UAAM,QAAQ,MAAM,MAAM,GA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WAAW,YAAY;AACjC,cAAM,QAAQ,OAAO;AACrB,YAAI,KAAK,eAAe,GAAG;AAC1B,qBAAW,KAAK;AAAA,YACf,MAAM,EAAE,OAAO,MAAM,QAAQ;AAAA,YAC7B,YAAY,EAAE,MAAM,UAAU;AAAA,UAC/B,CAAC;AACD;AAAA,QACD,OAAO;AACN,gBAAM,IAAI,uCAAmB,KAAK,QAAQ,GAAG,KAAK;AAAA,QACnD;AAAA,MACD;AACA,YAAM,WAAW,OAAO;AAExB,UAAI,UAAU,cAAc;AAC3B,cAAM,mBAAe;AAAA,UACpB,SAAS;AAAA,QACV;AACA,iBAAS,SAAS,cAAc,UAAU;AAAA,MAC3C;AAGA,YAAM,aAAa;AAAA,QAClB,UAAM,YAAAA;AAAA,UACL;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,QACD;AAAA,QACA,YAAY,EAAE,MAAM,UAAU;AAAA,MAC/B;AAEA,iBAAW,KAAK,UAAU;AAAA,IAC3B,CAAC;AAED,QAAI,IAAI,YAAY,MAAM,UAAU,sBAAsB,GAAG;AAC5D,gBAAM,2BAAM,mBAAmB;AAAA,IAChC;AAAA,EACD;AAEA,SAAO,CAAC,UAAU;AACnB;","names":["omit"]}
1
+ {"version":3,"sources":["../../../../../../../nodes/agents/Agent/agents/ToolsAgent/V2/execute.ts"],"sourcesContent":["import type { BaseChatModel } from '@langchain/core/language_models/chat_models';\nimport type { ChatPromptTemplate } from '@langchain/core/prompts';\nimport { RunnableSequence } from '@langchain/core/runnables';\nimport {\n\tAgentExecutor,\n\ttype AgentRunnableSequence,\n\tcreateToolCallingAgent,\n} from 'langchain/agents';\nimport type { BaseChatMemory } from 'langchain/memory';\nimport type { DynamicStructuredTool, Tool } from 'langchain/tools';\nimport omit from 'lodash/omit';\nimport { jsonParse, NodeOperationError, sleep } from 'n8n-workflow';\nimport type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';\nimport assert from 'node:assert';\n\nimport { getPromptInputByType } from '@utils/helpers';\nimport {\n\tgetOptionalOutputParser,\n\ttype N8nOutputParser,\n} from '@utils/output_parsers/N8nOutputParser';\n\nimport {\n\tfixEmptyContentMessage,\n\tgetAgentStepsParser,\n\tgetChatModel,\n\tgetOptionalMemory,\n\tgetTools,\n\tprepareMessages,\n\tpreparePrompt,\n} from '../common';\nimport { SYSTEM_MESSAGE } from '../prompt';\n\n/**\n * Creates an agent executor with the given configuration\n */\nfunction createAgentExecutor(\n\tmodel: BaseChatModel,\n\ttools: Array<DynamicStructuredTool | Tool>,\n\tprompt: ChatPromptTemplate,\n\toptions: { maxIterations?: number; returnIntermediateSteps?: boolean },\n\toutputParser?: N8nOutputParser,\n\tmemory?: BaseChatMemory,\n\tfallbackModel?: BaseChatModel | null,\n) {\n\tconst agent = createToolCallingAgent({\n\t\tllm: model,\n\t\ttools,\n\t\tprompt,\n\t\tstreamRunnable: false,\n\t});\n\n\tlet fallbackAgent: AgentRunnableSequence | undefined;\n\tif (fallbackModel) {\n\t\tfallbackAgent = createToolCallingAgent({\n\t\t\tllm: fallbackModel,\n\t\t\ttools,\n\t\t\tprompt,\n\t\t\tstreamRunnable: false,\n\t\t});\n\t}\n\tconst runnableAgent = RunnableSequence.from([\n\t\tfallbackAgent ? agent.withFallbacks([fallbackAgent]) : agent,\n\t\tgetAgentStepsParser(outputParser, memory),\n\t\tfixEmptyContentMessage,\n\t]);\n\n\treturn AgentExecutor.fromAgentAndTools({\n\t\tagent: runnableAgent,\n\t\tmemory,\n\t\ttools,\n\t\treturnIntermediateSteps: options.returnIntermediateSteps === true,\n\t\tmaxIterations: options.maxIterations ?? 10,\n\t});\n}\n\n/* -----------------------------------------------------------\n Main Executor Function\n----------------------------------------------------------- */\n/**\n * The main executor method for the Tools Agent.\n *\n * This function retrieves necessary components (model, memory, tools), prepares the prompt,\n * creates the agent, and processes each input item. The error handling for each item is also\n * managed here based on the node's continueOnFail setting.\n *\n * @returns The array of execution data for all processed items\n */\nexport async function toolsAgentExecute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {\n\tthis.logger.debug('Executing Tools Agent V2');\n\n\tconst returnData: INodeExecutionData[] = [];\n\tconst items = this.getInputData();\n\tconst batchSize = this.getNodeParameter('options.batching.batchSize', 0, 1) as number;\n\tconst delayBetweenBatches = this.getNodeParameter(\n\t\t'options.batching.delayBetweenBatches',\n\t\t0,\n\t\t0,\n\t) as number;\n\tconst needsFallback = this.getNodeParameter('needsFallback', 0, false) as boolean;\n\tconst memory = await getOptionalMemory(this);\n\tconst model = await getChatModel(this, 0);\n\tassert(model, 'Please connect a model to the Chat Model input');\n\tconst fallbackModel = needsFallback ? await getChatModel(this, 1) : null;\n\n\tif (needsFallback && !fallbackModel) {\n\t\tthrow new NodeOperationError(\n\t\t\tthis.getNode(),\n\t\t\t'Please connect a model to the Fallback Model input or disable the fallback option',\n\t\t);\n\t}\n\n\tfor (let i = 0; i < items.length; i += batchSize) {\n\t\tconst batch = items.slice(i, i + batchSize);\n\t\tconst batchPromises = batch.map(async (_item, batchItemIndex) => {\n\t\t\tconst itemIndex = i + batchItemIndex;\n\n\t\t\tconst input = getPromptInputByType({\n\t\t\t\tctx: this,\n\t\t\t\ti: itemIndex,\n\t\t\t\tinputKey: 'text',\n\t\t\t\tpromptTypeKey: 'promptType',\n\t\t\t});\n\t\t\tif (input === undefined) {\n\t\t\t\tthrow new NodeOperationError(this.getNode(), 'The \"text\" parameter is empty.');\n\t\t\t}\n\t\t\tconst outputParser = await getOptionalOutputParser(this, itemIndex);\n\t\t\tconst tools = await getTools(this, outputParser);\n\t\t\tconst options = this.getNodeParameter('options', itemIndex, {}) as {\n\t\t\t\tsystemMessage?: string;\n\t\t\t\tmaxIterations?: number;\n\t\t\t\treturnIntermediateSteps?: boolean;\n\t\t\t\tpassthroughBinaryImages?: boolean;\n\t\t\t};\n\n\t\t\t// Prepare the prompt messages and prompt template.\n\t\t\tconst messages = await prepareMessages(this, itemIndex, {\n\t\t\t\tsystemMessage: options.systemMessage,\n\t\t\t\tpassthroughBinaryImages: options.passthroughBinaryImages ?? true,\n\t\t\t\toutputParser,\n\t\t\t});\n\t\t\tconst prompt: ChatPromptTemplate = preparePrompt(messages);\n\n\t\t\t// Create executors for primary and fallback models\n\t\t\tconst executor = createAgentExecutor(\n\t\t\t\tmodel,\n\t\t\t\ttools,\n\t\t\t\tprompt,\n\t\t\t\toptions,\n\t\t\t\toutputParser,\n\t\t\t\tmemory,\n\t\t\t\tfallbackModel,\n\t\t\t);\n\t\t\t// Invoke with fallback logic\n\t\t\tconst invokeParams = {\n\t\t\t\tinput,\n\t\t\t\tsystem_message: options.systemMessage ?? SYSTEM_MESSAGE,\n\t\t\t\tformatting_instructions:\n\t\t\t\t\t'IMPORTANT: For your response to user, you MUST use the `format_final_json_response` tool with your complete answer formatted according to the required schema. Do not attempt to format the JSON manually - always use this tool. Your response will be rejected if it is not properly formatted through this tool. Only use this tool once you are ready to provide your final answer.',\n\t\t\t};\n\t\t\tconst executeOptions = { signal: this.getExecutionCancelSignal() };\n\n\t\t\treturn await executor.invoke(invokeParams, executeOptions);\n\t\t});\n\n\t\tconst batchResults = await Promise.allSettled(batchPromises);\n\t\t// This is only used to check if the output parser is connected\n\t\t// so we can parse the output if needed. Actual output parsing is done in the loop above\n\t\tconst outputParser = await getOptionalOutputParser(this, 0);\n\t\tbatchResults.forEach((result, index) => {\n\t\t\tconst itemIndex = i + index;\n\t\t\tif (result.status === 'rejected') {\n\t\t\t\tconst error = result.reason as Error;\n\t\t\t\tif (this.continueOnFail()) {\n\t\t\t\t\treturnData.push({\n\t\t\t\t\t\tjson: { error: error.message },\n\t\t\t\t\t\tpairedItem: { item: itemIndex },\n\t\t\t\t\t});\n\t\t\t\t\treturn;\n\t\t\t\t} else {\n\t\t\t\t\tthrow new NodeOperationError(this.getNode(), error);\n\t\t\t\t}\n\t\t\t}\n\t\t\tconst response = result.value;\n\t\t\t// If memory and outputParser are connected, parse the output.\n\t\t\tif (memory && outputParser) {\n\t\t\t\tconst parsedOutput = jsonParse<{ output: Record<string, unknown> }>(\n\t\t\t\t\tresponse.output as string,\n\t\t\t\t);\n\t\t\t\tresponse.output = parsedOutput?.output ?? parsedOutput;\n\t\t\t}\n\n\t\t\t// Omit internal keys before returning the result.\n\t\t\tconst itemResult = {\n\t\t\t\tjson: omit(\n\t\t\t\t\tresponse,\n\t\t\t\t\t'system_message',\n\t\t\t\t\t'formatting_instructions',\n\t\t\t\t\t'input',\n\t\t\t\t\t'chat_history',\n\t\t\t\t\t'agent_scratchpad',\n\t\t\t\t),\n\t\t\t\tpairedItem: { item: itemIndex },\n\t\t\t};\n\n\t\t\treturnData.push(itemResult);\n\t\t});\n\n\t\tif (i + batchSize < items.length && delayBetweenBatches > 0) {\n\t\t\tawait sleep(delayBetweenBatches);\n\t\t}\n\t}\n\n\treturn [returnData];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,uBAAiC;AACjC,oBAIO;AAGP,kBAAiB;AACjB,0BAAqD;AAErD,yBAAmB;AAEnB,qBAAqC;AACrC,6BAGO;AAEP,oBAQO;AACP,oBAA+B;AAK/B,SAAS,oBACR,OACA,OACA,QACA,SACA,cACA,QACA,eACC;AACD,QAAM,YAAQ,sCAAuB;AAAA,IACpC,KAAK;AAAA,IACL;AAAA,IACA;AAAA,IACA,gBAAgB;AAAA,EACjB,CAAC;AAED,MAAI;AACJ,MAAI,eAAe;AAClB,wBAAgB,sCAAuB;AAAA,MACtC,KAAK;AAAA,MACL;AAAA,MACA;AAAA,MACA,gBAAgB;AAAA,IACjB,CAAC;AAAA,EACF;AACA,QAAM,gBAAgB,kCAAiB,KAAK;AAAA,IAC3C,gBAAgB,MAAM,cAAc,CAAC,aAAa,CAAC,IAAI;AAAA,QACvD,mCAAoB,cAAc,MAAM;AAAA,IACxC;AAAA,EACD,CAAC;AAED,SAAO,4BAAc,kBAAkB;AAAA,IACtC,OAAO;AAAA,IACP;AAAA,IACA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C/B;AAEA,iBAAW,KAAK,UAAU;AAAA,IAC3B,CAAC;AAED,QAAI,IAAI,YAAY,MAAM,UAAU,sBAAsB,GAAG;AAC5D,gBAAM,2BAAM,mBAAmB;AAAA,IAChC;AAAA,EACD;AAEA,SAAO,CAAC,UAAU;AACnB;","names":["assert","outputParser","omit"]}
@@ -142,8 +142,18 @@ const getAgentStepsParser = (outputParser, memory) => async (steps) => {
142
142
  }
143
143
  return handleAgentFinishOutput(steps);
144
144
  };
145
- async function getChatModel(ctx) {
146
- const model = await ctx.getInputConnectionData(import_n8n_workflow.NodeConnectionTypes.AiLanguageModel, 0);
145
+ async function getChatModel(ctx, index = 0) {
146
+ const connectedModels = await ctx.getInputConnectionData(import_n8n_workflow.NodeConnectionTypes.AiLanguageModel, 0);
147
+ let model;
148
+ if (Array.isArray(connectedModels) && index !== void 0) {
149
+ if (connectedModels.length <= index) {
150
+ return void 0;
151
+ }
152
+ const reversedModels = [...connectedModels].reverse();
153
+ model = reversedModels[index];
154
+ } else {
155
+ model = connectedModels;
156
+ }
147
157
  if (!(0, import_helpers.isChatInstance)(model) || !model.bindTools) {
148
158
  throw new import_n8n_workflow.NodeOperationError(
149
159
  ctx.getNode(),
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../nodes/agents/Agent/agents/ToolsAgent/common.ts"],"sourcesContent":["import type { BaseChatModel } from '@langchain/core/language_models/chat_models';\nimport { HumanMessage } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport { ChatPromptTemplate, type BaseMessagePromptTemplateLike } from '@langchain/core/prompts';\nimport type { AgentAction, AgentFinish } from 'langchain/agents';\nimport type { ToolsAgentAction } from 'langchain/dist/agents/tool_calling/output_parser';\nimport type { BaseChatMemory } from 'langchain/memory';\nimport { DynamicStructuredTool, type Tool } from 'langchain/tools';\nimport { BINARY_ENCODING, jsonParse, NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';\nimport type { IExecuteFunctions } from 'n8n-workflow';\nimport type { ZodObject } from 'zod';\nimport { z } from 'zod';\n\nimport { isChatInstance, getConnectedTools } from '@utils/helpers';\nimport { type N8nOutputParser } from '@utils/output_parsers/N8nOutputParser';\n/* -----------------------------------------------------------\n Output Parser Helper\n----------------------------------------------------------- */\n/**\n * Retrieve the output parser schema.\n * If the parser does not return a valid schema, default to a schema with a single text field.\n */\nexport function getOutputParserSchema(\n\toutputParser: N8nOutputParser,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n): ZodObject<any, any, any, any> {\n\tconst schema =\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t(outputParser.getSchema() as ZodObject<any, any, any, any>) ?? z.object({ text: z.string() });\n\treturn schema;\n}\n\n/* -----------------------------------------------------------\n Binary Data Helpers\n----------------------------------------------------------- */\n/**\n * Extracts binary image messages from the input data.\n * When operating in filesystem mode, the binary stream is first converted to a buffer.\n *\n * @param ctx - The execution context\n * @param itemIndex - The current item index\n * @returns A HumanMessage containing the binary image messages.\n */\nexport async function extractBinaryMessages(\n\tctx: IExecuteFunctions,\n\titemIndex: number,\n): Promise<HumanMessage> {\n\tconst binaryData = ctx.getInputData()?.[itemIndex]?.binary ?? {};\n\tconst binaryMessages = await Promise.all(\n\t\tObject.values(binaryData)\n\t\t\t.filter((data) => data.mimeType.startsWith('image/'))\n\t\t\t.map(async (data) => {\n\t\t\t\tlet binaryUrlString: string;\n\n\t\t\t\t// In filesystem mode we need to get binary stream by id before converting it to buffer\n\t\t\t\tif (data.id) {\n\t\t\t\t\tconst binaryBuffer = await ctx.helpers.binaryToBuffer(\n\t\t\t\t\t\tawait ctx.helpers.getBinaryStream(data.id),\n\t\t\t\t\t);\n\t\t\t\t\tbinaryUrlString = `data:${data.mimeType};base64,${Buffer.from(binaryBuffer).toString(\n\t\t\t\t\t\tBINARY_ENCODING,\n\t\t\t\t\t)}`;\n\t\t\t\t} else {\n\t\t\t\t\tbinaryUrlString = data.data.includes('base64')\n\t\t\t\t\t\t? data.data\n\t\t\t\t\t\t: `data:${data.mimeType};base64,${data.data}`;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\ttype: 'image_url',\n\t\t\t\t\timage_url: {\n\t\t\t\t\t\turl: binaryUrlString,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}),\n\t);\n\treturn new HumanMessage({\n\t\tcontent: [...binaryMessages],\n\t});\n}\n\n/* -----------------------------------------------------------\n Agent Output Format Helpers\n----------------------------------------------------------- */\n/**\n * Fixes empty content messages in agent steps.\n *\n * This function is necessary when using RunnableSequence.from in LangChain.\n * If a tool doesn't have any arguments, LangChain returns input: '' (empty string).\n * This can throw an error for some providers (like Anthropic) which expect the input to always be an object.\n * This function replaces empty string inputs with empty objects to prevent such errors.\n *\n * @param steps - The agent steps to fix\n * @returns The fixed agent steps\n */\nexport function fixEmptyContentMessage(\n\tsteps: AgentFinish | ToolsAgentAction[],\n): AgentFinish | ToolsAgentAction[] {\n\tif (!Array.isArray(steps)) return steps;\n\n\tsteps.forEach((step) => {\n\t\tif ('messageLog' in step && step.messageLog !== undefined) {\n\t\t\tif (Array.isArray(step.messageLog)) {\n\t\t\t\tstep.messageLog.forEach((message: BaseMessage) => {\n\t\t\t\t\tif ('content' in message && Array.isArray(message.content)) {\n\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(message.content as Array<{ input?: string | object }>).forEach((content) => {\n\t\t\t\t\t\t\tif (content.input === '') {\n\t\t\t\t\t\t\t\tcontent.input = {};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t});\n\n\treturn steps;\n}\n\n/**\n * Ensures consistent handling of outputs regardless of the model used,\n * providing a unified output format for further processing.\n *\n * This method is necessary to handle different output formats from various language models.\n * Specifically, it checks if the agent step is the final step (contains returnValues) and determines\n * if the output is a simple string (e.g., from OpenAI models) or an array of outputs (e.g., from Anthropic models).\n *\n * Examples:\n * 1. Anthropic model output:\n * ```json\n * {\n * \"output\": [\n * {\n * \"index\": 0,\n * \"type\": \"text\",\n * \"text\": \"The result of the calculation is approximately 1001.8166...\"\n * }\n * ]\n * }\n *```\n * 2. OpenAI model output:\n * ```json\n * {\n * \"output\": \"The result of the calculation is approximately 1001.82...\"\n * }\n * ```\n *\n * @param steps - The agent finish or agent action steps.\n * @returns The modified agent finish steps or the original steps.\n */\nexport function handleAgentFinishOutput(\n\tsteps: AgentFinish | AgentAction[],\n): AgentFinish | AgentAction[] {\n\ttype AgentMultiOutputFinish = AgentFinish & {\n\t\treturnValues: { output: Array<{ text: string; type: string; index: number }> };\n\t};\n\tconst agentFinishSteps = steps as AgentMultiOutputFinish | AgentFinish;\n\n\tif (agentFinishSteps.returnValues) {\n\t\tconst isMultiOutput = Array.isArray(agentFinishSteps.returnValues?.output);\n\t\tif (isMultiOutput) {\n\t\t\t// If all items in the multi-output array are of type 'text', merge them into a single string\n\t\t\tconst multiOutputSteps = agentFinishSteps.returnValues.output as Array<{\n\t\t\t\tindex: number;\n\t\t\t\ttype: string;\n\t\t\t\ttext: string;\n\t\t\t}>;\n\t\t\tconst isTextOnly = multiOutputSteps.every((output) => 'text' in output);\n\t\t\tif (isTextOnly) {\n\t\t\t\tagentFinishSteps.returnValues.output = multiOutputSteps\n\t\t\t\t\t.map((output) => output.text)\n\t\t\t\t\t.join('\\n')\n\t\t\t\t\t.trim();\n\t\t\t}\n\t\t\treturn agentFinishSteps;\n\t\t}\n\t}\n\n\treturn agentFinishSteps;\n}\n\n/**\n * Wraps the parsed output so that it can be stored in memory.\n * If memory is connected, the output is stringified.\n *\n * @param output - The parsed output object\n * @param memory - The connected memory (if any)\n * @returns The formatted output object\n */\nexport function handleParsedStepOutput(\n\toutput: Record<string, unknown>,\n\tmemory?: BaseChatMemory,\n): { returnValues: Record<string, unknown>; log: string } {\n\treturn {\n\t\treturnValues: memory ? { output: JSON.stringify(output) } : output,\n\t\tlog: 'Final response formatted',\n\t};\n}\n\n/**\n * Parses agent steps using the provided output parser.\n * If the agent used the 'format_final_json_response' tool, the output is parsed accordingly.\n *\n * @param steps - The agent finish or action steps\n * @param outputParser - The output parser (if defined)\n * @param memory - The connected memory (if any)\n * @returns The parsed steps with the final output\n */\nexport const getAgentStepsParser =\n\t(outputParser?: N8nOutputParser, memory?: BaseChatMemory) =>\n\tasync (steps: AgentFinish | AgentAction[]): Promise<AgentFinish | AgentAction[]> => {\n\t\t// Check if the steps contain the 'format_final_json_response' tool invocation.\n\t\tif (Array.isArray(steps)) {\n\t\t\tconst responseParserTool = steps.find((step) => step.tool === 'format_final_json_response');\n\t\t\tif (responseParserTool && outputParser) {\n\t\t\t\tconst toolInput = responseParserTool.toolInput;\n\t\t\t\t// Ensure the tool input is a string\n\t\t\t\tconst parserInput = toolInput instanceof Object ? JSON.stringify(toolInput) : toolInput;\n\t\t\t\tconst returnValues = (await outputParser.parse(parserInput)) as Record<string, unknown>;\n\t\t\t\treturn handleParsedStepOutput(returnValues, memory);\n\t\t\t}\n\t\t}\n\n\t\t// Otherwise, if the steps contain a returnValues field, try to parse them manually.\n\t\tif (outputParser && typeof steps === 'object' && (steps as AgentFinish).returnValues) {\n\t\t\tconst finalResponse = (steps as AgentFinish).returnValues;\n\t\t\tlet parserInput: string;\n\n\t\t\tif (finalResponse instanceof Object) {\n\t\t\t\tif ('output' in finalResponse) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// If the output is an object, we will try to parse it as JSON\n\t\t\t\t\t\t// this is because parser expects stringified JSON object like { \"output\": { .... } }\n\t\t\t\t\t\t// so we try to parse the output before wrapping it and then stringify it\n\t\t\t\t\t\tparserInput = JSON.stringify({ output: jsonParse(finalResponse.output) });\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t// Fallback to the raw output if parsing fails.\n\t\t\t\t\t\tparserInput = finalResponse.output;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// If the output is not an object, we will stringify it as it is\n\t\t\t\t\tparserInput = JSON.stringify(finalResponse);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tparserInput = finalResponse;\n\t\t\t}\n\n\t\t\tconst returnValues = (await outputParser.parse(parserInput)) as Record<string, unknown>;\n\t\t\treturn handleParsedStepOutput(returnValues, memory);\n\t\t}\n\n\t\treturn handleAgentFinishOutput(steps);\n\t};\n\n/* -----------------------------------------------------------\n Agent Setup Helpers\n----------------------------------------------------------- */\n/**\n * Retrieves the language model from the input connection.\n * Throws an error if the model is not a valid chat instance or does not support tools.\n *\n * @param ctx - The execution context\n * @returns The validated chat model\n */\nexport async function getChatModel(ctx: IExecuteFunctions): Promise<BaseChatModel> {\n\tconst model = await ctx.getInputConnectionData(NodeConnectionTypes.AiLanguageModel, 0);\n\tif (!isChatInstance(model) || !model.bindTools) {\n\t\tthrow new NodeOperationError(\n\t\t\tctx.getNode(),\n\t\t\t'Tools Agent requires Chat Model which supports Tools calling',\n\t\t);\n\t}\n\treturn model;\n}\n\n/**\n * Retrieves the memory instance from the input connection if it is connected\n *\n * @param ctx - The execution context\n * @returns The connected memory (if any)\n */\nexport async function getOptionalMemory(\n\tctx: IExecuteFunctions,\n): Promise<BaseChatMemory | undefined> {\n\treturn (await ctx.getInputConnectionData(NodeConnectionTypes.AiMemory, 0)) as\n\t\t| BaseChatMemory\n\t\t| undefined;\n}\n\n/**\n * Retrieves the connected tools and (if an output parser is defined)\n * appends a structured output parser tool.\n *\n * @param ctx - The execution context\n * @param outputParser - The optional output parser\n * @returns The array of connected tools\n */\nexport async function getTools(\n\tctx: IExecuteFunctions,\n\toutputParser?: N8nOutputParser,\n): Promise<Array<DynamicStructuredTool | Tool>> {\n\tconst tools = (await getConnectedTools(ctx, true, false)) as Array<DynamicStructuredTool | Tool>;\n\n\t// If an output parser is available, create a dynamic tool to validate the final output.\n\tif (outputParser) {\n\t\tconst schema = getOutputParserSchema(outputParser);\n\t\tconst structuredOutputParserTool = new DynamicStructuredTool({\n\t\t\tschema,\n\t\t\tname: 'format_final_json_response',\n\t\t\tdescription:\n\t\t\t\t'Use this tool to format your final response to the user in a structured JSON format. This tool validates your output against a schema to ensure it meets the required format. ONLY use this tool when you have completed all necessary reasoning and are ready to provide your final answer. Do not use this tool for intermediate steps or for asking questions. The output from this tool will be directly returned to the user.',\n\t\t\t// We do not use a function here because we intercept the output with the parser.\n\t\t\tfunc: async () => '',\n\t\t});\n\t\ttools.push(structuredOutputParserTool);\n\t}\n\treturn tools;\n}\n\n/**\n * Prepares the prompt messages for the agent.\n *\n * @param ctx - The execution context\n * @param itemIndex - The current item index\n * @param options - Options containing systemMessage and other parameters\n * @returns The array of prompt messages\n */\nexport async function prepareMessages(\n\tctx: IExecuteFunctions,\n\titemIndex: number,\n\toptions: {\n\t\tsystemMessage?: string;\n\t\tpassthroughBinaryImages?: boolean;\n\t\toutputParser?: N8nOutputParser;\n\t},\n): Promise<BaseMessagePromptTemplateLike[]> {\n\tconst useSystemMessage = options.systemMessage ?? ctx.getNode().typeVersion < 1.9;\n\n\tconst messages: BaseMessagePromptTemplateLike[] = [];\n\n\tif (useSystemMessage) {\n\t\tmessages.push([\n\t\t\t'system',\n\t\t\t`{system_message}${options.outputParser ? '\\n\\n{formatting_instructions}' : ''}`,\n\t\t]);\n\t} else if (options.outputParser) {\n\t\tmessages.push(['system', '{formatting_instructions}']);\n\t}\n\n\tmessages.push(['placeholder', '{chat_history}'], ['human', '{input}']);\n\n\t// If there is binary data and the node option permits it, add a binary message\n\tconst hasBinaryData = ctx.getInputData()?.[itemIndex]?.binary !== undefined;\n\tif (hasBinaryData && options.passthroughBinaryImages) {\n\t\tconst binaryMessage = await extractBinaryMessages(ctx, itemIndex);\n\t\tif (binaryMessage.content.length !== 0) {\n\t\t\tmessages.push(binaryMessage);\n\t\t} else {\n\t\t\tctx.logger.debug('Not attaching binary message, since its content was empty');\n\t\t}\n\t}\n\n\t// We add the agent scratchpad last, so that the agent will not run in loops\n\t// by adding binary messages between each interaction\n\tmessages.push(['placeholder', '{agent_scratchpad}']);\n\treturn messages;\n}\n\n/**\n * Creates the chat prompt from messages.\n *\n * @param messages - The messages array\n * @returns The ChatPromptTemplate instance\n */\nexport function preparePrompt(messages: BaseMessagePromptTemplateLike[]): ChatPromptTemplate {\n\treturn ChatPromptTemplate.fromMessages(messages);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAA6B;AAE7B,qBAAuE;AAIvE,mBAAiD;AACjD,0BAAoF;AAGpF,iBAAkB;AAElB,qBAAkD;AAS3C,SAAS,sBACf,cAEgC;AAChC,QAAM;AAAA;AAAA,IAEJ,aAAa,UAAU,KAAuC,aAAE,OAAO,EAAE,MAAM,aAAE,OAAO,EAAE,CAAC;AAAA;AAC7F,SAAO;AACR;AAaA,eAAsB,sBACrB,KACA,WACwB;AACxB,QAAM,aAAa,IAAI,aAAa,IAAI,SAAS,GAAG,UAAU,CAAC;AAC/D,QAAM,iBAAiB,MAAM,QAAQ;AAAA,IACpC,OAAO,OAAO,UAAU,EACtB,OAAO,CAAC,SAAS,KAAK,SAAS,WAAW,QAAQ,CAAC,EACnD,IAAI,OAAO,SAAS;AACpB,UAAI;AAGJ,UAAI,KAAK,IAAI;AACZ,cAAM,eAAe,MAAM,IAAI,QAAQ;AAAA,UACtC,MAAM,IAAI,QAAQ,gBAAgB,KAAK,EAAE;AAAA,QAC1C;AACA,0BAAkB,QAAQ,KAAK,QAAQ,WAAW,OAAO,KAAK,YAAY,EAAE;AAAA,UAC3E;AAAA,QACD,CAAC;AAAA,MACF,OAAO;AACN,0BAAkB,KAAK,KAAK,SAAS,QAAQ,IAC1C,KAAK,OACL,QAAQ,KAAK,QAAQ,WAAW,KAAK,IAAI;AAAA,MAC7C;AAEA,aAAO;AAAA,QACN,MAAM;AAAA,QACN,WAAW;AAAA,UACV,KAAK;AAAA,QACN;AAAA,MACD;AAAA,IACD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Ac;AACjB,UAAM,SAAS,sBAAsB,YAAY;AACjD,UAAM,6BAA6B,IAAI,mCAAsB;AAAA,MAC5D;AAAA,MACA,MAAM;AAAA,MACN,aACC;AAAA;AAAA,MAED,MAAM,YAAY;AAAA,IACnB,CAAC;AACD,UAAM,KAAK,0BAA0B;AAAA,EACtC;AACA,SAAO;AACR;AAUA,eAAsB,gBACrB,KACA,WACA,SAK2C;AAC3C,QAAM,mBAAmB,QAAQ,iBAAiB,IAAI,QAAQ,EAAE,cAAc;AAE9E,QAAM,WAA4C,CAAC;AAEnD,MAAI,kBAAkB;AACrB,aAAS,KAAK;AAAA,MACb;AAAA,MACA,mBAAmB,QAAQ,eAAe,kCAAkC,EAAE;AAAA,IAC/E,CAAC;AAAA,EACF,WAAW,QAAQ,cAAc;AAChC,aAAS,KAAK,CAAC,UAAU,2BAA2B,CAAC;AAAA,EACtD;AAEA,WAAS,KAAK,CAAC,eAAe,gBAAgB,GAAG,CAAC,SAAS,SAAS,CAAC;AAGrE,QAAM,gBAAgB,IAAI,aAAa,IAAI,SAAS,GAAG,WAAW;AAClE,MAAI,iBAAiB,QAAQ,yBAAyB;AACrD,UAAM,gBAAgB,MAAM,sBAAsB,KAAK,SAAS;AAChE,QAAI,cAAc,QAAQ,WAAW,GAAG;AACvC,eAAS,KAAK,aAAa;AAAA,IAC5B,OAAO;AACN,UAAI,OAAO,MAAM,2DAA2D;AAAA,IAC7E;AAAA,EACD;AAIA,WAAS,KAAK,CAAC,eAAe,oBAAoB,CAAC;AACnD,SAAO;AACR;AAQO,SAAS,cAAc,UAA+D;AAC5F,SAAO,kCAAmB,aAAa,QAAQ;AAChD;","names":[]}
1
+ {"version":3,"sources":["../../../../../../nodes/agents/Agent/agents/ToolsAgent/common.ts"],"sourcesContent":["import type { BaseChatModel } from '@langchain/core/language_models/chat_models';\nimport { HumanMessage } from '@langchain/core/messages';\nimport type { BaseMessage } from '@langchain/core/messages';\nimport { ChatPromptTemplate, type BaseMessagePromptTemplateLike } from '@langchain/core/prompts';\nimport type { AgentAction, AgentFinish } from 'langchain/agents';\nimport type { ToolsAgentAction } from 'langchain/dist/agents/tool_calling/output_parser';\nimport type { BaseChatMemory } from 'langchain/memory';\nimport { DynamicStructuredTool, type Tool } from 'langchain/tools';\nimport { BINARY_ENCODING, jsonParse, NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';\nimport type { IExecuteFunctions } from 'n8n-workflow';\nimport type { ZodObject } from 'zod';\nimport { z } from 'zod';\n\nimport { isChatInstance, getConnectedTools } from '@utils/helpers';\nimport { type N8nOutputParser } from '@utils/output_parsers/N8nOutputParser';\n/* -----------------------------------------------------------\n Output Parser Helper\n----------------------------------------------------------- */\n/**\n * Retrieve the output parser schema.\n * If the parser does not return a valid schema, default to a schema with a single text field.\n */\nexport function getOutputParserSchema(\n\toutputParser: N8nOutputParser,\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n): ZodObject<any, any, any, any> {\n\tconst schema =\n\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\t\t(outputParser.getSchema() as ZodObject<any, any, any, any>) ?? z.object({ text: z.string() });\n\treturn schema;\n}\n\n/* -----------------------------------------------------------\n Binary Data Helpers\n----------------------------------------------------------- */\n/**\n * Extracts binary image messages from the input data.\n * When operating in filesystem mode, the binary stream is first converted to a buffer.\n *\n * @param ctx - The execution context\n * @param itemIndex - The current item index\n * @returns A HumanMessage containing the binary image messages.\n */\nexport async function extractBinaryMessages(\n\tctx: IExecuteFunctions,\n\titemIndex: number,\n): Promise<HumanMessage> {\n\tconst binaryData = ctx.getInputData()?.[itemIndex]?.binary ?? {};\n\tconst binaryMessages = await Promise.all(\n\t\tObject.values(binaryData)\n\t\t\t.filter((data) => data.mimeType.startsWith('image/'))\n\t\t\t.map(async (data) => {\n\t\t\t\tlet binaryUrlString: string;\n\n\t\t\t\t// In filesystem mode we need to get binary stream by id before converting it to buffer\n\t\t\t\tif (data.id) {\n\t\t\t\t\tconst binaryBuffer = await ctx.helpers.binaryToBuffer(\n\t\t\t\t\t\tawait ctx.helpers.getBinaryStream(data.id),\n\t\t\t\t\t);\n\t\t\t\t\tbinaryUrlString = `data:${data.mimeType};base64,${Buffer.from(binaryBuffer).toString(\n\t\t\t\t\t\tBINARY_ENCODING,\n\t\t\t\t\t)}`;\n\t\t\t\t} else {\n\t\t\t\t\tbinaryUrlString = data.data.includes('base64')\n\t\t\t\t\t\t? data.data\n\t\t\t\t\t\t: `data:${data.mimeType};base64,${data.data}`;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\ttype: 'image_url',\n\t\t\t\t\timage_url: {\n\t\t\t\t\t\turl: binaryUrlString,\n\t\t\t\t\t},\n\t\t\t\t};\n\t\t\t}),\n\t);\n\treturn new HumanMessage({\n\t\tcontent: [...binaryMessages],\n\t});\n}\n\n/* -----------------------------------------------------------\n Agent Output Format Helpers\n----------------------------------------------------------- */\n/**\n * Fixes empty content messages in agent steps.\n *\n * This function is necessary when using RunnableSequence.from in LangChain.\n * If a tool doesn't have any arguments, LangChain returns input: '' (empty string).\n * This can throw an error for some providers (like Anthropic) which expect the input to always be an object.\n * This function replaces empty string inputs with empty objects to prevent such errors.\n *\n * @param steps - The agent steps to fix\n * @returns The fixed agent steps\n */\nexport function fixEmptyContentMessage(\n\tsteps: AgentFinish | ToolsAgentAction[],\n): AgentFinish | ToolsAgentAction[] {\n\tif (!Array.isArray(steps)) return steps;\n\n\tsteps.forEach((step) => {\n\t\tif ('messageLog' in step && step.messageLog !== undefined) {\n\t\t\tif (Array.isArray(step.messageLog)) {\n\t\t\t\tstep.messageLog.forEach((message: BaseMessage) => {\n\t\t\t\t\tif ('content' in message && Array.isArray(message.content)) {\n\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(message.content as Array<{ input?: string | object }>).forEach((content) => {\n\t\t\t\t\t\t\tif (content.input === '') {\n\t\t\t\t\t\t\t\tcontent.input = {};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\t});\n\n\treturn steps;\n}\n\n/**\n * Ensures consistent handling of outputs regardless of the model used,\n * providing a unified output format for further processing.\n *\n * This method is necessary to handle different output formats from various language models.\n * Specifically, it checks if the agent step is the final step (contains returnValues) and determines\n * if the output is a simple string (e.g., from OpenAI models) or an array of outputs (e.g., from Anthropic models).\n *\n * Examples:\n * 1. Anthropic model output:\n * ```json\n * {\n * \"output\": [\n * {\n * \"index\": 0,\n * \"type\": \"text\",\n * \"text\": \"The result of the calculation is approximately 1001.8166...\"\n * }\n * ]\n * }\n *```\n * 2. OpenAI model output:\n * ```json\n * {\n * \"output\": \"The result of the calculation is approximately 1001.82...\"\n * }\n * ```\n *\n * @param steps - The agent finish or agent action steps.\n * @returns The modified agent finish steps or the original steps.\n */\nexport function handleAgentFinishOutput(\n\tsteps: AgentFinish | AgentAction[],\n): AgentFinish | AgentAction[] {\n\ttype AgentMultiOutputFinish = AgentFinish & {\n\t\treturnValues: { output: Array<{ text: string; type: string; index: number }> };\n\t};\n\tconst agentFinishSteps = steps as AgentMultiOutputFinish | AgentFinish;\n\n\tif (agentFinishSteps.returnValues) {\n\t\tconst isMultiOutput = Array.isArray(agentFinishSteps.returnValues?.output);\n\t\tif (isMultiOutput) {\n\t\t\t// If all items in the multi-output array are of type 'text', merge them into a single string\n\t\t\tconst multiOutputSteps = agentFinishSteps.returnValues.output as Array<{\n\t\t\t\tindex: number;\n\t\t\t\ttype: string;\n\t\t\t\ttext: string;\n\t\t\t}>;\n\t\t\tconst isTextOnly = multiOutputSteps.every((output) => 'text' in output);\n\t\t\tif (isTextOnly) {\n\t\t\t\tagentFinishSteps.returnValues.output = multiOutputSteps\n\t\t\t\t\t.map((output) => output.text)\n\t\t\t\t\t.join('\\n')\n\t\t\t\t\t.trim();\n\t\t\t}\n\t\t\treturn agentFinishSteps;\n\t\t}\n\t}\n\n\treturn agentFinishSteps;\n}\n\n/**\n * Wraps the parsed output so that it can be stored in memory.\n * If memory is connected, the output is stringified.\n *\n * @param output - The parsed output object\n * @param memory - The connected memory (if any)\n * @returns The formatted output object\n */\nexport function handleParsedStepOutput(\n\toutput: Record<string, unknown>,\n\tmemory?: BaseChatMemory,\n): { returnValues: Record<string, unknown>; log: string } {\n\treturn {\n\t\treturnValues: memory ? { output: JSON.stringify(output) } : output,\n\t\tlog: 'Final response formatted',\n\t};\n}\n\n/**\n * Parses agent steps using the provided output parser.\n * If the agent used the 'format_final_json_response' tool, the output is parsed accordingly.\n *\n * @param steps - The agent finish or action steps\n * @param outputParser - The output parser (if defined)\n * @param memory - The connected memory (if any)\n * @returns The parsed steps with the final output\n */\nexport const getAgentStepsParser =\n\t(outputParser?: N8nOutputParser, memory?: BaseChatMemory) =>\n\tasync (steps: AgentFinish | AgentAction[]): Promise<AgentFinish | AgentAction[]> => {\n\t\t// Check if the steps contain the 'format_final_json_response' tool invocation.\n\t\tif (Array.isArray(steps)) {\n\t\t\tconst responseParserTool = steps.find((step) => step.tool === 'format_final_json_response');\n\t\t\tif (responseParserTool && outputParser) {\n\t\t\t\tconst toolInput = responseParserTool.toolInput;\n\t\t\t\t// Ensure the tool input is a string\n\t\t\t\tconst parserInput = toolInput instanceof Object ? JSON.stringify(toolInput) : toolInput;\n\t\t\t\tconst returnValues = (await outputParser.parse(parserInput)) as Record<string, unknown>;\n\t\t\t\treturn handleParsedStepOutput(returnValues, memory);\n\t\t\t}\n\t\t}\n\n\t\t// Otherwise, if the steps contain a returnValues field, try to parse them manually.\n\t\tif (outputParser && typeof steps === 'object' && (steps as AgentFinish).returnValues) {\n\t\t\tconst finalResponse = (steps as AgentFinish).returnValues;\n\t\t\tlet parserInput: string;\n\n\t\t\tif (finalResponse instanceof Object) {\n\t\t\t\tif ('output' in finalResponse) {\n\t\t\t\t\ttry {\n\t\t\t\t\t\t// If the output is an object, we will try to parse it as JSON\n\t\t\t\t\t\t// this is because parser expects stringified JSON object like { \"output\": { .... } }\n\t\t\t\t\t\t// so we try to parse the output before wrapping it and then stringify it\n\t\t\t\t\t\tparserInput = JSON.stringify({ output: jsonParse(finalResponse.output) });\n\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t// Fallback to the raw output if parsing fails.\n\t\t\t\t\t\tparserInput = finalResponse.output;\n\t\t\t\t\t}\n\t\t\t\t} else {\n\t\t\t\t\t// If the output is not an object, we will stringify it as it is\n\t\t\t\t\tparserInput = JSON.stringify(finalResponse);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tparserInput = finalResponse;\n\t\t\t}\n\n\t\t\tconst returnValues = (await outputParser.parse(parserInput)) as Record<string, unknown>;\n\t\t\treturn handleParsedStepOutput(returnValues, memory);\n\t\t}\n\n\t\treturn handleAgentFinishOutput(steps);\n\t};\n\n/* -----------------------------------------------------------\n Agent Setup Helpers\n----------------------------------------------------------- */\n/**\n * Retrieves the language model from the input connection.\n * Throws an error if the model is not a valid chat instance or does not support tools.\n *\n * @param ctx - The execution context\n * @returns The validated chat model\n */\nexport async function getChatModel(\n\tctx: IExecuteFunctions,\n\tindex: number = 0,\n): Promise<BaseChatModel | undefined> {\n\tconst connectedModels = await ctx.getInputConnectionData(NodeConnectionTypes.AiLanguageModel, 0);\n\n\tlet model;\n\n\tif (Array.isArray(connectedModels) && index !== undefined) {\n\t\tif (connectedModels.length <= index) {\n\t\t\treturn undefined;\n\t\t}\n\t\t// We get the models in reversed order from the workflow so we need to reverse them to match the right index\n\t\tconst reversedModels = [...connectedModels].reverse();\n\t\tmodel = reversedModels[index] as BaseChatModel;\n\t} else {\n\t\tmodel = connectedModels as BaseChatModel;\n\t}\n\n\tif (!isChatInstance(model) || !model.bindTools) {\n\t\tthrow new NodeOperationError(\n\t\t\tctx.getNode(),\n\t\t\t'Tools Agent requires Chat Model which supports Tools calling',\n\t\t);\n\t}\n\treturn model;\n}\n\n/**\n * Retrieves the memory instance from the input connection if it is connected\n *\n * @param ctx - The execution context\n * @returns The connected memory (if any)\n */\nexport async function getOptionalMemory(\n\tctx: IExecuteFunctions,\n): Promise<BaseChatMemory | undefined> {\n\treturn (await ctx.getInputConnectionData(NodeConnectionTypes.AiMemory, 0)) as\n\t\t| BaseChatMemory\n\t\t| undefined;\n}\n\n/**\n * Retrieves the connected tools and (if an output parser is defined)\n * appends a structured output parser tool.\n *\n * @param ctx - The execution context\n * @param outputParser - The optional output parser\n * @returns The array of connected tools\n */\nexport async function getTools(\n\tctx: IExecuteFunctions,\n\toutputParser?: N8nOutputParser,\n): Promise<Array<DynamicStructuredTool | Tool>> {\n\tconst tools = (await getConnectedTools(ctx, true, false)) as Array<DynamicStructuredTool | Tool>;\n\n\t// If an output parser is available, create a dynamic tool to validate the final output.\n\tif (outputParser) {\n\t\tconst schema = getOutputParserSchema(outputParser);\n\t\tconst structuredOutputParserTool = new DynamicStructuredTool({\n\t\t\tschema,\n\t\t\tname: 'format_final_json_response',\n\t\t\tdescription:\n\t\t\t\t'Use this tool to format your final response to the user in a structured JSON format. This tool validates your output against a schema to ensure it meets the required format. ONLY use this tool when you have completed all necessary reasoning and are ready to provide your final answer. Do not use this tool for intermediate steps or for asking questions. The output from this tool will be directly returned to the user.',\n\t\t\t// We do not use a function here because we intercept the output with the parser.\n\t\t\tfunc: async () => '',\n\t\t});\n\t\ttools.push(structuredOutputParserTool);\n\t}\n\treturn tools;\n}\n\n/**\n * Prepares the prompt messages for the agent.\n *\n * @param ctx - The execution context\n * @param itemIndex - The current item index\n * @param options - Options containing systemMessage and other parameters\n * @returns The array of prompt messages\n */\nexport async function prepareMessages(\n\tctx: IExecuteFunctions,\n\titemIndex: number,\n\toptions: {\n\t\tsystemMessage?: string;\n\t\tpassthroughBinaryImages?: boolean;\n\t\toutputParser?: N8nOutputParser;\n\t},\n): Promise<BaseMessagePromptTemplateLike[]> {\n\tconst useSystemMessage = options.systemMessage ?? ctx.getNode().typeVersion < 1.9;\n\n\tconst messages: BaseMessagePromptTemplateLike[] = [];\n\n\tif (useSystemMessage) {\n\t\tmessages.push([\n\t\t\t'system',\n\t\t\t`{system_message}${options.outputParser ? '\\n\\n{formatting_instructions}' : ''}`,\n\t\t]);\n\t} else if (options.outputParser) {\n\t\tmessages.push(['system', '{formatting_instructions}']);\n\t}\n\n\tmessages.push(['placeholder', '{chat_history}'], ['human', '{input}']);\n\n\t// If there is binary data and the node option permits it, add a binary message\n\tconst hasBinaryData = ctx.getInputData()?.[itemIndex]?.binary !== undefined;\n\tif (hasBinaryData && options.passthroughBinaryImages) {\n\t\tconst binaryMessage = await extractBinaryMessages(ctx, itemIndex);\n\t\tif (binaryMessage.content.length !== 0) {\n\t\t\tmessages.push(binaryMessage);\n\t\t} else {\n\t\t\tctx.logger.debug('Not attaching binary message, since its content was empty');\n\t\t}\n\t}\n\n\t// We add the agent scratchpad last, so that the agent will not run in loops\n\t// by adding binary messages between each interaction\n\tmessages.push(['placeholder', '{agent_scratchpad}']);\n\treturn messages;\n}\n\n/**\n * Creates the chat prompt from messages.\n *\n * @param messages - The messages array\n * @returns The ChatPromptTemplate instance\n */\nexport function preparePrompt(messages: BaseMessagePromptTemplateLike[]): ChatPromptTemplate {\n\treturn ChatPromptTemplate.fromMessages(messages);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,sBAA6B;AAE7B,qBAAuE;AAIvE,mBAAiD;AACjD,0BAAoF;AAGpF,iBAAkB;AAElB,qBAAkD;AAS3C,SAAS,sBACf,cAEgC;AAChC,QAAM;AAAA;AAAA,IAEJ,aAAa,UAAU,KAAuC,aAAE,OAAO,EAAE,MAAM,aAAE,OAAO,EAAE,CAAC;AAAA;AAC7F,SAAO;AACR;AAaA,eAAsB,sBACrB,KACA,WACwB;AACxB,QAAM,aAAa,IAAI,aAAa,IAAI,SAAS,GAAG,UAAU,CAAC;AAC/D,QAAM,iBAAiB,MAAM,QAAQ;AAAA,IACpC,OAA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EAAE;AAAA,IAC/E,CAAC;AAAA,EACF,WAAW,QAAQ,cAAc;AAChC,aAAS,KAAK,CAAC,UAAU,2BAA2B,CAAC;AAAA,EACtD;AAEA,WAAS,KAAK,CAAC,eAAe,gBAAgB,GAAG,CAAC,SAAS,SAAS,CAAC;AAGrE,QAAM,gBAAgB,IAAI,aAAa,IAAI,SAAS,GAAG,WAAW;AAClE,MAAI,iBAAiB,QAAQ,yBAAyB;AACrD,UAAM,gBAAgB,MAAM,sBAAsB,KAAK,SAAS;AAChE,QAAI,cAAc,QAAQ,WAAW,GAAG;AACvC,eAAS,KAAK,aAAa;AAAA,IAC5B,OAAO;AACN,UAAI,OAAO,MAAM,2DAA2D;AAAA,IAC7E;AAAA,EACD;AAIA,WAAS,KAAK,CAAC,eAAe,oBAAoB,CAAC;AACnD,SAAO;AACR;AAQO,SAAS,cAAc,UAA+D;AAC5F,SAAO,kCAAmB,aAAa,QAAQ;AAChD;","names":[]}
@@ -64,10 +64,17 @@ async function executeSimpleChain({
64
64
  context,
65
65
  llm,
66
66
  query,
67
- prompt
67
+ prompt,
68
+ fallbackLlm
68
69
  }) {
69
70
  const outputParser = getOutputParserForLLM(llm);
70
- const chain = prompt.pipe(llm).pipe(outputParser).withConfig((0, import_tracing.getTracingConfig)(context));
71
+ let model;
72
+ if (fallbackLlm) {
73
+ model = llm.withFallbacks([fallbackLlm]);
74
+ } else {
75
+ model = llm;
76
+ }
77
+ const chain = prompt.pipe(model).pipe(outputParser).withConfig((0, import_tracing.getTracingConfig)(context));
71
78
  const response = await chain.invoke({
72
79
  query,
73
80
  signal: context.getExecutionCancelSignal()
@@ -80,7 +87,8 @@ async function executeChain({
80
87
  query,
81
88
  llm,
82
89
  outputParser,
83
- messages
90
+ messages,
91
+ fallbackLlm
84
92
  }) {
85
93
  if (!outputParser) {
86
94
  const promptTemplate = await (0, import_promptUtils.createPromptTemplate)({
@@ -94,7 +102,8 @@ async function executeChain({
94
102
  context,
95
103
  llm,
96
104
  query,
97
- prompt: promptTemplate
105
+ prompt: promptTemplate,
106
+ fallbackLlm
98
107
  });
99
108
  }
100
109
  const formatInstructions = outputParser.getFormatInstructions();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../nodes/chains/ChainLLM/methods/chainExecutor.ts"],"sourcesContent":["import type { BaseLanguageModel } from '@langchain/core/language_models/base';\nimport type { BaseLLMOutputParser } from '@langchain/core/output_parsers';\nimport { JsonOutputParser, StringOutputParser } from '@langchain/core/output_parsers';\nimport type { ChatPromptTemplate, PromptTemplate } from '@langchain/core/prompts';\nimport type { IExecuteFunctions } from 'n8n-workflow';\n\nimport { getTracingConfig } from '@utils/tracing';\n\nimport { createPromptTemplate } from './promptUtils';\nimport type { ChainExecutionParams } from './types';\n\nexport class NaiveJsonOutputParser<\n\tT extends Record<string, any> = Record<string, any>,\n> extends JsonOutputParser<T> {\n\tasync parse(text: string): Promise<T> {\n\t\t// First try direct JSON parsing\n\t\ttry {\n\t\t\tconst directParsed = JSON.parse(text);\n\t\t\treturn directParsed as T;\n\t\t} catch (e) {\n\t\t\t// If fails, fall back to JsonOutputParser parser\n\t\t\treturn await super.parse(text);\n\t\t}\n\t}\n}\n\n/**\n * Type guard to check if the LLM has a modelKwargs property(OpenAI)\n */\nexport function isModelWithResponseFormat(\n\tllm: BaseLanguageModel,\n): llm is BaseLanguageModel & { modelKwargs: { response_format: { type: string } } } {\n\treturn (\n\t\t'modelKwargs' in llm &&\n\t\t!!llm.modelKwargs &&\n\t\ttypeof llm.modelKwargs === 'object' &&\n\t\t'response_format' in llm.modelKwargs\n\t);\n}\n\nexport function isModelInThinkingMode(\n\tllm: BaseLanguageModel,\n): llm is BaseLanguageModel & { lc_kwargs: { invocationKwargs: { thinking: { type: string } } } } {\n\treturn (\n\t\t'lc_kwargs' in llm &&\n\t\t'invocationKwargs' in llm.lc_kwargs &&\n\t\ttypeof llm.lc_kwargs.invocationKwargs === 'object' &&\n\t\t'thinking' in llm.lc_kwargs.invocationKwargs &&\n\t\tllm.lc_kwargs.invocationKwargs.thinking.type === 'enabled'\n\t);\n}\n\n/**\n * Type guard to check if the LLM has a format property(Ollama)\n */\nexport function isModelWithFormat(\n\tllm: BaseLanguageModel,\n): llm is BaseLanguageModel & { format: string } {\n\treturn 'format' in llm && typeof llm.format !== 'undefined';\n}\n\n/**\n * Determines if an LLM is configured to output JSON and returns the appropriate output parser\n */\nexport function getOutputParserForLLM(\n\tllm: BaseLanguageModel,\n): BaseLLMOutputParser<string | Record<string, unknown>> {\n\tif (isModelWithResponseFormat(llm) && llm.modelKwargs?.response_format?.type === 'json_object') {\n\t\treturn new NaiveJsonOutputParser();\n\t}\n\n\tif (isModelWithFormat(llm) && llm.format === 'json') {\n\t\treturn new NaiveJsonOutputParser();\n\t}\n\n\tif (isModelInThinkingMode(llm)) {\n\t\treturn new NaiveJsonOutputParser();\n\t}\n\n\treturn new StringOutputParser();\n}\n\n/**\n * Creates a simple chain for LLMs without output parsers\n */\nasync function executeSimpleChain({\n\tcontext,\n\tllm,\n\tquery,\n\tprompt,\n}: {\n\tcontext: IExecuteFunctions;\n\tllm: BaseLanguageModel;\n\tquery: string;\n\tprompt: ChatPromptTemplate | PromptTemplate;\n}) {\n\tconst outputParser = getOutputParserForLLM(llm);\n\n\tconst chain = prompt.pipe(llm).pipe(outputParser).withConfig(getTracingConfig(context));\n\n\t// Execute the chain\n\tconst response = await chain.invoke({\n\t\tquery,\n\t\tsignal: context.getExecutionCancelSignal(),\n\t});\n\n\t// Ensure response is always returned as an array\n\treturn [response];\n}\n\n/**\n * Creates and executes an LLM chain with the given prompt and optional output parsers\n */\nexport async function executeChain({\n\tcontext,\n\titemIndex,\n\tquery,\n\tllm,\n\toutputParser,\n\tmessages,\n}: ChainExecutionParams): Promise<unknown[]> {\n\t// If no output parsers provided, use a simple chain with basic prompt template\n\tif (!outputParser) {\n\t\tconst promptTemplate = await createPromptTemplate({\n\t\t\tcontext,\n\t\t\titemIndex,\n\t\t\tllm,\n\t\t\tmessages,\n\t\t\tquery,\n\t\t});\n\n\t\treturn await executeSimpleChain({\n\t\t\tcontext,\n\t\t\tllm,\n\t\t\tquery,\n\t\t\tprompt: promptTemplate,\n\t\t});\n\t}\n\n\tconst formatInstructions = outputParser.getFormatInstructions();\n\n\t// Create a prompt template with format instructions\n\tconst promptWithInstructions = await createPromptTemplate({\n\t\tcontext,\n\t\titemIndex,\n\t\tllm,\n\t\tmessages,\n\t\tformatInstructions,\n\t\tquery,\n\t});\n\n\tconst chain = promptWithInstructions\n\t\t.pipe(llm)\n\t\t.pipe(outputParser)\n\t\t.withConfig(getTracingConfig(context));\n\tconst response = await chain.invoke({ query }, { signal: context.getExecutionCancelSignal() });\n\n\t// Ensure response is always returned as an array\n\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\treturn Array.isArray(response) ? response : [response];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,4BAAqD;AAIrD,qBAAiC;AAEjC,yBAAqC;AAG9B,MAAM,8BAEH,uCAAoB;AAAA,EAC7B,MAAM,MAAM,MAA0B;AAErC,QAAI;AACH,YAAM,eAAe,KAAK,MAAM,IAAI;AACpC,aAAO;AAAA,IACR,SAAS,GAAG;AAEX,aAAO,MAAM,MAAM,MAAM,IAAI;AAAA,IAC9B;AAAA,EACD;AACD;AAKO,SAAS,0BACf,KACoF;AACpF,SACC,iBAAiB,OACjB,CAAC,CAAC,IAAI,eACN,OAAO,IAAI,gBAAgB,YAC3B,qBAAqB,IAAI;AAE3B;AAEO,SAAS,sBACf,KACiG;AACjG,SACC,eAAe,OACf,sBAAsB,IAAI,aAC1B,OAAO,IAAI,UAAU,qBAAqB,YAC1C,cAAc,IAAI,UAAU,oBAC5B,IAAI,UAAU,iBAAiB,SAAS,SAAS;AAEnD;AAKO,SAAS,kBACf,KACgD;AAChD,SAAO,YAAY,OAAO,OAAO,IAAI,WAAW;AACjD;AAKO,SAAS,sBAC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,MAAM,MAAM,OAAO,EAAE,MAAM,GAAG,EAAE,QAAQ,QAAQ,yBAAyB,EAAE,CAAC;AAI7F,SAAO,MAAM,QAAQ,QAAQ,IAAI,WAAW,CAAC,QAAQ;AACtD;","names":[]}
1
+ {"version":3,"sources":["../../../../../nodes/chains/ChainLLM/methods/chainExecutor.ts"],"sourcesContent":["import type { BaseLanguageModel } from '@langchain/core/language_models/base';\nimport type { BaseLLMOutputParser } from '@langchain/core/output_parsers';\nimport { JsonOutputParser, StringOutputParser } from '@langchain/core/output_parsers';\nimport type { ChatPromptTemplate, PromptTemplate } from '@langchain/core/prompts';\nimport type { IExecuteFunctions } from 'n8n-workflow';\n\nimport { getTracingConfig } from '@utils/tracing';\n\nimport { createPromptTemplate } from './promptUtils';\nimport type { ChainExecutionParams } from './types';\n\nexport class NaiveJsonOutputParser<\n\tT extends Record<string, any> = Record<string, any>,\n> extends JsonOutputParser<T> {\n\tasync parse(text: string): Promise<T> {\n\t\t// First try direct JSON parsing\n\t\ttry {\n\t\t\tconst directParsed = JSON.parse(text);\n\t\t\treturn directParsed as T;\n\t\t} catch (e) {\n\t\t\t// If fails, fall back to JsonOutputParser parser\n\t\t\treturn await super.parse(text);\n\t\t}\n\t}\n}\n\n/**\n * Type guard to check if the LLM has a modelKwargs property(OpenAI)\n */\nexport function isModelWithResponseFormat(\n\tllm: BaseLanguageModel,\n): llm is BaseLanguageModel & { modelKwargs: { response_format: { type: string } } } {\n\treturn (\n\t\t'modelKwargs' in llm &&\n\t\t!!llm.modelKwargs &&\n\t\ttypeof llm.modelKwargs === 'object' &&\n\t\t'response_format' in llm.modelKwargs\n\t);\n}\n\nexport function isModelInThinkingMode(\n\tllm: BaseLanguageModel,\n): llm is BaseLanguageModel & { lc_kwargs: { invocationKwargs: { thinking: { type: string } } } } {\n\treturn (\n\t\t'lc_kwargs' in llm &&\n\t\t'invocationKwargs' in llm.lc_kwargs &&\n\t\ttypeof llm.lc_kwargs.invocationKwargs === 'object' &&\n\t\t'thinking' in llm.lc_kwargs.invocationKwargs &&\n\t\tllm.lc_kwargs.invocationKwargs.thinking.type === 'enabled'\n\t);\n}\n\n/**\n * Type guard to check if the LLM has a format property(Ollama)\n */\nexport function isModelWithFormat(\n\tllm: BaseLanguageModel,\n): llm is BaseLanguageModel & { format: string } {\n\treturn 'format' in llm && typeof llm.format !== 'undefined';\n}\n\n/**\n * Determines if an LLM is configured to output JSON and returns the appropriate output parser\n */\nexport function getOutputParserForLLM(\n\tllm: BaseLanguageModel,\n): BaseLLMOutputParser<string | Record<string, unknown>> {\n\tif (isModelWithResponseFormat(llm) && llm.modelKwargs?.response_format?.type === 'json_object') {\n\t\treturn new NaiveJsonOutputParser();\n\t}\n\n\tif (isModelWithFormat(llm) && llm.format === 'json') {\n\t\treturn new NaiveJsonOutputParser();\n\t}\n\n\tif (isModelInThinkingMode(llm)) {\n\t\treturn new NaiveJsonOutputParser();\n\t}\n\n\treturn new StringOutputParser();\n}\n\n/**\n * Creates a simple chain for LLMs without output parsers\n */\nasync function executeSimpleChain({\n\tcontext,\n\tllm,\n\tquery,\n\tprompt,\n\tfallbackLlm,\n}: {\n\tcontext: IExecuteFunctions;\n\tllm: BaseLanguageModel;\n\tquery: string;\n\tprompt: ChatPromptTemplate | PromptTemplate;\n\tfallbackLlm?: BaseLanguageModel | null;\n}) {\n\tconst outputParser = getOutputParserForLLM(llm);\n\tlet model;\n\n\tif (fallbackLlm) {\n\t\tmodel = llm.withFallbacks([fallbackLlm]);\n\t} else {\n\t\tmodel = llm;\n\t}\n\n\tconst chain = prompt.pipe(model).pipe(outputParser).withConfig(getTracingConfig(context));\n\n\t// Execute the chain\n\tconst response = await chain.invoke({\n\t\tquery,\n\t\tsignal: context.getExecutionCancelSignal(),\n\t});\n\n\t// Ensure response is always returned as an array\n\treturn [response];\n}\n\n/**\n * Creates and executes an LLM chain with the given prompt and optional output parsers\n */\nexport async function executeChain({\n\tcontext,\n\titemIndex,\n\tquery,\n\tllm,\n\toutputParser,\n\tmessages,\n\tfallbackLlm,\n}: ChainExecutionParams): Promise<unknown[]> {\n\t// If no output parsers provided, use a simple chain with basic prompt template\n\tif (!outputParser) {\n\t\tconst promptTemplate = await createPromptTemplate({\n\t\t\tcontext,\n\t\t\titemIndex,\n\t\t\tllm,\n\t\t\tmessages,\n\t\t\tquery,\n\t\t});\n\n\t\treturn await executeSimpleChain({\n\t\t\tcontext,\n\t\t\tllm,\n\t\t\tquery,\n\t\t\tprompt: promptTemplate,\n\t\t\tfallbackLlm,\n\t\t});\n\t}\n\n\tconst formatInstructions = outputParser.getFormatInstructions();\n\n\t// Create a prompt template with format instructions\n\tconst promptWithInstructions = await createPromptTemplate({\n\t\tcontext,\n\t\titemIndex,\n\t\tllm,\n\t\tmessages,\n\t\tformatInstructions,\n\t\tquery,\n\t});\n\n\tconst chain = promptWithInstructions\n\t\t.pipe(llm)\n\t\t.pipe(outputParser)\n\t\t.withConfig(getTracingConfig(context));\n\tconst response = await chain.invoke({ query }, { signal: context.getExecutionCancelSignal() });\n\n\t// Ensure response is always returned as an array\n\t// eslint-disable-next-line @typescript-eslint/no-unsafe-return\n\treturn Array.isArray(response) ? response : [response];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,4BAAqD;AAIrD,qBAAiC;AAEjC,yBAAqC;AAG9B,MAAM,8BAEH,uCAAoB;AAAA,EAC7B,MAAM,MAAM,MAA0B;AAErC,QAAI;AACH,YAAM,eAAe,KAAK,MAAM,IAAI;AACpC,aAAO;AAAA,IACR,SAAS,GAAG;AAEX,aAAO,MAAM,MAAM,MAAM,IAAI;AAAA,IAC9B;AAAA,EACD;AACD;AAKO,SAAS,0BACf,KACoF;AACpF,SACC,iBAAiB,OACjB,CAAC,CAAC,IAAI,eACN,OAAO,IAAI,gBAAgB,YAC3B,qBAAqB,IAAI;AAE3B;AAEO,SAAS,sBACf,KACiG;AACjG,SACC,eAAe,OAC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