Improve handling of missing or invalid OPENAI_API_KEY

This commit enhances the handling of missing or invalid OPENAI_API_KEY
by adding validation checks and providing clear error messages to the
user. It also stores the valid API key in the secret storage and
displays a welcome message when the key is set correctly.
This commit is contained in:
bobo.yang 2023-05-17 16:52:52 +08:00
parent 87e044e396
commit ec700c710d
2 changed files with 40 additions and 8 deletions

View File

@ -4,6 +4,7 @@ import { MessageHandler } from './messageHandler';
import messageHistory from '../util/messageHistory';
import { regInMessage, regOutMessage } from '../util/reg_messages';
import { checkOpenAiAPIKey } from '../contributes/commands';
import ExtensionContextHolder from '../util/extensionContext';
let isApiSetted: boolean = false;
@ -42,6 +43,7 @@ I can't find OPENAI_API_KEY in your environment variables or vscode settings. Yo
} as LogEntry;
}
regInMessage({command: 'historyMessages', options: { skip: 0, maxCount: 0 }});
regOutMessage({command: 'loadHistoryMessages', entries: [{hash: '',user: '',date: '',request: '',response: '',context: [{content: '',role: ''}]}]});
export async function historyMessages(message: any, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
@ -80,12 +82,17 @@ export async function historyMessages(message: any, panel: vscode.WebviewPanel|v
messageHistory.add(panel, entryNew);
}
let startMessage = [ welcomeMessage() ];
const isApiKeyReady = await checkOpenAiAPIKey();
isApiSetted = true;
if (!isApiKeyReady) {
startMessage = [ apiKeyMissedMessage() ];
const startMessage = [ apiKeyMissedMessage() ];
isApiSetted = false;
MessageHandler.sendMessage(panel, {
command: 'loadHistoryMessages',
entries: startMessage,
} as LoadHistoryMessages);
return;
}
const loadHistoryMessages: LoadHistoryMessages = {
@ -98,13 +105,30 @@ export async function historyMessages(message: any, panel: vscode.WebviewPanel|v
}
export function isValidApiKey(apiKey: string) {
let apiKeyStrim = apiKey.trim();
if (apiKeyStrim.indexOf('sk-') !== 0) {
return false;
}
return true;
}
export function isWaitForApiKey() {
return !isApiSetted;
}
export async function onApiKey(apiKey: string, panel: vscode.WebviewPanel|vscode.WebviewView): Promise<void> {
if (!isValidApiKey(apiKey)) {
MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: 'It is not a valid OPENAI_API_KEY, you should input the key like this: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx, please set the key again?', hash: '', user: 'system', date: '', isError: false });
return;
}
isApiSetted = true;
const loadHistoryMessages: LoadHistoryMessages = {
command: 'loadHistoryMessages',
entries: [welcomeMessage()],
};
const secretStorage: vscode.SecretStorage = ExtensionContextHolder.context?.secrets!;
secretStorage.store("devchat_OPENAI_API_KEY", apiKey);
const welcomeMessageText = welcomeMessage().response;
MessageHandler.sendMessage(panel, { command: 'receiveMessage', text: `OPENAI_API_KEY is setted, you can use DevChat now.\n${welcomeMessageText}`, hash: '', user: 'system', date: '', isError: false });
}
MessageHandler.sendMessage(panel, loadHistoryMessages);
}

View File

@ -5,6 +5,8 @@ import * as vscode from 'vscode';
import '../command/loadCommands';
import '../context/loadContexts';
import { logger } from '../util/logger';
import { on } from 'events';
import { isWaitForApiKey, onApiKey } from './historyMessages';
export class MessageHandler {
@ -30,6 +32,12 @@ export class MessageHandler {
} catch (e) {
}
}
if (message.command === 'sendMessage') {
if (isWaitForApiKey()) {
onApiKey(message.text, panel);
return;
}
}
const handler = this.handlers[message.command];
if (handler) {