langchain 1.2.37 → 1.2.39

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 (62) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/agents/ReactAgent.cjs +2 -1
  3. package/dist/agents/ReactAgent.cjs.map +1 -1
  4. package/dist/agents/ReactAgent.d.cts.map +1 -1
  5. package/dist/agents/ReactAgent.d.ts.map +1 -1
  6. package/dist/agents/ReactAgent.js +2 -1
  7. package/dist/agents/ReactAgent.js.map +1 -1
  8. package/dist/agents/middleware/modelFallback.cjs.map +1 -1
  9. package/dist/agents/middleware/modelFallback.d.cts +2 -2
  10. package/dist/agents/middleware/modelFallback.d.cts.map +1 -1
  11. package/dist/agents/middleware/modelFallback.d.ts +2 -2
  12. package/dist/agents/middleware/modelFallback.d.ts.map +1 -1
  13. package/dist/agents/middleware/modelFallback.js.map +1 -1
  14. package/dist/agents/middleware/summarization.cjs +1 -1
  15. package/dist/agents/middleware/summarization.js +21 -21
  16. package/dist/agents/middleware/summarization.js.map +1 -1
  17. package/dist/agents/middleware/toolCallLimit.cjs +1 -1
  18. package/dist/agents/middleware/toolCallLimit.js +11 -11
  19. package/dist/agents/middleware/toolCallLimit.js.map +1 -1
  20. package/dist/agents/model.cjs.map +1 -1
  21. package/dist/agents/model.d.cts +9 -0
  22. package/dist/agents/model.d.cts.map +1 -0
  23. package/dist/agents/model.d.ts +9 -0
  24. package/dist/agents/model.d.ts.map +1 -0
  25. package/dist/agents/model.js.map +1 -1
  26. package/dist/agents/nodes/AgentNode.cjs +26 -14
  27. package/dist/agents/nodes/AgentNode.cjs.map +1 -1
  28. package/dist/agents/nodes/AgentNode.js +26 -14
  29. package/dist/agents/nodes/AgentNode.js.map +1 -1
  30. package/dist/agents/nodes/types.d.cts +9 -3
  31. package/dist/agents/nodes/types.d.cts.map +1 -1
  32. package/dist/agents/nodes/types.d.ts +9 -3
  33. package/dist/agents/nodes/types.d.ts.map +1 -1
  34. package/dist/agents/nodes/utils.cjs +10 -10
  35. package/dist/agents/nodes/utils.cjs.map +1 -1
  36. package/dist/agents/nodes/utils.js +3 -3
  37. package/dist/agents/nodes/utils.js.map +1 -1
  38. package/dist/agents/responses.cjs.map +1 -1
  39. package/dist/agents/responses.d.cts +3 -2
  40. package/dist/agents/responses.d.cts.map +1 -1
  41. package/dist/agents/responses.d.ts +3 -2
  42. package/dist/agents/responses.d.ts.map +1 -1
  43. package/dist/agents/responses.js.map +1 -1
  44. package/dist/agents/tests/utils.d.cts +0 -1
  45. package/dist/agents/tests/utils.d.cts.map +1 -1
  46. package/dist/agents/tests/utils.d.ts +0 -1
  47. package/dist/agents/tests/utils.d.ts.map +1 -1
  48. package/dist/agents/types.d.cts +4 -5
  49. package/dist/agents/types.d.cts.map +1 -1
  50. package/dist/agents/types.d.ts +4 -5
  51. package/dist/agents/types.d.ts.map +1 -1
  52. package/dist/agents/utils.cjs.map +1 -1
  53. package/dist/agents/utils.js.map +1 -1
  54. package/dist/agents/withAgentName.cjs.map +1 -1
  55. package/dist/agents/withAgentName.js.map +1 -1
  56. package/dist/browser.cjs +106 -0
  57. package/dist/browser.cjs.map +1 -1
  58. package/dist/browser.d.cts +29 -1
  59. package/dist/browser.d.ts +29 -1
  60. package/dist/browser.js +66 -1
  61. package/dist/browser.js.map +1 -1
  62. package/package.json +8 -8
@@ -1,12 +1,11 @@
1
- import { JsonSchemaFormat, ProviderStrategy, ResponseFormat, ResponseFormatUndefined, ToolStrategy, TypedToolStrategy } from "./responses.cjs";
1
+ import { AgentLanguageModelLike } from "./model.cjs";
2
+ import { ResponseFormatInput, ResponseFormatUndefined } from "./responses.cjs";
2
3
  import { JumpToTarget } from "./constants.cjs";
3
4
  import { AgentMiddleware, AnyAnnotationRoot, InferMiddlewareContexts, InferMiddlewareStates, InferSchemaInput, InferSchemaValue } from "./middleware/types.cjs";
4
5
  import { BaseMessage, MessageStructure, MessageToolDefinition, SystemMessage } from "@langchain/core/messages";
5
- import { LanguageModelLike } from "@langchain/core/language_models/base";
6
6
  import { ClientTool, DynamicStructuredTool, ServerTool, StructuredToolInterface } from "@langchain/core/tools";
7
7
  import { InteropZodObject, InteropZodType } from "@langchain/core/utils/types";
8
8
  import { END, START, StateDefinitionInit, StateGraph } from "@langchain/langgraph";
9
- import { SerializableSchema } from "@langchain/core/utils/standard_schema";
10
9
  import { BaseCheckpointSaver, BaseStore } from "@langchain/langgraph-checkpoint";
11
10
  import { Messages } from "@langchain/langgraph/";
12
11
 
@@ -356,7 +355,7 @@ interface ExecutedToolCall {
356
355
  */
357
356
  result?: unknown;
358
357
  }
