Module: A2A::Streaming::SSEWriter
- Defined in:
- lib/a2a/streaming/sse_writer.rb
Overview
Server-side counterpart to SseParser. Formats Streaming::Response objects (or raw hashes) as SSE frames ready to write to an HTTP response body.
Class Method Summary collapse
-
.encode(response, id: nil) ⇒ Object
Returns the SSE wire representation of a Streaming::Response as a String.
-
.encode_error(error, id: nil) ⇒ Object
Encodes a JSON-RPC error as an SSE frame.
Class Method Details
.encode(response, id: nil) ⇒ Object
Returns the SSE wire representation of a Streaming::Response as a String. The result includes the trailing blank line that delimits SSE events.
12 13 14 15 16 |
# File 'lib/a2a/streaming/sse_writer.rb', line 12 def self.encode(response, id: nil) payload = response.is_a?(Response) ? response.to_h : response envelope = JSONRPCEnvelope.success(id: id, result: payload) "data: #{JSON.generate(envelope)}\n\n" end |
.encode_error(error, id: nil) ⇒ Object
Encodes a JSON-RPC error as an SSE frame.
19 20 21 22 |
# File 'lib/a2a/streaming/sse_writer.rb', line 19 def self.encode_error(error, id: nil) envelope = JSONRPCEnvelope.error(id: id, error: error) "data: #{JSON.generate(envelope)}\n\n" end |