UNPKG

langchain

Version:
74 lines (72 loc) 3.23 kB
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs'); const __langchain_langgraph = require_rolldown_runtime.__toESM(require("@langchain/langgraph")); const zod_v3 = require_rolldown_runtime.__toESM(require("zod/v3")); const __langchain_langgraph_zod = require_rolldown_runtime.__toESM(require("@langchain/langgraph/zod")); const __langchain_core_utils_types = require_rolldown_runtime.__toESM(require("@langchain/core/utils/types")); //#region src/agents/annotation.ts function createAgentAnnotationConditional(hasStructuredResponse = true, stateSchema, middlewareList = []) { /** * Create Zod schema object to preserve jsonSchemaExtra * metadata for LangGraph Studio using v3-compatible withLangGraph */ const schemaShape = { jumpTo: zod_v3.z.union([ zod_v3.z.literal("model_request"), zod_v3.z.literal("tools"), zod_v3.z.literal("end"), zod_v3.z.undefined() ]).optional() }; const ioSchemaShape = {}; const applySchema = (schema) => { const shape = (0, __langchain_core_utils_types.isZodSchemaV4)(schema) ? (0, __langchain_core_utils_types.getInteropZodObjectShape)(schema) : schema.shape; for (const [key, fieldSchema] of Object.entries(shape)) { /** * Skip private state properties */ if (key.startsWith("_")) continue; if (!(key in schemaShape)) /** * If the field schema is Zod v4, convert to v3-compatible z.any() * This allows the shape to be merged while preserving the key structure * Also transfer any registry metadata (reducers, defaults) to the new schema * using withLangGraph which properly registers the metadata */ if ((0, __langchain_core_utils_types.isZodSchemaV4)(fieldSchema)) { const meta = __langchain_langgraph_zod.schemaMetaRegistry.get(fieldSchema); if (meta) { schemaShape[key] = (0, __langchain_langgraph_zod.withLangGraph)(zod_v3.z.any(), meta); ioSchemaShape[key] = zod_v3.z.any(); } else { schemaShape[key] = zod_v3.z.any(); ioSchemaShape[key] = zod_v3.z.any(); } } else { schemaShape[key] = fieldSchema; ioSchemaShape[key] = fieldSchema; } } }; /** * Add state schema properties to the Zod schema */ if (stateSchema && ("shape" in stateSchema || (0, __langchain_core_utils_types.isZodSchemaV4)(stateSchema))) applySchema(stateSchema); for (const middleware of middlewareList) if (middleware.stateSchema) applySchema(middleware.stateSchema); if (hasStructuredResponse) { schemaShape.structuredResponse = zod_v3.z.string().optional(); ioSchemaShape.structuredResponse = zod_v3.z.string().optional(); } const messages = (0, __langchain_langgraph_zod.withLangGraph)(zod_v3.z.custom(), { jsonSchemaExtra: { langgraph_type: "messages" } }); return { state: __langchain_langgraph.MessagesZodState.extend(schemaShape), input: zod_v3.z.object({ messages, ...Object.fromEntries(Object.entries(ioSchemaShape).filter(([key]) => !["structuredResponse", "jumpTo"].includes(key))) }), output: zod_v3.z.object({ messages, ...Object.fromEntries(Object.entries(ioSchemaShape).filter(([key]) => key !== "jumpTo")) }) }; } //#endregion exports.createAgentAnnotationConditional = createAgentAnnotationConditional; //# sourceMappingURL=annotation.cjs.map