diff --git a/src/api/providers/anthropic.ts b/src/api/providers/anthropic.ts index bd141b1f57..c84cbf0921 100644 --- a/src/api/providers/anthropic.ts +++ b/src/api/providers/anthropic.ts @@ -34,6 +34,20 @@ export class AnthropicHandler implements ApiHandler { case "claude-3-5-haiku-20241022": case "claude-3-opus-20240229": case "claude-3-haiku-20240307": { + // don't use prompt caching for advisor model requests + if (modelType === "advisor") { + stream = (await this.client.messages.create({ + model: modelId, + max_tokens: model.info.maxTokens || 8192, + temperature: 0, + system: [{ text: systemPrompt, type: "text" }], + messages, + // tools, + // tool_choice: { type: "auto" }, + stream: true, + })) as any + break + } /* The latest message will be the new user message, one before will be the assistant message from a previous request, and the user message before that will be a previously cached user message. So we need to mark the latest user message as ephemeral to cache it for the next request, and mark the second to last user message as ephemeral to let the server know the last message to retrieve from the cache for the current request.. */ diff --git a/src/api/providers/openrouter.ts b/src/api/providers/openrouter.ts index ce91c2f1ed..d044caad19 100644 --- a/src/api/providers/openrouter.ts +++ b/src/api/providers/openrouter.ts @@ -55,6 +55,11 @@ export class OpenRouterHandler implements ApiHandler { case "anthropic/claude-3-haiku:beta": case "anthropic/claude-3-opus": case "anthropic/claude-3-opus:beta": + // don't use prompt caching for advisor model requests + if (modelType === "advisor") { + break + } + openAiMessages[0] = { role: "system", content: [ diff --git a/src/core/Cline.ts b/src/core/Cline.ts index abdcb7a253..345719d657 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -18,7 +18,7 @@ import { UrlContentFetcher } from "../services/browser/UrlContentFetcher" import { listFiles } from "../services/glob/list-files" import { regexSearchFiles } from "../services/ripgrep" import { parseSourceCodeForDefinitionsTopLevel } from "../services/tree-sitter" -import { ApiConfiguration } from "../shared/api" +import { ApiConfiguration, ModelInfo } from "../shared/api" import { findLast, findLastIndex } from "../shared/array" import { AutoApprovalSettings } from "../shared/AutoApprovalSettings" import { combineApiRequests } from "../shared/combineApiRequests" @@ -60,6 +60,7 @@ import getFolderSize from "get-folder-size" import { BrowserSettings } from "../shared/BrowserSettings" import { ADVISOR_SYSTEM_PROMPT } from "./prompts/advisor" import { ChatSettings } from "../shared/ChatSettings" +import { CHAT_SYSTEM_PROMPT } from "./prompts/chat" 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 @@ -1192,6 +1193,71 @@ export class Cline { return false } + estimateAdvisorModelCost(problem: string) { + const truncatedConversationHistory = getTruncatedMessages( + this.apiConversationHistory, + this.conversationHistoryDeletedRange, + ) + const advisorModel = this.api.getAdvisorModel?.() + if (!advisorModel) { + return 0 + } + const advisorMessage = this.createAdvisorMessage(truncatedConversationHistory, advisorModel, problem) + const prompt = ADVISOR_SYSTEM_PROMPT() + advisorMessage + // Estimate ~3 chars per token as a rough approximation + const estimatedInputTokens = Math.ceil(prompt.length / 3) + const estimatedOutputTokens = 300 // typical response size + // Note: we don't prompt cache since we only send up one request at a time + const inputCost = (estimatedInputTokens * (advisorModel.info.inputPrice ?? 0)) / 1_000_000 // Convert from per million tokens + const outputCost = (estimatedOutputTokens * (advisorModel.info.outputPrice ?? 0)) / 1_000_000 + return inputCost + outputCost + } + + createAdvisorMessage( + truncatedConversationHistory: Anthropic.Messages.MessageParam[], + advisorModel: { + id: string + info: ModelInfo + }, + advisorProblem: string, + ) { + // Generate markdown + const markdownContent = truncatedConversationHistory + .map((message) => { + const role = message.role === "user" ? "**User:**" : "**Coding Agent:**" + const content = Array.isArray(message.content) + ? message.content.map((block) => formatContentBlockToMarkdown(block)).join("\n") + : message.content + return `${role}\n\n${content}\n\n` + }) + .join("---\n\n") + + // Don't want to send the entire conv history, just the most recent context + // Get approximate char count from token limit + const advisorContextWindow = advisorModel.info.contextWindow || 128_000 + const tokensToKeep = Math.floor(advisorContextWindow / 2) + // Estimate ~3 chars per token as a rough approximation + const charsToKeep = tokensToKeep * 3 + // Get last n chars of markdown content + const isTruncated = markdownContent.length > charsToKeep + const firstMessage = truncatedConversationHistory.at(0) + const firstMessageContent = firstMessage + ? Array.isArray(firstMessage.content) + ? firstMessage.content.map((block) => (block.type === "text" ? block.text : "")).join("\n") + : firstMessage.content + : "" + const recentContext = + (isTruncated ? `**User:**:\n\n${firstMessageContent}\n\n... (older messages removed for brevity) ...\n\n` : "") + + markdownContent.slice(-charsToKeep) + const advisorMessage = + "\n\n# The conversation history leading up to this point:\n\n" + + recentContext + + "\n\n# The problem the coding agent needs advice on:\n\n" + + advisorProblem + + return advisorMessage + } + async *attemptApiRequest(previousApiReqIndex: number): ApiStream { // Wait for MCP servers to be connected before generating system prompt await pWaitFor(() => this.providerRef.deref()?.mcpHub?.isConnecting !== true, { timeout: 10_000 }).catch(() => { @@ -1206,14 +1272,26 @@ export class Cline { const advisorModel = this.api.getAdvisorModel?.() const supportsConsultAdvisor = advisorModel !== undefined - let systemPrompt = await SYSTEM_PROMPT( - cwd, - this.api.getModel().info.supportsComputerUse ?? false, - mcpHub, - this.browserSettings, - this.chatSettings, - supportsConsultAdvisor, - ) + let systemPrompt: string + + if (this.chatSettings.mode === "chat") { + systemPrompt = await CHAT_SYSTEM_PROMPT( + cwd, + this.api.getModel().info.supportsComputerUse ?? false, + mcpHub, + this.browserSettings, + supportsConsultAdvisor, + ) + } else { + systemPrompt = await SYSTEM_PROMPT( + cwd, + this.api.getModel().info.supportsComputerUse ?? false, + mcpHub, + this.browserSettings, + supportsConsultAdvisor, + ) + } + let settingsCustomInstructions = this.customInstructions?.trim() const clineRulesFilePath = path.resolve(cwd, GlobalFileNames.clineRules) let clineRulesFileInstructions: string | undefined @@ -1282,50 +1360,22 @@ export class Cline { // If we're consulting the advisor, override the request if (this.advisorProblem && advisorModel) { - // Generate markdown - const markdownContent = truncatedConversationHistory - .map((message) => { - const role = message.role === "user" ? "**User:**" : "**Coding Agent:**" - const content = Array.isArray(message.content) - ? message.content.map((block) => formatContentBlockToMarkdown(block)).join("\n") - : message.content - return `${role}\n\n${content}\n\n` - }) - .join("---\n\n") - - // Don't want to send the entire conv history, just the most recent context - // Get approximate char count from token limit - const advisorContextWindow = advisorModel.info.contextWindow || 128_000 - const tokensToKeep = Math.floor(advisorContextWindow / 2) - // Estimate ~3 chars per token as a rough approximation - const charsToKeep = tokensToKeep * 3 - // Get last n chars of markdown content - const isTruncated = markdownContent.length > charsToKeep - const firstMessage = truncatedConversationHistory.at(0) - const firstMessageContent = firstMessage - ? Array.isArray(firstMessage.content) - ? firstMessage.content.map((block) => (block.type === "text" ? block.text : "")).join("\n") - : firstMessage.content - : "" - const recentContext = - (isTruncated ? `**User:**:\n\n${firstMessageContent}\n\n... (older messages removed for brevity) ...\n\n` : "") + - markdownContent.slice(-charsToKeep) - const advisorMessage: Anthropic.Messages.MessageParam[] = [ - { - role: "user", - content: [ - { - type: "text", - text: - "\n\n# The conversation history leading up to this point:\n\n" + - recentContext + - "\n\n# The problem the coding agent needs advice on:\n\n" + - this.advisorProblem, - }, - ], - }, - ] - stream = this.api.createMessage(ADVISOR_SYSTEM_PROMPT(), advisorMessage, "advisor") + const advisorMessage = this.createAdvisorMessage(truncatedConversationHistory, advisorModel, this.advisorProblem) + stream = this.api.createMessage( + ADVISOR_SYSTEM_PROMPT(), + [ + { + role: "user", + content: [ + { + type: "text", + text: advisorMessage, + }, + ], + }, + ], + "advisor", + ) } const iterator = stream[Symbol.asyncIterator]() @@ -1480,6 +1530,8 @@ export class Cline { return `[${block.name} for '${block.params.problem}']` case "ask_followup_question": return `[${block.name} for '${block.params.question}']` + case "respond_to_inquiry": + return `[${block.name} for '${block.params.response}']` case "attempt_completion": return `[${block.name}]` } @@ -2591,9 +2643,12 @@ export class Cline { } this.consecutiveMistakeCount = 0 + + const estimatedCost = undefined //this.estimateAdvisorModelCost(problem) const completeMessage = JSON.stringify({ problem: removeClosingTag("problem", problem), advisorModelId: this.api.getAdvisorModel?.().id, + estimatedCost, } satisfies ClineConsultAdvisor) if (this.shouldAutoApproveTool(block.name)) { @@ -2621,6 +2676,7 @@ export class Cline { lastMessage.text = JSON.stringify({ problem: removeClosingTag("problem", problem), advisorModelId: this.api.getAdvisorModel?.().id, + estimatedCost, } satisfies ClineConsultAdvisor) } @@ -2673,6 +2729,42 @@ export class Cline { break } } + case "respond_to_inquiry": { + const response: string | undefined = block.params.response + try { + if (block.partial) { + await this.ask("respond_to_inquiry", removeClosingTag("response", response), block.partial).catch( + () => {}, + ) + break + } else { + if (!response) { + this.consecutiveMistakeCount++ + pushToolResult(await this.sayAndCreateMissingParamError("respond_to_inquiry", "response")) + await this.saveCheckpoint() + break + } + this.consecutiveMistakeCount = 0 + + // if (this.autoApprovalSettings.enabled && this.autoApprovalSettings.enableNotifications) { + // showSystemNotification({ + // subtitle: "Cline has a response...", + // message: response.replace(/\n/g, " "), + // }) + // } + + const { text, images } = await this.ask("respond_to_inquiry", response, false) + await this.say("user_feedback", text ?? "", images) + pushToolResult(formatResponse.toolResult(`\n${text}\n`, images)) + await this.saveCheckpoint() + break + } + } catch (error) { + await handleError("responding to inquiry", error) + await this.saveCheckpoint() + break + } + } case "attempt_completion": { /* this.consecutiveMistakeCount = 0 diff --git a/src/core/assistant-message/index.ts b/src/core/assistant-message/index.ts index 8da46213fb..de2ade7a30 100644 --- a/src/core/assistant-message/index.ts +++ b/src/core/assistant-message/index.ts @@ -21,6 +21,7 @@ export const toolUseNames = [ "access_mcp_resource", "consult_advisor", "ask_followup_question", + "respond_to_inquiry", "attempt_completion", ] as const @@ -46,6 +47,7 @@ export const toolParamNames = [ "uri", "problem", "question", + "response", "result", ] as const diff --git a/src/core/prompts/chat.ts b/src/core/prompts/chat.ts new file mode 100644 index 0000000000..76e41d6f05 --- /dev/null +++ b/src/core/prompts/chat.ts @@ -0,0 +1,427 @@ +import defaultShell from "default-shell" +import os from "os" +import osName from "os-name" +import { McpHub } from "../../services/mcp/McpHub" +import { BrowserSettings } from "../../shared/BrowserSettings" + +export const CHAT_SYSTEM_PROMPT = async ( + cwd: string, + supportsComputerUse: boolean, + mcpHub: McpHub, + browserSettings: BrowserSettings, + supportsConsultAdvisor: boolean, +) => `You are Cline, a highly skilled software engineer with extensive knowledge in many programming languages, frameworks, design patterns, and best practices. + +==== + +TOOL USE + +You have access to a set of tools that are executed upon the user's approval. You can use one tool per message, and will receive the result of that tool use in the user's response. You use tools step-by-step to respond to the user's inquiry, with each tool use informed by the result of the previous tool use. + +# Tool Use Formatting + +Tool use is formatted using XML-style tags. The tool name is enclosed in opening and closing tags, and each parameter is similarly enclosed within its own set of tags. Here's the structure: + + +value1 +value2 +... + + +For example: + + +src/main.js + + +Always adhere to this format for the tool use to ensure proper parsing and execution. + +# Tools + +## read_file +Description: Request to read the contents of a file at the specified path. Use this when you need to examine the contents of an existing file you do not know the contents of, for example to analyze code, review text files, or extract information from configuration files. Automatically extracts raw text from PDF and DOCX files. May not be suitable for other types of binary files, as it returns the raw content as a string. +Parameters: +- path: (required) The path of the file to read (relative to the current working directory ${cwd.toPosix()}) +Usage: + +File path here + + +## search_files +Description: Request to perform a regex search across files in a specified directory, providing context-rich results. This tool searches for patterns or specific content across multiple files, displaying each match with encapsulating context. +Parameters: +- path: (required) The path of the directory to search in (relative to the current working directory ${cwd.toPosix()}). This directory will be recursively searched. +- regex: (required) The regular expression pattern to search for. Uses Rust regex syntax. +- file_pattern: (optional) Glob pattern to filter files (e.g., '*.ts' for TypeScript files). If not provided, it will search all files (*). +Usage: + +Directory path here +Your regex pattern here +file pattern here (optional) + + +## list_files +Description: Request to list files and directories within the specified directory. If recursive is true, it will list all files and directories recursively. If recursive is false or not provided, it will only list the top-level contents. Do not use this tool to confirm the existence of files you may have created, as the user will let you know if the files were created successfully or not. +Parameters: +- path: (required) The path of the directory to list contents for (relative to the current working directory ${cwd.toPosix()}) +- recursive: (optional) Whether to list files recursively. Use true for recursive listing, false or omit for top-level only. +Usage: + +Directory path here +true or false (optional) + + +## list_code_definition_names +Description: Request to list definition names (classes, functions, methods, etc.) used in source code files at the top level of the specified directory. This tool provides insights into the codebase structure and important constructs, encapsulating high-level concepts and relationships that are crucial for understanding the overall architecture. +Parameters: +- path: (required) The path of the directory (relative to the current working directory ${cwd.toPosix()}) to list top level source code definitions for. +Usage: + +Directory path here +${ + supportsComputerUse + ? ` + +## browser_action +Description: Request to interact with a Puppeteer-controlled browser. Every action, except \`close\`, will be responded to with a screenshot of the browser's current state, along with any new console logs. You may only perform one browser action per message, and wait for the user's response including a screenshot and logs to determine the next action. +- The sequence of actions **must always start with** launching the browser at a URL, and **must always end with** closing the browser. If you need to visit a new URL that is not possible to navigate to from the current webpage, you must first close the browser, then launch again at the new URL. +- While the browser is active, only the \`browser_action\` tool can be used. No other tools should be called during this time. You may proceed to use other tools only after closing the browser. For example if you run into an error and need to fix a file, you must close the browser, then use other tools to make the necessary changes, then re-launch the browser to verify the result. +- The browser window has a resolution of **${browserSettings.viewport.width}x${browserSettings.viewport.height}** pixels. When performing any click actions, ensure the coordinates are within this resolution range. +- Before clicking on any elements such as icons, links, or buttons, you must consult the provided screenshot of the page to determine the coordinates of the element. The click should be targeted at the **center of the element**, not on its edges. +Parameters: +- action: (required) The action to perform. The available actions are: + * launch: Launch a new Puppeteer-controlled browser instance at the specified URL. This **must always be the first action**. + - Use with the \`url\` parameter to provide the URL. + - Ensure the URL is valid and includes the appropriate protocol (e.g. http://localhost:3000/page, file:///path/to/file.html, etc.) + * click: Click at a specific x,y coordinate. + - Use with the \`coordinate\` parameter to specify the location. + - Always click in the center of an element (icon, button, link, etc.) based on coordinates derived from a screenshot. + * type: Type a string of text on the keyboard. You might use this after clicking on a text field to input text. + - Use with the \`text\` parameter to provide the string to type. + * scroll_down: Scroll down the page by one page height. + * scroll_up: Scroll up the page by one page height. + * close: Close the Puppeteer-controlled browser instance. This **must always be the final browser action**. + - Example: \`close\` +- url: (optional) Use this for providing the URL for the \`launch\` action. + * Example: https://example.com +- coordinate: (optional) The X and Y coordinates for the \`click\` action. Coordinates should be within the **${browserSettings.viewport.width}x${browserSettings.viewport.height}** resolution. + * Example: 450,300 +- text: (optional) Use this for providing the text for the \`type\` action. + * Example: Hello, world! +Usage: + +Action to perform (e.g., launch, click, type, scroll_down, scroll_up, close) +URL to launch the browser at (optional) +x,y coordinates (optional) +Text to type (optional) +` + : "" +} + +## use_mcp_tool +Description: Request to use a tool provided by a connected MCP server. Each MCP server can provide multiple tools with different capabilities. Tools have defined input schemas that specify required and optional parameters. +Parameters: +- server_name: (required) The name of the MCP server providing the tool +- tool_name: (required) The name of the tool to execute +- arguments: (required) A JSON object containing the tool's input parameters, following the tool's input schema +Usage: + +server name here +tool name here + +{ + "param1": "value1", + "param2": "value2" +} + + + +## access_mcp_resource +Description: Request to access a resource provided by a connected MCP server. Resources represent data sources that can be used as context, such as files, API responses, or system information. +Parameters: +- server_name: (required) The name of the MCP server providing the resource +- uri: (required) The URI identifying the specific resource to access +Usage: + +server name here +resource URI here +${ + supportsConsultAdvisor + ? ` + +## consult_advisor +Description: Request to consult an advanced-reasoning AI model about a problem or question you are facing. This can be used to resolve errors you are stuck on, or get input from the model to work through a challenge you are facing. The relevant conversation history leading to the problem will also be provided to the advisor for additional context. +Parameters: +- problem: (required) A string describing the issue, question, or context you want the advisor to address. +Usage: + +Your problem or question here +` + : "" +} + +## respond_to_inquiry +Description: Respond to the user's inquiry with a clear answer. This tool should be used when you need to provide a response to a question or statement. It allows for direct communication with the user, ensuring they receive a clear answer that addresses their inquiry. It can also be used to ask the user for more information if needed. +Parameters: +- response: (required) The response to provide to the user. This should be a clear answer that addresses the user's inquiry. +Usage: + +Your response here + + +# Tool Use Examples + +## Example 1: Requesting to use an MCP tool + + +weather-server +get_forecast + +{ + "city": "San Francisco", + "days": 5 +} + + + +## Example 2: Requesting to access an MCP resource + + +weather-server +weather://san-francisco/current + + +# Tool Use Guidelines + +1. In tags, assess what information you already have and what information you need to proceed with the task. +2. Choose the most appropriate tool based on the task and the tool descriptions provided. Assess if you need additional information to proceed, and which of the available tools would be most effective for gathering this information. For example using the list_files tool is more effective than running a command like \`ls\` in the terminal. It's critical that you think about each available tool and use the one that best fits the current step in the task. +3. If multiple actions are needed, use one tool at a time per message to accomplish the task iteratively, with each tool use being informed by the result of the previous tool use. Do not assume the outcome of any tool use. Each step must be informed by the previous step's result. +4. Formulate your tool use using the XML format specified for each tool. +5. After each tool use, the user will respond with the result of that tool use. This result will provide you with the necessary information to continue your task or make further decisions. This response may include: + - Information about whether the tool succeeded or failed, along with any reasons for failure. + - Linter errors that may have arisen due to the changes you made, which you'll need to address. + - New terminal output in reaction to the changes, which you may need to consider or act upon. + - Any other relevant feedback or information related to the tool use. +6. ALWAYS wait for user confirmation after each tool use before proceeding. Never assume the success of a tool use without explicit confirmation of the result from the user. + +It is crucial to proceed step-by-step, waiting for the user's message after each tool use before moving forward with the task. This approach allows you to: +1. Confirm the success of each step before proceeding. +2. Address any issues or errors that arise immediately. +3. Adapt your approach based on new information or unexpected results. +4. Ensure that each action builds correctly on the previous ones. + +By waiting for and carefully considering the user's response after each tool use, you can react accordingly and make informed decisions about how to proceed with the task. This iterative process helps ensure the overall success and accuracy of your work. + +==== + +MCP SERVERS + +The Model Context Protocol (MCP) enables communication between the system and locally running MCP servers that provide additional tools and resources to extend your capabilities. + +# Connected MCP Servers + +When a server is connected, you can use the server's tools via the \`use_mcp_tool\` tool, and access the server's resources via the \`access_mcp_resource\` tool. + +${ + mcpHub.getServers().length > 0 + ? `${mcpHub + .getServers() + .filter((server) => server.status === "connected") + .map((server) => { + const tools = server.tools + ?.map((tool) => { + const schemaStr = tool.inputSchema + ? ` Input Schema: + ${JSON.stringify(tool.inputSchema, null, 2).split("\n").join("\n ")}` + : "" + + return `- ${tool.name}: ${tool.description}\n${schemaStr}` + }) + .join("\n\n") + + const templates = server.resourceTemplates + ?.map((template) => `- ${template.uriTemplate} (${template.name}): ${template.description}`) + .join("\n") + + const resources = server.resources + ?.map((resource) => `- ${resource.uri} (${resource.name}): ${resource.description}`) + .join("\n") + + const config = JSON.parse(server.config) + + return ( + `## ${server.name} (\`${config.command}${config.args && Array.isArray(config.args) ? ` ${config.args.join(" ")}` : ""}\`)` + + (tools ? `\n\n### Available Tools\n${tools}` : "") + + (templates ? `\n\n### Resource Templates\n${templates}` : "") + + (resources ? `\n\n### Direct Resources\n${resources}` : "") + ) + }) + .join("\n\n")}` + : "(No MCP servers currently connected)" +}${ + supportsConsultAdvisor + ? ` + +==== + +CONSULTING THE ADVISOR MODEL + +You can use the consult_advisor tool to get suggestions from an advisor model, a powerful AI model that can provide strategic guidance and help solve complex problems. The conversation history that led to the current situation is automatically passed to the advisor, allowing it to provide contextually relevant guidance based on the full picture of the task at hand. + +# When to Use the Advisor + +- When stuck on persistent bugs that you cannot resolve +- If you've tried multiple approaches without success +- When facing complex type errors or package incompatibilities +- When debugging intricate interactions between multiple systems +- If you need deeper insight into system behavior that may not be apparent + +# How to Use Effectively + +## Provide Clear Context +- Explain the current situation and challenge +- Include relevant code snippets or error messages +- Describe what you've already tried +- Specify what kind of guidance you're seeking + +## Ask Specific Questions +- Instead of "Why isn't this working?" +- Better: "I'm encountering this specific type error when integrating these packages, here's what I've tried..." + +Example Usage: + + +I'm encountering persistent type errors while working with @types/react-query v4.0.0: + +Error: Type 'QueryClient' is not assignable to parameter of type 'never'. + The types of 'getQueryCache().notify' are incompatible between these types. + +I've tried: +- Checking package versions compatibility +- Explicitly typing the QueryClient instance +- Updating @types/react and @types/react-query + +Current package versions: +react-query: ^3.39.3 +@types/react-query: ^4.0.0 +react: ^18.2.0 +typescript: ^4.9.5 + +The error persists despite these attempts. Could this be due to version mismatches or breaking changes I'm not aware of? + + + +# Benefits of Using the Advisor + +- Break through debugging roadblocks +- Get fresh perspectives on complex issues +- Understand root causes of persistent bugs +- Solve challenging technical issues + +Remember: While you should attempt to solve problems with your own reasoning first, the advisor is a powerful resource available when you're stuck on a bug. Don't hesitate to consult it when you've hit a persistent roadblock that you cannot resolve.` + : "" +} + +==== + +CAPABILITIES + +- You have access to tools that let you list files, view source code definitions, regex search${ + supportsComputerUse ? ", use the browser" : "" +}, read files${ + supportsConsultAdvisor ? ", consult an advisor" : "" +}, and ask follow-up questions. These tools help you effectively accomplish a wide range of tasks, such as understanding the current state of a project, and much more. +- When the user initially gives you a task, a recursive list of all filepaths in the current working directory ('${cwd.toPosix()}') will be included in environment_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). This can also guide decision-making on which files to explore further. If you need to further explore directories such as outside the current working directory, you can use the list_files tool. If you pass 'true' for the recursive parameter, it will list files recursively. Otherwise, it will list files at the top level, which is better suited for generic directories where you don't necessarily need the nested structure, like the Desktop. +- You can use search_files to perform regex searches across files in a specified directory, outputting context-rich results that include surrounding lines. This is particularly useful for understanding code patterns, finding specific implementations, or identifying areas that need refactoring. +- You can use the list_code_definition_names tool to get an overview of source code definitions for all files at the top level of a specified directory. This can be particularly useful when you need to understand the broader context and relationships between certain parts of the code. You may need to call this tool multiple times to understand various parts of the codebase related to the task.${ + supportsComputerUse + ? "\n- You can use the browser_action tool to interact with websites (including html files and locally running development servers) through a Puppeteer-controlled browser when you feel it is necessary in accomplishing the user's task. This tool is particularly useful for web development tasks as it allows you to launch a browser, navigate to pages, interact with elements through clicks and keyboard input, and capture the results through screenshots and console logs. This tool may be useful at key stages of web development tasks-such as after implementing new features, making substantial changes, when troubleshooting issues, or to verify the result of your work. You can analyze the provided screenshots to ensure correct rendering or identify errors, and review console logs for runtime issues.\n - For example, if asked to add a component to a react website, you might create the necessary files, use execute_command to run the site locally, then use browser_action to launch the browser, navigate to the local server, and verify the component renders & functions correctly before closing the browser." + : "" +} +- You have access to MCP servers that may provide additional tools and resources. Each server may provide different capabilities that you can use to accomplish tasks more effectively.${ + supportsConsultAdvisor + ? "\n- When you hit a roadblock, such as an error you've attempted to resolve several times without success, you can use the consult_advisor tool to get suggestions from an advanced-reasoning AI model. The conversation history that led to the current situation is automatically passed to the advisor, allowing it to provide contextually relevant guidance based on the full picture of the task at hand." + : "" +} + +==== + +RULES + +- Your current working directory is: ${cwd.toPosix()} +- You cannot \`cd\` into a different directory to complete a task. You are stuck operating from '${cwd.toPosix()}', so be sure to pass in the correct 'path' parameter when using tools that require a path. +- Do not use the ~ character or $HOME to refer to the home directory. +- 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 replace_in_file to make informed changes. +- The user may provide a file's contents directly in their message, in which case you shouldn't use the read_file tool to get the file contents again since you already have it.${ + supportsComputerUse + ? '\n- The user may ask generic non-development tasks, such as "what\'s the latest news" or "look up the weather in San Diego", in which case you might use the browser_action tool to complete the task if it makes sense to do so. However, if an available MCP server tool or resource can be used instead, you should prefer to use it over browser_action.' + : "" +} +- 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. +- 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 + ? " 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." + : "" +} + +==== + +SYSTEM INFORMATION + +Operating System: ${osName()} +Default Shell: ${defaultShell} +Home Directory: ${os.homedir().toPosix()} +Current Working Directory: ${cwd.toPosix()} + +==== + +OBJECTIVE + +You respond to user inquiries by gathering relevant information through available tools and providing clear, informed responses. + +1. Analyze the user's inquiry to understand what information is needed to provide a complete and accurate response. +2. Use available tools one at a time to gather the necessary information. Each tool use should be purposeful in building your understanding to address the inquiry. +3. Remember, you have extensive capabilities with access to a wide range of tools that can be used in powerful and clever ways to gather relevant information. Before calling a tool, do some analysis within tags. First, analyze the file structure provided in environment_details to gain context and insights for proceeding effectively. Then, think about which of the provided tools is the most relevant tool to gather the information needed. Next, go through each of the required parameters of the relevant tool and determine if the user has directly provided or given enough information to infer a value. When deciding if the parameter can be inferred, carefully consider all the context to see if it supports a specific value. If all of the required parameters are present or can be reasonably inferred, close the thinking tag and proceed with the tool use. BUT, if one of the values for a required parameter is missing, DO NOT invoke the tool (not even with fillers for the missing params) and instead, ask the user to provide the missing parameters using the respond_to_inquiry tool. DO NOT ask for more information on optional parameters if it is not provided. +4. Once you've gathered the necessary information to address the inquiry, you must use the respond_to_inquiry tool to present a clear, well-informed response to the user. + +==== + +CHAT MODE + +You are now in chat mode, which means you will engage in conversational interactions rather than completing development tasks. In this mode: + +1. Your primary purpose is to respond helpfully to the user's questions and engage in natural dialogue +2. While you still have access to all tools, you will use them only to gather information to inform your responses +3. Instead of working towards task completion, you will work towards providing clear, informative responses +4. You must use the respond_to_inquiry tool to deliver your responses, not attempt_completion +5. Keep responses focused and relevant to the user's questions +6. You may use tools like: + - read_file to look up code context + - search_files to find relevant information + - list_files to understand project structure + - MCP tools/resources to get external data + But always with the goal of informing your response + +Your objective is to be a helpful conversational partner, not a task-completing agent. Every tool use should be in service of building a more complete and accurate response to the user's inquiry. However, if you have enough information to respond to the user's inquiry, you should use the respond_to_inquiry tool to immediately deliver a response. + +Important: In chat mode, you should immediately use the respond_to_inquiry tool to deliver your response, rather than using tags to analyze when to respond. Do not talk about using respond_to_inquiry - just use it directly to share your thoughts and provide helpful answers.` + +export function addUserInstructions(settingsCustomInstructions?: string, clineRulesFileInstructions?: string) { + let customInstructions = "" + if (settingsCustomInstructions) { + customInstructions += settingsCustomInstructions + "\n\n" + } + if (clineRulesFileInstructions) { + customInstructions += clineRulesFileInstructions + } + + return ` +==== + +USER'S CUSTOM INSTRUCTIONS + +The following additional instructions are provided by the user, and should be followed to the best of your ability without interfering with the TOOL USE guidelines. + +${customInstructions.trim()}` +} diff --git a/src/core/prompts/system.ts b/src/core/prompts/system.ts index e2865a4e25..a0fe39e7e1 100644 --- a/src/core/prompts/system.ts +++ b/src/core/prompts/system.ts @@ -3,14 +3,12 @@ import os from "os" import osName from "os-name" import { McpHub } from "../../services/mcp/McpHub" import { BrowserSettings } from "../../shared/BrowserSettings" -import { ChatSettings } from "../../shared/ChatSettings" export const SYSTEM_PROMPT = async ( cwd: string, supportsComputerUse: boolean, mcpHub: McpHub, browserSettings: BrowserSettings, - chatSettings: ChatSettings, supportsConsultAdvisor: boolean, ) => `You are Cline, a highly skilled software engineer with extensive knowledge in many programming languages, frameworks, design patterns, and best practices. diff --git a/src/core/webview/ClineProvider.ts b/src/core/webview/ClineProvider.ts index 903d1a4745..105f51091e 100644 --- a/src/core/webview/ClineProvider.ts +++ b/src/core/webview/ClineProvider.ts @@ -488,6 +488,7 @@ export class ClineProvider implements vscode.WebviewViewProvider { this.cline.updateChatSettings(message.chatSettings) } await this.postStateToWebview() + this.cancelTask() } break // case "relaunchChromeDebugMode": diff --git a/src/shared/ExtensionMessage.ts b/src/shared/ExtensionMessage.ts index d5388d0467..3f6670b4f2 100644 --- a/src/shared/ExtensionMessage.ts +++ b/src/shared/ExtensionMessage.ts @@ -66,6 +66,7 @@ export interface ClineMessage { export type ClineAsk = | "followup" + | "respond_to_inquiry" | "command" | "command_output" | "completion_result" @@ -148,6 +149,7 @@ export interface ClineAskUseMcpServer { export interface ClineConsultAdvisor { problem: string advisorModelId?: string + estimatedCost?: number } export interface ClineApiReqInfo { diff --git a/webview-ui/src/components/chat/ChatRow.tsx b/webview-ui/src/components/chat/ChatRow.tsx index 7375472c92..a063905da4 100644 --- a/webview-ui/src/components/chat/ChatRow.tsx +++ b/webview-ui/src/components/chat/ChatRow.tsx @@ -762,7 +762,19 @@ export const ChatRowContent = ({ message, isExpanded, onToggleExpand, lastModifi padding: "8px 10px", marginTop: "8px", }}> - {consultAdvisor.problem} +
{consultAdvisor.problem}
+ {consultAdvisor.estimatedCost != null && ( +
+ Estimated cost: ${Number(consultAdvisor.estimatedCost).toFixed(4)} +
+ )}
) + case "respond_to_inquiry": + return ( +
+ +
+ ) default: return null } diff --git a/webview-ui/src/components/chat/ChatTextArea.tsx b/webview-ui/src/components/chat/ChatTextArea.tsx index 2d4833a350..a7c0742862 100644 --- a/webview-ui/src/components/chat/ChatTextArea.tsx +++ b/webview-ui/src/components/chat/ChatTextArea.tsx @@ -489,6 +489,10 @@ const ChatTextArea = forwardRef( mode: newMode, }, }) + // Focus the textarea after mode toggle with slight delay + setTimeout(() => { + textAreaRef.current?.focus() + }, 100) }, [chatSettings.mode, textAreaDisabled]) const handleContextButtonClick = useCallback(() => { diff --git a/webview-ui/src/components/chat/ChatView.tsx b/webview-ui/src/components/chat/ChatView.tsx index c1894ba03c..06ff6af2e0 100644 --- a/webview-ui/src/components/chat/ChatView.tsx +++ b/webview-ui/src/components/chat/ChatView.tsx @@ -103,6 +103,13 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie // setPrimaryButtonText(undefined) // setSecondaryButtonText(undefined) break + case "respond_to_inquiry": + setTextAreaDisabled(isPartial) + setClineAsk("respond_to_inquiry") + setEnableButtons(isPartial) + // setPrimaryButtonText(undefined) + // setSecondaryButtonText(undefined) + break case "tool": setTextAreaDisabled(isPartial) setClineAsk("tool") @@ -271,6 +278,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie } else if (clineAsk) { switch (clineAsk) { case "followup": + case "respond_to_inquiry": case "tool": case "browser_action_launch": case "command": // user can provide feedback to a tool or command use