@@ -70,6 +70,8 @@ import { HttpsProxyAgent } from "https-proxy-agent"
7070
7171import { makeCreateMessageMetadata } from "../../../test-utils/api"
7272import { clearAllMocks } from "../../../test-utils/reset"
73+ import { asyncStreamFrom , collectStream } from "../../../test-utils/stream"
74+ import type { ApiStreamChunk } from "../../transform/stream"
7375
7476// Get access to the mocked functions
7577const mockConverseStreamCommand = vi . mocked ( ConverseStreamCommand )
@@ -94,6 +96,68 @@ describe("AwsBedrockHandler", () => {
9496 } )
9597 } )
9698
99+ it . each ( [ "max_tokens" , "end_turn" , "tool_use" , "stop_sequence" ] ) (
100+ "reports truncation only for %s, after preserving usage" ,
101+ async ( stopReason ) => {
102+ handler [ "client" ] . send = vi . fn ( ) . mockResolvedValue ( {
103+ stream : asyncStreamFrom ( [
104+ { contentBlockDelta : { delta : { reasoningContent : { text : "Still thinking" } } } } ,
105+ { messageStop : { stopReason } } ,
106+ { metadata : { usage : { inputTokens : 37 , outputTokens : 16384 } } } ,
107+ ] ) ,
108+ } )
109+ const chunks : ApiStreamChunk [ ] = [ ]
110+ const consume = async ( ) => {
111+ for await ( const chunk of handler . createMessage ( "system" , [ { role : "user" , content : "hello" } ] ) ) {
112+ chunks . push ( chunk )
113+ }
114+ }
115+ if ( stopReason === "max_tokens" ) {
116+ await expect ( consume ( ) ) . rejects . toThrow (
117+ "Output token limit reached. Consider increasing Max Output Tokens" ,
118+ )
119+ } else {
120+ await expect ( consume ( ) ) . resolves . toBeUndefined ( )
121+ }
122+ expect ( chunks ) . toEqual (
123+ expect . arrayContaining ( [
124+ { type : "reasoning" , text : "Still thinking" } ,
125+ expect . objectContaining ( { type : "usage" , inputTokens : 37 , outputTokens : 16384 } ) ,
126+ ] ) ,
127+ )
128+ } ,
129+ )
130+
131+ it . each ( [
132+ [ "anthropic.claude-sonnet-4-5-20250929-v1:0" , 64_000 ] ,
133+ [ "anthropic.claude-sonnet-4-6" , 64_000 ] ,
134+ [ "anthropic.claude-sonnet-5" , 128_000 ] ,
135+ [ "anthropic.claude-opus-4-7" , 128_000 ] ,
136+ [ "anthropic.claude-opus-4-8" , 128_000 ] ,
137+ [ "anthropic.claude-opus-5" , 128_000 ] ,
138+ ] as const ) ( "exposes %s output ceiling without raising request defaults" , async ( apiModelId , ceiling ) => {
139+ for ( const enableReasoningEffort of [ true , false ] ) {
140+ for ( const modelMaxTokens of [ undefined , ceiling ] ) {
141+ const provider = new AwsBedrockHandler ( { apiModelId, enableReasoningEffort, modelMaxTokens } )
142+ expect ( provider . getModel ( ) . info . maxTokens ) . toBe ( ceiling )
143+ provider [ "client" ] . send = vi . fn ( ) . mockResolvedValue ( { stream : asyncStreamFrom ( [ ] ) } )
144+ await collectStream ( provider . createMessage ( "system" , [ { role : "user" , content : "hello" } ] ) )
145+ const configurableWithoutReasoning = apiModelId . endsWith ( "-5" )
146+ const expected =
147+ modelMaxTokens && ( enableReasoningEffort || configurableWithoutReasoning )
148+ ? ceiling
149+ : enableReasoningEffort
150+ ? 16_384
151+ : 8192
152+ expect ( mockConverseStreamCommand ) . toHaveBeenLastCalledWith (
153+ expect . objectContaining ( {
154+ inferenceConfig : expect . objectContaining ( { maxTokens : expected } ) ,
155+ } ) ,
156+ )
157+ }
158+ }
159+ } )
160+
97161 describe ( "getModel" , ( ) => {
98162 it ( "should return the correct model info for a standard model" , ( ) => {
99163 const modelInfo = handler . getModel ( )
0 commit comments