Files
writer-work-flow/apps/web/lib/review/sse.ts
Yaojia Wang 68f194a043 feat: M2 — 写→审(一致性)→裁决→验收(事务);未决冲突禁验收
- 续审 Agent 声明(AgentSpec) + 结构化输出契约(ContinuityReview/Conflict 五类)
- LangGraph 并行审子图(可扩四审) + collect 落 chapter_reviews 留痕 + review SSE(section/conflict)
- 验收-side Repository:章节 accepted 版本晋升 + digest append-only + 审稿留痕/裁决
- API:review(SSE) + reviews 历史 + accept(单原子事务:晋升 version + 终稿 digest + 裁决留痕)
- 冲突 gate:未决裁决拦截(CONFLICT_UNRESOLVED);digest 从终稿提炼(不变量#4)
- 前端:审稿报告页 + 冲突就地标注 + 裁决(采纳/忽略/手改) + 未决禁验收 + 「本次将更新」清单
- M2 E2E:真实 DB + 多档位 mock 网关零 token 走通 写→审→裁决→验收→摘要入库
- 多 agent 协同台账(PROGRESS.md) + 共享记忆(memory/contracts·decisions·gotchas)
2026-06-18 11:38:28 +02:00

156 lines
4.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 审稿 SSE 帧归一 + reducer对齐 C3 / C4 扩 / ARCH §7.3)。
// 帧:`section{name,status}` / `conflict{type,where,refs,suggestion}` / `done{length}` / `error{...}`。
// 纯逻辑,便于 node 环境单测(不依赖 DOM
// 五类冲突C6 ConflictType / ARCH §6.1)。
export type ConflictType =
| "性格漂移"
| "能力不符"
| "设定违例"
| "地理矛盾"
| "时间线倒错";
export interface ReviewConflict {
type: string;
where: string;
refs: string[];
suggestion: string;
}
export type SectionStatus = "started" | "done" | "incomplete";
export interface SectionEvent {
event: "section";
data: { name: string; status: SectionStatus };
}
export interface ConflictEvent {
event: "conflict";
data: ReviewConflict;
}
export interface DoneEvent {
event: "done";
data: { length: number };
}
export interface ErrorEvent {
event: "error";
data: { code: string; message: string; request_id?: string | null };
}
export type ReviewSseEvent =
| SectionEvent
| ConflictEvent
| DoneEvent
| ErrorEvent;
const KNOWN_EVENTS = new Set(["section", "conflict", "done", "error"]);
// 把一个完整 SSE 块(多行)解析成事件;无法解析则返回 null跳过
export function parseReviewBlock(block: string): ReviewSseEvent | null {
let event = "";
const dataLines: string[] = [];
for (const rawLine of block.split("\n")) {
const line = rawLine.replace(/\r$/, "");
if (line.startsWith(":")) continue; // 注释/心跳
const sep = line.indexOf(":");
if (sep === -1) continue;
const field = line.slice(0, sep);
const value = line.slice(sep + 1).replace(/^ /, "");
if (field === "event") event = value;
else if (field === "data") dataLines.push(value);
}
if (!KNOWN_EVENTS.has(event) || dataLines.length === 0) return null;
let data: unknown;
try {
data = JSON.parse(dataLines.join("\n"));
} catch {
return null;
}
return { event, data } as ReviewSseEvent;
}
// 增量缓冲:吃进一段文本,吐出已完成的事件块(空行分隔),保留未完成尾部。
export class ReviewFrameBuffer {
private buf = "";
push(chunk: string): ReviewSseEvent[] {
this.buf += chunk;
const events: ReviewSseEvent[] = [];
let idx: number;
while ((idx = this.findBoundary(this.buf)) !== -1) {
const block = this.buf.slice(0, idx);
this.buf = this.buf.slice(this.boundaryEnd(this.buf, idx));
const evt = parseReviewBlock(block);
if (evt) events.push(evt);
}
return events;
}
private findBoundary(s: string): number {
const a = s.indexOf("\n\n");
const b = s.indexOf("\r\n\r\n");
if (a === -1) return b;
if (b === -1) return a;
return Math.min(a, b);
}
private boundaryEnd(s: string, idx: number): number {
return s.startsWith("\r\n\r\n", idx) ? idx + 4 : idx + 2;
}
}
export type ReviewPhase = "idle" | "reviewing" | "done" | "error" | "aborted";
export interface ReviewSection {
name: string;
status: SectionStatus;
}
export interface ReviewStreamState {
phase: ReviewPhase;
sections: ReviewSection[];
conflicts: ReviewConflict[];
error: { code: string; message: string; request_id?: string | null } | null;
}
export const initialReviewState: ReviewStreamState = {
phase: "idle",
sections: [],
conflicts: [],
error: null,
};
// 纯 reducer把单个事件折叠进状态。section 按 name upsert最后状态生效
export function reduceReview(
state: ReviewStreamState,
event: ReviewSseEvent,
): ReviewStreamState {
switch (event.event) {
case "section":
return {
...state,
phase: "reviewing",
sections: upsertSection(state.sections, event.data),
};
case "conflict":
return {
...state,
phase: "reviewing",
conflicts: [...state.conflicts, event.data],
};
case "done":
return { ...state, phase: "done" };
case "error":
return { ...state, phase: "error", error: event.data };
default:
return state;
}
}
function upsertSection(
sections: ReviewSection[],
next: ReviewSection,
): ReviewSection[] {
const idx = sections.findIndex((s) => s.name === next.name);
if (idx === -1) return [...sections, next];
return sections.map((s, i) => (i === idx ? next : s));
}