Skip to content

Commit d1754ca

Browse files
committed
Add a tool to switch modes
1 parent 0a32e24 commit d1754ca

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

@@ -2006,6 +2008,74 @@ export class Cline {
20062008
break
20072009
}
20082010
}
2011+
case "switch_mode": {
2012+
const mode_slug: string | undefined = block.params.mode_slug
2013+
const reason: string | undefined = block.params.reason
2014+
try {
2015+
if (block.partial) {
2016+
const partialMessage = JSON.stringify({
2017+
tool: "switchMode",
2018+
mode: removeClosingTag("mode_slug", mode_slug),
2019+
reason: removeClosingTag("reason", reason),
2020+
})
2021+
await this.ask("tool", partialMessage, block.partial).catch(() => {})
2022+
break
2023+
} else {
2024+
if (!mode_slug) {
2025+
this.consecutiveMistakeCount++
2026+
pushToolResult(await this.sayAndCreateMissingParamError("switch_mode", "mode_slug"))
2027+
break
2028+
}
2029+
this.consecutiveMistakeCount = 0
2030+
2031+
// Verify the mode exists
2032+
const targetMode = getModeBySlug(
2033+
mode_slug,
2034+
(await this.providerRef.deref()?.getState())?.customModes,
2035+
)
2036+
if (!targetMode) {
2037+
pushToolResult(formatResponse.toolError(`Invalid mode: ${mode_slug}`))
2038+
break
2039+
}
2040+
2041+
// Check if already in requested mode
2042+
const currentMode =
2043+
(await this.providerRef.deref()?.getState())?.mode ?? defaultModeSlug
2044+
if (currentMode === mode_slug) {
2045+
pushToolResult(`Already in ${targetMode.name} mode.`)
2046+
break
2047+
}
2048+
2049+
const completeMessage = JSON.stringify({
2050+
tool: "switchMode",
2051+
mode: mode_slug,
2052+
reason,
2053+
})
2054+
2055+
const didApprove = await askApproval("tool", completeMessage)
2056+
if (!didApprove) {
2057+
break
2058+
}
2059+
2060+
// Switch the mode
2061+
const provider = this.providerRef.deref()
2062+
if (provider) {
2063+
await provider.updateGlobalState("mode", mode_slug)
2064+
await provider.postStateToWebview()
2065+
}
2066+
pushToolResult(
2067+
`Successfully switched from ${getModeBySlug(currentMode)?.name ?? currentMode} mode to ${
2068+
targetMode.name
2069+
} mode${reason ? ` because: ${reason}` : ""}.`,
2070+
)
2071+
break
2072+
}
2073+
} catch (error) {
2074+
await handleError("switching mode", error)
2075+
break
2076+
}
2077+
}
2078+
20092079
case "attempt_completion": {
20102080
/*
20112081
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.
@@ -466,6 +483,23 @@ I've updated the CSS
466483
<command>open index.html</command>
467484
</attempt_completion>
468485
486+
## switch_mode
487+
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.
488+
Parameters:
489+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
490+
- reason: (optional) The reason for switching modes
491+
Usage:
492+
<switch_mode>
493+
<mode_slug>Mode slug here</mode_slug>
494+
<reason>Reason for switching here</reason>
495+
</switch_mode>
496+
497+
Example: Requesting to switch to code mode
498+
<switch_mode>
499+
<mode_slug>code</mode_slug>
500+
<reason>Need to make code changes</reason>
501+
</switch_mode>
502+
469503
# Tool Use Guidelines
470504
471505
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -751,6 +785,23 @@ I've updated the CSS
751785
<command>open index.html</command>
752786
</attempt_completion>
753787
788+
## switch_mode
789+
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.
790+
Parameters:
791+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
792+
- reason: (optional) The reason for switching modes
793+
Usage:
794+
<switch_mode>
795+
<mode_slug>Mode slug here</mode_slug>
796+
<reason>Reason for switching here</reason>
797+
</switch_mode>
798+
799+
Example: Requesting to switch to code mode
800+
<switch_mode>
801+
<mode_slug>code</mode_slug>
802+
<reason>Need to make code changes</reason>
803+
</switch_mode>
804+
754805
# Tool Use Guidelines
755806
756807
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -1082,6 +1133,23 @@ I've updated the CSS
10821133
<command>open index.html</command>
10831134
</attempt_completion>
10841135
1136+
## switch_mode
1137+
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.
1138+
Parameters:
1139+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
1140+
- reason: (optional) The reason for switching modes
1141+
Usage:
1142+
<switch_mode>
1143+
<mode_slug>Mode slug here</mode_slug>
1144+
<reason>Reason for switching here</reason>
1145+
</switch_mode>
1146+
1147+
Example: Requesting to switch to code mode
1148+
<switch_mode>
1149+
<mode_slug>code</mode_slug>
1150+
<reason>Need to make code changes</reason>
1151+
</switch_mode>
1152+
10851153
# Tool Use Guidelines
10861154
10871155
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -1419,6 +1487,23 @@ I've updated the CSS
14191487
<command>open index.html</command>
14201488
</attempt_completion>
14211489
1490+
## switch_mode
1491+
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.
1492+
Parameters:
1493+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
1494+
- reason: (optional) The reason for switching modes
1495+
Usage:
1496+
<switch_mode>
1497+
<mode_slug>Mode slug here</mode_slug>
1498+
<reason>Reason for switching here</reason>
1499+
</switch_mode>
1500+
1501+
Example: Requesting to switch to code mode
1502+
<switch_mode>
1503+
<mode_slug>code</mode_slug>
1504+
<reason>Need to make code changes</reason>
1505+
</switch_mode>
1506+
14221507
# Tool Use Guidelines
14231508
14241509
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -2114,6 +2199,23 @@ I've updated the CSS
21142199
<command>open index.html</command>
21152200
</attempt_completion>
21162201
2202+
## switch_mode
2203+
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.
2204+
Parameters:
2205+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
2206+
- reason: (optional) The reason for switching modes
2207+
Usage:
2208+
<switch_mode>
2209+
<mode_slug>Mode slug here</mode_slug>
2210+
<reason>Reason for switching here</reason>
2211+
</switch_mode>
2212+
2213+
Example: Requesting to switch to code mode
2214+
<switch_mode>
2215+
<mode_slug>code</mode_slug>
2216+
<reason>Need to make code changes</reason>
2217+
</switch_mode>
2218+
21172219
# Tool Use Guidelines
21182220
21192221
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -2462,6 +2564,23 @@ I've updated the CSS
24622564
<command>open index.html</command>
24632565
</attempt_completion>
24642566
2567+
## switch_mode
2568+
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.
2569+
Parameters:
2570+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
2571+
- reason: (optional) The reason for switching modes
2572+
Usage:
2573+
<switch_mode>
2574+
<mode_slug>Mode slug here</mode_slug>
2575+
<reason>Reason for switching here</reason>
2576+
</switch_mode>
2577+
2578+
Example: Requesting to switch to code mode
2579+
<switch_mode>
2580+
<mode_slug>code</mode_slug>
2581+
<reason>Need to make code changes</reason>
2582+
</switch_mode>
2583+
24652584
# Tool Use Guidelines
24662585
24672586
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -2747,6 +2866,23 @@ I've updated the CSS
27472866
<command>open index.html</command>
27482867
</attempt_completion>
27492868
2869+
## switch_mode
2870+
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.
2871+
Parameters:
2872+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
2873+
- reason: (optional) The reason for switching modes
2874+
Usage:
2875+
<switch_mode>
2876+
<mode_slug>Mode slug here</mode_slug>
2877+
<reason>Reason for switching here</reason>
2878+
</switch_mode>
2879+
2880+
Example: Requesting to switch to code mode
2881+
<switch_mode>
2882+
<mode_slug>code</mode_slug>
2883+
<reason>Need to make code changes</reason>
2884+
</switch_mode>
2885+
27502886
# Tool Use Guidelines
27512887
27522888
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -3038,6 +3174,23 @@ I've updated the CSS
30383174
<command>open index.html</command>
30393175
</attempt_completion>
30403176
3177+
## switch_mode
3178+
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.
3179+
Parameters:
3180+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
3181+
- reason: (optional) The reason for switching modes
3182+
Usage:
3183+
<switch_mode>
3184+
<mode_slug>Mode slug here</mode_slug>
3185+
<reason>Reason for switching here</reason>
3186+
</switch_mode>
3187+
3188+
Example: Requesting to switch to code mode
3189+
<switch_mode>
3190+
<mode_slug>code</mode_slug>
3191+
<reason>Need to make code changes</reason>
3192+
</switch_mode>
3193+
30413194
# Tool Use Guidelines
30423195
30433196
1. In <thinking> tags, assess what information you already have and what information you need to proceed with the task.
@@ -3272,6 +3425,23 @@ I've updated the CSS
32723425
<command>open index.html</command>
32733426
</attempt_completion>
32743427
3428+
## switch_mode
3429+
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.
3430+
Parameters:
3431+
- mode_slug: (required) The slug of the mode to switch to (e.g., "code", "ask", "architect")
3432+
- reason: (optional) The reason for switching modes
3433+
Usage:
3434+
<switch_mode>
3435+
<mode_slug>Mode slug here</mode_slug>
3436+
<reason>Reason for switching here</reason>
3437+
</switch_mode>
3438+
3439+
Example: Requesting to switch to code mode
3440+
<switch_mode>
3441+
<mode_slug>code</mode_slug>
3442+
<reason>Need to make code changes</reason>
3443+
</switch_mode>
3444+
32753445
# Tool Use Guidelines
32763446
32773447
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 } 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
}
@@ -93,4 +95,5 @@ export {
9395
getAttemptCompletionDescription,
9496
getUseMcpToolDescription,
9597
getAccessMcpResourceDescription,
98+
getSwitchModeDescription,
9699
}
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)