Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Inbox: finished hand-offs and the answers that sum them up get a line
Milind Soni1dd8bab71a2c
diff --git a/server/coordinator.ts b/server/coordinator.tsindex 9070b79f0f..dc112ab12f 100644--- a/server/coordinator.ts+++ b/server/coordinator.ts@@ -41,6 +41,8 @@ export interface HandoffView {text: string;createdAt: number;requestBatchKey?: string;+ /** Its reply only acknowledged (runtime/status-reply.ts). */+ statusOnly?: boolean;status: HandoffStatus;result: string;reported: boolean;diff --git a/server/host/place-inbox.test.ts b/server/host/place-inbox.test.tsindex 9738925672..247e27fc85 100644--- a/server/host/place-inbox.test.ts+++ b/server/host/place-inbox.test.ts@@ -64,6 +64,71 @@ function setup(over: Partial<PlaceInboxDeps> = {}, dataDir = mkdtempSync(join(tmreturn { inbox, store, said, cards, sent, handed, guests, bots, listeners, ask, dataDir, tick: (ms: number) => { clock += ms; } };}+describe("finished work reaches the inbox once", () => {+ const work = (threadId: string, text = "Pricing page updated.") => ({ botId: "mira", threadId, text });+ const asked = { botId: "eli", threadId: "eli-main" };+ const summary = { kind: "done", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Done", body: "All three pages are live." } as Notification;+ const infoRows = (f: ReturnType<typeof setup>) => f.inbox.view(OWNER).rows.filter((row) => row.tab === "info");++ it("files handed-off work as an unread line, opening its own thread, and sends nothing", () => {+ const f = setup();+ f.inbox.handedResult(work("mira-pricing"), asked);+ expect(infoRows(f)).toMatchObject([{ kind: "result", from: { name: "Mira", botId: "mira" }, workingFor: { name: "Eli" }, text: "Pricing page updated.", unread: true,+ origin: { botId: "mira", threadId: "mira-pricing" } }]);+ expect(f.sent).toEqual([]);+ expect(f.inbox.count(OWNER)).toBe(1);+ });++ it("keeps one row per job: the next teammate's result and then the summary replace it", () => {+ const f = setup();+ f.inbox.handedResult(work("mira-pricing"), asked);+ f.tick(1);+ f.inbox.handedResult(work("mira-launch", "Launch post drafted."), asked);+ expect(infoRows(f).map((row) => row.text)).toEqual(["Launch post drafted."]);+ f.tick(1);+ f.inbox.route(summary, { summary: true });+ expect(infoRows(f)).toMatchObject([{ kind: "result", from: { name: "Eli" }, text: "All three pages are live.", origin: { threadId: "eli-main" } }]);+ });++ it("leaves what the person already read, other jobs and routine reports alone", () => {+ const f = setup();+ f.inbox.handedResult(work("mira-pricing"), asked);+ f.inbox.markRead(OWNER, infoRows(f)[0]!.id);+ f.inbox.handedResult(work("mira-launch", "Second."), { botId: "eli", threadId: "eli-launch" });+ f.inbox.route({ kind: "done", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Done", body: "Weekly report." } as Notification, { routine: true });+ f.tick(1);+ f.inbox.route(summary, { summary: true });+ expect(infoRows(f).map((row) => row.text).sort()).toEqual(["All three pages are live.", "Pricing page updated.", "Second.", "Weekly report."]);+ });++ it("adds no line for a muted bot, an empty reply or a plain turn's done, and a restart keeps the row", () => {+ const f = setup();+ f.bots.mira!.notifications = false;+ f.inbox.handedResult(work("mira-pricing"), asked);+ f.bots.mira!.notifications = true;+ f.inbox.handedResult(work("mira-pricing", " "), asked);+ f.inbox.route({ kind: "done", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Done", body: "Plain." } as Notification);+ expect(infoRows(f)).toEqual([]);++ f.inbox.handedResult(work("mira-pricing"), asked);+ const reopened = setup({}, f.dataDir);+ expect(infoRows(reopened).map((row) => row.text)).toEqual(["Pricing page updated."]);+ reopened.inbox.route(summary, { summary: true });+ expect(infoRows(reopened).map((row) => row.text)).toEqual(["All three pages are live."]);+ });++ it("files a resumed answer's results notice under the same job, and keeps a guest's work theirs", () => {+ const f = setup();+ f.inbox.route({ kind: "delegation-settled", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Results", body: "Results from Mira" } as Notification, { summary: true });+ f.inbox.route({ kind: "delegation-settled", botId: "eli", botName: "Eli", threadId: "eli-main", title: "Results", body: "ignored" } as Notification);+ expect(infoRows(f).map((row) => row.text)).toEqual(["Results from Mira"]);+ f.guests.set("guest-chat", "p_guest");+ f.inbox.handedResult(work("mira-pricing", "For the guest."), { botId: "eli", threadId: "guest-chat" });+ expect(f.inbox.view(GUEST).rows.map((row) => row.text)).toEqual(["For the guest."]);+ expect(infoRows(f).map((row) => row.text)).toEqual(["Results from Mira"]);+ });+});+describe("who is asking for the inbox", () => {const session = { kind: "session", session: { id: "s" } } as unknown as RequestAuth;const ownerShell = { kind: "loopback", trust: "owner" } as unknown as RequestAuth;diff --git a/server/host/place-inbox.ts b/server/host/place-inbox.tsindex 23dea3fed3..4f53a135e7 100644--- a/server/host/place-inbox.ts+++ b/server/host/place-inbox.ts@@ -37,6 +37,8 @@ const lineSchema = z.object({/** The guest whose conversation this is: it is theirs, not the owner's. */guest: id.optional(),text: z.string().max(NOTICE_TEXT_MAX), at: z.number(), seen: z.boolean().optional(), done: z.boolean().optional(),+ /** The conversation this result is news for: its next unread result replaces this line, so one job keeps one row. */+ job: id.optional(),});const fileSchema = z.object({ version: z.literal(1), lines: z.array(lineSchema).max(MAX_LINES), seenAsks: z.array(id).max(MAX_SEEN_ASKS) });type Line = z.infer<typeof lineSchema>;@@ -180,6 +182,7 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {}function addLine(line: Line): Line {+ if (line.job) lines = lines.filter((one) => one.job !== line.job || one.seen || one.done);lines.push(line);save();return line;@@ -211,21 +214,34 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {return { status: 200, text: "It is in their Inbox and they were notified. Their answer comes back here, and you carry on from it." };}- /** A routine's report or a failure of work nobody is watching gets a line; the notification about it goes out as it is. */- function route(notification: Notification, how: { routine?: boolean } = {}): Notification {+ /** A routine's report, the answer that sums up work handed out, or a failure of work nobody is watching gets a line; the+ * notification about it goes out as it is. */+ function route(notification: Notification, how: { routine?: boolean; summary?: boolean } = {}): Notification {const { kind, botId, threadId } = notification;- const result = kind === "done" && how.routine === true;+ const result = (kind === "done" && (how.routine === true || how.summary === true)) || (kind === "delegation-settled" && how.summary === true);const notice = ASK_NOTIFICATIONS.has(kind) && kind !== "approval" && kind !== "question" && (how.routine === true || deps.handedBy(threadId) !== null);const bot = result || notice ? store.bot(botId) : undefined;if (bot) {const handed = workingFor(threadId);const guest = deps.guestOf(threadId);addLine({ id: crypto.randomUUID(), botId: bot.id, threadId, ...(notification.groupId ? { groupId: notification.groupId } : {}), kind: result ? "result" : "notice", from: bot.name.slice(0, 200),- ...(handed ? { workingFor: handed } : {}), ...(guest ? { guest } : {}), text: clip(notification.body || notification.title, NOTICE_TEXT_MAX), at: now() });+ ...(handed ? { workingFor: handed } : {}), ...(guest ? { guest } : {}), text: clip(notification.body || notification.title, NOTICE_TEXT_MAX), at: now(), ...(how.summary ? { job: threadId } : {}) });}return notification;}+ /** Work handed on finished: one line, so the result is here even if the conversation that asked never reports back. It+ * sends nothing, and that conversation's next result or its summary replaces it. */+ function handedResult(work: { botId: string; threadId: string; groupId?: string; text: string }, requester: { botId: string; threadId: string }): void {+ const bot = store.bot(work.botId);+ const text = work.text.trim();+ if (!bot || bot.notifications === false || !text) return;+ const asked = store.bot(requester.botId);+ const guest = deps.guestOf(requester.threadId);+ addLine({ id: crypto.randomUUID(), botId: bot.id, threadId: work.threadId, ...(work.groupId ? { groupId: work.groupId } : {}), kind: "result", from: bot.name.slice(0, 200),+ ...(asked ? { workingFor: { botId: asked.id, name: asked.name } } : {}), ...(guest ? { guest } : {}), text: clip(text, NOTICE_TEXT_MAX), at: now(), job: requester.threadId });+ }+const unsubscribe = store.onChange((change: StoreChange) => {if (change.type !== "thread.deleted") return;const before = lines.length;@@ -237,6 +253,7 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {view,message,route,+ handedResult,/** What waits on this viewer: every open ask, and news they have not read. */count(auth: RequestAuth): number {return view(auth).rows.filter((row) => row.tab === "needs-you" || (row.tab === "info" && row.unread)).length;diff --git a/server/host/team-messages.ts b/server/host/team-messages.tsindex 0e0667307b..a8d2a24e95 100644--- a/server/host/team-messages.ts+++ b/server/host/team-messages.ts@@ -419,9 +419,13 @@ export function createTeamMessages(deps: TeamMessagesDeps) {return waiting ? { returned: false, open: { botId: waiting.botId, threadId: waiting.threadId } } : { returned: false };},/** A notification about to go out: an ask, a failure or a routine's report routed to a person reaches their inbox. */- route(notification: Notification, how: { routine?: boolean } = {}): Notification | null {+ route(notification: Notification, how: { routine?: boolean; summary?: boolean } = {}): Notification | null {return !team && deps.place ? deps.place.route(notification, how) : inbox.route(notification, how);},+ /** Work handed on finished: on a place that is no team's, a line in the inbox (place-inbox.ts). */+ handedResult(work: { botId: string; threadId: string; groupId?: string; text: string }, requester: { botId: string; threadId: string }): void {+ if (!team) deps.place?.handedResult(work, requester);+ },stop(): void {unsubscribe();inbox.stop();diff --git a/server/index.ts b/server/index.tsindex 2aee79bbd3..0d72844fba 100644--- a/server/index.ts+++ b/server/index.ts@@ -3970,7 +3970,7 @@ const handoffTicks = createHandoffTicks(), coordinator: Coordinator = createCoorif (parent.groupId ? !group || !store.groupTaskByThread(group.id, parent.threadId) : !store.taskByThread(parent.botId, parent.threadId)) return;const bot = store.bot(child.botId);if (store.messagesFor(parent.threadId).some(m => m.roomRequest?.id === child.id && m.roomRequest.phase === "result")) return;- const problem = roomHandoffProblem(child, parent);+ const problem = roomHandoffProblem(child, parent); if (child.status === "completed" && !problem && child.mode === "work" && !child.statusOnly) teamMessages.handedResult({ botId: child.botId, threadId: child.threadId, ...(child.groupId ? { groupId: child.groupId } : {}), text: redactSecretsInText(child.result) }, { botId: parent.botId, threadId: parent.threadId });store.appendMessage(parent.threadId, {role: "bot", kind: "activity",roomRequest: { id: child.id, phase: "result" },@@ -4030,7 +4030,7 @@ const handoffTicks = createHandoffTicks(), coordinator: Coordinator = createCoorconst quiet = turnAnswers(node.threadId, { cardContinuation: true, coordination: node }).quiet;const routineRoot = !node.parentId && activeRoutineRunForThread(node.threadId) !== null;if (node.restart !== "rerun" && !quiet && !routineRoot && !node.parentId && asking.length === 0 && !doneNotice.owes(node.threadId) && !unaskedBotThread(store.taskByThread(bot.id, node.threadId), store.activePath(node.threadId))) {- notify(buildNotification("delegation-settled", bot, node.threadId, serverLine(language, "server.notify.resultsFrom", { who }), { avatarUrl: bot.avatarUrl }));+ notify(buildNotification("delegation-settled", bot, node.threadId, serverLine(language, "server.notify.resultsFrom", { who }), { avatarUrl: bot.avatarUrl }), { summary: true });}if (!node.parentId && !quiet) reviewAnswerThreads.add(node.threadId);} else if (node.restart === "rerun" && !group) {@@ -4961,7 +4961,7 @@ const providerLabel = (provider: string): string => {const quietTurnThreads = new Set<string>(); const doneNotice = createDoneNotice({ awaitingTeammates: (threadId) => coordinator.awaitingTeammateThreads().has(threadId), queuedFollowUp: hasQueuedPersonMessage }); const personWaits = createPersonWaits({ store, notify, threadsWithCards: threadsWithCardsSince });const QUIET_TURN_NEWS: ReadonlySet<Notification["kind"]> = new Set(["done", "turn-failed", "incident"]);-function notify(notification: Notification | null, how?: { routine?: boolean }) {+function notify(notification: Notification | null, how?: { routine?: boolean; summary?: boolean }) {if (notification && QUIET_TURN_NEWS.has(notification.kind) && quietTurnThreads.has(notification.threadId)) return;const routed = notification && teamMessages.route(notification, how); if (routed) broadcast({ kind: "notify", notification: logNotification(routed) });}@@ -6822,7 +6822,7 @@ bus.subscribe((event: RuntimeEvent) => {if (!routineReportThread && (!internal || reviewAnswer)) store.patchTask(bot.id, event.threadId, { unread: true });if (routineRun && routineReportThread !== event.threadId) automationReports.runSettled(routineRun, completedTurnId);else if ((!routineRun || routineRun.status === "completed") && (!internal || reviewAnswer) && !computerSelectionTurns.get(event.threadId)?.selected && doneNotice.atTurnEnd({ botId: bot.id, threadId: event.threadId, review: reviewAnswer, ok: event.ok, routine: Boolean(routineRun) }) === "notify" && !personWaits.atTurnEnd(event.threadId, reply) && !unaskedBotThread(settledTask, store.activePath(event.threadId))) {- notify(buildNotification("done", bot, event.threadId, routineRun ? reply || routineRun.output || routineRun.routineName : reply, { avatarUrl: bot.avatarUrl }), routineRun ? { routine: true } : undefined);+ notify(buildNotification("done", bot, event.threadId, routineRun ? reply || routineRun.output || routineRun.routineName : reply, { avatarUrl: bot.avatarUrl }), routineRun ? { routine: true } : reviewAnswer ? { summary: true } : undefined);}if (screenPollers.has(event.threadId)) {// Keep the thread busy until the final END-state capture: an accepted follow-up must not take the desktop first.diff --git a/server/runtime-coordinator.ts b/server/runtime-coordinator.tsindex 013038cc82..be4ecd86f4 100644--- a/server/runtime-coordinator.ts+++ b/server/runtime-coordinator.ts@@ -104,6 +104,7 @@ export function view(a: Assignment, parent?: Assignment): HandoffView {if (a.parentId !== undefined) node.parentId = a.parentId;if (groupId !== undefined) node.groupId = groupId;if (a.batchKey !== undefined) node.requestBatchKey = a.batchKey;+ if (a.statusOnly) node.statusOnly = true;if (a.startedAt !== undefined) node.startedAt = a.startedAt;const origin = legacyOrigin(a.origin);if (origin) node.origin = origin;