Restore chat history
This commit is contained in:
		@@ -113,29 +113,39 @@ end
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
function M.update_content(content, is_new_chat, is_thinking)
 | 
					function M.update_content(content, is_new_chat, is_thinking)
 | 
				
			||||||
    vim.api.nvim_buf_set_option(state.bufnr, 'modifiable', true)
 | 
					    vim.api.nvim_buf_set_option(state.bufnr, 'modifiable', true)
 | 
				
			||||||
 | 
					 | 
				
			||||||
    local lines = vim.split(content, "\n")
 | 
					    local lines = vim.split(content, "\n")
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    -- Only add separator if:
 | 
					 | 
				
			||||||
    -- 1. Not the first message
 | 
					 | 
				
			||||||
    -- 2. Not a thinking message
 | 
					 | 
				
			||||||
    -- 3. There's existing content in the buffer
 | 
					 | 
				
			||||||
    if not is_thinking and not is_new_chat and vim.api.nvim_buf_line_count(state.bufnr) > 0 then
 | 
					 | 
				
			||||||
        local separator = "━━━━━━━━━━━━━━━━━━━━━━━━━━"
 | 
					 | 
				
			||||||
        table.insert(lines, 1, separator)
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if is_new_chat then
 | 
					    if is_thinking then
 | 
				
			||||||
        -- If it's a new chat but we have history, append to existing content
 | 
					        -- For thinking message, just replace/append without affecting history
 | 
				
			||||||
        if #state.conversation_history > 0 and not is_thinking then
 | 
					 | 
				
			||||||
            local current_lines = vim.api.nvim_buf_get_lines(state.bufnr, 0, -1, false)
 | 
					 | 
				
			||||||
            for i = #current_lines, 1, -1 do
 | 
					 | 
				
			||||||
                table.insert(lines, 1, current_lines[i])
 | 
					 | 
				
			||||||
            end
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
        vim.api.nvim_buf_set_lines(state.bufnr, 0, -1, false, lines)
 | 
					        vim.api.nvim_buf_set_lines(state.bufnr, 0, -1, false, lines)
 | 
				
			||||||
    else
 | 
					    else
 | 
				
			||||||
        vim.api.nvim_buf_set_lines(state.bufnr, -1, -1, false, lines)
 | 
					        if is_new_chat then
 | 
				
			||||||
 | 
					            -- For new messages, show the complete history
 | 
				
			||||||
 | 
					            local display_lines = {}
 | 
				
			||||||
 | 
					            for i, msg in ipairs(state.conversation_history) do
 | 
				
			||||||
 | 
					                if i > 1 then  -- Add separator before messages (except first)
 | 
				
			||||||
 | 
					                    table.insert(display_lines, "━━━━━━━━━━━━━━━━━━━━━━━━━━")
 | 
				
			||||||
 | 
					                end
 | 
				
			||||||
 | 
					                local msg_lines = vim.split(msg.content, "\n")
 | 
				
			||||||
 | 
					                for _, line in ipairs(msg_lines) do
 | 
				
			||||||
 | 
					                    table.insert(display_lines, line)
 | 
				
			||||||
 | 
					                end
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					            -- Add the new content
 | 
				
			||||||
 | 
					            if #state.conversation_history > 0 then
 | 
				
			||||||
 | 
					                table.insert(display_lines, "━━━━━━━━━━━━━━━━━━━━━━━━━━")
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					            for _, line in ipairs(lines) do
 | 
				
			||||||
 | 
					                table.insert(display_lines, line)
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					            vim.api.nvim_buf_set_lines(state.bufnr, 0, -1, false, display_lines)
 | 
				
			||||||
 | 
					        else
 | 
				
			||||||
 | 
					            -- For subsequent messages in the same chat
 | 
				
			||||||
 | 
					            if #state.conversation_history > 0 then
 | 
				
			||||||
 | 
					                table.insert(lines, 1, "━━━━━━━━━━━━━━━━━━━━━━━━━━")
 | 
				
			||||||
 | 
					            end
 | 
				
			||||||
 | 
					            vim.api.nvim_buf_set_lines(state.bufnr, -1, -1, false, lines)
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    vim.api.nvim_buf_set_option(state.bufnr, 'modifiable', false)
 | 
					    vim.api.nvim_buf_set_option(state.bufnr, 'modifiable', false)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user