langchain
Version:
Typescript bindings for langchain
1 lines • 7.39 kB
Source Map (JSON)
{"version":3,"file":"annotation.cjs","names":["UntrackedValue","StateSchema","ReducedValue","schemaMetaRegistry","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,EAAE,EAChC;;;;CAIA,MAAM,cAAmC,EAEvC,QAAQ,IAAIA,qBAAAA,gBAA8B,EAC3C;CAGD,MAAM,cAAmC,EAAE;CAC3C,MAAM,eAAoC,EAAE;CAE5C,MAAM,eAAe,WAAgD;AAEnE,MAAIC,qBAAAA,YAAY,WAAW,OAAO,EAAE;AAClC,QAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,OAAO,OAAO,CACtD,KAAI,EAAE,OAAO,cAAc;AAEzB,gBAAY,OAAO;AAInB,QAAI,IAAI,WAAW,IAAI,CACrB;AAIF,QAAIC,qBAAAA,aAAa,WAAW,MAAM,EAAE;AAElC,iBAAY,OAAO,MAAM,eAAe,MAAM;AAC9C,kBAAa,OAAO,MAAM;WACrB;AAEL,iBAAY,OAAO;AACnB,kBAAa,OAAO;;;AAI1B;;EAIF,MAAM,SAAA,GAAA,4BAAA,0BAAiC,OAAO;AAC9C,OAAK,MAAM,CAAC,KAAK,gBAAgB,OAAO,QAAQ,MAAM,EAAE;GACtD,MAAM,YAAY,IAAI,WAAW,IAAI;AAErC,OAAI,EAAE,OAAO,cAAc;AAEzB,SAAA,GAAA,4BAAA,eAAkB,YAAY,EAAE;KAC9B,MAAM,OAAOC,yBAAAA,mBAAmB,IAAI,YAAY;AAChD,SAAI,MAAM,SAAS;AAEjB,UAAI,KAAK,QAAQ,QAAQ;AACvB,mBAAY,OAAO,IAAID,qBAAAA,aAAa,aA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