Skip to content

Commit 698e1e2

Browse files
committed
fix: fix ollama provider
1 parent c37f154 commit 698e1e2

File tree

2 files changed

+46
-41
lines changed

2 files changed

+46
-41
lines changed

lua/gp/dispatcher.lua

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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

lua/gp/init.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,9 +1913,14 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
19131913
end
19141914

19151915
-- select from first_line to last_line
1916-
vim.api.nvim_win_set_cursor(0, { start + 1, 0 })
1916+
-- validate cursor positions are within buffer bounds
1917+
local buf_line_count = vim.api.nvim_buf_line_count(buf)
1918+
local start_pos = math.min(math.max(start + 1, 1), buf_line_count)
1919+
local finish_pos = math.min(math.max(finish + 1, 1), buf_line_count)
1920+
1921+
vim.api.nvim_win_set_cursor(0, { start_pos, 0 })
19171922
vim.api.nvim_command("normal! V")
1918-
vim.api.nvim_win_set_cursor(0, { finish + 1, 0 })
1923+
vim.api.nvim_win_set_cursor(0, { finish_pos, 0 })
19191924
end
19201925

19211926
-- prepare messages

0 commit comments

Comments
 (0)