Files
browser/src/App.zig
T
Adrià Arrufat c3afc75ce8 tools: ask Gemini Decisions only with LIGHTPANDA_DECISION_PROVIDER=gemini
#4590 sent decision asks (page content, a login's username) to Gemini
whenever GOOGLE_API_KEY or GEMINI_API_KEY was set, and preferred it over
TYPESAFE_API_KEY. A Google key is exported for many other reasons, so that
was no opt-in, and it silently moved anyone with both keys off TypeSafe.

TypeSafe answers by default again; Gemini only when
LIGHTPANDA_DECISION_PROVIDER=gemini. Missing-key messages, tool
descriptions and skill notes say so, from one App.decision_env_hint.
2026-11-13 10:31:24 +01:00

198 lines
6.8 KiB
Zig

// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
//
// Francis Bouvier <francis@lightpanda.io>
// Pierre Tachoire <pierre@lightpanda.io>
//
// 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/>.
const std = @import("std");
const lp = @import("lightpanda");
const Regex = @import("Regex.zig");
const Config = @import("Config.zig");
const Watchdog = @import("Watchdog.zig");
pub const ArenaPool = @import("ArenaPool.zig");
const Snapshot = @import("browser/js/Snapshot.zig");
const Platform = @import("browser/js/Platform.zig");
const screenshot = @import("browser/screenshot.zig");
const Sanitizer = @import("browser/webapi/Sanitizer.zig");
const Network = @import("network/Network.zig");
const Telemetry = @import("telemetry/telemetry.zig").Telemetry;
const zenai = @import("zenai");
const log = lp.log;
const Allocator = std.mem.Allocator;
const App = @This();
network: Network,
config: *const Config,
platform: Platform,
snapshot: Snapshot,
telemetry: Telemetry,
watchdog: Watchdog,
allocator: Allocator,
arena_pool: ArenaPool,
app_dir_path: ?[]const u8,
regex_context: *Regex.Context,
default_sanitizer: *Sanitizer,
render_fonts: screenshot.Fonts,
/// One connection pool and CA bundle for the API clients outside the page
/// network stack (decisions, search, the agent's model), so a call to a host
/// already reached skips the handshakes. Shared across threads, which its
/// allocator (`allocator`, thread-safe) allows.
http_client: std.http.Client,
/// Clients for asking a decision model (Gemini or TypeSafe Jev), pooled so
/// concurrent calls each get their own and keep its connection warm. It reads
/// the provider and its API key from the environment on every ask. Usable
/// from any thread: it touches no browser state.
decisions: zenai.decisions.Pool,
/// Picks the decision model's provider, "gemini" or "typesafe". Unset,
/// TypeSafe answers: a Google key alone never sends page content to Gemini.
pub const decision_provider_env_var_name = "LIGHTPANDA_DECISION_PROVIDER";
/// What to set for the decision model, for a message telling someone to.
pub const decision_env_hint = zenai.decisions.envHint(decision_provider_env_var_name);
pub fn init(allocator: Allocator, config: *const Config) !*App {
const platform = try Platform.init(.{
.v8_flags = config.v8Flags(),
.locale = config.locale(),
.timezone = config.timezone(),
});
errdefer platform.deinit();
const snapshot = try Snapshot.load();
errdefer snapshot.deinit();
const regex_context: *Regex.Context = try .init(allocator);
errdefer regex_context.deinit();
var render_fonts: screenshot.Fonts = try .load(allocator, config.renderFonts());
errdefer render_fonts.deinit(allocator);
const app = try allocator.create(App);
errdefer allocator.destroy(app);
app.* = .{
.config = config,
.allocator = allocator,
.platform = platform,
.snapshot = snapshot,
.regex_context = regex_context,
.render_fonts = render_fonts,
.network = undefined,
.app_dir_path = undefined,
.telemetry = undefined,
.arena_pool = undefined,
.default_sanitizer = undefined,
.watchdog = .init(config.watchdogMs()),
.http_client = zenai.http.newClient(lp.io, allocator),
// The calling session waits on an ask, so fail fast rather than
// honouring a Retry-After on the browser thread.
.decisions = .init(lp.io, allocator, lp.environ, .{
.retry_policy = .disabled,
.request_timeout_ms = 10_000,
.provider_env_var_name = decision_provider_env_var_name,
.http_client = &app.http_client,
}),
};
try app.watchdog.start();
errdefer app.watchdog.deinit();
app.network = try Network.init(app);
errdefer app.network.deinit();
app.app_dir_path = getAndMakeAppDir(allocator);
app.telemetry = try Telemetry.init(app);
errdefer app.telemetry.deinit(allocator);
app.arena_pool = ArenaPool.init(allocator, .{});
errdefer app.arena_pool.deinit();
app.default_sanitizer = try .initDefault(&app.arena_pool);
return app;
}
pub fn deinit(self: *App) void {
const allocator = self.allocator;
// All browsers are gone by now, so the entry list is empty; this just
// stops the checker thread.
self.watchdog.deinit();
if (self.app_dir_path) |app_dir_path| {
allocator.free(app_dir_path);
self.app_dir_path = null;
}
self.telemetry.deinit(allocator);
self.network.deinit();
// After `network`: its adblock regexes free through this context.
self.regex_context.deinit();
self.snapshot.deinit();
self.platform.deinit();
self.default_sanitizer.deinitDefault();
self.render_fonts.deinit(allocator);
self.arena_pool.deinit();
self.decisions.deinit();
self.http_client.deinit();
allocator.destroy(self);
}
fn getAndMakeAppDir(allocator: Allocator) ?[]const u8 {
if (lp.IS_TEST) {
return allocator.dupe(u8, "/tmp") catch unreachable;
}
const app_dir_path = getAppDataDir(allocator, "lightpanda") catch |err| {
log.warn(.app, "get data dir", .{ .err = err });
return null;
};
std.Io.Dir.cwd().createDirPath(lp.io, app_dir_path) catch |err| switch (err) {
else => {
allocator.free(app_dir_path);
log.warn(.app, "create data dir", .{ .err = err, .path = app_dir_path });
return null;
},
};
return app_dir_path;
}
pub fn getAppDataDir(allocator: Allocator, appname: []const u8) ![]const u8 {
switch (@import("builtin").os.tag) {
.macos, .ios => {
const home = std.c.getenv("HOME") orelse return error.AppDataDirUnavailable;
return std.fs.path.join(allocator, &.{ std.mem.span(home), "Library", "Application Support", appname });
},
else => {
if (std.c.getenv("XDG_DATA_HOME")) |xdg| {
const x = std.mem.span(xdg);
if (x.len > 0) {
return std.fs.path.join(allocator, &.{ x, appname });
}
}
const home = std.c.getenv("HOME") orelse return error.AppDataDirUnavailable;
return std.fs.path.join(allocator, &.{ std.mem.span(home), ".local", "share", appname });
},
}
}