Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Keep the pictures-held Steer notice accurate
koeria9b318b21db12
diff --git a/server/index.ts b/server/index.tsindex 7769ffc0af..d3ab8ac412 100644--- a/server/index.ts+++ b/server/index.ts@@ -7212,7 +7212,7 @@ async function acceptDirectSend(if (queued.text !== text || queued.replyToId !== replyTo?.id) {throw Object.assign(new Error("sendId already belongs to another message"), { status: 409 });}- return { ok: true as const, queued: true as const, queueId: queued.id, threadId, reason: queued.reason, why: waitWords.queued(threadId, queued.reason) };+ return { ok: true as const, queued: true as const, queueId: queued.id, threadId, reason: queued.reason, why: waitWords.queued(threadId, queued.reason), ...(queued.blockedBy === "images" ? { blockedBy: "images" as const } : {}) };}}@@ -7296,7 +7296,7 @@ async function acceptDirectSend(}const queued = queueSteeredMessage(current.id, threadId, text, {replyToId: replyTo?.id,- sendId,+ sendId, ...(busyAdmission.action === "queue" && busyAdmission.blockedBy === "images" ? { blockedBy: "images" as const } : {}),dataContext, fromApp: app?.fromApp, mentions,prompt: queuedPrompt,sender,@@ -7307,7 +7307,7 @@ async function acceptDirectSend(});if (personsOwn && !chose) personFirst.wait(threadId, {});if (chose === "restart") await restartForPerson(botId, threadId, trigger);- return { ok: true as const, queued: true as const, queueId: queued.id, threadId, ...(busyAdmission.action === "queue" && busyAdmission.blockedBy ? { blockedBy: busyAdmission.blockedBy } : {}) };+ return { ok: true as const, queued: true as const, queueId: queued.id, threadId, ...(busyAdmission.action === "queue" && busyAdmission.blockedBy === "images" ? { blockedBy: "images" as const } : {}) };}return startOrQueueDirectMessage(botId, threadId, text, replyTo, sendId, sender, trigger, via, callId, dataContext, app, mentions);},diff --git a/server/steer-e2e.test.ts b/server/steer-e2e.test.tsindex b200d3345c..9b24bc859d 100644--- a/server/steer-e2e.test.ts+++ b/server/steer-e2e.test.ts@@ -1049,10 +1049,15 @@ posixOnly("mid-turn steering e2e", () => {const imagePath = join(attachments, "123e4567-e89b-42d3-a456-426614174008.png");writeFileSync(imagePath, "codex png");const imageText = `see this\n\n<attached-image path="${imagePath}" name="codex.png" />`;- const queued = await api("POST", `/api/bots/${created.id}/messages`, { text: imageText, followUp: "steer" });+ const sendId = "codex_image_steer_retry_123456";+ const queued = await api("POST", `/api/bots/${created.id}/messages`, { text: imageText, followUp: "steer", sendId });expect(queued.status).toBe(202);expect(queued.body).toMatchObject({ ok: true, queued: true, blockedBy: "images" });expect(queued.body.steered).toBeUndefined();+ const retried = await api("POST", `/api/bots/${created.id}/messages`, { text: imageText, followUp: "steer", sendId });+ expect(retried.status).toBe(202);+ expect(retried.body).toMatchObject({ ok: true, queued: true, queueId: queued.body.queueId, blockedBy: "images" });+ expect(retried.body.steered).toBeUndefined();const still = await api("POST", `/api/bots/${created.id}/queue/${queued.body.queueId}/steer`, {threadId: created.threadId,diff --git a/server/steer-queue.test.ts b/server/steer-queue.test.tsindex c97f9acb62..b17433db08 100644--- a/server/steer-queue.test.ts+++ b/server/steer-queue.test.ts@@ -545,6 +545,19 @@ describe("steer-queue module", () => {expect(queuedSteeredMessage(bot.id, bot.threadId, "send_1234567890123456")).toBeNull();});+ it("keeps a pictures hold on the queued receipt so a retried send can say why", () => {+ const bot = fakeBot("bot-images-receipt", "thread-images-receipt", true);+ const queued = queueSteeredMessage(bot.id, bot.threadId, "see this", {+ sendId: "send_images_held_123456",+ blockedBy: "images",+ });+ expect(queuedSteeredMessage(bot.id, bot.threadId, "send_images_held_123456")).toEqual({+ id: queued.id,+ text: "see this",+ blockedBy: "images",+ });+ });+it("holds the queue while the bot is busy and drains it once when idle", () => {const bot = fakeBot("bot-b", "thread-b", true);const store = fakeStore([bot]);diff --git a/server/steer-queue.ts b/server/steer-queue.tsindex c4f22b2dd6..eb5a1aa35b 100644--- a/server/steer-queue.ts+++ b/server/steer-queue.ts@@ -27,6 +27,7 @@ interface QueueEntry {sendId?: string;dataContext?: Message["dataContext"];reason?: SteerQueueReason;+ blockedBy?: "images";/** Queued by a bot running unattended: the drained turn inherits it (a queue is a delay, not a person at the keyboard). */unattended?: boolean;peerAsk?: Message["peerAsk"];@@ -109,7 +110,7 @@ export function queueSteeredMessage(botId: string,threadId: string,text: string,- options: { prompt?: string; replyToId?: string; sendId?: string; dataContext?: Message["dataContext"]; reason?: SteerQueueReason; unattended?: boolean; peerAsk?: Message["peerAsk"]; origin?: WorkOrigin; sender?: ResolvedSender; trigger?: UsageTrigger; callId?: string; via?: "call"; reaction?: string; fromApp?: Message["fromApp"]; relay?: Message["relay"]; rowId?: string; first?: boolean; queuedAt?: number; mentions?: string[]; aheadOfOthers?: string } = {},+ options: { prompt?: string; replyToId?: string; sendId?: string; blockedBy?: "images"; dataContext?: Message["dataContext"]; reason?: SteerQueueReason; unattended?: boolean; peerAsk?: Message["peerAsk"]; origin?: WorkOrigin; sender?: ResolvedSender; trigger?: UsageTrigger; callId?: string; via?: "call"; reaction?: string; fromApp?: Message["fromApp"]; relay?: Message["relay"]; rowId?: string; first?: boolean; queuedAt?: number; mentions?: string[]; aheadOfOthers?: string } = {},): QueuedSteer {const id = newId();const entry = queues.get(threadId) ?? { botId, items: [] };@@ -122,6 +123,7 @@ export function queueSteeredMessage(sendId: options.sendId,dataContext: options.dataContext,reason: options.reason,+ ...(options.blockedBy ? { blockedBy: options.blockedBy } : {}),unattended: options.unattended,peerAsk: options.peerAsk,...(options.origin ? { origin: options.origin } : {}),@@ -267,11 +269,11 @@ export function queuedSteeredMessage(botId: string,threadId: string,sendId: string,-): { id: string; text: string; replyToId?: string; reason?: SteerQueueReason } | null {+): { id: string; text: string; replyToId?: string; reason?: SteerQueueReason; blockedBy?: "images" } | null {const entry = queues.get(threadId);if (!entry || entry.botId !== botId) return null;const item = entry.items.find((candidate) => candidate.sendId === sendId);- return item ? { id: item.messageId, text: item.text, replyToId: item.replyToId, ...(item.reason ? { reason: item.reason } : {}) } : null;+ return item ? { id: item.messageId, text: item.text, replyToId: item.replyToId, ...(item.reason ? { reason: item.reason } : {}), ...(item.blockedBy ? { blockedBy: item.blockedBy } : {}) } : null;}/** Whose waiting line this is, or null when nothing waits under that id. */diff --git a/src/state/store.test.ts b/src/state/store.test.tsindex b6bab26898..ab45ffbcd7 100644--- a/src/state/store.test.ts+++ b/src/state/store.test.ts@@ -3014,19 +3014,19 @@ describe("restoring the open conversation", () => {flushSync(() => dispatch({ type: "steerQueued", botId: "bot-1", queueId: "q-image", onSettled: settled }));await settle();expect(settled).toHaveBeenCalledTimes(2);- expect(seen.notice).toEqual({ kind: "steer-images", botName: null });+ expect(seen.notice).toEqual({ kind: "steer-images", botName: null, threadId: "bot-1-thread" });flushSync(() => dispatch({ type: "notice", notice: null }));flushSync(() => dispatch({ type: "steerGroupQueued", groupId: "room-1", queueId: "q-room", onSettled: settled }));await settle();expect(settled).toHaveBeenCalledTimes(3);- expect(seen.notice).toEqual({ kind: "steer-images", botName: null });+ expect(seen.notice).toEqual({ kind: "steer-images", botName: null, threadId: "room-thread" });flushSync(() => dispatch({ type: "notice", notice: null }));flushSync(() => dispatch({ type: "steerGroupQueued", groupId: "room-1", queueId: "q-room-plain", onSettled: settled }));await settle();expect(settled).toHaveBeenCalledTimes(4);- expect(seen.notice).toMatchObject({ kind: "note", text: expect.stringContaining("Nothing was stopped") });+ expect(seen.notice).toMatchObject({ kind: "note", text: expect.stringContaining("Nothing was stopped"), threadId: "room-thread" });});it("a Steer send that stays queued because of pictures says why", async () => {@@ -3047,7 +3047,39 @@ describe("restoring the open conversation", () => {sendId: "send-image",}));await settle();- expect(seen.notice).toEqual({ kind: "steer-images", botName: null });+ expect(seen.notice).toEqual({ kind: "steer-images", botName: null, threadId: "bot-1-thread" });+ });++ it("an earlier notice timer does not clear a newer pictures notice", async () => {+ const { stream } = await boot();+ stream.send(hello());+ await settle();+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });+ try {+ flushSync(() => dispatch({ type: "notice", notice: { kind: "note", botName: null, text: "held" } }));+ flushSync(() => vi.advanceTimersByTime(19_000));+ flushSync(() => dispatch({ type: "notice", notice: { kind: "steer-images", botName: null, threadId: "bot-1-thread" } }));+ flushSync(() => vi.advanceTimersByTime(2_000));+ expect(seen.notice).toEqual({ kind: "steer-images", botName: null, threadId: "bot-1-thread" });+ flushSync(() => vi.advanceTimersByTime(6_000));+ expect(seen.notice).toBeNull();+ } finally {+ vi.useRealTimers();+ }+ });++ it("a pictures notice leaves when the person opens another conversation", async () => {+ const { stream } = await boot();+ stream.send(hello());+ await settle();+ flushSync(() => dispatch({ type: "notice", notice: { kind: "steer-images", botName: null, threadId: "bot-1-thread" } }));+ flushSync(() => dispatch({ type: "select", id: "bot-1" }));+ expect(seen.notice).toEqual({ kind: "steer-images", botName: null, threadId: "bot-1-thread" });+ flushSync(() => dispatch({ type: "select", id: "bot-2" }));+ expect(seen.notice).toBeNull();+ flushSync(() => dispatch({ type: "notice", notice: { kind: "thread-gone", botName: "bot-1" } }));+ flushSync(() => dispatch({ type: "select", id: "bot-1" }));+ expect(seen.notice).toEqual({ kind: "thread-gone", botName: "bot-1" });});it("the provider restores the conversation remembered on this device", async () => {diff --git a/src/state/store.tsx b/src/state/store.tsxindex 61a12ba552..16cdcc6c65 100644--- a/src/state/store.tsx+++ b/src/state/store.tsx@@ -555,7 +555,7 @@ export interface AppState {connected: boolean;hydrated?: true;error: string | null;- notice: { kind: "thread-gone" | "bot-gone" | "steer-images" | "note"; botName: string | null; text?: string } | null;+ notice: { kind: "thread-gone" | "bot-gone" | "steer-images" | "note"; botName: string | null; text?: string; threadId?: string } | null;revealThread: { threadId: string; nonce: number } | null;mascotMotion: {botId: string;@@ -2095,6 +2095,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {const olderPagesInFlight = new Set<string>();const newTaskOnce = createNewTaskOnce();let creatingBot = false;+ let noticeTimer: ReturnType<typeof setTimeout> | undefined;const showError = (e: unknown) => { if (isBotLimitRefusal(e)) return; if (isNoAiRefusal(e)) wrapped({ type: "notice", notice: { kind: "note", botName: null, text: e.message } }); else rawDispatch({ type: "error", message: e instanceof Error ? e.message : String(e) }); };const showSavedState = (owner: SavedOwner) => (error: unknown) => {showError(error);@@ -2246,18 +2247,22 @@ export function StoreProvider({ children }: { children: ReactNode }) {if (action.type !== "send") return undefined;return botBeforeSend ? openOnboardingCard(botBeforeSend) : undefined;})();+ const threadOf = (s: AppState) => s.groups.find((g) => g.id === s.selectedId)?.threadId ?? s.bots.find((b) => b.id === s.selectedId)?.threadId, previousOpened = threadOf(store.getState());if (action.type !== "cancelQueued" &&action.type !== "cancelGroupQueued" &&action.type !== "deleteBot") rawDispatch(action);+ const cur = store.getState(), opened = threadOf(cur);+ if (previousOpened !== opened && cur.notice?.threadId && opened && cur.notice.threadId !== opened) rawDispatch({ type: "notice", notice: null });switch (action.type) {case "loadOlderMessages": {loadOlderPage(action, stateRef.current, olderPagesInFlight, rawDispatch, showError);break;}case "notice":- if (action.notice) setTimeout(() => rawDispatch({ type: "notice", notice: null }), action.notice.kind === "note" ? 20_000 : 6000);+ if (noticeTimer !== undefined) clearTimeout(noticeTimer);+ noticeTimer = action.notice ? setTimeout((shown) => { if (store.getState().notice === shown) rawDispatch({ type: "notice", notice: null }); }, action.notice.kind === "note" ? 20_000 : 6000, action.notice) : undefined;break;case "createRoutine":api("/api/routines", { method: "POST", body: JSON.stringify(action.input) }).catch(showError);@@ -2312,7 +2317,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {rawDispatch({ type: "consumePendingQueued", threadId: body.threadId, queueId });}} else if (body?.queued === true && body.blockedBy === "images") {- wrapped({ type: "notice", notice: { kind: "steer-images", botName: null } });+ wrapped({ type: "notice", notice: { kind: "steer-images", botName: null, threadId: body.threadId } });}action.onSettled?.();})@@ -2343,7 +2348,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {rawDispatch({ type: "consumePendingQueued", threadId: body.threadId, queueId });}} else if (body?.queued === true) {- wrapped({ type: "notice", notice: body.blockedBy === "images" ? { kind: "steer-images", botName: null } : { kind: "note", botName: null, text: t("composer.queued.roomSteerHeld") } });+ wrapped({ type: "notice", notice: body.blockedBy === "images" ? { kind: "steer-images", botName: null, threadId: body.threadId } : { kind: "note", botName: null, text: t("composer.queued.roomSteerHeld"), threadId: body.threadId } });}action.onSettled?.();})@@ -2383,7 +2388,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {text: action.text,reason: body.reason === "capacity" || body.reason === "group-turn" ? body.reason : undefined,});- if (body.blockedBy === "images") wrapped({ type: "notice", notice: { kind: "steer-images", botName: null } });+ if (body.blockedBy === "images") wrapped({ type: "notice", notice: { kind: "steer-images", botName: null, threadId: body.threadId } });}}).catch((error) => {