Class OpenAiModelMessage

All Implemented Interfaces:
PropertyChangeSource, Rebindable

public class OpenAiModelMessage extends AbstractModelMessage<OpenAiResponse>
Specialized ModelMessage for the OpenAI Responses API.

Aggregates multiple items (messages, reasoning, function calls, web searches, and code execution) from a single Responses API turn into a unified Anahata message. It handles complex multi-item merging and multimodal data harvesting.

Author:
anahata
  • Field Details

    • API_MAPPER

      private static final com.fasterxml.jackson.databind.ObjectMapper API_MAPPER
      Internal Jackson mapper for stream event processing.
    • ENCRYPTED_REASONING_PLACEHOLDER

      public static final String ENCRYPTED_REASONING_PLACEHOLDER
      Placeholder text used for reasoning items in stateless mode when no summary is available.
      See Also:
    • phase

      private String phase
      The generation phase reported by the model (e.g., 'commentary', 'final_answer').
    • toolThoughts

      private final Map<AbstractToolCall<?,?>, ModelTextPart> toolThoughts
      Tracks the association between tool calls and their parent reasoning chains. This is required for OpenAI's strict referential integrity.
    • lastParsedThought

      private transient ModelTextPart lastParsedThought
      Transient tracker for the most recently parsed reasoning item.
  • Constructor Details

    • OpenAiModelMessage

      public OpenAiModelMessage(Agi agi, String modelId)
      Constructs a new OpenAiModelMessage bound to a specific session and model.
      Parameters:
      agi - The parent AGI session.
      modelId - The ID of the model that generated this message.
  • Method Details

    • processItem

      public void processItem(com.fasterxml.jackson.databind.JsonNode item)
      Processes a single item from the OpenAI 'output' array and maps it to the appropriate Anahata parts.

      This includes handling messages, reasoning chains, function calls, web searches, and code interpreter executions.

      Parameters:
      item - The JSON node representing an OpenAI item.
    • processCitations

      private void processCitations(com.fasterxml.jackson.databind.JsonNode annotations)
      Extracts citations from OpenAI annotations and updates the grounding metadata.
      Parameters:
      annotations - The JSON array of annotations from the API.
    • updateGroundingMetadata

      private void updateGroundingMetadata(List<String> queries, List<String> texts, List<GroundingSource> sources, String html, String rawJson)
      Aggregates new metadata components into the message's GroundingMetadata.

      Ensures that citations, supporting texts, and search queries from multiple items are correctly merged and deduped.

      Parameters:
      queries - Suggested search queries.
      texts - Supporting text segments.
      sources - Grounding sources (citations).
      html - The search entry point HTML.
      rawJson - The raw JSON from the provider.
    • handleStreamEvent

      public void handleStreamEvent(com.fasterxml.jackson.databind.JsonNode eventNode)
      Handles a Server-Sent Event (SSE) from the OpenAI Responses API stream.

      Routes deltas for real-time text and reasoning generation, and defers complex items (function calls, web searches) to the completion of the item where the full JSON structure is guaranteed to be intact.

      Parameters:
      eventNode - The parsed JSON node of the stream event.
    • appendContent

      public void appendContent(String text)
      Appends text to the current active text part or creates a new one if needed.
      Parameters:
      text - The text delta to append.
    • appendThoughts

      public void appendThoughts(String text)
      Appends text to the current active reasoning part or creates a new thought part if needed.
      Parameters:
      text - The reasoning delta to append.
    • getFrom

      public String getFrom()
      Gets the identity of the sender of this message. For user messages, this is typically the user's name. For model messages, it's the model ID. For tool messages, it's the identity of the execution context.
      Overrides:
      getFrom in class AbstractModelMessage<OpenAiResponse>
      Returns:
      The sender's identity.
    • getDevice

      public String getDevice()
      Gets the ID of the device where this message was created or processed. This could be a hostname, a JVM ID, or a cloud identifier.
      Overrides:
      getDevice in class AbstractModelMessage<OpenAiResponse>
      Returns:
      The device ID.