Skip to content

Commit 7c41a9c

Browse files
authored
Merge pull request #526 from RooVetGit/switch_mode_tool
Add a tool to switch modes
2 parents 9464a1b + d1754ca commit 7c41a9c

File tree

9 files changed

+300
-1
lines changed

9 files changed

+300
-1
lines changed

.changeset/tidy-camels-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"roo-cline": patch
3+
---
4+
5+
Add a switch_mode tool to allow Roo to propose switching modes

src/core/Cline.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,8 @@ export class Cline {
10221022
return `[${block.name} for '${block.params.question}']`
10231023
case "attempt_completion":
10241024
return `[${block.name}]`
1025+
case "switch_mode":
1026+
return `[${block.name} to '${block.params.mode_slug}'${block.params.reason ? ` because: ${block.params.reason}` : ""}]`
10251027
}
10261028
}
10271029

@@ -2014,6 +2016,74 @@ export class Cline {
20142016
break
20152017
}
20162018
}
2019+
case "switch_mode": {
2020+
const mode_slug: string | undefined = block.params.mode_slug
2021+
const reason: string | undefined = block.params.reason
2022+
try {
2023+
if (block.partial) {
2024+
const partialMessage = JSON.stringify({
2025+
tool: "switchMode",
2026+
mode: removeClosingTag("mode_slug", mode_slug),
2027+
reason: removeClosingTag("reason", reason),
2028+
})
2029+
await this.ask("tool", partialMessage, block.partial).catch(() => {})
2030+
break
2031+
} else {
2032+
if (!mode_slug) {
2033+
this.consecutiveMistakeCount++
2034+
pushToolResult(await this.sayAndCreateMissingParamError("switch_mode", "mode_slug"))
2035+
break
2036+
}
2037+
this.consecutiveMistakeCount = 0
2038+
2039+
// Verify the mode exists
2040+
const targetMode = getModeBySlug(
2041+
mode_slug,
2042+
(await this.providerRef.deref()?.getState())?.customModes,
2043+
)
2044+
if (!targetMode) {
2045+
pushToolResult(formatResponse.toolError(`Invalid mode: ${mode_slug}`))
2046+
break
2047+
}
2048+
2049+
// Check if already in requested mode
2050+
const currentMode =
2051+
(await this.providerRef.deref()?.getState())?.mode ?? defaultModeSlug
2052+
if (currentMode === mode_slug) {
2053+
pushToolResult(`Already in ${targetMode.name} mode.`)
2054+
break
2055+
}
2056+
2057+
const completeMessage = JSON.stringify({
2058+
tool: "switchMode",
2059+
mode: mode_slug,
2060+
reason,
2061+
})
2062+
2063+
const didApprove = await askApproval("tool", completeMessage)
2064+
if (!didApprove) {
2065+
break
2066+
}
2067+
2068+
// Switch the mode
2069+
const provider = this.providerRef.deref()
2070+
if (provider) {
2071+
await provider.updateGlobalState("mode", mode_slug)
2072+
await provider.postStateToWebview()
2073+
}
2074+
pushToolResult(
2075+
`Successfully switched from ${getModeBySlug(currentMode)?.name ?? currentMode} mode to ${
2076+
targetMode.name
2077+
} mode${reason ? ` because: ${reason}` : ""}.`,
2078+
)
2079+
break
2080+
}
2081+
} catch (error) {
2082+
await handleError("switching mode", error)
2083+
break
2084+
}
2085+
}
2086+
20172087
case "attempt_completion": {
20182088
/*
20192089
this.consecutiveMistakeCount = 0

src/core/assistant-message/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export const toolUseNames = [
2121
"access_mcp_resource",
2222
"ask_followup_question",
2323
"attempt_completion",
24+
"switch_mode",
2425
] as const
2526

2627
// Converts array of tool call names into a union type ("execute_command" | "read_file" | ...)
@@ -47,6 +48,8 @@ export const toolParamNames = [
4748
"diff",
4849
"start_line",
4950
"end_line",
51+
"mode_slug",
52+
"reason",
5053
] as const
5154

5255
export type ToolParamName = (typeof toolParamNames)[number]
@@ -114,3 +117,8 @@ export interface AttemptCompletionToolUse extends ToolUse {
114117
name: "attempt_completion"
115118
params: Partial<Pick<Record<ToolParamName, string>, "result" | "command">>
116119
}
120+
121+
export interface SwitchModeToolUse extends ToolUse {
122+
name: "switch_mode"
123+
params: Partial<Pick<Record<ToolParamName, string>, "mode_slug" | "reason">>
124+
}

src/core/prompts/__tests__/__snapshots__/system.test.ts.snap

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,23 @@ I've updated the CSS
181181
<command>open index.html</command>
182182
</attempt_completion>
183183
184+
## switch_mode
185+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
186+
Parameters:
187+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
188+
- reason: (optional) The reason for switching modes
189+
Usage:
190+
<switch_mode>
191+
<mode_slug>Mode slug here</mode_slug>
192+
<reason>Reason for switching here</reason>
193+
</switch_mode>
194+
195+
Example: Requesting to switch to code mode
196+
<switch_mode>
197+
<mode_slug>code</mode_slug>
198+
<reason>Need to make code changes</reason>
199+
</switch_mode>
200+
184201
# Tool Use Guidelines
185202
186203
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -469,6 +486,23 @@ I've updated the CSS
469486
<command>open index.html</command>
470487
</attempt_completion>
471488
489+
## switch_mode
490+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
491+
Parameters:
492+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
493+
- reason: (optional) The reason for switching modes
494+
Usage:
495+
<switch_mode>
496+
<mode_slug>Mode slug here</mode_slug>
497+
<reason>Reason for switching here</reason>
498+
</switch_mode>
499+
500+
Example: Requesting to switch to code mode
501+
<switch_mode>
502+
<mode_slug>code</mode_slug>
503+
<reason>Need to make code changes</reason>
504+
</switch_mode>
505+
472506
# Tool Use Guidelines
473507
474508
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -757,6 +791,23 @@ I've updated the CSS
757791
<command>open index.html</command>
758792
</attempt_completion>
759793
794+
## switch_mode
795+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
796+
Parameters:
797+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
798+
- reason: (optional) The reason for switching modes
799+
Usage:
800+
<switch_mode>
801+
<mode_slug>Mode slug here</mode_slug>
802+
<reason>Reason for switching here</reason>
803+
</switch_mode>
804+
805+
Example: Requesting to switch to code mode
806+
<switch_mode>
807+
<mode_slug>code</mode_slug>
808+
<reason>Need to make code changes</reason>
809+
</switch_mode>
810+
760811
# Tool Use Guidelines
761812
762813
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -1091,6 +1142,23 @@ I've updated the CSS
10911142
<command>open index.html</command>
10921143
</attempt_completion>
10931144
1145+
## switch_mode
1146+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
1147+
Parameters:
1148+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
1149+
- reason: (optional) The reason for switching modes
1150+
Usage:
1151+
<switch_mode>
1152+
<mode_slug>Mode slug here</mode_slug>
1153+
<reason>Reason for switching here</reason>
1154+
</switch_mode>
1155+
1156+
Example: Requesting to switch to code mode
1157+
<switch_mode>
1158+
<mode_slug>code</mode_slug>
1159+
<reason>Need to make code changes</reason>
1160+
</switch_mode>
1161+
10941162
# Tool Use Guidelines
10951163
10961164
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -1431,6 +1499,23 @@ I've updated the CSS
14311499
<command>open index.html</command>
14321500
</attempt_completion>
14331501
1502+
## switch_mode
1503+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
1504+
Parameters:
1505+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
1506+
- reason: (optional) The reason for switching modes
1507+
Usage:
1508+
<switch_mode>
1509+
<mode_slug>Mode slug here</mode_slug>
1510+
<reason>Reason for switching here</reason>
1511+
</switch_mode>
1512+
1513+
Example: Requesting to switch to code mode
1514+
<switch_mode>
1515+
<mode_slug>code</mode_slug>
1516+
<reason>Need to make code changes</reason>
1517+
</switch_mode>
1518+
14341519
# Tool Use Guidelines
14351520
14361521
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -2129,6 +2214,23 @@ I've updated the CSS
21292214
<command>open index.html</command>
21302215
</attempt_completion>
21312216
2217+
## switch_mode
2218+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
2219+
Parameters:
2220+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
2221+
- reason: (optional) The reason for switching modes
2222+
Usage:
2223+
<switch_mode>
2224+
<mode_slug>Mode slug here</mode_slug>
2225+
<reason>Reason for switching here</reason>
2226+
</switch_mode>
2227+
2228+
Example: Requesting to switch to code mode
2229+
<switch_mode>
2230+
<mode_slug>code</mode_slug>
2231+
<reason>Need to make code changes</reason>
2232+
</switch_mode>
2233+
21322234
# Tool Use Guidelines
21332235
21342236
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -2480,6 +2582,23 @@ I've updated the CSS
24802582
<command>open index.html</command>
24812583
</attempt_completion>
24822584
2585+
## switch_mode
2586+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
2587+
Parameters:
2588+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
2589+
- reason: (optional) The reason for switching modes
2590+
Usage:
2591+
<switch_mode>
2592+
<mode_slug>Mode slug here</mode_slug>
2593+
<reason>Reason for switching here</reason>
2594+
</switch_mode>
2595+
2596+
Example: Requesting to switch to code mode
2597+
<switch_mode>
2598+
<mode_slug>code</mode_slug>
2599+
<reason>Need to make code changes</reason>
2600+
</switch_mode>
2601+
24832602
# Tool Use Guidelines
24842603
24852604
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -2768,6 +2887,23 @@ I've updated the CSS
27682887
<command>open index.html</command>
27692888
</attempt_completion>
27702889
2890+
## switch_mode
2891+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
2892+
Parameters:
2893+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
2894+
- reason: (optional) The reason for switching modes
2895+
Usage:
2896+
<switch_mode>
2897+
<mode_slug>Mode slug here</mode_slug>
2898+
<reason>Reason for switching here</reason>
2899+
</switch_mode>
2900+
2901+
Example: Requesting to switch to code mode
2902+
<switch_mode>
2903+
<mode_slug>code</mode_slug>
2904+
<reason>Need to make code changes</reason>
2905+
</switch_mode>
2906+
27712907
# Tool Use Guidelines
27722908
27732909
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -3099,6 +3235,23 @@ I've updated the CSS
30993235
<command>open index.html</command>
31003236
</attempt_completion>
31013237
3238+
## switch_mode
3239+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
3240+
Parameters:
3241+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
3242+
- reason: (optional) The reason for switching modes
3243+
Usage:
3244+
<switch_mode>
3245+
<mode_slug>Mode slug here</mode_slug>
3246+
<reason>Reason for switching here</reason>
3247+
</switch_mode>
3248+
3249+
Example: Requesting to switch to code mode
3250+
<switch_mode>
3251+
<mode_slug>code</mode_slug>
3252+
<reason>Need to make code changes</reason>
3253+
</switch_mode>
3254+
31023255
# Tool Use Guidelines
31033256
31043257
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -3373,6 +3526,23 @@ I've updated the CSS
33733526
<command>open index.html</command>
33743527
</attempt_completion>
33753528
3529+
## switch_mode
3530+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
3531+
Parameters:
3532+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
3533+
- reason: (optional) The reason for switching modes
3534+
Usage:
3535+
<switch_mode>
3536+
<mode_slug>Mode slug here</mode_slug>
3537+
<reason>Reason for switching here</reason>
3538+
</switch_mode>
3539+
3540+
Example: Requesting to switch to code mode
3541+
<switch_mode>
3542+
<mode_slug>code</mode_slug>
3543+
<reason>Need to make code changes</reason>
3544+
</switch_mode>
3545+
33763546
# Tool Use Guidelines
33773547
33783548
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.

src/core/prompts/tools/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getAskFollowupQuestionDescription } from "./ask-followup-question"
99
import { getAttemptCompletionDescription } from "./attempt-completion"
1010
import { getUseMcpToolDescription } from "./use-mcp-tool"
1111
import { getAccessMcpResourceDescription } from "./access-mcp-resource"
12+
import { getSwitchModeDescription } from "./switch-mode"
1213
import { DiffStrategy } from "../../diff/DiffStrategy"
1314
import { McpHub } from "../../../services/mcp/McpHub"
1415
import { Mode, ModeConfig, getModeConfig, isToolAllowedForMode, getGroupName } from "../../../shared/modes"
@@ -28,6 +29,7 @@ const toolDescriptionMap: Record<string, (args: ToolArgs) => string | undefined>
2829
attempt_completion: () => getAttemptCompletionDescription(),
2930
use_mcp_tool: (args) => getUseMcpToolDescription(args),
3031
access_mcp_resource: (args) => getAccessMcpResourceDescription(args),
32+
switch_mode: () => getSwitchModeDescription(),
3133
apply_diff: (args) =>
3234
args.diffStrategy ? args.diffStrategy.getToolDescription({ cwd: args.cwd, toolOptions: args.toolOptions }) : "",
3335
}
@@ -97,4 +99,5 @@ export {
9799
getAttemptCompletionDescription,
98100
getUseMcpToolDescription,
99101
getAccessMcpResourceDescription,
102+
getSwitchModeDescription,
100103
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function getSwitchModeDescription(): string {
2+
return `## switch_mode
3+
Description: Request to switch to a different mode. This tool allows modes to request switching to another mode when needed, such as switching to Code mode to make code changes. The user must approve the mode switch.
4+
Parameters:
5+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
6+
- reason: (optional) The reason for switching modes
7+
Usage:
8+
<switch_mode>
9+
<mode_slug>Mode slug here</mode_slug>
10+
<reason>Reason for switching here</reason>
11+
</switch_mode>
12+
13+
Example: Requesting to switch to code mode
14+
<switch_mode>
15+
<mode_slug>code</mode_slug>
16+
<reason>Need to make code changes</reason>
17+
</switch_mode>`
18+
}

0 commit comments

Comments
 (0)