Skip to content

Commit 13de490

Browse files
committed
Add a new_task tool
1 parent 4026a87 commit 13de490

File tree

12 files changed

+343
-9
lines changed

12 files changed

+343
-9
lines changed

src/core/Cline.ts

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ export class Cline {
631631

632632
let newUserContent: UserContent = [...modifiedOldUserContent]
633633

634-
const agoText = (() => {
634+
const agoText = ((): string => {
635635
const timestamp = lastClineMessage?.ts ?? Date.now()
636636
const now = Date.now()
637637
const diff = now - timestamp
@@ -996,7 +996,7 @@ export class Cline {
996996
break
997997
}
998998
case "tool_use":
999-
const toolDescription = () => {
999+
const toolDescription = (): string => {
10001000
switch (block.name) {
10011001
case "execute_command":
10021002
return `[${block.name} for '${block.params.command}']`
@@ -1030,6 +1030,12 @@ export class Cline {
10301030
return `[${block.name}]`
10311031
case "switch_mode":
10321032
return `[${block.name} to '${block.params.mode_slug}'${block.params.reason ? ` because: ${block.params.reason}` : ""}]`
1033+
case "new_task": {
1034+
const mode = block.params.mode ?? defaultModeSlug
1035+
const message = block.params.message ?? "(no message)"
1036+
const modeName = getModeBySlug(mode, customModes)?.name ?? mode
1037+
return `[${block.name} in ${modeName} mode: '${message}']`
1038+
}
10331039
}
10341040
}
10351041

@@ -2402,6 +2408,74 @@ export class Cline {
24022408
}
24032409
}
24042410

2411+
case "new_task": {
2412+
const mode: string | undefined = block.params.mode
2413+
const message: string | undefined = block.params.message
2414+
try {
2415+
if (block.partial) {
2416+
const partialMessage = JSON.stringify({
2417+
tool: "newTask",
2418+
mode: removeClosingTag("mode", mode),
2419+
message: removeClosingTag("message", message),
2420+
})
2421+
await this.ask("tool", partialMessage, block.partial).catch(() => {})
2422+
break
2423+
} else {
2424+
if (!mode) {
2425+
this.consecutiveMistakeCount++
2426+
pushToolResult(await this.sayAndCreateMissingParamError("new_task", "mode"))
2427+
break
2428+
}
2429+
if (!message) {
2430+
this.consecutiveMistakeCount++
2431+
pushToolResult(await this.sayAndCreateMissingParamError("new_task", "message"))
2432+
break
2433+
}
2434+
this.consecutiveMistakeCount = 0
2435+
2436+
// Verify the mode exists
2437+
const targetMode = getModeBySlug(
2438+
mode,
2439+
(await this.providerRef.deref()?.getState())?.customModes,
2440+
)
2441+
if (!targetMode) {
2442+
pushToolResult(formatResponse.toolError(`Invalid mode: ${mode}`))
2443+
break
2444+
}
2445+
2446+
// Show what we're about to do
2447+
const toolMessage = JSON.stringify({
2448+
tool: "newTask",
2449+
mode: targetMode.name,
2450+
content: message,
2451+
})
2452+
2453+
const didApprove = await askApproval("tool", toolMessage)
2454+
if (!didApprove) {
2455+
break
2456+
}
2457+
2458+
// Switch mode first, then create new task instance
2459+
const provider = this.providerRef.deref()
2460+
if (provider) {
2461+
await provider.handleModeSwitch(mode)
2462+
await provider.initClineWithTask(message)
2463+
pushToolResult(
2464+
`Successfully created new task in ${targetMode.name} mode with message: ${message}`,
2465+
)
2466+
} else {
2467+
pushToolResult(
2468+
formatResponse.toolError("Failed to create new task: provider not available"),
2469+
)
2470+
}
2471+
break
2472+
}
2473+
} catch (error) {
2474+
await handleError("creating new task", error)
2475+
break
2476+
}
2477+
}
2478+
24052479
case "attempt_completion": {
24062480
/*
24072481
this.consecutiveMistakeCount = 0

src/core/assistant-message/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const toolUseNames = [
2424
"ask_followup_question",
2525
"attempt_completion",
2626
"switch_mode",
27+
"new_task",
2728
] as const
2829

2930
// Converts array of tool call names into a union type ("execute_command" | "read_file" | ...)
@@ -53,6 +54,8 @@ export const toolParamNames = [
5354
"mode_slug",
5455
"reason",
5556
"operations",
57+
"mode",
58+
"message",
5659
] as const
5760

5861
export type ToolParamName = (typeof toolParamNames)[number]
@@ -130,3 +133,8 @@ export interface SwitchModeToolUse extends ToolUse {
130133
name: "switch_mode"
131134
params: Partial<Pick<Record<ToolParamName, string>, "mode_slug" | "reason">>
132135
}
136+
137+
export interface NewTaskToolUse extends ToolUse {
138+
name: "new_task"
139+
params: Partial<Pick<Record<ToolParamName, string>, "mode" | "message">>
140+
}

0 commit comments

Comments
 (0)