langchain 1.0.4 → 1.0.5

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 (100) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/dist/agents/ReactAgent.cjs +15 -17
  3. package/dist/agents/ReactAgent.cjs.map +1 -1
  4. package/dist/agents/ReactAgent.d.cts +3 -3
  5. package/dist/agents/ReactAgent.d.cts.map +1 -1
  6. package/dist/agents/ReactAgent.d.ts +3 -3
  7. package/dist/agents/ReactAgent.d.ts.map +1 -1
  8. package/dist/agents/ReactAgent.js +15 -17
  9. package/dist/agents/ReactAgent.js.map +1 -1
  10. package/dist/agents/middleware/contextEditing.cjs +302 -33
  11. package/dist/agents/middleware/contextEditing.cjs.map +1 -1
  12. package/dist/agents/middleware/contextEditing.d.cts +125 -41
  13. package/dist/agents/middleware/contextEditing.d.cts.map +1 -1
  14. package/dist/agents/middleware/contextEditing.d.ts +125 -41
  15. package/dist/agents/middleware/contextEditing.d.ts.map +1 -1
  16. package/dist/agents/middleware/contextEditing.js +302 -33
  17. package/dist/agents/middleware/contextEditing.js.map +1 -1
  18. package/dist/agents/middleware/dynamicSystemPrompt.d.cts.map +1 -1
  19. package/dist/agents/middleware/dynamicSystemPrompt.d.ts.map +1 -1
  20. package/dist/agents/middleware/hitl.d.cts.map +1 -1
  21. package/dist/agents/middleware/hitl.d.ts.map +1 -1
  22. package/dist/agents/middleware/index.cjs +2 -0
  23. package/dist/agents/middleware/index.js +2 -0
  24. package/dist/agents/middleware/llmToolSelector.d.cts +4 -4
  25. package/dist/agents/middleware/llmToolSelector.d.cts.map +1 -1
  26. package/dist/agents/middleware/modelCallLimit.cjs +6 -2
  27. package/dist/agents/middleware/modelCallLimit.cjs.map +1 -1
  28. package/dist/agents/middleware/modelCallLimit.d.cts +8 -8
  29. package/dist/agents/middleware/modelCallLimit.d.cts.map +1 -1
  30. package/dist/agents/middleware/modelCallLimit.d.ts +8 -8
  31. package/dist/agents/middleware/modelCallLimit.d.ts.map +1 -1
  32. package/dist/agents/middleware/modelCallLimit.js +6 -2
  33. package/dist/agents/middleware/modelCallLimit.js.map +1 -1
  34. package/dist/agents/middleware/modelFallback.cjs +2 -2
  35. package/dist/agents/middleware/modelFallback.cjs.map +1 -1
  36. package/dist/agents/middleware/modelFallback.d.cts +2 -2
  37. package/dist/agents/middleware/modelFallback.d.cts.map +1 -1
  38. package/dist/agents/middleware/modelFallback.d.ts +2 -2
  39. package/dist/agents/middleware/modelFallback.d.ts.map +1 -1
  40. package/dist/agents/middleware/modelFallback.js +2 -2
  41. package/dist/agents/middleware/modelFallback.js.map +1 -1
  42. package/dist/agents/middleware/pii.cjs +445 -0
  43. package/dist/agents/middleware/pii.cjs.map +1 -0
  44. package/dist/agents/middleware/pii.d.cts +216 -0
  45. package/dist/agents/middleware/pii.d.cts.map +1 -0
  46. package/dist/agents/middleware/pii.d.ts +216 -0
  47. package/dist/agents/middleware/pii.d.ts.map +1 -0
  48. package/dist/agents/middleware/pii.js +436 -0
  49. package/dist/agents/middleware/pii.js.map +1 -0
  50. package/dist/agents/middleware/piiRedaction.cjs +2 -1
  51. package/dist/agents/middleware/piiRedaction.cjs.map +1 -1
  52. package/dist/agents/middleware/piiRedaction.d.cts +4 -1
  53. package/dist/agents/middleware/piiRedaction.d.cts.map +1 -1
  54. package/dist/agents/middleware/piiRedaction.d.ts +4 -1
  55. package/dist/agents/middleware/piiRedaction.d.ts.map +1 -1
  56. package/dist/agents/middleware/piiRedaction.js +2 -1
  57. package/dist/agents/middleware/piiRedaction.js.map +1 -1
  58. package/dist/agents/middleware/promptCaching.d.cts.map +1 -1
  59. package/dist/agents/middleware/promptCaching.d.ts.map +1 -1
  60. package/dist/agents/middleware/summarization.cjs +15 -24
  61. package/dist/agents/middleware/summarization.cjs.map +1 -1
  62. package/dist/agents/middleware/summarization.d.cts +72 -9
  63. package/dist/agents/middleware/summarization.d.cts.map +1 -1
  64. package/dist/agents/middleware/summarization.d.ts +65 -2
  65. package/dist/agents/middleware/summarization.d.ts.map +1 -1
  66. package/dist/agents/middleware/summarization.js +13 -25
  67. package/dist/agents/middleware/summarization.js.map +1 -1
  68. package/dist/agents/middleware/todoListMiddleware.d.cts.map +1 -1
  69. package/dist/agents/middleware/todoListMiddleware.d.ts.map +1 -1
  70. package/dist/agents/middleware/toolCallLimit.d.cts.map +1 -1
  71. package/dist/agents/middleware/toolCallLimit.d.ts.map +1 -1
  72. package/dist/agents/middleware/toolEmulator.cjs +118 -0
  73. package/dist/agents/middleware/toolEmulator.cjs.map +1 -0
  74. package/dist/agents/middleware/toolEmulator.d.cts +76 -0
  75. package/dist/agents/middleware/toolEmulator.d.cts.map +1 -0
  76. package/dist/agents/middleware/toolEmulator.d.ts +76 -0
  77. package/dist/agents/middleware/toolEmulator.d.ts.map +1 -0
  78. package/dist/agents/middleware/toolEmulator.js +117 -0
  79. package/dist/agents/middleware/toolEmulator.js.map +1 -0
  80. package/dist/agents/middleware/types.d.cts.map +1 -1
  81. package/dist/agents/middleware/types.d.ts.map +1 -1
  82. package/dist/agents/middleware/utils.cjs +4 -0
  83. package/dist/agents/middleware/utils.cjs.map +1 -1
  84. package/dist/agents/middleware/utils.d.cts.map +1 -1
  85. package/dist/agents/middleware/utils.d.ts.map +1 -1
  86. package/dist/agents/middleware/utils.js +4 -0
  87. package/dist/agents/middleware/utils.js.map +1 -1
  88. package/dist/agents/nodes/AgentNode.cjs +1 -1
  89. package/dist/agents/nodes/AgentNode.cjs.map +1 -1
  90. package/dist/agents/nodes/AgentNode.js +1 -1
  91. package/dist/agents/nodes/AgentNode.js.map +1 -1
  92. package/dist/agents/runtime.d.cts +5 -5
  93. package/dist/agents/runtime.d.cts.map +1 -1
  94. package/dist/agents/runtime.d.ts +5 -5
  95. package/dist/agents/runtime.d.ts.map +1 -1
  96. package/dist/index.cjs +22 -0
  97. package/dist/index.d.cts +5 -3
  98. package/dist/index.d.ts +5 -3
  99. package/dist/index.js +13 -1
  100. package/package.json +9 -8
@@ -14,19 +14,19 @@ declare const contextSchema: z.ZodObject<{
14
14
  runLimit: z.ZodOptional<z.ZodNumber>;
15
15
  /**
16
16
  * The behavior to take when the limit is exceeded.
17
- * - "throw" will throw an error and stop the agent.
17
+ * - "error" will throw an error and stop the agent.
18
18
  * - "end" will end the agent.
19
19
  * @default "end"
20
20
  */
21
- exitBehavior: z.ZodOptional<z.ZodEnum<["throw", "end"]>>;
21
+ exitBehavior: z.ZodOptional<z.ZodEnum<["error", "end"]>>;
22
22
  }, "strip", z.ZodTypeAny, {
23
23
  threadLimit?: number | undefined;
24
24
  runLimit?: number | undefined;
25
- exitBehavior?: "end" | "throw" | undefined;
25
+ exitBehavior?: "end" | "error" | undefined;
26
26
  }, {
27
27
  threadLimit?: number | undefined;
28
28
  runLimit?: number | undefined;
29
- exitBehavior?: "end" | "throw" | undefined;
29
+ exitBehavior?: "end" | "error" | undefined;
30
30
  }>;
31
31
  type ModelCallLimitMiddlewareConfig = Partial<InferInteropZodInput<typeof contextSchema>>;
