fix: insert dummy view func for str_replace_editor tool (#1997)

This commit is contained in:
yetone
2025-05-07 00:51:19 +08:00
committed by GitHub
parent 23d8c8b24f
commit c29075ae02

View File

@@ -156,12 +156,32 @@ function M.generate_prompts(opts)
table.insert(history_messages, message) table.insert(history_messages, message)
if Utils.is_tool_result_message(message) then if Utils.is_tool_result_message(message) then
local tool_use_message = Utils.get_tool_use_message(message, opts.history_messages) local tool_use_message = Utils.get_tool_use_message(message, opts.history_messages)
local is_replace_func_call = false
local is_str_replace_editor_func_call = false
local path = nil
if tool_use_message then
if tool_use_message.message.content[1].name == "replace_in_file" then
is_replace_func_call = true
path = tool_use_message.message.content[1].input.path
end
if tool_use_message.message.content[1].name == "str_replace_editor" then
if tool_use_message.message.content[1].input.command == "str_replace" then
is_replace_func_call = true
is_str_replace_editor_func_call = true
path = tool_use_message.message.content[1].input.path
end
end
end
--- For models like gpt-4o, the input parameter of replace_in_file is treated as the latest file content, so here we need to insert a fake view tool call to ensure it uses the latest file content --- For models like gpt-4o, the input parameter of replace_in_file is treated as the latest file content, so here we need to insert a fake view tool call to ensure it uses the latest file content
if tool_use_message and tool_use_message.message.content[1].name == "replace_in_file" then if is_replace_func_call and path then
local path = tool_use_message.message.content[1].input.path
if path then
local lines = Utils.read_file_from_buf_or_disk(path) local lines = Utils.read_file_from_buf_or_disk(path)
local tool_use_id = Utils.uuid() local tool_use_id = Utils.uuid()
local view_tool_name = "view"
local view_tool_input = { path = path }
if is_str_replace_editor_func_call then
view_tool_name = "str_replace_editor"
view_tool_input = { command = "view", path = path }
end
history_messages = vim.list_extend(history_messages, { history_messages = vim.list_extend(history_messages, {
HistoryMessage:new({ HistoryMessage:new({
role = "assistant", role = "assistant",
@@ -175,10 +195,8 @@ function M.generate_prompts(opts)
{ {
type = "tool_use", type = "tool_use",
id = tool_use_id, id = tool_use_id,
name = "view", name = view_tool_name,
input = { input = view_tool_input,
path = path,
},
}, },
}, },
}, { }, {
@@ -201,7 +219,6 @@ function M.generate_prompts(opts)
end end
end end
end end
end
for _, message in ipairs(history_messages) do for _, message in ipairs(history_messages) do
local content = message.message.content local content = message.message.content
if type(content) ~= "table" then goto continue end if type(content) ~= "table" then goto continue end