@@ -286,57 +286,57 @@ local query = function(buf, provider, payload, handler, on_exit, callback)
286286 for _ , line in ipairs (lines ) do
287287 if line ~= " " and line ~= nil then
288288 qt .raw_response = qt .raw_response .. line .. " \n "
289- end
290- line = line :gsub (" ^data: " , " " )
291- local content = " "
292- if line :match (" choices" ) and line :match (" delta" ) and line :match (" content" ) then
293- line = vim .json .decode (line )
294- if line .choices [1 ] and line .choices [1 ].delta and line .choices [1 ].delta .content then
295- content = line .choices [1 ].delta .content
296- end
297- end
298289
299- if qt .provider == " anthropic" and (line :match (' "text":' ) or line :match (' "thinking"' )) then
300- if line :match (" content_block_start" ) or line :match (" content_block_delta" ) then
290+ line = line :gsub (" ^data: " , " " )
291+ local content = " "
292+ if line and line :match (" choices" ) and line :match (" delta" ) and line :match (" content" ) then
301293 line = vim .json .decode (line )
302- if line .content_block then
303- if line .content_block .type == " thinking" then
304- anthropic_thinking = true
305- content = " <think>"
306- elseif line .content_block .type == " text" and anthropic_thinking then
307- anthropic_thinking = false
308- content = " </think>\n\n "
309- end
294+ if line .choices [1 ] and line .choices [1 ].delta and line .choices [1 ].delta .content then
295+ content = line .choices [1 ].delta .content
310296 end
311- if line .delta then
312- if line .delta .type == " thinking_delta" then
313- content = line .delta .thinking or " "
314- elseif line .delta .type == " text_delta" then
315- content = line .delta .text or " "
297+ end
298+
299+ if qt .provider == " anthropic" and line and (line :match (' "text":' ) or line :match (' "thinking"' )) then
300+ if line :match (" content_block_start" ) or line :match (" content_block_delta" ) then
301+ line = vim .json .decode (line )
302+ if line .content_block then
303+ if line .content_block .type == " thinking" then
304+ anthropic_thinking = true
305+ content = " <think>"
306+ elseif line .content_block .type == " text" and anthropic_thinking then
307+ anthropic_thinking = false
308+ content = " </think>\n\n "
309+ end
310+ end
311+ if line .delta then
312+ if line .delta .type == " thinking_delta" then
313+ content = line .delta .thinking or " "
314+ elseif line .delta .type == " text_delta" then
315+ content = line .delta .text or " "
316+ end
316317 end
317318 end
318319 end
319- end
320320
321- if qt .provider == " googleai" then
322- if line :match (' "text":' ) then
323- content = vim .json .decode (" {" .. line .. " }" ).text
321+ if qt .provider == " googleai" then
322+ if line and line :match (' "text":' ) then
323+ content = vim .json .decode (" {" .. line .. " }" ).text
324+ end
324325 end
325- end
326326
327- if qt .provider == " ollama" then
328- if line :match (' "message":' ) and line :match (' "content":' ) then
329- local success , decoded = pcall (vim .json .decode , line )
330- if success and decoded .message and decoded .message .content then
331- content = decoded .message .content
327+ if qt .provider == " ollama" then
328+ if line and line :match (' "message":' ) and line :match (' "content":' ) then
329+ local success , decoded = pcall (vim .json .decode , line )
330+ if success and decoded .message and decoded .message .content then
331+ content = decoded .message .content
332+ end
332333 end
333334 end
334- end
335-
336335
337- if content and type (content ) == " string" then
338- qt .response = qt .response .. content
339- handler (qid , content )
336+ if content and type (content ) == " string" then
337+ qt .response = qt .response .. content
338+ handler (qid , content )
339+ end
340340 end
341341 end
342342 end
0 commit comments