Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
perf: waitingOn skips the target's threads that have no open question
mattmillartech0fa84e88d6cf
diff --git a/server/host/lending-handoff-answers.test.ts b/server/host/lending-handoff-answers.test.tsindex 6f1583097c..3e87d677a6 100644--- a/server/host/lending-handoff-answers.test.ts+++ b/server/host/lending-handoff-answers.test.ts@@ -37,7 +37,7 @@ function world({ parked = true, opener = "a", card = true } = {}) {});const routing = createQuestionRouting({store: store as never, active: () => [requester, node], node: (id: string) => [requester, node].find((one) => one.id === id), parkedFor: (botId: string) => parked && botId === "b" ? [node] : [],- answerNode, live: (_threadId: string, requestId: string) => requestId === "live-1", unattended: () => false, answerLive, settleSignIns: () => {}, keptPrivate: () => false, steer: async () => true, mayReach: () => true,+ answerNode, live: (_threadId: string, requestId: string) => requestId === "live-1", hasLive: () => true, unattended: () => false, answerLive, settleSignIns: () => {}, keptPrivate: () => false, steer: async () => true, mayReach: () => true,handedInUnproven: gate.handedInUnproven,} as never);const allowed = () => gate.cloudLendingRefusal({ botId: "b", threadId: "t-b", generation: "g1" });diff --git a/server/host/question-routing.test.ts b/server/host/question-routing.test.tsindex 27485a4119..975e4d929b 100644--- a/server/host/question-routing.test.ts+++ b/server/host/question-routing.test.ts@@ -1,4 +1,4 @@-import { describe, expect, it } from "vitest";+import { describe, expect, it, vi } from "vitest";import type { HandoffView } from "../coordinator.ts";import type { Answerer } from "../runtime/types.ts";@@ -61,6 +61,7 @@ function world(options: { live?: string[]; privateTo?: string; steers?: boolean;return true;},live: (_threadId, requestId) => (options.live ?? []).includes(requestId),+ hasLive: () => (options.live ?? []).length > 0,unattended: (_botId, threadId) => (options.unattended ?? []).includes(threadId),answerLive: async (threadId, requestId, text) => { if (options.takesAnswers === false) return false; answeredLive.push({ threadId, requestId, text }); return true; },settleSignIns: (parked) => { settled.push(parked.id); },@@ -228,6 +229,21 @@ describe("a Chief's answer, for the person, to a question a teammate asks them",const answer = (w: ReturnType<typeof world>, threadId: string) =>w.routing.answerForPerson({ sender: FUCHS, senderThreadId: "inbox", target: DACHS, threadId, message: "Ja, behalten" });+ it("reads no thread of the target's for a question no turn is waiting on, so a send does not parse every conversation it has", async () => {+ const idle = world();+ asks(idle, "work");+ asks(idle, "other-work");+ const read = vi.spyOn(idle.store, "messagesFor");+ await idle.routing.relay({ sender: FUCHS, senderThreadId: "inbox", target: DACHS, message: "Ja" });+ expect(read).not.toHaveBeenCalled();++ const waiting = world({ live: ["req-p"] });+ asks(waiting, "work");+ const readWaiting = vi.spyOn(waiting.store, "messagesFor");+ await waiting.routing.relay({ sender: FUCHS, senderThreadId: "inbox", target: DACHS, message: "Ja" });+ expect(readWaiting).toHaveBeenCalledWith("work");+ });+it("answers the card in the person's own chat with the teammate, and the card and the Chief's chat say who answered", async () => {const w = world({ live: ["req-p"] });asks(w, "chat");diff --git a/server/host/question-routing.ts b/server/host/question-routing.tsindex ee92fc8e9f..fa1dafa79b 100644--- a/server/host/question-routing.ts+++ b/server/host/question-routing.ts@@ -25,6 +25,8 @@ export interface QuestionRoutingDeps {answerNode(id: string, text: string, by: Answerer): boolean;/** The bot's turn in `threadId` waits on this card's live request. */live(threadId: string, requestId: string): boolean;+ /** Some question in `threadId` is live: `live` is false for every request of a thread that has none. */+ hasLive(threadId: string): boolean;/** The bot's work in `threadId` was started by an outside event or runs with no person behind it. */unattended(botId: string, threadId: string): boolean;/** Answers the live question request a turn waits on; true when the turn took the answer. */@@ -75,6 +77,7 @@ export function createQuestionRouting(deps: QuestionRoutingDeps) {const parked = deps.parkedFor(target.id).filter((node) => node.parentId && !node.groupId && (!node.cards?.length || signInsOnly(node))).map((node) => ({ threadId: node.threadId, question: withDefault(node.question ?? "").question, node }));const live = store.tasks(target.id).flatMap((task) => {+ if (!deps.hasLive(task.threadId)) return [];const card = store.messagesFor(task.threadId).findLast((row) => !row.inbox && answerableInWords(row.card) && deps.live(task.threadId, row.card!.requestId!));if (!card) return [];const node = deps.active().find((n) => n.threadId === task.threadId && n.botId === target.id && n.status === "running");diff --git a/server/index.ts b/server/index.tsindex 804da32de5..68a254f46d 100644--- a/server/index.ts+++ b/server/index.ts@@ -3893,7 +3893,7 @@ const followupEndedRoots = new Map<string, string | undefined>();const askCards = createAskCards({ store, node: (id) => coordinator.node(id), 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)) });-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}`),+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; },unattended: (botId, threadId) => teammatePrompts.unattendedWork(botId, threadId),answerLive: async (threadId, requestId, text) => await answerRequest(threadId, botForThread(store.botByThread(threadId)?.id ?? "", threadId)?.modelSelection.instanceId ?? "", requestId, "answer", text) === "answered", settleSignIns: (node) => signInSettle.answeredElsewhere(node),keptPrivate: (node, requester) => keptPrivate({ child: teamHost.privateTo(node.threadId), parent: teamHost.privateTo(requester.threadId) }),