wen aidev
Published on

RooCode 底層原理解析 03

RooCode 底層架構概覽圖

這篇文章偏向研究性質,介紹 RooCode 的底層運作原理,基本上不影響實際使用,但可以幫助開發者更深入理解這個工具的設計思維。

RooCode 核心架構

RooCode 的基本原理就是在 System Prompt 裡面寫了非常多個「Tools」,根據不同情況調用相應的工具。

System Prompt 結構

RooCode 的 System Prompt 非常龐大,以下是節錄的一段: 可以看到 <read_file></read_file>就是其中一個工具

# You are Roo, 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 accomplish a given task, 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:
<tool_name>
<parameter1_name>value1</parameter1_name>
<parameter2_name>value2</parameter2_name>
...
</tool_name>

For example:
<read_file>
<path>src/main.js</path>
</read_file>

因為 System Prompt 非常長,所以每次你開視窗問第一個問題就會突然暴增一堆 Token。工具越多(MCP),自然 Token 也越多。

Always Available Tools(永遠可用的工具)

無論當前模式如何,都可以存取某些核心工具:

  • ask_followup_question:詢問使用者更多資訊
  • attempt_completion:標記任務完成
  • switch_mode:更改操作模式
  • new_task:建立子任務

Available Tools(可用工具總覽)

RooCode 可用工具總覽

RooCode 可用工具列表

RooCode 工具分類

工具分類與功能說明

工具功能解析

這些工具其實很好理解,主要功能包括:

  • 檔案操作:VS Code 讀取、搜索、編輯、比較差異
  • 外部整合:調用 MCP(Model Context Protocol)
  • 瀏覽器工具:直接在 VS Code 中開啟瀏覽器
  • 任務管理:創建和管理子任務

特別值得注意的工具

1. Browser Tools

你可以直接命令 RooCode 開啟瀏覽器,這樣可以在 VS Code 中打開瀏覽器讓 RooCode 理解網頁內容。

參考影片:RooCode Browser Integration

2. new_task 工具

這是 Orchestrator 模式的運作核心

Orchestrator 模式的核心是將大型複雜項目拆分成可管理的小任務:

  • 任務拆分:將複雜項目分解成多個子任務
  • 模式分配:每個子任務可以使用不同的專門模式(code、ask、architect)
  • 層級管理:維持父子任務的層級關係

new_task 運作原理

當使用 new_task 工具時:

  1. 暫停父任務:當前任務狀態被保存
  2. 創建子任務:建立新的任務環境
  3. 結果傳遞:子任務完成後,結果傳回父任務
  4. 恢復執行:父任務繼續執行

運作流程

  1. 參數驗證:檢查模式和訊息參數
  2. 任務堆疊管理:暫停父任務,記錄狀態
  3. 上下文切換:建立新任務環境
  4. 模式切換:啟動指定的專門模式
  5. 結果傳遞:子任務完成後將結果傳回父任務

Orchestrator 模式 Prompt 範例

在 Orchestrator 設定的 Mode-specific Custom Instructions 中,會直接指定使用 new_task

Your role is to coordinate complex workflows by delegating tasks to specialized modes. As an orchestrator, you should:

1. When given a complex task, break it down into logical subtasks that can be delegated to appropriate specialized modes.

2. For each subtask, use the `new_task` tool to delegate. Choose the most appropriate mode for the subtask's specific goal and provide comprehensive instructions in the `message` parameter. These instructions must include:
    *   All necessary context from the parent task or previous subtasks required to complete the work.
    *   A clearly defined scope, specifying exactly what the subtask should accomplish.
    *   An explicit statement that the subtask should *only* perform the work outlined in these instructions and not deviate.
    *   An instruction for the subtask to signal completion by using the `attempt_completion` tool, providing a concise yet thorough summary of the outcome in the `result` parameter, keeping in mind that this summary will be the source of truth used to keep track of what was completed on this project.
    *   A statement that these specific instructions supersede any conflicting general instructions the subtask's mode might have.

3. Track and manage the progress of all subtasks. When a subtask is completed, analyze its results and determine the next steps.

4. Help the user understand how the different subtasks fit together in the overall workflow. Provide clear reasoning about why you're delegating specific tasks to specific modes.

5. When all subtasks are completed, synthesize the results and provide a comprehensive overview of what was accomplished.

6. Ask clarifying questions when necessary to better understand how to break down complex tasks effectively.

7. Suggest improvements to the workflow based on the results of completed subtasks.

Use subtasks to maintain clarity. If a request significantly shifts focus or requires a different expertise (mode), consider creating a subtask rather than overloading the current one.

實際運行範例

實際運行時的工作流程會是這樣的:

父任務(Orchestrator 模式)
├── 子任務 1(Code 模式)
├── 子任務 2(Architect 模式)
└── 子任務 3(Debug 模式)

每個子任務完成後,結果會自動傳回父任務,讓整體項目能夠有序進行。

自定義模式開發

如何創建自定義父子任務調度模式

所以如果想要自訂 (Customizing Modes)自己的父子任務調度模式可以參考Orchestrator prompt, 使用 new_task tool

總結

RooCode 的底層架構:

  1. 工具化設計:將所有功能封裝成可調用的工具
  2. 任務分解(Orchestrator 模式):是使用new_task 這個工具拆分成子任務
  3. 上下文維護:確保任務間的狀態和資訊正確傳遞,每個子任務完成後,結果會自動傳回父任務,讓整體項目能夠有序進行。

參考資料

台灣用戶:

透過 LINE Pay 支持

國際用戶:

透過 Ko-fi 支持

留言討論