Class: Ocak::StreamParser
- Inherits:
-
Object
- Object
- Ocak::StreamParser
- Defined in:
- lib/ocak/stream_parser.rb
Overview
Parses NDJSON lines from ‘claude –output-format stream-json`.
Constant Summary collapse
- TEST_CMD_PATTERN =
%r{ \b(rails\stest|bin/rails\stest|rspec|npm\stest| npx\svitest|cargo\stest|pytest|go\stest|mix\stest| rubocop|biome|clippy|eslint)\b }x
Instance Attribute Summary collapse
-
#cost_usd ⇒ Object
readonly
Returns the value of attribute cost_usd.
-
#duration_ms ⇒ Object
readonly
Returns the value of attribute duration_ms.
-
#files_edited ⇒ Object
readonly
Returns the value of attribute files_edited.
-
#num_turns ⇒ Object
readonly
Returns the value of attribute num_turns.
-
#result_text ⇒ Object
readonly
Returns the value of attribute result_text.
Instance Method Summary collapse
- #full_output ⇒ Object
-
#initialize(agent_name, logger) ⇒ StreamParser
constructor
A new instance of StreamParser.
- #parse_line(line) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(agent_name, logger) ⇒ StreamParser
Returns a new instance of StreamParser.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ocak/stream_parser.rb', line 16 def initialize(agent_name, logger) @agent_name = agent_name @logger = logger @result_text = nil @cost_usd = nil @duration_ms = nil @num_turns = nil @success = nil @files_edited = [] @pending_tools = {} @full_text_blocks = [] end |
Instance Attribute Details
#cost_usd ⇒ Object (readonly)
Returns the value of attribute cost_usd.
14 15 16 |
# File 'lib/ocak/stream_parser.rb', line 14 def cost_usd @cost_usd end |
#duration_ms ⇒ Object (readonly)
Returns the value of attribute duration_ms.
14 15 16 |
# File 'lib/ocak/stream_parser.rb', line 14 def duration_ms @duration_ms end |
#files_edited ⇒ Object (readonly)
Returns the value of attribute files_edited.
14 15 16 |
# File 'lib/ocak/stream_parser.rb', line 14 def files_edited @files_edited end |
#num_turns ⇒ Object (readonly)
Returns the value of attribute num_turns.
14 15 16 |
# File 'lib/ocak/stream_parser.rb', line 14 def num_turns @num_turns end |
#result_text ⇒ Object (readonly)
Returns the value of attribute result_text.
14 15 16 |
# File 'lib/ocak/stream_parser.rb', line 14 def result_text @result_text end |
Instance Method Details
#full_output ⇒ Object
29 30 31 |
# File 'lib/ocak/stream_parser.rb', line 29 def full_output @full_text_blocks.join("\n") end |
#parse_line(line) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ocak/stream_parser.rb', line 37 def parse_line(line) stripped = line.strip return [] if stripped.empty? data = JSON.parse(stripped) case data['type'] when 'system' then parse_system(data) when 'assistant' then parse_assistant(data) when 'user' then parse_user(data) when 'result' then parse_result(data) else [] end rescue JSON::ParserError => e @logger.debug("Failed to parse stream JSON line: #{e.}") [] end |
#success? ⇒ Boolean
33 34 35 |
# File 'lib/ocak/stream_parser.rb', line 33 def success? @success == true end |