mirror of
https://github.com/lightpanda-io/browser.git
synced 2026-03-30 09:08:55 +00:00
Compare commits
15 Commits
remove_cdp
...
fix/cdp-cr
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e354b7315f | ||
|
|
8723ecdd2d | ||
|
|
451178558a | ||
|
|
70dc0f6b95 | ||
|
|
d99599fa21 | ||
|
|
20e62a5551 | ||
|
|
e083d4a3d1 | ||
|
|
7a23686cbd | ||
|
|
25889ff918 | ||
|
|
b4e3f246ca | ||
|
|
f60e5cce6d | ||
|
|
81d4bdb157 | ||
|
|
cf5e4d7d1e | ||
|
|
9f81d7d3ff | ||
|
|
1f22462f13 |
@@ -24,6 +24,7 @@ const log = @import("log.zig");
|
||||
const dump = @import("browser/dump.zig");
|
||||
|
||||
const WebBotAuthConfig = @import("network/WebBotAuth.zig").Config;
|
||||
const mcp = @import("mcp.zig");
|
||||
|
||||
pub const RunMode = enum {
|
||||
help,
|
||||
@@ -222,6 +223,7 @@ pub const Serve = struct {
|
||||
|
||||
pub const Mcp = struct {
|
||||
common: Common = .{},
|
||||
version: mcp.Version = .default,
|
||||
};
|
||||
|
||||
pub const DumpFormat = enum {
|
||||
@@ -453,6 +455,12 @@ pub fn printUsageAndExit(self: *const Config, success: bool) void {
|
||||
\\Starts an MCP (Model Context Protocol) server over stdio
|
||||
\\Example: {s} mcp
|
||||
\\
|
||||
\\Options:
|
||||
\\--version
|
||||
\\ Override the reported MCP version.
|
||||
\\ Valid: 2024-11-05, 2025-03-26, 2025-06-18, 2025-11-25.
|
||||
\\ Defaults to "2024-11-05".
|
||||
\\
|
||||
++ common_options ++
|
||||
\\
|
||||
\\version command
|
||||
@@ -640,10 +648,22 @@ fn parseMcpArgs(
|
||||
allocator: Allocator,
|
||||
args: *std.process.ArgIterator,
|
||||
) !Mcp {
|
||||
var mcp: Mcp = .{};
|
||||
var result: Mcp = .{};
|
||||
|
||||
while (args.next()) |opt| {
|
||||
if (try parseCommonArg(allocator, opt, args, &mcp.common)) {
|
||||
if (std.mem.eql(u8, "--version", opt)) {
|
||||
const str = args.next() orelse {
|
||||
log.fatal(.mcp, "missing argument value", .{ .arg = opt });
|
||||
return error.InvalidArgument;
|
||||
};
|
||||
result.version = std.meta.stringToEnum(mcp.Version, str) orelse {
|
||||
log.fatal(.mcp, "invalid protocol version", .{ .value = str });
|
||||
return error.InvalidArgument;
|
||||
};
|
||||
continue;
|
||||
}
|
||||
|
||||
if (try parseCommonArg(allocator, opt, args, &result.common)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -651,7 +671,7 @@ fn parseMcpArgs(
|
||||
return error.UnkownOption;
|
||||
}
|
||||
|
||||
return mcp;
|
||||
return result;
|
||||
}
|
||||
|
||||
fn parseFetchArgs(
|
||||
|
||||
@@ -821,16 +821,16 @@ fn processOneMessage(self: *Client, msg: http.Handles.MultiMessage, transfer: *T
|
||||
break :blk std.ascii.eqlIgnoreCase(hdr.value, "close");
|
||||
};
|
||||
|
||||
if (msg.err != null and !is_conn_close_recv) {
|
||||
transfer.requestFailed(transfer._callback_error orelse msg.err.?, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
// make sure the transfer can't be immediately aborted from a callback
|
||||
// since we still need it here.
|
||||
transfer._performing = true;
|
||||
defer transfer._performing = false;
|
||||
|
||||
if (msg.err != null and !is_conn_close_recv) {
|
||||
transfer.requestFailed(transfer._callback_error orelse msg.err.?, true);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!transfer._header_done_called) {
|
||||
// In case of request w/o data, we need to call the header done
|
||||
// callback now.
|
||||
@@ -873,7 +873,6 @@ fn processMessages(self: *Client) !bool {
|
||||
var processed = false;
|
||||
while (self.handles.readMessage()) |msg| {
|
||||
const transfer = try Transfer.fromConnection(&msg.conn);
|
||||
|
||||
const done = self.processOneMessage(msg, transfer) catch |err| blk: {
|
||||
log.err(.http, "process_messages", .{ .err = err, .req = transfer });
|
||||
transfer.requestFailed(err, true);
|
||||
@@ -1068,6 +1067,24 @@ pub const Transfer = struct {
|
||||
if (self.req.shutdown_callback) |cb| {
|
||||
cb(self.ctx);
|
||||
}
|
||||
|
||||
if (self._performing or self.client.performing) {
|
||||
// We're currently inside of a callback. This client, and libcurl
|
||||
// generally don't expect a transfer to become deinitialized during
|
||||
// a callback. We can flag the transfer as aborted (which is what
|
||||
// we do when transfer.abort() is called in this condition) AND,
|
||||
// since this "kill()"should prevent any future callbacks, the best
|
||||
// we can do is null/noop them.
|
||||
self.aborted = true;
|
||||
self.req.start_callback = null;
|
||||
self.req.shutdown_callback = null;
|
||||
self.req.header_callback = Noop.headerCallback;
|
||||
self.req.data_callback = Noop.dataCallback;
|
||||
self.req.done_callback = Noop.doneCallback;
|
||||
self.req.error_callback = Noop.errorCallback;
|
||||
return;
|
||||
}
|
||||
|
||||
self.deinit();
|
||||
}
|
||||
|
||||
@@ -1492,3 +1509,12 @@ pub const Transfer = struct {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const Noop = struct {
|
||||
fn headerCallback(_: *Transfer) !bool {
|
||||
return true;
|
||||
}
|
||||
fn dataCallback(_: *Transfer, _: []const u8) !void {}
|
||||
fn doneCallback(_: *anyopaque) !void {}
|
||||
fn errorCallback(_: *anyopaque, _: anyerror) void {}
|
||||
};
|
||||
|
||||
@@ -148,3 +148,13 @@
|
||||
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id=identity>
|
||||
{
|
||||
const element = document.createElement('canvas');
|
||||
const ctx = element.getContext('2d');
|
||||
|
||||
testing.expectTrue(ctx === element.getContext('2d'));
|
||||
testing.expectEqual(null, element.getContext('webgl'));
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -85,3 +85,13 @@
|
||||
loseContext.restoreContext();
|
||||
}
|
||||
</script>
|
||||
|
||||
<script id=identity>
|
||||
{
|
||||
const element = document.createElement('canvas');
|
||||
const ctx = element.getContext('webgl');
|
||||
|
||||
testing.expectTrue(ctx === element.getContext('webgl'));
|
||||
testing.expectEqual(null, element.getContext('2d'));
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -59,6 +59,7 @@ pub fn fromError(err: anyerror) ?DOMException {
|
||||
error.TimeoutError => .{ ._code = .timeout_error },
|
||||
error.InvalidNodeType => .{ ._code = .invalid_node_type_error },
|
||||
error.DataClone => .{ ._code = .data_clone_error },
|
||||
error.InvalidAccessError => .{ ._code = .invalid_access_error },
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -29,6 +29,9 @@ const OffscreenCanvas = @import("../../canvas/OffscreenCanvas.zig");
|
||||
|
||||
const Canvas = @This();
|
||||
_proto: *HtmlElement,
|
||||
_cached: ?DrawingContext = null,
|
||||
|
||||
const ContextType = enum { none, @"2d", webgl };
|
||||
|
||||
pub fn asElement(self: *Canvas) *Element {
|
||||
return self._proto._proto;
|
||||
@@ -68,17 +71,28 @@ const DrawingContext = union(enum) {
|
||||
};
|
||||
|
||||
pub fn getContext(self: *Canvas, context_type: []const u8, page: *Page) !?DrawingContext {
|
||||
if (std.mem.eql(u8, context_type, "2d")) {
|
||||
const ctx = try page._factory.create(CanvasRenderingContext2D{ ._canvas = self });
|
||||
return .{ .@"2d" = ctx };
|
||||
if (self._cached) |cached| {
|
||||
const matches = switch (cached) {
|
||||
.@"2d" => std.mem.eql(u8, context_type, "2d"),
|
||||
.webgl => std.mem.eql(u8, context_type, "webgl") or std.mem.eql(u8, context_type, "experimental-webgl"),
|
||||
};
|
||||
return if (matches) cached else null;
|
||||
}
|
||||
|
||||
if (std.mem.eql(u8, context_type, "webgl") or std.mem.eql(u8, context_type, "experimental-webgl")) {
|
||||
const ctx = try page._factory.create(WebGLRenderingContext{});
|
||||
return .{ .webgl = ctx };
|
||||
}
|
||||
const drawing_context: DrawingContext = blk: {
|
||||
if (std.mem.eql(u8, context_type, "2d")) {
|
||||
const ctx = try page._factory.create(CanvasRenderingContext2D{ ._canvas = self });
|
||||
break :blk .{ .@"2d" = ctx };
|
||||
}
|
||||
|
||||
return null;
|
||||
if (std.mem.eql(u8, context_type, "webgl") or std.mem.eql(u8, context_type, "experimental-webgl")) {
|
||||
const ctx = try page._factory.create(WebGLRenderingContext{});
|
||||
break :blk .{ .webgl = ctx };
|
||||
}
|
||||
return null;
|
||||
};
|
||||
self._cached = drawing_context;
|
||||
return drawing_context;
|
||||
}
|
||||
|
||||
/// Transfers control of the canvas to an OffscreenCanvas.
|
||||
|
||||
@@ -158,15 +158,20 @@ fn createTarget(cmd: anytype) !void {
|
||||
else => return err,
|
||||
};
|
||||
|
||||
if (bc.target_id != null) {
|
||||
return error.TargetAlreadyLoaded;
|
||||
}
|
||||
if (params.browserContextId) |param_browser_context_id| {
|
||||
if (std.mem.eql(u8, param_browser_context_id, bc.id) == false) {
|
||||
return error.UnknownBrowserContextId;
|
||||
}
|
||||
}
|
||||
|
||||
// If a target already exists, close it first. Lightpanda only supports
|
||||
// one page at a time, so we replace the existing target rather than
|
||||
// rejecting the request. This unblocks automation frameworks (e.g.
|
||||
// Stagehand) that call createTarget multiple times.
|
||||
if (bc.target_id != null) {
|
||||
try doCloseTarget(cmd, bc);
|
||||
}
|
||||
|
||||
// if target_id is null, we should never have a page
|
||||
lp.assert(bc.session.page == null, "CDP.target.createTarget not null page", .{});
|
||||
|
||||
@@ -280,34 +285,9 @@ fn closeTarget(cmd: anytype) !void {
|
||||
return error.UnknownTargetId;
|
||||
}
|
||||
|
||||
// can't be null if we have a target_id
|
||||
lp.assert(bc.session.page != null, "CDP.target.closeTarget null page", .{});
|
||||
|
||||
try cmd.sendResult(.{ .success = true }, .{ .include_session_id = false });
|
||||
|
||||
// could be null, created but never attached
|
||||
if (bc.session_id) |session_id| {
|
||||
// Inspector.detached event
|
||||
try cmd.sendEvent("Inspector.detached", .{
|
||||
.reason = "Render process gone.",
|
||||
}, .{ .session_id = session_id });
|
||||
|
||||
// detachedFromTarget event
|
||||
try cmd.sendEvent("Target.detachedFromTarget", .{
|
||||
.targetId = target_id,
|
||||
.sessionId = session_id,
|
||||
.reason = "Render process gone.",
|
||||
}, .{});
|
||||
|
||||
bc.session_id = null;
|
||||
}
|
||||
|
||||
bc.session.removePage();
|
||||
for (bc.isolated_worlds.items) |world| {
|
||||
world.deinit();
|
||||
}
|
||||
bc.isolated_worlds.clearRetainingCapacity();
|
||||
bc.target_id = null;
|
||||
try doCloseTarget(cmd, bc);
|
||||
}
|
||||
|
||||
fn getTargetInfo(cmd: anytype) !void {
|
||||
@@ -468,6 +448,37 @@ fn setAutoAttach(cmd: anytype) !void {
|
||||
try cmd.sendResult(null, .{});
|
||||
}
|
||||
|
||||
/// Close the current target in a browser context: send detach events,
|
||||
/// remove the page, clean up isolated worlds, and clear the target_id.
|
||||
/// Shared by closeTarget and createTarget (which auto-closes before
|
||||
/// creating a replacement).
|
||||
fn doCloseTarget(cmd: anytype, bc: anytype) !void {
|
||||
// can't be null if we have a target_id
|
||||
lp.assert(bc.session.page != null, "CDP.target.doCloseTarget null page", .{});
|
||||
|
||||
// could be null, created but never attached
|
||||
if (bc.session_id) |session_id| {
|
||||
try cmd.sendEvent("Inspector.detached", .{
|
||||
.reason = "Render process gone.",
|
||||
}, .{ .session_id = session_id });
|
||||
|
||||
try cmd.sendEvent("Target.detachedFromTarget", .{
|
||||
.targetId = &bc.target_id.?,
|
||||
.sessionId = session_id,
|
||||
.reason = "Render process gone.",
|
||||
}, .{});
|
||||
|
||||
bc.session_id = null;
|
||||
}
|
||||
|
||||
bc.session.removePage();
|
||||
for (bc.isolated_worlds.items) |world| {
|
||||
world.deinit();
|
||||
}
|
||||
bc.isolated_worlds.clearRetainingCapacity();
|
||||
bc.target_id = null;
|
||||
}
|
||||
|
||||
fn doAttachtoTarget(cmd: anytype, target_id: []const u8) !void {
|
||||
const bc = cmd.browser_context.?;
|
||||
const session_id = bc.session_id orelse cmd.cdp.session_id_gen.next();
|
||||
@@ -771,6 +782,51 @@ test "cdp.target: detachFromTarget" {
|
||||
}
|
||||
}
|
||||
|
||||
test "cdp.target: createTarget closes existing target (issue #1962)" {
|
||||
var ctx = try testing.context();
|
||||
defer ctx.deinit();
|
||||
const bc = try ctx.loadBrowserContext(.{ .id = "BID-9" });
|
||||
{
|
||||
// Create first target
|
||||
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .browserContextId = "BID-9" } });
|
||||
try testing.expectEqual(true, bc.target_id != null);
|
||||
try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 10 });
|
||||
|
||||
// Create second target — should succeed by auto-closing the first
|
||||
try ctx.processMessage(.{ .id = 11, .method = "Target.createTarget", .params = .{ .browserContextId = "BID-9" } });
|
||||
try testing.expectEqual(true, bc.target_id != null);
|
||||
try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 11 });
|
||||
|
||||
// Page should exist (new target is active)
|
||||
try testing.expectEqual(true, bc.session.page != null);
|
||||
}
|
||||
}
|
||||
|
||||
test "cdp.target: createTarget closes existing attached target (issue #1962)" {
|
||||
var ctx = try testing.context();
|
||||
defer ctx.deinit();
|
||||
const bc = try ctx.loadBrowserContext(.{ .id = "BID-9" });
|
||||
{
|
||||
// Create and attach first target
|
||||
try ctx.processMessage(.{ .id = 10, .method = "Target.createTarget", .params = .{ .browserContextId = "BID-9" } });
|
||||
try testing.expectEqual(true, bc.target_id != null);
|
||||
try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 10 });
|
||||
|
||||
try ctx.processMessage(.{ .id = 11, .method = "Target.attachToTarget", .params = .{ .targetId = bc.target_id.? } });
|
||||
const session_id = bc.session_id.?;
|
||||
try ctx.expectSentResult(.{ .sessionId = session_id }, .{ .id = 11 });
|
||||
|
||||
// Create second target — should close and detach the first
|
||||
try ctx.processMessage(.{ .id = 12, .method = "Target.createTarget", .params = .{ .browserContextId = "BID-9" } });
|
||||
// Should have sent detach events for the old session
|
||||
try ctx.expectSentEvent("Inspector.detached", .{ .reason = "Render process gone." }, .{ .session_id = session_id });
|
||||
try ctx.expectSentEvent("Target.detachedFromTarget", .{ .sessionId = session_id, .reason = "Render process gone." }, .{});
|
||||
// New target should be created
|
||||
try testing.expectEqual(true, bc.target_id != null);
|
||||
try ctx.expectSentResult(.{ .targetId = bc.target_id.? }, .{ .id = 12 });
|
||||
}
|
||||
}
|
||||
|
||||
test "cdp.target: detachFromTarget without session" {
|
||||
var ctx = try testing.context();
|
||||
defer ctx.deinit();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub const protocol = @import("mcp/protocol.zig");
|
||||
pub const Version = protocol.Version;
|
||||
pub const router = @import("mcp/router.zig");
|
||||
pub const Server = @import("mcp/Server.zig");
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ test "MCP.Server - Integration: synchronous smoke test" {
|
||||
|
||||
try router.processRequests(server, &in_reader);
|
||||
|
||||
try testing.expectJson(.{ .jsonrpc = "2.0", .id = 1 }, out_alloc.writer.buffered());
|
||||
try testing.expectJson(.{ .jsonrpc = "2.0", .id = 1, .result = .{ .protocolVersion = "2024-11-05" } }, out_alloc.writer.buffered());
|
||||
}
|
||||
|
||||
test "MCP.Server - Integration: ping request returns an empty result" {
|
||||
|
||||
@@ -1,5 +1,14 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub const Version = enum {
|
||||
@"2024-11-05",
|
||||
@"2025-03-26",
|
||||
@"2025-06-18",
|
||||
@"2025-11-25",
|
||||
|
||||
pub const default: Version = .@"2024-11-05";
|
||||
};
|
||||
|
||||
pub const Request = struct {
|
||||
jsonrpc: []const u8 = "2.0",
|
||||
id: ?std.json.Value = null,
|
||||
|
||||
@@ -81,8 +81,12 @@ pub fn handleMessage(server: *Server, arena: std.mem.Allocator, msg: []const u8)
|
||||
|
||||
fn handleInitialize(server: *Server, req: protocol.Request) !void {
|
||||
const id = req.id orelse return;
|
||||
const result = protocol.InitializeResult{
|
||||
.protocolVersion = "2025-11-25",
|
||||
const version: protocol.Version = switch (server.app.config.mode) {
|
||||
.mcp => |opts| opts.version,
|
||||
else => .default,
|
||||
};
|
||||
const result: protocol.InitializeResult = .{
|
||||
.protocolVersion = @tagName(version),
|
||||
.capabilities = .{
|
||||
.resources = .{},
|
||||
.tools = .{},
|
||||
@@ -121,7 +125,7 @@ test "MCP.router - handleMessage - synchronous unit tests" {
|
||||
\\{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05","capabilities":{},"clientInfo":{"name":"test-client","version":"1.0.0"}}}
|
||||
);
|
||||
try testing.expectJson(
|
||||
\\{ "jsonrpc": "2.0", "id": 1, "result": { "capabilities": { "tools": {} } } }
|
||||
\\{ "jsonrpc": "2.0", "id": 1, "result": { "protocolVersion": "2024-11-05", "capabilities": { "tools": {} } } }
|
||||
, out_alloc.writer.buffered());
|
||||
out_alloc.writer.end = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user