Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(chat): keep reading until every shown exchange ends, and reopen a job by its own id
luishron123629e9a1a957c
diff --git a/src/components/BotExchangeLine.dom.test.ts b/src/components/BotExchangeLine.dom.test.tsindex b23d39e0bd..7b673f004d 100644--- a/src/components/BotExchangeLine.dom.test.ts+++ b/src/components/BotExchangeLine.dom.test.ts@@ -152,6 +152,19 @@ describe("the bot exchange line", () => {expect(host.querySelector("[data-bot-exchange-line]")).toBeNull();});+ it("keeps reading while a hand-off that waits for no answer runs after its asker went idle, and drops it when it ends", async () => {+ current = job([{ ...root(), status: "completed" }, handoff({ reply: "none" })]);+ let answer: (value: unknown) => void = () => {};+ fixture.api.mockImplementation(async (path: string) => path.includes("wait=") ? new Promise((resolve) => { answer = resolve; }) : current);+ render({ ...pepper, busy: false });+ await settle();+ expect(line()!.getAttribute("data-state")).toBe("working");+ expect(reads()[1]![0]).toContain("wait=25000");+ answer(job([{ ...root(), status: "completed" }, handoff({ reply: "none", status: "completed", finishedAt: 40_000 })], "v2"));+ await settle();+ expect(host.querySelector("[data-bot-exchange-line]")).toBeNull();+ });+it("shows nothing when no bot handed anything on", async () => {current = job([root()]);render();diff --git a/src/components/HandoffExchange.dom.test.ts b/src/components/HandoffExchange.dom.test.tsindex 864709a7d9..33146ce4d0 100644--- a/src/components/HandoffExchange.dom.test.ts+++ b/src/components/HandoffExchange.dom.test.ts@@ -190,6 +190,32 @@ describe("a teammate's hand-off line", () => {}});+ it("reopens each of two jobs one helper did in the same thread with only that job's lines", async () => {+ const request = (id: string, text: string, at: number, parentId: string | null, assignment: string): Message =>+ ({ id, role: "bot", kind: "text", text, at, parentId, from: { botId: "maestro", name: "Maestro", color: "blue" }, roomRequest: { id: assignment, phase: "request" } } as Message);+ const first = request("q1", "Please check the build", 1_000, null, "h1");+ const firstReply: Message = { id: "r1", role: "bot", kind: "text", text: "Build is green", at: 2_000, parentId: "q1" };+ const second = request("q2", "Now run the tests", 3_000, "r1", "h2");+ const secondReply: Message = { id: "r2", role: "bot", kind: "text", text: "Tests pass", at: 4_000, parentId: "q2" };+ fixture.load.mockResolvedValue({ messages: [first, firstReply, second, secondReply], activeLeafId: "r2" });+ const done = (id: string) => sent({ id: `sent-${id}`, handoff: { id, state: "done", at: 5_000 } });+ flushSync(() => root.render(createElement(HandoffExchangeHost, {+ bot: maestro, children: createElement("main", { "data-chat": "" },+ createElement(ThreadChip, { message: done("h1"), threadId: "maestro-main" }),+ createElement(ThreadChip, { message: done("h2"), threadId: "maestro-main" })),+ })));+ const reopen = async (index: number) => {+ flushSync(() => host.querySelectorAll<HTMLButtonElement>("[data-exchange-chip]")[index]!.click());+ await settle();+ flushSync(() => {});+ };+ await reopen(0);+ expect(rows()).toEqual(["asker: MaestroPlease check the build", "helper: FerramenteiroBuild is green"]);+ flushSync(() => host.querySelector<HTMLButtonElement>("[data-exchange-back]")!.click());+ await reopen(1);+ expect(rows()).toEqual(["asker: MaestroNow run the tests", "helper: FerramenteiroTests pass"]);+ });+it("opens the thread as before where no chat column hosts the exchange", () => {flushSync(() => root.render(createElement(ThreadChip, { message: sent(), threadId: "maestro-main" })));expect(chip().hasAttribute("data-exchange-chip")).toBe(false);diff --git a/src/components/ThreadChip.tsx b/src/components/ThreadChip.tsxindex 0c524caac3..cae3815449 100644--- a/src/components/ThreadChip.tsx+++ b/src/components/ThreadChip.tsx@@ -26,7 +26,8 @@ export function ThreadChip({ message, threadId }: { message: Message; threadId?:// The helper's main chat is its talk with the person, not with the asker: that still opens there.const workThread = bot && ref.threadId !== bot.threadId && (bot.tasks ?? []).some((task) => task.threadId === ref.threadId);const exchange = openExchange && !direct && asker && bot && asker.botId !== bot.id && workThread- ? { asker, helper: { botId: bot.id, name: bot.name, color: bot.color }, threadId: ref.threadId } : null;+ ? { asker, helper: { botId: bot.id, name: bot.name, color: bot.color }, threadId: ref.threadId, ...(message.handoff ? { id: message.handoff.id } : {}) }+ : null;const exchangeLabel = bot ? t("chat.handoffExchange.open", { name: bot.name }) : "";return (<HandoffReply message={message} threadId={threadId}>diff --git a/src/lib/bot-exchanges.test.ts b/src/lib/bot-exchanges.test.tsindex a187369b1a..67fb17c13b 100644--- a/src/lib/bot-exchanges.test.ts+++ b/src/lib/bot-exchanges.test.ts@@ -33,9 +33,9 @@ describe("the exchanges in a job", () => {const deeper = node("b", { parentId: "a", botId: "research", botName: "Research", threadId: "work-2", createdAt: 1_200 });expect(exchangesOf(job([root, done, deeper], ["b"]), "A teammate")).toEqual([{ id: "b", asker: { botId: "pessoal", name: "Pessoal" }, helper: { botId: "research", name: "Research" }, threadId: "work-2",- state: "needs-you", live: true, createdAt: 1_200 },+ state: "needs-you", createdAt: 1_200 },{ id: "a", asker: { botId: "maestro", name: "Maestro" }, helper: { botId: "pessoal", name: "Pessoal" }, threadId: "work",- state: "answered", live: false, createdAt: 1_100, finishedAt: 1_900 },+ state: "answered", createdAt: 1_100, finishedAt: 1_900 },]);});@@ -50,10 +50,6 @@ describe("the exchanges in a job", () => {expect(entry!.asker).toEqual({ name: "A teammate" });});- it("do not keep the line alive for a notice that waits for no answer", () => {- const [entry] = exchangesOf(job([root, node("n", { parentId: "root", botId: "pessoal", reply: "none" })]), "A teammate");- expect(entry).toMatchObject({ state: "working", live: false });- });});describe("an exchange's lines in the helper's thread", () => {diff --git a/src/lib/bot-exchanges.ts b/src/lib/bot-exchanges.tsindex 85607615ab..43659c6c44 100644--- a/src/lib/bot-exchanges.ts+++ b/src/lib/bot-exchanges.ts@@ -33,8 +33,6 @@ export interface BotExchangeEntry {helper: { botId: string; name: string };threadId: string;state: ExchangeState;- /** Still working on an answer someone waits for. */- live: boolean;createdAt: number;finishedAt?: number;}@@ -71,12 +69,12 @@ export function exchangesOf(job: JobRead, teammate: string): BotExchangeEntry[]helper: { botId: node.botId, name: node.botName },threadId: node.threadId,state,- live: !exchangeEnded(state) && node.reply !== "none",createdAt: node.createdAt,...(node.finishedAt !== undefined ? { finishedAt: node.finishedAt } : {}),});}- return entries.sort((a, b) => Number(b.live) - Number(a.live) || a.createdAt - b.createdAt);+ const open = (entry: BotExchangeEntry) => Number(!exchangeEnded(entry.state));+ return entries.sort((a, b) => open(b) - open(a) || a.createdAt - b.createdAt);}/** A helper-thread line, as far as tying it to one hand-off goes. */diff --git a/src/lib/use-bot-exchanges.ts b/src/lib/use-bot-exchanges.tsindex d714055ed9..70628b9cb9 100644--- a/src/lib/use-bot-exchanges.ts+++ b/src/lib/use-bot-exchanges.ts@@ -3,7 +3,7 @@ import { createContext, useCallback, useEffect, useMemo, useRef, useState } fromimport { personWrote } from "../../shared/person-wait";import { api } from "@/state/api";import type { Message } from "@/state/store";-import { exchangesOf, type BotExchangeEntry, type JobRead } from "./bot-exchanges";+import { exchangeEnded, exchangesOf, type BotExchangeEntry, type JobRead } from "./bot-exchanges";import { t } from "./i18n";/** The server holds a read at most this long (`JOB_WAIT_MAX_MS`). */@@ -47,7 +47,7 @@ export function useBotExchanges(threadId: string | undefined, since: number | unlet failures = 0;for (let first = true; !abort.signal.aborted; first = false) {const known = latest.current?.key === key ? latest.current.job : null;- const open = Boolean(known && exchangesOf(known, "").some((entry) => entry.live));+ const open = Boolean(known && exchangesOf(known, "").some((entry) => !exchangeEnded(entry.state)));const hold = !first && known !== null && (working || open);if (!first && !hold && failures === 0) return;const path = hold ? `${base}${join}wait=${HOLD_MS}&version=${encodeURIComponent(known!.version)}` : base;