deepagents 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,17 +1,18 @@
1
- import * as langchain0 from "langchain";
2
- import { AgentMiddleware, AgentMiddleware as AgentMiddleware$1, AgentTypeConfig, InterruptOnConfig, ReactAgent, StructuredTool, ToolMessage } from "langchain";
1
+ import * as langchain9 from "langchain";
2
+ import { AgentMiddleware, AgentMiddleware as AgentMiddleware$1, AgentTypeConfig, InferMiddlewareStates, InterruptOnConfig, ReactAgent, ResponseFormat, StructuredTool, SystemMessage, ToolMessage } from "langchain";
3
3
  import * as _Command from "@langchain/langgraph";
4
4
  import { AnnotationRoot, Command } from "@langchain/langgraph";
5
5
  import { z } from "zod/v4";
6
6
  import * as zod_v30 from "zod/v3";
7
+ import { z as z$1 } from "zod/v3";
7
8
  import * as _messages from "@langchain/core/messages";
8
9
  import * as zod0 from "zod";
9
10
  import * as zod_v4_core0 from "zod/v4/core";
10
11
  import { BaseCheckpointSaver, BaseStore } from "@langchain/langgraph-checkpoint";
11
12
  import * as _langchain_core_language_models_base0 from "@langchain/core/language_models/base";
12
13
  import { BaseLanguageModel, LanguageModelLike } from "@langchain/core/language_models/base";
13
- import * as _langchain_core_tools1 from "@langchain/core/tools";
14
- import { StructuredTool as StructuredTool$1 } from "@langchain/core/tools";
14
+ import * as _langchain_core_tools3 from "@langchain/core/tools";
15
+ import { ClientTool, ServerTool, StructuredTool as StructuredTool$1 } from "@langchain/core/tools";
15
16
  import { Runnable } from "@langchain/core/runnables";
16
17
  import { InteropZodObject } from "@langchain/core/utils/types";
17
18
 
@@ -306,13 +307,13 @@ declare function createFilesystemMiddleware(options?: FilesystemMiddlewareOption
306
307
  created_at: z.ZodString;
307
308
  modified_at: z.ZodString;
308
309
  }, z.core.$strip>>>;
