Class: A2A::Streaming::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/a2a/streaming/response.rb

Constant Summary collapse

PAYLOAD_TYPES =
{
  task: Task,
  message: Message,
  status_update: StatusUpdateEvent,
  artifact_update: ArtifactUpdateEvent
}.freeze
BUILDERS =
{
  "task" => ->(v) { new(:task, Task.from_h(v)) },
  "message" => ->(v) { new(:message, Message.from_h(v)) },
  "statusUpdate" => ->(v) { new(:status_update, StatusUpdateEvent.from_h(v)) },
  "artifactUpdate" => ->(v) { new(:artifact_update, ArtifactUpdateEvent.from_h(v)) }
}.freeze
WIRE_KEYS =
{
  task: "task",
  message: "message",
  status_update: "statusUpdate",
  artifact_update: "artifactUpdate"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, payload) ⇒ Response

Returns a new instance of Response.

Raises:

  • (TypeError)


22
23
24
25
26
27
28
# File 'lib/a2a/streaming/response.rb', line 22

def initialize(type, payload)
  expected = PAYLOAD_TYPES.fetch(type) { raise ArgumentError, "unknown type: #{type.inspect}" }
  raise TypeError, "payload must be a #{expected}" unless payload.is_a?(expected)

  @type = type
  @payload = payload
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



20
21
22
# File 'lib/a2a/streaming/response.rb', line 20

def payload
  @payload
end

#typeObject (readonly)

Returns the value of attribute type.



20
21
22
# File 'lib/a2a/streaming/response.rb', line 20

def type
  @type
end

Class Method Details

.from_h(hash) ⇒ Object

Raises:

  • (ArgumentError)


30
31
32
33
34
35
# File 'lib/a2a/streaming/response.rb', line 30

def self.from_h(hash)
  key, builder = BUILDERS.find { |k, _| hash.key?(k) }
  raise ArgumentError, "unrecognised StreamResponse keys: #{hash.keys.inspect}" unless key

  builder.call(hash[key])
end

Instance Method Details

#artifact_update?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/a2a/streaming/response.rb', line 60

def artifact_update?
  type == :artifact_update
end

#message?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/a2a/streaming/response.rb', line 52

def message?
  type == :message
end

#status_update?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/a2a/streaming/response.rb', line 56

def status_update?
  type == :status_update
end

#task?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/a2a/streaming/response.rb', line 48

def task?
  type == :task
end

#to_hObject



44
45
46
# File 'lib/a2a/streaming/response.rb', line 44

def to_h
  { WIRE_KEYS.fetch(type) => payload.to_h }
end