Skip to content

feat(cody) sourcegraph cody provider implementation #810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ _See [config.lua#L9](./lua/avante/config.lua) for the full config_

```lua
{
---@alias Provider "claude" | "openai" | "azure" | "gemini" | "cohere" | "copilot" | string
---@alias Provider "claude" | "openai" | "azure" | "gemini" | "cohere" | "copilot" | "cody" | string
provider = "claude", -- Recommend using Claude
auto_suggestions_provider = "claude", -- Since auto-suggestions are a high-frequency operation and therefore expensive, it is recommended to specify an inexpensive provider or even a free provider: copilot
claude = {
Expand Down Expand Up @@ -345,6 +345,11 @@ Given its early stage, `avante.nvim` currently supports the following basic func
> ```sh
> export AZURE_OPENAI_API_KEY=your-api-key
> ```
> For Cody:
>
> ```sh
> export SRC_ACCESS_TOKEN=your-api-key
> ```

1. Open a code file in Neovim.
2. Use the `:AvanteAsk` command to query the AI about the code.
Expand Down
13 changes: 12 additions & 1 deletion lua/avante/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local M = {}
---@class avante.Config
M.defaults = {
debug = false,
---@alias Provider "claude" | "openai" | "azure" | "gemini" | "cohere" | "copilot" | [string]
---@alias Provider "claude" | "openai" | "azure" | "gemini" | "cohere" | "copilot" | "cody" | [string]
provider = "claude", -- Only recommend using Claude
auto_suggestions_provider = "claude",
---@alias Tokenizer "tiktoken" | "hf"
Expand Down Expand Up @@ -74,6 +74,17 @@ M.defaults = {
max_tokens = 4096,
["local"] = false,
},
---@type AvanteSupportedProvider
cody = {
endpoint = "https://sourcegraph.com",
model = "anthropic::2024-10-22::claude-3-5-sonnet-latest",
timeout = 30000,
max_tokens = 4000,
temperature = 0,
stream = true,
topK = -1,
topP = -1,
},
---To add support for custom provider, follow the format below
---See https://github.com/yetone/avante.nvim/wiki#custom-providers for more details
---@type {[string]: AvanteProvider}
Expand Down
82 changes: 82 additions & 0 deletions lua/avante/providers/cody.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
--- Documentation for setting up Sourcegraph Cody
--- Generating an access token: https://sourcegraph.com/docs/cli/how-tos/creating_an_access_token

local P = require("avante.providers")
local Utils = require("avante.utils")

---@class AvanteProviderFunctor
local M = {}

M.api_key_name = "SRC_ACCESS_TOKEN"
M.role_map = {
user = "human",
assistant = "assistant",
system = "system",
}

M.parse_messages = function(opts)
local messages = {
{ role = "system", content = opts.system_prompt },
}
vim
.iter(opts.messages)
:each(function(msg) table.insert(messages, { speaker = M.role_map[msg.role], text = msg.content }) end)
return messages
end

M.parse_response = function(data_stream, event_state, opts)
-- vim.api.nvim_notify(vim.inspect(data_stream) .. "\n\n\n\n", 1, {})
if event_state == "done" then
opts.on_complete()
return
end

if data_stream == nil or data_stream == "" then return end

local json = vim.json.decode(data_stream)
local delta = json.deltaText
local stopReason = json.stopReason

if stopReason == "end_turn" then return end

opts.on_chunk(delta)
end

---@param provider AvanteProviderFunctor
---@param code_opts AvantePromptOptions
---@return table
M.parse_curl_args = function(provider, code_opts)
local base, body_opts = P.parse_config(provider)

local api_key = provider.parse_api_key()
if api_key == nil then
-- if no api key is available, make a request with a empty api key.
api_key = ""
end

local headers = {
["Content-Type"] = "application/json",
["Authorization"] = "token " .. api_key,
}

return {
url = Utils.trim(base.endpoint, { suffix = "/" })
.. "/.api/completions/stream?api-version=2&client-name=web&client-version=0.0.1",
timeout = base.timeout,
insecure = false,
headers = headers,
body = vim.tbl_deep_extend("force", {
model = base.model,
temperature = body_opts.temperature,
topK = body_opts.topK,
topP = body_opts.topP,
maxTokensToSample = body_opts.max_tokens,
stream = true,
messages = M.parse_messages(code_opts),
}, {}),
}
end

M.on_error = function() end

return M
1 change: 1 addition & 0 deletions lua/avante/providers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ local DressingState = { winid = nil, input_winid = nil, input_bufnr = nil }
---@field azure AvanteProviderFunctor
---@field gemini AvanteProviderFunctor
---@field cohere AvanteProviderFunctor
---@field cody AvanteProviderFunctor
local M = {}

---@class EnvironmentHandler
Expand Down