309
- }, z.core.$strip>, undefined, unknown, (langchain0.DynamicStructuredTool<z.ZodObject<{
310
+ }, z.core.$strip>, undefined, unknown, (langchain9.DynamicStructuredTool<z.ZodObject<{
310
311
  path: z.ZodDefault<z.ZodOptional<z.ZodString>>;
311
312
  }, z.core.$strip>, {
312
313
  path: string;
313
314
  }, {
314
315
  path?: string | undefined;
315
- }, string, "ls"> | langchain0.DynamicStructuredTool<z.ZodObject<{
316
+ }, string, "ls"> | langchain9.DynamicStructuredTool<z.ZodObject<{
316
317
  file_path: z.ZodString;
317
318
  offset: z.ZodDefault<z.ZodOptional<z.ZodCoercedNumber<unknown>>>;
318
319
  limit: z.ZodDefault<z.ZodOptional<z.ZodCoercedNumber<unknown>>>;
@@ -324,7 +325,7 @@ declare function createFilesystemMiddleware(options?: FilesystemMiddlewareOption
324
325
  file_path: string;
325
326
  offset?: unknown;
326
327
  limit?: unknown;
327
- }, string, "read_file"> | langchain0.DynamicStructuredTool<z.ZodObject<{
328
+ }, string, "read_file"> | langchain9.DynamicStructuredTool<z.ZodObject<{
328
329
  file_path: z.ZodString;
329
330
  content: z.ZodString;
330
331
  }, z.core.$strip>, {
@@ -336,7 +337,7 @@ declare function createFilesystemMiddleware(options?: FilesystemMiddlewareOption
336
337
  }, string | ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>> | Command<unknown, {
337
338
  files: Record<string, FileData>;
338
339
  messages: ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>>[];
339
- }, string>, "write_file"> | langchain0.DynamicStructuredTool<z.ZodObject<{
340
+ }, string>, "write_file"> | langchain9.DynamicStructuredTool<z.ZodObject<{
340
341
  file_path: z.ZodString;
341
342
  old_string: z.ZodString;
342
343
  new_string: z.ZodString;
@@ -354,7 +355,7 @@ declare function createFilesystemMiddleware(options?: FilesystemMiddlewareOption
354
355
  }, string | ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>> | Command<unknown, {
355
356
  files: Record<string, FileData>;
356
357
  messages: ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>>[];
357
- }, string>, "edit_file"> | langchain0.DynamicStructuredTool<z.ZodObject<{
358
+ }, string>, "edit_file"> | langchain9.DynamicStructuredTool<z.ZodObject<{
358
359
  pattern: z.ZodString;
359
360
  path: z.ZodDefault<z.ZodOptional<z.ZodString>>;
360
361
  }, z.core.$strip>, {
@@ -363,7 +364,7 @@ declare function createFilesystemMiddleware(options?: FilesystemMiddlewareOption
363
364
  }, {
364
365
  pattern: string;
365
366
  path?: string | undefined;
366
- }, string, "glob"> | langchain0.DynamicStructuredTool<z.ZodObject<{
367
+ }, string, "glob"> | langchain9.DynamicStructuredTool<z.ZodObject<{
367
368
  pattern: z.ZodString;
368
369
  path: z.ZodDefault<z.ZodOptional<z.ZodString>>;
369
370
  glob: z.ZodNullable<z.ZodOptional<z.ZodString>>;
@@ -375,7 +376,7 @@ declare function createFilesystemMiddleware(options?: FilesystemMiddlewareOption
375
376
  pattern: string;
376
377
  path?: string | undefined;
377
378
  glob?: string | null | undefined;
378
- }, string, "grep"> | langchain0.DynamicStructuredTool<z.ZodObject<{
379
+ }, string, "grep"> | langchain9.DynamicStructuredTool<z.ZodObject<{
379
380
  command: z.ZodString;
380
381
  }, z.core.$strip>, {
381
382
  command: string;
@@ -410,7 +411,7 @@ interface SubAgent {
410
411
  /** The model for the agent. Defaults to default_model */
411
412
  model?: LanguageModelLike | string;
412
413
  /** Additional middleware to append after default_middleware */
413
- middleware?: AgentMiddleware$1[];
414
+ middleware?: readonly AgentMiddleware$1[];
414
415
  /** The tool configs to use for the agent */
415
416
  interruptOn?: Record<string, boolean | InterruptOnConfig>;
416
417
  }
@@ -438,7 +439,22 @@ interface SubAgentMiddlewareOptions {
438
439
  /**
439
440
  * Create subagent middleware with task tool
440
441
  */
441
- declare function createSubAgentMiddleware(options: SubAgentMiddlewareOptions): AgentMiddleware$1;
442
+ declare function createSubAgentMiddleware(options: SubAgentMiddlewareOptions): AgentMiddleware$1<undefined, undefined, unknown, readonly [langchain9.DynamicStructuredTool<z$1.ZodObject<{
443
+ description: z$1.ZodString;
444
+ subagent_type: z$1.ZodString;
445
+ }, "strip", z$1.ZodTypeAny, {
446
+ description: string;
447
+ subagent_type: string;
448
+ }, {
449
+ description: string;
450
+ subagent_type: string;
451
+ }>, {
452
+ description: string;
453
+ subagent_type: string;
454
+ }, {
455
+ description: string;
456
+ subagent_type: string;
457
+ }, string | Command<unknown, Record<string, unknown>, string>, "task">]>;
442
458
  //#endregion
443
459
  //#region src/middleware/patch_tool_calls.d.ts
444
460
  /**
@@ -461,7 +477,7 @@ declare function createSubAgentMiddleware(options: SubAgentMiddlewareOptions): A
461
477
  * });
462
478
  * ```
463
479
  */
464
- declare function createPatchToolCallsMiddleware(): AgentMiddleware<undefined, undefined, unknown, readonly (_langchain_core_tools1.ClientTool | _langchain_core_tools1.ServerTool)[]>;
480
+ declare function createPatchToolCallsMiddleware(): AgentMiddleware<undefined, undefined, unknown, readonly (_langchain_core_tools3.ClientTool | _langchain_core_tools3.ServerTool)[]>;
465
481
  //#endregion
466
482
  //#region src/backends/state.d.ts
467
483
  /**
@@ -918,24 +934,49 @@ declare abstract class BaseSandbox implements SandboxBackendProtocol {
918
934
  edit(filePath: string, oldString: string, newString: string, replaceAll?: boolean): Promise<EditResult>;
919
935
  }
920
936
  //#endregion
921
- //#region src/agent.d.ts
937
+ //#region src/types.d.ts
938
+ /**
939
+ * Helper type to extract middleware from a SubAgent definition
940
+ * Handles both mutable and readonly middleware arrays
941
+ */
942
+ type ExtractSubAgentMiddleware<T> = T extends {
943
+ middleware?: infer M;
944
+ } ? M extends readonly AgentMiddleware[] ? M : M extends AgentMiddleware[] ? M : readonly [] : readonly [];
945
+ /**
946
+ * Helper type to flatten and merge middleware from all subagents
947
+ */
948
+ type FlattenSubAgentMiddleware<T extends readonly (SubAgent | CompiledSubAgent)[]> = T extends readonly [] ? readonly [] : T extends readonly [infer First, ...infer Rest] ? Rest extends readonly (SubAgent | CompiledSubAgent)[] ? readonly [...ExtractSubAgentMiddleware<First>, ...FlattenSubAgentMiddleware<Rest>] : ExtractSubAgentMiddleware<First> : readonly [];
949
+ /**
950
+ * Helper type to merge states from subagent middleware
951
+ */
952
+ type InferSubAgentMiddlewareStates<T extends readonly (SubAgent | CompiledSubAgent)[]> = InferMiddlewareStates<FlattenSubAgentMiddleware<T>>;
953
+ /**
954
+ * Combined state type including custom middleware and subagent middleware states
955
+ */
956
+ type MergedDeepAgentState<TMiddleware extends readonly AgentMiddleware[], TSubagents extends readonly (SubAgent | CompiledSubAgent)[]> = InferMiddlewareStates<TMiddleware> & InferSubAgentMiddlewareStates<TSubagents>;
922
957
  /**
923
958
  * Configuration parameters for creating a Deep Agent
924
959
  * Matches Python's create_deep_agent parameters
960
+ *
961
+ * @typeParam TResponse - The structured response type when using responseFormat
962
+ * @typeParam ContextSchema - The context schema type
963
+ * @typeParam TMiddleware - The middleware array type for proper type inference
964
+ * @typeParam TSubagents - The subagents array type for extracting subagent middleware states
965
+ * @typeParam TTools - The tools array type
925
966
  */
926
- interface CreateDeepAgentParams<ContextSchema extends AnnotationRoot<any> | InteropZodObject = AnnotationRoot<any>> {
967
+ interface CreateDeepAgentParams<TResponse extends ResponseFormat = ResponseFormat, ContextSchema extends AnnotationRoot<any> | InteropZodObject = AnnotationRoot<any>, TMiddleware extends readonly AgentMiddleware[] = readonly AgentMiddleware[], TSubagents extends readonly (SubAgent | CompiledSubAgent)[] = readonly (SubAgent | CompiledSubAgent)[], TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[]> {
927
968
  /** The model to use (model name string or LanguageModelLike instance). Defaults to claude-sonnet-4-5-20250929 */
928
969
  model?: BaseLanguageModel | string;
929
970
  /** Tools the agent should have access to */
930
- tools?: StructuredTool$1[];
971
+ tools?: TTools | StructuredTool$1[];
931
972
  /** Custom system prompt for the agent. This will be combined with the base agent prompt */
932
- systemPrompt?: string;
973
+ systemPrompt?: string | SystemMessage;
933
974
  /** Custom middleware to apply after standard middleware */
934
- middleware?: AgentMiddleware[];
975
+ middleware?: TMiddleware;
935
976
  /** List of subagent specifications for task delegation */
936
- subagents?: (SubAgent | CompiledSubAgent)[];
937
- /** Structured output response format for the agent */
938
- responseFormat?: any;
977
+ subagents?: TSubagents;
978
+ /** Structured output response format for the agent (Zod schema or other format) */
979
+ responseFormat?: TResponse;
939
980
  /** Optional schema for context (not persisted between invocations) */
940
981
  contextSchema?: ContextSchema;
941
982
  /** Optional checkpointer for persisting agent state between runs */
@@ -956,6 +997,8 @@ interface CreateDeepAgentParams<ContextSchema extends AnnotationRoot<any> | Inte
956
997
  /** The name of the agent */
957
998
  name?: string;
958
999
  }
1000
+ //#endregion
1001
+ //#region src/agent.d.ts
959
1002
  /**
960
1003
  * Create a Deep Agent with middleware-based architecture.
961
1004
  *
@@ -969,9 +1012,25 @@ interface CreateDeepAgentParams<ContextSchema extends AnnotationRoot<any> | Inte
969
1012
  * - Human-in-the-loop (humanInTheLoopMiddleware) - optional
970
1013
  *
971
1014
  * @param params Configuration parameters for the agent
972
- * @returns ReactAgent instance ready for invocation
1015
+ * @returns ReactAgent instance ready for invocation with properly inferred state types
1016
+ *
1017
+ * @example
1018
+ * ```typescript
1019
+ * // Middleware with custom state
1020
+ * const ResearchMiddleware = createMiddleware({
1021
+ * name: "ResearchMiddleware",
1022
+ * stateSchema: z.object({ research: z.string().default("") }),
1023
+ * });
1024
+ *
1025
+ * const agent = createDeepAgent({
1026
+ * middleware: [ResearchMiddleware],
1027
+ * });
1028
+ *
1029
+ * const result = await agent.invoke({ messages: [...] });
1030
+ * // result.research is properly typed as string
1031
+ * ```
973
1032
  */
974
- declare function createDeepAgent<ContextSchema extends InteropZodObject = InteropZodObject>(params?: CreateDeepAgentParams<ContextSchema>): ReactAgent<AgentTypeConfig<Record<string, any>, undefined, ContextSchema, (AgentMiddleware<any, any, any, readonly (_langchain_core_tools1.ClientTool | _langchain_core_tools1.ServerTool)[]> | AgentMiddleware<zod_v30.ZodObject<{
1033
+ declare function createDeepAgent<TResponse extends ResponseFormat = ResponseFormat, ContextSchema extends InteropZodObject = InteropZodObject, const TMiddleware extends readonly AgentMiddleware[] = readonly [], const TSubagents extends readonly (SubAgent | CompiledSubAgent)[] = readonly [], const TTools extends readonly (ClientTool | ServerTool)[] = readonly []>(params?: CreateDeepAgentParams<TResponse, ContextSchema, TMiddleware, TSubagents, TTools>): ReactAgent<AgentTypeConfig<TResponse, undefined, ContextSchema, readonly [AgentMiddleware<zod_v30.ZodObject<{
975
1034
  todos: zod_v30.ZodDefault<zod_v30.ZodArray<zod_v30.ZodObject<{
976
1035
  content: zod_v30.ZodString;
977
1036
  status: zod_v30.ZodEnum<["pending", "in_progress", "completed"]>;
@@ -992,7 +1051,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
992
1051
  content: string;
993
1052
  status: "completed" | "in_progress" | "pending";
994
1053
  }[] | undefined;
995
- }>, undefined, unknown, readonly [_langchain_core_tools1.DynamicStructuredTool<zod_v30.ZodObject<{
1054
+ }>, undefined, unknown, readonly [langchain9.DynamicStructuredTool<zod_v30.ZodObject<{
996
1055
  todos: zod_v30.ZodArray<zod_v30.ZodObject<{
997
1056
  content: zod_v30.ZodString;
998
1057
  status: zod_v30.ZodEnum<["pending", "in_progress", "completed"]>;
@@ -1029,19 +1088,19 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1029
1088
  status: "completed" | "in_progress" | "pending";
1030
1089
  }[];
1031
1090
  messages: _messages.ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>>[];
1032
- }, string>, "write_todos">]> | AgentMiddleware<zod0.ZodObject<{
1091
+ }, string>, "write_todos">]>, AgentMiddleware<zod0.ZodObject<{
1033
1092
  files: zod0.ZodDefault<zod0.ZodRecord<zod0.ZodString, zod0.ZodObject<{
1034
1093
  content: zod0.ZodArray<zod0.ZodString>;
1035
1094
  created_at: zod0.ZodString;
1036
1095
  modified_at: zod0.ZodString;
1037
1096
  }, zod_v4_core0.$strip>>>;
1038
- }, zod_v4_core0.$strip>, undefined, unknown, (_langchain_core_tools1.DynamicStructuredTool<zod0.ZodObject<{
1097
+ }, zod_v4_core0.$strip>, undefined, unknown, (langchain9.DynamicStructuredTool<zod0.ZodObject<{
1039
1098
  path: zod0.ZodDefault<zod0.ZodOptional<zod0.ZodString>>;
1040
1099
  }, zod_v4_core0.$strip>, {
1041
1100
  path: string;
1042
1101
  }, {
1043
1102
  path?: string | undefined;
1044
- }, string, "ls"> | _langchain_core_tools1.DynamicStructuredTool<zod0.ZodObject<{
1103
+ }, string, "ls"> | langchain9.DynamicStructuredTool<zod0.ZodObject<{
1045
1104
  file_path: zod0.ZodString;
1046
1105
  offset: zod0.ZodDefault<zod0.ZodOptional<zod0.ZodCoercedNumber<unknown>>>;
1047
1106
  limit: zod0.ZodDefault<zod0.ZodOptional<zod0.ZodCoercedNumber<unknown>>>;
@@ -1053,7 +1112,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1053
1112
  file_path: string;
1054
1113
  offset?: unknown;
1055
1114
  limit?: unknown;
1056
- }, string, "read_file"> | _langchain_core_tools1.DynamicStructuredTool<zod0.ZodObject<{
1115
+ }, string, "read_file"> | langchain9.DynamicStructuredTool<zod0.ZodObject<{
1057
1116
  file_path: zod0.ZodString;
1058
1117
  content: zod0.ZodString;
1059
1118
  }, zod_v4_core0.$strip>, {
@@ -1065,7 +1124,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1065
1124
  }, string | _messages.ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>> | _Command.Command<unknown, {
1066
1125
  files: Record<string, FileData>;
1067
1126
  messages: _messages.ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>>[];
1068
- }, string>, "write_file"> | _langchain_core_tools1.DynamicStructuredTool<zod0.ZodObject<{
1127
+ }, string>, "write_file"> | langchain9.DynamicStructuredTool<zod0.ZodObject<{
1069
1128
  file_path: zod0.ZodString;
1070
1129
  old_string: zod0.ZodString;
1071
1130
  new_string: zod0.ZodString;
@@ -1083,7 +1142,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1083
1142
  }, string | _messages.ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>> | _Command.Command<unknown, {
1084
1143
  files: Record<string, FileData>;
1085
1144
  messages: _messages.ToolMessage<_messages.MessageStructure<_messages.MessageToolSet>>[];
1086
- }, string>, "edit_file"> | _langchain_core_tools1.DynamicStructuredTool<zod0.ZodObject<{
1145
+ }, string>, "edit_file"> | langchain9.DynamicStructuredTool<zod0.ZodObject<{
1087
1146
  pattern: zod0.ZodString;
1088
1147
  path: zod0.ZodDefault<zod0.ZodOptional<zod0.ZodString>>;
1089
1148
  }, zod_v4_core0.$strip>, {
@@ -1092,7 +1151,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1092
1151
  }, {
1093
1152
  pattern: string;
1094
1153
  path?: string | undefined;
1095
- }, string, "glob"> | _langchain_core_tools1.DynamicStructuredTool<zod0.ZodObject<{
1154
+ }, string, "glob"> | langchain9.DynamicStructuredTool<zod0.ZodObject<{
1096
1155
  pattern: zod0.ZodString;
1097
1156
  path: zod0.ZodDefault<zod0.ZodOptional<zod0.ZodString>>;
1098
1157
  glob: zod0.ZodNullable<zod0.ZodOptional<zod0.ZodString>>;
@@ -1104,13 +1163,28 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1104
1163
  pattern: string;
1105
1164
  path?: string | undefined;
1106
1165
  glob?: string | null | undefined;
1107
- }, string, "grep"> | _langchain_core_tools1.DynamicStructuredTool<zod0.ZodObject<{
1166
+ }, string, "grep"> | langchain9.DynamicStructuredTool<zod0.ZodObject<{
1108
1167
  command: zod0.ZodString;
1109
1168
  }, zod_v4_core0.$strip>, {
1110
1169
  command: string;
1111
1170
  }, {
1112
1171
  command: string;
1113
- }, string, "execute">)[]> | AgentMiddleware<undefined, zod_v30.ZodObject<{
1172
+ }, string, "execute">)[]>, AgentMiddleware<undefined, undefined, unknown, readonly [langchain9.DynamicStructuredTool<zod_v30.ZodObject<{
1173
+ description: zod_v30.ZodString;
1174
+ subagent_type: zod_v30.ZodString;
1175
+ }, "strip", zod_v30.ZodTypeAny, {
1176
+ description: string;
1177
+ subagent_type: string;
1178
+ }, {
1179
+ description: string;
1180
+ subagent_type: string;
1181
+ }>, {
1182
+ description: string;
1183
+ subagent_type: string;
1184
+ }, {
1185
+ description: string;
1186
+ subagent_type: string;
1187
+ }, string | _Command.Command<unknown, Record<string, unknown>, string>, "task">]>, AgentMiddleware<undefined, zod_v30.ZodObject<{
1114
1188
  trigger: zod_v30.ZodOptional<zod_v30.ZodUnion<[zod_v30.ZodEffects<zod_v30.ZodObject<{
1115
1189
  fraction: zod_v30.ZodOptional<zod_v30.ZodNumber>;
1116
1190
  tokens: zod_v30.ZodOptional<zod_v30.ZodNumber>;
@@ -1180,7 +1254,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1180
1254
  maxTokensBeforeSummary: zod_v30.ZodOptional<zod_v30.ZodNumber>;
1181
1255
  messagesToKeep: zod_v30.ZodOptional<zod_v30.ZodNumber>;
1182
1256
  } & {
1183
- model: zod_v30.ZodOptional<zod_v30.ZodType<BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions>, zod_v30.ZodTypeDef, BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions>>>;
1257
+ model: zod_v30.ZodOptional<zod_v30.ZodType<_langchain_core_language_models_base0.BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions>, zod_v30.ZodTypeDef, _langchain_core_language_models_base0.BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions>>>;
1184
1258
  }, "strip", zod_v30.ZodTypeAny, {
1185
1259
  trigger?: {
1186
1260
  fraction?: number | undefined;
@@ -1202,7 +1276,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1202
1276
  summaryPrefix?: string | undefined;
1203
1277
  maxTokensBeforeSummary?: number | undefined;
1204
1278
  messagesToKeep?: number | undefined;
1205
- model?: BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions> | undefined;
1279
+ model?: _langchain_core_language_models_base0.BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions> | undefined;
1206
1280
  }, {
1207
1281
  trigger?: {
1208
1282
  fraction?: number | undefined;
@@ -1224,7 +1298,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1224
1298
  summaryPrefix?: string | undefined;
1225
1299
  maxTokensBeforeSummary?: number | undefined;
1226
1300
  messagesToKeep?: number | undefined;
1227
- model?: BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions> | undefined;
1301
+ model?: _langchain_core_language_models_base0.BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions> | undefined;
1228
1302
  }>, {
1229
1303
  trigger?: {
1230
1304
  fraction?: number | undefined;
@@ -1246,8 +1320,8 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1246
1320
  summaryPrefix?: string | undefined;
1247
1321
  maxTokensBeforeSummary?: number | undefined;
1248
1322
  messagesToKeep?: number | undefined;
1249
- model?: BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions> | undefined;
1250
- }, readonly (_langchain_core_tools1.ClientTool | _langchain_core_tools1.ServerTool)[]> | AgentMiddleware<undefined, zod_v30.ZodObject<{
1323
+ model?: _langchain_core_language_models_base0.BaseLanguageModel<any, _langchain_core_language_models_base0.BaseLanguageModelCallOptions> | undefined;
1324
+ }, readonly (ClientTool | ServerTool)[]>, AgentMiddleware<undefined, zod_v30.ZodObject<{
1251
1325
  enableCaching: zod_v30.ZodOptional<zod_v30.ZodBoolean>;
1252
1326
  ttl: zod_v30.ZodOptional<zod_v30.ZodEnum<["5m", "1h"]>>;
1253
1327
  minMessagesToCache: zod_v30.ZodOptional<zod_v30.ZodNumber>;
@@ -1267,7 +1341,7 @@ declare function createDeepAgent<ContextSchema extends InteropZodObject = Intero
1267
1341
  ttl?: "1h" | "5m" | undefined;
1268
1342
  minMessagesToCache?: number | undefined;
1269
1343
  unsupportedModelBehavior?: "ignore" | "raise" | "warn" | undefined;
1270
- }, readonly (_langchain_core_tools1.ClientTool | _langchain_core_tools1.ServerTool)[]> | AgentMiddleware<undefined, undefined, unknown, readonly (_langchain_core_tools1.ClientTool | _langchain_core_tools1.ServerTool)[]>)[], readonly StructuredTool$1<_langchain_core_tools1.ToolSchemaBase, any, any, any>[]>>;
1344
+ }, readonly (ClientTool | ServerTool)[]>, AgentMiddleware<undefined, undefined, unknown, readonly (ClientTool | ServerTool)[]>, ...TMiddleware, ...FlattenSubAgentMiddleware<TSubagents>], TTools>>;
1271
1345
  //#endregion
1272
1346
  //#region src/config.d.ts
1273
1347
  /**
@@ -1398,7 +1472,7 @@ interface SkillsMiddlewareOptions {
1398
1472
  * @param options - Configuration options
1399
1473
  * @returns AgentMiddleware for skills loading and injection
1400
1474
  */
1401
- declare function createSkillsMiddleware(options: SkillsMiddlewareOptions): AgentMiddleware;
1475
+ declare function createSkillsMiddleware(options: SkillsMiddlewareOptions): AgentMiddleware<any, undefined, unknown, readonly (_langchain_core_tools3.ClientTool | _langchain_core_tools3.ServerTool)[]>;
1402
1476
  //#endregion
1403
1477
  //#region src/middleware/agent-memory.d.ts
1404
1478
  /**
@@ -1422,7 +1496,7 @@ interface AgentMemoryMiddlewareOptions {
1422
1496
  * @param options - Configuration options
1423
1497
  * @returns AgentMiddleware for memory loading and injection
1424
1498
  */
1425
- declare function createAgentMemoryMiddleware(options: AgentMemoryMiddlewareOptions): AgentMiddleware<any, undefined, unknown, readonly (_langchain_core_tools1.ClientTool | _langchain_core_tools1.ServerTool)[]>;
1499
+ declare function createAgentMemoryMiddleware(options: AgentMemoryMiddlewareOptions): AgentMiddleware<any, undefined, unknown, readonly (_langchain_core_tools3.ClientTool | _langchain_core_tools3.ServerTool)[]>;
1426
1500
  //#endregion
1427
1501
  //#region src/skills/loader.d.ts
1428
1502
  /**
@@ -1506,5 +1580,5 @@ declare function parseSkillMetadata(skillMdPath: string, source: "user" | "proje
1506
1580
  */
1507
1581
  declare function listSkills(options: ListSkillsOptions): SkillMetadata[];
1508
1582
  //#endregion
1509
- export { type AgentMemoryMiddlewareOptions, type BackendFactory, type BackendProtocol, BaseSandbox, type CompiledSubAgent, CompositeBackend, type CreateDeepAgentParams, type EditResult, type ExecuteResponse, type FileData, type FileDownloadResponse, type FileInfo, type FileOperationError, type FileUploadResponse, FilesystemBackend, type FilesystemMiddlewareOptions, type GrepMatch, type ListSkillsOptions, MAX_SKILL_DESCRIPTION_LENGTH, MAX_SKILL_FILE_SIZE, MAX_SKILL_NAME_LENGTH, type MaybePromise, type SandboxBackendProtocol, type Settings, type SettingsOptions, type SkillMetadata, type SkillsMiddlewareOptions, StateBackend, StoreBackend, type SubAgent, type SubAgentMiddlewareOptions, type WriteResult, createAgentMemoryMiddleware, createDeepAgent, createFilesystemMiddleware, createPatchToolCallsMiddleware, createSettings, createSkillsMiddleware, createSubAgentMiddleware, findProjectRoot, isSandboxBackend, listSkills, parseSkillMetadata };
1583
+ export { type AgentMemoryMiddlewareOptions, type BackendFactory, type BackendProtocol, BaseSandbox, type CompiledSubAgent, CompositeBackend, type CreateDeepAgentParams, type EditResult, type ExecuteResponse, type FileData, type FileDownloadResponse, type FileInfo, type FileOperationError, type FileUploadResponse, FilesystemBackend, type FilesystemMiddlewareOptions, type GrepMatch, type ListSkillsOptions, MAX_SKILL_DESCRIPTION_LENGTH, MAX_SKILL_FILE_SIZE, MAX_SKILL_NAME_LENGTH, type MaybePromise, type MergedDeepAgentState, type SandboxBackendProtocol, type Settings, type SettingsOptions, type SkillMetadata, type SkillsMiddlewareOptions, StateBackend, StoreBackend, type SubAgent, type SubAgentMiddlewareOptions, type WriteResult, createAgentMemoryMiddleware, createDeepAgent, createFilesystemMiddleware, createPatchToolCallsMiddleware, createSettings, createSkillsMiddleware, createSubAgentMiddleware, findProjectRoot, isSandboxBackend, listSkills, parseSkillMetadata };
1510
1584
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { AIMessage, ToolMessage, anthropicPromptCachingMiddleware, createAgent, createMiddleware, humanInTheLoopMiddleware, summarizationMiddleware, todoListMiddleware, tool } from "langchain";
1
+ import { AIMessage, SystemMessage, ToolMessage, anthropicPromptCachingMiddleware, createAgent, createMiddleware, humanInTheLoopMiddleware, summarizationMiddleware, todoListMiddleware, tool } from "langchain";
2
2
  import { Command, REMOVE_ALL_MESSAGES, getCurrentTaskInput, isCommand } from "@langchain/langgraph";
3
3
  import { z } from "zod/v4";
4
4
  import micromatch from "micromatch";
@@ -810,7 +810,7 @@ function createFilesystemMiddleware(options = {}) {
810
810
  filesUpdate: null
811
811
  };
812
812
  }
813
- if (result instanceof ToolMessage) {
813
+ if (ToolMessage.isInstance(result)) {
814
814
  const processed = await processToolMessage(result);
815
815
  if (processed.filesUpdate) return new Command({ update: {
816
816
  files: processed.filesUpdate,
@@ -824,7 +824,7 @@ function createFilesystemMiddleware(options = {}) {
824
824
  let hasLargeResults = false;
825
825
  const accumulatedFiles = { ...update.files || {} };
826
826
  const processedMessages = [];
827
- for (const msg of update.messages) if (msg instanceof ToolMessage) {
827
+ for (const msg of update.messages) if (ToolMessage.isInstance(msg)) {
828
828
  const processed = await processToolMessage(msg);
829
829
  processedMessages.push(processed.message);
830
830
  if (processed.filesUpdate) {
@@ -2638,13 +2638,35 @@ const BASE_PROMPT = `In order to complete the objective that the user asks of yo
2638
2638
  * - Human-in-the-loop (humanInTheLoopMiddleware) - optional
2639
2639
  *
2640
2640
  * @param params Configuration parameters for the agent
2641
- * @returns ReactAgent instance ready for invocation
2641
+ * @returns ReactAgent instance ready for invocation with properly inferred state types
2642
+ *
2643
+ * @example
2644
+ * ```typescript
2645
+ * // Middleware with custom state
2646
+ * const ResearchMiddleware = createMiddleware({
2647
+ * name: "ResearchMiddleware",
2648
+ * stateSchema: z.object({ research: z.string().default("") }),
2649
+ * });
2650
+ *
2651
+ * const agent = createDeepAgent({
2652
+ * middleware: [ResearchMiddleware],
2653
+ * });
2654
+ *
2655
+ * const result = await agent.invoke({ messages: [...] });
2656
+ * // result.research is properly typed as string
2657
+ * ```
2642
2658
  */
2643
2659
  function createDeepAgent(params = {}) {
2644
2660
  const { model = "claude-sonnet-4-5-20250929", tools = [], systemPrompt, middleware: customMiddleware = [], subagents = [], responseFormat, contextSchema, checkpointer, store, backend, interruptOn, name } = params;
2645
- const finalSystemPrompt = systemPrompt ? `${systemPrompt}\n\n${BASE_PROMPT}` : BASE_PROMPT;
2661
+ const finalSystemPrompt = systemPrompt ? typeof systemPrompt === "string" ? `${systemPrompt}\n\n${BASE_PROMPT}` : new SystemMessage({ content: [{
2662
+ type: "text",
2663
+ text: BASE_PROMPT
2664
+ }, ...typeof systemPrompt.content === "string" ? [{
2665
+ type: "text",
2666
+ text: systemPrompt.content
2667
+ }] : systemPrompt.content] }) : BASE_PROMPT;
2646
2668
  const filesystemBackend = backend ? backend : (config) => new StateBackend(config);
2647
- const middleware = [
2669
+ const builtInMiddleware = [
2648
2670
  todoListMiddleware(),
2649
2671
  createFilesystemMiddleware({ backend: filesystemBackend }),
2650
2672
  createSubAgentMiddleware({
@@ -2673,13 +2695,12 @@ function createDeepAgent(params = {}) {
2673
2695
  anthropicPromptCachingMiddleware({ unsupportedModelBehavior: "ignore" }),
2674
2696
  createPatchToolCallsMiddleware()
2675
2697
  ];
2676
- if (interruptOn) middleware.push(humanInTheLoopMiddleware({ interruptOn }));
2677
- middleware.push(...customMiddleware);
2698
+ if (interruptOn) builtInMiddleware.push(humanInTheLoopMiddleware({ interruptOn }));
2678
2699
  return createAgent({
2679
2700
  model,
2680
2701
  systemPrompt: finalSystemPrompt,
2681
2702
  tools,
2682
- middleware,
2703
+ middleware: [...builtInMiddleware, ...customMiddleware],
2683
2704
  responseFormat,
2684
2705
  contextSchema,
2685
2706
  checkpointer,