Skip to content
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

move curl body to file #622

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ For building binary if you wish to build from source, then `cargo` is required.
},
-- if you want to build from source then do `make BUILD_FROM_SOURCE=true`
build = "make",
-- build = "powershell -ExecutionPolicy Bypass -File Build.ps1 -BuildFromSource false" -- for windows
-- build = "pwsh -ExecutionPolicy Bypass -File Build.ps1 -BuildFromSource false" -- for windows
dependencies = {
"nvim-treesitter/nvim-treesitter",
"stevearc/dressing.nvim",
Expand Down
2 changes: 1 addition & 1 deletion lua/avante/llm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ M.stream = function(opts)
headers = spec.headers,
proxy = spec.proxy,
insecure = spec.insecure,
body = vim.json.encode(spec.body),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is considered breaking, if the spec doesn't contains a binary body (iiuc that is what body_file is) then this will break for other provider.

body = spec.body_file,
stream = function(err, data, _)
if err then
completed = true
Expand Down
33 changes: 21 additions & 12 deletions lua/avante/providers/claude.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,33 @@ M.parse_curl_args = function(provider, prompt_opts)
if not P.env.is_local("claude") then headers["x-api-key"] = provider.parse_api_key() end

local messages = M.parse_message(prompt_opts)
local body_content = vim.tbl_deep_extend("force", {
model = base.model,
system = {
{
type = "text",
text = prompt_opts.system_prompt,
cache_control = { type = "ephemeral" },
},
},
messages = messages,
stream = true,
}, body_opts)

local id = string.gsub("xxxx4xxx", "[xy]", function(l)
local v = (l == "x") and math.random(0, 0xf) or math.random(0, 0xb)
return string.format("%x", v)
end)

local body_file_path = string.format("%s\\AppData\\Local\\Temp\\avante_body_%s.json", os.getenv("USERPROFILE"), id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's not do this, this won't work on linux.

vim.fn.writefile({ vim.fn.json_encode(body_content) }, body_file_path)

return {
url = Utils.trim(base.endpoint, { suffix = "/" }) .. "/v1/messages",
proxy = base.proxy,
insecure = base.allow_insecure,
headers = headers,
body = vim.tbl_deep_extend("force", {
model = base.model,
system = {
{
type = "text",
text = prompt_opts.system_prompt,
cache_control = { type = "ephemeral" },
},
},
messages = messages,
stream = true,
}, body_opts),
body_file = body_file_path,
}
end

Expand Down
2 changes: 1 addition & 1 deletion lua/avante/providers/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ local DressingState = { winid = nil, input_winid = nil, input_bufnr = nil }
---
---@alias AvanteMessageParser fun(opts: AvantePromptOptions): AvanteChatMessage[]
---
---@class AvanteCurlOutput: {url: string, proxy: string, insecure: boolean, body: table<string, any> | string, headers: table<string, string>}
---@class AvanteCurlOutput: {url: string, proxy: string, insecure: boolean, body: string, headers: table<string, string>}
---@alias AvanteCurlArgsParser fun(opts: AvanteProvider | AvanteProviderFunctor, code_opts: AvantePromptOptions): AvanteCurlOutput
---
---@class ResponseParser
Expand Down