Class AbstractPart

All Implemented Interfaces:
PropertyChangeSource, Rebindable
Direct Known Subclasses:
AbstractToolCall, BlobPart, TextPart

public abstract class AbstractPart extends BasicPropertyChangeSource
The foundational component of an AbstractMessage, representing a discrete block of multimodal content.

This class is central to the V2 context management system, implementing self-contained logic for intelligent, depth-based pruning and explicit pinning. It maintains its own token count and environmental awareness through its parent message.

Author:
anahata
  • Field Details

    • sequentialId

      private long sequentialId
      A unique, sequential identifier assigned to this part when it is added to a agi.
    • providerId

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

      private AbstractMessage message
      A backward reference to the Message that contains this part.
    • pruningState

      private PruningState pruningState
      The pruning and pinning state of this part.
    • prunedReason

      private String prunedReason
      An optional reason for why this part was pruned.
    • maxDepth

      private Integer maxDepth
      An explicit, instance-level override for the maximum depth this part should remain in the active context. If null, the effective value is determined by the part type's default, resolved via the getDefaultMaxDepth() template method.
    • tokenCount

      private Integer tokenCount
      The cached token count of this part under the currently selected model. A value of null indicates that the token cost needs to be lazily recalculated.
    • expanded

      private boolean expanded
      Persistent UI state indicating if this part's panel is expanded in the conversation view.
  • Constructor Details

    • AbstractPart

      public AbstractPart(@NonNull @NonNull AbstractMessage message)
      Constructs a new AbstractPart.
      Parameters:
      message - The parent message.
  • Method Details

    • setPruningState

      public void setPruningState(PruningState pruningState)
      Sets the pruning state of this part and fires a property change event.
      Parameters:
      pruningState - The new pruning state.
    • setPruningState

      public void setPruningState(PruningState pruningState, String reason)
      Sets the pruning state of this part with an optional reason.
      Parameters:
      pruningState - The new pruning state.
      reason - The reason for pruning.
    • setTokenCount

      public void setTokenCount(int tokenCount)
      Sets the token count and fires a property change event.
      Parameters:
      tokenCount - The new token count.
    • getTokenCount

      public int getTokenCount()
      Retrieves the cached token count for this part. If the value has not yet been calculated (is null), it triggers an on-the-fly calculation using the active model's tokenizer.
      Returns:
      The calculated token count, or 0 if no model is active.
    • calculateTokenCount

      protected abstract void calculateTokenCount()
      Performs the lazy, model-specific token calculation for this part. This is implemented by subclasses to perform the actual calculation.
    • resetTokenCount

      public void resetTokenCount()
      Resets the cached token count, forcing a lazy, background recalculation on the next call to getTokenCount().
    • setExpanded

      public void setExpanded(boolean expanded)
      Sets the expanded state and fires a property change event.
      Parameters:
      expanded - The new expanded state.
    • remove

      public void remove()
      Removes this part from its parent message and severs the bidirectional link.
      Throws:
      IllegalStateException - if the part is not attached to a message.
    • isPinned

      public boolean isPinned()
      Checks if this part is explicitly pinned.
      Returns:
      true if the part is pinned.
    • isAuto

      public boolean isAuto()
      Checks if this part is managed automatically.
      Returns:
      true if the part is in AUTO state.
    • isPruned

      public boolean isPruned()
      Checks if this part is explicitly pruned.
      Returns:
      true if the part is PRUNED.
    • isEffectivelyPruned

      public boolean isEffectivelyPruned()
      Calculates the EFFECTIVE pruned state of this part. A part is effectively pruned if it was explicitly pruned or if its time-to-live has expired (while in AUTO state).
      Returns:
      true if the part is effectively pruned, false otherwise.
    • getRemainingDepth

      public int getRemainingDepth()
      Calculates the remaining depth before this part is auto-pruned.
      Returns:
      The remaining depth, or a large positive number for indefinite retention.
    • getEffectiveMaxDepth

      public final int getEffectiveMaxDepth()
      The definitive method for resolving the max depth policy for this part. It follows the Template Method pattern, first checking for an explicit instance-level override before falling back to the subclass-specific default.
      Returns:
      The effective maximum depth for this part.
    • getDefaultMaxDepth

      protected abstract int getDefaultMaxDepth()
      Template method hook for subclasses to provide their specific default max depth policy. This is the fallback value used when no explicit maxDepth is set on the instance.
      Returns:
      The default maximum depth for this part type.
    • getAgi

      public Agi getAgi()
      Gets the parent agi session.
      Returns:
      The agi session, or null if not attached to a message.
    • getAgiConfig

      public AgiConfig getAgiConfig()
      Gets the agi configuration.
      Returns:
      The agi configuration, or null if not attached to a agi.
    • asText

      public abstract String asText()
      Returns the content of the part as a simple string. This is implemented by subclasses.
      Returns:
      The text representation of the part.
    • getPrunedHint

      public String getPrunedHint()
      Returns a summarized string representation of the part's content to be used as a "Hint" when the part is effectively pruned. Defaults to a formatted version of asText().
      Returns:
      The pruned hint string.
    • createMetadataHeader

      public String createMetadataHeader()
      Creates a standardized text header containing metadata for this part. This is used for in-band metadata injection to improve model self-awareness. If the part is effectively pruned, it includes a descriptive hint to maintain semantic context.
      Returns:
      A formatted metadata header string.
    • getMetadataTokenCount

      public int getMetadataTokenCount()
      Calculates the token count of the metadata header for this part.
      Returns:
      The token count of the metadata header.
    • getEffectiveTokenCount

      public int getEffectiveTokenCount()
      Calculates the total "effective" tokens contributed by this part.

      If the part is effectively pruned, this returns only the metadata overhead; otherwise, it returns the full token count plus metadata.

      Returns:
      The effective token count.
    • getIdentityLabel

      protected String getIdentityLabel()
      Returns the identity label for the metadata header (e.g., "Part ID: 45").
      Returns:
      The identity label.
    • appendMetadata

      protected void appendMetadata(StringBuilder sb)
      Hook for subclasses to inject specialized metadata into the part header.
      Parameters:
      sb - The StringBuilder building the header.