32
32
  /**
@@ -109,19 +109,19 @@ declare function modelCallLimitMiddleware(middlewareOptions?: ModelCallLimitMidd
109
109
  runLimit: z.ZodOptional<z.ZodNumber>;
110
110
  /**
111
111
  * The behavior to take when the limit is exceeded.
112
- * - "throw" will throw an error and stop the agent.
112
+ * - "error" will throw an error and stop the agent.
113
113
  * - "end" will end the agent.
114
114
  * @default "end"
115
115
  */
116
- exitBehavior: z.ZodOptional<z.ZodEnum<["throw", "end"]>>;
116
+ exitBehavior: z.ZodOptional<z.ZodEnum<["error", "end"]>>;
117
117
  }, "strip", z.ZodTypeAny, {
118
118
  threadLimit?: number | undefined;
119
119
  runLimit?: number | undefined;
120
- exitBehavior?: "end" | "throw" | undefined;
120
+ exitBehavior?: "end" | "error" | undefined;
121
121
  }, {
122
122
  threadLimit?: number | undefined;
123
123
  runLimit?: number | undefined;
124
- exitBehavior?: "end" | "throw" | undefined;
124
+ exitBehavior?: "end" | "error" | undefined;
125
125
  }>, any>;
126
126
  //#endregion
127
127
  export { ModelCallLimitMiddlewareConfig, modelCallLimitMiddleware };
