本文以 Cline 为拆解对象。
系统 prompt
src\core\prompts\system.ts
基本流程
用户提问后进行一次回复,一次回复使用一个“工具”。即使完成任务需要多次操作,依然需要每次一条信息、使用一个工具地逐步完成任务。
Cline 请求内容可以点击下箭头轻松获取。首次提问时 Cline 会收集项目目录结构和提问一同发送导到大模型,例(此处目录结构已作省略):
<task>删掉readme文件的table tag</task>
<environment_details># VSCode Visible FilesREADME.md
# VSCode Open Tabssrc/core/Cline.tssys_prompt.en.mdsrc/core/assistant-message/diff.tswebview-ui/src/App.tsxsrc/core/assistant-message/index.tswebview-ui/src/components/chat/ChatRow.tsxsrc/integrations/editor/DiffViewProvider.tsREADME.md
# Current Working Directory (d:/cline) Files.eslintrc.json.gitattributes.gitignore.nvmrc.prettierignore.prettierrc.json.vscode-test.mjs.vscodeignoreCHANGELOG.mdCODE_OF_CONDUCT.mdCONTRIBUTING.mdesbuild.jsLICENSE...docs/getting-started-new-coders/docs/getting-started-new-coders/installing-dev-essentials.mddocs/getting-started-new-coders/README.mddocs/mcp/docs/mcp/mcp-quickstart.mddocs/mcp/mcp-server-from-github.mddocs/mcp/mcp-server-from-scratch.mddocs/mcp/README.mdsrc/shared/combineCommandSequences.tswebview-ui/src/utils/vscode.ts
(File list truncated. Use list_files on specific subdirectories if you need to explore further.)</environment_details>
回复格式(即是工具)为 xml
,例:
<thinking>The task simply shows "1" which is unclear. I need to ask for clarification on what the user wants to accomplish. Looking at the environment details, this appears to be a Go-based project with a CI/CD focus, containing:- Go modules (go.mod, go.sum)- Makefile for build automation- Dockerfiles for containerization- SQL schema definitions- Protocol buffer definitions- Various service implementations
However, without more context about what the user wants to do with this project, I need to ask for clarification.</thinking>
<ask_followup_question><question>What would you like to accomplish with this project? The project appears to be a Go-based CI/CD system with Docker support, protocol buffers, and multiple services. Please specify what changes or actions you'd like me to perform.</question>
</ask_followup_question>
工具
Cline 可用工具如下:
execute_command
请求在系统上执行 CLI 命令。相比创建可执行脚本,更倾向于执行复杂的 CLI 命令。
<execute_command><command>Your command here</command><requires_approval>true or false</requires_approval></execute_command>
read_file
请求读取指定路径文件的内容。检查现有文件的内容(如分析代码、查看文本文件或从配置文件中提取信息)时使用此工具。
<read_file><path>File path here</path></read_file>
write_to_file
请求将内容写入指定路径的文件。如果文件存在,将用提供的内容覆盖。如果文件不存在,将创建新文件。此工具将自动创建写入文件所需的任何目录。
<write_to_file><path>File path here</path><content>Your file content here</content></write_to_file>
replace_in_file
请求使用定义的搜索/替换块替换现有文件中的内容部分。需要对文件的特定部分进行有针对性的更改时使用此工具。
<replace_in_file><path>File path here</path><diff>Search and replace blocks here</diff></replace_in_file>
实例:
<replace_in_file><path>src/components/App.tsx</path><diff><<<<<<< SEARCHimport React from 'react';=======import React, { useState } from 'react';>>>>>>> REPLACE
<<<<<<< SEARCHfunction handleSubmit() { saveData(); setLoading(false);}
=======>>>>>>> REPLACE
<<<<<<< SEARCHreturn ( <div>=======function handleSubmit() { saveData(); setLoading(false);}
return ( <div>>>>>>>> REPLACE</diff></replace_in_file>
search_files
请求在指定目录中执行正则表达式搜索,并提供包含上下文的结果。此工具在多个文件中搜索模式或特定内容,显示每个匹配项及其周围的上下文。
在让 Cline 找源头的时候他可能会直接用 search_files。(例如直接搜索 new XXX()
来查询对象在何处被初始化)
<search_files><path>Directory path here</path><regex>Your regex pattern here</regex><file_pattern>file pattern here (optional)</file_pattern></search_files>
list_files
请求列出指定目录中的文件和目录。如果recursive为true,将递归列出所有文件和目录。
<list_files><path>Directory path here</path><recursive>true or false (optional)</recursive></list_files>
list_code_definition_names
请求列出指定目录顶级源代码文件中使用的定义名称(类、函数、方法等)。此工具提供代码库结构和重要构造的见解,概括了理解整体架构所需的高层次概念和关系。
<list_code_definition_names><path>Directory path here</path></list_code_definition_names>
use_mcp_tool
请求使用由连接的MCP服务器提供的工具。每个 MCP 服务器可以提供具有不同功能的多个工具。工具具有定义的输入模式,指定必需和可选参数。
<use_mcp_tool><server_name>server name here</server_name><tool_name>tool name here</tool_name><arguments>{ "param1": "value1", "param2": "value2"}</arguments></use_mcp_tool>
access_mcp_resource
请求访问由连接的MCP服务器提供的资源。资源表示可以用作上下文的数据源,例如文件、API响应或系统信息。
<access_mcp_resource><server_name>server name here</server_name><uri>resource URI here</uri></access_mcp_resource>
ask_followup_question
向用户提问以收集完成任务所需的额外信息。当您遇到不明确的情况、需要澄清或需要更多详细信息以有效进行时使用此工具。通过启用与用户的直接沟通,实现互动式问题解决。明智地使用此工具,以在收集必要信息和避免过多来回之间保持平衡。
<ask_followup_question><question>Your question here</question></ask_followup_question>
attempt_completion
确定任务完成。
重要提示:在确认用户之前的工具使用成功之前,不能使用此工具。否则会导致代码损坏和系统故障。在使用此工具之前,您必须在<thinking></thinking>
标签中问自己是否已确认用户之前的工具使用成功。如果没有,则不要使用此工具。
<attempt_completion><result>Your final result description here</result><command>Command to demonstrate result (optional)</command></attempt_completion>
MCP
Model Context Protocol,为 Cline 扩展工具能力。
例子:https://github.com/modelcontextprotocol/servers/tree/main/src/github
thinking
可以在 <thinking>
标签中,评估已经拥有的信息以及完成任务所需的信息。
请求逻辑
系统 prompt + 上下文(超长会被裁剪)
裁剪算法 getNextTruncationRange
,超过 contextWindow
,保留第一条,直接裁剪一半。
处理返回
presentAssistantMessage
文件更新
src\integrations\editor\DiffViewProvider.ts
替换标记处理
”<<<<<<< SEARCH”、”=======“和”>>>>>>> REPLACE”标记处理逻辑,关键函数 constructNewFileContent
。
匹配逻辑:
- 精确匹配:在原始内容中查找完全相同的 SEARCH 块
- 行修剪匹配:忽略前后空白进行行匹配
- 块锚点匹配:对于3行以上的块,使用首尾行作为锚点匹配
匹配后就能流式输出 =======
后的内容。
获取顶层函数
应该是唯一使用了 AST 的地方:查询 list_code_definition_names
时会使用 tree-sitter
分析文件 AST 并传到大模型。
版本更新:Plan Mode
新版本系统 prompt 新增了一段:
ACT MODE V.S. PLAN MODE
In each user message, the environment_details will specify the current mode. There are two modes:
- ACT MODE: In this mode, you have access to all tools EXCEPT the plan_mode_response tool. - In ACT MODE, you use tools to accomplish the user's task. Once you've completed the user's task, you use the attempt_completion tool to present the result of the task to the user.- PLAN MODE: In this special mode, you have access to the plan_mode_response tool. - In PLAN MODE, the goal is to gather information and get context to create a detailed plan for accomplishing the task, which the user will review and approve before they switch you to ACT MODE to implement the solution. - In PLAN MODE, when you need to converse with the user or present a plan, you should use the plan_mode_response tool to deliver your response directly, rather than using <thinking> tags to analyze when to respond. Do not talk about using plan_mode_response - just use it directly to share your thoughts and provide helpful answers.
## What is PLAN MODE?
- While you are usually in ACT MODE, the user may switch to PLAN MODE in order to have a back and forth with you to plan how to best accomplish the task.- When starting in PLAN MODE, depending on the user's request, you may need to do some information gathering e.g. using read_file or search_files to get more context about the task. You may also ask the user clarifying questions to get a better understanding of the task.- Once you've gained more context about the user's request, you should architect a detailed plan for how you will accomplish the task.- Then you might ask the user if they are pleased with this plan, or if they would like to make any changes. Think of this as a brainstorming session where you can discuss the task and plan the best way to accomplish it.- Finally once it seems like you've reached a good plan, ask the user to switch you back to ACT MODE to implement the solution.
在 Plan Mode 下会在提问时额外再添加一段:
# Current ModePLAN MODEIn this mode you should focus on information gathering, asking questions, and architecting a solution. Once you have a plan, use the plan_mode_response tool to engage in a conversational back and forth with the user. Do not use the plan_mode_response tool until you've gathered all the information you need e.g. with read_file or ask_followup_question.(Remember: If it seems the user wants you to use tools only available in Act Mode, you should ask the user to "toggle to Act mode" (use those words) - they will have to manually do this themselves with the Plan/Act toggle button below. You do not have the ability to switch to Act Mode yourself, and must wait for the user to do it themselves once they are satisfied with the plan.)
简单来说 Plan Mode 就是多让用户参与方案制定,相反,Act Mode 就是自己思考怎么实现,用户只在一边看着,时而点一下允许读取文件就行了。Plan 和 Act 是可以随时切换的,所以可以在觉得讨论得差不多再进 Act Mode 让它自己跑。
存在问题
- 提问时 at 到一个文件,再在后续需要读同一个文件时,会重复发送该文件内容,大文件这么搞 context 直接爆炸
- 如果你一开始就 at 错文件,他很可能不会再找其他文件直接在当前文件强行回答你的问题