feat: Enhance config management in VSCode plugin
- Refactor to use a single instance of DevChatConfig for efficiency - Update regInMessage and regOutMessage functions for better flexibility - Implement new getters and setters in DevChatConfig to handle complex keys
This commit is contained in:
parent
0a340a8409
commit
40d0fa502b
@ -43,14 +43,15 @@ function getDefaultPythonCommand(): string | undefined {
|
|||||||
|
|
||||||
export function getValidPythonCommand(): string | undefined {
|
export function getValidPythonCommand(): string | undefined {
|
||||||
try {
|
try {
|
||||||
const pythonCommand = new DevChatConfig().get('python_for_chat');
|
const devchatConfig = new DevChatConfig();
|
||||||
|
const pythonCommand = devchatConfig.get('python_for_chat');
|
||||||
if (pythonCommand) {
|
if (pythonCommand) {
|
||||||
return pythonCommand;
|
return pythonCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultPythonCommand = getDefaultPythonCommand();
|
const defaultPythonCommand = getDefaultPythonCommand();
|
||||||
if (defaultPythonCommand) {
|
if (defaultPythonCommand) {
|
||||||
new DevChatConfig().set('python_for_chat', defaultPythonCommand);
|
devchatConfig.set('python_for_chat', defaultPythonCommand);
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultPythonCommand;
|
return defaultPythonCommand;
|
||||||
|
@ -35,8 +35,9 @@ import { DevChatConfig } from './util/config';
|
|||||||
|
|
||||||
|
|
||||||
async function migrateConfig() {
|
async function migrateConfig() {
|
||||||
|
const devchatConfig = new DevChatConfig();
|
||||||
const devchatProvider = "providers.devchat";
|
const devchatProvider = "providers.devchat";
|
||||||
const devchatProviderConfig: any = new DevChatConfig().get(devchatProvider);
|
const devchatProviderConfig: any = devchatConfig.get(devchatProvider);
|
||||||
if (devchatProviderConfig) {
|
if (devchatProviderConfig) {
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
@ -44,19 +45,19 @@ async function migrateConfig() {
|
|||||||
const devchatVScodeProvider: any = vscode.workspace.getConfiguration("devchat").get("Provider.devchat");
|
const devchatVScodeProvider: any = vscode.workspace.getConfiguration("devchat").get("Provider.devchat");
|
||||||
if (devchatVScodeProvider && Object.keys(devchatVScodeProvider).length > 0) {
|
if (devchatVScodeProvider && Object.keys(devchatVScodeProvider).length > 0) {
|
||||||
if (devchatVScodeProvider["access_key"]) {
|
if (devchatVScodeProvider["access_key"]) {
|
||||||
new DevChatConfig().set("providers.devchat.api_key", devchatVScodeProvider["access_key"]);
|
devchatConfig.set("providers.devchat.api_key", devchatVScodeProvider["access_key"]);
|
||||||
}
|
}
|
||||||
if (devchatVScodeProvider["api_base"]) {
|
if (devchatVScodeProvider["api_base"]) {
|
||||||
new DevChatConfig().set("providers.devchat.api_base", devchatVScodeProvider["api_base"]);
|
devchatConfig.set("providers.devchat.api_base", devchatVScodeProvider["api_base"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const openaiVScodeProvider: any = vscode.workspace.getConfiguration("devchat").get("Provider.openai");
|
const openaiVScodeProvider: any = vscode.workspace.getConfiguration("devchat").get("Provider.openai");
|
||||||
if (openaiVScodeProvider && Object.keys(openaiVScodeProvider).length > 0) {
|
if (openaiVScodeProvider && Object.keys(openaiVScodeProvider).length > 0) {
|
||||||
if (openaiVScodeProvider["access_key"]) {
|
if (openaiVScodeProvider["access_key"]) {
|
||||||
new DevChatConfig().set("providers.openai.api_key", openaiVScodeProvider["access_key"]);
|
devchatConfig.set("providers.openai.api_key", openaiVScodeProvider["access_key"]);
|
||||||
}
|
}
|
||||||
if (openaiVScodeProvider["api_base"]) {
|
if (openaiVScodeProvider["api_base"]) {
|
||||||
new DevChatConfig().set("providers.openai.api_base", openaiVScodeProvider["api_base"]);
|
devchatConfig.set("providers.openai.api_base", openaiVScodeProvider["api_base"]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,59 +65,59 @@ async function migrateConfig() {
|
|||||||
const openaiSecretKey = await UiUtilWrapper.secretStorageGet(`Access_KEY_OpenAI`);
|
const openaiSecretKey = await UiUtilWrapper.secretStorageGet(`Access_KEY_OpenAI`);
|
||||||
|
|
||||||
if (devchatSecretKey) {
|
if (devchatSecretKey) {
|
||||||
new DevChatConfig().set("providers.devchat.api_key", devchatSecretKey);
|
devchatConfig.set("providers.devchat.api_key", devchatSecretKey);
|
||||||
}
|
}
|
||||||
if (openaiSecretKey) {
|
if (openaiSecretKey) {
|
||||||
new DevChatConfig().set("providers.openai.api_key", openaiSecretKey);
|
devchatConfig.set("providers.openai.api_key", openaiSecretKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
const enableFunctionCalling = vscode.workspace.getConfiguration("DevChat").get("EnableFunctionCalling");
|
const enableFunctionCalling = vscode.workspace.getConfiguration("DevChat").get("EnableFunctionCalling");
|
||||||
if (enableFunctionCalling) {
|
if (enableFunctionCalling) {
|
||||||
new DevChatConfig().set("enable_function_calling", enableFunctionCalling);
|
devchatConfig.set("enable_function_calling", enableFunctionCalling);
|
||||||
} else {
|
} else {
|
||||||
new DevChatConfig().set("enable_function_calling", false);
|
devchatConfig.set("enable_function_calling", false);
|
||||||
}
|
}
|
||||||
|
|
||||||
const betaInvitationCode = vscode.workspace.getConfiguration("DevChat").get("betaInvitationCode");
|
const betaInvitationCode = vscode.workspace.getConfiguration("DevChat").get("betaInvitationCode");
|
||||||
if (betaInvitationCode) {
|
if (betaInvitationCode) {
|
||||||
new DevChatConfig().set("beta_invitation_code", betaInvitationCode);
|
devchatConfig.set("beta_invitation_code", betaInvitationCode);
|
||||||
} else {
|
} else {
|
||||||
new DevChatConfig().set("beta_invitation_code", "");
|
devchatConfig.set("beta_invitation_code", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
const maxLogCount = vscode.workspace.getConfiguration("DevChat").get("maxLogCount");
|
const maxLogCount = vscode.workspace.getConfiguration("DevChat").get("maxLogCount");
|
||||||
if (maxLogCount) {
|
if (maxLogCount) {
|
||||||
new DevChatConfig().set("max_log_count", maxLogCount);
|
devchatConfig.set("max_log_count", maxLogCount);
|
||||||
} else {
|
} else {
|
||||||
new DevChatConfig().set("max_log_count", 20);
|
devchatConfig.set("max_log_count", 20);
|
||||||
}
|
}
|
||||||
|
|
||||||
const pythonForChat = vscode.workspace.getConfiguration("DevChat").get("PythonForChat");
|
const pythonForChat = vscode.workspace.getConfiguration("DevChat").get("PythonForChat");
|
||||||
if (pythonForChat) {
|
if (pythonForChat) {
|
||||||
new DevChatConfig().set("python_for_chat", pythonForChat);
|
devchatConfig.set("python_for_chat", pythonForChat);
|
||||||
} else {
|
} else {
|
||||||
new DevChatConfig().set("python_for_chat", "");
|
devchatConfig.set("python_for_chat", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
const pythonForCommands = vscode.workspace.getConfiguration("DevChat").get("PythonForCommands");
|
const pythonForCommands = vscode.workspace.getConfiguration("DevChat").get("PythonForCommands");
|
||||||
if (pythonForCommands) {
|
if (pythonForCommands) {
|
||||||
new DevChatConfig().set("python_for_commands", pythonForCommands);
|
devchatConfig.set("python_for_commands", pythonForCommands);
|
||||||
} else {
|
} else {
|
||||||
new DevChatConfig().set("python_for_commands", "");
|
devchatConfig.set("python_for_commands", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
const language = vscode.workspace.getConfiguration("DevChat").get("Language");
|
const language = vscode.workspace.getConfiguration("DevChat").get("Language");
|
||||||
if (language) {
|
if (language) {
|
||||||
new DevChatConfig().set("language", language);
|
devchatConfig.set("language", language);
|
||||||
} else {
|
} else {
|
||||||
new DevChatConfig().set("language", "en");
|
devchatConfig.set("language", "en");
|
||||||
}
|
}
|
||||||
|
|
||||||
const defaultModel = vscode.workspace.getConfiguration("devchat").get("defaultModel");
|
const defaultModel = vscode.workspace.getConfiguration("devchat").get("defaultModel");
|
||||||
if (defaultModel) {
|
if (defaultModel) {
|
||||||
new DevChatConfig().set("default_model", defaultModel);
|
devchatConfig.set("default_model", defaultModel);
|
||||||
} else {
|
} else {
|
||||||
new DevChatConfig().set("default_model", "");
|
devchatConfig.set("default_model", "");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,10 +7,10 @@ import { regInMessage, regOutMessage } from '../util/reg_messages';
|
|||||||
import { MessageHandler } from './messageHandler';
|
import { MessageHandler } from './messageHandler';
|
||||||
import { DevChatConfig } from '../util/config';
|
import { DevChatConfig } from '../util/config';
|
||||||
|
|
||||||
regInMessage({command: 'readConfig', key: ''}); // when key is "", it will get all config values
|
regInMessage({command: 'readConfig', key: ['A','B']}); // when key is "", it will get all config values
|
||||||
regOutMessage({command: 'readConfig', key: '', value: 'any'});
|
regOutMessage({command: 'readConfig', key: ['A', 'B'], value: 'any'});
|
||||||
export async function readConfig(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
|
export async function readConfig(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
|
||||||
if (message.key === '*' || message.key === '') {
|
if (message.key.length === 0 || message.key[1] === '*') {
|
||||||
const config = new DevChatConfig().getAll();
|
const config = new DevChatConfig().getAll();
|
||||||
MessageHandler.sendMessage(panel, {command: 'readConfig', key: message.key, value: config});
|
MessageHandler.sendMessage(panel, {command: 'readConfig', key: message.key, value: config});
|
||||||
} else {
|
} else {
|
||||||
@ -19,9 +19,9 @@ export async function readConfig(message: any, panel: vscode.WebviewPanel|vscode
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
regInMessage({command: 'writeConfig', key: '', value: 'any'}); // when key is "", it will rewrite all config values
|
regInMessage({command: 'writeConfig', key: ['A', 'B'], value: 'any'}); // when key is "", it will rewrite all config values
|
||||||
export async function writeConfig(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
|
export async function writeConfig(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
|
||||||
if (message.key === '*' || message.key === '') {
|
if (message.key.length === 0 || message.key[1] === '*') {
|
||||||
new DevChatConfig().setAll(message.value);
|
new DevChatConfig().setAll(message.value);
|
||||||
} else {
|
} else {
|
||||||
new DevChatConfig().set(message.key, message.value);
|
new DevChatConfig().set(message.key, message.value);
|
||||||
|
@ -7,22 +7,23 @@ import { logger } from './logger';
|
|||||||
|
|
||||||
export class ApiKeyManager {
|
export class ApiKeyManager {
|
||||||
static async llmModel() {
|
static async llmModel() {
|
||||||
const defaultModel = new DevChatConfig().get('default_model');
|
const devchatConfig = new DevChatConfig();
|
||||||
|
const defaultModel = devchatConfig.get('default_model');
|
||||||
if (!defaultModel) {
|
if (!defaultModel) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get model provider
|
// get model provider
|
||||||
const defaultModelProvider = new DevChatConfig().get(`models.${defaultModel}.provider`);
|
const defaultModelProvider = devchatConfig.get(['models', defaultModel, 'provider']);
|
||||||
if (!defaultModelProvider) {
|
if (!defaultModelProvider) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get provider config
|
// get provider config
|
||||||
const defaultProvider = new DevChatConfig().get(`providers.${defaultModelProvider}`);
|
const defaultProvider = devchatConfig.get(['providers', defaultModelProvider]);
|
||||||
const devchatProvider = new DevChatConfig().get(`providers.devchat`);
|
const devchatProvider = devchatConfig.get(`providers.devchat`);
|
||||||
|
|
||||||
let defaultModelConfig = new DevChatConfig().get(`models.${defaultModel}`);
|
let defaultModelConfig = devchatConfig.get(['models', defaultModel]);
|
||||||
defaultModelConfig["model"] = defaultModel;
|
defaultModelConfig["model"] = defaultModel;
|
||||||
if (defaultProvider) {
|
if (defaultProvider) {
|
||||||
for (const key of Object.keys(defaultProvider || {})) {
|
for (const key of Object.keys(defaultProvider || {})) {
|
||||||
|
@ -35,12 +35,26 @@ export class DevChatConfig {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public get(key: string): any {
|
public get(key: string | string[]): any {
|
||||||
return key.split('.').reduce((prev, curr) => prev ? prev[curr] : undefined, this.data);
|
let keys: string[] = [];
|
||||||
|
|
||||||
|
if (typeof key === 'string') {
|
||||||
|
keys = key.split('.');
|
||||||
|
} else {
|
||||||
|
keys = key;
|
||||||
|
}
|
||||||
|
return keys.reduce((prev, curr) => prev ? prev[curr] : undefined, this.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
public set(key: string | string[], value: any): void {
|
||||||
|
let keys: string[] = [];
|
||||||
|
|
||||||
|
if (typeof key === 'string') {
|
||||||
|
keys = key.split('.');
|
||||||
|
} else {
|
||||||
|
keys = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public set(key: string, value: any): void {
|
|
||||||
let keys = key.split('.');
|
|
||||||
let lastKey = keys.pop();
|
let lastKey = keys.pop();
|
||||||
let lastObj = keys.reduce((prev, k) => prev[k] = prev[k] || {}, this.data); // 这创建一个嵌套的对象结构,如果不存在的话
|
let lastObj = keys.reduce((prev, k) => prev[k] = prev[k] || {}, this.data); // 这创建一个嵌套的对象结构,如果不存在的话
|
||||||
if (lastKey) {
|
if (lastKey) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user