Skip to content

Commit c9d74dd

Browse files
authored
fix(tools): remove globals for better concurrent tool usage (#2680)
1 parent b3851e7 commit c9d74dd

6 files changed

Lines changed: 16 additions & 21 deletions

File tree

doc/codecompanion.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5331,8 +5331,8 @@ required.
53315331
role = "user",
53325332
opts = { auto_submit = true },
53335333
-- Scope this prompt to the cmd_runner tool
5334-
condition = function()
5335-
return _G.codecompanion_current_tool == "cmd_runner"
5334+
condition = function(chat)
5335+
return chat.tools.tool and chat.tools.tool.name == "cmd_runner"
53365336
end,
53375337
-- Repeat until the tests pass, as indicated by the testing flag
53385338
-- which the cmd_runner tool sets on the chat buffer
@@ -5408,12 +5408,12 @@ Now let’s look at how we trigger the automated reflection prompts:
54085408
role = "user",
54095409
opts = { auto_submit = true },
54105410
-- Scope this prompt to only run when the cmd_runner tool is active
5411-
condition = function()
5412-
return _G.codecompanion_current_tool == "cmd_runner"
5411+
condition = function(chat)
5412+
return chat.tools.tool and chat.tools.tool.name == "cmd_runner"
54135413
end,
54145414
-- Repeat until the tests pass, as indicated by the testing flag
54155415
repeat_until = function(chat)
5416-
return chat.tools.flags.testing == true
5416+
return chat.tool_registry.flags.testing == true
54175417
end,
54185418
content = "The tests have failed. Can you edit the buffer and run the test suite again?",
54195419
},

doc/extending/agentic-workflows.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ Now let's look at how we trigger the automated reflection prompts:
134134
role = "user",
135135
opts = { auto_submit = true },
136136
-- Scope this prompt to only run when the cmd_runner tool is active
137-
condition = function()
138-
return _G.codecompanion_current_tool == "cmd_runner"
137+
condition = function(chat)
138+
return chat.tools.tool and chat.tools.tool.name == "cmd_runner"
139139
end,
140140
-- Repeat until the tests pass, as indicated by the testing flag
141141
repeat_until = function(chat)
142-
return chat.tools.flags.testing == true
142+
return chat.tool_registry.flags.testing == true
143143
end,
144144
content = "The tests have failed. Can you edit the buffer and run the test suite again?",
145145
},

lua/codecompanion/interactions/chat/init.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,6 @@ function Chat:stop()
14491449
local tool_job = self.current_tool
14501450
self.current_tool = nil
14511451

1452-
_G.codecompanion_cancel_tool = true
14531452
pcall(function()
14541453
tool_job.cancel()
14551454
end)

lua/codecompanion/interactions/chat/tools/orchestrator.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ function Orchestrator.new(tools, id)
114114
tools = tools,
115115
}, { __index = Orchestrator })
116116

117-
_G.codecompanion_cancel_tool = false
118-
119117
return self
120118
end
121119

@@ -127,7 +125,6 @@ function Orchestrator:_setup_handlers()
127125
if not self.tool then
128126
return
129127
end
130-
_G.codecompanion_current_tool = self.tool.name
131128
if self.tool.handlers and self.tool.handlers.setup then
132129
return self.tool.handlers.setup(self.tool, self.tools)
133130
end
@@ -234,6 +231,7 @@ end
234231
---@param self CodeCompanion.Tools.Orchestrator
235232
---@return nil
236233
function Orchestrator:_finalize_tools()
234+
self.tools.tool = nil
237235
return utils.fire("ToolsFinished", { id = self.id, bufnr = self.tools.bufnr })
238236
end
239237

tests/interactions/chat/test_subscribers.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ T = new_set({
2020
child.lua([[
2121
h.teardown_chat_buffer()
2222
_G.chat = nil
23-
_G.codecompanion_current_tool = nil
2423
]])
2524
end,
2625
post_once = child.stop,

tests/interactions/chat/test_workflows.lua

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ T = new_set({
1717
post_case = function()
1818
child.lua([[
1919
_G.chat = nil
20-
_G.codecompanion_current_tool = nil
2120
]])
2221
end,
2322
post_once = child.stop,
@@ -66,8 +65,8 @@ T["Workflows"]["prompts are sequentially added to the chat buffer"] = function()
6665
name = "Repeat On Failure",
6766
role = "user",
6867
opts = { auto_submit = false },
69-
condition = function()
70-
return _G.codecompanion_current_tool == "cmd_runner"
68+
condition = function(chat)
69+
return chat.tools.tool and chat.tools.tool.name == "cmd_runner"
7170
end,
7271
repeat_until = function(chat)
7372
return chat.tool_registry.flags.testing == true
@@ -80,8 +79,8 @@ T["Workflows"]["prompts are sequentially added to the chat buffer"] = function()
8079
name = "Success",
8180
role = "user",
8281
opts = { auto_submit = false },
83-
condition = function()
84-
return not _G.codecompanion_current_tool
82+
condition = function(chat)
83+
return not chat.tools.tool
8584
end,
8685
content = "Tests passed!",
8786
},
@@ -100,15 +99,15 @@ T["Workflows"]["prompts are sequentially added to the chat buffer"] = function()
10099
h.eq("First prompt", last_line)
101100

102101
-- Mock failing tool, twice
103-
child.lua([[_G.codecompanion_current_tool = "cmd_runner"]])
102+
child.lua([[_G.chat.tools.tool = { name = "cmd_runner" }]])
104103
child.lua([[h.send_to_llm(_G.chat, "Calling a tool...")]])
105104
last_line = child.lua([[
106105
local lines = h.get_buf_lines(_G.chat.bufnr)
107106
return lines[#lines]
108107
]])
109108
h.eq("The tests have failed", last_line)
110109

111-
child.lua([[_G.codecompanion_current_tool = "cmd_runner"]])
110+
child.lua([[_G.chat.tools.tool = { name = "cmd_runner" }]])
112111
child.lua([[h.send_to_llm(_G.chat, "Calling a tool...")]])
113112
last_line = child.lua([[
114113
local lines = h.get_buf_lines(_G.chat.bufnr)
@@ -119,7 +118,7 @@ T["Workflows"]["prompts are sequentially added to the chat buffer"] = function()
119118
-- Now pass tests; unset tool after this turn
120119
child.lua([[
121120
_G.chat.tool_registry.flags.testing = true
122-
h.send_to_llm(_G.chat, "Calling a tool...", function() _G.codecompanion_current_tool = nil end)
121+
h.send_to_llm(_G.chat, "Calling a tool...", function() _G.chat.tools.tool = nil end)
123122
]])
124123
last_line = child.lua([[
125124
local lines = h.get_buf_lines(_G.chat.bufnr)

0 commit comments

Comments
 (0)