@@ -1 +1 @@
1
- {"version":3,"file":"modelCallLimit.d.ts","names":["z","InferInteropZodInput","contextSchema","ZodNumber","ZodOptional","ZodEnum","ZodTypeAny","ZodObject","ModelCallLimitMiddlewareConfig","Partial","modelCallLimitMiddleware","ZodDefault","__types_js6","AgentMiddleware"],"sources":["../../../src/agents/middleware/modelCallLimit.d.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { InferInteropZodInput } from \"@langchain/core/utils/types\";\ndeclare const contextSchema: z.ZodObject<{\n /**\n * The maximum number of model calls allowed per thread.\n */\n threadLimit: z.ZodOptional<z.ZodNumber>;\n /**\n * The maximum number of model calls allowed per run.\n */\n runLimit: z.ZodOptional<z.ZodNumber>;\n /**\n * The behavior to take when the limit is exceeded.\n * - \"throw\" will throw an error and stop the agent.\n * - \"end\" will end the agent.\n * @default \"end\"\n */\n exitBehavior: z.ZodOptional<z.ZodEnum<[\"throw\", \"end\"]>>;\n}, \"strip\", z.ZodTypeAny, {\n threadLimit?: number | undefined;\n runLimit?: number | undefined;\n exitBehavior?: \"end\" | \"throw\" | undefined;\n}, {\n threadLimit?: number | undefined;\n runLimit?: number | undefined;\n exitBehavior?: \"end\" | \"throw\" | undefined;\n}>;\nexport type ModelCallLimitMiddlewareConfig = Partial<InferInteropZodInput<typeof contextSchema>>;\n/**\n * Creates a middleware to limit the number of model calls at both thread and run levels.\n *\n * This middleware helps prevent excessive model API calls by enforcing limits on how many\n * times the model can be invoked. It supports two types of limits:\n *\n * - **Thread-level limit**: Restricts the total number of model calls across an entire conversation thread\n * - **Run-level limit**: Restricts the number of model calls within a single agent run/invocation\n *\n * ## How It Works\n *\n * The middleware intercepts model requests before they are sent and checks the current call counts\n * against the configured limits. If either limit is exceeded, it throws a `ModelCallLimitMiddlewareError`\n * to stop execution and prevent further API calls.\n *\n * ## Use Cases\n *\n * - **Cost Control**: Prevent runaway costs from excessive model calls in production\n * - **Testing**: Ensure agents don't make too many calls during development/testing\n * - **Safety**: Limit potential infinite loops or recursive agent behaviors\n * - **Rate Limiting**: Enforce organizational policies on model usage per conversation\n *\n * @param middlewareOptions - Configuration options for the call limits\n * @param middlewareOptions.threadLimit - Maximum number of model calls allowed per thread (optional)\n * @param middlewareOptions.runLimit - Maximum number of model calls allowed per run (optional)\n *\n * @returns A middleware instance that can be passed to `createAgent`\n *\n * @throws {ModelCallLimitMiddlewareError} When either the thread or run limit is exceeded\n *\n * @example\n * ```typescript\n * import { createAgent, modelCallLimitMiddleware } from \"langchain\";\n *\n * // Limit to 10 calls per thread and 3 calls per run\n * const agent = createAgent({\n * model: \"openai:gpt-4o-mini\",\n * tools: [myTool],\n * middleware: [\n * modelCallLimitMiddleware({\n * threadLimit: 10,\n * runLimit: 3\n * })\n * ]\n * });\n * ```\n *\n * @example\n * ```typescript\n * // Limits can also be configured at runtime via context\n * const result = await agent.invoke(\n * { messages: [\"Hello\"] },\n * {\n * configurable: {\n * threadLimit: 5 // Override the default limit for this run\n * }\n * }\n * );\n * ```\n */\nexport declare function modelCallLimitMiddleware(middlewareOptions?: ModelCallLimitMiddlewareConfig): import(\"./types.js\").AgentMiddleware<z.ZodObject<{\n threadModelCallCount: z.ZodDefault<z.ZodNumber>;\n runModelCallCount: z.ZodDefault<z.ZodNumber>;\n}, \"strip\", z.ZodTypeAny, {\n threadModelCallCount: number;\n runModelCallCount: number;\n}, {\n threadModelCallCount?: number | undefined;\n runModelCallCount?: number | undefined;\n}>, z.ZodObject<{\n /**\n * The maximum number of model calls allowed per thread.\n */\n threadLimit: z.ZodOptional<z.ZodNumber>;\n /**\n * The maximum number of model calls allowed per run.\n */\n runLimit: z.ZodOptional<z.ZodNumber>;\n /**\n * The behavior to take when the limit is exceeded.\n * - \"throw\" will throw an error and stop the agent.\n * - \"end\" will end the agent.\n * @default \"end\"\n */\n exitBehavior: z.ZodOptional<z.ZodEnum<[\"throw\", \"end\"]>>;\n}, \"strip\", z.ZodTypeAny, {\n threadLimit?: number | undefined;\n runLimit?: number | undefined;\n exitBehavior?: \"end\" | \"throw\" | undefined;\n}, {\n threadLimit?: number | undefined;\n runLimit?: number | undefined;\n exitBehavior?: \"end\" | \"throw\" | undefined;\n}>, any>;\nexport {};\n"],"mappings":";;;;;cAEcE,eAAeF,CAAAA,CAAEO;;;AADoC;EAyBjE,WAAA,EApBeP,CAAAA,CAAEI,WAoBjB,CApB6BJ,CAAAA,CAAEG,SAoB/B,CAAA;EAAA;;;EAhBqC,QAAvBC,EAAFJ,CAAAA,CAAEI,WAAAA,CAAYJ,CAAAA,CAAEG,SAAdC,CAAAA;EAAW;;;;AARa;AAyBxC;EAA0C,YAAA,EAVxBJ,CAAAA,CAAEI,WAUsB,CAVVJ,CAAAA,CAAEK,OAUQ,CAAA,CAAA,OAAA,EAAA,KAAA,CAAA,CAAA,CAAA;CAAA,EAAA,OAAuCH,EATrEF,CAAAA,CAAEM,UASmEJ,EAAAA;EAAa,WAAzCD,CAAAA,EAAAA,MAAAA,GAAAA,SAAAA;EAAoB,QAA5BQ,CAAAA,EAAAA,MAAAA,GAAAA,SAAAA;EAAO,YAAA,CAAA,EAAA,KAAA,GAAA,OAAA,GAAA,SAAA;AA6DpD,CAAA,EAAA;EAAgD,WAAA,CAAA,EAAA,MAAA,GAAA,SAAA;EAAA,QAAqBD,CAAAA,EAAAA,MAAAA,GAAAA,SAAAA;EAA8B,YAC1DL,CAAAA,EAAAA,KAAAA,GAAAA,OAAAA,GAAAA,SAAAA;CAAS,CAAA;AACdH,KA/DxBQ,8BAAAA,GAAiCC,OA+DPN,CA/DeF,oBA+DfE,CAAAA,OA/D2CD,aA+D3CC,CAAAA,CAAAA;;;;;;;;;;;;;AAFoG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAlHO,wBAAAA,qBAA6CF,iCAStD,gBAT4HR,CAAAA,CAAEO;wBACnHP,CAAAA,CAAEW,WAAWX,CAAAA,CAAEG;qBAClBH,CAAAA,CAAEW,WAAWX,CAAAA,CAAEG;YAC1BH,CAAAA,CAAEM;;;;;;IAMVN,CAAAA,CAAEO;;;;eAIWP,CAAAA,CAAEI,YAAYJ,CAAAA,CAAEG;;;;YAInBH,CAAAA,CAAEI,YAAYJ,CAAAA,CAAEG;;;;;;;gBAOZH,CAAAA,CAAEI,YAAYJ,CAAAA,CAAEK;YACtBL,CAAAA,CAAEM"}
1
+ {"version":3,"file":"modelCallLimit.d.ts","names":["z","InferInteropZodInput","contextSchema","ZodNumber","ZodOptional","ZodEnum","ZodTypeAny","ZodObject","ModelCallLimitMiddlewareConfig","Partial","modelCallLimitMiddleware","ZodDefault","__types_js5","AgentMiddleware"],"sources":["../../../src/agents/middleware/modelCallLimit.d.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { InferInteropZodInput } from \"@langchain/core/utils/types\";\ndeclare const contextSchema: z.ZodObject<{\n /**\n * The maximum number of model calls allowed per thread.\n */\n threadLimit: z.ZodOptional<z.ZodNumber>;\n /**\n * The maximum number of model calls allowed per run.\n */\n runLimit: z.ZodOptional<z.ZodNumber>;\n /**\n * The behavior to take when the limit is exceeded.\n * - \"error\" will throw an error and stop the agent.\n * - \"end\" will end the agent.\n * @default \"end\"\n */\n exitBehavior: z.ZodOptional<z.ZodEnum<[\"error\", \"end\"]>>;\n}, \"strip\", z.ZodTypeAny, {\n threadLimit?: number | undefined;\n runLimit?: number | undefined;\n exitBehavior?: \"end\" | \"error\" | undefined;\n}, {\n threadLimit?: number | undefined;\n runLimit?: number | undefined;\n exitBehavior?: \"end\" | \"error\" | undefined;\n}>;\nexport type ModelCallLimitMiddlewareConfig = Partial<InferInteropZodInput<typeof contextSchema>>;\n/**\n * Creates a middleware to limit the number of model calls at both thread and run levels.\n *\n * This middleware helps prevent excessive model API calls by enforcing limits on how many\n * times the model can be invoked. It supports two types of limits:\n *\n * - **Thread-level limit**: Restricts the total number of model calls across an entire conversation thread\n * - **Run-level limit**: Restricts the number of model calls within a single agent run/invocation\n *\n * ## How It Works\n *\n * The middleware intercepts model requests before they are sent and checks the current call counts\n * against the configured limits. If either limit is exceeded, it throws a `ModelCallLimitMiddlewareError`\n * to stop execution and prevent further API calls.\n *\n * ## Use Cases\n *\n * - **Cost Control**: Prevent runaway costs from excessive model calls in production\n * - **Testing**: Ensure agents don't make too many calls during development/testing\n * - **Safety**: Limit potential infinite loops or recursive agent behaviors\n * - **Rate Limiting**: Enforce organizational policies on model usage per conversation\n *\n * @param middlewareOptions - Configuration options for the call limits\n * @param middlewareOptions.threadLimit - Maximum number of model calls allowed per thread (optional)\n * @param middlewareOptions.runLimit - Maximum number of model calls allowed per run (optional)\n *\n * @returns A middleware instance that can be passed to `createAgent`\n *\n * @throws {ModelCallLimitMiddlewareError} When either the thread or run limit is exceeded\n *\n * @example\n * ```typescript\n * import { createAgent, modelCallLimitMiddleware } from \"langchain\";\n *\n * // Limit to 10 calls per thread and 3 calls per run\n * const agent = createAgent({\n * model: \"openai:gpt-4o-mini\",\n * tools: [myTool],\n * middleware: [\n * modelCallLimitMiddleware({\n * threadLimit: 10,\n * runLimit: 3\n * })\n * ]\n * });\n * ```\n *\n * @example\n * ```typescript\n * // Limits can also be configured at runtime via context\n * const result = await agent.invoke(\n * { messages: [\"Hello\"] },\n * {\n * configurable: {\n * threadLimit: 5 // Override the default limit for this run\n * }\n * }\n * );\n * ```\n */\nexport declare function modelCallLimitMiddleware(middlewareOptions?: ModelCallLimitMiddlewareConfig): import(\"./types.js\").AgentMiddleware<z.ZodObject<{\n threadModelCallCount: z.ZodDefault<z.ZodNumber>;\n runModelCallCount: z.ZodDefault<z.ZodNumber>;\n}, \"strip\", z.ZodTypeAny, {\n threadModelCallCount: number;\n runModelCallCount: number;\n}, {\n threadModelCallCount?: number | undefined;\n runModelCallCount?: number | undefined;\n}>, z.ZodObject<{\n /**\n * The maximum number of model calls allowed per thread.\n */\n threadLimit: z.ZodOptional<z.ZodNumber>;\n /**\n * The maximum number of model calls allowed per run.\n */\n runLimit: z.ZodOptional<z.ZodNumber>;\n /**\n * The behavior to take when the limit is exceeded.\n * - \"error\" will throw an error and stop the agent.\n * - \"end\" will end the agent.\n * @default \"end\"\n */\n exitBehavior: z.ZodOptional<z.ZodEnum<[\"error\", \"end\"]>>;\n}, \"strip\", z.ZodTypeAny, {\n threadLimit?: number | undefined;\n runLimit?: number | undefined;\n exitBehavior?: \"end\" | \"error\" | undefined;\n}, {\n threadLimit?: number | undefined;\n runLimit?: number | undefined;\n exitBehavior?: \"end\" | \"error\" | undefined;\n}>, any>;\nexport {};\n"],"mappings":";;;;;cAEcE,eAAeF,CAAAA,CAAEO;;;AADoC;EAyBjE,WAAA,EApBeP,CAAAA,CAAEI,WAoBjB,CApB6BJ,CAAAA,CAAEG,SAoB/B,CAAA;EAAA;;;EAhBqC,QAAvBC,EAAFJ,CAAAA,CAAEI,WAAAA,CAAYJ,CAAAA,CAAEG,SAAdC,CAAAA;EAAW;;;;AARa;AAyBxC;EAA0C,YAAA,EAVxBJ,CAAAA,CAAEI,WAUsB,CAVVJ,CAAAA,CAAEK,OAUQ,CAAA,CAAA,OAAA,EAAA,KAAA,CAAA,CAAA,CAAA;CAAA,EAAA,OAAuCH,EATrEF,CAAAA,CAAEM,UASmEJ,EAAAA;EAAa,WAAzCD,CAAAA,EAAAA,MAAAA,GAAAA,SAAAA;EAAoB,QAA5BQ,CAAAA,EAAAA,MAAAA,GAAAA,SAAAA;EAAO,YAAA,CAAA,EAAA,KAAA,GAAA,OAAA,GAAA,SAAA;AA6DpD,CAAA,EAAA;EAAgD,WAAA,CAAA,EAAA,MAAA,GAAA,SAAA;EAAA,QAAqBD,CAAAA,EAAAA,MAAAA,GAAAA,SAAAA;EAA8B,YAC1DL,CAAAA,EAAAA,KAAAA,GAAAA,OAAAA,GAAAA,SAAAA;CAAS,CAAA;AACdH,KA/DxBQ,8BAAAA,GAAiCC,OA+DPN,CA/DeF,oBA+DfE,CAAAA,OA/D2CD,aA+D3CC,CAAAA,CAAAA;;;;;;;;;;;;;AAFoG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAAlHO,wBAAAA,qBAA6CF,iCAStD,gBAT4HR,CAAAA,CAAEO;wBACnHP,CAAAA,CAAEW,WAAWX,CAAAA,CAAEG;qBAClBH,CAAAA,CAAEW,WAAWX,CAAAA,CAAEG;YAC1BH,CAAAA,CAAEM;;;;;;IAMVN,CAAAA,CAAEO;;;;eAIWP,CAAAA,CAAEI,YAAYJ,CAAAA,CAAEG;;;;YAInBH,CAAAA,CAAEI,YAAYJ,CAAAA,CAAEG;;;;;;;gBAOZH,CAAAA,CAAEI,YAAYJ,CAAAA,CAAEK;YACtBL,CAAAA,CAAEM"}
@@ -7,7 +7,7 @@ const DEFAULT_EXIT_BEHAVIOR = "end";
7
7
  const contextSchema = z.object({
8
8
  threadLimit: z.number().optional(),
9
9
  runLimit: z.number().optional(),
10
- exitBehavior: z.enum(["throw", "end"]).optional()
10
+ exitBehavior: z.enum(["error", "end"]).optional()
11
11
  });
