langchain 1.0.0-alpha.6 → 1.0.0-alpha.7

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 (185) hide show
  1. package/dist/agents/ReactAgent.cjs.map +1 -1
  2. package/dist/agents/ReactAgent.d.cts +1 -2
  3. package/dist/agents/ReactAgent.d.cts.map +1 -1
  4. package/dist/agents/ReactAgent.d.ts +1 -2
  5. package/dist/agents/ReactAgent.d.ts.map +1 -1
  6. package/dist/agents/ReactAgent.js.map +1 -1
  7. package/dist/agents/annotation.cjs.map +1 -1
  8. package/dist/agents/annotation.d.cts +12 -5
  9. package/dist/agents/annotation.d.cts.map +1 -1
  10. package/dist/agents/annotation.d.ts +12 -5
  11. package/dist/agents/annotation.d.ts.map +1 -1
  12. package/dist/agents/annotation.js.map +1 -1
  13. package/dist/agents/createAgent.cjs.map +1 -1
  14. package/dist/agents/createAgent.js.map +1 -1
  15. package/dist/agents/index.cjs.map +1 -1
  16. package/dist/agents/index.d.cts +3 -3
  17. package/dist/agents/index.d.cts.map +1 -1
  18. package/dist/agents/index.d.ts +3 -3
  19. package/dist/agents/index.d.ts.map +1 -1
  20. package/dist/agents/index.js.map +1 -1
  21. package/dist/agents/interrupt.d.cts +14 -1
  22. package/dist/agents/interrupt.d.cts.map +1 -1
  23. package/dist/agents/interrupt.d.ts +14 -1
  24. package/dist/agents/interrupt.d.ts.map +1 -1
  25. package/dist/agents/middlewareAgent/ReactAgent.cjs +262 -55
  26. package/dist/agents/middlewareAgent/ReactAgent.cjs.map +1 -1
  27. package/dist/agents/middlewareAgent/ReactAgent.d.cts +97 -10
  28. package/dist/agents/middlewareAgent/ReactAgent.d.cts.map +1 -1
  29. package/dist/agents/middlewareAgent/ReactAgent.d.ts +97 -10
  30. package/dist/agents/middlewareAgent/ReactAgent.d.ts.map +1 -1
  31. package/dist/agents/middlewareAgent/ReactAgent.js +264 -57
  32. package/dist/agents/middlewareAgent/ReactAgent.js.map +1 -1
  33. package/dist/agents/middlewareAgent/annotation.cjs +10 -4
  34. package/dist/agents/middlewareAgent/annotation.cjs.map +1 -1
  35. package/dist/agents/middlewareAgent/annotation.js +10 -4
  36. package/dist/agents/middlewareAgent/annotation.js.map +1 -1
  37. package/dist/agents/middlewareAgent/constants.d.cts +5 -0
  38. package/dist/agents/middlewareAgent/constants.d.cts.map +1 -0
  39. package/dist/agents/middlewareAgent/constants.d.ts +5 -0
  40. package/dist/agents/middlewareAgent/constants.d.ts.map +1 -0
  41. package/dist/agents/middlewareAgent/index.cjs.map +1 -1
  42. package/dist/agents/middlewareAgent/index.js.map +1 -1
  43. package/dist/agents/middlewareAgent/middleware/bigTool.cjs +162 -0
  44. package/dist/agents/middlewareAgent/middleware/bigTool.cjs.map +1 -0
  45. package/dist/agents/middlewareAgent/middleware/bigTool.d.cts +113 -0
  46. package/dist/agents/middlewareAgent/middleware/bigTool.d.cts.map +1 -0
  47. package/dist/agents/middlewareAgent/middleware/bigTool.d.ts +113 -0
  48. package/dist/agents/middlewareAgent/middleware/bigTool.d.ts.map +1 -0
  49. package/dist/agents/middlewareAgent/middleware/bigTool.js +161 -0
  50. package/dist/agents/middlewareAgent/middleware/bigTool.js.map +1 -0
  51. package/dist/agents/middlewareAgent/middleware/dynamicSystemPrompt.cjs +58 -0
  52. package/dist/agents/middlewareAgent/middleware/dynamicSystemPrompt.cjs.map +1 -0
  53. package/dist/agents/middlewareAgent/middleware/dynamicSystemPrompt.d.cts +46 -0
  54. package/dist/agents/middlewareAgent/middleware/dynamicSystemPrompt.d.cts.map +1 -0
  55. package/dist/agents/middlewareAgent/middleware/dynamicSystemPrompt.d.ts +46 -0
  56. package/dist/agents/middlewareAgent/middleware/dynamicSystemPrompt.d.ts.map +1 -0
  57. package/dist/agents/middlewareAgent/middleware/dynamicSystemPrompt.js +58 -0
  58. package/dist/agents/middlewareAgent/middleware/dynamicSystemPrompt.js.map +1 -0
  59. package/dist/agents/middlewareAgent/middleware/hitl.cjs +160 -84
  60. package/dist/agents/middlewareAgent/middleware/hitl.cjs.map +1 -1
  61. package/dist/agents/middlewareAgent/middleware/hitl.d.cts +271 -51
  62. package/dist/agents/middlewareAgent/middleware/hitl.d.cts.map +1 -1
  63. package/dist/agents/middlewareAgent/middleware/hitl.d.ts +271 -51
  64. package/dist/agents/middlewareAgent/middleware/hitl.d.ts.map +1 -1
  65. package/dist/agents/middlewareAgent/middleware/hitl.js +161 -85
  66. package/dist/agents/middlewareAgent/middleware/hitl.js.map +1 -1
  67. package/dist/agents/middlewareAgent/middleware/index.cjs +6 -0
  68. package/dist/agents/middlewareAgent/middleware/index.cjs.map +1 -1
  69. package/dist/agents/middlewareAgent/middleware/index.d.cts +6 -4
  70. package/dist/agents/middlewareAgent/middleware/index.d.ts +6 -4
  71. package/dist/agents/middlewareAgent/middleware/index.js +5 -1
  72. package/dist/agents/middlewareAgent/middleware/index.js.map +1 -1
  73. package/dist/agents/middlewareAgent/middleware/promptCaching.cjs +24 -8
  74. package/dist/agents/middlewareAgent/middleware/promptCaching.cjs.map +1 -1
  75. package/dist/agents/middlewareAgent/middleware/promptCaching.d.cts +51 -6
  76. package/dist/agents/middlewareAgent/middleware/promptCaching.d.cts.map +1 -1
  77. package/dist/agents/middlewareAgent/middleware/promptCaching.d.ts +51 -6
  78. package/dist/agents/middlewareAgent/middleware/promptCaching.d.ts.map +1 -1
  79. package/dist/agents/middlewareAgent/middleware/promptCaching.js +24 -8
  80. package/dist/agents/middlewareAgent/middleware/promptCaching.js.map +1 -1
  81. package/dist/agents/middlewareAgent/middleware/summarization.cjs +24 -12
  82. package/dist/agents/middlewareAgent/middleware/summarization.cjs.map +1 -1
  83. package/dist/agents/middlewareAgent/middleware/summarization.d.cts +5 -3
  84. package/dist/agents/middlewareAgent/middleware/summarization.d.cts.map +1 -1
  85. package/dist/agents/middlewareAgent/middleware/summarization.d.ts +11 -9
  86. package/dist/agents/middlewareAgent/middleware/summarization.d.ts.map +1 -1
  87. package/dist/agents/middlewareAgent/middleware/summarization.js +25 -13
  88. package/dist/agents/middlewareAgent/middleware/summarization.js.map +1 -1
  89. package/dist/agents/middlewareAgent/middleware.cjs +8 -5
  90. package/dist/agents/middlewareAgent/middleware.cjs.map +1 -1
  91. package/dist/agents/middlewareAgent/middleware.d.cts +67 -7
  92. package/dist/agents/middlewareAgent/middleware.d.cts.map +1 -1
  93. package/dist/agents/middlewareAgent/middleware.d.ts +67 -7
  94. package/dist/agents/middlewareAgent/middleware.d.ts.map +1 -1
  95. package/dist/agents/middlewareAgent/middleware.js +8 -5
  96. package/dist/agents/middlewareAgent/middleware.js.map +1 -1
  97. package/dist/agents/middlewareAgent/nodes/AfterModalNode.cjs +2 -2
  98. package/dist/agents/middlewareAgent/nodes/AfterModalNode.cjs.map +1 -1
  99. package/dist/agents/middlewareAgent/nodes/AfterModalNode.js +2 -2
  100. package/dist/agents/middlewareAgent/nodes/AfterModalNode.js.map +1 -1
  101. package/dist/agents/middlewareAgent/nodes/AgentNode.cjs +117 -68
  102. package/dist/agents/middlewareAgent/nodes/AgentNode.cjs.map +1 -1
  103. package/dist/agents/middlewareAgent/nodes/AgentNode.js +121 -72
  104. package/dist/agents/middlewareAgent/nodes/AgentNode.js.map +1 -1
  105. package/dist/agents/middlewareAgent/nodes/BeforeModalNode.cjs +2 -2
  106. package/dist/agents/middlewareAgent/nodes/BeforeModalNode.cjs.map +1 -1
  107. package/dist/agents/middlewareAgent/nodes/BeforeModalNode.js +2 -2
  108. package/dist/agents/middlewareAgent/nodes/BeforeModalNode.js.map +1 -1
  109. package/dist/agents/middlewareAgent/nodes/middleware.cjs +42 -17
  110. package/dist/agents/middlewareAgent/nodes/middleware.cjs.map +1 -1
  111. package/dist/agents/middlewareAgent/nodes/middleware.js +42 -18
  112. package/dist/agents/middlewareAgent/nodes/middleware.js.map +1 -1
  113. package/dist/agents/middlewareAgent/nodes/utils.cjs +25 -11
  114. package/dist/agents/middlewareAgent/nodes/utils.cjs.map +1 -1
  115. package/dist/agents/middlewareAgent/nodes/utils.js +26 -12
  116. package/dist/agents/middlewareAgent/nodes/utils.js.map +1 -1
  117. package/dist/agents/middlewareAgent/types.d.cts +153 -66
  118. package/dist/agents/middlewareAgent/types.d.cts.map +1 -1
  119. package/dist/agents/middlewareAgent/types.d.ts +153 -66
  120. package/dist/agents/middlewareAgent/types.d.ts.map +1 -1
  121. package/dist/agents/model.cjs +13 -0
  122. package/dist/agents/model.cjs.map +1 -0
  123. package/dist/agents/model.js +11 -0
  124. package/dist/agents/model.js.map +1 -0
  125. package/dist/agents/nodes/AgentNode.cjs +56 -28
  126. package/dist/agents/nodes/AgentNode.cjs.map +1 -1
  127. package/dist/agents/nodes/AgentNode.js +57 -29
  128. package/dist/agents/nodes/AgentNode.js.map +1 -1
  129. package/dist/agents/nodes/ToolNode.cjs +1 -1
  130. package/dist/agents/nodes/ToolNode.cjs.map +1 -1
  131. package/dist/agents/nodes/ToolNode.d.cts +1 -1
  132. package/dist/agents/nodes/ToolNode.js +1 -1
  133. package/dist/agents/nodes/ToolNode.js.map +1 -1
  134. package/dist/agents/responses.cjs +52 -10
  135. package/dist/agents/responses.cjs.map +1 -1
  136. package/dist/agents/responses.d.cts +12 -20
  137. package/dist/agents/responses.d.cts.map +1 -1
  138. package/dist/agents/responses.d.ts +12 -20
  139. package/dist/agents/responses.d.ts.map +1 -1
  140. package/dist/agents/responses.js +52 -11
  141. package/dist/agents/responses.js.map +1 -1
  142. package/dist/agents/types.d.cts +2 -3
  143. package/dist/agents/types.d.cts.map +1 -1
  144. package/dist/agents/types.d.ts +2 -3
  145. package/dist/agents/types.d.ts.map +1 -1
  146. package/dist/agents/utils.cjs +5 -23
  147. package/dist/agents/utils.cjs.map +1 -1
  148. package/dist/agents/utils.js +2 -19
  149. package/dist/agents/utils.js.map +1 -1
  150. package/dist/chains/query_constructor/index.d.cts +1 -1
  151. package/dist/chains/summarization/load.d.ts +2 -2
  152. package/dist/chains/summarization/load.d.ts.map +1 -1
  153. package/dist/embeddings/cache_backed.d.ts +1 -1
  154. package/dist/evaluation/comparison/pairwise.d.cts.map +1 -1
  155. package/dist/evaluation/comparison/pairwise.d.ts.map +1 -1
  156. package/dist/evaluation/criteria/criteria.d.cts.map +1 -1
  157. package/dist/evaluation/criteria/criteria.d.ts.map +1 -1
  158. package/dist/index.cjs +38 -6
  159. package/dist/index.cjs.map +1 -1
  160. package/dist/index.d.cts +11 -4
  161. package/dist/index.d.ts +11 -4
  162. package/dist/index.js +18 -4
  163. package/dist/index.js.map +1 -1
  164. package/dist/langchain-core/dist/messages/base.d.cts.map +1 -1
  165. package/dist/langchain-core/dist/messages/content/index.d.cts +20 -1
  166. package/dist/langchain-core/dist/messages/content/index.d.cts.map +1 -1
  167. package/dist/langchain-core/dist/messages/content/tools.d.cts +67 -6
  168. package/dist/langchain-core/dist/messages/content/tools.d.cts.map +1 -1
  169. package/dist/langchain-core/dist/messages/message.d.cts +6 -6
  170. package/dist/langchain-core/dist/messages/message.d.cts.map +1 -1
  171. package/dist/langchain-core/dist/utils/types/index.d.cts.map +1 -1
  172. package/dist/libs/langchain-core/dist/messages/base.d.ts.map +1 -1
  173. package/dist/libs/langchain-core/dist/messages/content/index.d.ts +20 -1
  174. package/dist/libs/langchain-core/dist/messages/content/index.d.ts.map +1 -1
  175. package/dist/libs/langchain-core/dist/messages/content/tools.d.ts +67 -6
  176. package/dist/libs/langchain-core/dist/messages/content/tools.d.ts.map +1 -1
  177. package/dist/libs/langchain-core/dist/messages/message.d.ts +6 -6
  178. package/dist/libs/langchain-core/dist/messages/message.d.ts.map +1 -1
  179. package/dist/libs/langchain-core/dist/utils/types/index.d.ts.map +1 -1
  180. package/dist/load/import_map.cjs +1 -1
  181. package/dist/load/import_map.js +1 -1
  182. package/dist/output_parsers/structured.d.cts +1 -1
  183. package/dist/tools/fs.d.cts +1 -1
  184. package/dist/tools/retriever.d.cts +1 -1
  185. package/package.json +7 -7
