11
11
import software .amazon .awssdk .services .bedrockruntime .model .ContentBlockDeltaEvent ;
12
12
import software .amazon .awssdk .services .bedrockruntime .model .ContentBlockStart ;
13
13
import software .amazon .awssdk .services .bedrockruntime .model .ContentBlockStartEvent ;
14
- import software .amazon .awssdk .services .bedrockruntime .model .ContentBlockStopEvent ;
15
14
import software .amazon .awssdk .services .bedrockruntime .model .ConverseStreamMetadataEvent ;
16
15
import software .amazon .awssdk .services .bedrockruntime .model .ConverseStreamOutput ;
17
16
import software .amazon .awssdk .services .bedrockruntime .model .ConverseStreamResponseHandler ;
@@ -118,20 +117,20 @@ public void onNext(ConverseStreamOutput item) {
118
117
);
119
118
return ;
120
119
}
120
+ case ConverseStreamOutput .EventType .METADATA -> {
121
+ demand .set (0 ); // reset demand before we fork to another thread
122
+ item .accept (ConverseStreamResponseHandler .Visitor .builder ().onMetadata (event -> handleMetadata (event , chunks )).build ());
123
+ return ;
124
+ }
121
125
case ConverseStreamOutput .EventType .CONTENT_BLOCK_STOP -> {
122
126
demand .set (0 ); // reset demand before we fork to another thread
123
127
item .accept (
124
128
ConverseStreamResponseHandler .Visitor .builder ()
125
- .onContentBlockStop (event -> handleContentBlockStop ( event , chunks ))
129
+ .onContentBlockStop (event -> Stream . empty ( ))
126
130
.build ()
127
131
);
128
132
return ;
129
133
}
130
- case ConverseStreamOutput .EventType .METADATA -> {
131
- demand .set (0 ); // reset demand before we fork to another thread
132
- item .accept (ConverseStreamResponseHandler .Visitor .builder ().onMetadata (event -> handleMetadata (event , chunks )).build ());
133
- return ;
134
- }
135
134
default -> {
136
135
logger .debug ("Unknown event type [{}] for line [{}]." , eventType , item );
137
136
}
@@ -206,22 +205,6 @@ private void handleContentBlockDelta(
206
205
});
207
206
}
208
207
209
- private void handleContentBlockStop (
210
- ContentBlockStopEvent event ,
211
- ArrayDeque <StreamingUnifiedChatCompletionResults .ChatCompletionChunk > chunks
212
- ) {
213
- runOnUtilityThreadPool (() -> {
214
- try {
215
- var contentBlockStop = handleContentBlockStop (event );
216
- contentBlockStop .forEach (chunks ::offer );
217
- } catch (Exception e ) {
218
- logger .warn ("Failed to parse content block stop event from Amazon Bedrock provider: {}" , event );
219
- }
220
- var results = new StreamingUnifiedChatCompletionResults .Results (chunks );
221
- downstream .onNext (results );
222
- });
223
- }
224
-
225
208
private void handleMetadata (
226
209
ConverseStreamMetadataEvent event ,
227
210
ArrayDeque <StreamingUnifiedChatCompletionResults .ChatCompletionChunk > chunks
@@ -422,19 +405,21 @@ private static StreamingUnifiedChatCompletionResults.ChatCompletionChunk.Choice.
422
405
* @return a stream of ChatCompletionChunk
423
406
*/
424
407
public static Stream <StreamingUnifiedChatCompletionResults .ChatCompletionChunk > handleContentBlockStart (ContentBlockStartEvent event ) {
425
- StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta delta = null ;
426
408
var index = event .contentBlockIndex ();
427
409
var type = event .start ().type ();
428
410
429
411
switch (type ) {
430
412
case ContentBlockStart .Type .TOOL_USE -> {
431
413
var toolCall = handleToolUseStart (event .start ());
432
414
var role = "assistant" ;
433
- delta = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta (null , null , role , List .of (toolCall ));
415
+ var delta = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta (null , null , role , List .of (toolCall ));
416
+ var choice = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice (delta , null , index );
417
+ var chunk = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk (null , List .of (choice ), null , null , null );
418
+ return Stream .of (chunk );
434
419
}
435
420
default -> logger .debug ("unhandled content block start type [{}]." , type );
436
421
}
437
- delta = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta (null , null , null , null );
422
+ var delta = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta (null , null , null , null );
438
423
var choice = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice (delta , null , index );
439
424
var chunk = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk (null , List .of (choice ), null , null , null );
440
425
return Stream .of (chunk );
@@ -449,15 +434,15 @@ public static Stream<StreamingUnifiedChatCompletionResults.ChatCompletionChunk>
449
434
public static Stream <StreamingUnifiedChatCompletionResults .ChatCompletionChunk > handleContentBlockDelta (ContentBlockDeltaEvent event ) {
450
435
StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta delta = null ;
451
436
var type = event .delta ().type ();
437
+ var content = event .delta ().text ();
452
438
453
439
switch (type ) {
454
440
case ContentBlockDelta .Type .TEXT -> {
455
- var content = event .delta ().text ();
456
441
delta = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta (content , null , null , null );
457
442
}
458
443
case ContentBlockDelta .Type .TOOL_USE -> {
459
444
var toolCall = handleToolUseDelta (event .delta ());
460
- delta = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta (null , null , null , List .of (toolCall ));
445
+ delta = new StreamingUnifiedChatCompletionResults .ChatCompletionChunk .Choice .Delta (content , null , null , List .of (toolCall ));
461
446
}
462
447
default -> logger .debug ("unknown content block delta type [{}]." , type );
463
448
}
@@ -466,16 +451,6 @@ public static Stream<StreamingUnifiedChatCompletionResults.ChatCompletionChunk>
466
451
return Stream .of (chunk );
467
452
}
468
453
469
- /**
470
- * processes incremental content updates
471
- * Parse a ContentBlockStopEvent into a ChatCompletionChunk stream
472
- * @param event the event data
473
- * @return a stream of ChatCompletionChunk
474
- */
475
- public static Stream <StreamingUnifiedChatCompletionResults .ChatCompletionChunk > handleContentBlockStop (ContentBlockStopEvent event ) {
476
- return Stream .empty ();
477
- }
478
-
479
454
/**
480
455
* processes usage statistics
481
456
* Parse a ConverseStreamMetadataEvent into a ChatCompletionChunk stream
0 commit comments