From dadd890af85a670e0f6efc7ec5e90d7c2a030f49 Mon Sep 17 00:00:00 2001 From: Logan Yang Date: Thu, 8 Jun 2023 12:52:51 -0700 Subject: [PATCH] Fix plugin loading when there is no openai key (#59) * Fix onload issue when there is no openai key * Make openai key text field wider --- src/aiState.ts | 7 +++++++ src/langchainStream.ts | 23 ++++++++++++++++------- src/settings.ts | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/aiState.ts b/src/aiState.ts index 08306852..78a6b7a3 100644 --- a/src/aiState.ts +++ b/src/aiState.ts @@ -124,6 +124,13 @@ class AIState { key, model, temperature, maxTokens, } = this.langChainParams; + if (!key) { + new Notice( + 'No OpenAI API key provided. Please set it in Copilot settings, and restart the plugin.' + ); + return; + } + AIState.chatOpenAI = new ChatOpenAI({ openAIApiKey: key, modelName: model, diff --git a/src/langchainStream.ts b/src/langchainStream.ts index 3cd8b5a9..ef3aa1fe 100644 --- a/src/langchainStream.ts +++ b/src/langchainStream.ts @@ -1,5 +1,6 @@ import AIState from '@/aiState'; import { ChatMessage } from '@/sharedState'; +import { Notice } from 'obsidian'; export type Role = 'assistant' | 'user' | 'system'; @@ -12,17 +13,25 @@ export const getAIResponse = async ( updateShouldAbort: (abortController: AbortController | null) => void, debug = false, ) => { + const { + key, + model, + temperature, + maxTokens, + systemMessage, + chatContextTurns, + } = aiState.langChainParams; + if (!key) { + new Notice( + 'No OpenAI API key provided. Please set it in Copilot settings, and restart the plugin.' + ); + return; + } + const abortController = new AbortController(); updateShouldAbort(abortController); if (debug) { - const { - model, - temperature, - maxTokens, - systemMessage, - chatContextTurns, - } = aiState.langChainParams; console.log(`*** DEBUG INFO ***\n` + `user message: ${userMessage.message}\n` + `model: ${model}\n` diff --git a/src/settings.ts b/src/settings.ts index 65a325ff..df074d88 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -54,7 +54,7 @@ export class CopilotSettingTab extends PluginSettingTab { ) .addText((text) => { text.inputEl.type = "password"; - text.inputEl.style.width = "80%"; + text.inputEl.style.width = "100%"; text .setPlaceholder("OpenAI API key") .setValue(this.plugin.settings.openAiApiKey)