@langgraph-js/pro
Version:
The Pro SDK for LangGraph - seamlessly integrate your AI agents with frontend interfaces and build complex AI workflows
42 lines (41 loc) • 1.32 kB
JavaScript
import { Annotation } from "@langchain/langgraph";
/**
* create state for langgraph, like python version
* @example
* export const GraphState = createState(createReactAgentAnnotation(), ModelState, SwarmState).build({
* current_plan: createDefaultAnnotation<Plan | null>(() => null),
* title: createDefaultAnnotation<string>(() => ""),
*});
*/
export const createState = (...parents) => {
return {
build: (state = {}) => {
return Annotation.Root(Object.assign({}, ...parents.map((p) => p.spec), state));
},
};
};
/**
* 创建一个默认值注解
* @example
* const state = createState().build({
* current_plan: createDefaultAnnotation<Plan | null>(() => null),
* });
*/
export const createDefaultAnnotation = (defaultValue) => Annotation({
reducer: (_, a) => a,
default: defaultValue,
});
/**
* 创建一个数组注解
* @example
* const state = createState().build({
* current_plan: createArrayAnnotation<Plan>(),
* current_tags: createArrayAnnotation<Plan>(() => ['tag1', 'tag2']),
* });
*/
export const createArrayAnnotation = (defaultValue) => {
return Annotation({
reducer: (x, y) => x.concat(y),
default: () => (defaultValue ? (typeof defaultValue === "function" ? defaultValue() : defaultValue) : []),
});
};