You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于在VS Code Copilot Chat扩展中调用内置编辑类工具失败的技术求助

在VS Code Copilot Chat扩展中调用内置编辑类工具失败的技术求助

我正在开发一款VS Code Copilot Chat扩展,目标是通过代码直接调用Copilot的内置工具,比如copilot_createFile、copilot_editFile这类支持文件编辑/更新的工具。目前像copilot_readFile这种只读工具都能正常工作,但所有涉及修改的工具调用全部失败。

我已经尝试了多种调用变体,包括带工具调用令牌、使用新的取消令牌、不带令牌、甚至不传工具调用令牌等方式,但所有尝试都以失败告终。我非常不想重复实现自定义工具,更希望能直接复用VS Code内置的这些能力,所以想请教:

  • 是否可以从Copilot Chat扩展中直接调用VS Code内置的Copilot编辑/更新类工具?
  • 如果可行,正确的调用姿势是什么?
  • 如果不可行,是不是只能退而求其次实现自定义工具?

以下是我测试用的完整代码逻辑,所有copilot_createFile的尝试都失败了,最后只能用VS Code原生的文件系统API作为降级方案:

let resultMessage = '';

// Test Approach 1: Standard token approach
console.log(' Testing Approach 1: Standard token approach');
try {
    console.log(' Attempting copilot_createFile WITH token...');
    const result1 = await vscode.lm.invokeTool('copilot_createFile', {
        toolInvocationToken: toolInvocationToken,
        input: { filePath: filePath, content: fileContent }
    }, token);
    console.log('✅ Approach 1 SUCCESS: copilot_createFile with token worked!');
    resultMessage += '✅ **Approach 1 (Standard token):** SUCCESS\n';
    return {
        content: [new vscode.LanguageModelTextPart(resultMessage + '\n File created successfully!')]
    };
} catch (error1) {
    const err1 = error1 instanceof Error ? error1 : new Error(String(error1));
    console.log('❌ Approach 1 FAILED:', err1.message);
    resultMessage += `❌ **Approach 1 (Standard token):** FAILED - ${err1.message}\n`;

    // Test Approach 2: Fresh cancellation token
    console.log(' Testing Approach 2: Fresh cancellation token');
    try {
        const freshTokenSource = new vscode.CancellationTokenSource();
        console.log(' Attempting copilot_createFile with fresh cancellation token...');
        const result2 = await vscode.lm.invokeTool('copilot_createFile', {
            toolInvocationToken: toolInvocationToken,
            input: { filePath: filePath, content: fileContent }
        }, freshTokenSource.token);
        console.log('✅ Approach 2 SUCCESS: copilot_createFile with fresh token worked!');
        resultMessage += '✅ **Approach 2 (Fresh cancellation token):** SUCCESS\n';
        return {
            content: [new vscode.LanguageModelTextPart(resultMessage + '\n File created successfully!')]
        };
    } catch (error2) {
        const err2 = error2 instanceof Error ? error2 : new Error(String(error2));
        console.log('❌ Approach 2 FAILED:', err2.message);
        resultMessage += `❌ **Approach 2 (Fresh cancellation token):** FAILED - ${err2.message}\n`;

        // Test Approach 3: No cancellation token
        console.log(' Testing Approach 3: No cancellation token');
        try {
            console.log(' Attempting copilot_createFile without cancellation token...');
            const result3 = await vscode.lm.invokeTool('copilot_createFile', {
                toolInvocationToken: toolInvocationToken,
                input: { filePath: filePath, content: fileContent }
            });
            console.log('✅ Approach 3 SUCCESS: copilot_createFile without cancellation token worked!');
            resultMessage += '✅ **Approach 3 (No cancellation token):** SUCCESS\n';
            return {
                content: [new vscode.LanguageModelTextPart(resultMessage + '\n File created successfully!')]
            };
        } catch (error3) {
            const err3 = error3 instanceof Error ? error3 : new Error(String(error3));
            console.log('❌ Approach 3 FAILED:', err3.message);
            resultMessage += `❌ **Approach 3 (No cancellation token):** FAILED - ${err3.message}\n`;

            // Test Approach 4: Undefined token
            console.log(' Testing Approach 4: Undefined token');
            try {
                console.log(' Attempting copilot_createFile with undefined token...');
                const result4 = await vscode.lm.invokeTool('copilot_createFile', {
                    toolInvocationToken: undefined,
                    input: { filePath: filePath, content: fileContent }
                }, token);
                console.log('✅ Approach 4 SUCCESS: copilot_createFile with undefined token worked!');
                resultMessage += '✅ **Approach 4 (Undefined token):** SUCCESS\n';
                return {
                    content: [new vscode.LanguageModelTextPart(resultMessage + '\n File created successfully!')]
                };
            } catch (error4) {
                const err4 = error4 instanceof Error ? error4 : new Error(String(error4));
                console.log('❌ Approach 4 FAILED:', err4.message);
                resultMessage += `❌ **Approach 4 (Undefined token):** FAILED - ${err4.message}\n`;

                // Fallback: VS Code file system API
                console.log(' All copilot_createFile approaches failed, trying VS Code file system API...');
                try {
                    const uri = vscode.Uri.file(filePath);
                    const content = new TextEncoder().encode(fileContent);
                    await vscode.workspace.fs.writeFile(uri, content);
                    console.log('✅ Fallback SUCCESS: VS Code file system API worked!');
                    resultMessage += '✅ **Fallback (VS Code FS API):** SUCCESS\n';
                    // Try to open the file
                    try {
                        const document = await vscode.workspace.openTextDocument(uri);
                        await vscode.window.showTextDocument(document);
                        console.log('✅ File opened in editor');
                        resultMessage += '✅ **File opened in editor:** SUCCESS\n';
                    } catch (openError) {
                        console.log('⚠️ Could not open file in editor:', openError.message);
                        resultMessage += `⚠️ **File opened in editor:** FAILED - ${openError.message}\n`;
                    }
                    return {
                        content: [new vscode.LanguageModelTextPart(resultMessage + '\n File created successfully via fallback!')]
                    };
                } catch (fsError) {
                    const fsErr = fsError instanceof Error ? fsError : new Error(String(fsError));
                    console.log('❌ Fallback FAILED:', fsErr.message);
                    resultMessage += `❌ **Fallback (VS Code FS API):** FAILED - ${fsErr.message}\n`;
                    return {
                        content: [new vscode.LanguageModelTextPart(resultMessage + '\n❌ File creation failed entirely!')]
                    };
                }
            }
        }
    }
}

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 10:09:28