@@ -973,23 +973,40 @@ function M.ask(prompt, config)
973
973
if not config .headless then
974
974
utils .schedule_main ()
975
975
976
+ -- Remove any tool calls that we did not handle
977
+ local assistant_message = M .chat :get_message (' assistant' )
978
+ if assistant_message and assistant_message .tool_calls then
979
+ local handled_ids = {}
980
+ for _ , tool in ipairs (resolved_tools ) do
981
+ handled_ids [tool .id ] = true
982
+ end
983
+
984
+ assistant_message .tool_calls = vim
985
+ .iter (assistant_message .tool_calls )
986
+ :filter (function (tool_call )
987
+ return handled_ids [tool_call .id ]
988
+ end )
989
+ :totable ()
990
+ end
991
+
976
992
if not utils .empty (resolved_tools ) then
993
+ -- If we are handling tools, replace user message with tool results
977
994
M .chat :remove_message (' user' )
995
+ for _ , tool in ipairs (resolved_tools ) do
996
+ M .chat :add_message ({
997
+ id = tool .id ,
998
+ role = ' tool' ,
999
+ tool_call_id = tool .id ,
1000
+ content = ' \n ' .. tool .result .. ' \n ' ,
1001
+ })
1002
+ end
978
1003
else
1004
+ -- Otherwise just replace the user message with resolved prompt
979
1005
M .chat :add_message ({
980
1006
role = ' user' ,
981
1007
content = ' \n ' .. prompt .. ' \n ' ,
982
1008
}, true )
983
1009
end
984
-
985
- for _ , tool in ipairs (resolved_tools ) do
986
- M .chat :add_message ({
987
- id = tool .id ,
988
- role = ' tool' ,
989
- tool_call_id = tool .id ,
990
- content = ' \n ' .. tool .result .. ' \n ' ,
991
- })
992
- end
993
1010
end
994
1011
995
1012
if utils .empty (prompt ) and utils .empty (resolved_tools ) then
0 commit comments