feat: taskId 深链会商按任务隔离草稿 + 第三步自动结构化流程图

- 深链 ?taskId=&goPath=*-A 透传 taskId 进会商,按 taskId 隔离存取草稿(一个 taskId 多条、可新建);
  后端新增 GET /api/roundtable-drafts/by-task/{id} 列表 + RoundtableJobRepository.status_map_by_drafts 富化。
- 第三步生成 md 报告后自动结构化输出流程图(仅 taskId 模式):有报告基于报告、无报告基于各席位交付,
  整段内联数据来源逐条抽取;层级优先用业务链总控提示词写明的层级,否则用 stages 的 goal。
- 流程图节点加 group(层级名)标签 + detail(条目名称+理由);强 prompt 压制 coordinatorPrompt 报告锚定,
  禁阶段编号节点;FLOW_STRUCTURE_RULE 精确画法(根=任务、每层每条成节点、按对应关系串分支、排除汇总段落)。
- flow-graph 解析容错:JSON.parse 失败退回正则逐字段抽取,兼容裸换行/尾逗号,避免节点被整体丢弃。
- 修:重开第二步后第三步基于新一轮分析重新生成(复位 step3 闸门 + 中断旧三流)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
fengxiangdebaoziandClaude Opus 4.8 committed 2026-06-17 19:45:35 +08:00
1 parent e6a41dbc97
commit fca39eeaee
17 files changed
+472 -37

No files matched your search

