Class: A2A::Agent::Context
- Inherits:
-
Object
- Object
- A2A::Agent::Context
- Defined in:
- lib/a2a/agent.rb
Overview
Execution context for handler blocks. Provides helper methods so blocks can call store, respond, stream, etc. directly without holding a reference to the env hash.
Instance Method Summary collapse
- #agent_card ⇒ Object
- #execute(&block) ⇒ Object
-
#initialize(env) ⇒ Context
constructor
A new instance of Context.
- #request ⇒ Object
- #respond(result) ⇒ Object
- #store ⇒ Object
-
#stream ⇒ Object
Create an SSE stream for streaming responses.
Constructor Details
#initialize(env) ⇒ Context
Returns a new instance of Context.
74 75 76 |
# File 'lib/a2a/agent.rb', line 74 def initialize(env) @env = env end |
Instance Method Details
#agent_card ⇒ Object
90 91 92 |
# File 'lib/a2a/agent.rb', line 90 def agent_card @env["a2a.agent_card"] end |
#execute(&block) ⇒ Object
78 79 80 |
# File 'lib/a2a/agent.rb', line 78 def execute(&block) instance_exec(@env["a2a.request"], &block) end |
#request ⇒ Object
86 87 88 |
# File 'lib/a2a/agent.rb', line 86 def request @env["a2a.request"] end |
#respond(result) ⇒ Object
94 95 96 |
# File 'lib/a2a/agent.rb', line 94 def respond(result) @env["a2a.result"] = result end |
#store ⇒ Object
82 83 84 |
# File 'lib/a2a/agent.rb', line 82 def store @env["a2a.store"] end |
#stream ⇒ Object
Create an SSE stream for streaming responses.
Automatically selects the right stream type based on the binding:
- JSON-RPC binding -> JsonRpcStream (wraps events in envelopes)
- REST binding -> RestStream (bare JSON events)
The stream is registered on env so the binding middleware returns it as the Rack body. Falcon streams it natively via Protocol::HTTP::Body::Writable — no threads, no polling.
Usage in a handler block:
on "SendStreamingMessage" do |request|
s = stream
Async do
s.event({ "task" => { ... } })
s.event({ "statusUpdate" => { ... } })
s.finish
end
end
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/a2a/agent.rb', line 119 def stream require "a2a/sse" s = if @env["a2a.json_rpc_id"] A2A::SSE::JsonRpcStream.new(json_rpc_id: @env["a2a.json_rpc_id"]) else A2A::SSE::RestStream.new end @env["a2a.stream"] = s s end |