@@ -9,8 +9,12 @@ vitest.mock("vscode", () => ({
99 } ,
1010} ) )
1111
12- import { Anthropic } from "@anthropic-ai/sdk"
13- import OpenAI from "openai"
12+ import {
13+ Anthropic ,
14+ APIConnectionTimeoutError as AnthropicTimeoutError ,
15+ APIUserAbortError as AnthropicAbortError ,
16+ } from "@anthropic-ai/sdk"
17+ import OpenAI , { APIConnectionTimeoutError , APIUserAbortError } from "openai"
1418
1519import { opencodeGoDefaultModelId , opencodeGoModels , isOpencodeGoAnthropicFormatModel } from "@roo-code/types"
1620
@@ -49,15 +53,22 @@ const mockAnthropicCreate = vitest.fn()
4953 }
5054} )
5155
52- vitest . mock ( "@anthropic-ai/sdk" , ( ) => ( {
53- Anthropic : vitest . fn ( function ( ) {
54- return {
55- messages : {
56- create : mockAnthropicCreate ,
57- } ,
58- }
59- } ) ,
60- } ) )
56+ // The real SDK error classes are re-exported alongside the mocked client so
57+ // tests can emulate the SDK's abort/timeout rejections and the provider's
58+ // instanceof checks resolve against the same class identity.
59+ vitest . mock ( "@anthropic-ai/sdk" , async ( ) => {
60+ const actual = await vi . importActual < typeof import ( "@anthropic-ai/sdk" ) > ( "@anthropic-ai/sdk" )
61+ return {
62+ ...actual ,
63+ Anthropic : vitest . fn ( function ( ) {
64+ return {
65+ messages : {
66+ create : mockAnthropicCreate ,
67+ } ,
68+ }
69+ } ) ,
70+ }
71+ } )
6172
6273describe ( "OpencodeGoHandler" , ( ) => {
6374 const mockOptions : ApiHandlerOptions = {
@@ -709,6 +720,48 @@ describe("OpencodeGoHandler", () => {
709720 expect ( Object . keys ( requestOptions ?? { } ) ) . not . toContain ( "timeout" )
710721 } )
711722
723+ it ( "completePrompt preserves abort identity when the caller aborts (Anthropic path)" , async ( ) => {
724+ // Emulate the Anthropic SDK: an aborted request signal rejects with
725+ // APIUserAbortError ("Request was aborted." — the trailing period would
726+ // fail task-level abort detection, so the provider must normalize it).
727+ mockAnthropicCreate . mockImplementation ( async ( _params : unknown , options : { signal ?: AbortSignal } ) => {
728+ if ( options ?. signal ?. aborted ) {
729+ throw new AnthropicAbortError ( )
730+ }
731+ throw new Error ( "boom" )
732+ } )
733+ const handler = new OpencodeGoHandler ( anthropicOptions )
734+ const controller = new AbortController ( )
735+ controller . abort ( )
736+
737+ const error = await handler . completePrompt ( "ping" , { abortSignal : controller . signal } ) . then (
738+ ( ) => undefined ,
739+ ( e : unknown ) => e ,
740+ )
741+ expect ( error ) . toMatchObject ( { name : "AbortError" } )
742+ expect ( ( error as Error ) . message . endsWith ( "aborted" ) ) . toBe ( true )
743+ expect ( ( error as Error ) . message ) . not . toContain ( "completion error" )
744+ } )
745+
746+ it ( "completePrompt surfaces request timeouts as an AbortError (Anthropic path)" , async ( ) => {
747+ // Emulate the Anthropic SDK: when the request timeout fires, the SDK
748+ // surfaces APIConnectionTimeoutError ("Request timed out.") once retries
749+ // are exhausted — verified against @anthropic-ai/sdk against a hung server.
750+ mockAnthropicCreate . mockImplementation ( async ( _params : unknown , options : { timeout ?: number } ) => {
751+ await new Promise ( ( resolve ) => setTimeout ( resolve , options ?. timeout ?? 50 ) )
752+ throw new AnthropicTimeoutError ( )
753+ } )
754+ const handler = new OpencodeGoHandler ( anthropicOptions )
755+
756+ const error = await handler . completePrompt ( "ping" , { timeoutMs : 50 } ) . then (
757+ ( ) => undefined ,
758+ ( e : unknown ) => e ,
759+ )
760+ expect ( error ) . toMatchObject ( { name : "AbortError" } )
761+ expect ( ( error as Error ) . message . endsWith ( "aborted" ) ) . toBe ( true )
762+ expect ( ( error as Error ) . message ) . not . toContain ( "completion error" )
763+ } )
764+
712765 it ( "completePrompt works without options (backward compatible, Anthropic path)" , async ( ) => {
713766 mockAnthropicCreate . mockResolvedValue ( { content : [ { type : "text" , text : "response" } ] } )
714767 const handler = new OpencodeGoHandler ( anthropicOptions )
@@ -798,6 +851,48 @@ describe("OpencodeGoHandler", () => {
798851 expect ( requestOptions ) . not . toHaveProperty ( "timeout" )
799852 } )
800853
854+ it ( "completePrompt preserves abort identity when the caller aborts (OpenAI path)" , async ( ) => {
855+ // Emulate the OpenAI SDK: an aborted request signal rejects with
856+ // APIUserAbortError ("Request was aborted." — the trailing period would
857+ // fail task-level abort detection, so the provider must normalize it).
858+ mockCreate . mockImplementation ( async ( _params : unknown , options : { signal ?: AbortSignal } ) => {
859+ if ( options ?. signal ?. aborted ) {
860+ throw new APIUserAbortError ( )
861+ }
862+ throw new Error ( "boom" )
863+ } )
864+ const handler = new OpencodeGoHandler ( openaiOptions )
865+ const controller = new AbortController ( )
866+ controller . abort ( )
867+
868+ const error = await handler . completePrompt ( "ping" , { abortSignal : controller . signal } ) . then (
869+ ( ) => undefined ,
870+ ( e : unknown ) => e ,
871+ )
872+ expect ( error ) . toMatchObject ( { name : "AbortError" } )
873+ expect ( ( error as Error ) . message . endsWith ( "aborted" ) ) . toBe ( true )
874+ expect ( ( error as Error ) . message ) . not . toContain ( "completion error" )
875+ } )
876+
877+ it ( "completePrompt surfaces request timeouts as an AbortError (OpenAI path)" , async ( ) => {
878+ // Emulate the OpenAI SDK: when the request timeout fires, the SDK
879+ // surfaces APIConnectionTimeoutError ("Request timed out.") once retries
880+ // are exhausted — verified against openai v5.23.2 against a hung server.
881+ mockCreate . mockImplementation ( async ( _params : unknown , options : { timeout ?: number } ) => {
882+ await new Promise ( ( resolve ) => setTimeout ( resolve , options ?. timeout ?? 50 ) )
883+ throw new APIConnectionTimeoutError ( )
884+ } )
885+ const handler = new OpencodeGoHandler ( openaiOptions )
886+
887+ const error = await handler . completePrompt ( "ping" , { timeoutMs : 50 } ) . then (
888+ ( ) => undefined ,
889+ ( e : unknown ) => e ,
890+ )
891+ expect ( error ) . toMatchObject ( { name : "AbortError" } )
892+ expect ( ( error as Error ) . message . endsWith ( "aborted" ) ) . toBe ( true )
893+ expect ( ( error as Error ) . message ) . not . toContain ( "completion error" )
894+ } )
895+
801896 it ( "completePrompt works without options (backward compatible, OpenAI path)" , async ( ) => {
802897 mockCreate . mockResolvedValueOnce ( {
803898 choices : [ { message : { content : "response" } } ] ,
0 commit comments