refactor: summarize memory (#1508)

This commit is contained in:
yetone
2025-03-07 00:12:57 +08:00
committed by GitHub
parent 2b0e7e09ae
commit 8620ea3e12
18 changed files with 434 additions and 217 deletions

View File

@@ -18,8 +18,8 @@ M.parse_response = O.parse_response
M.parse_response_without_stream = O.parse_response_without_stream
M.is_disable_stream = O.is_disable_stream
function M:parse_curl_args(provider, prompt_opts)
local provider_conf, request_body = P.parse_config(provider)
function M:parse_curl_args(prompt_opts)
local provider_conf, request_body = P.parse_config(self)
local headers = {
["Content-Type"] = "application/json",
@@ -27,9 +27,9 @@ function M:parse_curl_args(provider, prompt_opts)
if P.env.require_api_key(provider_conf) then
if provider_conf.entra then
headers["Authorization"] = "Bearer " .. provider.parse_api_key()
headers["Authorization"] = "Bearer " .. self.parse_api_key()
else
headers["api-key"] = provider.parse_api_key()
headers["api-key"] = self.parse_api_key()
end
end

View File

@@ -51,13 +51,12 @@ function M:parse_response_without_stream(data, event_state, opts)
vim.schedule(function() opts.on_stop({ reason = "complete" }) end)
end
---@param provider AvanteBedrockProviderFunctor
---@param prompt_opts AvantePromptOptions
---@return table
function M:parse_curl_args(provider, prompt_opts)
local base, body_opts = P.parse_config(provider)
function M:parse_curl_args(prompt_opts)
local base, body_opts = P.parse_config(self)
local api_key = provider.parse_api_key()
local api_key = self.parse_api_key()
if api_key == nil then error("Cannot get the bedrock api key!") end
local parts = vim.split(api_key, ",")
local aws_access_key_id = parts[1]

View File

@@ -262,11 +262,10 @@ function M:parse_response(ctx, data_stream, event_state, opts)
end
end
---@param provider AvanteProviderFunctor
---@param prompt_opts AvantePromptOptions
---@return table
function M:parse_curl_args(provider, prompt_opts)
local provider_conf, request_body = P.parse_config(provider)
function M:parse_curl_args(prompt_opts)
local provider_conf, request_body = P.parse_config(self)
local disable_tools = provider_conf.disable_tools or false
local headers = {
@@ -275,7 +274,7 @@ function M:parse_curl_args(provider, prompt_opts)
["anthropic-beta"] = "prompt-caching-2024-07-31",
}
if P.env.require_api_key(provider_conf) then headers["x-api-key"] = provider.parse_api_key() end
if P.env.require_api_key(provider_conf) then headers["x-api-key"] = self.parse_api_key() end
local messages = self:parse_messages(prompt_opts)

View File

@@ -71,8 +71,8 @@ function M:parse_stream_data(ctx, data, opts)
end
end
function M:parse_curl_args(provider, prompt_opts)
local provider_conf, request_body = P.parse_config(provider)
function M:parse_curl_args(prompt_opts)
local provider_conf, request_body = P.parse_config(self)
local headers = {
["Accept"] = "application/json",
@@ -84,7 +84,7 @@ function M:parse_curl_args(provider, prompt_opts)
.. "."
.. vim.version().patch,
}
if P.env.require_api_key(provider_conf) then headers["Authorization"] = "Bearer " .. provider.parse_api_key() end
if P.env.require_api_key(provider_conf) then headers["Authorization"] = "Bearer " .. self.parse_api_key() end
return {
url = Utils.url_join(provider_conf.endpoint, "/chat"),

View File

@@ -215,12 +215,12 @@ M.parse_messages = OpenAI.parse_messages
M.parse_response = OpenAI.parse_response
function M:parse_curl_args(provider, prompt_opts)
function M:parse_curl_args(prompt_opts)
-- refresh token synchronously, only if it has expired
-- (this should rarely happen, as we refresh the token in the background)
H.refresh_token(false, false)
local provider_conf, request_body = P.parse_config(provider)
local provider_conf, request_body = P.parse_config(self)
local disable_tools = provider_conf.disable_tools or false
local tools = {}

View File

@@ -83,8 +83,8 @@ function M:parse_response(ctx, data_stream, _, opts)
end
end
function M:parse_curl_args(provider, prompt_opts)
local provider_conf, request_body = P.parse_config(provider)
function M:parse_curl_args(prompt_opts)
local provider_conf, request_body = P.parse_config(self)
request_body = vim.tbl_deep_extend("force", request_body, {
generationConfig = {
@@ -95,7 +95,7 @@ function M:parse_curl_args(provider, prompt_opts)
request_body.temperature = nil
request_body.max_tokens = nil
local api_key = provider.parse_api_key()
local api_key = self.parse_api_key()
if api_key == nil then error("Cannot get the gemini api key!") end
return {

View File

@@ -261,6 +261,13 @@ function M.setup()
E.setup({ provider = cursor_applying_provider })
end
end
if Config.memory_summary_provider then
local memory_summary_provider = M[Config.memory_summary_provider]
if memory_summary_provider and memory_summary_provider ~= provider then
E.setup({ provider = memory_summary_provider })
end
end
end
---@param provider Provider

View File

@@ -219,8 +219,8 @@ function M:parse_response_without_stream(data, _, opts)
end
end
function M:parse_curl_args(provider, prompt_opts)
local provider_conf, request_body = P.parse_config(provider)
function M:parse_curl_args(prompt_opts)
local provider_conf, request_body = P.parse_config(self)
local disable_tools = provider_conf.disable_tools or false
local headers = {
@@ -228,7 +228,7 @@ function M:parse_curl_args(provider, prompt_opts)
}
if P.env.require_api_key(provider_conf) then
local api_key = provider.parse_api_key()
local api_key = self.parse_api_key()
if api_key == nil then
error(Config.provider .. " API key is not set, please set it in your environment variable or config file")
end

View File

@@ -32,8 +32,8 @@ function M.parse_api_key()
return direct_output
end
function M:parse_curl_args(provider, prompt_opts)
local provider_conf, request_body = P.parse_config(provider)
function M:parse_curl_args(prompt_opts)
local provider_conf, request_body = P.parse_config(self)
local location = vim.fn.getenv("LOCATION")
local project_id = vim.fn.getenv("PROJECT_ID")
local model_id = provider_conf.model or "default-model-id"