- 薄自建 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 走通闭环
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
// 自动保存的纯逻辑:防抖调度器 + 时间格式化。与 React 解耦,便于单测。
|
|
|
|
export const AUTOSAVE_DELAY_MS = 1200;
|
|
|
|
export type SaveFn = (text: string) => Promise<void>;
|
|
|
|
export interface DebouncedSaver {
|
|
schedule: (text: string) => void;
|
|
flush: () => void;
|
|
cancel: () => void;
|
|
}
|
|
|
|
// 防抖:最后一次输入静默 delayMs 后才真正保存。flush 立即触发待保存项。
|
|
export function createDebouncedSaver(
|
|
save: SaveFn,
|
|
delayMs: number = AUTOSAVE_DELAY_MS,
|
|
): DebouncedSaver {
|
|
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
let pending: string | null = null;
|
|
|
|
const run = (): void => {
|
|
if (pending === null) return;
|
|
const text = pending;
|
|
pending = null;
|
|
timer = null;
|
|
void save(text);
|
|
};
|
|
|
|
return {
|
|
schedule(text: string): void {
|
|
pending = text;
|
|
if (timer !== null) clearTimeout(timer);
|
|
timer = setTimeout(run, delayMs);
|
|
},
|
|
flush(): void {
|
|
if (timer !== null) clearTimeout(timer);
|
|
run();
|
|
},
|
|
cancel(): void {
|
|
if (timer !== null) clearTimeout(timer);
|
|
timer = null;
|
|
pending = null;
|
|
},
|
|
};
|
|
}
|
|
|
|
// 「保存于 hh:mm」标签。
|
|
export function formatSavedAt(date: Date): string {
|
|
const hh = String(date.getHours()).padStart(2, "0");
|
|
const mm = String(date.getMinutes()).padStart(2, "0");
|
|
return `保存于 ${hh}:${mm}`;
|
|
}
|