mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(bedrock-runtime): stream ConverseStream incrementally (#3043)
ConverseStream previously buffered the whole proxied response, translated it in one shot, and returned it as a single byte[]. Both backends now write frames through a JAX-RS StreamingOutput as they're known, and the proxy backend reads the upstream SSE response via HttpResponse.BodyHandlers.ofLines() instead of ofString() - text deltas reach SDK clients as the model generates them instead of landing all at once when the response finishes. Once the StreamingOutput starts writing, JAX-RS has already committed the 200 response, so a failure discovered only after streaming begins - a stream that never reaches a finish_reason or "[DONE]" (upstream connection dropped mid-generation) - can no longer become an HTTP error status. It's now reported as a modelStreamErrorException event, the same wire shape (:message-type: exception, :exception-type: <name>) and modeled exception real Bedrock uses for a mid-stream failure, verified to decode correctly through BedrockRuntimeAsyncClient's generated ConverseStreamResponseHandler. A stream that completes normally (finish_reason or "[DONE]" seen) with no content is now treated as a legitimate empty completion rather than an error, matching real Bedrock. Added a compatibility-tests/sdk-test-java case exercising BedrockRuntimeAsyncClient.converseStream end to end, proving the emitted event-stream frames decode correctly through the real AWS SDK rather than only Floci's own byte-level assertions.
This commit is contained in:
1 parent
724be0a6a7
commit
0471d6f488
13 files changed
+475
-257
No files matched your search
+70
@@ -0,0 +1,70 @@
|
||||
package com.floci.test;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import software.amazon.awssdk.services.bedrockruntime.BedrockRuntimeAsyncClient;
|
||||
import software.amazon.awssdk.services.bedrockruntime.model.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Verifies Bedrock Runtime's ConverseStream against the real AWS SDK's event-stream decoder,
|
||||
* not just Floci's own raw-byte assertions - proves the {@code application/vnd.amazon.eventstream}
|
||||
* frames Floci emits actually round-trip through {@link BedrockRuntimeAsyncClient}'s generated
|
||||
* unmarshaller end to end.
|
||||
*/
|
||||
@DisplayName("Bedrock Runtime ConverseStream")
|
||||
class BedrockRuntimeConverseStreamTest {
|
||||
|
||||
private static final String MODEL_ID = "anthropic.claude-3-haiku-20240307-v1:0";
|
||||
|
||||
@Test
|
||||
void converseStream_deliversTextDeltasAndCompletes() throws Exception {
|
||||
try (BedrockRuntimeAsyncClient client = TestFixtures.bedrockRuntimeAsyncClient()) {
|
||||
List<ConverseStreamOutput> received = new ArrayList<>();
|
||||
AtomicReference<Throwable> error = new AtomicReference<>();
|
||||
CountDownLatch complete = new CountDownLatch(1);
|
||||
|
||||
ConverseStreamResponseHandler handler = ConverseStreamResponseHandler.builder()
|
||||
.subscriber(received::add)
|
||||
.onError(error::set)
|
||||
.onComplete(complete::countDown)
|
||||
.build();
|
||||
|
||||
ConverseStreamRequest request = ConverseStreamRequest.builder()
|
||||
.modelId(MODEL_ID)
|
||||
.messages(Message.builder()
|
||||
.role(ConversationRole.USER)
|
||||
.content(List.of(ContentBlock.fromText("hi")))
|
||||
.build())
|
||||
.build();
|
||||
|
||||
CompletableFuture<Void> future = client.converseStream(request, handler);
|
||||
future.get(15, TimeUnit.SECONDS);
|
||||
|
||||
assertThat(complete.await(15, TimeUnit.SECONDS)).as("onComplete should fire").isTrue();
|
||||
assertThat(error.get()).as("no stream error expected").isNull();
|
||||
assertThat(received).isNotEmpty();
|
||||
assertThat(received.get(0)).isInstanceOf(MessageStartEvent.class);
|
||||
|
||||
List<String> text = received.stream()
|
||||
.filter(ContentBlockDeltaEvent.class::isInstance)
|
||||
.map(ContentBlockDeltaEvent.class::cast)
|
||||
.map(ContentBlockDeltaEvent::delta)
|
||||
.map(ContentBlockDelta::text)
|
||||
.toList();
|
||||
assertThat(text)
|
||||
.containsExactly("Floci stub response", " for model=", MODEL_ID);
|
||||
|
||||
assertThat(received).anyMatch(MessageStopEvent.class::isInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user