Kward

Module: Kward::ModelStreamParser

Defined in:
lib/kward/model/stream_parser.rb

Overview

Parses streaming provider responses into Kward assistant messages.

ModelStreamParser is intentionally provider-shape focused and side-effect light: it accumulates assistant text, reasoning summaries, tool calls, and normalized usage from SSE events. Network IO, retries, credentials, and telemetry stay in Client; frontend rendering stays in CLI/RPC event handlers. Keep parser methods deterministic and easy to unit test with raw response bodies.

Class Method Summary collapse

Class Method Details

.active_codex_item(state) ⇒ Object



427
428
429
430
# File 'lib/kward/model/stream_parser.rb', line 427

def active_codex_item(state)
  key = state[:active_item_id]
  key ? state[:items_by_id][key] : nil
end

.active_or_known_codex_item(state, item) ⇒ Object



412
413
414
415
416
417
418
419
420
421
# File 'lib/kward/model/stream_parser.rb', line 412

def active_or_known_codex_item(state, item)
  key = codex_item_key(item)
  known = state[:items_by_id][key]
  return known if known

  active = active_codex_item(state)
  return active if active && (!codex_item_has_stable_key?(item) || codex_item_key(active) == key)

  nil
end

.anthropic_sse_error(data, request_error_class: nil) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/kward/model/stream_parser.rb', line 157

def anthropic_sse_error(data, request_error_class: nil)
  if request_error_class
    request_error_class.new(provider: "Anthropic", code: 400, body: data)
  else
    data
  end
end

.anthropic_sse_message(state) ⇒ Object



179
180
181
182
183
184
185
# File 'lib/kward/model/stream_parser.rb', line 179

def anthropic_sse_message(state)
  message = { "role" => "assistant", "content" => state[:content] }
  message["reasoning_summary"] = state[:reasoning_summary] unless state[:reasoning_summary].empty?
  message["tool_calls"] = state[:tool_calls] unless state[:tool_calls].empty?
  message["usage"] = state[:usage] if state[:usage]
  message
end

.anthropic_sse_stateObject



104
105
106
# File 'lib/kward/model/stream_parser.rb', line 104

def anthropic_sse_state
  { content: +"", reasoning_summary: +"", blocks: {}, tool_calls: [], usage: nil }
end

.anthropic_tool_call(block_data) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/kward/model/stream_parser.rb', line 165

def anthropic_tool_call(block_data)
  return nil unless block_data.is_a?(Hash) && block_data["type"] == "tool_use"

  name = from_claude_code_tool_name(block_data["name"])
  arguments = block_data["partial_json"].to_s
  arguments = JSON.dump(block_data["input"] || {}) if arguments.empty?
  arguments = "{}" if arguments.empty?
  {
    "id" => block_data["id"] || "call_#{name}",
    "type" => "function",
    "function" => { "name" => name, "arguments" => arguments }
  }
end

.codex_content_part_added(state, part) ⇒ Object



304
305
306
307
308
309
310
311
312
# File 'lib/kward/model/stream_parser.rb', line 304

def codex_content_part_added(state, part)
  item = active_codex_item(state)
  return unless item&.fetch("type", nil) == "message" && part.is_a?(Hash)
  return unless ["output_text", "text", "refusal"].include?(part["type"])

  item["content"] ||= []
  item["content"] << deep_dup_hash(part)
  state[:current_text_content_part] = item["content"].last
end

.codex_item_has_stable_key?(item) ⇒ Boolean

Returns:

  • (Boolean)


423
424
425
# File 'lib/kward/model/stream_parser.rb', line 423

def codex_item_has_stable_key?(item)
  item.key?("id") || item.key?("call_id")
end

.codex_item_key(item) ⇒ Object



443
444
445
# File 'lib/kward/model/stream_parser.rb', line 443

def codex_item_key(item)
  item["id"] || item["call_id"] || "item_#{item.object_id}"
end

.codex_message_phase(item) ⇒ Object



518
519
520
# File 'lib/kward/model/stream_parser.rb', line 518

