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 one question card for handed-on work
Afloat8e2e3223c4b8
diff --git a/server/host/ask-cards.test.ts b/server/host/ask-cards.test.tsindex 0a6fc559e0..644aa79e21 100644--- a/server/host/ask-cards.test.ts+++ b/server/host/ask-cards.test.ts@@ -1,6 +1,6 @@import { describe, expect, it, vi } from "vitest";-import type { HandoffView } from "../coordinator.ts";+import { createCoordinator, type HandoffView } from "../coordinator.ts";import type { Notification } from "../notify.ts";import type { RequestAuth } from "../request-auth.ts";import type { BotRecord, Message } from "../store.ts";@@ -50,6 +50,7 @@ function world(options: { inboxThread?: string; live?: string[]; privateThreads?const deps: AskCardsDeps = {store: store as unknown as AskCardsDeps["store"],node: (id) => nodes.get(id),+ runningIn: (threadId) => [...nodes.values()].filter((node) => node.threadId === threadId && node.status === "running"),delegatorOf: (threadId) => (threadId === "work" ? { threadId: "inbox", botId: "scout" } : undefined),notify: (notification) => { if (notification) notified.push(notification); },copiedInto: (inboxThreadId) => inboxThreadId === options.inboxThread,@@ -254,6 +255,80 @@ describe("nothing stale waits on the person", () => {});});+describe("a handed-on turn owns its question", () => {+ it.each(["", "The drafts are ready.\n"])("keeps one answerable card after a reply-none requester finishes, with prefix %j", async (prefix) => {+ const w = world();+ let markParked!: () => void;+ let markCompleted!: () => void;+ const parked = new Promise<void>((resolve) => { markParked = resolve; });+ const completed = new Promise<void>((resolve) => { markCompleted = resolve; });+ const parents: Array<HandoffView["status"] | undefined> = [];+ const assigners: Array<string | undefined> = [];+ const turns: string[] = [];+ const coordinator = createCoordinator({ dataDir: ":memory:", now: () => 1_000, host: {+ validate: () => undefined, busy: () => false, chiefOf: () => false,+ levelOf: () => ({ level: "ask" }), names: () => ({ assignee: "Ledger", requester: "Scout" }),+ changed: () => {},+ report: (node) => { if (node.parentId && node.status === "completed") markCompleted(); },+ park: (node, question) => { w.cards.park(node, question); markParked(); },+ run: async (node, _resumed, _signal, composed) => {+ turns.push(composed.turn);+ if (composed.turn === "answered") return { ok: true, text: "Used the chosen wording." };+ parents.push(node.parentId ? coordinator.node(node.parentId)?.status : undefined);+ assigners.push(coordinator.assignerOf(node.threadId)?.id);+ const text = `${prefix}NEEDS INPUT: ${QUESTION}`;+ w.store.appendMessage(node.threadId, { role: "bot", kind: "text", text, turnId: "handed-turn" });+ if (!coordinator.assignerOf(node.threadId)) w.cards.ownQuestion(node.botId, node.threadId, "handed-turn");+ return { ok: true, text };+ },+ } });+ w.deps.node = (id) => coordinator.node(id);+ w.deps.runningIn = (threadId) => coordinator.runningIn(threadId);+ w.deps.answerNode = (id, text) => coordinator.answerNode(id, text);+ try {+ const source = { botId: "scout", threadId: "inbox" };+ const root = coordinator.beginTurn({ id: "requester-turn", address: source, brief: "Prepare a footer", level: "ask" });+ const child = coordinator.enqueue(source, root.id, undefined, { botId: "ledger", threadId: "work" }, "Prepare a footer", { reply: "none" }).node;+ coordinator.endTurn(root.id, { ok: true, text: "The teammate has the work." });+ coordinator.tick();+ await parked;+ expect(parents).toEqual(["completed"]);+ expect(assigners).toEqual([undefined]);+ const requestId = `needs-input-${child.id}-1`;+ expect(w.rows("work").filter((row) => row.card).map((row) => row.card!.requestId)).toEqual([requestId]);+ expect(w.notified.map((notification) => notification.kind)).toEqual(["question"]);++ expect(coordinator.runningIn("work")).toEqual([]);+ w.cards.ownQuestion("ledger", "work", "handed-turn");+ expect(w.rows("work").filter((row) => row.card)).toHaveLength(1);+ expect(w.cards.answerParked("work", requestId, "Short and friendly")).toBe(true);+ coordinator.tick();+ await completed;+ expect(turns).toEqual(["first", "answered"]);+ expect(coordinator.node(child.id)?.status).toBe("completed");+ expect(w.rows("work").find((row) => row.card)?.card).toMatchObject({ answered: "answer", answeredText: "Short and friendly" });++ w.store.appendMessage("work", { role: "bot", kind: "text", text: `NEEDS INPUT: ${QUESTION}`, turnId: "own-turn" });+ w.cards.ownQuestion("ledger", "work", "own-turn");+ expect(w.rows("work").filter((row) => row.card && !row.card.answered).map((row) => row.card!.requestId))+ .toEqual(["needs-input-ledger-own-turn-1"]);+ } finally {+ coordinator.close();+ }+ });++ it.each([+ { status: "queued" }, { status: "waiting" }, { status: "completed" },+ { botId: "scout" }, { groupId: "room" }, { parentId: undefined }, { threadId: "elsewhere" },+ ] as Partial<HandoffView>[])("keeps an own-chat question beside unrelated work %j", (other) => {+ const w = world();+ w.node({ status: "running", ...other });+ w.store.appendMessage("work", { role: "bot", kind: "text", text: `NEEDS INPUT: ${QUESTION}`, turnId: "own-turn" });+ w.cards.ownQuestion("ledger", "work", "own-turn");+ expect(w.rows("work").filter((row) => row.card).map((row) => row.card!.requestId)).toEqual(["needs-input-ledger-own-turn-1"]);+ });+});+describe("how an open question card settles", () => {const parked = () => {const w = world();@@ -590,3 +665,4 @@ describe("askToolName", () => {expect(askToolName(null, claude)).toBeUndefined();});});+diff --git a/server/host/ask-cards.ts b/server/host/ask-cards.tsindex c13352e0ba..0ab66195ff 100644--- a/server/host/ask-cards.ts+++ b/server/host/ask-cards.ts@@ -35,6 +35,7 @@ export interface AskCardsDeps {store: Pick<Store, "bot" | "messagesFor" | "activePath" | "appendMessage" | "patchMessage" | "taskByThread" | "patchTask" | "group"| "groupTaskByThread" | "patchGroup" | "groupByThread">;node(id: string): HandoffView | undefined;+ runningIn(threadId: string): HandoffView[];/** The conversation that handed this one its work, while that work is live. */delegatorOf(threadId: string): { threadId: string; botId?: string } | undefined;notify(notification: Notification | null): void;@@ -381,6 +382,7 @@ export function createAskCards(deps: AskCardsDeps) {/** A bot's own reply in its own chat ended on a NEEDS INPUT line: the line becomes a question card the person taps or* answers in words, as their next message there. */function ownQuestion(botId: string, threadId: string, turnId: string): void {+ if (deps.runningIn(threadId).some((node) => node.parentId && !node.groupId && node.botId === botId)) return;const reply = store.activePath(threadId).findLast((message) => message.role === "bot" && message.kind === "text" && message.turnId === turnId);const question = reply ? needsInputQuestion(reply.text ?? "") : undefined;if (!reply || question === undefined) return;diff --git a/server/index.ts b/server/index.tsindex d68b86fc80..e47c5e1562 100644--- a/server/index.ts+++ b/server/index.ts@@ -3831,7 +3831,7 @@ function followupAtBoot(threadId: string): "dispatched" | "pending" | undefined}const followupEndedRoots = new Map<string, string | undefined>(); let pokeBoard = (): void => {}; // the board's dispatcher, once it is made below-const askCards = createAskCards({ store, node: (id) => coordinator.node(id), delegatorOf, notify, answerCard: respondToCard, answerNode: (id, text) => coordinator.answerNode(id, text),+const askCards = createAskCards({ store, node: (id) => coordinator.node(id), runningIn: (threadId) => coordinator.runningIn(threadId), delegatorOf, notify, answerCard: respondToCard, answerNode: (id, text) => coordinator.answerNode(id, text),answeredIn: (threadId, requestId) => automationReports.answeredIn(teamMessages.inbox.answeredIn(threadId, requestId), requestId), live: (threadId, requestId) => askMessageByRequest.has(`${threadId}:${requestId}`),copiedInto: (inboxThreadId, botId, threadId) => Boolean(CLOUD_TEAM) && teamMessages.inbox.isHomeOf(inboxThreadId, teamOwners.routing.askedPerson(botId, threadId)), keptPrivate: (threadId, parentThreadId) => keptPrivate({ child: teamHost.privateTo(threadId), parent: teamHost.privateTo(parentThreadId) }) });const questionRouting = createQuestionRouting({ store, handedInUnproven, active: () => coordinator.active(), node: (id) => coordinator.node(id), parkedFor: (botId) => coordinator.parkedFor(botId), answerNode: (id, text, by) => coordinator.answerNode(id, text, by), live: (threadId, requestId) => askMessageByRequest.has(`${threadId}:${requestId}`), hasLive: (threadId) => { for (const key of askMessageByRequest.keys()) if (key.startsWith(`${threadId}:`)) return true; return false; },