olimorris / codecompanion.nvim

✨ AI-powered coding, seamlessly in Neovim. Supports Anthropic, Copilot, Gemini, Ollama and OpenAI LLMs
MIT License
504 stars 32 forks source link

[Bug]: treesitter.lua:124: str: expected string, got nil #52

Closed codewiz closed 2 months ago

codewiz commented 3 months ago

Your minimal.lua config

local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- Your CodeCompanion setup
local plugins = {
  {
    "olimorris/codecompanion.nvim",
    dependencies = {
      { "nvim-treesitter/nvim-treesitter", build = ":TSUpdate"},
      "nvim-lua/plenary.nvim",
      "nvim-telescope/telescope.nvim",
      {
        "stevearc/dressing.nvim",
        opts = {},
      },
    },
    config = function()
      require("codecompanion").setup({
        strategies = { -- Change the adapters as required
          chat = "openai",
          inline = "openai",
        },
      })
    end,
  }
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

-- setup treesitter
local ok, treesitter = pcall(require, "nvim-treesitter.configs")
if ok then
  treesitter.setup({
    ensure_installed = "all",
    ignore_install = { "phpdoc" }, -- list of parser which cause issues or crashes
    highlight = { enable = true },
  })
end

Error messages

Error detected while processing User Autocommands for "CodeCompanionRequest":
Error executing lua callback: /usr/share/nvim/runtime/lua/vim/treesitter.lua:124: str: expected string, got nil                                                     
stack traceback:                                                                                                                                                    
        [C]: in function 'error'                                                                                                                                    
        vim/shared.lua: in function 'validate'                                                                                                                      
        /usr/share/nvim/runtime/lua/vim/treesitter.lua:124: in function 'get_string_parser'                                                                         
        ...codecompanion.nvim/lua/codecompanion/strategies/chat.lua:246: in function 'run_tools'                                                                    
        ...codecompanion.nvim/lua/codecompanion/strategies/chat.lua:599: in function '_user_on_exit'                                                                
        ...andelwow/.repro/plugins/plenary.nvim/lua/plenary/job.lua:241: in function '_shutdown'                                                                    
        ...andelwow/.repro/plugins/plenary.nvim/lua/plenary/job.lua:221: in function 'shutdown'                                                                     
        .../plugins/codecompanion.nvim/lua/codecompanion/client.lua:31: in function <.../plugins/codecompanion.nvim/lua/codecompanion/client.lua:28>                
        [C]: in function 'nvim_exec_autocmds'                                                                                                                       
        ...codecompanion.nvim/lua/codecompanion/strategies/chat.lua:609: in function 'cb'                                                                           
        .../plugins/codecompanion.nvim/lua/codecompanion/client.lua:115: in function ''                                                                             
        vim/_editor.lua: in function <vim/_editor.lua:0>                                                                                                            

Health check output

codecompanion: require("codecompanion.health").check()

codecompanion.nvim report ~
- Log file: /home/bernie/.local/state/nvim/codecompanion.log
- OK plenary.nvim installed
- OK nvim-treesitter installed
- OK telescope.nvim installed
- OK dressing.nvim installed
- WARNING edgy.nvim not found
- OK curl installed
- OK base64 installed

Log output

[DEBUG] 2024-05-25 18:15:52
Stream Request: { "-sSL", "-D", "/run/user/1000/plenary_curl_b9c14a5b.headers", "--compressed", "-X", "POST", "-H", "Authorization: Bearer REMOVED", "-H", "Content-Type: application/json", "--data-raw", '{"top_p":1,"temperature":1,"presence_penalty":0,"frequency_penalty":0,"messages":[{"role":"user"}],"model":"gpt-4o","stream":true}', "--no-buffer", "--silent", "https://api.openai.com/v1/chat/completions" }
[DEBUG] 2024-05-25 18:15:52
Couldn't parse JSON: {
[DEBUG] 2024-05-25 18:15:52
Couldn't parse JSON:     "error": {
[DEBUG] 2024-05-25 18:15:52
Couldn't parse JSON:         "message": "The model `gpt-4o` does not exist or you do not have access to it.",
[DEBUG] 2024-05-25 18:15:52
Couldn't parse JSON:         "type": "invalid_request_error",
[DEBUG] 2024-05-25 18:15:52
Couldn't parse JSON:         "param": null,
[DEBUG] 2024-05-25 18:15:52
Couldn't parse JSON:         "code": "model_not_found"
[DEBUG] 2024-05-25 18:15:52
Couldn't parse JSON:     }
[DEBUG] 2024-05-25 18:15:52
Couldn't parse JSON: }

Describe the bug

Calling :CodeCompanionChat followed by :w results in the above error.

Tested with nvim built from git head:

NVIM v0.11.0-dev-96+g7994fdba6
Build type: Release
LuaJIT 2.1.1713484068

Also tested with Arch system package:

NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1713773202

Reproduce the bug

  1. /usr/bin/nvim --clean -u minimal.lua
  2. :CodeCompanionChat
  3. :w

Final checks

olimorris commented 3 months ago

I think the answer lies in your log output:

The model `gpt-4o` does not exist or you do not have access to it.

I've just tried putting in my own API key with your minimal.lua file and it works as expected. Can you access gpt-4o outside of CodeCompanion?

codewiz commented 3 months ago

Topping up my OpenAI account with $10 cured the issue :-)

Can we leave this bug open to report API errors in the UI?

olimorris commented 3 months ago

I thought I'd handled that damn out of credit error. It got me last time 😆.

I'll try and handle those other errors better. Thanks for raising this.

olimorris commented 3 months ago

I did implement a hacky way of detecting an error from OpenAI back in 84597e0. The challenge is that they like to stream the error message back to you rather than just giving it as one full response. This means you have to handle { then "error": { then "message": "The model gpt-4o does not exist or you do not have access to it." etc.

On this occasion, it looks like I should have detected we had an error in the response and not allowed the run_tools method to be called and then the adapter would have caught the error and displayed it back to you in the UI.

My credit balance is running low (🤑) so I'll keep this issue open until I am confident I've handled it.

olimorris commented 2 months ago

Closing this as I have been able to confirm this is now handled.