Skip to content

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

fix(chat): keep stops aimed at another bot out of the current turn

Afloatfe4231c4b168
diff --git a/server/host/person-stop.test.ts b/server/host/person-stop.test.ts
index 5e65a6b8d5..54c8971fea 100644
--- a/server/host/person-stop.test.ts
+++ b/server/host/person-stop.test.ts
@@ -58,6 +58,24 @@ describe("a typed stop", () => {
expect(w.rows).toHaveLength(1);
});
+ it.each(["stop @Todo", "stop @Now", "@Adaplease stop", "@Ada.stop stop", "@everyone stop @Todo"])(
+ "leaves the current turn running when the stop names someone else: %s", async (text) => {
+ const w = world({ working: ["t", "other"] });
+ expect(await w.send(text, { busy: true })).toBeNull();
+ expect(w.stopped).toEqual([]);
+ expect(w.rows).toEqual([]);
+ },
+ );
+
+ it.each(["@aDa stop", "(**@Ada**) STOP!", "@all stop"])(
+ "still stops work addressed to the bot or everyone: %s", async (text) => {
+ const w = world({ working: ["t"] });
+ expect(await w.send(text, { busy: true })).toMatchObject({ ok: true });
+ expect(w.stopped).toEqual(["t"]);
+ expect(w.rows).toHaveLength(1);
+ },
+ );
+
it("leaves a real sentence, another's tag, an app's message, a routine and a spoken line alone", async () => {
const quiet = async (text: string, input: Partial<TypedStop> = {}, over = {}) => {
const w = world({ working: ["t"], ...over });
diff --git a/server/room-holds.test.ts b/server/room-holds.test.ts
index 72f9945788..9445ea3a89 100644
--- a/server/room-holds.test.ts
+++ b/server/room-holds.test.ts
@@ -131,6 +131,40 @@ describe("isStopCommand", () => {
expect(isStopCommand("@everyone-else stop")).toBe(false);
});
+ it.each([
+ "stop @Todo",
+ "stop @Now",
+ "stop @Please",
+ "stop @Stop",
+ "@Adaplease stop",
+ "@Ada.stop stop",
+ "@everyoneplease stop",
+ "/@Ada stop",
+ "@everyone stop @Todo",
+ ])("keeps another name or a non-mention out of a typed stop: %s", (text) => {
+ expect(isStopCommand(text, "Ada")).toBe(false);
+ });
+
+ it.each([
+ ["@aDa stop", "Ada"],
+ ["(@Ada) STOP!", "Ada"],
+ ["**@Ada** stop!", "Ada"],
+ ["@Ada. stop", "Ada"],
+ ["@Ada-Bot stop", "Ada-Bot"],
+ ["@Ada.Bot stop", "Ada.Bot"],
+ ["@A+Bot stop", "A+Bot"],
+ ["@Chris Assistent stop", "Chris Assistent"],
+ ["@Rene\u0301 stop", "René"],
+ ["【@小助手】停止", "小助手"],
+ ["@🐝 Bee stop", "🐝 Bee"],
+ ["@Todo stop", "Todo"],
+ ["@Everyone Please stop", "Everyone Please"],
+ ["@everyone stop", "Ada"],
+ ["@all stop", "Ada"],
+ ])("keeps a complete allowed name usable: %s", (text, name) => {
+ expect(isStopCommand(text, name)).toBe(true);
+ });
+
it("reads any other message as a message, including a quoted or coded stop", () => {
for (const text of ["", "all right", "stop using tabs", "don't stop", "pause", "stop the build and run tests", "`stop`", "\"stop\"", "> stop",
"why did it stop?", "it stopped", "never stop", "non-stop", "stop-loss at 5%", "cancel my 3pm", "please stop the server and restart it", "stop\nthen deploy"]) {
diff --git a/server/room-holds.ts b/server/room-holds.ts
index 77b328b0be..d13fe5c0c9 100644
--- a/server/room-holds.ts
+++ b/server/room-holds.ts
@@ -94,8 +94,21 @@ function isBareResume(text: string): boolean {
/** `ownName`: the bot it was sent to, which may be tagged. A tag of anyone else is a message about them, not a stop. */
export function isStopCommand(text: string, ownName?: string): boolean {
const line = maskQuotedAndCode(text).normalize("NFC").toLowerCase();
- const untagged = ["everyone", ...(ownName ? [ownName.normalize("NFC").toLowerCase()] : [])].reduce((rest, name) => rest.split(`@${name}`).join(" "), line);
- const words = untagged.match(/[\p{L}\p{M}\p{N}_-]+/gu) ?? [];
+ const names = ["everyone", "all", ...(ownName ? [ownName.normalize("NFC").toLowerCase()] : [])].sort((a, b) => b.length - a.length);
+ const parts: string[] = [];
+ let start = 0;
+ for (let at = line.indexOf("@"); at !== -1; at = line.indexOf("@", start)) {
+ if (!isMentionBoundary(line, at)) return false;
+ const name = names.find((candidate) => {
+ const tail = line.slice(at + 1 + candidate.length);
+ return line.startsWith(candidate, at + 1) && !isMentionNameContinuation(tail) && !/^[.-][\p{L}\p{M}\p{N}_]/u.test(tail);
+ });
+ if (!name) return false;
+ parts.push(line.slice(start, at));
+ start = at + 1 + name.length;
+ }
+ parts.push(line.slice(start));
+ const words = parts.join(" ").match(/[\p{L}\p{M}\p{N}_-]+/gu) ?? [];
return words.some((word) => STOP_COMMANDS.has(word)) && words.every((word) => STOP_COMMANDS.has(word) || STOP_FILLERS.has(word));
}