fix invalid key error
This commit is contained in:
parent
b408f72e38
commit
60906f9509
@ -101,8 +101,18 @@ function regAccessKeyCommand(context: vscode.ExtensionContext, provider: string)
|
|||||||
}
|
}
|
||||||
await ApiKeyManager.writeApiKeySecret(passwordInput, provider);
|
await ApiKeyManager.writeApiKeySecret(passwordInput, provider);
|
||||||
|
|
||||||
// run command
|
// update default model
|
||||||
vscode.commands.executeCommand("devchat-topicview.addTopic");
|
const accessKey = await ApiKeyManager.getApiKey();
|
||||||
|
if (!accessKey) {
|
||||||
|
const modelList = await ApiKeyManager.getValidModels();
|
||||||
|
if (modelList && modelList.length > 0) {
|
||||||
|
// update default llm model
|
||||||
|
await UiUtilWrapper.updateConfiguration('devchat', 'defaultModel', modelList[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// reload webview
|
||||||
|
ExtensionContextHolder.provider?.reloadWebview();
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -32,21 +32,36 @@ import { createStatusBarItem } from './panel/statusBarView';
|
|||||||
import { UiUtilWrapper } from './util/uiUtil';
|
import { UiUtilWrapper } from './util/uiUtil';
|
||||||
import { UiUtilVscode } from './util/uiUtil_vscode';
|
import { UiUtilVscode } from './util/uiUtil_vscode';
|
||||||
import { FT } from './util/feature_flags/feature_toggles';
|
import { FT } from './util/feature_flags/feature_toggles';
|
||||||
|
import { ApiKeyManager } from './util/apiKey';
|
||||||
|
|
||||||
async function isProviderHasSetted() {
|
async function isProviderHasSetted() {
|
||||||
const providerProperty = "Provider.devchat";
|
try {
|
||||||
const providerConfig = UiUtilWrapper.getConfiguration("devchat", providerProperty);
|
const providerProperty = "Provider.devchat";
|
||||||
if (providerConfig &&providerConfig["access_key"]) {
|
const providerConfig: any = UiUtilWrapper.getConfiguration("devchat", providerProperty);
|
||||||
return true;
|
if (Object.keys(providerConfig).length > 0) {
|
||||||
}
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const providerPropertyOpenAI = "Provider.openai";
|
const providerPropertyOpenAI = "Provider.openai";
|
||||||
const providerConfigOpenAI = UiUtilWrapper.getConfiguration("devchat", providerPropertyOpenAI);
|
const providerConfigOpenAI: any = UiUtilWrapper.getConfiguration("devchat", providerPropertyOpenAI);
|
||||||
if (providerConfigOpenAI &&providerConfigOpenAI["access_key"]) {
|
if (Object.keys(providerConfigOpenAI).length > 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
const apiOpenaiKey = await ApiKeyManager.getProviderApiKey("openai");
|
||||||
|
if (apiOpenaiKey) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
const devchatKey = await ApiKeyManager.getProviderApiKey("devchat");
|
||||||
|
if (devchatKey) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
} catch (error) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function configUpdateTo_0924() {
|
async function configUpdateTo_0924() {
|
||||||
@ -107,7 +122,7 @@ async function configUpdateTo_0924() {
|
|||||||
const modelConfig1: any = UiUtilWrapper.getConfiguration("devchat", model);
|
const modelConfig1: any = UiUtilWrapper.getConfiguration("devchat", model);
|
||||||
if (Object.keys(modelConfig1).length === 0) {
|
if (Object.keys(modelConfig1).length === 0) {
|
||||||
modelConfigNew = {"provider": "devchat"};
|
modelConfigNew = {"provider": "devchat"};
|
||||||
if (openaiKey && model.startsWith("Model.gpt-")) {
|
if (model.startsWith("Model.gpt-")) {
|
||||||
modelConfigNew = {"provider": "openai"};
|
modelConfigNew = {"provider": "openai"};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,9 +176,11 @@ async function configUpdate0912To_0924() {
|
|||||||
delete modelProperties["api_base"];
|
delete modelProperties["api_base"];
|
||||||
await vscode.workspace.getConfiguration("devchat").update(model, modelProperties, vscode.ConfigurationTarget.Global);
|
await vscode.workspace.getConfiguration("devchat").update(model, modelProperties, vscode.ConfigurationTarget.Global);
|
||||||
} else {
|
} else {
|
||||||
delete modelProperties["api_base"];
|
if (!modelProperties["provider"]) {
|
||||||
modelProperties["provider"] = "devchat";
|
delete modelProperties["api_base"];
|
||||||
await vscode.workspace.getConfiguration("devchat").update(model, modelProperties, vscode.ConfigurationTarget.Global);
|
modelProperties["provider"] = "devchat";
|
||||||
|
await vscode.workspace.getConfiguration("devchat").update(model, modelProperties, vscode.ConfigurationTarget.Global);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ import * as vscode from 'vscode';
|
|||||||
import { dependencyCheck } from './statusBarViewBase';
|
import { dependencyCheck } from './statusBarViewBase';
|
||||||
import { isIndexingStopped, isNeedIndexingCode } from '../util/askCodeUtil';
|
import { isIndexingStopped, isNeedIndexingCode } from '../util/askCodeUtil';
|
||||||
import { ProgressBar } from '../util/progressBar';
|
import { ProgressBar } from '../util/progressBar';
|
||||||
|
import ExtensionContextHolder from '../util/extensionContext';
|
||||||
|
|
||||||
|
|
||||||
export function createStatusBarItem(context: vscode.ExtensionContext): vscode.StatusBarItem {
|
export function createStatusBarItem(context: vscode.ExtensionContext): vscode.StatusBarItem {
|
||||||
@ -54,7 +55,7 @@ export function createStatusBarItem(context: vscode.ExtensionContext): vscode.St
|
|||||||
progressBar.end();
|
progressBar.end();
|
||||||
|
|
||||||
// execute command: DevChat.InstallCommands
|
// execute command: DevChat.InstallCommands
|
||||||
vscode.commands.executeCommand('DevChat.InstallCommands');
|
ExtensionContextHolder.provider?.reloadWebview();
|
||||||
clearInterval(timer);
|
clearInterval(timer);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
statusBarItem.text = `$(warning)DevChat`;
|
statusBarItem.text = `$(warning)DevChat`;
|
||||||
|
@ -272,16 +272,29 @@ class DevChat {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// activeLlmModelKey is an api key, I will output it to the log
|
||||||
|
// so, replace mid-sub string with *
|
||||||
|
// for example: sk-1234567890 -> sk-1*****7890, keep first 4 char and last 4 char visible
|
||||||
|
const newActiveLlmModelKey = activeLlmModelKey.replace(/^(.{4})(.*)(.{4})$/, (_, first, middle, last) => first + middle.replace(/./g, '*') + last);
|
||||||
|
const keyInfo = {
|
||||||
|
OPENAI_API_KEY: newActiveLlmModelKey ,
|
||||||
|
...openAiApiBaseObject
|
||||||
|
};
|
||||||
|
|
||||||
logger.channel()?.info(`Running devchat with arguments: ${args.join(" ")}`);
|
logger.channel()?.info(`Running devchat with arguments: ${args.join(" ")}`);
|
||||||
logger.channel()?.info(`Running devchat with environment: ${JSON.stringify(openAiApiBaseObject)}`);
|
logger.channel()?.info(`Running devchat with environment: ${JSON.stringify(keyInfo)}`);
|
||||||
const { exitCode: code, stdout, stderr } = await this.commandRun.spawnAsync(devChat, args, spawnAsyncOptions, onStdoutPartial, undefined, undefined, undefined);
|
const { exitCode: code, stdout, stderr } = await this.commandRun.spawnAsync(devChat, args, spawnAsyncOptions, onStdoutPartial, undefined, undefined, undefined);
|
||||||
|
|
||||||
if (stderr) {
|
if (stderr) {
|
||||||
|
let newStderr = stderr;
|
||||||
|
if (stderr.indexOf('Failed to verify access key') > 0) {
|
||||||
|
newStderr += `\nPlease check your key data: ${JSON.stringify(keyInfo)}`;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
"prompt-hash": "",
|
"prompt-hash": "",
|
||||||
user: "",
|
user: "",
|
||||||
date: "",
|
date: "",
|
||||||
response: stderr,
|
response: newStderr,
|
||||||
finish_reason: "",
|
finish_reason: "",
|
||||||
isError: true,
|
isError: true,
|
||||||
};
|
};
|
||||||
|
@ -46,30 +46,17 @@ export class ApiKeyManager {
|
|||||||
if (!modelConfig["provider"]) {
|
if (!modelConfig["provider"]) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const providerProperty = "Provider." + modelConfig["provider"];
|
|
||||||
const providerConfig = UiUtilWrapper.getConfiguration("devchat", providerProperty);
|
const apiKey = await this.getProviderApiKey(modelConfig["provider"]);
|
||||||
if (providerConfig) {
|
if (apiKey) {
|
||||||
if (providerConfig["access_key"]) {
|
modelProperties["api_key"] = apiKey;
|
||||||
modelProperties["api_key"] = providerConfig["access_key"];
|
} else {
|
||||||
}
|
const apiKeyDevChat = await this.getProviderApiKey("devchat");
|
||||||
if (providerConfig["api_base"]) {
|
if (apiKeyDevChat) {
|
||||||
modelProperties["api_base"] = providerConfig["api_base"];
|
modelProperties["api_key"] = apiKeyDevChat;
|
||||||
}
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
if (!modelProperties["api_key"]) {
|
|
||||||
const providerName = ApiKeyManager.toProviderKey(modelConfig["provider"]);
|
|
||||||
if (!providerName) {
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
let apiKey = await ApiKeyManager.loadApiKeySecret(providerName);
|
|
||||||
if (!apiKey) {
|
|
||||||
apiKey = await ApiKeyManager.loadApiKeySecret("DevChat");
|
|
||||||
if (!apiKey) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
modelProperties["api_key"] = apiKey;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
modelProperties['model'] = modelName;
|
modelProperties['model'] = modelName;
|
||||||
@ -113,34 +100,6 @@ export class ApiKeyManager {
|
|||||||
if (llama70BModel) {
|
if (llama70BModel) {
|
||||||
modelList.push(llama70BModel.model);
|
modelList.push(llama70BModel.model);
|
||||||
}
|
}
|
||||||
|
|
||||||
const customModelConfig: any = UiUtilWrapper.getConfiguration('devchat', 'customModel');
|
|
||||||
if (!customModelConfig) {
|
|
||||||
return modelList;
|
|
||||||
}
|
|
||||||
|
|
||||||
const customModels = customModelConfig as Array<any>;
|
|
||||||
for (const model of customModels) {
|
|
||||||
if (!model.model) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const modelProvider = model["model"].split('/')[0];
|
|
||||||
const modelName = model["model"].split('/').slice(1).join('/');
|
|
||||||
|
|
||||||
if (!model["api_key"]) {
|
|
||||||
const providerName = ApiKeyManager.toProviderKey(modelProvider);
|
|
||||||
if (!providerName) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const apiKey = await ApiKeyManager.loadApiKeySecret(providerName);
|
|
||||||
if (!apiKey) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
modelList.push(model["model"]);
|
|
||||||
}
|
|
||||||
|
|
||||||
return modelList;
|
return modelList;
|
||||||
}
|
}
|
||||||
@ -171,31 +130,22 @@ export class ApiKeyManager {
|
|||||||
if (!modelConfig["provider"]) {
|
if (!modelConfig["provider"]) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
const providerProperty = "Provider." + modelConfig["provider"];
|
|
||||||
const providerConfig = UiUtilWrapper.getConfiguration("devchat", providerProperty);
|
|
||||||
if (providerConfig) {
|
|
||||||
if (providerConfig["access_key"]) {
|
|
||||||
modelProperties["api_key"] = providerConfig["access_key"];
|
|
||||||
}
|
|
||||||
if (providerConfig["api_base"]) {
|
|
||||||
modelProperties["api_base"] = providerConfig["api_base"];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!modelProperties["api_key"]) {
|
const apiKey = await this.getProviderApiKey(modelConfig["provider"]);
|
||||||
const providerName = this.toProviderKey(modelConfig["provider"]);
|
const apiBase = await this.getProviderApiBase(modelConfig["provider"]);
|
||||||
if (!providerName) {
|
|
||||||
|
if (apiKey) {
|
||||||
|
modelProperties["api_key"] = apiKey;
|
||||||
|
} else {
|
||||||
|
const apiKeyDevChat = await this.getProviderApiKey("devchat");
|
||||||
|
if (apiKeyDevChat) {
|
||||||
|
modelProperties["api_key"] = apiKeyDevChat;
|
||||||
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
let apiKey = await this.loadApiKeySecret(providerName);
|
}
|
||||||
if (!apiKey) {
|
if (apiBase) {
|
||||||
apiKey = await this.loadApiKeySecret("DevChat");
|
modelProperties["api_base"] = apiBase;
|
||||||
if (!apiKey) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
modelProperties["api_key"] = apiKey;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!modelProperties["api_base"] && modelProperties["api_key"]?.startsWith("DC.")) {
|
if (!modelProperties["api_base"] && modelProperties["api_key"]?.startsWith("DC.")) {
|
||||||
@ -234,49 +184,6 @@ export class ApiKeyManager {
|
|||||||
return await modelProperties('Model.llama-2-70b-chat', "llama-2-70b-chat");
|
return await modelProperties('Model.llama-2-70b-chat', "llama-2-70b-chat");
|
||||||
}
|
}
|
||||||
|
|
||||||
const customModelConfig: any = UiUtilWrapper.getConfiguration('devchat', 'customModel');
|
|
||||||
if (!customModelConfig) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
const customModels = customModelConfig as Array<any>;
|
|
||||||
for (const model of customModels) {
|
|
||||||
if (!model.model) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (model.model === llmModelT) {
|
|
||||||
let modelProperties: any = {};
|
|
||||||
for (const key of Object.keys(model || {})) {
|
|
||||||
const property = model![key];
|
|
||||||
modelProperties[key] = property;
|
|
||||||
}
|
|
||||||
|
|
||||||
const modelProvider = model["model"].split('/')[0];
|
|
||||||
const modelName = model["model"].split('/').slice(1).join('/');
|
|
||||||
|
|
||||||
if (!model["api_key"]) {
|
|
||||||
const providerName = this.toProviderKey(modelProvider);
|
|
||||||
if (!providerName) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
const apiKey = await this.loadApiKeySecret(providerName);
|
|
||||||
if (!apiKey) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
modelProperties["api_key"] = apiKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!model["api_base"] && modelProperties["api_key"]?.startsWith("DC.")) {
|
|
||||||
modelProperties["api_base"] = "https://api.devchat.ai/v1";
|
|
||||||
}
|
|
||||||
|
|
||||||
modelProperties["provider"] = modelProvider;
|
|
||||||
modelProperties["model"] = modelName;
|
|
||||||
|
|
||||||
return modelProperties;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -296,4 +203,36 @@ export class ApiKeyManager {
|
|||||||
static async loadApiKeySecret(llmType: string = "Unknow"): Promise<string | undefined> {
|
static async loadApiKeySecret(llmType: string = "Unknow"): Promise<string | undefined> {
|
||||||
return await UiUtilWrapper.secretStorageGet(`Access_KEY_${llmType}`);
|
return await UiUtilWrapper.secretStorageGet(`Access_KEY_${llmType}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get some provider's api key
|
||||||
|
static async getProviderApiKey(provider: string): Promise<string | undefined> {
|
||||||
|
// read key from configration first
|
||||||
|
const providerProperty = `Provider.${provider}`;
|
||||||
|
const providerConfig = UiUtilWrapper.getConfiguration("devchat", providerProperty);
|
||||||
|
if (providerConfig) {
|
||||||
|
if (providerConfig["access_key"]) {
|
||||||
|
return providerConfig["access_key"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const providerName = this.toProviderKey(provider);
|
||||||
|
if (!providerName) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return await this.loadApiKeySecret(providerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
// get some provider's api base
|
||||||
|
static async getProviderApiBase(provider: string): Promise<string | undefined> {
|
||||||
|
// read key from configration first
|
||||||
|
const providerProperty = `Provider.${provider}`;
|
||||||
|
const providerConfig = UiUtilWrapper.getConfiguration("devchat", providerProperty);
|
||||||
|
if (providerConfig) {
|
||||||
|
if (providerConfig["api_base"]) {
|
||||||
|
return providerConfig["api_base"];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user