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 <bartlett.dc.1@gmail.com>
This commit is contained in:
canvrnoandDennis Bartlett authored and GitHub committed 2025-04-14 12:17:03 -07:00
1 parent 2964388585
commit 7276f50d9e
5 files changed
+64 -4

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"claude-dev": patch
---
Icons to indicate an action is occuring outside of the users workspace
+11 -1
View File
@@ -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")
+2
View File
@@ -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
+22
View File
@@ -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
}
}
+24 -3
View File
@@ -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) => (
<span
className={`codicon codicon-${name}`}
style={{
color: "var(--vscode-foreground)",
color: color ? colorMap[color as keyof typeof colorMap] || color : "var(--vscode-foreground)",
marginBottom: "-1.5px",
}}></span>
transform: rotation ? `rotate(${rotation}deg)` : undefined,
}}
title={title}></span>
)
switch (tool.tool) {
@@ -343,6 +350,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi
<>
<div style={headerStyle}>
{toolIcon("edit")}
{!tool.operationIsLocatedInWorkspace &&
toolIcon("sign-out", "yellow", -90, "This file is outside of your workspace")}
<span style={{ fontWeight: "bold" }}>Cline wants to edit this file:</span>
</div>
<CodeAccordian
@@ -359,6 +368,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi
<>
<div style={headerStyle}>
{toolIcon("new-file")}
{!tool.operationIsLocatedInWorkspace &&
toolIcon("sign-out", "yellow", -90, "This file is outside of your workspace")}
<span style={{ fontWeight: "bold" }}>Cline wants to create a new file:</span>
</div>
<CodeAccordian
@@ -375,6 +386,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi
<>
<div style={headerStyle}>
{toolIcon("file-code")}
{!tool.operationIsLocatedInWorkspace &&
toolIcon("sign-out", "yellow", -90, "This file is outside of your workspace")}
<span style={{ fontWeight: "bold" }}>
{/* {message.type === "ask" ? "" : "Cline read this file:"} */}
Cline wants to read this file:
@@ -433,6 +446,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi
<>
<div style={headerStyle}>
{toolIcon("folder-opened")}
{!tool.operationIsLocatedInWorkspace &&
toolIcon("sign-out", "yellow", -90, "This is outside of your workspace")}
<span style={{ fontWeight: "bold" }}>
{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
<>
<div style={headerStyle}>
{toolIcon("folder-opened")}
{!tool.operationIsLocatedInWorkspace &&
toolIcon("sign-out", "yellow", -90, "This is outside of your workspace")}
<span style={{ fontWeight: "bold" }}>
{message.type === "ask"
? "Cline wants to recursively view all files in this directory:"
@@ -473,6 +490,8 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi
<>
<div style={headerStyle}>
{toolIcon("file-code")}
{!tool.operationIsLocatedInWorkspace &&
toolIcon("sign-out", "yellow", -90, "This is outside of your workspace")}
<span style={{ fontWeight: "bold" }}>
{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
<>
<div style={headerStyle}>
{toolIcon("search")}
{!tool.operationIsLocatedInWorkspace &&
toolIcon("sign-out", "yellow", -90, "This is outside of your workspace")}
<span style={{ fontWeight: "bold" }}>
Cline wants to search this directory for <code>{tool.regex}</code>:
</span>