|
| 1 | +import * as vscode from "vscode" |
| 2 | +import { Task } from "../task/Task" |
| 3 | +import { BaseTool, ToolCallbacks } from "./BaseTool" |
| 4 | +import { formatResponse } from "../prompts/responses" |
| 5 | +import { Package } from "../../shared/package" |
| 6 | + |
| 7 | +export interface AttemptCompletionParams { |
| 8 | + result: string |
| 9 | + command?: string |
| 10 | +} |
| 11 | + |
| 12 | +export interface AttemptCompletionCallbacks extends ToolCallbacks { |
| 13 | + askFinishSubTaskApproval: (message: string) => Promise<boolean> |
| 14 | + toolDescription?: (toolName: string, json: string) => string |
| 15 | +} |
| 16 | + |
| 17 | +export class AttemptCompletionTool extends BaseTool<"attempt_completion"> { |
| 18 | + readonly name = "attempt_completion" as const |
| 19 | + |
| 20 | + parseLegacy(params: Partial<Record<string, string>>): AttemptCompletionParams { |
| 21 | + return { |
| 22 | + result: params.result || "", |
| 23 | + command: params.command, |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + async execute(params: AttemptCompletionParams, task: Task, callbacks: AttemptCompletionCallbacks): Promise<void> { |
| 28 | + const { result, command } = params |
| 29 | + const { askApproval, handleError, pushToolResult, askFinishSubTaskApproval } = callbacks |
| 30 | + |
| 31 | + try { |
| 32 | + if (!result) { |
| 33 | + task.consecutiveMistakeCount++ |
| 34 | + task.recordToolError("attempt_completion") |
| 35 | + pushToolResult(await task.sayAndCreateMissingParamError("attempt_completion", "result")) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + // Check for open todos if setting enabled |
| 40 | + const preventCompletionWithOpenTodos = vscode.workspace |
| 41 | + .getConfiguration(Package.name) |
| 42 | + .get<boolean>("preventCompletionWithOpenTodos", false) |
| 43 | + |
| 44 | + if (preventCompletionWithOpenTodos && task.todoList) { |
| 45 | + const incompleteTodos = task.todoList.filter((todo) => todo.status !== "completed") |
| 46 | + if (incompleteTodos.length > 0) { |
| 47 | + task.consecutiveMistakeCount++ |
| 48 | + task.recordToolError("attempt_completion") |
| 49 | + pushToolResult( |
| 50 | + formatResponse.toolError( |
| 51 | + "Cannot complete task while there are incomplete todos. Please complete or cancel them first.", |
| 52 | + ), |
| 53 | + ) |
| 54 | + return |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + task.consecutiveMistakeCount = 0 |
| 59 | + |
| 60 | + // If it's a subtask (has parentTaskId), we need special handling |
| 61 | + if (task.parentTaskId) { |
| 62 | + const toolMessage = JSON.stringify({ |
| 63 | + tool: "attemptCompletion", |
| 64 | + content: result, |
| 65 | + command, |
| 66 | + }) |
| 67 | + |
| 68 | + const didApprove = await askFinishSubTaskApproval(toolMessage) |
| 69 | + if (!didApprove) { |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + const provider = task.providerRef.deref() |
| 74 | + if (provider) { |
| 75 | + await (provider as any).reopenParentFromDelegation({ |
| 76 | + parentTaskId: task.parentTaskId, |
| 77 | + childTaskId: task.taskId, |
| 78 | + completionResultSummary: result, |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + pushToolResult(result) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + // Root task completion |
| 87 | + const toolMessage = JSON.stringify({ |
| 88 | + tool: "attemptCompletion", |
| 89 | + content: result, |
| 90 | + command, |
| 91 | + }) |
| 92 | + |
| 93 | + const didApprove = await askApproval("tool", toolMessage) |
| 94 | + if (!didApprove) { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if (command) { |
| 99 | + await task.say("text", `Executing command: ${command}`) |
| 100 | + // We don't await the command execution here because we're completing the task |
| 101 | + } |
| 102 | + |
| 103 | + pushToolResult(result) |
| 104 | + return |
| 105 | + } catch (error) { |
| 106 | + await handleError("attempting completion", error) |
| 107 | + return |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + override async handlePartial(task: Task, block: any): Promise<void> { |
| 112 | + // No partial handling needed for attempt_completion as it's usually final |
| 113 | + // But we can implement it if needed for consistent UI |
| 114 | + const result = block.params.result |
| 115 | + const command = block.params.command |
| 116 | + if (result || command) { |
| 117 | + const partialMessage = JSON.stringify({ |
| 118 | + tool: "attemptCompletion", |
| 119 | + content: this.removeClosingTag("result", result, block.partial), |
| 120 | + command: this.removeClosingTag("command", command, block.partial), |
| 121 | + }) |
| 122 | + await task.ask("tool", partialMessage, block.partial).catch(() => {}) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +export const attemptCompletionTool = new AttemptCompletionTool() |
0 commit comments