Class: PhaseoAgentSdk::StreamResult
- Inherits:
-
Object
- Object
- PhaseoAgentSdk::StreamResult
- Defined in:
- lib/phaseo_agent_sdk.rb
Instance Method Summary collapse
- #cancel ⇒ Object
- #full_stream ⇒ Object
-
#initialize(&work) ⇒ StreamResult
constructor
A new instance of StreamResult.
- #item_stream ⇒ Object
- #push(event) ⇒ Object
- #reasoning_stream ⇒ Object
- #result ⇒ Object
- #text_stream ⇒ Object
Constructor Details
#initialize(&work) ⇒ StreamResult
Returns a new instance of StreamResult.
35 36 37 38 39 40 |
# File 'lib/phaseo_agent_sdk.rb', line 35 def initialize(&work) @events=[];@mutex=Mutex.new;@condition=ConditionVariable.new;@done=false @worker=Thread.new do begin;@result=work.call(method(:push));rescue Exception=>error;@error=error;ensure;@mutex.synchronize{@done=true;@condition.broadcast};end end end |
Instance Method Details
#cancel ⇒ Object
43 |
# File 'lib/phaseo_agent_sdk.rb', line 43 def cancel;@worker.kill if @worker&.alive?;@mutex.synchronize{@done=true;@condition.broadcast};end |
#full_stream ⇒ Object
44 45 46 |
# File 'lib/phaseo_agent_sdk.rb', line 44 def full_stream Enumerator.new do|yielded|;index=0;loop do;event=nil;finished=false;@mutex.synchronize do;@condition.wait(@mutex) while index>=@events.length&&!@done;event=@events[index] if index<@events.length;index+=1 if event;finished=@done&&event.nil?;end;break if finished;yielded<<event if event;end;raise @error if @error;end end |
#item_stream ⇒ Object
49 |
# File 'lib/phaseo_agent_sdk.rb', line 49 def item_stream = full_stream.lazy.select{|event|event[:type]=="response.item"}.map{|event|event[:item]} |
#push(event) ⇒ Object
41 |
# File 'lib/phaseo_agent_sdk.rb', line 41 def push(event)=@mutex.synchronize{@events<<event;@condition.broadcast} |
#reasoning_stream ⇒ Object
48 |
# File 'lib/phaseo_agent_sdk.rb', line 48 def reasoning_stream = full_stream.lazy.select{|event|event[:type]=="response.reasoning.delta"}.map{|event|event[:delta].to_s} |
#result ⇒ Object
42 |
# File 'lib/phaseo_agent_sdk.rb', line 42 def result;@worker.join;raise @error if @error;@result;end |
#text_stream ⇒ Object
47 |
# File 'lib/phaseo_agent_sdk.rb', line 47 def text_stream = full_stream.lazy.select{|event|event[:type]=="response.output_text.delta"}.map{|event|event[:delta].to_s} |