|
| 1 | +package dev.braintrust.instrumentation; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 5 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 6 | +import dev.braintrust.json.BraintrustJsonMapper; |
| 7 | +import java.util.LinkedHashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +/** |
| 11 | + * Reconstructs a full (non-streaming) OpenAI-style chat completion response by generically merging |
| 12 | + * SSE chunks. |
| 13 | + * |
| 14 | + * <p>The merge is field-agnostic on purpose: every field the provider streams (content, reasoning, |
| 15 | + * tool calls, refusals, ...) is preserved rather than a hand-picked subset, so new streaming fields |
| 16 | + * are captured without changes here. Top-level fields (id, model, created, usage, ...) are |
| 17 | + * last-non-null-wins; each {@code choices[].delta} is merged into the reconstructed {@code |
| 18 | + * choices[].message}, where textual leaves are concatenated and structured leaves are merged |
| 19 | + * recursively. |
| 20 | + * |
| 21 | + * <p>Shared across instrumentation modules (langchain, spring-ai, ...) so there is one |
| 22 | + * reconstruction rather than divergent per-module copies. Instances are not thread-safe; feed one |
| 23 | + * accumulator the chunks of a single response from a single thread. |
| 24 | + */ |
| 25 | +public final class SseResponseAccumulator { |
| 26 | + private final ObjectNode responseRoot = BraintrustJsonMapper.get().createObjectNode(); |
| 27 | + // Accumulated choices keyed by their "index" so multi-choice (n>1) streams merge correctly. |
| 28 | + private final Map<Integer, ObjectNode> choicesByIndex = new LinkedHashMap<>(); |
| 29 | + |
| 30 | + /** Merge one parsed SSE chunk into the reconstructed response. Non-objects are ignored. */ |
| 31 | + public void merge(JsonNode chunk) { |
| 32 | + if (chunk == null || !chunk.isObject()) return; |
| 33 | + var fields = chunk.fields(); |
| 34 | + while (fields.hasNext()) { |
| 35 | + var entry = fields.next(); |
| 36 | + String name = entry.getKey(); |
| 37 | + JsonNode value = entry.getValue(); |
| 38 | + if ("choices".equals(name)) { |
| 39 | + if (value.isArray()) { |
| 40 | + value.forEach(this::mergeChoice); |
| 41 | + } |
| 42 | + } else if (!value.isNull()) { |
| 43 | + responseRoot.set(name, value); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** Build the reconstructed response object, assembling accumulated choices in index order. */ |
| 49 | + public ObjectNode build() { |
| 50 | + var choicesArray = BraintrustJsonMapper.get().createArrayNode(); |
| 51 | + choicesByIndex.values().forEach(choicesArray::add); |
| 52 | + responseRoot.set("choices", choicesArray); |
| 53 | + return responseRoot; |
| 54 | + } |
| 55 | + |
| 56 | + private void mergeChoice(JsonNode choiceChunk) { |
| 57 | + if (!choiceChunk.isObject()) return; |
| 58 | + int index = choiceChunk.has("index") ? choiceChunk.get("index").asInt() : 0; |
| 59 | + ObjectNode choice = |
| 60 | + choicesByIndex.computeIfAbsent( |
| 61 | + index, |
| 62 | + i -> { |
| 63 | + var node = BraintrustJsonMapper.get().createObjectNode(); |
| 64 | + node.put("index", i); |
| 65 | + return node; |
| 66 | + }); |
| 67 | + var fields = choiceChunk.fields(); |
| 68 | + while (fields.hasNext()) { |
| 69 | + var entry = fields.next(); |
| 70 | + String name = entry.getKey(); |
| 71 | + JsonNode value = entry.getValue(); |
| 72 | + if ("delta".equals(name)) { |
| 73 | + // Streaming nests the message under "delta"; the reconstructed non-streaming shape |
| 74 | + // the UI expects uses "message". |
| 75 | + ObjectNode message = |
| 76 | + choice.has("message") && choice.get("message").isObject() |
| 77 | + ? (ObjectNode) choice.get("message") |
| 78 | + : choice.putObject("message"); |
| 79 | + deepMerge(message, value); |
| 80 | + } else if (!"index".equals(name) && !value.isNull()) { |
| 81 | + // finish_reason, logprobs, ... last-non-null-wins. |
| 82 | + choice.set(name, value); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Recursively merges {@code source} into {@code target}. Textual leaves are concatenated (so |
| 89 | + * streamed content / reasoning / tool-call arguments accumulate), nested objects are merged |
| 90 | + * key-by-key, and arrays whose elements carry an {@code index} (e.g. {@code tool_calls}) are |
| 91 | + * merged by that index. Other scalars are last-write-wins. |
| 92 | + */ |
| 93 | + private static void deepMerge(ObjectNode target, JsonNode source) { |
| 94 | + if (!source.isObject()) return; |
| 95 | + var fields = source.fields(); |
| 96 | + while (fields.hasNext()) { |
| 97 | + var entry = fields.next(); |
| 98 | + String name = entry.getKey(); |
| 99 | + JsonNode value = entry.getValue(); |
| 100 | + JsonNode existing = target.get(name); |
| 101 | + if (value.isTextual() && existing != null && existing.isTextual()) { |
| 102 | + target.put(name, existing.asText() + value.asText()); |
| 103 | + } else if (value.isObject()) { |
| 104 | + if (existing != null && existing.isObject()) { |
| 105 | + deepMerge((ObjectNode) existing, value); |
| 106 | + } else { |
| 107 | + target.set(name, value.deepCopy()); |
| 108 | + } |
| 109 | + } else if (value.isArray()) { |
| 110 | + mergeArray(target, name, value); |
| 111 | + } else if (!value.isNull()) { |
| 112 | + target.set(name, value); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static void mergeArray(ObjectNode target, String name, JsonNode sourceArray) { |
| 118 | + if (!(target.get(name) instanceof ArrayNode targetArray)) { |
| 119 | + target.set(name, sourceArray.deepCopy()); |
| 120 | + return; |
| 121 | + } |
| 122 | + for (JsonNode element : sourceArray) { |
| 123 | + ObjectNode match = |
| 124 | + element.isObject() && element.has("index") |
| 125 | + ? findByIndex(targetArray, element.get("index").asInt()) |
| 126 | + : null; |
| 127 | + if (match != null) { |
| 128 | + deepMerge(match, element); |
| 129 | + } else { |
| 130 | + targetArray.add(element.deepCopy()); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static ObjectNode findByIndex(ArrayNode array, int index) { |
| 136 | + for (JsonNode candidate : array) { |
| 137 | + if (candidate.isObject() |
| 138 | + && candidate.has("index") |
| 139 | + && candidate.get("index").asInt() == index) { |
| 140 | + return (ObjectNode) candidate; |
| 141 | + } |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | +} |
0 commit comments