001/*
002 * Copyright (c) 2026 Singular
003 * SPDX-License-Identifier: MIT
004 */
005
006package ai.singlr.openai;
007
008import ai.singlr.core.common.HttpClientFactory;
009import ai.singlr.core.common.Strings;
010import ai.singlr.core.model.CloseableIterator;
011import ai.singlr.core.model.FinishReason;
012import ai.singlr.core.model.Message;
013import ai.singlr.core.model.Model;
014import ai.singlr.core.model.ModelConfig;
015import ai.singlr.core.model.Response;
016import ai.singlr.core.model.StreamEvent;
017import ai.singlr.core.model.ThinkingLevel;
018import ai.singlr.core.model.ToolCall;
019import ai.singlr.core.model.ToolChoice;
020import ai.singlr.core.model.TransientStreamException;
021import ai.singlr.core.schema.OutputSchema;
022import ai.singlr.core.schema.StructuredContentParser;
023import ai.singlr.core.tool.Tool;
024import ai.singlr.openai.api.ApiStreamEvent;
025import ai.singlr.openai.api.ContentPart;
026import ai.singlr.openai.api.InputItem;
027import ai.singlr.openai.api.ResponsesRequest;
028import ai.singlr.openai.api.TextFormatConfig;
029import ai.singlr.openai.api.ToolDefinition;
030import java.io.BufferedReader;
031import java.io.IOException;
032import java.io.InputStream;
033import java.io.InputStreamReader;
034import java.net.URI;
035import java.net.http.HttpClient;
036import java.net.http.HttpRequest;
037import java.net.http.HttpResponse;
038import java.nio.charset.StandardCharsets;
039import java.time.Duration;
040import java.util.ArrayList;
041import java.util.Base64;
042import java.util.HashMap;
043import java.util.LinkedHashMap;
044import java.util.List;
045import java.util.Map;
046import java.util.concurrent.Callable;
047import java.util.concurrent.ExecutionException;
048import java.util.concurrent.ExecutorService;
049import java.util.concurrent.Executors;
050import java.util.concurrent.Future;
051import java.util.concurrent.TimeUnit;
052import java.util.concurrent.TimeoutException;
053import tools.jackson.databind.DeserializationFeature;
054import tools.jackson.databind.ObjectMapper;
055import tools.jackson.databind.json.JsonMapper;
056
057/**
058 * OpenAI model implementation using the Responses API.
059 *
060 * <p>All requests use SSE streaming internally for robust timeout handling. Synchronous {@link
061 * #chat} methods stream under the hood and accumulate the response, avoiding HTTP read timeouts on
062 * long-running generations. A per-line idle timeout detects stalled streams and throws a retryable
063 * {@link OpenAIException}.
064 */
065public class OpenAIModel implements Model {
066
067  private static final String PROVIDER_NAME = "openai";
068  static final String DEFAULT_BASE_URL = "https://api.openai.com/v1/responses";
069
070  static final String REASONING_KEY = "openai.reasoning";
071
072  private final String wireModelId;
073  private final OpenAIModelId knownModel;
074  private final ModelConfig config;
075  private final HttpClient httpClient;
076  private final ObjectMapper objectMapper;
077
078  OpenAIModel(OpenAIModelId modelId, ModelConfig config) {
079    this(modelId != null ? modelId.id() : null, modelId, config);
080  }
081
082  OpenAIModel(String wireModelId, ModelConfig config) {
083    this(wireModelId, OpenAIModelId.fromId(wireModelId), config);
084  }
085
086  private OpenAIModel(String wireModelId, OpenAIModelId knownModel, ModelConfig config) {
087    if (Strings.isBlank(wireModelId)) {
088      throw new IllegalArgumentException("modelId is required");
089    }
090    if (config == null) {
091      throw new IllegalArgumentException("config is required");
092    }
093    var hasCustomEndpoint = !Strings.isBlank(config.baseUrl());
094    if (!hasCustomEndpoint && Strings.isBlank(config.apiKey())) {
095      throw new IllegalArgumentException(
096          "config with valid apiKey is required (or set baseUrl + auth header)");
097    }
098    if (config.webSearch()) {
099      throw new IllegalArgumentException(
100          "ModelConfig.webSearch is not supported by helios-openai; disable it or use a provider"
101              + " with native web search (Anthropic, Gemini)");
102    }
103    if (config.webFetch()) {
104      throw new IllegalArgumentException(
105          "ModelConfig.webFetch is not supported by helios-openai; disable it or use a provider"
106              + " with native web fetch (Anthropic, Gemini)");
107    }
108    this.wireModelId = wireModelId;
109    this.knownModel = knownModel;
110    this.config = config;
111    this.httpClient = HttpClientFactory.create(config);
112    this.objectMapper =
113        JsonMapper.builder().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES).build();
114  }
115
116  @Override
117  public String id() {
118    return wireModelId;
119  }
120
121  @Override
122  public String provider() {
123    return PROVIDER_NAME;
124  }
125
126  @Override
127  public int contextWindow() {
128    if (config.contextWindow() != null) {
129      return config.contextWindow();
130    }
131    return knownModel != null ? knownModel.contextWindow() : 0;
132  }
133
134  @Override
135  public int maxOutputTokens() {
136    return knownModel != null ? knownModel.maxOutputTokens() : 0;
137  }
138
139  @Override
140  public void close() {
141    HttpClientFactory.shutdownGracefully(httpClient);
142  }
143
144  @Override
145  public Response<Void> chat(List<Message> messages, List<Tool> tools) {
146    var request = buildRequest(messages, tools, null);
147    return streamAndDrain(request);
148  }
149
150  @Override
151  public <T> Response<T> chat(
152      List<Message> messages, List<Tool> tools, OutputSchema<T> outputSchema) {
153    var request = buildRequest(messages, tools, outputSchema.schema().toMap());
154    var response = streamAndDrain(request);
155    // Tool-calling turns are intermediate — structured output is the deliverable of a later
156    // text-only turn. Parsing incidental prose here throws and kills the session before the loop
157    // dispatches the tool.
158    T parsed = null;
159    if (response.toolCalls().isEmpty()) {
160      parsed = parseStructuredContent(response.content(), outputSchema);
161    }
162
163    return Response.<T>newBuilder(outputSchema.type())
164        .withContent(response.content())
165        .withParsed(parsed)
166        .withToolCalls(response.toolCalls())
167        .withFinishReason(response.finishReason())
168        .withUsage(response.usage())
169        .withThinking(response.thinking())
170        .withCitations(response.citations())
171        .withMetadata(response.metadata())
172        .build();
173  }
174
175  @Override
176  public CloseableIterator<StreamEvent> chatStream(List<Message> messages, List<Tool> tools) {
177    var request = buildRequest(messages, tools, null);
178    try {
179      return openStream(request);
180    } catch (OpenAIException e) {
181      return CloseableIterator.of(
182          List.of((StreamEvent) new StreamEvent.Error(e.getMessage(), e)).iterator());
183    } catch (IOException e) {
184      return CloseableIterator.of(
185          List.of((StreamEvent) new StreamEvent.Error("Failed to connect", e)).iterator());
186    } catch (InterruptedException e) {
187      Thread.currentThread().interrupt();
188      return CloseableIterator.of(
189          List.of((StreamEvent) new StreamEvent.Error("Request interrupted", e)).iterator());
190    }
191  }
192
193  <T> T parseStructuredContent(String content, OutputSchema<T> schema) {
194    return StructuredContentParser.parse(content, schema, jsonAdapter);
195  }
196
197  @SuppressWarnings({"unchecked", "rawtypes"})
198  private final StructuredContentParser.JsonAdapter jsonAdapter =
199      new StructuredContentParser.JsonAdapter() {
200        @Override
201        public Map<String, Object> toMap(String json) throws Exception {
202          return objectMapper.readValue(json, Map.class);
203        }
204
205        @Override
206        public <T> T fromMap(Map<String, Object> map, Class<T> type) {
207          return objectMapper.convertValue(map, type);
208        }
209      };
210
211  private StreamingIterator openStream(ResponsesRequest request)
212      throws IOException, InterruptedException {
213    var jsonBody = serializeRequest(request);
214    var httpRequest = buildHttpRequest(jsonBody);
215    var httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandlers.ofInputStream());
216    if (httpResponse.statusCode() != 200) {
217      try (var body = httpResponse.body()) {
218        var errorBody = HttpClientFactory.readBoundedErrorBody(body);
219        throw new OpenAIException(
220            "API error (status " + httpResponse.statusCode() + "): " + errorBody,
221            httpResponse.statusCode());
222      }
223    }
224    return new StreamingIterator(httpResponse, objectMapper, config.streamIdleTimeout());
225  }
226
227  private Response<Void> streamAndDrain(ResponsesRequest request) {
228    try (var iterator = openStream(request)) {
229      return drainToResponse(iterator);
230    } catch (OpenAIException | TransientStreamException e) {
231      throw e;
232    } catch (IOException e) {
233      throw new TransientStreamException("Failed to communicate with OpenAI API", e, PROVIDER_NAME);
234    } catch (InterruptedException e) {
235      Thread.currentThread().interrupt();
236      throw new OpenAIException("Request interrupted", e);
237    }
238  }
239
240  @SuppressWarnings("unchecked")
241  static Response<Void> drainToResponse(StreamingIterator iterator) {
242    while (iterator.hasNext()) {
243      var event = iterator.next();
244      if (event instanceof StreamEvent.Done(var response)) {
245        return (Response<Void>) response;
246      }
247      if (event instanceof StreamEvent.Error(String message, Exception cause)) {
248        if (cause instanceof OpenAIException oe) {
249          throw oe;
250        }
251        if (cause instanceof IOException) {
252          throw new TransientStreamException(message, cause, PROVIDER_NAME);
253        }
254        throw new OpenAIException(message, cause);
255      }
256    }
257    throw new OpenAIException("Stream ended without completion event");
258  }
259
260  ResponsesRequest buildRequest(
261      List<Message> messages, List<Tool> tools, Map<String, Object> outputSchema) {
262    var inputItems = new ArrayList<InputItem>();
263    String instructions = null;
264
265    for (var message : messages) {
266      switch (message.role()) {
267        case SYSTEM -> instructions = appendSystemText(instructions, message.content());
268        case USER -> inputItems.add(convertUserMessage(message));
269        case ASSISTANT -> inputItems.addAll(convertAssistantMessage(message));
270        case TOOL ->
271            inputItems.add(InputItem.functionCallOutput(message.toolCallId(), message.content()));
272      }
273    }
274
275    List<ToolDefinition> toolDefs = null;
276    if (tools != null && !tools.isEmpty()) {
277      toolDefs =
278          tools.stream()
279              .map(
280                  t ->
281                      ToolDefinition.function(
282                          t.name(), t.description(), t.parametersAsJsonSchema()))
283              .toList();
284    }
285
286    var toolChoiceValue = buildToolChoice(tools);
287    var reasoningConfig = buildReasoningConfig();
288
289    Double temperature = config.temperature();
290    if (reasoningConfig != null) {
291      temperature = null;
292    }
293
294    var builder =
295        ResponsesRequest.newBuilder()
296            .withModel(wireModelId)
297            .withInput(inputItems)
298            .withInstructions(instructions)
299            .withStream(true)
300            .withTools(toolDefs)
301            .withToolChoice(toolChoiceValue)
302            .withTemperature(temperature)
303            .withTopP(config.topP())
304            .withMaxOutputTokens(
305                config.maxOutputTokens() != null ? config.maxOutputTokens() : maxOutputTokens())
306            .withStop(config.stopSequences())
307            .withReasoning(reasoningConfig)
308            .withPromptCacheKey(config.promptCacheKey());
309
310    if (outputSchema != null) {
311      var hasOpenMap = hasOpenMapShape(outputSchema);
312      var schema = hasOpenMap ? outputSchema : addAdditionalPropertiesFalse(outputSchema);
313      var textFormat = TextFormatConfig.jsonSchema("output", schema, !hasOpenMap);
314      builder.withText(new ResponsesRequest.TextConfig(textFormat));
315    }
316
317    return builder.build();
318  }
319
320  private static String appendSystemText(String existing, String additional) {
321    if (existing == null) {
322      return additional;
323    }
324    return existing + "\n\n" + additional;
325  }
326
327  /**
328   * Returns {@code true} when the schema contains any open-keyed object — an {@code object} type
329   * whose {@code additionalProperties} is a value-schema (i.e., a {@code Map<String, X>} shape)
330   * rather than {@code false}.
331   *
332   * <p>OpenAI's strict mode rejects schemas with open-keyed objects: strict mode requires every
333   * {@code object} to set {@code additionalProperties: false} and list every property in {@code
334   * required}. Open Maps violate both. Detecting this lets {@link #buildRequest} fall back to
335   * non-strict json_schema mode, which preserves structured output without the strict-mode
336   * validator.
337   */
338  @SuppressWarnings("unchecked")
339  static boolean hasOpenMapShape(Map<String, Object> schema) {
340    if (schema == null) {
341      return false;
342    }
343    if ("object".equals(schema.get("type"))
344        && schema.get("additionalProperties") instanceof Map<?, ?>) {
345      return true;
346    }
347    if (schema.get("properties") instanceof Map<?, ?> props) {
348      for (var entry : ((Map<String, Object>) props).entrySet()) {
349        if (entry.getValue() instanceof Map<?, ?> nested
350            && hasOpenMapShape((Map<String, Object>) nested)) {
351          return true;
352        }
353      }
354    }
355    if (schema.get("items") instanceof Map<?, ?> items
356        && hasOpenMapShape((Map<String, Object>) items)) {
357      return true;
358    }
359    if (schema.get("additionalProperties") instanceof Map<?, ?> ap
360        && hasOpenMapShape((Map<String, Object>) ap)) {
361      return true;
362    }
363    return false;
364  }
365
366  @SuppressWarnings("unchecked")
367  static Map<String, Object> addAdditionalPropertiesFalse(Map<String, Object> schema) {
368    var result = new HashMap<>(schema);
369    if ("object".equals(result.get("type"))) {
370      var existing = result.get("additionalProperties");
371      if (existing instanceof Map<?, ?> existingSchema) {
372        // Map value schema — recurse into it instead of overwriting
373        result.put(
374            "additionalProperties",
375            addAdditionalPropertiesFalse((Map<String, Object>) existingSchema));
376      } else {
377        result.put("additionalProperties", false);
378      }
379      if (result.get("properties") instanceof Map<?, ?> props) {
380        var newProps = new HashMap<String, Object>();
381        for (var entry : ((Map<String, Object>) props).entrySet()) {
382          if (entry.getValue() instanceof Map<?, ?> nested) {
383            newProps.put(
384                entry.getKey(), addAdditionalPropertiesFalse((Map<String, Object>) nested));
385          } else {
386            newProps.put(entry.getKey(), entry.getValue());
387          }
388        }
389        result.put("properties", newProps);
390      }
391    }
392    if ("array".equals(result.get("type")) && result.get("items") instanceof Map<?, ?> items) {
393      result.put("items", addAdditionalPropertiesFalse((Map<String, Object>) items));
394    }
395    return result;
396  }
397
398  /**
399   * Convert a Helios USER {@link Message} into a Responses-API input item. Plain text is emitted as
400   * the bare-string overload (the Responses API accepts that form). When the message carries inline
401   * files, the wire shape becomes a content-part array so the provider receives the image/file
402   * blocks alongside the text.
403   *
404   * @param message the user message; non-null
405   * @return the input item
406   */
407  static InputItem convertUserMessage(Message message) {
408    var text = message.content() != null ? message.content() : "";
409    if (!message.hasInlineFiles()) {
410      return InputItem.userMessage(text);
411    }
412    var parts = new ArrayList<ContentPart>(message.inlineFiles().size() + 1);
413    for (var file : message.inlineFiles()) {
414      var data = Base64.getEncoder().encodeToString(file.data());
415      var media = file.mimeType();
416      if (media != null && media.startsWith("image/")) {
417        parts.add(ContentPart.inputImage(media, data));
418      } else {
419        parts.add(ContentPart.inputFile(media, data, null));
420      }
421    }
422    if (!text.isEmpty()) {
423      parts.add(ContentPart.inputText(text));
424    }
425    return InputItem.userMessage(parts);
426  }
427
428  List<InputItem> convertAssistantMessage(Message message) {
429    var items = new ArrayList<InputItem>();
430
431    if (message.content() != null && !message.content().isEmpty()) {
432      items.add(InputItem.assistantMessage(message.content()));
433    }
434
435    if (message.hasToolCalls()) {
436      for (var tc : message.toolCalls()) {
437        var argsJson = serializeArguments(tc.arguments());
438        items.add(InputItem.functionCall(tc.id(), tc.name(), argsJson));
439      }
440    }
441
442    if (items.isEmpty()) {
443      items.add(InputItem.assistantMessage(""));
444    }
445
446    return items;
447  }
448
449  private String serializeArguments(Map<String, Object> arguments) {
450    if (arguments == null || arguments.isEmpty()) {
451      return "{}";
452    }
453    try {
454      return objectMapper.writeValueAsString(arguments);
455    } catch (Exception e) {
456      throw new OpenAIException("Failed to serialize tool call arguments", e);
457    }
458  }
459
460  private Object buildToolChoice(List<Tool> tools) {
461    if (config.toolChoice() == null) {
462      return null;
463    }
464
465    return switch (config.toolChoice()) {
466      case ToolChoice.Auto a -> "auto";
467      case ToolChoice.Any a -> "required";
468      case ToolChoice.None n -> "none";
469      case ToolChoice.Required r -> {
470        var name = r.allowedTools().iterator().next();
471        yield Map.of("type", "function", "name", name);
472      }
473    };
474  }
475
476  /**
477   * Model-aware effort dispatch driven by {@link OpenAIModelId.EffortSupport}. FULL models (gpt-5.6
478   * family) accept the whole none..max range, so {@code ThinkingLevel.NONE} maps to an explicit
479   * {@code "none"} (omitting the config would run the model's default medium reasoning) and {@code
480   * MAX} maps to {@code "max"}. EXTENDED models (gpt-5.4/5.5) top out at {@code xhigh}; STANDARD
481   * models clamp everything above {@code high} down so requests stay valid.
482   */
483  private ResponsesRequest.ReasoningConfig buildReasoningConfig() {
484    var support =
485        knownModel != null ? knownModel.effortSupport() : OpenAIModelId.EffortSupport.STANDARD;
486    var level = config.thinkingLevel() == null ? ThinkingLevel.NONE : config.thinkingLevel();
487
488    if (level == ThinkingLevel.NONE) {
489      return support == OpenAIModelId.EffortSupport.FULL
490          ? ResponsesRequest.ReasoningConfig.of("none")
491          : null;
492    }
493
494    var effort =
495        switch (level) {
496          case NONE -> throw new IllegalStateException("NONE handled above; unreachable");
497          case MINIMAL, LOW -> "low";
498          case MEDIUM -> "medium";
499          case HIGH -> "high";
500          case XHIGH -> support == OpenAIModelId.EffortSupport.STANDARD ? "high" : "xhigh";
501          case MAX ->
502              switch (support) {
503                case STANDARD -> "high";
504                case EXTENDED -> "xhigh";
505                case FULL -> "max";
506              };
507        };
508
509    return ResponsesRequest.ReasoningConfig.of(effort);
510  }
511
512  String serializeRequest(ResponsesRequest request) {
513    try {
514      return objectMapper.writeValueAsString(request);
515    } catch (Exception e) {
516      throw new OpenAIException("Failed to serialize request", e);
517    }
518  }
519
520  HttpRequest buildHttpRequest(String jsonBody) {
521    var defaults = new LinkedHashMap<String, String>();
522    defaults.put("Content-Type", "application/json");
523    if (!Strings.isBlank(config.apiKey())) {
524      defaults.put("Authorization", "Bearer " + config.apiKey());
525    }
526    var builder =
527        HttpRequest.newBuilder()
528            .uri(URI.create(config.effectiveBaseUrl(DEFAULT_BASE_URL)))
529            .POST(HttpRequest.BodyPublishers.ofString(jsonBody));
530    for (var entry : config.effectiveHeaders(defaults).entrySet()) {
531      builder.header(entry.getKey(), entry.getValue());
532    }
533    // Null-guard matches Anthropic/Gemini parity — HttpRequest.Builder.timeout(null) NPEs and
534    // ModelConfig.Builder.withResponseTimeout(null) is currently legal.
535    if (config.responseTimeout() != null) {
536      builder.timeout(config.responseTimeout());
537    }
538    return builder.build();
539  }
540
541  static FinishReason mapStatus(String status) {
542    if (status == null) {
543      return FinishReason.STOP;
544    }
545    return switch (status) {
546      case "completed" -> FinishReason.STOP;
547      case "incomplete" -> FinishReason.LENGTH;
548      case "failed" -> FinishReason.ERROR;
549      default -> FinishReason.STOP;
550    };
551  }
552
553  static class StreamingIterator implements CloseableIterator<StreamEvent> {
554    private final InputStream rawStream;
555    private final BufferedReader reader;
556    private final ObjectMapper objectMapper;
557    private final Duration streamIdleTimeout;
558    private final ExecutorService readExecutor;
559    private final StringBuilder contentBuilder = new StringBuilder();
560    private final List<ToolCall> toolCalls = new ArrayList<>();
561    private final Map<String, ToolCallAccumulator> toolCallAccumulators = new HashMap<>();
562    private final StringBuilder reasoningBuilder = new StringBuilder();
563    private StreamEvent nextEvent = null;
564    private boolean done = false;
565    private int inputTokens = 0;
566    private int outputTokens = 0;
567    private int cachedInputTokens = 0;
568    private int cacheWriteTokens = 0;
569    private String responseStatus = null;
570
571    StreamingIterator(
572        HttpResponse<InputStream> response, ObjectMapper objectMapper, Duration streamIdleTimeout) {
573      this.rawStream = response.body();
574      this.reader =
575          new BufferedReader(new InputStreamReader(this.rawStream, StandardCharsets.UTF_8));
576      this.objectMapper = objectMapper;
577      this.streamIdleTimeout = streamIdleTimeout;
578      this.readExecutor = Executors.newVirtualThreadPerTaskExecutor();
579    }
580
581    @Override
582    public boolean hasNext() {
583      if (done) {
584        return false;
585      }
586      if (nextEvent != null) {
587        return true;
588      }
589      nextEvent = readNextEvent();
590      return nextEvent != null;
591    }
592
593    @Override
594    public StreamEvent next() {
595      if (nextEvent == null) {
596        nextEvent = readNextEvent();
597      }
598      var event = nextEvent;
599      nextEvent = null;
600      return event;
601    }
602
603    private String readLineWithTimeout() throws IOException {
604      Future<String> future = readExecutor.submit((Callable<String>) () -> reader.readLine());
605      try {
606        return future.get(streamIdleTimeout.toMillis(), TimeUnit.MILLISECONDS);
607      } catch (TimeoutException e) {
608        future.cancel(true);
609        throw new OpenAIException(
610            "Stream idle timeout: no data received for " + streamIdleTimeout.toSeconds() + "s");
611      } catch (ExecutionException e) {
612        if (e.getCause() instanceof IOException ioe) {
613          throw ioe;
614        }
615        throw new IOException("Stream read failed", e.getCause());
616      } catch (InterruptedException e) {
617        future.cancel(true);
618        Thread.currentThread().interrupt();
619        throw new IOException("Stream read interrupted", e);
620      }
621    }
622
623    private StreamEvent readNextEvent() {
624      try {
625        String line;
626        while ((line = readLineWithTimeout()) != null) {
627          if (line.startsWith("data: ")) {
628            var json = line.substring(6).trim();
629            if (json.isEmpty() || json.equals("[DONE]")) {
630              continue;
631            }
632            var event = parseStreamEvent(json);
633            if (event != null) {
634              return event;
635            }
636          }
637        }
638        done = true;
639        close();
640        return buildDoneEvent();
641      } catch (OpenAIException e) {
642        done = true;
643        close();
644        return new StreamEvent.Error(e.getMessage(), e);
645      } catch (IOException e) {
646        done = true;
647        close();
648        return new StreamEvent.Error("Stream read error", e);
649      }
650    }
651
652    @SuppressWarnings("unchecked")
653    private StreamEvent parseStreamEvent(String json) {
654      try {
655        var event = objectMapper.readValue(json, ApiStreamEvent.class);
656
657        if (event.hasTypeResponseOutputTextDelta()) {
658          if (event.delta() != null) {
659            contentBuilder.append(event.delta());
660            return new StreamEvent.TextDelta(event.delta());
661          }
662          return null;
663        }
664
665        if (event.hasTypeResponseOutputItemAdded()) {
666          if (event.item() != null && event.item().hasTypeFunctionCall()) {
667            toolCallAccumulators.put(
668                event.item().id(),
669                new ToolCallAccumulator(
670                    event.item().callId(), event.item().name(), new StringBuilder()));
671            return new StreamEvent.ToolCallStart(event.item().callId(), event.item().name());
672          }
673          return null;
674        }
675
676        if (event.hasTypeFunctionCallArgumentsDelta()) {
677          if (event.delta() != null && event.itemId() != null) {
678            var accumulator = toolCallAccumulators.get(event.itemId());
679            if (accumulator != null) {
680              accumulator.jsonBuilder().append(event.delta());
681            }
682          }
683          return null;
684        }
685
686        if (event.hasTypeFunctionCallArgumentsDone()) {
687          if (event.itemId() != null) {
688            var accumulator = toolCallAccumulators.remove(event.itemId());
689            if (accumulator != null) {
690              var jsonStr = accumulator.jsonBuilder().toString();
691              Map<String, Object> arguments = Map.of();
692              if (!jsonStr.isEmpty()) {
693                try {
694                  arguments = objectMapper.readValue(jsonStr, Map.class);
695                } catch (Exception e) {
696                  arguments = Map.of("_raw", jsonStr);
697                }
698              }
699              var tc =
700                  ToolCall.newBuilder()
701                      .withId(accumulator.callId())
702                      .withName(accumulator.name())
703                      .withArguments(arguments)
704                      .build();
705              toolCalls.add(tc);
706              return new StreamEvent.ToolCallComplete(tc);
707            }
708          }
709          return null;
710        }
711
712        if (event.hasTypeResponseCompleted()) {
713          if (event.response() != null) {
714            responseStatus = event.response().status();
715            if (event.response().usage() != null) {
716              var usage = event.response().usage();
717              if (usage.inputTokens() != null) {
718                inputTokens = usage.inputTokens();
719              }
720              if (usage.outputTokens() != null) {
721                outputTokens = usage.outputTokens();
722              }
723              cachedInputTokens = usage.cachedTokensOrZero();
724              cacheWriteTokens = usage.cacheWriteTokensOrZero();
725            }
726          }
727          done = true;
728          close();
729          return buildDoneEvent();
730        }
731
732        if (event.hasTypeResponseFailed()) {
733          done = true;
734          close();
735          return new StreamEvent.Error("API response failed: " + json, null);
736        }
737
738        if (event.hasTypeError()) {
739          return new StreamEvent.Error("API stream error: " + json, null);
740        }
741
742        if (event.hasTypeReasoningSummaryTextDelta()) {
743          if (event.text() != null) {
744            reasoningBuilder.append(event.text());
745            return new StreamEvent.ThinkingDelta(event.text());
746          }
747          return null;
748        }
749
750        // Reasoning summary block closing — emit terminal aggregation so consumers can stop
751        // accumulating deltas and capture the full reasoning text. OpenAI's Responses API does
752        // not surface a signature for reasoning summaries, so the second arg is null.
753        if (event.hasTypeReasoningSummaryTextDone() && !reasoningBuilder.isEmpty()) {
754          return new StreamEvent.ThinkingComplete(reasoningBuilder.toString(), null);
755        }
756
757        return null;
758      } catch (Exception e) {
759        return new StreamEvent.Error("Failed to parse stream event", e);
760      }
761    }
762
763    private StreamEvent buildDoneEvent() {
764      var content = contentBuilder.toString();
765      var calls = toolCalls.isEmpty() ? List.<ToolCall>of() : List.copyOf(toolCalls);
766
767      var finishReason = mapStatus(responseStatus);
768      if (!calls.isEmpty() && finishReason != FinishReason.TOOL_CALLS) {
769        finishReason = FinishReason.TOOL_CALLS;
770      }
771
772      Response.Usage usage = null;
773      if (inputTokens > 0 || outputTokens > 0 || cachedInputTokens > 0 || cacheWriteTokens > 0) {
774        // OpenAI's wire shape reports input_tokens as TOTAL and input_tokens_details.cached_tokens
775        // / cache_write_tokens as SUBSETS. The Helios canonical shape is disjoint — every token in
776        // exactly one class — so we subtract both here. Bounded by zero in case the server ever
777        // reports subsets exceeding the total (would indicate a server-side accounting bug; we'd
778        // rather under-report uncached than synthesize a negative count). cache_write_tokens is
779        // populated on gpt-5.6+ where writes bill at a premium; zero elsewhere.
780        var uncachedInput = Math.max(0, inputTokens - cachedInputTokens - cacheWriteTokens);
781        usage = Response.Usage.of(uncachedInput, outputTokens, cacheWriteTokens, cachedInputTokens);
782      }
783
784      String thinking = reasoningBuilder.isEmpty() ? null : reasoningBuilder.toString();
785
786      var metadata = new HashMap<String, String>();
787      if (thinking != null) {
788        metadata.put(REASONING_KEY, thinking);
789      }
790
791      var response =
792          Response.newBuilder()
793              .withContent(content)
794              .withToolCalls(calls)
795              .withFinishReason(finishReason)
796              .withUsage(usage)
797              .withThinking(thinking)
798              .withMetadata(metadata.isEmpty() ? Map.of() : Map.copyOf(metadata))
799              .build();
800
801      return new StreamEvent.Done(response);
802    }
803
804    @Override
805    public void close() {
806      done = true;
807      readExecutor.shutdownNow();
808      try {
809        rawStream.close();
810      } catch (IOException ignored) {
811      }
812      try {
813        reader.close();
814      } catch (IOException ignored) {
815      }
816    }
817
818    private record ToolCallAccumulator(String callId, String name, StringBuilder jsonBuilder) {}
819  }
820}