diff --git a/docs/release_notes.md b/docs/release_notes.md index c4e5c55a9..b60c091dc 100644 --- a/docs/release_notes.md +++ b/docs/release_notes.md @@ -20,4 +20,4 @@ ### 🐛 Fixed Issues -- +- [Realtime] Fixed possible race condition in Realtime API implementation diff --git a/foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/realtime/WSOpenAiRealtimeClient.java b/foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/realtime/WSOpenAiRealtimeClient.java index a5c44e4f4..1e590fd18 100644 --- a/foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/realtime/WSOpenAiRealtimeClient.java +++ b/foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/realtime/WSOpenAiRealtimeClient.java @@ -130,7 +130,7 @@ protected void sendMessage(@Nonnull final Object message) { } synchronized (this) { try { - ws.sendText(JACKSON.writeValueAsString(message), true); + ws.sendText(JACKSON.writeValueAsString(message), true).join(); ws.request(1); } catch (final JsonProcessingException e) { throw new ClientException("Failed to serialize message", e); @@ -165,6 +165,7 @@ protected synchronized void sendPing(@Nonnull final WebSocket ws) { if (ws.isInputClosed()) { return; } + log.trace("Sending wss ping"); ws.sendPing(ByteBuffer.wrap("ping".getBytes(StandardCharsets.UTF_8))).join(); ws.request(1); } diff --git a/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/RealtimeApiTest.java b/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/RealtimeApiTest.java index 7830670f0..f5f7d014f 100644 --- a/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/RealtimeApiTest.java +++ b/sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/RealtimeApiTest.java @@ -23,14 +23,16 @@ public class RealtimeApiTest { private static final double LOG_2 = Math.log(2.0); - private static byte[] HELLO_FIXTURE_PCM; + private static byte[] QUESTION_FIXTURE_PCM; private final OpenAiService service = new OpenAiService(); @BeforeAll public static void setUp() { - try (var fis = new FileInputStream("src/test/resources/fixtures/hello.pcm")) { - HELLO_FIXTURE_PCM = fis.readAllBytes(); + try (var fis = + new FileInputStream( + "src/test/resources/fixtures/what-is-the-german-word-for-chicken.pcm")) { + QUESTION_FIXTURE_PCM = fis.readAllBytes(); } catch (IOException e) { fail(e.getMessage()); } @@ -90,7 +92,7 @@ void testSpeechToSpeech() { }, RealtimeParamTurnDetection.EACH_CALL_IS_A_TURN, systemPrompt)) { - input.inputAudio(HELLO_FIXTURE_PCM); + input.inputAudio(QUESTION_FIXTURE_PCM); completed = monitor.await(55, TimeUnit.SECONDS); } catch (Exception e) { if (!(e instanceof InterruptedException)) { diff --git a/sample-code/spring-app/src/test/resources/fixtures/hello.pcm b/sample-code/spring-app/src/test/resources/fixtures/hello.pcm deleted file mode 100644 index 7aa7f9435..000000000 Binary files a/sample-code/spring-app/src/test/resources/fixtures/hello.pcm and /dev/null differ diff --git a/sample-code/spring-app/src/test/resources/fixtures/what-is-the-german-word-for-chicken.pcm b/sample-code/spring-app/src/test/resources/fixtures/what-is-the-german-word-for-chicken.pcm new file mode 100644 index 000000000..d0707295b Binary files /dev/null and b/sample-code/spring-app/src/test/resources/fixtures/what-is-the-german-word-for-chicken.pcm differ