@@ -77,6 +77,62 @@ test("extractFlowGraph:无 fence / 无完整节点返回 null", () => {
assert.equal(extractFlowGraph('```flow-json\n{"nodes":[{"id":"a"'), null);
});
test("extractFlowGraph:group 层级名解析(兼容 group/level)", () => {
const text = [
"```flow-json",
'{"nodes":[',
'{"id":"n1","label":"暑期文旅","type":"start","group":"任务"},',
'{"id":"n2","label":"情绪管理","type":"task","level":"目的","from":["n1"]}',
"]}",
"```",
].join("\n");
const g = extractFlowGraph(text);
assert.ok(g);
assert.equal(g.nodes[0].group, "任务");
assert.equal(g.nodes[1].group, "目的"); // level 别名也认
});
test("extractFlowGraph:容错——detail 含裸换行的非法 JSON 节点不被整个丢弃", () => {
// 模型常在 JSON 字符串里吐真实换行(非法 JSON):n2 的 detail 跨行,
// 旧逻辑会 JSON.parse 失败把 n2/n3 整个丢掉,只剩 n1(即「只出两个节点」bug)。
const text = [
"```flow-json",
'{"title":"T","nodes":[',
'{"id":"n1","label":"任务X","type":"start","group":"任务"},',
'{"id":"n2","label":"目的A","type":"task","group":"目的","detail":"名称:目的A',
'理由:跨了行的说明文字","from":["n1"]},',
'{"id":"n3","label":"行为体A","type":"role","group":"行为体","detail":"名称:行为体A;理由:单行说明","from":["n2"]}',
"]}",
"```",
].join("\n");
const g = extractFlowGraph(text);
assert.ok(g);
// 三个节点都救回来(含裸换行的 n2 不再丢失)。
assert.equal(g.nodes.length, 3);
assert.equal(g.nodes[1].group, "目的");
assert.ok((g.nodes[1].detail ?? "").includes("目的A"));
// 连线也按 from 串起来。
assert.deepEqual(g.edges, [
{ from: "n1", to: "n2" },
{ from: "n2", to: "n3" },
]);
});
test("extractFlowGraph:容错——尾逗号的非法 JSON 节点仍解析", () => {
const text = [
"```flow-json",
'{"nodes":[',
'{"id":"a","label":"A","type":"task","group":"目的",},', // 尾逗号 → JSON.parse 失败
'{"id":"b","label":"B","type":"task","from":["a"]}',
"]}",
"```",
].join("\n");
const g = extractFlowGraph(text);
assert.ok(g);
assert.equal(g.nodes.length, 2);
assert.equal(g.nodes[0].group, "目的");
});
test("extractFlowGraph:兼容独立 edges 数组(source/target 别名)", () => {
const text = [
"```flow-json",
@@ -33,6 +33,12 @@ export interface FlowGraphNode {
label: string;
type: FlowNodeType;
detail?: string;
/**
* 该节点所在的**层级名称**(如 任务 / 目的 / 行为体 / 预期行为 / 驱动因素 / 叙事)。
* 由模型在 flow-json 里给出,画布的分类标签优先显示它(缺省回退到 type 的固定中文名)。
* 让流程图按业务链层级标注,而非只显示通用的 开始/任务/角色 等 type 名。
*/
group?: string;
}
export interface FlowGraphEdge {
@@ -66,10 +72,66 @@ const VALID_TYPES = new Set<string>(Object.keys(FLOW_NODE_TYPE_META));
export const FLOW_FENCE_OPEN = "```flow-json";
/**
* 容错解析单个对象切片(JSON.parse 失败时回退)——模型输出不可控,常在 JSON 字符串里吐
* **裸换行 / 尾逗号 / 中文标点**等非法内容,导致整对象 JSON.parse 失败、节点被整个丢弃
* (表现为「流程图只剩前两个节点」)。这里用正则逐字段抽取,最大限度把节点 / 边救回来。
* 字符串字段正则 `(?:\\.|[^"\\])*` 能跨裸换行匹配(换行既不是 `"` 也不是 `\`)。
*/
function looseParseObject(slice: string): Record<string, unknown> | null {
const pickStr = (...keys: string[]): string | undefined => {
for (const k of keys) {
const m = new RegExp(`"${k}"\\s*:\\s*"((?:\\\\.|[^"\\\\])*)"`).exec(slice);
if (m) {
const raw = m[1];
try {
return JSON.parse(`"${raw.replace(/\r?\n/g, "\\n")}"`) as string;
} catch {
return raw.replace(/\\n/g, "\n").replace(/\\"/g, '"');
}
}
}
return undefined;
};
const obj: Record<string, unknown> = {};
const id = pickStr("id");
if (id) obj.id = id;
const label = pickStr("label", "name");
if (label) obj.label = label;
const type = pickStr("type");
if (type) obj.type = type;
const group = pickStr("group", "level", "tier", "layer");
if (group) obj.group = group;
const detail = pickStr("detail", "description");
if (detail) obj.detail = detail;
// 边的简单形态(独立 edges 数组里):from/to 为字符串。
const toStr = pickStr("to", "target");
if (toStr) obj.to = toStr;
// 节点的 from 为数组(["n1"] 或 [{"id":"n1","label":"是"}]);先按数组抽,抽不到再当字符串。
const fm = /"(?:from|prev)"\s*:\s*\[([\s\S]*?)\]/.exec(slice);
if (fm) {
const inner = fm[1];
const ids: unknown[] = [];
const objRe = /\{[^{}]*?"id"\s*:\s*"([^"]+)"[^{}]*?\}/g;
let om: RegExpExecArray | null;
while ((om = objRe.exec(inner)) !== null) ids.push({ id: om[1] });
if (ids.length === 0) {
const sRe = /"([^"]+)"/g;
let sm: RegExpExecArray | null;
while ((sm = sRe.exec(inner)) !== null) ids.push(sm[1]);
}
if (ids.length > 0) obj.from = ids;
} else {
const fromStr = pickStr("from", "source");
if (fromStr) obj.from = fromStr;
}
return Object.keys(obj).length > 0 ? obj : null;
}
/**
* 平衡括号扫描:从 `key`(如 `"nodes"`)后面的 `[` 开始,把数组里**已完整闭合**的
* 顶层 `{…}` 逐个 JSON.parse 收集;跳过字符串内的括号与转义;数组未闭合(流式半截)
* 时收集到哪算哪,截断的尾对象忽略。
* 顶层 `{…}` 逐个解析收集(JSON.parse 失败则用 looseParseObject 容错回退);跳过字符串内的
* 括号与转义;数组未闭合(流式半截)时收集到哪算哪,截断的尾对象忽略。
*/
function collectObjects(text: string, key: string): Record<string, unknown>[] {
const keyIdx = text.indexOf(key);
@@ -101,14 +163,18 @@ function collectObjects(text: string, key: string): Record<string, unknown>[] {
if (ch === "}") {
depth--;
if (depth === 0 && objStart >= 0) {
const objSlice = text.slice(objStart, i + 1);
let parsed: Record<string, unknown> | null = null;
try {
const parsed = JSON.parse(text.slice(objStart, i + 1)) as unknown;
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
out.push(parsed as Record<string, unknown>);
const j = JSON.parse(objSlice) as unknown;
if (j && typeof j === "object" && !Array.isArray(j)) {
parsed = j as Record<string, unknown>;
}
} catch {
/* 截断/非法对象:跳过 */
// 非法 JSON(裸换行/尾逗号等)→ 容错正则抽取,别整节点丢掉。
parsed = looseParseObject(objSlice);
}
if (parsed) out.push(parsed);
objStart = -1;
}
continue;
@@ -130,7 +196,10 @@ function toNode(raw: Record<string, unknown>): FlowGraphNode | null {
const rawType = asString(raw.type).toLowerCase();
const type = (VALID_TYPES.has(rawType) ? rawType : "task") as FlowNodeType;
const detail = asString(raw.detail) || asString(raw.description) || undefined;
return { id, label, type, detail };
// 层级名(任务/目的/行为体/…):兼容 group / level / tier / layer 几种键名。
const group =
asString(raw.group) || asString(raw.level) || asString(raw.tier) || asString(raw.layer) || undefined;
return { id, label, type, detail, group };
}
/** 节点 `from` 字段(前驱列表,元素是 "n1" 或 {id:"n1",label:"是"})→ 入边列表。 */