def codex_message_phase(item)
  item["phase"].to_s
end

.codex_output_item_added(state, item) ⇒ Object



293
294
295
296
297
298
299
300
301
302
# File 'lib/kward/model/stream_parser.rb', line 293

def codex_output_item_added(state, item)
  return unless item.is_a?(Hash)

  item = deep_dup_hash(item)
  item["content"] = [] if item["type"] == "message" && !item["content"].is_a?(Array)
  item["summary"] = [] if item["type"] == "reasoning" && !item["summary"].is_a?(Array)
  remember_codex_item(state, item)
  state[:active_item_id] = codex_item_key(item)
  state[:current_text_content_part] = nil
end

.codex_output_item_done(state, item, on_assistant_delta: nil, on_reasoning_delta: nil) ⇒ Object



378
379
380
381
382
383
384
385
386
# File 'lib/kward/model/stream_parser.rb', line 378

def codex_output_item_done(state, item, on_assistant_delta: nil, on_reasoning_delta: nil)
  return unless item.is_a?(Hash)

  item = merge_codex_item(active_or_known_codex_item(state, item), item)
  remember_codex_item(state, item)
  collect_codex_item_output(state, item, on_assistant_delta: on_assistant_delta, on_reasoning_delta: on_reasoning_delta)
  state[:active_item_id] = nil if state[:active_item_id] == codex_item_key(item)
  state[:current_text_content_part] = nil
end

.codex_output_text_delta(state, delta, on_assistant_delta: nil) ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/kward/model/stream_parser.rb', line 314

def codex_output_text_delta(state, delta, on_assistant_delta: nil)
  text = delta.to_s
  return if text.empty?

  item = active_codex_item(state)
  if item&.fetch("type", nil) == "message"
    item["content"] ||= [{ "type" => "output_text", "text" => +"" }]
    part = state[:current_text_content_part] || item["content"].last
    part = item["content"].last unless part.is_a?(Hash)
    part["type"] ||= "output_text"
    text_key = part["type"] == "refusal" ? "refusal" : "text"
    part[text_key] = part[text_key].to_s + text
  end
  state[:raw_content] << text
end

.codex_reasoning_delta(state, delta, on_reasoning_delta: nil) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/kward/model/stream_parser.rb', line 338

def codex_reasoning_delta(state, delta, on_reasoning_delta: nil)
  text = delta.to_s
  return if text.empty?

  item = active_codex_item(state)
  if item&.fetch("type", nil) == "reasoning"
    item["summary"] ||= []
    item["summary"] << { "type" => "summary_text", "text" => +"" } if item["summary"].empty?
    item["summary"].last["text"] = item["summary"].last["text"].to_s + text
  end
  state[:reasoning_summary] << text
  on_reasoning_delta&.call(text)
end

.codex_reasoning_part_done(state, on_reasoning_delta: nil) ⇒ Object



352
353
354
355
356
357
358
359
360
361
# File 'lib/kward/model/stream_parser.rb', line 352

def codex_reasoning_part_done(state, on_reasoning_delta: nil)
  item = active_codex_item(state)
  return unless item&.fetch("type", nil) == "reasoning"
  return if item["summary"].to_a.empty?

  text = "\n\n"
  item["summary"].last["text"] = item["summary"].last["text"].to_s + text
  state[:reasoning_summary] << text
  on_reasoning_delta&.call(text)
end

.codex_reasoning_summary_part_added(state, part) ⇒ Object



330
331
332
333
334
335
336
# File 'lib/kward/model/stream_parser.rb', line 330

def codex_reasoning_summary_part_added(state, part)
  item = active_codex_item(state)
  return unless item&.fetch("type", nil) == "reasoning" && part.is_a?(Hash)

  item["summary"] ||= []
  item["summary"] << deep_dup_hash(part)
end

.codex_response_items(state) ⇒ Object



522
523
524
# File 'lib/kward/model/stream_parser.rb', line 522

def codex_response_items(state)
  state[:response_item_keys].filter_map { |key| state[:items_by_id][key] }
