From 45dd2ba6556c19576fd935e794496453e2e4857f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Arrufat?= Date: Fri, 24 Apr 2026 07:53:06 +0200 Subject: [PATCH] browser.tools: swap arena and registry argument order --- src/agent/ToolExecutor.zig | 4 ++-- src/browser/tools.zig | 46 +++++++++++++++++++------------------- src/mcp/tools.zig | 4 ++-- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/agent/ToolExecutor.zig b/src/agent/ToolExecutor.zig index 859da99e..ec7578f2 100644 --- a/src/agent/ToolExecutor.zig +++ b/src/agent/ToolExecutor.zig @@ -85,7 +85,7 @@ pub fn getCurrentUrl(self: *Self) []const u8 { pub fn callEval(self: *Self, arena: std.mem.Allocator, script: []const u8) ?[]const u8 { var obj: std.json.ObjectMap = .init(arena); obj.put("script", .{ .string = script }) catch return null; - const result = browser_tools.callEval(self.session, &self.node_registry, arena, .{ .object = obj }); + const result = browser_tools.callEval(self.session, arena, &self.node_registry, .{ .object = obj }); if (result.is_error) return null; return result.text; } @@ -97,5 +97,5 @@ pub fn call(self: *Self, arena: std.mem.Allocator, tool_name: []const u8, argume else null; - return browser_tools.call(self.session, &self.node_registry, arena, tool_name, arguments); + return browser_tools.call(self.session, arena, &self.node_registry, tool_name, arguments); } diff --git a/src/browser/tools.zig b/src/browser/tools.zig index 6ace5df2..6b2a289b 100644 --- a/src/browser/tools.zig +++ b/src/browser/tools.zig @@ -356,43 +356,43 @@ pub const Action = enum { pub fn call( session: *lp.Session, - registry: *CDPNode.Registry, arena: std.mem.Allocator, + registry: *CDPNode.Registry, tool_name: []const u8, arguments: ?std.json.Value, ) ToolError![]const u8 { const action = std.meta.stringToEnum(Action, tool_name) orelse return ToolError.InvalidParams; return switch (action) { - .eval => execEval(session, registry, arena, arguments).text, + .eval => execEval(session, arena, registry, arguments).text, .getEnv => execGetEnv(arena, arguments), .consoleLogs => execConsoleLogs(session, arena), .getUrl => execGetUrl(session), .getCookies => execGetCookies(session, arena), - inline else => |tag| @field(@This(), "exec" ++ [1]u8{std.ascii.toUpper(@tagName(tag)[0])} ++ @tagName(tag)[1..])(session, registry, arena, arguments), + inline else => |tag| @field(@This(), "exec" ++ [1]u8{std.ascii.toUpper(@tagName(tag)[0])} ++ @tagName(tag)[1..])(session, arena, registry, arguments), }; } pub fn callEval( session: *lp.Session, - registry: *CDPNode.Registry, arena: std.mem.Allocator, + registry: *CDPNode.Registry, arguments: ?std.json.Value, ) EvalResult { - return execEval(session, registry, arena, arguments); + return execEval(session, arena, registry, arguments); } pub fn isKnownTool(tool_name: []const u8) bool { return std.meta.stringToEnum(Action, tool_name) != null; } -fn execGoto(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execGoto(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const args = try parseArgsOrErr(GotoParams, arena, arguments) orelse return ToolError.InvalidParams; try performGoto(session, registry, args.url, args.timeout, args.waitUntil); return "Navigated successfully."; } -fn execMarkdown(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execMarkdown(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const args = try parseArgsOrDefault(UrlParams, arena, arguments); const page = try ensurePage(session, registry, args.url, args.timeout, args.waitUntil); @@ -402,7 +402,7 @@ fn execMarkdown(session: *lp.Session, registry: *CDPNode.Registry, arena: std.me return aw.written(); } -fn execLinks(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execLinks(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const args = try parseArgsOrDefault(UrlParams, arena, arguments); const page = try ensurePage(session, registry, args.url, args.timeout, args.waitUntil); @@ -417,7 +417,7 @@ fn execLinks(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.A return aw.written(); } -fn execTree(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execTree(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const TreeParams = struct { url: ?[:0]const u8 = null, backendNodeId: ?u32 = null, @@ -449,7 +449,7 @@ fn execTree(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Al return aw.written(); } -fn execNodeDetails(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execNodeDetails(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { backendNodeId: CDPNode.Id }; const args = try parseArgsOrErr(Params, arena, arguments) orelse return ToolError.InvalidParams; @@ -465,7 +465,7 @@ fn execNodeDetails(session: *lp.Session, registry: *CDPNode.Registry, arena: std return aw.written(); } -fn execInteractiveElements(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execInteractiveElements(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const args = try parseArgsOrDefault(UrlParams, arena, arguments); const page = try ensurePage(session, registry, args.url, args.timeout, args.waitUntil); @@ -479,7 +479,7 @@ fn execInteractiveElements(session: *lp.Session, registry: *CDPNode.Registry, ar return aw.written(); } -fn execStructuredData(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execStructuredData(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const args = try parseArgsOrDefault(UrlParams, arena, arguments); const page = try ensurePage(session, registry, args.url, args.timeout, args.waitUntil); @@ -490,7 +490,7 @@ fn execStructuredData(session: *lp.Session, registry: *CDPNode.Registry, arena: return aw.written(); } -fn execDetectForms(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execDetectForms(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const args = try parseArgsOrDefault(UrlParams, arena, arguments); const page = try ensurePage(session, registry, args.url, args.timeout, args.waitUntil); @@ -504,7 +504,7 @@ fn execDetectForms(session: *lp.Session, registry: *CDPNode.Registry, arena: std return aw.written(); } -fn execEval(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) EvalResult { +fn execEval(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) EvalResult { const Params = struct { script: [:0]const u8, url: ?[:0]const u8 = null, @@ -565,7 +565,7 @@ fn formatActionResult( }) catch ToolError.InternalError; } -fn execClick(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execClick(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { backendNodeId: ?CDPNode.Id = null, selector: ?[]const u8 = null, @@ -592,7 +592,7 @@ fn execClick(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.A return formatActionResult(arena, "Clicked element", args.selector, args.backendNodeId, "", page); } -fn execFill(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execFill(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { backendNodeId: ?CDPNode.Id = null, selector: ?[]const u8 = null, @@ -615,7 +615,7 @@ fn execFill(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Al return formatActionResult(arena, "Filled element", args.selector, args.backendNodeId, suffix, resolved.page); } -fn execScroll(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execScroll(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { backendNodeId: ?CDPNode.Id = null, x: ?i32 = null, @@ -644,7 +644,7 @@ fn execScroll(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem. }) catch return ToolError.InternalError; } -fn execWaitForSelector(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execWaitForSelector(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { selector: [:0]const u8, timeout: ?u32 = null, @@ -664,7 +664,7 @@ fn execWaitForSelector(session: *lp.Session, registry: *CDPNode.Registry, arena: return std.fmt.allocPrint(arena, "Element found. backendNodeId: {d}", .{registered.id}) catch return ToolError.InternalError; } -fn execHover(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execHover(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { backendNodeId: ?CDPNode.Id = null, selector: ?[]const u8 = null, @@ -680,7 +680,7 @@ fn execHover(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.A return formatActionResult(arena, "Hovered element", args.selector, args.backendNodeId, "", resolved.page); } -fn execPress(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execPress(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { key: []const u8, backendNodeId: ?CDPNode.Id = null, @@ -717,7 +717,7 @@ fn execPress(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.A }) catch return ToolError.InternalError; } -fn execSelectOption(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execSelectOption(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { backendNodeId: ?CDPNode.Id = null, selector: ?[]const u8 = null, @@ -735,7 +735,7 @@ fn execSelectOption(session: *lp.Session, registry: *CDPNode.Registry, arena: st return formatActionResult(arena, prefix, args.selector, args.backendNodeId, "", resolved.page); } -fn execSetChecked(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execSetChecked(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { backendNodeId: ?CDPNode.Id = null, selector: ?[]const u8 = null, @@ -754,7 +754,7 @@ fn execSetChecked(session: *lp.Session, registry: *CDPNode.Registry, arena: std. return formatActionResult(arena, "Set element", args.selector, args.backendNodeId, suffix, resolved.page); } -fn execFindElement(session: *lp.Session, registry: *CDPNode.Registry, arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError![]const u8 { +fn execFindElement(session: *lp.Session, arena: std.mem.Allocator, registry: *CDPNode.Registry, arguments: ?std.json.Value) ToolError![]const u8 { const Params = struct { role: ?[]const u8 = null, name: ?[]const u8 = null, diff --git a/src/mcp/tools.zig b/src/mcp/tools.zig index c94e7c04..9418f7e9 100644 --- a/src/mcp/tools.zig +++ b/src/mcp/tools.zig @@ -48,12 +48,12 @@ pub fn handleCall(server: *Server, arena: std.mem.Allocator, req: protocol.Reque // JS errors are returned as isError tool results, not protocol errors if (std.mem.eql(u8, call_params.name, "eval")) { - const result = browser_tools.callEval(server.session, &server.node_registry, arena, call_params.arguments); + const result = browser_tools.callEval(server.session, arena, &server.node_registry, call_params.arguments); const content = [_]protocol.TextContent([]const u8){.{ .text = result.text }}; return server.sendResult(id, protocol.CallToolResult([]const u8){ .content = &content, .isError = result.is_error }); } - const result = browser_tools.call(server.session, &server.node_registry, arena, call_params.name, call_params.arguments) catch |err| { + const result = browser_tools.call(server.session, arena, &server.node_registry, call_params.name, call_params.arguments) catch |err| { const code: protocol.ErrorCode = switch (err) { error.FrameNotLoaded => .FrameNotLoaded, error.NodeNotFound, error.InvalidParams => .InvalidParams,