12
12
  /**
13
13
  * Middleware state schema to track the number of model calls made at the thread and run level.
@@ -101,7 +101,11 @@ function modelCallLimitMiddleware(middlewareOptions) {
101
101
  beforeModel: {
102
102
  canJumpTo: ["end"],
103
103
  hook: (state, runtime) => {
104
- const exitBehavior = runtime.context.exitBehavior ?? middlewareOptions?.exitBehavior ?? DEFAULT_EXIT_BEHAVIOR;
104
+ let exitBehavior = runtime.context.exitBehavior ?? middlewareOptions?.exitBehavior ?? DEFAULT_EXIT_BEHAVIOR;
105
+ if (exitBehavior === "throw") {
106
+ console.warn("The 'throw' exit behavior is deprecated. Please use 'error' instead.");
107
+ exitBehavior = "error";
108
+ }
105
109
  const threadLimit = runtime.context.threadLimit ?? middlewareOptions?.threadLimit;
106
110
  const runLimit = runtime.context.runLimit ?? middlewareOptions?.runLimit;
107
111
  const threadCount = state.threadModelCallCount;
@@ -1 +1 @@
1
- {"version":3,"file":"modelCallLimit.js","names":["exceededHint: string[]","middlewareOptions?: ModelCallLimitMiddlewareConfig"],"sources":["../../../src/agents/middleware/modelCallLimit.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { AIMessage } from \"@langchain/core/messages\";\nimport { InferInteropZodInput } from \"@langchain/core/utils/types\";\n\nimport { createMiddleware } from \"../middleware.js\";\n\nconst DEFAULT_EXIT_BEHAVIOR = \"end\";\n\nconst contextSchema = z.object({\n /**\n * The maximum number of model calls allowed per thread.\n */\n threadLimit: z.number().optional(),\n /**\n * The maximum number of model calls allowed per run.\n */\n runLimit: z.number().optional(),\n /**\n * The behavior to take when the limit is exceeded.\n * - \"throw\" will throw an error and stop the agent.\n * - \"end\" will end the agent.\n * @default \"end\"\n */\n exitBehavior: z.enum([\"throw\", \"end\"]).optional(),\n});\nexport type ModelCallLimitMiddlewareConfig = Partial<\n InferInteropZodInput<typeof contextSchema>\n>;\n\n/**\n * Middleware state schema to track the number of model calls made at the thread and run level.\n */\nconst stateSchema = z.object({\n threadModelCallCount: z.number().default(0),\n runModelCallCount: z.number().default(0),\n});\n\n/**\n * Error thrown when the model call limit is exceeded.\n *\n * @param threadLimit - The maximum number of model calls allowed per thread.\n * @param runLimit - The maximum number of model calls allowed per run.\n * @param threadCount - The number of model calls made at the thread level.\n * @param runCount - The number of model calls made at the run level.\n */\nclass ModelCallLimitMiddlewareError extends Error {\n constructor({\n threadLimit,\n runLimit,\n threadCount,\n runCount,\n }: {\n threadLimit?: number;\n runLimit?: number;\n threadCount?: number;\n runCount?: number;\n }) {\n const exceededHint: string[] = [];\n if (typeof threadLimit === \"number\" && typeof threadCount === \"number\") {\n exceededHint.push(\n `thread level call limit reached with ${threadCount} model calls`\n );\n }\n if (typeof runLimit === \"number\" && typeof runCount === \"number\") {\n exceededHint.push(\n `run level call limit reached with ${runCount} model calls`\n );\n }\n super(\n `Model call limits exceeded${\n exceededHint.length > 0 ? `: ${exceededHint.join(\", \")}` : \"\"\n }`\n );\n this.name = \"ModelCallLimitMiddlewareError\";\n }\n}\n\n/**\n * Creates a middleware to limit the number of model calls at both thread and run levels.\n *\n * This middleware helps prevent excessive model API calls by enforcing limits on how many\n * times the model can be invoked. It supports two types of limits:\n *\n * - **Thread-level limit**: Restricts the total number of model calls across an entire conversation thread\n * - **Run-level limit**: Restricts the number of model calls within a single agent run/invocation\n *\n * ## How It Works\n *\n * The middleware intercepts model requests before they are sent and checks the current call counts\n * against the configured limits. If either limit is exceeded, it throws a `ModelCallLimitMiddlewareError`\n * to stop execution and prevent further API calls.\n *\n * ## Use Cases\n *\n * - **Cost Control**: Prevent runaway costs from excessive model calls in production\n * - **Testing**: Ensure agents don't make too many calls during development/testing\n * - **Safety**: Limit potential infinite loops or recursive agent behaviors\n * - **Rate Limiting**: Enforce organizational policies on model usage per conversation\n *\n * @param middlewareOptions - Configuration options for the call limits\n * @param middlewareOptions.threadLimit - Maximum number of model calls allowed per thread (optional)\n * @param middlewareOptions.runLimit - Maximum number of model calls allowed per run (optional)\n *\n * @returns A middleware instance that can be passed to `createAgent`\n *\n * @throws {ModelCallLimitMiddlewareError} When either the thread or run limit is exceeded\n *\n * @example\n * ```typescript\n * import { createAgent, modelCallLimitMiddleware } from \"langchain\";\n *\n * // Limit to 10 calls per thread and 3 calls per run\n * const agent = createAgent({\n * model: \"openai:gpt-4o-mini\",\n * tools: [myTool],\n * middleware: [\n * modelCallLimitMiddleware({\n * threadLimit: 10,\n * runLimit: 3\n * })\n * ]\n * });\n * ```\n *\n * @example\n * ```typescript\n * // Limits can also be configured at runtime via context\n * const result = await agent.invoke(\n * { messages: [\"Hello\"] },\n * {\n * configurable: {\n * threadLimit: 5 // Override the default limit for this run\n * }\n * }\n * );\n * ```\n */\nexport function modelCallLimitMiddleware(\n middlewareOptions?: ModelCallLimitMiddlewareConfig\n) {\n return createMiddleware({\n name: \"ModelCallLimitMiddleware\",\n contextSchema,\n stateSchema,\n beforeModel: {\n canJumpTo: [\"end\"],\n hook: (state, runtime) => {\n const exitBehavior =\n runtime.context.exitBehavior ??\n middlewareOptions?.exitBehavior ??\n DEFAULT_EXIT_BEHAVIOR;\n const threadLimit =\n runtime.context.threadLimit ?? middlewareOptions?.threadLimit;\n const runLimit =\n runtime.context.runLimit ?? middlewareOptions?.runLimit;\n\n const threadCount = state.threadModelCallCount;\n const runCount = state.runModelCallCount;\n if (typeof threadLimit === \"number\" && threadLimit <= threadCount) {\n const error = new ModelCallLimitMiddlewareError({\n threadLimit,\n threadCount,\n });\n if (exitBehavior === \"end\") {\n return {\n jumpTo: \"end\",\n messages: [new AIMessage(error.message)],\n };\n }\n\n throw error;\n }\n if (typeof runLimit === \"number\" && runLimit <= runCount) {\n const error = new ModelCallLimitMiddlewareError({\n runLimit,\n runCount,\n });\n if (exitBehavior === \"end\") {\n return {\n jumpTo: \"end\",\n messages: [new AIMessage(error.message)],\n };\n }\n\n throw error;\n }\n\n return state;\n },\n },\n afterModel: (state) => ({\n runModelCallCount: state.runModelCallCount + 1,\n threadModelCallCount: state.threadModelCallCount + 1,\n }),\n afterAgent: () => ({\n runModelCallCount: 0,\n }),\n });\n}\n"],"mappings":";;;;;AAMA,MAAM,wBAAwB;AAE9B,MAAM,gBAAgB,EAAE,OAAO;CAI7B,aAAa,EAAE,QAAQ,CAAC,UAAU;CAIlC,UAAU,EAAE,QAAQ,CAAC,UAAU;CAO/B,cAAc,EAAE,KAAK,CAAC,SAAS,KAAM,EAAC,CAAC,UAAU;AAClD,EAAC;;;;AAQF,MAAM,cAAc,EAAE,OAAO;CAC3B,sBAAsB,EAAE,QAAQ,CAAC,QAAQ,EAAE;CAC3C,mBAAmB,EAAE,QAAQ,CAAC,QAAQ,EAAE;AACzC,EAAC;;;;;;;;;AAUF,IAAM,gCAAN,cAA4C,MAAM;CAChD,YAAY,EACV,aACA,UACA,aACA,UAMD,EAAE;EACD,MAAMA,eAAyB,CAAE;AACjC,MAAI,OAAO,gBAAgB,YAAY,OAAO,gBAAgB,UAC5D,aAAa,KACX,CAAC,qCAAqC,EAAE,YAAY,YAAY,CAAC,CAClE;AAEH,MAAI,OAAO,aAAa,YAAY,OAAO,aAAa,UACtD,aAAa,KACX,CAAC,kCAAkC,EAAE,SAAS,YAAY,CAAC,CAC5D;EAEH,MACE,CAAC,0BAA0B,EACzB,aAAa,SAAS,IAAI,CAAC,EAAE,EAAE,aAAa,KAAK,KAAK,EAAE,GAAG,IAC3D,CACH;EACD,KAAK,OAAO;CACb;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8DD,SAAgB,yBACdC,mBACA;AACA,QAAO,iBAAiB;EACtB,MAAM;EACN;EACA;EACA,aAAa;GACX,WAAW,CAAC,KAAM;GAClB,MAAM,CAAC,OAAO,YAAY;IACxB,MAAM,eACJ,QAAQ,QAAQ,gBAChB,mBAAmB,gBACnB;IACF,MAAM,cACJ,QAAQ,QAAQ,eAAe,mBAAmB;IACpD,MAAM,WACJ,QAAQ,QAAQ,YAAY,mBAAmB;IAEjD,MAAM,cAAc,MAAM;IAC1B,MAAM,WAAW,MAAM;AACvB,QAAI,OAAO,gBAAgB,YAAY,eAAe,aAAa;KACjE,MAAM,QAAQ,IAAI,8BAA8B;MAC9C;MACA;KACD;AACD,SAAI,iBAAiB,MACnB,QAAO;MACL,QAAQ;MACR,UAAU,CAAC,IAAI,UAAU,MAAM,QAAS;KACzC;AAGH,WAAM;IACP;AACD,QAAI,OAAO,aAAa,YAAY,YAAY,UAAU;KACxD,MAAM,QAAQ,IAAI,8BAA8B;MAC9C;MACA;KACD;AACD,SAAI,iBAAiB,MACnB,QAAO;MACL,QAAQ;MACR,UAAU,CAAC,IAAI,UAAU,MAAM,QAAS;KACzC;AAGH,WAAM;IACP;AAED,WAAO;GACR;EACF;EACD,YAAY,CAAC,WAAW;GACtB,mBAAmB,MAAM,oBAAoB;GAC7C,sBAAsB,MAAM,uBAAuB;EACpD;EACD,YAAY,OAAO,EACjB,mBAAmB,EACpB;CACF,EAAC;AACH"}
1
+ {"version":3,"file":"modelCallLimit.js","names":["exceededHint: string[]","middlewareOptions?: ModelCallLimitMiddlewareConfig"],"sources":["../../../src/agents/middleware/modelCallLimit.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { AIMessage } from \"@langchain/core/messages\";\nimport { InferInteropZodInput } from \"@langchain/core/utils/types\";\n\nimport { createMiddleware } from \"../middleware.js\";\n\nconst DEFAULT_EXIT_BEHAVIOR = \"end\";\n\nconst contextSchema = z.object({\n /**\n * The maximum number of model calls allowed per thread.\n */\n threadLimit: z.number().optional(),\n /**\n * The maximum number of model calls allowed per run.\n */\n runLimit: z.number().optional(),\n /**\n * The behavior to take when the limit is exceeded.\n * - \"error\" will throw an error and stop the agent.\n * - \"end\" will end the agent.\n * @default \"end\"\n */\n exitBehavior: z.enum([\"error\", \"end\"]).optional(),\n});\nexport type ModelCallLimitMiddlewareConfig = Partial<\n InferInteropZodInput<typeof contextSchema>\n>;\n\n/**\n * Middleware state schema to track the number of model calls made at the thread and run level.\n */\nconst stateSchema = z.object({\n threadModelCallCount: z.number().default(0),\n runModelCallCount: z.number().default(0),\n});\n\n/**\n * Error thrown when the model call limit is exceeded.\n *\n * @param threadLimit - The maximum number of model calls allowed per thread.\n * @param runLimit - The maximum number of model calls allowed per run.\n * @param threadCount - The number of model calls made at the thread level.\n * @param runCount - The number of model calls made at the run level.\n */\nclass ModelCallLimitMiddlewareError extends Error {\n constructor({\n threadLimit,\n runLimit,\n threadCount,\n runCount,\n }: {\n threadLimit?: number;\n runLimit?: number;\n threadCount?: number;\n runCount?: number;\n }) {\n const exceededHint: string[] = [];\n if (typeof threadLimit === \"number\" && typeof threadCount === \"number\") {\n exceededHint.push(\n `thread level call limit reached with ${threadCount} model calls`\n );\n }\n if (typeof runLimit === \"number\" && typeof runCount === \"number\") {\n exceededHint.push(\n `run level call limit reached with ${runCount} model calls`\n );\n }\n super(\n `Model call limits exceeded${\n exceededHint.length > 0 ? `: ${exceededHint.join(\", \")}` : \"\"\n }`\n );\n this.name = \"ModelCallLimitMiddlewareError\";\n }\n}\n\n/**\n * Creates a middleware to limit the number of model calls at both thread and run levels.\n *\n * This middleware helps prevent excessive model API calls by enforcing limits on how many\n * times the model can be invoked. It supports two types of limits:\n *\n * - **Thread-level limit**: Restricts the total number of model calls across an entire conversation thread\n * - **Run-level limit**: Restricts the number of model calls within a single agent run/invocation\n *\n * ## How It Works\n *\n * The middleware intercepts model requests before they are sent and checks the current call counts\n * against the configured limits. If either limit is exceeded, it throws a `ModelCallLimitMiddlewareError`\n * to stop execution and prevent further API calls.\n *\n * ## Use Cases\n *\n * - **Cost Control**: Prevent runaway costs from excessive model calls in production\n * - **Testing**: Ensure agents don't make too many calls during development/testing\n * - **Safety**: Limit potential infinite loops or recursive agent behaviors\n * - **Rate Limiting**: Enforce organizational policies on model usage per conversation\n *\n * @param middlewareOptions - Configuration options for the call limits\n * @param middlewareOptions.threadLimit - Maximum number of model calls allowed per thread (optional)\n * @param middlewareOptions.runLimit - Maximum number of model calls allowed per run (optional)\n *\n * @returns A middleware instance that can be passed to `createAgent`\n *\n * @throws {ModelCallLimitMiddlewareError} When either the thread or run limit is exceeded\n *\n * @example\n * ```typescript\n * import { createAgent, modelCallLimitMiddleware } from \"langchain\";\n *\n * // Limit to 10 calls per thread and 3 calls per run\n * const agent = createAgent({\n * model: \"openai:gpt-4o-mini\",\n * tools: [myTool],\n * middleware: [\n * modelCallLimitMiddleware({\n * threadLimit: 10,\n * runLimit: 3\n * })\n * ]\n * });\n * ```\n *\n * @example\n * ```typescript\n * // Limits can also be configured at runtime via context\n * const result = await agent.invoke(\n * { messages: [\"Hello\"] },\n * {\n * configurable: {\n * threadLimit: 5 // Override the default limit for this run\n * }\n * }\n * );\n * ```\n */\nexport function modelCallLimitMiddleware(\n middlewareOptions?: ModelCallLimitMiddlewareConfig\n) {\n return createMiddleware({\n name: \"ModelCallLimitMiddleware\",\n contextSchema,\n stateSchema,\n beforeModel: {\n canJumpTo: [\"end\"],\n hook: (state, runtime) => {\n let exitBehavior =\n runtime.context.exitBehavior ??\n middlewareOptions?.exitBehavior ??\n DEFAULT_EXIT_BEHAVIOR;\n\n // @ts-expect-error - throw is deprecated\n if (exitBehavior === \"throw\") {\n console.warn(\n \"The 'throw' exit behavior is deprecated. Please use 'error' instead.\"\n );\n exitBehavior = \"error\";\n }\n\n const threadLimit =\n runtime.context.threadLimit ?? middlewareOptions?.threadLimit;\n const runLimit =\n runtime.context.runLimit ?? middlewareOptions?.runLimit;\n\n const threadCount = state.threadModelCallCount;\n const runCount = state.runModelCallCount;\n if (typeof threadLimit === \"number\" && threadLimit <= threadCount) {\n const error = new ModelCallLimitMiddlewareError({\n threadLimit,\n threadCount,\n });\n if (exitBehavior === \"end\") {\n return {\n jumpTo: \"end\",\n messages: [new AIMessage(error.message)],\n };\n }\n\n throw error;\n }\n if (typeof runLimit === \"number\" && runLimit <= runCount) {\n const error = new ModelCallLimitMiddlewareError({\n runLimit,\n runCount,\n });\n if (exitBehavior === \"end\") {\n return {\n jumpTo: \"end\",\n messages: [new AIMessage(error.message)],\n };\n }\n\n throw error;\n }\n\n return state;\n },\n },\n afterModel: (state) => ({\n runModelCallCount: state.runModelCallCount + 1,\n threadModelCallCount: state.threadModelCallCount + 1,\n }),\n afterAgent: () => ({\n runModelCallCount: 0,\n }),\n });\n}\n"],"mappings":";;;;;AAMA,MAAM,wBAAwB;AAE9B,MAAM,gBAAgB,EAAE,OAAO;CAI7B,aAAa,EAAE,QAAQ,CAAC,UAAU;CAIlC,UAAU,EAAE,QAAQ,CAAC,UAAU;CAO/B,cAAc,EAAE,KAAK,CAAC,SAAS,KAAM,EAAC,CAAC,UAAU;AAClD,EAAC;;;;AAQF,MAAM,cAAc,EAAE,OAAO;CAC3B,sBAAsB,EAAE,QAAQ,CAAC,QAAQ,EAAE;CAC3C,mBAAmB,EAAE,QAAQ,CAAC,QAAQ,EAAE;AACzC,EAAC;;;;;;;;;AAUF,IAAM,gCAAN,cAA4C,MAAM;CAChD,YAAY,EACV,aACA,UACA,aACA,UAMD,EAAE;EACD,MAAMA,eAAyB,CAAE;AACjC,MAAI,OAAO,gBAAgB,YAAY,OAAO,gBAAgB,UAC5D,aAAa,KACX,CAAC,qCAAqC,EAAE,YAAY,YAAY,CAAC,CAClE;AAEH,MAAI,OAAO,aAAa,YAAY,OAAO,aAAa,UACtD,aAAa,KACX,CAAC,kCAAkC,EAAE,SAAS,YAAY,CAAC,CAC5D;EAEH,MACE,CAAC,0BAA0B,EACzB,aAAa,SAAS,IAAI,CAAC,EAAE,EAAE,aAAa,KAAK,KAAK,EAAE,GAAG,IAC3D,CACH;EACD,KAAK,OAAO;CACb;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8DD,SAAgB,yBACdC,mBACA;AACA,QAAO,iBAAiB;EACtB,MAAM;EACN;EACA;EACA,aAAa;GACX,WAAW,CAAC,KAAM;GAClB,MAAM,CAAC,OAAO,YAAY;IACxB,IAAI,eACF,QAAQ,QAAQ,gBAChB,mBAAmB,gBACnB;AAGF,QAAI,iBAAiB,SAAS;KAC5B,QAAQ,KACN,uEACD;KACD,eAAe;IAChB;IAED,MAAM,cACJ,QAAQ,QAAQ,eAAe,mBAAmB;IACpD,MAAM,WACJ,QAAQ,QAAQ,YAAY,mBAAmB;IAEjD,MAAM,cAAc,MAAM;IAC1B,MAAM,WAAW,MAAM;AACvB,QAAI,OAAO,gBAAgB,YAAY,eAAe,aAAa;KACjE,MAAM,QAAQ,IAAI,8BAA8B;MAC9C;MACA;KACD;AACD,SAAI,iBAAiB,MACnB,QAAO;MACL,QAAQ;MACR,UAAU,CAAC,IAAI,UAAU,MAAM,QAAS;KACzC;AAGH,WAAM;IACP;AACD,QAAI,OAAO,aAAa,YAAY,YAAY,UAAU;KACxD,MAAM,QAAQ,IAAI,8BAA8B;MAC9C;MACA;KACD;AACD,SAAI,iBAAiB,MACnB,QAAO;MACL,QAAQ;MACR,UAAU,CAAC,IAAI,UAAU,MAAM,QAAS;KACzC;AAGH,WAAM;IACP;AAED,WAAO;GACR;EACF;EACD,YAAY,CAAC,WAAW;GACtB,mBAAmB,MAAM,oBAAoB;GAC7C,sBAAsB,MAAM,uBAAuB;EACpD;EACD,YAAY,OAAO,EACjB,mBAAmB,EACpB;CACF,EAAC;AACH"}
@@ -14,10 +14,10 @@ const require_middleware = require('../middleware.cjs');
14
14
  * import { createAgent, modelFallbackMiddleware } from "langchain";
