@@ -68,6 +68,7 @@ import { NodeHttpHandler } from "@smithy/node-http-handler"
6868import { HttpProxyAgent } from "http-proxy-agent"
6969import { HttpsProxyAgent } from "https-proxy-agent"
7070
71+ import { makeCreateMessageMetadata } from "../../../test-utils/api"
7172import { clearAllMocks } from "../../../test-utils/reset"
7273
7374// Get access to the mocked functions
@@ -1819,6 +1820,372 @@ describe("AwsBedrockHandler", () => {
18191820 expect ( isAdaptiveThinkingModel ( "anthropic.claude-3-5-sonnet-20241022-v2:0" ) ) . toBe ( false )
18201821 expect ( isAdaptiveThinkingModel ( "amazon.nova-lite-v1:0" ) ) . toBe ( false )
18211822 } )
1823+ it ( "should pass abort signal through to client.send" , async ( ) => {
1824+ const mockSend = vi . fn ( )
1825+
1826+ const handler = new AwsBedrockHandler ( {
1827+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1828+ awsAccessKey : "test-access-key" ,
1829+ awsSecretKey : "test-secret-key" ,
1830+ awsRegion : "us-east-1" ,
1831+ } )
1832+
1833+ // Set up the mock on the handler's client instance directly
1834+ const clientInstance = handler [ "client" ]
1835+ expect ( clientInstance ) . toBeDefined ( )
1836+ clientInstance . send = mockSend
1837+
1838+ const controller = new AbortController ( )
1839+ mockSend . mockResolvedValueOnce ( {
1840+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
1841+ } )
1842+
1843+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal } )
1844+
1845+ expect ( mockSend ) . toHaveBeenCalledWith ( expect . any ( Object ) , { abortSignal : controller . signal } )
1846+ } )
1847+
1848+ it ( "should work without options (backward compatible)" , async ( ) => {
1849+ const mockSend = vi . fn ( )
1850+
1851+ const handler = new AwsBedrockHandler ( {
1852+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1853+ awsAccessKey : "test-access-key" ,
1854+ awsSecretKey : "test-secret-key" ,
1855+ awsRegion : "us-east-1" ,
1856+ } )
1857+
1858+ const clientInstance = handler [ "client" ]
1859+ expect ( clientInstance ) . toBeDefined ( )
1860+ clientInstance . send = mockSend
1861+
1862+ mockSend . mockResolvedValueOnce ( {
1863+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
1864+ } )
1865+
1866+ const result = await handler . completePrompt ( "test prompt" )
1867+
1868+ expect ( result ) . toBe ( "response" )
1869+ expect ( mockSend ) . toHaveBeenCalledWith ( expect . any ( Object ) , undefined )
1870+ } )
1871+
1872+ it ( "completePrompt should pass timeoutMs through to client" , async ( ) => {
1873+ const mockSend = vi . fn ( )
1874+
1875+ const handler = new AwsBedrockHandler ( {
1876+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1877+ awsAccessKey : "test-access-key" ,
1878+ awsSecretKey : "test-secret-key" ,
1879+ awsRegion : "us-east-1" ,
1880+ } )
1881+
1882+ const clientInstance = handler [ "client" ]
1883+ clientInstance . send = mockSend
1884+
1885+ mockSend . mockResolvedValueOnce ( {
1886+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
1887+ } )
1888+
1889+ await handler . completePrompt ( "test prompt" , { timeoutMs : 5000 } )
1890+
1891+ expect ( mockSend ) . toHaveBeenCalled ( )
1892+ // Verify the second argument (sendOptions) contains an abortSignal derived from timeoutMs
1893+ const sendOptions = mockSend . mock . calls [ 0 ] [ 1 ]
1894+ expect ( sendOptions ) . toBeDefined ( )
1895+ expect ( sendOptions ?. abortSignal ) . toBeDefined ( )
1896+ } )
1897+
1898+ it ( "completePrompt should merge abortSignal and timeoutMs" , async ( ) => {
1899+ const mockSend = vi . fn ( )
1900+
1901+ const handler = new AwsBedrockHandler ( {
1902+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1903+ awsAccessKey : "test-access-key" ,
1904+ awsSecretKey : "test-secret-key" ,
1905+ awsRegion : "us-east-1" ,
1906+ } )
1907+
1908+ const clientInstance = handler [ "client" ]
1909+ clientInstance . send = mockSend
1910+
1911+ mockSend . mockResolvedValueOnce ( {
1912+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
1913+ } )
1914+
1915+ const controller = new AbortController ( )
1916+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal , timeoutMs : 5000 } )
1917+
1918+ expect ( mockSend ) . toHaveBeenCalled ( )
1919+ const sendOptions = mockSend . mock . calls [ 0 ] [ 1 ]
1920+ expect ( sendOptions ?. abortSignal ) . toBeDefined ( )
1921+ } )
1922+
1923+ it ( "should abort internal controller when external abortSignal is triggered" , async ( ) => {
1924+ const mockResult = {
1925+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
1926+ }
1927+ const mockSend = vi . fn ( ) . mockResolvedValue ( mockResult )
1928+
1929+ const handler = new AwsBedrockHandler ( {
1930+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1931+ awsAccessKey : "test-access-key" ,
1932+ awsSecretKey : "test-secret-key" ,
1933+ awsRegion : "us-east-1" ,
1934+ } )
1935+
1936+ const clientInstance = handler [ "client" ]
1937+ clientInstance . send = mockSend
1938+
1939+ const controller = new AbortController ( )
1940+ let internalSignalCaptured : AbortSignal | undefined
1941+
1942+ // Spy on the send call to capture the abortSignal
1943+ mockSend . mockImplementation ( async ( _command : unknown , options ?: { abortSignal ?: AbortSignal } ) => {
1944+ internalSignalCaptured = options ?. abortSignal
1945+ return mockResult
1946+ } )
1947+
1948+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal } )
1949+
1950+ expect ( internalSignalCaptured ) . toBeDefined ( )
1951+ expect ( internalSignalCaptured ) . toBeInstanceOf ( AbortSignal )
1952+
1953+ // Abort the external signal and verify it propagates to the captured signal
1954+ controller . abort ( )
1955+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
1956+ expect ( internalSignalCaptured ?. aborted ) . toBe ( true )
1957+ } )
1958+
1959+ it ( "should abort immediately when signal is already aborted and timeoutMs > 0" , async ( ) => {
1960+ const mockSend = vi . fn ( )
1961+
1962+ const handler = new AwsBedrockHandler ( {
1963+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1964+ awsAccessKey : "test-access-key" ,
1965+ awsSecretKey : "test-secret-key" ,
1966+ awsRegion : "us-east-1" ,
1967+ } )
1968+
1969+ const clientInstance = handler [ "client" ]
1970+ clientInstance . send = mockSend
1971+
1972+ mockSend . mockResolvedValueOnce ( {
1973+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
1974+ } )
1975+
1976+ const controller = new AbortController ( )
1977+ controller . abort ( ) // Pre-abort the signal
1978+
1979+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal , timeoutMs : 5000 } )
1980+
1981+ expect ( mockSend ) . toHaveBeenCalled ( )
1982+ const sendOptions = mockSend . mock . calls [ 0 ] [ 1 ]
1983+ expect ( sendOptions ?. abortSignal ) . toBeDefined ( )
1984+ expect ( sendOptions ?. abortSignal . aborted ) . toBe ( true )
1985+ } )
1986+
1987+ it ( "should return undefined sendOptions when timeoutMs is 0 and no signal" , async ( ) => {
1988+ const mockSend = vi . fn ( )
1989+
1990+ const handler = new AwsBedrockHandler ( {
1991+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
1992+ awsAccessKey : "test-access-key" ,
1993+ awsSecretKey : "test-secret-key" ,
1994+ awsRegion : "us-east-1" ,
1995+ } )
1996+
1997+ const clientInstance = handler [ "client" ]
1998+ clientInstance . send = mockSend
1999+
2000+ mockSend . mockResolvedValueOnce ( {
2001+ output : { message : { content : [ { type : "text" , text : "response" } ] } , stopReason : null } ,
2002+ } )
2003+
2004+ await handler . completePrompt ( "test prompt" , { timeoutMs : 0 } )
2005+
2006+ expect ( mockSend ) . toHaveBeenCalled ( )
2007+ const sendOptions = mockSend . mock . calls [ 0 ] [ 1 ]
2008+ // When timeoutMs is 0 and no abortSignal, bedrock.ts returns undefined (no signal created)
2009+ expect ( sendOptions ) . toBeUndefined ( )
2010+ } )
2011+
2012+ it ( "should return empty string when response content is empty" , async ( ) => {
2013+ const mockSend = vi . fn ( )
2014+
2015+ const handler = new AwsBedrockHandler ( {
2016+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
2017+ awsAccessKey : "test-access-key" ,
2018+ awsSecretKey : "test-secret-key" ,
2019+ awsRegion : "us-east-1" ,
2020+ } )
2021+
2022+ const clientInstance = handler [ "client" ]
2023+ clientInstance . send = mockSend
2024+
2025+ mockSend . mockResolvedValueOnce ( {
2026+ output : { message : { content : [ { type : "text" , text : "" } ] } , stopReason : null } ,
2027+ } )
2028+
2029+ const result = await handler . completePrompt ( "test prompt" )
2030+
2031+ expect ( result ) . toBe ( "" )
2032+ } )
2033+
2034+ it ( "should return empty string when response text extraction throws after validation" , async ( ) => {
2035+ const mockSend = vi . fn ( )
2036+
2037+ const handler = new AwsBedrockHandler ( {
2038+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
2039+ awsAccessKey : "test-access-key" ,
2040+ awsSecretKey : "test-secret-key" ,
2041+ awsRegion : "us-east-1" ,
2042+ } )
2043+
2044+ const clientInstance = handler [ "client" ]
2045+ clientInstance . send = mockSend
2046+
2047+ let textAccessCount = 0
2048+ const contentBlock = {
2049+ type : "text" ,
2050+ get text ( ) {
2051+ textAccessCount ++
2052+ if ( textAccessCount >= 3 ) {
2053+ throw new Error ( "text getter failed" )
2054+ }
2055+ return "response"
2056+ } ,
2057+ }
2058+
2059+ mockSend . mockResolvedValueOnce ( {
2060+ output : { message : { content : [ contentBlock ] } , stopReason : null } ,
2061+ } )
2062+
2063+ const result = await handler . completePrompt ( "test prompt" )
2064+
2065+ expect ( result ) . toBe ( "" )
2066+ } )
2067+
2068+ it ( "should return empty string when response content array is empty" , async ( ) => {
2069+ const mockSend = vi . fn ( )
2070+
2071+ const handler = new AwsBedrockHandler ( {
2072+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
2073+ awsAccessKey : "test-access-key" ,
2074+ awsSecretKey : "test-secret-key" ,
2075+ awsRegion : "us-east-1" ,
2076+ } )
2077+
2078+ const clientInstance = handler [ "client" ]
2079+ clientInstance . send = mockSend
2080+
2081+ mockSend . mockResolvedValueOnce ( {
2082+ output : { message : { content : [ ] } , stopReason : null } ,
2083+ } )
2084+
2085+ const result = await handler . completePrompt ( "test prompt" )
2086+
2087+ expect ( result ) . toBe ( "" )
2088+ } )
2089+
2090+ it ( "createMessage should reject with an AbortError when the external signal is already aborted" , async ( ) => {
2091+ const handler = new AwsBedrockHandler ( {
2092+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
2093+ awsAccessKey : "test-access-key" ,
2094+ awsSecretKey : "test-secret-key" ,
2095+ awsRegion : "us-east-1" ,
2096+ } )
2097+
2098+ let sendAbortSignal : AbortSignal | undefined
2099+ const mockSend = vi
2100+ . fn ( )
2101+ . mockImplementation ( async ( _command : unknown , options ?: { abortSignal ?: AbortSignal } ) => {
2102+ sendAbortSignal = options ?. abortSignal
2103+ if ( options ?. abortSignal ?. aborted ) {
2104+ const abortError = new Error ( "The operation was aborted" )
2105+ abortError . name = "AbortError"
2106+ throw abortError
2107+ }
2108+ return { stream : [ ] }
2109+ } )
2110+ handler [ "client" ] . send = mockSend
2111+
2112+ const controller = new AbortController ( )
2113+ controller . abort ( )
2114+
2115+ const generator = handler . createMessage (
2116+ "You are a helpful assistant" ,
2117+ [ { role : "user" , content : "Hello" } ] ,
2118+ makeCreateMessageMetadata ( { abortSignal : controller . signal } ) ,
2119+ )
2120+
2121+ let thrown : unknown
2122+ try {
2123+ for await ( const _chunk of generator ) {
2124+ // error chunks are yielded before the rethrow
2125+ }
2126+ } catch ( error ) {
2127+ thrown = error
2128+ }
2129+
2130+ expect ( mockSend ) . toHaveBeenCalledTimes ( 1 )
2131+ // The internal controller must have been aborted by the pre-aborted external signal
2132+ expect ( sendAbortSignal ) . toBeDefined ( )
2133+ expect ( sendAbortSignal ?. aborted ) . toBe ( true )
2134+ expect ( thrown ) . toMatchObject ( { name : "AbortError" } )
2135+ } )
2136+
2137+ it ( "createMessage should abort the in-flight request when the external signal is aborted mid-stream" , async ( ) => {
2138+ const handler = new AwsBedrockHandler ( {
2139+ apiModelId : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
2140+ awsAccessKey : "test-access-key" ,
2141+ awsSecretKey : "test-secret-key" ,
2142+ awsRegion : "us-east-1" ,
2143+ } )
2144+
2145+ let internalSignal : AbortSignal | undefined
2146+ const mockSend = vi
2147+ . fn ( )
2148+ . mockImplementation ( ( _command : unknown , options ?: { abortSignal ?: AbortSignal } ) => {
2149+ internalSignal = options ?. abortSignal
2150+ return new Promise < void > ( ( _resolve , reject ) => {
2151+ internalSignal ?. addEventListener (
2152+ "abort" ,
2153+ ( ) => {
2154+ const abortError = new Error ( "The operation was aborted" )
2155+ abortError . name = "AbortError"
2156+ reject ( abortError )
2157+ } ,
2158+ { once : true } ,
2159+ )
2160+ } )
2161+ } )
2162+ handler [ "client" ] . send = mockSend
2163+
2164+ const controller = new AbortController ( )
2165+
2166+ const generator = handler . createMessage (
2167+ "You are a helpful assistant" ,
2168+ [ { role : "user" , content : "Hello" } ] ,
2169+ makeCreateMessageMetadata ( { abortSignal : controller . signal } ) ,
2170+ )
2171+
2172+ const consumed = ( async ( ) => {
2173+ for await ( const _chunk of generator ) {
2174+ // ignore chunks
2175+ }
2176+ } ) ( )
2177+
2178+ // Wait until the request is in flight and the internal signal is captured
2179+ await vi . waitFor ( ( ) => {
2180+ expect ( internalSignal ) . toBeDefined ( )
2181+ } )
2182+ expect ( internalSignal ?. aborted ) . toBe ( false )
2183+
2184+ // Abort the external signal mid-flight; the stream must reject with an AbortError
2185+ controller . abort ( )
2186+
2187+ await expect ( consumed ) . rejects . toMatchObject ( { name : "AbortError" } )
2188+ } )
18222189 } )
18232190 } )
18242191} )
0 commit comments