Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions apps/server/src/browserUse/BrowserUseDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import * as PinnedTool from "../tools/PinnedTool.ts";

// Update the version, sizes and hashes together from the assets of
// https://github.com/aislopware/oni-browser-use/releases (tag v<version>).
const VERSION = "4.8.1";
export const VERSION = "4.9.0";
const BINARIES = {
"darwin-arm64": {
bytes: 7_186_880,
sha256: "1e9643bd93b39e865c2fdf443194aa11e779b5349f3a68282e71a51c8e498e33",
bytes: 7_220_160,
sha256: "e84a4c47b133510c0bc82c0f54ddc05669962209b62e5e540443d9e31b77d39e",
},
"linux-x64": {
bytes: 8_460_336,
sha256: "63cbac9b0a6ba5211b1949b116995b697d185c512414a68e76574484aba08dd0",
bytes: 8_504_504,
sha256: "f0a4f15c8ed9714d1d339307eda75bb6419761baf334d9629953d586c351c359",
},
} as const;

Expand Down
8 changes: 4 additions & 4 deletions apps/server/src/browserUse/BrowserUseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ const STOP_TIMEOUT = "20 seconds";
const SHARED_ENVIRONMENT = { OBU_NO_UPDATE_CHECK: "1" };
// obu tells the agent these in place of its own guess that an attached browser is the user's.
// The personal browser needs none: that guess is right there.
const PANEL_NOTE =
"Use these tools for browser work: they drive your tab in Onicode's Browser panel, which the user watches.";
const ISOLATED_NOTE =
"Use these tools for browser work: they drive a Chrome of your own, which the user cannot see.";
export const PANEL_NOTE =
"Use these tools for browser work, in place of starting a browser of your own: they drive a tab of your own in Onicode's Browser panel, where the user can watch. It is not the user's own browser, so a task that names theirs is not for these tools. It opens http and https pages only: for a local file, serve its folder over http, as with `python3 -m http.server`, and open that address.";
export const ISOLATED_NOTE =
"Use these tools for browser work, in place of starting a browser of your own: they drive a Chrome of your own, which the user cannot see. It is not the user's own browser, so a task that names theirs is not for these tools.";

const RealBrowserConfig = Schema.fromJsonString(Schema.Record(Schema.String, Schema.Unknown));
const decodeRealBrowserConfig = Schema.decodeUnknownOption(RealBrowserConfig);
Expand Down
5 changes: 4 additions & 1 deletion apps/server/src/preview/PreviewCdpBridge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ it.live("serves a thread's panel tabs as a debug endpoint only its driver can re
});
expect(yield* page.call(4, "Page.navigate", { url: "file:///etc/passwd" })).toMatchObject({
id: 4,
error: { message: "The Browser panel opens http and https pages only." },
error: {
message:
"The Browser panel opens http and https pages only. To open a local file, serve its folder over http, as with `python3 -m http.server`, and open that address.",
},
});
expect(yield* page.call(5, "Target.getTargets")).toMatchObject({
id: 5,
Expand Down
5 changes: 3 additions & 2 deletions apps/server/src/preview/PreviewCdpBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
cdpErrorMessage,
driverTargetId,
isPanelUrl,
PANEL_URL_REQUIRED,
pageCommandParams,
routePageCommand,
} from "./previewCdpPolicy.ts";
import * as ServerBrowser from "./ServerBrowser.ts";
Expand Down Expand Up @@ -90,7 +92,6 @@ const HOST = "127.0.0.1";
const ENDPOINT_PATH = /^\/k\/([A-Za-z0-9_-]+)(\/.*)$/;
const BROWSER_SOCKET = "/devtools/browser/onicode";
const PAGE_SOCKET = "/devtools/page/";
const PANEL_URL_REQUIRED = "The Browser panel opens http and https pages only.";
const textDecoder = new TextDecoder();

const stringField = (value: unknown, field: string) =>
Expand Down Expand Up @@ -244,7 +245,7 @@ const make = Effect.gen(function* () {
return Effect.fail(new CommandRefused({ message: PANEL_URL_REQUIRED }));
}
return Effect.tryPromise({
try: () => driver.send(command.method, command.params ?? {}),
try: () => driver.send(command.method, pageCommandParams(command.method, command.params)),
catch: (cause) => new CommandRefused({ message: cdpErrorMessage(cause) }),
});
}
Expand Down
188 changes: 187 additions & 1 deletion apps/server/src/preview/ServerBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,12 +969,25 @@ it.effect("a tab the desktop does not draw is marked headless, so the desktop st
operation: "open",
input: { reuseExistingTab: false, show: false },
});
// A hidden window: the desktop mounts the agent's tab, but its page answers no capture.
desktopRendersNext = true;
heldStill = () => Promise.reject(new Error("No frame"));
const hiddenTab = yield* broker.invoke<PreviewAutomationStatus>({
scope,
operation: "open",
input: { reuseExistingTab: false, show: false },
});
heldStill = null;
const { sessions } = yield* manager.list({ threadId: scope.thread.threadId });
const headlessOf = (id: string | null) =>
sessions.find((session) => session.tabId === id)?.headless;