15
15
  *
16
16
  * // Create middleware with fallback models (not including primary)
17
- * const fallback = modelFallbackMiddleware({
17
+ * const fallback = modelFallbackMiddleware(
18
18
  * "openai:gpt-4o-mini", // First fallback
19
19
  * "anthropic:claude-sonnet-4-5-20250929", // Second fallback
20
- * });
20
+ * );
21
21
  *
22
22
  * const agent = createAgent({
23
23
  * model: "openai:gpt-4o", // Primary model
@@ -1 +1 @@
1
- {"version":3,"file":"modelFallback.cjs","names":["createMiddleware","initChatModel"],"sources":["../../../src/agents/middleware/modelFallback.ts"],"sourcesContent":["import type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport { initChatModel } from \"../../chat_models/universal.js\";\nimport type { AgentMiddleware } from \"./types.js\";\nimport { createMiddleware } from \"../middleware.js\";\n\n/**\n * Middleware that provides automatic model fallback on errors.\n *\n * This middleware attempts to retry failed model calls with alternative models\n * in sequence. When a model call fails, it tries the next model in the fallback\n * list until either a call succeeds or all models have been exhausted.\n *\n * @example\n * ```ts\n * import { createAgent, modelFallbackMiddleware } from \"langchain\";\n *\n * // Create middleware with fallback models (not including primary)\n * const fallback = modelFallbackMiddleware({\n * \"openai:gpt-4o-mini\", // First fallback\n * \"anthropic:claude-sonnet-4-5-20250929\", // Second fallback\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\", // Primary model\n * middleware: [fallback],\n * tools: [],\n * });\n *\n * // If gpt-4o fails, automatically tries gpt-4o-mini, then claude\n * const result = await agent.invoke({\n * messages: [{ role: \"user\", content: \"Hello\" }]\n * });\n * ```\n *\n * @param fallbackModels - The fallback models to try, in order.\n * @returns A middleware instance that handles model failures with fallbacks\n */\nexport function modelFallbackMiddleware(\n /**\n * The fallback models to try, in order.\n */\n ...fallbackModels: (string | LanguageModelLike)[]\n): AgentMiddleware {\n return createMiddleware({\n name: \"modelFallbackMiddleware\",\n wrapModelCall: async (request, handler) => {\n /**\n * Try the primary model first\n */\n try {\n return await handler(request);\n } catch (error) {\n /**\n * If primary model fails, try fallback models in sequence\n */\n for (let i = 0; i < fallbackModels.length; i++) {\n try {\n const fallbackModel = fallbackModels[i];\n const model =\n typeof fallbackModel === \"string\"\n ? await initChatModel(fallbackModel)\n : fallbackModel;\n\n return await handler({\n ...request,\n model,\n });\n } catch (fallbackError) {\n /**\n * If this is the last fallback, throw the error\n */\n if (i === fallbackModels.length - 1) {\n throw fallbackError;\n }\n // Otherwise, continue to next fallback\n }\n }\n /**\n * If no fallbacks were provided, re-throw the original error\n */\n throw error;\n }\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,wBAId,GAAG,gBACc;AACjB,QAAOA,oCAAiB;EACtB,MAAM;EACN,eAAe,OAAO,SAAS,YAAY;;;;AAIzC,OAAI;AACF,WAAO,MAAM,QAAQ,QAAQ;GAC9B,SAAQ,OAAO;;;;AAId,SAAK,IAAI,IAAI,GAAG,IAAI,eAAe,QAAQ,IACzC,KAAI;KACF,MAAM,gBAAgB,eAAe;KACrC,MAAM,QACJ,OAAO,kBAAkB,WACrB,MAAMC,4CAAc,cAAc,GAClC;AAEN,YAAO,MAAM,QAAQ;MACnB,GAAG;MACH;KACD,EAAC;IACH,SAAQ,eAAe;;;;AAItB,SAAI,MAAM,eAAe,SAAS,EAChC,OAAM;IAGT;;;;AAKH,UAAM;GACP;EACF;CACF,EAAC;AACH"}
1
+ {"version":3,"file":"modelFallback.cjs","names":["createMiddleware","initChatModel"],"sources":["../../../src/agents/middleware/modelFallback.ts"],"sourcesContent":["import type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport { initChatModel } from \"../../chat_models/universal.js\";\nimport type { AgentMiddleware } from \"./types.js\";\nimport { createMiddleware } from \"../middleware.js\";\n\n/**\n * Middleware that provides automatic model fallback on errors.\n *\n * This middleware attempts to retry failed model calls with alternative models\n * in sequence. When a model call fails, it tries the next model in the fallback\n * list until either a call succeeds or all models have been exhausted.\n *\n * @example\n * ```ts\n * import { createAgent, modelFallbackMiddleware } from \"langchain\";\n *\n * // Create middleware with fallback models (not including primary)\n * const fallback = modelFallbackMiddleware(\n * \"openai:gpt-4o-mini\", // First fallback\n * \"anthropic:claude-sonnet-4-5-20250929\", // Second fallback\n * );\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\", // Primary model\n * middleware: [fallback],\n * tools: [],\n * });\n *\n * // If gpt-4o fails, automatically tries gpt-4o-mini, then claude\n * const result = await agent.invoke({\n * messages: [{ role: \"user\", content: \"Hello\" }]\n * });\n * ```\n *\n * @param fallbackModels - The fallback models to try, in order.\n * @returns A middleware instance that handles model failures with fallbacks\n */\nexport function modelFallbackMiddleware(\n /**\n * The fallback models to try, in order.\n */\n ...fallbackModels: (string | LanguageModelLike)[]\n): AgentMiddleware {\n return createMiddleware({\n name: \"modelFallbackMiddleware\",\n wrapModelCall: async (request, handler) => {\n /**\n * Try the primary model first\n */\n try {\n return await handler(request);\n } catch (error) {\n /**\n * If primary model fails, try fallback models in sequence\n */\n for (let i = 0; i < fallbackModels.length; i++) {\n try {\n const fallbackModel = fallbackModels[i];\n const model =\n typeof fallbackModel === \"string\"\n ? await initChatModel(fallbackModel)\n : fallbackModel;\n\n return await handler({\n ...request,\n model,\n });\n } catch (fallbackError) {\n /**\n * If this is the last fallback, throw the error\n */\n if (i === fallbackModels.length - 1) {\n throw fallbackError;\n }\n // Otherwise, continue to next fallback\n }\n }\n /**\n * If no fallbacks were provided, re-throw the original error\n */\n throw error;\n }\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,wBAId,GAAG,gBACc;AACjB,QAAOA,oCAAiB;EACtB,MAAM;EACN,eAAe,OAAO,SAAS,YAAY;;;;AAIzC,OAAI;AACF,WAAO,MAAM,QAAQ,QAAQ;GAC9B,SAAQ,OAAO;;;;AAId,SAAK,IAAI,IAAI,GAAG,IAAI,eAAe,QAAQ,IACzC,KAAI;KACF,MAAM,gBAAgB,eAAe;KACrC,MAAM,QACJ,OAAO,kBAAkB,WACrB,MAAMC,4CAAc,cAAc,GAClC;AAEN,YAAO,MAAM,QAAQ;MACnB,GAAG;MACH;KACD,EAAC;IACH,SAAQ,eAAe;;;;AAItB,SAAI,MAAM,eAAe,SAAS,EAChC,OAAM;IAGT;;;;AAKH,UAAM;GACP;EACF;CACF,EAAC;AACH"}
@@ -15,10 +15,10 @@ import { LanguageModelLike } from "@langchain/core/language_models/base";
15
15
  * import { createAgent, modelFallbackMiddleware } from "langchain";
16
16
  *
17
17
  * // Create middleware with fallback models (not including primary)
18
- * const fallback = modelFallbackMiddleware({
18
+ * const fallback = modelFallbackMiddleware(
19
19
  * "openai:gpt-4o-mini", // First fallback
20
20
  * "anthropic:claude-sonnet-4-5-20250929", // Second fallback
21
- * });
21
+ * );
22
22
  *
23
23
  * const agent = createAgent({
24
24
  * model: "openai:gpt-4o", // Primary model
@@ -1 +1 @@
1
- {"version":3,"file":"modelFallback.d.cts","names":["LanguageModelLike","AgentMiddleware","modelFallbackMiddleware"],"sources":["../../../src/agents/middleware/modelFallback.d.ts"],"sourcesContent":["import type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport type { AgentMiddleware } from \"./types.js\";\n/**\n * Middleware that provides automatic model fallback on errors.\n *\n * This middleware attempts to retry failed model calls with alternative models\n * in sequence. When a model call fails, it tries the next model in the fallback\n * list until either a call succeeds or all models have been exhausted.\n *\n * @example\n * ```ts\n * import { createAgent, modelFallbackMiddleware } from \"langchain\";\n *\n * // Create middleware with fallback models (not including primary)\n * const fallback = modelFallbackMiddleware({\n * \"openai:gpt-4o-mini\", // First fallback\n * \"anthropic:claude-sonnet-4-5-20250929\", // Second fallback\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\", // Primary model\n * middleware: [fallback],\n * tools: [],\n * });\n *\n * // If gpt-4o fails, automatically tries gpt-4o-mini, then claude\n * const result = await agent.invoke({\n * messages: [{ role: \"user\", content: \"Hello\" }]\n * });\n * ```\n *\n * @param fallbackModels - The fallback models to try, in order.\n * @returns A middleware instance that handles model failures with fallbacks\n */\nexport declare function modelFallbackMiddleware(\n/**\n * The fallback models to try, in order.\n */\n...fallbackModels: (string | LanguageModelLike)[]): AgentMiddleware;\n"],"mappings":";;;;;;;AAkCA;;;;AAImE;;;;;;;;;;;;;;;;;;;;;;;;;;iBAJ3CE,uBAAAA;;;;6BAIKF,uBAAuBC"}
1
+ {"version":3,"file":"modelFallback.d.cts","names":["LanguageModelLike","AgentMiddleware","modelFallbackMiddleware"],"sources":["../../../src/agents/middleware/modelFallback.d.ts"],"sourcesContent":["import type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport type { AgentMiddleware } from \"./types.js\";\n/**\n * Middleware that provides automatic model fallback on errors.\n *\n * This middleware attempts to retry failed model calls with alternative models\n * in sequence. When a model call fails, it tries the next model in the fallback\n * list until either a call succeeds or all models have been exhausted.\n *\n * @example\n * ```ts\n * import { createAgent, modelFallbackMiddleware } from \"langchain\";\n *\n * // Create middleware with fallback models (not including primary)\n * const fallback = modelFallbackMiddleware(\n * \"openai:gpt-4o-mini\", // First fallback\n * \"anthropic:claude-sonnet-4-5-20250929\", // Second fallback\n * );\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\", // Primary model\n * middleware: [fallback],\n * tools: [],\n * });\n *\n * // If gpt-4o fails, automatically tries gpt-4o-mini, then claude\n * const result = await agent.invoke({\n * messages: [{ role: \"user\", content: \"Hello\" }]\n * });\n * ```\n *\n * @param fallbackModels - The fallback models to try, in order.\n * @returns A middleware instance that handles model failures with fallbacks\n */\nexport declare function modelFallbackMiddleware(\n/**\n * The fallback models to try, in order.\n */\n...fallbackModels: (string | LanguageModelLike)[]): AgentMiddleware;\n"],"mappings":";;;;;;;AAkCA;;;;AAImE;;;;;;;;;;;;;;;;;;;;;;;;;;iBAJ3CE,uBAAAA;;;;6BAIKF,uBAAuBC"}
@@ -15,10 +15,10 @@ import { LanguageModelLike } from "@langchain/core/language_models/base";
15
15
  * import { createAgent, modelFallbackMiddleware } from "langchain";
16
16
  *
17
17
  * // Create middleware with fallback models (not including primary)
18
- * const fallback = modelFallbackMiddleware({
18
+ * const fallback = modelFallbackMiddleware(
19
19
  * "openai:gpt-4o-mini", // First fallback
20
20
  * "anthropic:claude-sonnet-4-5-20250929", // Second fallback
21
- * });
21
+ * );
22
22
  *
23
23
  * const agent = createAgent({
24
24
  * model: "openai:gpt-4o", // Primary model
@@ -1 +1 @@
1
- {"version":3,"file":"modelFallback.d.ts","names":["LanguageModelLike","AgentMiddleware","modelFallbackMiddleware"],"sources":["../../../src/agents/middleware/modelFallback.d.ts"],"sourcesContent":["import type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport type { AgentMiddleware } from \"./types.js\";\n/**\n * Middleware that provides automatic model fallback on errors.\n *\n * This middleware attempts to retry failed model calls with alternative models\n * in sequence. When a model call fails, it tries the next model in the fallback\n * list until either a call succeeds or all models have been exhausted.\n *\n * @example\n * ```ts\n * import { createAgent, modelFallbackMiddleware } from \"langchain\";\n *\n * // Create middleware with fallback models (not including primary)\n * const fallback = modelFallbackMiddleware({\n * \"openai:gpt-4o-mini\", // First fallback\n * \"anthropic:claude-sonnet-4-5-20250929\", // Second fallback\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\", // Primary model\n * middleware: [fallback],\n * tools: [],\n * });\n *\n * // If gpt-4o fails, automatically tries gpt-4o-mini, then claude\n * const result = await agent.invoke({\n * messages: [{ role: \"user\", content: \"Hello\" }]\n * });\n * ```\n *\n * @param fallbackModels - The fallback models to try, in order.\n * @returns A middleware instance that handles model failures with fallbacks\n */\nexport declare function modelFallbackMiddleware(\n/**\n * The fallback models to try, in order.\n */\n...fallbackModels: (string | LanguageModelLike)[]): AgentMiddleware;\n"],"mappings":";;;;;;;AAkCA;;;;AAImE;;;;;;;;;;;;;;;;;;;;;;;;;;iBAJ3CE,uBAAAA;;;;6BAIKF,uBAAuBC"}
1
+ {"version":3,"file":"modelFallback.d.ts","names":["LanguageModelLike","AgentMiddleware","modelFallbackMiddleware"],"sources":["../../../src/agents/middleware/modelFallback.d.ts"],"sourcesContent":["import type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport type { AgentMiddleware } from \"./types.js\";\n/**\n * Middleware that provides automatic model fallback on errors.\n *\n * This middleware attempts to retry failed model calls with alternative models\n * in sequence. When a model call fails, it tries the next model in the fallback\n * list until either a call succeeds or all models have been exhausted.\n *\n * @example\n * ```ts\n * import { createAgent, modelFallbackMiddleware } from \"langchain\";\n *\n * // Create middleware with fallback models (not including primary)\n * const fallback = modelFallbackMiddleware(\n * \"openai:gpt-4o-mini\", // First fallback\n * \"anthropic:claude-sonnet-4-5-20250929\", // Second fallback\n * );\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\", // Primary model\n * middleware: [fallback],\n * tools: [],\n * });\n *\n * // If gpt-4o fails, automatically tries gpt-4o-mini, then claude\n * const result = await agent.invoke({\n * messages: [{ role: \"user\", content: \"Hello\" }]\n * });\n * ```\n *\n * @param fallbackModels - The fallback models to try, in order.\n * @returns A middleware instance that handles model failures with fallbacks\n */\nexport declare function modelFallbackMiddleware(\n/**\n * The fallback models to try, in order.\n */\n...fallbackModels: (string | LanguageModelLike)[]): AgentMiddleware;\n"],"mappings":";;;;;;;AAkCA;;;;AAImE;;;;;;;;;;;;;;;;;;;;;;;;;;iBAJ3CE,uBAAAA;;;;6BAIKF,uBAAuBC"}
@@ -14,10 +14,10 @@ import { createMiddleware } from "../middleware.js";
14
14
  * import { createAgent, modelFallbackMiddleware } from "langchain";
15
15
  *
16
16
  * // Create middleware with fallback models (not including primary)
17
- * const fallback = modelFallbackMiddleware({
17
+ * const fallback = modelFallbackMiddleware(
18
18
  * "openai:gpt-4o-mini", // First fallback
19
19
  * "anthropic:claude-sonnet-4-5-20250929", // Second fallback
20
- * });
20
+ * );
21
21
  *
22
22
  * const agent = createAgent({
23
23
  * model: "openai:gpt-4o", // Primary model
@@ -1 +1 @@
1
- {"version":3,"file":"modelFallback.js","names":[],"sources":["../../../src/agents/middleware/modelFallback.ts"],"sourcesContent":["import type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport { initChatModel } from \"../../chat_models/universal.js\";\nimport type { AgentMiddleware } from \"./types.js\";\nimport { createMiddleware } from \"../middleware.js\";\n\n/**\n * Middleware that provides automatic model fallback on errors.\n *\n * This middleware attempts to retry failed model calls with alternative models\n * in sequence. When a model call fails, it tries the next model in the fallback\n * list until either a call succeeds or all models have been exhausted.\n *\n * @example\n * ```ts\n * import { createAgent, modelFallbackMiddleware } from \"langchain\";\n *\n * // Create middleware with fallback models (not including primary)\n * const fallback = modelFallbackMiddleware({\n * \"openai:gpt-4o-mini\", // First fallback\n * \"anthropic:claude-sonnet-4-5-20250929\", // Second fallback\n * });\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\", // Primary model\n * middleware: [fallback],\n * tools: [],\n * });\n *\n * // If gpt-4o fails, automatically tries gpt-4o-mini, then claude\n * const result = await agent.invoke({\n * messages: [{ role: \"user\", content: \"Hello\" }]\n * });\n * ```\n *\n * @param fallbackModels - The fallback models to try, in order.\n * @returns A middleware instance that handles model failures with fallbacks\n */\nexport function modelFallbackMiddleware(\n /**\n * The fallback models to try, in order.\n */\n ...fallbackModels: (string | LanguageModelLike)[]\n): AgentMiddleware {\n return createMiddleware({\n name: \"modelFallbackMiddleware\",\n wrapModelCall: async (request, handler) => {\n /**\n * Try the primary model first\n */\n try {\n return await handler(request);\n } catch (error) {\n /**\n * If primary model fails, try fallback models in sequence\n */\n for (let i = 0; i < fallbackModels.length; i++) {\n try {\n const fallbackModel = fallbackModels[i];\n const model =\n typeof fallbackModel === \"string\"\n ? await initChatModel(fallbackModel)\n : fallbackModel;\n\n return await handler({\n ...request,\n model,\n });\n } catch (fallbackError) {\n /**\n * If this is the last fallback, throw the error\n */\n if (i === fallbackModels.length - 1) {\n throw fallbackError;\n }\n // Otherwise, continue to next fallback\n }\n }\n /**\n * If no fallbacks were provided, re-throw the original error\n */\n throw error;\n }\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,wBAId,GAAG,gBACc;AACjB,QAAO,iBAAiB;EACtB,MAAM;EACN,eAAe,OAAO,SAAS,YAAY;;;;AAIzC,OAAI;AACF,WAAO,MAAM,QAAQ,QAAQ;GAC9B,SAAQ,OAAO;;;;AAId,SAAK,IAAI,IAAI,GAAG,IAAI,eAAe,QAAQ,IACzC,KAAI;KACF,MAAM,gBAAgB,eAAe;KACrC,MAAM,QACJ,OAAO,kBAAkB,WACrB,MAAM,cAAc,cAAc,GAClC;AAEN,YAAO,MAAM,QAAQ;MACnB,GAAG;MACH;KACD,EAAC;IACH,SAAQ,eAAe;;;;AAItB,SAAI,MAAM,eAAe,SAAS,EAChC,OAAM;IAGT;;;;AAKH,UAAM;GACP;EACF;CACF,EAAC;AACH"}
1
+ {"version":3,"file":"modelFallback.js","names":[],"sources":["../../../src/agents/middleware/modelFallback.ts"],"sourcesContent":["import type { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport { initChatModel } from \"../../chat_models/universal.js\";\nimport type { AgentMiddleware } from \"./types.js\";\nimport { createMiddleware } from \"../middleware.js\";\n\n/**\n * Middleware that provides automatic model fallback on errors.\n *\n * This middleware attempts to retry failed model calls with alternative models\n * in sequence. When a model call fails, it tries the next model in the fallback\n * list until either a call succeeds or all models have been exhausted.\n *\n * @example\n * ```ts\n * import { createAgent, modelFallbackMiddleware } from \"langchain\";\n *\n * // Create middleware with fallback models (not including primary)\n * const fallback = modelFallbackMiddleware(\n * \"openai:gpt-4o-mini\", // First fallback\n * \"anthropic:claude-sonnet-4-5-20250929\", // Second fallback\n * );\n *\n * const agent = createAgent({\n * model: \"openai:gpt-4o\", // Primary model\n * middleware: [fallback],\n * tools: [],\n * });\n *\n * // If gpt-4o fails, automatically tries gpt-4o-mini, then claude\n * const result = await agent.invoke({\n * messages: [{ role: \"user\", content: \"Hello\" }]\n * });\n * ```\n *\n * @param fallbackModels - The fallback models to try, in order.\n * @returns A middleware instance that handles model failures with fallbacks\n */\nexport function modelFallbackMiddleware(\n /**\n * The fallback models to try, in order.\n */\n ...fallbackModels: (string | LanguageModelLike)[]\n): AgentMiddleware {\n return createMiddleware({\n name: \"modelFallbackMiddleware\",\n wrapModelCall: async (request, handler) => {\n /**\n * Try the primary model first\n */\n try {\n return await handler(request);\n } catch (error) {\n /**\n * If primary model fails, try fallback models in sequence\n */\n for (let i = 0; i < fallbackModels.length; i++) {\n try {\n const fallbackModel = fallbackModels[i];\n const model =\n typeof fallbackModel === \"string\"\n ? await initChatModel(fallbackModel)\n : fallbackModel;\n\n return await handler({\n ...request,\n model,\n });\n } catch (fallbackError) {\n /**\n * If this is the last fallback, throw the error\n */\n if (i === fallbackModels.length - 1) {\n throw fallbackError;\n }\n // Otherwise, continue to next fallback\n }\n }\n /**\n * If no fallbacks were provided, re-throw the original error\n */\n throw error;\n }\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCA,SAAgB,wBAId,GAAG,gBACc;AACjB,QAAO,iBAAiB;EACtB,MAAM;EACN,eAAe,OAAO,SAAS,YAAY;;;;AAIzC,OAAI;AACF,WAAO,MAAM,QAAQ,QAAQ;GAC9B,SAAQ,OAAO;;;;AAId,SAAK,IAAI,IAAI,GAAG,IAAI,eAAe,QAAQ,IACzC,KAAI;KACF,MAAM,gBAAgB,eAAe;KACrC,MAAM,QACJ,OAAO,kBAAkB,WACrB,MAAM,cAAc,cAAc,GAClC;AAEN,YAAO,MAAM,QAAQ;MACnB,GAAG;MACH;KACD,EAAC;IACH,SAAQ,eAAe;;;;AAItB,SAAI,MAAM,eAAe,SAAS,EAChC,OAAM;IAGT;;;;AAKH,UAAM;GACP;EACF;CACF,EAAC;AACH"}