From bfe935e5f5949f5ca2ad209630188045b58799e8 Mon Sep 17 00:00:00 2001 From: Ikko Eltociear Ashimine Date: Tue, 28 Jan 2025 03:13:14 +0900 Subject: [PATCH 01/32] chore: update CodeBlock.tsx langauge -> language --- webview-ui/src/components/common/CodeBlock.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webview-ui/src/components/common/CodeBlock.tsx b/webview-ui/src/components/common/CodeBlock.tsx index b00f641d5d..bc5c1fcbcf 100644 --- a/webview-ui/src/components/common/CodeBlock.tsx +++ b/webview-ui/src/components/common/CodeBlock.tsx @@ -120,7 +120,7 @@ const CodeBlock = memo(({ source, forceWrap = false }: CodeBlockProps) => { if (!node.lang) { node.lang = "javascript" } else if (node.lang.includes(".")) { - // if the langauge is a file, get the extension + // if the language is a file, get the extension node.lang = node.lang.split(".").slice(-1)[0] } }) From cb23be634661882b154739a47f9d9137ba228fd7 Mon Sep 17 00:00:00 2001 From: Piotr Rogowski Date: Mon, 27 Jan 2025 21:27:24 +0100 Subject: [PATCH 02/32] Extend deepseek-r1 support --- src/api/providers/openai.ts | 38 +++++++------ src/api/providers/openrouter.ts | 6 +- src/api/transform/r1-format.ts | 98 +++++++++++++++++++++++++++++++++ src/core/Cline.ts | 4 ++ 4 files changed, 129 insertions(+), 17 deletions(-) create mode 100644 src/api/transform/r1-format.ts diff --git a/src/api/providers/openai.ts b/src/api/providers/openai.ts index c5e3ae9e48..1515620084 100644 --- a/src/api/providers/openai.ts +++ b/src/api/providers/openai.ts @@ -1,5 +1,6 @@ import { Anthropic } from "@anthropic-ai/sdk" import OpenAI, { AzureOpenAI } from "openai" + import { ApiHandlerOptions, azureOpenAiDefaultApiVersion, @@ -8,6 +9,7 @@ import { } from "../../shared/api" import { ApiHandler, SingleCompletionHandler } from "../index" import { convertToOpenAiMessages } from "../transform/openai-format" +import { convertToR1Format } from "../transform/r1-format" import { ApiStream } from "../transform/stream" export class OpenAiHandler implements ApiHandler, SingleCompletionHandler { @@ -16,7 +18,8 @@ export class OpenAiHandler implements ApiHandler, SingleCompletionHandler { constructor(options: ApiHandlerOptions) { this.options = options - // Azure API shape slightly differs from the core API shape: https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai + // Azure API shape slightly differs from the core API shape: + // https://github.com/openai/openai-node?tab=readme-ov-file#microsoft-azure-openai const urlHost = new URL(this.options.openAiBaseUrl ?? "").host if (urlHost === "azure.com" || urlHost.endsWith(".azure.com") || options.openAiUseAzure) { this.client = new AzureOpenAI({ @@ -38,7 +41,7 @@ export class OpenAiHandler implements ApiHandler, SingleCompletionHandler { const deepseekReasoner = modelId.includes("deepseek-reasoner") - if (!deepseekReasoner && (this.options.openAiStreamingEnabled ?? true)) { + if (this.options.openAiStreamingEnabled ?? true) { const systemMessage: OpenAI.Chat.ChatCompletionSystemMessageParam = { role: "system", content: systemPrompt, @@ -46,7 +49,9 @@ export class OpenAiHandler implements ApiHandler, SingleCompletionHandler { const requestOptions: OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming = { model: modelId, temperature: 0, - messages: [systemMessage, ...convertToOpenAiMessages(messages)], + messages: deepseekReasoner + ? convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) + : [systemMessage, ...convertToOpenAiMessages(messages)], stream: true as const, stream_options: { include_usage: true }, } @@ -64,6 +69,12 @@ export class OpenAiHandler implements ApiHandler, SingleCompletionHandler { text: delta.content, } } + if ("reasoning_content" in delta && delta.reasoning_content) { + yield { + type: "reasoning", + text: (delta.reasoning_content as string | undefined) || "", + } + } if (chunk.usage) { yield { type: "usage", @@ -73,24 +84,19 @@ export class OpenAiHandler implements ApiHandler, SingleCompletionHandler { } } } else { - let systemMessage: OpenAI.Chat.ChatCompletionUserMessageParam | OpenAI.Chat.ChatCompletionSystemMessageParam - // o1 for instance doesnt support streaming, non-1 temp, or system prompt - // deepseek reasoner supports system prompt - systemMessage = deepseekReasoner - ? { - role: "system", - content: systemPrompt, - } - : { - role: "user", - content: systemPrompt, - } + const systemMessage: OpenAI.Chat.ChatCompletionUserMessageParam = { + role: "user", + content: systemPrompt, + } const requestOptions: OpenAI.Chat.Completions.ChatCompletionCreateParamsNonStreaming = { model: modelId, - messages: [systemMessage, ...convertToOpenAiMessages(messages)], + messages: deepseekReasoner + ? convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) + : [systemMessage, ...convertToOpenAiMessages(messages)], } + const response = await this.client.chat.completions.create(requestOptions) yield { diff --git a/src/api/providers/openrouter.ts b/src/api/providers/openrouter.ts index 63aeb055b4..6922e40613 100644 --- a/src/api/providers/openrouter.ts +++ b/src/api/providers/openrouter.ts @@ -19,6 +19,7 @@ interface OpenRouterApiStreamUsageChunk extends ApiStreamUsageChunk { } import { SingleCompletionHandler } from ".." +import { convertToR1Format } from "../transform/r1-format" export class OpenRouterHandler implements ApiHandler, SingleCompletionHandler { private options: ApiHandlerOptions @@ -41,7 +42,7 @@ export class OpenRouterHandler implements ApiHandler, SingleCompletionHandler { messages: Anthropic.Messages.MessageParam[], ): AsyncGenerator { // Convert Anthropic messages to OpenAI format - const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ + let openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ { role: "system", content: systemPrompt }, ...convertToOpenAiMessages(messages), ] @@ -117,6 +118,9 @@ export class OpenRouterHandler implements ApiHandler, SingleCompletionHandler { case "deepseek/deepseek-r1": // Recommended temperature for DeepSeek reasoning models temperature = 0.6 + // DeepSeek highly recommends using user instead of system role + openAiMessages[0].role = "user" + openAiMessages = convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) } // https://openrouter.ai/docs/transforms diff --git a/src/api/transform/r1-format.ts b/src/api/transform/r1-format.ts new file mode 100644 index 0000000000..51a4b94dbc --- /dev/null +++ b/src/api/transform/r1-format.ts @@ -0,0 +1,98 @@ +import { Anthropic } from "@anthropic-ai/sdk" +import OpenAI from "openai" + +type ContentPartText = OpenAI.Chat.ChatCompletionContentPartText +type ContentPartImage = OpenAI.Chat.ChatCompletionContentPartImage +type UserMessage = OpenAI.Chat.ChatCompletionUserMessageParam +type AssistantMessage = OpenAI.Chat.ChatCompletionAssistantMessageParam +type Message = OpenAI.Chat.ChatCompletionMessageParam +type AnthropicMessage = Anthropic.Messages.MessageParam + +/** + * Converts Anthropic messages to OpenAI format while merging consecutive messages with the same role. + * This is required for DeepSeek Reasoner which does not support successive messages with the same role. + * + * @param messages Array of Anthropic messages + * @returns Array of OpenAI messages where consecutive messages with the same role are combined + */ +export function convertToR1Format(messages: AnthropicMessage[]): Message[] { + return messages.reduce((merged, message) => { + const lastMessage = merged[merged.length - 1] + let messageContent: string | (ContentPartText | ContentPartImage)[] = "" + let hasImages = false + + // Convert content to appropriate format + if (Array.isArray(message.content)) { + const textParts: string[] = [] + const imageParts: ContentPartImage[] = [] + + message.content.forEach((part) => { + if (part.type === "text") { + textParts.push(part.text) + } + if (part.type === "image") { + hasImages = true + imageParts.push({ + type: "image_url", + image_url: { url: `data:${part.source.media_type};base64,${part.source.data}` }, + }) + } + }) + + if (hasImages) { + const parts: (ContentPartText | ContentPartImage)[] = [] + if (textParts.length > 0) { + parts.push({ type: "text", text: textParts.join("\n") }) + } + parts.push(...imageParts) + messageContent = parts + } else { + messageContent = textParts.join("\n") + } + } else { + messageContent = message.content + } + + // If last message has same role, merge the content + if (lastMessage?.role === message.role) { + if (typeof lastMessage.content === "string" && typeof messageContent === "string") { + lastMessage.content += `\n${messageContent}` + } + // If either has image content, convert both to array format + else { + const lastContent = Array.isArray(lastMessage.content) + ? lastMessage.content + : [{ type: "text" as const, text: lastMessage.content || "" }] + + const newContent = Array.isArray(messageContent) + ? messageContent + : [{ type: "text" as const, text: messageContent }] + + if (message.role === "assistant") { + const mergedContent = [...lastContent, ...newContent] as AssistantMessage["content"] + lastMessage.content = mergedContent + } else { + const mergedContent = [...lastContent, ...newContent] as UserMessage["content"] + lastMessage.content = mergedContent + } + } + } else { + // Add as new message with the correct type based on role + if (message.role === "assistant") { + const newMessage: AssistantMessage = { + role: "assistant", + content: messageContent as AssistantMessage["content"], + } + merged.push(newMessage) + } else { + const newMessage: UserMessage = { + role: "user", + content: messageContent as UserMessage["content"], + } + merged.push(newMessage) + } + } + + return merged + }, []) +} diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 859d2d5484..afaa923f29 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -2391,6 +2391,10 @@ export class Cline { let reasoningMessage = "" try { for await (const chunk of stream) { + if (!chunk) { + // Sometimes chunk is undefined, no idea that can cause it, but this workaround seems to fix it + continue + } switch (chunk.type) { case "reasoning": reasoningMessage += chunk.text From e668169ed9136014b87721548c587c7939e2dafc Mon Sep 17 00:00:00 2001 From: MFPires Date: Mon, 27 Jan 2025 23:02:25 -0300 Subject: [PATCH 03/32] feat: Add conversation context token counter - Add contextTokens to ApiMetrics interface - Calculate context size using last API request's tokens - Display context token count in TaskHeader below total tokens - Use exact token counts instead of character estimation This helps users track the total size of their conversation context, which is useful for managing context window limits. --- src/shared/getApiMetrics.ts | 12 ++++++++++++ webview-ui/src/components/chat/ChatView.tsx | 1 + webview-ui/src/components/chat/TaskHeader.tsx | 9 +++++++++ 3 files changed, 22 insertions(+) diff --git a/src/shared/getApiMetrics.ts b/src/shared/getApiMetrics.ts index bd7b1bbce0..b4caadc385 100644 --- a/src/shared/getApiMetrics.ts +++ b/src/shared/getApiMetrics.ts @@ -6,6 +6,7 @@ interface ApiMetrics { totalCacheWrites?: number totalCacheReads?: number totalCost: number + contextTokens: number // Total tokens in conversation (last message's tokensIn + tokensOut) } /** @@ -32,8 +33,14 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics { totalCacheWrites: undefined, totalCacheReads: undefined, totalCost: 0, + contextTokens: 0, } + // Find the last api_req_started message to get the context size + const lastApiReq = [...messages] + .reverse() + .find((message) => message.type === "say" && message.say === "api_req_started" && message.text) + messages.forEach((message) => { if (message.type === "say" && message.say === "api_req_started" && message.text) { try { @@ -55,6 +62,11 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics { if (typeof cost === "number") { result.totalCost += cost } + + // If this is the last api request, use its tokens for context size + if (message === lastApiReq) { + result.contextTokens = (tokensIn || 0) + (tokensOut || 0) + } } catch (error) { console.error("Error parsing JSON:", error) } diff --git a/webview-ui/src/components/chat/ChatView.tsx b/webview-ui/src/components/chat/ChatView.tsx index da15b4e41f..7769f63056 100644 --- a/webview-ui/src/components/chat/ChatView.tsx +++ b/webview-ui/src/components/chat/ChatView.tsx @@ -915,6 +915,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie cacheWrites={apiMetrics.totalCacheWrites} cacheReads={apiMetrics.totalCacheReads} totalCost={apiMetrics.totalCost} + contextTokens={apiMetrics.contextTokens} onClose={handleTaskCloseButtonClick} /> ) : ( diff --git a/webview-ui/src/components/chat/TaskHeader.tsx b/webview-ui/src/components/chat/TaskHeader.tsx index 0b3f494b66..646dce364f 100644 --- a/webview-ui/src/components/chat/TaskHeader.tsx +++ b/webview-ui/src/components/chat/TaskHeader.tsx @@ -16,6 +16,7 @@ interface TaskHeaderProps { cacheWrites?: number cacheReads?: number totalCost: number + contextTokens: number onClose: () => void } @@ -27,6 +28,7 @@ const TaskHeader: React.FC = ({ cacheWrites, cacheReads, totalCost, + contextTokens, onClose, }) => { const { apiConfiguration } = useExtensionState() @@ -272,6 +274,13 @@ const TaskHeader: React.FC = ({ {!isCostAvailable && } +
+ Context: + + {formatLargeNumber(contextTokens || 0)} + +
+ {shouldShowPromptCacheInfo && (cacheReads !== undefined || cacheWrites !== undefined) && (
Cache: From 97fe13dcb163ae422b05bbdb691a81bb47093379 Mon Sep 17 00:00:00 2001 From: MFPires Date: Mon, 27 Jan 2025 23:12:01 -0300 Subject: [PATCH 04/32] fix: Prevent context token counter from resetting on failed API calls - Only update context tokens when both input and output tokens are non-zero - Keep previous context token count when API calls fail - Avoid resetting counter on partial or failed responses This makes the context token counter more resilient against edge cases and provides more accurate context size tracking during API failures. --- src/shared/getApiMetrics.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/shared/getApiMetrics.ts b/src/shared/getApiMetrics.ts index b4caadc385..0c8a61ed6d 100644 --- a/src/shared/getApiMetrics.ts +++ b/src/shared/getApiMetrics.ts @@ -65,7 +65,10 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics { // If this is the last api request, use its tokens for context size if (message === lastApiReq) { - result.contextTokens = (tokensIn || 0) + (tokensOut || 0) + // Only update context tokens if both input and output tokens are non-zero + if (tokensIn > 0 && tokensOut > 0) { + result.contextTokens = tokensIn + tokensOut + } } } catch (error) { console.error("Error parsing JSON:", error) From 5311e0c8abfb52fa76fc251d70b8809ebdac5904 Mon Sep 17 00:00:00 2001 From: MFPires Date: Tue, 28 Jan 2025 00:27:17 -0300 Subject: [PATCH 05/32] fix: Make context token counter more reliable - Only consider API requests with valid token information - Skip messages with invalid/missing token data - Prevent counter from resetting on action approval messages - Ensure both tokensIn and tokensOut are valid numbers This makes the context token counter more stable and accurate by only updating on valid API responses with complete token data. --- src/shared/getApiMetrics.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/shared/getApiMetrics.ts b/src/shared/getApiMetrics.ts index 0c8a61ed6d..882c4aa14b 100644 --- a/src/shared/getApiMetrics.ts +++ b/src/shared/getApiMetrics.ts @@ -36,10 +36,18 @@ export function getApiMetrics(messages: ClineMessage[]): ApiMetrics { contextTokens: 0, } - // Find the last api_req_started message to get the context size - const lastApiReq = [...messages] - .reverse() - .find((message) => message.type === "say" && message.say === "api_req_started" && message.text) + // Find the last api_req_started message that has valid token information + const lastApiReq = [...messages].reverse().find((message) => { + if (message.type === "say" && message.say === "api_req_started" && message.text) { + try { + const parsedData = JSON.parse(message.text) + return typeof parsedData.tokensIn === "number" && typeof parsedData.tokensOut === "number" + } catch { + return false + } + } + return false + }) messages.forEach((message) => { if (message.type === "say" && message.say === "api_req_started" && message.text) { From 1ba632fe89e3491714f7deee5310f8536ecc4b4a Mon Sep 17 00:00:00 2001 From: Murilo Pires <50873657+MuriloFP@users.noreply.github.com> Date: Tue, 28 Jan 2025 00:39:40 -0300 Subject: [PATCH 06/32] Update webview-ui/src/components/chat/TaskHeader.tsx Co-authored-by: Matt Rubens --- webview-ui/src/components/chat/TaskHeader.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webview-ui/src/components/chat/TaskHeader.tsx b/webview-ui/src/components/chat/TaskHeader.tsx index 646dce364f..52b3756a54 100644 --- a/webview-ui/src/components/chat/TaskHeader.tsx +++ b/webview-ui/src/components/chat/TaskHeader.tsx @@ -277,7 +277,7 @@ const TaskHeader: React.FC = ({
Context: - {formatLargeNumber(contextTokens || 0)} + {contextTokens ? formatLargeNumber(contextTokens) : '-'}
From b3be00c05011eb48867fea3b64cf17ed290a576b Mon Sep 17 00:00:00 2001 From: MFPires Date: Tue, 28 Jan 2025 01:20:19 -0300 Subject: [PATCH 07/32] feat: Add auto-approval for mode switching Implements automatic approval for mode switching operations when enabled, following existing auto-approval patterns in the codebase. Implementation: - Added `alwaysAllowModeSwitch` to state management - Updated `isAutoApproved` function in ChatView to handle mode switch requests - Added mode switch option to AutoApproveMenu with appropriate handler - Integrated with existing auto-approval flow Tests: - Added three test cases in ChatView.auto-approve.test.tsx: 1. Verifies mode switch auto-approval when enabled 2. Verifies no auto-approval when mode switch setting is disabled 3. Verifies no auto-approval when global auto-approval is disabled The implementation follows existing patterns for other auto-approve features (read, write, browser, etc.) to maintain consistency in the codebase. --- src/core/webview/ClineProvider.ts | 12 +- src/shared/ExtensionMessage.ts | 1 + src/shared/WebviewMessage.ts | 1 + .../src/components/chat/AutoApproveMenu.tsx | 16 ++ webview-ui/src/components/chat/ChatView.tsx | 7 +- .../__tests__/ChatView.auto-approve.test.tsx | 164 ++++++++++++++++++ .../src/context/ExtensionStateContext.tsx | 2 + 7 files changed, 201 insertions(+), 2 deletions(-) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index d532c27651..0f96df27eb 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -79,6 +79,8 @@ type GlobalStateKey = | "alwaysAllowWrite" | "alwaysAllowExecute" | "alwaysAllowBrowser" + | "alwaysAllowMcp" + | "alwaysAllowModeSwitch" | "taskHistory" | "openAiBaseUrl" | "openAiModelId" @@ -99,7 +101,6 @@ type GlobalStateKey = | "soundEnabled" | "soundVolume" | "diffEnabled" - | "alwaysAllowMcp" | "browserViewportSize" | "screenshotQuality" | "fuzzyMatchThreshold" @@ -620,6 +621,10 @@ export class ClineProvider implements vscode.WebviewViewProvider { await this.updateGlobalState("alwaysAllowMcp", message.bool) await this.postStateToWebview() break + case "alwaysAllowModeSwitch": + await this.updateGlobalState("alwaysAllowModeSwitch", message.bool) + await this.postStateToWebview() + break case "askResponse": this.cline?.handleWebviewAskResponse(message.askResponse!, message.text, message.images) break @@ -1848,6 +1853,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { alwaysAllowExecute, alwaysAllowBrowser, alwaysAllowMcp, + alwaysAllowModeSwitch, soundEnabled, diffEnabled, taskHistory, @@ -1882,6 +1888,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { alwaysAllowExecute: alwaysAllowExecute ?? false, alwaysAllowBrowser: alwaysAllowBrowser ?? false, alwaysAllowMcp: alwaysAllowMcp ?? false, + alwaysAllowModeSwitch: alwaysAllowModeSwitch ?? false, uriScheme: vscode.env.uriScheme, clineMessages: this.cline?.clineMessages || [], taskHistory: (taskHistory || []) @@ -2009,6 +2016,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { alwaysAllowExecute, alwaysAllowBrowser, alwaysAllowMcp, + alwaysAllowModeSwitch, taskHistory, allowedCommands, soundEnabled, @@ -2078,6 +2086,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { this.getGlobalState("alwaysAllowExecute") as Promise, this.getGlobalState("alwaysAllowBrowser") as Promise, this.getGlobalState("alwaysAllowMcp") as Promise, + this.getGlobalState("alwaysAllowModeSwitch") as Promise, this.getGlobalState("taskHistory") as Promise, this.getGlobalState("allowedCommands") as Promise, this.getGlobalState("soundEnabled") as Promise, @@ -2166,6 +2175,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { alwaysAllowExecute: alwaysAllowExecute ?? false, alwaysAllowBrowser: alwaysAllowBrowser ?? false, alwaysAllowMcp: alwaysAllowMcp ?? false, + alwaysAllowModeSwitch: alwaysAllowModeSwitch ?? false, taskHistory, allowedCommands, soundEnabled: soundEnabled ?? false, diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index e8f61b3a9c..56075b8676 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -91,6 +91,7 @@ export interface ExtensionState { alwaysAllowBrowser?: boolean alwaysAllowMcp?: boolean alwaysApproveResubmit?: boolean + alwaysAllowModeSwitch?: boolean requestDelaySeconds: number uriScheme?: string allowedCommands?: string[] diff --git a/src/shared/WebviewMessage.ts b/src/shared/WebviewMessage.ts index 113b23386f..a027a936a5 100644 --- a/src/shared/WebviewMessage.ts +++ b/src/shared/WebviewMessage.ts @@ -41,6 +41,7 @@ export interface WebviewMessage { | "refreshOpenAiModels" | "alwaysAllowBrowser" | "alwaysAllowMcp" + | "alwaysAllowModeSwitch" | "playSound" | "soundEnabled" | "soundVolume" diff --git a/webview-ui/src/components/chat/AutoApproveMenu.tsx b/webview-ui/src/components/chat/AutoApproveMenu.tsx index c317109af6..b6b5c9a4f7 100644 --- a/webview-ui/src/components/chat/AutoApproveMenu.tsx +++ b/webview-ui/src/components/chat/AutoApproveMenu.tsx @@ -28,6 +28,8 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => { setAlwaysAllowBrowser, alwaysAllowMcp, setAlwaysAllowMcp, + alwaysAllowModeSwitch, + setAlwaysAllowModeSwitch, alwaysApproveResubmit, setAlwaysApproveResubmit, autoApprovalEnabled, @@ -71,6 +73,13 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => { enabled: alwaysAllowMcp ?? false, description: "Allows use of configured MCP servers which may modify filesystem or interact with APIs.", }, + { + id: "switchModes", + label: "Switch between modes", + shortName: "Modes", + enabled: alwaysAllowModeSwitch ?? false, + description: "Allows automatic switching between different AI modes without requiring approval.", + }, { id: "retryRequests", label: "Retry failed requests", @@ -120,6 +129,12 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => { vscode.postMessage({ type: "alwaysAllowMcp", bool: newValue }) }, [alwaysAllowMcp, setAlwaysAllowMcp]) + const handleModeSwitchChange = useCallback(() => { + const newValue = !(alwaysAllowModeSwitch ?? false) + setAlwaysAllowModeSwitch(newValue) + vscode.postMessage({ type: "alwaysAllowModeSwitch", bool: newValue }) + }, [alwaysAllowModeSwitch, setAlwaysAllowModeSwitch]) + const handleRetryChange = useCallback(() => { const newValue = !(alwaysApproveResubmit ?? false) setAlwaysApproveResubmit(newValue) @@ -133,6 +148,7 @@ const AutoApproveMenu = ({ style }: AutoApproveMenuProps) => { executeCommands: handleExecuteChange, useBrowser: handleBrowserChange, useMcp: handleMcpChange, + switchModes: handleModeSwitchChange, retryRequests: handleRetryChange, } diff --git a/webview-ui/src/components/chat/ChatView.tsx b/webview-ui/src/components/chat/ChatView.tsx index da15b4e41f..59510cff55 100644 --- a/webview-ui/src/components/chat/ChatView.tsx +++ b/webview-ui/src/components/chat/ChatView.tsx @@ -55,6 +55,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie mode, setMode, autoApprovalEnabled, + alwaysAllowModeSwitch, } = useExtensionState() //const task = messages.length > 0 ? (messages[0].say === "task" ? messages[0] : undefined) : undefined) : undefined @@ -565,7 +566,10 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie (alwaysAllowReadOnly && message.ask === "tool" && isReadOnlyToolAction(message)) || (alwaysAllowWrite && message.ask === "tool" && isWriteToolAction(message)) || (alwaysAllowExecute && message.ask === "command" && isAllowedCommand(message)) || - (alwaysAllowMcp && message.ask === "use_mcp_server" && isMcpToolAlwaysAllowed(message)) + (alwaysAllowMcp && message.ask === "use_mcp_server" && isMcpToolAlwaysAllowed(message)) || + (alwaysAllowModeSwitch && + message.ask === "tool" && + JSON.parse(message.text || "{}")?.tool === "switchMode") ) }, [ @@ -579,6 +583,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie isAllowedCommand, alwaysAllowMcp, isMcpToolAlwaysAllowed, + alwaysAllowModeSwitch, ], ) diff --git a/webview-ui/src/components/chat/__tests__/ChatView.auto-approve.test.tsx b/webview-ui/src/components/chat/__tests__/ChatView.auto-approve.test.tsx index 6e720d92aa..f16e045383 100644 --- a/webview-ui/src/components/chat/__tests__/ChatView.auto-approve.test.tsx +++ b/webview-ui/src/components/chat/__tests__/ChatView.auto-approve.test.tsx @@ -313,4 +313,168 @@ describe("ChatView - Auto Approval Tests", () => { }) }) }) + + it("auto-approves mode switch when enabled", async () => { + render( + + {}} + showHistoryView={() => {}} + /> + , + ) + + // First hydrate state with initial task + mockPostMessage({ + alwaysAllowModeSwitch: true, + autoApprovalEnabled: true, + clineMessages: [ + { + type: "say", + say: "task", + ts: Date.now() - 2000, + text: "Initial task", + }, + ], + }) + + // Then send the mode switch ask message + mockPostMessage({ + alwaysAllowModeSwitch: true, + autoApprovalEnabled: true, + clineMessages: [ + { + type: "say", + say: "task", + ts: Date.now() - 2000, + text: "Initial task", + }, + { + type: "ask", + ask: "tool", + ts: Date.now(), + text: JSON.stringify({ tool: "switchMode" }), + partial: false, + }, + ], + }) + + // Wait for the auto-approval message + await waitFor(() => { + expect(vscode.postMessage).toHaveBeenCalledWith({ + type: "askResponse", + askResponse: "yesButtonClicked", + }) + }) + }) + + it("does not auto-approve mode switch when disabled", async () => { + render( + + {}} + showHistoryView={() => {}} + /> + , + ) + + // First hydrate state with initial task + mockPostMessage({ + alwaysAllowModeSwitch: false, + autoApprovalEnabled: true, + clineMessages: [ + { + type: "say", + say: "task", + ts: Date.now() - 2000, + text: "Initial task", + }, + ], + }) + + // Then send the mode switch ask message + mockPostMessage({ + alwaysAllowModeSwitch: false, + autoApprovalEnabled: true, + clineMessages: [ + { + type: "say", + say: "task", + ts: Date.now() - 2000, + text: "Initial task", + }, + { + type: "ask", + ask: "tool", + ts: Date.now(), + text: JSON.stringify({ tool: "switchMode" }), + partial: false, + }, + ], + }) + + // Verify no auto-approval message was sent + expect(vscode.postMessage).not.toHaveBeenCalledWith({ + type: "askResponse", + askResponse: "yesButtonClicked", + }) + }) + + it("does not auto-approve mode switch when auto-approval is disabled", async () => { + render( + + {}} + showHistoryView={() => {}} + /> + , + ) + + // First hydrate state with initial task + mockPostMessage({ + alwaysAllowModeSwitch: true, + autoApprovalEnabled: false, + clineMessages: [ + { + type: "say", + say: "task", + ts: Date.now() - 2000, + text: "Initial task", + }, + ], + }) + + // Then send the mode switch ask message + mockPostMessage({ + alwaysAllowModeSwitch: true, + autoApprovalEnabled: false, + clineMessages: [ + { + type: "say", + say: "task", + ts: Date.now() - 2000, + text: "Initial task", + }, + { + type: "ask", + ask: "tool", + ts: Date.now(), + text: JSON.stringify({ tool: "switchMode" }), + partial: false, + }, + ], + }) + + // Verify no auto-approval message was sent + expect(vscode.postMessage).not.toHaveBeenCalledWith({ + type: "askResponse", + askResponse: "yesButtonClicked", + }) + }) }) diff --git a/webview-ui/src/context/ExtensionStateContext.tsx b/webview-ui/src/context/ExtensionStateContext.tsx index 2d9fda0517..7d0159d840 100644 --- a/webview-ui/src/context/ExtensionStateContext.tsx +++ b/webview-ui/src/context/ExtensionStateContext.tsx @@ -33,6 +33,7 @@ export interface ExtensionStateContextType extends ExtensionState { setAlwaysAllowExecute: (value: boolean) => void setAlwaysAllowBrowser: (value: boolean) => void setAlwaysAllowMcp: (value: boolean) => void + setAlwaysAllowModeSwitch: (value: boolean) => void setShowAnnouncement: (value: boolean) => void setAllowedCommands: (value: string[]) => void setSoundEnabled: (value: boolean) => void @@ -253,6 +254,7 @@ export const ExtensionStateContextProvider: React.FC<{ children: React.ReactNode setAlwaysAllowExecute: (value) => setState((prevState) => ({ ...prevState, alwaysAllowExecute: value })), setAlwaysAllowBrowser: (value) => setState((prevState) => ({ ...prevState, alwaysAllowBrowser: value })), setAlwaysAllowMcp: (value) => setState((prevState) => ({ ...prevState, alwaysAllowMcp: value })), + setAlwaysAllowModeSwitch: (value) => setState((prevState) => ({ ...prevState, alwaysAllowModeSwitch: value })), setShowAnnouncement: (value) => setState((prevState) => ({ ...prevState, shouldShowAnnouncement: value })), setAllowedCommands: (value) => setState((prevState) => ({ ...prevState, allowedCommands: value })), setSoundEnabled: (value) => setState((prevState) => ({ ...prevState, soundEnabled: value })), From f50214b01736ddea82619ee82076be06b9373e20 Mon Sep 17 00:00:00 2001 From: MFPires Date: Tue, 28 Jan 2025 01:48:47 -0300 Subject: [PATCH 08/32] feat(settings): Add auto-approve mode switching option to Settings UI Add the ability to configure automatic mode switching approval in the Settings UI. Implementation: - Added alwaysAllowModeSwitch checkbox in the Auto-Approve Settings section - Added state management integration with useExtensionState - Added vscode.postMessage handler for state updates - Placed the setting logically between MCP tools and execute operations settings The new setting allows users to: - Enable/disable automatic approval of mode switching operations - Configure mode switching approval independently of other auto-approve settings - Maintain consistent UX with other auto-approve settings This completes the mode switching auto-approval feature, working in conjunction with: - Previously added state management in ExtensionStateContext - Core logic changes in ClineProvider - WebviewMessage type updates - Existing test coverage in ChatView.auto-approve.test.tsx --- .../src/components/settings/SettingsView.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/webview-ui/src/components/settings/SettingsView.tsx b/webview-ui/src/components/settings/SettingsView.tsx index 81a929cc8d..12b97f7157 100644 --- a/webview-ui/src/components/settings/SettingsView.tsx +++ b/webview-ui/src/components/settings/SettingsView.tsx @@ -53,6 +53,8 @@ const SettingsView = ({ onDone }: SettingsViewProps) => { listApiConfigMeta, experimentalDiffStrategy, setExperimentalDiffStrategy, + alwaysAllowModeSwitch, + setAlwaysAllowModeSwitch, } = useExtensionState() const [apiErrorMessage, setApiErrorMessage] = useState(undefined) const [modelIdErrorMessage, setModelIdErrorMessage] = useState(undefined) @@ -93,6 +95,7 @@ const SettingsView = ({ onDone }: SettingsViewProps) => { apiConfiguration, }) vscode.postMessage({ type: "experimentalDiffStrategy", bool: experimentalDiffStrategy }) + vscode.postMessage({ type: "alwaysAllowModeSwitch", bool: alwaysAllowModeSwitch }) onDone() } } @@ -328,6 +331,17 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {

+
+ setAlwaysAllowModeSwitch(e.target.checked)}> + Always approve mode switching + +

+ Automatically switch between different AI modes without requiring approval +

+
+
Date: Tue, 28 Jan 2025 00:15:14 -0500 Subject: [PATCH 09/32] Add test --- src/api/transform/__tests__/r1-format.test.ts | 180 ++++++++++++++++++ 1 file changed, 180 insertions(+) create mode 100644 src/api/transform/__tests__/r1-format.test.ts diff --git a/src/api/transform/__tests__/r1-format.test.ts b/src/api/transform/__tests__/r1-format.test.ts new file mode 100644 index 0000000000..fce1f99da8 --- /dev/null +++ b/src/api/transform/__tests__/r1-format.test.ts @@ -0,0 +1,180 @@ +import { convertToR1Format } from "../r1-format" +import { Anthropic } from "@anthropic-ai/sdk" +import OpenAI from "openai" + +describe("convertToR1Format", () => { + it("should convert basic text messages", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: "Hello" }, + { role: "assistant", content: "Hi there" }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "user", content: "Hello" }, + { role: "assistant", content: "Hi there" }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should merge consecutive messages with same role", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: "Hello" }, + { role: "user", content: "How are you?" }, + { role: "assistant", content: "Hi!" }, + { role: "assistant", content: "I'm doing well" }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "user", content: "Hello\nHow are you?" }, + { role: "assistant", content: "Hi!\nI'm doing well" }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should handle image content", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { + role: "user", + content: [ + { + type: "image", + source: { + type: "base64", + media_type: "image/jpeg", + data: "base64data", + }, + }, + ], + }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { + role: "user", + content: [ + { + type: "image_url", + image_url: { + url: "data:image/jpeg;base64,base64data", + }, + }, + ], + }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should handle mixed text and image content", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { + role: "user", + content: [ + { type: "text", text: "Check this image:" }, + { + type: "image", + source: { + type: "base64", + media_type: "image/jpeg", + data: "base64data", + }, + }, + ], + }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { + role: "user", + content: [ + { type: "text", text: "Check this image:" }, + { + type: "image_url", + image_url: { + url: "data:image/jpeg;base64,base64data", + }, + }, + ], + }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should merge mixed content messages with same role", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { + role: "user", + content: [ + { type: "text", text: "First image:" }, + { + type: "image", + source: { + type: "base64", + media_type: "image/jpeg", + data: "image1", + }, + }, + ], + }, + { + role: "user", + content: [ + { type: "text", text: "Second image:" }, + { + type: "image", + source: { + type: "base64", + media_type: "image/png", + data: "image2", + }, + }, + ], + }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { + role: "user", + content: [ + { type: "text", text: "First image:" }, + { + type: "image_url", + image_url: { + url: "data:image/jpeg;base64,image1", + }, + }, + { type: "text", text: "Second image:" }, + { + type: "image_url", + image_url: { + url: "data:image/png;base64,image2", + }, + }, + ], + }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) + + it("should handle empty messages array", () => { + expect(convertToR1Format([])).toEqual([]) + }) + + it("should handle messages with empty content", () => { + const input: Anthropic.Messages.MessageParam[] = [ + { role: "user", content: "" }, + { role: "assistant", content: "" }, + ] + + const expected: OpenAI.Chat.ChatCompletionMessageParam[] = [ + { role: "user", content: "" }, + { role: "assistant", content: "" }, + ] + + expect(convertToR1Format(input)).toEqual(expected) + }) +}) From 2c97b59ed1d412b0c298cc9f41fac26697ed9e98 Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Tue, 21 Jan 2025 23:49:48 -0800 Subject: [PATCH 10/32] Checkpoint on insert and search/replace tools --- src/core/Cline.ts | 326 +++++++++++++++++++ src/core/assistant-message/index.ts | 8 + src/core/diff/insert-groups.ts | 31 ++ src/core/prompts/tools/index.ts | 6 + src/core/prompts/tools/insert-code-block.ts | 35 ++ src/core/prompts/tools/search-and-replace.ts | 52 +++ src/shared/tool-groups.ts | 2 +- 7 files changed, 459 insertions(+), 1 deletion(-) create mode 100644 src/core/diff/insert-groups.ts create mode 100644 src/core/prompts/tools/insert-code-block.ts create mode 100644 src/core/prompts/tools/search-and-replace.ts diff --git a/src/core/Cline.ts b/src/core/Cline.ts index afaa923f29..f98ddcc6cb 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -60,6 +60,7 @@ import { BrowserSession } from "../services/browser/BrowserSession" import { OpenRouterHandler } from "../api/providers/openrouter" import { McpHub } from "../services/mcp/McpHub" import crypto from "crypto" +import { insertGroups } from "./diff/insert-groups" const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution @@ -1008,6 +1009,10 @@ export class Cline { return `[${block.name} for '${block.params.regex}'${ block.params.file_pattern ? ` in '${block.params.file_pattern}'` : "" }]` + case "insert_code_block": + return `[${block.name} for '${block.params.path}']` + case "search_and_replace": + return `[${block.name} for '${block.params.path}']` case "list_files": return `[${block.name} for '${block.params.path}']` case "list_code_definition_names": @@ -1479,6 +1484,323 @@ export class Cline { break } } + + case "insert_code_block": { + const relPath: string | undefined = block.params.path + const operations: string | undefined = block.params.operations + + const sharedMessageProps: ClineSayTool = { + tool: "appliedDiff", + path: getReadablePath(cwd, removeClosingTag("path", relPath)), + } + + try { + if (block.partial) { + const partialMessage = JSON.stringify(sharedMessageProps) + await this.ask("tool", partialMessage, block.partial).catch(() => {}) + break + } + + // Validate required parameters + if (!relPath) { + this.consecutiveMistakeCount++ + pushToolResult(await this.sayAndCreateMissingParamError("insert_code_block", "path")) + break + } + + if (!operations) { + this.consecutiveMistakeCount++ + pushToolResult( + await this.sayAndCreateMissingParamError("insert_code_block", "operations"), + ) + break + } + + const absolutePath = path.resolve(cwd, relPath) + const fileExists = await fileExistsAtPath(absolutePath) + + if (!fileExists) { + this.consecutiveMistakeCount++ + const formattedError = `File does not exist at path: ${absolutePath}\n\n\nThe specified file could not be found. Please verify the file path and try again.\n` + await this.say("error", formattedError) + pushToolResult(formattedError) + break + } + + let parsedOperations: Array<{ + start_line: number + content: string + }> + + try { + parsedOperations = JSON.parse(operations) + if (!Array.isArray(parsedOperations)) { + throw new Error("Operations must be an array") + } + } catch (error) { + this.consecutiveMistakeCount++ + await this.say("error", `Failed to parse operations JSON: ${error.message}`) + pushToolResult(formatResponse.toolError("Invalid operations JSON format")) + break + } + + this.consecutiveMistakeCount = 0 + + // Read the file + const fileContent = await fs.readFile(absolutePath, "utf8") + this.diffViewProvider.editType = "modify" + this.diffViewProvider.originalContent = fileContent + const lines = fileContent.split("\n") + + const updatedContent = insertGroups( + lines, + parsedOperations.map((elem) => { + return { + index: elem.start_line - 1, + elements: elem.content.split("\n"), + } + }), + ).join("\n") + + // Show changes in diff view + if (!this.diffViewProvider.isEditing) { + await this.ask("tool", JSON.stringify(sharedMessageProps), true).catch(() => {}) + // First open with original content + await this.diffViewProvider.open(relPath) + await this.diffViewProvider.update(fileContent, false) + this.diffViewProvider.scrollToFirstDiff() + await delay(200) + } + + const diff = formatResponse.createPrettyPatch( + relPath, + this.diffViewProvider.originalContent, + updatedContent, + ) + + if (!diff) { + pushToolResult(`No changes needed for '${relPath}'`) + break + } + + await this.diffViewProvider.update(updatedContent, true) + + const completeMessage = JSON.stringify({ + ...sharedMessageProps, + diff, + } satisfies ClineSayTool) + + const didApprove = await this.ask("tool", completeMessage, false).then( + (response) => response.response === "yesButtonClicked", + ) + + if (!didApprove) { + await this.diffViewProvider.revertChanges() + pushToolResult("Changes were rejected by the user.") + break + } + + const { newProblemsMessage, userEdits, finalContent } = + await this.diffViewProvider.saveChanges() + this.didEditFile = true + + if (!userEdits) { + pushToolResult( + `The code block was successfully inserted in ${relPath.toPosix()}.${newProblemsMessage}`, + ) + await this.diffViewProvider.reset() + break + } + + const userFeedbackDiff = JSON.stringify({ + tool: "appliedDiff", + path: getReadablePath(cwd, relPath), + diff: userEdits, + } satisfies ClineSayTool) + + console.debug("[DEBUG] User made edits, sending feedback diff:", userFeedbackDiff) + await this.say("user_feedback_diff", userFeedbackDiff) + pushToolResult( + `The user made the following updates to your content:\n\n${userEdits}\n\n` + + `The updated content, which includes both your original modifications and the user's edits, has been successfully saved to ${relPath.toPosix()}. Here is the full, updated content of the file:\n\n` + + `\n${finalContent}\n\n\n` + + `Please note:\n` + + `1. You do not need to re-write the file with these changes, as they have already been applied.\n` + + `2. Proceed with the task using this updated file content as the new baseline.\n` + + `3. If the user's edits have addressed part of the task or changed the requirements, adjust your approach accordingly.` + + `${newProblemsMessage}`, + ) + await this.diffViewProvider.reset() + } catch (error) { + handleError("insert block", error) + await this.diffViewProvider.reset() + } + break + } + + case "search_and_replace": { + const relPath: string | undefined = block.params.path + const operations: string | undefined = block.params.operations + + const sharedMessageProps: ClineSayTool = { + tool: "appliedDiff", + path: getReadablePath(cwd, removeClosingTag("path", relPath)), + } + + try { + if (block.partial) { + const partialMessage = JSON.stringify({ + path: removeClosingTag("path", relPath), + operations: removeClosingTag("operations", operations), + }) + await this.ask("tool", partialMessage, block.partial).catch(() => {}) + break + } else { + if (!relPath) { + this.consecutiveMistakeCount++ + pushToolResult( + await this.sayAndCreateMissingParamError("search_and_replace", "path"), + ) + break + } + if (!operations) { + this.consecutiveMistakeCount++ + pushToolResult( + await this.sayAndCreateMissingParamError("search_and_replace", "operations"), + ) + break + } + + const absolutePath = path.resolve(cwd, relPath) + const fileExists = await fileExistsAtPath(absolutePath) + + if (!fileExists) { + this.consecutiveMistakeCount++ + const formattedError = `File does not exist at path: ${absolutePath}\n\n\nThe specified file could not be found. Please verify the file path and try again.\n` + await this.say("error", formattedError) + pushToolResult(formattedError) + break + } + + let parsedOperations: Array<{ + search: string + replace: string + start_line?: number + end_line?: number + use_regex?: boolean + ignore_case?: boolean + regex_flags?: string + }> + + try { + parsedOperations = JSON.parse(operations) + if (!Array.isArray(parsedOperations)) { + throw new Error("Operations must be an array") + } + } catch (error) { + this.consecutiveMistakeCount++ + await this.say("error", `Failed to parse operations JSON: ${error.message}`) + pushToolResult(formatResponse.toolError("Invalid operations JSON format")) + break + } + + // Read the original file content + const fileContent = await fs.readFile(absolutePath, "utf-8") + const lines = fileContent.split("\n") + let newContent = fileContent + + // Apply each search/replace operation + for (const op of parsedOperations) { + const searchPattern = op.use_regex + ? new RegExp(op.search, op.regex_flags || (op.ignore_case ? "gi" : "g")) + : new RegExp(escapeRegExp(op.search), op.ignore_case ? "gi" : "g") + + if (op.start_line || op.end_line) { + // Line-restricted replacement + const startLine = (op.start_line || 1) - 1 + const endLine = (op.end_line || lines.length) - 1 + + const beforeLines = lines.slice(0, startLine) + const targetLines = lines.slice(startLine, endLine + 1) + const afterLines = lines.slice(endLine + 1) + + const modifiedLines = targetLines.map((line) => + line.replace(searchPattern, op.replace), + ) + + newContent = [...beforeLines, ...modifiedLines, ...afterLines].join("\n") + } else { + // Global replacement + newContent = newContent.replace(searchPattern, op.replace) + } + } + + this.consecutiveMistakeCount = 0 + + // Show diff preview + const diff = formatResponse.createPrettyPatch( + relPath, + this.diffViewProvider.originalContent, + newContent, + ) + + if (!diff) { + pushToolResult(`No changes needed for '${relPath}'`) + break + } + + await this.diffViewProvider.open(relPath) + await this.diffViewProvider.update(newContent, true) + this.diffViewProvider.scrollToFirstDiff() + + const completeMessage = JSON.stringify({ + ...sharedMessageProps, + diff: diff, + } satisfies ClineSayTool) + + const didApprove = await askApproval("tool", completeMessage) + if (!didApprove) { + await this.diffViewProvider.revertChanges() // This likely handles closing the diff view + break + } + + const { newProblemsMessage, userEdits, finalContent } = + await this.diffViewProvider.saveChanges() + this.didEditFile = true // used to determine if we should wait for busy terminal to update before sending api request + if (userEdits) { + await this.say( + "user_feedback_diff", + JSON.stringify({ + tool: fileExists ? "editedExistingFile" : "newFileCreated", + path: getReadablePath(cwd, relPath), + diff: userEdits, + } satisfies ClineSayTool), + ) + pushToolResult( + `The user made the following updates to your content:\n\n${userEdits}\n\n` + + `The updated content, which includes both your original modifications and the user's edits, has been successfully saved to ${relPath.toPosix()}. Here is the full, updated content of the file, including line numbers:\n\n` + + `\n${addLineNumbers(finalContent || "")}\n\n\n` + + `Please note:\n` + + `1. You do not need to re-write the file with these changes, as they have already been applied.\n` + + `2. Proceed with the task using this updated file content as the new baseline.\n` + + `3. If the user's edits have addressed part of the task or changed the requirements, adjust your approach accordingly.` + + `${newProblemsMessage}`, + ) + } else { + pushToolResult( + `Changes successfully applied to ${relPath.toPosix()}:\n\n${newProblemsMessage}`, + ) + } + await this.diffViewProvider.reset() + break + } + } catch (error) { + await handleError("applying search and replace", error) + await this.diffViewProvider.reset() + break + } + } + case "read_file": { const relPath: string | undefined = block.params.path const sharedMessageProps: ClineSayTool = { @@ -2750,3 +3072,7 @@ export class Cline { return `\n${details.trim()}\n` } } + +function escapeRegExp(string: string): string { + return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") +} diff --git a/src/core/assistant-message/index.ts b/src/core/assistant-message/index.ts index 56e5e6662d..092dd06c60 100644 --- a/src/core/assistant-message/index.ts +++ b/src/core/assistant-message/index.ts @@ -13,6 +13,8 @@ export const toolUseNames = [ "read_file", "write_to_file", "apply_diff", + "insert_code_block", + "search_and_replace", "search_files", "list_files", "list_code_definition_names", @@ -50,6 +52,7 @@ export const toolParamNames = [ "end_line", "mode_slug", "reason", + "operations", ] as const export type ToolParamName = (typeof toolParamNames)[number] @@ -78,6 +81,11 @@ export interface WriteToFileToolUse extends ToolUse { params: Partial, "path" | "content" | "line_count">> } +export interface InsertCodeBlockToolUse extends ToolUse { + name: "insert_code_block" + params: Partial, "path" | "operations">> +} + export interface SearchFilesToolUse extends ToolUse { name: "search_files" params: Partial, "path" | "regex" | "file_pattern">> diff --git a/src/core/diff/insert-groups.ts b/src/core/diff/insert-groups.ts new file mode 100644 index 0000000000..805f65892d --- /dev/null +++ b/src/core/diff/insert-groups.ts @@ -0,0 +1,31 @@ +/** + * Inserts multiple groups of elements at specified indices in an array + * @param original Array to insert into, split by lines + * @param insertGroups Array of groups to insert, each with an index and elements to insert + * @returns New array with all insertions applied + */ +export interface InsertGroup { + index: number + elements: string[] +} + +export function insertGroups(original: string[], insertGroups: InsertGroup[]): string[] { + // Sort groups by index to maintain order + insertGroups.sort((a, b) => a.index - b.index) + + let result: string[] = [] + let lastIndex = 0 + + insertGroups.forEach(({ index, elements }) => { + // Add elements from original array up to insertion point + result.push(...original.slice(lastIndex, index)) + // Add the group of elements + result.push(...elements) + lastIndex = index + }) + + // Add remaining elements from original array + result.push(...original.slice(lastIndex)) + + return result +} diff --git a/src/core/prompts/tools/index.ts b/src/core/prompts/tools/index.ts index b704f4f206..ad9f114f13 100644 --- a/src/core/prompts/tools/index.ts +++ b/src/core/prompts/tools/index.ts @@ -3,6 +3,8 @@ import { getReadFileDescription } from "./read-file" import { getWriteToFileDescription } from "./write-to-file" import { getSearchFilesDescription } from "./search-files" import { getListFilesDescription } from "./list-files" +import { getInsertCodeBlockDescription } from "./insert-code-block" +import { getSearchAndReplaceDescription } from "./search-and-replace" import { getListCodeDefinitionNamesDescription } from "./list-code-definition-names" import { getBrowserActionDescription } from "./browser-action" import { getAskFollowupQuestionDescription } from "./ask-followup-question" @@ -30,6 +32,8 @@ const toolDescriptionMap: Record string | undefined> use_mcp_tool: (args) => getUseMcpToolDescription(args), access_mcp_resource: (args) => getAccessMcpResourceDescription(args), switch_mode: () => getSwitchModeDescription(), + insert_code_block: (args) => getInsertCodeBlockDescription(args), + search_and_replace: (args) => getSearchAndReplaceDescription(args), apply_diff: (args) => args.diffStrategy ? args.diffStrategy.getToolDescription({ cwd: args.cwd, toolOptions: args.toolOptions }) : "", } @@ -100,4 +104,6 @@ export { getUseMcpToolDescription, getAccessMcpResourceDescription, getSwitchModeDescription, + getInsertCodeBlockDescription, + getSearchAndReplaceDescription, } diff --git a/src/core/prompts/tools/insert-code-block.ts b/src/core/prompts/tools/insert-code-block.ts new file mode 100644 index 0000000000..f6c6a32b87 --- /dev/null +++ b/src/core/prompts/tools/insert-code-block.ts @@ -0,0 +1,35 @@ +import { ToolArgs } from "./types" + +export function getInsertCodeBlockDescription(args: ToolArgs): string { + return `## insert_code_block +Description: Inserts code blocks at specific line positions in a file. This is the primary tool for adding new code (functions/methods/classes, imports, attributes etc.) as it allows for precise insertions without overwriting existing content. The tool uses an efficient line-based insertion system that maintains file integrity and proper ordering of multiple insertions. Beware to use the proper indentation. This tool is the preferred way to add new code to files. +Parameters: +- path: (required) The path of the file to insert code into (relative to the current working directory ${args.cwd.toPosix()}) +- operations: (required) A JSON array of insertion operations. Each operation is an object with: + * start_line: (required) The line number where the code block should be inserted + * content: (required) The code block to insert at the specified position +Usage: + +File path here +[ + { + "start_line": 10, + "content": "Your code block here" + } +] + +Example: Insert a new function and its import statement + +src/app.ts +[ + { + "start_line": 1, + "content": "import { sum } from './utils';" + }, + { + "start_line": 10, + "content": "function calculateTotal(items: number[]): number {\n return items.reduce((sum, item) => sum + item, 0);\n}" + } +] +` +} diff --git a/src/core/prompts/tools/search-and-replace.ts b/src/core/prompts/tools/search-and-replace.ts new file mode 100644 index 0000000000..590cb35726 --- /dev/null +++ b/src/core/prompts/tools/search-and-replace.ts @@ -0,0 +1,52 @@ +import { ToolArgs } from "./types" + +export function getSearchAndReplaceDescription(args: ToolArgs): string { + return `## search_and_replace +Description: Request to perform search and replace operations on a file. Each operation can specify a search pattern (string or regex) and replacement text, with optional line range restrictions and regex flags. Shows a diff preview before applying changes. +Parameters: +- path: (required) The path of the file to modify (relative to the current working directory ${args.cwd.toPosix()}) +- operations: (required) A JSON array of search/replace operations. Each operation is an object with: + * search: (required) The text or pattern to search for + * replace: (required) The text to replace matches with + * start_line: (optional) Starting line number for restricted replacement + * end_line: (optional) Ending line number for restricted replacement + * use_regex: (optional) Whether to treat search as a regex pattern + * ignore_case: (optional) Whether to ignore case when matching + * regex_flags: (optional) Additional regex flags when use_regex is true +Usage: + +File path here +[ + { + "search": "text to find", + "replace": "replacement text", + "start_line": 1, + "end_line": 10 + } +] + +Example: Replace "foo" with "bar" in lines 1-10 of example.ts + +example.ts +[ + { + "search": "foo", + "replace": "bar", + "start_line": 1, + "end_line": 10 + } +] + +Example: Replace all occurrences of "old" with "new" using regex + +example.ts +[ + { + "search": "old\\w+", + "replace": "new$&", + "use_regex": true, + "ignore_case": true + } +] +` +} diff --git a/src/shared/tool-groups.ts b/src/shared/tool-groups.ts index 96c9f03951..d95b16e153 100644 --- a/src/shared/tool-groups.ts +++ b/src/shared/tool-groups.ts @@ -21,7 +21,7 @@ export const TOOL_DISPLAY_NAMES = { // Define available tool groups export const TOOL_GROUPS: Record = { read: ["read_file", "search_files", "list_files", "list_code_definition_names"], - edit: ["write_to_file", "apply_diff"], + edit: ["write_to_file", "apply_diff", "insert_code_block", "search_and_replace"], browser: ["browser_action"], command: ["execute_command"], mcp: ["use_mcp_tool", "access_mcp_resource"], From ad552ea0268bbfb4f3105eaa3a19e6cbeb55dc0d Mon Sep 17 00:00:00 2001 From: sam hoang Date: Mon, 27 Jan 2025 03:48:24 +0700 Subject: [PATCH 11/32] feat: implement experimental features system - Add experiments.ts to manage experimental features - Refactor experimental diff strategy into experiments system - Add UI components for managing experimental features - Add tests for experimental tools - Update system prompts to handle experiments --- src/core/Cline.ts | 9 +- src/core/prompts/__tests__/system.test.ts | 142 +++++++++++++++++- src/core/prompts/system.ts | 4 + src/core/prompts/tools/index.ts | 3 +- src/core/webview/ClineProvider.ts | 55 ++++--- .../webview/__tests__/ClineProvider.test.ts | 12 +- src/shared/ExtensionMessage.ts | 2 +- src/shared/WebviewMessage.ts | 2 +- src/shared/__tests__/modes.test.ts | 87 +++++++++++ src/shared/experiments.ts | 63 ++++++++ src/shared/modes.ts | 7 + .../settings/ExperimentalFeature.tsx | 37 +++++ .../src/components/settings/SettingsView.tsx | 62 ++++---- .../src/context/ExtensionStateContext.tsx | 13 +- 14 files changed, 429 insertions(+), 69 deletions(-) create mode 100644 src/shared/experiments.ts create mode 100644 webview-ui/src/components/settings/ExperimentalFeature.tsx diff --git a/src/core/Cline.ts b/src/core/Cline.ts index f98ddcc6cb..f7c7b4003c 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -61,6 +61,7 @@ import { OpenRouterHandler } from "../api/providers/openrouter" import { McpHub } from "../services/mcp/McpHub" import crypto from "crypto" import { insertGroups } from "./diff/insert-groups" +import { EXPERIMENT_IDS } from "../shared/experiments" const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution @@ -151,9 +152,8 @@ export class Cline { async updateDiffStrategy(experimentalDiffStrategy?: boolean) { // If not provided, get from current state if (experimentalDiffStrategy === undefined) { - const { experimentalDiffStrategy: stateExperimentalDiffStrategy } = - (await this.providerRef.deref()?.getState()) ?? {} - experimentalDiffStrategy = stateExperimentalDiffStrategy ?? false + const { experiments: stateExperimental } = (await this.providerRef.deref()?.getState()) ?? {} + experimentalDiffStrategy = stateExperimental?.[EXPERIMENT_IDS.DIFF_STRATEGY] ?? false } this.diffStrategy = getDiffStrategy(this.api.getModel().id, this.fuzzyMatchThreshold, experimentalDiffStrategy) } @@ -810,7 +810,7 @@ export class Cline { }) } - const { browserViewportSize, mode, customModePrompts, preferredLanguage } = + const { browserViewportSize, mode, customModePrompts, preferredLanguage, experiments } = (await this.providerRef.deref()?.getState()) ?? {} const { customModes } = (await this.providerRef.deref()?.getState()) ?? {} const systemPrompt = await (async () => { @@ -831,6 +831,7 @@ export class Cline { this.customInstructions, preferredLanguage, this.diffEnabled, + experiments, ) })() diff --git a/src/core/prompts/__tests__/system.test.ts b/src/core/prompts/__tests__/system.test.ts index 4138d11d2a..cf3cb329cd 100644 --- a/src/core/prompts/__tests__/system.test.ts +++ b/src/core/prompts/__tests__/system.test.ts @@ -11,6 +11,7 @@ import { defaultModeSlug, modes } from "../../../shared/modes" import "../../../utils/path" import { addCustomInstructions } from "../sections/custom-instructions" import * as modesSection from "../sections/modes" +import { EXPERIMENT_IDS } from "../../../shared/experiments" // Mock the sections jest.mock("../sections/modes", () => ({ @@ -121,6 +122,7 @@ const createMockMcpHub = (): McpHub => describe("SYSTEM_PROMPT", () => { let mockMcpHub: McpHub + let experiments: Record beforeAll(() => { // Ensure fs mock is properly initialized @@ -140,6 +142,10 @@ describe("SYSTEM_PROMPT", () => { "/mock/mcp/path", ] dirs.forEach((dir) => mockFs._mockDirectories.add(dir)) + experiments = { + [EXPERIMENT_IDS.SEARCH_AND_REPLACE]: false, + [EXPERIMENT_IDS.INSERT_BLOCK]: false, + } }) beforeEach(() => { @@ -164,6 +170,10 @@ describe("SYSTEM_PROMPT", () => { defaultModeSlug, // mode undefined, // customModePrompts undefined, // customModes + undefined, // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, ) expect(prompt).toMatchSnapshot() @@ -179,7 +189,11 @@ describe("SYSTEM_PROMPT", () => { "1280x800", // browserViewportSize defaultModeSlug, // mode undefined, // customModePrompts - undefined, // customModes + undefined, // customModes, + undefined, // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, ) expect(prompt).toMatchSnapshot() @@ -197,7 +211,11 @@ describe("SYSTEM_PROMPT", () => { undefined, // browserViewportSize defaultModeSlug, // mode undefined, // customModePrompts - undefined, // customModes + undefined, // customModes, + undefined, // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, ) expect(prompt).toMatchSnapshot() @@ -213,7 +231,11 @@ describe("SYSTEM_PROMPT", () => { undefined, // browserViewportSize defaultModeSlug, // mode undefined, // customModePrompts - undefined, // customModes + undefined, // customModes, + undefined, // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, ) expect(prompt).toMatchSnapshot() @@ -229,7 +251,11 @@ describe("SYSTEM_PROMPT", () => { "900x600", // different viewport size defaultModeSlug, // mode undefined, // customModePrompts - undefined, // customModes + undefined, // customModes, + undefined, // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, ) expect(prompt).toMatchSnapshot() @@ -249,6 +275,7 @@ describe("SYSTEM_PROMPT", () => { undefined, // globalCustomInstructions undefined, // preferredLanguage true, // diffEnabled + experiments, ) expect(prompt).toContain("apply_diff") @@ -269,6 +296,7 @@ describe("SYSTEM_PROMPT", () => { undefined, // globalCustomInstructions undefined, // preferredLanguage false, // diffEnabled + experiments, ) expect(prompt).not.toContain("apply_diff") @@ -289,6 +317,7 @@ describe("SYSTEM_PROMPT", () => { undefined, // globalCustomInstructions undefined, // preferredLanguage undefined, // diffEnabled + experiments, ) expect(prompt).not.toContain("apply_diff") @@ -308,6 +337,8 @@ describe("SYSTEM_PROMPT", () => { undefined, // customModes undefined, // globalCustomInstructions "Spanish", // preferredLanguage + undefined, // diffEnabled + experiments, ) expect(prompt).toContain("Language Preference:") @@ -337,6 +368,9 @@ describe("SYSTEM_PROMPT", () => { undefined, // customModePrompts customModes, // customModes "Global instructions", // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, ) // Role definition should be at the top @@ -368,6 +402,10 @@ describe("SYSTEM_PROMPT", () => { defaultModeSlug, customModePrompts, undefined, + undefined, + undefined, + undefined, + experiments, ) // Role definition from promptComponent should be at the top @@ -394,18 +432,101 @@ describe("SYSTEM_PROMPT", () => { defaultModeSlug, customModePrompts, undefined, + undefined, + undefined, + undefined, + experiments, ) // Should use the default mode's role definition expect(prompt.indexOf(modes[0].roleDefinition)).toBeLessThan(prompt.indexOf("TOOL USE")) }) + describe("experimental tools", () => { + it("should disable experimental tools by default", async () => { + const prompt = await SYSTEM_PROMPT( + mockContext, + "/test/path", + false, // supportsComputerUse + undefined, // mcpHub + undefined, // diffStrategy + undefined, // browserViewportSize + defaultModeSlug, // mode + undefined, // customModePrompts + undefined, // customModes + undefined, // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, // experiments - undefined should disable all experimental tools + ) + + // Verify experimental tools are not included in the prompt + expect(prompt).not.toContain(EXPERIMENT_IDS.SEARCH_AND_REPLACE) + expect(prompt).not.toContain(EXPERIMENT_IDS.INSERT_BLOCK) + }) + + it("should enable experimental tools when explicitly enabled", async () => { + const experiments = { + [EXPERIMENT_IDS.SEARCH_AND_REPLACE]: true, + [EXPERIMENT_IDS.INSERT_BLOCK]: true, + } + + const prompt = await SYSTEM_PROMPT( + mockContext, + "/test/path", + false, // supportsComputerUse + undefined, // mcpHub + undefined, // diffStrategy + undefined, // browserViewportSize + defaultModeSlug, // mode + undefined, // customModePrompts + undefined, // customModes + undefined, // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, + ) + + // Verify experimental tools are included in the prompt when enabled + expect(prompt).toContain(EXPERIMENT_IDS.SEARCH_AND_REPLACE) + expect(prompt).toContain(EXPERIMENT_IDS.INSERT_BLOCK) + }) + + it("should selectively enable experimental tools", async () => { + const experiments = { + [EXPERIMENT_IDS.SEARCH_AND_REPLACE]: true, + [EXPERIMENT_IDS.INSERT_BLOCK]: false, + } + + const prompt = await SYSTEM_PROMPT( + mockContext, + "/test/path", + false, // supportsComputerUse + undefined, // mcpHub + undefined, // diffStrategy + undefined, // browserViewportSize + defaultModeSlug, // mode + undefined, // customModePrompts + undefined, // customModes + undefined, // globalCustomInstructions + undefined, // preferredLanguage + undefined, // diffEnabled + experiments, + ) + + // Verify only enabled experimental tools are included + expect(prompt).toContain(EXPERIMENT_IDS.SEARCH_AND_REPLACE) + expect(prompt).not.toContain(EXPERIMENT_IDS.INSERT_BLOCK) + }) + }) + afterAll(() => { jest.restoreAllMocks() }) }) describe("addCustomInstructions", () => { + let experiments: Record beforeAll(() => { // Ensure fs mock is properly initialized const mockFs = jest.requireMock("fs/promises") @@ -417,6 +538,11 @@ describe("addCustomInstructions", () => { } throw new Error(`ENOENT: no such file or directory, mkdir '${path}'`) }) + + experiments = { + [EXPERIMENT_IDS.SEARCH_AND_REPLACE]: false, + [EXPERIMENT_IDS.INSERT_BLOCK]: false, + } }) beforeEach(() => { @@ -434,6 +560,10 @@ describe("addCustomInstructions", () => { "architect", // mode undefined, // customModePrompts undefined, // customModes + undefined, + undefined, + undefined, + experiments, ) expect(prompt).toMatchSnapshot() @@ -450,6 +580,10 @@ describe("addCustomInstructions", () => { "ask", // mode undefined, // customModePrompts undefined, // customModes + undefined, + undefined, + undefined, + experiments, ) expect(prompt).toMatchSnapshot() diff --git a/src/core/prompts/system.ts b/src/core/prompts/system.ts index 51719c3d68..9b8e49f4e6 100644 --- a/src/core/prompts/system.ts +++ b/src/core/prompts/system.ts @@ -39,6 +39,7 @@ async function generatePrompt( globalCustomInstructions?: string, preferredLanguage?: string, diffEnabled?: boolean, + experiments?: Record, ): Promise { if (!context) { throw new Error("Extension context is required for generating system prompt") @@ -68,6 +69,7 @@ ${getToolDescriptionsForMode( browserViewportSize, mcpHub, customModeConfigs, + experiments, )} ${getToolUseGuidelinesSection()} @@ -102,6 +104,7 @@ export const SYSTEM_PROMPT = async ( globalCustomInstructions?: string, preferredLanguage?: string, diffEnabled?: boolean, + experiments?: Record, ): Promise => { if (!context) { throw new Error("Extension context is required for generating system prompt") @@ -135,5 +138,6 @@ export const SYSTEM_PROMPT = async ( globalCustomInstructions, preferredLanguage, diffEnabled, + experiments, ) } diff --git a/src/core/prompts/tools/index.ts b/src/core/prompts/tools/index.ts index ad9f114f13..23e77fb8d6 100644 --- a/src/core/prompts/tools/index.ts +++ b/src/core/prompts/tools/index.ts @@ -46,6 +46,7 @@ export function getToolDescriptionsForMode( browserViewportSize?: string, mcpHub?: McpHub, customModes?: ModeConfig[], + experiments?: Record, ): string { const config = getModeConfig(mode, customModes) const args: ToolArgs = { @@ -64,7 +65,7 @@ export function getToolDescriptionsForMode( const toolGroup = TOOL_GROUPS[groupName] if (toolGroup) { toolGroup.forEach((tool) => { - if (isToolAllowedForMode(tool as ToolName, mode, customModes ?? [])) { + if (isToolAllowedForMode(tool as ToolName, mode, customModes ?? [], experiments ?? {})) { tools.add(tool) } }) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index 0f96df27eb..6a1cf1ae74 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -40,6 +40,12 @@ import { singleCompletionHandler } from "../../utils/single-completion-handler" import { getCommitInfo, searchCommits, getWorkingState } from "../../utils/git" import { ConfigManager } from "../config/ConfigManager" import { CustomModesManager } from "../config/CustomModesManager" +import { + EXPERIMENT_IDS, + experimentConfigs, + experiments as Experiments, + experimentDefault, +} from "../../shared/experiments" import { CustomSupportPrompts, supportPrompt } from "../../shared/support-prompt" import { ACTION_NAMES } from "../CodeActionProvider" @@ -118,7 +124,7 @@ type GlobalStateKey = | "customModePrompts" | "customSupportPrompts" | "enhancementApiConfigId" - | "experimentalDiffStrategy" + | "experiments" // Map of experiment IDs to their enabled state | "autoApprovalEnabled" | "customModes" // Array of custom modes @@ -339,7 +345,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { fuzzyMatchThreshold, mode, customInstructions: globalInstructions, - experimentalDiffStrategy, + experiments, } = await this.getState() const modePrompt = customModePrompts?.[mode] as PromptComponent @@ -354,7 +360,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { task, images, undefined, - experimentalDiffStrategy, + Experiments.isEnabled(experiments, EXPERIMENT_IDS.DIFF_STRATEGY), ) } @@ -367,7 +373,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { fuzzyMatchThreshold, mode, customInstructions: globalInstructions, - experimentalDiffStrategy, + experiments, } = await this.getState() const modePrompt = customModePrompts?.[mode] as PromptComponent @@ -382,7 +388,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { undefined, undefined, historyItem, - experimentalDiffStrategy, + Experiments.isEnabled(experiments, EXPERIMENT_IDS.DIFF_STRATEGY), ) } @@ -1044,14 +1050,14 @@ export class ClineProvider implements vscode.WebviewViewProvider { diffEnabled, mcpEnabled, fuzzyMatchThreshold, - experimentalDiffStrategy, + experiments, } = await this.getState() // Create diffStrategy based on current model and settings const diffStrategy = getDiffStrategy( apiConfiguration.apiModelId || apiConfiguration.openRouterModelId || "", fuzzyMatchThreshold, - experimentalDiffStrategy, + Experiments.isEnabled(experiments, EXPERIMENT_IDS.DIFF_STRATEGY), ) const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) || "" @@ -1072,6 +1078,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { customInstructions, preferredLanguage, diffEnabled, + experiments, ) await this.postMessageToWebview({ @@ -1207,14 +1214,28 @@ export class ClineProvider implements vscode.WebviewViewProvider { vscode.window.showErrorMessage("Failed to get list api configuration") } break - case "experimentalDiffStrategy": - await this.updateGlobalState("experimentalDiffStrategy", message.bool ?? false) - // Update diffStrategy in current Cline instance if it exists - if (this.cline) { - await this.cline.updateDiffStrategy(message.bool ?? false) + case "updateExperimental": { + if (!message.values) { + break } + + const updatedExperiments = { + ...((await this.getGlobalState("experiments")) ?? experimentDefault), + ...message.values, + } + + await this.updateGlobalState("experiments", updatedExperiments) + + // Update diffStrategy in current Cline instance if it exists + if (message.values[EXPERIMENT_IDS.DIFF_STRATEGY] !== undefined && this.cline) { + await this.cline.updateDiffStrategy( + Experiments.isEnabled(updatedExperiments, EXPERIMENT_IDS.DIFF_STRATEGY), + ) + } + await this.postStateToWebview() break + } case "updateMcpTimeout": if (message.serverName && typeof message.timeout === "number") { try { @@ -1873,8 +1894,8 @@ export class ClineProvider implements vscode.WebviewViewProvider { customModePrompts, customSupportPrompts, enhancementApiConfigId, - experimentalDiffStrategy, autoApprovalEnabled, + experiments, } = await this.getState() const allowedCommands = vscode.workspace.getConfiguration("roo-cline").get("allowedCommands") || [] @@ -1914,9 +1935,9 @@ export class ClineProvider implements vscode.WebviewViewProvider { customModePrompts: customModePrompts ?? {}, customSupportPrompts: customSupportPrompts ?? {}, enhancementApiConfigId, - experimentalDiffStrategy: experimentalDiffStrategy ?? false, autoApprovalEnabled: autoApprovalEnabled ?? false, customModes: await this.customModesManager.getCustomModes(), + experiments: experiments ?? experimentDefault, } } @@ -2039,9 +2060,9 @@ export class ClineProvider implements vscode.WebviewViewProvider { customModePrompts, customSupportPrompts, enhancementApiConfigId, - experimentalDiffStrategy, autoApprovalEnabled, customModes, + experiments, ] = await Promise.all([ this.getGlobalState("apiProvider") as Promise, this.getGlobalState("apiModelId") as Promise, @@ -2109,9 +2130,9 @@ export class ClineProvider implements vscode.WebviewViewProvider { this.getGlobalState("customModePrompts") as Promise, this.getGlobalState("customSupportPrompts") as Promise, this.getGlobalState("enhancementApiConfigId") as Promise, - this.getGlobalState("experimentalDiffStrategy") as Promise, this.getGlobalState("autoApprovalEnabled") as Promise, this.customModesManager.getCustomModes(), + this.getGlobalState("experiments") as Promise | undefined>, ]) let apiProvider: ApiProvider @@ -2225,7 +2246,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { customModePrompts: customModePrompts ?? {}, customSupportPrompts: customSupportPrompts ?? {}, enhancementApiConfigId, - experimentalDiffStrategy: experimentalDiffStrategy ?? false, + experiments: experiments ?? experimentDefault, autoApprovalEnabled: autoApprovalEnabled ?? false, customModes, } diff --git a/src/core/webview/__tests__/ClineProvider.test.ts b/src/core/webview/__tests__/ClineProvider.test.ts index 09b3db312d..bab54b0474 100644 --- a/src/core/webview/__tests__/ClineProvider.test.ts +++ b/src/core/webview/__tests__/ClineProvider.test.ts @@ -4,6 +4,7 @@ import { ExtensionMessage, ExtensionState } from "../../../shared/ExtensionMessa import { setSoundEnabled } from "../../../utils/sound" import { defaultModeSlug, modes } from "../../../shared/modes" import { addCustomInstructions } from "../../prompts/sections/custom-instructions" +import { experimentDefault, experiments } from "../../../shared/experiments" // Mock custom-instructions module const mockAddCustomInstructions = jest.fn() @@ -320,6 +321,7 @@ describe("ClineProvider", () => { requestDelaySeconds: 5, mode: defaultModeSlug, customModes: [], + experiments: experimentDefault, } const message: ExtensionMessage = { @@ -617,6 +619,7 @@ describe("ClineProvider", () => { mode: "code", diffEnabled: true, fuzzyMatchThreshold: 1.0, + experiments: experimentDefault, } as any) // Reset Cline mock @@ -636,7 +639,7 @@ describe("ClineProvider", () => { "Test task", undefined, undefined, - undefined, + false, ) }) test("handles mode-specific custom instructions updates", async () => { @@ -887,6 +890,7 @@ describe("ClineProvider", () => { }, mcpEnabled: true, mode: "code" as const, + experiments: experimentDefault, } as any) const handler1 = getMessageHandler() @@ -918,6 +922,7 @@ describe("ClineProvider", () => { }, mcpEnabled: false, mode: "code" as const, + experiments: experimentDefault, } as any) const handler2 = getMessageHandler() @@ -985,6 +990,7 @@ describe("ClineProvider", () => { experimentalDiffStrategy: true, diffEnabled: true, fuzzyMatchThreshold: 0.8, + experiments: experimentDefault, } as any) // Mock SYSTEM_PROMPT to verify diffStrategy and diffEnabled are passed @@ -1012,6 +1018,7 @@ describe("ClineProvider", () => { undefined, // effectiveInstructions undefined, // preferredLanguage true, // diffEnabled + experimentDefault, ) // Run the test again to verify it's consistent @@ -1034,6 +1041,7 @@ describe("ClineProvider", () => { experimentalDiffStrategy: true, diffEnabled: false, fuzzyMatchThreshold: 0.8, + experiments: experimentDefault, } as any) // Mock SYSTEM_PROMPT to verify diffEnabled is passed as false @@ -1061,6 +1069,7 @@ describe("ClineProvider", () => { undefined, // effectiveInstructions undefined, // preferredLanguage false, // diffEnabled + experimentDefault, ) }) @@ -1077,6 +1086,7 @@ describe("ClineProvider", () => { mode: "architect", mcpEnabled: false, browserViewportSize: "900x600", + experiments: experimentDefault, } as any) // Mock SYSTEM_PROMPT to call addCustomInstructions diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index 56075b8676..75b21831b2 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -108,7 +108,7 @@ export interface ExtensionState { mode: Mode modeApiConfigs?: Record enhancementApiConfigId?: string - experimentalDiffStrategy?: boolean + experiments: Record // Map of experiment IDs to their enabled state autoApprovalEnabled?: boolean customModes: ModeConfig[] toolRequirements?: Record // Map of tool names to their requirements (e.g. {"apply_diff": true} if diffEnabled) diff --git a/src/shared/WebviewMessage.ts b/src/shared/WebviewMessage.ts index a027a936a5..357a237caf 100644 --- a/src/shared/WebviewMessage.ts +++ b/src/shared/WebviewMessage.ts @@ -75,7 +75,7 @@ export interface WebviewMessage { | "getSystemPrompt" | "systemPrompt" | "enhancementApiConfigId" - | "experimentalDiffStrategy" + | "updateExperimental" | "autoApprovalEnabled" | "updateCustomMode" | "deleteCustomMode" diff --git a/src/shared/__tests__/modes.test.ts b/src/shared/__tests__/modes.test.ts index 252ed6e4ed..b9e46cb8d8 100644 --- a/src/shared/__tests__/modes.test.ts +++ b/src/shared/__tests__/modes.test.ts @@ -14,6 +14,12 @@ describe("isToolAllowedForMode", () => { roleDefinition: "You are a CSS editor", groups: ["read", ["edit", { fileRegex: "\\.css$" }], "browser"], }, + { + slug: "test-exp-mode", + name: "Test Exp Mode", + roleDefinition: "You are an experimental tester", + groups: ["read", "edit", "browser"], + }, ] it("allows always available tools", () => { @@ -240,6 +246,87 @@ describe("isToolAllowedForMode", () => { expect(isToolAllowedForMode("write_to_file", "markdown-editor", customModes, toolRequirements)).toBe(false) }) + + describe("experimental tools", () => { + it("disables tools when experiment is disabled", () => { + const experiments = { + search_and_replace: false, + insert_code_block: false, + } + + expect( + isToolAllowedForMode( + "search_and_replace", + "test-exp-mode", + customModes, + undefined, + undefined, + experiments, + ), + ).toBe(false) + + expect( + isToolAllowedForMode( + "insert_code_block", + "test-exp-mode", + customModes, + undefined, + undefined, + experiments, + ), + ).toBe(false) + }) + + it("allows tools when experiment is enabled", () => { + const experiments = { + search_and_replace: true, + insert_code_block: true, + } + + expect( + isToolAllowedForMode( + "search_and_replace", + "test-exp-mode", + customModes, + undefined, + undefined, + experiments, + ), + ).toBe(true) + + expect( + isToolAllowedForMode( + "insert_code_block", + "test-exp-mode", + customModes, + undefined, + undefined, + experiments, + ), + ).toBe(true) + }) + + it("allows non-experimental tools when experiments are disabled", () => { + const experiments = { + search_and_replace: false, + insert_code_block: false, + } + + expect( + isToolAllowedForMode("read_file", "markdown-editor", customModes, undefined, undefined, experiments), + ).toBe(true) + expect( + isToolAllowedForMode( + "write_to_file", + "markdown-editor", + customModes, + undefined, + { path: "test.md" }, + experiments, + ), + ).toBe(true) + }) + }) }) describe("FileRestrictionError", () => { diff --git a/src/shared/experiments.ts b/src/shared/experiments.ts new file mode 100644 index 0000000000..0c36ed6265 --- /dev/null +++ b/src/shared/experiments.ts @@ -0,0 +1,63 @@ +export interface ExperimentConfig { + id: string + name: string + description: string + enabled: boolean +} + +export const EXPERIMENT_IDS = { + DIFF_STRATEGY: "experimentalDiffStrategy", + SEARCH_AND_REPLACE: "search_and_replace", + INSERT_BLOCK: "insert_code_block", +} as const + +export type ExperimentId = keyof typeof EXPERIMENT_IDS + +export const experimentConfigsMap: Record = { + DIFF_STRATEGY: { + id: EXPERIMENT_IDS.DIFF_STRATEGY, + name: "Use experimental unified diff strategy", + description: + "Enable the experimental unified diff strategy. This strategy might reduce the number of retries caused by model errors but may cause unexpected behavior or incorrect edits. Only enable if you understand the risks and are willing to carefully review all changes.", + enabled: false, + }, + SEARCH_AND_REPLACE: { + id: EXPERIMENT_IDS.SEARCH_AND_REPLACE, + name: "Use experimental search and replace tool", + description: + "Enable the experimental Search and Replace tool. This tool allows Roo to search and replace term. Can be run multiple search and replace in sequence at once request.", + enabled: false, + }, + INSERT_BLOCK: { + id: EXPERIMENT_IDS.INSERT_BLOCK, + name: "Use experimental insert block tool", + + description: + "Enable the experimental insert block tool. This tool allows Roo to insert code blocks into files. Can be insert multiple blocks at once.", + enabled: false, + }, +} + +// Keep the array version for backward compatibility +export const experimentConfigs = Object.values(experimentConfigsMap) +export const experimentDefault = Object.fromEntries( + Object.entries(experimentConfigsMap).map(([_, config]) => [config.id, config.enabled]), +) + +export const experiments = { + get: (id: ExperimentId): ExperimentConfig | undefined => { + return experimentConfigsMap[id] + }, + isEnabled: (experimentsConfig: Record, id: string): boolean => { + return experimentsConfig[id] ?? experimentDefault[id] + }, +} as const + +// Expose experiment details for UI - pre-compute from map for better performance +export const experimentLabels = Object.fromEntries( + Object.values(experimentConfigsMap).map((config) => [config.id, config.name]), +) as Record + +export const experimentDescriptions = Object.fromEntries( + Object.values(experimentConfigsMap).map((config) => [config.id, config.description]), +) as Record diff --git a/src/shared/modes.ts b/src/shared/modes.ts index 4fa9fb0353..012ee63945 100644 --- a/src/shared/modes.ts +++ b/src/shared/modes.ts @@ -160,12 +160,19 @@ export function isToolAllowedForMode( customModes: ModeConfig[], toolRequirements?: Record, toolParams?: Record, // All tool parameters + experiments?: Record, ): boolean { // Always allow these tools if (ALWAYS_AVAILABLE_TOOLS.includes(tool as any)) { return true } + if (experiments && tool in experiments) { + if (!experiments[tool]) { + return false + } + } + // Check tool requirements if any exist if (toolRequirements && tool in toolRequirements) { if (!toolRequirements[tool]) { diff --git a/webview-ui/src/components/settings/ExperimentalFeature.tsx b/webview-ui/src/components/settings/ExperimentalFeature.tsx new file mode 100644 index 0000000000..b5542b9f0e --- /dev/null +++ b/webview-ui/src/components/settings/ExperimentalFeature.tsx @@ -0,0 +1,37 @@ +import { VSCodeCheckbox } from "@vscode/webview-ui-toolkit/react" + +interface ExperimentalFeatureProps { + id: string + name: string + description: string + enabled: boolean + onChange: (value: boolean) => void +} + +const ExperimentalFeature = ({ id, name, description, enabled, onChange }: ExperimentalFeatureProps) => { + return ( +
+
+ ⚠️ + onChange(e.target.checked)}> + {name} + +
+

+ {description} +

+
+ ) +} + +export default ExperimentalFeature diff --git a/webview-ui/src/components/settings/SettingsView.tsx b/webview-ui/src/components/settings/SettingsView.tsx index 12b97f7157..b78abd89cf 100644 --- a/webview-ui/src/components/settings/SettingsView.tsx +++ b/webview-ui/src/components/settings/SettingsView.tsx @@ -4,6 +4,8 @@ import { useExtensionState } from "../../context/ExtensionStateContext" import { validateApiConfiguration, validateModelId } from "../../utils/validate" import { vscode } from "../../utils/vscode" import ApiOptions from "./ApiOptions" +import ExperimentalFeature from "./ExperimentalFeature" +import { experimentConfigs, EXPERIMENT_IDS, experimentConfigsMap } from "../../../../src/shared/experiments" import ApiConfigManager from "./ApiConfigManager" type SettingsViewProps = { @@ -51,8 +53,8 @@ const SettingsView = ({ onDone }: SettingsViewProps) => { setRequestDelaySeconds, currentApiConfigName, listApiConfigMeta, - experimentalDiffStrategy, - setExperimentalDiffStrategy, + experiments, + setExperimentEnabled, alwaysAllowModeSwitch, setAlwaysAllowModeSwitch, } = useExtensionState() @@ -94,7 +96,12 @@ const SettingsView = ({ onDone }: SettingsViewProps) => { text: currentApiConfigName, apiConfiguration, }) - vscode.postMessage({ type: "experimentalDiffStrategy", bool: experimentalDiffStrategy }) + + vscode.postMessage({ + type: "updateExperimental", + values: experiments, + }) + vscode.postMessage({ type: "alwaysAllowModeSwitch", bool: alwaysAllowModeSwitch }) onDone() } @@ -583,7 +590,7 @@ const SettingsView = ({ onDone }: SettingsViewProps) => { setDiffEnabled(e.target.checked) if (!e.target.checked) { // Reset experimental strategy when diffs are disabled - setExperimentalDiffStrategy(false) + setExperimentEnabled(EXPERIMENT_IDS.DIFF_STRATEGY, false) } }}> Enable editing through diffs @@ -599,35 +606,14 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {

{diffEnabled && ( -
-
- ⚠️ - setExperimentalDiffStrategy(e.target.checked)}> - - Use experimental unified diff strategy - - -
-

- Enable the experimental unified diff strategy. This strategy might reduce the number - of retries caused by model errors but may cause unexpected behavior or incorrect - edits. Only enable if you understand the risks and are willing to carefully review - all changes. -

- -
+
+ setExperimentEnabled(EXPERIMENT_IDS.DIFF_STRATEGY, enabled)} + /> +
Match precision {

)} + {experimentConfigs + .filter((config) => config.id !== EXPERIMENT_IDS.DIFF_STRATEGY) + .map((config) => ( + setExperimentEnabled(config.id, enabled)} + /> + ))}
diff --git a/webview-ui/src/context/ExtensionStateContext.tsx b/webview-ui/src/context/ExtensionStateContext.tsx index 7d0159d840..6c7e0983a0 100644 --- a/webview-ui/src/context/ExtensionStateContext.tsx +++ b/webview-ui/src/context/ExtensionStateContext.tsx @@ -16,6 +16,7 @@ import { McpServer } from "../../../src/shared/mcp" import { checkExistKey } from "../../../src/shared/checkExistApiConfig" import { Mode, CustomModePrompts, defaultModeSlug, defaultPrompts, ModeConfig } from "../../../src/shared/modes" import { CustomSupportPrompts } from "../../../src/shared/support-prompt" +import { EXPERIMENT_IDS, experimentDefault } from "../../../src/shared/experiments" export interface ExtensionStateContextType extends ExtensionState { didHydrateState: boolean @@ -63,9 +64,8 @@ export interface ExtensionStateContextType extends ExtensionState { setCustomSupportPrompts: (value: CustomSupportPrompts) => void enhancementApiConfigId?: string setEnhancementApiConfigId: (value: string) => void - experimentalDiffStrategy: boolean - setExperimentalDiffStrategy: (value: boolean) => void - autoApprovalEnabled?: boolean + experiments: Record + setExperimentEnabled: (id: string, enabled: boolean) => void setAutoApprovalEnabled: (value: boolean) => void handleInputChange: (field: keyof ApiConfiguration) => (event: any) => void customModes: ModeConfig[] @@ -98,8 +98,8 @@ export const ExtensionStateContextProvider: React.FC<{ children: React.ReactNode mode: defaultModeSlug, customModePrompts: defaultPrompts, customSupportPrompts: {}, + experiments: experimentDefault, enhancementApiConfigId: "", - experimentalDiffStrategy: false, autoApprovalEnabled: false, customModes: [], }) @@ -242,7 +242,8 @@ export const ExtensionStateContextProvider: React.FC<{ children: React.ReactNode fuzzyMatchThreshold: state.fuzzyMatchThreshold, writeDelayMs: state.writeDelayMs, screenshotQuality: state.screenshotQuality, - experimentalDiffStrategy: state.experimentalDiffStrategy ?? false, + setExperimentEnabled: (id, enabled) => + setState((prevState) => ({ ...prevState, experiments: { ...prevState.experiments, [id]: enabled } })), setApiConfiguration: (value) => setState((prevState) => ({ ...prevState, @@ -279,8 +280,6 @@ export const ExtensionStateContextProvider: React.FC<{ children: React.ReactNode setCustomSupportPrompts: (value) => setState((prevState) => ({ ...prevState, customSupportPrompts: value })), setEnhancementApiConfigId: (value) => setState((prevState) => ({ ...prevState, enhancementApiConfigId: value })), - setExperimentalDiffStrategy: (value) => - setState((prevState) => ({ ...prevState, experimentalDiffStrategy: value })), setAutoApprovalEnabled: (value) => setState((prevState) => ({ ...prevState, autoApprovalEnabled: value })), handleInputChange, setCustomModes: (value) => setState((prevState) => ({ ...prevState, customModes: value })), From bb84d79af13e7f54e34dcab81f0708897a31a8b0 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Mon, 27 Jan 2025 03:51:43 +0700 Subject: [PATCH 12/32] chore: remove unused --- webview-ui/src/context/ExtensionStateContext.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webview-ui/src/context/ExtensionStateContext.tsx b/webview-ui/src/context/ExtensionStateContext.tsx index 6c7e0983a0..76c5ad92ce 100644 --- a/webview-ui/src/context/ExtensionStateContext.tsx +++ b/webview-ui/src/context/ExtensionStateContext.tsx @@ -16,7 +16,7 @@ import { McpServer } from "../../../src/shared/mcp" import { checkExistKey } from "../../../src/shared/checkExistApiConfig" import { Mode, CustomModePrompts, defaultModeSlug, defaultPrompts, ModeConfig } from "../../../src/shared/modes" import { CustomSupportPrompts } from "../../../src/shared/support-prompt" -import { EXPERIMENT_IDS, experimentDefault } from "../../../src/shared/experiments" +import { experimentDefault } from "../../../src/shared/experiments" export interface ExtensionStateContextType extends ExtensionState { didHydrateState: boolean From 3ed8540ebafacb40fc169f04d9f721aee28ed450 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Mon, 27 Jan 2025 14:59:52 +0700 Subject: [PATCH 13/32] refactor(experiments): improve type safety for experiment configuration Change ExperimentId type to be value-based rather than key-based Make experiment record types more strict with proper typing Pass full experiment config object instead of single boolean flag Update type definitions and usages across codebase --- src/core/Cline.ts | 6 ++--- src/core/webview/ClineProvider.ts | 9 ++++--- .../webview/__tests__/ClineProvider.test.ts | 2 +- src/shared/ExtensionMessage.ts | 3 ++- src/shared/experiments.ts | 27 ++++++++++--------- .../src/context/ExtensionStateContext.tsx | 5 ++-- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/core/Cline.ts b/src/core/Cline.ts index f7c7b4003c..42358ec9cf 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -61,7 +61,7 @@ import { OpenRouterHandler } from "../api/providers/openrouter" import { McpHub } from "../services/mcp/McpHub" import crypto from "crypto" import { insertGroups } from "./diff/insert-groups" -import { EXPERIMENT_IDS } from "../shared/experiments" +import { EXPERIMENT_IDS, experiments as Experiments } from "../shared/experiments" const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution @@ -117,7 +117,7 @@ export class Cline { task?: string | undefined, images?: string[] | undefined, historyItem?: HistoryItem | undefined, - experimentalDiffStrategy: boolean = false, + experiments?: Record, ) { if (!task && !images && !historyItem) { throw new Error("Either historyItem or task/images must be provided") @@ -139,7 +139,7 @@ export class Cline { } // Initialize diffStrategy based on current state - this.updateDiffStrategy(experimentalDiffStrategy) + this.updateDiffStrategy(Experiments.isEnabled(experiments ?? {}, EXPERIMENT_IDS.DIFF_STRATEGY)) if (task || images) { this.startTask(task, images) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index 6a1cf1ae74..09f4e7742f 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -45,6 +45,7 @@ import { experimentConfigs, experiments as Experiments, experimentDefault, + ExperimentId, } from "../../shared/experiments" import { CustomSupportPrompts, supportPrompt } from "../../shared/support-prompt" @@ -360,7 +361,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { task, images, undefined, - Experiments.isEnabled(experiments, EXPERIMENT_IDS.DIFF_STRATEGY), + experiments, ) } @@ -388,7 +389,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { undefined, undefined, historyItem, - Experiments.isEnabled(experiments, EXPERIMENT_IDS.DIFF_STRATEGY), + experiments, ) } @@ -1222,7 +1223,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { const updatedExperiments = { ...((await this.getGlobalState("experiments")) ?? experimentDefault), ...message.values, - } + } as Record await this.updateGlobalState("experiments", updatedExperiments) @@ -2132,7 +2133,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { this.getGlobalState("enhancementApiConfigId") as Promise, this.getGlobalState("autoApprovalEnabled") as Promise, this.customModesManager.getCustomModes(), - this.getGlobalState("experiments") as Promise | undefined>, + this.getGlobalState("experiments") as Promise | undefined>, ]) let apiProvider: ApiProvider diff --git a/src/core/webview/__tests__/ClineProvider.test.ts b/src/core/webview/__tests__/ClineProvider.test.ts index bab54b0474..0fb7012bba 100644 --- a/src/core/webview/__tests__/ClineProvider.test.ts +++ b/src/core/webview/__tests__/ClineProvider.test.ts @@ -639,7 +639,7 @@ describe("ClineProvider", () => { "Test task", undefined, undefined, - false, + experimentDefault, ) }) test("handles mode-specific custom instructions updates", async () => { diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index 75b21831b2..4cd6370697 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -6,6 +6,7 @@ import { McpServer } from "./mcp" import { GitCommit } from "../utils/git" import { Mode, CustomModePrompts, ModeConfig } from "./modes" import { CustomSupportPrompts } from "./support-prompt" +import { ExperimentId } from "./experiments" export interface LanguageModelChatSelector { vendor?: string @@ -108,7 +109,7 @@ export interface ExtensionState { mode: Mode modeApiConfigs?: Record enhancementApiConfigId?: string - experiments: Record // Map of experiment IDs to their enabled state + experiments: Record // Map of experiment IDs to their enabled state autoApprovalEnabled?: boolean customModes: ModeConfig[] toolRequirements?: Record // Map of tool names to their requirements (e.g. {"apply_diff": true} if diffEnabled) diff --git a/src/shared/experiments.ts b/src/shared/experiments.ts index 0c36ed6265..90805a2dc8 100644 --- a/src/shared/experiments.ts +++ b/src/shared/experiments.ts @@ -1,19 +1,22 @@ -export interface ExperimentConfig { - id: string - name: string - description: string - enabled: boolean -} - export const EXPERIMENT_IDS = { DIFF_STRATEGY: "experimentalDiffStrategy", SEARCH_AND_REPLACE: "search_and_replace", INSERT_BLOCK: "insert_code_block", } as const -export type ExperimentId = keyof typeof EXPERIMENT_IDS +export type ExperimentKey = keyof typeof EXPERIMENT_IDS +export type ExperimentId = valueof -export const experimentConfigsMap: Record = { +export interface ExperimentConfig { + id: ExperimentId + name: string + description: string + enabled: boolean +} + +type valueof = X[keyof X] + +export const experimentConfigsMap: Record = { DIFF_STRATEGY: { id: EXPERIMENT_IDS.DIFF_STRATEGY, name: "Use experimental unified diff strategy", @@ -42,13 +45,13 @@ export const experimentConfigsMap: Record = { export const experimentConfigs = Object.values(experimentConfigsMap) export const experimentDefault = Object.fromEntries( Object.entries(experimentConfigsMap).map(([_, config]) => [config.id, config.enabled]), -) +) as Record export const experiments = { - get: (id: ExperimentId): ExperimentConfig | undefined => { + get: (id: ExperimentKey): ExperimentConfig | undefined => { return experimentConfigsMap[id] }, - isEnabled: (experimentsConfig: Record, id: string): boolean => { + isEnabled: (experimentsConfig: Record, id: ExperimentId): boolean => { return experimentsConfig[id] ?? experimentDefault[id] }, } as const diff --git a/webview-ui/src/context/ExtensionStateContext.tsx b/webview-ui/src/context/ExtensionStateContext.tsx index 76c5ad92ce..9738edfdc4 100644 --- a/webview-ui/src/context/ExtensionStateContext.tsx +++ b/webview-ui/src/context/ExtensionStateContext.tsx @@ -16,7 +16,7 @@ import { McpServer } from "../../../src/shared/mcp" import { checkExistKey } from "../../../src/shared/checkExistApiConfig" import { Mode, CustomModePrompts, defaultModeSlug, defaultPrompts, ModeConfig } from "../../../src/shared/modes" import { CustomSupportPrompts } from "../../../src/shared/support-prompt" -import { experimentDefault } from "../../../src/shared/experiments" +import { experimentDefault, ExperimentId } from "../../../src/shared/experiments" export interface ExtensionStateContextType extends ExtensionState { didHydrateState: boolean @@ -64,8 +64,7 @@ export interface ExtensionStateContextType extends ExtensionState { setCustomSupportPrompts: (value: CustomSupportPrompts) => void enhancementApiConfigId?: string setEnhancementApiConfigId: (value: string) => void - experiments: Record - setExperimentEnabled: (id: string, enabled: boolean) => void + setExperimentEnabled: (id: ExperimentId, enabled: boolean) => void setAutoApprovalEnabled: (value: boolean) => void handleInputChange: (field: keyof ApiConfiguration) => (event: any) => void customModes: ModeConfig[] From 681fe4a95660804aa4a1a540a3d9415777d7b518 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Mon, 27 Jan 2025 15:01:49 +0700 Subject: [PATCH 14/32] chore: update description for exp tool --- src/shared/experiments.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/shared/experiments.ts b/src/shared/experiments.ts index 90805a2dc8..962a32b01c 100644 --- a/src/shared/experiments.ts +++ b/src/shared/experiments.ts @@ -28,7 +28,7 @@ export const experimentConfigsMap: Record = { id: EXPERIMENT_IDS.SEARCH_AND_REPLACE, name: "Use experimental search and replace tool", description: - "Enable the experimental Search and Replace tool. This tool allows Roo to search and replace term. Can be run multiple search and replace in sequence at once request.", + "Enable the experimental search and replace tool, allowing Roo to replace multiple instances of a search term in one request.", enabled: false, }, INSERT_BLOCK: { @@ -36,7 +36,7 @@ export const experimentConfigsMap: Record = { name: "Use experimental insert block tool", description: - "Enable the experimental insert block tool. This tool allows Roo to insert code blocks into files. Can be insert multiple blocks at once.", + "Enable the experimental insert block tool, allowing Roo to insert multiple code blocks at once at specific line numbers without needing to create a diff.", enabled: false, }, } From 7dd161824baf880950d91a0b858c72fe755d9a32 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Mon, 27 Jan 2025 15:04:26 +0700 Subject: [PATCH 15/32] refactor: remove redundant experimentConfigs array, use experimentConfigsMap directly --- src/core/webview/ClineProvider.ts | 8 +------- src/shared/experiments.ts | 2 -- webview-ui/src/components/settings/SettingsView.tsx | 4 ++-- 3 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index 09f4e7742f..d2db67ff4b 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -40,13 +40,7 @@ import { singleCompletionHandler } from "../../utils/single-completion-handler" import { getCommitInfo, searchCommits, getWorkingState } from "../../utils/git" import { ConfigManager } from "../config/ConfigManager" import { CustomModesManager } from "../config/CustomModesManager" -import { - EXPERIMENT_IDS, - experimentConfigs, - experiments as Experiments, - experimentDefault, - ExperimentId, -} from "../../shared/experiments" +import { EXPERIMENT_IDS, experiments as Experiments, experimentDefault, ExperimentId } from "../../shared/experiments" import { CustomSupportPrompts, supportPrompt } from "../../shared/support-prompt" import { ACTION_NAMES } from "../CodeActionProvider" diff --git a/src/shared/experiments.ts b/src/shared/experiments.ts index 962a32b01c..0418334f6c 100644 --- a/src/shared/experiments.ts +++ b/src/shared/experiments.ts @@ -41,8 +41,6 @@ export const experimentConfigsMap: Record = { }, } -// Keep the array version for backward compatibility -export const experimentConfigs = Object.values(experimentConfigsMap) export const experimentDefault = Object.fromEntries( Object.entries(experimentConfigsMap).map(([_, config]) => [config.id, config.enabled]), ) as Record diff --git a/webview-ui/src/components/settings/SettingsView.tsx b/webview-ui/src/components/settings/SettingsView.tsx index b78abd89cf..0db367fc98 100644 --- a/webview-ui/src/components/settings/SettingsView.tsx +++ b/webview-ui/src/components/settings/SettingsView.tsx @@ -5,7 +5,7 @@ import { validateApiConfiguration, validateModelId } from "../../utils/validate" import { vscode } from "../../utils/vscode" import ApiOptions from "./ApiOptions" import ExperimentalFeature from "./ExperimentalFeature" -import { experimentConfigs, EXPERIMENT_IDS, experimentConfigsMap } from "../../../../src/shared/experiments" +import { EXPERIMENT_IDS, experimentConfigsMap } from "../../../../src/shared/experiments" import ApiConfigManager from "./ApiConfigManager" type SettingsViewProps = { @@ -646,7 +646,7 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {

)} - {experimentConfigs + {Object.values(experimentConfigsMap) .filter((config) => config.id !== EXPERIMENT_IDS.DIFF_STRATEGY) .map((config) => ( Date: Mon, 27 Jan 2025 15:30:35 +0700 Subject: [PATCH 16/32] refactor(experiments): simplify experiment config structure - Remove redundant id field from ExperimentConfig interface - Update UI components to use experiment keys directly - Improve type safety by using key-based mapping instead of object values --- src/shared/experiments.ts | 19 +++++++++------ .../settings/ExperimentalFeature.tsx | 3 +-- .../src/components/settings/SettingsView.tsx | 23 +++++++++++++------ 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/shared/experiments.ts b/src/shared/experiments.ts index 0418334f6c..71eda740d3 100644 --- a/src/shared/experiments.ts +++ b/src/shared/experiments.ts @@ -8,7 +8,6 @@ export type ExperimentKey = keyof typeof EXPERIMENT_IDS export type ExperimentId = valueof export interface ExperimentConfig { - id: ExperimentId name: string description: string enabled: boolean @@ -18,21 +17,18 @@ type valueof = X[keyof X] export const experimentConfigsMap: Record = { DIFF_STRATEGY: { - id: EXPERIMENT_IDS.DIFF_STRATEGY, name: "Use experimental unified diff strategy", description: "Enable the experimental unified diff strategy. This strategy might reduce the number of retries caused by model errors but may cause unexpected behavior or incorrect edits. Only enable if you understand the risks and are willing to carefully review all changes.", enabled: false, }, SEARCH_AND_REPLACE: { - id: EXPERIMENT_IDS.SEARCH_AND_REPLACE, name: "Use experimental search and replace tool", description: "Enable the experimental search and replace tool, allowing Roo to replace multiple instances of a search term in one request.", enabled: false, }, INSERT_BLOCK: { - id: EXPERIMENT_IDS.INSERT_BLOCK, name: "Use experimental insert block tool", description: @@ -42,7 +38,10 @@ export const experimentConfigsMap: Record = { } export const experimentDefault = Object.fromEntries( - Object.entries(experimentConfigsMap).map(([_, config]) => [config.id, config.enabled]), + Object.entries(experimentConfigsMap).map(([_, config]) => [ + EXPERIMENT_IDS[_ as keyof typeof EXPERIMENT_IDS] as ExperimentId, + config.enabled, + ]), ) as Record export const experiments = { @@ -56,9 +55,15 @@ export const experiments = { // Expose experiment details for UI - pre-compute from map for better performance export const experimentLabels = Object.fromEntries( - Object.values(experimentConfigsMap).map((config) => [config.id, config.name]), + Object.entries(experimentConfigsMap).map(([_, config]) => [ + EXPERIMENT_IDS[_ as keyof typeof EXPERIMENT_IDS] as ExperimentId, + config.name, + ]), ) as Record export const experimentDescriptions = Object.fromEntries( - Object.values(experimentConfigsMap).map((config) => [config.id, config.description]), + Object.entries(experimentConfigsMap).map(([_, config]) => [ + EXPERIMENT_IDS[_ as keyof typeof EXPERIMENT_IDS] as ExperimentId, + config.description, + ]), ) as Record diff --git a/webview-ui/src/components/settings/ExperimentalFeature.tsx b/webview-ui/src/components/settings/ExperimentalFeature.tsx index b5542b9f0e..b896e8e989 100644 --- a/webview-ui/src/components/settings/ExperimentalFeature.tsx +++ b/webview-ui/src/components/settings/ExperimentalFeature.tsx @@ -1,14 +1,13 @@ import { VSCodeCheckbox } from "@vscode/webview-ui-toolkit/react" interface ExperimentalFeatureProps { - id: string name: string description: string enabled: boolean onChange: (value: boolean) => void } -const ExperimentalFeature = ({ id, name, description, enabled, onChange }: ExperimentalFeatureProps) => { +const ExperimentalFeature = ({ name, description, enabled, onChange }: ExperimentalFeatureProps) => { return (
{ apiConfiguration, }) + console.log("Experiments", experiments) + vscode.postMessage({ type: "updateExperimental", values: experiments, @@ -646,14 +648,21 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {

)} - {Object.values(experimentConfigsMap) - .filter((config) => config.id !== EXPERIMENT_IDS.DIFF_STRATEGY) + {Object.entries(experimentConfigsMap) + .filter((config) => config[0] !== "DIFF_STRATEGY") .map((config) => ( setExperimentEnabled(config.id, enabled)} + key={config[0]} + {...config[1]} + enabled={ + experiments[EXPERIMENT_IDS[config[0] as keyof typeof EXPERIMENT_IDS]] ?? false + } + onChange={(enabled) => + setExperimentEnabled( + EXPERIMENT_IDS[config[0] as keyof typeof EXPERIMENT_IDS], + enabled, + ) + } /> ))}
From 0fa6fd4ddbb8577bbc834605d4953b38c15fe302 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Mon, 27 Jan 2025 15:31:33 +0700 Subject: [PATCH 17/32] chore: remove unused --- webview-ui/src/components/settings/SettingsView.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webview-ui/src/components/settings/SettingsView.tsx b/webview-ui/src/components/settings/SettingsView.tsx index 9561a55cb8..51826c833c 100644 --- a/webview-ui/src/components/settings/SettingsView.tsx +++ b/webview-ui/src/components/settings/SettingsView.tsx @@ -5,7 +5,7 @@ import { validateApiConfiguration, validateModelId } from "../../utils/validate" import { vscode } from "../../utils/vscode" import ApiOptions from "./ApiOptions" import ExperimentalFeature from "./ExperimentalFeature" -import { EXPERIMENT_IDS, experimentConfigsMap, ExperimentId, ExperimentKey } from "../../../../src/shared/experiments" +import { EXPERIMENT_IDS, experimentConfigsMap } from "../../../../src/shared/experiments" import ApiConfigManager from "./ApiConfigManager" type SettingsViewProps = { From 179ea7904b8e18ab48548e215a400e5aab699383 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Tue, 28 Jan 2025 11:18:51 +0700 Subject: [PATCH 18/32] update with comment in pr --- src/core/prompts/tools/insert-code-block.ts | 2 +- src/shared/modes.ts | 2 +- webview-ui/src/components/settings/SettingsView.tsx | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/core/prompts/tools/insert-code-block.ts b/src/core/prompts/tools/insert-code-block.ts index f6c6a32b87..16855958bc 100644 --- a/src/core/prompts/tools/insert-code-block.ts +++ b/src/core/prompts/tools/insert-code-block.ts @@ -6,7 +6,7 @@ Description: Inserts code blocks at specific line positions in a file. This is t Parameters: - path: (required) The path of the file to insert code into (relative to the current working directory ${args.cwd.toPosix()}) - operations: (required) A JSON array of insertion operations. Each operation is an object with: - * start_line: (required) The line number where the code block should be inserted + * start_line: (required) The line number where the code block should be inserted. The content currently at that line will end up below the inserted code block. * content: (required) The code block to insert at the specified position Usage: diff --git a/src/shared/modes.ts b/src/shared/modes.ts index 012ee63945..abbeea72b7 100644 --- a/src/shared/modes.ts +++ b/src/shared/modes.ts @@ -205,7 +205,7 @@ export function isToolAllowedForMode( const filePath = toolParams?.path if ( filePath && - (toolParams.diff || toolParams.content) && + (toolParams.diff || toolParams.content || toolParams.operations) && !doesFileMatchRegex(filePath, options.fileRegex) ) { throw new FileRestrictionError(mode.name, options.fileRegex, options.description, filePath) diff --git a/webview-ui/src/components/settings/SettingsView.tsx b/webview-ui/src/components/settings/SettingsView.tsx index 51826c833c..64b762a68a 100644 --- a/webview-ui/src/components/settings/SettingsView.tsx +++ b/webview-ui/src/components/settings/SettingsView.tsx @@ -97,8 +97,6 @@ const SettingsView = ({ onDone }: SettingsViewProps) => { apiConfiguration, }) - console.log("Experiments", experiments) - vscode.postMessage({ type: "updateExperimental", values: experiments, From 4e3ea695d820cbedaf49d9f07a743c7c09a91676 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Tue, 28 Jan 2025 15:34:56 +0700 Subject: [PATCH 19/32] update rule & prompt for insert-code-block --- src/core/prompts/sections/rules.ts | 3 +++ src/core/prompts/system.ts | 2 +- src/core/prompts/tools/insert-code-block.ts | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/prompts/sections/rules.ts b/src/core/prompts/sections/rules.ts index cfb8a24d04..09719931a7 100644 --- a/src/core/prompts/sections/rules.ts +++ b/src/core/prompts/sections/rules.ts @@ -8,6 +8,7 @@ export function getRulesSection( supportsComputerUse: boolean, diffStrategy?: DiffStrategy, context?: vscode.ExtensionContext, + experiments?: Record | undefined, ): string { const settingsDir = context ? path.join(context.globalStorageUri.fsPath, "settings") : "" const customModesPath = path.join(settingsDir, "cline_custom_modes.json") @@ -26,6 +27,8 @@ ${ ? "- You should use apply_diff instead of write_to_file when making changes to existing files since it is much faster and easier to apply a diff than to write the entire file again. Only use write_to_file to edit files when apply_diff has failed repeatedly to apply the diff." : "- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool." } +${experiments?.["insert_code_block"] === true ? "- Use the insert_code_block tool to add code snippets or content block to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location." : ""} +${experiments?.["search_and_replace"] === true ? "- Use the search_and_replace tool to find and replace text or regex in files. This tool allows you to search for a specific regex pattern or text and replace it with another value. Be cautious when using this tool to ensure you are replacing the correct text." : ""} - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" diff --git a/src/core/prompts/system.ts b/src/core/prompts/system.ts index 9b8e49f4e6..8ae0e40f4a 100644 --- a/src/core/prompts/system.ts +++ b/src/core/prompts/system.ts @@ -80,7 +80,7 @@ ${getCapabilitiesSection(cwd, supportsComputerUse, mcpHub, effectiveDiffStrategy ${modesSection} -${getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, context)} +${getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, context, experiments)} ${getSystemInfoSection(cwd, mode, customModeConfigs)} diff --git a/src/core/prompts/tools/insert-code-block.ts b/src/core/prompts/tools/insert-code-block.ts index 16855958bc..7dce56e0f9 100644 --- a/src/core/prompts/tools/insert-code-block.ts +++ b/src/core/prompts/tools/insert-code-block.ts @@ -7,7 +7,7 @@ Parameters: - path: (required) The path of the file to insert code into (relative to the current working directory ${args.cwd.toPosix()}) - operations: (required) A JSON array of insertion operations. Each operation is an object with: * start_line: (required) The line number where the code block should be inserted. The content currently at that line will end up below the inserted code block. - * content: (required) The code block to insert at the specified position + * content: (required) The code block to insert at the specified position. IMPORTANT NOTE: If the content is a single line, it can be a string. If it's a multi-line content, it should be a string with newline characters (\n) for line breaks. Usage: File path here From 9b175a736ed42da528f0bd009c1b864c75fe5ac3 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Tue, 28 Jan 2025 15:36:39 +0700 Subject: [PATCH 20/32] update prompt for search and replace tool --- src/core/prompts/tools/search-and-replace.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/prompts/tools/search-and-replace.ts b/src/core/prompts/tools/search-and-replace.ts index 590cb35726..eb48d8585b 100644 --- a/src/core/prompts/tools/search-and-replace.ts +++ b/src/core/prompts/tools/search-and-replace.ts @@ -7,7 +7,7 @@ Parameters: - path: (required) The path of the file to modify (relative to the current working directory ${args.cwd.toPosix()}) - operations: (required) A JSON array of search/replace operations. Each operation is an object with: * search: (required) The text or pattern to search for - * replace: (required) The text to replace matches with + * replace: (required) The text to replace matches with. If multiple lines need to be replaced, use "\n" for newlines * start_line: (optional) Starting line number for restricted replacement * end_line: (optional) Ending line number for restricted replacement * use_regex: (optional) Whether to treat search as a regex pattern From 411182a5d9fd8ef7e99c462ecf6842cc4759d6eb Mon Sep 17 00:00:00 2001 From: sam hoang Date: Tue, 28 Jan 2025 21:04:36 +0700 Subject: [PATCH 21/32] fix error in search and replace, update rule to utilize multiple operation --- src/core/Cline.ts | 34 ++++++++++++++++++------------ src/core/prompts/sections/rules.ts | 4 ++-- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 42358ec9cf..852f48c5c5 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -1707,35 +1707,41 @@ export class Cline { // Read the original file content const fileContent = await fs.readFile(absolutePath, "utf-8") - const lines = fileContent.split("\n") - let newContent = fileContent + let lines = fileContent.split("\n") - // Apply each search/replace operation for (const op of parsedOperations) { + const flags = op.regex_flags ?? (op.ignore_case ? "gi" : "g") + const multilineFlags = flags.includes("m") ? flags : flags + "m" + const searchPattern = op.use_regex - ? new RegExp(op.search, op.regex_flags || (op.ignore_case ? "gi" : "g")) - : new RegExp(escapeRegExp(op.search), op.ignore_case ? "gi" : "g") + ? new RegExp(op.search, multilineFlags) + : new RegExp(escapeRegExp(op.search), multilineFlags) if (op.start_line || op.end_line) { - // Line-restricted replacement - const startLine = (op.start_line || 1) - 1 - const endLine = (op.end_line || lines.length) - 1 + const startLine = Math.max((op.start_line ?? 1) - 1, 0) + const endLine = Math.min((op.end_line ?? lines.length) - 1, lines.length - 1) + // Get the content before and after the target section const beforeLines = lines.slice(0, startLine) - const targetLines = lines.slice(startLine, endLine + 1) const afterLines = lines.slice(endLine + 1) - const modifiedLines = targetLines.map((line) => - line.replace(searchPattern, op.replace), - ) + // Get the target section and perform replacement + const targetContent = lines.slice(startLine, endLine + 1).join("\n") + const modifiedContent = targetContent.replace(searchPattern, op.replace) + const modifiedLines = modifiedContent.split("\n") - newContent = [...beforeLines, ...modifiedLines, ...afterLines].join("\n") + // Reconstruct the full content with the modified section + lines = [...beforeLines, ...modifiedLines, ...afterLines] } else { // Global replacement - newContent = newContent.replace(searchPattern, op.replace) + const fullContent = lines.join("\n") + const modifiedContent = fullContent.replace(searchPattern, op.replace) + lines = modifiedContent.split("\n") } } + const newContent = lines.join("\n") + this.consecutiveMistakeCount = 0 // Show diff preview diff --git a/src/core/prompts/sections/rules.ts b/src/core/prompts/sections/rules.ts index 09719931a7..6e11c09f2e 100644 --- a/src/core/prompts/sections/rules.ts +++ b/src/core/prompts/sections/rules.ts @@ -27,8 +27,8 @@ ${ ? "- You should use apply_diff instead of write_to_file when making changes to existing files since it is much faster and easier to apply a diff than to write the entire file again. Only use write_to_file to edit files when apply_diff has failed repeatedly to apply the diff." : "- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool." } -${experiments?.["insert_code_block"] === true ? "- Use the insert_code_block tool to add code snippets or content block to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location." : ""} -${experiments?.["search_and_replace"] === true ? "- Use the search_and_replace tool to find and replace text or regex in files. This tool allows you to search for a specific regex pattern or text and replace it with another value. Be cautious when using this tool to ensure you are replacing the correct text." : ""} +${experiments?.["insert_code_block"] === true ? "- Use the insert_code_block tool to add code snippets or content block to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location. It's can support multiple operations" : ""} +${experiments?.["search_and_replace"] === true ? "- Use the search_and_replace tool to find and replace text or regex in files. This tool allows you to search for a specific regex pattern or text and replace it with another value. Be cautious when using this tool to ensure you are replacing the correct text. It's can support multiple operations" : ""} - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" From e7ff4ed3974a3cacb12f18f686039d10474ae4a5 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Tue, 28 Jan 2025 21:47:24 +0700 Subject: [PATCH 22/32] fix missing in make input lag --- webview-ui/src/components/settings/OpenAiModelPicker.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/webview-ui/src/components/settings/OpenAiModelPicker.tsx b/webview-ui/src/components/settings/OpenAiModelPicker.tsx index 611fd70ef9..721c45d183 100644 --- a/webview-ui/src/components/settings/OpenAiModelPicker.tsx +++ b/webview-ui/src/components/settings/OpenAiModelPicker.tsx @@ -25,6 +25,7 @@ const OpenAiModelPicker: React.FC = () => { } setApiConfiguration(apiConfig) onUpdateApiConfig(apiConfig) + setSearchTerm(newModelId) } useEffect(() => { From eef34c03dce3a4eecb026d486e1f0bdaf4510829 Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Tue, 28 Jan 2025 10:11:00 -0500 Subject: [PATCH 23/32] Update prompts and fix tests --- .../__snapshots__/system.test.ts.snap | 42 ++++--------- src/core/prompts/__tests__/system.test.ts | 59 +++++++++++++++++++ src/core/prompts/sections/rules.ts | 59 +++++++++++++++---- src/core/prompts/system.ts | 2 +- 4 files changed, 119 insertions(+), 43 deletions(-) diff --git a/src/core/prompts/__tests__/__snapshots__/system.test.ts.snap b/src/core/prompts/__tests__/__snapshots__/system.test.ts.snap index 8801e45d36..926a03209c 100644 --- a/src/core/prompts/__tests__/__snapshots__/system.test.ts.snap +++ b/src/core/prompts/__tests__/__snapshots__/system.test.ts.snap @@ -248,11 +248,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -264,7 +263,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. @@ -553,11 +551,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -569,7 +566,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. @@ -858,11 +854,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -874,7 +869,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. @@ -1211,11 +1205,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -1228,7 +1221,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. Then if you want to test your work, you might use browser_action to launch the site, wait for the user's response confirming the site was launched along with a screenshot, then perhaps e.g., click a button to test functionality if needed, wait for the user's response confirming the button was clicked along with a screenshot of the new state, before finally closing the browser. @@ -1930,11 +1922,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -1946,7 +1937,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. @@ -2283,11 +2273,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -2300,7 +2289,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. Then if you want to test your work, you might use browser_action to launch the site, wait for the user's response confirming the site was launched along with a screenshot, then perhaps e.g., click a button to test functionality if needed, wait for the user's response confirming the button was clicked along with a screenshot of the new state, before finally closing the browser. @@ -2649,11 +2637,12 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- You should use apply_diff instead of write_to_file when making changes to existing files since it is much faster and easier to apply a diff than to write the entire file again. Only use write_to_file to edit files when apply_diff has failed repeatedly to apply the diff. +- For editing files, you have access to these tools: write_to_file (for creating new files or complete file rewrites), apply_diff (for replacing lines in existing files). +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. +- You should always prefer using other editing tools over write_to_file when making changes to existing files since write_to_file is much slower and cannot handle large files. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -2665,7 +2654,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. @@ -2954,11 +2942,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -2970,7 +2957,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. @@ -3302,11 +3288,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -3318,7 +3303,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. @@ -3593,11 +3577,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '/test/path', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '/test/path'). For example, if you needed to run \`npm install\` in a project outside of '/test/path', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool. +- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -3609,7 +3592,6 @@ RULES - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc. diff --git a/src/core/prompts/__tests__/system.test.ts b/src/core/prompts/__tests__/system.test.ts index cf3cb329cd..276a06eb34 100644 --- a/src/core/prompts/__tests__/system.test.ts +++ b/src/core/prompts/__tests__/system.test.ts @@ -518,6 +518,65 @@ describe("SYSTEM_PROMPT", () => { expect(prompt).toContain(EXPERIMENT_IDS.SEARCH_AND_REPLACE) expect(prompt).not.toContain(EXPERIMENT_IDS.INSERT_BLOCK) }) + + it("should list all available editing tools in base instruction", async () => { + const experiments = { + [EXPERIMENT_IDS.SEARCH_AND_REPLACE]: true, + [EXPERIMENT_IDS.INSERT_BLOCK]: true, + } + + const prompt = await SYSTEM_PROMPT( + mockContext, + "/test/path", + false, + undefined, + new SearchReplaceDiffStrategy(), + undefined, + defaultModeSlug, + undefined, + undefined, + undefined, + undefined, + true, // diffEnabled + experiments, + ) + + // Verify base instruction lists all available tools + expect(prompt).toContain("apply_diff (for replacing lines in existing files)") + expect(prompt).toContain("write_to_file (for creating new files or complete file rewrites)") + expect(prompt).toContain("insert_code_block (for adding sections to existing files)") + expect(prompt).toContain("search_and_replace (for finding and replacing individual pieces of text)") + }) + + it("should provide detailed instructions for each enabled tool", async () => { + const experiments = { + [EXPERIMENT_IDS.SEARCH_AND_REPLACE]: true, + [EXPERIMENT_IDS.INSERT_BLOCK]: true, + } + + const prompt = await SYSTEM_PROMPT( + mockContext, + "/test/path", + false, + undefined, + new SearchReplaceDiffStrategy(), + undefined, + defaultModeSlug, + undefined, + undefined, + undefined, + undefined, + true, + experiments, + ) + + // Verify detailed instructions for each tool + expect(prompt).toContain( + "You should always prefer using other editing tools over write_to_file when making changes to existing files since write_to_file is much slower and cannot handle large files.", + ) + expect(prompt).toContain("The insert_code_block tool adds code snippets or content blocks to files") + expect(prompt).toContain("The search_and_replace tool finds and replaces text or regex in files") + }) }) afterAll(() => { diff --git a/src/core/prompts/sections/rules.ts b/src/core/prompts/sections/rules.ts index 6e11c09f2e..cb944a14a5 100644 --- a/src/core/prompts/sections/rules.ts +++ b/src/core/prompts/sections/rules.ts @@ -3,15 +3,58 @@ import { modes, ModeConfig } from "../../../shared/modes" import * as vscode from "vscode" import * as path from "path" +function getEditingInstructions(diffStrategy?: DiffStrategy, experiments?: Record): string { + const instructions: string[] = [] + const availableTools: string[] = ["write_to_file (for creating new files or complete file rewrites)"] + + // Collect available editing tools + if (diffStrategy) { + availableTools.push("apply_diff (for replacing lines in existing files)") + } + if (experiments?.["insert_code_block"]) { + availableTools.push("insert_code_block (for adding sections to existing files)") + } + if (experiments?.["search_and_replace"]) { + availableTools.push("search_and_replace (for finding and replacing individual pieces of text)") + } + + // Base editing instruction mentioning all available tools + if (availableTools.length > 1) { + instructions.push(`- For editing files, you have access to these tools: ${availableTools.join(", ")}.`) + } + + // Additional details for experimental features + if (experiments?.["insert_code_block"]) { + instructions.push( + "- The insert_code_block tool adds code snippets or content blocks to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location. It can support multiple operations at once.", + ) + } + + if (experiments?.["search_and_replace"]) { + instructions.push( + "- The search_and_replace tool finds and replaces text or regex in files. This tool allows you to search for a specific regex pattern or text and replace it with another value. Be cautious when using this tool to ensure you are replacing the correct text. It can support multiple operations at once.", + ) + } + + instructions.push( + "- When using the write_to_file tool to modify a file, use the tool directly with the desired content. You do not need to display the content before using the tool. ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project.", + ) + + if (availableTools.length > 1) { + instructions.push( + "- You should always prefer using other editing tools over write_to_file when making changes to existing files since write_to_file is much slower and cannot handle large files.", + ) + } + + return instructions.join("\n") +} + export function getRulesSection( cwd: string, supportsComputerUse: boolean, diffStrategy?: DiffStrategy, - context?: vscode.ExtensionContext, experiments?: Record | undefined, ): string { - const settingsDir = context ? path.join(context.globalStorageUri.fsPath, "settings") : "" - const customModesPath = path.join(settingsDir, "cline_custom_modes.json") return `==== RULES @@ -22,17 +65,10 @@ RULES - Before using the execute_command tool, you must first think about the SYSTEM INFORMATION context provided to understand the user's environment and tailor your commands to ensure they are compatible with their system. You must also consider if the command you need to run should be executed in a specific directory outside of the current working directory '${cwd.toPosix()}', and if so prepend with \`cd\`'ing into that directory && then executing the command (as one command since you are stuck operating from '${cwd.toPosix()}'). For example, if you needed to run \`npm install\` in a project outside of '${cwd.toPosix()}', you would need to prepend with a \`cd\` i.e. pseudocode for this would be \`cd (path to project) && (command, in this case npm install)\`. - When using the search_files tool, craft your regex patterns carefully to balance specificity and flexibility. Based on the user's task you may use it to find code patterns, TODO comments, function definitions, or any text-based information across the project. The results include context, so analyze the surrounding code to better understand the matches. Leverage the search_files tool in combination with other tools for more comprehensive analysis. For example, use it to find specific code patterns, then use read_file to examine the full context of interesting matches before using write_to_file to make informed changes. - When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when writing files, as the write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser. -${ - diffStrategy - ? "- You should use apply_diff instead of write_to_file when making changes to existing files since it is much faster and easier to apply a diff than to write the entire file again. Only use write_to_file to edit files when apply_diff has failed repeatedly to apply the diff." - : "- When you want to modify a file, use the write_to_file tool directly with the desired content. You do not need to display the content before using the tool." -} -${experiments?.["insert_code_block"] === true ? "- Use the insert_code_block tool to add code snippets or content block to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location. It's can support multiple operations" : ""} -${experiments?.["search_and_replace"] === true ? "- Use the search_and_replace tool to find and replace text or regex in files. This tool allows you to search for a specific regex pattern or text and replace it with another value. Be cautious when using this tool to ensure you are replacing the correct text. It's can support multiple operations" : ""} +${getEditingInstructions(diffStrategy, experiments)} - Some modes have restrictions on which files they can edit. If you attempt to edit a restricted file, the operation will be rejected with a FileRestrictionError that will specify which file patterns are allowed for the current mode. - Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. * For example, in architect mode trying to edit app.js would be rejected because architect mode can only edit files matching "\\.md$" -- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write. - When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices. - Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the attempt_completion tool to present the result to the user. The user may provide feedback, which you can use to make improvements and try again. - You are only allowed to ask the user questions using the ask_followup_question tool. Use this tool only when you need additional details to complete a task, and be sure to use a clear and concise question that will help you move forward with the task. However if you can use the available tools to avoid having to ask the user questions, you should do so. For example, if the user mentions a file that may be in an outside directory like the Desktop, you should use the list_files tool to list the files in the Desktop and check if the file they are talking about is there, rather than asking the user to provide the file path themselves. @@ -48,7 +84,6 @@ ${experiments?.["search_and_replace"] === true ? "- Use the search_and_replace t - When presented with images, utilize your vision capabilities to thoroughly examine them and extract meaningful information. Incorporate these insights into your thought process as you accomplish the user's task. - At the end of each user message, you will automatically receive environment_details. This information is not written by the user themselves, but is auto-generated to provide potentially relevant context about the project structure and environment. While this information can be valuable for understanding the project context, do not treat it as a direct part of the user's request or response. Use it to inform your actions and decisions, but don't assume the user is explicitly asking about or referring to this information unless they clearly do so in their message. When using environment_details, explain your actions clearly to ensure the user understands, as they may not be aware of these details. - Before executing commands, check the "Actively Running Terminals" section in environment_details. If present, consider how these active processes might impact your task. For example, if a local development server is already running, you wouldn't need to start it again. If no active terminals are listed, proceed with command execution as normal. -- When using the write_to_file tool, ALWAYS provide the COMPLETE file content in your response. This is NON-NEGOTIABLE. Partial updates or placeholders like '// rest of code unchanged' are STRICTLY FORBIDDEN. You MUST include ALL parts of the file, even if they haven't been modified. Failure to do so will result in incomplete or broken code, severely impacting the user's project. - MCP operations should be used one at a time, similar to other tool usage. Wait for confirmation of success before proceeding with additional operations. - It is critical you wait for the user's response after each tool use, in order to confirm the success of the tool use. For example, if asked to make a todo app, you would create a file, wait for the user's response it was created successfully, then create another file if needed, wait for the user's response it was created successfully, etc.${ supportsComputerUse diff --git a/src/core/prompts/system.ts b/src/core/prompts/system.ts index 8ae0e40f4a..f579771d3d 100644 --- a/src/core/prompts/system.ts +++ b/src/core/prompts/system.ts @@ -80,7 +80,7 @@ ${getCapabilitiesSection(cwd, supportsComputerUse, mcpHub, effectiveDiffStrategy ${modesSection} -${getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, context, experiments)} +${getRulesSection(cwd, supportsComputerUse, effectiveDiffStrategy, experiments)} ${getSystemInfoSection(cwd, mode, customModeConfigs)} From 003bb5cabc52d7d26fd59160dd0df5c113e36ad8 Mon Sep 17 00:00:00 2001 From: sam hoang Date: Tue, 28 Jan 2025 22:13:51 +0700 Subject: [PATCH 24/32] fix diffview for search-and-replace tool and insert tool --- src/core/Cline.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 852f48c5c5..8895b1f84a 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -1573,11 +1573,7 @@ export class Cline { await delay(200) } - const diff = formatResponse.createPrettyPatch( - relPath, - this.diffViewProvider.originalContent, - updatedContent, - ) + const diff = formatResponse.createPrettyPatch(relPath, fileContent, updatedContent) if (!diff) { pushToolResult(`No changes needed for '${relPath}'`) @@ -1707,6 +1703,8 @@ export class Cline { // Read the original file content const fileContent = await fs.readFile(absolutePath, "utf-8") + this.diffViewProvider.editType = "modify" + this.diffViewProvider.originalContent = fileContent let lines = fileContent.split("\n") for (const op of parsedOperations) { @@ -1745,11 +1743,7 @@ export class Cline { this.consecutiveMistakeCount = 0 // Show diff preview - const diff = formatResponse.createPrettyPatch( - relPath, - this.diffViewProvider.originalContent, - newContent, - ) + const diff = formatResponse.createPrettyPatch(relPath, fileContent, newContent) if (!diff) { pushToolResult(`No changes needed for '${relPath}'`) From 5579922f98bf24a4528bb9a8376a260afaf1765d Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Tue, 28 Jan 2025 10:24:16 -0500 Subject: [PATCH 25/32] Sneak in a change to apply_diff error message --- src/core/diff/strategies/search-replace.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/diff/strategies/search-replace.ts b/src/core/diff/strategies/search-replace.ts index 1ede3c3238..959f9493b0 100644 --- a/src/core/diff/strategies/search-replace.ts +++ b/src/core/diff/strategies/search-replace.ts @@ -273,7 +273,7 @@ Your search/replace content here : "" return { success: false, - error: `No sufficiently similar match found${lineRange} (${Math.floor(bestMatchScore * 100)}% similar, needs ${Math.floor(this.fuzzyThreshold * 100)}%)\n\nDebug Info:\n- Similarity Score: ${Math.floor(bestMatchScore * 100)}%\n- Required Threshold: ${Math.floor(this.fuzzyThreshold * 100)}%\n- Search Range: ${startLine && endLine ? `lines ${startLine}-${endLine}` : "start to end"}\n\nSearch Content:\n${searchChunk}${bestMatchSection}${originalContentSection}`, + error: `No sufficiently similar match found${lineRange} (${Math.floor(bestMatchScore * 100)}% similar, needs ${Math.floor(this.fuzzyThreshold * 100)}%)\n\nDebug Info:\n- Similarity Score: ${Math.floor(bestMatchScore * 100)}%\n- Required Threshold: ${Math.floor(this.fuzzyThreshold * 100)}%\n- Search Range: ${startLine && endLine ? `lines ${startLine}-${endLine}` : "start to end"}\n- Tip: Use read_file to get the latest content of the file before attempting the diff again, as the file content may have changed\n\nSearch Content:\n${searchChunk}${bestMatchSection}${originalContentSection}`, } } From bd0a613be1f18c6fe3de002a05671d26a64931fc Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Tue, 28 Jan 2025 11:11:28 -0500 Subject: [PATCH 26/32] Add a delay to allow auto approved mode changes to take effect --- src/core/Cline.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 859d2d5484..acbb6c4c8d 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -2072,6 +2072,7 @@ export class Cline { targetMode.name } mode${reason ? ` because: ${reason}` : ""}.`, ) + await delay(500) // delay to allow mode change to take effect before next tool is executed break } } catch (error) { From 68035507afa3166e8d049b0b9cc4ae5683999941 Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Tue, 28 Jan 2025 13:22:16 -0500 Subject: [PATCH 27/32] v3.3.5 --- .changeset/khaki-zoos-share.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/khaki-zoos-share.md diff --git a/.changeset/khaki-zoos-share.md b/.changeset/khaki-zoos-share.md new file mode 100644 index 0000000000..e3ba48928d --- /dev/null +++ b/.changeset/khaki-zoos-share.md @@ -0,0 +1,5 @@ +--- +"roo-cline": patch +--- + +v3.3.5 From f7cf08c6075d47fadf385195a44c0a797fb85306 Mon Sep 17 00:00:00 2001 From: MFPires Date: Tue, 28 Jan 2025 15:49:36 -0300 Subject: [PATCH 28/32] feat: Add context tokens to environment details Adds the current conversation's context token count to the environment details section by: - Using existing getApiMetrics() to retrieve contextTokens from the last API request - Adding a new "Context Tokens" section between "Current Time" and "Current Mode" - Formatting the token count with toLocaleString() for better readability - Showing "(Not available)" when no context tokens are present This provides visibility into the current conversation's token usage directly in the environment details, helping track context window utilization. --- src/core/Cline.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 86a4ec5093..f85bf18b61 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -3041,6 +3041,10 @@ export class Cline { const timeZoneOffsetStr = `${timeZoneOffset >= 0 ? "+" : ""}${timeZoneOffset}:00` details += `\n\n# Current Time\n${formatter.format(now)} (${timeZone}, UTC${timeZoneOffsetStr})` + // Add context tokens information + const { contextTokens } = getApiMetrics(this.clineMessages) + details += `\n\n# Current Context Size (Tokens)\n${contextTokens ? contextTokens.toLocaleString() : "(Not available)"}` + // Add current mode and any mode-specific warnings const { mode, customModes } = (await this.providerRef.deref()?.getState()) ?? {} const currentMode = mode ?? defaultModeSlug From ea6dc7c16e0c6becd60da82abc9a746ab3c32aa9 Mon Sep 17 00:00:00 2001 From: MFPires Date: Tue, 28 Jan 2025 16:33:06 -0300 Subject: [PATCH 29/32] feat(ui): add context window percentage to task header Display the percentage of total context window used alongside token count in the task header. This helps users better understand their context usage relative to the model's capacity. Implementation details: - Retrieve model's context window size from normalized API configuration - Calculate percentage of context window used based on current context tokens - Display percentage in parentheses next to token count (e.g. "40,000 (20%)") - Default to context window size of 1 if model info is unavailable - Format numbers with commas for better readability --- src/core/Cline.ts | 6 +++++- webview-ui/src/components/chat/TaskHeader.tsx | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/core/Cline.ts b/src/core/Cline.ts index f85bf18b61..82a4314856 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -3043,7 +3043,11 @@ export class Cline { // Add context tokens information const { contextTokens } = getApiMetrics(this.clineMessages) - details += `\n\n# Current Context Size (Tokens)\n${contextTokens ? contextTokens.toLocaleString() : "(Not available)"}` + const modelInfo = this.api.getModel().info + const contextWindow = modelInfo.contextWindow + const contextPercentage = + contextTokens && contextWindow ? Math.round((contextTokens / contextWindow) * 100) : undefined + details += `\n\n# Current Context Size (Tokens)\n${contextTokens ? `${contextTokens.toLocaleString()} (${contextPercentage}%)` : "(Not available)"}` // Add current mode and any mode-specific warnings const { mode, customModes } = (await this.providerRef.deref()?.getState()) ?? {} diff --git a/webview-ui/src/components/chat/TaskHeader.tsx b/webview-ui/src/components/chat/TaskHeader.tsx index 52b3756a54..f51bf13b0e 100644 --- a/webview-ui/src/components/chat/TaskHeader.tsx +++ b/webview-ui/src/components/chat/TaskHeader.tsx @@ -7,6 +7,7 @@ import { vscode } from "../../utils/vscode" import Thumbnails from "../common/Thumbnails" import { mentionRegexGlobal } from "../../../../src/shared/context-mentions" import { formatLargeNumber } from "../../utils/format" +import { normalizeApiConfiguration } from "../settings/ApiOptions" interface TaskHeaderProps { task: ClineMessage @@ -32,11 +33,14 @@ const TaskHeader: React.FC = ({ onClose, }) => { const { apiConfiguration } = useExtensionState() + const { selectedModelInfo } = useMemo(() => normalizeApiConfiguration(apiConfiguration), [apiConfiguration]) const [isTaskExpanded, setIsTaskExpanded] = useState(true) const [isTextExpanded, setIsTextExpanded] = useState(false) const [showSeeMore, setShowSeeMore] = useState(false) const textContainerRef = useRef(null) const textRef = useRef(null) + const contextWindow = selectedModelInfo?.contextWindow || 1 + const contextPercentage = Math.round((contextTokens / contextWindow) * 100) /* When dealing with event listeners in React components that depend on state variables, we face a challenge. We want our listener to always use the most up-to-date version of a callback function that relies on current state, but we don't want to constantly add and remove event listeners as that function updates. This scenario often arises with resize listeners or other window events. Simply adding the listener in a useEffect with an empty dependency array risks using stale state, while including the callback in the dependencies can lead to unnecessary re-registrations of the listener. There are react hook libraries that provide a elegant solution to this problem by utilizing the useRef hook to maintain a reference to the latest callback function without triggering re-renders or effect re-runs. This approach ensures that our event listener always has access to the most current state while minimizing performance overhead and potential memory leaks from multiple listener registrations. @@ -277,7 +281,9 @@ const TaskHeader: React.FC = ({
Context: - {contextTokens ? formatLargeNumber(contextTokens) : '-'} + {contextTokens + ? `${formatLargeNumber(contextTokens)} (${contextPercentage}%)` + : "-"}
From d836548c19cabd67b229e6bbbb38e510ac82e59d Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Tue, 28 Jan 2025 14:36:12 -0500 Subject: [PATCH 30/32] insert_code_block -> insert_content --- src/core/Cline.ts | 12 +++++------ src/core/assistant-message/index.ts | 4 ++-- src/core/prompts/__tests__/system.test.ts | 4 ++-- src/core/prompts/sections/rules.ts | 8 +++---- src/core/prompts/tools/index.ts | 2 +- src/core/prompts/tools/insert-code-block.ts | 10 ++++----- src/shared/__tests__/modes.test.ts | 24 +++++---------------- src/shared/experiments.ts | 6 +++--- src/shared/tool-groups.ts | 2 +- 9 files changed, 28 insertions(+), 44 deletions(-) diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 86a4ec5093..4f5206e5d2 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -1010,7 +1010,7 @@ export class Cline { return `[${block.name} for '${block.params.regex}'${ block.params.file_pattern ? ` in '${block.params.file_pattern}'` : "" }]` - case "insert_code_block": + case "insert_content": return `[${block.name} for '${block.params.path}']` case "search_and_replace": return `[${block.name} for '${block.params.path}']` @@ -1486,7 +1486,7 @@ export class Cline { } } - case "insert_code_block": { + case "insert_content": { const relPath: string | undefined = block.params.path const operations: string | undefined = block.params.operations @@ -1505,15 +1505,13 @@ export class Cline { // Validate required parameters if (!relPath) { this.consecutiveMistakeCount++ - pushToolResult(await this.sayAndCreateMissingParamError("insert_code_block", "path")) + pushToolResult(await this.sayAndCreateMissingParamError("insert_content", "path")) break } if (!operations) { this.consecutiveMistakeCount++ - pushToolResult( - await this.sayAndCreateMissingParamError("insert_code_block", "operations"), - ) + pushToolResult(await this.sayAndCreateMissingParamError("insert_content", "operations")) break } @@ -1629,7 +1627,7 @@ export class Cline { ) await this.diffViewProvider.reset() } catch (error) { - handleError("insert block", error) + handleError("insert content", error) await this.diffViewProvider.reset() } break diff --git a/src/core/assistant-message/index.ts b/src/core/assistant-message/index.ts index 092dd06c60..c2a2d6290e 100644 --- a/src/core/assistant-message/index.ts +++ b/src/core/assistant-message/index.ts @@ -13,7 +13,7 @@ export const toolUseNames = [ "read_file", "write_to_file", "apply_diff", - "insert_code_block", + "insert_content", "search_and_replace", "search_files", "list_files", @@ -82,7 +82,7 @@ export interface WriteToFileToolUse extends ToolUse { } export interface InsertCodeBlockToolUse extends ToolUse { - name: "insert_code_block" + name: "insert_content" params: Partial, "path" | "operations">> } diff --git a/src/core/prompts/__tests__/system.test.ts b/src/core/prompts/__tests__/system.test.ts index 276a06eb34..d4edea8046 100644 --- a/src/core/prompts/__tests__/system.test.ts +++ b/src/core/prompts/__tests__/system.test.ts @@ -544,7 +544,7 @@ describe("SYSTEM_PROMPT", () => { // Verify base instruction lists all available tools expect(prompt).toContain("apply_diff (for replacing lines in existing files)") expect(prompt).toContain("write_to_file (for creating new files or complete file rewrites)") - expect(prompt).toContain("insert_code_block (for adding sections to existing files)") + expect(prompt).toContain("insert_content (for adding lines to existing files)") expect(prompt).toContain("search_and_replace (for finding and replacing individual pieces of text)") }) @@ -574,7 +574,7 @@ describe("SYSTEM_PROMPT", () => { expect(prompt).toContain( "You should always prefer using other editing tools over write_to_file when making changes to existing files since write_to_file is much slower and cannot handle large files.", ) - expect(prompt).toContain("The insert_code_block tool adds code snippets or content blocks to files") + expect(prompt).toContain("The insert_content tool adds lines of text to files") expect(prompt).toContain("The search_and_replace tool finds and replaces text or regex in files") }) }) diff --git a/src/core/prompts/sections/rules.ts b/src/core/prompts/sections/rules.ts index cb944a14a5..b6e19eb08c 100644 --- a/src/core/prompts/sections/rules.ts +++ b/src/core/prompts/sections/rules.ts @@ -11,8 +11,8 @@ function getEditingInstructions(diffStrategy?: DiffStrategy, experiments?: Recor if (diffStrategy) { availableTools.push("apply_diff (for replacing lines in existing files)") } - if (experiments?.["insert_code_block"]) { - availableTools.push("insert_code_block (for adding sections to existing files)") + if (experiments?.["insert_content"]) { + availableTools.push("insert_content (for adding lines to existing files)") } if (experiments?.["search_and_replace"]) { availableTools.push("search_and_replace (for finding and replacing individual pieces of text)") @@ -24,9 +24,9 @@ function getEditingInstructions(diffStrategy?: DiffStrategy, experiments?: Recor } // Additional details for experimental features - if (experiments?.["insert_code_block"]) { + if (experiments?.["insert_content"]) { instructions.push( - "- The insert_code_block tool adds code snippets or content blocks to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location. It can support multiple operations at once.", + "- The insert_content tool adds lines of text to files, such as adding a new function to a JavaScript file or inserting a new route in a Python file. This tool will insert it at the specified line location. It can support multiple operations at once.", ) } diff --git a/src/core/prompts/tools/index.ts b/src/core/prompts/tools/index.ts index 23e77fb8d6..2e3a514602 100644 --- a/src/core/prompts/tools/index.ts +++ b/src/core/prompts/tools/index.ts @@ -32,7 +32,7 @@ const toolDescriptionMap: Record string | undefined> use_mcp_tool: (args) => getUseMcpToolDescription(args), access_mcp_resource: (args) => getAccessMcpResourceDescription(args), switch_mode: () => getSwitchModeDescription(), - insert_code_block: (args) => getInsertCodeBlockDescription(args), + insert_content: (args) => getInsertCodeBlockDescription(args), search_and_replace: (args) => getSearchAndReplaceDescription(args), apply_diff: (args) => args.diffStrategy ? args.diffStrategy.getToolDescription({ cwd: args.cwd, toolOptions: args.toolOptions }) : "", diff --git a/src/core/prompts/tools/insert-code-block.ts b/src/core/prompts/tools/insert-code-block.ts index 7dce56e0f9..fa9843325b 100644 --- a/src/core/prompts/tools/insert-code-block.ts +++ b/src/core/prompts/tools/insert-code-block.ts @@ -1,7 +1,7 @@ import { ToolArgs } from "./types" export function getInsertCodeBlockDescription(args: ToolArgs): string { - return `## insert_code_block + return `## insert_content Description: Inserts code blocks at specific line positions in a file. This is the primary tool for adding new code (functions/methods/classes, imports, attributes etc.) as it allows for precise insertions without overwriting existing content. The tool uses an efficient line-based insertion system that maintains file integrity and proper ordering of multiple insertions. Beware to use the proper indentation. This tool is the preferred way to add new code to files. Parameters: - path: (required) The path of the file to insert code into (relative to the current working directory ${args.cwd.toPosix()}) @@ -9,7 +9,7 @@ Parameters: * start_line: (required) The line number where the code block should be inserted. The content currently at that line will end up below the inserted code block. * content: (required) The code block to insert at the specified position. IMPORTANT NOTE: If the content is a single line, it can be a string. If it's a multi-line content, it should be a string with newline characters (\n) for line breaks. Usage: - + File path here [ { @@ -17,9 +17,9 @@ Usage: "content": "Your code block here" } ] - + Example: Insert a new function and its import statement - + src/app.ts [ { @@ -31,5 +31,5 @@ Example: Insert a new function and its import statement "content": "function calculateTotal(items: number[]): number {\n return items.reduce((sum, item) => sum + item, 0);\n}" } ] -` +` } diff --git a/src/shared/__tests__/modes.test.ts b/src/shared/__tests__/modes.test.ts index b9e46cb8d8..44d237630c 100644 --- a/src/shared/__tests__/modes.test.ts +++ b/src/shared/__tests__/modes.test.ts @@ -251,7 +251,7 @@ describe("isToolAllowedForMode", () => { it("disables tools when experiment is disabled", () => { const experiments = { search_and_replace: false, - insert_code_block: false, + insert_content: false, } expect( @@ -266,21 +266,14 @@ describe("isToolAllowedForMode", () => { ).toBe(false) expect( - isToolAllowedForMode( - "insert_code_block", - "test-exp-mode", - customModes, - undefined, - undefined, - experiments, - ), + isToolAllowedForMode("insert_content", "test-exp-mode", customModes, undefined, undefined, experiments), ).toBe(false) }) it("allows tools when experiment is enabled", () => { const experiments = { search_and_replace: true, - insert_code_block: true, + insert_content: true, } expect( @@ -295,21 +288,14 @@ describe("isToolAllowedForMode", () => { ).toBe(true) expect( - isToolAllowedForMode( - "insert_code_block", - "test-exp-mode", - customModes, - undefined, - undefined, - experiments, - ), + isToolAllowedForMode("insert_content", "test-exp-mode", customModes, undefined, undefined, experiments), ).toBe(true) }) it("allows non-experimental tools when experiments are disabled", () => { const experiments = { search_and_replace: false, - insert_code_block: false, + insert_content: false, } expect( diff --git a/src/shared/experiments.ts b/src/shared/experiments.ts index 71eda740d3..9849e22526 100644 --- a/src/shared/experiments.ts +++ b/src/shared/experiments.ts @@ -1,7 +1,7 @@ export const EXPERIMENT_IDS = { DIFF_STRATEGY: "experimentalDiffStrategy", SEARCH_AND_REPLACE: "search_and_replace", - INSERT_BLOCK: "insert_code_block", + INSERT_BLOCK: "insert_content", } as const export type ExperimentKey = keyof typeof EXPERIMENT_IDS @@ -29,10 +29,10 @@ export const experimentConfigsMap: Record = { enabled: false, }, INSERT_BLOCK: { - name: "Use experimental insert block tool", + name: "Use experimental insert content tool", description: - "Enable the experimental insert block tool, allowing Roo to insert multiple code blocks at once at specific line numbers without needing to create a diff.", + "Enable the experimental insert content tool, allowing Roo to insert content at specific line numbers without needing to create a diff.", enabled: false, }, } diff --git a/src/shared/tool-groups.ts b/src/shared/tool-groups.ts index d95b16e153..19f43a59e1 100644 --- a/src/shared/tool-groups.ts +++ b/src/shared/tool-groups.ts @@ -21,7 +21,7 @@ export const TOOL_DISPLAY_NAMES = { // Define available tool groups export const TOOL_GROUPS: Record = { read: ["read_file", "search_files", "list_files", "list_code_definition_names"], - edit: ["write_to_file", "apply_diff", "insert_code_block", "search_and_replace"], + edit: ["write_to_file", "apply_diff", "insert_content", "search_and_replace"], browser: ["browser_action"], command: ["execute_command"], mcp: ["use_mcp_tool", "access_mcp_resource"], From 9710fd111055741590b408cb53ef0ff38a75d3be Mon Sep 17 00:00:00 2001 From: Piotr Rogowski Date: Tue, 28 Jan 2025 20:53:07 +0100 Subject: [PATCH 31/32] handle other deepseek-r1 variants on openrouter --- src/api/providers/openrouter.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/api/providers/openrouter.ts b/src/api/providers/openrouter.ts index 6922e40613..e5390cb75c 100644 --- a/src/api/providers/openrouter.ts +++ b/src/api/providers/openrouter.ts @@ -114,13 +114,11 @@ export class OpenRouterHandler implements ApiHandler, SingleCompletionHandler { } let temperature = 0 - switch (this.getModel().id) { - case "deepseek/deepseek-r1": - // Recommended temperature for DeepSeek reasoning models - temperature = 0.6 - // DeepSeek highly recommends using user instead of system role - openAiMessages[0].role = "user" - openAiMessages = convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) + if (this.getModel().id === "deepseek/deepseek-r1" || this.getModel().id.startsWith("deepseek/deepseek-r1:")) { + // Recommended temperature for DeepSeek reasoning models + temperature = 0.6 + // DeepSeek highly recommends using user instead of system role + openAiMessages = convertToR1Format([{ role: "user", content: systemPrompt }, ...messages]) } // https://openrouter.ai/docs/transforms From 93f43bea43d5b5a5372db6c5989f585320a6c5d9 Mon Sep 17 00:00:00 2001 From: Matt Rubens Date: Tue, 28 Jan 2025 15:16:15 -0500 Subject: [PATCH 32/32] More renaming --- src/core/Cline.ts | 2 +- src/core/prompts/tools/index.ts | 8 ++--- src/core/prompts/tools/insert-code-block.ts | 35 --------------------- src/core/prompts/tools/insert-content.ts | 35 +++++++++++++++++++++ 4 files changed, 40 insertions(+), 40 deletions(-) delete mode 100644 src/core/prompts/tools/insert-code-block.ts create mode 100644 src/core/prompts/tools/insert-content.ts diff --git a/src/core/Cline.ts b/src/core/Cline.ts index acd6a700bb..3037043b1f 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -1601,7 +1601,7 @@ export class Cline { if (!userEdits) { pushToolResult( - `The code block was successfully inserted in ${relPath.toPosix()}.${newProblemsMessage}`, + `The content was successfully inserted in ${relPath.toPosix()}.${newProblemsMessage}`, ) await this.diffViewProvider.reset() break diff --git a/src/core/prompts/tools/index.ts b/src/core/prompts/tools/index.ts index 2e3a514602..8ad785c4d2 100644 --- a/src/core/prompts/tools/index.ts +++ b/src/core/prompts/tools/index.ts @@ -3,7 +3,7 @@ import { getReadFileDescription } from "./read-file" import { getWriteToFileDescription } from "./write-to-file" import { getSearchFilesDescription } from "./search-files" import { getListFilesDescription } from "./list-files" -import { getInsertCodeBlockDescription } from "./insert-code-block" +import { getInsertContentDescription } from "./insert-content" import { getSearchAndReplaceDescription } from "./search-and-replace" import { getListCodeDefinitionNamesDescription } from "./list-code-definition-names" import { getBrowserActionDescription } from "./browser-action" @@ -15,7 +15,7 @@ import { getSwitchModeDescription } from "./switch-mode" import { DiffStrategy } from "../../diff/DiffStrategy" import { McpHub } from "../../../services/mcp/McpHub" import { Mode, ModeConfig, getModeConfig, isToolAllowedForMode, getGroupName } from "../../../shared/modes" -import { ToolName, getToolName, getToolOptions, TOOL_GROUPS, ALWAYS_AVAILABLE_TOOLS } from "../../../shared/tool-groups" +import { ToolName, TOOL_GROUPS, ALWAYS_AVAILABLE_TOOLS } from "../../../shared/tool-groups" import { ToolArgs } from "./types" // Map of tool names to their description functions @@ -32,7 +32,7 @@ const toolDescriptionMap: Record string | undefined> use_mcp_tool: (args) => getUseMcpToolDescription(args), access_mcp_resource: (args) => getAccessMcpResourceDescription(args), switch_mode: () => getSwitchModeDescription(), - insert_content: (args) => getInsertCodeBlockDescription(args), + insert_content: (args) => getInsertContentDescription(args), search_and_replace: (args) => getSearchAndReplaceDescription(args), apply_diff: (args) => args.diffStrategy ? args.diffStrategy.getToolDescription({ cwd: args.cwd, toolOptions: args.toolOptions }) : "", @@ -105,6 +105,6 @@ export { getUseMcpToolDescription, getAccessMcpResourceDescription, getSwitchModeDescription, - getInsertCodeBlockDescription, + getInsertContentDescription, getSearchAndReplaceDescription, } diff --git a/src/core/prompts/tools/insert-code-block.ts b/src/core/prompts/tools/insert-code-block.ts deleted file mode 100644 index fa9843325b..0000000000 --- a/src/core/prompts/tools/insert-code-block.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { ToolArgs } from "./types" - -export function getInsertCodeBlockDescription(args: ToolArgs): string { - return `## insert_content -Description: Inserts code blocks at specific line positions in a file. This is the primary tool for adding new code (functions/methods/classes, imports, attributes etc.) as it allows for precise insertions without overwriting existing content. The tool uses an efficient line-based insertion system that maintains file integrity and proper ordering of multiple insertions. Beware to use the proper indentation. This tool is the preferred way to add new code to files. -Parameters: -- path: (required) The path of the file to insert code into (relative to the current working directory ${args.cwd.toPosix()}) -- operations: (required) A JSON array of insertion operations. Each operation is an object with: - * start_line: (required) The line number where the code block should be inserted. The content currently at that line will end up below the inserted code block. - * content: (required) The code block to insert at the specified position. IMPORTANT NOTE: If the content is a single line, it can be a string. If it's a multi-line content, it should be a string with newline characters (\n) for line breaks. -Usage: - -File path here -[ - { - "start_line": 10, - "content": "Your code block here" - } -] - -Example: Insert a new function and its import statement - -src/app.ts -[ - { - "start_line": 1, - "content": "import { sum } from './utils';" - }, - { - "start_line": 10, - "content": "function calculateTotal(items: number[]): number {\n return items.reduce((sum, item) => sum + item, 0);\n}" - } -] -` -} diff --git a/src/core/prompts/tools/insert-content.ts b/src/core/prompts/tools/insert-content.ts new file mode 100644 index 0000000000..92204f02fa --- /dev/null +++ b/src/core/prompts/tools/insert-content.ts @@ -0,0 +1,35 @@ +import { ToolArgs } from "./types" + +export function getInsertContentDescription(args: ToolArgs): string { + return `## insert_content +Description: Inserts content at specific line positions in a file. This is the primary tool for adding new content and code (functions/methods/classes, imports, attributes etc.) as it allows for precise insertions without overwriting existing content. The tool uses an efficient line-based insertion system that maintains file integrity and proper ordering of multiple insertions. Beware to use the proper indentation. This tool is the preferred way to add new content and code to files. +Parameters: +- path: (required) The path of the file to insert content into (relative to the current working directory ${args.cwd.toPosix()}) +- operations: (required) A JSON array of insertion operations. Each operation is an object with: + * start_line: (required) The line number where the content should be inserted. The content currently at that line will end up below the inserted content. + * content: (required) The content to insert at the specified position. IMPORTANT NOTE: If the content is a single line, it can be a string. If it's a multi-line content, it should be a string with newline characters (\n) for line breaks. Make sure to include the correct indentation for the content. +Usage: + +File path here +[ + { + "start_line": 10, + "content": "Your content here" + } +] + +Example: Insert a new function and its import statement + +File path here +[ + { + "start_line": 1, + "content": "import { sum } from './utils';" + }, + { + "start_line": 10, + "content": "function calculateTotal(items: number[]): number {\n return items.reduce((sum, item) => sum + item, 0);\n}" + } +] +` +}