langchain 1.4.2 → 1.4.4
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/CHANGELOG.md +22 -0
- package/dist/agents/ReactAgent.cjs +15 -1
- package/dist/agents/ReactAgent.cjs.map +1 -1
- package/dist/agents/ReactAgent.d.cts +2 -2
- package/dist/agents/ReactAgent.d.cts.map +1 -1
- package/dist/agents/ReactAgent.d.ts +2 -2
- package/dist/agents/ReactAgent.d.ts.map +1 -1
- package/dist/agents/ReactAgent.js +16 -2
- package/dist/agents/ReactAgent.js.map +1 -1
- package/dist/agents/annotation.cjs.map +1 -1
- package/dist/agents/annotation.js.map +1 -1
- package/dist/agents/index.cjs.map +1 -1
- package/dist/agents/index.d.cts +28 -28
- package/dist/agents/index.d.cts.map +1 -1
- package/dist/agents/index.d.ts +28 -28
- package/dist/agents/index.d.ts.map +1 -1
- package/dist/agents/index.js.map +1 -1
- package/dist/agents/middleware/contextEditing.d.cts +1 -1
- package/dist/agents/middleware/contextEditing.d.ts +1 -1
- package/dist/agents/middleware/dynamicSystemPrompt.d.cts +1 -1
- package/dist/agents/middleware/dynamicSystemPrompt.d.ts +1 -1
- package/dist/agents/middleware/hitl.d.cts +1 -1
- package/dist/agents/middleware/hitl.d.ts +1 -1
- package/dist/agents/middleware/llmToolSelector.d.cts +1 -1
- package/dist/agents/middleware/llmToolSelector.d.ts +1 -1
- package/dist/agents/middleware/modelCallLimit.d.cts +1 -1
- package/dist/agents/middleware/modelCallLimit.d.ts +1 -1
- package/dist/agents/middleware/modelFallback.d.cts +1 -1
- package/dist/agents/middleware/modelFallback.d.ts +1 -1
- package/dist/agents/middleware/modelRetry.d.cts +1 -1
- package/dist/agents/middleware/modelRetry.d.ts +1 -1
- package/dist/agents/middleware/pii.d.cts +1 -1
- package/dist/agents/middleware/pii.d.ts +1 -1
- package/dist/agents/middleware/piiRedaction.d.cts +1 -1
- package/dist/agents/middleware/piiRedaction.d.ts +1 -1
- package/dist/agents/middleware/provider/anthropic/promptCaching.d.cts +1 -1
- package/dist/agents/middleware/provider/anthropic/promptCaching.d.ts +1 -1
- package/dist/agents/middleware/summarization.d.cts +1 -1
- package/dist/agents/middleware/summarization.d.ts +1 -1
- package/dist/agents/middleware/todoListMiddleware.d.cts +1 -1
- package/dist/agents/middleware/todoListMiddleware.d.ts +1 -1
- package/dist/agents/middleware/toolCallLimit.d.cts +1 -1
- package/dist/agents/middleware/toolCallLimit.d.ts +1 -1
- package/dist/agents/middleware/toolEmulator.d.cts +1 -1
- package/dist/agents/middleware/toolEmulator.d.ts +1 -1
- package/dist/agents/middleware/toolRetry.d.cts +1 -1
- package/dist/agents/middleware/toolRetry.d.ts +1 -1
- package/dist/agents/middleware/types.cjs.map +1 -1
- package/dist/agents/middleware/types.d.cts +41 -18
- package/dist/agents/middleware/types.d.cts.map +1 -1
- package/dist/agents/middleware/types.d.ts +41 -18
- package/dist/agents/middleware/types.d.ts.map +1 -1
- package/dist/agents/middleware/types.js.map +1 -1
- package/dist/agents/middleware.cjs +4 -1
- package/dist/agents/middleware.cjs.map +1 -1
- package/dist/agents/middleware.d.cts +10 -3
- package/dist/agents/middleware.d.cts.map +1 -1
- package/dist/agents/middleware.d.ts +10 -3
- package/dist/agents/middleware.d.ts.map +1 -1
- package/dist/agents/middleware.js +4 -1
- package/dist/agents/middleware.js.map +1 -1
- package/dist/agents/nodes/AfterAgentNode.cjs.map +1 -1
- package/dist/agents/nodes/AfterAgentNode.js.map +1 -1
- package/dist/agents/nodes/AfterModelNode.cjs.map +1 -1
- package/dist/agents/nodes/AfterModelNode.js.map +1 -1
- package/dist/agents/nodes/AgentNode.cjs +1 -1
- package/dist/agents/nodes/AgentNode.cjs.map +1 -1
- package/dist/agents/nodes/AgentNode.js +1 -1
- package/dist/agents/nodes/AgentNode.js.map +1 -1
- package/dist/agents/nodes/BeforeAgentNode.cjs.map +1 -1
- package/dist/agents/nodes/BeforeAgentNode.js.map +1 -1
- package/dist/agents/nodes/BeforeModelNode.cjs.map +1 -1
- package/dist/agents/nodes/BeforeModelNode.js.map +1 -1
- package/dist/agents/nodes/middleware.cjs +2 -2
- package/dist/agents/nodes/middleware.cjs.map +1 -1
- package/dist/agents/nodes/middleware.js +3 -3
- package/dist/agents/nodes/middleware.js.map +1 -1
- package/dist/agents/nodes/utils.cjs +5 -3
- package/dist/agents/nodes/utils.cjs.map +1 -1
- package/dist/agents/nodes/utils.js +5 -3
- package/dist/agents/nodes/utils.js.map +1 -1
- package/dist/agents/types.d.cts +24 -12
- package/dist/agents/types.d.cts.map +1 -1
- package/dist/agents/types.d.ts +24 -12
- package/dist/agents/types.d.ts.map +1 -1
- package/dist/agents/utils.cjs +22 -0
- package/dist/agents/utils.cjs.map +1 -1
- package/dist/agents/utils.js +22 -1
- package/dist/agents/utils.js.map +1 -1
- package/dist/browser.d.cts +3 -3
- package/dist/browser.d.ts +3 -3
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BeforeAgentNode.js","names":[],"sources":["../../../src/agents/nodes/BeforeAgentNode.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"BeforeAgentNode.js","names":[],"sources":["../../../src/agents/nodes/BeforeAgentNode.ts"],"sourcesContent":["import { RunnableConfig } from \"@langchain/core/runnables\";\nimport { MiddlewareNode } from \"./middleware.js\";\nimport type {\n AnyAgentMiddleware,\n MiddlewareResult,\n} from \"../middleware/types.js\";\nimport type { AgentBuiltInState, Runtime } from \"../runtime.js\";\nimport { getHookFunction } from \"../middleware/utils.js\";\n\n/**\n * Node for executing a single middleware's beforeAgent hook.\n */\nexport class BeforeAgentNode<\n TStateSchema extends Record<string, unknown> = Record<string, unknown>,\n TContextSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends MiddlewareNode<TStateSchema, TContextSchema> {\n lc_namespace = [\"langchain\", \"agents\", \"beforeAgentNodes\"];\n\n constructor(public middleware: AnyAgentMiddleware) {\n super({\n name: `BeforeAgentNode_${middleware.name}`,\n func: async (\n state: TStateSchema,\n config?: RunnableConfig<TContextSchema>\n ) => this.invokeMiddleware(state, config),\n });\n }\n\n runHook(state: TStateSchema, runtime: Runtime<TContextSchema>) {\n const fn = getHookFunction(this.middleware.beforeAgent!);\n return fn(\n state as Record<string, unknown> & AgentBuiltInState,\n runtime as Runtime<unknown>\n ) as Promise<MiddlewareResult<TStateSchema>>;\n }\n}\n"],"mappings":";;;;;;AAYA,IAAa,kBAAb,cAGU,eAA6C;CACrD,eAAe;EAAC;EAAa;EAAU;EAAmB;CAE1D,YAAY,YAAuC;AACjD,QAAM;GACJ,MAAM,mBAAmB,WAAW;GACpC,MAAM,OACJ,OACA,WACG,KAAK,iBAAiB,OAAO,OAAO;GAC1C,CAAC;AAPe,OAAA,aAAA;;CAUnB,QAAQ,OAAqB,SAAkC;AAE7D,SADW,gBAAgB,KAAK,WAAW,YAAa,CAEtD,OACA,QACD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BeforeModelNode.cjs","names":["MiddlewareNode","getHookFunction"],"sources":["../../../src/agents/nodes/BeforeModelNode.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"BeforeModelNode.cjs","names":["MiddlewareNode","getHookFunction"],"sources":["../../../src/agents/nodes/BeforeModelNode.ts"],"sourcesContent":["import { RunnableConfig } from \"@langchain/core/runnables\";\nimport { MiddlewareNode } from \"./middleware.js\";\nimport type {\n AnyAgentMiddleware,\n MiddlewareResult,\n} from \"../middleware/types.js\";\nimport type { AgentBuiltInState, Runtime } from \"../runtime.js\";\nimport { getHookFunction } from \"../middleware/utils.js\";\n\n/**\n * Node for executing a single middleware's beforeModel hook.\n */\nexport class BeforeModelNode<\n TStateSchema extends Record<string, unknown> = Record<string, unknown>,\n TContextSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends MiddlewareNode<TStateSchema, TContextSchema> {\n lc_namespace = [\"langchain\", \"agents\", \"beforeModelNodes\"];\n\n constructor(public middleware: AnyAgentMiddleware) {\n super({\n name: `BeforeModelNode_${middleware.name}`,\n func: async (\n state: TStateSchema,\n config?: RunnableConfig<TContextSchema>\n ) => this.invokeMiddleware(state, config),\n });\n }\n\n runHook(state: TStateSchema, runtime: Runtime<TContextSchema>) {\n const fn = getHookFunction(this.middleware.beforeModel!);\n return fn(\n state as Record<string, unknown> & AgentBuiltInState,\n runtime as Runtime<unknown>\n ) as Promise<MiddlewareResult<TStateSchema>>;\n }\n}\n"],"mappings":";;;;;;AAYA,IAAa,kBAAb,cAGUA,mBAAAA,eAA6C;CACrD,eAAe;EAAC;EAAa;EAAU;EAAmB;CAE1D,YAAY,YAAuC;AACjD,QAAM;GACJ,MAAM,mBAAmB,WAAW;GACpC,MAAM,OACJ,OACA,WACG,KAAK,iBAAiB,OAAO,OAAO;GAC1C,CAAC;AAPe,OAAA,aAAA;;CAUnB,QAAQ,OAAqB,SAAkC;AAE7D,SADWC,cAAAA,gBAAgB,KAAK,WAAW,YAAa,CAEtD,OACA,QACD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BeforeModelNode.js","names":[],"sources":["../../../src/agents/nodes/BeforeModelNode.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"BeforeModelNode.js","names":[],"sources":["../../../src/agents/nodes/BeforeModelNode.ts"],"sourcesContent":["import { RunnableConfig } from \"@langchain/core/runnables\";\nimport { MiddlewareNode } from \"./middleware.js\";\nimport type {\n AnyAgentMiddleware,\n MiddlewareResult,\n} from \"../middleware/types.js\";\nimport type { AgentBuiltInState, Runtime } from \"../runtime.js\";\nimport { getHookFunction } from \"../middleware/utils.js\";\n\n/**\n * Node for executing a single middleware's beforeModel hook.\n */\nexport class BeforeModelNode<\n TStateSchema extends Record<string, unknown> = Record<string, unknown>,\n TContextSchema extends Record<string, unknown> = Record<string, unknown>,\n> extends MiddlewareNode<TStateSchema, TContextSchema> {\n lc_namespace = [\"langchain\", \"agents\", \"beforeModelNodes\"];\n\n constructor(public middleware: AnyAgentMiddleware) {\n super({\n name: `BeforeModelNode_${middleware.name}`,\n func: async (\n state: TStateSchema,\n config?: RunnableConfig<TContextSchema>\n ) => this.invokeMiddleware(state, config),\n });\n }\n\n runHook(state: TStateSchema, runtime: Runtime<TContextSchema>) {\n const fn = getHookFunction(this.middleware.beforeModel!);\n return fn(\n state as Record<string, unknown> & AgentBuiltInState,\n runtime as Runtime<unknown>\n ) as Promise<MiddlewareResult<TStateSchema>>;\n }\n}\n"],"mappings":";;;;;;AAYA,IAAa,kBAAb,cAGU,eAA6C;CACrD,eAAe;EAAC;EAAa;EAAU;EAAmB;CAE1D,YAAY,YAAuC;AACjD,QAAM;GACJ,MAAM,mBAAmB,WAAW;GACpC,MAAM,OACJ,OACA,WACG,KAAK,iBAAiB,OAAO,OAAO;GAC1C,CAAC;AAPe,OAAA,aAAA;;CAUnB,QAAQ,OAAqB,SAAkC;AAE7D,SADW,gBAAgB,KAAK,WAAW,YAAa,CAEtD,OACA,QACD"}
|
|
@@ -21,11 +21,11 @@ var MiddlewareNode = class extends require_RunnableCallable.RunnableCallable {
|
|
|
21
21
|
/**
|
|
22
22
|
* Parse context using middleware's contextSchema to apply defaults and validation
|
|
23
23
|
*/
|
|
24
|
-
if (this.middleware.contextSchema) {
|
|
24
|
+
if (this.middleware.contextSchema && (0, _langchain_core_utils_types.isInteropZodObject)(this.middleware.contextSchema)) {
|
|
25
25
|
/**
|
|
26
26
|
* Extract only the fields relevant to this middleware's schema
|
|
27
27
|
*/
|
|
28
|
-
const schemaShape = this.middleware.contextSchema
|
|
28
|
+
const schemaShape = (0, _langchain_core_utils_types.getInteropZodObjectShape)(this.middleware.contextSchema);
|
|
29
29
|
if (schemaShape) {
|
|
30
30
|
const relevantContext = {};
|
|
31
31
|
const invokeContext = config?.context || {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.cjs","names":["RunnableCallable","getHookConstraint","derivePrivateState"],"sources":["../../../src/agents/nodes/middleware.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport {
|
|
1
|
+
{"version":3,"file":"middleware.cjs","names":["RunnableCallable","getHookConstraint","derivePrivateState"],"sources":["../../../src/agents/nodes/middleware.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { LangGraphRunnableConfig, Command } from \"@langchain/langgraph\";\nimport {\n getInteropZodObjectShape,\n interopParse,\n isInteropZodObject,\n} from \"@langchain/core/utils/types\";\n\nimport { RunnableCallable, RunnableCallableArgs } from \"../RunnableCallable.js\";\nimport type { JumpToTarget } from \"../constants.js\";\nimport type { Runtime } from \"../runtime.js\";\nimport type {\n AnyAgentMiddleware,\n MiddlewareResult,\n} from \"../middleware/types.js\";\nimport { derivePrivateState } from \"./utils.js\";\nimport { getHookConstraint } from \"../middleware/utils.js\";\n\n/**\n * Named class for context objects to provide better error messages\n */\nclass AgentContext {}\nclass AgentRuntime {}\n\ntype NodeOutput<TStateSchema extends Record<string, any>> =\n | TStateSchema\n | Command<any, TStateSchema, string>\n | { jumpTo?: JumpToTarget };\n\nexport abstract class MiddlewareNode<\n TStateSchema extends Record<string, any>,\n TContextSchema extends Record<string, any>,\n> extends RunnableCallable<TStateSchema, NodeOutput<TStateSchema>> {\n abstract middleware: AnyAgentMiddleware;\n\n constructor(\n fields: RunnableCallableArgs<TStateSchema, NodeOutput<TStateSchema>>\n ) {\n super(fields);\n }\n\n abstract runHook(\n state: TStateSchema,\n config?: Runtime<TContextSchema>\n ): Promise<MiddlewareResult<TStateSchema>> | MiddlewareResult<TStateSchema>;\n\n async invokeMiddleware(\n invokeState: TStateSchema,\n config?: LangGraphRunnableConfig\n ): Promise<NodeOutput<TStateSchema>> {\n /**\n * Filter context based on middleware's contextSchema\n */\n let filteredContext = {} as TContextSchema;\n /**\n * Parse context using middleware's contextSchema to apply defaults and validation\n */\n if (\n this.middleware.contextSchema &&\n isInteropZodObject(this.middleware.contextSchema)\n ) {\n /**\n * Extract only the fields relevant to this middleware's schema\n */\n const schemaShape = getInteropZodObjectShape(\n this.middleware.contextSchema\n );\n if (schemaShape) {\n const relevantContext: Record<string, unknown> = {};\n const invokeContext = config?.context || {};\n for (const key of Object.keys(schemaShape)) {\n if (key in invokeContext) {\n relevantContext[key] = invokeContext[key];\n }\n }\n /**\n * Parse to apply defaults and validation, even if relevantContext is empty\n * This will throw if required fields are missing and no defaults exist\n */\n filteredContext = interopParse(\n this.middleware.contextSchema,\n relevantContext\n ) as TContextSchema;\n }\n }\n\n const state: TStateSchema = {\n ...invokeState,\n /**\n * don't overwrite possible outdated messages from other middleware nodes\n */\n messages: invokeState.messages,\n };\n\n const runtime: Runtime<TContextSchema> = {\n context: filteredContext,\n store: config?.store,\n configurable: config?.configurable,\n writer: config?.writer,\n interrupt: config?.interrupt,\n signal: config?.signal,\n };\n\n const result = await this.runHook(\n state,\n /**\n * assign runtime and context values into empty named class\n * instances to create a better error message.\n */\n Object.freeze(\n Object.assign(new AgentRuntime(), {\n ...runtime,\n context: Object.freeze(\n Object.assign(new AgentContext(), filteredContext)\n ),\n })\n )\n );\n\n /**\n * If result is undefined, the hook made no state changes — return\n * only the jumpTo sentinel so we don't re-emit every input key as\n * a state update.\n */\n if (!result) {\n return { jumpTo: undefined };\n }\n\n /**\n * Verify that the jump target is allowed for the middleware\n */\n let jumpToConstraint: JumpToTarget[] | undefined;\n let constraint: string | undefined;\n\n if (this.name?.startsWith(\"BeforeAgentNode_\")) {\n jumpToConstraint = getHookConstraint(this.middleware.beforeAgent);\n constraint = \"beforeAgent.canJumpTo\";\n } else if (this.name?.startsWith(\"BeforeModelNode_\")) {\n jumpToConstraint = getHookConstraint(this.middleware.beforeModel);\n constraint = \"beforeModel.canJumpTo\";\n } else if (this.name?.startsWith(\"AfterAgentNode_\")) {\n jumpToConstraint = getHookConstraint(this.middleware.afterAgent);\n constraint = \"afterAgent.canJumpTo\";\n } else if (this.name?.startsWith(\"AfterModelNode_\")) {\n jumpToConstraint = getHookConstraint(this.middleware.afterModel);\n constraint = \"afterModel.canJumpTo\";\n }\n\n if (\n typeof result.jumpTo === \"string\" &&\n !jumpToConstraint?.includes(result.jumpTo as JumpToTarget)\n ) {\n const suggestion =\n jumpToConstraint && jumpToConstraint.length > 0\n ? `must be one of: ${jumpToConstraint?.join(\", \")}.`\n : constraint\n ? `no ${constraint} defined in middleware ${this.middleware.name}`\n : \"\";\n throw new Error(`Invalid jump target: ${result.jumpTo}, ${suggestion}.`);\n }\n\n /**\n * If result is a control action, handle it\n */\n if (typeof result === \"object\" && \"type\" in result) {\n // Handle control actions\n if (result.type === \"terminate\") {\n if (result.error) {\n throw result.error;\n }\n return {\n ...state,\n ...(result.result || {}),\n jumpTo: result.jumpTo,\n };\n }\n\n throw new Error(`Invalid control action: ${JSON.stringify(result)}`);\n }\n\n /**\n * If result is a state update, merge it with current state\n */\n return { ...state, ...result, jumpTo: result.jumpTo };\n }\n\n get nodeOptions() {\n return {\n input: derivePrivateState(this.middleware.stateSchema),\n };\n }\n}\n"],"mappings":";;;;;;;;;AAqBA,IAAM,eAAN,MAAmB;AACnB,IAAM,eAAN,MAAmB;AAOnB,IAAsB,iBAAtB,cAGUA,yBAAAA,iBAAyD;CAGjE,YACE,QACA;AACA,QAAM,OAAO;;CAQf,MAAM,iBACJ,aACA,QACmC;;;;EAInC,IAAI,kBAAkB,EAAE;;;;AAIxB,MACE,KAAK,WAAW,kBAAA,GAAA,4BAAA,oBACG,KAAK,WAAW,cAAc,EACjD;;;;GAIA,MAAM,eAAA,GAAA,4BAAA,0BACJ,KAAK,WAAW,cACjB;AACD,OAAI,aAAa;IACf,MAAM,kBAA2C,EAAE;IACnD,MAAM,gBAAgB,QAAQ,WAAW,EAAE;AAC3C,SAAK,MAAM,OAAO,OAAO,KAAK,YAAY,CACxC,KAAI,OAAO,cACT,iBAAgB,OAAO,cAAc;;;;;AAOzC,uBAAA,GAAA,4BAAA,cACE,KAAK,WAAW,e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SAAS,aAAa;AAC/B,QAAI,OAAO,MACT,OAAM,OAAO;AAEf,WAAO;KACL,GAAG;KACH,GAAI,OAAO,UAAU,EAAE;KACvB,QAAQ,OAAO;KAChB;;AAGH,SAAM,IAAI,MAAM,2BAA2B,KAAK,UAAU,OAAO,GAAG;;;;;AAMtE,SAAO;GAAE,GAAG;GAAO,GAAG;GAAQ,QAAQ,OAAO;GAAQ;;CAGvD,IAAI,cAAc;AAChB,SAAO,EACL,OAAOC,cAAAA,mBAAmB,KAAK,WAAW,YAAY,EACvD"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { RunnableCallable } from "../RunnableCallable.js";
|
|
2
2
|
import { derivePrivateState } from "./utils.js";
|
|
3
3
|
import { getHookConstraint } from "../middleware/utils.js";
|
|
4
|
-
import { interopParse } from "@langchain/core/utils/types";
|
|
4
|
+
import { getInteropZodObjectShape, interopParse, isInteropZodObject } from "@langchain/core/utils/types";
|
|
5
5
|
//#region src/agents/nodes/middleware.ts
|
|
6
6
|
/**
|
|
7
7
|
* Named class for context objects to provide better error messages
|
|
@@ -20,11 +20,11 @@ var MiddlewareNode = class extends RunnableCallable {
|
|
|
20
20
|
/**
|
|
21
21
|
* Parse context using middleware's contextSchema to apply defaults and validation
|
|
22
22
|
*/
|
|
23
|
-
if (this.middleware.contextSchema) {
|
|
23
|
+
if (this.middleware.contextSchema && isInteropZodObject(this.middleware.contextSchema)) {
|
|
24
24
|
/**
|
|
25
25
|
* Extract only the fields relevant to this middleware's schema
|
|
26
26
|
*/
|
|
27
|
-
const schemaShape = this.middleware.contextSchema
|
|
27
|
+
const schemaShape = getInteropZodObjectShape(this.middleware.contextSchema);
|
|
28
28
|
if (schemaShape) {
|
|
29
29
|
const relevantContext = {};
|
|
30
30
|
const invokeContext = config?.context || {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.js","names":[],"sources":["../../../src/agents/nodes/middleware.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport {
|
|
1
|
+
{"version":3,"file":"middleware.js","names":[],"sources":["../../../src/agents/nodes/middleware.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { LangGraphRunnableConfig, Command } from \"@langchain/langgraph\";\nimport {\n getInteropZodObjectShape,\n interopParse,\n isInteropZodObject,\n} from \"@langchain/core/utils/types\";\n\nimport { RunnableCallable, RunnableCallableArgs } from \"../RunnableCallable.js\";\nimport type { JumpToTarget } from \"../constants.js\";\nimport type { Runtime } from \"../runtime.js\";\nimport type {\n AnyAgentMiddleware,\n MiddlewareResult,\n} from \"../middleware/types.js\";\nimport { derivePrivateState } from \"./utils.js\";\nimport { getHookConstraint } from \"../middleware/utils.js\";\n\n/**\n * Named class for context objects to provide better error messages\n */\nclass AgentContext {}\nclass AgentRuntime {}\n\ntype NodeOutput<TStateSchema extends Record<string, any>> =\n | TStateSchema\n | Command<any, TStateSchema, string>\n | { jumpTo?: JumpToTarget };\n\nexport abstract class MiddlewareNode<\n TStateSchema extends Record<string, any>,\n TContextSchema extends Record<string, any>,\n> extends RunnableCallable<TStateSchema, NodeOutput<TStateSchema>> {\n abstract middleware: AnyAgentMiddleware;\n\n constructor(\n fields: RunnableCallableArgs<TStateSchema, NodeOutput<TStateSchema>>\n ) {\n super(fields);\n }\n\n abstract runHook(\n state: TStateSchema,\n config?: Runtime<TContextSchema>\n ): Promise<MiddlewareResult<TStateSchema>> | MiddlewareResult<TStateSchema>;\n\n async invokeMiddleware(\n invokeState: TStateSchema,\n config?: LangGraphRunnableConfig\n ): Promise<NodeOutput<TStateSchema>> {\n /**\n * Filter context based on middleware's contextSchema\n */\n let filteredContext = {} as TContextSchema;\n /**\n * Parse context using middleware's contextSchema to apply defaults and validation\n */\n if (\n this.middleware.contextSchema &&\n isInteropZodObject(this.middleware.contextSchema)\n ) {\n /**\n * Extract only the fields relevant to this middleware's schema\n */\n const schemaShape = getInteropZodObjectShape(\n this.middleware.contextSchema\n );\n if (schemaShape) {\n const relevantContext: Record<string, unknown> = {};\n const invokeContext = config?.context || {};\n for (const key of Object.keys(schemaShape)) {\n if (key in invokeContext) {\n relevantContext[key] = invokeContext[key];\n }\n }\n /**\n * Parse to apply defaults and validation, even if relevantContext is empty\n * This will throw if required fields are missing and no defaults exist\n */\n filteredContext = interopParse(\n this.middleware.contextSchema,\n relevantContext\n ) as TContextSchema;\n }\n }\n\n const state: TStateSchema = {\n ...invokeState,\n /**\n * don't overwrite possible outdated messages from other middleware nodes\n */\n messages: invokeState.messages,\n };\n\n const runtime: Runtime<TContextSchema> = {\n context: filteredContext,\n store: config?.store,\n configurable: config?.configurable,\n writer: config?.writer,\n interrupt: config?.interrupt,\n signal: config?.signal,\n };\n\n const result = await this.runHook(\n state,\n /**\n * assign runtime and context values into empty named class\n * instances to create a better error message.\n */\n Object.freeze(\n Object.assign(new AgentRuntime(), {\n ...runtime,\n context: Object.freeze(\n Object.assign(new AgentContext(), filteredContext)\n ),\n })\n )\n );\n\n /**\n * If result is undefined, the hook made no state changes — return\n * only the jumpTo sentinel so we don't re-emit every input key as\n * a state update.\n */\n if (!result) {\n return { jumpTo: undefined };\n }\n\n /**\n * Verify that the jump target is allowed for the middleware\n */\n let jumpToConstraint: JumpToTarget[] | undefined;\n let constraint: string | undefined;\n\n if (this.name?.startsWith(\"BeforeAgentNode_\")) {\n jumpToConstraint = getHookConstraint(this.middleware.beforeAgent);\n constraint = \"beforeAgent.canJumpTo\";\n } else if (this.name?.startsWith(\"BeforeModelNode_\")) {\n jumpToConstraint = getHookConstraint(this.middleware.beforeModel);\n constraint = \"beforeModel.canJumpTo\";\n } else if (this.name?.startsWith(\"AfterAgentNode_\")) {\n jumpToConstraint = getHookConstraint(this.middleware.afterAgent);\n constraint = \"afterAgent.canJumpTo\";\n } else if (this.name?.startsWith(\"AfterModelNode_\")) {\n jumpToConstraint = getHookConstraint(this.middleware.afterModel);\n constraint = \"afterModel.canJumpTo\";\n }\n\n if (\n typeof result.jumpTo === \"string\" &&\n !jumpToConstraint?.includes(result.jumpTo as JumpToTarget)\n ) {\n const suggestion =\n jumpToConstraint && jumpToConstraint.length > 0\n ? `must be one of: ${jumpToConstraint?.join(\", \")}.`\n : constraint\n ? `no ${constraint} defined in middleware ${this.middleware.name}`\n : \"\";\n throw new Error(`Invalid jump target: ${result.jumpTo}, ${suggestion}.`);\n }\n\n /**\n * If result is a control action, handle it\n */\n if (typeof result === \"object\" && \"type\" in result) {\n // Handle control actions\n if (result.type === \"terminate\") {\n if (result.error) {\n throw result.error;\n }\n return {\n ...state,\n ...(result.result || {}),\n jumpTo: result.jumpTo,\n };\n }\n\n throw new Error(`Invalid control action: ${JSON.stringify(result)}`);\n }\n\n /**\n * If result is a state update, merge it with current state\n */\n return { ...state, ...result, jumpTo: result.jumpTo };\n }\n\n get nodeOptions() {\n return {\n input: derivePrivateState(this.middleware.stateSchema),\n };\n }\n}\n"],"mappings":";;;;;;;;AAqBA,IAAM,eAAN,MAAmB;AACnB,IAAM,eAAN,MAAmB;AAOnB,IAAsB,iBAAtB,cAGU,iBAAyD;CAGjE,YACE,QACA;AACA,QAAM,OAAO;;CAQf,MAAM,iBACJ,aACA,QACmC;;;;EAInC,IAAI,kBAAkB,EAAE;;;;AAIxB,MACE,KAAK,WAAW,iBAChB,mBAAmB,KAAK,WAAW,cAAc,EACjD;;;;GAIA,MAAM,cAAc,yBAClB,KAAK,WAAW,cACjB;AACD,OAAI,aAAa;IACf,MAAM,kBAA2C,EAAE;IACnD,MAAM,gBAAgB,QAAQ,WAAW,EAAE;AAC3C,SAAK,MAAM,OAAO,OAAO,KAAK,YAAY,CACxC,KAAI,OAAO,cACT,iBAAgB,OAAO,cAAc;;;;;AAOzC,sBAAkB,aAChB,KAAK,WAAW,eAChB,gBACD;;;EAIL,MAAM,QAAsB;GAC1B,GAAG;GAIH,UAAU,YAAY;GACvB;EAED,MAAM,UAAmC;GACvC,SAAS;GACT,OAAO,QAAQ;GACf,cAAc,QAAQ;GACtB,QAAQ,Q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AMtE,SAAO;GAAE,GAAG;GAAO,GAAG;GAAQ,QAAQ,OAAO;GAAQ;;CAGvD,IAAI,cAAc;AAChB,SAAO,EACL,OAAO,mBAAmB,KAAK,WAAW,YAAY,EACvD"}
|
|
@@ -17,13 +17,14 @@ async function initializeMiddlewareStates(middlewareList, state) {
|
|
|
17
17
|
* skip middleware if it doesn't have a state schema
|
|
18
18
|
*/
|
|
19
19
|
if (!middleware.stateSchema) continue;
|
|
20
|
-
let zodSchema
|
|
20
|
+
let zodSchema;
|
|
21
21
|
if (_langchain_langgraph.StateSchema.isInstance(middleware.stateSchema)) {
|
|
22
22
|
const zodShape = {};
|
|
23
23
|
for (const [key, field] of Object.entries(middleware.stateSchema.fields)) if (_langchain_langgraph.ReducedValue.isInstance(field)) zodShape[key] = field.inputSchema || field.valueSchema;
|
|
24
24
|
else zodShape[key] = field;
|
|
25
25
|
zodSchema = zod_v4.z.object(zodShape);
|
|
26
|
-
}
|
|
26
|
+
} else if ((0, _langchain_core_utils_types.isInteropZodObject)(middleware.stateSchema)) zodSchema = middleware.stateSchema;
|
|
27
|
+
else continue;
|
|
27
28
|
const parseResult = await (0, _langchain_core_utils_types.interopSafeParseAsync)((0, _langchain_core_utils_types.interopZodObjectMakeFieldsOptional)(zodSchema, (key) => key.startsWith("_")), state);
|
|
28
29
|
if (parseResult.success) {
|
|
29
30
|
Object.assign(middlewareStates, parseResult.data);
|
|
@@ -55,7 +56,8 @@ function derivePrivateState(stateSchema) {
|
|
|
55
56
|
shape = {};
|
|
56
57
|
for (const [key, field] of Object.entries(stateSchema.fields)) if (_langchain_langgraph.ReducedValue.isInstance(field)) shape[key] = field.inputSchema || field.valueSchema;
|
|
57
58
|
else shape[key] = field;
|
|
58
|
-
} else shape = (0, _langchain_core_utils_types.getInteropZodObjectShape)(stateSchema);
|
|
59
|
+
} else if ((0, _langchain_core_utils_types.isInteropZodObject)(stateSchema)) shape = (0, _langchain_core_utils_types.getInteropZodObjectShape)(stateSchema);
|
|
60
|
+
else return zod_v4.z.object(builtInStateSchema);
|
|
59
61
|
const privateShape = { ...builtInStateSchema };
|
|
60
62
|
for (const [key, value] of Object.entries(shape)) if (key.startsWith("_")) privateShape[key] = value.optional();
|
|
61
63
|
else privateShape[key] = value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.cjs","names":["StateSchema","ReducedValue","z","END"],"sources":["../../../src/agents/nodes/utils.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v4\";\nimport { type BaseMessage } from \"@langchain/core/messages\";\nimport {\n getInteropZodObjectShape,\n interopSafeParseAsync,\n interopZodObjectMakeFieldsOptional,\n interopZodObjectPartial,\n isInteropZodObject,\n isZodSchemaV4,\n type InteropZodObject,\n} from \"@langchain/core/utils/types\";\nimport type { StateDefinitionInit } from \"@langchain/langgraph\";\nimport { END, StateSchema, ReducedValue } from \"@langchain/langgraph\";\n\nimport type { JumpTo } from \"../types.js\";\nimport type { AgentMiddleware } from \"../middleware/types.js\";\n\n/**\n * Helper function to initialize middleware state defaults.\n * This is used to ensure all middleware state properties are initialized.\n *\n * Private properties (starting with _) are automatically made optional since\n * users cannot provide them when invoking the agent.\n */\nexport async function initializeMiddlewareStates(\n middlewareList: readonly AgentMiddleware[],\n state: unknown\n): Promise<Record<string, any>> {\n const middlewareStates: Record<string, any> = {};\n\n for (const middleware of middlewareList) {\n /**\n * skip middleware if it doesn't have a state schema\n */\n if (!middleware.stateSchema) {\n continue;\n }\n\n // Convert StateSchema to Zod object if needed\n let zodSchema = middleware.stateSchema;\n if (StateSchema.isInstance(middleware.stateSchema)) {\n const zodShape: Record<string, any> = {};\n for (const [key, field] of Object.entries(\n middleware.stateSchema.fields\n )) {\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n zodShape[key] = field.inputSchema || field.valueSchema;\n } else {\n zodShape[key] = field;\n }\n }\n zodSchema = z.object(zodShape);\n }\n\n // Create a modified schema where private properties are optional\n const modifiedSchema = interopZodObjectMakeFieldsOptional(\n zodSchema,\n (key) => key.startsWith(\"_\")\n );\n\n // Use safeParse with the modified schema\n const parseResult = await interopSafeParseAsync(modifiedSchema, state);\n if (parseResult.success) {\n Object.assign(middlewareStates, parseResult.data);\n continue;\n }\n\n /**\n * If safeParse fails, there are required public fields missing.\n * Note: Zod v3 uses message \"Required\", Zod v4 uses \"Invalid input: expected X, received undefined\"\n */\n const requiredFields = parseResult.error.issues\n .filter((issue) => issue.code === \"invalid_type\")\n .map((issue) => ` - ${issue.path.join(\".\")}: Required`)\n .join(\"\\n\");\n\n throw new Error(\n `Middleware \"${middleware.name}\" has required state fields that must be initialized:\\n` +\n `${requiredFields}\\n\\n` +\n `To fix this, either:\\n` +\n `1. Provide default values in your middleware's state schema using .default():\\n` +\n ` stateSchema: z.object({\\n` +\n ` myField: z.string().default(\"default value\")\\n` +\n ` })\\n\\n` +\n `2. Or make the fields optional using .optional():\\n` +\n ` stateSchema: z.object({\\n` +\n ` myField: z.string().optional()\\n` +\n ` })\\n\\n` +\n `3. Or ensure you pass these values when invoking the agent:\\n` +\n ` agent.invoke({\\n` +\n ` messages: [...],\\n` +\n ` ${parseResult.error.issues[0]?.path.join(\".\")}: \"value\"\\n` +\n ` })`\n );\n }\n\n return middlewareStates;\n}\n\n/**\n * Users can define private and public state for a middleware. Private state properties start with an underscore.\n * This function will return the private state properties from the state schema, making all of them optional.\n * @param stateSchema - The middleware state schema\n * @returns A new schema containing only the private properties (underscore-prefixed), all made optional\n */\nexport function derivePrivateState(\n stateSchema?: InteropZodObject | StateSchema<any>\n): InteropZodObject {\n const builtInStateSchema = {\n messages: z.custom<BaseMessage[]>(() => []),\n // Include optional structuredResponse so after_agent hooks can access/modify it\n structuredResponse: z.any().optional(),\n };\n\n if (!stateSchema) {\n return z.object(builtInStateSchema);\n }\n\n // Extract shape from either StateSchema or Zod object\n let shape: Record<string, any>;\n if (StateSchema.isInstance(stateSchema)) {\n // For StateSchema, extract Zod schemas from fields\n shape = {};\n for (const [key, field] of Object.entries(stateSchema.fields)) {\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n shape[key] = field.inputSchema || field.valueSchema;\n } else {\n shape[key] = field;\n }\n }\n } else {\n shape = getInteropZodObjectShape(stateSchema);\n }\n\n const privateShape: Record<string, any> = { ...builtInStateSchema };\n\n // Filter properties that start with underscore and make them optional\n for (const [key, value] of Object.entries(shape)) {\n if (key.startsWith(\"_\")) {\n // Make the private property optional\n privateShape[key] = value.optional();\n } else {\n privateShape[key] = value;\n }\n }\n\n // Return a new schema with only private properties (all optional)\n return z.object(privateShape);\n}\n\n/**\n * Converts any supported schema type (ZodObject, StateSchema, AnnotationRoot) to a partial Zod object.\n * This is useful for parsing state loosely where all fields are optional.\n *\n * @param schema - The schema to convert (InteropZodObject, StateSchema, or AnnotationRoot)\n * @returns A partial Zod object schema where all fields are optional\n */\nexport function toPartialZodObject(\n schema: StateDefinitionInit\n): InteropZodObject {\n // Handle ZodObject directly\n if (isInteropZodObject(schema)) {\n return interopZodObjectPartial(schema);\n }\n\n // Handle StateSchema: convert fields to Zod shape, then make partial\n if (StateSchema.isInstance(schema)) {\n const partialShape: Record<string, any> = {};\n for (const [key, field] of Object.entries(schema.fields)) {\n let fieldSchema: unknown;\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n fieldSchema = field.inputSchema || field.valueSchema;\n } else {\n fieldSchema = field;\n }\n // Only call .optional() on Zod v4 schemas, otherwise use z.any()\n partialShape[key] = isZodSchemaV4(fieldSchema)\n ? (fieldSchema as any).optional()\n : z.any().optional();\n }\n return z.object(partialShape);\n }\n\n // Fallback: return empty object schema\n return z.object({});\n}\n\n/**\n * Parse `jumpTo` target from user facing labels to a LangGraph node names\n */\nexport function parseJumpToTarget(target: string): JumpTo;\nexport function parseJumpToTarget(target?: string): JumpTo | undefined {\n if (!target) {\n return undefined;\n }\n\n /**\n * if target is already a valid jump target, return it\n */\n if ([\"model_request\", \"tools\", END].includes(target)) {\n return target as JumpTo;\n }\n\n if (target === \"model\") {\n return \"model_request\";\n }\n if (target === \"tools\") {\n return \"tools\";\n }\n if (target === \"end\") {\n return END;\n }\n\n throw new Error(\n `Invalid jump target: ${target}, must be \"model\", \"tools\" or \"end\".`\n );\n}\n\n/**\n * TypeScript currently doesn't support types for `AbortSignal.any`\n * @see https://github.com/microsoft/TypeScript/issues/60695\n */\ndeclare const AbortSignal: {\n any(signals: AbortSignal[]): AbortSignal;\n};\n\n/**\n * `config` always contains a signal from LangGraphs Pregel class.\n * To ensure we acknowledge the abort signal from the user, we merge it\n * with the signal from the ToolNode.\n *\n * @param signals - The signals to merge.\n * @returns The merged signal.\n */\nexport function mergeAbortSignals(\n ...signals: (AbortSignal | undefined)[]\n): AbortSignal {\n return AbortSignal.any(\n signals.filter(\n (maybeSignal): maybeSignal is AbortSignal =>\n maybeSignal !== null &&\n maybeSignal !== undefined &&\n typeof maybeSignal === \"object\" &&\n \"aborted\" in maybeSignal &&\n typeof maybeSignal.aborted === \"boolean\"\n )\n );\n}\n"],"mappings":";;;;;;;;;;;;AAyBA,eAAsB,2BACpB,gBACA,OAC8B;CAC9B,MAAM,mBAAwC,EAAE;AAEhD,MAAK,MAAM,cAAc,gBAAgB;;;;AAIvC,MAAI,CAAC,WAAW,YACd;EAIF,IAAI,YAAY,WAAW;AAC3B,MAAIA,qBAAAA,YAAY,WAAW,WAAW,YAAY,EAAE;GAClD,MAAM,WAAgC,EAAE;AACxC,QAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAChC,WAAW,YAAY,OACxB,CACC,KAAIC,qBAAAA,aAAa,WAAW,MAAM,CAEhC,UAAS,OAAO,MAAM,eAAe,MAAM;OAE3C,UAAS,OAAO;AAGpB,eAAYC,OAAAA,EAAE,OAAO,SAAS;;EAUhC,MAAM,cAAc,OAAA,GAAA,4BAAA,wBAAA,GAAA,4BAAA,oCALlB,YACC,QAAQ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BAAAA,YAAY,WAAW,OAAO,EAAE;EAClC,MAAM,eAAoC,EAAE;AAC5C,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,OAAO,EAAE;GACxD,IAAI;AACJ,OAAIC,qBAAAA,aAAa,WAAW,MAAM,CAEhC,eAAc,MAAM,eAAe,MAAM;OAEzC,eAAc;AAGhB,gBAAa,QAAA,GAAA,4BAAA,eAAqB,YAAY,GACzC,YAAoB,UAAU,GAC/BC,OAAAA,EAAE,KAAK,CAAC,UAAU;;AAExB,SAAOA,OAAAA,EAAE,OAAO,aAAa;;AAI/B,QAAOA,OAAAA,EAAE,OAAO,EAAE,CAAC;;AAOrB,SAAgB,kBAAkB,QAAqC;AACrE,KAAI,CAAC,OACH;;;;AAMF,KAAI;EAAC;EAAiB;EAASC,qBAAAA;EAAI,CAAC,SAAS,OAAO,CAClD,QAAO;AAGT,KAAI,WAAW,QACb,QAAO;AAET,KAAI,WAAW,QACb,QAAO;AAET,KAAI,WAAW,MACb,QAAOA,qBAAAA;AAGT,OAAM,IAAI,MACR,wBAAwB,OAAO,sCAChC;;;;;;;;;;AAmBH,SAAgB,kBACd,GAAG,SACU;AACb,QAAO,YAAY,IACjB,QAAQ,QACL,gBACC,gBAAgB,QAChB,gBAAgB,KAAA,KAChB,OAAO,gBAAgB,YACvB,aAAa,eACb,OAAO,YAAY,YAAY,UAClC,CACF"}
|
|
1
|
+
{"version":3,"file":"utils.cjs","names":["StateSchema","ReducedValue","z","END"],"sources":["../../../src/agents/nodes/utils.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v4\";\nimport { type BaseMessage } from \"@langchain/core/messages\";\nimport {\n getInteropZodObjectShape,\n interopSafeParseAsync,\n interopZodObjectMakeFieldsOptional,\n interopZodObjectPartial,\n isInteropZodObject,\n isZodSchemaV4,\n type InteropZodObject,\n} from \"@langchain/core/utils/types\";\nimport type { StateDefinitionInit } from \"@langchain/langgraph\";\nimport { END, StateSchema, ReducedValue } from \"@langchain/langgraph\";\n\nimport type { JumpTo } from \"../types.js\";\nimport type { AnyAgentMiddleware } from \"../middleware/types.js\";\n\n/**\n * Helper function to initialize middleware state defaults.\n * This is used to ensure all middleware state properties are initialized.\n *\n * Private properties (starting with _) are automatically made optional since\n * users cannot provide them when invoking the agent.\n */\nexport async function initializeMiddlewareStates(\n middlewareList: readonly AnyAgentMiddleware[],\n state: unknown\n): Promise<Record<string, any>> {\n const middlewareStates: Record<string, any> = {};\n\n for (const middleware of middlewareList) {\n /**\n * skip middleware if it doesn't have a state schema\n */\n if (!middleware.stateSchema) {\n continue;\n }\n\n // Convert StateSchema to Zod object if needed\n let zodSchema: InteropZodObject;\n if (StateSchema.isInstance(middleware.stateSchema)) {\n const zodShape: Record<string, any> = {};\n for (const [key, field] of Object.entries(\n middleware.stateSchema.fields\n )) {\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n zodShape[key] = field.inputSchema || field.valueSchema;\n } else {\n zodShape[key] = field;\n }\n }\n zodSchema = z.object(zodShape);\n } else if (isInteropZodObject(middleware.stateSchema)) {\n zodSchema = middleware.stateSchema;\n } else {\n continue;\n }\n\n // Create a modified schema where private properties are optional\n const modifiedSchema = interopZodObjectMakeFieldsOptional(\n zodSchema,\n (key) => key.startsWith(\"_\")\n );\n\n // Use safeParse with the modified schema\n const parseResult = await interopSafeParseAsync(modifiedSchema, state);\n if (parseResult.success) {\n Object.assign(middlewareStates, parseResult.data);\n continue;\n }\n\n /**\n * If safeParse fails, there are required public fields missing.\n * Note: Zod v3 uses message \"Required\", Zod v4 uses \"Invalid input: expected X, received undefined\"\n */\n const requiredFields = parseResult.error.issues\n .filter((issue) => issue.code === \"invalid_type\")\n .map((issue) => ` - ${issue.path.join(\".\")}: Required`)\n .join(\"\\n\");\n\n throw new Error(\n `Middleware \"${middleware.name}\" has required state fields that must be initialized:\\n` +\n `${requiredFields}\\n\\n` +\n `To fix this, either:\\n` +\n `1. Provide default values in your middleware's state schema using .default():\\n` +\n ` stateSchema: z.object({\\n` +\n ` myField: z.string().default(\"default value\")\\n` +\n ` })\\n\\n` +\n `2. Or make the fields optional using .optional():\\n` +\n ` stateSchema: z.object({\\n` +\n ` myField: z.string().optional()\\n` +\n ` })\\n\\n` +\n `3. Or ensure you pass these values when invoking the agent:\\n` +\n ` agent.invoke({\\n` +\n ` messages: [...],\\n` +\n ` ${parseResult.error.issues[0]?.path.join(\".\")}: \"value\"\\n` +\n ` })`\n );\n }\n\n return middlewareStates;\n}\n\n/**\n * Users can define private and public state for a middleware. Private state properties start with an underscore.\n * This function will return the private state properties from the state schema, making all of them optional.\n * @param stateSchema - The middleware state schema\n * @returns A new schema containing only the private properties (underscore-prefixed), all made optional\n */\nexport function derivePrivateState(\n stateSchema?: StateDefinitionInit\n): InteropZodObject {\n const builtInStateSchema = {\n messages: z.custom<BaseMessage[]>(() => []),\n // Include optional structuredResponse so after_agent hooks can access/modify it\n structuredResponse: z.any().optional(),\n };\n\n if (!stateSchema) {\n return z.object(builtInStateSchema);\n }\n\n // Extract shape from either StateSchema or Zod object\n let shape: Record<string, any>;\n if (StateSchema.isInstance(stateSchema)) {\n // For StateSchema, extract Zod schemas from fields\n shape = {};\n for (const [key, field] of Object.entries(stateSchema.fields)) {\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n shape[key] = field.inputSchema || field.valueSchema;\n } else {\n shape[key] = field;\n }\n }\n } else if (isInteropZodObject(stateSchema)) {\n shape = getInteropZodObjectShape(stateSchema);\n } else {\n return z.object(builtInStateSchema);\n }\n\n const privateShape: Record<string, any> = { ...builtInStateSchema };\n\n // Filter properties that start with underscore and make them optional\n for (const [key, value] of Object.entries(shape)) {\n if (key.startsWith(\"_\")) {\n // Make the private property optional\n privateShape[key] = value.optional();\n } else {\n privateShape[key] = value;\n }\n }\n\n // Return a new schema with only private properties (all optional)\n return z.object(privateShape);\n}\n\n/**\n * Converts any supported schema type (ZodObject, StateSchema, AnnotationRoot) to a partial Zod object.\n * This is useful for parsing state loosely where all fields are optional.\n *\n * @param schema - The schema to convert (InteropZodObject, StateSchema, or AnnotationRoot)\n * @returns A partial Zod object schema where all fields are optional\n */\nexport function toPartialZodObject(\n schema: StateDefinitionInit\n): InteropZodObject {\n // Handle ZodObject directly\n if (isInteropZodObject(schema)) {\n return interopZodObjectPartial(schema);\n }\n\n // Handle StateSchema: convert fields to Zod shape, then make partial\n if (StateSchema.isInstance(schema)) {\n const partialShape: Record<string, any> = {};\n for (const [key, field] of Object.entries(schema.fields)) {\n let fieldSchema: unknown;\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n fieldSchema = field.inputSchema || field.valueSchema;\n } else {\n fieldSchema = field;\n }\n // Only call .optional() on Zod v4 schemas, otherwise use z.any()\n partialShape[key] = isZodSchemaV4(fieldSchema)\n ? (fieldSchema as any).optional()\n : z.any().optional();\n }\n return z.object(partialShape);\n }\n\n // Fallback: return empty object schema\n return z.object({});\n}\n\n/**\n * Parse `jumpTo` target from user facing labels to a LangGraph node names\n */\nexport function parseJumpToTarget(target: string): JumpTo;\nexport function parseJumpToTarget(target?: string): JumpTo | undefined {\n if (!target) {\n return undefined;\n }\n\n /**\n * if target is already a valid jump target, return it\n */\n if ([\"model_request\", \"tools\", END].includes(target)) {\n return target as JumpTo;\n }\n\n if (target === \"model\") {\n return \"model_request\";\n }\n if (target === \"tools\") {\n return \"tools\";\n }\n if (target === \"end\") {\n return END;\n }\n\n throw new Error(\n `Invalid jump target: ${target}, must be \"model\", \"tools\" or \"end\".`\n );\n}\n\n/**\n * TypeScript currently doesn't support types for `AbortSignal.any`\n * @see https://github.com/microsoft/TypeScript/issues/60695\n */\ndeclare const AbortSignal: {\n any(signals: AbortSignal[]): AbortSignal;\n};\n\n/**\n * `config` always contains a signal from LangGraphs Pregel class.\n * To ensure we acknowledge the abort signal from the user, we merge it\n * with the signal from the ToolNode.\n *\n * @param signals - The signals to merge.\n * @returns The merged signal.\n */\nexport function mergeAbortSignals(\n ...signals: (AbortSignal | undefined)[]\n): AbortSignal {\n return AbortSignal.any(\n signals.filter(\n (maybeSignal): maybeSignal is AbortSignal =>\n maybeSignal !== null &&\n maybeSignal !== undefined &&\n typeof maybeSignal === \"object\" &&\n \"aborted\" in maybeSignal &&\n typeof maybeSignal.aborted === \"boolean\"\n )\n );\n}\n"],"mappings":";;;;;;;;;;;;AAyBA,eAAsB,2BACpB,gBACA,OAC8B;CAC9B,MAAM,mBAAwC,EAAE;AAEhD,MAAK,MAAM,cAAc,gBAAgB;;;;AAIvC,MAAI,CAAC,WAAW,YACd;EAIF,IAAI;AACJ,MAAIA,qBAAAA,YAAY,WAAW,WAAW,YAAY,EAAE;GAClD,MAAM,WAAgC,EAAE;AACxC,QAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAChC,WAAW,YAAY,OACxB,CACC,KAAIC,qBAAAA,aAAa,WAAW,MAAM,CAEhC,UAAS,OAAO,MAAM,eAAe,MAAM;OAE3C,UAAS,OAAO;AAGpB,eAAYC,OAAAA,EAAE,OAAO,SAAS;iEACF,WAAW,YAAY,CACnD,aAAY,WAAW;MAEvB;EAUF,MAAM,cAAc,OAAA,GAAA,4BAAA,wBAAA,GAAA,4BAAA,oCALlB,YACC,QAAQ,IAAI,WAAW,IAAI,CAC7B,EAG+D,MAAM;AACtE,MAAI,YAAY,SAAS;AACvB,UAAO,OAAO,kBAAkB,YAAY,KAAK;AACjD;;;;;;EAOF,MAAM,iBAAiB,YAAY,MAAM,OACtC,QAAQ,UAAU,MAAM,SAAS,eAAe,CAChD,KAAK,UAAU,OAAO,MAAM,KAAK,KAAK,IAAI,CAAC,YAAY,CACvD,KAAK,KAAK;AAEb,QAAM,IAAI,MACR,eAAe,WAAW,KAAK,yDAC1B,eAAe,4aAaV,YAAY,MAAM,OAAO,IAAI,KAAK,KAAK,IAAI,CAAC,kBAEvD;;AAGH,QAAO;;;;;;;;AAST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,OAAO,aAAa;;AAI/B,QAAOA,OAAAA,EAAE,OAAO,EAAE,CAAC;;AAOrB,SAAgB,kBAAkB,QAAqC;AACrE,KAAI,CAAC,OACH;;;;AAMF,KAAI;EAAC;EAAiB;EAASC,qBAAAA;EAAI,CAAC,SAAS,OAAO,CAClD,QAAO;AAGT,KAAI,WAAW,QACb,QAAO;AAET,KAAI,WAAW,QACb,QAAO;AAET,KAAI,WAAW,MACb,QAAOA,qBAAAA;AAGT,OAAM,IAAI,MACR,wBAAwB,OAAO,sCAChC;;;;;;;;;;AAmBH,SAAgB,kBACd,GAAG,SACU;AACb,QAAO,YAAY,IACjB,QAAQ,QACL,gBACC,gBAAgB,QAChB,gBAAgB,KAAA,KAChB,OAAO,gBAAgB,YACvB,aAAa,eACb,OAAO,YAAY,YAAY,UAClC,CACF"}
|
|
@@ -16,13 +16,14 @@ async function initializeMiddlewareStates(middlewareList, state) {
|
|
|
16
16
|
* skip middleware if it doesn't have a state schema
|
|
17
17
|
*/
|
|
18
18
|
if (!middleware.stateSchema) continue;
|
|
19
|
-
let zodSchema
|
|
19
|
+
let zodSchema;
|
|
20
20
|
if (StateSchema.isInstance(middleware.stateSchema)) {
|
|
21
21
|
const zodShape = {};
|
|
22
22
|
for (const [key, field] of Object.entries(middleware.stateSchema.fields)) if (ReducedValue.isInstance(field)) zodShape[key] = field.inputSchema || field.valueSchema;
|
|
23
23
|
else zodShape[key] = field;
|
|
24
24
|
zodSchema = z.object(zodShape);
|
|
25
|
-
}
|
|
25
|
+
} else if (isInteropZodObject(middleware.stateSchema)) zodSchema = middleware.stateSchema;
|
|
26
|
+
else continue;
|
|
26
27
|
const parseResult = await interopSafeParseAsync(interopZodObjectMakeFieldsOptional(zodSchema, (key) => key.startsWith("_")), state);
|
|
27
28
|
if (parseResult.success) {
|
|
28
29
|
Object.assign(middlewareStates, parseResult.data);
|
|
@@ -54,7 +55,8 @@ function derivePrivateState(stateSchema) {
|
|
|
54
55
|
shape = {};
|
|
55
56
|
for (const [key, field] of Object.entries(stateSchema.fields)) if (ReducedValue.isInstance(field)) shape[key] = field.inputSchema || field.valueSchema;
|
|
56
57
|
else shape[key] = field;
|
|
57
|
-
} else shape = getInteropZodObjectShape(stateSchema);
|
|
58
|
+
} else if (isInteropZodObject(stateSchema)) shape = getInteropZodObjectShape(stateSchema);
|
|
59
|
+
else return z.object(builtInStateSchema);
|
|
58
60
|
const privateShape = { ...builtInStateSchema };
|
|
59
61
|
for (const [key, value] of Object.entries(shape)) if (key.startsWith("_")) privateShape[key] = value.optional();
|
|
60
62
|
else privateShape[key] = value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","names":[],"sources":["../../../src/agents/nodes/utils.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v4\";\nimport { type BaseMessage } from \"@langchain/core/messages\";\nimport {\n getInteropZodObjectShape,\n interopSafeParseAsync,\n interopZodObjectMakeFieldsOptional,\n interopZodObjectPartial,\n isInteropZodObject,\n isZodSchemaV4,\n type InteropZodObject,\n} from \"@langchain/core/utils/types\";\nimport type { StateDefinitionInit } from \"@langchain/langgraph\";\nimport { END, StateSchema, ReducedValue } from \"@langchain/langgraph\";\n\nimport type { JumpTo } from \"../types.js\";\nimport type { AgentMiddleware } from \"../middleware/types.js\";\n\n/**\n * Helper function to initialize middleware state defaults.\n * This is used to ensure all middleware state properties are initialized.\n *\n * Private properties (starting with _) are automatically made optional since\n * users cannot provide them when invoking the agent.\n */\nexport async function initializeMiddlewareStates(\n middlewareList: readonly AgentMiddleware[],\n state: unknown\n): Promise<Record<string, any>> {\n const middlewareStates: Record<string, any> = {};\n\n for (const middleware of middlewareList) {\n /**\n * skip middleware if it doesn't have a state schema\n */\n if (!middleware.stateSchema) {\n continue;\n }\n\n // Convert StateSchema to Zod object if needed\n let zodSchema = middleware.stateSchema;\n if (StateSchema.isInstance(middleware.stateSchema)) {\n const zodShape: Record<string, any> = {};\n for (const [key, field] of Object.entries(\n middleware.stateSchema.fields\n )) {\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n zodShape[key] = field.inputSchema || field.valueSchema;\n } else {\n zodShape[key] = field;\n }\n }\n zodSchema = z.object(zodShape);\n }\n\n // Create a modified schema where private properties are optional\n const modifiedSchema = interopZodObjectMakeFieldsOptional(\n zodSchema,\n (key) => key.startsWith(\"_\")\n );\n\n // Use safeParse with the modified schema\n const parseResult = await interopSafeParseAsync(modifiedSchema, state);\n if (parseResult.success) {\n Object.assign(middlewareStates, parseResult.data);\n continue;\n }\n\n /**\n * If safeParse fails, there are required public fields missing.\n * Note: Zod v3 uses message \"Required\", Zod v4 uses \"Invalid input: expected X, received undefined\"\n */\n const requiredFields = parseResult.error.issues\n .filter((issue) => issue.code === \"invalid_type\")\n .map((issue) => ` - ${issue.path.join(\".\")}: Required`)\n .join(\"\\n\");\n\n throw new Error(\n `Middleware \"${middleware.name}\" has required state fields that must be initialized:\\n` +\n `${requiredFields}\\n\\n` +\n `To fix this, either:\\n` +\n `1. Provide default values in your middleware's state schema using .default():\\n` +\n ` stateSchema: z.object({\\n` +\n ` myField: z.string().default(\"default value\")\\n` +\n ` })\\n\\n` +\n `2. Or make the fields optional using .optional():\\n` +\n ` stateSchema: z.object({\\n` +\n ` myField: z.string().optional()\\n` +\n ` })\\n\\n` +\n `3. Or ensure you pass these values when invoking the agent:\\n` +\n ` agent.invoke({\\n` +\n ` messages: [...],\\n` +\n ` ${parseResult.error.issues[0]?.path.join(\".\")}: \"value\"\\n` +\n ` })`\n );\n }\n\n return middlewareStates;\n}\n\n/**\n * Users can define private and public state for a middleware. Private state properties start with an underscore.\n * This function will return the private state properties from the state schema, making all of them optional.\n * @param stateSchema - The middleware state schema\n * @returns A new schema containing only the private properties (underscore-prefixed), all made optional\n */\nexport function derivePrivateState(\n stateSchema?: InteropZodObject | StateSchema<any>\n): InteropZodObject {\n const builtInStateSchema = {\n messages: z.custom<BaseMessage[]>(() => []),\n // Include optional structuredResponse so after_agent hooks can access/modify it\n structuredResponse: z.any().optional(),\n };\n\n if (!stateSchema) {\n return z.object(builtInStateSchema);\n }\n\n // Extract shape from either StateSchema or Zod object\n let shape: Record<string, any>;\n if (StateSchema.isInstance(stateSchema)) {\n // For StateSchema, extract Zod schemas from fields\n shape = {};\n for (const [key, field] of Object.entries(stateSchema.fields)) {\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n shape[key] = field.inputSchema || field.valueSchema;\n } else {\n shape[key] = field;\n }\n }\n } else {\n shape = getInteropZodObjectShape(stateSchema);\n }\n\n const privateShape: Record<string, any> = { ...builtInStateSchema };\n\n // Filter properties that start with underscore and make them optional\n for (const [key, value] of Object.entries(shape)) {\n if (key.startsWith(\"_\")) {\n // Make the private property optional\n privateShape[key] = value.optional();\n } else {\n privateShape[key] = value;\n }\n }\n\n // Return a new schema with only private properties (all optional)\n return z.object(privateShape);\n}\n\n/**\n * Converts any supported schema type (ZodObject, StateSchema, AnnotationRoot) to a partial Zod object.\n * This is useful for parsing state loosely where all fields are optional.\n *\n * @param schema - The schema to convert (InteropZodObject, StateSchema, or AnnotationRoot)\n * @returns A partial Zod object schema where all fields are optional\n */\nexport function toPartialZodObject(\n schema: StateDefinitionInit\n): InteropZodObject {\n // Handle ZodObject directly\n if (isInteropZodObject(schema)) {\n return interopZodObjectPartial(schema);\n }\n\n // Handle StateSchema: convert fields to Zod shape, then make partial\n if (StateSchema.isInstance(schema)) {\n const partialShape: Record<string, any> = {};\n for (const [key, field] of Object.entries(schema.fields)) {\n let fieldSchema: unknown;\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n fieldSchema = field.inputSchema || field.valueSchema;\n } else {\n fieldSchema = field;\n }\n // Only call .optional() on Zod v4 schemas, otherwise use z.any()\n partialShape[key] = isZodSchemaV4(fieldSchema)\n ? (fieldSchema as any).optional()\n : z.any().optional();\n }\n return z.object(partialShape);\n }\n\n // Fallback: return empty object schema\n return z.object({});\n}\n\n/**\n * Parse `jumpTo` target from user facing labels to a LangGraph node names\n */\nexport function parseJumpToTarget(target: string): JumpTo;\nexport function parseJumpToTarget(target?: string): JumpTo | undefined {\n if (!target) {\n return undefined;\n }\n\n /**\n * if target is already a valid jump target, return it\n */\n if ([\"model_request\", \"tools\", END].includes(target)) {\n return target as JumpTo;\n }\n\n if (target === \"model\") {\n return \"model_request\";\n }\n if (target === \"tools\") {\n return \"tools\";\n }\n if (target === \"end\") {\n return END;\n }\n\n throw new Error(\n `Invalid jump target: ${target}, must be \"model\", \"tools\" or \"end\".`\n );\n}\n\n/**\n * TypeScript currently doesn't support types for `AbortSignal.any`\n * @see https://github.com/microsoft/TypeScript/issues/60695\n */\ndeclare const AbortSignal: {\n any(signals: AbortSignal[]): AbortSignal;\n};\n\n/**\n * `config` always contains a signal from LangGraphs Pregel class.\n * To ensure we acknowledge the abort signal from the user, we merge it\n * with the signal from the ToolNode.\n *\n * @param signals - The signals to merge.\n * @returns The merged signal.\n */\nexport function mergeAbortSignals(\n ...signals: (AbortSignal | undefined)[]\n): AbortSignal {\n return AbortSignal.any(\n signals.filter(\n (maybeSignal): maybeSignal is AbortSignal =>\n maybeSignal !== null &&\n maybeSignal !== undefined &&\n typeof maybeSignal === \"object\" &&\n \"aborted\" in maybeSignal &&\n typeof maybeSignal.aborted === \"boolean\"\n )\n );\n}\n"],"mappings":";;;;;;;;;;;AAyBA,eAAsB,2BACpB,gBACA,OAC8B;CAC9B,MAAM,mBAAwC,EAAE;AAEhD,MAAK,MAAM,cAAc,gBAAgB;;;;AAIvC,MAAI,CAAC,WAAW,YACd;EAIF,IAAI,YAAY,WAAW;AAC3B,MAAI,YAAY,WAAW,WAAW,YAAY,EAAE;GAClD,MAAM,WAAgC,EAAE;AACxC,QAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAChC,WAAW,YAAY,OACxB,CACC,KAAI,aAAa,WAAW,MAAM,CAEhC,UAA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AAO,wBAAwB,OAAO;AAIxC,KAAI,YAAY,WAAW,OAAO,EAAE;EAClC,MAAM,eAAoC,EAAE;AAC5C,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,OAAO,EAAE;GACxD,IAAI;AACJ,OAAI,aAAa,WAAW,MAAM,CAEhC,eAAc,MAAM,eAAe,MAAM;OAEzC,eAAc;AAGhB,gBAAa,OAAO,cAAc,YAAY,GACzC,YAAoB,UAAU,GAC/B,EAAE,KAAK,CAAC,UAAU;;AAExB,SAAO,EAAE,OAAO,aAAa;;AAI/B,QAAO,EAAE,OAAO,EAAE,CAAC;;AAOrB,SAAgB,kBAAkB,QAAqC;AACrE,KAAI,CAAC,OACH;;;;AAMF,KAAI;EAAC;EAAiB;EAAS;EAAI,CAAC,SAAS,OAAO,CAClD,QAAO;AAGT,KAAI,WAAW,QACb,QAAO;AAET,KAAI,WAAW,QACb,QAAO;AAET,KAAI,WAAW,MACb,QAAO;AAGT,OAAM,IAAI,MACR,wBAAwB,OAAO,sCAChC;;;;;;;;;;AAmBH,SAAgB,kBACd,GAAG,SACU;AACb,QAAO,YAAY,IACjB,QAAQ,QACL,gBACC,gBAAgB,QAChB,gBAAgB,KAAA,KAChB,OAAO,gBAAgB,YACvB,aAAa,eACb,OAAO,YAAY,YAAY,UAClC,CACF"}
|
|
1
|
+
{"version":3,"file":"utils.js","names":[],"sources":["../../../src/agents/nodes/utils.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport { z } from \"zod/v4\";\nimport { type BaseMessage } from \"@langchain/core/messages\";\nimport {\n getInteropZodObjectShape,\n interopSafeParseAsync,\n interopZodObjectMakeFieldsOptional,\n interopZodObjectPartial,\n isInteropZodObject,\n isZodSchemaV4,\n type InteropZodObject,\n} from \"@langchain/core/utils/types\";\nimport type { StateDefinitionInit } from \"@langchain/langgraph\";\nimport { END, StateSchema, ReducedValue } from \"@langchain/langgraph\";\n\nimport type { JumpTo } from \"../types.js\";\nimport type { AnyAgentMiddleware } from \"../middleware/types.js\";\n\n/**\n * Helper function to initialize middleware state defaults.\n * This is used to ensure all middleware state properties are initialized.\n *\n * Private properties (starting with _) are automatically made optional since\n * users cannot provide them when invoking the agent.\n */\nexport async function initializeMiddlewareStates(\n middlewareList: readonly AnyAgentMiddleware[],\n state: unknown\n): Promise<Record<string, any>> {\n const middlewareStates: Record<string, any> = {};\n\n for (const middleware of middlewareList) {\n /**\n * skip middleware if it doesn't have a state schema\n */\n if (!middleware.stateSchema) {\n continue;\n }\n\n // Convert StateSchema to Zod object if needed\n let zodSchema: InteropZodObject;\n if (StateSchema.isInstance(middleware.stateSchema)) {\n const zodShape: Record<string, any> = {};\n for (const [key, field] of Object.entries(\n middleware.stateSchema.fields\n )) {\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n zodShape[key] = field.inputSchema || field.valueSchema;\n } else {\n zodShape[key] = field;\n }\n }\n zodSchema = z.object(zodShape);\n } else if (isInteropZodObject(middleware.stateSchema)) {\n zodSchema = middleware.stateSchema;\n } else {\n continue;\n }\n\n // Create a modified schema where private properties are optional\n const modifiedSchema = interopZodObjectMakeFieldsOptional(\n zodSchema,\n (key) => key.startsWith(\"_\")\n );\n\n // Use safeParse with the modified schema\n const parseResult = await interopSafeParseAsync(modifiedSchema, state);\n if (parseResult.success) {\n Object.assign(middlewareStates, parseResult.data);\n continue;\n }\n\n /**\n * If safeParse fails, there are required public fields missing.\n * Note: Zod v3 uses message \"Required\", Zod v4 uses \"Invalid input: expected X, received undefined\"\n */\n const requiredFields = parseResult.error.issues\n .filter((issue) => issue.code === \"invalid_type\")\n .map((issue) => ` - ${issue.path.join(\".\")}: Required`)\n .join(\"\\n\");\n\n throw new Error(\n `Middleware \"${middleware.name}\" has required state fields that must be initialized:\\n` +\n `${requiredFields}\\n\\n` +\n `To fix this, either:\\n` +\n `1. Provide default values in your middleware's state schema using .default():\\n` +\n ` stateSchema: z.object({\\n` +\n ` myField: z.string().default(\"default value\")\\n` +\n ` })\\n\\n` +\n `2. Or make the fields optional using .optional():\\n` +\n ` stateSchema: z.object({\\n` +\n ` myField: z.string().optional()\\n` +\n ` })\\n\\n` +\n `3. Or ensure you pass these values when invoking the agent:\\n` +\n ` agent.invoke({\\n` +\n ` messages: [...],\\n` +\n ` ${parseResult.error.issues[0]?.path.join(\".\")}: \"value\"\\n` +\n ` })`\n );\n }\n\n return middlewareStates;\n}\n\n/**\n * Users can define private and public state for a middleware. Private state properties start with an underscore.\n * This function will return the private state properties from the state schema, making all of them optional.\n * @param stateSchema - The middleware state schema\n * @returns A new schema containing only the private properties (underscore-prefixed), all made optional\n */\nexport function derivePrivateState(\n stateSchema?: StateDefinitionInit\n): InteropZodObject {\n const builtInStateSchema = {\n messages: z.custom<BaseMessage[]>(() => []),\n // Include optional structuredResponse so after_agent hooks can access/modify it\n structuredResponse: z.any().optional(),\n };\n\n if (!stateSchema) {\n return z.object(builtInStateSchema);\n }\n\n // Extract shape from either StateSchema or Zod object\n let shape: Record<string, any>;\n if (StateSchema.isInstance(stateSchema)) {\n // For StateSchema, extract Zod schemas from fields\n shape = {};\n for (const [key, field] of Object.entries(stateSchema.fields)) {\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n shape[key] = field.inputSchema || field.valueSchema;\n } else {\n shape[key] = field;\n }\n }\n } else if (isInteropZodObject(stateSchema)) {\n shape = getInteropZodObjectShape(stateSchema);\n } else {\n return z.object(builtInStateSchema);\n }\n\n const privateShape: Record<string, any> = { ...builtInStateSchema };\n\n // Filter properties that start with underscore and make them optional\n for (const [key, value] of Object.entries(shape)) {\n if (key.startsWith(\"_\")) {\n // Make the private property optional\n privateShape[key] = value.optional();\n } else {\n privateShape[key] = value;\n }\n }\n\n // Return a new schema with only private properties (all optional)\n return z.object(privateShape);\n}\n\n/**\n * Converts any supported schema type (ZodObject, StateSchema, AnnotationRoot) to a partial Zod object.\n * This is useful for parsing state loosely where all fields are optional.\n *\n * @param schema - The schema to convert (InteropZodObject, StateSchema, or AnnotationRoot)\n * @returns A partial Zod object schema where all fields are optional\n */\nexport function toPartialZodObject(\n schema: StateDefinitionInit\n): InteropZodObject {\n // Handle ZodObject directly\n if (isInteropZodObject(schema)) {\n return interopZodObjectPartial(schema);\n }\n\n // Handle StateSchema: convert fields to Zod shape, then make partial\n if (StateSchema.isInstance(schema)) {\n const partialShape: Record<string, any> = {};\n for (const [key, field] of Object.entries(schema.fields)) {\n let fieldSchema: unknown;\n if (ReducedValue.isInstance(field)) {\n // For ReducedValue, use inputSchema if available, otherwise valueSchema\n fieldSchema = field.inputSchema || field.valueSchema;\n } else {\n fieldSchema = field;\n }\n // Only call .optional() on Zod v4 schemas, otherwise use z.any()\n partialShape[key] = isZodSchemaV4(fieldSchema)\n ? (fieldSchema as any).optional()\n : z.any().optional();\n }\n return z.object(partialShape);\n }\n\n // Fallback: return empty object schema\n return z.object({});\n}\n\n/**\n * Parse `jumpTo` target from user facing labels to a LangGraph node names\n */\nexport function parseJumpToTarget(target: string): JumpTo;\nexport function parseJumpToTarget(target?: string): JumpTo | undefined {\n if (!target) {\n return undefined;\n }\n\n /**\n * if target is already a valid jump target, return it\n */\n if ([\"model_request\", \"tools\", END].includes(target)) {\n return target as JumpTo;\n }\n\n if (target === \"model\") {\n return \"model_request\";\n }\n if (target === \"tools\") {\n return \"tools\";\n }\n if (target === \"end\") {\n return END;\n }\n\n throw new Error(\n `Invalid jump target: ${target}, must be \"model\", \"tools\" or \"end\".`\n );\n}\n\n/**\n * TypeScript currently doesn't support types for `AbortSignal.any`\n * @see https://github.com/microsoft/TypeScript/issues/60695\n */\ndeclare const AbortSignal: {\n any(signals: AbortSignal[]): AbortSignal;\n};\n\n/**\n * `config` always contains a signal from LangGraphs Pregel class.\n * To ensure we acknowledge the abort signal from the user, we merge it\n * with the signal from the ToolNode.\n *\n * @param signals - The signals to merge.\n * @returns The merged signal.\n */\nexport function mergeAbortSignals(\n ...signals: (AbortSignal | undefined)[]\n): AbortSignal {\n return AbortSignal.any(\n signals.filter(\n (maybeSignal): maybeSignal is AbortSignal =>\n maybeSignal !== null &&\n maybeSignal !== undefined &&\n typeof maybeSignal === \"object\" &&\n \"aborted\" in maybeSignal &&\n typeof maybeSignal.aborted === \"boolean\"\n )\n );\n}\n"],"mappings":";;;;;;;;;;;AAyBA,eAAsB,2BACpB,gBACA,OAC8B;CAC9B,MAAM,mBAAwC,EAAE;AAEhD,MAAK,MAAM,cAAc,gBAAgB;;;;AAIvC,MAAI,CAAC,WAAW,YACd;EAIF,IAAI;AACJ,MAAI,YAAY,WAAW,WAAW,YAAY,EAAE;GAClD,MAAM,WAAgC,EAAE;AACxC,QAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAChC,WAAW,YAAY,OACxB,CACC,KAAI,aAAa,WAAW,MAAM,CAEhC,UAAS,OAAO,MAAM,eAAe,MAAM;OAE3C,UAAS,OAAO;AAGpB,eAAY,EAAE,OAAO,SAAS;aACrB,mBAAmB,WAAW,YAAY,CACnD,aAAY,WAAW;MAEvB;EAUF,MAAM,cAAc,MAAM,sBANH,mCACrB,YACC,QAAQ,IAAI,WAAW,IAAI,CAC7B,EAG+D,MAAM;AACtE,MAAI,YAAY,SAAS;AACvB,UAAO,OAAO,kBAAkB,YAAY,KAAK;AACjD;;;;;;EAOF,MAAM,iBAAiB,YAAY,MAAM,OACtC,QAAQ,UAAU,MAAM,SAAS,eAAe,CAChD,KAAK,UAAU,OAAO,MAAM,KAAK,KAAK,IAAI,CAAC,YAAY,CACvD,KAAK,KAAK;AAEb,QAAM,IAAI,MACR,eAAe,WAAW,KAAK,yDAC1B,eAAe,4aAaV,YAAY,MAAM,OAAO,IAAI,KAAK,KAAK,IAAI,CAAC,kBAEvD;;AAGH,QAAO;;;;;;;;AAST,SAAgB,mBACd,aACkB;CAClB,MAAM,qBAAqB;EACzB,UAAU,EAAE,aAA4B,EAAE,CAAC;EAE3C,oBAAoB,EAAE,KAAK,CAAC,U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AAO;AAGT,OAAM,IAAI,MACR,wBAAwB,OAAO,sCAChC;;;;;;;;;;AAmBH,SAAgB,kBACd,GAAG,SACU;AACb,QAAO,YAAY,IACjB,QAAQ,QACL,gBACC,gBAAgB,QAChB,gBAAgB,KAAA,KAChB,OAAO,gBAAgB,YACvB,aAAa,eACb,OAAO,YAAY,YAAY,UAClC,CACF"}
|
package/dist/agents/types.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AgentLanguageModelLike } from "./model.cjs";
|
|
2
2
|
import { ResponseFormatInput, ResponseFormatUndefined } from "./responses.cjs";
|
|
3
3
|
import { JumpToTarget } from "./constants.cjs";
|
|
4
|
-
import { AgentMiddleware, AnyAnnotationRoot, InferMiddlewareContexts, InferMiddlewareStates, InferSchemaInput, InferSchemaValue } from "./middleware/types.cjs";
|
|
4
|
+
import { AgentMiddleware, AnyAgentMiddleware, AnyAnnotationRoot, InferMiddlewareContexts, InferMiddlewareStates, InferSchemaInput, InferSchemaValue } from "./middleware/types.cjs";
|
|
5
5
|
import { BaseMessage, MessageStructure, MessageToolDefinition, SystemMessage } from "@langchain/core/messages";
|
|
6
6
|
import { ClientTool, DynamicStructuredTool, ServerTool, StructuredToolInterface } from "@langchain/core/tools";
|
|
7
7
|
import { InteropZodObject, InteropZodType } from "@langchain/core/utils/types";
|
|
@@ -34,10 +34,10 @@ import { Messages } from "@langchain/langgraph/";
|
|
|
34
34
|
* @typeParam TTools - The combined tools type from both `createAgent` tools parameter
|
|
35
35
|
* and middleware tools. This is a readonly array of `ClientTool | ServerTool`.
|
|
36
36
|
*
|
|
37
|
-
* @typeParam TStreamTransformers - The tuple of
|
|
38
|
-
* factories
|
|
39
|
-
* `run.extensions` on the stream returned
|
|
40
|
-
* `streamEvents(..., { version: "v3" })`.
|
|
37
|
+
* @typeParam TStreamTransformers - The combined tuple of stream transformer
|
|
38
|
+
* factories from `createAgent({ streamTransformers })` and middleware
|
|
39
|
+
* `streamTransformers`. Used to type `run.extensions` on the stream returned
|
|
40
|
+
* from `streamEvents(..., { version: "v3" })`.
|
|
41
41
|
*
|
|
42
42
|
* @example
|
|
43
43
|
* ```typescript
|
|
@@ -55,7 +55,7 @@ import { Messages } from "@langchain/langgraph/";
|
|
|
55
55
|
* const agent: ReactAgent<MyAgentTypes> = createAgent({ ... });
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
|
-
interface AgentTypeConfig<TResponse extends Record<string, any> | ResponseFormatUndefined = Record<string, any> | ResponseFormatUndefined, TState extends StateDefinitionInit | undefined = StateDefinitionInit | undefined, TContext extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot | InteropZodObject, TMiddleware extends readonly
|
|
58
|
+
interface AgentTypeConfig<TResponse extends Record<string, any> | ResponseFormatUndefined = Record<string, any> | ResponseFormatUndefined, TState extends StateDefinitionInit | undefined = StateDefinitionInit | undefined, TContext extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot | InteropZodObject, TMiddleware extends readonly AnyAgentMiddleware[] = readonly AnyAgentMiddleware[], TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[], TStreamTransformers extends ReadonlyArray<() => StreamTransformer<any>> = ReadonlyArray<() => StreamTransformer<any>>> {
|
|
59
59
|
/** The structured response type when using `responseFormat` */
|
|
60
60
|
Response: TResponse;
|
|
61
61
|
/** The custom state schema type */
|
|
@@ -82,7 +82,7 @@ interface DefaultAgentTypeConfig extends AgentTypeConfig {
|
|
|
82
82
|
Response: Record<string, any>;
|
|
83
83
|
State: undefined;
|
|
84
84
|
Context: AnyAnnotationRoot;
|
|
85
|
-
Middleware: readonly
|
|
85
|
+
Middleware: readonly AnyAgentMiddleware[];
|
|
86
86
|
Tools: readonly (ClientTool | ServerTool)[];
|
|
87
87
|
StreamTransformers: readonly [];
|
|
88
88
|
}
|
|
@@ -90,16 +90,28 @@ interface DefaultAgentTypeConfig extends AgentTypeConfig {
|
|
|
90
90
|
* Helper type to infer tools from a single middleware instance.
|
|
91
91
|
* Extracts the TTools type parameter from AgentMiddleware.
|
|
92
92
|
*/
|
|
93
|
-
type InferMiddlewareTools<T extends AgentMiddleware> = T extends AgentMiddleware<any, any, any, infer TTools> ? TTools extends readonly (ClientTool | ServerTool)[] ? TTools : readonly [] : readonly [];
|
|
93
|
+
type InferMiddlewareTools<T extends AgentMiddleware> = T extends AgentMiddleware<any, any, any, infer TTools, any> ? TTools extends readonly (ClientTool | ServerTool)[] ? TTools : readonly [] : readonly [];
|
|
94
|
+
/**
|
|
95
|
+
* Helper type to infer stream transformers from a single middleware instance.
|
|
96
|
+
*/
|
|
97
|
+
type InferMiddlewareStreamTransformers<T extends AgentMiddleware> = T extends AgentMiddleware<any, any, any, any, infer TStreamTransformers> ? [TStreamTransformers] extends [readonly []] ? readonly [] : TStreamTransformers extends ReadonlyArray<() => StreamTransformer<any>> ? TStreamTransformers : readonly [] : readonly [];
|
|
94
98
|
/**
|
|
95
99
|
* Helper type to infer and merge tools from an array of middleware.
|
|
96
100
|
* Recursively extracts tools from each middleware and combines them into a single tuple.
|
|
97
101
|
*/
|
|
98
|
-
type InferMiddlewareToolsArray<T extends readonly
|
|
102
|
+
type InferMiddlewareToolsArray<T extends readonly AnyAgentMiddleware[]> = T extends readonly [] ? readonly [] : T extends readonly [infer First, ...infer Rest] ? First extends AgentMiddleware ? Rest extends readonly AnyAgentMiddleware[] ? readonly [...InferMiddlewareTools<First>, ...InferMiddlewareToolsArray<Rest>] : InferMiddlewareTools<First> : readonly [] : readonly [];
|
|
99
103
|
/**
|
|
100
104
|
* Helper type to combine agent tools with middleware tools into a single readonly array.
|
|
101
105
|
*/
|
|
102
|
-
type CombineTools<TAgentTools extends readonly (ClientTool | ServerTool)[], TMiddleware extends readonly
|
|
106
|
+
type CombineTools<TAgentTools extends readonly (ClientTool | ServerTool)[], TMiddleware extends readonly AnyAgentMiddleware[]> = readonly [...TAgentTools, ...InferMiddlewareToolsArray<TMiddleware>];
|
|
107
|
+
/**
|
|
108
|
+
* Helper type to infer and merge stream transformers from an array of middleware.
|
|
109
|
+
*/
|
|
110
|
+
type InferMiddlewareStreamTransformersArray<T extends readonly AnyAgentMiddleware[]> = T extends readonly [] ? readonly [] : T extends readonly [infer First, ...infer Rest] ? First extends AgentMiddleware ? Rest extends readonly AnyAgentMiddleware[] ? readonly [...InferMiddlewareStreamTransformers<First>, ...InferMiddlewareStreamTransformersArray<Rest>] : InferMiddlewareStreamTransformers<First> : readonly [] : readonly [];
|
|
111
|
+
/**
|
|
112
|
+
* Helper type to combine agent stream transformers with middleware stream transformers.
|
|
113
|
+
*/
|
|
114
|
+
type CombineStreamTransformers<TAgentStreamTransformers extends ReadonlyArray<() => StreamTransformer<any>>, TMiddleware extends readonly AnyAgentMiddleware[]> = readonly [...TAgentStreamTransformers, ...InferMiddlewareStreamTransformersArray<TMiddleware>];
|
|
103
115
|
/**
|
|
104
116
|
* Helper type to extract the tool name, input type, and output type from a tool.
|
|
105
117
|
* Converts a single tool to a MessageToolDefinition entry.
|
|
@@ -650,7 +662,7 @@ type CreateAgentParams<StructuredResponseType extends Record<string, any> = Reco
|
|
|
650
662
|
*
|
|
651
663
|
* @see {@link https://docs.langchain.com/oss/javascript/langchain/middleware | Middleware}
|
|
652
664
|
*/
|
|
653
|
-
middleware?: readonly
|
|
665
|
+
middleware?: readonly AnyAgentMiddleware[];
|
|
654
666
|
/**
|
|
655
667
|
* An optional name for the agent.
|
|
656
668
|
*/
|
|
@@ -734,5 +746,5 @@ type CreateAgentParams<StructuredResponseType extends Record<string, any> = Reco
|
|
|
734
746
|
type ExtractZodArrayTypes<T extends readonly InteropZodType<any>[]> = T extends readonly [InteropZodType<infer A>, ...infer Rest] ? Rest extends readonly InteropZodType<any>[] ? A | ExtractZodArrayTypes<Rest> : A : never;
|
|
735
747
|
type WithStateGraphNodes<K extends string, Graph> = Graph extends StateGraph<infer SD, infer S, infer U, infer N, infer I, infer O, infer C> ? StateGraph<SD, S, U, N | K, I, O, C> : never;
|
|
736
748
|
//#endregion
|
|
737
|
-
export { AgentTypeConfig, BuiltInState, CombineTools, CreateAgentParams, DefaultAgentTypeConfig, ExecutedToolCall, ExtractZodArrayTypes, InferAgentContext, InferAgentContextSchema, InferAgentMiddleware, InferAgentResponse, InferAgentState, InferAgentStateSchema, InferAgentStreamTransformers, InferAgentTools, InferAgentType, InferMiddlewareTools, InferMiddlewareToolsArray, Interrupt, JumpTo, N, ResolveAgentTypeConfig, ToolCall$1 as ToolCall, ToolResult, ToolsToMessageToolSet, UserInput, WithStateGraphNodes };
|
|
749
|
+
export { 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$1 as ToolCall, ToolResult, ToolsToMessageToolSet, UserInput, WithStateGraphNodes };
|
|
738
750
|
//# sourceMappingURL=types.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/agents/types.ts"],"mappings":";;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/agents/types.ts"],"mappings":";;;;;;;;;;;;;;AA2FA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAAiB,eAAA,mBACG,MAAA,gBAAsB,uBAAA,GACpC,MAAA,gBACA,uBAAA,iBACW,mBAAA,eACX,mBAAA,+BAEa,iBAAA,GAAoB,gBAAA,GACjC,iBAAA,GACA,gBAAA,+BACyB,kBAAA,cAClB,kBAAA,6BACc,UAAA,GAAa,UAAA,gBAClC,UAAA,GACA,UAAA,iCAEwB,aAAA,OAAoB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;;;;;;;;;;;;;;;;;;;;;;;;;EAhEjB,aAAA,GAAgB,aAAA;EAgEhB;;;;EA3DA,YAAA,GAAe,mBAAA;EA8Ef;;;;EAzEA,KAAA,GAAQ,SAAA;EAgJR;;;;;AAMF;;;;;;;;;;;;;;;;;;;;;;;;;;AAOA;;;;;;;;;;;;;;;;;;;;;;EAvGE,cAAA,GAAiB,kBAAA;EA4GT;;;;;;EApGR,UAAA,YAAsB,kBAAA;EAyGA;;;EApGtB,IAAA;EAoGmC;;;;EA9FnC,WAAA;;;;;;;EAQA,gBAAA;;;;EAKA,MAAA,GAAS,WAAA;;;;;;;;;;;;;;;;;;;;EAqBT,OAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAqCA,kBAAA,GAAqB,aAAA,OAAoB,iBAAA;AAAA;;;;KAM/B,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"}
|
package/dist/agents/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AgentLanguageModelLike } from "./model.js";
|
|
2
2
|
import { ResponseFormatInput, ResponseFormatUndefined } from "./responses.js";
|
|
3
3
|
import { JumpToTarget } from "./constants.js";
|
|
4
|
-
import { AgentMiddleware, AnyAnnotationRoot, InferMiddlewareContexts, InferMiddlewareStates, InferSchemaInput, InferSchemaValue } from "./middleware/types.js";
|
|
4
|
+
import { AgentMiddleware, AnyAgentMiddleware, AnyAnnotationRoot, InferMiddlewareContexts, InferMiddlewareStates, InferSchemaInput, InferSchemaValue } from "./middleware/types.js";
|
|
5
5
|
import { BaseMessage, MessageStructure, MessageToolDefinition, SystemMessage } from "@langchain/core/messages";
|
|
6
6
|
import { ClientTool, DynamicStructuredTool, ServerTool, StructuredToolInterface } from "@langchain/core/tools";
|
|
7
7
|
import { END, START, StateDefinitionInit, StateGraph, StreamTransformer } from "@langchain/langgraph";
|
|
@@ -34,10 +34,10 @@ import { Messages } from "@langchain/langgraph/";
|
|
|
34
34
|
* @typeParam TTools - The combined tools type from both `createAgent` tools parameter
|
|
35
35
|
* and middleware tools. This is a readonly array of `ClientTool | ServerTool`.
|
|
36
36
|
*
|
|
37
|
-
* @typeParam TStreamTransformers - The tuple of
|
|
38
|
-
* factories
|
|
39
|
-
* `run.extensions` on the stream returned
|
|
40
|
-
* `streamEvents(..., { version: "v3" })`.
|
|
37
|
+
* @typeParam TStreamTransformers - The combined tuple of stream transformer
|
|
38
|
+
* factories from `createAgent({ streamTransformers })` and middleware
|
|
39
|
+
* `streamTransformers`. Used to type `run.extensions` on the stream returned
|
|
40
|
+
* from `streamEvents(..., { version: "v3" })`.
|
|
41
41
|
*
|
|
42
42
|
* @example
|
|
43
43
|
* ```typescript
|
|
@@ -55,7 +55,7 @@ import { Messages } from "@langchain/langgraph/";
|
|
|
55
55
|
* const agent: ReactAgent<MyAgentTypes> = createAgent({ ... });
|
|
56
56
|
* ```
|
|
57
57
|
*/
|
|
58
|
-
interface AgentTypeConfig<TResponse extends Record<string, any> | ResponseFormatUndefined = Record<string, any> | ResponseFormatUndefined, TState extends StateDefinitionInit | undefined = StateDefinitionInit | undefined, TContext extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot | InteropZodObject, TMiddleware extends readonly
|
|
58
|
+
interface AgentTypeConfig<TResponse extends Record<string, any> | ResponseFormatUndefined = Record<string, any> | ResponseFormatUndefined, TState extends StateDefinitionInit | undefined = StateDefinitionInit | undefined, TContext extends AnyAnnotationRoot | InteropZodObject = AnyAnnotationRoot | InteropZodObject, TMiddleware extends readonly AnyAgentMiddleware[] = readonly AnyAgentMiddleware[], TTools extends readonly (ClientTool | ServerTool)[] = readonly (ClientTool | ServerTool)[], TStreamTransformers extends ReadonlyArray<() => StreamTransformer<any>> = ReadonlyArray<() => StreamTransformer<any>>> {
|
|
59
59
|
/** The structured response type when using `responseFormat` */
|
|
60
60
|
Response: TResponse;
|
|
61
61
|
/** The custom state schema type */
|
|
@@ -82,7 +82,7 @@ interface DefaultAgentTypeConfig extends AgentTypeConfig {
|
|
|
82
82
|
Response: Record<string, any>;
|
|
83
83
|
State: undefined;
|
|
84
84
|
Context: AnyAnnotationRoot;
|
|
85
|
-
Middleware: readonly
|
|
85
|
+
Middleware: readonly AnyAgentMiddleware[];
|
|
86
86
|
Tools: readonly (ClientTool | ServerTool)[];
|
|
87
87
|
StreamTransformers: readonly [];
|
|
88
88
|
}
|
|
@@ -90,16 +90,28 @@ interface DefaultAgentTypeConfig extends AgentTypeConfig {
|
|
|
90
90
|
* Helper type to infer tools from a single middleware instance.
|
|
91
91
|
* Extracts the TTools type parameter from AgentMiddleware.
|
|
92
92
|
*/
|
|
93
|
-
type InferMiddlewareTools<T extends AgentMiddleware> = T extends AgentMiddleware<any, any, any, infer TTools> ? TTools extends readonly (ClientTool | ServerTool)[] ? TTools : readonly [] : readonly [];
|
|
93
|
+
type InferMiddlewareTools<T extends AgentMiddleware> = T extends AgentMiddleware<any, any, any, infer TTools, any> ? TTools extends readonly (ClientTool | ServerTool)[] ? TTools : readonly [] : readonly [];
|
|
94
|
+
/**
|
|
95
|
+
* Helper type to infer stream transformers from a single middleware instance.
|
|
96
|
+
*/
|
|
97
|
+
type InferMiddlewareStreamTransformers<T extends AgentMiddleware> = T extends AgentMiddleware<any, any, any, any, infer TStreamTransformers> ? [TStreamTransformers] extends [readonly []] ? readonly [] : TStreamTransformers extends ReadonlyArray<() => StreamTransformer<any>> ? TStreamTransformers : readonly [] : readonly [];
|
|
94
98
|
/**
|
|
95
99
|
* Helper type to infer and merge tools from an array of middleware.
|
|
96
100
|
* Recursively extracts tools from each middleware and combines them into a single tuple.
|
|
97
101
|
*/
|
|
98
|
-
type InferMiddlewareToolsArray<T extends readonly
|
|
102
|
+
type InferMiddlewareToolsArray<T extends readonly AnyAgentMiddleware[]> = T extends readonly [] ? readonly [] : T extends readonly [infer First, ...infer Rest] ? First extends AgentMiddleware ? Rest extends readonly AnyAgentMiddleware[] ? readonly [...InferMiddlewareTools<First>, ...InferMiddlewareToolsArray<Rest>] : InferMiddlewareTools<First> : readonly [] : readonly [];
|
|
99
103
|
/**
|
|
100
104
|
* Helper type to combine agent tools with middleware tools into a single readonly array.
|
|
101
105
|
*/
|
|
102
|
-
type CombineTools<TAgentTools extends readonly (ClientTool | ServerTool)[], TMiddleware extends readonly
|
|
106
|
+
type CombineTools<TAgentTools extends readonly (ClientTool | ServerTool)[], TMiddleware extends readonly AnyAgentMiddleware[]> = readonly [...TAgentTools, ...InferMiddlewareToolsArray<TMiddleware>];
|
|
107
|
+
/**
|
|
108
|
+
* Helper type to infer and merge stream transformers from an array of middleware.
|
|
109
|
+
*/
|
|
110
|
+
type InferMiddlewareStreamTransformersArray<T extends readonly AnyAgentMiddleware[]> = T extends readonly [] ? readonly [] : T extends readonly [infer First, ...infer Rest] ? First extends AgentMiddleware ? Rest extends readonly AnyAgentMiddleware[] ? readonly [...InferMiddlewareStreamTransformers<First>, ...InferMiddlewareStreamTransformersArray<Rest>] : InferMiddlewareStreamTransformers<First> : readonly [] : readonly [];
|
|
111
|
+
/**
|
|
112
|
+
* Helper type to combine agent stream transformers with middleware stream transformers.
|
|
113
|
+
*/
|
|
114
|
+
type CombineStreamTransformers<TAgentStreamTransformers extends ReadonlyArray<() => StreamTransformer<any>>, TMiddleware extends readonly AnyAgentMiddleware[]> = readonly [...TAgentStreamTransformers, ...InferMiddlewareStreamTransformersArray<TMiddleware>];
|
|
103
115
|
/**
|
|
104
116
|
* Helper type to extract the tool name, input type, and output type from a tool.
|
|
105
117
|
* Converts a single tool to a MessageToolDefinition entry.
|
|
@@ -650,7 +662,7 @@ type CreateAgentParams<StructuredResponseType extends Record<string, any> = Reco
|
|
|
650
662
|
*
|
|
651
663
|
* @see {@link https://docs.langchain.com/oss/javascript/langchain/middleware | Middleware}
|
|
652
664
|
*/
|
|
653
|
-
middleware?: readonly
|
|
665
|
+
middleware?: readonly AnyAgentMiddleware[];
|
|
654
666
|
/**
|
|
655
667
|
* An optional name for the agent.
|
|
656
668
|
*/
|
|
@@ -734,5 +746,5 @@ type CreateAgentParams<StructuredResponseType extends Record<string, any> = Reco
|
|
|
734
746
|
type ExtractZodArrayTypes<T extends readonly InteropZodType<any>[]> = T extends readonly [InteropZodType<infer A>, ...infer Rest] ? Rest extends readonly InteropZodType<any>[] ? A | ExtractZodArrayTypes<Rest> : A : never;
|
|
735
747
|
type WithStateGraphNodes<K extends string, Graph> = Graph extends StateGraph<infer SD, infer S, infer U, infer N, infer I, infer O, infer C> ? StateGraph<SD, S, U, N | K, I, O, C> : never;
|
|
736
748
|
//#endregion
|
|
737
|
-
export { AgentTypeConfig, BuiltInState, CombineTools, CreateAgentParams, DefaultAgentTypeConfig, ExecutedToolCall, ExtractZodArrayTypes, InferAgentContext, InferAgentContextSchema, InferAgentMiddleware, InferAgentResponse, InferAgentState, InferAgentStateSchema, InferAgentStreamTransformers, InferAgentTools, InferAgentType, InferMiddlewareTools, InferMiddlewareToolsArray, Interrupt, JumpTo, N, ResolveAgentTypeConfig, ToolCall$1 as ToolCall, ToolResult, ToolsToMessageToolSet, UserInput, WithStateGraphNodes };
|
|
749
|
+
export { 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$1 as ToolCall, ToolResult, ToolsToMessageToolSet, UserInput, WithStateGraphNodes };
|
|
738
750
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../../src/agents/types.ts"],"mappings":";;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../../src/agents/types.ts"],"mappings":";;;;;;;;;;;;;;AA2FA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAAiB,eAAA,mBACG,MAAA,gBAAsB,uBAAA,GACpC,MAAA,gBACA,uBAAA,iBACW,mBAAA,eACX,mBAAA,+BAEa,iBAAA,GAAoB,gBAAA,GACjC,iBAAA,GACA,gBAAA,+BACyB,kBAAA,cAClB,kBAAA,6BACc,UAAA,GAAa,UAAA,gBAClC,UAAA,GACA,UAAA,iCAEwB,aAAA,OAAoB,iBAAA,SAC9C,aAAA,OAAoB,iBAAA;EAPtB;EAUA,QAAA,EAAU,SAAA;EATC;EAWX,KAAA,EAAO,MAAA;EAVkB;EAYzB,OAAA,EAAS,QAAA;EAXL;EAaJ,UAAA,EAAY,WAAA;EAVZ;EAYA,KAAA,EAAO,MAAA;EAZyC;;;;;;EAmBhD,kBAAA,EAAoB,mBAAA;AAAA;;;;;UAOL,sBAAA,SAA+B,eAAA;EAC9C,QAAA,EAAU,MAAA;EACV,KAAA;EACA,OAAA,EAAS,iBAAA;EACT,UAAA,WAAqB,kBAAA;EACrB,KAAA,YAAiB,UAAA,GAAa,UAAA;EAC9B,kBAAA;AAAA;;;;;KAOU,oBAAA,WAA+B,eAAA,IACzC,CAAA,SAAU,eAAA,qCACN,MAAA,mBAAyB,UAAA,GAAa,UAAA,MACpC,MAAA;;;;KAOI,iCAAA,WAA4C,eAAA,IACtD,CAAA,SAAU,eAAA,mDACL,mBAAA,wCAEC,mBAAA,SAA4B,aAAA,OAAoB,iBAAA,SAC9C,mBAAA;;;;;KAQE,yBAAA,o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