Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Collect a routine report's files by the turn that sent them
Xiaolei Shi611767356e82
diff --git a/server/automation-reports.e2e.test.ts b/server/automation-reports.e2e.test.tsindex 8259148f2c..8f94c29dc0 100644--- a/server/automation-reports.e2e.test.ts+++ b/server/automation-reports.e2e.test.ts@@ -428,6 +428,11 @@ it("brings the files a run sent with attach_file into its report", async () => {expect(await s.lines()).toEqual([expect.objectContaining({text: "Today's brief is attached.", attachments: [expect.objectContaining({ kind: "file", name: "brief.pdf" })],})]);+ const inRun = await s.messages((await s.run(id)).threadId);+ const reply = inRun.findLast((message) => message.role === "bot" && message.turnTerminal);+ const sentLine = inRun.find((message) => message.attachments?.some((file: any) => file.name === "brief.pdf"));+ expect(sentLine).toMatchObject({ sentInTurn: reply.turnId });+ expect(sentLine).not.toHaveProperty("turnId");}, 120_000);it("leaves the images a run's tools returned in the run, and reports the files it sent", async () => {diff --git a/server/automation-reports.test.ts b/server/automation-reports.test.tsindex 79e31bdda5..1636db3e3d 100644--- a/server/automation-reports.test.ts+++ b/server/automation-reports.test.ts@@ -441,52 +441,147 @@ describe("automation reports", () => {expect(f.lines()).toEqual([expect.objectContaining({ text: "Here is this week's report.", attachments })]);});+ /** A file, and the line attach_file posts for it in `turn`. */+ const pdf = (name: string) => ({ kind: "file" as const, path: `${name}-1.pdf`, mime: "application/pdf", name: `${name}.pdf` });+ const sent = (store: Store, threadId: string, sentInTurn: string, file: NonNullable<Message["attachments"]>[number]) =>+ store.appendMessage(threadId, { role: "bot", kind: "text", text: "", attachments: [file], sentInTurn });++ it("carries only a reply's own files after a restart when no line of the run names a turn", () => {+ const f = fixture();+ const run = f.finished(null);+ const attachments = [{ kind: "file" as const, path: "report-1.pdf", mime: "application/pdf", name: "Weekly report.pdf" }];+ f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "", attachments: [pdf("Earlier")] });+ f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "Here is this week's report.", attachments });+ f.runs.get(run.id)!.report = { state: "pending" };+ f.create().flush();+ expect(f.lines()).toEqual([expect.objectContaining({ text: "Here is this week's report.", attachments })]);+ });+it("carries the files the run's last turn sent, in order, and not the images its tools returned", () => {const f = fixture();const run = f.finished(null);const thread = run.threadId!;- const earlier = { kind: "file" as const, path: "old.pdf", mime: "application/pdf", name: "Old.pdf" };- const pdf = { kind: "file" as const, path: "brief-1.pdf", mime: "application/pdf", name: "Brief.pdf" };- const mp3 = { kind: "file" as const, path: "brief-1.mp3", mime: "audio/mpeg", name: "Brief.mp3" };+ const turn = `turn-${run.id}`;const page = { kind: "image" as const, path: "page-1.png", mime: "image/png", fromTool: true as const };const drawn = { kind: "image" as const, path: "chart-1.png", mime: "image/png" };const note = { kind: "audio" as const, path: "note-1.ogg", mime: "audio/ogg", text: "Today's brief." };- const slides = { kind: "file" as const, path: "brief-1.pptx", mime: "application/vnd.openxmlformats-officedocument.presentationml.presentation", name: "Brief.pptx" };- f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [earlier] });+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [pdf("Unowned")] });+ sent(f.store, thread, "turn-before", pdf("Earlier"));f.store.appendMessage(thread, { role: "user", kind: "text", text: "Go on." });- f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [pdf] });+ sent(f.store, thread, turn, pdf("Brief"));f.store.appendMessage(thread, { role: "user", kind: "text", text: "Add the audio too.", steered: true });- f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [mp3] });- f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Your brief.", attachments: [page, drawn, note], turnId: `turn-${run.id}` });- f.store.appendMessage(thread, { role: "bot", kind: "text", text: "", attachments: [slides] });- f.reports.runSettled(run, `turn-${run.id}`);- expect(f.lines()).toEqual([expect.objectContaining({ text: "Your brief.", attachments: [pdf, mp3, drawn, note, slides] })]);+ sent(f.store, thread, turn, pdf("Audio"));+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Your brief.", attachments: [page, drawn, note], turnId: turn });+ sent(f.store, thread, turn, pdf("Slides"));+ f.reports.runSettled(run, turn);+ expect(f.lines()).toEqual([expect.objectContaining({ text: "Your brief.", attachments: [pdf("Brief"), pdf("Audio"), drawn, note, pdf("Slides")] })]);});- it("reports a run that only sent files, without a word", () => {+ it("keeps the files sent before and after a goal asked while the run's turn worked", () => {const f = fixture();const run = f.finished(null);- const pdf = { kind: "file" as const, path: "brief-1.pdf", mime: "application/pdf", name: "Brief.pdf" };- f.store.appendMessage(run.threadId!, { role: "user", kind: "text", text: "Do the Daily brief check." });- f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "", attachments: [pdf] });- f.store.appendMessage(run.threadId!, { role: "bot", kind: "activity", tool: { name: "Bash", ok: true }, turnId: `turn-${run.id}` });+ const thread = run.threadId!;+ const turn = `turn-${run.id}`;+ f.store.appendMessage(thread, { role: "user", kind: "text", text: "Do the Daily brief check." });+ sent(f.store, thread, turn, pdf("Brief"));+ f.store.appendMessage(thread, { role: "user", kind: "text", text: "Also plan next week.", channelMode: "goal" });+ sent(f.store, thread, turn, pdf("Audio"));+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Your brief.", turnId: turn, turnTerminal: true });+ f.reports.runSettled(run, turn);+ expect(f.lines()).toEqual([expect.objectContaining({ text: "Your brief.", attachments: [pdf("Brief"), pdf("Audio")] })]);+ });++ it("leaves out the files of earlier turns and of a goal that ran before the run resumed", () => {+ const f = fixture();+ const run = f.finished(null);+ const thread = run.threadId!;+ const turn = `turn-${run.id}`;+ f.store.appendMessage(thread, { role: "user", kind: "text", text: "Do the Stock check." });+ sent(f.store, thread, "turn-before", pdf("Earlier"));+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Asked Sam to count the stock.", turnId: "turn-before", turnTerminal: true });+ f.store.appendMessage(thread, { role: "user", kind: "text", text: "Plan next week.", channelMode: "goal" });+ sent(f.store, thread, "turn-goal", pdf("Plan"));+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Next week is planned.", turnId: "turn-goal", turnTerminal: true });+ sent(f.store, thread, turn, pdf("Stock"));+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "We have 42 boxes in stock.", turnId: turn, turnTerminal: true });+ f.reports.runSettled(run, turn);+ expect(f.lines()).toEqual([expect.objectContaining({ text: "We have 42 boxes in stock.", attachments: [pdf("Stock")] })]);+ });++ it("reports a run whose last turn only sent files, without a word", () => {+ const f = fixture();+ const run = f.finished(null);+ const thread = run.threadId!;+ f.store.appendMessage(thread, { role: "user", kind: "text", text: "Do the Daily brief check." });+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Asked Sam for the numbers.", turnId: "turn-before", turnTerminal: true });+ sent(f.store, thread, `turn-${run.id}`, pdf("Brief"));f.reports.runSettled(run, `turn-${run.id}`);- expect(f.lines()).toEqual([expect.objectContaining({ text: "", attachments: [pdf] })]);+ expect(f.lines()).toEqual([expect.objectContaining({ text: "", attachments: [pdf("Brief")] })]);expect(f.runs.get(run.id)!.report).toMatchObject({ state: "posted" });});+ it("reports what a run's last turn sent after a restart, though that turn wrote no words", () => {+ const f = fixture();+ const run = f.finished(null);+ const thread = run.threadId!;+ sent(f.store, thread, "turn-before", pdf("Earlier"));+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Asked Sam to count the stock.", turnId: "turn-before", turnTerminal: true });+ sent(f.store, thread, `turn-${run.id}`, pdf("Stock"));+ f.busy.add(f.main);+ f.reports.runSettled(run, `turn-${run.id}`);+ f.busy.delete(f.main);+ f.create().flush();+ expect(f.lines()).toEqual([expect.objectContaining({ text: "", attachments: [pdf("Stock")] })]);+ });++ it("reports what a run's last turn sent after a restart, though a later turn in the run was cut off mid-reply", () => {+ const f = fixture();+ const run = f.finished(null);+ const thread = run.threadId!;+ sent(f.store, thread, `turn-${run.id}`, pdf("Stock"));+ f.store.appendMessage(thread, { role: "user", kind: "text", text: "Count the shelves too." });+ f.store.appendMessage(thread, { role: "bot", kind: "text", text: "Counting the shelves", turnId: "turn-after" });+ f.runs.get(run.id)!.report = { state: "pending" };+ f.create().flush();+ expect(f.lines()).toEqual([expect.objectContaining({ text: "", attachments: [pdf("Stock")] })]);+ });++ it("reads a run's words and the file it sent after them again after a restart, without an earlier turn's file", () => {+ const f = fixture();+ const run = f.finished(null);+ sent(f.store, run.threadId!, "turn-before", pdf("Earlier"));+ f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "Asked Sam for the numbers.", turnId: "turn-before", turnTerminal: true });+ f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "Your brief.", turnId: `turn-${run.id}`, turnTerminal: true });+ sent(f.store, run.threadId!, `turn-${run.id}`, pdf("Brief"));+ f.runs.get(run.id)!.report = { state: "pending" };+ f.create().flush();+ expect(f.lines()).toEqual([expect.objectContaining({ text: "Your brief.", attachments: [pdf("Brief")] })]);+ });++ it("says only that an automation recovered when the turn that resumed its run said and sent nothing", () => {+ const f = fixture();+ const failed = f.finished(null, "Inbox brief", { status: "failed", error: "Timed out" });+ f.reports.runFailed(failed, "it ran past its 25-minute limit.");+ const finishedAt = Date.UTC(2026, 9, 26, 2, 40);+ const run = f.finished(null, "Inbox brief", { finishedAt });+ sent(f.store, run.threadId!, "turn-before", pdf("Earlier"));+ f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "Asked Sam to count the stock.", turnId: "turn-before", turnTerminal: true });+ f.reports.runSettled(run, `turn-${run.id}`);+ expect(f.lines().at(-1)).toMatchObject({ text: recoveredText("Inbox brief", finishedAt), automation: expect.objectContaining({ recovered: true }) });+ expect(f.lines().at(-1)).not.toHaveProperty("attachments");+ });+it("still carries the turn's files when the person wrote in the run before its report was written", () => {const f = fixture();const run = f.finished(null);- const pdf = { kind: "file" as const, path: "brief-1.pdf", mime: "application/pdf", name: "Brief.pdf" };- f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "", attachments: [pdf] });+ sent(f.store, run.threadId!, `turn-${run.id}`, pdf("Brief"));f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "Your brief.", turnId: `turn-${run.id}`, turnTerminal: true });f.busy.add(f.main);f.reports.runSettled(run, `turn-${run.id}`);f.store.appendMessage(run.threadId!, { role: "user", kind: "text", text: "Thanks." });f.busy.delete(f.main);f.reports.flush();- expect(f.lines()).toEqual([expect.objectContaining({ text: "Your brief.", attachments: [pdf] })]);+ expect(f.lines()).toEqual([expect.objectContaining({ text: "Your brief.", attachments: [pdf("Brief")] })]);});it("says only that an automation recovered, without the files its quiet run sent", () => {@@ -494,7 +589,7 @@ describe("automation reports", () => {const failed = f.finished(null, "Inbox brief", { status: "failed", error: "Timed out" });f.reports.runFailed(failed, "it ran past its 25-minute limit.");const ok = f.finished(null);- f.store.appendMessage(ok.threadId!, { role: "bot", kind: "text", text: "", attachments: [{ kind: "file", path: "brief-1.pdf", mime: "application/pdf", name: "Brief.pdf" }] });+ sent(f.store, ok.threadId!, `turn-${ok.id}`, pdf("Brief"));f.store.appendMessage(ok.threadId!, { role: "bot", kind: "text", text: "NOTHING NEW", turnId: `turn-${ok.id}`, turnTerminal: true });f.reports.runSettled(ok, `turn-${ok.id}`);expect(f.lines().at(-1)).toMatchObject({ automation: expect.objectContaining({ recovered: true }) });@@ -505,6 +600,7 @@ describe("automation reports", () => {const f = fixture();const run = f.finished(null);f.store.appendMessage(run.threadId!, { role: "user", kind: "text", text: "Do the Stock check." });+ sent(f.store, run.threadId!, "turn-before", pdf("Earlier"));f.store.appendMessage(run.threadId!, { role: "bot", kind: "text", text: "Asked Sam to count the stock.", turnId: "turn-before", turnTerminal: true });f.reports.runSettled(run, `turn-${run.id}`);expect(f.added()).toEqual([]);diff --git a/server/automation-reports.ts b/server/automation-reports.tsindex adf1da86fa..38981f76ee 100644--- a/server/automation-reports.ts+++ b/server/automation-reports.ts@@ -80,18 +80,12 @@ export function newestAutomationLines(messages: readonly Pick<Message, "id" | "a}const saidLine = (message: Message) => message.role === "bot" && message.kind === "text";-/** A person's line that starts a turn, not one folded into a turn already running. */-const asks = (message: Message) => message.role === "user" && !message.steered && !message.aside;--/** The lines of the turn that holds this line: after the person's line that started it, up to their next. */-function turnOf(path: readonly Message[], line: Message): Message[] {- const at = path.findIndex((message) => message.id === line.id);- const next = path.findIndex((message, index) => index > at && asks(message));- return path.slice(path.findLastIndex((message, index) => index < at && asks(message)) + 1, next < 0 ? undefined : next);-}+const turnOfLine = (message: Message) => message.turnId ?? message.sentInTurn;type Waiting =- | { kind: "reply"; run: RoutineRun; messageId?: string }+ /** `messageId`: the line the run's reply is read from; null when its last turn said and sent nothing, absent when it is+ * unknown (a report still waiting after a restart). */+ | { kind: "reply"; run: RoutineRun; messageId?: string | null }| { kind: "failure"; run: RoutineRun; detail: string }| { kind: "card"; run: RoutineRun; threadId: string; messageId: string };@@ -126,18 +120,23 @@ export function createAutomationReports(deps: AutomationReportDeps) {executionThreadId: run.threadId!, scheduledFor: run.scheduledFor, ...extra,});- /** The reply a run ends with: its last turn's last text, or after a restart the last one a turn settled on. */- const replyOf = (run: RoutineRun, messageId?: string) => {+ /** The reply a run ends with: its last turn's last text, or after a restart the last line of the last turn that+ * settled or sent a file. */+ const replyOf = (run: RoutineRun, messageId?: string | null) => {const path = run.threadId ? store.activePath(run.threadId) : [];- return messageId ? path.find((message) => message.id === messageId) : path.findLast((message) => saidLine(message) && message.turnTerminal) ?? path.findLast(saidLine);+ if (messageId !== undefined) return messageId === null ? undefined : path.find((message) => message.id === messageId);+ const last = path.findLast((message) => saidLine(message) && (message.turnTerminal || message.sentInTurn !== undefined));+ const turn = last?.sentInTurn;+ return (turn ? path.findLast((message) => saidLine(message) && message.turnId === turn) ?? last : last) ?? path.findLast(saidLine);};/** The files the reply's turn sent the person, in order: its attach_file lines' and the reply's own, without the* images its tools returned. */const sentWith = (run: RoutineRun, reply: Message | undefined): NonNullable<Message["attachments"]> => {if (!reply) return [];- return turnOf(store.activePath(run.threadId!), reply).flatMap((message) => message.role === "bot" && message.kind === "text" ? message.attachments ?? [] : [])- .filter((file) => file.kind !== "image" || !file.fromTool);+ const turn = turnOfLine(reply);+ const lines = turn ? store.activePath(run.threadId!).filter((message) => saidLine(message) && turnOfLine(message) === turn) : [reply];+ return lines.flatMap((message) => message.attachments ?? []).filter((file) => file.kind !== "image" || !file.fromTool);};const read = (run: RoutineRun, message: Message | undefined) => readRunReply(message?.text ?? "", sentWith(run, message).length > 0);@@ -264,13 +263,12 @@ export function createAutomationReports(deps: AutomationReportDeps) {if (run.target !== "bot" || run.status !== "completed" || !run.threadId) return;if (run.deliver === "brief") return deps.brief?.(run, turnId);const path = store.activePath(run.threadId);- const earlier = (message: Message) => asks(message) || (message.turnId !== undefined && message.turnId !== turnId);- const said = turnId ? path.findLast((message) => saidLine(message) && message.turnId === turnId) ?? path.slice(path.findLastIndex(earlier) + 1).findLast(saidLine)+ const said = turnId ? path.findLast((message) => saidLine(message) && message.turnId === turnId) ?? path.findLast((message) => saidLine(message) && message.sentInTurn === turnId): replyOf(run);const reply = read(run, said);const bot = store.bot(run.botId);const target = destination(run);- if (target && (!reply.quiet || lastSaidFailed(run, target.threadId))) hold(run.id, { kind: "reply", run, ...(said ? { messageId: said.id } : {}) });+ if (target && (!reply.quiet || lastSaidFailed(run, target.threadId))) hold(run.id, { kind: "reply", run, messageId: said?.id ?? null });else if (reply.quiet) deps.routines()?.noteReport(run.id, { state: "quiet" });else if (bot) deps.notify("done", bot, run.threadId, reply.text);},diff --git a/server/bot-attachment.ts b/server/bot-attachment.tsindex d8e8b894a7..0259f2f49b 100644--- a/server/bot-attachment.ts+++ b/server/bot-attachment.ts@@ -118,6 +118,10 @@ export function deliveredCopy(messages: readonly Message[], reply: Message, namereturn found;}+export function attachedFileLine(attachment: BotAttachment, sentInTurn: string | undefined, from?: Message["from"]): Omit<Message, "id" | "at"> {+ return { role: "bot", kind: "text", text: "", attachments: [attachment], ...(sentInTurn ? { sentInTurn } : {}), ...(from ? { from } : {}) };+}+export type AttachForTurnOutcome<S> =| { status: "attached"; saved: S }| { status: "limit" }diff --git a/server/index.ts b/server/index.tsindex 030ed86253..985da4f6da 100644--- a/server/index.ts+++ b/server/index.ts@@ -145,7 +145,7 @@ import {type Runtime,} from "./container-computer.ts";import { containerComputerMcp } from "./local-vm-box.ts";-import { attachForTurn, saveBotAttachment } from "./bot-attachment.ts";+import { attachedFileLine, attachForTurn, saveBotAttachment } from "./bot-attachment.ts";import {cacheUntilConfigChanges,ensureDirs,@@ -13263,15 +13263,8 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {},stillLive: () => internalCapabilityIsActive({ ...internalCapability, ...(vm ? { localVmTarget: vm } : {}) }) &&connectorThread(from.id, threadId) !== null,- publish: (saved) => {- store.appendMessage(threadId, {- role: "bot",- kind: "text",- text: "",- attachments: [saved.attachment],- ...(source.group ? { from: { botId: from.id, name: from.name, color: from.color } } : {}),- });- },+ publish: (saved) => store.appendMessage(threadId, attachedFileLine(saved.attachment, liveTurnByThread.get(threadId),+ source.group ? { botId: from.id, name: from.name, color: from.color } : undefined)),discard: (saved) => deleteAttachment(saved.attachment.path),});if (outcome.status === "limit") {diff --git a/server/store.ts b/server/store.tsindex 9dbfa4a5f0..09c6d9d946 100644--- a/server/store.ts+++ b/server/store.ts@@ -49,7 +49,7 @@ export type {export type { GroupTask as GroupTaskRecord, BotProject as BotProjectRecord } from "../shared/wire.ts";export type { InstalledPlaybook, InstalledPackageMetadata, MausColor, MausExpression } from "../shared/wire.ts";-export type Message = WireMessage;+export type Message = WireMessage & { /** The turn an attach_file line was sent in; never makes it that turn's reply. */ sentInTurn?: string };export interface GroupPackageStamp {installId: string;