expect(headlessOf(tabId)).toBe(true);
expect(headlessOf(desktopTab.tabId)).toBeUndefined();
expect(headlessOf(hiddenTab.tabId)).toBe(true);
expect(desktopConnections.map(({ browser }) => browser.close.mock.calls.length)).toEqual([
0, 1,
]);
}),
).pipe(Effect.provide(layer)),
);
Expand Down Expand Up @@ -4250,6 +4263,63 @@ it.effect("an agent's own driver works its tab's page until a human takes contro
["mouseReleased", 5],
]);

// A capture's size goes to the page the way a resize does, since Playwright holds its metrics.
const { page } = contexts[0]!;
const sized = () =>
driver.send("Emulation.setDeviceMetricsOverride", {
width: 1920,
height: 1080,
deviceScaleFactor: 1,
mobile: false,
});
// A driver pipelines: the clear that follows a set undoes it, however long the set takes.
page.setViewportSize.mockClear();
yield* Effect.promise(() =>
Promise.all([sized(), driver.send("Emulation.clearDeviceMetricsOverride", undefined)]),
);
const widths = page.setViewportSize.mock.calls.map(([size]) => size?.width);
expect([widths[0], widths.at(-1)]).toEqual([1920, 1280]);
yield* Effect.promise(sized);
expect(page.setViewportSize).toHaveBeenLastCalledWith({ width: 1920, height: 1080 });

// A driver that leaves while the page has not answered its size change: the wait is
// bounded, and the page has its own size back before the session detaches, since a clear
// sent after that reaches no page.
const left = Promise.withResolvers<unknown>();
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
let unanswered: Promise<string> = Promise.resolve("never sent");
yield* Effect.scoped(
Effect.gen(function* () {
const leaving = yield* drivers.attach({
threadId,
tabId,
agentSessionId,
onEvent: () => {},
});
contexts[0]!.sessions
.at(-1)!
.detach.mockImplementation(async () =>
left.resolve(page.setViewportSize.mock.lastCall?.[0]),
);
page.setViewportSize.mockImplementationOnce(() => new Promise(() => {}));
unanswered = leaving
.send("Emulation.setDeviceMetricsOverride", { width: 390, height: 844 })
.then(String, String);
}),
);
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(5_000));
expect(yield* Effect.promise(() => unanswered)).toContain("did not answer a size change");
expect(yield* Effect.promise(() => left.promise)).toEqual({ width: 1280, height: 800 });
// A pointer move the page never answers holds the command behind it that long and no more.
session.send.mockImplementationOnce(() => new Promise(() => {}));
yield* Effect.promise(() => mouse("mouseMoved", 9));
const behind = driver.send("Runtime.evaluate", { expression: "2" });
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(5_000));
yield* Effect.promise(() => behind);
expect(session.send).toHaveBeenLastCalledWith("Runtime.evaluate", { expression: "2" });
vi.useRealTimers();
yield* Effect.promise(sized);

const viewer = yield* browser.attachViewer(viewerInput(tabId, true));
yield* viewer.input({ type: "takeControl" });
const refused = yield* Effect.promise(() =>
Expand All @@ -4260,13 +4330,129 @@ it.effect("an agent's own driver works its tab's page until a human takes contro
);
expect(refused).toMatchObject({ reason: "humanControl" });
expect(session.send).not.toHaveBeenCalledWith("Input.dispatchKeyEvent", expect.anything());
// The page gets its own size back even while the human holds it.
yield* Effect.promise(() => driver.send("Emulation.clearDeviceMetricsOverride", undefined));
expect(page.setViewportSize).toHaveBeenLastCalledWith({ width: 1280, height: 800 });
expect(session.send).not.toHaveBeenCalledWith(
expect.stringContaining("DeviceMetricsOverride"),
expect.anything(),
);

contexts[0]!.page.emit("close");
yield* Effect.promise(() => driver.closed);
// A browser that has stopped answering must not hold the driver's release, or shutdown.
session.detach.mockReturnValue(new Promise(() => {}));
}),
).pipe(Effect.provide(layer)),
).pipe(Effect.ensuring(Effect.sync(() => vi.useRealTimers())), Effect.provide(layer)),
);

