Restore chat history

This commit is contained in:
Jonas Widen 2025-03-16 20:07:35 +01:00
parent 1f8fd2c270
commit 38aca36dfa

View File

@ -127,20 +127,29 @@ function M.update_content(content, is_new_chat, is_thinking)
if i > 1 then -- Add separator before messages (except first)
table.insert(display_lines, "━━━━━━━━━━━━━━━━━━━━━━━━━━")
end
-- Add prefix based on role
local prefix = msg.role == "user" and "User: " or "Assistant: "
local first_line = true
local msg_lines = vim.split(msg.content, "\n")
for _, line in ipairs(msg_lines) do
table.insert(display_lines, line)
if first_line then
table.insert(display_lines, prefix .. line)
first_line = false
else
table.insert(display_lines, line)
end
end
end
-- Set the buffer content
vim.api.nvim_buf_set_lines(state.bufnr, 0, -1, false, display_lines)
if #display_lines > 0 then
vim.api.nvim_buf_set_lines(state.bufnr, 0, -1, false, display_lines)
end
end
vim.api.nvim_buf_set_option(state.bufnr, 'modifiable', false)
-- Scroll to show new content
local line_count = vim.api.nvim_buf_line_count(state.bufnr)
vim.api.nvim_win_set_cursor(state.winnr, {1, 0})
vim.cmd('normal! zt')
vim.cmd('wincmd p')
@ -159,6 +168,8 @@ end
-- Add functions to manage conversation history
function M.add_message(role, content)
-- Remove any existing prefix from the content
content = content:gsub("^User: ", ""):gsub("^Assistant: ", "")
table.insert(state.conversation_history, {role = role, content = content})
end