end

.codex_sse_error(event, request_error_class: nil) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/kward/model/stream_parser.rb', line 276

def codex_sse_error(event, request_error_class: nil)
  response = event["response"]
  error = event["error"] || (response["error"] if response.is_a?(Hash)) || {}
  message = if error.is_a?(Hash)
              [error["code"], error["message"]].compact.join(": ")
            else
              error.to_s
            end
  message = event["type"].to_s if message.empty?
  code = error.is_a?(Hash) && error["code"].to_s == "server_error" ? 500 : 400
  if request_error_class
    request_error_class.new(provider: "Codex", code: code, body: "#{event["type"]}: #{message}")
  else
    "#{event["type"]}: #{message}"
  end
end

.codex_sse_message(state) ⇒ Object



484
485
486
487
488
489
490
491
492
# File 'lib/kward/model/stream_parser.rb', line 484

def codex_sse_message(state)
  message = { "role" => "assistant", "content" => codex_visible_content(state) }
  message["reasoning_summary"] = state[:reasoning_summary] unless state[:reasoning_summary].empty?
  message["tool_calls"] = state[:tool_calls] unless state[:tool_calls].empty?
  response_items = codex_response_items(state)
  message["response_items"] = response_items unless response_items.empty?
  message["usage"] = state[:usage] if state[:usage]
  message
end

.codex_sse_stateObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/kward/model/stream_parser.rb', line 221

def codex_sse_state
  {
    content: +"",
    raw_content: +"",
    emitted_message_keys: [],
    reasoning_summary: +"",
    tool_calls: [],
    response_item_keys: [],
    items_by_id: {},
    active_item_id: nil,
    current_text_content_part: nil,
    usage: nil
  }
end

.codex_streamable_message_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


514
515
516
# File 'lib/kward/model/stream_parser.rb', line 514

def codex_streamable_message_item?(item)
  codex_message_phase(item) == "final_answer"
end

.codex_tool_arguments_delta(state, delta) ⇒ Object



363
364
365
366
367
368
369
# File 'lib/kward/model/stream_parser.rb', line 363

def codex_tool_arguments_delta(state, delta)
  item = active_codex_item(state)
  return unless item && ["function_call", "custom_tool_call"].include?(item["type"])

  key = item["type"] == "custom_tool_call" ? "input" : "arguments"
  item[key] = item[key].to_s + delta.to_s
end

.codex_tool_arguments_done(state, arguments) ⇒ Object



371
372
373
374
375
376
# File 'lib/kward/model/stream_parser.rb', line 371

def codex_tool_arguments_done(state, arguments)
  item = active_codex_item(state)
  return unless item&.fetch("type", nil) == "function_call"

  item["arguments"] = arguments.to_s
end

.codex_tool_call(item) ⇒ Object



526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/kward/model/stream_parser.rb', line 526

def codex_tool_call(item)
  return nil unless item.is_a?(Hash) && ["function_call", "custom_tool_call"].include?(item["type"])

  name = item["name"].to_s
  return nil if name.empty?

  arguments = item["arguments"] || item["input"] || "{}"
  arguments = JSON.dump(arguments) unless arguments.is_a?(String)
  {
    "id" => (item["call_id"] || item["id"] || "call_#{name}"),
    "type" => "function",
    "function" => { "name" => name, "arguments" => arguments }
  }
end

.codex_visible_content(state) ⇒ Object



494
495
496
497
498
499
500
501
502
503
# File 'lib/kward/model/stream_parser.rb', line 494

def codex_visible_content(state)
  return state[:content] unless state[:content].empty?

  response_items = codex_response_items(state)
  visible_text = text_from_codex_items(visible_codex_message_items(response_items, tool_calls: state[:tool_calls]))
  return visible_text unless visible_text.empty?
  return "" unless state[:tool_calls].empty?

  state[:raw_content]
end

.collect_codex_item_output(state, item, on_assistant_delta: nil, on_reasoning_delta: nil) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/kward/model/stream_parser.rb', line 388

