Record Class OnChunk

java.lang.Object
java.lang.Record
com.tnsai.hooks.events.OnChunk
Record Components:
eventId - ULID-shaped correlation id (stable across chunks of the same turn — use chunkIndex to order)
timestamp - when this chunk arrived
context - surrounding hook context
chunk - the chunk about to be delivered to the handler
chunkIndex - 0-based position in the stream
All Implemented Interfaces:
ChatHookEvent, HookEvent

public record OnChunk(String eventId, Instant timestamp, HookContext context, ChatChunk chunk, long chunkIndex) extends Record implements ChatHookEvent
Fires for each ChatChunk during streaming (issue #60). Cost-sensitive by volume — a chatty streaming response produces hundreds of chunk events per turn. Hooks bound here MUST be fast and non-blocking; anything slow belongs on PostChat.

Hooks may Modify via withChunk(ChatChunk) to transform the streamed content (real-time content filter, truncation, stream replacement).

chunkIndex is zero-based — first chunk of a turn is 0.

Since:
0.5.0 (issue #60)
  • Constructor Details

    • OnChunk

      public OnChunk(String eventId, Instant timestamp, HookContext context, ChatChunk chunk, long chunkIndex)
      Creates an instance of a OnChunk record class.
      Parameters:
      eventId - the value for the eventId record component
      timestamp - the value for the timestamp record component
      context - the value for the context record component
      chunk - the value for the chunk record component
      chunkIndex - the value for the chunkIndex record component
  • Method Details

    • eventType

      public String eventType()
      Description copied from interface: HookEvent
      Short stable string identifier of the event kind, e.g. "PreToolUse". Default implementation derives it from the concrete record's simple class name; concrete records may override if a different naming is desirable.
      Specified by:
      eventType in interface HookEvent
    • withChunk

      public OnChunk withChunk(ChatChunk newChunk)
      Build a copy with newChunk replacing chunk.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • eventId

      public String eventId()
      Returns the value of the eventId record component.
      Specified by:
      eventId in interface HookEvent
      Returns:
      the value of the eventId record component
    • timestamp

      public Instant timestamp()
      Returns the value of the timestamp record component.
      Specified by:
      timestamp in interface HookEvent
      Returns:
      the value of the timestamp record component
    • context

      public HookContext context()
      Returns the value of the context record component.
      Specified by:
      context in interface HookEvent
      Returns:
      the value of the context record component
    • chunk

      public ChatChunk chunk()
      Returns the value of the chunk record component.
      Returns:
      the value of the chunk record component
    • chunkIndex

      public long chunkIndex()
      Returns the value of the chunkIndex record component.
      Returns:
      the value of the chunkIndex record component