359
- type CreateAgentParams<StructuredResponseType extends Record<string, any> = Record<string, any>, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, ResponseFormatType = InteropZodType<StructuredResponseType> | InteropZodType<unknown>[] | SerializableSchema<StructuredResponseType> | SerializableSchema[] | JsonSchemaFormat | JsonSchemaFormat[] | ResponseFormat | TypedToolStrategy<StructuredResponseType> | ToolStrategy<StructuredResponseType> | ProviderStrategy<StructuredResponseType> | ResponseFormatUndefined> = {
358
+ type CreateAgentParams<StructuredResponseType extends Record<string, any> = Record<string, any>, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, ResponseFormatType = ResponseFormatInput<StructuredResponseType>> = {
360
359
  /**
361
360
  * Defines a model to use for the agent. You can either pass in an instance of a LangChain chat model
362
361
  * or a string. If a string is provided the agent initializes a ChatModel based on the provided model name and provider.
@@ -380,7 +379,7 @@ type CreateAgentParams<StructuredResponseType extends Record<string, any> = Reco
380
379
  * });
381
380
  * ```
382
381
  */
383
- model: string | LanguageModelLike;
382
+ model: string | AgentLanguageModelLike;
384
383
  /**
385
384
  * A list of tools or a ToolNode.
386
385
  *
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.cts","names":[],"sources":["../../src/agents/types.ts"],"mappings":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;;;;;;;EA8HE,KAAA,WAAgB,sBAAA;EA7HU;;;;;;;;;;;AAkB5B;;;;;;;;;;EAkIE,KAAA,IAAS,UAAA,GAAa,UAAA;EAhIpB;;;;;;AAOJ;;;;;;;;;;;;AA0BA;;;;;;;;;AAkBA;;;;;AAKA;;;;;;;;;;;AAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;EA4HE,YAAA,YAAwB,aAAA;EA1HxB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqUF;;;;;;;;EA7JE,WAAA,GAAc,YAAA;EAgKJ;;;;;;;;;;;;;;;;;AAIZ;;;;;;;;;;;;;;;;EAlIE,aAAA,GAAgB,aAAA;EAkIgC;;;;EA7HhD,YAAA,GAAe,mBAAA;EAiIP;;;;EA5HR,KAAA,GAAQ,SAAA;EAkIJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA5EJ,cAAA,GAAiB,kBAAA;;;;;;;EAQjB,UAAA,YAAsB,eAAA;;;;EAKtB,IAAA;;;;;EAMA,WAAA;;;;;;;EAQA,gBAAA;;;;EAKA,MAAA,GAAS,WAAA;;;;;;;;;;;;;;;;;;;;EAqBT,OAAA;AAAA;;;;KAMU,oBAAA,oBAAwC,cAAA,WAClD,CAAA,mBAAoB,cAAA,4BAChB,IAAA,kBAAsB,cAAA,UACpB,CAAA,GAAI,oBAAA,CAAqB,IAAA,IACzB,CAAA;AAAA,KAGI,mBAAA,4BACV,KAAA,SAAc,UAAA,mEASV,UAAA,CAAW,EAAA,EAAI,CAAA,EAAG,CAAA,EAAG,CAAA,GAAI,CAAA,EAAG,CAAA,EAAG,CAAA,EAAG,CAAA"}
@@ -1,13 +1,12 @@
1
- import { JsonSchemaFormat, ProviderStrategy, ResponseFormat, ResponseFormatUndefined, ToolStrategy, TypedToolStrategy } from "./responses.js";
1
+ import { AgentLanguageModelLike } from "./model.js";
2
+ import { ResponseFormatInput, ResponseFormatUndefined } from "./responses.js";
2
3
  import { JumpToTarget } from "./constants.js";
3
4
  import { AgentMiddleware, AnyAnnotationRoot, InferMiddlewareContexts, InferMiddlewareStates, InferSchemaInput, InferSchemaValue } from "./middleware/types.js";
4
5
  import { BaseMessage, MessageStructure, MessageToolDefinition, SystemMessage } from "@langchain/core/messages";
5
6
  import { ClientTool, DynamicStructuredTool, ServerTool, StructuredToolInterface } from "@langchain/core/tools";
6
7
  import { END, START, StateDefinitionInit, StateGraph } from "@langchain/langgraph";
7
8
  import { InteropZodObject, InteropZodType } from "@langchain/core/utils/types";
8
- import { SerializableSchema } from "@langchain/core/utils/standard_schema";
9
9
  import { BaseCheckpointSaver, BaseStore } from "@langchain/langgraph-checkpoint";
10
- import { LanguageModelLike } from "@langchain/core/language_models/base";
11
10
  import { Messages } from "@langchain/langgraph/";
12
11
 
13
12
  //#region src/agents/types.d.ts
@@ -356,7 +355,7 @@ interface ExecutedToolCall {
356
355
  */
357
356
  result?: unknown;
358
357
  }
359
- type CreateAgentParams<StructuredResponseType extends Record<string, any> = Record<string, any>, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, ResponseFormatType = InteropZodType<StructuredResponseType> | InteropZodType<unknown>[] | SerializableSchema<StructuredResponseType> | SerializableSchema[] | JsonSchemaFormat | JsonSchemaFormat[] | ResponseFormat | TypedToolStrategy<StructuredResponseType> | ToolStrategy<StructuredResponseType> | ProviderStrategy<StructuredResponseType> | ResponseFormatUndefined> = {
358
+ type CreateAgentParams<StructuredResponseType extends Record<string, any> = Record<string, any>, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, ResponseFormatType = ResponseFormatInput<StructuredResponseType>> = {
360
359
  /**
361
360
  * Defines a model to use for the agent. You can either pass in an instance of a LangChain chat model
362
361
  * or a string. If a string is provided the agent initializes a ChatModel based on the provided model name and provider.
@@ -380,7 +379,7 @@ type CreateAgentParams<StructuredResponseType extends Record<string, any> = Reco
380
379
  * });
381
380
  * ```
382
381
  */
383
- model: string | LanguageModelLike;
382
+ model: string | AgentLanguageModelLike;
384
383
  /**
385
384
  * A list of tools or a ToolNode.
386
385
  *
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","names":[],"sources":["../../src/agents/types.ts"],"mappings":";;;;;;;;;;;;;;;AAwFA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAAiB,eAAA,mBACG,MAAA,gBAAsB,uBAAA,GACpC,MAAA,gBACA,uBAAA,iBACW,mBAAA,eACX,mBAAA,+BAEa,iBAAA,GAAoB,gBAAA,GACjC,iBAAA,GACA,gBAAA,+BACyB,eAAA,cAA6B,eAAA,6BACjC,UAAA,GAAa,UAAA,gBAClC,UAAA,GACA,UAAA;EAJA;EAQJ,QAAA,EAAU,SAAA;EAPmB;EAS7B,KAAA,EAAO,MAAA;EARP;EAUA,OAAA,EAAS,QAAA;EAV6B;EAYtC,UAAA,EAAY,WAAA;EAVR;EAYJ,KAAA,EAAO,MAAA;AAAA;;;;;UAOQ,sBAAA,SAA+B,eAAA;EAC9C,QAAA,EAAU,MAAA;EACV,KAAA;EACA,OAAA,EAAS,iBAAA;EACT,UAAA,WAAqB,eAAA;EACrB,KAAA,YAAiB,UAAA,GAAa,UAAA;AAAA;;;;;KAOpB,oBAAA,WAA+B,eAAA,IACzC,CAAA,SAAU,eAAA,gCACN,MAAA,mBAAyB,UAAA,GAAa,UAAA,MACpC,MAAA;;;;;KAQI,yBAAA,oBAA6C,eAAA,MACvD,CAAA,qCAEI,CAAA,iDACE,KAAA,SAAc,eAAA,GACZ,IAAA,kBAAsB,eAAA,kBAEf,oBAAA,CAAqB,KAAA,MACrB,yBAAA,CAA0B,IAAA,KAE/B,oBAAA,CAAqB,KAAA;;;;KAOrB,YAAA,+BACoB,UAAA,GAAa,UAAA,kCACd,eAAA,mBACd,WAAA,KAAgB,yBAAA,CAA0B,WAAA;;;;;KAMtD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EAAG,CAAA,EAAG,CAAA,EAAG,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","names":["StateSchema","AIMessage","AIMessageChunk","Runnable","isBaseChatModel","RunnableBinding","RunnableSequence","isConfigurableModel","MultipleToolsBoundError","SystemMessage","ToolMessage","MiddlewareError"],"sources":["../../src/agents/utils.ts"],"sourcesContent":["import {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n BaseMessageLike,\n SystemMessage,\n MessageContent,\n ToolMessage,\n} from \"@langchain/core/messages\";\nimport { isCommand, StateSchema } from \"@langchain/langgraph\";\nimport {\n type InteropZodObject,\n interopParse,\n isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n BaseChatModel,\n type BaseChatModelCallOptions,\n} from \"@langchain/core/language_models/chat_models\";\nimport {\n LanguageModelLike,\n BaseLanguageModelInput,\n} from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnableLike,\n RunnableConfig,\n RunnableSequence,\n RunnableBinding,\n} from \"@langchain/core/runnables\";\nimport type { ClientTool, ServerTool } from \"@langchain/core/tools\";\n\nimport { isBaseChatModel, isConfigurableModel } from \"./model.js\";\nimport { MultipleToolsBoundError, MiddlewareError } from \"./errors.js\";\nimport type { AgentBuiltInState } from \"./runtime.js\";\nimport type {\n ToolCallHandler,\n AgentMiddleware,\n ToolCallRequest,\n WrapToolCallHook,\n} from \"./middleware/types.js\";\n\nconst NAME_PATTERN = /<name>(.*?)<\\/name>/s;\nconst CONTENT_PATTERN = /<content>(.*?)<\\/content>/s;\n\n/**\n * Parse middleware state from the full agent state based on the middleware's stateSchema.\n *\n * Handles two types of state schemas:\n * 1. Zod schemas (v3 or v4) - parsed using interopParse\n * 2. LangGraph StateSchema - extracts only the keys defined in `fields`\n *\n * @param stateSchema - The middleware's state schema (Zod or LangGraph StateSchema)\n * @param state - The full agent state to parse from\n * @returns Parsed state containing only the keys defined in the schema\n */\nfunction parseMiddlewareState(\n stateSchema: unknown,\n state: Record<string, unknown>\n): Record<string, unknown> {\n // Handle LangGraph StateSchema (has `fields` property)\n if (StateSchema.isInstance(stateSchema)) {\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(stateSchema.fields)) {\n if (key in state) {\n result[key] = state[key];\n }\n }\n return result;\n }\n\n // Handle Zod schemas using interopParse\n if (isInteropZodSchema(stateSchema)) {\n return interopParse(stateSchema as InteropZodObject, state);\n }\n\n throw new Error(`Invalid state schema type: ${typeof stateSchema}`);\n}\n\nexport type AgentNameMode = \"inline\";\n\n/**\n * Attach formatted agent names to the messages passed to and from a language model.\n *\n * This is useful for making a message history with multiple agents more coherent.\n *\n * NOTE: agent name is consumed from the message.name field.\n * If you're using an agent built with createAgent, name is automatically set.\n * If you're building a custom agent, make sure to set the name on the AI message returned by the LLM.\n *\n * @param message - Message to add agent name formatting to\n * @returns Message with agent name formatting\n *\n * @internal\n */\nexport function _addInlineAgentName<T extends BaseMessageLike>(\n message: T\n): T | AIMessage {\n if (!AIMessage.isInstance(message) || AIMessageChunk.isInstance(message)) {\n return message;\n }\n\n if (!message.name) {\n return message;\n }\n\n const { name } = message;\n\n if (typeof message.content === \"string\") {\n return new AIMessage({\n ...message.lc_kwargs,\n content: `<name>${name}</name><content>${message.content}</content>`,\n name: undefined,\n });\n }\n\n const updatedContent = [];\n let textBlockCount = 0;\n\n for (const contentBlock of message.content) {\n if (typeof contentBlock === \"string\") {\n textBlockCount += 1;\n updatedContent.push(\n `<name>${name}</name><content>${contentBlock}</content>`\n );\n } else if (\n typeof contentBlock === \"object\" &&\n \"type\" in contentBlock &&\n contentBlock.type === \"text\"\n ) {\n textBlockCount += 1;\n updatedContent.push({\n ...contentBlock,\n text: `<name>${name}</name><content>${contentBlock.text}</content>`,\n });\n } else {\n updatedContent.push(contentBlock);\n }\n }\n\n if (!textBlockCount) {\n updatedContent.unshift({\n type: \"text\",\n text: `<name>${name}</name><content></content>`,\n });\n }\n return new AIMessage({\n ...message.lc_kwargs,\n content: updatedContent as MessageContent,\n name: undefined,\n });\n}\n\n/**\n * Remove explicit name and content XML tags from the AI message content.\n *\n * Examples:\n *\n * @example\n * ```typescript\n * removeInlineAgentName(new AIMessage({ content: \"<name>assistant</name><content>Hello</content>\", name: \"assistant\" }))\n * // AIMessage with content: \"Hello\"\n *\n * removeInlineAgentName(new AIMessage({ content: [{type: \"text\", text: \"<name>assistant</name><content>Hello</content>\"}], name: \"assistant\" }))\n * // AIMessage with content: [{type: \"text\", text: \"Hello\"}]\n * ```\n *\n * @internal\n */\nexport function _removeInlineAgentName<T extends BaseMessage>(message: T): T {\n if (!AIMessage.isInstance(message) || !message.content) {\n return message;\n }\n\n let updatedContent: MessageContent = [];\n let updatedName: string | undefined;\n\n if (Array.isArray(message.content)) {\n updatedContent = message.content\n .filter((block) => {\n if (block.type === \"text\" && typeof block.text === \"string\") {\n const nameMatch = block.text.match(NAME_PATTERN);\n const contentMatch = block.text.match(CONTENT_PATTERN);\n // don't include empty content blocks that were added because there was no text block to modify\n if (nameMatch && (!contentMatch || contentMatch[1] === \"\")) {\n // capture name from text block\n updatedName = nameMatch[1];\n return false;\n }\n return true;\n }\n return true;\n })\n .map((block) => {\n if (block.type === \"text\" && typeof block.text === \"string\") {\n const nameMatch = block.text.match(NAME_PATTERN);\n const contentMatch = block.text.match(CONTENT_PATTERN);\n\n if (!nameMatch || !contentMatch) {\n return block;\n }\n\n // capture name from text block\n updatedName = nameMatch[1];\n\n return {\n ...block,\n text: contentMatch[1],\n };\n }\n return block;\n });\n } else {\n const content = message.content as string;\n const nameMatch = content.match(NAME_PATTERN);\n const contentMatch = content.match(CONTENT_PATTERN);\n\n if (!nameMatch || !contentMatch) {\n return message;\n }\n\n updatedName = nameMatch[1];\n updatedContent = contentMatch[1];\n }\n\n return new AIMessage({\n ...(Object.keys(message.lc_kwargs ?? {}).length > 0\n ? message.lc_kwargs\n : message),\n content: updatedContent,\n name: updatedName,\n }) as T;\n}\n\nexport function isClientTool(\n tool: ClientTool | ServerTool\n): tool is ClientTool {\n return Runnable.isRunnable(tool);\n}\n\n/**\n * Helper function to check if a language model has a bindTools method.\n * @param llm - The language model to check if it has a bindTools method.\n * @returns True if the language model has a bindTools method, false otherwise.\n */\nfunction _isChatModelWithBindTools(\n llm: LanguageModelLike\n): llm is BaseChatModel & Required<Pick<BaseChatModel, \"bindTools\">> {\n if (!isBaseChatModel(llm)) return false;\n return \"bindTools\" in llm && typeof llm.bindTools === \"function\";\n}\n\n/**\n * Helper function to bind tools to a language model.\n * @param llm - The language model to bind tools to.\n * @param toolClasses - The tools to bind to the language model.\n * @param options - The options to pass to the language model.\n * @returns The language model with the tools bound to it.\n */\nconst _simpleBindTools = (\n llm: LanguageModelLike,\n toolClasses: (ClientTool | ServerTool)[],\n options: Partial<BaseChatModelCallOptions> = {}\n) => {\n if (_isChatModelWithBindTools(llm)) {\n return llm.bindTools(toolClasses, options);\n }\n\n if (\n RunnableBinding.isRunnableBinding(llm) &&\n _isChatModelWithBindTools(llm.bound)\n ) {\n const newBound = llm.bound.bindTools(toolClasses, options);\n\n if (RunnableBinding.isRunnableBinding(newBound)) {\n return new RunnableBinding({\n bound: newBound.bound,\n config: { ...llm.config, ...newBound.config },\n kwargs: { ...llm.kwargs, ...newBound.kwargs },\n configFactories: newBound.configFactories ?? llm.configFactories,\n });\n }\n\n return new RunnableBinding({\n bound: newBound,\n config: llm.config,\n kwargs: llm.kwargs,\n configFactories: llm.configFactories,\n });\n }\n\n return null;\n};\n\n/**\n * Check if the LLM already has bound tools and throw if it does.\n *\n * @param llm - The LLM to check.\n * @returns void\n */\nexport function validateLLMHasNoBoundTools(llm: LanguageModelLike): void {\n /**\n * If llm is a function, we can't validate until runtime, so skip\n */\n if (typeof llm === \"function\") {\n return;\n }\n\n let model = llm;\n\n /**\n * If model is a RunnableSequence, find a RunnableBinding in its steps\n */\n if (RunnableSequence.isRunnableSequence(model)) {\n model =\n model.steps.find((step: RunnableLike) =>\n RunnableBinding.isRunnableBinding(step)\n ) || model;\n }\n\n /**\n * If model is configurable, get the underlying model\n */\n if (isConfigurableModel(model)) {\n /**\n * Can't validate async model retrieval in constructor\n */\n return;\n }\n\n /**\n * Check if model is a RunnableBinding with bound tools\n */\n if (RunnableBinding.isRunnableBinding(model)) {\n const hasToolsInKwargs =\n model.kwargs != null &&\n typeof model.kwargs === \"object\" &&\n \"tools\" in model.kwargs &&\n Array.isArray(model.kwargs.tools) &&\n model.kwargs.tools.length > 0;\n\n const hasToolsInConfig =\n model.config != null &&\n typeof model.config === \"object\" &&\n \"tools\" in model.config &&\n Array.isArray(model.config.tools) &&\n model.config.tools.length > 0;\n\n if (hasToolsInKwargs || hasToolsInConfig) {\n throw new MultipleToolsBoundError();\n }\n }\n\n /**\n * Also check if model has tools property directly (e.g., FakeToolCallingModel)\n */\n if (\n \"tools\" in model &&\n model.tools !== undefined &&\n Array.isArray(model.tools) &&\n model.tools.length > 0\n ) {\n throw new MultipleToolsBoundError();\n }\n}\n\n/**\n * Check if the last message in the messages array has tool calls.\n *\n * @param messages - The messages to check.\n * @returns True if the last message has tool calls, false otherwise.\n */\nexport function hasToolCalls(message?: BaseMessage): boolean {\n return Boolean(\n AIMessage.isInstance(message) &&\n message.tool_calls &&\n message.tool_calls.length > 0\n );\n}\n\n/**\n * Normalizes a system prompt to a SystemMessage object.\n * If it's already a SystemMessage, returns it as-is.\n * If it's a string, converts it to a SystemMessage.\n * If it's undefined, creates an empty system message so it is easier to append to it later.\n */\nexport function normalizeSystemPrompt(\n systemPrompt?: string | SystemMessage\n): SystemMessage {\n if (systemPrompt == null) {\n return new SystemMessage(\"\");\n }\n if (SystemMessage.isInstance(systemPrompt)) {\n return systemPrompt;\n }\n if (typeof systemPrompt === \"string\") {\n return new SystemMessage({\n content: [{ type: \"text\", text: systemPrompt }],\n });\n }\n throw new Error(\n `Invalid systemPrompt type: expected string or SystemMessage, got ${typeof systemPrompt}`\n );\n}\n\n/**\n * Helper function to bind tools to a language model.\n * @param llm - The language model to bind tools to.\n * @param toolClasses - The tools to bind to the language model.\n * @param options - The options to pass to the language model.\n * @returns The language model with the tools bound to it.\n */\nexport async function bindTools(\n llm: LanguageModelLike,\n toolClasses: (ClientTool | ServerTool)[],\n options: Partial<BaseChatModelCallOptions> = {}\n): Promise<\n | RunnableSequence<unknown, unknown>\n | RunnableBinding<unknown, unknown, RunnableConfig<Record<string, unknown>>>\n | Runnable<BaseLanguageModelInput, AIMessageChunk, BaseChatModelCallOptions>\n> {\n const model = _simpleBindTools(llm, toolClasses, options);\n if (model) return model;\n\n if (isConfigurableModel(llm)) {\n const model = _simpleBindTools(\n await llm._getModelInstance(),\n toolClasses,\n options\n );\n if (model) return model;\n }\n\n if (RunnableSequence.isRunnableSequence(llm)) {\n const modelStep = llm.steps.findIndex(\n (step) =>\n RunnableBinding.isRunnableBinding(step) ||\n isBaseChatModel(step) ||\n isConfigurableModel(step)\n );\n\n if (modelStep >= 0) {\n const model = _simpleBindTools(\n llm.steps[modelStep],\n toolClasses,\n options\n );\n if (model) {\n const nextSteps: unknown[] = llm.steps.slice();\n nextSteps.splice(modelStep, 1, model);\n\n return RunnableSequence.from(\n nextSteps as [RunnableLike, ...RunnableLike[], RunnableLike]\n );\n }\n }\n }\n\n throw new Error(`llm ${llm} must define bindTools method.`);\n}\n\n/**\n * Compose multiple wrapToolCall handlers into a single middleware stack.\n *\n * Composes handlers so the first in the list becomes the outermost layer.\n * Each handler receives a handler callback to execute inner layers.\n *\n * @param handlers - List of handlers. First handler wraps all others.\n * @returns Composed handler, or undefined if handlers array is empty.\n *\n * @example\n * ```typescript\n * // handlers=[auth, retry] means: auth wraps retry\n * // Flow: auth calls retry, retry calls base handler\n * const auth: ToolCallWrapper = async (request, handler) => {\n * try {\n * return await handler(request);\n * } catch (error) {\n * if (error.message === \"Unauthorized\") {\n * await refreshToken();\n * return await handler(request);\n * }\n * throw error;\n * }\n * };\n *\n * const retry: ToolCallWrapper = async (request, handler) => {\n * for (let attempt = 0; attempt < 3; attempt++) {\n * try {\n * return await handler(request);\n * } catch (error) {\n * if (attempt === 2) throw error;\n * }\n * }\n * throw new Error(\"Unreachable\");\n * };\n *\n * const composedHandler = chainToolCallHandlers([auth, retry]);\n * ```\n */\nfunction chainToolCallHandlers(\n handlers: WrapToolCallHook[]\n): WrapToolCallHook | undefined {\n if (handlers.length === 0) {\n return undefined;\n }\n\n if (handlers.length === 1) {\n return handlers[0];\n }\n\n // Compose two handlers where outer wraps inner\n // The key is to properly propagate request modifications through the chain\n function composeTwo(\n outer: WrapToolCallHook,\n inner: WrapToolCallHook\n ): WrapToolCallHook {\n return async (request, handler) => {\n // Create a wrapper that calls inner with the base handler\n // The innerHandler receives the (possibly modified) request from outer\n // and passes it to inner, which then calls the base handler\n const innerHandler: ToolCallHandler = async (passedRequest) => {\n // inner receives the request passed by outer (which may be modified)\n return inner(passedRequest, handler);\n };\n\n // Call outer with the wrapped inner as its handler\n return outer(request, innerHandler);\n };\n }\n\n // Compose right-to-left: outer(inner(innermost(handler)))\n let result = handlers[handlers.length - 1];\n for (let i = handlers.length - 2; i >= 0; i--) {\n result = composeTwo(handlers[i], result);\n }\n\n return result;\n}\n\n/**\n * Wrapping `wrapToolCall` invocation so we can inject middleware name into\n * the error message.\n *\n * @param middleware list of middleware passed to the agent\n * @param state state of the agent\n * @returns single wrap function\n */\nexport function wrapToolCall(\n middleware: readonly AgentMiddleware<InteropZodObject | undefined>[]\n) {\n const middlewareWithWrapToolCall = middleware.filter((m) => m.wrapToolCall);\n\n if (middlewareWithWrapToolCall.length === 0) {\n return;\n }\n\n return chainToolCallHandlers(\n middlewareWithWrapToolCall.map((m) => {\n const originalHandler = m.wrapToolCall!;\n /**\n * Wrap with error handling and validation\n */\n const wrappedHandler: WrapToolCallHook = async (request, handler) => {\n /**\n * Capture the original state for this middleware's schema parsing.\n * This is important because the request may be modified (via override)\n * as it passes through the middleware chain, but each middleware\n * should always see the full original state for its schema parsing.\n */\n const originalState = request.state;\n\n /**\n * Create a handler that preserves state parsing for this middleware\n * while allowing tool/toolCall/state modifications from inner middleware\n */\n const wrappedInnerHandler: ToolCallHandler = async (passedRequest) => {\n /**\n * Merge the passed request with the original state for parsing.\n * This ensures middleware can override tool/toolCall while\n * maintaining proper state parsing for each middleware in the chain.\n */\n const mergedState = {\n ...originalState,\n ...passedRequest.state,\n };\n return handler({\n ...passedRequest,\n state: mergedState,\n });\n };\n\n try {\n const result = await originalHandler(\n {\n ...request,\n /**\n * override state with the state from the specific middleware\n */\n state: {\n messages: originalState.messages,\n ...(m.stateSchema\n ? parseMiddlewareState(m.stateSchema, { ...originalState })\n : {}),\n },\n } as ToolCallRequest<AgentBuiltInState, unknown>,\n wrappedInnerHandler\n );\n\n /**\n * Validate return type\n */\n if (!ToolMessage.isInstance(result) && !isCommand(result)) {\n throw new Error(\n `Invalid response from \"wrapToolCall\" in middleware \"${m.name}\": ` +\n `expected ToolMessage or Command, got ${typeof result}`\n );\n }\n\n return result;\n } catch (error) {\n throw MiddlewareError.wrap(error, m.name);\n }\n };\n return wrappedHandler;\n })\n );\n}\n"],"mappings":";;;;;;;;AA0CA,MAAM,eAAe;AACrB,MAAM,kBAAkB;;;;;;;;;;;;AAaxB,SAAS,qBACP,aACA,OACyB;AAEzB,KAAIA,qBAAAA,YAAY,WAAW,YAAY,EAAE;EACvC,MAAM,SAAkC,EAAE;AAC1C,OAAK,MAAM,OAAO,OAAO,KAAK,YAAY,OAAO,CAC/C,KAAI,OAAO,MACT,QAAO,OAAO,MAAM;AAGxB,SAAO;;AAIT,MAAA,GAAA,4BAAA,oBAAuB,YAAY,CACjC,SAAA,GAAA,4BAAA,cAAoB,aAAiC,MAAM;AAG7D,OAAM,IAAI,MAAM,8BAA8B,OAAO,cAAc;;;;;;;;;;;;;;;;AAmBrE,SAAgB,oBACd,SACe;AACf,KAAI,CAACC,yBAAAA,UAAU,WAAW,QAAQ,IAAIC,yBAAAA,eAAe,WAAW,QAAQ,CACtE,QAAO;AAGT,KAAI,CAAC,QAAQ,KACX,QAAO;CAGT,MAAM,EAAE,SAAS;AAEj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AKvC,IAAI,SAAS,SAAS,SAAS,SAAS;AACxC,MAAK,IAAI,IAAI,SAAS,SAAS,GAAG,KAAK,GAAG,IACxC,UAAS,WAAW,SAAS,IAAI,OAAO;AAG1C,QAAO;;;;;;;;;;AAWT,SAAgB,aACd,YACA;CACA,MAAM,6BAA6B,WAAW,QAAQ,MAAM,EAAE,aAAa;AAE3E,KAAI,2BAA2B,WAAW,EACxC;AAGF,QAAO,sBACL,2BAA2B,KAAK,MAAM;EACpC,MAAM,kBAAkB,EAAE;;;;EAI1B,MAAM,iBAAmC,OAAO,SAAS,YAAY;;;;;;;GAOnE,MAAM,gBAAgB,QAAQ;;;;;GAM9B,MAAM,sBAAuC,OAAO,kBAAkB;;;;;;IAMpE,MAAM,cAAc;KAClB,GAAG;KACH,GAAG,cAAc;KAClB;AACD,WAAO,QAAQ;KACb,GAAG;KACH,OAAO;KACR,CAAC;;AAGJ,OAAI;IACF,MAAM,SAAS,MAAM,gBACnB;KACE,GAAG;KAIH,OAAO;MACL,UAAU,cAAc;MACxB,GAAI,EAAE,cACF,qBAAqB,EAAE,aAAa,EAAE,GAAG,eAAe,CAAC,GACzD,EAAE;MACP;KACF,EACD,oBACD;;;;AAKD,QAAI,CAACI,yBAAAA,YAAY,WAAW,OAAO,IAAI,EAAA,GAAA,qBAAA,WAAW,OAAO,CACvD,OAAM,IAAI,MACR,uDAAuD,EAAE,KAAK,0CACpB,OAAO,SAClD;AAGH,WAAO;YACA,OAAO;AACd,UAAMC,eAAAA,gBAAgB,KAAK,OAAO,EAAE,KAAK;;;AAG7C,SAAO;GACP,CACH"}
1
+ {"version":3,"file":"utils.cjs","names":["StateSchema","AIMessage","AIMessageChunk","Runnable","isBaseChatModel","RunnableBinding","RunnableSequence","isConfigurableModel","MultipleToolsBoundError","SystemMessage","ToolMessage","MiddlewareError"],"sources":["../../src/agents/utils.ts"],"sourcesContent":["import {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n BaseMessageLike,\n SystemMessage,\n MessageContent,\n ToolMessage,\n} from \"@langchain/core/messages\";\nimport { isCommand, StateSchema } from \"@langchain/langgraph\";\nimport {\n type InteropZodObject,\n interopParse,\n isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n BaseChatModel,\n type BaseChatModelCallOptions,\n} from \"@langchain/core/language_models/chat_models\";\nimport { BaseLanguageModelInput } from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnableLike,\n RunnableConfig,\n RunnableSequence,\n RunnableBinding,\n} from \"@langchain/core/runnables\";\nimport type { ClientTool, ServerTool } from \"@langchain/core/tools\";\n\nimport {\n isBaseChatModel,\n isConfigurableModel,\n type AgentLanguageModelLike as LanguageModelLike,\n} from \"./model.js\";\nimport { MultipleToolsBoundError, MiddlewareError } from \"./errors.js\";\nimport type { AgentBuiltInState } from \"./runtime.js\";\nimport type {\n ToolCallHandler,\n AgentMiddleware,\n ToolCallRequest,\n WrapToolCallHook,\n} from \"./middleware/types.js\";\n\nconst NAME_PATTERN = /<name>(.*?)<\\/name>/s;\nconst CONTENT_PATTERN = /<content>(.*?)<\\/content>/s;\n\n/**\n * Parse middleware state from the full agent state based on the middleware's stateSchema.\n *\n * Handles two types of state schemas:\n * 1. Zod schemas (v3 or v4) - parsed using interopParse\n * 2. LangGraph StateSchema - extracts only the keys defined in `fields`\n *\n * @param stateSchema - The middleware's state schema (Zod or LangGraph StateSchema)\n * @param state - The full agent state to parse from\n * @returns Parsed state containing only the keys defined in the schema\n */\nfunction parseMiddlewareState(\n stateSchema: unknown,\n state: Record<string, unknown>\n): Record<string, unknown> {\n // Handle LangGraph StateSchema (has `fields` property)\n if (StateSchema.isInstance(stateSchema)) {\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(stateSchema.fields)) {\n if (key in state) {\n result[key] = state[key];\n }\n }\n return result;\n }\n\n // Handle Zod schemas using interopParse\n if (isInteropZodSchema(stateSchema)) {\n return interopParse(stateSchema as InteropZodObject, state);\n }\n\n throw new Error(`Invalid state schema type: ${typeof stateSchema}`);\n}\n\nexport type AgentNameMode = \"inline\";\n\n/**\n * Attach formatted agent names to the messages passed to and from a language model.\n *\n * This is useful for making a message history with multiple agents more coherent.\n *\n * NOTE: agent name is consumed from the message.name field.\n * If you're using an agent built with createAgent, name is automatically set.\n * If you're building a custom agent, make sure to set the name on the AI message returned by the LLM.\n *\n * @param message - Message to add agent name formatting to\n * @returns Message with agent name formatting\n *\n * @internal\n */\nexport function _addInlineAgentName<T extends BaseMessageLike>(\n message: T\n): T | AIMessage {\n if (!AIMessage.isInstance(message) || AIMessageChunk.isInstance(message)) {\n return message;\n }\n\n if (!message.name) {\n return message;\n }\n\n const { name } = message;\n\n if (typeof message.content === \"string\") {\n return new AIMessage({\n ...message.lc_kwargs,\n content: `<name>${name}</name><content>${message.content}</content>`,\n name: undefined,\n });\n }\n\n const updatedContent = [];\n let textBlockCount = 0;\n\n for (const contentBlock of message.content) {\n if (typeof contentBlock === \"string\") {\n textBlockCount += 1;\n updatedContent.push(\n `<name>${name}</name><content>${contentBlock}</content>`\n );\n } else if (\n typeof contentBlock === \"object\" &&\n \"type\" in contentBlock &&\n contentBlock.type === \"text\"\n ) {\n textBlockCount += 1;\n updatedContent.push({\n ...contentBlock,\n text: `<name>${name}</name><content>${contentBlock.text}</content>`,\n });\n } else {\n updatedContent.push(contentBlock);\n }\n }\n\n if (!textBlockCount) {\n updatedContent.unshift({\n type: \"text\",\n text: `<name>${name}</name><content></content>`,\n });\n }\n return new AIMessage({\n ...message.lc_kwargs,\n content: updatedContent as MessageContent,\n name: undefined,\n });\n}\n\n/**\n * Remove explicit name and content XML tags from the AI message content.\n *\n * Examples:\n *\n * @example\n * ```typescript\n * removeInlineAgentName(new AIMessage({ content: \"<name>assistant</name><content>Hello</content>\", name: \"assistant\" }))\n * // AIMessage with content: \"Hello\"\n *\n * removeInlineAgentName(new AIMessage({ content: [{type: \"text\", text: \"<name>assistant</name><content>Hello</content>\"}], name: \"assistant\" }))\n * // AIMessage with content: [{type: \"text\", text: \"Hello\"}]\n * ```\n *\n * @internal\n */\nexport function _removeInlineAgentName<T extends BaseMessage>(message: T): T {\n if (!AIMessage.isInstance(message) || !message.content) {\n return message;\n }\n\n let updatedContent: MessageContent = [];\n let updatedName: string | undefined;\n\n if (Array.isArray(message.content)) {\n updatedContent = message.content\n .filter((block) => {\n if (block.type === \"text\" && typeof block.text === \"string\") {\n const nameMatch = block.text.match(NAME_PATTERN);\n const contentMatch = block.text.match(CONTENT_PATTERN);\n // don't include empty content blocks that were added because there was no text block to modify\n if (nameMatch && (!contentMatch || contentMatch[1] === \"\")) {\n // capture name from text block\n updatedName = nameMatch[1];\n return false;\n }\n return true;\n }\n return true;\n })\n .map((block) => {\n if (block.type === \"text\" && typeof block.text === \"string\") {\n const nameMatch = block.text.match(NAME_PATTERN);\n const contentMatch = block.text.match(CONTENT_PATTERN);\n\n if (!nameMatch || !contentMatch) {\n return block;\n }\n\n // capture name from text block\n updatedName = nameMatch[1];\n\n return {\n ...block,\n text: contentMatch[1],\n };\n }\n return block;\n });\n } else {\n const content = message.content as string;\n const nameMatch = content.match(NAME_PATTERN);\n const contentMatch = content.match(CONTENT_PATTERN);\n\n if (!nameMatch || !contentMatch) {\n return message;\n }\n\n updatedName = nameMatch[1];\n updatedContent = contentMatch[1];\n }\n\n return new AIMessage({\n ...(Object.keys(message.lc_kwargs ?? {}).length > 0\n ? message.lc_kwargs\n : message),\n content: updatedContent,\n name: updatedName,\n }) as T;\n}\n\nexport function isClientTool(\n tool: ClientTool | ServerTool\n): tool is ClientTool {\n return Runnable.isRunnable(tool);\n}\n\n/**\n * Helper function to check if a language model has a bindTools method.\n * @param llm - The language model to check if it has a bindTools method.\n * @returns True if the language model has a bindTools method, false otherwise.\n */\nfunction _isChatModelWithBindTools(\n llm: LanguageModelLike\n): llm is BaseChatModel & Required<Pick<BaseChatModel, \"bindTools\">> {\n if (!isBaseChatModel(llm)) return false;\n return \"bindTools\" in llm && typeof llm.bindTools === \"function\";\n}\n\n/**\n * Helper function to bind tools to a language model.\n * @param llm - The language model to bind tools to.\n * @param toolClasses - The tools to bind to the language model.\n * @param options - The options to pass to the language model.\n * @returns The language model with the tools bound to it.\n */\nconst _simpleBindTools = (\n llm: LanguageModelLike,\n toolClasses: (ClientTool | ServerTool)[],\n options: Partial<BaseChatModelCallOptions> = {}\n) => {\n if (_isChatModelWithBindTools(llm)) {\n return llm.bindTools(toolClasses, options);\n }\n\n if (\n RunnableBinding.isRunnableBinding(llm) &&\n _isChatModelWithBindTools(llm.bound)\n ) {\n const newBound = llm.bound.bindTools(toolClasses, options);\n\n if (RunnableBinding.isRunnableBinding(newBound)) {\n return new RunnableBinding({\n bound: newBound.bound,\n config: { ...llm.config, ...newBound.config },\n kwargs: { ...llm.kwargs, ...newBound.kwargs },\n configFactories: newBound.configFactories ?? llm.configFactories,\n });\n }\n\n return new RunnableBinding({\n bound: newBound,\n config: llm.config,\n kwargs: llm.kwargs,\n configFactories: llm.configFactories,\n });\n }\n\n return null;\n};\n\n/**\n * Check if the LLM already has bound tools and throw if it does.\n *\n * @param llm - The LLM to check.\n * @returns void\n */\nexport function validateLLMHasNoBoundTools(llm: LanguageModelLike): void {\n /**\n * If llm is a function, we can't validate until runtime, so skip\n */\n if (typeof llm === \"function\") {\n return;\n }\n\n let model = llm;\n\n /**\n * If model is a RunnableSequence, find a RunnableBinding in its steps\n */\n if (RunnableSequence.isRunnableSequence(model)) {\n model =\n model.steps.find((step: RunnableLike) =>\n RunnableBinding.isRunnableBinding(step)\n ) || model;\n }\n\n /**\n * If model is configurable, get the underlying model\n */\n if (isConfigurableModel(model)) {\n /**\n * Can't validate async model retrieval in constructor\n */\n return;\n }\n\n /**\n * Check if model is a RunnableBinding with bound tools\n */\n if (RunnableBinding.isRunnableBinding(model)) {\n const hasToolsInKwargs =\n model.kwargs != null &&\n typeof model.kwargs === \"object\" &&\n \"tools\" in model.kwargs &&\n Array.isArray(model.kwargs.tools) &&\n model.kwargs.tools.length > 0;\n\n const hasToolsInConfig =\n model.config != null &&\n typeof model.config === \"object\" &&\n \"tools\" in model.config &&\n Array.isArray(model.config.tools) &&\n model.config.tools.length > 0;\n\n if (hasToolsInKwargs || hasToolsInConfig) {\n throw new MultipleToolsBoundError();\n }\n }\n\n /**\n * Also check if model has tools property directly (e.g., FakeToolCallingModel)\n */\n if (\n \"tools\" in model &&\n model.tools !== undefined &&\n Array.isArray(model.tools) &&\n model.tools.length > 0\n ) {\n throw new MultipleToolsBoundError();\n }\n}\n\n/**\n * Check if the last message in the messages array has tool calls.\n *\n * @param messages - The messages to check.\n * @returns True if the last message has tool calls, false otherwise.\n */\nexport function hasToolCalls(message?: BaseMessage): boolean {\n return Boolean(\n AIMessage.isInstance(message) &&\n message.tool_calls &&\n message.tool_calls.length > 0\n );\n}\n\n/**\n * Normalizes a system prompt to a SystemMessage object.\n * If it's already a SystemMessage, returns it as-is.\n * If it's a string, converts it to a SystemMessage.\n * If it's undefined, creates an empty system message so it is easier to append to it later.\n */\nexport function normalizeSystemPrompt(\n systemPrompt?: string | SystemMessage\n): SystemMessage {\n if (systemPrompt == null) {\n return new SystemMessage(\"\");\n }\n if (SystemMessage.isInstance(systemPrompt)) {\n return systemPrompt;\n }\n if (typeof systemPrompt === \"string\") {\n return new SystemMessage({\n content: [{ type: \"text\", text: systemPrompt }],\n });\n }\n throw new Error(\n `Invalid systemPrompt type: expected string or SystemMessage, got ${typeof systemPrompt}`\n );\n}\n\n/**\n * Helper function to bind tools to a language model.\n * @param llm - The language model to bind tools to.\n * @param toolClasses - The tools to bind to the language model.\n * @param options - The options to pass to the language model.\n * @returns The language model with the tools bound to it.\n */\nexport async function bindTools(\n llm: LanguageModelLike,\n toolClasses: (ClientTool | ServerTool)[],\n options: Partial<BaseChatModelCallOptions> = {}\n): Promise<\n | RunnableSequence<unknown, unknown>\n | RunnableBinding<unknown, unknown, RunnableConfig<Record<string, unknown>>>\n | Runnable<BaseLanguageModelInput, AIMessageChunk, BaseChatModelCallOptions>\n> {\n const model = _simpleBindTools(llm, toolClasses, options);\n if (model) return model;\n\n if (isConfigurableModel(llm)) {\n const model = _simpleBindTools(\n await llm._getModelInstance(),\n toolClasses,\n options\n );\n if (model) return model;\n }\n\n if (RunnableSequence.isRunnableSequence(llm)) {\n const modelStep = llm.steps.findIndex(\n (step) =>\n RunnableBinding.isRunnableBinding(step) ||\n isBaseChatModel(step) ||\n isConfigurableModel(step)\n );\n\n if (modelStep >= 0) {\n const model = _simpleBindTools(\n llm.steps[modelStep],\n toolClasses,\n options\n );\n if (model) {\n const nextSteps: unknown[] = llm.steps.slice();\n nextSteps.splice(modelStep, 1, model);\n\n return RunnableSequence.from(\n nextSteps as [RunnableLike, ...RunnableLike[], RunnableLike]\n );\n }\n }\n }\n\n throw new Error(`llm ${llm} must define bindTools method.`);\n}\n\n/**\n * Compose multiple wrapToolCall handlers into a single middleware stack.\n *\n * Composes handlers so the first in the list becomes the outermost layer.\n * Each handler receives a handler callback to execute inner layers.\n *\n * @param handlers - List of handlers. First handler wraps all others.\n * @returns Composed handler, or undefined if handlers array is empty.\n *\n * @example\n * ```typescript\n * // handlers=[auth, retry] means: auth wraps retry\n * // Flow: auth calls retry, retry calls base handler\n * const auth: ToolCallWrapper = async (request, handler) => {\n * try {\n * return await handler(request);\n * } catch (error) {\n * if (error.message === \"Unauthorized\") {\n * await refreshToken();\n * return await handler(request);\n * }\n * throw error;\n * }\n * };\n *\n * const retry: ToolCallWrapper = async (request, handler) => {\n * for (let attempt = 0; attempt < 3; attempt++) {\n * try {\n * return await handler(request);\n * } catch (error) {\n * if (attempt === 2) throw error;\n * }\n * }\n * throw new Error(\"Unreachable\");\n * };\n *\n * const composedHandler = chainToolCallHandlers([auth, retry]);\n * ```\n */\nfunction chainToolCallHandlers(\n handlers: WrapToolCallHook[]\n): WrapToolCallHook | undefined {\n if (handlers.length === 0) {\n return undefined;\n }\n\n if (handlers.length === 1) {\n return handlers[0];\n }\n\n // Compose two handlers where outer wraps inner\n // The key is to properly propagate request modifications through the chain\n function composeTwo(\n outer: WrapToolCallHook,\n inner: WrapToolCallHook\n ): WrapToolCallHook {\n return async (request, handler) => {\n // Create a wrapper that calls inner with the base handler\n // The innerHandler receives the (possibly modified) request from outer\n // and passes it to inner, which then calls the base handler\n const innerHandler: ToolCallHandler = async (passedRequest) => {\n // inner receives the request passed by outer (which may be modified)\n return inner(passedRequest, handler);\n };\n\n // Call outer with the wrapped inner as its handler\n return outer(request, innerHandler);\n };\n }\n\n // Compose right-to-left: outer(inner(innermost(handler)))\n let result = handlers[handlers.length - 1];\n for (let i = handlers.length - 2; i >= 0; i--) {\n result = composeTwo(handlers[i], result);\n }\n\n return result;\n}\n\n/**\n * Wrapping `wrapToolCall` invocation so we can inject middleware name into\n * the error message.\n *\n * @param middleware list of middleware passed to the agent\n * @param state state of the agent\n * @returns single wrap function\n */\nexport function wrapToolCall(\n middleware: readonly AgentMiddleware<InteropZodObject | undefined>[]\n) {\n const middlewareWithWrapToolCall = middleware.filter((m) => m.wrapToolCall);\n\n if (middlewareWithWrapToolCall.length === 0) {\n return;\n }\n\n return chainToolCallHandlers(\n middlewareWithWrapToolCall.map((m) => {\n const originalHandler = m.wrapToolCall!;\n /**\n * Wrap with error handling and validation\n */\n const wrappedHandler: WrapToolCallHook = async (request, handler) => {\n /**\n * Capture the original state for this middleware's schema parsing.\n * This is important because the request may be modified (via override)\n * as it passes through the middleware chain, but each middleware\n * should always see the full original state for its schema parsing.\n */\n const originalState = request.state;\n\n /**\n * Create a handler that preserves state parsing for this middleware\n * while allowing tool/toolCall/state modifications from inner middleware\n */\n const wrappedInnerHandler: ToolCallHandler = async (passedRequest) => {\n /**\n * Merge the passed request with the original state for parsing.\n * This ensures middleware can override tool/toolCall while\n * maintaining proper state parsing for each middleware in the chain.\n */\n const mergedState = {\n ...originalState,\n ...passedRequest.state,\n };\n return handler({\n ...passedRequest,\n state: mergedState,\n });\n };\n\n try {\n const result = await originalHandler(\n {\n ...request,\n /**\n * override state with the state from the specific middleware\n */\n state: {\n messages: originalState.messages,\n ...(m.stateSchema\n ? parseMiddlewareState(m.stateSchema, { ...originalState })\n : {}),\n },\n } as ToolCallRequest<AgentBuiltInState, unknown>,\n wrappedInnerHandler\n );\n\n /**\n * Validate return type\n */\n if (!ToolMessage.isInstance(result) && !isCommand(result)) {\n throw new Error(\n `Invalid response from \"wrapToolCall\" in middleware \"${m.name}\": ` +\n `expected ToolMessage or Command, got ${typeof result}`\n );\n }\n\n return result;\n } catch (error) {\n throw MiddlewareError.wrap(error, m.name);\n }\n };\n return wrappedHandler;\n })\n );\n}\n"],"mappings":";;;;;;;;AA2CA,MAAM,eAAe;AACrB,MAAM,kBAAkB;;;;;;;;;;;;AAaxB,SAAS,qBACP,aACA,OACyB;AAEzB,KAAIA,qBAAAA,YAAY,WAAW,YAAY,EAAE;EACvC,MAAM,SAAkC,EAAE;AAC1C,OAAK,MAAM,OAAO,OAAO,KAAK,YAAY,OAAO,CAC/C,KAAI,OAAO,MACT,QAAO,OAAO,MAAM;AAGxB,SAAO;;AAIT,MAAA,GAAA,4BAAA,oBAAuB,YAAY,CACjC,SAAA,GAAA,4BAAA,cAAoB,aAAiC,MAAM;AAG7D,OAAM,IAAI,MAAM,8BAA8B,OAAO,cAAc;;;;;;;;;;;;;;;;AAmBrE,SAAgB,oBACd,SACe;AACf,KAAI,CAACC,yBAAAA,UAAU,WAAW,QAAQ,IAAIC,yBAAAA,eAAe,WAAW,QAAQ,CACtE,QAAO;AAGT,KAAI,CAAC,QAAQ,KACX,QAAO;CAGT,MAAM,EAAE,SAAS;AAEjB,KAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,IAAID,yBAAAA,UAAU;EACnB,GAAG,QAAQ;EACX,SAAS,SAAS,KAAK,kBAAkB,QAAQ,QAAQ;EACzD,MAAM,KAAA;EACP,CAAC;CAGJ,MAAM,iBAAiB,EAAE;CACzB,IAAI,iBAAiB;AAErB,MAAK,MAAM,gBAAgB,QAAQ,QACjC,KAAI,OAAO,iBAAiB,UAAU;AACpC,oB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AA2B,KAAK,MAAM;EACpC,MAAM,kBAAkB,EAAE;;;;EAI1B,MAAM,iBAAmC,OAAO,SAAS,YAAY;;;;;;;GAOnE,MAAM,gBAAgB,QAAQ;;;;;GAM9B,MAAM,sBAAuC,OAAO,kBAAkB;;;;;;IAMpE,MAAM,cAAc;KAClB,GAAG;KACH,GAAG,cAAc;KAClB;AACD,WAAO,QAAQ;KACb,GAAG;KACH,OAAO;KACR,CAAC;;AAGJ,OAAI;IACF,MAAM,SAAS,MAAM,gBACnB;KACE,GAAG;KAIH,OAAO;MACL,UAAU,cAAc;MACxB,GAAI,EAAE,cACF,qBAAqB,EAAE,aAAa,EAAE,GAAG,eAAe,CAAC,GACzD,EAAE;MACP;KACF,EACD,oBACD;;;;AAKD,QAAI,CAACI,yBAAAA,YAAY,WAAW,OAAO,IAAI,EAAA,GAAA,qBAAA,WAAW,OAAO,CACvD,OAAM,IAAI,MACR,uDAAuD,EAAE,KAAK,0CACpB,OAAO,SAClD;AAGH,WAAO;YACA,OAAO;AACd,UAAMC,eAAAA,gBAAgB,KAAK,OAAO,EAAE,KAAK;;;AAG7C,SAAO;GACP,CACH"}
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","names":[],"sources":["../../src/agents/utils.ts"],"sourcesContent":["import {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n BaseMessageLike,\n SystemMessage,\n MessageContent,\n ToolMessage,\n} from \"@langchain/core/messages\";\nimport { isCommand, StateSchema } from \"@langchain/langgraph\";\nimport {\n type InteropZodObject,\n interopParse,\n isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n BaseChatModel,\n type BaseChatModelCallOptions,\n} from \"@langchain/core/language_models/chat_models\";\nimport {\n LanguageModelLike,\n BaseLanguageModelInput,\n} from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnableLike,\n RunnableConfig,\n RunnableSequence,\n RunnableBinding,\n} from \"@langchain/core/runnables\";\nimport type { ClientTool, ServerTool } from \"@langchain/core/tools\";\n\nimport { isBaseChatModel, isConfigurableModel } from \"./model.js\";\nimport { MultipleToolsBoundError, MiddlewareError } from \"./errors.js\";\nimport type { AgentBuiltInState } from \"./runtime.js\";\nimport type {\n ToolCallHandler,\n AgentMiddleware,\n ToolCallRequest,\n WrapToolCallHook,\n} from \"./middleware/types.js\";\n\nconst NAME_PATTERN = /<name>(.*?)<\\/name>/s;\nconst CONTENT_PATTERN = /<content>(.*?)<\\/content>/s;\n\n/**\n * Parse middleware state from the full agent state based on the middleware's stateSchema.\n *\n * Handles two types of state schemas:\n * 1. Zod schemas (v3 or v4) - parsed using interopParse\n * 2. LangGraph StateSchema - extracts only the keys defined in `fields`\n *\n * @param stateSchema - The middleware's state schema (Zod or LangGraph StateSchema)\n * @param state - The full agent state to parse from\n * @returns Parsed state containing only the keys defined in the schema\n */\nfunction parseMiddlewareState(\n stateSchema: unknown,\n state: Record<string, unknown>\n): Record<string, unknown> {\n // Handle LangGraph StateSchema (has `fields` property)\n if (StateSchema.isInstance(stateSchema)) {\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(stateSchema.fields)) {\n if (key in state) {\n result[key] = state[key];\n }\n }\n return result;\n }\n\n // Handle Zod schemas using interopParse\n if (isInteropZodSchema(stateSchema)) {\n return interopParse(stateSchema as InteropZodObject, state);\n }\n\n throw new Error(`Invalid state schema type: ${typeof stateSchema}`);\n}\n\nexport type AgentNameMode = \"inline\";\n\n/**\n * Attach formatted agent names to the messages passed to and from a language model.\n *\n * This is useful for making a message history with multiple agents more coherent.\n *\n * NOTE: agent name is consumed from the message.name field.\n * If you're using an agent built with createAgent, name is automatically set.\n * If you're building a custom agent, make sure to set the name on the AI message returned by the LLM.\n *\n * @param message - Message to add agent name formatting to\n * @returns Message with agent name formatting\n *\n * @internal\n */\nexport function _addInlineAgentName<T extends BaseMessageLike>(\n message: T\n): T | AIMessage {\n if (!AIMessage.isInstance(message) || AIMessageChunk.isInstance(message)) {\n return message;\n }\n\n if (!message.name) {\n return message;\n }\n\n const { name } = message;\n\n if (typeof message.content === \"string\") {\n return new AIMessage({\n ...message.lc_kwargs,\n content: `<name>${name}</name><content>${message.content}</content>`,\n name: undefined,\n });\n }\n\n const updatedContent = [];\n let textBlockCount = 0;\n\n for (const contentBlock of message.content) {\n if (typeof contentBlock === \"string\") {\n textBlockCount += 1;\n updatedContent.push(\n `<name>${name}</name><content>${contentBlock}</content>`\n );\n } else if (\n typeof contentBlock === \"object\" &&\n \"type\" in contentBlock &&\n contentBlock.type === \"text\"\n ) {\n textBlockCount += 1;\n updatedContent.push({\n ...contentBlock,\n text: `<name>${name}</name><content>${contentBlock.text}</content>`,\n });\n } else {\n updatedContent.push(contentBlock);\n }\n }\n\n if (!textBlockCount) {\n updatedContent.unshift({\n type: \"text\",\n text: `<name>${name}</name><content></content>`,\n });\n }\n return new AIMessage({\n ...message.lc_kwargs,\n content: updatedContent as MessageContent,\n name: undefined,\n });\n}\n\n/**\n * Remove explicit name and content XML tags from the AI message content.\n *\n * Examples:\n *\n * @example\n * ```typescript\n * removeInlineAgentName(new AIMessage({ content: \"<name>assistant</name><content>Hello</content>\", name: \"assistant\" }))\n * // AIMessage with content: \"Hello\"\n *\n * removeInlineAgentName(new AIMessage({ content: [{type: \"text\", text: \"<name>assistant</name><content>Hello</content>\"}], name: \"assistant\" }))\n * // AIMessage with content: [{type: \"text\", text: \"Hello\"}]\n * ```\n *\n * @internal\n */\nexport function _removeInlineAgentName<T extends BaseMessage>(message: T): T {\n if (!AIMessage.isInstance(message) || !message.content) {\n return message;\n }\n\n let updatedContent: MessageContent = [];\n let updatedName: string | undefined;\n\n if (Array.isArray(message.content)) {\n updatedContent = message.content\n .filter((block) => {\n if (block.type === \"text\" && typeof block.text === \"string\") {\n const nameMatch = block.text.match(NAME_PATTERN);\n const contentMatch = block.text.match(CONTENT_PATTERN);\n // don't include empty content blocks that were added because there was no text block to modify\n if (nameMatch && (!contentMatch || contentMatch[1] === \"\")) {\n // capture name from text block\n updatedName = nameMatch[1];\n return false;\n }\n return true;\n }\n return true;\n })\n .map((block) => {\n if (block.type === \"text\" && typeof block.text === \"string\") {\n const nameMatch = block.text.match(NAME_PATTERN);\n const contentMatch = block.text.match(CONTENT_PATTERN);\n\n if (!nameMatch || !contentMatch) {\n return block;\n }\n\n // capture name from text block\n updatedName = nameMatch[1];\n\n return {\n ...block,\n text: contentMatch[1],\n };\n }\n return block;\n });\n } else {\n const content = message.content as string;\n const nameMatch = content.match(NAME_PATTERN);\n const contentMatch = content.match(CONTENT_PATTERN);\n\n if (!nameMatch || !contentMatch) {\n return message;\n }\n\n updatedName = nameMatch[1];\n updatedContent = contentMatch[1];\n }\n\n return new AIMessage({\n ...(Object.keys(message.lc_kwargs ?? {}).length > 0\n ? message.lc_kwargs\n : message),\n content: updatedContent,\n name: updatedName,\n }) as T;\n}\n\nexport function isClientTool(\n tool: ClientTool | ServerTool\n): tool is ClientTool {\n return Runnable.isRunnable(tool);\n}\n\n/**\n * Helper function to check if a language model has a bindTools method.\n * @param llm - The language model to check if it has a bindTools method.\n * @returns True if the language model has a bindTools method, false otherwise.\n */\nfunction _isChatModelWithBindTools(\n llm: LanguageModelLike\n): llm is BaseChatModel & Required<Pick<BaseChatModel, \"bindTools\">> {\n if (!isBaseChatModel(llm)) return false;\n return \"bindTools\" in llm && typeof llm.bindTools === \"function\";\n}\n\n/**\n * Helper function to bind tools to a language model.\n * @param llm - The language model to bind tools to.\n * @param toolClasses - The tools to bind to the language model.\n * @param options - The options to pass to the language model.\n * @returns The language model with the tools bound to it.\n */\nconst _simpleBindTools = (\n llm: LanguageModelLike,\n toolClasses: (ClientTool | ServerTool)[],\n options: Partial<BaseChatModelCallOptions> = {}\n) => {\n if (_isChatModelWithBindTools(llm)) {\n return llm.bindTools(toolClasses, options);\n }\n\n if (\n RunnableBinding.isRunnableBinding(llm) &&\n _isChatModelWithBindTools(llm.bound)\n ) {\n const newBound = llm.bound.bindTools(toolClasses, options);\n\n if (RunnableBinding.isRunnableBinding(newBound)) {\n return new RunnableBinding({\n bound: newBound.bound,\n config: { ...llm.config, ...newBound.config },\n kwargs: { ...llm.kwargs, ...newBound.kwargs },\n configFactories: newBound.configFactories ?? llm.configFactories,\n });\n }\n\n return new RunnableBinding({\n bound: newBound,\n config: llm.config,\n kwargs: llm.kwargs,\n configFactories: llm.configFactories,\n });\n }\n\n return null;\n};\n\n/**\n * Check if the LLM already has bound tools and throw if it does.\n *\n * @param llm - The LLM to check.\n * @returns void\n */\nexport function validateLLMHasNoBoundTools(llm: LanguageModelLike): void {\n /**\n * If llm is a function, we can't validate until runtime, so skip\n */\n if (typeof llm === \"function\") {\n return;\n }\n\n let model = llm;\n\n /**\n * If model is a RunnableSequence, find a RunnableBinding in its steps\n */\n if (RunnableSequence.isRunnableSequence(model)) {\n model =\n model.steps.find((step: RunnableLike) =>\n RunnableBinding.isRunnableBinding(step)\n ) || model;\n }\n\n /**\n * If model is configurable, get the underlying model\n */\n if (isConfigurableModel(model)) {\n /**\n * Can't validate async model retrieval in constructor\n */\n return;\n }\n\n /**\n * Check if model is a RunnableBinding with bound tools\n */\n if (RunnableBinding.isRunnableBinding(model)) {\n const hasToolsInKwargs =\n model.kwargs != null &&\n typeof model.kwargs === \"object\" &&\n \"tools\" in model.kwargs &&\n Array.isArray(model.kwargs.tools) &&\n model.kwargs.tools.length > 0;\n\n const hasToolsInConfig =\n model.config != null &&\n typeof model.config === \"object\" &&\n \"tools\" in model.config &&\n Array.isArray(model.config.tools) &&\n model.config.tools.length > 0;\n\n if (hasToolsInKwargs || hasToolsInConfig) {\n throw new MultipleToolsBoundError();\n }\n }\n\n /**\n * Also check if model has tools property directly (e.g., FakeToolCallingModel)\n */\n if (\n \"tools\" in model &&\n model.tools !== undefined &&\n Array.isArray(model.tools) &&\n model.tools.length > 0\n ) {\n throw new MultipleToolsBoundError();\n }\n}\n\n/**\n * Check if the last message in the messages array has tool calls.\n *\n * @param messages - The messages to check.\n * @returns True if the last message has tool calls, false otherwise.\n */\nexport function hasToolCalls(message?: BaseMessage): boolean {\n return Boolean(\n AIMessage.isInstance(message) &&\n message.tool_calls &&\n message.tool_calls.length > 0\n );\n}\n\n/**\n * Normalizes a system prompt to a SystemMessage object.\n * If it's already a SystemMessage, returns it as-is.\n * If it's a string, converts it to a SystemMessage.\n * If it's undefined, creates an empty system message so it is easier to append to it later.\n */\nexport function normalizeSystemPrompt(\n systemPrompt?: string | SystemMessage\n): SystemMessage {\n if (systemPrompt == null) {\n return new SystemMessage(\"\");\n }\n if (SystemMessage.isInstance(systemPrompt)) {\n return systemPrompt;\n }\n if (typeof systemPrompt === \"string\") {\n return new SystemMessage({\n content: [{ type: \"text\", text: systemPrompt }],\n });\n }\n throw new Error(\n `Invalid systemPrompt type: expected string or SystemMessage, got ${typeof systemPrompt}`\n );\n}\n\n/**\n * Helper function to bind tools to a language model.\n * @param llm - The language model to bind tools to.\n * @param toolClasses - The tools to bind to the language model.\n * @param options - The options to pass to the language model.\n * @returns The language model with the tools bound to it.\n */\nexport async function bindTools(\n llm: LanguageModelLike,\n toolClasses: (ClientTool | ServerTool)[],\n options: Partial<BaseChatModelCallOptions> = {}\n): Promise<\n | RunnableSequence<unknown, unknown>\n | RunnableBinding<unknown, unknown, RunnableConfig<Record<string, unknown>>>\n | Runnable<BaseLanguageModelInput, AIMessageChunk, BaseChatModelCallOptions>\n> {\n const model = _simpleBindTools(llm, toolClasses, options);\n if (model) return model;\n\n if (isConfigurableModel(llm)) {\n const model = _simpleBindTools(\n await llm._getModelInstance(),\n toolClasses,\n options\n );\n if (model) return model;\n }\n\n if (RunnableSequence.isRunnableSequence(llm)) {\n const modelStep = llm.steps.findIndex(\n (step) =>\n RunnableBinding.isRunnableBinding(step) ||\n isBaseChatModel(step) ||\n isConfigurableModel(step)\n );\n\n if (modelStep >= 0) {\n const model = _simpleBindTools(\n llm.steps[modelStep],\n toolClasses,\n options\n );\n if (model) {\n const nextSteps: unknown[] = llm.steps.slice();\n nextSteps.splice(modelStep, 1, model);\n\n return RunnableSequence.from(\n nextSteps as [RunnableLike, ...RunnableLike[], RunnableLike]\n );\n }\n }\n }\n\n throw new Error(`llm ${llm} must define bindTools method.`);\n}\n\n/**\n * Compose multiple wrapToolCall handlers into a single middleware stack.\n *\n * Composes handlers so the first in the list becomes the outermost layer.\n * Each handler receives a handler callback to execute inner layers.\n *\n * @param handlers - List of handlers. First handler wraps all others.\n * @returns Composed handler, or undefined if handlers array is empty.\n *\n * @example\n * ```typescript\n * // handlers=[auth, retry] means: auth wraps retry\n * // Flow: auth calls retry, retry calls base handler\n * const auth: ToolCallWrapper = async (request, handler) => {\n * try {\n * return await handler(request);\n * } catch (error) {\n * if (error.message === \"Unauthorized\") {\n * await refreshToken();\n * return await handler(request);\n * }\n * throw error;\n * }\n * };\n *\n * const retry: ToolCallWrapper = async (request, handler) => {\n * for (let attempt = 0; attempt < 3; attempt++) {\n * try {\n * return await handler(request);\n * } catch (error) {\n * if (attempt === 2) throw error;\n * }\n * }\n * throw new Error(\"Unreachable\");\n * };\n *\n * const composedHandler = chainToolCallHandlers([auth, retry]);\n * ```\n */\nfunction chainToolCallHandlers(\n handlers: WrapToolCallHook[]\n): WrapToolCallHook | undefined {\n if (handlers.length === 0) {\n return undefined;\n }\n\n if (handlers.length === 1) {\n return handlers[0];\n }\n\n // Compose two handlers where outer wraps inner\n // The key is to properly propagate request modifications through the chain\n function composeTwo(\n outer: WrapToolCallHook,\n inner: WrapToolCallHook\n ): WrapToolCallHook {\n return async (request, handler) => {\n // Create a wrapper that calls inner with the base handler\n // The innerHandler receives the (possibly modified) request from outer\n // and passes it to inner, which then calls the base handler\n const innerHandler: ToolCallHandler = async (passedRequest) => {\n // inner receives the request passed by outer (which may be modified)\n return inner(passedRequest, handler);\n };\n\n // Call outer with the wrapped inner as its handler\n return outer(request, innerHandler);\n };\n }\n\n // Compose right-to-left: outer(inner(innermost(handler)))\n let result = handlers[handlers.length - 1];\n for (let i = handlers.length - 2; i >= 0; i--) {\n result = composeTwo(handlers[i], result);\n }\n\n return result;\n}\n\n/**\n * Wrapping `wrapToolCall` invocation so we can inject middleware name into\n * the error message.\n *\n * @param middleware list of middleware passed to the agent\n * @param state state of the agent\n * @returns single wrap function\n */\nexport function wrapToolCall(\n middleware: readonly AgentMiddleware<InteropZodObject | undefined>[]\n) {\n const middlewareWithWrapToolCall = middleware.filter((m) => m.wrapToolCall);\n\n if (middlewareWithWrapToolCall.length === 0) {\n return;\n }\n\n return chainToolCallHandlers(\n middlewareWithWrapToolCall.map((m) => {\n const originalHandler = m.wrapToolCall!;\n /**\n * Wrap with error handling and validation\n */\n const wrappedHandler: WrapToolCallHook = async (request, handler) => {\n /**\n * Capture the original state for this middleware's schema parsing.\n * This is important because the request may be modified (via override)\n * as it passes through the middleware chain, but each middleware\n * should always see the full original state for its schema parsing.\n */\n const originalState = request.state;\n\n /**\n * Create a handler that preserves state parsing for this middleware\n * while allowing tool/toolCall/state modifications from inner middleware\n */\n const wrappedInnerHandler: ToolCallHandler = async (passedRequest) => {\n /**\n * Merge the passed request with the original state for parsing.\n * This ensures middleware can override tool/toolCall while\n * maintaining proper state parsing for each middleware in the chain.\n */\n const mergedState = {\n ...originalState,\n ...passedRequest.state,\n };\n return handler({\n ...passedRequest,\n state: mergedState,\n });\n };\n\n try {\n const result = await originalHandler(\n {\n ...request,\n /**\n * override state with the state from the specific middleware\n */\n state: {\n messages: originalState.messages,\n ...(m.stateSchema\n ? parseMiddlewareState(m.stateSchema, { ...originalState })\n : {}),\n },\n } as ToolCallRequest<AgentBuiltInState, unknown>,\n wrappedInnerHandler\n );\n\n /**\n * Validate return type\n */\n if (!ToolMessage.isInstance(result) && !isCommand(result)) {\n throw new Error(\n `Invalid response from \"wrapToolCall\" in middleware \"${m.name}\": ` +\n `expected ToolMessage or Command, got ${typeof result}`\n );\n }\n\n return result;\n } catch (error) {\n throw MiddlewareError.wrap(error, m.name);\n }\n };\n return wrappedHandler;\n })\n );\n}\n"],"mappings":";;;;;;;AA0CA,MAAM,eAAe;AACrB,MAAM,kBAAkB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file":"utils.js","names":[],"sources":["../../src/agents/utils.ts"],"sourcesContent":["import {\n AIMessage,\n AIMessageChunk,\n BaseMessage,\n BaseMessageLike,\n SystemMessage,\n MessageContent,\n ToolMessage,\n} from \"@langchain/core/messages\";\nimport { isCommand, StateSchema } from \"@langchain/langgraph\";\nimport {\n type InteropZodObject,\n interopParse,\n isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n BaseChatModel,\n type BaseChatModelCallOptions,\n} from \"@langchain/core/language_models/chat_models\";\nimport { BaseLanguageModelInput } from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnableLike,\n RunnableConfig,\n RunnableSequence,\n RunnableBinding,\n} from \"@langchain/core/runnables\";\nimport type { ClientTool, ServerTool } from \"@langchain/core/tools\";\n\nimport {\n isBaseChatModel,\n isConfigurableModel,\n type AgentLanguageModelLike as LanguageModelLike,\n} from \"./model.js\";\nimport { MultipleToolsBoundError, MiddlewareError } from \"./errors.js\";\nimport type { AgentBuiltInState } from \"./runtime.js\";\nimport type {\n ToolCallHandler,\n AgentMiddleware,\n ToolCallRequest,\n WrapToolCallHook,\n} from \"./middleware/types.js\";\n\nconst NAME_PATTERN = /<name>(.*?)<\\/name>/s;\nconst CONTENT_PATTERN = /<content>(.*?)<\\/content>/s;\n\n/**\n * Parse middleware state from the full agent state based on the middleware's stateSchema.\n *\n * Handles two types of state schemas:\n * 1. Zod schemas (v3 or v4) - parsed using interopParse\n * 2. LangGraph StateSchema - extracts only the keys defined in `fields`\n *\n * @param stateSchema - The middleware's state schema (Zod or LangGraph StateSchema)\n * @param state - The full agent state to parse from\n * @returns Parsed state containing only the keys defined in the schema\n */\nfunction parseMiddlewareState(\n stateSchema: unknown,\n state: Record<string, unknown>\n): Record<string, unknown> {\n // Handle LangGraph StateSchema (has `fields` property)\n if (StateSchema.isInstance(stateSchema)) {\n const result: Record<string, unknown> = {};\n for (const key of Object.keys(stateSchema.fields)) {\n if (key in state) {\n result[key] = state[key];\n }\n }\n return result;\n }\n\n // Handle Zod schemas using interopParse\n if (isInteropZodSchema(stateSchema)) {\n return interopParse(stateSchema as InteropZodObject, state);\n }\n\n throw new Error(`Invalid state schema type: ${typeof stateSchema}`);\n}\n\nexport type AgentNameMode = \"inline\";\n\n/**\n * Attach formatted agent names to the messages passed to and from a language model.\n *\n * This is useful for making a message history with multiple agents more coherent.\n *\n * NOTE: agent name is consumed from the message.name field.\n * If you're using an agent built with createAgent, name is automatically set.\n * If you're building a custom agent, make sure to set the name on the AI message returned by the LLM.\n *\n * @param message - Message to add agent name formatting to\n * @returns Message with agent name formatting\n *\n * @internal\n */\nexport function _addInlineAgentName<T extends BaseMessageLike>(\n message: T\n): T | AIMessage {\n if (!AIMessage.isInstance(message) || AIMessageChunk.isInstance(message)) {\n return message;\n }\n\n if (!message.name) {\n return message;\n }\n\n const { name } = message;\n\n if (typeof message.content === \"string\") {\n return new AIMessage({\n ...message.lc_kwargs,\n content: `<name>${name}</name><content>${message.content}</content>`,\n name: undefined,\n });\n }\n\n const updatedContent = [];\n let textBlockCount = 0;\n\n for (const contentBlock of message.content) {\n if (typeof contentBlock === \"string\") {\n textBlockCount += 1;\n updatedContent.push(\n `<name>${name}</name><content>${contentBlock}</content>`\n );\n } else if (\n typeof contentBlock === \"object\" &&\n \"type\" in contentBlock &&\n contentBlock.type === \"text\"\n ) {\n textBlockCount += 1;\n updatedContent.push({\n ...contentBlock,\n text: `<name>${name}</name><content>${contentBlock.text}</content>`,\n });\n } else {\n updatedContent.push(contentBlock);\n }\n }\n\n if (!textBlockCount) {\n updatedContent.unshift({\n type: \"text\",\n text: `<name>${name}</name><content></content>`,\n });\n }\n return new AIMessage({\n ...message.lc_kwargs,\n content: updatedContent as MessageContent,\n name: undefined,\n });\n}\n\n/**\n * Remove explicit name and content XML tags from the AI message content.\n *\n * Examples:\n *\n * @example\n * ```typescript\n * removeInlineAgentName(new AIMessage({ content: \"<name>assistant</name><content>Hello</content>\", name: \"assistant\" }))\n * // AIMessage with content: \"Hello\"\n *\n * removeInlineAgentName(new AIMessage({ content: [{type: \"text\", text: \"<name>assistant</name><content>Hello</content>\"}], name: \"assistant\" }))\n * // AIMessage with content: [{type: \"text\", text: \"Hello\"}]\n * ```\n *\n * @internal\n */\nexport function _removeInlineAgentName<T extends BaseMessage>(message: T): T {\n if (!AIMessage.isInstance(message) || !message.content) {\n return message;\n }\n\n let updatedContent: MessageContent = [];\n let updatedName: string | undefined;\n\n if (Array.isArray(message.content)) {\n updatedContent = message.content\n .filter((block) => {\n if (block.type === \"text\" && typeof block.text === \"string\") {\n const nameMatch = block.text.match(NAME_PATTERN);\n const contentMatch = block.text.match(CONTENT_PATTERN);\n // don't include empty content blocks that were added because there was no text block to modify\n if (nameMatch && (!contentMatch || contentMatch[1] === \"\")) {\n // capture name from text block\n updatedName = nameMatch[1];\n return false;\n }\n return true;\n }\n return true;\n })\n .map((block) => {\n if (block.type === \"text\" && typeof block.text === \"string\") {\n const nameMatch = block.text.match(NAME_PATTERN);\n const contentMatch = block.text.match(CONTENT_PATTERN);\n\n if (!nameMatch || !contentMatch) {\n return block;\n }\n\n // capture name from text block\n updatedName = nameMatch[1];\n\n return {\n ...block,\n text: contentMatch[1],\n };\n }\n return block;\n });\n } else {\n const content = message.content as string;\n const nameMatch = content.match(NAME_PATTERN);\n const contentMatch = content.match(CONTENT_PATTERN);\n\n if (!nameMatch || !contentMatch) {\n return message;\n }\n\n updatedName = nameMatch[1];\n updatedContent = contentMatch[1];\n }\n\n return new AIMessage({\n ...(Object.keys(message.lc_kwargs ?? {}).length > 0\n ? message.lc_kwargs\n : message),\n content: updatedContent,\n name: updatedName,\n }) as T;\n}\n\nexport function isClientTool(\n tool: ClientTool | ServerTool\n): tool is ClientTool {\n return Runnable.isRunnable(tool);\n}\n\n/**\n * Helper function to check if a language model has a bindTools method.\n * @param llm - The language model to check if it has a bindTools method.\n * @returns True if the language model has a bindTools method, false otherwise.\n */\nfunction _isChatModelWithBindTools(\n llm: LanguageModelLike\n): llm is BaseChatModel & Required<Pick<BaseChatModel, \"bindTools\">> {\n if (!isBaseChatModel(llm)) return false;\n return \"bindTools\" in llm && typeof llm.bindTools === \"function\";\n}\n\n/**\n * Helper function to bind tools to a language model.\n * @param llm - The language model to bind tools to.\n * @param toolClasses - The tools to bind to the language model.\n * @param options - The options to pass to the language model.\n * @returns The language model with the tools bound to it.\n */\nconst _simpleBindTools = (\n llm: LanguageModelLike,\n toolClasses: (ClientTool | ServerTool)[],\n options: Partial<BaseChatModelCallOptions> = {}\n) => {\n if (_isChatModelWithBindTools(llm)) {\n return llm.bindTools(toolClasses, options);\n }\n\n if (\n RunnableBinding.isRunnableBinding(llm) &&\n _isChatModelWithBindTools(llm.bound)\n ) {\n const newBound = llm.bound.bindTools(toolClasses, options);\n\n if (RunnableBinding.isRunnableBinding(newBound)) {\n return new RunnableBinding({\n bound: newBound.bound,\n config: { ...llm.config, ...newBound.config },\n kwargs: { ...llm.kwargs, ...newBound.kwargs },\n configFactories: newBound.configFactories ?? llm.configFactories,\n });\n }\n\n return new RunnableBinding({\n bound: newBound,\n config: llm.config,\n kwargs: llm.kwargs,\n configFactories: llm.configFactories,\n });\n }\n\n return null;\n};\n\n/**\n * Check if the LLM already has bound tools and throw if it does.\n *\n * @param llm - The LLM to check.\n * @returns void\n */\nexport function validateLLMHasNoBoundTools(llm: LanguageModelLike): void {\n /**\n * If llm is a function, we can't validate until runtime, so skip\n */\n if (typeof llm === \"function\") {\n return;\n }\n\n let model = llm;\n\n /**\n * If model is a RunnableSequence, find a RunnableBinding in its steps\n */\n if (RunnableSequence.isRunnableSequence(model)) {\n model =\n model.steps.find((step: RunnableLike) =>\n RunnableBinding.isRunnableBinding(step)\n ) || model;\n }\n\n /**\n * If model is configurable, get the underlying model\n */\n if (isConfigurableModel(model)) {\n /**\n * Can't validate async model retrieval in constructor\n */\n return;\n }\n\n /**\n * Check if model is a RunnableBinding with bound tools\n */\n if (RunnableBinding.isRunnableBinding(model)) {\n const hasToolsInKwargs =\n model.kwargs != null &&\n typeof model.kwargs === \"object\" &&\n \"tools\" in model.kwargs &&\n Array.isArray(model.kwargs.tools) &&\n model.kwargs.tools.length > 0;\n\n const hasToolsInConfig =\n model.config != null &&\n typeof model.config === \"object\" &&\n \"tools\" in model.config &&\n Array.isArray(model.config.tools) &&\n model.config.tools.length > 0;\n\n if (hasToolsInKwargs || hasToolsInConfig) {\n throw new MultipleToolsBoundError();\n }\n }\n\n /**\n * Also check if model has tools property directly (e.g., FakeToolCallingModel)\n */\n if (\n \"tools\" in model &&\n model.tools !== undefined &&\n Array.isArray(model.tools) &&\n model.tools.length > 0\n ) {\n throw new MultipleToolsBoundError();\n }\n}\n\n/**\n * Check if the last message in the messages array has tool calls.\n *\n * @param messages - The messages to check.\n * @returns True if the last message has tool calls, false otherwise.\n */\nexport function hasToolCalls(message?: BaseMessage): boolean {\n return Boolean(\n AIMessage.isInstance(message) &&\n message.tool_calls &&\n message.tool_calls.length > 0\n );\n}\n\n/**\n * Normalizes a system prompt to a SystemMessage object.\n * If it's already a SystemMessage, returns it as-is.\n * If it's a string, converts it to a SystemMessage.\n * If it's undefined, creates an empty system message so it is easier to append to it later.\n */\nexport function normalizeSystemPrompt(\n systemPrompt?: string | SystemMessage\n): SystemMessage {\n if (systemPrompt == null) {\n return new SystemMessage(\"\");\n }\n if (SystemMessage.isInstance(systemPrompt)) {\n return systemPrompt;\n }\n if (typeof systemPrompt === \"string\") {\n return new SystemMessage({\n content: [{ type: \"text\", text: systemPrompt }],\n });\n }\n throw new Error(\n `Invalid systemPrompt type: expected string or SystemMessage, got ${typeof systemPrompt}`\n );\n}\n\n/**\n * Helper function to bind tools to a language model.\n * @param llm - The language model to bind tools to.\n * @param toolClasses - The tools to bind to the language model.\n * @param options - The options to pass to the language model.\n * @returns The language model with the tools bound to it.\n */\nexport async function bindTools(\n llm: LanguageModelLike,\n toolClasses: (ClientTool | ServerTool)[],\n options: Partial<BaseChatModelCallOptions> = {}\n): Promise<\n | RunnableSequence<unknown, unknown>\n | RunnableBinding<unknown, unknown, RunnableConfig<Record<string, unknown>>>\n | Runnable<BaseLanguageModelInput, AIMessageChunk, BaseChatModelCallOptions>\n> {\n const model = _simpleBindTools(llm, toolClasses, options);\n if (model) return model;\n\n if (isConfigurableModel(llm)) {\n const model = _simpleBindTools(\n await llm._getModelInstance(),\n toolClasses,\n options\n );\n if (model) return model;\n }\n\n if (RunnableSequence.isRunnableSequence(llm)) {\n const modelStep = llm.steps.findIndex(\n (step) =>\n RunnableBinding.isRunnableBinding(step) ||\n isBaseChatModel(step) ||\n isConfigurableModel(step)\n );\n\n if (modelStep >= 0) {\n const model = _simpleBindTools(\n llm.steps[modelStep],\n toolClasses,\n options\n );\n if (model) {\n const nextSteps: unknown[] = llm.steps.slice();\n nextSteps.splice(modelStep, 1, model);\n\n return RunnableSequence.from(\n nextSteps as [RunnableLike, ...RunnableLike[], RunnableLike]\n );\n }\n }\n }\n\n throw new Error(`llm ${llm} must define bindTools method.`);\n}\n\n/**\n * Compose multiple wrapToolCall handlers into a single middleware stack.\n *\n * Composes handlers so the first in the list becomes the outermost layer.\n * Each handler receives a handler callback to execute inner layers.\n *\n * @param handlers - List of handlers. First handler wraps all others.\n * @returns Composed handler, or undefined if handlers array is empty.\n *\n * @example\n * ```typescript\n * // handlers=[auth, retry] means: auth wraps retry\n * // Flow: auth calls retry, retry calls base handler\n * const auth: ToolCallWrapper = async (request, handler) => {\n * try {\n * return await handler(request);\n * } catch (error) {\n * if (error.message === \"Unauthorized\") {\n * await refreshToken();\n * return await handler(request);\n * }\n * throw error;\n * }\n * };\n *\n * const retry: ToolCallWrapper = async (request, handler) => {\n * for (let attempt = 0; attempt < 3; attempt++) {\n * try {\n * return await handler(request);\n * } catch (error) {\n * if (attempt === 2) throw error;\n * }\n * }\n * throw new Error(\"Unreachable\");\n * };\n *\n * const composedHandler = chainToolCallHandlers([auth, retry]);\n * ```\n */\nfunction chainToolCallHandlers(\n handlers: WrapToolCallHook[]\n): WrapToolCallHook | undefined {\n if (handlers.length === 0) {\n return undefined;\n }\n\n if (handlers.length === 1) {\n return handlers[0];\n }\n\n // Compose two handlers where outer wraps inner\n // The key is to properly propagate request modifications through the chain\n function composeTwo(\n outer: WrapToolCallHook,\n inner: WrapToolCallHook\n ): WrapToolCallHook {\n return async (request, handler) => {\n // Create a wrapper that calls inner with the base handler\n // The innerHandler receives the (possibly modified) request from outer\n // and passes it to inner, which then calls the base handler\n const innerHandler: ToolCallHandler = async (passedRequest) => {\n // inner receives the request passed by outer (which may be modified)\n return inner(passedRequest, handler);\n };\n\n // Call outer with the wrapped inner as its handler\n return outer(request, innerHandler);\n };\n }\n\n // Compose right-to-left: outer(inner(innermost(handler)))\n let result = handlers[handlers.length - 1];\n for (let i = handlers.length - 2; i >= 0; i--) {\n result = composeTwo(handlers[i], result);\n }\n\n return result;\n}\n\n/**\n * Wrapping `wrapToolCall` invocation so we can inject middleware name into\n * the error message.\n *\n * @param middleware list of middleware passed to the agent\n * @param state state of the agent\n * @returns single wrap function\n */\nexport function wrapToolCall(\n middleware: readonly AgentMiddleware<InteropZodObject | undefined>[]\n) {\n const middlewareWithWrapToolCall = middleware.filter((m) => m.wrapToolCall);\n\n if (middlewareWithWrapToolCall.length === 0) {\n return;\n }\n\n return chainToolCallHandlers(\n middlewareWithWrapToolCall.map((m) => {\n const originalHandler = m.wrapToolCall!;\n /**\n * Wrap with error handling and validation\n */\n const wrappedHandler: WrapToolCallHook = async (request, handler) => {\n /**\n * Capture the original state for this middleware's schema parsing.\n * This is important because the request may be modified (via override)\n * as it passes through the middleware chain, but each middleware\n * should always see the full original state for its schema parsing.\n */\n const originalState = request.state;\n\n /**\n * Create a handler that preserves state parsing for this middleware\n * while allowing tool/toolCall/state modifications from inner middleware\n */\n const wrappedInnerHandler: ToolCallHandler = async (passedRequest) => {\n /**\n * Merge the passed request with the original state for parsing.\n * This ensures middleware can override tool/toolCall while\n * maintaining proper state parsing for each middleware in the chain.\n */\n const mergedState = {\n ...originalState,\n ...passedRequest.state,\n };\n return handler({\n ...passedRequest,\n state: mergedState,\n });\n };\n\n try {\n const result = await originalHandler(\n {\n ...request,\n /**\n * override state with the state from the specific middleware\n */\n state: {\n messages: originalState.messages,\n ...(m.stateSchema\n ? parseMiddlewareState(m.stateSchema, { ...originalState })\n : {}),\n },\n } as ToolCallRequest<AgentBuiltInState, unknown>,\n wrappedInnerHandler\n );\n\n /**\n * Validate return type\n */\n if (!ToolMessage.isInstance(result) && !isCommand(result)) {\n throw new Error(\n `Invalid response from \"wrapToolCall\" in middleware \"${m.name}\": ` +\n `expected ToolMessage or Command, got ${typeof result}`\n );\n }\n\n return result;\n } catch (error) {\n throw MiddlewareError.wrap(error, m.name);\n }\n };\n return wrappedHandler;\n })\n );\n}\n"],"mappings":";;;;;;;AA2CA,MAAM,eAAe;AACrB,MAAM,kBAAkB;;;;;;;;;;;;AAaxB,SAAS,qBACP,aACA,OACyB;AAEzB,KAAI,YAAY,WAAW,YAAY,EAAE;EACvC,MAAM,SAAkC,EAAE;AAC1C,OAAK,MAAM,OAAO,OAAO,KAAK,YAAY,OAAO,CAC/C,KAAI,OAAO,MACT,QAAO,OAAO,MAAM;AAGxB,SAAO;;AAIT,KAAI,mBAAmB,YAAY,CACjC,QAAO,aAAa,aAAiC,MAAM;AAG7D,OAAM,IAAI,MAAM,8BAA8B,OAAO,cAAc;;;;;;;;;;;;;;;;AAmBrE,SAAgB,oBACd,SACe;AACf,KAAI,CAAC,UAAU,WAAW,QAAQ,IAAI,eAAe,WAAW,QAAQ,CACtE,QAAO;AAGT,KAAI,CAAC,QAAQ,KACX,QAAO;CAGT,MAAM,EAAE,SAAS;AAEjB,KAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,IAAI,UAAU;EACnB,GAAG,QAAQ;EACX,SAAS,SAAS,KAAK,kBAAkB,QAAQ,QAAQ;EACzD,MAAM,KAAA;EACP,CAAC;CAGJ,MAAM,iBAAiB,EAAE;CACzB,IAAI,iBAAiB;AAErB,MAAK,MAAM,gBAAg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AAM,cAAc;KAClB,GAAG;KACH,GAAG,cAAc;KAClB;AACD,WAAO,QAAQ;KACb,GAAG;KACH,OAAO;KACR,CAAC;;AAGJ,OAAI;IACF,MAAM,SAAS,MAAM,gBACnB;KACE,GAAG;KAIH,OAAO;MACL,UAAU,cAAc;MACxB,GAAI,EAAE,cACF,qBAAqB,EAAE,aAAa,EAAE,GAAG,eAAe,CAAC,GACzD,EAAE;MACP;KACF,EACD,oBACD;;;;AAKD,QAAI,CAAC,YAAY,WAAW,OAAO,IAAI,CAAC,UAAU,OAAO,CACvD,OAAM,IAAI,MACR,uDAAuD,EAAE,KAAK,0CACpB,OAAO,SAClD;AAGH,WAAO;YACA,OAAO;AACd,UAAM,gBAAgB,KAAK,OAAO,EAAE,KAAK;;;AAG7C,SAAO;GACP,CACH"}
@@ -1 +1 @@
1
- {"version":3,"file":"withAgentName.cjs","names":["_addInlineAgentName","_removeInlineAgentName","RunnableSequence","RunnableLambda"],"sources":["../../src/agents/withAgentName.ts"],"sourcesContent":["import { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\nimport { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnableLambda,\n RunnableSequence,\n type RunnableConfig,\n} from \"@langchain/core/runnables\";\n\nimport {\n AgentNameMode,\n _addInlineAgentName,\n _removeInlineAgentName,\n} from \"./utils.js\";\n\n/**\n * Attach formatted agent names to the messages passed to and from a language model.\n *\n * This is useful for making a message history with multiple agents more coherent.\n *\n * NOTE: agent name is consumed from the message.name field.\n * If you're using an agent built with createAgent, name is automatically set.\n * If you're building a custom agent, make sure to set the name on the AI message returned by the LLM.\n *\n * @param model - Language model to add agent name formatting to\n * @param agentNameMode - How to expose the agent name to the LLM\n * - \"inline\": Add the agent name directly into the content field of the AI message using XML-style tags.\n * Example: \"How can I help you\" -> \"<name>agent_name</name><content>How can I help you?</content>\".\n */\nexport function withAgentName(\n model:\n | LanguageModelLike\n | Runnable<unknown, unknown, RunnableConfig<Record<string, unknown>>>,\n agentNameMode: AgentNameMode\n): LanguageModelLike {\n let processInputMessage: (message: BaseMessageLike) => BaseMessageLike;\n let processOutputMessage: (message: BaseMessage) => BaseMessage;\n\n if (agentNameMode === \"inline\") {\n processInputMessage = _addInlineAgentName;\n processOutputMessage = _removeInlineAgentName;\n } else {\n throw new Error(\n `Invalid agent name mode: ${agentNameMode}. Needs to be one of: \"inline\"`\n );\n }\n\n function processInputMessages(\n messages: BaseMessageLike[]\n ): BaseMessageLike[] {\n return messages.map(processInputMessage);\n }\n\n return RunnableSequence.from([\n RunnableLambda.from(processInputMessages),\n model,\n RunnableLambda.from(processOutputMessage),\n ]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA6BA,SAAgB,cACd,OAGA,eACmB;CACnB,IAAI;CACJ,IAAI;AAEJ,KAAI,kBAAkB,UAAU;AAC9B,wBAAsBA,cAAAA;AACtB,yBAAuBC,cAAAA;OAEvB,OAAM,IAAI,MACR,4BAA4B,cAAc,gCAC3C;CAGH,SAAS,qBACP,UACmB;AACnB,SAAO,SAAS,IAAI,oBAAoB;;AAG1C,QAAOC,0BAAAA,iBAAiB,KAAK;EAC3BC,0BAAAA,eAAe,KAAK,qBAAqB;EACzC;EACAA,0BAAAA,eAAe,KAAK,qBAAqB;EAC1C,CAAC"}
1
+ {"version":3,"file":"withAgentName.cjs","names":["_addInlineAgentName","_removeInlineAgentName","RunnableSequence","RunnableLambda"],"sources":["../../src/agents/withAgentName.ts"],"sourcesContent":["import { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\nimport {\n Runnable,\n RunnableLambda,\n RunnableSequence,\n type RunnableConfig,\n} from \"@langchain/core/runnables\";\n\nimport {\n AgentNameMode,\n _addInlineAgentName,\n _removeInlineAgentName,\n} from \"./utils.js\";\nimport { type AgentLanguageModelLike as LanguageModelLike } from \"./model.js\";\n\n/**\n * Attach formatted agent names to the messages passed to and from a language model.\n *\n * This is useful for making a message history with multiple agents more coherent.\n *\n * NOTE: agent name is consumed from the message.name field.\n * If you're using an agent built with createAgent, name is automatically set.\n * If you're building a custom agent, make sure to set the name on the AI message returned by the LLM.\n *\n * @param model - Language model to add agent name formatting to\n * @param agentNameMode - How to expose the agent name to the LLM\n * - \"inline\": Add the agent name directly into the content field of the AI message using XML-style tags.\n * Example: \"How can I help you\" -> \"<name>agent_name</name><content>How can I help you?</content>\".\n */\nexport function withAgentName(\n model:\n | LanguageModelLike\n | Runnable<unknown, unknown, RunnableConfig<Record<string, unknown>>>,\n agentNameMode: AgentNameMode\n): LanguageModelLike {\n let processInputMessage: (message: BaseMessageLike) => BaseMessageLike;\n let processOutputMessage: (message: BaseMessage) => BaseMessage;\n\n if (agentNameMode === \"inline\") {\n processInputMessage = _addInlineAgentName;\n processOutputMessage = _removeInlineAgentName;\n } else {\n throw new Error(\n `Invalid agent name mode: ${agentNameMode}. Needs to be one of: \"inline\"`\n );\n }\n\n function processInputMessages(\n messages: BaseMessageLike[]\n ): BaseMessageLike[] {\n return messages.map(processInputMessage);\n }\n\n return RunnableSequence.from([\n RunnableLambda.from(processInputMessages),\n model,\n RunnableLambda.from(processOutputMessage),\n ]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA6BA,SAAgB,cACd,OAGA,eACmB;CACnB,IAAI;CACJ,IAAI;AAEJ,KAAI,kBAAkB,UAAU;AAC9B,wBAAsBA,cAAAA;AACtB,yBAAuBC,cAAAA;OAEvB,OAAM,IAAI,MACR,4BAA4B,cAAc,gCAC3C;CAGH,SAAS,qBACP,UACmB;AACnB,SAAO,SAAS,IAAI,oBAAoB;;AAG1C,QAAOC,0BAAAA,iBAAiB,KAAK;EAC3BC,0BAAAA,eAAe,KAAK,qBAAqB;EACzC;EACAA,0BAAAA,eAAe,KAAK,qBAAqB;EAC1C,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"withAgentName.js","names":[],"sources":["../../src/agents/withAgentName.ts"],"sourcesContent":["import { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\nimport { LanguageModelLike } from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnableLambda,\n RunnableSequence,\n type RunnableConfig,\n} from \"@langchain/core/runnables\";\n\nimport {\n AgentNameMode,\n _addInlineAgentName,\n _removeInlineAgentName,\n} from \"./utils.js\";\n\n/**\n * Attach formatted agent names to the messages passed to and from a language model.\n *\n * This is useful for making a message history with multiple agents more coherent.\n *\n * NOTE: agent name is consumed from the message.name field.\n * If you're using an agent built with createAgent, name is automatically set.\n * If you're building a custom agent, make sure to set the name on the AI message returned by the LLM.\n *\n * @param model - Language model to add agent name formatting to\n * @param agentNameMode - How to expose the agent name to the LLM\n * - \"inline\": Add the agent name directly into the content field of the AI message using XML-style tags.\n * Example: \"How can I help you\" -> \"<name>agent_name</name><content>How can I help you?</content>\".\n */\nexport function withAgentName(\n model:\n | LanguageModelLike\n | Runnable<unknown, unknown, RunnableConfig<Record<string, unknown>>>,\n agentNameMode: AgentNameMode\n): LanguageModelLike {\n let processInputMessage: (message: BaseMessageLike) => BaseMessageLike;\n let processOutputMessage: (message: BaseMessage) => BaseMessage;\n\n if (agentNameMode === \"inline\") {\n processInputMessage = _addInlineAgentName;\n processOutputMessage = _removeInlineAgentName;\n } else {\n throw new Error(\n `Invalid agent name mode: ${agentNameMode}. Needs to be one of: \"inline\"`\n );\n }\n\n function processInputMessages(\n messages: BaseMessageLike[]\n ): BaseMessageLike[] {\n return messages.map(processInputMessage);\n }\n\n return RunnableSequence.from([\n RunnableLambda.from(processInputMessages),\n model,\n RunnableLambda.from(processOutputMessage),\n ]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA6BA,SAAgB,cACd,OAGA,eACmB;CACnB,IAAI;CACJ,IAAI;AAEJ,KAAI,kBAAkB,UAAU;AAC9B,wBAAsB;AACtB,yBAAuB;OAEvB,OAAM,IAAI,MACR,4BAA4B,cAAc,gCAC3C;CAGH,SAAS,qBACP,UACmB;AACnB,SAAO,SAAS,IAAI,oBAAoB;;AAG1C,QAAO,iBAAiB,KAAK;EAC3B,eAAe,KAAK,qBAAqB;EACzC;EACA,eAAe,KAAK,qBAAqB;EAC1C,CAAC"}
1
+ {"version":3,"file":"withAgentName.js","names":[],"sources":["../../src/agents/withAgentName.ts"],"sourcesContent":["import { BaseMessage, BaseMessageLike } from \"@langchain/core/messages\";\nimport {\n Runnable,\n RunnableLambda,\n RunnableSequence,\n type RunnableConfig,\n} from \"@langchain/core/runnables\";\n\nimport {\n AgentNameMode,\n _addInlineAgentName,\n _removeInlineAgentName,\n} from \"./utils.js\";\nimport { type AgentLanguageModelLike as LanguageModelLike } from \"./model.js\";\n\n/**\n * Attach formatted agent names to the messages passed to and from a language model.\n *\n * This is useful for making a message history with multiple agents more coherent.\n *\n * NOTE: agent name is consumed from the message.name field.\n * If you're using an agent built with createAgent, name is automatically set.\n * If you're building a custom agent, make sure to set the name on the AI message returned by the LLM.\n *\n * @param model - Language model to add agent name formatting to\n * @param agentNameMode - How to expose the agent name to the LLM\n * - \"inline\": Add the agent name directly into the content field of the AI message using XML-style tags.\n * Example: \"How can I help you\" -> \"<name>agent_name</name><content>How can I help you?</content>\".\n */\nexport function withAgentName(\n model:\n | LanguageModelLike\n | Runnable<unknown, unknown, RunnableConfig<Record<string, unknown>>>,\n agentNameMode: AgentNameMode\n): LanguageModelLike {\n let processInputMessage: (message: BaseMessageLike) => BaseMessageLike;\n let processOutputMessage: (message: BaseMessage) => BaseMessage;\n\n if (agentNameMode === \"inline\") {\n processInputMessage = _addInlineAgentName;\n processOutputMessage = _removeInlineAgentName;\n } else {\n throw new Error(\n `Invalid agent name mode: ${agentNameMode}. Needs to be one of: \"inline\"`\n );\n }\n\n function processInputMessages(\n messages: BaseMessageLike[]\n ): BaseMessageLike[] {\n return messages.map(processInputMessage);\n }\n\n return RunnableSequence.from([\n RunnableLambda.from(processInputMessages),\n model,\n RunnableLambda.from(processOutputMessage),\n ]);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA6BA,SAAgB,cACd,OAGA,eACmB;CACnB,IAAI;CACJ,IAAI;AAEJ,KAAI,kBAAkB,UAAU;AAC9B,wBAAsB;AACtB,yBAAuB;OAEvB,OAAM,IAAI,MACR,4BAA4B,cAAc,gCAC3C;CAGH,SAAS,qBACP,UACmB;AACnB,SAAO,SAAS,IAAI,oBAAoB;;AAG1C,QAAO,iBAAiB,KAAK;EAC3B,eAAe,KAAK,qBAAqB;EACzC;EACA,eAAe,KAAK,qBAAqB;EAC1C,CAAC"}
package/dist/browser.cjs CHANGED
@@ -1,6 +1,30 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
3
3
  const require_chat_models_universal = require("./chat_models/universal.cjs");
4
+ const require_errors = require("./agents/errors.cjs");
5
+ const require_responses = require("./agents/responses.cjs");
6
+ const require_utils = require("./agents/middleware/utils.cjs");
7
+ const require_types = require("./agents/middleware/types.cjs");
8
+ const require_middleware = require("./agents/middleware.cjs");
9
+ const require_utils$1 = require("./agents/tests/utils.cjs");
10
+ const require_index = require("./agents/index.cjs");
11
+ const require_hitl = require("./agents/middleware/hitl.cjs");
12
+ const require_summarization = require("./agents/middleware/summarization.cjs");
13
+ const require_dynamicSystemPrompt = require("./agents/middleware/dynamicSystemPrompt.cjs");
14
+ const require_llmToolSelector = require("./agents/middleware/llmToolSelector.cjs");
15
+ const require_pii = require("./agents/middleware/pii.cjs");
16
+ const require_piiRedaction = require("./agents/middleware/piiRedaction.cjs");
17
+ const require_contextEditing = require("./agents/middleware/contextEditing.cjs");
18
+ const require_toolCallLimit = require("./agents/middleware/toolCallLimit.cjs");
19
+ const require_todoListMiddleware = require("./agents/middleware/todoListMiddleware.cjs");
20
+ const require_modelCallLimit = require("./agents/middleware/modelCallLimit.cjs");
21
+ const require_modelFallback = require("./agents/middleware/modelFallback.cjs");
22
+ const require_modelRetry = require("./agents/middleware/modelRetry.cjs");
23
+ const require_toolRetry = require("./agents/middleware/toolRetry.cjs");
24
+ const require_toolEmulator = require("./agents/middleware/toolEmulator.cjs");
25
+ const require_moderation = require("./agents/middleware/provider/openai/moderation.cjs");
26
+ const require_promptCaching = require("./agents/middleware/provider/anthropic/promptCaching.cjs");
27
+ require("./agents/middleware/index.cjs");
4
28
  let _langchain_core_messages = require("@langchain/core/messages");
5
29
  let _langchain_core_tools = require("@langchain/core/tools");
6
30
  let _langchain_core_utils_context = require("@langchain/core/utils/context");
@@ -13,24 +37,65 @@ var browser_exports = /* @__PURE__ */ require_runtime.__exportAll({
13
37
  AIMessageChunk: () => _langchain_core_messages.AIMessageChunk,
14
38
  BaseMessage: () => _langchain_core_messages.BaseMessage,
15
39
  BaseMessageChunk: () => _langchain_core_messages.BaseMessageChunk,
40
+ ClearToolUsesEdit: () => require_contextEditing.ClearToolUsesEdit,
16
41
  Document: () => _langchain_core_documents.Document,
17
42
  DynamicStructuredTool: () => _langchain_core_tools.DynamicStructuredTool,
18
43
  DynamicTool: () => _langchain_core_tools.DynamicTool,
44
+ FakeToolCallingModel: () => require_utils$1.FakeToolCallingModel,
19
45
  HumanMessage: () => _langchain_core_messages.HumanMessage,
20
46
  HumanMessageChunk: () => _langchain_core_messages.HumanMessageChunk,
21
47
  InMemoryStore: () => _langchain_core_stores.InMemoryStore,
48
+ MIDDLEWARE_BRAND: () => require_types.MIDDLEWARE_BRAND,
49
+ MiddlewareError: () => require_errors.MiddlewareError,
50
+ MultipleStructuredOutputsError: () => require_errors.MultipleStructuredOutputsError,
51
+ MultipleToolsBoundError: () => require_errors.MultipleToolsBoundError,
52
+ PIIDetectionError: () => require_pii.PIIDetectionError,
53
+ ProviderStrategy: () => require_responses.ProviderStrategy,
54
+ StructuredOutputParsingError: () => require_errors.StructuredOutputParsingError,
22
55
  StructuredTool: () => _langchain_core_tools.StructuredTool,
23
56
  SystemMessage: () => _langchain_core_messages.SystemMessage,
24
57
  SystemMessageChunk: () => _langchain_core_messages.SystemMessageChunk,
58
+ TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT: () => require_todoListMiddleware.TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT,
25
59
  Tool: () => _langchain_core_tools.Tool,
60
+ ToolCallLimitExceededError: () => require_toolCallLimit.ToolCallLimitExceededError,
61
+ ToolInvocationError: () => require_errors.ToolInvocationError,
26
62
  ToolMessage: () => _langchain_core_messages.ToolMessage,
27
63
  ToolMessageChunk: () => _langchain_core_messages.ToolMessageChunk,
64
+ ToolStrategy: () => require_responses.ToolStrategy,
65
+ anthropicPromptCachingMiddleware: () => require_promptCaching.anthropicPromptCachingMiddleware,
66
+ applyStrategy: () => require_pii.applyStrategy,
28
67
  context: () => _langchain_core_utils_context.context,
68
+ contextEditingMiddleware: () => require_contextEditing.contextEditingMiddleware,
69
+ countTokensApproximately: () => require_utils.countTokensApproximately,
70
+ createAgent: () => require_index.createAgent,
71
+ createMiddleware: () => require_middleware.createMiddleware,
72
+ detectCreditCard: () => require_pii.detectCreditCard,
73
+ detectEmail: () => require_pii.detectEmail,
74
+ detectIP: () => require_pii.detectIP,
75
+ detectMacAddress: () => require_pii.detectMacAddress,
76
+ detectUrl: () => require_pii.detectUrl,
77
+ dynamicSystemPromptMiddleware: () => require_dynamicSystemPrompt.dynamicSystemPromptMiddleware,
29
78
  fakeModel: () => _langchain_core_testing.fakeModel,
30
79
  filterMessages: () => _langchain_core_messages.filterMessages,
80
+ humanInTheLoopMiddleware: () => require_hitl.humanInTheLoopMiddleware,
31
81
  initChatModel: () => require_chat_models_universal.initChatModel,
32
82
  langchainMatchers: () => _langchain_core_testing.langchainMatchers,
83
+ llmToolSelectorMiddleware: () => require_llmToolSelector.llmToolSelectorMiddleware,
84
+ modelCallLimitMiddleware: () => require_modelCallLimit.modelCallLimitMiddleware,
85
+ modelFallbackMiddleware: () => require_modelFallback.modelFallbackMiddleware,
86
+ modelRetryMiddleware: () => require_modelRetry.modelRetryMiddleware,
87
+ openAIModerationMiddleware: () => require_moderation.openAIModerationMiddleware,
88
+ piiMiddleware: () => require_pii.piiMiddleware,
89
+ piiRedactionMiddleware: () => require_piiRedaction.piiRedactionMiddleware,
90
+ providerStrategy: () => require_responses.providerStrategy,
91
+ resolveRedactionRule: () => require_pii.resolveRedactionRule,
92
+ summarizationMiddleware: () => require_summarization.summarizationMiddleware,
93
+ todoListMiddleware: () => require_todoListMiddleware.todoListMiddleware,
33
94
  tool: () => _langchain_core_tools.tool,
95
+ toolCallLimitMiddleware: () => require_toolCallLimit.toolCallLimitMiddleware,
96
+ toolEmulatorMiddleware: () => require_toolEmulator.toolEmulatorMiddleware,
97
+ toolRetryMiddleware: () => require_toolRetry.toolRetryMiddleware,
98
+ toolStrategy: () => require_responses.toolStrategy,
34
99
  trimMessages: () => _langchain_core_messages.trimMessages
35
100
  });
36
101
  //#endregion
@@ -58,6 +123,7 @@ Object.defineProperty(exports, "BaseMessageChunk", {
58
123
  return _langchain_core_messages.BaseMessageChunk;
59
124
  }
60
125
  });
126
+ exports.ClearToolUsesEdit = require_contextEditing.ClearToolUsesEdit;
61
127
  Object.defineProperty(exports, "Document", {
62
128
  enumerable: true,
63
129
  get: function() {
@@ -76,6 +142,7 @@ Object.defineProperty(exports, "DynamicTool", {
76
142
  return _langchain_core_tools.DynamicTool;
77
143
  }
78
144
  });
145
+ exports.FakeToolCallingModel = require_utils$1.FakeToolCallingModel;
79
146
  Object.defineProperty(exports, "HumanMessage", {
80
147
  enumerable: true,
81
148
  get: function() {
@@ -94,6 +161,13 @@ Object.defineProperty(exports, "InMemoryStore", {
94
161
  return _langchain_core_stores.InMemoryStore;
95
162
  }
96
163
  });
164
+ exports.MIDDLEWARE_BRAND = require_types.MIDDLEWARE_BRAND;
165
+ exports.MiddlewareError = require_errors.MiddlewareError;
166
+ exports.MultipleStructuredOutputsError = require_errors.MultipleStructuredOutputsError;
167
+ exports.MultipleToolsBoundError = require_errors.MultipleToolsBoundError;
168
+ exports.PIIDetectionError = require_pii.PIIDetectionError;
169
+ exports.ProviderStrategy = require_responses.ProviderStrategy;
170
+ exports.StructuredOutputParsingError = require_errors.StructuredOutputParsingError;
97
171
  Object.defineProperty(exports, "StructuredTool", {
98
172
  enumerable: true,
99
173
  get: function() {
@@ -112,12 +186,15 @@ Object.defineProperty(exports, "SystemMessageChunk", {
112
186
  return _langchain_core_messages.SystemMessageChunk;
113
187
  }
114
188
  });
189
+ exports.TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT = require_todoListMiddleware.TODO_LIST_MIDDLEWARE_SYSTEM_PROMPT;
115
190
  Object.defineProperty(exports, "Tool", {
116
191
  enumerable: true,
117
192
  get: function() {
118
193
  return _langchain_core_tools.Tool;
119
194
  }
120
195
  });
196
+ exports.ToolCallLimitExceededError = require_toolCallLimit.ToolCallLimitExceededError;
197
+ exports.ToolInvocationError = require_errors.ToolInvocationError;
121
198
  Object.defineProperty(exports, "ToolMessage", {
122
199
  enumerable: true,
123
200
  get: function() {
@@ -130,6 +207,9 @@ Object.defineProperty(exports, "ToolMessageChunk", {
130
207
  return _langchain_core_messages.ToolMessageChunk;
131
208
  }
132
209
  });
210
+ exports.ToolStrategy = require_responses.ToolStrategy;
211
+ exports.anthropicPromptCachingMiddleware = require_promptCaching.anthropicPromptCachingMiddleware;
212
+ exports.applyStrategy = require_pii.applyStrategy;
133
213
  Object.defineProperty(exports, "browser_exports", {
134
214
  enumerable: true,
135
215
  get: function() {
@@ -142,6 +222,16 @@ Object.defineProperty(exports, "context", {
142
222
  return _langchain_core_utils_context.context;
143
223
  }
144
224
  });
225
+ exports.contextEditingMiddleware = require_contextEditing.contextEditingMiddleware;
226
+ exports.countTokensApproximately = require_utils.countTokensApproximately;
227
+ exports.createAgent = require_index.createAgent;
228
+ exports.createMiddleware = require_middleware.createMiddleware;
229
+ exports.detectCreditCard = require_pii.detectCreditCard;
230
+ exports.detectEmail = require_pii.detectEmail;
231
+ exports.detectIP = require_pii.detectIP;
232
+ exports.detectMacAddress = require_pii.detectMacAddress;
233
+ exports.detectUrl = require_pii.detectUrl;
234
+ exports.dynamicSystemPromptMiddleware = require_dynamicSystemPrompt.dynamicSystemPromptMiddleware;
145
235
  Object.defineProperty(exports, "fakeModel", {
146
236
  enumerable: true,
147
237
  get: function() {
@@ -154,6 +244,7 @@ Object.defineProperty(exports, "filterMessages", {
154
244
  return _langchain_core_messages.filterMessages;
155
245
  }
156
246
  });
247
+ exports.humanInTheLoopMiddleware = require_hitl.humanInTheLoopMiddleware;
157
248
  exports.initChatModel = require_chat_models_universal.initChatModel;
158
249
  Object.defineProperty(exports, "langchainMatchers", {
159
250
  enumerable: true,
@@ -161,12 +252,27 @@ Object.defineProperty(exports, "langchainMatchers", {
161
252
  return _langchain_core_testing.langchainMatchers;
162
253
  }
163
254
  });
255
+ exports.llmToolSelectorMiddleware = require_llmToolSelector.llmToolSelectorMiddleware;
256
+ exports.modelCallLimitMiddleware = require_modelCallLimit.modelCallLimitMiddleware;
257
+ exports.modelFallbackMiddleware = require_modelFallback.modelFallbackMiddleware;
258
+ exports.modelRetryMiddleware = require_modelRetry.modelRetryMiddleware;
259
+ exports.openAIModerationMiddleware = require_moderation.openAIModerationMiddleware;
260
+ exports.piiMiddleware = require_pii.piiMiddleware;
261
+ exports.piiRedactionMiddleware = require_piiRedaction.piiRedactionMiddleware;
262
+ exports.providerStrategy = require_responses.providerStrategy;
263
+ exports.resolveRedactionRule = require_pii.resolveRedactionRule;
264
+ exports.summarizationMiddleware = require_summarization.summarizationMiddleware;
265
+ exports.todoListMiddleware = require_todoListMiddleware.todoListMiddleware;
164
266
  Object.defineProperty(exports, "tool", {
165
267
  enumerable: true,
166
268
  get: function() {
167
269
  return _langchain_core_tools.tool;
168
270
  }
169
271
  });
272
+ exports.toolCallLimitMiddleware = require_toolCallLimit.toolCallLimitMiddleware;
273
+ exports.toolEmulatorMiddleware = require_toolEmulator.toolEmulatorMiddleware;
274
+ exports.toolRetryMiddleware = require_toolRetry.toolRetryMiddleware;
275
+ exports.toolStrategy = require_responses.toolStrategy;
170
276
  Object.defineProperty(exports, "trimMessages", {
171
277
  enumerable: true,
172
278
  get: function() {
@@ -1 +1 @@
1
- {"version":3,"file":"browser.cjs","names":[],"sources":["../src/browser.ts"],"sourcesContent":["/**\n * LangChain browser exports\n */\n\n/**\n * LangChain Messages\n */\nexport {\n BaseMessage,\n BaseMessageChunk,\n AIMessage,\n AIMessageChunk,\n SystemMessage,\n SystemMessageChunk,\n HumanMessage,\n HumanMessageChunk,\n ToolMessage,\n ToolMessageChunk,\n type ContentBlock,\n filterMessages,\n trimMessages,\n} from \"@langchain/core/messages\";\n\n/**\n * Universal Chat Model\n */\nexport { initChatModel } from \"./chat_models/universal.js\";\n\n/**\n * LangChain Tools\n */\nexport {\n tool,\n Tool,\n type ToolRuntime,\n DynamicTool,\n StructuredTool,\n DynamicStructuredTool,\n} from \"@langchain/core/tools\";\n\n/**\n * LangChain utilities\n */\nexport { context } from \"@langchain/core/utils/context\";\n\n/**\n * LangChain Stores\n */\nexport { InMemoryStore } from \"@langchain/core/stores\";\n\n/**\n * LangChain Documents\n */\nexport { type DocumentInput, Document } from \"@langchain/core/documents\";\n\n/**\n * LangChain Testing Utilities\n */\nexport {\n langchainMatchers,\n type LangChainMatchers,\n fakeModel,\n} from \"@langchain/core/testing\";\n"],"mappings":""}
1
+ {"version":3,"file":"browser.cjs","names":[],"sources":["../src/browser.ts"],"sourcesContent":["/**\n * LangChain browser exports\n */\n\n/**\n * LangChain Messages\n */\nexport {\n BaseMessage,\n BaseMessageChunk,\n AIMessage,\n AIMessageChunk,\n SystemMessage,\n SystemMessageChunk,\n HumanMessage,\n HumanMessageChunk,\n ToolMessage,\n ToolMessageChunk,\n type ContentBlock,\n filterMessages,\n trimMessages,\n} from \"@langchain/core/messages\";\n\n/**\n * Universal Chat Model\n */\nexport { initChatModel } from \"./chat_models/universal.js\";\n\n/**\n * LangChain Tools\n */\nexport {\n tool,\n Tool,\n type ToolRuntime,\n DynamicTool,\n StructuredTool,\n DynamicStructuredTool,\n} from \"@langchain/core/tools\";\n\n/**\n * LangChain utilities\n */\nexport { context } from \"@langchain/core/utils/context\";\n\n/**\n * LangChain Agents\n */\nexport * from \"./agents/index.js\";\n\n/**\n * `createAgent` pre-built middleware\n */\nexport * from \"./agents/middleware/index.js\";\n\n/**\n * LangChain Stores\n */\nexport { InMemoryStore } from \"@langchain/core/stores\";\n\n/**\n * LangChain Documents\n */\nexport { type DocumentInput, Document } from \"@langchain/core/documents\";\n\n/**\n * LangChain Testing Utilities\n */\nexport {\n langchainMatchers,\n type LangChainMatchers,\n fakeModel,\n} from \"@langchain/core/testing\";\n"],"mappings":""}