From 170e9140ebf77ba4d79de69f25fe0ef199b703ab Mon Sep 17 00:00:00 2001 From: Olasunkanmi Oyinlola Date: Sun, 15 Dec 2024 13:00:46 +0800 Subject: [PATCH] deprecate chatManager --- src/constant.ts | 2 +- src/events/event-generator.ts | 58 +-- src/extension.ts | 40 +- src/providers/anthropic-web-view-provider.ts | 25 +- src/providers/base-web-view-provider.ts | 14 +- src/services/chat-manager.ts | 503 ++++++++++--------- src/services/generative-ai-model-manager.ts | 16 +- src/utils.ts | 14 +- 8 files changed, 345 insertions(+), 327 deletions(-) diff --git a/src/constant.ts b/src/constant.ts index 82c714c..5ad41ba 100644 --- a/src/constant.ts +++ b/src/constant.ts @@ -42,7 +42,7 @@ export const APP_CONFIG = { grokModel: "grok.model", }; -export enum generativeAiModel { +export enum generativeAiModels { GEMINI = "Gemini", GROQ = "Groq", ANTHROPIC = "Anthropic", diff --git a/src/events/event-generator.ts b/src/events/event-generator.ts index be3e877..34b24cc 100644 --- a/src/events/event-generator.ts +++ b/src/events/event-generator.ts @@ -3,7 +3,7 @@ import Anthropic from "@anthropic-ai/sdk"; import { GenerativeModel, GoogleGenerativeAI } from "@google/generative-ai"; import Groq from "groq-sdk"; import * as vscode from "vscode"; -import { APP_CONFIG, COMMON, generativeAiModel } from "../constant"; +import { APP_CONFIG, COMMON, generativeAiModels } from "../constant"; import { AnthropicWebViewProvider } from "../providers/anthropic-web-view-provider"; import { GeminiWebViewProvider } from "../providers/gemini-web-view-provider"; import { GroqWebViewProvider } from "../providers/groq-web-view-provider"; @@ -38,7 +38,7 @@ export abstract class EventGenerator implements IEventGenerator { constructor( private readonly action: string, _context: vscode.ExtensionContext, - errorMessage?: string, + errorMessage?: string ) { this.context = _context; this.error = errorMessage; @@ -76,33 +76,33 @@ export abstract class EventGenerator implements IEventGenerator { let modelName = ""; if (!this.generativeAi) { vscodeErrorMessage( - "Configuration not found. Go to settings, search for Your coding buddy. Fill up the model and model name", + "Configuration not found. Go to settings, search for Your coding buddy. Fill up the model and model name" ); } - if (this.generativeAi === generativeAiModel.GROQ) { + if (this.generativeAi === generativeAiModels.GROQ) { const apiKey = this.groqApiKey; modelName = this.groqModel; if (!apiKey || !modelName) { vscodeErrorMessage( - "Configuration not found. Go to settings, search for Your coding buddy. Fill up the model and model name", + "Configuration not found. Go to settings, search for Your coding buddy. Fill up the model and model name" ); } model = this.createGroqModel(apiKey); } - if (this.generativeAi === generativeAiModel.GEMINI) { + if (this.generativeAi === generativeAiModels.GEMINI) { const apiKey = this.geminiApiKey; modelName = this.geminiModel; model = this.createGeminiModel(apiKey, modelName); } - if (this.generativeAi === generativeAiModel.ANTHROPIC) { + if (this.generativeAi === generativeAiModels.ANTHROPIC) { const apiKey: string = this.anthropicApiKey; modelName = this.anthropicModel; model = this.createAnthropicModel(apiKey); } - if (this.generativeAi === generativeAiModel.GROK) { + if (this.generativeAi === generativeAiModels.GROK) { const apiKey: string = this.xGrokApiKey; modelName = this.xGrokModel; model = this.createAnthropicModel(apiKey); @@ -111,7 +111,7 @@ export abstract class EventGenerator implements IEventGenerator { } catch (error) { console.error("Error creating model:", error); vscode.window.showErrorMessage( - "An error occurred while creating the model. Please try again.", + "An error occurred while creating the model. Please try again." ); } } @@ -139,7 +139,7 @@ export abstract class EventGenerator implements IEventGenerator { private createAnthropicModel(apiKey: string): Anthropic { let xGrokBaseURL; - if (getConfigValue(APP_CONFIG.generativeAi) === generativeAiModel.GROK) { + if (getConfigValue(APP_CONFIG.generativeAi) === generativeAiModels.GROK) { xGrokBaseURL = getXGroKBaseURL(); } return createAnthropicClient(apiKey, xGrokBaseURL); @@ -150,7 +150,7 @@ export abstract class EventGenerator implements IEventGenerator { } protected async generateModelResponse( - text: string, + text: string ): Promise { try { const activeModel = this.createModel(); @@ -160,7 +160,7 @@ export abstract class EventGenerator implements IEventGenerator { const { generativeAi, model, modelName } = activeModel; // - if (!generativeAi || !generativeAiModel) { + if (!generativeAi || !generativeAiModels) { throw new Error("Model not found. Check your settings."); } let response; @@ -178,7 +178,7 @@ export abstract class EventGenerator implements IEventGenerator { response = await this.groqResponse(model, text, modelName); } break; - case generativeAiModel.GROK: + case generativeAiModels.GROK: if (modelName) { response = await this.anthropicResponse(model, modelName, text); } @@ -189,7 +189,7 @@ export abstract class EventGenerator implements IEventGenerator { if (!response) { throw new Error( - "Could not generate response. Check your settings, ensure the API keys and Model Name is added properly.", + "Could not generate response. Check your settings, ensure the API keys and Model Name is added properly." ); } if (this.action.includes("chart")) { @@ -199,7 +199,7 @@ export abstract class EventGenerator implements IEventGenerator { } catch (error) { console.error("Error generating response:", error); vscode.window.showErrorMessage( - "An error occurred while generating the response. Please try again.", + "An error occurred while generating the response. Please try again." ); } } @@ -213,7 +213,7 @@ export abstract class EventGenerator implements IEventGenerator { async generateGeminiResponse( model: any, - text: string, + text: string ): Promise { const result = await model.generateContent(text); return result ? await result.response.text() : undefined; @@ -222,7 +222,7 @@ export abstract class EventGenerator implements IEventGenerator { private async anthropicResponse( model: Anthropic, generativeAiModel: string, - userPrompt: string, + userPrompt: string ) { try { const response = await model.messages.create({ @@ -235,7 +235,7 @@ export abstract class EventGenerator implements IEventGenerator { } catch (error) { console.error("Error generating response:", error); vscode.window.showErrorMessage( - "An error occurred while generating the response. Please try again.", + "An error occurred while generating the response. Please try again." ); return; } @@ -244,7 +244,7 @@ export abstract class EventGenerator implements IEventGenerator { private async groqResponse( model: Groq, prompt: string, - generativeAiModel: string, + generativeAiModel: string ): Promise { try { const chatHistory = MemoryCache.has(COMMON.ANTHROPIC_CHAT_HISTORY) @@ -267,7 +267,7 @@ export abstract class EventGenerator implements IEventGenerator { } catch (error) { console.error("Error generating response:", error); vscode.window.showErrorMessage( - "An error occurred while generating the response. Please try again.", + "An error occurred while generating the response. Please try again." ); return; } @@ -278,7 +278,7 @@ export abstract class EventGenerator implements IEventGenerator { abstract createPrompt(text?: string): any; async generateResponse( - errorMessage?: string, + errorMessage?: string ): Promise { this.showInformationMessage(); let prompt; @@ -303,7 +303,7 @@ export abstract class EventGenerator implements IEventGenerator { if (prompt && response) { let chatHistory; switch (model) { - case generativeAiModel.GEMINI: + case generativeAiModels.GEMINI: chatHistory = getLatestChatHistory(COMMON.GEMINI_CHAT_HISTORY); MemoryCache.set(COMMON.GEMINI_CHAT_HISTORY, [ ...chatHistory, @@ -317,7 +317,7 @@ export abstract class EventGenerator implements IEventGenerator { }, ]); break; - case generativeAiModel.GROQ: + case generativeAiModels.GROQ: chatHistory = getLatestChatHistory(COMMON.GROQ_CHAT_HISTORY); MemoryCache.set(COMMON.GROQ_CHAT_HISTORY, [ ...chatHistory, @@ -331,7 +331,7 @@ export abstract class EventGenerator implements IEventGenerator { }, ]); break; - case generativeAiModel.ANTHROPIC: + case generativeAiModels.ANTHROPIC: chatHistory = getLatestChatHistory(COMMON.ANTHROPIC_CHAT_HISTORY); MemoryCache.set(COMMON.ANTHROPIC_CHAT_HISTORY, [ ...chatHistory, @@ -345,7 +345,7 @@ export abstract class EventGenerator implements IEventGenerator { }, ]); break; - case generativeAiModel.GROK: + case generativeAiModels.GROK: chatHistory = getLatestChatHistory(COMMON.ANTHROPIC_CHAT_HISTORY); MemoryCache.set(COMMON.ANTHROPIC_CHAT_HISTORY, [ ...chatHistory, @@ -377,28 +377,28 @@ export abstract class EventGenerator implements IEventGenerator { vscode.window.showErrorMessage("model not reponding, try again later"); return; } - if (this.generativeAi === generativeAiModel.GROQ) { + if (this.generativeAi === generativeAiModels.GROQ) { await GroqWebViewProvider.webView?.webview.postMessage({ type: "user-input", message: formattedResponse, }); } - if (this.generativeAi === generativeAiModel.GEMINI) { + if (this.generativeAi === generativeAiModels.GEMINI) { await GeminiWebViewProvider.webView?.webview.postMessage({ type: "user-input", message: formattedResponse, }); } - if (this.generativeAi === generativeAiModel.ANTHROPIC) { + if (this.generativeAi === generativeAiModels.ANTHROPIC) { await AnthropicWebViewProvider.webView?.webview.postMessage({ type: "user-input", message: formattedResponse, }); } - if (this.generativeAi === generativeAiModel.GROK) { + if (this.generativeAi === generativeAiModels.GROK) { await AnthropicWebViewProvider.webView?.webview.postMessage({ type: "user-input", message: formattedResponse, diff --git a/src/extension.ts b/src/extension.ts index 6ccb2a6..48783a3 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -2,7 +2,7 @@ import * as vscode from "vscode"; import { APP_CONFIG, COMMON, - generativeAiModel, + generativeAiModels, OLA_ACTIONS, USER_MESSAGE, } from "./constant"; @@ -56,45 +56,45 @@ export async function activate(context: vscode.ExtensionContext) { } = OLA_ACTIONS; const getComment = new Comments( `${USER_MESSAGE} generates the code comments...`, - context, + context ); const generateOptimizeCode = new OptimizeCode( `${USER_MESSAGE} optimizes the code...`, - context, + context ); const generateRefactoredCode = new RefactorCode( `${USER_MESSAGE} refactors the code...`, - context, + context ); const explainCode = new ExplainCode( `${USER_MESSAGE} explains the code...`, - context, + context ); const generateReview = new ReviewCode( `${USER_MESSAGE} reviews the code...`, - context, + context ); const codeChartGenerator = new CodeChartGenerator( `${USER_MESSAGE} creates the code chart...`, - context, + context ); const codePattern = new FileUploader(context); const knowledgeBase = new ReadFromKnowledgeBase( `${USER_MESSAGE} generate your code pattern...`, - context, + context ); const generateCommitMessage = new GenerateCommitMessage( `${USER_MESSAGE} generates a commit message...`, - context, + context ); const generateInterviewQuestions = new InterviewMe( `${USER_MESSAGE} generates interview questions...`, - context, + context ); const generateUnitTests = new GenerateUnitTest( `${USER_MESSAGE} generates unit tests...`, - context, + context ); const actionMap = { @@ -108,7 +108,7 @@ export async function activate(context: vscode.ExtensionContext) { new FixError( `${USER_MESSAGE} finds a solution to the error...`, context, - errorMessage, + errorMessage ).execute(errorMessage), [explain]: () => explainCode.execute(), [pattern]: () => codePattern.uploadFileHandler(), @@ -118,7 +118,7 @@ export async function activate(context: vscode.ExtensionContext) { }; const subscriptions: vscode.Disposable[] = Object.entries(actionMap).map( - ([action, handler]) => vscode.commands.registerCommand(action, handler), + ([action, handler]) => vscode.commands.registerCommand(action, handler) ); const selectedGenerativeAiModel = getConfigValue("generativeAi.option"); @@ -127,7 +127,7 @@ export async function activate(context: vscode.ExtensionContext) { const quickFixCodeAction: vscode.Disposable = vscode.languages.registerCodeActionsProvider( { scheme: "file", language: "*" }, - quickFix, + quickFix ); const modelConfigurations: { @@ -139,28 +139,28 @@ export async function activate(context: vscode.ExtensionContext) { quickFixCodeAction: vscode.Disposable; }; } = { - [generativeAiModel.GEMINI]: { + [generativeAiModels.GEMINI]: { key: geminiKey, model: geminiModel, webviewProviderClass: GeminiWebViewProvider, subscriptions, quickFixCodeAction, }, - [generativeAiModel.GROQ]: { + [generativeAiModels.GROQ]: { key: groqApiKey, model: groqModel, webviewProviderClass: GroqWebViewProvider, subscriptions, quickFixCodeAction, }, - [generativeAiModel.ANTHROPIC]: { + [generativeAiModels.ANTHROPIC]: { key: anthropicApiKey, model: anthropicModel, webviewProviderClass: AnthropicWebViewProvider, subscriptions, quickFixCodeAction, }, - [generativeAiModel.GROK]: { + [generativeAiModels.GROK]: { key: grokApiKey, model: grokModel, webviewProviderClass: AnthropicWebViewProvider, @@ -177,13 +177,13 @@ export async function activate(context: vscode.ExtensionContext) { key, webviewProviderClass, subscriptions, - quickFixCodeAction, + quickFixCodeAction ); } } catch (error) { MemoryCache.clear(); vscode.window.showErrorMessage( - "An Error occured while setting up generative AI model", + "An Error occured while setting up generative AI model" ); console.log(error); } diff --git a/src/providers/anthropic-web-view-provider.ts b/src/providers/anthropic-web-view-provider.ts index 63d9b54..81e4c5b 100644 --- a/src/providers/anthropic-web-view-provider.ts +++ b/src/providers/anthropic-web-view-provider.ts @@ -1,9 +1,13 @@ import * as vscode from "vscode"; import { BaseWebViewProvider } from "./base-web-view-provider"; -import { COMMON, GROQ_CONFIG } from "../constant"; +import { COMMON, generativeAiModels, GROQ_CONFIG } from "../constant"; import Anthropic from "@anthropic-ai/sdk"; import { MemoryCache } from "../services/memory"; -import { createAnthropicClient } from "../utils"; +import { + createAnthropicClient, + getGenerativeAiModel, + getXGroKBaseURL, +} from "../utils"; type Role = "user" | "assistant"; export interface IHistory { @@ -18,14 +22,14 @@ export class AnthropicWebViewProvider extends BaseWebViewProvider { apiKey: string, generativeAiModel: string, context: vscode.ExtensionContext, - protected readonly baseUrl?: string, + protected baseUrl?: string ) { super(extensionUri, apiKey, generativeAiModel, context); } public async sendResponse( response: string, - currentChat: string, + currentChat: string ): Promise { try { const type = currentChat === "bot" ? "bot-response" : "user-input"; @@ -59,12 +63,19 @@ export class AnthropicWebViewProvider extends BaseWebViewProvider { } } - async generateResponse(message: string): Promise { + async generateResponse( + apiKey = undefined, + name = undefined, + message: string + ): Promise { try { const { max_tokens } = GROQ_CONFIG; + if (getGenerativeAiModel() === generativeAiModels.GROK) { + this.baseUrl = getXGroKBaseURL(); + } const anthropic: Anthropic = createAnthropicClient( this.apiKey, - this.baseUrl, + this.baseUrl ); let chatHistory = MemoryCache.has(COMMON.ANTHROPIC_CHAT_HISTORY) ? MemoryCache.get(COMMON.ANTHROPIC_CHAT_HISTORY) @@ -94,7 +105,7 @@ export class AnthropicWebViewProvider extends BaseWebViewProvider { console.error(error); MemoryCache.set(COMMON.ANTHROPIC_CHAT_HISTORY, []); vscode.window.showErrorMessage( - "Model not responding, please resend your question", + "Model not responding, please resend your question" ); } } diff --git a/src/providers/base-web-view-provider.ts b/src/providers/base-web-view-provider.ts index dc64f24..94dcfd6 100644 --- a/src/providers/base-web-view-provider.ts +++ b/src/providers/base-web-view-provider.ts @@ -14,7 +14,7 @@ export abstract class BaseWebViewProvider { private readonly _extensionUri: vscode.Uri, protected readonly apiKey: string, protected readonly generativeAiModel: string, - context: vscode.ExtensionContext, + context: vscode.ExtensionContext ) { this._context = context; } @@ -32,7 +32,7 @@ export abstract class BaseWebViewProvider { if (!this.apiKey) { vscode.window.showErrorMessage( - "API key not configured. Check your settings.", + "API key not configured. Check your settings." ); return; } @@ -40,7 +40,7 @@ export abstract class BaseWebViewProvider { this.setupMessageHandler( this.apiKey, this.generativeAiModel, - this.currentWebView, + this.currentWebView ); } @@ -53,7 +53,7 @@ export abstract class BaseWebViewProvider { private setupMessageHandler( apiKey: string, modelName: string, - _view: vscode.WebviewView, + _view: vscode.WebviewView ): void { try { _view.webview.onDidReceiveMessage(async (message) => { @@ -61,7 +61,7 @@ export abstract class BaseWebViewProvider { const response = await this.generateResponse( apiKey, modelName, - formatText(message.message), + formatText(message.message) ); if (response) { this.sendResponse(formatText(response), "bot"); @@ -76,11 +76,11 @@ export abstract class BaseWebViewProvider { abstract generateResponse( apiKey?: string, name?: string, - message?: string, + message?: string ): Promise; abstract sendResponse( response: string, - currentChat?: string, + currentChat?: string ): Promise; } diff --git a/src/services/chat-manager.ts b/src/services/chat-manager.ts index 40d9fe8..a0a4786 100644 --- a/src/services/chat-manager.ts +++ b/src/services/chat-manager.ts @@ -1,261 +1,266 @@ -import * as vscode from "vscode"; -import { - aIProviderConfig, - APP_CONFIG, - COMMON, - generativeAiModel, -} from "../constant"; -import { AnthropicWebViewProvider } from "../providers/anthropic-web-view-provider"; -import { GeminiWebViewProvider } from "../providers/gemini-web-view-provider"; -import { GroqWebViewProvider } from "../providers/groq-web-view-provider"; -import { - formatText, - getConfigValue, - getXGroKBaseURL, - resetChatHistory, - vscodeErrorMessage, -} from "../utils"; +// import * as vscode from "vscode"; +// import { +// aIProviderConfig, +// APP_CONFIG, +// COMMON, +// generativeAiModels, +// } from "../constant"; +// import { AnthropicWebViewProvider } from "../providers/anthropic-web-view-provider"; +// import { GeminiWebViewProvider } from "../providers/gemini-web-view-provider"; +// import { GroqWebViewProvider } from "../providers/groq-web-view-provider"; +// import { +// formatText, +// getConfigValue, +// getGenerativeAiModel, +// getXGroKBaseURL, +// resetChatHistory, +// vscodeErrorMessage, +// } from "../utils"; -/** - * Manages chat functionality, including registering chat commands, - * validating configurations, and generating responses. - * This class is responsible for handling chat-related operations, - * such as sending and receiving messages, and interacting with the Groq web view provider. - */ +// /** +// * Manages chat functionality, including registering chat commands, +// * validating configurations, and generating responses. +// * This class is responsible for handling chat-related operations, +// * such as sending and receiving messages, and interacting with the Groq web view provider. +// */ -// The chatManager only caters for send to code buddy event. This should be deprecated. -export class ChatManager { - private readonly _context: vscode.ExtensionContext; - private readonly geminiApiKey: string; - private readonly geminiModel: string; - private readonly grokApiKey: string; - private readonly grokModel: string; - private readonly generativeAi: string; - private readonly anthropicApiKey: string; - private readonly anthropicModel: string; - private readonly groqApiKey: string; - private readonly groqModel: string; +// // The chatManager only caters for send to code buddy event. This should be deprecated. +// export class ChatManager { +// private readonly _context: vscode.ExtensionContext; +// private readonly geminiApiKey: string; +// private readonly geminiModel: string; +// private readonly grokApiKey: string; +// private readonly grokModel: string; +// private readonly generativeAi: string; +// private readonly anthropicApiKey: string; +// private readonly anthropicModel: string; +// private readonly groqApiKey: string; +// private readonly groqModel: string; - constructor(context: vscode.ExtensionContext) { - const { - geminiKey, - geminiModel, - groqApiKey, - groqModel, - generativeAi, - anthropicApiKey, - anthropicModel, - grokApiKey, - grokModel, - } = APP_CONFIG; - this._context = context; - this.geminiApiKey = getConfigValue(geminiKey); - this.geminiModel = getConfigValue(geminiModel); - this.groqApiKey = getConfigValue(groqApiKey); - this.groqModel = getConfigValue(groqModel); - this.anthropicApiKey = getConfigValue(anthropicApiKey); - this.anthropicModel = getConfigValue(anthropicModel); - this.generativeAi = getConfigValue(generativeAi); - this.grokApiKey = getConfigValue(grokApiKey); - this.grokModel = getConfigValue(grokModel); - } +// constructor(context: vscode.ExtensionContext) { +// const { +// geminiKey, +// geminiModel, +// groqApiKey, +// groqModel, +// generativeAi, +// anthropicApiKey, +// anthropicModel, +// grokApiKey, +// grokModel, +// } = APP_CONFIG; +// this._context = context; +// this.geminiApiKey = getConfigValue(geminiKey); +// this.geminiModel = getConfigValue(geminiModel); +// this.groqApiKey = getConfigValue(groqApiKey); +// this.groqModel = getConfigValue(groqModel); +// this.anthropicApiKey = getConfigValue(anthropicApiKey); +// this.anthropicModel = getConfigValue(anthropicModel); +// this.generativeAi = getConfigValue(generativeAi); +// this.grokApiKey = getConfigValue(grokApiKey); +// this.grokModel = getConfigValue(grokModel); +// } - registerChatCommand() { - return vscode.commands.registerCommand("ola.sendChatMessage", async () => { - try { - vscode.window.showInformationMessage("☕️ Asking CodeBuddy for Help"); - const selectedText = this.getActiveEditorText(); - const response = await this.generateResponse(selectedText); - this.sendResponse(selectedText, response); - } catch (error) { - console.error(error); - vscodeErrorMessage( - "Failed to generate content. Please try again later.", - ); - } - }); - } +// registerChatCommand() { +// return vscode.commands.registerCommand("ola.sendChatMessage", async () => { +// try { +// vscode.window.showInformationMessage("☕️ Asking CodeBuddy for Help"); +// const selectedText = this.getActiveEditorText(); +// const response = await this.generateResponse(selectedText); +// this.sendResponse(selectedText, response); +// } catch (error) { +// console.error(error); +// vscodeErrorMessage( +// "Failed to generate content. Please try again later." +// ); +// } +// }); +// } - private getGenerativeAiModel(): string | undefined { - return getConfigValue("generativeAi.option"); - } +// private getActiveEditorText(): string { +// const activeEditor = vscode.window.activeTextEditor; +// if (!activeEditor) { +// vscodeErrorMessage("Select a text in your editor"); +// console.debug("Abandon: no open text editor."); +// throw new Error("No active editor"); +// } +// return activeEditor.document.getText(activeEditor.selection); +// } - private getActiveEditorText(): string { - const activeEditor = vscode.window.activeTextEditor; - if (!activeEditor) { - vscodeErrorMessage("Select a text in your editor"); - console.debug("Abandon: no open text editor."); - throw new Error("No active editor"); - } - return activeEditor.document.getText(activeEditor.selection); - } +// private aiProviderConfig(): Record { +// return { +// [generativeAiModels.ANTHROPIC]: { +// apiKey: this.anthropicApiKey, +// model: this.anthropicModel, +// providerName: generativeAiModels.ANTHROPIC, +// }, +// [generativeAiModels.GEMINI]: { +// apiKey: this.geminiApiKey, +// model: this.geminiModel, +// providerName: generativeAiModels.GEMINI, +// }, +// [generativeAiModels.GROK]: { +// apiKey: this.grokApiKey, +// model: this.grokModel, +// providerName: generativeAiModels.GROK, +// }, +// [generativeAiModels.GROQ]: { +// apiKey: this.groqApiKey, +// model: this.groqModel, +// providerName: generativeAiModels.GROQ, +// }, +// }; +// } - private aiProviderConfig(): Record { - return { - [generativeAiModel.ANTHROPIC]: { - apiKey: this.anthropicApiKey, - model: this.anthropicModel, - providerName: generativeAiModel.ANTHROPIC, - }, - [generativeAiModel.GEMINI]: { - apiKey: this.geminiApiKey, - model: this.geminiModel, - providerName: generativeAiModel.GEMINI, - }, - [generativeAiModel.GROK]: { - apiKey: this.grokApiKey, - model: this.grokModel, - providerName: generativeAiModel.GROK, - }, - [generativeAiModel.GROQ]: { - apiKey: this.groqApiKey, - model: this.groqModel, - providerName: generativeAiModel.GROQ, - }, - }; - } +// private handleAiProvider(generativeAi: generativeAiModels): aIProviderConfig { +// const aiConfig = this.aiProviderConfig(); +// const config = aiConfig[generativeAi]; +// if (!config) { +// throw new Error(`Configuration not found for ${generativeAi}`); +// } +// if (!config.apiKey || !config.model) { +// ("Configuration not found. Go to settings, search for codingBuddy. Fill up the model and model name"); +// } +// return config; +// } - private handleAiProvider(generativeAi: generativeAiModel): aIProviderConfig { - const aiConfig = this.aiProviderConfig(); - const config = aiConfig[generativeAi]; - if (!config) { - throw new Error(`Configuration not found for ${generativeAi}`); - } - if (!config.apiKey || !config.model) { - ("Configuration not found. Go to settings, search for codingBuddy. Fill up the model and model name"); - } - return config; - } +// private async generateResponse(message: string): Promise { +// try { +// const generativeAi: string | undefined = getGenerativeAiModel(); +// if (generativeAi === generativeAiModels.GROQ) { +// const groqAiConfigurations = this.handleAiProvider( +// generativeAiModels.GROQ +// ); +// const chatViewProvider = new GroqWebViewProvider( +// this._context.extensionUri, +// groqAiConfigurations.apiKey, +// groqAiConfigurations.model, +// this._context +// ); +// return await chatViewProvider.generateResponse(message); +// } +// if (generativeAi === generativeAiModels.GEMINI) { +// const geminiConfigurations = this.handleAiProvider( +// generativeAiModels.GEMINI +// ); +// const geminiWebViewProvider = new GeminiWebViewProvider( +// this._context.extensionUri, +// geminiConfigurations.apiKey, +// geminiConfigurations.model, +// this._context +// ); +// return await geminiWebViewProvider.generateResponse( +// geminiConfigurations.apiKey, +// geminiConfigurations.model, +// message +// ); +// } +// if (generativeAi === "Anthropic") { +// const anthropicConfigurations: aIProviderConfig = this.handleAiProvider( +// generativeAiModels.ANTHROPIC +// ); +// const anthropicWebViewProvider = this.getAnthropicWebViewProvider( +// anthropicConfigurations +// ); +// return await anthropicWebViewProvider.generateResponse( +// undefined, +// undefined, +// message +// ); +// } - private async generateResponse(message: string): Promise { - try { - const generativeAi: string | undefined = this.getGenerativeAiModel(); - if (generativeAi === generativeAiModel.GROQ) { - const groqAiConfigurations = this.handleAiProvider( - generativeAiModel.GROQ, - ); - const chatViewProvider = new GroqWebViewProvider( - this._context.extensionUri, - groqAiConfigurations.apiKey, - groqAiConfigurations.model, - this._context, - ); - return await chatViewProvider.generateResponse(message); - } - if (generativeAi === generativeAiModel.GEMINI) { - const geminiConfigurations = this.handleAiProvider( - generativeAiModel.GEMINI, - ); - const geminiWebViewProvider = new GeminiWebViewProvider( - this._context.extensionUri, - geminiConfigurations.apiKey, - geminiConfigurations.model, - this._context, - ); - return await geminiWebViewProvider.generateResponse( - geminiConfigurations.apiKey, - geminiConfigurations.model, - message, - ); - } - if (generativeAi === "Anthropic") { - const anthropicConfigurations: aIProviderConfig = this.handleAiProvider( - generativeAiModel.ANTHROPIC, - ); - const anthropicWebViewProvider = this.getAnthropicWebViewProvider( - anthropicConfigurations, - ); - return await anthropicWebViewProvider.generateResponse(message); - } +// if (generativeAi === generativeAiModels.GROK) { +// const grokConfigurations: aIProviderConfig = this.handleAiProvider( +// generativeAiModels.GROK +// ); +// const anthropicWebViewProvider = +// this.getAnthropicWebViewProvider(grokConfigurations); +// return await anthropicWebViewProvider.generateResponse( +// undefined, +// undefined, +// message +// ); +// } +// } catch (error) { +// const model = getConfigValue("generativeAi.option"); +// if (model) resetChatHistory(model); +// console.log(error); +// } +// } - if (generativeAi === generativeAiModel.GROK) { - const grokConfigurations: aIProviderConfig = this.handleAiProvider( - generativeAiModel.GROK, - ); - const anthropicWebViewProvider = - this.getAnthropicWebViewProvider(grokConfigurations); - return await anthropicWebViewProvider.generateResponse(message); - } - } catch (error) { - const model = getConfigValue("generativeAi.option"); - if (model) resetChatHistory(model); - console.log(error); - } - } +// private sendResponse(userInput: string, response: string | undefined) { +// try { +// if (this.generativeAi === generativeAiModels.GROQ) { +// const chatViewProvider = new GroqWebViewProvider( +// this._context.extensionUri, +// this.groqApiKey, +// this.groqModel, +// this._context +// ); +// chatViewProvider.sendResponse(formatText(userInput), COMMON.USER_INPUT); +// chatViewProvider.sendResponse(formatText(response), COMMON.BOT); +// } +// if (this.generativeAi === generativeAiModels.GEMINI) { +// const geminiWebViewProvider = new GeminiWebViewProvider( +// this._context.extensionUri, +// this.geminiApiKey, +// this.geminiModel, +// this._context +// ); +// geminiWebViewProvider.sendResponse( +// formatText(userInput), +// COMMON.USER_INPUT +// ); +// geminiWebViewProvider.sendResponse(formatText(response), COMMON.BOT); +// } +// if ( +// this.generativeAi === generativeAiModels.ANTHROPIC || +// this.generativeAi === generativeAiModels.GROK +// ) { +// let anthropicConfigurations: aIProviderConfig | undefined; +// if (this.generativeAi === generativeAiModels.ANTHROPIC) { +// anthropicConfigurations = this.handleAiProvider( +// generativeAiModels.ANTHROPIC +// ); +// } +// if (this.generativeAi === generativeAiModels.GROK) { +// anthropicConfigurations = this.handleAiProvider( +// generativeAiModels.GROK +// ); +// } +// if (!anthropicConfigurations) { +// throw new Error(`Configuration not found for ${this.generativeAi}`); +// } - private sendResponse(userInput: string, response: string | undefined) { - try { - if (this.generativeAi === generativeAiModel.GROQ) { - const chatViewProvider = new GroqWebViewProvider( - this._context.extensionUri, - this.groqApiKey, - this.groqModel, - this._context, - ); - chatViewProvider.sendResponse(formatText(userInput), COMMON.USER_INPUT); - chatViewProvider.sendResponse(formatText(response), COMMON.BOT); - } - if (this.generativeAi === generativeAiModel.GEMINI) { - const geminiWebViewProvider = new GeminiWebViewProvider( - this._context.extensionUri, - this.geminiApiKey, - this.geminiModel, - this._context, - ); - geminiWebViewProvider.sendResponse( - formatText(userInput), - COMMON.USER_INPUT, - ); - geminiWebViewProvider.sendResponse(formatText(response), COMMON.BOT); - } - if ( - this.generativeAi === generativeAiModel.ANTHROPIC || - this.generativeAi === generativeAiModel.GROK - ) { - let anthropicConfigurations: aIProviderConfig | undefined; - if (this.generativeAi === generativeAiModel.ANTHROPIC) { - anthropicConfigurations = this.handleAiProvider( - generativeAiModel.ANTHROPIC, - ); - } - if (this.generativeAi === generativeAiModel.GROK) { - anthropicConfigurations = this.handleAiProvider( - generativeAiModel.GROK, - ); - } - if (!anthropicConfigurations) { - throw new Error(`Configuration not found for ${this.generativeAi}`); - } +// const anthropicWebViewProvider = this.getAnthropicWebViewProvider( +// anthropicConfigurations +// ); +// anthropicWebViewProvider.sendResponse( +// formatText(userInput), +// COMMON.USER_INPUT +// ); +// anthropicWebViewProvider.sendResponse(formatText(response), COMMON.BOT); +// } +// } catch (error) { +// const model = getConfigValue("generativeAi.option"); +// if (model) resetChatHistory(model); +// console.error(error); +// } +// } - const anthropicWebViewProvider = this.getAnthropicWebViewProvider( - anthropicConfigurations, - ); - anthropicWebViewProvider.sendResponse( - formatText(userInput), - COMMON.USER_INPUT, - ); - anthropicWebViewProvider.sendResponse(formatText(response), COMMON.BOT); - } - } catch (error) { - const model = getConfigValue("generativeAi.option"); - if (model) resetChatHistory(model); - console.error(error); - } - } - - private getAnthropicWebViewProvider( - config: aIProviderConfig, - ): AnthropicWebViewProvider { - let xGrokBaseURL; - if (getConfigValue(APP_CONFIG.generativeAi) === generativeAiModel.GROK) { - xGrokBaseURL = getXGroKBaseURL(); - } - return new AnthropicWebViewProvider( - this._context.extensionUri, - config.apiKey, - config.model, - this._context, - xGrokBaseURL, - ); - } -} +// private getAnthropicWebViewProvider( +// config: aIProviderConfig +// ): AnthropicWebViewProvider { +// let xGrokBaseURL; +// if (getConfigValue(APP_CONFIG.generativeAi) === generativeAiModels.GROK) { +// xGrokBaseURL = getXGroKBaseURL(); +// } +// return new AnthropicWebViewProvider( +// this._context.extensionUri, +// config.apiKey, +// config.model, +// this._context, +// xGrokBaseURL +// ); +// } +// } diff --git a/src/services/generative-ai-model-manager.ts b/src/services/generative-ai-model-manager.ts index 565d094..150e783 100644 --- a/src/services/generative-ai-model-manager.ts +++ b/src/services/generative-ai-model-manager.ts @@ -1,6 +1,5 @@ import * as vscode from "vscode"; import { getConfigValue } from "../utils"; -import { ChatManager } from "./chat-manager"; export const setUpGenerativeAiModel = ( context: vscode.ExtensionContext, @@ -8,7 +7,7 @@ export const setUpGenerativeAiModel = ( key: string, webViewProviderClass: any, subscriptions: vscode.Disposable[], - quickFixCodeAction: vscode.Disposable, + quickFixCodeAction: vscode.Disposable ) => { try { const apiKey = getConfigValue(key); @@ -17,27 +16,26 @@ export const setUpGenerativeAiModel = ( context.extensionUri, apiKey, apiModel, - context, + context ); const registerWebViewProvider: vscode.Disposable = vscode.window.registerWebviewViewProvider( webViewProviderClass.viewId, - webViewProvider, + webViewProvider ); - const chatManager = new ChatManager(context); - const chatWithCodeBuddy = chatManager.registerChatCommand(); + // const chatManager = new ChatManager(context); + // const chatWithCodeBuddy = chatManager.registerChatCommand(); context.subscriptions.push( ...subscriptions, quickFixCodeAction, - registerWebViewProvider, - chatWithCodeBuddy, + registerWebViewProvider ); } catch (error) { vscode.window.showErrorMessage( - "An Error occured while registering event subscriptions", + "An Error occured while registering event subscriptions" ); console.log(error); } diff --git a/src/utils.ts b/src/utils.ts index 6b5c6f2..f24851b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ import * as markdownit from "markdown-it"; import * as vscode from "vscode"; import { MemoryCache } from "./services/memory"; -import { COMMON, generativeAiModel } from "./constant"; +import { COMMON, generativeAiModels } from "./constant"; import Anthropic from "@anthropic-ai/sdk"; type GetConfigValueType = (key: string) => T | undefined; @@ -15,7 +15,7 @@ export const formatText = (text?: string): string => { }; export const getConfigValue: GetConfigValueType = ( - key: string, + key: string ): T | undefined => { return vscode.workspace.getConfiguration().get(key); }; @@ -34,13 +34,13 @@ export const getLatestChatHistory = (key: string) => { export const resetChatHistory = (model: string) => { switch (model) { - case generativeAiModel.ANTHROPIC: + case generativeAiModels.ANTHROPIC: MemoryCache.set(COMMON.ANTHROPIC_CHAT_HISTORY, []); break; - case generativeAiModel.GEMINI: + case generativeAiModels.GEMINI: MemoryCache.set(COMMON.GEMINI_CHAT_HISTORY, []); break; - case generativeAiModel.GROQ: + case generativeAiModels.GROQ: MemoryCache.set(COMMON.GROQ_CHAT_HISTORY, []); break; default: @@ -63,3 +63,7 @@ export const createAnthropicClient = (apiKey: string, baseURL?: string) => { apiKey, }); }; + +export const getGenerativeAiModel = (): string | undefined => { + return getConfigValue("generativeAi.option"); +};