langchain
Version:
Typescript bindings for langchain
1 lines • 7.54 kB
Source Map (JSON)
{"version":3,"file":"annotation.cjs","names":["UntrackedValue","StateSchema","ReducedValue","getInteropZodObjectShape","isZodSchemaV4","schemaMetaRegistry","isInteropZodObject","MessagesValue"],"sources":["../../src/agents/annotation.ts"],"sourcesContent":["/* oxlint-disable @typescript-eslint/no-explicit-any */\nimport {\n StateSchema,\n MessagesValue,\n UntrackedValue,\n ReducedValue,\n type StateDefinitionInit,\n} from \"@langchain/langgraph\";\nimport { schemaMetaRegistry } from \"@langchain/langgraph/zod\";\n\nimport type { AnyAgentMiddleware } from \"./middleware/types.js\";\nimport {\n type InteropZodObject,\n isZodSchemaV4,\n getInteropZodObjectShape,\n isInteropZodObject,\n} from \"@langchain/core/utils/types\";\n\n/**\n * Type for jumpTo navigation targets\n */\ntype JumpToTarget = \"model_request\" | \"tools\" | \"end\" | undefined;\n\nexport function createAgentState<\n TStateSchema extends StateDefinitionInit | undefined = undefined,\n TMiddleware extends readonly AnyAgentMiddleware[] = [],\n>(\n hasStructuredResponse = true,\n stateSchema: TStateSchema,\n middlewareList: TMiddleware = [] as unknown as TMiddleware\n) {\n /**\n * Collect fields from state schemas\n */\n const stateFields: Record<string, any> = {\n // jumpTo is used for internal navigation control\n jumpTo: new UntrackedValue<JumpToTarget>(),\n };\n\n // Separate shapes for input/output without reducer metadata (to avoid channel conflicts)\n const inputFields: Record<string, any> = {};\n const outputFields: Record<string, any> = {};\n\n const applySchema = (schema: InteropZodObject | StateSchema<any>) => {\n // Handle StateSchema: extract from .fields\n if (StateSchema.isInstance(schema)) {\n for (const [key, field] of Object.entries(schema.fields)) {\n if (!(key in stateFields)) {\n // Add to stateFields to preserve ReducedValue/UntrackedValue behavior\n stateFields[key] = field;\n\n // Private state (underscore-prefixed) persists in the graph state\n // but is not exposed as input/output channels.\n if (key.startsWith(\"_\")) {\n continue;\n }\n\n // For ioFields, extract the appropriate schema from ReducedValue\n if (ReducedValue.isInstance(field)) {\n // For input, use inputSchema if available, otherwise use the value schema\n inputFields[key] = field.inputSchema || field.valueSchema;\n outputFields[key] = field.valueSchema;\n } else {\n // For non-ReducedValue fields, use the field as-is\n inputFields[key] = field;\n outputFields[key] = field;\n }\n }\n }\n return;\n }\n\n // Handle Zod v3/v4: extract shape using interop utilities\n const shape = getInteropZodObjectShape(schema);\n for (const [key, fieldSchema] of Object.entries(shape)) {\n const isPrivate = key.startsWith(\"_\");\n\n if (!(key in stateFields)) {\n // Check for reducer metadata (Zod v4 only supports schemaMetaRegistry)\n if (isZodSchemaV4(fieldSchema)) {\n const meta = schemaMetaRegistry.get(fieldSchema);\n if (meta?.reducer) {\n // Wrap with ReducedValue to preserve reducer behavior\n if (meta.reducer.schema) {\n stateFields[key] = new ReducedValue(fieldSchema as any, {\n inputSchema: meta.reducer.schema as any,\n reducer: meta.reducer.fn,\n });\n if (!isPrivate) {\n // For input, use the inputSchema\n inputFields[key] = meta.reducer.schema;\n outputFields[key] = fieldSchema;\n }\n } else {\n stateFields[key] = new ReducedValue(fieldSchema as any, {\n reducer: meta.reducer.fn,\n });\n if (!isPrivate) {\n // No inputSchema, use the value schema\n inputFields[key] = fieldSchema;\n outputFields[key] = fieldSchema;\n }\n }\n continue;\n }\n }\n\n // No reducer - use schema directly\n stateFields[key] = fieldSchema;\n\n // Private state (underscore-prefixed) persists in the graph state\n // but is not exposed as input/output channels.\n if (!isPrivate) {\n inputFields[key] = fieldSchema;\n outputFields[key] = fieldSchema;\n }\n }\n }\n };\n\n /**\n * Add state schema properties from user-provided schema.\n * Supports both StateSchema and Zod v3/v4 objects.\n */\n if (\n stateSchema &&\n (StateSchema.isInstance(stateSchema) || isInteropZodObject(stateSchema))\n ) {\n applySchema(stateSchema);\n }\n\n /**\n * Add state schema properties from middleware.\n * Supports both StateSchema and Zod v3/v4 objects.\n */\n for (const middleware of middlewareList) {\n if (\n middleware.stateSchema &&\n (StateSchema.isInstance(middleware.stateSchema) ||\n isInteropZodObject(middleware.stateSchema))\n ) {\n applySchema(middleware.stateSchema);\n }\n }\n\n // Only include structuredResponse when responseFormat is defined\n if (hasStructuredResponse) {\n outputFields.structuredResponse = new UntrackedValue<any>();\n }\n\n /**\n * Create StateSchema instances for state, input, and output.\n * Using MessagesValue provides the proper message reducer behavior.\n */\n return {\n state: new StateSchema({\n messages: MessagesValue,\n ...stateFields,\n }),\n input: new StateSchema({\n messages: MessagesValue,\n ...inputFields,\n }),\n output: new StateSchema({\n messages: MessagesValue,\n ...outputFields,\n }),\n };\n}\n"],"mappings":";;;;AAuBA,SAAgB,iBAId,wBAAwB,MACxB,aACA,iBAA8B,CAAC,GAC/B;;;;CAIA,MAAM,cAAmC,EAEvC,QAAQ,IAAIA,qBAAAA,eAA6B,EAC3C;CAGA,MAAM,cAAmC,CAAC;CAC1C,MAAM,eAAoC,CAAC;CAE3C,MAAM,eAAe,WAAgD;EAEnE,IAAIC,qBAAAA,YAAY,WAAW,MAAM,GAAG;GAClC,KAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,MAAM,GACrD,IAAI,EAAE,OAAO,cAAc;IAEzB,YAAY,OAAO;IAInB,IAAI,IAAI,WAAW,GAAG,GACpB;IAIF,IAAIC,qBAAAA,aAAa,WAAW,KAAK,GAAG;KAElC,YAAY,OAAO,MAAM,eAAe,MAAM;KAC9C,aAAa,OAAO,MAAM;IAC5B,OAAO;KAEL,YAAY,OAAO;KACnB,aAAa,OAAO;IACtB;GACF;GAEF;EACF;EAGA,MAAM,SAAA,GAAQC,4BAAAA,yBAAAA,CAAyB,MAAM;EAC7C,KAAK,MAAM,CAAC,KAAK,gBAAgB,OAAO,QAAQ,KAAK,GAAG;GACtD,MAAM,YAAY,IAAI,WAAW,GAAG;GAEpC,IAAI,EAAE,OAAO,cAAc;IAEzB,KAAA,GAAIC,4BAAAA,cAAAA,CAAc,WAAW,GAAG;KAC9B,MAAM,OAAOC,yBAAAA,mBAAmB,IAAI,WAAW;KAC/C,IAAI,MAAM,SAAS;MAEjB,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