def collect_codex_item_output(state, item, on_assistant_delta: nil, on_reasoning_delta: nil)
  case item["type"]
  when "message"
    text = text_from_codex_items([item])
    return if text.empty? || !codex_streamable_message_item?(item)

    state[:content] << text
    key = codex_item_key(item)
    unless state[:emitted_message_keys].include?(key)
      on_assistant_delta&.call(text)
      state[:emitted_message_keys] << key
    end
  when "reasoning"
    text = reasoning_summary_from_codex_items([item])
    if state[:reasoning_summary].empty? && !text.empty?
      state[:reasoning_summary] << text
      on_reasoning_delta&.call(text)
    end
  when "function_call", "custom_tool_call"
    tool_call = codex_tool_call(item)
    state[:tool_calls] << tool_call if tool_call && !state[:tool_calls].any? { |call| call["id"] == tool_call["id"] }
  end
end

.deep_dup(value) ⇒ Object



471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/kward/model/stream_parser.rb', line 471

def deep_dup(value)
  case value
  when Hash
    value.each_with_object({}) { |(key, entry), result| result[key] = deep_dup(entry) }
  when Array
    value.map { |entry| deep_dup(entry) }
  when String
    value.dup
  else
    value
  end
end

.deep_dup_hash(hash) ⇒ Object



467
468
469
# File 'lib/kward/model/stream_parser.rb', line 467

def deep_dup_hash(hash)
  deep_dup(hash)
end

.finalized_streaming_tool_calls(tool_calls) ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/kward/model/stream_parser.rb', line 211

def finalized_streaming_tool_calls(tool_calls)
  tool_calls.compact.each_with_index.map do |tool_call, index|
    tool_call["id"] ||= "call_#{index}"
    tool_call["type"] ||= "function"
    tool_call["function"] ||= { "name" => "", "arguments" => "{}" }
    tool_call["function"]["arguments"] = "{}" if tool_call["function"]["arguments"].to_s.empty?
    tool_call
  end
end

.from_claude_code_tool_name(name) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/kward/model/stream_parser.rb', line 187

def from_claude_code_tool_name(name)
  case name.to_s
  when "Read" then "read_file"
  when "Write" then "write_file"
  when "Edit" then "edit_file"
  when "Bash" then "run_shell_command"
  when "WebSearch" then "web_search"
  when "WebFetch" then "fetch_content"
  when "AskUserQuestion" then "ask_user_question"
  else name.to_s
  end
end

.merge_codex_item(existing, update) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/kward/model/stream_parser.rb', line 447

def merge_codex_item(existing, update)
  return deep_dup_hash(update) unless existing.is_a?(Hash)

  merged = deep_dup_hash(existing)
  update.each do |key, value|
    next if value.nil?

    merged[key] = if key == "content" && merged[key].is_a?(Array) && value.is_a?(Array)
                    value.empty? ? merged[key] : value
                  elsif key == "summary" && merged[key].is_a?(Array) && value.is_a?(Array)
                    value.empty? ? merged[key] : value
                  elsif key == "arguments" && value.to_s.empty? && !merged[key].to_s.empty?
                    merged[key]
                  else
                    deep_dup(value)
                  end
  end
  merged
end

.merge_streaming_tool_call(tool_calls, delta) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/kward/model/stream_parser.rb', line 200

def merge_streaming_tool_call(tool_calls, delta)
  index = (delta["index"] || tool_calls.length).to_i
  tool_calls[index] ||= { "id" => nil, "type" => "function", "function" => { "name" => "", "arguments" => "" } }
  current = tool_calls[index]
  current["id"] = delta["id"] if delta["id"]
  current["type"] = delta["type"] if delta["type"]
  function = delta["function"] || {}
  current["function"]["name"] << function["name"].to_s if function["name"]
  current["function"]["arguments"] << function["arguments"].to_s if function["arguments"]
end

