* Handle switching to a new mode, including updating the associated API configuration * @param newMode The mode to switch to
(newMode: Mode)
| 1417 | * @param newMode The mode to switch to |
| 1418 | */ |
| 1419 | public async handleModeSwitch(newMode: Mode) { |
| 1420 | const task = this.getCurrentTask() |
| 1421 | |
| 1422 | if (task) { |
| 1423 | TelemetryService.instance.captureModeSwitch(task.taskId, newMode) |
| 1424 | task.emit(RooCodeEventName.TaskModeSwitched, task.taskId, newMode) |
| 1425 | |
| 1426 | try { |
| 1427 | // Update the task history with the new mode first. |
| 1428 | const taskHistoryItem = |
| 1429 | this.taskHistoryStore.get(task.taskId) ?? |
| 1430 | (this.getGlobalState("taskHistory") ?? []).find((item) => item.id === task.taskId) |
| 1431 | |
| 1432 | if (taskHistoryItem) { |
| 1433 | await this.updateTaskHistory({ ...taskHistoryItem, mode: newMode }) |
| 1434 | } |
| 1435 | |
| 1436 | // Only update the task's mode after successful persistence. |
| 1437 | ;(task as any)._taskMode = newMode |
| 1438 | } catch (error) { |
| 1439 | // If persistence fails, log the error but don't update the in-memory state. |
| 1440 | this.log( |
| 1441 | `Failed to persist mode switch for task ${task.taskId}: ${error instanceof Error ? error.message : String(error)}`, |
| 1442 | ) |
| 1443 | |
| 1444 | // Optionally, we could emit an event to notify about the failure. |
| 1445 | // This ensures the in-memory state remains consistent with persisted state. |
| 1446 | throw error |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | await this.updateGlobalState("mode", newMode) |
| 1451 | |
| 1452 | this.emit(RooCodeEventName.ModeChanged, newMode) |
| 1453 | |
| 1454 | // If workspace lock is on, keep the current API config — don't load mode-specific config |
| 1455 | const lockApiConfigAcrossModes = this.context.workspaceState.get("lockApiConfigAcrossModes", false) |
| 1456 | if (lockApiConfigAcrossModes) { |
| 1457 | await this.postStateToWebview() |
| 1458 | return |
| 1459 | } |
| 1460 | |
| 1461 | // Load the saved API config for the new mode if it exists. |
| 1462 | const savedConfigId = await this.providerSettingsManager.getModeConfigId(newMode) |
| 1463 | const listApiConfig = await this.providerSettingsManager.listConfig() |
| 1464 | |
| 1465 | // Update listApiConfigMeta first to ensure UI has latest data. |
| 1466 | await this.updateGlobalState("listApiConfigMeta", listApiConfig) |
| 1467 | |
| 1468 | // If this mode has a saved config, use it. |
| 1469 | if (savedConfigId) { |
| 1470 | const profile = listApiConfig.find(({ id }) => id === savedConfigId) |
| 1471 | |
| 1472 | if (profile?.name) { |
| 1473 | // Check if the profile has actual API configuration (not just an id). |
| 1474 | // In CLI mode, the ProviderSettingsManager may return empty default profiles |
| 1475 | // that only contain 'id' and 'name' fields. Activating such a profile would |
| 1476 | // overwrite the CLI's working API configuration with empty settings. |
no test coverage detected