Class: A2A::SSE::JsonRpcStream

Inherits:
Stream
  • Object
show all
Defined in:
lib/a2a/sse/json_rpc_stream.rb

Overview

SSE stream that wraps each event in a JSON-RPC 2.0 response envelope.

Per the A2A spec, JSON-RPC streaming returns SSE where each ‘data:` line is a full JSON-RPC response: “jsonrpc”:“2.0”,“id”:N,“result”:{…}

Usage:

stream = A2A::SSE::JsonRpcStream.new(
  task_id: "t1", context_id: "c1", json_rpc_id: 1
)

Async do
  stream.task(status: { state: "TASK_STATE_WORKING", timestamp: "..." })
  stream.finish
end

[200, A2A::SSE::Stream.headers, stream]

Constant Summary

Constants inherited from Stream

Stream::SSE_HEADERS

Instance Attribute Summary

Attributes inherited from Stream

#context_id, #task_id

Instance Method Summary collapse

Methods inherited from Stream

#finish, headers

Constructor Details

#initialize(json_rpc_id:, **options) ⇒ JsonRpcStream

Returns a new instance of JsonRpcStream.



26
27
28
29
# File 'lib/a2a/sse/json_rpc_stream.rb', line 26

def initialize(json_rpc_id:, **options)
  @json_rpc_id = json_rpc_id
  super(**options)
end

Instance Method Details

#event(data, **opts) ⇒ Object

Emit an SSE event wrapped in a JSON-RPC envelope.

Parameters:

  • data (Hash)

    the StreamResponse payload (becomes “result”)



35
36
37
38
39
40
41
42
# File 'lib/a2a/sse/json_rpc_stream.rb', line 35

def event(data, **opts)
  envelope = {
    "jsonrpc" => "2.0",
    "id"      => @json_rpc_id,
    "result"  => data.respond_to?(:to_h) ? data.to_h : data,
  }
  super(envelope, **opts)
end