Skip to content

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

Python

Fix variant timers and collapse

Abi Rajae40a71a4e263
diff --git a/frontend/src/components/agent/AgentActivity.tsx b/frontend/src/components/agent/AgentActivity.tsx
index 03c5d2ab..ec906cb7 100644
--- a/frontend/src/components/agent/AgentActivity.tsx
+++ b/frontend/src/components/agent/AgentActivity.tsx
@@ -69,23 +69,21 @@ function formatDuration(startedAt?: number, endedAt?: number): string {
return formatDurationMs(endedAt - startedAt);
}
-function formatTotalDuration(events: AgentEvent[]): string {
- if (events.length === 0) return "";
- const now = Date.now();
- const totalMs = events.reduce((acc, event) => {
- if (!isFiniteNumber(event.startedAt)) return acc;
- const end = isFiniteNumber(event.endedAt) ? event.endedAt : now;
- const duration = Math.max(0, end - event.startedAt);
- return acc + duration;
- }, 0);
- return totalMs > 0 ? formatDurationMs(totalMs) : "";
-}
-
function formatElapsedSince(timestampMs: number | undefined, nowMs: number): string {
if (!isFiniteNumber(timestampMs)) return "";
return formatDurationMs(Math.max(0, nowMs - timestampMs));
}
+function formatVariantWallClockDuration(
+ requestStartedAt: number | undefined,
+ completedAt: number | undefined,
+ nowMs: number
+): string {
+ if (!isFiniteNumber(requestStartedAt)) return "";
+ const end = isFiniteNumber(completedAt) ? completedAt : nowMs;
+ return formatDurationMs(Math.max(0, end - requestStartedAt));
+}
+
function getEventIcon(type: AgentEventType, toolName?: string) {
if (type === "thinking") {
@@ -443,11 +441,11 @@ function AgentActivity() {
const lastAssistantId = [...events]
.reverse()
.find((event) => event.type === "assistant")?.id;
- const totalDuration = formatTotalDuration(events);
- const requestStartMs = currentCommit?.dateCreated
- ? new Date(currentCommit.dateCreated).getTime()
- : undefined;
- const runningDuration = formatElapsedSince(requestStartMs, nowMs);
+ const requestStartMs =
+ selectedVariant?.requestStartedAt ??
+ (currentCommit?.dateCreated
+ ? new Date(currentCommit.dateCreated).getTime()
+ : undefined);
const isLatestCommit = head === latestCommitHash;
if (!isLatestCommit || events.length === 0) {
@@ -458,6 +456,12 @@ function AgentActivity() {
selectedVariantStatus === "complete" ||
selectedVariantStatus === "error" ||
selectedVariantStatus === "cancelled";
+ const runningDuration = formatElapsedSince(requestStartMs, nowMs);
+ const variantDuration = formatVariantWallClockDuration(
+ requestStartMs,
+ selectedVariant?.completedAt,
+ nowMs
+ );
const stepsExpanded = variantUiKey
? Boolean(stepsExpandedByVariant[variantUiKey])
: false;
@@ -484,7 +488,7 @@ function AgentActivity() {
<BsChevronRight className="text-gray-400 text-xs" />
)}
<span className="text-xs text-gray-500 dark:text-gray-400">
- Worked through {stepEvents.length} step{stepEvents.length !== 1 ? "s" : ""}{totalDuration ? ` in ${totalDuration}` : ""}
+ Worked through {stepEvents.length} step{stepEvents.length !== 1 ? "s" : ""}{variantDuration ? ` in ${variantDuration}` : ""}
</span>
</button>
{stepsExpanded && (
diff --git a/frontend/src/components/commits/types.ts b/frontend/src/components/commits/types.ts
index 353623c2..f95450f8 100644
--- a/frontend/src/components/commits/types.ts
+++ b/frontend/src/components/commits/types.ts
@@ -29,6 +29,8 @@ export type VariantHistoryMessage = {
export type Variant = {
code: string;
history: VariantHistoryMessage[];
+ requestStartedAt?: number;
+ completedAt?: number;
status?: VariantStatus;
errorMessage?: string;
thinking?: string;
diff --git a/frontend/src/components/sidebar/Sidebar.tsx b/frontend/src/components/sidebar/Sidebar.tsx
index b1797a1a..35e09a33 100644
--- a/frontend/src/components/sidebar/Sidebar.tsx
+++ b/frontend/src/components/sidebar/Sidebar.tsx
@@ -92,9 +92,11 @@ function Sidebar({
const selectedVariantEvents = selectedVariant?.agentEvents ?? [];
const showWorkingIndicator =
appState === AppState.CODING && selectedVariantEvents.length === 0;
- const requestStartMs = currentCommit?.dateCreated
- ? new Date(currentCommit.dateCreated).getTime()
- : undefined;
+ const requestStartMs =
+ selectedVariant?.requestStartedAt ??
+ (currentCommit?.dateCreated
+ ? new Date(currentCommit.dateCreated).getTime()
+ : undefined);
const elapsedSeconds = requestStartMs
? Math.max(1, Math.round((nowMs - requestStartMs) / 1000))
: undefined;
diff --git a/frontend/src/store/project-store.ts b/frontend/src/store/project-store.ts
index 5054136a..275e4abf 100644
--- a/frontend/src/store/project-store.ts
+++ b/frontend/src/store/project-store.ts
@@ -108,12 +108,15 @@ export const useProjectStore = create<ProjectStore>((set) => ({
latestCommitHash: null,
addCommit: (commit: Commit) => {
+ const requestStartedAt = new Date(commit.dateCreated).getTime();
// Initialize variant statuses as 'generating' and start thinking timer
const commitsWithStatus = {
...commit,
variants: commit.variants.map((variant) => ({
...variant,
history: variant.history || [],
+ requestStartedAt:
+ variant.requestStartedAt ?? requestStartedAt,
status: variant.status || ("generating" as VariantStatus),
thinkingStartTime: Date.now(),
agentEvents: [],
@@ -281,7 +284,15 @@ export const useProjectStore = create<ProjectStore>((set) => ({
...commit,
variants: commit.variants.map((variant, index) =>
index === numVariant
- ? { ...variant, status, errorMessage: status === 'error' ? errorMessage : undefined }
+ ? {
+ ...variant,
+ status,
+ completedAt:
+ status === "generating"
+ ? undefined
+ : variant.completedAt ?? Date.now(),
+ errorMessage: status === "error" ? errorMessage : undefined,
+ }
: variant
),
},
@@ -295,6 +306,7 @@ export const useProjectStore = create<ProjectStore>((set) => ({
// Resize variants array to match backend count
const currentVariants = commit.variants;
+ const requestStartedAt = new Date(commit.dateCreated).getTime();
const seedHistory = currentVariants[0]?.history || [];
const newVariants = Array(count).fill(null).map((_, index) =>
currentVariants[index] || {
@@ -304,6 +316,7 @@ export const useProjectStore = create<ProjectStore>((set) => ({
imageAssetIds: [...message.imageAssetIds],
videoAssetIds: [...message.videoAssetIds],
})),
+ requestStartedAt,
status: "generating" as VariantStatus,
agentEvents: [],
}