|
| 1 | +import * as vscode from "vscode" |
| 2 | +import { formatResponse } from "../prompts/responses" |
| 3 | +import { ToolUse, AskApproval, HandleError, PushToolResult, RemoveClosingTag } from "../../shared/tools" |
| 4 | +import { Task } from "../task/Task" |
| 5 | + |
| 6 | +export async function useVSCLMT( |
| 7 | + cline: Task, |
| 8 | + toolUse: ToolUse, |
| 9 | + askApproval: AskApproval, |
| 10 | + handleError: HandleError, |
| 11 | + pushToolResult: PushToolResult, |
| 12 | + removeClosingTag: RemoveClosingTag, |
| 13 | +): Promise<void> { |
| 14 | + const { tool_name, arguments: toolArgs } = toolUse.params |
| 15 | + |
| 16 | + // Handle partial tool invocation (missing parameters) |
| 17 | + if (toolUse.partial) { |
| 18 | + const partialMessage = JSON.stringify({ |
| 19 | + type: "vsclmt_tool", |
| 20 | + toolName: removeClosingTag("tool_name", tool_name), |
| 21 | + arguments: removeClosingTag("arguments", toolArgs), |
| 22 | + }) |
| 23 | + |
| 24 | + await cline.ask("tool", partialMessage, toolUse.partial).catch(() => {}) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + // Non-partial: require tool_name |
| 29 | + if (!tool_name) { |
| 30 | + cline.recordToolError("use_vsclmt") |
| 31 | + pushToolResult(await cline.sayAndCreateMissingParamError("use_vsclmt", "tool_name")) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + // Get the vsclmt service from ClineProvider |
| 36 | + const provider = cline.providerRef.deref() |
| 37 | + const vsclmtService = provider?.getVSCLMToolService() |
| 38 | + |
| 39 | + if (!vsclmtService) { |
| 40 | + pushToolResult(formatResponse.toolError("VS Code LM tool system not available")) |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + // Parse arguments if provided |
| 45 | + let parsedArgs: any = {} |
| 46 | + if (toolArgs) { |
| 47 | + try { |
| 48 | + parsedArgs = JSON.parse(toolArgs) |
| 49 | + } catch (error) { |
| 50 | + pushToolResult(formatResponse.toolError(`Invalid JSON arguments: ${error.message}`)) |
| 51 | + return |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + // Check if tool is selected |
| 57 | + if (!vsclmtService.isToolSelected(tool_name)) { |
| 58 | + pushToolResult( |
| 59 | + formatResponse.toolError( |
| 60 | + `Tool '${tool_name}' is not selected for use. Please select it in the Tool Selection panel.`, |
| 61 | + ), |
| 62 | + ) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // Prepare tool invocation to get any user confirmation message |
| 67 | + const prepared = await vsclmtService.prepareToolInvocation(tool_name, parsedArgs) |
| 68 | + if (prepared?.confirmationMessages) { |
| 69 | + // Ask for approval with the tool's custom confirmation message |
| 70 | + const message = |
| 71 | + prepared.confirmationMessages.message instanceof vscode.MarkdownString |
| 72 | + ? prepared.confirmationMessages.message.value |
| 73 | + : prepared.confirmationMessages.message |
| 74 | + |
| 75 | + const confirmText = `${prepared.confirmationMessages.title}\n${message}` |
| 76 | + const approved = await askApproval("tool", confirmText) |
| 77 | + |
| 78 | + if (!approved) { |
| 79 | + pushToolResult(formatResponse.toolDenied()) |
| 80 | + return |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Invoke the tool with any progress message from preparation |
| 85 | + const result = await vsclmtService.invokeTool(tool_name, parsedArgs) |
| 86 | + |
| 87 | + // Format the result for display |
| 88 | + let resultText: string |
| 89 | + if (typeof result === "string") { |
| 90 | + resultText = result |
| 91 | + } else if (result && typeof result === "object") { |
| 92 | + resultText = JSON.stringify(result, null, 2) |
| 93 | + } else { |
| 94 | + resultText = String(result) |
| 95 | + } |
| 96 | + |
| 97 | + pushToolResult(`VS Code LM tool '${tool_name}' executed successfully:\n\n${resultText}`) |
| 98 | + } catch (error) { |
| 99 | + await handleError("VS Code LM tool invocation", error) |
| 100 | + pushToolResult(formatResponse.toolError(`Failed to invoke VS Code LM tool '${tool_name}': ${error.message}`)) |
| 101 | + } |
| 102 | +} |
0 commit comments