it.effect("a driver's captures of a desktop tab take their turn with its size changes", () =>
Effect.scoped(
Effect.gen(function* () {
const { broker } = yield* ready;
const drivers = yield* ServerBrowser.ServerBrowserDrivers;
desktopRendersNext = true;
const opened = yield* broker.invoke<PreviewAutomationStatus>({
scope,
operation: "open",
input: { reuseExistingTab: false, show: false },
});
const driver = yield* drivers.attach({
threadId: scope.thread.threadId,
tabId: PreviewTabId.make(opened.tabId!),
agentSessionId: Broker.hostAssignmentKey(scope),
onEvent: () => {},
});
const session = desktopConnections[0]!.context.sessions.at(-1)!;
const sent = () =>
session.send.mock.calls.flatMap(([method, input]) =>
method === "Page.captureScreenshot"
? ["capture"]
: method === "Emulation.setDeviceMetricsOverride"
? [`set ${(input as { width: number }).width}`]
: method === "Emulation.clearDeviceMetricsOverride"
? ["clear"]
: [],
);
const size = (width: number) =>
driver.send("Emulation.setDeviceMetricsOverride", { width, height: 1080 });
const shot = () =>
driver.send("Page.captureScreenshot", { format: "png" }).then(() => "captured", String);
const clear = () => driver.send("Emulation.clearDeviceMetricsOverride", undefined);

// A driver pipelines: the capture is taken at the size set before it, not after the clear
// behind it. The first capture is the server's own, measuring what the page paints.
yield* Effect.promise(() => Promise.all([size(1920), shot(), clear()]));
expect(sent()).toEqual(["capture", "set 1920", "capture", "clear"]);

// A capture the page answers too late: Chromium then puts back the size that capture
// began at, so no other is taken until it has answered and the size is repaired.
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
yield* Effect.promise(() => size(1920));
const late = Promise.withResolvers<Record<string, unknown>>();
session.send.mockImplementationOnce(() => late.promise);
const missed = shot();
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(3_000));
expect(yield* Effect.promise(() => missed)).toContain("not producing screenshot frames");
// A clear partway through that capture costs Chromium the size to go back to, so it is
// left to the repair, and the driver hears when the capture has not answered by then.
session.send.mockClear();
const leftSized = clear().then(() => "cleared", String);
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(5_000));
expect(yield* Effect.promise(() => leftSized)).toContain("did not answer a size change");
yield* Effect.promise(() => size(390));
expect(yield* Effect.promise(shot)).toContain("not producing screenshot frames");
expect(sent()).toEqual(["set 390"]);

const repaired = Promise.withResolvers<void>();
const answer = session.send.getMockImplementation()!;
session.send.mockImplementation(async (method, input) => {
const result = await answer(method, input);
if (method === "Emulation.setDeviceMetricsOverride") repaired.resolve();
return result;
});
session.send.mockClear();
late.resolve({ data: "ZnJhbWU=" });
yield* Effect.promise(() => repaired.promise);
expect(yield* Effect.promise(shot)).toBe("captured");
expect(sent()).toEqual(["set 390", "capture"]);

// A repair the page refuses leaves its size unknown: no capture until the driver's next
// size change lands.
const again = Promise.withResolvers<Record<string, unknown>>();
session.send.mockImplementationOnce(() => again.promise);
const missedAgain = shot();
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(3_000));
expect(yield* Effect.promise(() => missedAgain)).toContain("not producing screenshot frames");
const refusedRepair = Promise.withResolvers<void>();
let refuse = true;
session.send.mockImplementation(async (method, input) => {
if (method !== "Emulation.setDeviceMetricsOverride" || !refuse)
return answer(method, input);
refuse = false;
refusedRepair.resolve();
throw new Error("No page");
});
again.resolve({ data: "ZnJhbWU=" });
yield* Effect.promise(() => refusedRepair.promise);
expect(yield* Effect.promise(shot)).toContain("not producing screenshot frames");
yield* Effect.promise(() => size(390));
expect(yield* Effect.promise(shot)).toBe("captured");

// A clear behind a late capture is sent once that capture has answered, and only then.
session.send.mockImplementation(answer);
const last = Promise.withResolvers<Record<string, unknown>>();
session.send.mockImplementationOnce(() => last.promise);
const missedLast = shot();
yield* Effect.promise(() => vi.advanceTimersByTimeAsync(3_000));
expect(yield* Effect.promise(() => missedLast)).toContain("not producing screenshot frames");
session.send.mockClear();
const handedBack = clear();
last.resolve({ data: "ZnJhbWU=" });
yield* Effect.promise(() => handedBack);
expect(sent()).toEqual(["clear"]);
}),
).pipe(Effect.ensuring(Effect.sync(() => vi.useRealTimers())), Effect.provide(layer)),
);

it.live("a page's title change without navigation reaches its session", () =>
Expand Down
Loading