2023-05-04 07:27:26 +08:00
|
|
|
import * as vscode from 'vscode';
|
2023-05-04 11:50:45 +08:00
|
|
|
|
2023-05-10 18:30:17 +08:00
|
|
|
|
2023-05-05 21:27:40 +08:00
|
|
|
import {
|
2023-05-16 14:24:24 +08:00
|
|
|
checkOpenAiAPIKey,
|
|
|
|
checkDevChatDependency,
|
2023-05-11 10:27:54 +08:00
|
|
|
checkDependencyPackage,
|
|
|
|
registerOpenChatPanelCommand,
|
|
|
|
registerAddContextCommand,
|
|
|
|
registerAskForCodeCommand,
|
|
|
|
registerAskForFileCommand,
|
2023-05-05 21:27:40 +08:00
|
|
|
} from './contributes/commands';
|
2023-04-27 14:07:46 +08:00
|
|
|
|
2023-05-05 21:27:40 +08:00
|
|
|
import ExtensionContextHolder from './util/extensionContext';
|
2023-05-09 08:52:07 +08:00
|
|
|
import { logger } from './util/logger';
|
2023-05-16 14:35:01 +08:00
|
|
|
import { DevChatViewProvider } from './panel/devchatView';
|
|
|
|
import path from 'path';
|
|
|
|
|
2023-05-04 11:50:45 +08:00
|
|
|
|
|
|
|
|
2023-05-04 07:27:26 +08:00
|
|
|
function activate(context: vscode.ExtensionContext) {
|
2023-05-11 10:27:54 +08:00
|
|
|
ExtensionContextHolder.context = context;
|
|
|
|
logger.init(context);
|
2023-05-04 11:50:45 +08:00
|
|
|
|
2023-05-16 10:40:57 +08:00
|
|
|
const secretStorage: vscode.SecretStorage = context.secrets;
|
|
|
|
vscode.commands.registerCommand('DevChat.OPENAI_API_KEY', async () => {
|
|
|
|
const passwordInput: string = await vscode.window.showInputBox({
|
|
|
|
password: true,
|
|
|
|
title: "OPENAI_API_KEY"
|
|
|
|
}) ?? '';
|
|
|
|
|
|
|
|
secretStorage.store("devchat_OPENAI_API_KEY", passwordInput);
|
|
|
|
});
|
|
|
|
|
|
|
|
const currentLocale = vscode.env.language;
|
|
|
|
if (currentLocale === 'zh-cn' || currentLocale === 'zh-tw') {
|
|
|
|
vscode.commands.executeCommand('setContext', 'isChineseLocale', true);
|
|
|
|
} else {
|
|
|
|
vscode.commands.executeCommand('setContext', 'isChineseLocale', false);
|
|
|
|
}
|
|
|
|
|
2023-05-11 10:27:54 +08:00
|
|
|
registerOpenChatPanelCommand(context);
|
|
|
|
registerAddContextCommand(context);
|
|
|
|
registerAskForCodeCommand(context);
|
|
|
|
registerAskForFileCommand(context);
|
2023-05-16 14:24:24 +08:00
|
|
|
|
|
|
|
const statusBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Right, 100);
|
|
|
|
|
|
|
|
// Set the status bar item properties
|
|
|
|
// const iconPath = context.asAbsolutePath(path.join('assets', 'tank.png'));
|
|
|
|
|
|
|
|
// Set the status bar item properties
|
|
|
|
statusBarItem.text = `$(warning)DevChat`;
|
|
|
|
statusBarItem.tooltip = 'DevChat checking ..., please wait.';
|
|
|
|
statusBarItem.command = '';
|
|
|
|
|
|
|
|
// add a timer to update the status bar item
|
|
|
|
let devchatStatus = '';
|
|
|
|
let apiKeyStatus = '';
|
|
|
|
setInterval(async () => {
|
|
|
|
// status item has three status type
|
|
|
|
// 1. not in a folder
|
|
|
|
// 2. dependence is invalid
|
|
|
|
// 3. ready
|
|
|
|
if (devchatStatus === '' || devchatStatus === 'waitting install devchat') {
|
|
|
|
const bOk = checkDevChatDependency();
|
|
|
|
if (bOk) {
|
|
|
|
devchatStatus = 'ready';
|
|
|
|
} else {
|
|
|
|
if (devchatStatus === '') {
|
|
|
|
devchatStatus = 'not ready';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (devchatStatus === 'not ready') {
|
|
|
|
// auto install devchat
|
|
|
|
const terminal = vscode.window.createTerminal("DevChat Install");
|
|
|
|
terminal.sendText("pip3 install devchat");
|
|
|
|
terminal.show();
|
|
|
|
devchatStatus = 'waitting install devchat';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (devchatStatus !== 'ready') {
|
|
|
|
statusBarItem.text = `$(warning)DevChat`;
|
|
|
|
statusBarItem.tooltip = `${devchatStatus}`;
|
|
|
|
statusBarItem.command = '';
|
|
|
|
// set statusBarItem warning color
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check api key
|
|
|
|
if (apiKeyStatus === '' || apiKeyStatus === 'please set api key') {
|
|
|
|
const bOk = await checkOpenAiAPIKey();
|
|
|
|
if (bOk) {
|
|
|
|
apiKeyStatus = 'ready';
|
|
|
|
} else {
|
|
|
|
apiKeyStatus = 'please set api key';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (apiKeyStatus !== 'ready') {
|
|
|
|
statusBarItem.text = `$(warning)DevChat`;
|
|
|
|
statusBarItem.tooltip = `${apiKeyStatus}`;
|
|
|
|
statusBarItem.command = 'DevChat.OPENAI_API_KEY';
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
statusBarItem.text = `$(pass)DevChat`;
|
|
|
|
statusBarItem.tooltip = `ready to chat`;
|
2023-05-16 17:49:13 +08:00
|
|
|
statusBarItem.command = 'devcaht.onStatusBarClick';
|
2023-05-16 14:24:24 +08:00
|
|
|
}, 3000);
|
|
|
|
|
|
|
|
// Add the status bar item to the status bar
|
|
|
|
statusBarItem.show();
|
|
|
|
context.subscriptions.push(statusBarItem);
|
2023-05-16 14:35:01 +08:00
|
|
|
|
|
|
|
// Register the command
|
|
|
|
context.subscriptions.push(
|
2023-05-16 17:49:13 +08:00
|
|
|
vscode.commands.registerCommand('devcaht.onStatusBarClick',async () => {
|
|
|
|
await vscode.commands.executeCommand('devchat-view.focus');
|
2023-05-16 14:35:01 +08:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2023-05-16 17:49:13 +08:00
|
|
|
ExtensionContextHolder.provider = new DevChatViewProvider(context);
|
2023-05-16 14:35:01 +08:00
|
|
|
context.subscriptions.push(
|
2023-05-16 17:49:13 +08:00
|
|
|
vscode.window.registerWebviewViewProvider('devchat-view', ExtensionContextHolder.provider, {
|
|
|
|
webviewOptions: { retainContextWhenHidden: true }
|
|
|
|
})
|
2023-05-16 14:35:01 +08:00
|
|
|
);
|
2023-04-14 08:05:41 +08:00
|
|
|
}
|
2023-04-21 06:44:26 +08:00
|
|
|
exports.activate = activate;
|