langchain
Version:
Typescript bindings for langchain
44 lines (42 loc) • 1.5 kB
JavaScript
import { MessagesZodMeta } from "@langchain/langgraph";
import { z } from "zod/v3";
import { withLangGraph } from "@langchain/langgraph/zod";
//#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 zodSchema = {
messages: withLangGraph(z.custom(), MessagesZodMeta),
jumpTo: z.union([
z.literal("model_request"),
z.literal("tools"),
z.undefined()
]).optional()
};
const applySchema = (schema) => {
const { shape } = schema;
for (const [key, schema$1] of Object.entries(shape)) {
/**
* Skip private state properties
*/
if (key.startsWith("_")) continue;
if (!(key in zodSchema)) zodSchema[key] = schema$1;
}
};
if (stateSchema && "shape" in stateSchema) applySchema(stateSchema);
for (const middleware of middlewareList) if (middleware.stateSchema) applySchema(middleware.stateSchema);
if (hasStructuredResponse) zodSchema.structuredResponse = z.any().optional();
return z.object(zodSchema);
}
const PreHookAnnotation = z.object({
llmInputMessages: withLangGraph(z.custom(), {
reducer: { fn: (_x, update) => MessagesZodMeta.reducer.fn([], update) },
default: () => []
}),
messages: withLangGraph(z.custom(), MessagesZodMeta)
});
//#endregion
export { createAgentAnnotationConditional };
//# sourceMappingURL=annotation.js.map