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(json_rpc_id: 1)

Async do
  stream.event({ "task" => { ... } })
  stream.finish
end

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

Constant Summary

Constants inherited from Stream

Stream::SSE_HEADERS

Instance Method Summary collapse

Methods inherited from Stream

#finish, headers

Constructor Details

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

Returns a new instance of JsonRpcStream.



24
25
26
27
# File 'lib/a2a/sse/json_rpc_stream.rb', line 24

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”)



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

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