@@ -68,11 +68,17 @@ def perform_completion!(
6868 feature_context : nil ,
6969 partial_tool_calls : false ,
7070 output_thinking : false ,
71+ cancel_manager : nil ,
7172 &blk
7273 )
7374 LlmQuota . check_quotas! ( @llm_model , user )
7475 start_time = Time . now
7576
77+ if cancel_manager && cancel_manager . cancelled?
78+ # nothing to do
79+ return
80+ end
81+
7682 @forced_json_through_prefill = false
7783 @partial_tool_calls = partial_tool_calls
7884 @output_thinking = output_thinking
@@ -90,6 +96,7 @@ def perform_completion!(
9096 feature_context : feature_context ,
9197 partial_tool_calls : partial_tool_calls ,
9298 output_thinking : output_thinking ,
99+ cancel_manager : cancel_manager ,
93100 )
94101
95102 wrapped = result
@@ -118,6 +125,9 @@ def perform_completion!(
118125 end
119126 end
120127
128+ cancel_manager_callback = nil
129+ cancelled = false
130+
121131 FinalDestination ::HTTP . start (
122132 model_uri . host ,
123133 model_uri . port ,
@@ -126,6 +136,14 @@ def perform_completion!(
126136 open_timeout : TIMEOUT ,
127137 write_timeout : TIMEOUT ,
128138 ) do |http |
139+ if cancel_manager
140+ cancel_manager_callback =
141+ lambda do
142+ cancelled = true
143+ http . finish
144+ end
145+ cancel_manager . add_callback ( cancel_manager_callback )
146+ end
129147 response_data = +""
130148 response_raw = +""
131149
@@ -196,7 +214,6 @@ def perform_completion!(
196214 end
197215
198216 begin
199- cancelled = false
200217 cancel = -> do
201218 cancelled = true
202219 http . finish
@@ -224,8 +241,6 @@ def perform_completion!(
224241 partials . each { |inner_partial | blk . call ( inner_partial , cancel ) }
225242 end
226243 end
227- rescue IOError , StandardError
228- raise if !cancelled
229244 end
230245 if xml_stripper
231246 stripped = xml_stripper . finish
@@ -293,6 +308,12 @@ def perform_completion!(
293308 end
294309 end
295310 end
311+ rescue IOError , StandardError
312+ raise if !cancelled
313+ ensure
314+ if cancel_manager && cancel_manager_callback
315+ cancel_manager . remove_callback ( cancel_manager_callback )
316+ end
296317 end
297318
298319 def final_log_update ( log )
0 commit comments