feat(ai写作): 左栏智能体 Tab 内嵌问答并优化模块侧栏
AI 写作页默认展示智能体 Tab,在页内复用 ai-writing 模块配置与 AgentChatPage;确认类型后侧栏扁平展示类型名并支持整块收起动画。同步历史回看、流式续跑、改写菜单与 researcher 检索相关改动。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
c11307aa67
commit
5de74e90dc
21 files changed
+1379
-386
No files matched your search
@@ -26,6 +26,8 @@ import { saveAIWritingTranscript } from '../api/ai-writing-sessions';
|
||||
import { countArticleWords } from '../utils/wordCount';
|
||||
import {
|
||||
INITIAL_STATE,
|
||||
buildAwaitUserEventFromCheckpointPause,
|
||||
buildCompletedInterventionFromCheckpointPause,
|
||||
buildRehydrateSnapshot,
|
||||
customToProgressEvent,
|
||||
interruptToEvent,
|
||||
@@ -96,14 +98,104 @@ type AIWritingStreamApi = Pick<
|
||||
'submit' | 'stop' | 'interrupt'
|
||||
>;
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> {
|
||||
return value && typeof value === 'object' ? value as Record<string, unknown> : {};
|
||||
}
|
||||
|
||||
function pausePointFromStatus(status: string): string | null {
|
||||
return status.startsWith('awaiting_') ? status.slice('awaiting_'.length) : null;
|
||||
}
|
||||
|
||||
function defaultResumePayloadForPause(
|
||||
pausePoint: string | null | undefined,
|
||||
interruptValue: unknown,
|
||||
values: AIWritingValues,
|
||||
): Record<string, unknown> {
|
||||
const intr = asRecord(interruptValue);
|
||||
const valueRecord = asRecord(values);
|
||||
|
||||
if (pausePoint === 'material_confirm') {
|
||||
return { action: 'confirm' };
|
||||
}
|
||||
|
||||
if (pausePoint === 'outline_confirm') {
|
||||
return { action: 'confirm' };
|
||||
}
|
||||
|
||||
if (pausePoint === 'section_help') {
|
||||
const blocked = Array.isArray(intr.blocked_sections)
|
||||
? intr.blocked_sections
|
||||
: (Array.isArray(valueRecord.blocked_sections) ? valueRecord.blocked_sections : []);
|
||||
const decisions = blocked
|
||||
.map((b) => asRecord(b))
|
||||
.map((b) => String(b.section_title ?? b.sectionTitle ?? ''))
|
||||
.filter(Boolean)
|
||||
.map((sectionTitle) => ({ sectionTitle, decision: 'loose' }));
|
||||
return { action: 'section_help', sectionDecisions: decisions };
|
||||
}
|
||||
|
||||
if (pausePoint === 'draft_confirm') {
|
||||
const review = asRecord(valueRecord.review_result);
|
||||
const hasReview = Object.keys(review).length > 0;
|
||||
const revisionCount = Number(valueRecord.revision_count ?? 0);
|
||||
const maxRevisions = Number(valueRecord.max_revisions ?? 3);
|
||||
if (!hasReview) return { action: 'to_editor' };
|
||||
if (review.verdict === 'pass') return { action: 'finalize' };
|
||||
if (revisionCount >= maxRevisions) return { action: 'finalize' };
|
||||
return { action: 'to_editor' };
|
||||
}
|
||||
|
||||
if (pausePoint === 'review_confirm') {
|
||||
return { action: 'accept_review' };
|
||||
}
|
||||
|
||||
return { action: 'confirm' };
|
||||
}
|
||||
|
||||
function nextStatusAfterResumePayload(
|
||||
pausePoint: string | null | undefined,
|
||||
payload: Record<string, unknown>,
|
||||
): WritingStatus {
|
||||
const action = String(payload.action ?? 'confirm');
|
||||
|
||||
if (pausePoint === 'material_confirm') {
|
||||
return (action === 're_search' || action === 'enable_skill_search')
|
||||
? 'researching'
|
||||
: 'writing_outline';
|
||||
}
|
||||
|
||||
if (pausePoint === 'outline_confirm') {
|
||||
return action === 're_outline' ? 'writing_outline' : 'writing_draft';
|
||||
}
|
||||
|
||||
if (pausePoint === 'draft_confirm') {
|
||||
if (action === 'finalize') return 'done';
|
||||
if (action === 'to_editor') return 'reviewing';
|
||||
return 'revising';
|
||||
}
|
||||
|
||||
if (pausePoint === 'review_confirm') {
|
||||
return action === 'force_finalize' ? 'done' : 'revising';
|
||||
}
|
||||
|
||||
if (pausePoint === 'section_help') {
|
||||
const decisions = Array.isArray(payload.sectionDecisions) ? payload.sectionDecisions : [];
|
||||
return decisions.some((d) => asRecord(d).decision === 'supplement')
|
||||
? 'researching'
|
||||
: 'writing_draft';
|
||||
}
|
||||
|
||||
return 'researching';
|
||||
}
|
||||
|
||||
export type AIWritingApi = {
|
||||
state: AIWritingState;
|
||||
startWriting: (request: AIWritingRequest) => Promise<void>;
|
||||
submitIntervention: (payload: UserInterventionPayload) => Promise<void>;
|
||||
pauseWriting: () => void;
|
||||
resumeWriting: () => void;
|
||||
/** 继续一条未完成的历史会话:加载其 checkpoint 重建已完成阶段,并从断点续跑当前阶段
|
||||
* (停在确认点则显示确认卡)。多 worker 安全(基于共享 checkpoint,同 4 个确认卡机制)。 */
|
||||
/** Restore an unfinished history session: rehydrate completed stages from checkpoint,
|
||||
* auto-advance completed awaiting_* stages, and re-run the active unfinished stage. */
|
||||
continueWriting: (sessionId: string) => void;
|
||||
reset: () => void;
|
||||
};
|
||||
@@ -144,7 +236,7 @@ const AIWritingStreamHost = memo(function AIWritingStreamHost({
|
||||
sessionIdRef: RefObject<string | null>;
|
||||
streamRef: MutableRefObject<AIWritingStreamApi | null>;
|
||||
seenInterruptRef: MutableRefObject<string | null>;
|
||||
/** 非空 = 本次要"继续未完成的历史会话",等 checkpoint values 到位后 rehydrate + 驱动续跑。 */
|
||||
/** Non-empty while an explicit history restore is waiting for checkpoint values. */
|
||||
continueTargetRef: MutableRefObject<string | null>;
|
||||
}) {
|
||||
const eventBatcherRef = useRef<ReturnType<typeof createProgressEventBatcher> | null>(null);
|
||||
@@ -240,16 +332,18 @@ const AIWritingStreamHost = memo(function AIWritingStreamHost({
|
||||
}
|
||||
const key = intr.id ?? JSON.stringify(intr.value);
|
||||
if (seenInterruptRef.current === key) return;
|
||||
if (continueTargetRef.current && continueTargetRef.current === threadId) {
|
||||
seenInterruptRef.current = key;
|
||||
return;
|
||||
}
|
||||
seenInterruptRef.current = key;
|
||||
const ev = interruptToEvent(intr.value, sessionIdRef.current ?? '');
|
||||
if (ev) emitProgressEvent(ev);
|
||||
}, [stream.interrupt, emitProgressEvent, seenInterruptRef, sessionIdRef]);
|
||||
}, [stream.interrupt, emitProgressEvent, seenInterruptRef, sessionIdRef, continueTargetRef, threadId]);
|
||||
|
||||
// ── 继续未完成的历史会话:等该 thread 的 checkpoint values 到位后 rehydrate + 驱动续跑 ──
|
||||
// 仅在显式点「继续」时触发(continueTargetRef 由 continueWriting 置位),非自动重连。
|
||||
// 每个 thread 只做一次:rehydrate 用 checkpoint 全量重建已完成阶段(素材/大纲/草稿/审核/时间线),
|
||||
// 然后——停在确认点(awaiting_*)就显示确认卡等用户决策;停在某活跃阶段中途就发空 command:{}
|
||||
// 让后端从 checkpoint 续跑该阶段(worker 空 command → astream(None))。
|
||||
// History restore is explicit: after the history detail "restore" button is clicked,
|
||||
// rehydrate from checkpoint, auto-advance completed awaiting_* stages with default
|
||||
// decisions, and re-run the currently active unfinished stage with an empty command.
|
||||
const rehydratedForRef = useRef<string | null>(null);
|
||||
useEffect(() => {
|
||||
const target = continueTargetRef.current;
|
||||
@@ -262,21 +356,35 @@ const AIWritingStreamHost = memo(function AIWritingStreamHost({
|
||||
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 续跑该阶段。
|
||||
const checkpointModelName = typeof values.model_name === 'string' ? values.model_name : '';
|
||||
const submitFromCheckpoint = (command: Record<string, unknown>) => {
|
||||
void streamRef.current?.submit(undefined, {
|
||||
command: {},
|
||||
command,
|
||||
streamMode: [...WRITING_STREAM_MODE],
|
||||
...(checkpointModelName ? { config: { configurable: { model_name: checkpointModelName } } } : {}),
|
||||
}).catch(() => { /* 流式错误由 onError 反映 */ });
|
||||
};
|
||||
if (status.startsWith('awaiting_')) {
|
||||
const intr = stream.interrupt;
|
||||
if (intr) seenInterruptRef.current = intr.id ?? JSON.stringify(intr.value);
|
||||
const pausePoint = (asRecord(intr?.value).pause_point as string | undefined) ?? pausePointFromStatus(status);
|
||||
const resumePayload = defaultResumePayloadForPause(pausePoint, intr?.value, values);
|
||||
const pauseEvent = buildAwaitUserEventFromCheckpointPause(values, threadId, pausePoint, intr?.value);
|
||||
if (pauseEvent) {
|
||||
dispatchRef.current?.({ type: 'handle', event: pauseEvent });
|
||||
}
|
||||
const record = buildCompletedInterventionFromCheckpointPause(values, pausePoint, resumePayload.action);
|
||||
if (record) {
|
||||
dispatchRef.current?.({
|
||||
type: 'intervention_submitted',
|
||||
record,
|
||||
nextStatus: nextStatusAfterResumePayload(pausePoint, resumePayload),
|
||||
pendingRevisionContext: null,
|
||||
});
|
||||
}
|
||||
submitFromCheckpoint({ resume: resumePayload });
|
||||
} else {
|
||||
submitFromCheckpoint({});
|
||||
}
|
||||
}, [stream.values, stream.interrupt, threadId, continueTargetRef, dispatchRef, seenInterruptRef, streamRef]);
|
||||
|
||||
@@ -294,7 +402,7 @@ 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。
|
||||
// Explicit history restore target; the host rehydrates once this thread's checkpoint arrives.
|
||||
const continueTargetRef = useRef<string | null>(null);
|
||||
|
||||
// 刷新页面 = 重置:不持久化、不自动重连。provider 重新挂载即回到 idle 初始态
|
||||
@@ -514,8 +622,8 @@ export function AIWritingProvider({ children }: { children: ReactNode }) {
|
||||
})();
|
||||
}, [state.request]);
|
||||
|
||||
// 继续一条未完成的历史会话:把该会话设为活跃 sessionId → useStream 加载其 checkpoint →
|
||||
// host 的 rehydrate effect 重建已完成阶段并驱动续跑(停在确认点则显示确认卡)。
|
||||
// Restore an unfinished history session: make it active, load its checkpoint, then let
|
||||
// the host skip completed awaiting_* stages or re-run the current unfinished stage.
|
||||
// 与「刷新=重置」不冲突:这是显式点击触发,不是自动重连。
|
||||
const continueWriting = useCallback((sessionId: string) => {
|
||||
if (!sessionId) return;
|
||||
|
||||
Reference in new issue
Block a user