|
@@ -0,0 +1,68 @@
|
|
|
|
|
+/**
|
|
|
|
|
+ * LangGraph 状态定义和工作流
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+import { Annotation, StateGraph, END } from '@langchain/langgraph';
|
|
|
|
|
+
|
|
|
|
|
+// ============ 状态定义(借鉴 OpenMAIC Annotation 模式)============
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 进度 reducer:只增不减,防止中间步骤回退导致进度丢失
|
|
|
|
|
+ */
|
|
|
|
|
+const maxReducer = (prev: number, update: number) => Math.max(prev, update);
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 章节完成数 reducer:累加而非覆盖
|
|
|
|
|
+ */
|
|
|
|
|
+const appendReducer = <T>(prev: T[], update: T | T[] | undefined) => {
|
|
|
|
|
+ if (!update) return prev;
|
|
|
|
|
+ const items = Array.isArray(update) ? update : [update];
|
|
|
|
|
+ return [...prev, ...items];
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+export const GraphState = Annotation.Root({
|
|
|
|
|
+ bookId: Annotation<string>({
|
|
|
|
|
+ reducer: (_prev, update) => update ?? _prev,
|
|
|
|
|
+ default: () => '' as string,
|
|
|
|
|
+ }),
|
|
|
|
|
+ topic: Annotation<string>({
|
|
|
|
|
+ reducer: (_prev, update) => update ?? _prev,
|
|
|
|
|
+ default: () => '' as string,
|
|
|
|
|
+ }),
|
|
|
|
|
+ bookScale: Annotation<string>({
|
|
|
|
|
+ reducer: (_prev, update) => update ?? _prev,
|
|
|
|
|
+ default: () => '标准教程' as string,
|
|
|
|
|
+ }),
|
|
|
|
|
+ description: Annotation<string>({
|
|
|
|
|
+ reducer: (_prev, update) => update ?? _prev,
|
|
|
|
|
+ default: () => '' as string,
|
|
|
|
|
+ }),
|
|
|
|
|
+ /** 当前正在处理的章节号 */
|
|
|
|
|
+ currentChapter: Annotation<number>({
|
|
|
|
|
+ reducer: maxReducer,
|
|
|
|
|
+ default: () => 0,
|
|
|
|
|
+ }),
|
|
|
|
|
+ /** 已成功完成的章节数(只增不减) */
|
|
|
|
|
+ completedChapters: Annotation<number[]>({
|
|
|
|
|
+ reducer: appendReducer,
|
|
|
|
|
+ default: () => [] as number[],
|
|
|
|
|
+ }),
|
|
|
|
|
+ finished: Annotation<boolean>({
|
|
|
|
|
+ reducer: (_prev, update) => update ?? _prev,
|
|
|
|
|
+ default: () => false,
|
|
|
|
|
+ }),
|
|
|
|
|
+ error: Annotation<string | undefined>({
|
|
|
|
|
+ reducer: (_prev, update) => update ?? _prev,
|
|
|
|
|
+ default: () => undefined,
|
|
|
|
|
+ }),
|
|
|
|
|
+ /** 生成进度 0-100,只增不减 */
|
|
|
|
|
+ progress: Annotation<number>({
|
|
|
|
|
+ reducer: maxReducer,
|
|
|
|
|
+ default: () => 0,
|
|
|
|
|
+ }),
|
|
|
|
|
+ /** 失败章节列表,通过 reducer 合并而非覆盖 */
|
|
|
|
|
+ failedChapters: Annotation<number[]>({
|
|
|
|
|
+ reducer: appendReducer,
|
|
|
|
|
+ default: () => [] as number[],
|
|
|
|
|
+ }),
|
|
|
|
|
+});
|