refactor(history): reduce computational complexity when handling history

When updating chat history to be used in LLM request there are several
instances where we do O(n^2) operations: scanning all messages to locate
a tool "use" and for each use scan messages again to locate
corresponding "result".

Refactor the code to scan messages once collecting tool uses and results
together, and then do 2nd scan to drop incomplete tool invocations and
refresh "view" and "edit" results with the latest content.

Also reduce number of pre-scan loops (where we discard partially
generated messages or messages that are not interesting or too-old) by
combining them when possible.

This reduces time to scan initial 417 messages on my system (which
result in 576 final messages) from 0.32 to 0.12 seconds.
This commit is contained in:
Dmitry Torokhov
2025-07-08 17:22:39 -07:00
parent c2e4ae5ef6
commit 2335ea3d15
5 changed files with 312 additions and 264 deletions

View File

@@ -824,7 +824,7 @@ function M._stream(opts)
table.insert(tool_results, tool_result)
return handle_next_tool_use(tool_uses, tool_use_messages, tool_use_index + 1, tool_results)
end
local is_edit_tool_use = Utils.is_edit_func_call_tool_use(partial_tool_use)
local is_edit_tool_use = Utils.is_edit_tool_use(partial_tool_use)
local support_streaming = false
local llm_tool = vim.iter(prompt_opts.tools):find(function(tool) return tool.name == partial_tool_use.name end)
if llm_tool then support_streaming = llm_tool.support_streaming == true end