Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Chat: a wait's note names its wait only when that wait's card confirms it

luishron1238051284ed91d
diff --git a/packages/phone-protocol/src/wire.ts b/packages/phone-protocol/src/wire.ts
index e9eb98856f..70bccce42d 100644
--- a/packages/phone-protocol/src/wire.ts
+++ b/packages/phone-protocol/src/wire.ts
@@ -520,7 +520,10 @@ export interface WireMessage extends ServerTextLine, HelperIntroField, SetupCard
relayed?: boolean;
/** A user line a routine, webhook, room coordination or another bot's
* message started a turn with: the person did not write it. */
- automated?: boolean; /** On a wait follow-up's line: that wait's id, so the chat names it when two waits write the same words. */ waitId?: string;
+ automated?: boolean;
+ /** On a wait follow-up's line: that wait's id, so the chat names it when
+ * two waits write the same words. */
+ waitId?: string;
/** Which person sent this user message, when the workspace has more than
* one. Absent reads as the profile name. */
sender?: ResolvedSender;
@@ -605,17 +608,10 @@ export interface WireMessage extends ServerTextLine, HelperIntroField, SetupCard
export const movesThreadToTop = (message: Pick<WireMessage, "role" | "kind" | "peerAsk" | "turnId" | "turnTerminal">): boolean =>
message.role === "user" ? !message.peerAsk : message.kind === "text" && (message.turnTerminal === true || !message.turnId);
+/** Pinned first, then newest; ties keep their order, since sort is stable. */
export function orderedThreadList<T extends { pinned?: boolean; lastThreadOrderAt?: number; createdAt?: number }>(tasks: readonly T[]): T[] {
- return tasks
- .map((task, index) => ({ task, index }))
- .sort((a, b) => {
- const pin = Number(b.task.pinned === true) - Number(a.task.pinned === true);
- if (pin) return pin;
- const recency = (b.task.lastThreadOrderAt ?? b.task.createdAt ?? 0) - (a.task.lastThreadOrderAt ?? a.task.createdAt ?? 0);
- if (recency) return recency;
- return a.index - b.index;
- })
- .map((entry) => entry.task);
+ const recency = (task: T) => task.lastThreadOrderAt ?? task.createdAt ?? 0;
+ return [...tasks].sort((a, b) => Number(b.pinned === true) - Number(a.pinned === true) || recency(b) - recency(a));
}
/** 0 is the "until new activity" sentinel. The event stream never drops an expired snooze, so check the clock too. */
diff --git a/src/components/sidebar-preview.test.ts b/src/components/sidebar-preview.test.ts
index d6e9a1cd44..e24566e47e 100644
--- a/src/components/sidebar-preview.test.ts
+++ b/src/components/sidebar-preview.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import type { Bot, Group, Message } from "@/state/store";
import { botPreview, groupPreview, lastPreviewable, serverCardText, waitingHandoffPreview } from "./sidebar-preview";
import { setLocale } from "@/lib/i18n";
+import { WAIT_MESSAGES, waitSourceRefusal } from "../../server/routes/waits";
let at = 0;
const message = (fields: Partial<Message>): Message => ({ id: `m${++at}`, at, role: "bot", kind: "text", ...fields }) as Message;
@@ -39,21 +40,36 @@ describe("sidebar previews", () => {
expect(botPreview(bot, [message({ text: "Earlier reply" }), message({ role: "user", text: "Tidy my inbox" }), step("Bash")], [], false)).not.toBe("Earlier reply");
});
+ const card = (label: string, followUp: string) => message({ kind: "options", card: { title: "", options: [], waitRequest: { waitId: label, label, followUp } } } as unknown as Partial<Message>);
+
it("quotes a wait's note in Simple mode after a step that went fine, but not a person's line written the same way", () => {
- const note = message({ role: "user", text: "[Wait finished: report] Summarize.", automated: true } as Partial<Message>);
- expect(botPreview(bot, [note], [], false)).toBe("Following up on the wait: report");
- expect(botPreview(bot, [note, step("Read")], [], false)).toBe("Following up on the wait: report");
- expect(botPreview(bot, [message({ text: "Earlier reply" }), note, step("Read")], [], false)).toBe("Following up on the wait: report");
+ const report = card("report", "Summarize.");
+ const note = message({ role: "user", text: "[Wait finished: report] Summarize.", automated: true, waitId: "report" } as Partial<Message>);
+ expect(botPreview(bot, [report, note], [], false)).toBe("Following up on the wait: report");
+ expect(botPreview(bot, [report, note, step("Read")], [], false)).toBe("Following up on the wait: report");
+ expect(botPreview(bot, [report, message({ text: "Earlier reply" }), note, step("Read")], [], false)).toBe("Following up on the wait: report");
const typed = message({ role: "user", text: "[Wait finished: report] Summarize." });
- expect(botPreview(bot, [message({ text: "Earlier reply" }), typed, step("Read")], [], false)).not.toBe("Earlier reply");
- expect(botPreview(bot, [message({ text: "Earlier reply" }), typed, step("Read")], [], false)).not.toBe("Following up on the wait: report");
+ expect(botPreview(bot, [report, message({ text: "Earlier reply" }), typed, step("Read")], [], false)).not.toBe("Earlier reply");
+ expect(botPreview(bot, [report, message({ text: "Earlier reply" }), typed, step("Read")], [], false)).not.toBe("Following up on the wait: report");
+ });
+
+ it("names no wait in the preview when the note's own card is not loaded, a step after it included", () => {
+ const text = "[Wait finished: build] prod] Check logs";
+ for (const [waitId, other] of [["build", card("build] prod", "Check logs")], ["build] prod", card("build", "prod] Check logs")]] as const) {
+ const note = message({ role: "user", text, automated: true, waitId } as Partial<Message>);
+ expect(botPreview(bot, [other, note], [], false)).toBe("Following up on a wait");
+ expect(botPreview(bot, [other, note, step("Read")], [], false)).toBe("Following up on a wait");
+ }
+ const bracketed = message({ role: "user", text: "[Wait finished: release [QA] aprovada] Publish.", automated: true } as Partial<Message>);
+ expect(botPreview(bot, [bracketed, step("Read")], [], false)).toBe("Following up on a wait");
});
- it("shows a wait's note in a group chat as the line it is, a step after it included", () => {
- const note = message({ role: "user", text: "[Wait finished: report] Summarize.", automated: true } as Partial<Message>);
+ it("credits a line the person typed in a group chat to them", () => {
+ const typed = message({ role: "user", text: "Summarize the report." });
const group = (messages: Message[]) => ({ id: "g", name: "Team", memberIds: ["ada"], messages }) as unknown as Group;
- expect(groupPreview(group([note]), [bot], [], false)).toBe("You: [Wait finished: report] Summarize.");
- expect(groupPreview(group([note, step("Read")]), [bot], [], false)).toBe("You: [Wait finished: report] Summarize.");
+ expect(groupPreview(group([typed]), [bot], [], false)).toBe("You: Summarize the report.");
+ // A group chat refuses waits (server/routes/waits.ts), so no wait's note reaches one and its preview credits no one here.
+ expect(waitSourceRefusal({ owned: true, room: true, depth: 0, handedOff: false, routineRun: false, outsideDriven: false, personStarted: true, guest: false, op: "create" })).toBe(WAIT_MESSAGES.room);
});
it("keeps a failed step's error line and a recovery or notice line as they are", () => {
diff --git a/src/lib/automation-note.test.ts b/src/lib/automation-note.test.ts
index 01014b1790..6194efedf0 100644
--- a/src/lib/automation-note.test.ts
+++ b/src/lib/automation-note.test.ts
@@ -33,12 +33,13 @@ describe("automationNote", () => {
it("reads a wait's follow-up as the app's line, not the person's", () => {
const text = followUpLine("the report", "Read /tmp/report.txt\nand say what changed.");
expect(text).toBe("[Wait finished: the report] Read /tmp/report.txt\nand say what changed.");
- expect(automationNote(line(text))).toEqual({ wait: "the report", instructions: "Read /tmp/report.txt\nand say what changed." });
+ expect(automationNote(line(text), knownWaits([waitCard("the report", "Read /tmp/report.txt\nand say what changed.")]))).toEqual({ wait: "the report", instructions: "Read /tmp/report.txt\nand say what changed." });
});
it("keeps the app's second-start line out of the instructions and names what cut the first start off", () => {
- expect(automationNote(line(followUpLine("the build", "Ship it.", "restarted")))).toEqual({ wait: "the build", instructions: "Ship it.", resumed: "restart" });
- expect(automationNote(line(followUpLine("the build", "Ship it.", "stalled")))).toEqual({ wait: "the build", instructions: "Ship it.", resumed: "interrupted" });
+ const waits = knownWaits([waitCard("the build", "Ship it.")]);
+ expect(automationNote(line(followUpLine("the build", "Ship it.", "restarted")), waits)).toEqual({ wait: "the build", instructions: "Ship it.", resumed: "restart" });
+ expect(automationNote(line(followUpLine("the build", "Ship it.", "stalled")), waits)).toEqual({ wait: "the build", instructions: "Ship it.", resumed: "interrupted" });
});
it("reads a second start that only carries the app's line", () => {
@@ -68,7 +69,15 @@ describe("automationNote", () => {
expect(automationNote(line(text), knownWaits([card]))).toEqual({ wait: "release [QA] aprovada", instructions: "Publish [the] notes." });
expect(automationNote(line(text), knownWaits([waitCard("release [QA", "Something else."), card]))?.wait).toBe("release [QA] aprovada");
expect(lastLine({} as Bot, [card, line(text)], [], false)).toBe("Following up on the wait: release [QA] aprovada");
- expect(automationNote(line(text))?.wait).toBe("release [QA");
+ });
+
+ it("names no wait, and keeps the whole line, when no loaded card confirms where the name ends", () => {
+ const text = followUpLine("release [QA] aprovada", "Publish [the] notes.");
+ for (const waits of [[], knownWaits([waitCard("other", "Publish [the] notes.")])]) {
+ expect(automationNote(line(text), waits)).toEqual({ unnamed: true, instructions: text });
+ expect(automationNote(line(followUpLine("release [QA] aprovada", "Publish [the] notes.", "restarted")), waits)).toEqual({ unnamed: true, instructions: text, resumed: "restart" });
+ }
+ expect(lastLine({} as Bot, [line(text)], [], false)).toBe("Following up on a wait");
});
it("picks the wait whose card matches the whole line, not the longest name that starts it", () => {
@@ -94,6 +103,20 @@ describe("automationNote", () => {
expect(automationNote(line(followUpLine("build", "prod] Check logs", "restarted")), waits)).toEqual({ unnamed: true, instructions: text, resumed: "restart" });
});
+ it("names no wait when the line's own card is not loaded, even if another wait's card matches it", () => {
+ const short = waitCard("build", "prod] Check logs"), long = waitCard("build] prod", "Check logs");
+ const text = followUpLine("build", "prod] Check logs"), resumed = followUpLine("build", "prod] Check logs", "restarted");
+ for (const [waitId, other] of [["build", long], ["build] prod", short]] as const) {
+ expect(automationNote(line(text, { waitId }), knownWaits([other]))).toEqual({ unnamed: true, instructions: text });
+ expect(automationNote(line(resumed, { waitId }), knownWaits([other]))).toEqual({ unnamed: true, instructions: text, resumed: "restart" });
+ expect(automationNote(line(text, { waitId }), [])).toEqual({ unnamed: true, instructions: text });
+ expect(lastLine({} as Bot, [other, line(text, { waitId })], [], false)).toBe("Following up on a wait");
+ expect(lastLine({} as Bot, [other, line(resumed, { waitId })], [], false)).toBe("Following up on a wait");
+ }
+ setLocale("pt-br");
+ expect(automationNoteTitle(automationNote(line(text, { waitId: "build" }), knownWaits([long]))!)).toBe("Continuação de uma espera");
+ });
+
it("sends again the line a reply answers, and offers for editing only what the person typed", () => {
const asked = typed("Tell me when the report is ready", { id: "asked" });
const wait = line(followUpLine("the report", "Say it is ready."), { id: "wait" });
@@ -106,16 +129,16 @@ describe("automationNote", () => {
});
it("is no bubble of the person's, no line they wrote, and previews in the app's words", () => {
- const wait = line(followUpLine("the report", "Say it is ready."), { id: "wait" });
+ const wait = line(followUpLine("the report", "Say it is ready."), { id: "wait" }), card = waitCard("the report", "Say it is ready.");
const asked = line("Tell me when the report is ready", { id: "asked" });
const answer = { id: "answer", role: "bot", kind: "text", at: 3, text: "It is ready." } as Message;
expect(bubbleGroup(asked, "bot")).toBe("user");
expect(bubbleGroup(wait, "bot")).toBeNull();
expect(firstUnreadMessageId([asked, wait, answer])).toBe("wait");
expect(unreadContextMessage([asked, wait, answer], "answer")?.id).toBe("asked");
- expect(lastLine({} as Bot, [asked, wait], [], false)).toBe("Following up on the wait: the report");
+ expect(lastLine({} as Bot, [card, asked, wait], [], false)).toBe("Following up on the wait: the report");
setLocale("pt-br");
- expect(automationNoteTitle(automationNote(wait)!)).toBe("Continuação da espera: the report");
+ expect(automationNoteTitle(automationNote(wait, knownWaits([card]))!)).toBe("Continuação da espera: the report");
expect(automationNoteTitle(automationNote(line(continueNote("restarted")))!)).toBe("Retomada depois que o OpenMausBot reiniciou");
});
});
diff --git a/src/lib/automation-note.ts b/src/lib/automation-note.ts
index 7bd373ac00..ef6f5c56e4 100644
--- a/src/lib/automation-note.ts
+++ b/src/lib/automation-note.ts
@@ -6,7 +6,7 @@ import type { WireMessage as Message } from "../../shared/wire.ts";
export interface AutomationNote {
/** The person's words for what the wait was for. Absent on a second start that only carries the app's own line. */
wait?: string;
- /** A wait's follow-up whose wait the line cannot single out: an older line, with no id, that two waits' cards both match whole. */
+ /** A wait's follow-up whose wait no loaded card confirms: its own card not loaded yet, or an older line, with no id, that two cards match whole or none names. */
unnamed?: true;
/** What the bot was asked to do then. Empty when the line holds none. */
instructions: string;
@@ -15,7 +15,6 @@ export interface AutomationNote {
}
const PREFIX = "[Wait finished: ";
-const WAIT_FINISHED = /^\[Wait finished: ([^\n]*?)\] /;
const SECOND_START = /(?:^|\n\n)\[OpenMausBot: this run's first start ([^\]]*?) before it finished\. [^\]]*\]$/;
/** The waits a transcript's own cards record: what each was called and what it was to do afterwards. */
@@ -27,19 +26,19 @@ export function knownWaits(messages: readonly Pick<Message, "card">[]): KnownWai
return waits;
}
-// A label may itself hold "] ", where the text alone cannot tell it from the instructions: the wait's card can.
-// The wait the server recorded on the line decides. A line without one takes the card that matches it whole, second-start part aside,
-// then one that starts it, then the longest name; null when two cards match it whole, rather than guess.
+// A label may itself hold "] ", where the text alone cannot tell it from the instructions: only the wait's card can.
+// A line with the wait's id takes that wait's card or no name at all, since a paged transcript may not hold it yet.
+// A line without one takes the card that matches it whole, second-start part aside, then one that starts it, then the longest name;
+// null, never a guess, when two cards match it whole or no card names it.
function waitLabel(text: string, waitId: string | undefined, waits: readonly KnownWait[]): string | null | undefined {
if (!text.startsWith(PREFIX)) return undefined;
const named = waits.filter((wait) => text.startsWith(`${PREFIX}${wait.label}] `)).sort((a, b) => b.label.length - a.label.length);
- const own = waitId === undefined ? undefined : named.find((wait) => wait.waitId === waitId);
- if (own) return own.label;
+ if (waitId !== undefined) return named.find((wait) => wait.waitId === waitId)?.label ?? null;
const body = text.slice(0, SECOND_START.exec(text)?.index ?? text.length).trim();
const said = (wait: KnownWait) => `${PREFIX}${wait.label}] ${wait.followUp}`;
const whole = named.filter((wait) => body === said(wait).trim());
if (new Set(whole.map((wait) => wait.label)).size > 1) return null;
- return (whole[0] ?? named.find((wait) => body.startsWith(said(wait))) ?? named[0])?.label ?? WAIT_FINISHED.exec(text)?.[1];
+ return (whole[0] ?? named.find((wait) => body.startsWith(said(wait))) ?? named[0])?.label ?? null;
}
/** Only a line the server marked as started by an automation: the same words typed by a person stay theirs. */