feat: add template moderator table — office → differ (edit) / END (generate)

This commit is contained in:
jiayiyan
2026-05-18 15:48:37 +08:00
parent 1a9a5d6692
commit e2cd1d1ae5
2 changed files with 78 additions and 0 deletions
@@ -0,0 +1,57 @@
import { START } from "@uncaged/workflow-runtime";
import { describe, expect, test } from "bun:test";
import { documentEditorTable } from "../src/moderator.js";
import type { DocumentEditorMeta } from "../src/roles.js";
import type { ThreadContext } from "@uncaged/workflow-runtime";
function makeCtx(steps: Array<{ role: string; meta: Record<string, unknown> }>): ThreadContext<DocumentEditorMeta> {
return {
threadId: "test-thread",
depth: 0,
bundleHash: "",
start: { role: START, content: "", meta: {}, timestamp: 0, parentState: null },
steps: steps.map((s) => ({ ...s, contentHash: "", refs: [], timestamp: 0 })),
} as unknown as ThreadContext<DocumentEditorMeta>;
}
const officeIsEditCondition = documentEditorTable.office?.find(
(t) => t.condition !== "FALLBACK" && (t.condition as { name: string }).name === "officeIsEdit",
)?.condition as { check: (ctx: ThreadContext<DocumentEditorMeta>) => boolean } | undefined;
describe("officeIsEdit condition", () => {
test("returns false when no office step in context", () => {
const ctx = makeCtx([]);
expect(officeIsEditCondition?.check(ctx)).toBe(false);
});
test("returns false when office step is generate mode", () => {
const ctx = makeCtx([
{ role: "office", meta: { mode: "generate", outputDocx: "/out.docx", sourceDocx: null } },
]);
expect(officeIsEditCondition?.check(ctx)).toBe(false);
});
test("returns true when office step is edit mode", () => {
const ctx = makeCtx([
{ role: "office", meta: { mode: "edit", outputDocx: "/out.docx", sourceDocx: "/src.docx" } },
]);
expect(officeIsEditCondition?.check(ctx)).toBe(true);
});
});
describe("documentEditorTable structure", () => {
test("START routes to office via FALLBACK", () => {
const startTransitions = documentEditorTable[START];
expect(startTransitions).toHaveLength(1);
expect(startTransitions[0].condition).toBe("FALLBACK");
expect(startTransitions[0].role).toBe("office");
});
test("differ routes to END via FALLBACK", () => {
const { END } = require("@uncaged/workflow-runtime");
const differTransitions = documentEditorTable.differ;
expect(differTransitions).toHaveLength(1);
expect(differTransitions[0].condition).toBe("FALLBACK");
expect(differTransitions[0].role).toBe(END);
});
});
@@ -0,0 +1,21 @@
import { END, type ModeratorCondition, type ModeratorTable, START } from "@uncaged/workflow-runtime";
import type { DocumentEditorMeta } from "./roles.js";
const officeIsEdit: ModeratorCondition<DocumentEditorMeta> = {
name: "officeIsEdit",
description: "The office step produced an edited document (not a generated one)",
check: (ctx) => {
const officeStep = ctx.steps.find((s) => s.role === "office");
if (officeStep === undefined) return false;
return (officeStep.meta as { mode: string }).mode === "edit";
},
};
export const documentEditorTable: ModeratorTable<DocumentEditorMeta> = {
[START]: [{ condition: "FALLBACK", role: "office" }],
office: [
{ condition: officeIsEdit, role: "differ" },
{ condition: "FALLBACK", role: END },
],
differ: [{ condition: "FALLBACK", role: END }],
};