@@ -13,20 +13,46 @@ const __langchain_core_utils_types = require_rolldown_runtime.__toESM(require("@
13
13
  //#region src/agents/nodes/AgentNode.ts
14
14
  var AgentNode = class extends require_RunnableCallable.RunnableCallable {
15
15
  #options;
16
- #structuredToolInfo = {};
17
16
  constructor(options) {
18
17
  super({
19
18
  name: options.name ?? "model",
20
19
  func: (input, config) => this.#run(input, config)
21
20
  });
22
21
  this.#options = options;
22
+ }
23
+ /**
24
+ * Returns response format primtivies based on given model and response format provided by the user.
25
+ *
26
+ * If the the user selects a tool output:
27
+ * - return a record of tools to extract structured output from the model's response
28
+ *
29
+ * if the the user selects a native schema output or if the model supports JSON schema output:
30
+ * - return a provider strategy to extract structured output from the model's response
31
+ *
32
+ * @param model - The model to get the response format for.
33
+ * @returns The response format.
34
+ */
35
+ #getResponseFormat(model) {
36
+ if (!this.#options.responseFormat) return void 0;
37
+ const strategies = require_responses.transformResponseFormat(this.#options.responseFormat, void 0, model);
38
+ /**
39
+ * we either define a list of provider strategies or a list of tool strategies
40
+ */
41
+ const isProviderStrategy = strategies.every((format) => format instanceof require_responses.ProviderStrategy);
23
42
  /**
24
43
  * Populate a list of structured tool info.
25
44
  */
26
- this.#structuredToolInfo = require_responses.transformResponseFormat(this.#options.responseFormat).filter((format) => format instanceof require_responses.ToolStrategy).reduce((acc, format) => {
27
- acc[format.name] = format;
28
- return acc;
29
- }, {});
45
+ if (!isProviderStrategy) return {
46
+ type: "tool",
47
+ tools: strategies.filter((format) => format instanceof require_responses.ToolStrategy).reduce((acc, format) => {
48
+ acc[format.name] = format;
49
+ return acc;
50
+ }, {})
51
+ };
52
+ return {
53
+ type: "native",
54
+ strategy: strategies[0]
55
+ };
30
56
  }
31
57
  async #run(state, config) {
32
58
  /**
@@ -85,7 +111,8 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
85
111
  * Check if the LLM already has bound tools and throw if it does.
86
112
  */
87
113
  require_utils.validateLLMHasNoBoundTools(model);
88
- const modelWithTools = await this.#bindTools(model);
114
+ const structuredResponseFormat = this.#getResponseFormat(model);
115
+ const modelWithTools = await this.#bindTools(model, structuredResponseFormat);
89
116
  const modelInput = this.#getModelInputState(state);
90
117
  const signal = require_utils$1.mergeAbortSignals(this.#options.signal, config.signal);
91
118
  const invokeConfig = {
@@ -97,15 +124,16 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
97
124
  * if the user requests a native schema output, try to parse the response
98
125
  * and return the structured response if it is valid
99
126
  */
100
- if (this.#options.responseFormat instanceof require_responses.ProviderStrategy) {
101
- const structuredResponse = this.#options.responseFormat.parse(response);
127
+ if (structuredResponseFormat?.type === "native") {
128
+ const structuredResponse = structuredResponseFormat.strategy.parse(response);
102
129
  if (structuredResponse) return {
103
130
  structuredResponse,
104
131
  messages: [response]
105
132
  };
133
+ return response;
106
134
  }
107
- if (!response.tool_calls) return response;
108
- const toolCalls = response.tool_calls.filter((call) => call.name in this.#structuredToolInfo);
135
+ if (!structuredResponseFormat || !response.tool_calls) return response;
136
+ const toolCalls = response.tool_calls.filter((call) => call.name in structuredResponseFormat.tools);
109
137
  /**
110
138
  * if there were not structured tool calls, we can return the response
111
139
  */
@@ -114,10 +142,10 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
114
142
  * if there were multiple structured tool calls, we should throw an error as this
115
143
  * scenario is not defined/supported.
116
144
  */
117
- if (toolCalls.length > 1) return this.#handleMultipleStructuredOutputs(response, toolCalls);
118
- const toolStrategy = this.#structuredToolInfo[toolCalls[0].name];
145
+ if (toolCalls.length > 1) return this.#handleMultipleStructuredOutputs(response, toolCalls, structuredResponseFormat);
146
+ const toolStrategy = structuredResponseFormat.tools[toolCalls[0].name];
119
147
  const toolMessageContent = toolStrategy?.options?.toolMessageContent;
120
- return this.#handleSingleStructuredOutput(response, toolCalls[0], toolMessageContent ?? options.lastMessage);
148
+ return this.#handleSingleStructuredOutput(response, toolCalls[0], structuredResponseFormat, toolMessageContent ?? options.lastMessage);
121
149
  }
122
150
  /**
123
151
  * If the model returns multiple structured outputs, we need to handle it.
@@ -125,21 +153,21 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
125
153
  * @param toolCalls - The tool calls that were made
126
154
  * @returns The response from the model
127
155
  */
128
- #handleMultipleStructuredOutputs(response, toolCalls) {
156
+ #handleMultipleStructuredOutputs(response, toolCalls, structuredResponseFormat) {
129
157
  /**
130
158
  * the following should never happen, let's throw an error if it does
131
159
  */
132
160
  if (this.#options.responseFormat instanceof require_responses.ProviderStrategy) throw new Error("Multiple structured outputs should not apply to native structured output responses");
133
161
  const multipleStructuredOutputsError = new require_errors.MultipleStructuredOutputsError(toolCalls.map((call) => call.name));
134
- return this.#handleToolStrategyError(multipleStructuredOutputsError, response, toolCalls[0]);
162
+ return this.#handleToolStrategyError(multipleStructuredOutputsError, response, toolCalls[0], structuredResponseFormat);
135
163
  }
136
164
  /**
137
165
  * If the model returns a single structured output, we need to handle it.
138
166
  * @param toolCall - The tool call that was made
139
167
  * @returns The structured response and a message to the LLM if needed
140
168
  */
141
- #handleSingleStructuredOutput(response, toolCall, lastMessage) {
142
- const tool = this.#structuredToolInfo[toolCall.name];
169
+ #handleSingleStructuredOutput(response, toolCall, structuredResponseFormat, lastMessage) {
170
+ const tool = structuredResponseFormat.tools[toolCall.name];
143
171
  try {
144
172
  const structuredResponse = tool.parse(toolCall.args);
145
173
  return {
@@ -147,10 +175,10 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
147
175
  messages: [response, new __langchain_core_messages.AIMessage(lastMessage ?? `Returning structured response: ${JSON.stringify(structuredResponse)}`)]
148
176
  };
149
177
  } catch (error) {
150
- return this.#handleToolStrategyError(error, response, toolCall);
178
+ return this.#handleToolStrategyError(error, response, toolCall, structuredResponseFormat);
151
179
  }
152
180
  }
153
- async #handleToolStrategyError(error, response, toolCall) {
181
+ async #handleToolStrategyError(error, response, toolCall, structuredResponseFormat) {
154
182
  /**
155
183
  * Using the `errorHandler` option of the first `ToolStrategy` entry is sufficient here.
156
184
  * There is technically only one `ToolStrategy` entry in `structuredToolInfo` if the user
@@ -158,7 +186,7 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
158
186
  * schema objects, these will be transformed into multiple `ToolStrategy` entries but all
159
187
  * with the same `handleError` option.
160
188
  */
161
- const errorHandler = Object.values(this.#structuredToolInfo).at(0)?.options?.handleError;
189
+ const errorHandler = Object.values(structuredResponseFormat.tools).at(0)?.options?.handleError;
162
190
  const toolCallId = toolCall.id;
163
191
  if (!toolCallId) throw new Error("Tool call ID is required to handle tool output errors. Please provide a tool call ID.");
164
192
  /**
@@ -216,9 +244,9 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
216
244
  ...rest
217
245
  };
218
246
  }
219
- async #bindTools(model) {
247
+ async #bindTools(model, structuredResponseFormat) {
220
248
  const options = {};
221
- const structuredTools = Object.values(this.#structuredToolInfo);
249
+ const structuredTools = structuredResponseFormat?.type === "tool" ? Object.values(structuredResponseFormat.tools) : [];
222
250
  const allTools = this.#options.toolClasses.concat(...structuredTools.map((toolStrategy) => toolStrategy.tool));
223
251
  /**
224
252
  * If there are structured tools, we need to set the tool choice to "any"
@@ -228,15 +256,15 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
228
256
  /**
229
257
  * check if the user requests a native schema output
230
258
  */
231
- if (this.#options.responseFormat instanceof require_responses.ProviderStrategy) {
259
+ if (structuredResponseFormat?.type === "native") {
232
260
  /**
233
261
  * if the model does not support JSON schema output, throw an error
234
262
  */
235
- if (!require_utils.hasSupportForJsonSchemaOutput(model)) throw new Error("Model does not support native structured output responses. Please use a model that supports native structured output responses or use a tool output.");
263
+ if (!require_responses.hasSupportForJsonSchemaOutput(model)) throw new Error("Model does not support native structured output responses. Please use a model that supports native structured output responses or use a tool output.");
236
264
  const jsonSchemaParams = {
237
- name: this.#options.responseFormat.schema?.name ?? "extract",
238
- description: (0, __langchain_core_utils_types.getSchemaDescription)(this.#options.responseFormat.schema),
239
- schema: this.#options.responseFormat.schema,
265
+ name: structuredResponseFormat.strategy.schema?.name ?? "extract",
266
+ description: (0, __langchain_core_utils_types.getSchemaDescription)(structuredResponseFormat.strategy.schema),
267
+ schema: structuredResponseFormat.strategy.schema,
240
268
  strict: true
241
269
  };
242
270
  Object.assign(options, {
@@ -246,7 +274,7 @@ var AgentNode = class extends require_RunnableCallable.RunnableCallable {
246
274
  },
247
275
  ls_structured_output_format: {
248
276
  kwargs: { method: "json_schema" },
249
- schema: this.#options.responseFormat.schema
277
+ schema: structuredResponseFormat.strategy.schema
250
278
  },
251
279
  strict: true
252
280
  });
@@ -1 +1 @@
1
- {"version":3,"file":"AgentNode.cjs","names":["RunnableCallable","options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >","#run","#options","#structuredToolInfo","transformResponseFormat","ToolStrategy","state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"]","config: RunnableConfig","ToolMessage","#invokeModel","Command","#areMoreStepsNeeded","AIMessage","initChatModel","options: {\n lastMessage?: string;\n }","#deriveModel","validateLLMHasNoBoundTools","#bindTools","#getModelInputState","mergeAbortSignals","ProviderStrategy","#handleMultipleStructuredOutputs","#handleSingleStructuredOutput","response: AIMessage","toolCalls: ToolCall[]","MultipleStructuredOutputsError","#handleToolStrategyError","toolCall: ToolCall","lastMessage?: string","error: ToolStrategyError","response: BaseMessage","hasToolCalls","model: LanguageModelLike","options: Partial<BaseChatModelCallOptions>","hasSupportForJsonSchemaOutput","bindTools","getPromptRunnable","withAgentName"],"sources":["../../../src/agents/nodes/AgentNode.ts"],"sourcesContent":["/* eslint-disable no-instanceof/no-instanceof */\nimport { Runnable, RunnableConfig } from \"@langchain/core/runnables\";\nimport { BaseMessage, AIMessage, ToolMessage } from \"@langchain/core/messages\";\nimport { Command } from \"@langchain/langgraph\";\nimport { type LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport { type BaseChatModelCallOptions } from \"@langchain/core/language_models/chat_models\";\nimport {\n InteropZodObject,\n getSchemaDescription,\n} from \"@langchain/core/utils/types\";\nimport type { ToolCall } from \"@langchain/core/messages/tool\";\n\nimport { initChatModel } from \"../../chat_models/universal.js\";\nimport { MultipleStructuredOutputsError } from \"../errors.js\";\nimport { RunnableCallable } from \"../RunnableCallable.js\";\nimport { PreHookAnnotation, AnyAnnotationRoot } from \"../annotation.js\";\nimport { mergeAbortSignals } from \"./utils.js\";\nimport {\n bindTools,\n getPromptRunnable,\n validateLLMHasNoBoundTools,\n hasToolCalls,\n hasSupportForJsonSchemaOutput,\n} from \"../utils.js\";\nimport {\n InternalAgentState,\n ClientTool,\n ServerTool,\n CreateAgentParams,\n} from \"../types.js\";\nimport { withAgentName } from \"../withAgentName.js\";\nimport {\n ToolStrategy,\n ProviderStrategy,\n transformResponseFormat,\n ToolStrategyError,\n} from \"../responses.js\";\n\ntype ResponseHandlerResult<StructuredResponseFormat> =\n | {\n structuredResponse: StructuredResponseFormat;\n messages: BaseMessage[];\n }\n | Promise<Command>;\n\nexport interface AgentNodeOptions<\n StateSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,\n StructuredResponseFormat extends Record<string, unknown> = Record<\n string,\n unknown\n >,\n ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot\n> extends Pick<\n CreateAgentParams<StateSchema, StructuredResponseFormat, ContextSchema>,\n \"llm\" | \"model\" | \"prompt\" | \"includeAgentName\" | \"name\" | \"responseFormat\"\n > {\n toolClasses: (ClientTool | ServerTool)[];\n shouldReturnDirect: Set<string>;\n signal?: AbortSignal;\n}\n\nexport class AgentNode<\n StateSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,\n StructuredResponseFormat extends Record<string, unknown> = Record<\n string,\n unknown\n >,\n ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot\n> extends RunnableCallable<\n InternalAgentState<StructuredResponseFormat> & PreHookAnnotation[\"State\"],\n { messages: BaseMessage[] } | { structuredResponse: StructuredResponseFormat }\n> {\n #options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >;\n\n #structuredToolInfo: Record<string, ToolStrategy> = {};\n\n constructor(\n options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >\n ) {\n super({\n name: options.name ?? \"model\",\n func: (input, config) =>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.#run(input, config as RunnableConfig) as any,\n });\n\n this.#options = options;\n\n /**\n * Populate a list of structured tool info.\n */\n this.#structuredToolInfo = (\n transformResponseFormat(this.#options.responseFormat).filter(\n (format) => format instanceof ToolStrategy\n ) as ToolStrategy[]\n ).reduce((acc, format) => {\n acc[format.name] = format;\n return acc;\n }, {} as Record<string, ToolStrategy>);\n }\n\n async #run(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig\n ) {\n /**\n * Check if we just executed a returnDirect tool\n * If so, we should generate structured response (if needed) and stop\n */\n const lastMessage = state.messages[state.messages.length - 1];\n if (\n ToolMessage.isInstance(lastMessage) &&\n lastMessage.name &&\n this.#options.shouldReturnDirect.has(lastMessage.name)\n ) {\n /**\n * return directly without invoking the model again\n */\n return { messages: [] };\n }\n\n const response = await this.#invokeModel(state, config);\n\n /**\n * if we were able to generate a structured response, return it\n */\n if (\"structuredResponse\" in response) {\n return {\n messages: [...state.messages, ...(response.messages || [])],\n structuredResponse: response.structuredResponse,\n };\n }\n\n /**\n * if we need to direct the agent to the model, return the update\n */\n if (response instanceof Command) {\n return response;\n }\n\n response.name = this.name;\n response.lc_kwargs.name = this.name;\n\n if (this.#areMoreStepsNeeded(state, response)) {\n return {\n messages: [\n new AIMessage({\n content: \"Sorry, need more steps to process this request.\",\n name: this.name,\n id: response.id,\n }),\n ],\n };\n }\n\n return { messages: [response] };\n }\n\n /**\n * Derive the model from the options.\n * @param state - The state of the agent.\n * @param config - The config of the agent.\n * @returns The model.\n */\n #deriveModel(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig\n ) {\n if (this.#options.model) {\n if (typeof this.#options.model === \"string\") {\n return initChatModel(this.#options.model);\n }\n\n throw new Error(\"`model` option must be a string.\");\n }\n\n const model = this.#options.llm;\n\n /**\n * If the model is a function, call it to get the model.\n */\n if (typeof model === \"function\") {\n return model(state, config);\n }\n\n if (model) {\n return model;\n }\n\n throw new Error(\n \"No model option was provided, either via `model` or via `llm` option.\"\n );\n }\n\n async #invokeModel(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig,\n options: {\n lastMessage?: string;\n } = {}\n ): Promise<AIMessage | ResponseHandlerResult<StructuredResponseFormat>> {\n const model = await this.#deriveModel(state, config);\n\n /**\n * Check if the LLM already has bound tools and throw if it does.\n */\n validateLLMHasNoBoundTools(model);\n\n const modelWithTools = await this.#bindTools(model);\n const modelInput = this.#getModelInputState(state);\n const signal = mergeAbortSignals(this.#options.signal, config.signal);\n const invokeConfig = {\n ...config,\n signal,\n };\n\n const response = (await modelWithTools.invoke(\n modelInput,\n invokeConfig\n )) as AIMessage;\n\n /**\n * if the user requests a native schema output, try to parse the response\n * and return the structured response if it is valid\n */\n if (this.#options.responseFormat instanceof ProviderStrategy) {\n const structuredResponse = this.#options.responseFormat.parse(response);\n if (structuredResponse) {\n return { structuredResponse, messages: [response] };\n }\n }\n\n if (!response.tool_calls) {\n return response;\n }\n\n const toolCalls = response.tool_calls.filter(\n (call) => call.name in this.#structuredToolInfo\n );\n\n /**\n * if there were not structured tool calls, we can return the response\n */\n if (toolCalls.length === 0) {\n return response;\n }\n\n /**\n * if there were multiple structured tool calls, we should throw an error as this\n * scenario is not defined/supported.\n */\n if (toolCalls.length > 1) {\n return this.#handleMultipleStructuredOutputs(response, toolCalls);\n }\n\n const toolStrategy = this.#structuredToolInfo[toolCalls[0].name];\n const toolMessageContent = toolStrategy?.options?.toolMessageContent;\n return this.#handleSingleStructuredOutput(\n response,\n toolCalls[0],\n toolMessageContent ?? options.lastMessage\n );\n }\n\n /**\n * If the model returns multiple structured outputs, we need to handle it.\n * @param response - The response from the model\n * @param toolCalls - The tool calls that were made\n * @returns The response from the model\n */\n #handleMultipleStructuredOutputs(\n response: AIMessage,\n toolCalls: ToolCall[]\n ): Promise<Command> {\n /**\n * the following should never happen, let's throw an error if it does\n */\n if (this.#options.responseFormat instanceof ProviderStrategy) {\n throw new Error(\n \"Multiple structured outputs should not apply to native structured output responses\"\n );\n }\n\n const multipleStructuredOutputsError = new MultipleStructuredOutputsError(\n toolCalls.map((call) => call.name)\n );\n\n return this.#handleToolStrategyError(\n multipleStructuredOutputsError,\n response,\n toolCalls[0]\n );\n }\n\n /**\n * If the model returns a single structured output, we need to handle it.\n * @param toolCall - The tool call that was made\n * @returns The structured response and a message to the LLM if needed\n */\n #handleSingleStructuredOutput(\n response: AIMessage,\n toolCall: ToolCall,\n lastMessage?: string\n ): ResponseHandlerResult<StructuredResponseFormat> {\n const tool = this.#structuredToolInfo[toolCall.name];\n\n try {\n const structuredResponse = tool.parse(\n toolCall.args\n ) as StructuredResponseFormat;\n\n return {\n structuredResponse,\n messages: [\n response,\n new AIMessage(\n lastMessage ??\n `Returning structured response: ${JSON.stringify(\n structuredResponse\n )}`\n ),\n ],\n };\n } catch (error) {\n return this.#handleToolStrategyError(\n error as ToolStrategyError,\n response,\n toolCall\n );\n }\n }\n\n async #handleToolStrategyError(\n error: ToolStrategyError,\n response: AIMessage,\n toolCall: ToolCall\n ): Promise<Command> {\n /**\n * Using the `errorHandler` option of the first `ToolStrategy` entry is sufficient here.\n * There is technically only one `ToolStrategy` entry in `structuredToolInfo` if the user\n * uses `toolStrategy` to define the response format. If the user applies a list of json\n * schema objects, these will be transformed into multiple `ToolStrategy` entries but all\n * with the same `handleError` option.\n */\n const errorHandler = Object.values(this.#structuredToolInfo).at(0)?.options\n ?.handleError;\n\n const toolCallId = toolCall.id;\n if (!toolCallId) {\n throw new Error(\n \"Tool call ID is required to handle tool output errors. Please provide a tool call ID.\"\n );\n }\n\n /**\n * retry if:\n */\n if (\n /**\n * if the user has provided `true` as the `errorHandler` option, return a new AIMessage\n * with the error message and retry the tool call.\n */\n (typeof errorHandler === \"boolean\" && errorHandler) ||\n /**\n * if `errorHandler` is an array and contains MultipleStructuredOutputsError\n */\n (Array.isArray(errorHandler) &&\n errorHandler.some((h) => h instanceof MultipleStructuredOutputsError))\n ) {\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content: error.message,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * if `errorHandler` is a string, retry the tool call with given string\n */\n if (typeof errorHandler === \"string\") {\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content: errorHandler,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * if `errorHandler` is a function, retry the tool call with the function\n */\n if (typeof errorHandler === \"function\") {\n const content = await errorHandler(error);\n if (typeof content !== \"string\") {\n throw new Error(\"Error handler must return a string.\");\n }\n\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * throw otherwise, e.g. if `errorHandler` is not defined or set to `false`\n */\n throw error;\n }\n\n #areMoreStepsNeeded(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n response: BaseMessage\n ): boolean {\n const allToolsReturnDirect =\n AIMessage.isInstance(response) &&\n response.tool_calls?.every((call) =>\n this.#options.shouldReturnDirect.has(call.name)\n );\n const remainingSteps =\n \"remainingSteps\" in state ? (state.remainingSteps as number) : undefined;\n return Boolean(\n remainingSteps &&\n ((remainingSteps < 1 && allToolsReturnDirect) ||\n (remainingSteps < 2 && hasToolCalls(state.messages)))\n );\n }\n\n #getModelInputState(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"]\n ): Omit<InternalAgentState<StructuredResponseFormat>, \"llmInputMessages\"> {\n const { messages, llmInputMessages, ...rest } = state;\n if (llmInputMessages && llmInputMessages.length > 0) {\n return { messages: llmInputMessages, ...rest } as Omit<\n InternalAgentState<StructuredResponseFormat>,\n \"llmInputMessages\"\n >;\n }\n return { messages, ...rest } as Omit<\n InternalAgentState<StructuredResponseFormat>,\n \"llmInputMessages\"\n >;\n }\n\n async #bindTools(model: LanguageModelLike): Promise<Runnable> {\n const options: Partial<BaseChatModelCallOptions> = {};\n const structuredTools = Object.values(this.#structuredToolInfo);\n const allTools = this.#options.toolClasses.concat(\n ...structuredTools.map((toolStrategy) => toolStrategy.tool)\n );\n\n /**\n * If there are structured tools, we need to set the tool choice to \"any\"\n * so that the model can choose to use a structured tool or not.\n */\n const toolChoice = structuredTools.length > 0 ? \"any\" : undefined;\n\n /**\n * check if the user requests a native schema output\n */\n if (this.#options.responseFormat instanceof ProviderStrategy) {\n /**\n * if the model does not support JSON schema output, throw an error\n */\n if (!hasSupportForJsonSchemaOutput(model)) {\n throw new Error(\n \"Model does not support native structured output responses. Please use a model that supports native structured output responses or use a tool output.\"\n );\n }\n\n const jsonSchemaParams = {\n name: this.#options.responseFormat.schema?.name ?? \"extract\",\n description: getSchemaDescription(this.#options.responseFormat.schema),\n schema: this.#options.responseFormat.schema,\n strict: true,\n };\n\n Object.assign(options, {\n response_format: {\n type: \"json_schema\",\n json_schema: jsonSchemaParams,\n },\n ls_structured_output_format: {\n kwargs: { method: \"json_schema\" },\n schema: this.#options.responseFormat.schema,\n },\n strict: true,\n });\n }\n\n /**\n * Bind tools to the model if they are not already bound.\n */\n const modelWithTools = await bindTools(model, allTools, {\n ...options,\n tool_choice: toolChoice,\n });\n\n /**\n * Create a model runnable with the prompt and agent name\n */\n const modelRunnable = getPromptRunnable(this.#options.prompt).pipe(\n this.#options.includeAgentName === \"inline\"\n ? withAgentName(modelWithTools, this.#options.includeAgentName)\n : modelWithTools\n );\n\n return modelRunnable;\n }\n}\n"],"mappings":";;;;;;;;;;;;;AA6DA,IAAa,YAAb,cAOUA,0CAGR;CACA;CAMA,sBAAoD,CAAE;CAEtD,YACEC,S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AS,OAAO,CAAC,KAC5D,KAAKA,SAAS,qBAAqB,WAC/BmC,oCAAc,gBAAgB,KAAKnC,SAAS,iBAAiB,GAC7D,eACL;AAED,SAAO;CACR;AACF"}
1
+ {"version":3,"file":"AgentNode.cjs","names":["RunnableCallable","options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >","#run","#options","model: string | LanguageModelLike","transformResponseFormat","ProviderStrategy","ToolStrategy","state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"]","config: RunnableConfig","ToolMessage","#invokeModel","Command","#areMoreStepsNeeded","AIMessage","initChatModel","options: {\n lastMessage?: string;\n }","#deriveModel","validateLLMHasNoBoundTools","#getResponseFormat","#bindTools","#getModelInputState","mergeAbortSignals","#handleMultipleStructuredOutputs","#handleSingleStructuredOutput","response: AIMessage","toolCalls: ToolCall[]","structuredResponseFormat: ToolResponseFormat","MultipleStructuredOutputsError","#handleToolStrategyError","toolCall: ToolCall","lastMessage?: string","error: ToolStrategyError","response: BaseMessage","hasToolCalls","model: LanguageModelLike","structuredResponseFormat: ResponseFormat | undefined","options: Partial<BaseChatModelCallOptions>","hasSupportForJsonSchemaOutput","bindTools","getPromptRunnable","withAgentName"],"sources":["../../../src/agents/nodes/AgentNode.ts"],"sourcesContent":["/* eslint-disable no-instanceof/no-instanceof */\nimport { Runnable, RunnableConfig } from \"@langchain/core/runnables\";\nimport { BaseMessage, AIMessage, ToolMessage } from \"@langchain/core/messages\";\nimport { Command } from \"@langchain/langgraph\";\nimport { type LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport { type BaseChatModelCallOptions } from \"@langchain/core/language_models/chat_models\";\nimport {\n InteropZodObject,\n getSchemaDescription,\n} from \"@langchain/core/utils/types\";\nimport type { ToolCall } from \"@langchain/core/messages/tool\";\n\nimport { initChatModel } from \"../../chat_models/universal.js\";\nimport { MultipleStructuredOutputsError } from \"../errors.js\";\nimport { RunnableCallable } from \"../RunnableCallable.js\";\nimport { PreHookAnnotation, AnyAnnotationRoot } from \"../annotation.js\";\nimport { mergeAbortSignals } from \"./utils.js\";\nimport {\n bindTools,\n getPromptRunnable,\n validateLLMHasNoBoundTools,\n hasToolCalls,\n} from \"../utils.js\";\nimport {\n InternalAgentState,\n ClientTool,\n ServerTool,\n CreateAgentParams,\n} from \"../types.js\";\nimport { withAgentName } from \"../withAgentName.js\";\nimport {\n ToolStrategy,\n ProviderStrategy,\n transformResponseFormat,\n ToolStrategyError,\n hasSupportForJsonSchemaOutput,\n} from \"../responses.js\";\n\ntype ResponseHandlerResult<StructuredResponseFormat> =\n | {\n structuredResponse: StructuredResponseFormat;\n messages: BaseMessage[];\n }\n | Promise<Command>;\n\nexport interface AgentNodeOptions<\n StateSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,\n StructuredResponseFormat extends Record<string, unknown> = Record<\n string,\n unknown\n >,\n ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot\n> extends Pick<\n CreateAgentParams<StateSchema, StructuredResponseFormat, ContextSchema>,\n \"llm\" | \"model\" | \"prompt\" | \"includeAgentName\" | \"name\" | \"responseFormat\"\n > {\n toolClasses: (ClientTool | ServerTool)[];\n shouldReturnDirect: Set<string>;\n signal?: AbortSignal;\n}\n\ninterface NativeResponseFormat {\n type: \"native\";\n strategy: ProviderStrategy;\n}\n\ninterface ToolResponseFormat {\n type: \"tool\";\n tools: Record<string, ToolStrategy>;\n}\n\ntype ResponseFormat = NativeResponseFormat | ToolResponseFormat;\n\nexport class AgentNode<\n StateSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,\n StructuredResponseFormat extends Record<string, unknown> = Record<\n string,\n unknown\n >,\n ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot\n> extends RunnableCallable<\n InternalAgentState<StructuredResponseFormat> & PreHookAnnotation[\"State\"],\n { messages: BaseMessage[] } | { structuredResponse: StructuredResponseFormat }\n> {\n #options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >;\n\n constructor(\n options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >\n ) {\n super({\n name: options.name ?? \"model\",\n func: (input, config) =>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.#run(input, config as RunnableConfig) as any,\n });\n\n this.#options = options;\n }\n\n /**\n * Returns response format primtivies based on given model and response format provided by the user.\n *\n * If the the user selects a tool output:\n * - return a record of tools to extract structured output from the model's response\n *\n * if the the user selects a native schema output or if the model supports JSON schema output:\n * - return a provider strategy to extract structured output from the model's response\n *\n * @param model - The model to get the response format for.\n * @returns The response format.\n */\n #getResponseFormat(\n model: string | LanguageModelLike\n ): ResponseFormat | undefined {\n if (!this.#options.responseFormat) {\n return undefined;\n }\n\n const strategies = transformResponseFormat(\n this.#options.responseFormat,\n undefined,\n model\n );\n\n /**\n * we either define a list of provider strategies or a list of tool strategies\n */\n const isProviderStrategy = strategies.every(\n (format) => format instanceof ProviderStrategy\n );\n\n /**\n * Populate a list of structured tool info.\n */\n if (!isProviderStrategy) {\n return {\n type: \"tool\",\n tools: (\n strategies.filter(\n (format) => format instanceof ToolStrategy\n ) as ToolStrategy[]\n ).reduce((acc, format) => {\n acc[format.name] = format;\n return acc;\n }, {} as Record<string, ToolStrategy>),\n };\n }\n\n return {\n type: \"native\",\n /**\n * there can only be one provider strategy\n */\n strategy: strategies[0],\n };\n }\n\n async #run(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig\n ) {\n /**\n * Check if we just executed a returnDirect tool\n * If so, we should generate structured response (if needed) and stop\n */\n const lastMessage = state.messages[state.messages.length - 1];\n if (\n ToolMessage.isInstance(lastMessage) &&\n lastMessage.name &&\n this.#options.shouldReturnDirect.has(lastMessage.name)\n ) {\n /**\n * return directly without invoking the model again\n */\n return { messages: [] };\n }\n\n const response = await this.#invokeModel(state, config);\n\n /**\n * if we were able to generate a structured response, return it\n */\n if (\"structuredResponse\" in response) {\n return {\n messages: [...state.messages, ...(response.messages || [])],\n structuredResponse: response.structuredResponse,\n };\n }\n\n /**\n * if we need to direct the agent to the model, return the update\n */\n if (response instanceof Command) {\n return response;\n }\n\n response.name = this.name;\n response.lc_kwargs.name = this.name;\n\n if (this.#areMoreStepsNeeded(state, response)) {\n return {\n messages: [\n new AIMessage({\n content: \"Sorry, need more steps to process this request.\",\n name: this.name,\n id: response.id,\n }),\n ],\n };\n }\n\n return { messages: [response] };\n }\n\n /**\n * Derive the model from the options.\n * @param state - The state of the agent.\n * @param config - The config of the agent.\n * @returns The model.\n */\n #deriveModel(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig\n ) {\n if (this.#options.model) {\n if (typeof this.#options.model === \"string\") {\n return initChatModel(this.#options.model);\n }\n\n throw new Error(\"`model` option must be a string.\");\n }\n\n const model = this.#options.llm;\n\n /**\n * If the model is a function, call it to get the model.\n */\n if (typeof model === \"function\") {\n return model(state, config);\n }\n\n if (model) {\n return model;\n }\n\n throw new Error(\n \"No model option was provided, either via `model` or via `llm` option.\"\n );\n }\n\n async #invokeModel(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig,\n options: {\n lastMessage?: string;\n } = {}\n ): Promise<AIMessage | ResponseHandlerResult<StructuredResponseFormat>> {\n const model = await this.#deriveModel(state, config);\n\n /**\n * Check if the LLM already has bound tools and throw if it does.\n */\n validateLLMHasNoBoundTools(model);\n\n const structuredResponseFormat = this.#getResponseFormat(model);\n const modelWithTools = await this.#bindTools(\n model,\n structuredResponseFormat\n );\n const modelInput = this.#getModelInputState(state);\n const signal = mergeAbortSignals(this.#options.signal, config.signal);\n const invokeConfig = {\n ...config,\n signal,\n };\n\n const response = (await modelWithTools.invoke(\n modelInput,\n invokeConfig\n )) as AIMessage;\n\n /**\n * if the user requests a native schema output, try to parse the response\n * and return the structured response if it is valid\n */\n if (structuredResponseFormat?.type === \"native\") {\n const structuredResponse =\n structuredResponseFormat.strategy.parse(response);\n if (structuredResponse) {\n return { structuredResponse, messages: [response] };\n }\n\n return response;\n }\n\n if (!structuredResponseFormat || !response.tool_calls) {\n return response;\n }\n\n const toolCalls = response.tool_calls.filter(\n (call) => call.name in structuredResponseFormat.tools\n );\n\n /**\n * if there were not structured tool calls, we can return the response\n */\n if (toolCalls.length === 0) {\n return response;\n }\n\n /**\n * if there were multiple structured tool calls, we should throw an error as this\n * scenario is not defined/supported.\n */\n if (toolCalls.length > 1) {\n return this.#handleMultipleStructuredOutputs(\n response,\n toolCalls,\n structuredResponseFormat\n );\n }\n\n const toolStrategy = structuredResponseFormat.tools[toolCalls[0].name];\n const toolMessageContent = toolStrategy?.options?.toolMessageContent;\n return this.#handleSingleStructuredOutput(\n response,\n toolCalls[0],\n structuredResponseFormat,\n toolMessageContent ?? options.lastMessage\n );\n }\n\n /**\n * If the model returns multiple structured outputs, we need to handle it.\n * @param response - The response from the model\n * @param toolCalls - The tool calls that were made\n * @returns The response from the model\n */\n #handleMultipleStructuredOutputs(\n response: AIMessage,\n toolCalls: ToolCall[],\n structuredResponseFormat: ToolResponseFormat\n ): Promise<Command> {\n /**\n * the following should never happen, let's throw an error if it does\n */\n if (this.#options.responseFormat instanceof ProviderStrategy) {\n throw new Error(\n \"Multiple structured outputs should not apply to native structured output responses\"\n );\n }\n\n const multipleStructuredOutputsError = new MultipleStructuredOutputsError(\n toolCalls.map((call) => call.name)\n );\n\n return this.#handleToolStrategyError(\n multipleStructuredOutputsError,\n response,\n toolCalls[0],\n structuredResponseFormat\n );\n }\n\n /**\n * If the model returns a single structured output, we need to handle it.\n * @param toolCall - The tool call that was made\n * @returns The structured response and a message to the LLM if needed\n */\n #handleSingleStructuredOutput(\n response: AIMessage,\n toolCall: ToolCall,\n structuredResponseFormat: ToolResponseFormat,\n lastMessage?: string\n ): ResponseHandlerResult<StructuredResponseFormat> {\n const tool = structuredResponseFormat.tools[toolCall.name];\n\n try {\n const structuredResponse = tool.parse(\n toolCall.args\n ) as StructuredResponseFormat;\n\n return {\n structuredResponse,\n messages: [\n response,\n new AIMessage(\n lastMessage ??\n `Returning structured response: ${JSON.stringify(\n structuredResponse\n )}`\n ),\n ],\n };\n } catch (error) {\n return this.#handleToolStrategyError(\n error as ToolStrategyError,\n response,\n toolCall,\n structuredResponseFormat\n );\n }\n }\n\n async #handleToolStrategyError(\n error: ToolStrategyError,\n response: AIMessage,\n toolCall: ToolCall,\n structuredResponseFormat: ToolResponseFormat\n ): Promise<Command> {\n /**\n * Using the `errorHandler` option of the first `ToolStrategy` entry is sufficient here.\n * There is technically only one `ToolStrategy` entry in `structuredToolInfo` if the user\n * uses `toolStrategy` to define the response format. If the user applies a list of json\n * schema objects, these will be transformed into multiple `ToolStrategy` entries but all\n * with the same `handleError` option.\n */\n const errorHandler = Object.values(structuredResponseFormat.tools).at(0)\n ?.options?.handleError;\n\n const toolCallId = toolCall.id;\n if (!toolCallId) {\n throw new Error(\n \"Tool call ID is required to handle tool output errors. Please provide a tool call ID.\"\n );\n }\n\n /**\n * retry if:\n */\n if (\n /**\n * if the user has provided `true` as the `errorHandler` option, return a new AIMessage\n * with the error message and retry the tool call.\n */\n (typeof errorHandler === \"boolean\" && errorHandler) ||\n /**\n * if `errorHandler` is an array and contains MultipleStructuredOutputsError\n */\n (Array.isArray(errorHandler) &&\n errorHandler.some((h) => h instanceof MultipleStructuredOutputsError))\n ) {\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content: error.message,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * if `errorHandler` is a string, retry the tool call with given string\n */\n if (typeof errorHandler === \"string\") {\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content: errorHandler,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * if `errorHandler` is a function, retry the tool call with the function\n */\n if (typeof errorHandler === \"function\") {\n const content = await errorHandler(error);\n if (typeof content !== \"string\") {\n throw new Error(\"Error handler must return a string.\");\n }\n\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * throw otherwise, e.g. if `errorHandler` is not defined or set to `false`\n */\n throw error;\n }\n\n #areMoreStepsNeeded(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n response: BaseMessage\n ): boolean {\n const allToolsReturnDirect =\n AIMessage.isInstance(response) &&\n response.tool_calls?.every((call) =>\n this.#options.shouldReturnDirect.has(call.name)\n );\n const remainingSteps =\n \"remainingSteps\" in state ? (state.remainingSteps as number) : undefined;\n return Boolean(\n remainingSteps &&\n ((remainingSteps < 1 && allToolsReturnDirect) ||\n (remainingSteps < 2 && hasToolCalls(state.messages)))\n );\n }\n\n #getModelInputState(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"]\n ): Omit<InternalAgentState<StructuredResponseFormat>, \"llmInputMessages\"> {\n const { messages, llmInputMessages, ...rest } = state;\n if (llmInputMessages && llmInputMessages.length > 0) {\n return { messages: llmInputMessages, ...rest } as Omit<\n InternalAgentState<StructuredResponseFormat>,\n \"llmInputMessages\"\n >;\n }\n return { messages, ...rest } as Omit<\n InternalAgentState<StructuredResponseFormat>,\n \"llmInputMessages\"\n >;\n }\n\n async #bindTools(\n model: LanguageModelLike,\n structuredResponseFormat: ResponseFormat | undefined\n ): Promise<Runnable> {\n const options: Partial<BaseChatModelCallOptions> = {};\n const structuredTools =\n structuredResponseFormat?.type === \"tool\"\n ? Object.values(structuredResponseFormat.tools)\n : [];\n const allTools = this.#options.toolClasses.concat(\n ...structuredTools.map((toolStrategy) => toolStrategy.tool)\n );\n\n /**\n * If there are structured tools, we need to set the tool choice to \"any\"\n * so that the model can choose to use a structured tool or not.\n */\n const toolChoice = structuredTools.length > 0 ? \"any\" : undefined;\n\n /**\n * check if the user requests a native schema output\n */\n if (structuredResponseFormat?.type === \"native\") {\n /**\n * if the model does not support JSON schema output, throw an error\n */\n if (!hasSupportForJsonSchemaOutput(model)) {\n throw new Error(\n \"Model does not support native structured output responses. Please use a model that supports native structured output responses or use a tool output.\"\n );\n }\n\n const jsonSchemaParams = {\n name: structuredResponseFormat.strategy.schema?.name ?? \"extract\",\n description: getSchemaDescription(\n structuredResponseFormat.strategy.schema\n ),\n schema: structuredResponseFormat.strategy.schema,\n strict: true,\n };\n\n Object.assign(options, {\n response_format: {\n type: \"json_schema\",\n json_schema: jsonSchemaParams,\n },\n ls_structured_output_format: {\n kwargs: { method: \"json_schema\" },\n schema: structuredResponseFormat.strategy.schema,\n },\n strict: true,\n });\n }\n\n /**\n * Bind tools to the model if they are not already bound.\n */\n const modelWithTools = await bindTools(model, allTools, {\n ...options,\n tool_choice: toolChoice,\n });\n\n /**\n * Create a model runnable with the prompt and agent name\n */\n const modelRunnable = getPromptRunnable(this.#options.prompt).pipe(\n this.#options.includeAgentName === \"inline\"\n ? withAgentName(modelWithTools, this.#options.includeAgentName)\n : modelWithTools\n );\n\n return modelRunnable;\n }\n}\n"],"mappings":";;;;;;;;;;;;;AAyEA,IAAa,YAAb,cAOUA,0CAGR;CACA;CAMA,YACEC,SAKA;EACA,MAAM;GACJ,MAAM,QAAQ,QAAQ;GACtB,MAAM,CAAC,OAAO,WAEZ,KAAKC,KAAK,OAAO,OAAyB;EAC7C,EAAC;EAEF,KAAKC,WAAW;CACjB;;;;;;;;;;;;;CAcD,mBACEC,OAC4B;AAC5B,MAAI,CAAC,KAAKD,SAAS,eACjB,QAAO;EAGT,MAAM,aAAaE,0CACjB,KAAKF,SAAS,gBACd,QACA,MACD;;;;EAKD,MAAM,qBAAqB,WAAW,MACpC,CAAC,WAAW,kBAAkBG,mCAC/B;;;;AAKD,MAAI,CAAC,mBACH,QAAO;GACL,MAAM;GACN,OACE,WAAW,OACT,CAAC,WAAW,kBAAkBC,+BAC/B,CACD,OAAO,CAAC,KAAK,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;CACR;AACF"}
@@ -1,10 +1,10 @@
1
1
  import { initChatModel } from "../../chat_models/universal.js";
2
2
  import { MultipleStructuredOutputsError } from "../errors.js";
3
- import { bindTools, getPromptRunnable, hasSupportForJsonSchemaOutput, hasToolCalls, validateLLMHasNoBoundTools } from "../utils.js";
3
+ import { bindTools, getPromptRunnable, hasToolCalls, validateLLMHasNoBoundTools } from "../utils.js";
4
4
  import { RunnableCallable } from "../RunnableCallable.js";
5
5
  import { mergeAbortSignals } from "./utils.js";
6
6
  import { withAgentName } from "../withAgentName.js";
7
- import { ProviderStrategy, ToolStrategy, transformResponseFormat } from "../responses.js";
7
+ import { ProviderStrategy, ToolStrategy, hasSupportForJsonSchemaOutput, transformResponseFormat } from "../responses.js";
8
8
  import { AIMessage, ToolMessage } from "@langchain/core/messages";
9
9
  import { Command } from "@langchain/langgraph";
10
10
  import { getSchemaDescription } from "@langchain/core/utils/types";
@@ -12,20 +12,46 @@ import { getSchemaDescription } from "@langchain/core/utils/types";
12
12
  //#region src/agents/nodes/AgentNode.ts
13
13
  var AgentNode = class extends RunnableCallable {
14
14
  #options;
15
- #structuredToolInfo = {};
16
15
  constructor(options) {
17
16
  super({
18
17
  name: options.name ?? "model",
19
18
  func: (input, config) => this.#run(input, config)
20
19
  });
21
20
  this.#options = options;
21
+ }
22
+ /**
23
+ * Returns response format primtivies based on given model and response format provided by the user.
24
+ *
25
+ * If the the user selects a tool output:
26
+ * - return a record of tools to extract structured output from the model's response
27
+ *
28
+ * if the the user selects a native schema output or if the model supports JSON schema output:
29
+ * - return a provider strategy to extract structured output from the model's response
30
+ *
31
+ * @param model - The model to get the response format for.
32
+ * @returns The response format.
33
+ */
34
+ #getResponseFormat(model) {
35
+ if (!this.#options.responseFormat) return void 0;
36
+ const strategies = transformResponseFormat(this.#options.responseFormat, void 0, model);
37
+ /**
38
+ * we either define a list of provider strategies or a list of tool strategies
39
+ */
40
+ const isProviderStrategy = strategies.every((format) => format instanceof ProviderStrategy);
22
41
  /**
23
42
  * Populate a list of structured tool info.
24
43
  */
25
- this.#structuredToolInfo = transformResponseFormat(this.#options.responseFormat).filter((format) => format instanceof ToolStrategy).reduce((acc, format) => {
26
- acc[format.name] = format;
27
- return acc;
28
- }, {});
44
+ if (!isProviderStrategy) return {
45
+ type: "tool",
46
+ tools: strategies.filter((format) => format instanceof ToolStrategy).reduce((acc, format) => {
47
+ acc[format.name] = format;
48
+ return acc;
49
+ }, {})
50
+ };
51
+ return {
52
+ type: "native",
53
+ strategy: strategies[0]
54
+ };
29
55
  }
30
56
  async #run(state, config) {
31
57
  /**
@@ -84,7 +110,8 @@ var AgentNode = class extends RunnableCallable {
84
110
  * Check if the LLM already has bound tools and throw if it does.
85
111
  */
86
112
  validateLLMHasNoBoundTools(model);
87
- const modelWithTools = await this.#bindTools(model);
113
+ const structuredResponseFormat = this.#getResponseFormat(model);
114
+ const modelWithTools = await this.#bindTools(model, structuredResponseFormat);
88
115
  const modelInput = this.#getModelInputState(state);
89
116
  const signal = mergeAbortSignals(this.#options.signal, config.signal);
90
117
  const invokeConfig = {
@@ -96,15 +123,16 @@ var AgentNode = class extends RunnableCallable {
96
123
  * if the user requests a native schema output, try to parse the response
97
124
  * and return the structured response if it is valid
98
125
  */
99
- if (this.#options.responseFormat instanceof ProviderStrategy) {
100
- const structuredResponse = this.#options.responseFormat.parse(response);
126
+ if (structuredResponseFormat?.type === "native") {
127
+ const structuredResponse = structuredResponseFormat.strategy.parse(response);
101
128
  if (structuredResponse) return {
102
129
  structuredResponse,
103
130
  messages: [response]
104
131
  };
132
+ return response;
105
133
  }
106
- if (!response.tool_calls) return response;
107
- const toolCalls = response.tool_calls.filter((call) => call.name in this.#structuredToolInfo);
134
+ if (!structuredResponseFormat || !response.tool_calls) return response;
135
+ const toolCalls = response.tool_calls.filter((call) => call.name in structuredResponseFormat.tools);
108
136
  /**
109
137
  * if there were not structured tool calls, we can return the response
110
138
  */
@@ -113,10 +141,10 @@ var AgentNode = class extends RunnableCallable {
113
141
  * if there were multiple structured tool calls, we should throw an error as this
114
142
  * scenario is not defined/supported.
115
143
  */
116
- if (toolCalls.length > 1) return this.#handleMultipleStructuredOutputs(response, toolCalls);
117
- const toolStrategy = this.#structuredToolInfo[toolCalls[0].name];
144
+ if (toolCalls.length > 1) return this.#handleMultipleStructuredOutputs(response, toolCalls, structuredResponseFormat);
145
+ const toolStrategy = structuredResponseFormat.tools[toolCalls[0].name];
118
146
  const toolMessageContent = toolStrategy?.options?.toolMessageContent;
119
- return this.#handleSingleStructuredOutput(response, toolCalls[0], toolMessageContent ?? options.lastMessage);
147
+ return this.#handleSingleStructuredOutput(response, toolCalls[0], structuredResponseFormat, toolMessageContent ?? options.lastMessage);
120
148
  }
121
149
  /**
122
150
  * If the model returns multiple structured outputs, we need to handle it.
@@ -124,21 +152,21 @@ var AgentNode = class extends RunnableCallable {
124
152
  * @param toolCalls - The tool calls that were made
125
153
  * @returns The response from the model
126
154
  */
127
- #handleMultipleStructuredOutputs(response, toolCalls) {
155
+ #handleMultipleStructuredOutputs(response, toolCalls, structuredResponseFormat) {
128
156
  /**
129
157
  * the following should never happen, let's throw an error if it does
130
158
  */
131
159
  if (this.#options.responseFormat instanceof ProviderStrategy) throw new Error("Multiple structured outputs should not apply to native structured output responses");
132
160
  const multipleStructuredOutputsError = new MultipleStructuredOutputsError(toolCalls.map((call) => call.name));
133
- return this.#handleToolStrategyError(multipleStructuredOutputsError, response, toolCalls[0]);
161
+ return this.#handleToolStrategyError(multipleStructuredOutputsError, response, toolCalls[0], structuredResponseFormat);
134
162
  }
135
163
  /**
136
164
  * If the model returns a single structured output, we need to handle it.
137
165
  * @param toolCall - The tool call that was made
138
166
  * @returns The structured response and a message to the LLM if needed
139
167
  */
140
- #handleSingleStructuredOutput(response, toolCall, lastMessage) {
141
- const tool = this.#structuredToolInfo[toolCall.name];
168
+ #handleSingleStructuredOutput(response, toolCall, structuredResponseFormat, lastMessage) {
169
+ const tool = structuredResponseFormat.tools[toolCall.name];
142
170
  try {
143
171
  const structuredResponse = tool.parse(toolCall.args);
144
172
  return {
@@ -146,10 +174,10 @@ var AgentNode = class extends RunnableCallable {
146
174
  messages: [response, new AIMessage(lastMessage ?? `Returning structured response: ${JSON.stringify(structuredResponse)}`)]
147
175
  };
148
176
  } catch (error) {
149
- return this.#handleToolStrategyError(error, response, toolCall);
177
+ return this.#handleToolStrategyError(error, response, toolCall, structuredResponseFormat);
150
178
  }
151
179
  }
152
- async #handleToolStrategyError(error, response, toolCall) {
180
+ async #handleToolStrategyError(error, response, toolCall, structuredResponseFormat) {
153
181
  /**
154
182
  * Using the `errorHandler` option of the first `ToolStrategy` entry is sufficient here.
155
183
  * There is technically only one `ToolStrategy` entry in `structuredToolInfo` if the user
@@ -157,7 +185,7 @@ var AgentNode = class extends RunnableCallable {
157
185
  * schema objects, these will be transformed into multiple `ToolStrategy` entries but all
158
186
  * with the same `handleError` option.
159
187
  */
160
- const errorHandler = Object.values(this.#structuredToolInfo).at(0)?.options?.handleError;
188
+ const errorHandler = Object.values(structuredResponseFormat.tools).at(0)?.options?.handleError;
161
189
  const toolCallId = toolCall.id;
162
190
  if (!toolCallId) throw new Error("Tool call ID is required to handle tool output errors. Please provide a tool call ID.");
163
191
  /**
@@ -215,9 +243,9 @@ var AgentNode = class extends RunnableCallable {
215
243
  ...rest
216
244
  };
217
245
  }
218
- async #bindTools(model) {
246
+ async #bindTools(model, structuredResponseFormat) {
219
247
  const options = {};
220
- const structuredTools = Object.values(this.#structuredToolInfo);
248
+ const structuredTools = structuredResponseFormat?.type === "tool" ? Object.values(structuredResponseFormat.tools) : [];
221
249
  const allTools = this.#options.toolClasses.concat(...structuredTools.map((toolStrategy) => toolStrategy.tool));
222
250
  /**
223
251
  * If there are structured tools, we need to set the tool choice to "any"
@@ -227,15 +255,15 @@ var AgentNode = class extends RunnableCallable {
227
255
  /**
228
256
  * check if the user requests a native schema output
229
257
  */
230
- if (this.#options.responseFormat instanceof ProviderStrategy) {
258
+ if (structuredResponseFormat?.type === "native") {
231
259
  /**
232
260
  * if the model does not support JSON schema output, throw an error
233
261
  */
234
262
  if (!hasSupportForJsonSchemaOutput(model)) throw new Error("Model does not support native structured output responses. Please use a model that supports native structured output responses or use a tool output.");
235
263
  const jsonSchemaParams = {
236
- name: this.#options.responseFormat.schema?.name ?? "extract",
237
- description: getSchemaDescription(this.#options.responseFormat.schema),
238
- schema: this.#options.responseFormat.schema,
264
+ name: structuredResponseFormat.strategy.schema?.name ?? "extract",
265
+ description: getSchemaDescription(structuredResponseFormat.strategy.schema),
266
+ schema: structuredResponseFormat.strategy.schema,
239
267
  strict: true
240
268
  };
241
269
  Object.assign(options, {
@@ -245,7 +273,7 @@ var AgentNode = class extends RunnableCallable {
245
273
  },
246
274
  ls_structured_output_format: {
247
275
  kwargs: { method: "json_schema" },
248
- schema: this.#options.responseFormat.schema
276
+ schema: structuredResponseFormat.strategy.schema
249
277
  },
250
278
  strict: true
251
279
  });
@@ -1 +1 @@
1
- {"version":3,"file":"AgentNode.js","names":["options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >","#run","#options","#structuredToolInfo","state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"]","config: RunnableConfig","#invokeModel","#areMoreStepsNeeded","options: {\n lastMessage?: string;\n }","#deriveModel","#bindTools","#getModelInputState","#handleMultipleStructuredOutputs","#handleSingleStructuredOutput","response: AIMessage","toolCalls: ToolCall[]","#handleToolStrategyError","toolCall: ToolCall","lastMessage?: string","error: ToolStrategyError","response: BaseMessage","model: LanguageModelLike","options: Partial<BaseChatModelCallOptions>"],"sources":["../../../src/agents/nodes/AgentNode.ts"],"sourcesContent":["/* eslint-disable no-instanceof/no-instanceof */\nimport { Runnable, RunnableConfig } from \"@langchain/core/runnables\";\nimport { BaseMessage, AIMessage, ToolMessage } from \"@langchain/core/messages\";\nimport { Command } from \"@langchain/langgraph\";\nimport { type LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport { type BaseChatModelCallOptions } from \"@langchain/core/language_models/chat_models\";\nimport {\n InteropZodObject,\n getSchemaDescription,\n} from \"@langchain/core/utils/types\";\nimport type { ToolCall } from \"@langchain/core/messages/tool\";\n\nimport { initChatModel } from \"../../chat_models/universal.js\";\nimport { MultipleStructuredOutputsError } from \"../errors.js\";\nimport { RunnableCallable } from \"../RunnableCallable.js\";\nimport { PreHookAnnotation, AnyAnnotationRoot } from \"../annotation.js\";\nimport { mergeAbortSignals } from \"./utils.js\";\nimport {\n bindTools,\n getPromptRunnable,\n validateLLMHasNoBoundTools,\n hasToolCalls,\n hasSupportForJsonSchemaOutput,\n} from \"../utils.js\";\nimport {\n InternalAgentState,\n ClientTool,\n ServerTool,\n CreateAgentParams,\n} from \"../types.js\";\nimport { withAgentName } from \"../withAgentName.js\";\nimport {\n ToolStrategy,\n ProviderStrategy,\n transformResponseFormat,\n ToolStrategyError,\n} from \"../responses.js\";\n\ntype ResponseHandlerResult<StructuredResponseFormat> =\n | {\n structuredResponse: StructuredResponseFormat;\n messages: BaseMessage[];\n }\n | Promise<Command>;\n\nexport interface AgentNodeOptions<\n StateSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,\n StructuredResponseFormat extends Record<string, unknown> = Record<\n string,\n unknown\n >,\n ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot\n> extends Pick<\n CreateAgentParams<StateSchema, StructuredResponseFormat, ContextSchema>,\n \"llm\" | \"model\" | \"prompt\" | \"includeAgentName\" | \"name\" | \"responseFormat\"\n > {\n toolClasses: (ClientTool | ServerTool)[];\n shouldReturnDirect: Set<string>;\n signal?: AbortSignal;\n}\n\nexport class AgentNode<\n StateSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,\n StructuredResponseFormat extends Record<string, unknown> = Record<\n string,\n unknown\n >,\n ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot\n> extends RunnableCallable<\n InternalAgentState<StructuredResponseFormat> & PreHookAnnotation[\"State\"],\n { messages: BaseMessage[] } | { structuredResponse: StructuredResponseFormat }\n> {\n #options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >;\n\n #structuredToolInfo: Record<string, ToolStrategy> = {};\n\n constructor(\n options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >\n ) {\n super({\n name: options.name ?? \"model\",\n func: (input, config) =>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.#run(input, config as RunnableConfig) as any,\n });\n\n this.#options = options;\n\n /**\n * Populate a list of structured tool info.\n */\n this.#structuredToolInfo = (\n transformResponseFormat(this.#options.responseFormat).filter(\n (format) => format instanceof ToolStrategy\n ) as ToolStrategy[]\n ).reduce((acc, format) => {\n acc[format.name] = format;\n return acc;\n }, {} as Record<string, ToolStrategy>);\n }\n\n async #run(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig\n ) {\n /**\n * Check if we just executed a returnDirect tool\n * If so, we should generate structured response (if needed) and stop\n */\n const lastMessage = state.messages[state.messages.length - 1];\n if (\n ToolMessage.isInstance(lastMessage) &&\n lastMessage.name &&\n this.#options.shouldReturnDirect.has(lastMessage.name)\n ) {\n /**\n * return directly without invoking the model again\n */\n return { messages: [] };\n }\n\n const response = await this.#invokeModel(state, config);\n\n /**\n * if we were able to generate a structured response, return it\n */\n if (\"structuredResponse\" in response) {\n return {\n messages: [...state.messages, ...(response.messages || [])],\n structuredResponse: response.structuredResponse,\n };\n }\n\n /**\n * if we need to direct the agent to the model, return the update\n */\n if (response instanceof Command) {\n return response;\n }\n\n response.name = this.name;\n response.lc_kwargs.name = this.name;\n\n if (this.#areMoreStepsNeeded(state, response)) {\n return {\n messages: [\n new AIMessage({\n content: \"Sorry, need more steps to process this request.\",\n name: this.name,\n id: response.id,\n }),\n ],\n };\n }\n\n return { messages: [response] };\n }\n\n /**\n * Derive the model from the options.\n * @param state - The state of the agent.\n * @param config - The config of the agent.\n * @returns The model.\n */\n #deriveModel(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig\n ) {\n if (this.#options.model) {\n if (typeof this.#options.model === \"string\") {\n return initChatModel(this.#options.model);\n }\n\n throw new Error(\"`model` option must be a string.\");\n }\n\n const model = this.#options.llm;\n\n /**\n * If the model is a function, call it to get the model.\n */\n if (typeof model === \"function\") {\n return model(state, config);\n }\n\n if (model) {\n return model;\n }\n\n throw new Error(\n \"No model option was provided, either via `model` or via `llm` option.\"\n );\n }\n\n async #invokeModel(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig,\n options: {\n lastMessage?: string;\n } = {}\n ): Promise<AIMessage | ResponseHandlerResult<StructuredResponseFormat>> {\n const model = await this.#deriveModel(state, config);\n\n /**\n * Check if the LLM already has bound tools and throw if it does.\n */\n validateLLMHasNoBoundTools(model);\n\n const modelWithTools = await this.#bindTools(model);\n const modelInput = this.#getModelInputState(state);\n const signal = mergeAbortSignals(this.#options.signal, config.signal);\n const invokeConfig = {\n ...config,\n signal,\n };\n\n const response = (await modelWithTools.invoke(\n modelInput,\n invokeConfig\n )) as AIMessage;\n\n /**\n * if the user requests a native schema output, try to parse the response\n * and return the structured response if it is valid\n */\n if (this.#options.responseFormat instanceof ProviderStrategy) {\n const structuredResponse = this.#options.responseFormat.parse(response);\n if (structuredResponse) {\n return { structuredResponse, messages: [response] };\n }\n }\n\n if (!response.tool_calls) {\n return response;\n }\n\n const toolCalls = response.tool_calls.filter(\n (call) => call.name in this.#structuredToolInfo\n );\n\n /**\n * if there were not structured tool calls, we can return the response\n */\n if (toolCalls.length === 0) {\n return response;\n }\n\n /**\n * if there were multiple structured tool calls, we should throw an error as this\n * scenario is not defined/supported.\n */\n if (toolCalls.length > 1) {\n return this.#handleMultipleStructuredOutputs(response, toolCalls);\n }\n\n const toolStrategy = this.#structuredToolInfo[toolCalls[0].name];\n const toolMessageContent = toolStrategy?.options?.toolMessageContent;\n return this.#handleSingleStructuredOutput(\n response,\n toolCalls[0],\n toolMessageContent ?? options.lastMessage\n );\n }\n\n /**\n * If the model returns multiple structured outputs, we need to handle it.\n * @param response - The response from the model\n * @param toolCalls - The tool calls that were made\n * @returns The response from the model\n */\n #handleMultipleStructuredOutputs(\n response: AIMessage,\n toolCalls: ToolCall[]\n ): Promise<Command> {\n /**\n * the following should never happen, let's throw an error if it does\n */\n if (this.#options.responseFormat instanceof ProviderStrategy) {\n throw new Error(\n \"Multiple structured outputs should not apply to native structured output responses\"\n );\n }\n\n const multipleStructuredOutputsError = new MultipleStructuredOutputsError(\n toolCalls.map((call) => call.name)\n );\n\n return this.#handleToolStrategyError(\n multipleStructuredOutputsError,\n response,\n toolCalls[0]\n );\n }\n\n /**\n * If the model returns a single structured output, we need to handle it.\n * @param toolCall - The tool call that was made\n * @returns The structured response and a message to the LLM if needed\n */\n #handleSingleStructuredOutput(\n response: AIMessage,\n toolCall: ToolCall,\n lastMessage?: string\n ): ResponseHandlerResult<StructuredResponseFormat> {\n const tool = this.#structuredToolInfo[toolCall.name];\n\n try {\n const structuredResponse = tool.parse(\n toolCall.args\n ) as StructuredResponseFormat;\n\n return {\n structuredResponse,\n messages: [\n response,\n new AIMessage(\n lastMessage ??\n `Returning structured response: ${JSON.stringify(\n structuredResponse\n )}`\n ),\n ],\n };\n } catch (error) {\n return this.#handleToolStrategyError(\n error as ToolStrategyError,\n response,\n toolCall\n );\n }\n }\n\n async #handleToolStrategyError(\n error: ToolStrategyError,\n response: AIMessage,\n toolCall: ToolCall\n ): Promise<Command> {\n /**\n * Using the `errorHandler` option of the first `ToolStrategy` entry is sufficient here.\n * There is technically only one `ToolStrategy` entry in `structuredToolInfo` if the user\n * uses `toolStrategy` to define the response format. If the user applies a list of json\n * schema objects, these will be transformed into multiple `ToolStrategy` entries but all\n * with the same `handleError` option.\n */\n const errorHandler = Object.values(this.#structuredToolInfo).at(0)?.options\n ?.handleError;\n\n const toolCallId = toolCall.id;\n if (!toolCallId) {\n throw new Error(\n \"Tool call ID is required to handle tool output errors. Please provide a tool call ID.\"\n );\n }\n\n /**\n * retry if:\n */\n if (\n /**\n * if the user has provided `true` as the `errorHandler` option, return a new AIMessage\n * with the error message and retry the tool call.\n */\n (typeof errorHandler === \"boolean\" && errorHandler) ||\n /**\n * if `errorHandler` is an array and contains MultipleStructuredOutputsError\n */\n (Array.isArray(errorHandler) &&\n errorHandler.some((h) => h instanceof MultipleStructuredOutputsError))\n ) {\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content: error.message,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * if `errorHandler` is a string, retry the tool call with given string\n */\n if (typeof errorHandler === \"string\") {\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content: errorHandler,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * if `errorHandler` is a function, retry the tool call with the function\n */\n if (typeof errorHandler === \"function\") {\n const content = await errorHandler(error);\n if (typeof content !== \"string\") {\n throw new Error(\"Error handler must return a string.\");\n }\n\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * throw otherwise, e.g. if `errorHandler` is not defined or set to `false`\n */\n throw error;\n }\n\n #areMoreStepsNeeded(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n response: BaseMessage\n ): boolean {\n const allToolsReturnDirect =\n AIMessage.isInstance(response) &&\n response.tool_calls?.every((call) =>\n this.#options.shouldReturnDirect.has(call.name)\n );\n const remainingSteps =\n \"remainingSteps\" in state ? (state.remainingSteps as number) : undefined;\n return Boolean(\n remainingSteps &&\n ((remainingSteps < 1 && allToolsReturnDirect) ||\n (remainingSteps < 2 && hasToolCalls(state.messages)))\n );\n }\n\n #getModelInputState(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"]\n ): Omit<InternalAgentState<StructuredResponseFormat>, \"llmInputMessages\"> {\n const { messages, llmInputMessages, ...rest } = state;\n if (llmInputMessages && llmInputMessages.length > 0) {\n return { messages: llmInputMessages, ...rest } as Omit<\n InternalAgentState<StructuredResponseFormat>,\n \"llmInputMessages\"\n >;\n }\n return { messages, ...rest } as Omit<\n InternalAgentState<StructuredResponseFormat>,\n \"llmInputMessages\"\n >;\n }\n\n async #bindTools(model: LanguageModelLike): Promise<Runnable> {\n const options: Partial<BaseChatModelCallOptions> = {};\n const structuredTools = Object.values(this.#structuredToolInfo);\n const allTools = this.#options.toolClasses.concat(\n ...structuredTools.map((toolStrategy) => toolStrategy.tool)\n );\n\n /**\n * If there are structured tools, we need to set the tool choice to \"any\"\n * so that the model can choose to use a structured tool or not.\n */\n const toolChoice = structuredTools.length > 0 ? \"any\" : undefined;\n\n /**\n * check if the user requests a native schema output\n */\n if (this.#options.responseFormat instanceof ProviderStrategy) {\n /**\n * if the model does not support JSON schema output, throw an error\n */\n if (!hasSupportForJsonSchemaOutput(model)) {\n throw new Error(\n \"Model does not support native structured output responses. Please use a model that supports native structured output responses or use a tool output.\"\n );\n }\n\n const jsonSchemaParams = {\n name: this.#options.responseFormat.schema?.name ?? \"extract\",\n description: getSchemaDescription(this.#options.responseFormat.schema),\n schema: this.#options.responseFormat.schema,\n strict: true,\n };\n\n Object.assign(options, {\n response_format: {\n type: \"json_schema\",\n json_schema: jsonSchemaParams,\n },\n ls_structured_output_format: {\n kwargs: { method: \"json_schema\" },\n schema: this.#options.responseFormat.schema,\n },\n strict: true,\n });\n }\n\n /**\n * Bind tools to the model if they are not already bound.\n */\n const modelWithTools = await bindTools(model, allTools, {\n ...options,\n tool_choice: toolChoice,\n });\n\n /**\n * Create a model runnable with the prompt and agent name\n */\n const modelRunnable = getPromptRunnable(this.#options.prompt).pipe(\n this.#options.includeAgentName === \"inline\"\n ? withAgentName(modelWithTools, this.#options.includeAgentName)\n : modelWithTools\n );\n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AAa;EACd,EAAC;;;;EAKF,MAAM,gBAAgB,kBAAkB,KAAKA,SAAS,OAAO,CAAC,KAC5D,KAAKA,SAAS,qBAAqB,WAC/B,cAAc,gBAAgB,KAAKA,SAAS,iBAAiB,GAC7D,eACL;AAED,SAAO;CACR;AACF"}
1
+ {"version":3,"file":"AgentNode.js","names":["options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >","#run","#options","model: string | LanguageModelLike","state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"]","config: RunnableConfig","#invokeModel","#areMoreStepsNeeded","options: {\n lastMessage?: string;\n }","#deriveModel","#getResponseFormat","#bindTools","#getModelInputState","#handleMultipleStructuredOutputs","#handleSingleStructuredOutput","response: AIMessage","toolCalls: ToolCall[]","structuredResponseFormat: ToolResponseFormat","#handleToolStrategyError","toolCall: ToolCall","lastMessage?: string","error: ToolStrategyError","response: BaseMessage","model: LanguageModelLike","structuredResponseFormat: ResponseFormat | undefined","options: Partial<BaseChatModelCallOptions>"],"sources":["../../../src/agents/nodes/AgentNode.ts"],"sourcesContent":["/* eslint-disable no-instanceof/no-instanceof */\nimport { Runnable, RunnableConfig } from \"@langchain/core/runnables\";\nimport { BaseMessage, AIMessage, ToolMessage } from \"@langchain/core/messages\";\nimport { Command } from \"@langchain/langgraph\";\nimport { type LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport { type BaseChatModelCallOptions } from \"@langchain/core/language_models/chat_models\";\nimport {\n InteropZodObject,\n getSchemaDescription,\n} from \"@langchain/core/utils/types\";\nimport type { ToolCall } from \"@langchain/core/messages/tool\";\n\nimport { initChatModel } from \"../../chat_models/universal.js\";\nimport { MultipleStructuredOutputsError } from \"../errors.js\";\nimport { RunnableCallable } from \"../RunnableCallable.js\";\nimport { PreHookAnnotation, AnyAnnotationRoot } from \"../annotation.js\";\nimport { mergeAbortSignals } from \"./utils.js\";\nimport {\n bindTools,\n getPromptRunnable,\n validateLLMHasNoBoundTools,\n hasToolCalls,\n} from \"../utils.js\";\nimport {\n InternalAgentState,\n ClientTool,\n ServerTool,\n CreateAgentParams,\n} from \"../types.js\";\nimport { withAgentName } from \"../withAgentName.js\";\nimport {\n ToolStrategy,\n ProviderStrategy,\n transformResponseFormat,\n ToolStrategyError,\n hasSupportForJsonSchemaOutput,\n} from \"../responses.js\";\n\ntype ResponseHandlerResult<StructuredResponseFormat> =\n | {\n structuredResponse: StructuredResponseFormat;\n messages: BaseMessage[];\n }\n | Promise<Command>;\n\nexport interface AgentNodeOptions<\n StateSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,\n StructuredResponseFormat extends Record<string, unknown> = Record<\n string,\n unknown\n >,\n ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot\n> extends Pick<\n CreateAgentParams<StateSchema, StructuredResponseFormat, ContextSchema>,\n \"llm\" | \"model\" | \"prompt\" | \"includeAgentName\" | \"name\" | \"responseFormat\"\n > {\n toolClasses: (ClientTool | ServerTool)[];\n shouldReturnDirect: Set<string>;\n signal?: AbortSignal;\n}\n\ninterface NativeResponseFormat {\n type: \"native\";\n strategy: ProviderStrategy;\n}\n\ninterface ToolResponseFormat {\n type: \"tool\";\n tools: Record<string, ToolStrategy>;\n}\n\ntype ResponseFormat = NativeResponseFormat | ToolResponseFormat;\n\nexport class AgentNode<\n StateSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot,\n StructuredResponseFormat extends Record<string, unknown> = Record<\n string,\n unknown\n >,\n ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot\n> extends RunnableCallable<\n InternalAgentState<StructuredResponseFormat> & PreHookAnnotation[\"State\"],\n { messages: BaseMessage[] } | { structuredResponse: StructuredResponseFormat }\n> {\n #options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >;\n\n constructor(\n options: AgentNodeOptions<\n StateSchema,\n StructuredResponseFormat,\n ContextSchema\n >\n ) {\n super({\n name: options.name ?? \"model\",\n func: (input, config) =>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n this.#run(input, config as RunnableConfig) as any,\n });\n\n this.#options = options;\n }\n\n /**\n * Returns response format primtivies based on given model and response format provided by the user.\n *\n * If the the user selects a tool output:\n * - return a record of tools to extract structured output from the model's response\n *\n * if the the user selects a native schema output or if the model supports JSON schema output:\n * - return a provider strategy to extract structured output from the model's response\n *\n * @param model - The model to get the response format for.\n * @returns The response format.\n */\n #getResponseFormat(\n model: string | LanguageModelLike\n ): ResponseFormat | undefined {\n if (!this.#options.responseFormat) {\n return undefined;\n }\n\n const strategies = transformResponseFormat(\n this.#options.responseFormat,\n undefined,\n model\n );\n\n /**\n * we either define a list of provider strategies or a list of tool strategies\n */\n const isProviderStrategy = strategies.every(\n (format) => format instanceof ProviderStrategy\n );\n\n /**\n * Populate a list of structured tool info.\n */\n if (!isProviderStrategy) {\n return {\n type: \"tool\",\n tools: (\n strategies.filter(\n (format) => format instanceof ToolStrategy\n ) as ToolStrategy[]\n ).reduce((acc, format) => {\n acc[format.name] = format;\n return acc;\n }, {} as Record<string, ToolStrategy>),\n };\n }\n\n return {\n type: \"native\",\n /**\n * there can only be one provider strategy\n */\n strategy: strategies[0],\n };\n }\n\n async #run(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig\n ) {\n /**\n * Check if we just executed a returnDirect tool\n * If so, we should generate structured response (if needed) and stop\n */\n const lastMessage = state.messages[state.messages.length - 1];\n if (\n ToolMessage.isInstance(lastMessage) &&\n lastMessage.name &&\n this.#options.shouldReturnDirect.has(lastMessage.name)\n ) {\n /**\n * return directly without invoking the model again\n */\n return { messages: [] };\n }\n\n const response = await this.#invokeModel(state, config);\n\n /**\n * if we were able to generate a structured response, return it\n */\n if (\"structuredResponse\" in response) {\n return {\n messages: [...state.messages, ...(response.messages || [])],\n structuredResponse: response.structuredResponse,\n };\n }\n\n /**\n * if we need to direct the agent to the model, return the update\n */\n if (response instanceof Command) {\n return response;\n }\n\n response.name = this.name;\n response.lc_kwargs.name = this.name;\n\n if (this.#areMoreStepsNeeded(state, response)) {\n return {\n messages: [\n new AIMessage({\n content: \"Sorry, need more steps to process this request.\",\n name: this.name,\n id: response.id,\n }),\n ],\n };\n }\n\n return { messages: [response] };\n }\n\n /**\n * Derive the model from the options.\n * @param state - The state of the agent.\n * @param config - The config of the agent.\n * @returns The model.\n */\n #deriveModel(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig\n ) {\n if (this.#options.model) {\n if (typeof this.#options.model === \"string\") {\n return initChatModel(this.#options.model);\n }\n\n throw new Error(\"`model` option must be a string.\");\n }\n\n const model = this.#options.llm;\n\n /**\n * If the model is a function, call it to get the model.\n */\n if (typeof model === \"function\") {\n return model(state, config);\n }\n\n if (model) {\n return model;\n }\n\n throw new Error(\n \"No model option was provided, either via `model` or via `llm` option.\"\n );\n }\n\n async #invokeModel(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n config: RunnableConfig,\n options: {\n lastMessage?: string;\n } = {}\n ): Promise<AIMessage | ResponseHandlerResult<StructuredResponseFormat>> {\n const model = await this.#deriveModel(state, config);\n\n /**\n * Check if the LLM already has bound tools and throw if it does.\n */\n validateLLMHasNoBoundTools(model);\n\n const structuredResponseFormat = this.#getResponseFormat(model);\n const modelWithTools = await this.#bindTools(\n model,\n structuredResponseFormat\n );\n const modelInput = this.#getModelInputState(state);\n const signal = mergeAbortSignals(this.#options.signal, config.signal);\n const invokeConfig = {\n ...config,\n signal,\n };\n\n const response = (await modelWithTools.invoke(\n modelInput,\n invokeConfig\n )) as AIMessage;\n\n /**\n * if the user requests a native schema output, try to parse the response\n * and return the structured response if it is valid\n */\n if (structuredResponseFormat?.type === \"native\") {\n const structuredResponse =\n structuredResponseFormat.strategy.parse(response);\n if (structuredResponse) {\n return { structuredResponse, messages: [response] };\n }\n\n return response;\n }\n\n if (!structuredResponseFormat || !response.tool_calls) {\n return response;\n }\n\n const toolCalls = response.tool_calls.filter(\n (call) => call.name in structuredResponseFormat.tools\n );\n\n /**\n * if there were not structured tool calls, we can return the response\n */\n if (toolCalls.length === 0) {\n return response;\n }\n\n /**\n * if there were multiple structured tool calls, we should throw an error as this\n * scenario is not defined/supported.\n */\n if (toolCalls.length > 1) {\n return this.#handleMultipleStructuredOutputs(\n response,\n toolCalls,\n structuredResponseFormat\n );\n }\n\n const toolStrategy = structuredResponseFormat.tools[toolCalls[0].name];\n const toolMessageContent = toolStrategy?.options?.toolMessageContent;\n return this.#handleSingleStructuredOutput(\n response,\n toolCalls[0],\n structuredResponseFormat,\n toolMessageContent ?? options.lastMessage\n );\n }\n\n /**\n * If the model returns multiple structured outputs, we need to handle it.\n * @param response - The response from the model\n * @param toolCalls - The tool calls that were made\n * @returns The response from the model\n */\n #handleMultipleStructuredOutputs(\n response: AIMessage,\n toolCalls: ToolCall[],\n structuredResponseFormat: ToolResponseFormat\n ): Promise<Command> {\n /**\n * the following should never happen, let's throw an error if it does\n */\n if (this.#options.responseFormat instanceof ProviderStrategy) {\n throw new Error(\n \"Multiple structured outputs should not apply to native structured output responses\"\n );\n }\n\n const multipleStructuredOutputsError = new MultipleStructuredOutputsError(\n toolCalls.map((call) => call.name)\n );\n\n return this.#handleToolStrategyError(\n multipleStructuredOutputsError,\n response,\n toolCalls[0],\n structuredResponseFormat\n );\n }\n\n /**\n * If the model returns a single structured output, we need to handle it.\n * @param toolCall - The tool call that was made\n * @returns The structured response and a message to the LLM if needed\n */\n #handleSingleStructuredOutput(\n response: AIMessage,\n toolCall: ToolCall,\n structuredResponseFormat: ToolResponseFormat,\n lastMessage?: string\n ): ResponseHandlerResult<StructuredResponseFormat> {\n const tool = structuredResponseFormat.tools[toolCall.name];\n\n try {\n const structuredResponse = tool.parse(\n toolCall.args\n ) as StructuredResponseFormat;\n\n return {\n structuredResponse,\n messages: [\n response,\n new AIMessage(\n lastMessage ??\n `Returning structured response: ${JSON.stringify(\n structuredResponse\n )}`\n ),\n ],\n };\n } catch (error) {\n return this.#handleToolStrategyError(\n error as ToolStrategyError,\n response,\n toolCall,\n structuredResponseFormat\n );\n }\n }\n\n async #handleToolStrategyError(\n error: ToolStrategyError,\n response: AIMessage,\n toolCall: ToolCall,\n structuredResponseFormat: ToolResponseFormat\n ): Promise<Command> {\n /**\n * Using the `errorHandler` option of the first `ToolStrategy` entry is sufficient here.\n * There is technically only one `ToolStrategy` entry in `structuredToolInfo` if the user\n * uses `toolStrategy` to define the response format. If the user applies a list of json\n * schema objects, these will be transformed into multiple `ToolStrategy` entries but all\n * with the same `handleError` option.\n */\n const errorHandler = Object.values(structuredResponseFormat.tools).at(0)\n ?.options?.handleError;\n\n const toolCallId = toolCall.id;\n if (!toolCallId) {\n throw new Error(\n \"Tool call ID is required to handle tool output errors. Please provide a tool call ID.\"\n );\n }\n\n /**\n * retry if:\n */\n if (\n /**\n * if the user has provided `true` as the `errorHandler` option, return a new AIMessage\n * with the error message and retry the tool call.\n */\n (typeof errorHandler === \"boolean\" && errorHandler) ||\n /**\n * if `errorHandler` is an array and contains MultipleStructuredOutputsError\n */\n (Array.isArray(errorHandler) &&\n errorHandler.some((h) => h instanceof MultipleStructuredOutputsError))\n ) {\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content: error.message,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * if `errorHandler` is a string, retry the tool call with given string\n */\n if (typeof errorHandler === \"string\") {\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content: errorHandler,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * if `errorHandler` is a function, retry the tool call with the function\n */\n if (typeof errorHandler === \"function\") {\n const content = await errorHandler(error);\n if (typeof content !== \"string\") {\n throw new Error(\"Error handler must return a string.\");\n }\n\n return new Command({\n update: {\n messages: [\n response,\n new ToolMessage({\n content,\n tool_call_id: toolCallId,\n }),\n ],\n },\n goto: \"model\",\n });\n }\n\n /**\n * throw otherwise, e.g. if `errorHandler` is not defined or set to `false`\n */\n throw error;\n }\n\n #areMoreStepsNeeded(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"],\n response: BaseMessage\n ): boolean {\n const allToolsReturnDirect =\n AIMessage.isInstance(response) &&\n response.tool_calls?.every((call) =>\n this.#options.shouldReturnDirect.has(call.name)\n );\n const remainingSteps =\n \"remainingSteps\" in state ? (state.remainingSteps as number) : undefined;\n return Boolean(\n remainingSteps &&\n ((remainingSteps < 1 && allToolsReturnDirect) ||\n (remainingSteps < 2 && hasToolCalls(state.messages)))\n );\n }\n\n #getModelInputState(\n state: InternalAgentState<StructuredResponseFormat> &\n PreHookAnnotation[\"State\"]\n ): Omit<InternalAgentState<StructuredResponseFormat>, \"llmInputMessages\"> {\n const { messages, llmInputMessages, ...rest } = state;\n if (llmInputMessages && llmInputMessages.length > 0) {\n return { messages: llmInputMessages, ...rest } as Omit<\n InternalAgentState<StructuredResponseFormat>,\n \"llmInputMessages\"\n >;\n }\n return { messages, ...rest } as Omit<\n InternalAgentState<StructuredResponseFormat>,\n \"llmInputMessages\"\n >;\n }\n\n async #bindTools(\n model: LanguageModelLike,\n structuredResponseFormat: ResponseFormat | undefined\n ): Promise<Runnable> {\n const options: Partial<BaseChatModelCallOptions> = {};\n const structuredTools =\n structuredResponseFormat?.type === \"tool\"\n ? Object.values(structuredResponseFormat.tools)\n : [];\n const allTools = this.#options.toolClasses.concat(\n ...structuredTools.map((toolStrategy) => toolStrategy.tool)\n );\n\n /**\n * If there are structured tools, we need to set the tool choice to \"any\"\n * so that the model can choose to use a structured tool or not.\n */\n const toolChoice = structuredTools.length > 0 ? \"any\" : undefined;\n\n /**\n * check if the user requests a native schema output\n */\n if (structuredResponseFormat?.type === \"native\") {\n /**\n * if the model does not support JSON schema output, throw an error\n */\n if (!hasSupportForJsonSchemaOutput(model)) {\n throw new Error(\n \"Model does not support native structured output responses. Please use a model that supports native structured output responses or use a tool output.\"\n );\n }\n\n const jsonSchemaParams = {\n name: structuredResponseFormat.strategy.schema?.name ?? \"extract\",\n description: getSchemaDescription(\n structuredResponseFormat.strategy.schema\n ),\n schema: structuredResponseFormat.strategy.schema,\n strict: true,\n };\n\n Object.assign(options, {\n response_format: {\n type: \"json_schema\",\n json_schema: jsonSchemaParams,\n },\n ls_structured_output_format: {\n kwargs: { method: \"json_schema\" },\n schema: structuredResponseFormat.strategy.schema,\n },\n strict: true,\n });\n }\n\n /**\n * Bind tools to the model if they are not already bound.\n */\n const modelWithTools = await bindTools(model, allTools, {\n ...options,\n tool_choice: toolChoice,\n });\n\n /**\n * Create a model runnable with the prompt and agent name\n */\n const modelRunnable = getPromptRunnable(this.#options.prompt).pipe(\n this.#options.includeAgentName === \"inline\"\n ? withAgentName(modelWithTools, this.#options.includeAgentName)\n : modelWithTools\n );\n\n return modelRunnable;\n }\n}\n"],"mappings":";;;;;;;;;;;;AAyEA,IAAa,YAAb,cAOU,iBAGR;CACA;CAMA,YACEA,SAKA;EACA,MAAM;GACJ,MAAM,QAAQ,QAAQ;GACtB,MAAM,CAAC,OAAO,WAEZ,KAAKC,KAAK,OAAO,OAAyB;EAC7C,EAAC;EAEF,KAAKC,WAAW;CACjB;;;;;;;;;;;;;CAcD,mBACEC,OAC4B;AAC5B,MAAI,CAAC,KAAKD,SAAS,eACjB,QAAO;EAGT,MAAM,aAAa,wBACjB,KAAKA,SAAS,gBACd,QACA,MACD;;;;EAKD,MAAM,qBAAqB,WAAW,MACpC,CAAC,WAAW,kBAAkB,iBAC/B;;;;AAKD,MAAI,CAAC,mBACH,QAAO;GACL,MAAM;GACN,OACE,WAAW,OACT,CAAC,WAAW,kBAAkB,aAC/B,CACD,OAAO,CAAC,KAAK,WAAW;IACxB,IAAI,OAAO,QAAQ;AACnB,WAAO;GACR,GAAE,CAAE,EAAiC;EACvC;AAGH,SAAO;GACL,MAAM;GAIN,UAAU,WAAW;EACtB;CACF;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,MAAI,oBAAoB,iBAAiB,SAAS,EAChD,QAAO;GAAE,UAAU;GAAkB,GAAG;EAAM;AAKhD,SAAO;GAAE;GAAU,GAAG;EAAM;CAI7B;CAED,MAAMO,WACJY,OACAC,0BACmB;EACnB,MAAMC,UAA6C,CAAE;EACrD,MAAM,kBACJ,0BAA0B,SAAS,SAC/B,OAAO,OAAO,yBAAyB,MAAM,GAC7C,CAAE;EACR,MAAM,WAAW,KAAKvB,SAAS,YAAY,OACzC,GAAG,gBAAgB,IAAI,CAAC,iBAAiB,aAAa,KAAK,CAC5D;;;;;EAMD,MAAM,aAAa,gBAAgB,SAAS,IAAI,QAAQ;;;;AAKxD,MAAI,0BAA0B,SAAS,UAAU;;;;AAI/C,OAAI,CAAC,8BAA8B,MAAM,CACvC,OAAM,IAAI,MACR;GAIJ,MAAM,mBAAmB;IACvB,MAAM,yBAAyB,SAAS,QAAQ,QAAQ;IACxD,aAAa,qBACX,yBAAyB,SAAS,OACnC;IACD,QAAQ,yBAAyB,SAAS;IAC1C,QAAQ;GACT;GAED,OAAO,OAAO,SAAS;IACrB,iBAAiB;KACf,MAAM;KACN,aAAa;IACd;IACD,6BAA6B;KAC3B,QAAQ,EAAE,QAAQ,cAAe;KACjC,QAAQ,yBAAyB,SAAS;IAC3C;IACD,QAAQ;GACT,EAAC;EACH;;;;EAKD,MAAM,iBAAiB,MAAM,UAAU,OAAO,UAAU;GACtD,GAAG;GACH,aAAa;EACd,EAAC;;;;EAKF,MAAM,gBAAgB,kBAAkB,KAAKA,SAAS,OAAO,CAAC,KAC5D,KAAKA,SAAS,qBAAqB,WAC/B,cAAc,gBAAgB,KAAKA,SAAS,iBAAiB,GAC7D,eACL;AAED,SAAO;CACR;AACF"}
@@ -82,7 +82,7 @@ var ToolNode = class extends require_RunnableCallable.RunnableCallable {
82
82
  ...config,
83
83
  signal: require_utils.mergeAbortSignals(this.signal, config.signal)
84
84
  });
85
- if ((0, __langchain_core_messages.isBaseMessage)(output) && output.getType() === "tool" || (0, __langchain_langgraph.isCommand)(output)) return output;
85
+ if (__langchain_core_messages.ToolMessage.isInstance(output) || (0, __langchain_langgraph.isCommand)(output)) return output;
86
86
  return new __langchain_core_messages.ToolMessage({
87
87
  name: tool.name,
88
88
  content: typeof output === "string" ? output : JSON.stringify(output),