( options: CreateWorkspaceOptions )
| 1416 | * Shared between /new command and NewWorkspaceModal |
| 1417 | */ |
| 1418 | export async function createNewWorkspace( |
| 1419 | options: CreateWorkspaceOptions |
| 1420 | ): Promise<CreateWorkspaceResult> { |
| 1421 | // Get recommended trunk if not provided |
| 1422 | let effectiveTrunk = options.trunkBranch; |
| 1423 | if (!effectiveTrunk) { |
| 1424 | const { recommendedTrunk } = await options.client.projects.listBranches({ |
| 1425 | projectPath: options.projectPath, |
| 1426 | }); |
| 1427 | effectiveTrunk = recommendedTrunk ?? "main"; |
| 1428 | } |
| 1429 | |
| 1430 | // Use saved default runtime preference if not explicitly provided |
| 1431 | let effectiveRuntime = options.runtime; |
| 1432 | if (effectiveRuntime === undefined) { |
| 1433 | const runtimeKey = getRuntimeKey(options.projectPath); |
| 1434 | const savedRuntime = localStorage.getItem(runtimeKey); |
| 1435 | if (savedRuntime) { |
| 1436 | effectiveRuntime = savedRuntime; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | // Parse runtime config if provided. |
| 1441 | const runtimeConfig = parseRuntimeString(effectiveRuntime); |
| 1442 | |
| 1443 | const result = await options.client.workspace.create({ |
| 1444 | projectPath: options.projectPath, |
| 1445 | branchName: options.workspaceName, |
| 1446 | trunkBranch: effectiveTrunk, |
| 1447 | runtimeConfig, |
| 1448 | pendingAutoTitle: options.pendingAutoTitle, |
| 1449 | }); |
| 1450 | |
| 1451 | if (!result.success) { |
| 1452 | return { success: false, error: result.error ?? "Failed to create workspace" }; |
| 1453 | } |
| 1454 | |
| 1455 | // Get workspace info for switching |
| 1456 | const workspaceInfo = await options.client.workspace.getInfo({ workspaceId: result.metadata.id }); |
| 1457 | if (!workspaceInfo) { |
| 1458 | return { success: false, error: "Failed to get workspace info after creation" }; |
| 1459 | } |
| 1460 | |
| 1461 | // Dispatch event to switch workspace |
| 1462 | dispatchWorkspaceSwitch(workspaceInfo); |
| 1463 | |
| 1464 | // If there's a start message, defer until React finishes rendering and WorkspaceStore subscribes |
| 1465 | const startMessage = options.startMessage; |
| 1466 | const sendMessageOptions = options.sendMessageOptions; |
| 1467 | const client = options.client; |
| 1468 | if (startMessage && sendMessageOptions) { |
| 1469 | requestAnimationFrame(() => { |
| 1470 | client.workspace |
| 1471 | .sendMessage({ |
| 1472 | workspaceId: result.metadata.id, |
| 1473 | message: startMessage, |
| 1474 | options: sendMessageOptions, |
| 1475 | }) |
no test coverage detected