Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
feat(surface): keep the built-in browser beside the computer so one task can use both
AdityaUmaleb16d1b9d4aa0
diff --git a/server/index.ts b/server/index.tsindex 6f5d35afc4..5dc42ed207 100644--- a/server/index.ts+++ b/server/index.ts@@ -512,7 +512,7 @@ import { keepTransient, lazyClaimFailedText, lazyClaimRetryText, settledWithin,import { CONTROL_ACT_WITHIN_MS, CONTROL_BUSY, CONTROL_HANDED_BACK, CONTROL_WAIT_MS, CONTROL_WAITED, controlRefusal, type ControlState } from "./control-client.ts";import { computerFreeAfterText, computerParkedText, computerPersonAwayText, computerStoppedWaitingText, computerWaitingText, type ComputerHolder } from "./computer-wait.ts";import { modelContextWindow } from "./model-context-window.ts";-import { browserIsThePlace, chosenPin, cloudPlaceRefusal, computerSkippedNote, computerToolsRefusal, handoffUsesOwnCloud, isHandedWork, ownCloudStateWithin, parseSurface, pinSource, PlaceUnavailableError, placeUnavailable, resolveSurface, surfaceLabel, surfaceOfComputerKind, surfacePrompt, type ComputerWant, type PlaceSource, type Surface } from "./surface.ts";+import { browserIsThePlace, chosenPin, cloudPlaceRefusal, computerSkippedNote, computerToolsRefusal, handoffUsesOwnCloud, isHandedWork, openBrowserBeside, ownCloudStateWithin, parseSurface, pinSource, PlaceUnavailableError, placeUnavailable, resolveSurface, surfaceLabel, surfaceOfComputerKind, surfacePrompt, type ComputerWant, type PlaceSource, type Surface } from "./surface.ts";import { cloudRefusal, type PlaceRow } from "../shared/place-view.ts";import { cloudFailureOf, cloudLimitFields, cloudLimitText } from "./boat-limit.ts";import {@@ -8313,13 +8313,13 @@ async function beginTurn(if (liveBot &&plan.browser &&- !(plan.computer === undefined && mountedComputer && mountedComputer !== "local" && !plan.botOpened) &&builtInBrowserEnabled(cfg) &&liveBot.browser !== false &&instance.adapter.capabilities.browserMcp === true) {const selectedProfile = liveBot.browserProfile;- browser = await browserIntegration(bot.id, selectedProfile, { threadId, generation: dispatchClaimId });+ browser = await openBrowserBeside(() => browserIntegration(bot.id, selectedProfile, { threadId, generation: dispatchClaimId }),+ mountedComputer, (error) => console.warn(`[browser] thread ${threadId}: left out beside ${mountedComputer} (${redactSecretsInText(String(error))})`));if (browser) integrations.browser = browser.integration;if (browser) {const spec = browser.spec;diff --git a/server/surface-pins.e2e.test.ts b/server/surface-pins.e2e.test.tsindex 8aaf3ba853..7641688569 100644--- a/server/surface-pins.e2e.test.ts+++ b/server/surface-pins.e2e.test.ts@@ -275,7 +275,7 @@ describe("surface pin provenance against the real server", () => {await stop();});- it("mounts the built-in browser beside the computer in a thread a teammate opened, and not in a person's thread", async () => {+ it("mounts the built-in browser beside the computer in a person's thread and in a thread a teammate opened", async () => {await start(true);const { bot } = await apiOk("POST", "/api/bots", { name: "Delegated Browser Bot" });const [own, delegated] = (await Promise.all([@@ -310,7 +310,7 @@ describe("surface pin provenance against the real server", () => {await start(true);const person = await turnIn(own.threadId);expect(person.computer?.args.some((arg: string) => arg.includes("container-mcp")), `${computer} person`).toBe(true);- expect(person.browser, `${computer} person`).toBeUndefined();+ expect(person.browser, `${computer} person`).toBeDefined();const teammate = await turnIn(delegated.threadId);expect(teammate.computer?.args.some((arg: string) => arg.includes("container-mcp")), `${computer} teammate`).toBe(true);expect(teammate.browser, `${computer} teammate`).toBeDefined();@@ -474,7 +474,9 @@ describe("surface pin provenance against the real server", () => {const tester = (await apiOk("POST", "/api/bots", { name: "Tester" })).bot;await apiOk("PATCH", `/api/bots/${tester.id}`, { modelSelection: { ...tester.modelSelection, instanceId: "teammate" } });await stop();- editSavedBot(tester.id, saved => { saved.browserProfile = "guest"; });+ // Both bots use the guest browser: the asker's own turn mounts it beside its computer too, and the fixture's inert+ // browser binary cannot prepare a saved profile in time.+ for (const id of [asker.id, tester.id]) editSavedBot(id, saved => { saved.browserProfile = "guest"; });await start(true);const { task: own } = await apiOk("POST", `/api/bots/${asker.id}/tasks`, {});resetTurn();diff --git a/server/surface.test.ts b/server/surface.test.tsindex 89f6e829d5..d5f8c54c34 100644--- a/server/surface.test.ts+++ b/server/surface.test.ts@@ -8,6 +8,7 @@ import {computerToolsRefusal,handoffUsesOwnCloud,isHandedWork,+ openBrowserBeside,ownCloudStateWithin,parseSurface,pinSource,@@ -70,6 +71,25 @@ describe("handoffUsesOwnCloud", () => {});});+describe("openBrowserBeside", () => {+ it("leaves out a browser that fails beside a computer, so the turn goes on with the computer", async () => {+ const skipped: unknown[] = [];+ const failing = () => Promise.reject(new Error("Could not safely prepare saved browser logins."));+ for (const computer of ["cloud", "vm", "local"] as const) {+ await expect(openBrowserBeside(failing, computer, (error) => skipped.push(error))).resolves.toBeNull();+ }+ expect(skipped).toHaveLength(3);+ });++ it("keeps the failure where the browser is the only place, and opens it as usual otherwise", async () => {+ const skipped: unknown[] = [];+ await expect(openBrowserBeside(() => Promise.reject(new Error("no browser")), null, (error) => skipped.push(error))).rejects.toThrow("no browser");+ await expect(openBrowserBeside(() => Promise.resolve({ session: "s" }), "cloud", (error) => skipped.push(error))).resolves.toEqual({ session: "s" });+ await expect(openBrowserBeside(() => Promise.resolve(null), null, (error) => skipped.push(error))).resolves.toBeNull();+ expect(skipped).toEqual([]);+ });+});+describe("pinSource", () => {it("counts a turn's own pin to the place Works on names as Works on, so This computer is not asked for again", () => {expect(pinSource("local", { surfaceSource: "auto" }, "local")).toBe("works-on");@@ -121,14 +141,22 @@ describe("resolveSurface", () => {expect(plan.note).toMatch(/no browser and no computer/);});- it("a computer destination mounts only that computer: one place per turn", () => {+ it("a computer destination mounts the built-in browser beside it, so one task can use both", () => {for (const destination of ["cloud", "vm", "local"] as const) {- expect(resolveSurface({ destination, browserOn: true })).toEqual({ computer: destination, browser: false, pinned: null, note: "" });+ expect(resolveSurface({ destination, browserOn: true })).toEqual({ computer: destination, browser: true, pinned: null, note: "" });expect(resolveSurface({ destination, browserOn: false })).toMatchObject({ computer: destination, browser: false });}});- it("a thread a bot opened mounts the built-in browser beside any computer, since no person watches it", () => {+ it("a pinned computer keeps the built-in browser beside it, in a person's thread and a bot's", () => {+ for (const pinnedSurface of ["cloud", "vm", "local"] as const) {+ expect(resolveSurface({ destination: undefined, pinnedSurface, browserOn: true })).toEqual({ computer: pinnedSurface, browser: true, pinned: pinnedSurface, note: "" });+ expect(resolveSurface({ destination: "browser", pinnedSurface, browserOn: true })).toMatchObject({ computer: pinnedSurface, browser: true });+ expect(resolveSurface({ destination: undefined, pinnedSurface, browserOn: false })).toMatchObject({ computer: pinnedSurface, browser: false });+ }+ });++ it("a thread a bot opened is marked, and gets the same places as a person's", () => {for (const destination of ["cloud", "vm", "local"] as const) {expect(resolveSurface({ destination, browserOn: true, botOpened: true })).toEqual({ computer: destination, browser: true, pinned: null, note: "", botOpened: true });expect(resolveSurface({ destination, browserOn: false, botOpened: true })).toMatchObject({ computer: destination, browser: false });@@ -170,7 +198,7 @@ describe("resolveSurface", () => {for (const destination of ["browser", undefined] as const) {for (const pin of ["cloud", "vm", "local"] as const) {expect(resolveSurface({ destination, pinnedSurface: pin, browserOn: true }))- .toEqual({ computer: pin, browser: false, pinned: pin, note: "" });+ .toEqual({ computer: pin, browser: true, pinned: pin, note: "" });}}});@@ -200,8 +228,12 @@ describe("surfacePrompt", () => {expect(text).toMatch(/Two surfaces are mounted/);expect(text).toMatch(/Web tasks → the built-in browser/);expect(text).toMatch(/Desktop apps, files and shell → the cloud computer tools/);- expect(text).toMatch(/Pick one surface for a task and stay on it/);+ expect(text).not.toMatch(/Pick one surface for a task and stay on it/);+ expect(text).toContain("one task can use each for its own part (for example a website in the built-in browser and a desktop app or file on the computer) without asking anyone to switch");+ expect(text).toContain("keep any one website on one surface for the whole task");expect(text).toMatch(/say which surface — the Browser tab or the cloud computer/);+ expect(surfacePrompt({ computer: "local", browser: true })).toContain("one task can use each for its own part");+ expect(surfacePrompt({ computer: "cloud", browser: false })).not.toContain("one task can use each for its own part");});it("starts a new task on the computer where the bot already saw its site signed in, without a check that would start it", () => {diff --git a/server/surface.ts b/server/surface.tsindex 13f518f2df..bb06e4dd11 100644--- a/server/surface.ts+++ b/server/surface.ts@@ -21,7 +21,7 @@ export interface SurfacePlan {browser: boolean;pinned: Surface | null;note: string;- /** A bot opened this thread, so no person watches its Screen panel: the browser is mounted beside any computer. */+ /** A bot opened this thread, so no person watches its Screen panel: a missing place is reported to that bot. */botOpened?: true;}@@ -48,7 +48,8 @@ export function computerSkippedNote(cause: string): string {return ` This turn answers a Team incidents report and your computer could not be prepared for it (${why}), so no computer is mounted: you cannot open, click or check anything on it this turn. Act through your teammates, or tell the person what only they can fix — never claim to have checked that computer yourself.`;}-/** One place per turn, plus the browser beside it in a thread a bot opened. A person's pin beats the bot's default (only Off overrides it), an Auto pin yields to a Works on change, and a pin the turn cannot honour is reported, never swapped. */+/** One computer per turn, with the built-in browser beside it whenever the browser is on, so one task can use a website+ * and a desktop app at once. A person's pin beats the bot's default (only Off overrides it), an Auto pin yields to a Works on change, and a pin the turn cannot honour is reported, never swapped. */export function resolveSurface(input: {destination: Destination;pinnedSurface?: Surface | null;@@ -59,12 +60,11 @@ export function resolveSurface(input: {return input.botOpened ? { ...plan, botOpened: true } : plan;}-function placeOf(input: { destination: Destination; pinnedSurface?: Surface | null; browserOn: boolean; botOpened?: boolean }): SurfacePlan {+function placeOf(input: { destination: Destination; pinnedSurface?: Surface | null; browserOn: boolean }): SurfacePlan {const { destination, browserOn } = input;if (destination === "off") {return { computer: "off", browser: false, pinned: null, note: OFF_NOTE };}- const beside = input.botOpened === true && browserOn;const pin = input.pinnedSurface ?? null;if (pin === "browser") {if (browserOn) return { computer: "off", browser: true, pinned: "browser", note: "" };@@ -72,14 +72,14 @@ function placeOf(input: { destination: Destination; pinnedSurface?: Surface | nureturn { computer: "off", browser: false, pinned: pin,note: " This conversation is pinned to the built-in browser, but its tools are unavailable this turn. No computer is mounted instead. Do not claim to have used it; a different place must be selected before acting there." };}- if (pin) return { computer: pin, browser: beside, pinned: pin, note: "" };+ if (pin) return { computer: pin, browser: browserOn, pinned: pin, note: "" };if (destination === "browser") {return browserOn? { computer: "off", browser: true, pinned: null, note: "" }: { computer: "off", browser: false, pinned: null, note: NO_BROWSER_NOTE };}if (destination !== undefined) {- return { computer: destination, browser: beside, pinned: null, note: "" };+ return { computer: destination, browser: browserOn, pinned: null, note: "" };}return { computer: undefined, browser: browserOn, pinned: null, note: "" };}@@ -105,6 +105,18 @@ export function ownCloudStateWithin(status: Promise<{ box?: { state?: string | n]).finally(() => clearTimeout(timer));}+/** Opens the built-in browser for a turn. Beside a computer it is extra: a browser that fails to open is left out and+ * the turn goes on with the computer alone, never failed. Where the browser is the only place, its failure stands. */+export async function openBrowserBeside<T>(open: () => Promise<T | null>, computer: Surface | null, skipped: (error: unknown) => void): Promise<T | null> {+ try {+ return await open();+ } catch (error) {+ if (!computer) throw error;+ skipped(error);+ return null;+ }+}+export function browserIsThePlace(plan: SurfacePlan): boolean {return plan.computer === "off" && plan.browser;}@@ -173,6 +185,10 @@ const YOUR_COMPUTER_ALONE =const CONNECTED_APP_FIRST =" When a connected app can do the task, use it before any browser or screen. If that app is refused or fails, say so in one plain line with what fixes it (connecting it, or who can), and ask before doing the task on a screen instead.";+/** Both surfaces stay mounted for the whole turn: a task that needs a website and a desktop app uses each for its part. */+const BOTH_IN_ONE_TASK =+ "Both stay available for the whole turn, so one task can use each for its own part (for example a website in the built-in browser and a desktop app or file on the computer) without asking anyone to switch; keep any one website on one surface for the whole task, and say in one short sentence which surface you move to.";+const RESTATE_SENTENCE =" Before your first action on a screen or page in a task, say in one short sentence where you are working, using that same name.";@@ -222,13 +238,13 @@ export function surfacePrompt(let text = "";if (mounted.computer === "local" && !opts.cloudHome) {text = mounted.browser- ? ` Two surfaces are mounted this turn: the built-in browser (the browser server's browser_navigate, browser_snapshot, browser_click, browser_fill and friends) and this computer (the computer server's tools). Web tasks → the built-in browser. Desktop apps, files and shell → this computer tools. Pick one surface for a task and stay on it; if you need the user to sign in, say which surface — the Browser tab or this computer.${YOUR_COMPUTER_BESIDE_BROWSER}`+ ? ` Two surfaces are mounted this turn: the built-in browser (the browser server's browser_navigate, browser_snapshot, browser_click, browser_fill and friends) and this computer (the computer server's tools). Web tasks → the built-in browser. Desktop apps, files and shell → this computer tools. ${BOTH_IN_ONE_TASK} If you need the user to sign in, say which surface — the Browser tab or this computer.${YOUR_COMPUTER_BESIDE_BROWSER}`: YOUR_COMPUTER_ALONE;} else if (computer && mounted.browser) {// The two browsers usually share no cookies: "Web tasks → the built-in browser" alone sent a bot whose site was// signed in on the computer to a signed-out tab, where it asked the person to sign in again.text =- ` Two surfaces are mounted this turn: the built-in browser (the browser server's browser_navigate, browser_snapshot, browser_click, browser_fill and friends) and ${computer} (the computer server's tools). Web tasks → the built-in browser. Desktop apps, files and shell → ${computer} tools. Pick one surface for a task and stay on it; if you need the user to sign in, say which surface — the Browser tab or ${computer}. The built-in browser and ${computer} may not share sign-ins. When you pick the surface for a new task and the user did not name a place, if you already saw its site signed in on ${computer} with the account it needs, pick ${computer} instead of the Browser tab, unless the task needs something already in the Browser tab, such as a cart; if the site turns out signed out on ${computer}, use the Browser tab when it is signed in there.`;+ ` Two surfaces are mounted this turn: the built-in browser (the browser server's browser_navigate, browser_snapshot, browser_click, browser_fill and friends) and ${computer} (the computer server's tools). Web tasks → the built-in browser. Desktop apps, files and shell → ${computer} tools. ${BOTH_IN_ONE_TASK} If you need the user to sign in, say which surface — the Browser tab or ${computer}. The built-in browser and ${computer} may not share sign-ins. When you pick the surface for a new task and the user did not name a place, if you already saw its site signed in on ${computer} with the account it needs, pick ${computer} instead of the Browser tab, unless the task needs something already in the Browser tab, such as a cart; if the site turns out signed out on ${computer}, use the Browser tab when it is signed in there.`;} else if (computer) {text =` Everything you do on screen happens on ${computer}, web pages included, through its own browser; there is no separate built-in browser this turn. If you need the user to sign in, tell them it is on ${computer}.`;