import { MiddlewareError, MultipleStructuredOutputsError, MultipleToolsBoundError, StructuredOutputParsingError, ToolInvocationError } from "./errors.js"; import { JsonSchemaFormat, ProviderStrategy, ResponseFormat, ResponseFormatUndefined, ToolStrategy, TypedToolStrategy, providerStrategy, toolStrategy } from "./responses.js"; import { JumpToTarget } from "./constants.js"; import { Runtime as Runtime$1 } from "./runtime.js"; import { ModelRequest } from "./nodes/types.js"; import { AfterAgentHook, AfterModelHook, AgentMiddleware, AnyAgentMiddleware, AnyAnnotationRoot, BeforeAgentHook, BeforeModelHook, DefaultMiddlewareTypeConfig, InferChannelType, InferContextInput, InferMergedInputState, InferMergedState, InferMiddlewareContext, InferMiddlewareContextInput, InferMiddlewareContextInputs, InferMiddlewareContextSchema, InferMiddlewareContexts, InferMiddlewareFullContext, InferMiddlewareInputState, InferMiddlewareInputStates, InferMiddlewareSchema, InferMiddlewareState, InferMiddlewareStates, InferMiddlewareStreamTransformersFromConfig, InferMiddlewareToolsFromConfig, InferMiddlewareType, InferSchemaInput, InferSchemaUpdateType, InferSchemaValue, InferSchemaValueType, MIDDLEWARE_BRAND, MiddlewareResult, MiddlewareTypeConfig, NormalizedSchemaInput, NormalizedSchemaUpdate, ResolveMiddlewareTypeConfig, ToAnnotationRoot, ToolCallHandler, ToolCallRequest, WrapModelCallHandler, WrapModelCallHook, WrapToolCallHook } from "./middleware/types.js"; import { AgentTypeConfig, BuiltInState, CombineStreamTransformers, CombineTools, CreateAgentParams, DefaultAgentTypeConfig, ExecutedToolCall, ExtractZodArrayTypes, InferAgentContext, InferAgentContextSchema, InferAgentMiddleware, InferAgentResponse, InferAgentState, InferAgentStateSchema, InferAgentStreamTransformers, InferAgentTools, InferAgentType, InferMiddlewareStreamTransformers, InferMiddlewareStreamTransformersArray, InferMiddlewareTools, InferMiddlewareToolsArray, Interrupt, JumpTo, N, ResolveAgentTypeConfig, ToolCall, ToolResult, ToolsToMessageToolSet, UserInput, WithStateGraphNodes } from "./types.js"; import { createToolCallTransformer } from "./transformers/tool-call.js"; import { AgentRunStream, SubagentRunStream, ToolCallStreamUnion } from "./transformers/types.js"; import { createSubagentTransformer } from "./transformers/subagent.js"; import "./transformers/index.js"; import { ReactAgent } from "./ReactAgent.js"; import { createMiddleware } from "./middleware.js"; import { FakeToolCallingModel } from "./tests/utils.js"; import { ClientTool, ServerTool } from "@langchain/core/tools"; import { StateDefinitionInit, StreamTransformer } from "@langchain/langgraph"; import { InteropZodObject, InteropZodType } from "@langchain/core/utils/types"; import { SerializableSchema } from "@langchain/core/utils/standard_schema"; //#region src/agents/index.d.ts /** * Creates a production-ready ReAct (Reasoning + Acting) agent that combines language models with tools * and middleware to create systems that can reason about tasks, decide which tools to use, and iteratively * work towards solutions. * * The agent follows the ReAct pattern, interleaving reasoning steps with tool calls to iteratively * work towards solutions. It can handle multiple tool calls in sequence or parallel, maintain state * across interactions, and provide auditable decision processes. * * ## Core Components * * ### Model * The reasoning engine can be specified as: * - **String identifier**: `"openai:gpt-4o"` for simple setup * - **Model instance**: Configured model object for full control * - **Dynamic function**: Select models at runtime based on state * * ### Tools * Tools give agents the ability to take actions: * - Pass an array of tools created with the `tool` function * - Or provide a configured `ToolNode` for custom error handling * * ### Prompt * Shape how your agent approaches tasks: * - String for simple instructions * - SystemMessage for structured prompts * - Function for dynamic prompts based on state * * ### Middleware * Middleware allows you to extend the agent's behavior: * - Add pre/post-model processing for context injection or validation * - Add dynamic control flows, e.g. terminate invocation or retries * - Add human-in-the-loop capabilities * - Add tool calls to the agent * - Add tool results to the agent * * ## Advanced Features * * - **Structured Output**: Use `responseFormat` with a Zod schema to get typed responses * - **Memory**: Extend the state schema to remember information across interactions * - **Streaming**: Get real-time updates as the agent processes * * @param options - Configuration options for the agent * @param options.llm - The language model as an instance of a chat model * @param options.model - The language model as a string identifier, see more in {@link https://docs.langchain.com/oss/javascript/langchain/models#basic-usage | Models}. * @param options.tools - Array of tools or configured ToolNode * @param options.prompt - System instructions (string, SystemMessage, or function) * @param options.responseFormat - Zod schema for structured output * @param options.stateSchema - Custom state schema for memory * @param options.middleware - Array of middleware for extending agent behavior, see more in {@link https://docs.langchain.com/oss/javascript/langchain/middleware | Middleware}. * * @returns A ReactAgent instance with `invoke` and `stream` methods * * @example Basic agent with tools * ```ts * import { createAgent, tool } from "langchain"; * import { z } from "zod"; * * const search = tool( * ({ query }) => `Results for: ${query}`, * { * name: "search", * description: "Search for information", * schema: z.object({ * query: z.string().describe("The search query"), * }) * } * ); * * const agent = createAgent({ * llm: "openai:gpt-4o", * tools: [search], * }); * * const result = await agent.invoke({ * messages: [{ role: "user", content: "Search for ReAct agents" }], * }); * ``` * * @example Structured output * ```ts * import { createAgent } from "langchain"; * import { z } from "zod"; * * const ContactInfo = z.object({ * name: z.string(), * email: z.string(), * phone: z.string(), * }); * * const agent = createAgent({ * llm: "openai:gpt-4o", * tools: [], * responseFormat: ContactInfo, * }); * * const result = await agent.invoke({ * messages: [{ * role: "user", * content: "Extract: John Doe, john@example.com, (555) 123-4567" * }], * }); * * console.log(result.structuredResponse); * // { name: 'John Doe', email: 'john@example.com', phone: '(555) 123-4567' } * ``` * * @example Streaming responses * ```ts * const stream = await agent.stream( * { messages: [{ role: "user", content: "What's the weather?" }] }, * { streamMode: "values" } * ); * * for await (const chunk of stream) { * // ... * } * ``` * * @example With StateSchema * ```ts * import { createAgent } from "langchain"; * import { StateSchema, ReducedValue } from "@langchain/langgraph"; * import { z } from "zod"; * * const AgentState = new StateSchema({ * userId: z.string(), * count: z.number().default(0), * history: new ReducedValue( * z.array(z.string()).default(() => []), * { inputSchema: z.string(), reducer: (c, n) => [...c, n] } * ), * }); * * const agent = createAgent({ * model: "openai:gpt-4o", * tools: [searchTool], * stateSchema: AgentState, * }); * ``` */ declare function createAgent = Record, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, const TMiddleware extends readonly AnyAgentMiddleware[] = readonly AnyAgentMiddleware[], const TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[], const TStreamTransformers extends ReadonlyArray<() => StreamTransformer> = readonly []>(params: CreateAgentParams> & { responseFormat: InteropZodType; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, CombineStreamTransformers>>; declare function createAgent[], TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, const TMiddleware extends readonly AnyAgentMiddleware[] = readonly AnyAgentMiddleware[], const TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[], const TStreamTransformers extends ReadonlyArray<() => StreamTransformer> = readonly []>(params: CreateAgentParams extends Record ? ExtractZodArrayTypes : Record, TStateSchema, ContextSchema, StructuredResponseFormat> & { responseFormat: StructuredResponseFormat; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent extends Record ? ExtractZodArrayTypes : Record, TStateSchema, ContextSchema, TMiddleware, CombineTools, CombineStreamTransformers>>; declare function createAgent StreamTransformer> = readonly []>(params: CreateAgentParams, TStateSchema, ContextSchema, JsonSchemaFormat> & { responseFormat: JsonSchemaFormat; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, TStateSchema, ContextSchema, TMiddleware, CombineTools, CombineStreamTransformers>>; declare function createAgent StreamTransformer> = readonly []>(params: CreateAgentParams, TStateSchema, ContextSchema, JsonSchemaFormat[]> & { responseFormat: JsonSchemaFormat[]; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, TStateSchema, ContextSchema, TMiddleware, CombineTools, CombineStreamTransformers>>; declare function createAgent StreamTransformer> = readonly []>(params: CreateAgentParams, TStateSchema, ContextSchema, JsonSchemaFormat | JsonSchemaFormat[]> & { responseFormat: JsonSchemaFormat | JsonSchemaFormat[]; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, TStateSchema, ContextSchema, TMiddleware, CombineTools, CombineStreamTransformers>>; declare function createAgent StreamTransformer> = readonly []>(params: CreateAgentParams, TStateSchema, ContextSchema, SerializableSchema> & { responseFormat: SerializableSchema; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, TStateSchema, ContextSchema, TMiddleware, CombineTools, CombineStreamTransformers>>; declare function createAgent StreamTransformer> = readonly []>(params: CreateAgentParams, TStateSchema, ContextSchema, SerializableSchema[]> & { responseFormat: SerializableSchema[]; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, TStateSchema, ContextSchema, TMiddleware, CombineTools, CombineStreamTransformers>>; declare function createAgent = Record, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, const TMiddleware extends readonly AnyAgentMiddleware[] = readonly AnyAgentMiddleware[], const TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[], const TStreamTransformers extends ReadonlyArray<() => StreamTransformer> = readonly []>(params: CreateAgentParams> & { responseFormat: TypedToolStrategy; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, CombineStreamTransformers>>; declare function createAgent = Record, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, const TMiddleware extends readonly AnyAgentMiddleware[] = readonly AnyAgentMiddleware[], const TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[], const TStreamTransformers extends ReadonlyArray<() => StreamTransformer> = readonly []>(params: CreateAgentParams> & { responseFormat: ToolStrategy; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, CombineStreamTransformers>>; declare function createAgent = Record, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, const TMiddleware extends readonly AnyAgentMiddleware[] = readonly AnyAgentMiddleware[], const TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[], const TStreamTransformers extends ReadonlyArray<() => StreamTransformer> = readonly []>(params: CreateAgentParams> & { responseFormat: ProviderStrategy; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, CombineStreamTransformers>>; declare function createAgent StreamTransformer> = readonly []>(params: Omit, "responseFormat"> & { middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, CombineStreamTransformers>>; declare function createAgent StreamTransformer> = readonly []>(params: Omit, "responseFormat"> & { responseFormat?: undefined; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, CombineStreamTransformers>>; declare function createAgent = Record, TStateSchema extends StateDefinitionInit | undefined = undefined, ContextSchema extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot, const TMiddleware extends readonly AnyAgentMiddleware[] = readonly AnyAgentMiddleware[], const TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[], const TStreamTransformers extends ReadonlyArray<() => StreamTransformer> = readonly []>(params: CreateAgentParams & { responseFormat: ResponseFormat; middleware?: TMiddleware; tools?: TTools; streamTransformers?: TStreamTransformers; }): ReactAgent, CombineStreamTransformers>>; //#endregion export { type AfterAgentHook, type AfterModelHook, type AgentMiddleware, type AgentRunStream, AgentTypeConfig, type AnyAgentMiddleware, type AnyAnnotationRoot, type BeforeAgentHook, type BeforeModelHook, BuiltInState, CombineStreamTransformers, CombineTools, CreateAgentParams, DefaultAgentTypeConfig, type DefaultMiddlewareTypeConfig, ExecutedToolCall, ExtractZodArrayTypes, FakeToolCallingModel, InferAgentContext, InferAgentContextSchema, InferAgentMiddleware, InferAgentResponse, InferAgentState, InferAgentStateSchema, InferAgentStreamTransformers, InferAgentTools, InferAgentType, type InferChannelType, type InferContextInput, type InferMergedInputState, type InferMergedState, type InferMiddlewareContext, type InferMiddlewareContextInput, type InferMiddlewareContextInputs, type InferMiddlewareContextSchema, type InferMiddlewareContexts, type InferMiddlewareFullContext, type InferMiddlewareInputState, type InferMiddlewareInputStates, type InferMiddlewareSchema, type InferMiddlewareState, type InferMiddlewareStates, InferMiddlewareStreamTransformers, InferMiddlewareStreamTransformersArray, type InferMiddlewareStreamTransformersFromConfig, InferMiddlewareTools, InferMiddlewareToolsArray, type InferMiddlewareToolsFromConfig, type InferMiddlewareType, type InferSchemaInput, type InferSchemaUpdateType, type InferSchemaValue, type InferSchemaValueType, Interrupt, JumpTo, type JumpToTarget, MIDDLEWARE_BRAND, MiddlewareError, type MiddlewareResult, type MiddlewareTypeConfig, ModelRequest, MultipleStructuredOutputsError, MultipleToolsBoundError, N, type NormalizedSchemaInput, type NormalizedSchemaUpdate, ProviderStrategy, type ReactAgent, ResolveAgentTypeConfig, type ResolveMiddlewareTypeConfig, type ResponseFormat, type ResponseFormatUndefined, type Runtime$1 as Runtime, StructuredOutputParsingError, type SubagentRunStream, type ToAnnotationRoot, ToolCall, type ToolCallHandler, type ToolCallRequest, type ToolCallStreamUnion, ToolInvocationError, ToolResult, ToolStrategy, ToolsToMessageToolSet, type TypedToolStrategy, UserInput, WithStateGraphNodes, type WrapModelCallHandler, type WrapModelCallHook, type WrapToolCallHook, createAgent, createMiddleware, createSubagentTransformer, createToolCallTransformer, providerStrategy, toolStrategy }; //# sourceMappingURL=index.d.ts.map