第三步结构化输出优化
This commit is contained in:
1 parent
fca39eeaee
commit
d3880f51b3
10 files changed
+1095
-127
No files matched your search
@@ -9,10 +9,13 @@ import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
|
||||
import {
|
||||
applyNodeEdit,
|
||||
deleteNodeFromGraph,
|
||||
detectFlowIntent,
|
||||
extractFlowGraph,
|
||||
hasFlowFence,
|
||||
layoutFlowGraph,
|
||||
serializeFlowGraph,
|
||||
stripFlowFence,
|
||||
} from "./flow-graph";
|
||||
|
||||
@@ -206,3 +209,51 @@ test("layoutFlowGraph:环不死循环", () => {
|
||||
assert.equal(positioned.length, 2);
|
||||
positioned.forEach((n) => assert.ok(Number.isFinite(n.x) && Number.isFinite(n.y)));
|
||||
});
|
||||
|
||||
test("serializeFlowGraph:与 extractFlowGraph 往返一致(含 group/detail/边标签)", () => {
|
||||
const text = [
|
||||
"```flow-json",
|
||||
'{"title":"方案","nodes":[',
|
||||
'{"id":"t","label":"任务X","type":"start","group":"任务","detail":"目标说明"},',
|
||||
'{"id":"p1","label":"目的1","type":"task","group":"目的","detail":"名称+理由","from":["t"]},',
|
||||
'{"id":"d","label":"判断","type":"decision","from":[{"id":"p1","label":"可行"}]}',
|
||||
"]}",
|
||||
"```",
|
||||
].join("\n");
|
||||
const g = extractFlowGraph(text)!;
|
||||
const round = extractFlowGraph(serializeFlowGraph(g))!;
|
||||
assert.equal(round.title, "方案");
|
||||
assert.deepEqual(
|
||||
round.nodes.map((n) => [n.id, n.label, n.group, n.detail]),
|
||||
g.nodes.map((n) => [n.id, n.label, n.group, n.detail]),
|
||||
);
|
||||
assert.deepEqual(round.edges, g.edges); // 含 {from:"p1",to:"d",label:"可行"}
|
||||
});
|
||||
|
||||
test("applyNodeEdit:改 label/group/detail,其余节点不变", () => {
|
||||
const g = extractFlowGraph(
|
||||
'```flow-json\n{"nodes":[{"id":"a","label":"A","type":"task","group":"目的"},{"id":"b","label":"B","type":"role","group":"行为体","from":["a"]}]}\n```',
|
||||
)!;
|
||||
const next = applyNodeEdit(g, "a", { label: "新目的", group: "目标", detail: "改后的详情" });
|
||||
const a = next.nodes.find((n) => n.id === "a")!;
|
||||
assert.equal(a.label, "新目的");
|
||||
assert.equal(a.group, "目标");
|
||||
assert.equal(a.detail, "改后的详情");
|
||||
// b 不变;边不变。
|
||||
assert.equal(next.nodes.find((n) => n.id === "b")!.label, "B");
|
||||
assert.deepEqual(next.edges, g.edges);
|
||||
// 空 group/detail → 清空(置 undefined)。
|
||||
const cleared = applyNodeEdit(g, "a", { group: " ", detail: "" });
|
||||
const a2 = cleared.nodes.find((n) => n.id === "a")!;
|
||||
assert.equal(a2.group, undefined);
|
||||
});
|
||||
|
||||
test("deleteNodeFromGraph:删节点同时删掉与它相连的所有边", () => {
|
||||
const g = extractFlowGraph(
|
||||
'```flow-json\n{"nodes":[{"id":"t","label":"任务"},{"id":"p","label":"目的","from":["t"]},{"id":"b","label":"行为体","from":["p"]}]}\n```',
|
||||
)!;
|
||||
const next = deleteNodeFromGraph(g, "p");
|
||||
assert.deepEqual(next.nodes.map((n) => n.id), ["t", "b"]);
|
||||
// t→p 和 p→b 两条边都没了(b 变孤点)。
|
||||
assert.equal(next.edges.length, 0);
|
||||
});
|
||||
@@ -281,6 +281,64 @@ export function extractFlowGraph(text: string): FlowGraphData | null {
|
||||
return { title, nodes, edges: validEdges };
|
||||
}
|
||||
|
||||
/**
|
||||
* 把结构化图序列化回一个 ```flow-json 代码块(与 extractFlowGraph 互逆)。
|
||||
* 用途:后端校验后拿到 FlowGraphData → 序列化存进草稿快照(closing),恢复时仍走
|
||||
* extractFlowGraph 重建;手动编辑后也用它回存。`from` 由 edges 按 `to` 归集(带边标签)。
|
||||
*/
|
||||
export function serializeFlowGraph(data: FlowGraphData): string {
|
||||
const fromMap = new Map<string, Array<string | { id: string; label: string }>>();
|
||||
for (const e of data.edges) {
|
||||
const list = fromMap.get(e.to) ?? [];
|
||||
list.push(e.label ? { id: e.from, label: e.label } : e.from);
|
||||
fromMap.set(e.to, list);
|
||||
}
|
||||
const nodes = data.nodes.map((n) => {
|
||||
const o: Record<string, unknown> = { id: n.id, label: n.label, type: n.type };
|
||||
if (n.group) o.group = n.group;
|
||||
if (n.detail) o.detail = n.detail;
|
||||
const from = fromMap.get(n.id);
|
||||
if (from && from.length > 0) o.from = from;
|
||||
return o;
|
||||
});
|
||||
return `${FLOW_FENCE_OPEN}\n${JSON.stringify({ title: data.title || "方案流程图", nodes })}\n\`\`\``;
|
||||
}
|
||||
|
||||
/**
|
||||
* Phase 3 画布手动编辑:改某节点的 label/group/detail(其余不变)。返回新图(不可变)。
|
||||
*/
|
||||
export function applyNodeEdit(
|
||||
data: FlowGraphData,
|
||||
id: string,
|
||||
patch: { label?: string; group?: string; detail?: string },
|
||||
): FlowGraphData {
|
||||
return {
|
||||
...data,
|
||||
nodes: data.nodes.map((n) =>
|
||||
n.id === id
|
||||
? {
|
||||
...n,
|
||||
...(patch.label !== undefined ? { label: patch.label.trim() || n.label } : {}),
|
||||
// group/detail 允许清空(传空串 → 置 undefined)。
|
||||
...(patch.group !== undefined ? { group: patch.group.trim() || undefined } : {}),
|
||||
...(patch.detail !== undefined ? { detail: patch.detail.trim() || undefined } : {}),
|
||||
}
|
||||
: n,
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Phase 3 画布手动删除:删一个节点 + 所有与它相连的边。返回新图。
|
||||
*/
|
||||
export function deleteNodeFromGraph(data: FlowGraphData, id: string): FlowGraphData {
|
||||
return {
|
||||
...data,
|
||||
nodes: data.nodes.filter((n) => n.id !== id),
|
||||
edges: data.edges.filter((e) => e.from !== id && e.to !== id),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 识别文本(业务链编排提示 / 用户输入框消息)里是否明确要求了结构化输出 / 流程图。
|
||||
* 命中 → 总结走「流程图模式」:跳过 .md 文件,报告用文字展示 + 末尾输出 flow-json。
|
||||
|
||||
Reference in new issue
Block a user