diff --git a/lua/avante/config.lua b/lua/avante/config.lua index ef6e883..4a04ac1 100644 --- a/lua/avante/config.lua +++ b/lua/avante/config.lua @@ -220,7 +220,7 @@ M._defaults = { endpoint = "https://api.openai.com/v1", model = "gpt-4o", timeout = 30000, -- Timeout in milliseconds, increase this for reasoning models - temperature = 0, + temperature = 0.75, max_completion_tokens = 16384, -- Increase this to include reasoning tokens (for reasoning models) reasoning_effort = "medium", -- low|medium|high, only used for reasoning models }, @@ -231,7 +231,7 @@ M._defaults = { proxy = nil, -- [protocol://]host[:port] Use this proxy allow_insecure = false, -- Allow insecure server connections timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 20480, }, ---@type AvanteAzureProvider @@ -240,7 +240,7 @@ M._defaults = { deployment = "", -- Azure deployment name (e.g., "gpt-4o", "my-gpt-4o-deployment") api_version = "2024-12-01-preview", timeout = 30000, -- Timeout in milliseconds, increase this for reasoning models - temperature = 0, + temperature = 0.75, max_completion_tokens = 20480, -- Increase this to include reasoning tokens (for reasoning models) reasoning_effort = "medium", -- low|medium|high, only used for reasoning models }, @@ -249,14 +249,14 @@ M._defaults = { endpoint = "https://api.anthropic.com", model = "claude-3-7-sonnet-20250219", timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 20480, }, ---@type AvanteSupportedProvider bedrock = { model = "anthropic.claude-3-5-sonnet-20241022-v2:0", timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 20480, aws_region = "", -- AWS region to use for authentication and bedrock API aws_profile = "", -- AWS profile to use for authentication, if unspecified uses default credentials chain @@ -266,7 +266,7 @@ M._defaults = { endpoint = "https://generativelanguage.googleapis.com/v1beta/models", model = "gemini-2.0-flash", timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 8192, }, ---@type AvanteSupportedProvider @@ -274,7 +274,7 @@ M._defaults = { endpoint = "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models", model = "gemini-1.5-flash-002", timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 20480, }, ---@type AvanteSupportedProvider @@ -282,7 +282,7 @@ M._defaults = { endpoint = "https://api.cohere.com/v2", model = "command-r-plus-08-2024", timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 20480, }, ---@type AvanteSupportedProvider @@ -290,7 +290,7 @@ M._defaults = { endpoint = "http://127.0.0.1:11434", timeout = 30000, -- Timeout in milliseconds options = { - temperature = 0, + temperature = 0.75, num_ctx = 20480, keep_alive = "5m", }, @@ -300,7 +300,7 @@ M._defaults = { endpoint = "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/antrhopic/models", model = "claude-3-5-sonnet-v2@20241022", timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 20480, }, ---To add support for custom provider, follow the format below @@ -312,7 +312,7 @@ M._defaults = { __inherited_from = "claude", model = "claude-3-5-haiku-20241022", timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 8192, }, ---@type AvanteSupportedProvider @@ -320,7 +320,7 @@ M._defaults = { __inherited_from = "claude", model = "claude-3-opus-20240229", timeout = 30000, -- Timeout in milliseconds - temperature = 0, + temperature = 0.75, max_tokens = 20480, }, ["openai-gpt-4o-mini"] = { diff --git a/lua/avante/llm.lua b/lua/avante/llm.lua index eaec5bb..dcc9356 100644 --- a/lua/avante/llm.lua +++ b/lua/avante/llm.lua @@ -805,6 +805,11 @@ function M._stream(opts) }) end opts.on_messages_add(messages) + local the_last_tool_use = tool_use_list[#tool_use_list] + if the_last_tool_use and the_last_tool_use.name == "attempt_completion" then + opts.on_stop({ reason = "complete" }) + return + end local new_opts = vim.tbl_deep_extend("force", opts, { history_messages = opts.get_history_messages(), }) @@ -908,7 +913,9 @@ function M._stream(opts) if message then break end ::continue:: end - if not completed_attempt_completion_tool_use and opts.on_messages_add then + local user_reminder_count = opts.session_ctx.user_reminder_count or 0 + if not completed_attempt_completion_tool_use and opts.on_messages_add and user_reminder_count < 3 then + opts.session_ctx.user_reminder_count = user_reminder_count + 1 local message = HistoryMessage:new({ role = "user", content = "You should use tool calls to answer the question, for example, use attempt_completion if the job is done.", diff --git a/lua/avante/llm_tools/attempt_completion.lua b/lua/avante/llm_tools/attempt_completion.lua index 4bc1d83..ca875a6 100644 --- a/lua/avante/llm_tools/attempt_completion.lua +++ b/lua/avante/llm_tools/attempt_completion.lua @@ -1,6 +1,6 @@ local Base = require("avante.llm_tools.base") local Config = require("avante.config") --- local HistoryMessage = require("avante.history_message") +local HistoryMessage = require("avante.history_message") ---@alias AttemptCompletionInput {result: string, command?: string} @@ -58,13 +58,13 @@ function M.func(opts, on_log, on_complete, session_ctx) local sidebar = require("avante").get() if not sidebar then return false, "Avante sidebar not found" end session_ctx.attempt_completion_is_called = true - -- local message = HistoryMessage:new({ - -- role = "assistant", - -- content = opts.result, - -- }, { - -- just_for_display = true, - -- }) - -- sidebar:add_history_messages({ message }) + local message = HistoryMessage:new({ + role = "assistant", + content = opts.result, + }, { + just_for_display = true, + }) + sidebar:add_history_messages({ message }) if opts.command then require("avante.llm_tools.bash").func({ command = opts.command }, on_log, on_complete, session_ctx) else diff --git a/lua/avante/llm_tools/glob.lua b/lua/avante/llm_tools/glob.lua index 18a5cf5..2e8f6b2 100644 --- a/lua/avante/llm_tools/glob.lua +++ b/lua/avante/llm_tools/glob.lua @@ -41,13 +41,29 @@ M.returns = { } ---@type AvanteLLMToolFunc<{ rel_path: string, pattern: string }> -function M.func(opts, on_log) +function M.func(opts, on_log, on_complete, session_ctx) local abs_path = Helpers.get_abs_path(opts.rel_path) if not Helpers.has_permission_to_access(abs_path) then return "", "No permission to access path: " .. abs_path end if on_log then on_log("path: " .. abs_path) end if on_log then on_log("pattern: " .. opts.pattern) end local files = vim.fn.glob(abs_path .. "/" .. opts.pattern, true, true) - return vim.json.encode(files), nil + local truncated_files = {} + local is_truncated = false + local size = 0 + for _, file in ipairs(files) do + size = size + #file + if size > 1024 * 10 then + is_truncated = true + break + end + table.insert(truncated_files, file) + end + local result = vim.json.encode({ + matches = truncated_files, + is_truncated = is_truncated, + }) + if not on_complete then return result, nil end + on_complete(result, nil) end return M diff --git a/tests/llm_tools_spec.lua b/tests/llm_tools_spec.lua index b31830a..7e5062e 100644 --- a/tests/llm_tools_spec.lua +++ b/tests/llm_tools_spec.lua @@ -372,7 +372,7 @@ describe("llm_tools", function() -- Test for lua files in the root local result, err = glob({ rel_path = ".", pattern = "*.lua" }) assert.is_nil(err) - local files = vim.json.decode(result) + local files = vim.json.decode(result).matches assert.equals(2, #files) assert.truthy(vim.tbl_contains(files, test_dir .. "/file1.lua")) assert.truthy(vim.tbl_contains(files, test_dir .. "/file2.lua")) @@ -382,7 +382,7 @@ describe("llm_tools", function() -- Test with recursive pattern local result2, err2 = glob({ rel_path = ".", pattern = "**/*.lua" }) assert.is_nil(err2) - local files2 = vim.json.decode(result2) + local files2 = vim.json.decode(result2).matches assert.equals(3, #files2) assert.truthy(vim.tbl_contains(files2, test_dir .. "/file1.lua")) assert.truthy(vim.tbl_contains(files2, test_dir .. "/file2.lua")) @@ -398,7 +398,7 @@ describe("llm_tools", function() it("should handle patterns without matches", function() local result, err = glob({ rel_path = ".", pattern = "*.nonexistent" }) assert.is_nil(err) - local files = vim.json.decode(result) + local files = vim.json.decode(result).matches assert.equals(0, #files) end) @@ -413,7 +413,7 @@ describe("llm_tools", function() local result, err = glob({ rel_path = ".", pattern = "**/*.lua" }) assert.is_nil(err) - local files = vim.json.decode(result) + local files = vim.json.decode(result).matches -- Check that files from non-ignored directory are found local found_notignored = false