feat(ai写作): 历史记录可继续未完成的会话(从checkpoint续跑)
历史列表里 in_progress/draft_ready/review_ready 记录加绿色▶️「继续未完成」按钮 (点标题亦可);done/error/background 不可继续。多 worker 安全——与4个确认卡同机制, 全基于共享 checkpoint(gateway GET /threads/{id}/state + /history 读,任意 worker)。 链路:continueWriting(sessionId) → closeHistory + 设活跃 sessionId → useStream fetchStateHistory 拉该 thread checkpoint 进 stream.values → host rehydrate effect (continueTargetRef 守卫,每 thread 一次)用 buildRehydrateSnapshot 重建已完成阶段; status=awaiting_* → 显示确认卡等用户操作,否则 submit({command:{}}) 从 checkpoint 续跑当前阶段。重新加回 buildRehydrateSnapshot + rehydrate action。 入口在侧栏 WritingHistoryItem。刷新=重置不受影响(这是显式点击,非自动重连)。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e889cc25cf
commit
c11307aa67
3 files changed
+135
-10
No files matched your search
@@ -26,6 +26,7 @@ import { saveAIWritingTranscript } from '../api/ai-writing-sessions';
|
||||
import { countArticleWords } from '../utils/wordCount';
|
||||
import {
|
||||
INITIAL_STATE,
|
||||
buildRehydrateSnapshot,
|
||||
customToProgressEvent,
|
||||
interruptToEvent,
|
||||
nodeUpdatesToEvents,
|
||||
@@ -101,6 +102,9 @@ export type AIWritingApi = {
|
||||
submitIntervention: (payload: UserInterventionPayload) => Promise<void>;
|
||||
pauseWriting: () => void;
|
||||
resumeWriting: () => void;
|
||||
/** 继续一条未完成的历史会话:加载其 checkpoint 重建已完成阶段,并从断点续跑当前阶段
|
||||
* (停在确认点则显示确认卡)。多 worker 安全(基于共享 checkpoint,同 4 个确认卡机制)。 */
|
||||
continueWriting: (sessionId: string) => void;
|
||||
reset: () => void;
|
||||
};
|
||||
|
||||
@@ -133,12 +137,15 @@ const AIWritingStreamHost = memo(function AIWritingStreamHost({
|
||||
sessionIdRef,
|
||||
streamRef,
|
||||
seenInterruptRef,
|
||||
continueTargetRef,
|
||||
}: {
|
||||
threadId: string | undefined;
|
||||
dispatchRef: RefObject<Dispatch<Action>>;
|
||||
sessionIdRef: RefObject<string | null>;
|
||||
streamRef: MutableRefObject<AIWritingStreamApi | null>;
|
||||
seenInterruptRef: MutableRefObject<string | null>;
|
||||
/** 非空 = 本次要"继续未完成的历史会话",等 checkpoint values 到位后 rehydrate + 驱动续跑。 */
|
||||
continueTargetRef: MutableRefObject<string | null>;
|
||||
}) {
|
||||
const eventBatcherRef = useRef<ReturnType<typeof createProgressEventBatcher> | null>(null);
|
||||
if (!eventBatcherRef.current) {
|
||||
@@ -238,6 +245,41 @@ const AIWritingStreamHost = memo(function AIWritingStreamHost({
|
||||
if (ev) emitProgressEvent(ev);
|
||||
}, [stream.interrupt, emitProgressEvent, seenInterruptRef, sessionIdRef]);
|
||||
|
||||
// ── 继续未完成的历史会话:等该 thread 的 checkpoint values 到位后 rehydrate + 驱动续跑 ──
|
||||
// 仅在显式点「继续」时触发(continueTargetRef 由 continueWriting 置位),非自动重连。
|
||||
// 每个 thread 只做一次:rehydrate 用 checkpoint 全量重建已完成阶段(素材/大纲/草稿/审核/时间线),
|
||||
// 然后——停在确认点(awaiting_*)就显示确认卡等用户决策;停在某活跃阶段中途就发空 command:{}
|
||||
// 让后端从 checkpoint 续跑该阶段(worker 空 command → astream(None))。
|
||||
const rehydratedForRef = useRef<string | null>(null);
|
||||
useEffect(() => {
|
||||
const target = continueTargetRef.current;
|
||||
if (!target || !threadId || target !== threadId) return;
|
||||
if (rehydratedForRef.current === threadId) return;
|
||||
const values = stream.values as AIWritingValues | undefined;
|
||||
if (!values || !values.status) return; // 等 fetchStateHistory 把 checkpoint 拉到
|
||||
rehydratedForRef.current = threadId;
|
||||
continueTargetRef.current = null;
|
||||
const snapshot = buildRehydrateSnapshot(values, threadId);
|
||||
dispatchRef.current?.({ type: 'rehydrate', snapshot });
|
||||
const status = String(snapshot.status ?? '');
|
||||
if (status.startsWith('awaiting_')) {
|
||||
// 停在确认点:补一条 await_user 设好 currentPause(与下方既有 interrupt effect 二者其一生效,
|
||||
// 对齐 seenInterruptRef 防重复派发);不自动驱动,等用户在确认卡上操作。
|
||||
const intr = stream.interrupt;
|
||||
if (intr) {
|
||||
const ev = interruptToEvent(intr.value, threadId);
|
||||
if (ev) dispatchRef.current?.({ type: 'handle', event: ev });
|
||||
seenInterruptRef.current = intr.id ?? JSON.stringify(intr.value);
|
||||
}
|
||||
} else {
|
||||
// 停在某活跃阶段中途:从 checkpoint 续跑该阶段。
|
||||
void streamRef.current?.submit(undefined, {
|
||||
command: {},
|
||||
streamMode: [...WRITING_STREAM_MODE],
|
||||
}).catch(() => { /* 流式错误由 onError 反映 */ });
|
||||
}
|
||||
}, [stream.values, stream.interrupt, threadId, continueTargetRef, dispatchRef, seenInterruptRef, streamRef]);
|
||||
|
||||
useEffect(() => () => { eventBatcherRef.current?.cancel(); }, []);
|
||||
|
||||
return null;
|
||||
@@ -252,6 +294,8 @@ export function AIWritingProvider({ children }: { children: ReactNode }) {
|
||||
sessionIdRef.current = state.sessionId;
|
||||
const streamRef = useRef<AIWritingStreamApi | null>(null);
|
||||
const seenInterruptRef = useRef<string | null>(null);
|
||||
// 「继续未完成历史」目标:continueWriting 置位,host 在该 thread 的 checkpoint 到位后 rehydrate。
|
||||
const continueTargetRef = useRef<string | null>(null);
|
||||
|
||||
// 刷新页面 = 重置:不持久化、不自动重连。provider 重新挂载即回到 idle 初始态
|
||||
// (刷新会断开 SSE,后端默认 on_disconnect=cancel 取消该 run,不留孤儿)。
|
||||
@@ -470,16 +514,30 @@ export function AIWritingProvider({ children }: { children: ReactNode }) {
|
||||
})();
|
||||
}, [state.request]);
|
||||
|
||||
// 继续一条未完成的历史会话:把该会话设为活跃 sessionId → useStream 加载其 checkpoint →
|
||||
// host 的 rehydrate effect 重建已完成阶段并驱动续跑(停在确认点则显示确认卡)。
|
||||
// 与「刷新=重置」不冲突:这是显式点击触发,不是自动重连。
|
||||
const continueWriting = useCallback((sessionId: string) => {
|
||||
if (!sessionId) return;
|
||||
void streamRef.current?.stop();
|
||||
setArtifact(null);
|
||||
seenInterruptRef.current = null;
|
||||
continueTargetRef.current = sessionId;
|
||||
dispatch({ type: 'reset' });
|
||||
dispatch({ type: 'set_session', sessionId });
|
||||
}, [setArtifact]);
|
||||
|
||||
const reset = useCallback(() => {
|
||||
void streamRef.current?.stop();
|
||||
setArtifact(null);
|
||||
seenInterruptRef.current = null;
|
||||
continueTargetRef.current = null;
|
||||
dispatch({ type: 'reset' });
|
||||
}, [setArtifact]);
|
||||
|
||||
const value = useMemo(
|
||||
() => ({ state, startWriting, submitIntervention, pauseWriting, resumeWriting, reset }),
|
||||
[state, startWriting, submitIntervention, pauseWriting, resumeWriting, reset],
|
||||
() => ({ state, startWriting, submitIntervention, pauseWriting, resumeWriting, continueWriting, reset }),
|
||||
[state, startWriting, submitIntervention, pauseWriting, resumeWriting, continueWriting, reset],
|
||||
);
|
||||
|
||||
return (
|
||||
@@ -490,6 +548,7 @@ export function AIWritingProvider({ children }: { children: ReactNode }) {
|
||||
sessionIdRef={sessionIdRef}
|
||||
streamRef={streamRef}
|
||||
seenInterruptRef={seenInterruptRef}
|
||||
continueTargetRef={continueTargetRef}
|
||||
/>
|
||||
{children}
|
||||
</AIWritingContext.Provider>
|
||||
|
||||
Reference in new issue
Block a user