Skip to content

Commit bad83db

Browse files
authored
fix(chat): handle empty prompt and tools before ask (#1258)
Move the check for empty prompt and resolved tools before making the ask call. This ensures that unnecessary user messages are removed and the finish callback is called early, preventing redundant processing. Also remove assistant message if all tool calls are handled. Signed-off-by: Tomas Slusny <[email protected]>
1 parent 4d2586b commit bad83db

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lua/CopilotChat/init.lua

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -969,10 +969,17 @@ function M.ask(prompt, config)
969969
end
970970

971971
prompt = vim.trim(prompt)
972+
utils.schedule_main()
972973

973-
if not config.headless then
974-
utils.schedule_main()
974+
if utils.empty(prompt) and utils.empty(resolved_tools) then
975+
if not config.headless then
976+
M.chat:remove_message('user')
977+
finish()
978+
end
979+
return
980+
end
975981

982+
if not config.headless then
976983
-- Remove any tool calls that we did not handle
977984
local assistant_message = M.chat:get_message('assistant')
978985
if assistant_message and assistant_message.tool_calls then
@@ -987,6 +994,10 @@ function M.ask(prompt, config)
987994
return handled_ids[tool_call.id]
988995
end)
989996
:totable()
997+
998+
if utils.empty(assistant_message.tool_calls) then
999+
M.chat:remove_message('assistant')
1000+
end
9901001
end
9911002

9921003
if not utils.empty(resolved_tools) then
@@ -1009,14 +1020,6 @@ function M.ask(prompt, config)
10091020
end
10101021
end
10111022

1012-
if utils.empty(prompt) and utils.empty(resolved_tools) then
1013-
if not config.headless then
1014-
M.chat:remove_message('user')
1015-
finish()
1016-
end
1017-
return
1018-
end
1019-
10201023
local ask_ok, ask_response = pcall(client.ask, client, prompt, {
10211024
headless = config.headless,
10221025
history = M.chat.messages,

0 commit comments

Comments
 (0)