feat: M1 — 立项→写章草稿(SSE)→自动保存;连一家 provider
- 薄自建 LLM 网关:OpenAI 兼容适配器(DeepSeek) + instructor 结构化输出 + usage_ledger 记账 + 档位路由 - 记忆服务 assemble:确定性选择(显式+主角+近况) + 渲染卡 + 缓存断点(中性文本) - LangGraph 写章节点 + Postgres checkpointer + SSE 归一(token/done/error) - API:立项 + 写章 draft(SSE) + PUT 自动保存 + 提供商凭据(Fernet 加密/测试连接) - 前端:AppShell + 作品库 + 5 步立项向导 + 写作工作台(流式打字机+自动保存) + 设置页 - M1 E2E:真实 DB + mock 网关零 token 走通闭环
This commit is contained in:
96
apps/web/lib/stream/sse.test.ts
Normal file
96
apps/web/lib/stream/sse.test.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
SseFrameBuffer,
|
||||
initialStreamState,
|
||||
parseSseBlock,
|
||||
reduceStream,
|
||||
type SseEvent,
|
||||
} from "./sse";
|
||||
|
||||
describe("parseSseBlock", () => {
|
||||
it("parses a token frame", () => {
|
||||
const evt = parseSseBlock('event:token\ndata:{"text":"夜"}');
|
||||
expect(evt).toEqual({ event: "token", data: { text: "夜" } });
|
||||
});
|
||||
|
||||
it("parses a done frame", () => {
|
||||
const evt = parseSseBlock('event:done\ndata:{"length":12}');
|
||||
expect(evt).toEqual({ event: "done", data: { length: 12 } });
|
||||
});
|
||||
|
||||
it("parses an error frame with request_id", () => {
|
||||
const evt = parseSseBlock(
|
||||
'event:error\ndata:{"code":"INTERNAL","message":"boom","request_id":"r1"}',
|
||||
);
|
||||
expect(evt).toEqual({
|
||||
event: "error",
|
||||
data: { code: "INTERNAL", message: "boom", request_id: "r1" },
|
||||
});
|
||||
});
|
||||
|
||||
it("ignores comment/heartbeat lines and tolerates leading space after colon", () => {
|
||||
const evt = parseSseBlock(': keep-alive\nevent: token\ndata: {"text":"x"}');
|
||||
expect(evt).toEqual({ event: "token", data: { text: "x" } });
|
||||
});
|
||||
|
||||
it("returns null for unknown event types", () => {
|
||||
expect(parseSseBlock('event:section\ndata:{"x":1}')).toBeNull();
|
||||
});
|
||||
|
||||
it("returns null for malformed json", () => {
|
||||
expect(parseSseBlock("event:token\ndata:{not json")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("SseFrameBuffer", () => {
|
||||
it("emits complete blocks split by blank line and buffers the tail", () => {
|
||||
const buf = new SseFrameBuffer();
|
||||
const first = buf.push('event:token\ndata:{"text":"a"}\n\nevent:tok');
|
||||
expect(first).toEqual([{ event: "token", data: { text: "a" } }]);
|
||||
const second = buf.push('en\ndata:{"text":"b"}\n\n');
|
||||
expect(second).toEqual([{ event: "token", data: { text: "b" } }]);
|
||||
});
|
||||
|
||||
it("handles CRLF boundaries", () => {
|
||||
const buf = new SseFrameBuffer();
|
||||
const out = buf.push('event:token\r\ndata:{"text":"z"}\r\n\r\n');
|
||||
expect(out).toEqual([{ event: "token", data: { text: "z" } }]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("reduceStream", () => {
|
||||
it("accumulates token text and marks streaming", () => {
|
||||
const tokens: SseEvent[] = [
|
||||
{ event: "token", data: { text: "夜色" } },
|
||||
{ event: "token", data: { text: "如墨" } },
|
||||
];
|
||||
const state = tokens.reduce(reduceStream, initialStreamState);
|
||||
expect(state.text).toBe("夜色如墨");
|
||||
expect(state.phase).toBe("streaming");
|
||||
});
|
||||
|
||||
it("marks done on done event preserving text", () => {
|
||||
let s = reduceStream(initialStreamState, {
|
||||
event: "token",
|
||||
data: { text: "abc" },
|
||||
});
|
||||
s = reduceStream(s, { event: "done", data: { length: 3 } });
|
||||
expect(s.phase).toBe("done");
|
||||
expect(s.text).toBe("abc");
|
||||
});
|
||||
|
||||
it("captures error and retains partial text", () => {
|
||||
let s = reduceStream(initialStreamState, {
|
||||
event: "token",
|
||||
data: { text: "partial" },
|
||||
});
|
||||
s = reduceStream(s, {
|
||||
event: "error",
|
||||
data: { code: "LLM_UNAVAILABLE", message: "no key" },
|
||||
});
|
||||
expect(s.phase).toBe("error");
|
||||
expect(s.text).toBe("partial");
|
||||
expect(s.error?.code).toBe("LLM_UNAVAILABLE");
|
||||
});
|
||||
});
|
||||
106
apps/web/lib/stream/sse.ts
Normal file
106
apps/web/lib/stream/sse.ts
Normal file
@@ -0,0 +1,106 @@
|
||||
// SSE 帧解析 + 草稿流归一(对齐 C3 / ARCH §7.3)。
|
||||
// 帧格式:`event:<token|done|error>\ndata:<json>\n\n`。
|
||||
// 纯逻辑,便于单测(不依赖浏览器/DOM)。
|
||||
|
||||
export interface TokenEvent {
|
||||
event: "token";
|
||||
data: { text: string };
|
||||
}
|
||||
export interface DoneEvent {
|
||||
event: "done";
|
||||
data: { length: number };
|
||||
}
|
||||
export interface ErrorEvent {
|
||||
event: "error";
|
||||
data: { code: string; message: string; request_id?: string | null };
|
||||
}
|
||||
export type SseEvent = TokenEvent | DoneEvent | ErrorEvent;
|
||||
|
||||
const KNOWN_EVENTS = new Set(["token", "done", "error"]);
|
||||
|
||||
// 把一个完整 SSE 块(多行)解析成事件;无法解析则返回 null(跳过)。
|
||||
export function parseSseBlock(block: string): SseEvent | 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 SseEvent;
|
||||
}
|
||||
|
||||
// 增量缓冲:吃进一段文本,吐出已完成的事件块(以空行分隔),保留未完成尾部。
|
||||
export class SseFrameBuffer {
|
||||
private buf = "";
|
||||
|
||||
push(chunk: string): SseEvent[] {
|
||||
this.buf += chunk;
|
||||
const events: SseEvent[] = [];
|
||||
let idx: number;
|
||||
// 块之间以空行(\n\n,兼容 \r\n\r\n)分隔。
|
||||
while ((idx = this.findBoundary(this.buf)) !== -1) {
|
||||
const block = this.buf.slice(0, idx.valueOf());
|
||||
this.buf = this.buf.slice(this.boundaryEnd(this.buf, idx));
|
||||
const evt = parseSseBlock(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 StreamPhase = "idle" | "streaming" | "done" | "error" | "aborted";
|
||||
|
||||
export interface StreamState {
|
||||
phase: StreamPhase;
|
||||
text: string;
|
||||
error: { code: string; message: string; request_id?: string | null } | null;
|
||||
}
|
||||
|
||||
export const initialStreamState: StreamState = {
|
||||
phase: "idle",
|
||||
text: "",
|
||||
error: null,
|
||||
};
|
||||
|
||||
// 纯 reducer:把单个事件折叠进状态(打字机文本累积)。
|
||||
export function reduceStream(state: StreamState, event: SseEvent): StreamState {
|
||||
switch (event.event) {
|
||||
case "token":
|
||||
return {
|
||||
...state,
|
||||
phase: "streaming",
|
||||
text: state.text + event.data.text,
|
||||
};
|
||||
case "done":
|
||||
return { ...state, phase: "done" };
|
||||
case "error":
|
||||
return { ...state, phase: "error", error: event.data };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
122
apps/web/lib/stream/useDraftStream.ts
Normal file
122
apps/web/lib/stream/useDraftStream.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useReducer, useRef } from "react";
|
||||
|
||||
import { API_BASE_PUBLIC } from "@/lib/api/config";
|
||||
import {
|
||||
SseFrameBuffer,
|
||||
initialStreamState,
|
||||
reduceStream,
|
||||
type StreamState,
|
||||
} from "./sse";
|
||||
|
||||
type Action =
|
||||
| { type: "start" }
|
||||
| { type: "events"; events: ReturnType<SseFrameBuffer["push"]> }
|
||||
| { type: "abort" }
|
||||
| { type: "fail"; code: string; message: string }
|
||||
| { type: "reset"; text: string };
|
||||
|
||||
function reducer(state: StreamState, action: Action): StreamState {
|
||||
switch (action.type) {
|
||||
case "start":
|
||||
return { phase: "streaming", text: "", error: null };
|
||||
case "events":
|
||||
return action.events.reduce(reduceStream, state);
|
||||
case "abort":
|
||||
return { ...state, phase: "aborted" };
|
||||
case "fail":
|
||||
return {
|
||||
...state,
|
||||
phase: "error",
|
||||
error: { code: action.code, message: action.message },
|
||||
};
|
||||
case "reset":
|
||||
return { ...initialStreamState, text: action.text };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
export interface UseDraftStream {
|
||||
state: StreamState;
|
||||
isStreaming: boolean;
|
||||
start: (projectId: string, chapterNo: number) => Promise<void>;
|
||||
stop: () => void;
|
||||
reset: (text: string) => void;
|
||||
}
|
||||
|
||||
// 消费 POST .../draft 的 SSE 流。用 fetch+ReadableStream(EventSource 不支持 POST)。
|
||||
// "停" = abort 连接(已生成部分保留在 state.text,由调用方落入草稿)。
|
||||
export function useDraftStream(): UseDraftStream {
|
||||
const [state, dispatch] = useReducer(reducer, initialStreamState);
|
||||
const controllerRef = useRef<AbortController | null>(null);
|
||||
|
||||
const stop = useCallback(() => {
|
||||
controllerRef.current?.abort();
|
||||
controllerRef.current = null;
|
||||
dispatch({ type: "abort" });
|
||||
}, []);
|
||||
|
||||
const reset = useCallback((text: string) => {
|
||||
dispatch({ type: "reset", text });
|
||||
}, []);
|
||||
|
||||
const start = useCallback(async (projectId: string, chapterNo: number) => {
|
||||
const controller = new AbortController();
|
||||
controllerRef.current = controller;
|
||||
dispatch({ type: "start" });
|
||||
try {
|
||||
const res = await fetch(
|
||||
`${API_BASE_PUBLIC}/projects/${projectId}/chapters/${chapterNo}/draft`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { Accept: "text/event-stream" },
|
||||
signal: controller.signal,
|
||||
},
|
||||
);
|
||||
if (!res.ok || !res.body) {
|
||||
// 流前错误(如无凭据 → 503 LLM_UNAVAILABLE,JSON 信封而非帧)。
|
||||
let code = "STREAM_FAILED";
|
||||
let message = `写章请求失败(${res.status})`;
|
||||
try {
|
||||
const body = (await res.json()) as {
|
||||
error?: { code?: string; message?: string };
|
||||
};
|
||||
if (body.error?.code) code = body.error.code;
|
||||
if (body.error?.message) message = body.error.message;
|
||||
} catch {
|
||||
// 非 JSON 信封,沿用默认文案。
|
||||
}
|
||||
dispatch({ type: "fail", code, message });
|
||||
return;
|
||||
}
|
||||
const reader = res.body.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
const buffer = new SseFrameBuffer();
|
||||
for (;;) {
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
const events = buffer.push(decoder.decode(value, { stream: true }));
|
||||
if (events.length > 0) dispatch({ type: "events", events });
|
||||
}
|
||||
} catch (err: unknown) {
|
||||
if (err instanceof DOMException && err.name === "AbortError") {
|
||||
// 用户主动停止:state 已置 aborted。
|
||||
return;
|
||||
}
|
||||
const message = err instanceof Error ? err.message : "未知网络错误";
|
||||
dispatch({ type: "fail", code: "NETWORK", message });
|
||||
} finally {
|
||||
controllerRef.current = null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
return {
|
||||
state,
|
||||
isStreaming: state.phase === "streaming",
|
||||
start,
|
||||
stop,
|
||||
reset,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user