diff --git a/.changeset/young-bulldogs-raise.md b/.changeset/young-bulldogs-raise.md new file mode 100644 index 00000000..bba9851e --- /dev/null +++ b/.changeset/young-bulldogs-raise.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Refactor task class, moving auto approve diff --git a/src/core/controller/state/updateAutoApprovalSettings.ts b/src/core/controller/state/updateAutoApprovalSettings.ts index cc38e230..cab4ca3c 100644 --- a/src/core/controller/state/updateAutoApprovalSettings.ts +++ b/src/core/controller/state/updateAutoApprovalSettings.ts @@ -22,7 +22,7 @@ export async function updateAutoApprovalSettings(controller: Controller, request await updateGlobalState(controller.context, "autoApprovalSettings", settings) if (controller.task) { - controller.task.autoApprovalSettings = settings + controller.task.updateAutoApprovalSettings(settings) } await controller.postStateToWebview() diff --git a/src/core/task/ToolExecutor.ts b/src/core/task/ToolExecutor.ts index c85059f8..6507b39e 100644 --- a/src/core/task/ToolExecutor.ts +++ b/src/core/task/ToolExecutor.ts @@ -11,6 +11,7 @@ import { ClineIgnoreController } from "@core/ignore/ClineIgnoreController" import { AutoApprovalSettings } from "@shared/AutoApprovalSettings" import { ChatSettings } from "@shared/ChatSettings" import { ToolParamName, ToolUse, ToolUseName } from "../assistant-message" +import { AutoApprove } from "./tools/autoApprove" import { BrowserAction, BrowserActionResult, @@ -57,6 +58,17 @@ import { setTimeout as setTimeoutPromise } from "node:timers/promises" import { ChangeLocation, StreamingJsonReplacer } from "../assistant-message/diff-json" export class ToolExecutor { + private autoApprover: AutoApprove + + // Auto-approval methods using the AutoApprove class + private shouldAutoApproveTool(toolName: ToolUseName): boolean | [boolean, boolean] { + return this.autoApprover.shouldAutoApproveTool(toolName) + } + + private shouldAutoApproveToolWithPath(blockname: ToolUseName, autoApproveActionpath: string | undefined): boolean { + return this.autoApprover.shouldAutoApproveToolWithPath(blockname, autoApproveActionpath) + } + constructor( // Core Services & Managers private context: vscode.ExtensionContext, @@ -96,13 +108,20 @@ export class ToolExecutor { private saveCheckpoint: (isAttemptCompletionMessage?: boolean) => Promise, private reinitExistingTaskFromId: (taskId: string) => Promise, private cancelTask: () => Promise, - private shouldAutoApproveTool: (toolName: ToolUseName) => boolean | [boolean, boolean], - private shouldAutoApproveToolWithPath: (blockname: ToolUseName, autoApproveActionpath: string | undefined) => boolean, private sayAndCreateMissingParamError: (toolName: ToolUseName, paramName: string, relPath?: string) => Promise, private removeLastPartialMessageIfExistsWithType: (type: "ask" | "say", askOrSay: ClineAsk | ClineSay) => Promise, private executeCommandTool: (command: string) => Promise<[boolean, any]>, private doesLatestTaskCompletionHaveNewChanges: () => Promise, - ) {} + ) { + this.autoApprover = new AutoApprove(autoApprovalSettings) + } + + /** + * Updates the auto approval settings + */ + public updateAutoApprovalSettings(settings: AutoApprovalSettings): void { + this.autoApprover.updateSettings(settings) + } private pushToolResult = (content: ToolResponse, block: ToolUse) => { const isNextGenModel = isClaude4ModelFamily(this.api) || isGemini2dot5ModelFamily(this.api) diff --git a/src/core/task/index.ts b/src/core/task/index.ts index b2566707..456db6ff 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -314,8 +314,6 @@ export class Task { this.saveCheckpoint.bind(this), this.reinitExistingTaskFromId.bind(this), this.cancelTask.bind(this), - this.shouldAutoApproveTool.bind(this), - this.shouldAutoApproveToolWithPath.bind(this), this.sayAndCreateMissingParamError.bind(this), this.removeLastPartialMessageIfExistsWithType.bind(this), this.executeCommandTool.bind(this), @@ -333,6 +331,13 @@ export class Task { return context } + /** + * Updates the auto approval settings for this task + */ + public updateAutoApprovalSettings(settings: AutoApprovalSettings): void { + this.toolExecutor.updateAutoApprovalSettings(settings) + } + async restoreCheckpoint(messageTs: number, restoreType: ClineCheckpointRestore, offset?: number) { const clineMessages = this.messageStateHandler.getClineMessages() const messageIndex = clineMessages.findIndex((m) => m.ts === messageTs) - (offset || 0) @@ -1554,69 +1559,6 @@ export class Task { } } - // Check if the tool should be auto-approved based on the settings - // Returns bool for most tools, and tuple for tools with nested settings - shouldAutoApproveTool(toolName: ToolUseName): boolean | [boolean, boolean] { - if (this.autoApprovalSettings.enabled) { - switch (toolName) { - case "read_file": - case "list_files": - case "list_code_definition_names": - case "search_files": - return [ - this.autoApprovalSettings.actions.readFiles, - this.autoApprovalSettings.actions.readFilesExternally ?? false, - ] - case "new_rule": - case "write_to_file": - case "replace_in_file": - return [ - this.autoApprovalSettings.actions.editFiles, - this.autoApprovalSettings.actions.editFilesExternally ?? false, - ] - case "execute_command": - return [ - this.autoApprovalSettings.actions.executeSafeCommands ?? false, - this.autoApprovalSettings.actions.executeAllCommands ?? false, - ] - case "browser_action": - return this.autoApprovalSettings.actions.useBrowser - case "web_fetch": - return this.autoApprovalSettings.actions.useBrowser - case "access_mcp_resource": - case "use_mcp_tool": - return this.autoApprovalSettings.actions.useMcp - } - } - return false - } - - // Check if the tool should be auto-approved based on the settings - // and the path of the action. Returns true if the tool should be auto-approved - // based on the user's settings and the path of the action. - shouldAutoApproveToolWithPath(blockname: ToolUseName, autoApproveActionpath: string | undefined): boolean { - let isLocalRead: boolean = false - if (autoApproveActionpath) { - const absolutePath = path.resolve(cwd, autoApproveActionpath) - isLocalRead = absolutePath.startsWith(cwd) - } else { - // If we do not get a path for some reason, default to a (safer) false return - isLocalRead = false - } - - // Get auto-approve settings for local and external edits - const autoApproveResult = this.shouldAutoApproveTool(blockname) - const [autoApproveLocal, autoApproveExternal] = Array.isArray(autoApproveResult) - ? autoApproveResult - : [autoApproveResult, false] - - if ((isLocalRead && autoApproveLocal) || (!isLocalRead && autoApproveLocal && autoApproveExternal)) { - return true - } else { - return false - } - } - /** * Migrates the disableBrowserTool setting from VSCode configuration to browserSettings */ diff --git a/src/core/task/tools/autoApprove.ts b/src/core/task/tools/autoApprove.ts new file mode 100644 index 00000000..8411a4ae --- /dev/null +++ b/src/core/task/tools/autoApprove.ts @@ -0,0 +1,83 @@ +import { AutoApprovalSettings } from "@shared/AutoApprovalSettings" +import { ToolUseName } from "@core/assistant-message" +import * as path from "path" +import * as vscode from "vscode" +import os from "os" + +export const cwd = + vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") + +export class AutoApprove { + autoApprovalSettings: AutoApprovalSettings + + constructor(autoApprovalSettings: AutoApprovalSettings) { + this.autoApprovalSettings = autoApprovalSettings + } + + // Check if the tool should be auto-approved based on the settings + // Returns bool for most tools, and tuple for tools with nested settings + shouldAutoApproveTool(toolName: ToolUseName): boolean | [boolean, boolean] { + if (this.autoApprovalSettings.enabled) { + switch (toolName) { + case "read_file": + case "list_files": + case "list_code_definition_names": + case "search_files": + return [ + this.autoApprovalSettings.actions.readFiles, + this.autoApprovalSettings.actions.readFilesExternally ?? false, + ] + case "new_rule": + case "write_to_file": + case "replace_in_file": + return [ + this.autoApprovalSettings.actions.editFiles, + this.autoApprovalSettings.actions.editFilesExternally ?? false, + ] + case "execute_command": + return [ + this.autoApprovalSettings.actions.executeSafeCommands ?? false, + this.autoApprovalSettings.actions.executeAllCommands ?? false, + ] + case "browser_action": + return this.autoApprovalSettings.actions.useBrowser + case "web_fetch": + return this.autoApprovalSettings.actions.useBrowser + case "access_mcp_resource": + case "use_mcp_tool": + return this.autoApprovalSettings.actions.useMcp + } + } + return false + } + + // Check if the tool should be auto-approved based on the settings + // and the path of the action. Returns true if the tool should be auto-approved + // based on the user's settings and the path of the action. + shouldAutoApproveToolWithPath(blockname: ToolUseName, autoApproveActionpath: string | undefined): boolean { + let isLocalRead: boolean = false + if (autoApproveActionpath) { + const absolutePath = path.resolve(cwd, autoApproveActionpath) + isLocalRead = absolutePath.startsWith(cwd) + } else { + // If we do not get a path for some reason, default to a (safer) false return + isLocalRead = false + } + + // Get auto-approve settings for local and external edits + const autoApproveResult = this.shouldAutoApproveTool(blockname) + const [autoApproveLocal, autoApproveExternal] = Array.isArray(autoApproveResult) + ? autoApproveResult + : [autoApproveResult, false] + + if ((isLocalRead && autoApproveLocal) || (!isLocalRead && autoApproveLocal && autoApproveExternal)) { + return true + } else { + return false + } + } + + updateSettings(settings: AutoApprovalSettings): void { + this.autoApprovalSettings = settings + } +}