Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Merge branch 'feat/e2e-sidecar' into feat/e2e-desktop
milind-soni91bc84f12071
diff --cc electron/companion-account-service.mjsindex fa3f29943e,ae89f0cc4f..406567c458--- a/electron/companion-account-service.mjs+++ b/electron/companion-account-service.mjs@@@ -780,16 -768,18 +777,24 @@@ export function createCompanionAccountSconst savedAddress = (document) => normalizeManagedCompanionEndpoint(document[MANAGED_COMPANION_ENDPOINT_FIELD]);+ let phonesUnused = false;+ /** "unused" (paired phones, none seen for 30 days) reads as false and is remembered for the release. */const currentNeed = async () => {+ phonesUnused = false;- if (requestedRecently()) return true;+ let need = null;try {- const need = await managedEndpointNeeded();- phonesUnused = need === MANAGED_ENDPOINT_PHONES_UNUSED;- return phonesUnused ? false : typeof need === "boolean" ? need : null;+ const answer = await managedEndpointNeeded();- need = typeof answer === "boolean" ? answer : null;++ phonesUnused = answer === MANAGED_ENDPOINT_PHONES_UNUSED;++ need = phonesUnused ? false : typeof answer === "boolean" ? answer : null;} catch {- return null;+ need = null;+ }+ if (need === false && relayCapable()) return false;- return requestedRecently() ? true : need;++ if (requestedRecently()) {++ phonesUnused = false;++ return true;+ }++ return need;};const releaseScheduledAt = () => {diff --cc electron/companion-account-service.test.mjsindex ecabc0592c,6ca8f4763c..c6c631b51d--- a/electron/companion-account-service.test.mjs+++ b/electron/companion-account-service.test.mjs@@@ -1403,34 -1405,6 +1405,35 @@@ describe("Companion account on demand"}});+ it("needs a tunnel only for an old phone seen this month once the encrypted connection is on", () => {+ const now = LAUNCH_AT;+ const on = (devices, extra = {}) => ({ enabled: true, devices, pairing: null, e2e: { enabled: true }, ...extra });+ const pairing = { code: "123456", token: `omb_pair_${"a".repeat(43)}`, expiresAt: now };+ expect(managedEndpointNeed(on([{ id: "old", lastSeenAt: now - HOUR_MS }]), now)).toBe(true);+ expect(managedEndpointNeed(on([{ id: "new", lastSeenAt: now, e2eAt: now - HOUR_MS }]), now)).toBe(false);- expect(managedEndpointNeed(on([{ id: "stale", lastSeenAt: now - 31 * 24 * HOUR_MS }]), now)).toBe(false);++ expect(managedEndpointNeed(on([{ id: "stale", lastSeenAt: now - 31 * 24 * HOUR_MS }]), now)).toBe("unused");++ expect(managedEndpointNeed(on([{ id: "new", lastSeenAt: now, e2eAt: now }, { id: "stale", lastSeenAt: now - 31 * 24 * HOUR_MS }]), now)).toBe("unused");+ expect(managedEndpointNeed(on([], { pairing }), now)).toBe(false);+ expect(managedEndpointNeed({ ...on([], { pairing }), e2e: { enabled: false } }, now)).toBe(true);+ });++ it("signs a relay-capable computer in without a tunnel when no phone needs one", async () => {+ for (const relayCapable of [true, false]) {+ const f = onDemandFixture({ initial: {}, relayCapable: () => relayCapable });+ f.companion.need = false;+ await f.service.requestCode("[email protected]");+ const state = await f.service.verifyCode("[email protected]", "12345678");+ expect(f.client.ensureInstallation).toHaveBeenCalledOnce();+ if (relayCapable) {+ expect(f.client.ensureEndpoint).not.toHaveBeenCalled();+ expect(state).toEqual({ available: true, status: "ready", email: "[email protected]" });+ } else {+ expect(f.client.ensureEndpoint).toHaveBeenCalledOnce();+ }+ f.service.dispose();+ }+ });+it("never asks for an address at launch or on the watchdog, whatever is paired", async () => {for (const need of [false, true, null]) {const f = onDemandFixture({diff --cc electron/companion-endpoint-need.mjsindex 0000000000,4ff9a2cc4a..64c10941efmode 000000,100644..100644--- a/electron/companion-endpoint-need.mjs+++ b/electron/companion-endpoint-need.mjs@@@ -1,0 -1,23 +1,27 @@@+ // Whether this computer's secure HTTPS address is needed. Each address holds one Cloudflare tunnel from a small+ // shared pool, so a phone paired once and never used again must not hold one forever.+ export const PHONE_UNUSED_RELEASE_MS = 30 * 24 * 60 * 60_000;++ /** Phones are paired but none was seen for 30 days: give the address back, keep it ready to come back. */+ export const MANAGED_ENDPOINT_PHONES_UNUSED = "unused";++ /** Only a known last-seen time more than 30 days old: a missing one, or one in the future, keeps the phone in use. */+ export function phoneUnused(device, now = Date.now()) {+ const seen = device?.lastSeenAt;+ return typeof seen === "number" && Number.isFinite(seen) && seen > 0 && now - seen > PHONE_UNUSED_RELEASE_MS;+ }+-/** true, false, "unused", or null when that cannot be told; only false or "unused" ever gives an address back. */++/** true, false, "unused", or null when that cannot be told; only false or "unused" ever gives an address back. Once++ * this computer serves the end-to-end encrypted connection, only an old phone (no `e2eAt`) needs a tunnel: a new++ * pairing never makes one. */+ export function managedEndpointNeed(state, now = Date.now()) {+ if (!state || typeof state !== "object" || state.enabled !== true || state.error) return null;+ if (state.devicesUnreadable === true || !Array.isArray(state.devices)) return null;- if (state.pairing) return true;- if (state.devices.length === 0) return false;++ const encrypted = state.e2e?.enabled === true;++ if (state.pairing && !encrypted) return true;++ const devices = encrypted ? state.devices.filter((device) => !Number.isSafeInteger(device?.e2eAt)) : state.devices;++ if (devices.length === 0) return false;+ const connected = new Set(Array.isArray(state.connectedDeviceIds) ? state.connectedDeviceIds : []);- return state.devices.some((device) => connected.has(device?.id) || !phoneUnused(device, now))++ return devices.some((device) => connected.has(device?.id) || !phoneUnused(device, now))+ || MANAGED_ENDPOINT_PHONES_UNUSED;+ }diff --cc src/components/CompanionSection.tsxindex a94c90c1da,f683c09441..90fa61877b--- a/src/components/CompanionSection.tsx+++ b/src/components/CompanionSection.tsx@@@ -5,8 -5,11 +5,9 @@@ importLoader2,LogOut,ShieldCheck,- Smartphone,- Trash2,Wifi,} from "lucide-react";+ import type { CompanionAccountState } from "../types/ogb";import {PhoneSetupFlowView,companionAccountActionError,diff --cc src/components/PairedPhones.tsxindex bb6b8adefe,0000000000..678b5f5fb7mode 100644,000000..100644--- a/src/components/PairedPhones.tsx+++ b/src/components/PairedPhones.tsx@@@ -1,142 -1,0 +1,143 @@@+import { useState } from "react";+import { MoreHorizontal } from "lucide-react";++import { t } from "@/lib/i18n";+import type { CompanionState, PhoneDevice, PhoneSetupController } from "./PhoneSetupFlow";+import { Switch } from "./SettingsPrimitives";+import { relative } from "./CompanionSection";++const ROUTE_LABEL = { wifi: "remote.devices.route.wifi", tailscale: "remote.devices.route.tailscale", relay: "remote.devices.route.relay" } as const;++/** Connected first, then the most recently seen. */+export function sortPhones(devices: PhoneDevice[], connected: ReadonlySet<string>): PhoneDevice[] {+ return [...devices].sort((a, b) => Number(connected.has(b.id)) - Number(connected.has(a.id)) || b.lastSeenAt - a.lastSeenAt);+}++/** One line under the name: the live route, or when it was last seen. An old app connected over plain HTTP says so. */+export function phoneSubtitle(device: PhoneDevice, connected: boolean, e2eEnabled: boolean): string {+ const parts: string[] = [];+ if (device.route) parts.push(t("remote.devices.connected", { route: t(ROUTE_LABEL[device.route]) }));+ else if (connected) parts.push(e2eEnabled && !device.e2eAt ? t("remote.devices.connectedLegacy") : t("remote.devices.connectedNow"));+ else parts.push(t("remote.devices.lastSeen", { when: relative(device.lastSeenAt) }));+ if (e2eEnabled && device.routes === "local") parts.push(t("remote.devices.localOnly"));+ return parts.join(" · ");+}++export function PhoneRow({ device, connected, state, c, onRemoved, defaultOpen = false }: {+ device: PhoneDevice;+ connected: boolean;+ state: CompanionState;+ c: PhoneSetupController;+ onRemoved: (name: string) => void;+ defaultOpen?: boolean;+}) {+ const [open, setOpen] = useState(defaultOpen);+ const e2eEnabled = state.e2e?.enabled === true;+ const live = connected || Boolean(device.route);+ return (+ <li className="border-t border-hairline/30 first:border-t-0">+ <div className="flex items-center gap-2.5 px-3 py-3">+ <span className="flex size-3 shrink-0 items-center justify-center" title={phoneSubtitle(device, connected, e2eEnabled)}>+ <span className={`size-2 rounded-full ${live ? "bg-success" : "bg-ink-tertiary/40"}`} />+ </span>+ <div className="min-w-0 flex-1">+ <div className="truncate text-[14px] font-medium text-ink">{device.name}</div>+ <div className="truncate text-[12px] text-ink-secondary">{phoneSubtitle(device, connected, e2eEnabled)}</div>++ {c.account?.paused && <div className="truncate text-[12px] text-ink-secondary">{t("remote.devices.paused")}</div>}+ </div>+ <button+ onClick={() => setOpen((value) => !value)}+ aria-expanded={open}+ aria-label={t("remote.devices.more", { name: device.name })}+ className="shrink-0 rounded p-1.5 text-ink-secondary hover:bg-control"+ >+ <MoreHorizontal size={15} />+ </button>+ </div>+ {open && (+ <div className="flex flex-col gap-3 border-t border-hairline/30 px-3 py-3">+ {e2eEnabled && (+ <div className="flex items-center justify-between gap-3">+ <div className="text-[12px] text-ink">{t("remote.devices.connectFrom")}</div>+ <div role="radiogroup" aria-label={t("remote.devices.connectFrom")} className="flex rounded-lg bg-inset p-0.5 text-[12px]">+ {(["any", "local"] as const).map((routes) => (+ <button+ key={routes}+ role="radio"+ aria-checked={(device.routes ?? "any") === routes}+ disabled={c.busy}+ onClick={() => void c.act((companion) => (companion.routes ? companion.routes(device.id, routes) : companion.state()))}+ className={`rounded-md px-2.5 py-1 ${(device.routes ?? "any") === routes ? "bg-card text-ink shadow-sm" : "text-ink-secondary"}`}+ >+ {routes === "any" ? t("remote.devices.anywhere") : t("remote.devices.localOnly")}+ </button>+ ))}+ </div>+ </div>+ )}+ <div className="flex items-center justify-between gap-3">+ <div>+ <div className="text-[12px] text-ink">{t("remote.devices.allowView")}</div>+ <div className="mt-0.5 text-[11px] text-ink-secondary">{t("remote.devices.allowViewDetail")}</div>+ </div>+ <Switch+ checked={device.cloudDesktopAccess}+ aria-label={t("remote.devices.viewAria", { name: device.name })}+ disabled={c.busy}+ onClick={() => void c.act((companion) => companion.cloudDesktop(device.id, !device.cloudDesktopAccess))}+ />+ </div>+ <div className="flex items-center justify-between gap-3">+ <div>+ <div className="text-[12px] text-ink">{t("remote.devices.allowBrowser")}</div>+ <div className="mt-0.5 text-[11px] text-ink-secondary">{t("remote.devices.allowBrowserDetail")}</div>+ </div>+ <Switch+ checked={device.browserControlAccess === true}+ aria-label={t("remote.devices.browserAria", { name: device.name })}+ disabled={c.busy}+ onClick={() => void c.act((companion) => companion.browserControl(device.id, !device.browserControlAccess))}+ />+ </div>+ <button+ disabled={c.busy}+ onClick={() => void c.act((companion) => companion.revoke(device.id)).then(() => onRemoved(device.name))}+ aria-label={t("remote.devices.remove", { name: device.name })}+ className="self-start text-[12px] text-danger hover:underline disabled:opacity-40"+ >+ {t("remote.devices.removePhone")}+ </button>+ </div>+ )}+ </li>+ );+}++/** One grouped card: a dot, the name and one line per phone. Remove acts at once. */+export function PairedPhones({ c, state }: { c: PhoneSetupController; state: CompanionState }) {+ const [removed, setRemoved] = useState<string | null>(null);+ const connected = new Set(state.connectedDeviceIds ?? []);+ if (state.devices.length === 0 && !removed) return null;+ return (+ <>+ {state.devices.length > 0 && (+ <ul className="rounded-xl border border-hairline/40 bg-card">+ {sortPhones(state.devices, connected).map((device) => (+ <PhoneRow key={device.id} device={device} connected={connected.has(device.id)} state={state} c={c} onRemoved={setRemoved} />+ ))}+ </ul>+ )}+ {removed && (+ <div role="status" className="mt-2 text-[12px] text-ink-secondary">{t("remote.devices.removed", { name: removed })}</div>+ )}+ </>+ );+}++/** The line under Remote access: where phones can reach this computer from, once it is end-to-end encrypted. */+export function relayStatusLine(state: Pick<CompanionState, "enabled" | "e2e">): string | null {+ if (!state.enabled || state.e2e?.enabled !== true) return null;+ if (state.e2e.relay === "connected") return t("remote.e2e.anywhere");+ if (state.e2e.relay === "connecting") return t("remote.e2e.connecting");+ return t("remote.e2e.local");+}