Record Class TextDeltaEvent

java.lang.Object
java.lang.Record
com.tnsai.events.TextDeltaEvent
Record Components:
eventId - unique identifier for this event
timestamp - when the event occurred
messageId - identifier for the complete message this chunk belongs to
content - the text chunk content
role - the message role (typically "assistant")
isFinal - true if this is the final chunk of the message
All Implemented Interfaces:
TnsAIEvent

public record TextDeltaEvent(String eventId, Instant timestamp, String messageId, String content, String role, boolean isFinal) extends Record implements TnsAIEvent
Event representing a streaming text chunk from the LLM.

TextDeltaEvent is emitted during streaming responses, allowing real-time display of generated text.

Example usage for real-time display:


 agent.chatWithEvents("Tell me a story")
     .subscribe(event -> {
         if (event instanceof TextDeltaEvent delta) {
             System.out.print(delta.content());
             System.out.flush();
         }
     });
 
  • Constructor Details

    • TextDeltaEvent

      public TextDeltaEvent(String eventId, Instant timestamp, String messageId, String content, String role, boolean isFinal)
      Creates an instance of a TextDeltaEvent record class.
      Parameters:
      eventId - the value for the eventId record component
      timestamp - the value for the timestamp record component
      messageId - the value for the messageId record component
      content - the value for the content record component
      role - the value for the role record component
      isFinal - the value for the isFinal record component
  • Method Details

    • of

      public static TextDeltaEvent of(String messageId, String content)
      Creates a text delta event for an assistant message.
      Parameters:
      messageId - the message identifier
      content - the text chunk
      Returns:
      new TextDeltaEvent
    • finalChunk

      public static TextDeltaEvent finalChunk(String messageId, String content)
      Creates a final text delta event.
      Parameters:
      messageId - the message identifier
      content - the final text chunk
      Returns:
      new TextDeltaEvent with isFinal=true
    • of

      public static TextDeltaEvent of(String messageId, String content, String role)
      Creates a text delta event with custom role.
      Parameters:
      messageId - the message identifier
      content - the text chunk
      role - the message role
      Returns:
      new TextDeltaEvent
    • 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 TnsAIEvent
      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 TnsAIEvent
      Returns:
      the value of the timestamp record component
    • messageId

      public String messageId()
      Returns the value of the messageId record component.
      Returns:
      the value of the messageId record component
    • content

      public String content()
      Returns the value of the content record component.
      Returns:
      the value of the content record component
    • role

      public String role()
      Returns the value of the role record component.
      Returns:
      the value of the role record component
    • isFinal

      public boolean isFinal()
      Returns the value of the isFinal record component.
      Returns:
      the value of the isFinal record component