|
|
|
|
@ -47,8 +47,33 @@ export function activate(context: vscode.ExtensionContext) {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 注册选择文件命令
|
|
|
|
|
const selectFileCommand = vscode.commands.registerCommand('ai-chat.selectFileAsContext', async () => {
|
|
|
|
|
const uris = await vscode.window.showOpenDialog({
|
|
|
|
|
canSelectFiles: true,
|
|
|
|
|
canSelectFolders: false,
|
|
|
|
|
canSelectMany: false, // 单选
|
|
|
|
|
title: "选择一个文件作为上下文"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (uris && uris.length > 0) {
|
|
|
|
|
const selectedFileUri = uris[0];
|
|
|
|
|
const fileContent = await vscode.workspace.fs.readFile(selectedFileUri);
|
|
|
|
|
const decodedContent = new TextDecoder("utf-8").decode(fileContent);
|
|
|
|
|
|
|
|
|
|
// 发送文件内容到 Webview
|
|
|
|
|
if (panel && panel.webview) {
|
|
|
|
|
panel.webview.postMessage({
|
|
|
|
|
command: 'setContextFile',
|
|
|
|
|
fileName: selectedFileUri.fsPath,
|
|
|
|
|
fileContent: decodedContent
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 添加到 subscriptions
|
|
|
|
|
context.subscriptions.push(statusBarItem, openWebviewCommand, addToChatCommand);
|
|
|
|
|
context.subscriptions.push(statusBarItem, openWebviewCommand, addToChatCommand,selectFileCommand);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function openWebview(
|
|
|
|
|
@ -117,6 +142,27 @@ function openWebview(
|
|
|
|
|
|
|
|
|
|
panel.webview.postMessage({ command: 'hideLoading' });
|
|
|
|
|
break;
|
|
|
|
|
case 'selectContextFile':
|
|
|
|
|
// 执行文件选择逻辑
|
|
|
|
|
const uris = await vscode.window.showOpenDialog({
|
|
|
|
|
canSelectFiles: true,
|
|
|
|
|
canSelectFolders: false,
|
|
|
|
|
canSelectMany: false,
|
|
|
|
|
title: "选择一个文件作为上下文"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (uris && uris.length > 0) {
|
|
|
|
|
const selectedFileUri = uris[0];
|
|
|
|
|
const fileContent = await vscode.workspace.fs.readFile(selectedFileUri);
|
|
|
|
|
const decodedContent = new TextDecoder("utf-8").decode(fileContent);
|
|
|
|
|
|
|
|
|
|
panel.webview.postMessage({
|
|
|
|
|
command: 'setContextFile',
|
|
|
|
|
fileName: selectedFileUri.fsPath,
|
|
|
|
|
fileContent: decodedContent
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
undefined,
|
|
|
|
|
@ -187,8 +233,22 @@ function getWebviewContent(styleUri: vscode.Uri, scriptUri: vscode.Uri,highlight
|
|
|
|
|
</head>
|
|
|
|
|
<body>
|
|
|
|
|
<div class="chat-container">
|
|
|
|
|
<!--对话区-->
|
|
|
|
|
<div id="chat-box"></div>
|
|
|
|
|
|
|
|
|
|
<!--文件选择-->
|
|
|
|
|
<div class="context-container">
|
|
|
|
|
<button id="select-context-btn" class="select-context-btn" title="选择上下文文件">+</button>
|
|
|
|
|
|
|
|
|
|
<div id="context-placeholder" class="context-placeholder">
|
|
|
|
|
<span class="placeholder-text">添加上下文</span>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div id="context-tab" class="context-tab hidden">
|
|
|
|
|
<span id="context-file-name" class="file-name">添加上下文</span>
|
|
|
|
|
<button id="close-context-btn" class="close-btn" title="清除上下文">×</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<!-- 聊天输入表单 -->
|
|
|
|
|
<form id="chat-form">
|
|
|
|
|
<input type="text" id="user-input" placeholder="输入你的问题..." required />
|
|
|
|
|
|