Class AbstractPart
java.lang.Object
uno.anahata.asi.agi.event.BasicPropertyChangeSource
uno.anahata.asi.agi.message.AbstractPart
- All Implemented Interfaces:
PropertyChangeSource, Rebindable
- Direct Known Subclasses:
AbstractToolCall, BlobPart, TextPart
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 Summary
FieldsModifier and TypeFieldDescriptionprivate booleanPersistent UI state indicating if this part's panel is expanded in the conversation view.private IntegerAn explicit, instance-level override for the maximum depth this part should remain in the active context.private AbstractMessageA backward reference to the Message that contains this part.private StringIf an AI provider produced this part and assigned an Id to it (e.g. an openai item id).private StringAn optional reason for why this part was pruned.private PruningStateThe pruning and pinning state of this part.private longA unique, sequential identifier assigned to this part when it is added to a agi.private IntegerThe cached token count of this part under the currently selected model.Fields inherited from class BasicPropertyChangeSource
propertyChangeSupport -
Constructor Summary
ConstructorsConstructorDescriptionAbstractPart(@NonNull AbstractMessage message) Constructs a new AbstractPart. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidHook for subclasses to inject specialized metadata into the part header.abstract StringasText()Returns the content of the part as a simple string.protected abstract voidPerforms the lazy, model-specific token calculation for this part.Creates a standardized text header containing metadata for this part.getAgi()Gets the parent agi session.Gets the agi configuration.protected abstract intTemplate method hook for subclasses to provide their specific default max depth policy.final intThe definitive method for resolving the max depth policy for this part.intCalculates the total "effective" tokens contributed by this part.protected StringReturns the identity label for the metadata header (e.g., "Part ID: 45").intCalculates the token count of the metadata header for this part.Returns a summarized string representation of the part's content to be used as a "Hint" when the part is effectively pruned.intCalculates the remaining depth before this part is auto-pruned.intRetrieves the cached token count for this part.booleanisAuto()Checks if this part is managed automatically.booleanCalculates the EFFECTIVE pruned state of this part.booleanisPinned()Checks if this part is explicitly pinned.booleanisPruned()Checks if this part is explicitly pruned.voidremove()Removes this part from its parent message and severs the bidirectional link.voidResets the cached token count, forcing a lazy, background recalculation on the next call to getTokenCount().voidsetExpanded(boolean expanded) Sets the expanded state and fires a property change event.voidsetPruningState(PruningState pruningState) Sets the pruning state of this part and fires a property change event.voidsetPruningState(PruningState pruningState, String reason) Sets the pruning state of this part with an optional reason.voidsetTokenCount(int tokenCount) Sets the token count and fires a property change event.Methods inherited from class BasicPropertyChangeSource
addPropertyChangeListener, addPropertyChangeListener, rebind, removePropertyChangeListener, removePropertyChangeListenerMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface PropertyChangeSource
getPropertyChangeSupport
-
Field Details
-
sequentialId
private long sequentialIdA unique, sequential identifier assigned to this part when it is added to a agi. -
providerId
If an AI provider produced this part and assigned an Id to it (e.g. an openai item id). -
message
A backward reference to the Message that contains this part. -
pruningState
The pruning and pinning state of this part. -
prunedReason
An optional reason for why this part was pruned. -
maxDepth
An explicit, instance-level override for the maximum depth this part should remain in the active context. Ifnull, the effective value is determined by the part type's default, resolved via thegetDefaultMaxDepth()template method. -
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 expandedPersistent UI state indicating if this part's panel is expanded in the conversation view.
-
-
Constructor Details
-
AbstractPart
Constructs a new AbstractPart.- Parameters:
message- The parent message.
-
-
Method Details
-
setPruningState
Sets the pruning state of this part and fires a property change event.- Parameters:
pruningState- The new pruning state.
-
setPruningState
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:
trueif the part is pinned.
-
isAuto
public boolean isAuto()Checks if this part is managed automatically.- Returns:
trueif the part is in AUTO state.
-
isPruned
public boolean isPruned()Checks if this part is explicitly pruned.- Returns:
trueif 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:
trueif the part is effectively pruned,falseotherwise.
-
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 explicitmaxDepthis set on the instance.- Returns:
- The default maximum depth for this part type.
-
getAgi
Gets the parent agi session.- Returns:
- The agi session, or null if not attached to a message.
-
getAgiConfig
Gets the agi configuration.- Returns:
- The agi configuration, or null if not attached to a agi.
-
asText
Returns the content of the part as a simple string. This is implemented by subclasses.- Returns:
- The text representation of the part.
-
getPrunedHint
-
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
Returns the identity label for the metadata header (e.g., "Part ID: 45").- Returns:
- The identity label.
-
appendMetadata
Hook for subclasses to inject specialized metadata into the part header.- Parameters:
sb- The StringBuilder building the header.
-

