@@ -136,6 +136,7 @@ export class PoeHandler extends BaseProvider implements SingleCompletionHandler
136136
137137 async completePrompt ( prompt : string , options ?: import ( "../index" ) . CompletePromptOptions ) : Promise < string > {
138138 const { id } = this . getModel ( )
139+ let timeoutId : ReturnType < typeof setTimeout > | undefined
139140 try {
140141 const generateOptions : Parameters < typeof generateText > [ 0 ] & { abortSignal ?: AbortSignal } = {
141142 model : this . poe ( id ) ,
@@ -145,17 +146,12 @@ export class PoeHandler extends BaseProvider implements SingleCompletionHandler
145146 // Merge abortSignal and timeoutMs into a single abortSignal
146147 if ( options ?. abortSignal && options ?. timeoutMs && options . timeoutMs > 0 ) {
147148 const controller = new AbortController ( )
148- const timeoutId = setTimeout ( ( ) => controller . abort ( ) , options . timeoutMs )
149-
150- options . abortSignal . addEventListener (
151- "abort" ,
152- ( ) => {
153- clearTimeout ( timeoutId )
154- controller . abort ( )
155- } ,
156- { once : true } ,
157- )
158-
149+ if ( options . abortSignal . aborted ) {
150+ controller . abort ( )
151+ } else {
152+ timeoutId = setTimeout ( ( ) => controller . abort ( ) , options . timeoutMs )
153+ options . abortSignal . addEventListener ( "abort" , ( ) => controller . abort ( ) , { once : true } )
154+ }
159155 generateOptions . abortSignal = controller . signal
160156 } else if ( options ?. abortSignal ) {
161157 generateOptions . abortSignal = options . abortSignal
@@ -169,6 +165,10 @@ export class PoeHandler extends BaseProvider implements SingleCompletionHandler
169165 const errorMessage = error instanceof Error ? error . message : String ( error )
170166 TelemetryService . instance . captureException ( new ApiProviderError ( errorMessage , "poe" , id , "completePrompt" ) )
171167 throw new Error ( `Poe completion error: ${ errorMessage } ` )
168+ } finally {
169+ if ( timeoutId ) {
170+ clearTimeout ( timeoutId )
171+ }
172172 }
173173 }
174174}
0 commit comments