From 7276f50d9ef25868ae2037a77cdfc951fb85e05b Mon Sep 17 00:00:00 2001 From: canvrno <46584286+canvrno@users.noreply.github.com> Date: Mon, 14 Apr 2025 12:17:03 -0700 Subject: [PATCH] ENG-319/Add indicators when tools operate outside of workspace (#2836) * rebased/mergefix * one small remaining rebase fix * more fixes --------- Co-authored-by: Dennis Bartlett --- .changeset/soft-insects-wonder.md | 5 ++++ src/core/task/index.ts | 12 +++++++++- src/shared/ExtensionMessage.ts | 2 ++ src/utils/path.ts | 22 ++++++++++++++++++ webview-ui/src/components/chat/ChatRow.tsx | 27 +++++++++++++++++++--- 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 .changeset/soft-insects-wonder.md diff --git a/.changeset/soft-insects-wonder.md b/.changeset/soft-insects-wonder.md new file mode 100644 index 00000000..f3ff2401 --- /dev/null +++ b/.changeset/soft-insects-wonder.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Icons to indicate an action is occuring outside of the users workspace diff --git a/src/core/task/index.ts b/src/core/task/index.ts index c483c15a..11d2d5be 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -55,7 +55,7 @@ import { DEFAULT_LANGUAGE_SETTINGS, getLanguageKey, LanguageDisplay } from "../. import { ClineAskResponse, ClineCheckpointRestore } from "../../shared/WebviewMessage" import { calculateApiCostAnthropic } from "../../utils/cost" import { fileExistsAtPath } from "../../utils/fs" -import { arePathsEqual, getReadablePath } from "../../utils/path" +import { arePathsEqual, getReadablePath, isLocatedInWorkspace } from "../../utils/path" import { fixModelHtmlEscaping, removeInvalidChars } from "../../utils/string" import { AssistantMessageContent, parseAssistantMessage, ToolParamName, ToolUseName } from ".././assistant-message" import { constructNewFileContent } from ".././assistant-message/diff" @@ -1749,6 +1749,7 @@ export class Task { tool: fileExists ? "editedExistingFile" : "newFileCreated", path: getReadablePath(cwd, removeClosingTag("path", relPath)), content: diff || content, + operationIsLocatedInWorkspace: isLocatedInWorkspace(relPath), } if (block.partial) { @@ -1812,6 +1813,7 @@ export class Task { const completeMessage = JSON.stringify({ ...sharedMessageProps, content: diff || content, + operationIsLocatedInWorkspace: isLocatedInWorkspace(relPath), // ? formatResponse.createPrettyPatch( // relPath, // this.diffViewProvider.originalContent, @@ -1937,6 +1939,7 @@ export class Task { const partialMessage = JSON.stringify({ ...sharedMessageProps, content: undefined, + operationIsLocatedInWorkspace: isLocatedInWorkspace(relPath), } satisfies ClineSayTool) if (this.shouldAutoApproveToolWithPath(block.name, block.params.path)) { this.removeLastPartialMessageIfExistsWithType("ask", "tool") @@ -1967,6 +1970,7 @@ export class Task { const completeMessage = JSON.stringify({ ...sharedMessageProps, content: absolutePath, + operationIsLocatedInWorkspace: isLocatedInWorkspace(relPath), } satisfies ClineSayTool) if (this.shouldAutoApproveToolWithPath(block.name, block.params.path)) { this.removeLastPartialMessageIfExistsWithType("ask", "tool") @@ -2014,6 +2018,7 @@ export class Task { const partialMessage = JSON.stringify({ ...sharedMessageProps, content: "", + operationIsLocatedInWorkspace: isLocatedInWorkspace(block.params.path), } satisfies ClineSayTool) if (this.shouldAutoApproveToolWithPath(block.name, block.params.path)) { this.removeLastPartialMessageIfExistsWithType("ask", "tool") @@ -2045,6 +2050,7 @@ export class Task { const completeMessage = JSON.stringify({ ...sharedMessageProps, content: result, + operationIsLocatedInWorkspace: isLocatedInWorkspace(block.params.path), } satisfies ClineSayTool) if (this.shouldAutoApproveToolWithPath(block.name, block.params.path)) { this.removeLastPartialMessageIfExistsWithType("ask", "tool") @@ -2084,6 +2090,7 @@ export class Task { const partialMessage = JSON.stringify({ ...sharedMessageProps, content: "", + operationIsLocatedInWorkspace: isLocatedInWorkspace(block.params.path), } satisfies ClineSayTool) if (this.shouldAutoApproveToolWithPath(block.name, block.params.path)) { this.removeLastPartialMessageIfExistsWithType("ask", "tool") @@ -2112,6 +2119,7 @@ export class Task { const completeMessage = JSON.stringify({ ...sharedMessageProps, content: result, + operationIsLocatedInWorkspace: isLocatedInWorkspace(block.params.path), } satisfies ClineSayTool) if (this.shouldAutoApproveToolWithPath(block.name, block.params.path)) { this.removeLastPartialMessageIfExistsWithType("ask", "tool") @@ -2155,6 +2163,7 @@ export class Task { const partialMessage = JSON.stringify({ ...sharedMessageProps, content: "", + operationIsLocatedInWorkspace: isLocatedInWorkspace(block.params.path), } satisfies ClineSayTool) if (this.shouldAutoApproveToolWithPath(block.name, block.params.path)) { this.removeLastPartialMessageIfExistsWithType("ask", "tool") @@ -2191,6 +2200,7 @@ export class Task { const completeMessage = JSON.stringify({ ...sharedMessageProps, content: results, + operationIsLocatedInWorkspace: isLocatedInWorkspace(block.params.path), } satisfies ClineSayTool) if (this.shouldAutoApproveToolWithPath(block.name, block.params.path)) { this.removeLastPartialMessageIfExistsWithType("ask", "tool") diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index c0843138..85b8a637 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -154,6 +154,7 @@ export interface ClineMessage { partial?: boolean lastCheckpointHash?: string isCheckpointCheckedOut?: boolean + isOperationOutsideWorkspace?: boolean conversationHistoryIndex?: number conversationHistoryDeletedRange?: [number, number] // for when conversation history is truncated for API requests } @@ -215,6 +216,7 @@ export interface ClineSayTool { content?: string regex?: string filePattern?: string + operationIsLocatedInWorkspace?: boolean } // must keep in sync with system prompt diff --git a/src/utils/path.ts b/src/utils/path.ts index c7dacbd4..17b7ad70 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -1,6 +1,7 @@ import * as path from "path" import os from "os" import * as vscode from "vscode" +import { realpathSync } from "fs" /* The Node.js 'path' module resolves and normalizes paths differently depending on the platform: @@ -110,3 +111,24 @@ export const getWorkspacePath = (defaultCwdPath = "") => { } return cwdPath } + +export const isLocatedInWorkspace = (pathToCheck: string = ""): boolean => { + const workspacePath = getWorkspacePath() + + // Handle long paths in Windows + if (pathToCheck.startsWith("\\\\?\\") || workspacePath.startsWith("\\\\?\\")) { + return pathToCheck.startsWith(workspacePath) + } + + const resolvedPath = path.resolve(workspacePath, pathToCheck) + + // Using realpathSync to resolve any symbolic links + try { + const realWorkspacePath = realpathSync(workspacePath) + const realPath = realpathSync(resolvedPath) + return realPath.startsWith(realWorkspacePath) + } catch (error) { + console.error("Error resolving paths:", error) + return false + } +} diff --git a/webview-ui/src/components/chat/ChatRow.tsx b/webview-ui/src/components/chat/ChatRow.tsx index 20ade237..66d4ae9a 100644 --- a/webview-ui/src/components/chat/ChatRow.tsx +++ b/webview-ui/src/components/chat/ChatRow.tsx @@ -328,13 +328,20 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi }, [message.ask, message.say, message.text]) if (tool) { - const toolIcon = (name: string) => ( + const colorMap = { + red: "var(--vscode-errorForeground)", + yellow: "var(--vscode-editorWarning-foreground)", + green: "var(--vscode-charts-green)", + } + const toolIcon = (name: string, color?: string, rotation?: number, title?: string) => ( + transform: rotation ? `rotate(${rotation}deg)` : undefined, + }} + title={title}> ) switch (tool.tool) { @@ -343,6 +350,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi <>
{toolIcon("edit")} + {!tool.operationIsLocatedInWorkspace && + toolIcon("sign-out", "yellow", -90, "This file is outside of your workspace")} Cline wants to edit this file:
{toolIcon("new-file")} + {!tool.operationIsLocatedInWorkspace && + toolIcon("sign-out", "yellow", -90, "This file is outside of your workspace")} Cline wants to create a new file:
{toolIcon("file-code")} + {!tool.operationIsLocatedInWorkspace && + toolIcon("sign-out", "yellow", -90, "This file is outside of your workspace")} {/* {message.type === "ask" ? "" : "Cline read this file:"} */} Cline wants to read this file: @@ -433,6 +446,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi <>
{toolIcon("folder-opened")} + {!tool.operationIsLocatedInWorkspace && + toolIcon("sign-out", "yellow", -90, "This is outside of your workspace")} {message.type === "ask" ? "Cline wants to view the top level files in this directory:" @@ -453,6 +468,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi <>
{toolIcon("folder-opened")} + {!tool.operationIsLocatedInWorkspace && + toolIcon("sign-out", "yellow", -90, "This is outside of your workspace")} {message.type === "ask" ? "Cline wants to recursively view all files in this directory:" @@ -473,6 +490,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi <>
{toolIcon("file-code")} + {!tool.operationIsLocatedInWorkspace && + toolIcon("sign-out", "yellow", -90, "This is outside of your workspace")} {message.type === "ask" ? "Cline wants to view source code definition names used in this directory:" @@ -492,6 +511,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi <>
{toolIcon("search")} + {!tool.operationIsLocatedInWorkspace && + toolIcon("sign-out", "yellow", -90, "This is outside of your workspace")} Cline wants to search this directory for {tool.regex}: