nomnivore / ollama.nvim

A plugin for managing and integrating your ollama workflows in neovim.
MIT License
311 stars 22 forks source link

Make sense to store response in a file? #24

Open kbwhodat opened 4 months ago

kbwhodat commented 4 months ago

Perhaps it could be a feature toggle so that if someone wants it they could enable it. Sometimes you think you understood something until a couple days pass by and you forgot. I attempted to update the util.handle_stream function. The file path could certainly be configurable. Let me know what you think, i'm not the best coder. It does work just not sure on the performance impact

local accumulated_response = ""
---@param cb fun(body: table, job: Job?)
function util.handle_stream(cb, prompt)
    return function(_, chunk, job)
        vim.schedule(function()
            local _, body = pcall(function()
                return vim.json.decode(chunk)
            end)
            if type(body) ~= "table" or body.response == nil then
                if body.error ~= nil then
                    vim.api.nvim_notify("Error: " .. body.error, vim.log.levels.ERROR, { title = "Ollama" })
                end
                return
            end

            if body.response then
                accumulated_response = accumulated_response .. body.response
            end

            if body.done then
                local seperate = "\n\n------------------------------------------------------------------------------\n\n"
                local message = seperate .. "\nQUESTION:\n\n\n" .. prompt .. "\n\n\nRESPONSE:\n\n\n" .. accumulated_response .. seperate
                local file_path = "/tmp/testing.md"

                local file = io.open(file_path, "a")
                if file then
                    file:write(message)
                    file:close()
                else
                    vim.api.nvim_notify("Failed to open file for writing.", vim.log.levels.ERROR, { title = "Ollama" })
                end
            end
            cb(body, job)
        end)
    end
end