Class AbstractModelMessage<R extends Response>

Type Parameters:
R - The type of the model response.
All Implemented Interfaces:
PropertyChangeSource, Rebindable
Direct Known Subclasses:
AnthropicMessage, GeminiModelMessage, OpenAiCompatibleModelMessage, OpenAiModelMessage

public abstract class AbstractModelMessage<R extends Response> extends AbstractMessage
Represents a message originating from the AI model. It extends AbstractMessage, sets its role to MODEL, and provides convenience methods for accessing tool calls. In the V2 simplified architecture, this class acts as the atomic unit of a "turn", containing both model content and any resulting tool calls/responses.
Author:
anahata-gemini-pro-2.5
  • Field Details

    • providerId

      private String providerId
      If an AI provider produced this part and assigned an Id to it (e.g. an openai message id).
    • providerUuid

      private String providerUuid
      The UUID of the AbstractAiProvider that generated this message.
    • modelId

      private String modelId
      The ID of the AbstractModel that generated this message.
    • finishReason

      private FinishReason finishReason
      The reason why the model stopped generating content for this candidate.
    • finishMessage

      private String finishMessage
      The message explaining why the model stopped generating content for this candidate.
    • groundingMetadata

      private GroundingMetadata groundingMetadata
      The grounding metadata for the response.
    • safetyRatings

      private String safetyRatings
      The safety ratings for the response, summarized as a string.
    • billedPromptTokens

      private int billedPromptTokens
      The number of billed prompt (input) tokens for this candidate, as reported by the API.
    • billedCompletionTokens

      private int billedCompletionTokens
      The number of billed completion (output) tokens for this candidate, as reported by the API.
    • rawJson

      private String rawJson
      The raw JSON response from the model.
    • citationMetadata

      private String citationMetadata
      The citation metadata for the response, summarized as a string.
    • response

      private R extends Response response
      The response that returned this message.
    • streaming

      private boolean streaming
      Whether the model is currently streaming content for this message.
    • runningAllPending

      private volatile boolean runningAllPending
      Whether a batch execution of all pending tools is actively underway on this message.
    • turnAttributes

      private final Map turnAttributes
      A turn scoped map for tools to store turn-scoped attributes.
  • Constructor Details

    • AbstractModelMessage

      public AbstractModelMessage(@NonNull @NonNull Agi agi, @NonNull @NonNull String modelId)
      Constructs a new AbstractModelMessage.
      Parameters:
      agi - The parent agi session.
      modelId - The ID of the model.
  • Method Details

    • getRole

      public Role getRole()
      Gets the role of the entity that created this message. This is implemented by subclasses to provide compile-time type safety.
      Specified by:
      getRole in class AbstractMessage
      Returns:
      The role of the message creator.
    • 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.
      Specified by:
      getFrom in class AbstractMessage
      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.
      Specified by:
      getDevice in class AbstractMessage
      Returns:
      The device ID.
    • setRawJson

      public void setRawJson(String rawJson)
      Sets the raw JSON representation of the model's response and fires a property change event. This method replaces any existing content.
      Parameters:
      rawJson - The raw JSON string.
    • appendRawJson

      public void appendRawJson(String chunk)
      Appends a raw JSON chunk to the existing content. If multiple chunks are appended, they are automatically wrapped in a JSON array to maintain validity for pretty-printing.
      Parameters:
      chunk - The JSON chunk to append.
    • setBilledPromptTokens

      public void setBilledPromptTokens(int billedPromptTokens)
      Sets the billed prompt token count and fires a property change event.
      Parameters:
      billedPromptTokens - The new billed prompt token count.
    • setBilledCompletionTokens

      public void setBilledCompletionTokens(int billedCompletionTokens)
      Sets the billed completion token count and fires a property change event.
      Parameters:
      billedCompletionTokens - The new billed completion token count.
    • setFinishReason

      public void setFinishReason(FinishReason finishReason)
      Sets the finish reason and fires a property change event.
      Parameters:
      finishReason - The new finish reason.
    • setGroundingMetadata

      public void setGroundingMetadata(GroundingMetadata groundingMetadata)
      Sets the grounding metadata and fires a property change event.
      Parameters:
      groundingMetadata - The new grounding metadata.
    • isEffectivelyPruned

      public boolean isEffectivelyPruned()
      Calculates the EFFECTIVE pruned state of this message. A message is effectively pruned ONLY if it contains no visible (un-pruned) parts. A model message is never effectively pruned while it is streaming.
      Overrides:
      isEffectivelyPruned in class AbstractMessage
      Returns:
      true if the message is effectively pruned.
    • isToolPromptMessage

      public boolean isToolPromptMessage()
      Checks if this model message is the current tool prompt message for the agi.
      Returns:
      true if this message is the tool prompt message.
    • removePart

      public void removePart(AbstractPart part)
      Removes a part from this message and severs the bidirectional link.

      Checks tool prompt completion after a part removal.

      Overrides:
      removePart in class AbstractMessage
      Parameters:
      part - The part to remove.
    • remove

      public void remove()
      Removes this message from the agi history.

      Halts any active batch execution of pending tools before removing the message from history and re-evaluating tool prompt completion.

      Overrides:
      remove in class AbstractMessage
    • onPartPruningStateChanged

      protected void onPartPruningStateChanged(AbstractPart part, PruningState oldState, PruningState newState)
      Lifecycle callback invoked whenever a child part's pruning state changes. Subclasses can override to propagate reactive updates.

      Reacts to pruning state changes on tool calls, broadcasting remainingTools updates and re-evaluating tool prompt completion.

      Overrides:
      onPartPruningStateChanged in class AbstractMessage
      Parameters:
      part - The part whose pruning state changed.
      oldState - The previous pruning state.
      newState - The new pruning state.
    • getToolCalls

      public List<AbstractToolCall<?,?>> getToolCalls()
      Filters and returns only the tool call parts from this message.
      Returns:
      A list of AbstractToolCall parts, or an empty list if none exist.
    • getPendingToolCalls

      public List<AbstractToolCall<?,?>> getPendingToolCalls()
      Filters and returns all tool calls belonging to this model message that are currently awaiting execution in ToolExecutionStatus.PENDING state.

      This method acts as the authoritative domain selector for batch tool dispatching, UI pending counter badges, and human-in-the-loop review actions.

      Returns:
      a list of pending AbstractToolCall instances in this message.
    • getRemainingToolCallsCount

      public int getRemainingToolCallsCount()
      Calculates the number of tool calls in this message that are currently active in the execution queue (either actively EXECUTING or still PENDING).
      Returns:
      The count of remaining tools in the execution pipeline.
    • getToolResponses

      public List<AbstractToolResponse<?>> getToolResponses()
      Returns all tool responses associated with the tool calls in this message.
      Returns:
      A list of tool responses.
    • isAutoRunnable

      public boolean isAutoRunnable()
      Determines if this entire batch of tool calls can be executed automatically without user intervention.
      Returns:
      true if all conditions for automatic execution are met.
    • hasPendingTools

      public boolean hasPendingTools()
      Checks if there are any tool calls with responses in a PENDING state.
      Returns:
      true if at least one tool is pending.
    • executeAllPending

      public boolean executeAllPending()
      Executes all tool calls in this message that are currently in a PENDING state, sequentially checking runningAllPending before starting each tool call. Broadcasts remainingTools count updates for live countdown UI display and automatically collapses successfully executed tools.
      Returns:
      true if all pending tool calls were executed, false if execution was stopped early and tools remain pending.
    • stopRunningAllPending

      public void stopRunningAllPending()
      Signals the sequential tool execution on this message to halt after the currently executing tool finishes, leaving any subsequent unstarted tools in PENDING state.
    • declineAllPending

      public void declineAllPending()
      Sets all tool calls in this message that are currently in a PENDING state to DECLINED and collapses them.
    • addTextPart

      public final TextPart addTextPart(String text)
      Creates and adds a new text part to this message. Creates and adds a ModelTextPart without thought metadata.
      Specified by:
      addTextPart in class AbstractMessage
      Parameters:
      text - The text content.
      Returns:
      The created text part.
    • addBlobPart

      public final BlobPart addBlobPart(String mimeType, byte[] data)
      Creates and adds a new binary data part to this message. Creates and adds a ModelBlobPart without thought metadata.
      Specified by:
      addBlobPart in class AbstractMessage
      Parameters:
      mimeType - The MIME type.
      data - The binary data.
      Returns:
      The created blob part.
    • addBlobPart

      public final ModelBlobPart addBlobPart(Path path) throws Exception
      Creates and adds a new binary data part from a local file path. Creates and adds a ModelBlobPart from a file path, without thought metadata.
      Specified by:
      addBlobPart in class AbstractMessage
      Parameters:
      path - The path to the file.
      Returns:
      The created blob part.
      Throws:
      Exception - if the file cannot be read or the MIME type cannot be detected.
    • addTextPart

      public final ModelTextPart addTextPart(String text, byte[] thoughtSignature, boolean thought)
      Creates and adds a new model text part with thought process metadata.
      Parameters:
      text - The text content.
      thoughtSignature - The thought signature.
      thought - Whether this is a thought part.
      Returns:
      The created model text part.
    • addBlobPart

      public final ModelBlobPart addBlobPart(String mimeType, byte[] data, byte[] thoughtSignature)
      Creates and adds a new model blob part with thought process metadata.
      Parameters:
      mimeType - The MIME type.
      data - The binary data.
      thoughtSignature - The thought signature.
      Returns:
      The created model blob part.
    • appendMetadata

      protected void appendMetadata(StringBuilder sb)
      Hook for subclasses to inject specialized metadata into the message header.
      Overrides:
      appendMetadata in class AbstractMessage
      Parameters:
      sb - The StringBuilder building the header.