xonecas.io
79113
Ma

mini-coding-agent / commit 2523000

fix(tool calling): respect the config when calling tools

xonecas2026-10-08 2523000da9b622f484c90885c728467c0c99958e parent 2ffca78

5 files changed

src/agent.zig+1−0
@@ -248,6 +248,7 @@ pub fn runTurn(opts: Options, messages: *std.ArrayList(types.Message), interacti
248248 result = tools.execute(opts.a, scratch, call.name, call.arguments, .{
249249 .cancel = opts.cancel,
250250 .supports_images = opts.supports_images,
251+ .tools = opts.config.tools,
251252 .on_output = .{ .ctx = &stream_ctx.listener, .on_chunk = onToolOutput },
252253 .snapshot_ignore_dirs = opts.config.snapshot_ignore_dirs,
253254 .snapshot_uses_gitignore = opts.config.snapshot_uses_gitignore,
src/main.zig+1−1
@@ -100,7 +100,7 @@ fn run() !u8 {
100100 .model = model_ptr,
101101 .system_prompt = built.prompt,
102102 .loaded = .{ .agent_files = built.loaded.agent_files, .skills = built.loaded.skills },
103- .tools_json = tools.json(a, cfg.tools, supports_images),
103+ .tools_json = tools.json(a, cfg.tools, supports_images, cwd),
104104 .supports_images = supports_images,
105105 .session = &sess,
106106 .cancel = &cancel,
src/tools/common.zig+2−0
@@ -1,4 +1,5 @@
11 const std = @import("std");
2+const config = @import("../config.zig");
23 const types = @import("../types.zig");
34
45 pub const OutputFn = struct {
@@ -13,6 +14,7 @@ pub const OutputFn = struct {
1314 pub const Context = struct {
1415 cancel: *const std.atomic.Value(bool),
1516 supports_images: bool,
17+ tools: []const config.ToolName = &.{},
1618 on_output: ?OutputFn = null,
1719 snapshot_ignore_dirs: []const []const u8 = &.{},
1820 snapshot_uses_gitignore: bool = true,
src/tools/index.zig+14−8
@@ -20,23 +20,28 @@ const bash_params =
2020 const read_description = "Read a file. Returns its text. Returns the whole file unless offset and range give a line window. Prefer bash for search or binary files.";
2121 const read_image_description = "Read a file. Returns its text, or the image itself when the file is a png, jpg, or webp. Returns the whole file unless offset and range give a line window. Prefer bash for search or binary files.";
2222 const edit_description = "Edit a file by exact text replacement. oldText must occur exactly once. With empty oldText, create a new file (fails if it exists).";
23-const bash_description = "Run a bash command in the current working directory.";
23+const bash_description = "Run a bash command in the current working directory ({s}).";
2424
2525 pub fn execute(a: std.mem.Allocator, scratch: std.mem.Allocator, name: []const u8, args_json: []const u8, ctx: common.Context) common.Result {
26- if (std.mem.eql(u8, name, "read")) return read_tool.run(a, scratch, args_json, ctx);
27- if (std.mem.eql(u8, name, "edit")) return edit_tool.run(a, scratch, args_json, ctx);
28- if (std.mem.eql(u8, name, "bash")) return bash_tool.run(a, scratch, args_json, ctx);
26+ const enabled = for (ctx.tools) |tool| {
27+ if (std.mem.eql(u8, name, @tagName(tool))) break true;
28+ } else false;
29+ if (enabled) {
30+ if (std.mem.eql(u8, name, "read")) return read_tool.run(a, scratch, args_json, ctx);
31+ if (std.mem.eql(u8, name, "edit")) return edit_tool.run(a, scratch, args_json, ctx);
32+ if (std.mem.eql(u8, name, "bash")) return bash_tool.run(a, scratch, args_json, ctx);
33+ }
2934 return .{
3035 .text = std.fmt.allocPrint(a, "unknown tool: {s}", .{name}) catch "unknown tool",
3136 .is_error = true,
3237 };
3338 }
3439
35-fn writeTool(w: *std.Io.Writer, name: config.ToolName, with_images: bool) !void {
40+fn writeTool(w: *std.Io.Writer, name: config.ToolName, with_images: bool, bash_desc: []const u8) !void {
3641 const description = switch (name) {
3742 .read => if (with_images) read_image_description else read_description,
3843 .edit => edit_description,
39- .bash => bash_description,
44+ .bash => bash_desc,
4045 };
4146 try w.writeAll("{\"name\":");
4247 try std.json.Stringify.encodeJsonString(@tagName(name), .{}, w);
@@ -51,13 +56,14 @@ fn writeTool(w: *std.Io.Writer, name: config.ToolName, with_images: bool) !void
5156 try w.writeByte('}');
5257 }
5358
54-pub fn json(a: std.mem.Allocator, names: []const config.ToolName, with_images: bool) []const u8 {
59+pub fn json(a: std.mem.Allocator, names: []const config.ToolName, with_images: bool, cwd: []const u8) []const u8 {
60+ const bash_desc = std.fmt.allocPrint(a, bash_description, .{cwd}) catch "Run a bash command in the current working directory.";
5561 var out: std.Io.Writer.Allocating = .init(a);
5662 const w = &out.writer;
5763 w.writeByte('[') catch return "[]";
5864 for (names, 0..) |name, i| {
5965 if (i > 0) w.writeByte(',') catch return out.written();
60- writeTool(w, name, with_images) catch return out.written();
66+ writeTool(w, name, with_images, bash_desc) catch return out.written();
6167 }
6268 w.writeByte(']') catch return out.written();
6369 return out.written();
src/tui/tui.zig+1−1
@@ -645,7 +645,7 @@ const Tui = struct {
645645 fn select(self: *Tui, m: *const types.Model) void {
646646 self.opts.model = m;
647647 self.opts.supports_images = m.supports_images;
648- self.opts.tools_json = tools_index.json(self.a, self.tool_names, self.opts.supports_images);
648+ self.opts.tools_json = tools_index.json(self.a, self.tool_names, self.opts.supports_images, self.opts.session.cwd);
649649 self.pushBanner();
650650 self.dirty = true;
651651 }