Class: Ocak::StreamParser

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_usdObject (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_msObject (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_editedObject (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_turnsObject (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_textObject (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_outputObject



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.message}")
  []
end

#success?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/ocak/stream_parser.rb', line 33

def success?
  @success == true
end