@@ -376,31 +376,15 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
376376 // Ensure clean state before starting a new request
377377 this . ensureCleanState ( )
378378
379- // The VS Code LanguageModelChat API cannot carry an AbortSignal, so a
379+ // The VS Code LanguageModelChat API cannot carry an AbortSignal, so the
380+ // request cancellation is established before client initialization and a
380381 // pre-aborted external signal is reported immediately instead of being
381382 // sent to the host.
382383 const externalAbortSignal = metadata ?. abortSignal
383- if ( externalAbortSignal ?. aborted ) {
384- const abortError = new Error ( "Zoo Code <Language Model API>: Request aborted" )
385- abortError . name = "AbortError"
386- throw abortError
387- }
388-
389- const client : vscode . LanguageModelChat = await this . getClient ( )
390-
391- // Process messages
392- const cleanedMessages = messages . map ( ( msg ) => ( {
393- ...msg ,
394- content : this . cleanMessageContent ( msg . content ) ,
395- } ) )
396-
397- // Convert Anthropic messages to VS Code LM messages
398- const vsCodeLmMessages : vscode . LanguageModelChatMessage [ ] = [
399- vscode . LanguageModelChatMessage . Assistant ( systemPrompt ) ,
400- ...convertToVsCodeLmMessages ( cleanedMessages ) ,
401- ]
402384
403- // Initialize cancellation token for the request
385+ // Initialize cancellation token for the request before getClient() so the
386+ // client-initialization await is covered by the abort bridge and the
387+ // finally cleanup below.
404388 this . currentRequestCancellation = new vscode . CancellationTokenSource ( )
405389 const cancellationTokenSource = this . currentRequestCancellation
406390
@@ -415,23 +399,46 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
415399 externalAbortSignal . addEventListener ( "abort" , onExternalAbort , { once : true } )
416400 }
417401
418- // Calculate input tokens before starting the stream
419- const totalInputTokens : number = await this . calculateTotalInputTokens ( vsCodeLmMessages )
420-
421402 // Accumulate the text and count at the end of the stream to reduce token counting overhead.
422403 let accumulatedText : string = ""
423404
424405 try {
425- // Re-check after client initialization: the external signal may have
426- // aborted while getClient() (or the token calculation) was pending. Bail
427- // before invoking the host so no request starts for a cancelled request.
406+ // Fail fast if the caller already aborted before we even started: do not
407+ // initialize or invoke the host request for a cancelled request.
408+ if ( externalAbortSignal ?. aborted ) {
409+ cancellationTokenSource . cancel ( )
410+ const abortError = new Error ( "Zoo Code <Language Model API>: Request aborted" )
411+ abortError . name = "AbortError"
412+ throw abortError
413+ }
414+
415+ const client : vscode . LanguageModelChat = await this . getClient ( )
416+
417+ // Re-check immediately after client initialization: the signal may have
418+ // aborted while getClient() was pending. Bail before counting input
419+ // tokens or invoking the host so no work happens for a cancelled request.
428420 if ( externalAbortSignal ?. aborted ) {
429421 cancellationTokenSource . cancel ( )
430422 const abortError = new Error ( "Zoo Code <Language Model API>: Request aborted" )
431423 abortError . name = "AbortError"
432424 throw abortError
433425 }
434426
427+ // Process messages
428+ const cleanedMessages = messages . map ( ( msg ) => ( {
429+ ...msg ,
430+ content : this . cleanMessageContent ( msg . content ) ,
431+ } ) )
432+
433+ // Convert Anthropic messages to VS Code LM messages
434+ const vsCodeLmMessages : vscode . LanguageModelChatMessage [ ] = [
435+ vscode . LanguageModelChatMessage . Assistant ( systemPrompt ) ,
436+ ...convertToVsCodeLmMessages ( cleanedMessages ) ,
437+ ]
438+
439+ // Calculate input tokens before starting the stream
440+ const totalInputTokens : number = await this . calculateTotalInputTokens ( vsCodeLmMessages )
441+
435442 // Create the response stream with required options
436443 const requestOptions : vscode . LanguageModelChatRequestOptions = {
437444 justification : `Zoo Code would like to use '${ client . name } ' from '${ client . vendor } ', Click 'Allow' to proceed.` ,
@@ -522,6 +529,16 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
522529 outputTokens : totalOutputTokens ,
523530 }
524531 } catch ( error : unknown ) {
532+ // When the external signal wins during client initialization (the signal
533+ // was already aborted while getClient() was pending or rejected), surface a
534+ // standard abort error instead of wrapping the getClient() failure as a
535+ // generic stream error.
536+ if ( externalAbortSignal ?. aborted ) {
537+ const abortError = new Error ( "Zoo Code <Language Model API>: Request aborted" )
538+ abortError . name = "AbortError"
539+ throw abortError
540+ }
541+
525542 if ( error instanceof vscode . CancellationError ) {
526543 // The host rejected because the request was cancelled: either the
527544 // bridged external signal aborted or the user cancelled the request
@@ -647,24 +664,18 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
647664 }
648665
649666 async completePrompt ( prompt : string , options ?: CompletePromptOptions ) : Promise < string > {
650- // Fail fast if the caller already aborted before we even started: do not
651- // initialize or invoke the host request for a cancelled request.
652- if ( options ?. abortSignal ?. aborted ) {
653- const abortError = new Error ( "VSCode LM completion aborted" )
654- abortError . name = "AbortError"
655- throw abortError
656- }
657-
658- const client = await this . getClient ( )
659-
660667 // The VS Code LanguageModelChat API cannot carry an AbortSignal: sendRequest
661668 // only accepts a CancellationToken. Bridge the external signal and timeout
662- // into a request-local CancellationTokenSource instead.
669+ // into a request-local CancellationTokenSource instead. Cancellation is
670+ // established before client initialization so the getClient() await is covered
671+ // by the timeout, the abort bridge, and the finally cleanup below.
663672 const tokenSource = new vscode . CancellationTokenSource ( )
664673 const externalAbortSignal = options ?. abortSignal
665674
666675 // Apply the timeout only when it is a positive value: cancelling at once for a
667- // zero/negative timeout would abort every such request immediately.
676+ // zero/negative timeout would abort every such request immediately. Starting
677+ // the timer before getClient() means a timeout that fires during a slow client
678+ // lookup still cancels the request before any sendRequest call.
668679 let timeoutId : ReturnType < typeof setTimeout > | undefined
669680 if ( options ?. timeoutMs !== undefined && options . timeoutMs > 0 ) {
670681 timeoutId = setTimeout ( ( ) => tokenSource . cancel ( ) , options . timeoutMs )
@@ -688,9 +699,21 @@ export class VsCodeLmHandler extends BaseProvider implements SingleCompletionHan
688699 tokenSource . token . isCancellationRequested === true || externalAbortSignal ?. aborted === true
689700
690701 try {
691- // Re-check before invoking the host: the signal may have aborted while the
692- // client was being initialized above.
693- if ( externalAbortSignal ?. aborted ) {
702+ // Fail fast if the caller already aborted before we even started: do not
703+ // initialize or invoke the host request for a cancelled request.
704+ if ( isAborted ( ) ) {
705+ const abortError = new Error ( "VSCode LM completion aborted" )
706+ abortError . name = "AbortError"
707+ throw abortError
708+ }
709+
710+ const client = await this . getClient ( )
711+
712+ // Re-check after client initialization: the signal may have aborted (or the
713+ // timeout fired) while getClient() was pending. Bail before invoking the host
714+ // so a timeout that fired during a slow client lookup can never lead to a
715+ // sendRequest call.
716+ if ( isAborted ( ) ) {
694717 const abortError = new Error ( "VSCode LM completion aborted" )
695718 abortError . name = "AbortError"
696719 throw abortError
0 commit comments