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);
|
||||
|
||||
// run command
|
||||
vscode.commands.executeCommand("devchat-topicview.addTopic");
|
||||
// update default model
|
||||
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 { UiUtilVscode } from './util/uiUtil_vscode';
|
||||
import { FT } from './util/feature_flags/feature_toggles';
|
||||
import { ApiKeyManager } from './util/apiKey';
|
||||
|
||||
async function isProviderHasSetted() {
|
||||
try {
|
||||
const providerProperty = "Provider.devchat";
|
||||
const providerConfig = UiUtilWrapper.getConfiguration("devchat", providerProperty);
|
||||
if (providerConfig &&providerConfig["access_key"]) {
|
||||
const providerConfig: any = UiUtilWrapper.getConfiguration("devchat", providerProperty);
|
||||
if (Object.keys(providerConfig).length > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const providerPropertyOpenAI = "Provider.openai";
|
||||
const providerConfigOpenAI = UiUtilWrapper.getConfiguration("devchat", providerPropertyOpenAI);
|
||||
if (providerConfigOpenAI &&providerConfigOpenAI["access_key"]) {
|
||||
const providerConfigOpenAI: any = UiUtilWrapper.getConfiguration("devchat", providerPropertyOpenAI);
|
||||
if (Object.keys(providerConfigOpenAI).length > 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
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() {
|
||||
@ -107,7 +122,7 @@ async function configUpdateTo_0924() {
|
||||
const modelConfig1: any = UiUtilWrapper.getConfiguration("devchat", model);
|
||||
if (Object.keys(modelConfig1).length === 0) {
|
||||
modelConfigNew = {"provider": "devchat"};
|
||||
if (openaiKey && model.startsWith("Model.gpt-")) {
|
||||
if (model.startsWith("Model.gpt-")) {
|
||||
modelConfigNew = {"provider": "openai"};
|
||||
}
|
||||
|
||||
@ -161,12 +176,14 @@ async function configUpdate0912To_0924() {
|
||||
delete modelProperties["api_base"];
|
||||
await vscode.workspace.getConfiguration("devchat").update(model, modelProperties, vscode.ConfigurationTarget.Global);
|
||||
} else {
|
||||
if (!modelProperties["provider"]) {
|
||||
delete modelProperties["api_base"];
|
||||
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 { isIndexingStopped, isNeedIndexingCode } from '../util/askCodeUtil';
|
||||
import { ProgressBar } from '../util/progressBar';
|
||||
import ExtensionContextHolder from '../util/extensionContext';
|
||||
|
||||
|
||||
export function createStatusBarItem(context: vscode.ExtensionContext): vscode.StatusBarItem {
|
||||
@ -54,7 +55,7 @@ export function createStatusBarItem(context: vscode.ExtensionContext): vscode.St
|
||||
progressBar.end();
|
||||
|
||||
// execute command: DevChat.InstallCommands
|
||||
vscode.commands.executeCommand('DevChat.InstallCommands');
|
||||
ExtensionContextHolder.provider?.reloadWebview();
|
||||
clearInterval(timer);
|
||||
} catch (error) {
|
||||
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 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);
|
||||
|
||||
if (stderr) {
|
||||
let newStderr = stderr;
|
||||
if (stderr.indexOf('Failed to verify access key') > 0) {
|
||||
newStderr += `\nPlease check your key data: ${JSON.stringify(keyInfo)}`;
|
||||
}
|
||||
return {
|
||||
"prompt-hash": "",
|
||||
user: "",
|
||||
date: "",
|
||||
response: stderr,
|
||||
response: newStderr,
|
||||
finish_reason: "",
|
||||
isError: true,
|
||||
};
|
||||
|
@ -46,30 +46,17 @@ export class ApiKeyManager {
|
||||
if (!modelConfig["provider"]) {
|
||||
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 providerName = ApiKeyManager.toProviderKey(modelConfig["provider"]);
|
||||
if (!providerName) {
|
||||
return undefined;
|
||||
}
|
||||
let apiKey = await ApiKeyManager.loadApiKeySecret(providerName);
|
||||
if (!apiKey) {
|
||||
apiKey = await ApiKeyManager.loadApiKeySecret("DevChat");
|
||||
if (!apiKey) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
const apiKey = await this.getProviderApiKey(modelConfig["provider"]);
|
||||
if (apiKey) {
|
||||
modelProperties["api_key"] = apiKey;
|
||||
} else {
|
||||
const apiKeyDevChat = await this.getProviderApiKey("devchat");
|
||||
if (apiKeyDevChat) {
|
||||
modelProperties["api_key"] = apiKeyDevChat;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
modelProperties['model'] = modelName;
|
||||
@ -114,34 +101,6 @@ export class ApiKeyManager {
|
||||
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;
|
||||
}
|
||||
|
||||
@ -171,31 +130,22 @@ export class ApiKeyManager {
|
||||
if (!modelConfig["provider"]) {
|
||||
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 providerName = this.toProviderKey(modelConfig["provider"]);
|
||||
if (!providerName) {
|
||||
return undefined;
|
||||
}
|
||||
let apiKey = await this.loadApiKeySecret(providerName);
|
||||
if (!apiKey) {
|
||||
apiKey = await this.loadApiKeySecret("DevChat");
|
||||
if (!apiKey) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
const apiKey = await this.getProviderApiKey(modelConfig["provider"]);
|
||||
const apiBase = await this.getProviderApiBase(modelConfig["provider"]);
|
||||
|
||||
if (apiKey) {
|
||||
modelProperties["api_key"] = apiKey;
|
||||
} else {
|
||||
const apiKeyDevChat = await this.getProviderApiKey("devchat");
|
||||
if (apiKeyDevChat) {
|
||||
modelProperties["api_key"] = apiKeyDevChat;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
if (apiBase) {
|
||||
modelProperties["api_base"] = apiBase;
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -296,4 +203,36 @@ export class ApiKeyManager {
|
||||
static async loadApiKeySecret(llmType: string = "Unknow"): Promise<string | undefined> {
|
||||
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