Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Chat: a page card is a clean preview that opens from anywhere on it
luishron1233ee1296d6b9f
diff --git a/src/components/app/AppChrome.tsx b/src/components/app/AppChrome.tsxindex f8a52d7e78..3addc7e323 100644--- a/src/components/app/AppChrome.tsx+++ b/src/components/app/AppChrome.tsx@@ -70,6 +70,44 @@ export function AppHeader({);}+/** The compact page card's footer: what the page is, with the card's own tools shown on hover, focus and touch. */+export function PageCardFooter({ title, detail, updated, infoId, infoOpen, onInfo, onReload, reloading = false }: {+ title: string;+ detail: string;+ updated: boolean;+ infoId: string;+ infoOpen: boolean;+ onInfo(): void;+ onReload?: () => void;+ reloading?: boolean;+}) {+ return (+ <div data-page-footer className="flex items-center gap-3 border-t border-ink/[0.08] px-4 py-3">+ <AppWindow aria-hidden="true" size={16} className="shrink-0 text-ink-tertiary" />+ <div className="min-w-0 flex-1">+ <div title={title} className="truncate text-[13px] font-medium leading-[18px] text-ink"><bdi dir="auto">{title}</bdi></div>+ <div className="truncate text-xs leading-4 text-ink-tertiary">{detail}</div>+ </div>+ {updated && <span role="status" data-page-updated className="shrink-0 rounded-full bg-accent/15 px-2 py-px text-[13px] font-medium text-accent-text">{t("chat.preview.updated")}</span>}+ <div className={cn(+ "relative z-[1] -me-1.5 flex shrink-0 items-center gap-0.5 transition-opacity duration-[120ms] motion-reduce:transition-none",+ "group-hover/page:opacity-100 group-focus-within/page:opacity-100 touch:opacity-100",+ !infoOpen && "opacity-0",+ )}>+ <button type="button" aria-label={t("app.info")} title={t("app.info")} aria-expanded={infoOpen} aria-controls={infoId} onClick={onInfo}+ className={cn(ICON_BUTTON, infoOpen && "bg-ink/[0.07] text-ink")}>+ <Info size={15} />+ </button>+ {onReload && (+ <button type="button" aria-label={t("app.reload")} title={t("app.reload")} onClick={onReload} disabled={reloading} className={ICON_BUTTON}>+ <RotateCw size={14} className={cn(reloading && "animate-spin motion-reduce:animate-none")} />+ </button>+ )}+ </div>+ </div>+ );+}+function OriginList({ origins }: { origins: readonly string[] }) {if (!origins.length) return <span className="text-ink-tertiary">{t("app.info.nothing")}</span>;return <span className="[overflow-wrap:anywhere]">{origins.join(", ")}</span>;diff --git a/src/components/app/AppMessage.compact.test.ts b/src/components/app/AppMessage.compact.test.tsnew file mode 100644index 0000000000..57e2cd7414--- /dev/null+++ b/src/components/app/AppMessage.compact.test.ts@@ -0,0 +1,90 @@+// @vitest-environment happy-dom+// A bot's page in its own chat: a compact card that opens the chat's page pane from anywhere on it.+import { act, createElement } from "react";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import { BotEditorStore, type Message, type useStore } from "@/state/store";+import type { AppPayload } from "../../../shared/app";+import { botHtmlDocument } from "../../../shared/app-frame/document";+import { createAppPool } from "./app-pool";+import { AppMessage, forgetAppRows } from "./AppMessage";+import { PAGE_PREVIEW_HEIGHT, PagePaneContext } from "./PageTools";++(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;++const payload = (more: Partial<AppPayload> = {}): AppPayload => ({+ v: 1, source: "html", title: "Pricing", doc: "d0", rev: 0, bytes: 900, border: true, network: { connect: [], resources: [] }, ...more,+});+const row = (app: AppPayload): Message => ({ id: "a1", role: "bot", kind: "app", at: 1, text: "[App] Pricing", app });+const store = { state: {}, dispatch: vi.fn(), flushBotPatches: vi.fn(async () => null) } as unknown as ReturnType<typeof useStore>;++let host: HTMLDivElement;+let pane: HTMLDivElement;+let root: Root;++const settle = () => act(async () => { for (let i = 0; i < 8; i += 1) await new Promise((resolve) => setTimeout(resolve, 0)); });+const render = async (message: Message) => {+ await act(async () => {+ root.render(createElement(BotEditorStore, { value: store, children:+ createElement(PagePaneContext, { value: pane },+ createElement(AppMessage, { botId: "pepper", botName: "Pepper", threadId: "t1", message, pool: createAppPool({ limit: 3 }) })) }));+ });+ await settle();+};+const card = () => host.querySelector<HTMLElement>("[data-app=a1]")!;+const opener = () => card().querySelector<HTMLButtonElement>("[data-page-open]")!;+const press = (element: HTMLElement) => act(async () => { element.click(); await new Promise((resolve) => setTimeout(resolve, 0)); });++beforeEach(() => {+ vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify(botHtmlDocument({ messageId: "a1", rev: 0, html: "<p>page</p>" })),+ { status: 200, headers: { "content-type": "application/json" } })));+ vi.stubGlobal("IntersectionObserver", undefined);+ forgetAppRows();+ host = document.createElement("div");+ pane = document.createElement("div");+ document.body.append(host, pane);+ root = createRoot(host);+});++afterEach(() => {+ act(() => root.unmount());+ host.remove();+ pane.remove();+ vi.unstubAllGlobals();+});++describe("a bot's page as a compact card", () => {+ it("shows only the preview and a footer with the title and what it is", async () => {+ await render(row(payload()));+ expect(card().querySelector<HTMLElement>("[data-app-box]")!.style.height).toBe(`${PAGE_PREVIEW_HEIGHT}px`);+ expect(card().querySelector("[data-page-footer]")!.textContent).toBe("PricingPage");+ expect(card().querySelector("[data-app-header]")).toBeNull();+ expect(card().querySelector("[data-page-tools]")).toBeNull();+ expect(Array.from(card().querySelectorAll("button"), (button) => button.getAttribute("aria-label"))).toEqual(["About this app", "Reload app", "Open page"]);+ });++ it("names the version once the page has been updated", async () => {+ await render(row(payload({ rev: 1, doc: "d1", history: ["d0"] })));+ expect(card().querySelector("[data-page-footer]")!.textContent).toBe("PricingPage · v2");+ });++ it("opens the page pane with its tools from the card, and closes it again", async () => {+ await render(row(payload()));+ expect(opener().getAttribute("aria-pressed")).toBe("false");+ await press(opener());+ const opened = pane.querySelector<HTMLElement>("[data-page-pane=a1]")!;+ expect(opened.querySelector("[data-page-tools]")).not.toBeNull();+ expect(opener().getAttribute("aria-pressed")).toBe("true");+ expect(opener().getAttribute("aria-label")).toBe("Close page");+ await press(opener());+ expect(pane.querySelector("[data-page-pane]")).toBeNull();+ });++ it("keeps the card's own tools working without opening the page", async () => {+ await render(row(payload()));+ await press(card().querySelector<HTMLButtonElement>("[aria-label='About this app']")!);+ expect(card().querySelector("[data-app-info]")).not.toBeNull();+ expect(pane.querySelector("[data-page-pane]")).toBeNull();+ });+});diff --git a/src/components/app/AppMessage.tsx b/src/components/app/AppMessage.tsxindex 270b4a32a5..b0c0f6c35b 100644--- a/src/components/app/AppMessage.tsx+++ b/src/components/app/AppMessage.tsx@@ -7,7 +7,7 @@ import { appBlockedLine } from "./app-blocked";import { APP_FRAME_LIMITS } from "../../../shared/app-frame/bridge";import { AppDocumentError, fetchAppDocument, type AppFetchFailure } from "./app-api";import { APP_POOL_LIMITS, useAppPoolSlot, type AppPool } from "./app-pool";-import { AppDone, AppHeader, AppInfo, AppOpenButton, AppPlaceholder, AppStrip, APP_SURFACE } from "./AppChrome";+import { AppDone, AppHeader, AppInfo, AppOpenButton, AppPlaceholder, AppStrip, APP_SURFACE, PageCardFooter } from "./AppChrome";import { AppFrame, type AppFrameHandle, type AppFrameProblem } from "./AppFrame";import type { AppSelection } from "./app-frame-host";import { PageCitationButton } from "../CitationUI";@@ -225,13 +225,15 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {// The compact card clips the page at its natural size, so it reads as a preview of the real thing.const boxHeight = pane ? Math.min(height, PAGE_PREVIEW_HEIGHT) : height;const body = collapsed ? null : fullScreen && current ? (- <div data-app-box className={cn("relative w-full", bordered && "border-t border-ink/[0.08]")} style={{ height: boxHeight }}>- <AppPlaceholder height={boxHeight} open={() => setFullScreen(false)} label={t(pane ? "chat.preview.closePage" : "chat.preview.exitFullScreen")} />+ <div data-app-box className={cn("relative w-full", bordered && !pane && "border-t border-ink/[0.08]")} style={{ height: boxHeight }}>+ {pane+ ? <AppPlaceholder height={boxHeight}><span className="ui-pill">{t("chat.preview.closePage")}</span></AppPlaceholder>+ : <AppPlaceholder height={boxHeight} open={() => setFullScreen(false)} label={t("chat.preview.exitFullScreen")} />}</div>) : noPage ? (- message.text ? <div dir="auto" className={cn("line-clamp-6 whitespace-pre-wrap text-[13px] leading-[18px] text-ink-secondary", bordered ? "px-4 pb-3" : "pb-2")}>{message.text}</div> : null+ message.text ? <div dir="auto" className={cn("line-clamp-6 whitespace-pre-wrap text-[13px] leading-[18px] text-ink-secondary", pane ? "px-4 pt-3" : bordered ? "px-4 pb-3" : "pb-2")}>{message.text}</div> : null) : (- <div data-app-box className={cn("relative w-full", pane && "overflow-hidden", bordered && "border-t border-ink/[0.08]", point.pointing && "ring-2 ring-inset ring-accent/60")}+ <div data-app-box className={cn("relative w-full", pane && "overflow-hidden", bordered && !pane && "border-t border-ink/[0.08]", point.pointing && "ring-2 ring-inset ring-accent/60")}style={{ height: boxHeight }}>{current ? (<AppFrame@@ -251,14 +253,15 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {) : (<AppPlaceholder height={boxHeight} loading={slot.live} open={slot.live ? undefined : reloadNow} />)}- {pane && current && !point.pointing && (- <button type="button" data-page-open onClick={() => setFullScreen(true)} aria-label={t("chat.preview.openPage")}- className="absolute inset-0 flex items-end justify-center bg-gradient-to-t from-panel via-transparent to-transparent pb-3">- <span className="ui-pill">{t("chat.preview.openPage")}</span>- </button>- )}</div>);+ const info = infoOpen && <AppInfo id={infoId} network={payload.network ?? { connect: [], resources: [] }} bytes={payload.bytes ?? 0} bordered={bordered || Boolean(pane)} />;+ const reopen = collapsed && (+ <div className={cn("flex", bordered || pane ? "px-4 pb-3.5" : "pb-2", pane && "pt-3.5")}>+ <AppOpenButton label={t("app.openAgain")} onClick={reloadNow} />+ </div>+ );+ const kind = t("chat.preview.kind");return (<div@@ -272,29 +275,52 @@ function AppRow({ botId, botName, threadId, message, payload, pool, room }: {{...(room ? { "data-app-room": "" } : {})}className={cn("w-full text-start", WIDTH)}>- <div className={cn(bordered ? APP_SURFACE : "w-full")}>- <AppHeader- title={payload.title}- origin={origin}- infoId={infoId}- infoOpen={infoOpen}- onInfo={() => setInfoOpen((open) => !open)}- onReload={docKey ? reloadNow : undefined}- reloading={fetching && Boolean(current)}- bordered={bordered}- />- {page && <PageToolbar {...tools} className={bordered ? "px-4 pb-3" : "pb-2"} />}- {page && <PageNotes version={version} point={point} share={share} save={save} room={room} className={bordered ? "px-4 pb-3" : "pb-2"} />}- {infoOpen && <AppInfo id={infoId} network={payload.network ?? { connect: [], resources: [] }} bytes={payload.bytes ?? 0} bordered={bordered} />}- {collapsed && (- <div className={cn("flex", bordered ? "px-4 pb-3.5" : "pb-2")}>- <AppOpenButton label={t("app.openAgain")} onClick={reloadNow} />+ {pane ? (+ <div className={APP_SURFACE}>+ <div className="group/page relative">+ {reopen}+ {body}+ <PageCardFooter+ title={payload.title}+ detail={version.last > 0 ? `${kind} · ${t("chat.preview.version", { n: version.rev + 1 })}` : kind}+ updated={version.flash}+ infoId={infoId}+ infoOpen={infoOpen}+ onInfo={() => setInfoOpen((open) => !open)}+ onReload={docKey ? reloadNow : undefined}+ reloading={fetching && Boolean(current)}+ />+ {current && !point.pointing && (+ <button type="button" data-page-open onClick={tools.fullScreen.toggle} aria-pressed={paneOpen}+ aria-label={t(paneOpen ? "chat.preview.closePage" : "chat.preview.openPage")}+ className="absolute inset-0 cursor-pointer rounded-2xl transition-colors duration-[120ms] hover:bg-ink/[0.04] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-focus motion-reduce:transition-none" />+ )}</div>- )}- {body}- {payload.done && <AppDone done={payload.done} bordered={bordered} />}- {strip && <AppStrip text={strip.text} action={strip.action} bordered={bordered} />}- </div>+ {info}+ {payload.done && <AppDone done={payload.done} bordered />}+ {strip && <AppStrip text={strip.text} action={strip.action} bordered />}+ </div>+ ) : (+ <div className={cn(bordered ? APP_SURFACE : "w-full")}>+ <AppHeader+ title={payload.title}+ origin={origin}+ infoId={infoId}+ infoOpen={infoOpen}+ onInfo={() => setInfoOpen((open) => !open)}+ onReload={docKey ? reloadNow : undefined}+ reloading={fetching && Boolean(current)}+ bordered={bordered}+ />+ {page && <PageToolbar {...tools} className={bordered ? "px-4 pb-3" : "pb-2"} />}+ {page && <PageNotes version={version} point={point} share={share} save={save} room={room} className={bordered ? "px-4 pb-3" : "pb-2"} />}+ {info}+ {reopen}+ {body}+ {payload.done && <AppDone done={payload.done} bordered={bordered} />}+ {strip && <AppStrip text={strip.text} action={strip.action} bordered={bordered} />}+ </div>+ )}{!room && canWrite && (<PageCitationButtonselection={selection && { text: selection.text, point: { left: selection.left, top: selection.bottom + 8 } }}diff --git a/src/locales/en.json b/src/locales/en.jsonindex acee464452..df8f2852c2 100644--- a/src/locales/en.json+++ b/src/locales/en.json@@ -7923,6 +7923,7 @@"chat.preview.exitFullScreen": "Close full screen","chat.preview.openPage": "Open page","chat.preview.closePage": "Close page",+ "chat.preview.kind": "Page","chat.preview.pointedPart": "a part of the page","chat.preview.removePoint": "Remove {label}","chat.preview.notePoint": "Write a note on {label}",