Class: A2A::Server::Events::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/server/events/event_queue.rb

Overview

Event object for the event queue system

Represents an event that can be published to and consumed from an event queue. Events can be Task objects, Message objects, or status/artifact update events.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, data:, id: nil) ⇒ Event

Initialize a new event

Parameters:

  • type (String)

    The event type (e.g., 'task', 'message', 'task_status_update')

  • data (Object)

    The event data (Task, Message, or update event object)

  • id (String, nil) (defaults to: nil)

    Optional event ID (generated if not provided)



21
22
23
24
25
26
# File 'lib/a2a/server/events/event_queue.rb', line 21

def initialize(type:, data:, id: nil)
  @type = type
  @data = data
  @timestamp = Time.now.utc
  @id = id || SecureRandom.uuid
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



13
14
15
# File 'lib/a2a/server/events/event_queue.rb', line 13

def data
  @data
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/a2a/server/events/event_queue.rb', line 13

def id
  @id
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



13
14
15
# File 'lib/a2a/server/events/event_queue.rb', line 13

def timestamp
  @timestamp
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/a2a/server/events/event_queue.rb', line 13

def type
  @type
end

Instance Method Details

#context_idString?

Get the context ID from the event data if available

Returns:

  • (String, nil)

    The context ID or nil if not available



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/a2a/server/events/event_queue.rb', line 76

def context_id
  case @data
  when A2A::Types::Task
    @data.context_id
  when A2A::Types::TaskStatusUpdateEvent, A2A::Types::TaskArtifactUpdateEvent
    @data.context_id
  when A2A::Types::Message
    @data.context_id
  else
    nil
  end
end

#message_event?Boolean

Check if this is a message event

Returns:

  • (Boolean)

    True if the event is a message



53
54
55
# File 'lib/a2a/server/events/event_queue.rb', line 53

def message_event?
  @type == "message"
end

#task_event?Boolean

Check if this is a task-related event

Returns:

  • (Boolean)

    True if the event is task-related



45
46
47
# File 'lib/a2a/server/events/event_queue.rb', line 45

def task_event?
  %w[task task_status_update task_artifact_update].include?(@type)
end

#task_idString?

Get the task ID from the event data if available

Returns:

  • (String, nil)

    The task ID or nil if not available



61
62
63
64
65
66
67
68
69
70
# File 'lib/a2a/server/events/event_queue.rb', line 61

def task_id
  case @data
  when A2A::Types::Task
    @data.id
  when A2A::Types::TaskStatusUpdateEvent, A2A::Types::TaskArtifactUpdateEvent
    @data.task_id
  else
    nil
  end
end

#to_hHash

Convert event to hash representation

Returns:

  • (Hash)

    Hash representation of the event



32
33
34
35
36
37
38
39
# File 'lib/a2a/server/events/event_queue.rb', line 32

def to_h
  {
    id: @id,
    type: @type,
    data: @data.respond_to?(:to_h) ? @data.to_h : @data,
    timestamp: @timestamp.iso8601
  }
end