Skip to content
Open
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: 2 additions & 0 deletions lua/avante/path.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ function History.new(bufnr)
entries = {},
messages = {},
todos = {},
last_state = nil,
filename = filepath_to_filename(filepath),
}
return history
Expand All @@ -155,6 +156,7 @@ function History.from_file(filepath)
if not vim.islist(history.entries) then history.entries = {} end
if not vim.islist(history.messages) then history.messages = {} end
if not vim.islist(history.todos) then history.todos = {} end
if history.last_state == vim.NIL then history.last_state = nil end
---@cast history avante.ChatHistory
history.filename = filepath_to_filename(filepath)
return history
Expand Down
33 changes: 23 additions & 10 deletions lua/avante/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ function Sidebar:new(id)
},
containers = {},
file_selector = FileSelector:new(id),
is_generating = false,
chat_history = nil,
current_state = nil,
state_timer = nil,
Expand Down Expand Up @@ -2039,6 +2038,12 @@ end

local _message_to_lines_lru_cache = LRUCache:new(100)

---@param state avante.GenerateState | nil
---@return boolean
local function is_active_request_state(state)
return state == "generating" or state == "thinking" or state == "tool calling"
end

---@param ctx table
---@param message avante.HistoryMessage
---@param messages avante.HistoryMessage[]
Expand Down Expand Up @@ -2441,13 +2446,18 @@ function Sidebar:add_history_messages(messages, opts)
end
end
local last_message = messages[#messages]
if last_message then
if History.Helpers.is_tool_use_message(last_message) then
self.current_state = "tool calling"
if last_message and is_active_request_state(self.current_state) then
local next_state = nil
if History.Helpers.is_tool_use_message(last_message) and last_message.is_calling then
next_state = "tool calling"
elseif History.Helpers.is_thinking_message(last_message) then
self.current_state = "thinking"
else
self.current_state = "generating"
next_state = "thinking"
end

if next_state ~= nil then
self.current_state = next_state
self.chat_history.last_state = next_state
self:save_history()
end
end
if opts and opts.eager_update then
Expand Down Expand Up @@ -2606,6 +2616,7 @@ function Sidebar:reload_chat_history()
self.token_count = nil
if not self.code.bufnr or not api.nvim_buf_is_valid(self.code.bufnr) then return end
self.chat_history = Path.history.load(self.code.bufnr)
self.current_state = self.chat_history.last_state
self._history_cache_invalidated = true
end

Expand Down Expand Up @@ -2780,7 +2791,7 @@ end
function Sidebar:handle_submit(request)
if Config.prompt_logger.enabled then PromptLogger.log_prompt(request) end

if self.is_generating then
if is_active_request_state(self.current_state) then

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

maybe self.request_pending() ?

self:add_history_messages({ History.Message:new("user", request) })
return
end
Expand Down Expand Up @@ -2873,6 +2884,10 @@ function Sidebar:handle_submit(request)
local function on_state_change(state)
self:clear_state()
self.current_state = state
if self.chat_history then
self.chat_history.last_state = state
self:save_history()
end
self:render_state()
end

Expand Down Expand Up @@ -2912,8 +2927,6 @@ function Sidebar:handle_submit(request)

---@type AvanteLLMStopCallback
local function on_stop(stop_opts)
self.is_generating = false

pcall(function()
---remove keymaps
vim.keymap.del("n", "j", { buffer = self.containers.result.bufnr })
Expand Down
1 change: 1 addition & 0 deletions lua/avante/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ vim.g.avante_login = vim.g.avante_login
---@field system_prompt string | nil
---@field tokens_usage avante.LLMTokenUsage | nil
---@field acp_session_id string | nil
---@field last_state avante.GenerateState | nil
---
---@class avante.ChatMemory
---@field content string
Expand Down
Loading