@@ -3,7 +3,11 @@ import Anthropic from "@anthropic-ai/sdk";
33import { GenerativeModel , GoogleGenerativeAI } from "@google/generative-ai" ;
44import Groq from "groq-sdk" ;
55import * as vscode from "vscode" ;
6- import { APP_CONFIG , COMMON , generativeAiModels } from "../application/constant" ;
6+ import {
7+ APP_CONFIG ,
8+ COMMON ,
9+ generativeAiModels ,
10+ } from "../application/constant" ;
711import { AnthropicWebViewProvider } from "../webview-providers/anthropic" ;
812import { GeminiWebViewProvider } from "../webview-providers/gemini" ;
913import { GroqWebViewProvider } from "../webview-providers/groq" ;
@@ -38,7 +42,7 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
3842 constructor (
3943 private readonly action : string ,
4044 _context : vscode . ExtensionContext ,
41- errorMessage ?: string
45+ errorMessage ?: string ,
4246 ) {
4347 this . context = _context ;
4448 this . error = errorMessage ;
@@ -69,21 +73,23 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
6973 return getConfigValue ( configKey ) ;
7074 }
7175
72- protected createModel ( ) : { generativeAi : string ; model : any ; modelName : string } | undefined {
76+ protected createModel ( ) :
77+ | { generativeAi : string ; model : any ; modelName : string }
78+ | undefined {
7379 try {
7480 let model ;
7581 let modelName = "" ;
7682 if ( ! this . generativeAi ) {
7783 vscodeErrorMessage (
78- "Configuration not found. Go to settings, search for Your coding buddy. Fill up the model and model name"
84+ "Configuration not found. Go to settings, search for Your coding buddy. Fill up the model and model name" ,
7985 ) ;
8086 }
8187 if ( this . generativeAi === generativeAiModels . GROQ ) {
8288 const apiKey = this . groqApiKey ;
8389 modelName = this . groqModel ;
8490 if ( ! apiKey || ! modelName ) {
8591 vscodeErrorMessage (
86- "Configuration not found. Go to settings, search for Your coding buddy. Fill up the model and model name"
92+ "Configuration not found. Go to settings, search for Your coding buddy. Fill up the model and model name" ,
8793 ) ;
8894 }
8995 model = this . createGroqModel ( apiKey ) ;
@@ -109,7 +115,9 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
109115 return { generativeAi : this . generativeAi , model, modelName } ;
110116 } catch ( error ) {
111117 console . error ( "Error creating model:" , error ) ;
112- vscode . window . showErrorMessage ( "An error occurred while creating the model. Please try again." ) ;
118+ vscode . window . showErrorMessage (
119+ "An error occurred while creating the model. Please try again." ,
120+ ) ;
113121 }
114122 }
115123
@@ -142,7 +150,9 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
142150 return new Groq ( { apiKey } ) ;
143151 }
144152
145- protected async generateModelResponse ( text : string ) : Promise < string | Anthropic . Messages . Message | undefined > {
153+ protected async generateModelResponse (
154+ text : string ,
155+ ) : Promise < string | Anthropic . Messages . Message | undefined > {
146156 try {
147157 const activeModel = this . createModel ( ) ;
148158 if ( ! activeModel ) {
@@ -179,7 +189,7 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
179189
180190 if ( ! response ) {
181191 throw new Error (
182- "Could not generate response. Check your settings, ensure the API keys and Model Name is added properly."
192+ "Could not generate response. Check your settings, ensure the API keys and Model Name is added properly." ,
183193 ) ;
184194 }
185195 if ( this . action . includes ( "chart" ) ) {
@@ -188,7 +198,9 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
188198 return response ;
189199 } catch ( error ) {
190200 this . logger . error ( "Error generating response:" , error ) ;
191- vscode . window . showErrorMessage ( "An error occurred while generating the response. Please try again." ) ;
201+ vscode . window . showErrorMessage (
202+ "An error occurred while generating the response. Please try again." ,
203+ ) ;
192204 }
193205 }
194206
@@ -199,12 +211,19 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
199211 return inputString ;
200212 }
201213
202- async generateGeminiResponse ( model : any , text : string ) : Promise < string | undefined > {
214+ async generateGeminiResponse (
215+ model : any ,
216+ text : string ,
217+ ) : Promise < string | undefined > {
203218 const result = await model . generateContent ( text ) ;
204219 return result ? await result . response . text ( ) : undefined ;
205220 }
206221
207- private async anthropicResponse ( model : Anthropic , generativeAiModel : string , userPrompt : string ) {
222+ private async anthropicResponse (
223+ model : Anthropic ,
224+ generativeAiModel : string ,
225+ userPrompt : string ,
226+ ) {
208227 try {
209228 const response = await model . messages . create ( {
210229 model : generativeAiModel ,
@@ -218,9 +237,15 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
218237 }
219238 }
220239
221- private async groqResponse ( model : Groq , prompt : string , generativeAiModel : string ) : Promise < string | undefined > {
240+ private async groqResponse (
241+ model : Groq ,
242+ prompt : string ,
243+ generativeAiModel : string ,
244+ ) : Promise < string | undefined > {
222245 try {
223- const chatHistory = Memory . has ( COMMON . ANTHROPIC_CHAT_HISTORY ) ? Memory . get ( COMMON . GROQ_CHAT_HISTORY ) : [ ] ;
246+ const chatHistory = Memory . has ( COMMON . ANTHROPIC_CHAT_HISTORY )
247+ ? Memory . get ( COMMON . GROQ_CHAT_HISTORY )
248+ : [ ] ;
224249 const params = {
225250 messages : [
226251 ...chatHistory ,
@@ -232,7 +257,8 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
232257 model : generativeAiModel ,
233258 } ;
234259
235- const completion : Groq . Chat . ChatCompletion = await model . chat . completions . create ( params ) ;
260+ const completion : Groq . Chat . ChatCompletion =
261+ await model . chat . completions . create ( params ) ;
236262 return completion . choices [ 0 ] ?. message ?. content ?? undefined ;
237263 } catch ( error ) {
238264 this . logger . error ( "Error generating response:" , error ) ;
@@ -243,7 +269,9 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
243269
244270 abstract createPrompt ( text ?: string ) : any ;
245271
246- async generateResponse ( message ?: string ) : Promise < string | Anthropic . Messages . Message | undefined > {
272+ async generateResponse (
273+ message ?: string ,
274+ ) : Promise < string | Anthropic . Messages . Message | undefined > {
247275 this . logger . info ( this . action ) ;
248276 let prompt ;
249277 const selectedCode = this . getSelectedWindowArea ( ) ;
@@ -255,7 +283,9 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
255283 if ( message && selectedCode ) {
256284 prompt = await this . createPrompt ( `${ message } \n ${ selectedCode } ` ) ;
257285 } else {
258- message ? ( prompt = await this . createPrompt ( message ) ) : ( prompt = await this . createPrompt ( selectedCode ) ) ;
286+ message
287+ ? ( prompt = await this . createPrompt ( message ) )
288+ : ( prompt = await this . createPrompt ( selectedCode ) ) ;
259289 }
260290
261291 if ( ! prompt ) {
@@ -338,7 +368,9 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
338368 placeHolder : "Enter instructions for CodeBuddy" ,
339369 ignoreFocusOut : true ,
340370 validateInput : ( text ) => {
341- return text === "" ? "Enter instructions for CodeBuddy or press Escape to close chat box" : null ;
371+ return text === ""
372+ ? "Enter instructions for CodeBuddy or press Escape to close chat box"
373+ : null ;
342374 } ,
343375 } ) ;
344376 return userPrompt ;
@@ -350,7 +382,9 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
350382 async execute ( message ?: string ) : Promise < void > {
351383 try {
352384 let prompt : string | undefined ;
353- const response = ( await this . generateResponse ( prompt ? prompt : message ) ) as string ;
385+ const response = ( await this . generateResponse (
386+ prompt ? prompt : message ,
387+ ) ) as string ;
354388 if ( ! response ) {
355389 vscode . window . showErrorMessage ( "model not reponding, try again later" ) ;
356390 return ;
@@ -383,7 +417,10 @@ export abstract class CodeCommandHandler implements ICodeCommandHandler {
383417 break ;
384418 }
385419 } catch ( error ) {
386- this . logger . error ( "Error while passing model response to the webview" , error ) ;
420+ this . logger . error (
421+ "Error while passing model response to the webview" ,
422+ error ,
423+ ) ;
387424 }
388425 }
389426}
0 commit comments