Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Preserve create state on full request failure (#567)

Abi Rajaa2ab9734bfa1
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 21b9dd88..125ab223 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -199,7 +199,7 @@ function App() {
wsRef.current?.close?.(USER_CLOSE_WEB_SOCKET_CODE);
};
- // Used for code generation failure as well
+ // Used for user-initiated cancellation and failed edit rollbacks
const cancelCodeGenerationAndReset = (commit: Commit) => {
// When the current commit is the first version, reset the entire app state
if (commit.type === "ai_create") {
@@ -299,6 +299,18 @@ function App() {
delete lastToolEventIdRef.current[variantIndex];
};
+ const finishInFlightEvents = (status: "complete" | "error") => {
+ Object.keys(lastThinkingEventIdRef.current).forEach((key) => {
+ finishThinkingEvent(Number(key), status);
+ });
+ Object.keys(lastAssistantEventIdRef.current).forEach((key) => {
+ finishAssistantEvent(Number(key), status);
+ });
+ Object.keys(lastToolEventIdRef.current).forEach((key) => {
+ finishToolEvent(Number(key), status);
+ });
+ };
+
generateCode(wsRef, updatedParams, {
onChange: (token, variantIndex) => {
appendCommitCode(commit.hash, variantIndex, token);
@@ -414,19 +426,31 @@ function App() {
delete lastToolEventIdRef.current[variantIndex];
}
},
- onCancel: () => {
+ onCancel: (reason, errorMessage) => {
+ // Close any running agent events when the socket ends without per-event
+ // terminal messages, otherwise they remain stuck in "running" state.
+ finishInFlightEvents(reason === "request_failed" ? "error" : "complete");
+
+ if (reason === "request_failed" && commit.type === "ai_create") {
+ const latestCreateCommit = useProjectStore.getState().commits[commit.hash];
+ latestCreateCommit?.variants.forEach((variant, variantIndex) => {
+ if (variant.status === "generating") {
+ updateVariantStatus(
+ commit.hash,
+ variantIndex,
+ "error",
+ errorMessage || "Generation failed. Please retry."
+ );
+ }
+ });
+ setAppState(AppState.CODE_READY);
+ return;
+ }
+
cancelCodeGenerationAndReset(commit);
},
onComplete: () => {
- Object.keys(lastThinkingEventIdRef.current).forEach((key) => {
- finishThinkingEvent(Number(key), "complete");
- });
- Object.keys(lastAssistantEventIdRef.current).forEach((key) => {
- finishAssistantEvent(Number(key), "complete");
- });
- Object.keys(lastToolEventIdRef.current).forEach((key) => {
- finishToolEvent(Number(key), "complete");
- });
+ finishInFlightEvents("complete");
setAppState(AppState.CODE_READY);
},
});
diff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsx
index e115f406..ac0b6a83 100644
--- a/frontend/src/components/sidebar/Sidebar.tsx
+++ b/frontend/src/components/sidebar/Sidebar.tsx
@@ -395,8 +395,18 @@ function Sidebar({
<AgentActivity />
)}
- {/* Regenerate button for first generation */}
- {isFirstGeneration && head === latestCommitHash && (appState === AppState.CODE_READY || isSelectedVariantComplete) && !isSelectedVariantError && (
+ {/* Regenerate button for first generation.
+ Scenarios:
+ 1) `appState === CODE_READY`: request fully ended and user can retry.
+ 2) `isSelectedVariantComplete`: selected option completed even if app state
+ has not yet fully transitioned.
+ 3) `isSelectedVariantError`: selected option failed; keep retry visible so
+ users can rerun create without losing uploaded inputs. */}
+ {isFirstGeneration &&
+ head === latestCommitHash &&
+ (appState === AppState.CODE_READY ||
+ isSelectedVariantComplete ||
+ isSelectedVariantError) && (
<div className="flex justify-end mb-3">
<button
onClick={regenerate}
@@ -444,7 +454,11 @@ function Sidebar({
)}
</div>
)}
- <div>Switch to another option above to make updates.</div>
+ <div>
+ {isFirstGeneration
+ ? "Click Retry to run the create request again."
+ : "Switch to another option above to make updates."}
+ </div>
</div>
</div>
)}
diff --git a/frontend/src/generateCode.ts b/frontend/src/generateCode.ts
index e1d641d6..50033fd1 100644
--- a/frontend/src/generateCode.ts
+++ b/frontend/src/generateCode.ts
@@ -43,7 +43,10 @@ interface CodeGenerationCallbacks {
onAssistant: (content: string, variantIndex: number, eventId?: string) => void;
onToolStart: (data: any, variantIndex: number, eventId?: string) => void;
onToolResult: (data: any, variantIndex: number, eventId?: string) => void;
- onCancel: () => void;
+ onCancel: (
+ reason: "user_cancelled" | "request_failed" | "connection_error",
+ errorMessage?: string
+ ) => void;
onComplete: () => void;
}
@@ -96,14 +99,14 @@ export function generateCode(
console.log("Connection closed", event.code, event.reason);
if (event.code === USER_CLOSE_WEB_SOCKET_CODE) {
toast.success(CANCEL_MESSAGE);
- callbacks.onCancel();
+ callbacks.onCancel("user_cancelled");
} else if (event.code === APP_ERROR_WEB_SOCKET_CODE) {
console.error("Known server error", event);
- callbacks.onCancel();
+ callbacks.onCancel("request_failed", event.reason || ERROR_MESSAGE);
} else if (event.code !== 1000) {
console.error("Unknown server or connection error", event);
toast.error(ERROR_MESSAGE);
- callbacks.onCancel();
+ callbacks.onCancel("connection_error", event.reason || ERROR_MESSAGE);
} else {
callbacks.onComplete();
}