deepagents
Version:
Deep Agents - a library for building controllable AI agents with LangGraph
54 lines (53 loc) • 1.53 kB
JavaScript
/**
* State definitions for Deep Agents
*
* TypeScript equivalents of the Python state classes using LangGraph's Annotation.Root() pattern.
* Defines Todo interface and DeepAgentState using MessagesAnnotation as base with proper reducer functions.
*/
import "@langchain/langgraph/zod";
import { MessagesZodState } from "@langchain/langgraph";
import { withLangGraph } from "@langchain/langgraph/zod";
import { z } from "zod";
/**
* File reducer function that merges file dictionaries
* Matches the Python file_reducer function behavior exactly
*/
export function fileReducer(left, right) {
if (left == null) {
return right || {};
}
else if (right == null) {
return left;
}
else {
return { ...left, ...right };
}
}
/**
* Todo reducer function that replaces the entire todo list
* This matches the Python behavior where todos are completely replaced
*/
export function todoReducer(left, right) {
if (right != null) {
return right;
}
return left || [];
}
/**
* DeepAgentState using LangGraph's Annotation.Root() pattern
* Extends MessagesAnnotation (equivalent to Python's AgentState) with todos and files channels
*/
export const DeepAgentState = MessagesZodState.extend({
todos: withLangGraph(z.custom(), {
reducer: {
schema: z.custom(),
fn: todoReducer,
},
}),
files: withLangGraph(z.custom(), {
reducer: {
schema: z.custom(),
fn: fileReducer,
},
}),
});