@@ -25,8 +25,10 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
2525 private handler ! : AnthropicHandler | PearAIGenericHandler
2626 private pearAiModelsResponse : PearAiModelsResponse | null = null
2727 private options : ApiHandlerOptions
28+ private initializationPromise : Promise < void > | null = null
2829
29- constructor ( options : ApiHandlerOptions ) {
30+ // Private constructor - use the static create method instead
31+ private constructor ( options : ApiHandlerOptions ) {
3032 super ( )
3133 if ( ! options . pearaiApiKey ) {
3234 vscode . window . showErrorMessage ( "PearAI API key not found." , "Login to PearAI" ) . then ( async ( selection ) => {
@@ -45,18 +47,16 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
4547 vscode . commands . executeCommand ( "pearai.checkPearAITokens" , undefined )
4648 }
4749 this . options = options
50+ // Start initialization immediately
51+ this . initializationPromise = this . initializeHandler ( options )
52+ }
4853
49- this . handler = new PearAIGenericHandler ( {
50- ...options ,
51- openAiBaseUrl : PEARAI_URL ,
52- openAiApiKey : options . pearaiApiKey ,
53- openAiModelId : "deepseek/deepseek-chat" ,
54- } )
55-
56- // Then try to initialize the correct handler asynchronously
57- this . initializeHandler ( options ) . catch ( ( error ) => {
58- console . error ( "Failed to initialize PearAI handler:" , error )
59- } )
54+ // Static factory method that properly awaits initialization
55+ public static async create ( options : ApiHandlerOptions ) : Promise < PearAiHandler > {
56+ const instance = new PearAiHandler ( options )
57+ // Wait for initialization to complete
58+ await instance . initializationPromise
59+ return instance
6060 }
6161
6262 private async initializeHandler ( options : ApiHandlerOptions ) : Promise < void > {
@@ -116,7 +116,15 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
116116 }
117117 }
118118
119- getModel ( ) : { id : string ; info : ModelInfo } {
119+ private async ensureInitialized ( ) : Promise < void > {
120+ if ( this . initializationPromise ) {
121+ await this . initializationPromise
122+ }
123+ }
124+
125+ async getModel ( ) : Promise < { id : string ; info : ModelInfo } > {
126+ await this . ensureInitialized ( )
127+
120128 if ( this . options . apiModelId ) {
121129 let modelInfo = null
122130 if ( this . options . apiModelId . startsWith ( "pearai" ) ) {
@@ -142,6 +150,7 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
142150 }
143151
144152 async * createMessage ( systemPrompt : string , messages : any [ ] ) : AsyncGenerator < any > {
153+ await this . ensureInitialized ( )
145154 const generator = this . handler . createMessage ( systemPrompt , messages )
146155 let warningMsg = ""
147156
@@ -168,6 +177,7 @@ export class PearAiHandler extends BaseProvider implements SingleCompletionHandl
168177 }
169178
170179 async completePrompt ( prompt : string ) : Promise < string > {
180+ await this . ensureInitialized ( )
171181 return this . handler . completePrompt ( prompt )
172182 }
173183}
0 commit comments