10
11
12
13
14
15
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
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/llm_gateway/adapters/openai/responses/stream_mapper.rb', line 10
def map(chunk)
queued_event = shift_queued_event
return queued_event if queued_event
event_type = chunk[:event]
data = chunk[:data] || {}
raise_stream_error!(data) if event_type == "error" || data[:error] || data[:type] == "error"
case event_type
when "response.created"
stash_response(data[:response])
nil
when "response.output_item.added"
map_output_item_added(data)
when "response.output_item.done"
map_output_item_done(data)
when "response.content_part.added"
map_content_part_added(data)
when "response.content_part.done", "response.output_text.done"
map_text_done(data)
when "response.output_text.delta"
AssistantStreamEvent.new(
type: :text_delta,
content_index: content_index_for(data[:output_index] || 0),
delta: data[:delta] || ""
)
when "response.function_call_arguments.delta"
AssistantStreamEvent.new(
type: :tool_delta,
content_index: content_index_for(data[:output_index] || 0),
delta: data[:delta] || ""
)
when "response.function_call_arguments.done"
map_tool_done(data)
when "response.reasoning_summary_text.delta"
output_index = data[:output_index] || 0
mark_reasoning_has_content(output_index)
AssistantStreamReasoningEvent.new(
type: :reasoning_delta,
content_index: content_index_for(output_index),
delta: data[:delta] || "",
signature: ""
)
when "response.completed"
map_response_completed(data[:response])
else
nil
end
end
|