Lightpanda: the headless browser designed for AI and automation
agent: keep the session pumped while the model thinks or a search runs
Adrià Arrufat36003cf7b5ab
diff --git a/src/agent/Agent.zig b/src/agent/Agent.zigindex e4b8d6ae0..33020bec3 100644--- a/src/agent/Agent.zig+++ b/src/agent/Agent.zig@@ -33,6 +33,7 @@ const Candidate = zenai.provider.Candidate;const App = @import("../App.zig");const Conversation = @import("Conversation.zig");+const ModelCall = @import("ModelCall.zig");const Terminal = @import("Terminal.zig");const SlashCommand = @import("SlashCommand.zig");const settings = @import("settings.zig");@@ -1314,7 +1315,9 @@ fn synthesizeSaveTo(self: *Agent, arena: std.mem.Allocator, path: []const u8, moself.http_interrupt.reset();self.terminal.spinner.start();- var result = provider_client.runTools(+ var result = ModelCall.run(+ self.ts.session,+ provider_client,self.model,&self.conversation.messages,self.allocator,@@ -1771,7 +1774,9 @@ fn processUserMessage(self: *Agent, input: TurnInput) !?[]const u8 {self.save_calls.clearRetainingCapacity();self.terminal.spinner.start();- var result = provider_client.runTools(+ var result = ModelCall.run(+ self.ts.session,+ provider_client,self.model,&self.conversation.messages,self.allocator,@@ -1864,7 +1869,9 @@ fn processUserMessage(self: *Agent, input: TurnInput) !?[]const u8 {.content = try ma.dupe(u8, synthesis_prompt),});- var synth = provider_client.runTools(+ var synth = ModelCall.run(+ self.ts.session,+ provider_client,self.model,&self.conversation.messages,self.allocator,diff --git a/src/agent/ModelCall.zig b/src/agent/ModelCall.zignew file mode 100644index 000000000..e4a8091af--- /dev/null+++ b/src/agent/ModelCall.zig@@ -0,0 +1,132 @@+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)+//+// Francis Bouvier <[email protected]>+// Pierre Tachoire <[email protected]>+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as+// published by the Free Software Foundation, either version 3 of the+// License, or (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++//! Runs `Client.runTools` on a helper thread so the agent thread keeps pumping+//! the session while the model thinks; otherwise page transfers stall and the+//! watchdog kills the page's JS. Tool calls and streamed text still run on the+//! agent thread, and the two threads take turns.++const std = @import("std");+const zenai = @import("zenai");+const lp = @import("lightpanda");++const Client = zenai.provider.Client;+const Message = zenai.provider.Message;++const ModelCall = @This();++const ToolCall = struct {+ allocator: std.mem.Allocator,+ name: []const u8,+ arguments: ?std.json.Value,+ result: Client.ToolHandler.Result = undefined,+};++allocator: std.mem.Allocator,+handler: Client.ToolHandler,+stream: ?Client.TextDeltaHook,+posted: std.Io.Event = .unset,+answered: std.Io.Event = .unset,+outcome: Client.Error!Client.RunToolsResult = undefined,++// What the helper hands over. Streamed text is buffered rather than waited on,+// so the helper keeps reading the model while the agent thread is in a page tick.+mutex: std.Io.Mutex = .init,+text: std.ArrayList(u8) = .empty,+tool_call: ?*ToolCall = null,+finished: bool = false,++/// `client.runTools`, with `session` pumped until the model is done. Falls+/// back to a call on this thread if the helper thread can't start.+pub fn run(+ session: *lp.Session,+ client: Client,+ model: []const u8,+ messages: *std.ArrayList(Message),+ list_alloc: std.mem.Allocator,+ data_alloc: std.mem.Allocator,+ handler: Client.ToolHandler,+ config: Client.RunToolsConfig,+) Client.Error!Client.RunToolsResult {+ var self: ModelCall = .{ .allocator = list_alloc, .handler = handler, .stream = config.stream };+ defer self.text.deinit(list_alloc);++ var forwarded = config;+ if (config.stream != null) forwarded.stream = .{ .context = @ptrCast(&self), .onText = forwardText };+ const forward_tool: Client.ToolHandler = .{ .context = @ptrCast(&self), .callFn = forwardTool };++ const args = .{ client, model, messages, list_alloc, data_alloc, forward_tool, forwarded };+ const thread = std.Thread.spawn(.{}, helperMain, .{ &self, args }) catch+ return client.runTools(model, messages, list_alloc, data_alloc, handler, config);+ self.serve(session);+ thread.join();+ return self.outcome;+}++fn helperMain(self: *ModelCall, args: anytype) void {+ self.outcome = @call(.auto, Client.runTools, args);+ self.mutex.lockUncancelable(lp.io);+ self.finished = true;+ self.mutex.unlock(lp.io);+ self.posted.set(lp.io);+}++/// Agent thread: run what the helper hands over, pump the session in between.+fn serve(self: *ModelCall, session: *lp.Session) void {+ while (true) {+ session.pumpUntil(&self.posted);+ self.posted.reset();++ self.mutex.lockUncancelable(lp.io);+ var text = self.text;+ self.text = .empty;+ const tool_call = self.tool_call;+ self.tool_call = null;+ const finished = self.finished;+ self.mutex.unlock(lp.io);++ if (text.items.len > 0) self.stream.?.call(text.items);+ text.deinit(self.allocator);+ if (tool_call) |call| {+ call.result = self.handler.callFn(self.handler.context, call.allocator, call.name, call.arguments);+ self.answered.set(lp.io);+ }+ if (finished) return;+ }+}++fn forwardTool(ctx: *anyopaque, allocator: std.mem.Allocator, name: []const u8, arguments: ?std.json.Value) Client.ToolHandler.Result {+ const self: *ModelCall = @ptrCast(@alignCast(ctx));+ var call: ToolCall = .{ .allocator = allocator, .name = name, .arguments = arguments };+ self.answered.reset();+ self.mutex.lockUncancelable(lp.io);+ self.tool_call = &call;+ self.mutex.unlock(lp.io);+ self.posted.set(lp.io);+ self.answered.waitUncancelable(lp.io);+ return call.result;+}++fn forwardText(ctx: *anyopaque, delta: []const u8) void {+ const self: *ModelCall = @ptrCast(@alignCast(ctx));+ self.mutex.lockUncancelable(lp.io);+ defer self.mutex.unlock(lp.io);+ // Streaming is display only: on OOM the chunk is dropped, not the turn.+ self.text.appendSlice(self.allocator, delta) catch return;+ self.posted.set(lp.io);+}diff --git a/src/browser/Session.zig b/src/browser/Session.zigindex 2eac5061c..2ecdf8a66 100644--- a/src/browser/Session.zig+++ b/src/browser/Session.zig@@ -580,6 +580,34 @@ pub fn idleSlice(self: *Session) u31 {};}+/// Pump until another thread sets `event`, so a blocking call made off this+/// thread doesn't starve the page.+pub fn pumpUntil(self: *Session, event: *std.Io.Event) void {+ while (!event.isSet()) {+ const idle_ms = self.idleSlice();+ event.waitTimeout(lp.io, .{ .duration = .{ .raw = .fromMilliseconds(idle_ms), .clock = .awake } }) catch {};+ }+}++/// `@call(.auto, func, args)` on a helper thread, pumping until it returns.+/// Falls back to calling it here if the thread can't start.+pub fn runPumped(self: *Session, comptime func: anytype, args: anytype) @TypeOf(@call(.auto, func, args)) {+ const Call = struct {+ result: @TypeOf(@call(.auto, func, args)) = undefined,+ done: std.Io.Event = .unset,++ fn run(call: *@This(), call_args: @TypeOf(args)) void {+ call.result = @call(.auto, func, call_args);+ call.done.set(lp.io);+ }+ };+ var call: Call = .{};+ const thread = std.Thread.spawn(.{}, Call.run, .{ &call, args }) catch return @call(.auto, func, args);+ self.pumpUntil(&call.done);+ thread.join();+ return call.result;+}+pub fn scheduleNavigation(_: *Session, frame: *Frame) !void {return frame.page.scheduleNavigation(frame);}diff --git a/src/browser/tools.zig b/src/browser/tools.zigindex f524555b7..7ff544485 100644--- a/src/browser/tools.zig+++ b/src/browser/tools.zig@@ -997,7 +997,7 @@ fn dispatch() ToolError!ToolResult {return switch (tool) {.goto => .{ .text = try execGoto(arena, session, registry, substituted) },- .search => execSearch(arena, substituted),+ .search => execSearch(arena, session, substituted),.markdown => .{ .text = try execMarkdown(arena, session, registry, substituted) },.html => .{ .text = try execHtml(arena, session, registry, substituted) },.screenshot => try execScreenshot(arena, session, registry, substituted, opts.inline_image),@@ -1284,7 +1284,7 @@ pub fn searchKeyStatus(engine: SearchEngine) ?KeyStatus {return null;}-fn execSearch(arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError!ToolResult {+fn execSearch(arena: std.mem.Allocator, session: *lp.Session, arguments: ?std.json.Value) ToolError!ToolResult {const args = try parseArgs(SearchParams, arena, arguments);if (args.query.len == 0) return ToolError.InvalidParams;@@ -1299,7 +1299,7 @@ fn execSearch(arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError!To// Fall through on any failure so one outage doesn't kill// a whole benchmark run.var detail: Failure = .{};- if (apiSearch(engine, arena, api_key, timeout_ms, args.query, &detail)) |markdown_| {+ if (session.runPumped(apiSearch, .{ engine, arena, api_key, timeout_ms, args.query, &detail })) |markdown_| {return .{ .text = markdown_ };} else |err| {last_err = err;@@ -1314,7 +1314,7 @@ fn execSearch(arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError!To},inline else => |tag| {inline for (api_engines) |engine| {- if (engine.tag == tag) return searchExplicit(arena, engine, timeout_ms, args.query);+ if (engine.tag == tag) return searchExplicit(arena, session, engine, timeout_ms, args.query);}@compileError("engine missing from api_engines: " ++ @tagName(tag));},@@ -1322,14 +1322,14 @@ fn execSearch(arena: std.mem.Allocator, arguments: ?std.json.Value) ToolError!To}/// No fallback: a failed call or a missing key is an error result.-fn searchExplicit(arena: std.mem.Allocator, comptime engine: anytype, timeout_ms: u32, query: []const u8) ToolError!ToolResult {+fn searchExplicit(arena: std.mem.Allocator, session: *lp.Session, comptime engine: anytype, timeout_ms: u32, query: []const u8) ToolError!ToolResult {const label = @tagName(engine.tag);const api_key = engineKey(engine) catch return .{.text = "web search engine is set to " ++ label ++ " but " ++ engine.env_var ++ " is not set in the environment",.is_error = true,};var detail: Failure = .{};- const markdown_ = apiSearch(engine, arena, api_key, timeout_ms, query, &detail) catch |err|+ const markdown_ = session.runPumped(apiSearch, .{ engine, arena, api_key, timeout_ms, query, &detail }) catch |err|return searchFailed(arena, label, err, detail);return .{ .text = markdown_ };}