Module: Opencode::ResponseParser
- Defined in:
- lib/opencode/response_parser.rb
Constant Summary collapse
- TERMINAL_STATUSES =
%w[completed error].freeze
- MAX_ARTIFACT_SIZE =
10.megabytes
- ARTIFACT_TOOLS =
%w[write apply_patch].freeze
Class Method Summary collapse
- .extract_artifact_files(response_body) ⇒ Object
- .extract_artifacts_from_messages(messages) ⇒ Object
- .extract_cache_tokens(response_body) ⇒ Object
- .extract_cost(response_body) ⇒ Object
- .extract_error(response_body) ⇒ Object
- .extract_interleaved_parts(response_body) ⇒ Object
- .extract_reasoning(response_body) ⇒ Object
- .extract_text(response_body) ⇒ Object
- .extract_tokens(response_body) ⇒ Object
-
.extract_tool_summary(response_body) ⇒ Object
Terminal-only tool list.
Class Method Details
.extract_artifact_files(response_body) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/opencode/response_parser.rb', line 95 def self.extract_artifact_files(response_body) parts = response_body[:parts] || [] completed_tools = parts.select do |p| p[:type] == "tool" && ARTIFACT_TOOLS.include?(p[:tool]) && p.dig(:state, :status) == "completed" end return [] if completed_tools.empty? files = completed_tools.flat_map { |part| extract_files_from_tool_part(part) } files.uniq { |f| f[:filename] } end |
.extract_artifacts_from_messages(messages) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/opencode/response_parser.rb', line 108 def self.() return [] unless .is_a?(Array) .select { |m| m.dig(:info, :role) == "assistant" } .flat_map { |m| extract_artifact_files(m) } .uniq { |f| f[:filename] } end |
.extract_cache_tokens(response_body) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/opencode/response_parser.rb', line 71 def self.extract_cache_tokens(response_body) tokens = response_body.dig(:info, :tokens) || {} { cache_read: tokens.dig(:cache, :read) || 0, cache_write: tokens.dig(:cache, :write) || 0 } end |
.extract_cost(response_body) ⇒ Object
67 68 69 |
# File 'lib/opencode/response_parser.rb', line 67 def self.extract_cost(response_body) response_body.dig(:info, :cost) end |
.extract_error(response_body) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/opencode/response_parser.rb', line 79 def self.extract_error(response_body) error = response_body.dig(:info, :error) return nil unless error.is_a?(Hash) { name: error[:name], message: error.dig(:data, :message), status_code: error.dig(:data, :statusCode), retryable: error.dig(:data, :isRetryable), url: error.dig(:data, :metadata, :url) }.compact end |
.extract_interleaved_parts(response_body) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/opencode/response_parser.rb', line 34 def self.extract_interleaved_parts(response_body) parts = response_body[:parts] || [] parts.filter_map do |part| case part[:type] when "text" { "type" => "text", "content" => part[:text] } when "reasoning" { "type" => "reasoning", "content" => part[:text] } when "tool" status = part.dig(:state, :status) next unless status.in?(TERMINAL_STATUSES) build_tool_summary(part) else nil end end end |
.extract_reasoning(response_body) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/opencode/response_parser.rb', line 13 def self.extract_reasoning(response_body) parts = response_body[:parts] || [] reasoning = parts .select { |p| p[:type] == "reasoning" } .map { |p| p[:text] } .join("\n\n") reasoning.presence end |
.extract_text(response_body) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/opencode/response_parser.rb', line 5 def self.extract_text(response_body) parts = response_body[:parts] || [] parts .select { |p| p[:type] == "text" } .map { |p| p[:text] } .join("\n\n") end |
.extract_tokens(response_body) ⇒ Object
63 64 65 |
# File 'lib/opencode/response_parser.rb', line 63 def self.extract_tokens(response_body) response_body.dig(:info, :tokens) end |
.extract_tool_summary(response_body) ⇒ Object
Terminal-only tool list. Returned as canonical string-keyed hashes (same shape ‘extract_interleaved_parts` returns) so callers do not have to know which path produced the data.
27 28 29 30 31 32 |
# File 'lib/opencode/response_parser.rb', line 27 def self.extract_tool_summary(response_body) parts = response_body[:parts] || [] parts .select { |p| p[:type] == "tool" && p.dig(:state, :status).in?(TERMINAL_STATUSES) } .map { |p| build_tool_summary(p) } end |