@@ -66,7 +66,10 @@ import {
6666import { supportsBedrockPromptCaching , type BedrockOptions } from "./bedrock-options.js" ;
6767import { supportsBedrockNativeMaxEffort } from "./thinking-policy.js" ;
6868
69- type Block = ( TextContent | ThinkingContent | ToolCall ) & { index ?: number ; partialJson ?: string } ;
69+ type Block = ( TextContent | ThinkingContent | ToolCall ) & {
70+ index ?: number ;
71+ partialJson ?: string ;
72+ } ;
7073type BedrockEventSink = { push ( event : AssistantMessageEvent ) : void } ;
7174
7275function usesClaudeFable5BedrockContract ( model : Model < "bedrock-converse-stream" > ) : boolean {
@@ -211,7 +214,9 @@ export const streamBedrock: StreamFunction<"bedrock-converse-stream", BedrockOpt
211214 messages : convertMessages ( context , model , cacheRetention ) ,
212215 system : buildSystemPrompt ( context . systemPrompt , model , cacheRetention ) ,
213216 inferenceConfig : {
214- ...( options . maxTokens !== undefined && { maxTokens : options . maxTokens } ) ,
217+ ...( options . maxTokens !== undefined && {
218+ maxTokens : options . maxTokens ,
219+ } ) ,
215220 ...( options . temperature !== undefined &&
216221 ! sendsAdaptiveThinking && { temperature : options . temperature } ) ,
217222 } ,
@@ -221,22 +226,29 @@ export const streamBedrock: StreamFunction<"bedrock-converse-stream", BedrockOpt
221226 ) ,
222227 additionalModelRequestFields,
223228 ...( fable5 ? { additionalModelResponseFieldPaths : [ "/stop_details" ] } : { } ) ,
224- ...( options . requestMetadata !== undefined && { requestMetadata : options . requestMetadata } ) ,
229+ ...( options . requestMetadata !== undefined && {
230+ requestMetadata : options . requestMetadata ,
231+ } ) ,
225232 } ;
226233 const nextCommandInput = await options ?. onPayload ?.( commandInput , model ) ;
227234 if ( nextCommandInput !== undefined ) {
228235 commandInput = nextCommandInput as typeof commandInput ;
229236 }
230237 const command = new ConverseStreamCommand ( commandInput ) ;
231238
232- const response = await client . send ( command , { abortSignal : options . signal } ) ;
239+ const response = await client . send ( command , {
240+ abortSignal : options . signal ,
241+ } ) ;
233242 if ( response . $metadata . httpStatusCode !== undefined ) {
234243 const responseHeaders : Record < string , string > = { } ;
235244 if ( response . $metadata . requestId ) {
236245 responseHeaders [ "x-amzn-requestid" ] = response . $metadata . requestId ;
237246 }
238247 await options ?. onResponse ?.(
239- { status : response . $metadata . httpStatusCode , headers : responseHeaders } ,
248+ {
249+ status : response . $metadata . httpStatusCode ,
250+ headers : responseHeaders ,
251+ } ,
240252 model ,
241253 ) ;
242254 }
@@ -346,12 +358,16 @@ function formatBedrockError(error: unknown): string {
346358 return message ;
347359}
348360
349- /** Stream a Bedrock Converse request from the generic OpenClaw stream options. */
350- export const streamSimpleBedrock : StreamFunction < "bedrock-converse-stream" , SimpleStreamOptions > = (
351- model : Model < "bedrock-converse-stream" > ,
352- context : Context ,
353- options ?: SimpleStreamOptions ,
354- ) => streamBedrock ( model , context , resolveSimpleBedrockOptions ( model , options ) ) ;
361+ /** When the caller did not set an explicit maxTokens cap, use the
362+ * model's resolved maxTokens instead of leaving it at the core default
363+ * 4096. This prevents adaptive-thinking and Fable 5 Bedrock requests
364+ * from being hard-capped at 4096 output tokens (#97176). */
365+ function resolveModelBackedMaxTokens (
366+ baseMaxTokens : number | undefined ,
367+ modelMaxTokens : number | undefined ,
368+ ) : number | undefined {
369+ return baseMaxTokens ?? modelMaxTokens ;
370+ }
355371
356372function resolveSimpleBedrockOptions (
357373 model : Model < "bedrock-converse-stream" > ,
@@ -361,6 +377,7 @@ function resolveSimpleBedrockOptions(
361377 if ( usesClaudeFable5BedrockContract ( model ) ) {
362378 return {
363379 ...base ,
380+ maxTokens : resolveModelBackedMaxTokens ( base . maxTokens , model . maxTokens ) ,
364381 reasoning : options ?. reasoning ?? "high" ,
365382 thinkingBudgets : options ?. thinkingBudgets ,
366383 } satisfies BedrockOptions ;
@@ -380,6 +397,7 @@ function resolveSimpleBedrockOptions(
380397 if ( supportsAdaptiveThinking ( model ) ) {
381398 return {
382399 ...base ,
400+ maxTokens : resolveModelBackedMaxTokens ( base . maxTokens , model . maxTokens ) ,
383401 reasoning : options . reasoning ,
384402 thinkingBudgets : options . thinkingBudgets ,
385403 } satisfies BedrockOptions ;
@@ -412,6 +430,13 @@ function resolveSimpleBedrockOptions(
412430 } satisfies BedrockOptions ;
413431}
414432
433+ /** Stream a Bedrock Converse request from the generic OpenClaw stream options. */
434+ export const streamSimpleBedrock : StreamFunction < "bedrock-converse-stream" , SimpleStreamOptions > = (
435+ model : Model < "bedrock-converse-stream" > ,
436+ context : Context ,
437+ options ?: SimpleStreamOptions ,
438+ ) => streamBedrock ( model , context , resolveSimpleBedrockOptions ( model , options ) ) ;
439+
415440function handleContentBlockStart (
416441 event : ContentBlockStartEvent ,
417442 blocks : Block [ ] ,
@@ -431,7 +456,11 @@ function handleContentBlockStart(
431456 index,
432457 } ;
433458 output . content . push ( block ) ;
434- stream . push ( { type : "toolcall_start" , contentIndex : blocks . length - 1 , partial : output } ) ;
459+ stream . push ( {
460+ type : "toolcall_start" ,
461+ contentIndex : blocks . length - 1 ,
462+ partial : output ,
463+ } ) ;
435464 }
436465}
437466
@@ -449,15 +478,24 @@ function handleContentBlockDelta(
449478 if ( delta ?. text !== undefined ) {
450479 // If no text block exists yet, create one, as `handleContentBlockStart` is not sent for text blocks
451480 if ( ! block ) {
452- const newBlock : Block = { type : "text" , text : "" , index : contentBlockIndex } ;
481+ const newBlock : Block = {
482+ type : "text" ,
483+ text : "" ,
484+ index : contentBlockIndex ,
485+ } ;
453486 output . content . push ( newBlock ) ;
454487 index = blocks . length - 1 ;
455488 block = blocks [ index ] ;
456489 stream . push ( { type : "text_start" , contentIndex : index , partial : output } ) ;
457490 }
458491 if ( block . type === "text" ) {
459492 block . text += delta . text ;
460- stream . push ( { type : "text_delta" , contentIndex : index , delta : delta . text , partial : output } ) ;
493+ stream . push ( {
494+ type : "text_delta" ,
495+ contentIndex : index ,
496+ delta : delta . text ,
497+ partial : output ,
498+ } ) ;
461499 }
462500 } else if ( delta ?. toolUse && block ?. type === "toolCall" ) {
463501 block . partialJson = ( block . partialJson || "" ) + ( delta . toolUse . input || "" ) ;
@@ -482,7 +520,11 @@ function handleContentBlockDelta(
482520 output . content . push ( newBlock ) ;
483521 thinkingIndex = blocks . length - 1 ;
484522 thinkingBlock = blocks [ thinkingIndex ] ;
485- stream . push ( { type : "thinking_start" , contentIndex : thinkingIndex , partial : output } ) ;
523+ stream . push ( {
524+ type : "thinking_start" ,
525+ contentIndex : thinkingIndex ,
526+ partial : output ,
527+ } ) ;
486528 }
487529
488530 if ( thinkingBlock ?. type === "thinking" ) {
@@ -533,7 +575,12 @@ function handleContentBlockStop(
533575
534576 switch ( block . type ) {
535577 case "text" :
536- stream . push ( { type : "text_end" , contentIndex : index , content : block . text , partial : output } ) ;
578+ stream . push ( {
579+ type : "text_end" ,
580+ contentIndex : index ,
581+ content : block . text ,
582+ partial : output ,
583+ } ) ;
537584 break ;
538585 case "thinking" :
539586 stream . push ( {
@@ -548,7 +595,12 @@ function handleContentBlockStop(
548595 // Finalize in-place and strip the scratch buffer so replay only
549596 // carries parsed arguments.
550597 delete ( block as Block ) . partialJson ;
551- stream . push ( { type : "toolcall_end" , contentIndex : index , toolCall : block , partial : output } ) ;
598+ stream . push ( {
599+ type : "toolcall_end" ,
600+ contentIndex : index ,
601+ toolCall : block ,
602+ partial : output ,
603+ } ) ;
552604 break ;
553605 }
554606}
@@ -784,7 +836,11 @@ function convertMessages(
784836 break ;
785837 case "toolCall" :
786838 contentBlocks . push ( {
787- toolUse : { toolUseId : c . id , name : c . name , input : c . arguments as DocumentType } ,
839+ toolUse : {
840+ toolUseId : c . id ,
841+ name : c . name ,
842+ input : c . arguments as DocumentType ,
843+ } ,
788844 } ) ;
789845 break ;
790846 case "thinking" : {
@@ -1035,8 +1091,13 @@ function buildAdditionalModelRequestFields(
10351091 : ( options . thinkingDisplay ?? "summarized" ) ;
10361092 const result : Record < string , unknown > = supportsAdaptiveThinking ( model )
10371093 ? {
1038- thinking : { type : "adaptive" , ...( display !== undefined ? { display } : { } ) } ,
1039- output_config : { effort : mapThinkingLevelToEffort ( model , options . reasoning ) } ,
1094+ thinking : {
1095+ type : "adaptive" ,
1096+ ...( display !== undefined ? { display } : { } ) ,
1097+ } ,
1098+ output_config : {
1099+ effort : mapThinkingLevelToEffort ( model , options . reasoning ) ,
1100+ } ,
10401101 }
10411102 : ( ( ) => {
10421103 const defaultBudgets : Record < ThinkingLevel , number > = {
0 commit comments