fix: Refactor graph state to use zod (#10)

* fix: Refactor graph state to use zod

* handle err
This commit is contained in:
Brace Sproul 2025-05-22 13:44:44 -07:00 • committed by GitHub
parent 6e470e6bd8
commit d3fdfc228e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 27 additions and 21 deletions

View file

@ -81,5 +81,6 @@ const workflow = new StateGraph(GraphAnnotation, GraphConfiguration)
.addEdge("take-action", "progress-plan-step")
.addEdge("progress-plan-step", "take-action");
export const graph = workflow.compile();
// Zod types are messed up
export const graph = workflow.compile() as any;
graph.name = "LangGraph ReAct MCP";

View file

@ -71,6 +71,6 @@ export async function generateAction(
]);
return {
messages: response,
messages: [response],
};
}

View file

@ -41,7 +41,7 @@ export async function generatePlan(
// No tool calls generated, instead we should just return the messages.
return {
messages: response,
messages: [response],
proposedPlan: [],
plan: [],
};

View file

@ -1,10 +1,11 @@
import "@langchain/langgraph/zod";
import { z } from "zod";
import {
Annotation,
addMessages,
LangGraphRunnableConfig,
MessagesAnnotation,
Messages,
} from "@langchain/langgraph";
import { BaseMessage } from "@langchain/core/messages";
export type PlanItem = {
/**
@ -28,24 +29,28 @@ export type TargetRepository = {
branch?: string;
};
export const GraphAnnotation = Annotation.Root({
messages: MessagesAnnotation.spec.messages,
proposedPlan: Annotation<string[]>({
reducer: (_state, update) => update,
default: () => [],
}),
plan: Annotation<PlanItem[]>({
reducer: (_state, update) => update,
default: () => [],
}),
planChangeRequest: Annotation<string | undefined>({
reducer: (_state, update) => update,
default: () => undefined,
}),
export const GraphAnnotation = z.object({
messages: z
.custom<BaseMessage[]>()
.default(() => [])
.langgraph.reducer<Messages>((state, update) => addMessages(state, update)),
proposedPlan: z
.array(z.string())
.default(() => [])
.langgraph.reducer((_state, update) => update),
plan: z
.custom<PlanItem[]>()
.default(() => [])
.langgraph.reducer<PlanItem[]>((_state, update) => update),
planChangeRequest: z
.string()
.nullable()
.default(() => null)
.langgraph.reducer((_state, update) => update),
});
export type GraphState = typeof GraphAnnotation.State;
export type GraphUpdate = typeof GraphAnnotation.Update;
export type GraphState = z.infer<typeof GraphAnnotation>;
export type GraphUpdate = Partial<GraphState>;
const MODEL_OPTIONS = [
{