|
5 | 5 | package net.ladenthin.llama.server; |
6 | 6 |
|
7 | 7 | import static org.hamcrest.MatcherAssert.assertThat; |
| 8 | +import static org.hamcrest.Matchers.contains; |
8 | 9 | import static org.hamcrest.Matchers.containsString; |
9 | 10 | import static org.hamcrest.Matchers.is; |
10 | 11 | import static org.hamcrest.Matchers.not; |
11 | 12 |
|
| 13 | +import com.fasterxml.jackson.databind.JsonNode; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
12 | 18 | import org.junit.jupiter.api.Test; |
13 | 19 |
|
14 | 20 | /** |
|
18 | 24 | */ |
19 | 25 | public class ResponsesStreamTranslatorTest { |
20 | 26 |
|
| 27 | + private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 28 | + |
| 29 | + /** The {@code data:} payloads of the framed SSE events, in order. */ |
| 30 | + private static List<JsonNode> events(String sse) throws IOException { |
| 31 | + List<JsonNode> events = new ArrayList<>(); |
| 32 | + for (String line : sse.split("\n")) { |
| 33 | + if (line.startsWith("data: ")) { |
| 34 | + events.add(MAPPER.readTree(line.substring("data: ".length()))); |
| 35 | + } |
| 36 | + } |
| 37 | + return events; |
| 38 | + } |
| 39 | + |
| 40 | + /** The first event of {@code type} whose {@code output_index} is {@code outputIndex}. */ |
| 41 | + private static JsonNode eventAt(List<JsonNode> events, String type, int outputIndex) { |
| 42 | + for (JsonNode event : events) { |
| 43 | + if (event.path("type").asText().equals(type) |
| 44 | + && event.path("output_index").asInt(-1) == outputIndex) { |
| 45 | + return event; |
| 46 | + } |
| 47 | + } |
| 48 | + throw new AssertionError("no " + type + " at output_index " + outputIndex); |
| 49 | + } |
| 50 | + |
21 | 51 | @Test |
22 | 52 | public void beginEmitsResponseCreated() { |
23 | 53 | ResponsesStreamTranslator translator = new ResponsesStreamTranslator("m", "resp_1"); |
@@ -77,6 +107,75 @@ public void reasoningDeltasStreamAsAReasoningItemLikeUpstream() { |
77 | 107 | assertThat(completed, containsString("\"type\":\"reasoning_text\",\"text\":\"6 times 7\"")); |
78 | 108 | } |
79 | 109 |
|
| 110 | + @Test |
| 111 | + public void reasoningTextAndToolCallGetConsecutiveOutputIndicesAndSequenceNumbers() throws IOException { |
| 112 | + // Responses clients key the items by output_index (the OpenAI SDK raises on a duplicate) and |
| 113 | + // order the events by sequence_number; reasoning, message and function_call are items 0, 1, 2. |
| 114 | + ResponsesStreamTranslator translator = new ResponsesStreamTranslator("m", "resp_1"); |
| 115 | + List<JsonNode> events = events(translator.begin() |
| 116 | + + translator.onChunk("{\"choices\":[{\"delta\":{\"reasoning_content\":\"6 times 7\"}}]}") |
| 117 | + + translator.onChunk("{\"choices\":[{\"delta\":{\"content\":\"42\"}}]}") |
| 118 | + + translator.onChunk("{\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"c1\"," |
| 119 | + + "\"function\":{\"name\":\"f\",\"arguments\":\"{}\"}}]},\"finish_reason\":\"tool_calls\"}]}") |
| 120 | + + translator.end()); |
| 121 | + |
| 122 | + assertThat(events.size(), is(15)); |
| 123 | + List<Integer> sequence = new ArrayList<>(); |
| 124 | + List<Integer> expected = new ArrayList<>(); |
| 125 | + for (int i = 0; i < events.size(); i++) { |
| 126 | + sequence.add(events.get(i).path("sequence_number").asInt(-1)); |
| 127 | + expected.add(i); |
| 128 | + } |
| 129 | + assertThat(sequence, is(expected)); |
| 130 | + |
| 131 | + String[] itemTypes = {"reasoning", "message", "function_call"}; |
| 132 | + for (int index = 0; index < itemTypes.length; index++) { |
| 133 | + assertThat( |
| 134 | + eventAt(events, "response.output_item.added", index) |
| 135 | + .path("item") |
| 136 | + .path("type") |
| 137 | + .asText(), |
| 138 | + is(itemTypes[index])); |
| 139 | + assertThat( |
| 140 | + eventAt(events, "response.output_item.done", index) |
| 141 | + .path("item") |
| 142 | + .path("type") |
| 143 | + .asText(), |
| 144 | + is(itemTypes[index])); |
| 145 | + } |
| 146 | + |
| 147 | + JsonNode reasoningDone = eventAt(events, "response.reasoning_text.done", 0); |
| 148 | + assertThat(reasoningDone.path("item_id").asText(), is("rs_resp_1")); |
| 149 | + assertThat(reasoningDone.path("content_index").asInt(-1), is(0)); |
| 150 | + assertThat(reasoningDone.path("text").asText(), is("6 times 7")); |
| 151 | + assertThat(reasoningDone.path("sequence_number").asInt(-1), is(6)); |
| 152 | + |
| 153 | + JsonNode completed = events.get(events.size() - 1); |
| 154 | + assertThat(completed.path("type").asText(), is("response.completed")); |
| 155 | + List<String> outputTypes = new ArrayList<>(); |
| 156 | + for (JsonNode item : completed.path("response").path("output")) { |
| 157 | + outputTypes.add(item.path("type").asText()); |
| 158 | + } |
| 159 | + assertThat(outputTypes, contains("reasoning", "message", "function_call")); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void failedContinuesTheSequenceNumber() throws IOException { |
| 164 | + // response.failed ends a stream that already emitted events; its sequence_number follows them. |
| 165 | + ResponsesStreamTranslator translator = new ResponsesStreamTranslator("m", "resp_1"); |
| 166 | + List<JsonNode> events = events(translator.begin() |
| 167 | + + translator.onChunk("{\"choices\":[{\"delta\":{\"content\":\"he\"}}]}") |
| 168 | + + translator.failed("boom", "server_error")); |
| 169 | + |
| 170 | + assertThat(events.size(), is(5)); |
| 171 | + JsonNode failed = events.get(4); |
| 172 | + assertThat(failed.path("type").asText(), is("response.failed")); |
| 173 | + assertThat(failed.path("sequence_number").asInt(-1), is(4)); |
| 174 | + assertThat(failed.path("response").path("status").asText(), is("failed")); |
| 175 | + assertThat(failed.path("response").path("error").path("code").asText(), is("server_error")); |
| 176 | + assertThat(failed.path("response").path("error").path("message").asText(), is("boom")); |
| 177 | + } |
| 178 | + |
80 | 179 | @Test |
81 | 180 | public void toolCallsBecomeFunctionCallItemsBeforeCompleted() { |
82 | 181 | ResponsesStreamTranslator translator = new ResponsesStreamTranslator("m", "resp_1"); |
|
0 commit comments