类 Event

java.lang.Object
io.agentscope.core.agent.Event

public class Event extends Object
An event emitted during streaming agent execution.

Events provide a structured way to observe agent execution, with clear type identification and completion status for each message.

Example usage:


 agent.stream(userMsg, options)
     .subscribe(event -> {
         switch (event.getType()) {
             case REASONING -> {
                 if (event.isLast()) {
                     System.out.println("✓ Reasoning complete");
                 } else {
                     System.out.print("...");  // Progress indicator
                 }
             }
             case TOOL_RESULT -> {
                 System.out.println("Tool: " + event.getMessage().getTextContent());
             }
         }
     });
 
  • 构造器详细资料

    • Event

      public Event(EventType type, Msg message, boolean isLast)
      Creates a new event.
      参数:
      type - The event type (REASONING, TOOL_RESULT, etc.)
      message - The message content
      isLast - Whether this is the last/complete message for this event
  • 方法详细资料

    • getType

      public EventType getType()
      Get the event type.

      Use this to determine what kind of message this is and how to process it.

      返回:
      The event type
    • getMessage

      public Msg getMessage()
      Get the message content.

      The message contains the actual data - inspect Msg.getRole(), Msg.getContent(), and other fields for details.

      返回:
      The message
    • isLast

      public boolean isLast()
      Check if this is the last/complete message for this event.

      Return values:

      • true: Complete message or final chunk. Safe to persist, display as final, or trigger downstream processing.
      • false: Intermediate chunk. More events with the same message ID will follow. Useful for real-time UI updates.

      Streaming behavior: For streaming events (e.g., LLM streaming output), multiple events will be emitted with the same Msg.getId():

       Event(type=REASONING, msg(id="abc", content=[...]), isLast=false)
       Event(type=REASONING, msg(id="abc", content=[...]), isLast=false)
       Event(type=REASONING, msg(id="abc", content=[...]), isLast=true)  ← Final
       

      Non-streaming behavior: For non-streaming events, there will be only one event and isLast is always true.

      Example usage:

      
       agent.stream(userMsg, options)
           .subscribe(event -> {
               if (event.isLast()) {
                   // Complete message - safe to persist or process
                   database.save(event.getMessage());
               } else {
                   // Intermediate chunk - update UI
                   ui.append(event.getMessage().getTextContent());
               }
           });
       
      返回:
      true if this is the last chunk, false if more chunks will follow
    • getMessageId

      public String getMessageId()
      Get the message ID (delegates to Msg.getId()).

      Events with the same message ID are parts of the same logical message. Use this to group streaming chunks together.

      返回:
      The message ID
    • toString

      public String toString()
      覆盖:
      toString 在类中 Object