@@ -6,6 +6,7 @@ class StreamAccumulator
6
6
attr_reader :content , :model_id , :tool_calls
7
7
8
8
def initialize
9
+ #@content = nil
9
10
@content = +''
10
11
@tool_calls = { }
11
12
@input_tokens = 0
@@ -21,6 +22,7 @@ def add(chunk)
21
22
accumulate_tool_calls chunk . tool_calls
22
23
else
23
24
@content << ( chunk . content || '' )
25
+ # accumulate_content(chunk.content)
24
26
end
25
27
26
28
count_tokens chunk
@@ -30,6 +32,7 @@ def add(chunk)
30
32
def to_message ( response )
31
33
Message . new (
32
34
role : :assistant ,
35
+ #content: final_content,
33
36
content : content . empty? ? nil : content ,
34
37
model_id : model_id ,
35
38
tool_calls : tool_calls_from_stream ,
@@ -41,6 +44,53 @@ def to_message(response)
41
44
42
45
private
43
46
47
+ def accumulate_content ( new_content )
48
+ return unless new_content
49
+
50
+ if @content . nil?
51
+ @content = new_content . is_a? ( String ) ? +new_content : new_content
52
+ else
53
+ case [ @content . class , new_content . class ]
54
+ when [ String , String ]
55
+ @content << new_content
56
+ when [ String , Content ]
57
+ # Convert accumulated string to Content and merge
58
+ @content = Content . new ( @content )
59
+ merge_content ( new_content )
60
+ when [ Content , String ]
61
+ # Append string to existing Content's text
62
+ @content . instance_variable_set ( :@text , ( @content . text || '' ) + new_content )
63
+ when [ Content , Content ]
64
+ merge_content ( new_content )
65
+ end
66
+ end
67
+ end
68
+
69
+ def merge_content ( new_content )
70
+ # Merge text
71
+ current_text = @content . text || ''
72
+ new_text = new_content . text || ''
73
+ @content . instance_variable_set ( :@text , current_text + new_text )
74
+
75
+ # Merge attachments
76
+ new_content . attachments . each do |attachment |
77
+ @content . attach ( attachment )
78
+ end
79
+ end
80
+
81
+ def final_content
82
+ case @content
83
+ when nil
84
+ nil
85
+ when String
86
+ @content . empty? ? nil : @content
87
+ when Content
88
+ @content . text . nil? && @content . attachments . empty? ? nil : @content
89
+ else
90
+ @content
91
+ end
92
+ end
93
+
44
94
def tool_calls_from_stream
45
95
tool_calls . transform_values do |tc |
46
96
arguments = if tc . arguments . is_a? ( String ) && !tc . arguments . empty?
0 commit comments