.parse_anthropic_sse_stream(response, on_reasoning_delta: nil, on_assistant_delta: nil, cancellation: nil, usage_normalizer: nil, request_error_class: nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/kward/model/stream_parser.rb', line 84

def parse_anthropic_sse_stream(response, on_reasoning_delta: nil, on_assistant_delta: nil, cancellation: nil, usage_normalizer: nil, request_error_class: nil)
  state = anthropic_sse_state
  buffer = +""
  response.read_body do |chunk|
    cancellation&.raise_if_cancelled!
    buffer << chunk
    while (index = buffer.index(/\r?\n\r?\n/))
      delimiter = Regexp.last_match[0]
      block = buffer[0...index]
      buffer = buffer[(index + delimiter.length)..] || +""
      process_anthropic_sse_block(block, state, on_reasoning_delta: on_reasoning_delta, on_assistant_delta: on_assistant_delta, usage_normalizer: usage_normalizer, request_error_class: request_error_class)
    end
  end
  cancellation&.raise_if_cancelled!
  process_anthropic_sse_block(buffer, state, on_reasoning_delta: on_reasoning_delta, on_assistant_delta: on_assistant_delta, usage_normalizer: usage_normalizer, request_error_class: request_error_class) unless buffer.empty?
  anthropic_sse_message(state)
rescue JSON::ParserError => e
  raise "Anthropic returned invalid SSE JSON: #{e.message}"
end

.parse_codex_sse(body, on_reasoning_delta: nil, on_assistant_delta: nil, usage_normalizer: nil, request_error_class: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/kward/model/stream_parser.rb', line 48

def parse_codex_sse(body, on_reasoning_delta: nil, on_assistant_delta: nil, usage_normalizer: nil, request_error_class: nil)
  state = codex_sse_state
  body.split(/\r?\n\r?\n/).each do |block|
    process_codex_sse_block(block, state, on_reasoning_delta: on_reasoning_delta, on_assistant_delta: on_assistant_delta, usage_normalizer: usage_normalizer, request_error_class: request_error_class)
  end
  codex_sse_message(state)
rescue JSON::ParserError => e
  raise "Codex OAuth returned invalid SSE JSON: #{e.message}"
end

.parse_codex_sse_stream(response, on_reasoning_delta: nil, on_assistant_delta: nil, cancellation: nil, usage_normalizer: nil, request_error_class: nil) ⇒ Object

Incrementally parses a Codex/Responses SSE HTTP response body.

Deltas are yielded as soon as complete SSE blocks arrive so interactive frontends can render streamed assistant and reasoning text without waiting for the provider to close the response.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kward/model/stream_parser.rb', line 63

def parse_codex_sse_stream(response, on_reasoning_delta: nil, on_assistant_delta: nil, cancellation: nil, usage_normalizer: nil, request_error_class: nil)
  state = codex_sse_state
  buffer = +""

  response.read_body do |chunk|
    cancellation&.raise_if_cancelled!
    buffer << chunk
    while (index = buffer.index(/\r?\n\r?\n/))
      delimiter = Regexp.last_match[0]
      block = buffer[0...index]
      buffer = buffer[(index + delimiter.length)..] || +""
      process_codex_sse_block(block, state, on_reasoning_delta: on_reasoning_delta, on_assistant_delta: on_assistant_delta, usage_normalizer: usage_normalizer, request_error_class: request_error_class)
    end
  end
  cancellation&.raise_if_cancelled!
  process_codex_sse_block(buffer, state, on_reasoning_delta: on_reasoning_delta, on_assistant_delta: on_assistant_delta, usage_normalizer: usage_normalizer, request_error_class: request_error_class) unless buffer.empty?
  codex_sse_message(state)
rescue JSON::ParserError => e
  raise "Codex OAuth returned invalid SSE JSON: #{e.message}"
end

.parse_openai_chat_sse(body, on_assistant_delta: nil, usage_normalizer: nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kward/model/stream_parser.rb', line 16

def parse_openai_chat_sse(body, on_assistant_delta: nil, usage_normalizer: nil)
  content = +""
  tool_calls = []
  usage = nil
  body.split(/\r?\n\r?\n/).each do |block|
    data = block.lines.filter_map { |line| line.start_with?("data:") ? line.delete_prefix("data:").strip : nil }.join("\n")
    next if data.empty? || data == "[DONE]"

    event = JSON.parse(data)
    usage ||= usage_normalizer&.call(event["usage"])
    choice = Array(event["choices"]).first || {}
    delta = choice["delta"] || {}
    if delta["content"]
      text = delta["content"].to_s
      content << text
      on_assistant_delta&.call(text)
    end
    Array(delta["tool_calls"]).each do |tool_call|
      merge_streaming_tool_call(tool_calls, tool_call)
    end
    message = choice["message"] || {}
    content << message["content"].to_s if content.empty? && message["content"]
    Array(message["tool_calls"]).each { |tool_call| merge_streaming_tool_call(tool_calls, tool_call) }
  end
  result = { "role" => "assistant", "content" => content }
  result["tool_calls"] = finalized_streaming_tool_calls(tool_calls) unless tool_calls.empty?
  result["usage"] = usage if usage
  result
rescue JSON::ParserError => e
  raise "Copilot returned invalid SSE JSON: #{e.message}"
end

.process_anthropic_sse_block(block, state, on_reasoning_delta: nil, on_assistant_delta: nil, usage_normalizer: nil, request_error_class: nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/kward/model/stream_parser.rb', line 108

def process_anthropic_sse_block(block, state, on_reasoning_delta: nil, on_assistant_delta: nil, usage_normalizer: nil, request_error_class: nil)
  event_name = nil
  data = block.lines.filter_map do |line|
    event_name = line.delete_prefix("event:").strip if line.start_with?("event:")
    line.start_with?("data:") ? line.delete_prefix("data:").strip : nil
  end.join("\n")
  return if data.empty? || data == "[DONE]"
  raise anthropic_sse_error(data, request_error_class: request_error_class) if event_name == "error"

  event = JSON.parse(data)
  case event["type"]
  when "message_start"
    state[:usage] ||= usage_normalizer&.call(event.dig("message", "usage"))
  when "content_block_start"
    block_data = event["content_block"] || {}
    state[:blocks][event["index"].to_i] = block_data.merge("partial_json" => "")
    if block_data["type"] == "text"
      text = block_data["text"].to_s
      state[:content] << text
      on_assistant_delta&.call(text) unless text.empty?
    elsif block_data["type"] == "thinking"
      text = block_data["thinking"].to_s
      state[:reasoning_summary] << text
      on_reasoning_delta&.call(text) unless text.empty?
    end
  when "content_block_delta"
    current = state[:blocks][event["index"].to_i] ||= { "partial_json" => "" }
    delta = event["delta"] || {}
    case delta["type"]
    when "text_delta"
      text = delta["text"].to_s
      state[:content] << text
      on_assistant_delta&.call(text)
    when "thinking_delta"
      text = delta["thinking"].to_s
      state[:reasoning_summary] << text
      on_reasoning_delta&.call(text)
    when "input_json_delta"
      current["partial_json"] = current["partial_json"].to_s + delta["partial_json"].to_s
    end
  when "content_block_stop"
    block_data = state[:blocks][event["index"].to_i]
    tool_call = anthropic_tool_call(block_data)
    state[:tool_calls] << tool_call if tool_call
  when "message_delta"
    state[:usage] = usage_normalizer&.call(event["usage"]) || state[:usage]
  end
end

.process_codex_sse_block(block, state, on_reasoning_delta: nil, on_assistant_delta: nil, usage_normalizer: nil, request_error_class: nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/kward/model/stream_parser.rb', line 236

def process_codex_sse_block(block, state, on_reasoning_delta: nil, on_assistant_delta: nil, usage_normalizer: nil, request_error_class: nil)
  data = block.lines.filter_map { |line| line.start_with?("data:") ? line.delete_prefix("data:").strip : nil }.join("\n")
  return if data.empty? || data == "[DONE]"

  event = JSON.parse(data)
  case event["type"]
  when "response.output_item.added"
    codex_output_item_added(state, event["item"])
  when "response.content_part.added"
    codex_content_part_added(state, event["part"])
  when "response.output_text.delta", "response.refusal.delta"
    codex_output_text_delta(state, event["delta"], on_assistant_delta: on_assistant_delta)
  when "response.reasoning_summary_part.added"
    codex_reasoning_summary_part_added(state, event["part"])
  when "response.reasoning_summary_text.delta"
    codex_reasoning_delta(state, event["delta"], on_reasoning_delta: on_reasoning_delta)
  when "response.reasoning_summary_part.done"
    codex_reasoning_part_done(state, on_reasoning_delta: on_reasoning_delta)
  when "response.reasoning_text.delta"
    codex_reasoning_delta(state, event["delta"], on_reasoning_delta: on_reasoning_delta)
  when "response.function_call_arguments.delta", "response.custom_tool_call_input.delta"
    codex_tool_arguments_delta(state, event["delta"])
  when "response.function_call_arguments.done"
    codex_tool_arguments_done(state, event["arguments"])
  when "response.output_item.done"
    codex_output_item_done(state, event["item"], on_assistant_delta: on_assistant_delta, on_reasoning_delta: on_reasoning_delta)
  when "response.completed"
    response = event["response"]
    state[:usage] ||= usage_normalizer&.call(response["usage"]) if response.is_a?(Hash)
    state[:usage] ||= usage_normalizer&.call(event["usage"])
    if response.is_a?(Hash) && response["output"].is_a?(Array) && state[:response_item_keys].empty?
      response["output"].each do |item|
        codex_output_item_done(state, item, on_assistant_delta: on_assistant_delta, on_reasoning_delta: on_reasoning_delta)
      end
    end
  when "response.failed", "response.incomplete"
    raise codex_sse_error(event, request_error_class: request_error_class)
  end
end

.reasoning_summary_from_codex_items(items) ⇒ Object



555
556
557
558
559
560
561
562
563
564
565
# File 'lib/kward/model/stream_parser.rb', line 555

def reasoning_summary_from_codex_items(items)
  items.flat_map do |item|
    next [] unless item.is_a?(Hash)

    if item["type"] == "reasoning" && item["summary"].is_a?(Array)
      item["summary"].filter_map { |part| part["text"] if part.is_a?(Hash) }
    else
      []
    end
  end.join
end

.remember_codex_item(state, item) ⇒ Object



432
433
434
435
436
437
438
439
440
441
# File 'lib/kward/model/stream_parser.rb', line 432

def remember_codex_item(state, item)
  key = codex_item_key(item)
  if state[:items_by_id].key?(key)
    stored = state[:items_by_id][key]
    stored.replace(merge_codex_item(stored, item))
  else
    state[:items_by_id][key] = item
    state[:response_item_keys] << key
  end
end

.text_from_codex_items(items) ⇒ Object



541
542
543
544
545
546
547
548
549
550
551
552
553
# File 'lib/kward/model/stream_parser.rb', line 541

def text_from_codex_items(items)
  items.flat_map do |item|
    next [] unless item.is_a?(Hash)

    if ["output_text", "text"].include?(item["type"])
      item["text"].to_s
    elsif item["type"] == "message" && item["content"].is_a?(Array)
      item["content"].filter_map { |part| part["text"] if part.is_a?(Hash) && ["output_text", "text"].include?(part["type"]) }
    else
      []
    end
  end.join
end

.visible_codex_message_items(items, tool_calls: []) ⇒ Object



505
506
507
508
509
510
511
512
# File 'lib/kward/model/stream_parser.rb', line 505

def visible_codex_message_items(items, tool_calls: [])
  messages = Array(items).select { |item| item.is_a?(Hash) && item["type"] == "message" }
  final_messages = messages.select { |item| codex_message_phase(item) == "final_answer" }
  return final_messages unless final_messages.empty?
  return [] unless tool_calls.empty?

  messages.reject { |item| codex_message_phase(item) == "commentary" }
end