Skip to content

Commit 3cb881d

Browse files
committed
feat: enhance mode validation and error handling in newTaskTool
1 parent 63ff96d commit 3cb881d

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/core/tools/newTaskTool.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RooCodeEventName } from "@roo-code/types"
55
import { ToolUse, AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "../../shared/tools"
66
import { Task } from "../task/Task"
77
import { defaultModeSlug, getModeBySlug } from "../../shared/modes"
8+
import { ModeManager } from "../../services/ModeManager"
89
import { formatResponse } from "../prompts/responses"
910
import { t } from "../../i18n"
1011

@@ -57,6 +58,28 @@ export async function newTaskTool(
5758
return
5859
}
5960

61+
// Validate mode availability (not disabled)
62+
const provider = cline.providerRef.deref()
63+
64+
if (!provider || !provider.context || !provider.customModesManager) {
65+
pushToolResult(formatResponse.toolError("Unable to access mode configuration."))
66+
return
67+
}
68+
69+
const modeManager = new ModeManager(provider.context, provider.customModesManager)
70+
const validationResult = await modeManager.validateModeSwitch(mode)
71+
72+
if (!validationResult.isValid) {
73+
cline.recordToolError("new_task")
74+
// Provide helpful error message with available modes
75+
const enabledModes = await modeManager.getEnabledModes()
76+
const availableModesList = enabledModes.map((m) => `- ${m.slug}: ${m.name}`).join("\n")
77+
78+
const errorMessage = `${validationResult.errorMessage}\n\nAvailable enabled modes:\n${availableModesList}`
79+
pushToolResult(formatResponse.toolError(errorMessage))
80+
return
81+
}
82+
6083
const toolMessage = JSON.stringify({
6184
tool: "newTask",
6285
mode: targetMode.name,
@@ -69,9 +92,10 @@ export async function newTaskTool(
6992
return
7093
}
7194

72-
const provider = cline.providerRef.deref()
95+
// provider used for creating task
96+
const providerForCreation = cline.providerRef.deref()
7397

74-
if (!provider) {
98+
if (!providerForCreation) {
7599
return
76100
}
77101

@@ -80,18 +104,18 @@ export async function newTaskTool(
80104
}
81105

82106
// Preserve the current mode so we can resume with it later.
83-
cline.pausedModeSlug = (await provider.getState()).mode ?? defaultModeSlug
107+
cline.pausedModeSlug = (await providerForCreation.getState()).mode ?? defaultModeSlug
84108

85109
// Create new task instance first (this preserves parent's current mode in its history)
86-
const newCline = await provider.createTask(unescapedMessage, undefined, cline)
110+
const newCline = await providerForCreation.createTask(unescapedMessage, undefined, cline)
87111

88112
if (!newCline) {
89113
pushToolResult(t("tools:newTask.errors.policy_restriction"))
90114
return
91115
}
92116

93117
// Now switch the newly created task to the desired mode
94-
await provider.handleModeSwitch(mode)
118+
await providerForCreation.handleModeSwitch(mode)
95119

96120
// Delay to allow mode change to take effect
97121
await delay(500)

0 commit comments

Comments
 (0)