Skip to content

Commit a4b0ad3

Browse files
committed
feat: make task mode sticky across sessions
- Add mode field to HistoryItem schema to persist selected mode - Update taskMetadata to save current mode when creating history items - Update handleModeSwitch to save mode changes to task history for active tasks - Restore saved mode when reopening tasks from history This ensures that when a task is reopened, it automatically switches to the last mode that was active on that task.
1 parent a824557 commit a4b0ad3

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

packages/types/src/history.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const historyItemSchema = z.object({
1616
totalCost: z.number(),
1717
size: z.number().optional(),
1818
workspace: z.string().optional(),
19+
mode: z.string().optional(),
1920
})
2021

2122
export type HistoryItem = z.infer<typeof historyItemSchema>

src/core/task-persistence/taskMetadata.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type TaskMetadataOptions = {
1818
taskNumber: number
1919
globalStoragePath: string
2020
workspace: string
21+
mode?: string
2122
}
2223

2324
export async function taskMetadata({
@@ -26,6 +27,7 @@ export async function taskMetadata({
2627
taskNumber,
2728
globalStoragePath,
2829
workspace,
30+
mode,
2931
}: TaskMetadataOptions) {
3032
const taskDir = await getTaskDirectoryPath(globalStoragePath, taskId)
3133

@@ -92,6 +94,7 @@ export async function taskMetadata({
9294
totalCost: tokenUsage.totalCost,
9395
size: taskDirSize,
9496
workspace,
97+
mode,
9598
}
9699

97100
return { historyItem, tokenUsage }

src/core/task/Task.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,17 @@ export class Task extends EventEmitter<ClineEvents> {
405405
globalStoragePath: this.globalStoragePath,
406406
})
407407

408+
const provider = this.providerRef.deref()
409+
const state = await provider?.getState()
410+
const currentMode = state?.mode
411+
408412
const { historyItem, tokenUsage } = await taskMetadata({
409413
messages: this.clineMessages,
410414
taskId: this.taskId,
411415
taskNumber: this.taskNumber,
412416
globalStoragePath: this.globalStoragePath,
413417
workspace: this.cwd,
418+
mode: currentMode,
414419
})
415420

416421
this.emit("taskTokenUsageUpdated", this.taskId, tokenUsage)

src/core/webview/ClineProvider.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,11 @@ export class ClineProvider
578578
public async initClineWithHistoryItem(historyItem: HistoryItem & { rootTask?: Task; parentTask?: Task }) {
579579
await this.removeClineFromStack()
580580

581+
// If the history item has a saved mode, restore it
582+
if (historyItem.mode) {
583+
await this.updateGlobalState("mode", historyItem.mode)
584+
}
585+
581586
const {
582587
apiConfiguration,
583588
diffEnabled: enableDiff,
@@ -807,6 +812,14 @@ export class ClineProvider
807812
if (cline) {
808813
TelemetryService.instance.captureModeSwitch(cline.taskId, newMode)
809814
cline.emit("taskModeSwitched", cline.taskId, newMode)
815+
816+
// Update the task history with the new mode
817+
const history = this.getGlobalState("taskHistory") ?? []
818+
const taskHistoryItem = history.find((item) => item.id === cline.taskId)
819+
if (taskHistoryItem) {
820+
taskHistoryItem.mode = newMode
821+
await this.updateTaskHistory(taskHistoryItem)
822+
}
810823
}
811824

812825
await this.updateGlobalState("mode", newMode)

0 commit comments

Comments
 (0)