feat: 深度研究支持独立新报告与原文改写,并完善沙箱、模型管理与全文改写

新报告不再覆盖当前文件卡片,写入时打开沙箱;步骤条仅在改写原文时位于卡片上方。同时补齐改写本地回退、模型管理与会商第三步相关改动。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
董宏凯andCursor committed 2026-08-21 21:46:04 +08:00
1 parent 11f928bc95
commit a5a9c952e8
63 files changed
+4113 -929

No files matched your search

@@ -11,6 +11,8 @@ import { createClientId } from "@/lib/client-id";
export type DocumentRewritePresentation = {
id: string;
threadId: string;
/** A report regeneration is a new document, not an edit of the current one. */
operation: "rewrite" | "generate";
source: {
kind: "artifact" | "deep_research_report";
id: string;
@@ -37,6 +39,10 @@ export type DocumentRewritePresentation = {
originalContent: string;
rewrittenContent: string | null;
versionId: string | null;
/** Hash of the currently committed rewrite, used by the local undo fallback. */
committedHash: string | null;
/** A browser-only undo backup is available because the server snapshot failed. */
localFallbackAvailable: boolean;
/** Inverse snapshot created by a safe undo; enables a one-click reapply. */
redoVersionId: string | null;
revertedAt: string | null;
@@ -49,6 +55,7 @@ type CreatePresentationInput = Omit<
DocumentRewritePresentation,
| "id"
| "stage"
| "operation"
| "generateImages"
| "maxGeneratedImages"
| "generatedImageCount"
@@ -62,14 +69,19 @@ type CreatePresentationInput = Omit<
| "originalContent"
| "rewrittenContent"
| "versionId"
| "committedHash"
| "localFallbackAvailable"
| "redoVersionId"
| "revertedAt"
| "error"
| "createdAt"
| "updatedAt"
> & {
operation?: DocumentRewritePresentation["operation"];
originalContent?: string;
rewrittenContent?: string | null;
committedHash?: string | null;
localFallbackAvailable?: boolean;
requirementsPlan?: string;
generateImages?: boolean;
maxGeneratedImages?: number;
@@ -107,6 +119,7 @@ export function DocumentRewritePresentationProvider({
...input,
id,
stage: "snapshot",
operation: input.operation ?? "rewrite",
generateImages: input.generateImages === true,
maxGeneratedImages: Math.min(Math.max(input.maxGeneratedImages ?? 2, 1), 4),
generatedImageCount: 0,
@@ -120,6 +133,8 @@ export function DocumentRewritePresentationProvider({
originalContent: input.originalContent ?? "",
rewrittenContent: input.rewrittenContent ?? null,
versionId: null,
committedHash: input.committedHash ?? null,
localFallbackAvailable: input.localFallbackAvailable === true,
redoVersionId: null,
revertedAt: null,
error: null,
@@ -180,3 +195,12 @@ export function useDocumentRewritePresentation() {
/** Browser event consumed by the open sandbox after an in-feed undo succeeds. */
export const DOCUMENT_REWRITE_UNDONE_EVENT = "deerflow:document-rewrite-undone";
/** Synchronous guard used before a browser-only undo writes back the original text. */
export const DOCUMENT_REWRITE_LOCAL_UNDO_GUARD_EVENT = "deerflow:document-rewrite-local-undo-guard";
/** Sent after the user confirms a rewrite so the conversation can reveal its new step card. */
export const DOCUMENT_REWRITE_STARTED_EVENT = "deerflow:document-rewrite-started";
/** A completed structural-regeneration card requests an in-place rewrite of that new report. */
export const DEEP_RESEARCH_REPORT_REWRITE_REQUESTED_EVENT = "deerflow:deep-research-report-rewrite-requested";
@@ -0,0 +1,133 @@
import { getAccountIdentityKey } from "@/core/auth";
import { authStorage } from "@/core/auth/scoped-storage";
const STORAGE_PREFIX = "deerflow.document-rewrite.local-fallback:v1";
// Keep the browser fallback comfortably below the typical 5 MB storage quota.
const MAX_BACKUP_CHARACTERS = 1_000_000;
type PendingArtifactRewriteFallback = {
threadId: string;
path: string;
originalContent: string;
committedHash: string | null;
createdAt: string;
};
export type ArtifactRewriteFallback = PendingArtifactRewriteFallback & {
committedHash: string;
};
function storageKey(threadId: string, path: string) {
const account = getAccountIdentityKey() ?? "anonymous";
return [STORAGE_PREFIX, account, threadId, path]
.map((part) => encodeURIComponent(part))
.join(":");
}
function getStorage(): Storage | null {
if (typeof window === "undefined") return null;
return authStorage();
}
function canPersist(originalContent: string): boolean {
if (originalContent.length <= MAX_BACKUP_CHARACTERS) return true;
console.error("AI 全文改写的本地撤销备份过大,未写入浏览器存储。");
return false;
}
/**
* Save the original text before the durable rewrite starts. It is completed
* with the rewritten file hash only if the server-side undo snapshot fails.
*/
export function primeArtifactRewriteFallback({
threadId,
path,
originalContent,
}: Omit<PendingArtifactRewriteFallback, "committedHash" | "createdAt">): void {
if (!canPersist(originalContent)) return;
try {
getStorage()?.setItem(
storageKey(threadId, path),
JSON.stringify({
threadId,
path,
originalContent,
committedHash: null,
createdAt: new Date().toISOString(),
} satisfies PendingArtifactRewriteFallback),
);
} catch (error) {
console.error("保存 AI 全文改写的本地撤销备份失败:", error);
}
}
/** Mark a primed backup as safe to use for the server snapshot failure path. */
export function completeArtifactRewriteFallback({
threadId,
path,
originalContent,
committedHash,
}: Omit<ArtifactRewriteFallback, "createdAt">): boolean {
if (!committedHash || !canPersist(originalContent)) return false;
try {
const storage = getStorage();
if (!storage) return false;
storage.setItem(
storageKey(threadId, path),
JSON.stringify({
threadId,
path,
originalContent,
committedHash,
createdAt: new Date().toISOString(),
} satisfies ArtifactRewriteFallback),
);
return true;
} catch (error) {
console.error("完成 AI 全文改写的本地撤销备份失败:", error);
return false;
}
}
export function loadArtifactRewriteFallback({
threadId,
path,
committedHash,
}: Pick<ArtifactRewriteFallback, "threadId" | "path" | "committedHash">): ArtifactRewriteFallback | null {
try {
const raw = getStorage()?.getItem(storageKey(threadId, path));
if (!raw) return null;
const parsed: unknown = JSON.parse(raw);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return null;
const fallback = parsed as Partial<ArtifactRewriteFallback>;
if (
fallback.threadId !== threadId ||
fallback.path !== path ||
fallback.committedHash !== committedHash ||
typeof fallback.originalContent !== "string"
) {
return null;
}
return fallback as ArtifactRewriteFallback;
} catch (error) {
console.error("读取 AI 全文改写的本地撤销备份失败:", error);
return null;
}
}
export function clearArtifactRewriteFallback(threadId: string, path: string): void {
try {
getStorage()?.removeItem(storageKey(threadId, path));
} catch (error) {
console.error("清理 AI 全文改写的本地撤销备份失败:", error);
}
}
export async function sha256DocumentContent(content: string): Promise<string | null> {
if (!globalThis.crypto?.subtle) return null;
const digest = await globalThis.crypto.subtle.digest(
"SHA-256",
new TextEncoder().encode(content),
);
return Array.from(new Uint8Array(digest), (byte) => byte.toString(16).padStart(2, "0")).join("");
}