172 lines
5.8 KiB
TypeScript
Raw Normal View History

2023-05-05 21:27:40 +08:00
import * as vscode from 'vscode';
import ChatPanel from '../panel/chatPanel';
import { sendFileSelectMessage, sendCodeSelectMessage } from './util';
import { logger } from '../util/logger';
import * as childProcess from 'child_process';
2023-05-16 14:35:37 +08:00
import { DevChatViewProvider } from '../panel/devchatView';
2023-05-16 14:24:24 +08:00
import ExtensionContextHolder from '../util/extensionContext';
2023-05-16 14:24:24 +08:00
export function checkDevChatDependency() {
// 执行系统命令,检查依赖程序是否已经安装
try {
const result = childProcess.execSync('devchat --help');
// 命令执行成功,依赖程序已经安装
return true;
} catch (error) {
// 命令执行失败,依赖程序未安装
return false;
}
}
2023-05-16 14:24:24 +08:00
export async function checkOpenAiAPIKey() {
const secretStorage: vscode.SecretStorage = ExtensionContextHolder.context!.secrets;
let openaiApiKey = await secretStorage.get("devchat_OPENAI_API_KEY");
if (!openaiApiKey) {
openaiApiKey = vscode.workspace.getConfiguration('DevChat').get('OpenAI.apiKey');
}
if (!openaiApiKey) {
openaiApiKey = process.env.OPENAI_API_KEY;
}
if (!openaiApiKey) {
return false;
}
return true;
}
function checkOpenAIKey() {
let openaiApiKey = vscode.workspace.getConfiguration('DevChat').get('OpenAI.apiKey');
if (!openaiApiKey) {
openaiApiKey = process.env.OPENAI_API_KEY;
}
if (!openaiApiKey) {
// openAI key 未设置请用户输入API Key
vscode.window.showInputBox({
placeHolder: 'Please input your openAI API Key'
}).then((value) => {
if (value) {
// 设置用户输入的API Key
vscode.workspace.getConfiguration('DevChat').update('OpenAI.apiKey', value, true);
}
});
return false;
}
return true;
}
2023-05-05 21:27:40 +08:00
2023-05-11 10:27:54 +08:00
function checkDependencyPackage() {
2023-05-16 14:24:24 +08:00
const dependencyInstalled = checkDevChatDependency();
2023-05-11 10:27:54 +08:00
if (!dependencyInstalled) {
2023-05-12 10:56:24 +08:00
// Prompt the user, whether to install devchat using pip3 install devchat
const installPrompt = 'devchat is not installed. Do you want to install it using pip3 install devchat?';
const installAction = 'Install';
vscode.window.showInformationMessage(installPrompt, installAction).then((selectedAction) => {
if (selectedAction === installAction) {
// Install devchat using pip3 install devchat
const terminal = vscode.window.createTerminal("DevChat Install");
terminal.sendText("pip3 install --upgrade devchat");
2023-05-12 10:56:24 +08:00
terminal.show();
}
});
2023-05-11 10:27:54 +08:00
}
2023-05-11 10:27:54 +08:00
if (!checkOpenAIKey()) {
return;
}
}
2023-05-11 10:27:54 +08:00
function registerOpenChatPanelCommand(context: vscode.ExtensionContext) {
let disposable = vscode.commands.registerCommand('devchat.openChatPanel',async () => {
await vscode.commands.executeCommand('devchat-view.focus');
2023-05-05 21:27:40 +08:00
});
context.subscriptions.push(disposable);
}
2023-05-16 14:35:37 +08:00
async function ensureChatPanel(context: vscode.ExtensionContext): Promise<boolean> {
await vscode.commands.executeCommand('devchat-view.focus');
2023-05-05 21:27:40 +08:00
return true;
}
function registerAddContextCommand(context: vscode.ExtensionContext) {
const disposableAddContext = vscode.commands.registerCommand('devchat.addConext', async (uri: { path: any; }) => {
2023-05-16 14:35:37 +08:00
if (!await ensureChatPanel(context)) {
2023-05-05 21:27:40 +08:00
return;
}
2023-05-16 14:35:37 +08:00
await sendFileSelectMessage(ExtensionContextHolder.provider?.view()!, uri.path);
2023-05-05 21:27:40 +08:00
});
context.subscriptions.push(disposableAddContext);
const disposableAddContextChinese = vscode.commands.registerCommand('devchat.addConext_chinese', async (uri: { path: any; }) => {
2023-05-16 14:35:37 +08:00
if (!await ensureChatPanel(context)) {
return;
}
2023-05-16 14:35:37 +08:00
await sendFileSelectMessage(ExtensionContextHolder.provider?.view()!, uri.path);
});
context.subscriptions.push(disposableAddContextChinese);
2023-05-05 21:27:40 +08:00
}
function registerAskForCodeCommand(context: vscode.ExtensionContext) {
const disposableCodeContext = vscode.commands.registerCommand('devchat.askForCode', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
2023-05-16 14:35:37 +08:00
if (!await ensureChatPanel(context)) {
2023-05-05 21:27:40 +08:00
return;
}
const selectedText = editor.document.getText(editor.selection);
2023-05-16 14:35:37 +08:00
await sendCodeSelectMessage(ExtensionContextHolder.provider?.view()!, editor.document.fileName, selectedText);
2023-05-05 21:27:40 +08:00
}
});
context.subscriptions.push(disposableCodeContext);
const disposableCodeContextChinese = vscode.commands.registerCommand('devchat.askForCode_chinese', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
2023-05-16 14:35:37 +08:00
if (!await ensureChatPanel(context)) {
return;
}
const selectedText = editor.document.getText(editor.selection);
2023-05-16 14:35:37 +08:00
await sendCodeSelectMessage(ExtensionContextHolder.provider?.view()!, editor.document.fileName, selectedText);
}
});
context.subscriptions.push(disposableCodeContextChinese);
2023-05-05 21:27:40 +08:00
}
function registerAskForFileCommand(context: vscode.ExtensionContext) {
const disposableAskFile = vscode.commands.registerCommand('devchat.askForFile', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
2023-05-16 14:35:37 +08:00
if (!await ensureChatPanel(context)) {
2023-05-05 21:27:40 +08:00
return;
}
2023-05-16 14:35:37 +08:00
await sendFileSelectMessage(ExtensionContextHolder.provider?.view()!, editor.document.fileName);
2023-05-05 21:27:40 +08:00
}
});
context.subscriptions.push(disposableAskFile);
const disposableAskFileChinese = vscode.commands.registerCommand('devchat.askForFile_chinese', async () => {
const editor = vscode.window.activeTextEditor;
if (editor) {
2023-05-16 14:35:37 +08:00
if (!await ensureChatPanel(context)) {
return;
}
2023-05-16 14:35:37 +08:00
await sendFileSelectMessage(ExtensionContextHolder.provider?.view()!, editor.document.fileName);
}
});
context.subscriptions.push(disposableAskFileChinese);
2023-05-05 21:27:40 +08:00
}
export {
2023-05-11 10:27:54 +08:00
checkDependencyPackage,
2023-05-05 21:27:40 +08:00
registerOpenChatPanelCommand,
registerAddContextCommand,
registerAskForCodeCommand,
registerAskForFileCommand,
};