Skip to content

Commit 3340f43

Browse files
committed
🤖 feat: show pending workspace states in sidebar
- Add status?: 'creating' field to WorkspaceMetadata for pending workspaces - Backend emits pending metadata immediately before slow AI title generation - generatePlaceholderName() creates git-safe placeholder from user's message - Frontend shows shimmer on workspace name during creation - Disable selection/remove actions while workspace is being created - Clear pending state on error by emitting null metadata This provides immediate visual feedback when creating workspaces instead of the UI appearing frozen during title generation (2-5s).
1 parent d086f87 commit 3340f43

File tree

5 files changed

+126
-58
lines changed

5 files changed

+126
-58
lines changed

src/browser/components/WorkspaceListItem.tsx

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ const WorkspaceListItemInner: React.FC<WorkspaceListItemProps> = ({
4141
onToggleUnread,
4242
}) => {
4343
// Destructure metadata for convenience
44-
const { id: workspaceId, name: workspaceName, namedWorkspacePath } = metadata;
44+
const { id: workspaceId, name: workspaceName, namedWorkspacePath, status } = metadata;
45+
const isCreating = status === "creating";
4546
const gitStatus = useGitStatus(workspaceId);
4647

4748
// Get rename context
@@ -103,18 +104,23 @@ const WorkspaceListItemInner: React.FC<WorkspaceListItemProps> = ({
103104
<React.Fragment>
104105
<div
105106
className={cn(
106-
"py-1.5 pl-4 pr-2 cursor-pointer border-l-[3px] border-transparent transition-all duration-150 text-[13px] relative hover:bg-hover [&:hover_button]:opacity-100 flex gap-2",
107-
isSelected && "bg-hover border-l-blue-400"
107+
"py-1.5 pl-4 pr-2 border-l-[3px] border-transparent transition-all duration-150 text-[13px] relative flex gap-2",
108+
isCreating
109+
? "cursor-default opacity-70"
110+
: "cursor-pointer hover:bg-hover [&:hover_button]:opacity-100",
111+
isSelected && !isCreating && "bg-hover border-l-blue-400"
108112
)}
109-
onClick={() =>
113+
onClick={() => {
114+
if (isCreating) return; // Disable click while creating
110115
onSelectWorkspace({
111116
projectPath,
112117
projectName,
113118
namedWorkspacePath,
114119
workspaceId,
115-
})
116-
}
120+
});
121+
}}
117122
onKeyDown={(e) => {
123+
if (isCreating) return; // Disable keyboard while creating
118124
if (e.key === "Enter" || e.key === " ") {
119125
e.preventDefault();
120126
onSelectWorkspace({
@@ -126,9 +132,10 @@ const WorkspaceListItemInner: React.FC<WorkspaceListItemProps> = ({
126132
}
127133
}}
128134
role="button"
129-
tabIndex={0}
135+
tabIndex={isCreating ? -1 : 0}
130136
aria-current={isSelected ? "true" : undefined}
131-
aria-label={`Select workspace ${displayName}`}
137+
aria-label={isCreating ? `Creating workspace ${displayName}` : `Select workspace ${displayName}`}
138+
aria-disabled={isCreating}
132139
data-workspace-path={namedWorkspacePath}
133140
data-workspace-id={workspaceId}
134141
>
@@ -156,14 +163,18 @@ const WorkspaceListItemInner: React.FC<WorkspaceListItemProps> = ({
156163
/>
157164
) : (
158165
<span
159-
className="text-foreground -mx-1 min-w-0 flex-1 cursor-pointer truncate rounded-sm px-1 text-left text-[14px] transition-colors duration-200 hover:bg-white/5"
166+
className={cn(
167+
"text-foreground -mx-1 min-w-0 flex-1 truncate rounded-sm px-1 text-left text-[14px] transition-colors duration-200",
168+
!isCreating && "cursor-pointer hover:bg-white/5"
169+
)}
160170
onDoubleClick={(e) => {
171+
if (isCreating) return; // Disable rename while creating
161172
e.stopPropagation();
162173
startRenaming();
163174
}}
164-
title="Double-click to rename"
175+
title={isCreating ? "Creating workspace..." : "Double-click to rename"}
165176
>
166-
{canInterrupt ? (
177+
{canInterrupt || isCreating ? (
167178
<Shimmer className="w-full truncate" colorClass="var(--color-foreground)">
168179
{displayName}
169180
</Shimmer>
@@ -174,33 +185,39 @@ const WorkspaceListItemInner: React.FC<WorkspaceListItemProps> = ({
174185
)}
175186

176187
<div className="ml-auto flex items-center gap-1">
177-
<GitStatusIndicator
178-
gitStatus={gitStatus}
179-
workspaceId={workspaceId}
180-
tooltipPosition="right"
181-
/>
182-
183-
<TooltipWrapper inline>
184-
<button
185-
className="text-muted hover:text-foreground col-start-1 flex h-5 w-5 shrink-0 cursor-pointer items-center justify-center border-none bg-transparent p-0 text-base opacity-0 transition-all duration-200 hover:rounded-sm hover:bg-white/10"
186-
onClick={(e) => {
187-
e.stopPropagation();
188-
void onRemoveWorkspace(workspaceId, e.currentTarget);
189-
}}
190-
aria-label={`Remove workspace ${displayName}`}
191-
data-workspace-id={workspaceId}
192-
>
193-
×
194-
</button>
195-
<Tooltip className="tooltip" align="right">
196-
Remove workspace
197-
</Tooltip>
198-
</TooltipWrapper>
188+
{!isCreating && (
189+
<>
190+
<GitStatusIndicator
191+
gitStatus={gitStatus}
192+
workspaceId={workspaceId}
193+
tooltipPosition="right"
194+
/>
195+
196+
<TooltipWrapper inline>
197+
<button
198+
className="text-muted hover:text-foreground col-start-1 flex h-5 w-5 shrink-0 cursor-pointer items-center justify-center border-none bg-transparent p-0 text-base opacity-0 transition-all duration-200 hover:rounded-sm hover:bg-white/10"
199+
onClick={(e) => {
200+
e.stopPropagation();
201+
void onRemoveWorkspace(workspaceId, e.currentTarget);
202+
}}
203+
aria-label={`Remove workspace ${displayName}`}
204+
data-workspace-id={workspaceId}
205+
>
206+
×
207+
</button>
208+
<Tooltip className="tooltip" align="right">
209+
Remove workspace
210+
</Tooltip>
211+
</TooltipWrapper>
212+
</>
213+
)}
199214
</div>
200215
</div>
201-
<div className="min-w-0">
202-
<WorkspaceStatusIndicator workspaceId={workspaceId} />
203-
</div>
216+
{!isCreating && (
217+
<div className="min-w-0">
218+
<WorkspaceStatusIndicator workspaceId={workspaceId} />
219+
</div>
220+
)}
204221
</div>
205222
</div>
206223
{renameError && isEditing && (

src/common/types/workspace.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ export interface WorkspaceMetadata {
5454

5555
/** Runtime configuration for this workspace (always set, defaults to local on load) */
5656
runtimeConfig: RuntimeConfig;
57+
58+
/**
59+
* Workspace creation status. When 'creating', the workspace is being set up
60+
* (title generation, git operations). Undefined or absent means ready.
61+
* Pending workspaces are ephemeral (not persisted to config).
62+
*/
63+
status?: "creating";
5764
}
5865

5966
/**

src/node/services/agentSession.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { AIService } from "@/node/services/aiService";
88
import type { HistoryService } from "@/node/services/historyService";
99
import type { PartialService } from "@/node/services/partialService";
1010
import type { InitStateManager } from "@/node/services/initStateManager";
11-
import type { WorkspaceMetadata } from "@/common/types/workspace";
11+
import type { FrontendWorkspaceMetadata } from "@/common/types/workspace";
1212
import { DEFAULT_RUNTIME_CONFIG } from "@/common/constants/workspace";
1313
import type {
1414
WorkspaceChatMessage,
@@ -34,7 +34,7 @@ export interface AgentSessionChatEvent {
3434

3535
export interface AgentSessionMetadataEvent {
3636
workspaceId: string;
37-
metadata: WorkspaceMetadata | null;
37+
metadata: FrontendWorkspaceMetadata | null;
3838
}
3939

4040
interface AgentSessionOptions {
@@ -136,7 +136,7 @@ export class AgentSession {
136136
await this.emitHistoricalEvents(listener);
137137
}
138138

139-
emitMetadata(metadata: WorkspaceMetadata | null): void {
139+
emitMetadata(metadata: FrontendWorkspaceMetadata | null): void {
140140
this.assertNotDisposed("emitMetadata");
141141
this.emitter.emit("metadata-event", {
142142
workspaceId: this.workspaceId,
@@ -240,11 +240,12 @@ export class AgentSession {
240240
: PlatformPaths.basename(normalizedWorkspacePath) || "unknown";
241241
}
242242

243-
const metadata: WorkspaceMetadata = {
243+
const metadata: FrontendWorkspaceMetadata = {
244244
id: this.workspaceId,
245245
name: workspaceName,
246246
projectName: derivedProjectName,
247247
projectPath: derivedProjectPath,
248+
namedWorkspacePath: normalizedWorkspacePath,
248249
runtimeConfig: DEFAULT_RUNTIME_CONFIG,
249250
};
250251

src/node/services/ipcMain.ts

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import type {
2626
import { Ok, Err, type Result } from "@/common/types/result";
2727
import { validateWorkspaceName } from "@/common/utils/validation/workspaceValidation";
2828
import type {
29-
WorkspaceMetadata,
3029
FrontendWorkspaceMetadata,
3130
WorkspaceActivitySnapshot,
3231
} from "@/common/types/workspace";
@@ -44,7 +43,7 @@ import { PTYService } from "@/node/services/ptyService";
4443
import type { TerminalWindowManager } from "@/desktop/terminalWindowManager";
4544
import type { TerminalCreateParams, TerminalResizeParams } from "@/common/types/terminal";
4645
import { ExtensionMetadataService } from "@/node/services/ExtensionMetadataService";
47-
import { generateWorkspaceName } from "./workspaceTitleGenerator";
46+
import { generateWorkspaceName, generatePlaceholderName } from "./workspaceTitleGenerator";
4847
/**
4948
* IpcMain - Manages all IPC handlers and service coordination
5049
*
@@ -242,15 +241,44 @@ export class IpcMain {
242241
| { success: true; workspaceId: string; metadata: FrontendWorkspaceMetadata }
243242
| Result<void, SendMessageError>
244243
> {
244+
// Generate IDs and placeholder upfront for immediate UI feedback
245+
const workspaceId = this.config.generateStableId();
246+
const placeholderName = generatePlaceholderName(message);
247+
const projectName =
248+
projectPath.split("/").pop() ?? projectPath.split("\\").pop() ?? "unknown";
249+
const createdAt = new Date().toISOString();
250+
251+
// Prepare runtime config early for pending metadata
252+
const finalRuntimeConfig: RuntimeConfig = options.runtimeConfig ?? {
253+
type: "local",
254+
srcBaseDir: this.config.srcDir,
255+
};
256+
257+
// Create session and emit pending metadata IMMEDIATELY
258+
// This allows the sidebar to show the workspace while we do slow operations
259+
const session = this.getOrCreateSession(workspaceId);
260+
session.emitMetadata({
261+
id: workspaceId,
262+
name: placeholderName,
263+
projectName,
264+
projectPath,
265+
namedWorkspacePath: "", // Not yet created
266+
createdAt,
267+
runtimeConfig: finalRuntimeConfig,
268+
status: "creating",
269+
});
270+
245271
try {
246-
// 1. Generate workspace branch name using AI (use same model as message)
272+
// 1. Generate workspace branch name using AI (SLOW - but user sees pending state)
247273
let branchName: string;
248274
{
249275
const isErrLike = (v: unknown): v is { type: string } =>
250276
typeof v === "object" && v !== null && "type" in v;
251277
const nameResult = await generateWorkspaceName(message, options.model, this.aiService);
252278
if (!nameResult.success) {
253279
const err = nameResult.error;
280+
// Clear pending state on error
281+
session.emitMetadata(null);
254282
if (isErrLike(err)) {
255283
return Err(err);
256284
}
@@ -275,14 +303,7 @@ export class IpcMain {
275303
const recommendedTrunk =
276304
options.trunkBranch ?? (await detectDefaultTrunkBranch(projectPath, branches)) ?? "main";
277305

278-
// 3. Create workspace
279-
const finalRuntimeConfig: RuntimeConfig = options.runtimeConfig ?? {
280-
type: "local",
281-
srcBaseDir: this.config.srcDir,
282-
};
283-
284-
const workspaceId = this.config.generateStableId();
285-
306+
// 3. Resolve runtime paths
286307
let runtime;
287308
let resolvedSrcBaseDir: string;
288309
try {
@@ -299,14 +320,15 @@ export class IpcMain {
299320
}
300321
} catch (error) {
301322
const errorMsg = error instanceof Error ? error.message : String(error);
323+
// Clear pending state on error
324+
session.emitMetadata(null);
302325
return Err({ type: "unknown", raw: `Failed to prepare runtime: ${errorMsg}` });
303326
}
304327

305-
const session = this.getOrCreateSession(workspaceId);
306328
this.initStateManager.startInit(workspaceId, projectPath);
307-
308329
const initLogger = this.createInitLogger(workspaceId);
309330

331+
// 4. Create workspace with final name
310332
const createResult = await runtime.createWorkspace({
311333
projectPath,
312334
branchName,
@@ -316,18 +338,17 @@ export class IpcMain {
316338
});
317339

318340
if (!createResult.success || !createResult.workspacePath) {
341+
// Clear pending state on error
342+
session.emitMetadata(null);
319343
return Err({ type: "unknown", raw: createResult.error ?? "Failed to create workspace" });
320344
}
321345

322-
const projectName =
323-
projectPath.split("/").pop() ?? projectPath.split("\\").pop() ?? "unknown";
324-
325346
const metadata = {
326347
id: workspaceId,
327348
name: branchName,
328349
projectName,
329350
projectPath,
330-
createdAt: new Date().toISOString(),
351+
createdAt,
331352
};
332353

333354
await this.config.editConfig((config) => {
@@ -349,9 +370,12 @@ export class IpcMain {
349370
const allMetadata = await this.config.getAllWorkspaceMetadata();
350371
const completeMetadata = allMetadata.find((m) => m.id === workspaceId);
351372
if (!completeMetadata) {
373+
// Clear pending state on error
374+
session.emitMetadata(null);
352375
return Err({ type: "unknown", raw: "Failed to retrieve workspace metadata" });
353376
}
354377

378+
// Emit final metadata (no status = ready)
355379
session.emitMetadata(completeMetadata);
356380

357381
void runtime
@@ -380,6 +404,8 @@ export class IpcMain {
380404
} catch (error) {
381405
const errorMessage = error instanceof Error ? error.message : String(error);
382406
log.error("Unexpected error in createWorkspaceForFirstMessage:", error);
407+
// Clear pending state on error
408+
session.emitMetadata(null);
383409
return Err({ type: "unknown", raw: `Failed to create workspace: ${errorMessage}` });
384410
}
385411
}
@@ -933,11 +959,12 @@ export class IpcMain {
933959
}
934960

935961
// Initialize workspace metadata
936-
const metadata: WorkspaceMetadata = {
962+
const metadata: FrontendWorkspaceMetadata = {
937963
id: newWorkspaceId,
938964
name: newName,
939965
projectName,
940966
projectPath: foundProjectPath,
967+
namedWorkspacePath: runtime.getWorkspacePath(foundProjectPath, newName),
941968
createdAt: new Date().toISOString(),
942969
runtimeConfig: DEFAULT_RUNTIME_CONFIG,
943970
};

src/node/services/workspaceTitleGenerator.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,19 @@ function validateBranchName(name: string): string {
5757
.replace(/-+/g, "-")
5858
.substring(0, 50);
5959
}
60+
61+
/**
62+
* Generate a placeholder name from the user's message for immediate display
63+
* while the AI generates the real title. This is git-safe and human-readable.
64+
*/
65+
export function generatePlaceholderName(message: string): string {
66+
// Take first ~40 chars, sanitize for git branch name
67+
const truncated = message.slice(0, 40).trim();
68+
const sanitized = truncated
69+
.toLowerCase()
70+
.replace(/[^a-z0-9]+/g, "-")
71+
.replace(/^-+|-+$/g, "")
72+
.replace(/-+/g, "-")
73+
.substring(0, 30);
74+
return sanitized || "new-workspace";
75+
}

0 commit comments

Comments
 (0)