sync: 鍚屾灞€鍩熺綉鏈€鏂颁唬鐮?(source master @ b69568e4)
This commit is contained in:
1 parent
4f7f07504a
commit
8bc8b0e082
1651 files changed
+278599
-11416
No files matched your search
@@ -1,13 +1,30 @@
|
||||
"use client";
|
||||
|
||||
import { createContext, useCallback, useContext, useMemo, useState } from "react";
|
||||
import { createContext, useCallback, useContext, useMemo, useRef, useState } from "react";
|
||||
|
||||
import type {
|
||||
DocumentRewriteStage,
|
||||
} from "@/open-canvas/hooks/useDocumentRewrite";
|
||||
import type { DocumentRewriteSummary } from "@/open-canvas/api/rewrite";
|
||||
import {
|
||||
cancelDurableDocumentRewrite,
|
||||
type DocumentRewriteDiagnostic,
|
||||
type DocumentRewriteSummary,
|
||||
} from "@/open-canvas/api/rewrite";
|
||||
import { createClientId } from "@/lib/client-id";
|
||||
|
||||
const ACTIVE_DOCUMENT_REWRITE_STAGES: DocumentRewriteStage[] = [
|
||||
"snapshot",
|
||||
"requirements",
|
||||
"writing",
|
||||
"illustrating",
|
||||
"validation",
|
||||
"committing",
|
||||
];
|
||||
|
||||
export function isDocumentRewriteActiveStage(stage: DocumentRewriteStage) {
|
||||
return ACTIVE_DOCUMENT_REWRITE_STAGES.includes(stage);
|
||||
}
|
||||
|
||||
export type DocumentRewritePresentation = {
|
||||
id: string;
|
||||
threadId: string;
|
||||
@@ -18,6 +35,8 @@ export type DocumentRewritePresentation = {
|
||||
id: string;
|
||||
path?: string;
|
||||
};
|
||||
/** Separate artifact created for this rewrite; the source file remains unchanged. */
|
||||
outputPath: string | null;
|
||||
fileName: string;
|
||||
instruction: string;
|
||||
style: string;
|
||||
@@ -39,6 +58,8 @@ export type DocumentRewritePresentation = {
|
||||
originalContent: string;
|
||||
rewrittenContent: string | null;
|
||||
versionId: string | null;
|
||||
/** Durable job identity. Enables cancellation after a page or sandbox remount. */
|
||||
jobId: 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. */
|
||||
@@ -47,6 +68,7 @@ export type DocumentRewritePresentation = {
|
||||
redoVersionId: string | null;
|
||||
revertedAt: string | null;
|
||||
error: string | null;
|
||||
diagnostic: DocumentRewriteDiagnostic | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
};
|
||||
@@ -56,6 +78,7 @@ type CreatePresentationInput = Omit<
|
||||
| "id"
|
||||
| "stage"
|
||||
| "operation"
|
||||
| "outputPath"
|
||||
| "generateImages"
|
||||
| "maxGeneratedImages"
|
||||
| "generatedImageCount"
|
||||
@@ -69,18 +92,22 @@ type CreatePresentationInput = Omit<
|
||||
| "originalContent"
|
||||
| "rewrittenContent"
|
||||
| "versionId"
|
||||
| "jobId"
|
||||
| "committedHash"
|
||||
| "localFallbackAvailable"
|
||||
| "redoVersionId"
|
||||
| "revertedAt"
|
||||
| "error"
|
||||
| "diagnostic"
|
||||
| "createdAt"
|
||||
| "updatedAt"
|
||||
> & {
|
||||
operation?: DocumentRewritePresentation["operation"];
|
||||
outputPath?: string | null;
|
||||
originalContent?: string;
|
||||
rewrittenContent?: string | null;
|
||||
committedHash?: string | null;
|
||||
jobId?: string | null;
|
||||
localFallbackAvailable?: boolean;
|
||||
requirementsPlan?: string;
|
||||
generateImages?: boolean;
|
||||
@@ -94,6 +121,19 @@ type DocumentRewritePresentationContextValue = {
|
||||
patch: Partial<Omit<DocumentRewritePresentation, "id" | "threadId" | "createdAt">>,
|
||||
) => void;
|
||||
forThread: (threadId: string) => DocumentRewritePresentation[];
|
||||
/**
|
||||
* Binds a live sandbox stream to its in-feed presentation. The shared
|
||||
* composer uses this before falling back to the durable cancellation API.
|
||||
*/
|
||||
registerCancellation: (
|
||||
presentationId: string,
|
||||
cancel: () => void | Promise<void>,
|
||||
) => () => void;
|
||||
/** Stop every active rewrite visible in this conversation. */
|
||||
cancelActiveForThread: (threadId: string) => Promise<{
|
||||
cancelled: boolean;
|
||||
error?: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
const DocumentRewritePresentationContext =
|
||||
@@ -109,6 +149,9 @@ export function DocumentRewritePresentationProvider({
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const [presentations, setPresentations] = useState<DocumentRewritePresentation[]>([]);
|
||||
const cancellationHandlersRef = useRef<
|
||||
Map<string, () => void | Promise<void>>
|
||||
>(new Map());
|
||||
|
||||
const create = useCallback((input: CreatePresentationInput) => {
|
||||
const id = createId();
|
||||
@@ -120,6 +163,7 @@ export function DocumentRewritePresentationProvider({
|
||||
id,
|
||||
stage: "snapshot",
|
||||
operation: input.operation ?? "rewrite",
|
||||
outputPath: input.outputPath ?? null,
|
||||
generateImages: input.generateImages === true,
|
||||
maxGeneratedImages: Math.min(Math.max(input.maxGeneratedImages ?? 2, 1), 4),
|
||||
generatedImageCount: 0,
|
||||
@@ -133,11 +177,13 @@ export function DocumentRewritePresentationProvider({
|
||||
originalContent: input.originalContent ?? "",
|
||||
rewrittenContent: input.rewrittenContent ?? null,
|
||||
versionId: null,
|
||||
jobId: input.jobId ?? null,
|
||||
committedHash: input.committedHash ?? null,
|
||||
localFallbackAvailable: input.localFallbackAvailable === true,
|
||||
redoVersionId: null,
|
||||
revertedAt: null,
|
||||
error: null,
|
||||
diagnostic: null,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
},
|
||||
@@ -172,14 +218,83 @@ export function DocumentRewritePresentationProvider({
|
||||
[],
|
||||
);
|
||||
|
||||
const registerCancellation = useCallback(
|
||||
(presentationId: string, cancel: () => void | Promise<void>) => {
|
||||
cancellationHandlersRef.current.set(presentationId, cancel);
|
||||
return () => {
|
||||
if (cancellationHandlersRef.current.get(presentationId) === cancel) {
|
||||
cancellationHandlersRef.current.delete(presentationId);
|
||||
}
|
||||
};
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const cancelActiveForThread = useCallback(
|
||||
async (threadId: string) => {
|
||||
const active = presentations.filter(
|
||||
(item) => item.threadId === threadId && isDocumentRewriteActiveStage(item.stage),
|
||||
);
|
||||
if (active.length === 0) return { cancelled: false };
|
||||
|
||||
const errors: string[] = [];
|
||||
let cancelled = false;
|
||||
await Promise.all(
|
||||
active.map(async (item) => {
|
||||
const cancel = cancellationHandlersRef.current.get(item.id);
|
||||
if (cancel) {
|
||||
try {
|
||||
await cancel();
|
||||
cancelled = true;
|
||||
} catch (error) {
|
||||
errors.push(error instanceof Error ? error.message : "停止全文改写失败");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
item.source.kind !== "artifact" ||
|
||||
!item.source.path ||
|
||||
!item.jobId
|
||||
) {
|
||||
errors.push("改写任务尚未就绪,请稍候再试。");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await cancelDurableDocumentRewrite(
|
||||
{ threadId: item.source.id, path: item.source.path },
|
||||
item.jobId,
|
||||
);
|
||||
update(item.id, {
|
||||
stage: "cancelled",
|
||||
error: "已停止全文改写,原文件未被修改。",
|
||||
});
|
||||
cancelled = true;
|
||||
} catch (error) {
|
||||
errors.push(error instanceof Error ? error.message : "停止全文改写失败");
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return {
|
||||
cancelled,
|
||||
...(errors.length > 0 ? { error: errors[0] } : {}),
|
||||
};
|
||||
},
|
||||
[presentations, update],
|
||||
);
|
||||
|
||||
const value = useMemo<DocumentRewritePresentationContextValue>(
|
||||
() => ({
|
||||
create,
|
||||
update,
|
||||
forThread: (threadId) =>
|
||||
presentations.filter((item) => item.threadId === threadId),
|
||||
registerCancellation,
|
||||
cancelActiveForThread,
|
||||
}),
|
||||
[create, presentations, update],
|
||||
[cancelActiveForThread, create, presentations, registerCancellation, update],
|
||||
);
|
||||
|
||||
return (
|
||||
|
||||
Reference in new issue
Block a user