Skip to content

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

Support Chinese mention and slash-menu queries in the composer

focuspointse39a25f816e6
diff --git a/src/lib/composer-commands.test.ts b/src/lib/composer-commands.test.ts
index 74710c974e..291ef19af5 100644
--- a/src/lib/composer-commands.test.ts
+++ b/src/lib/composer-commands.test.ts
@@ -1,4 +1,5 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { activeLocale, loadLocale, setLocale, t } from "./i18n";
import {
composerSlashTrigger,
goalTextFromComposer,
@@ -33,6 +34,28 @@ describe("composer slash commands", () => {
expect(composerSlashTrigger("```\nx\n```\n/st", 13)).toEqual({ query: "st", start: 10, end: 13 });
});
+ describe("localized search", () => {
+ let previousLocale: string;
+ beforeEach(() => { previousLocale = activeLocale(); });
+ afterEach(() => { setLocale(previousLocale); });
+
+ it.each([["zh", "让这"], ["zh-tw", "讓這"]])("finds the translated goal description for %s without translating the command", async (locale, query) => {
+ expect(await loadLocale(locale)).toBe(locale);
+ const text = `/${query}`;
+ const trigger = composerSlashTrigger(text, text.length);
+ expect(trigger).toEqual({ query, start: 0, end: text.length });
+ expect(slashCommandMatches({ kind: "command", id: "goal", label: "/goal", description: t("composer.command.goalBotDesc") }, trigger!.query)).toBe(true);
+ expect(goalTextFromComposer(text)).toBeNull();
+ expect(skillSlashCommands([{ name: query, description: "Not an executable skill name" }])).toEqual([]);
+ });
+
+ it("keeps localized queries out of URLs and code", () => {
+ expect(composerSlashTrigger("https://host/让这", 15)).toBeNull();
+ expect(composerSlashTrigger("```\n/讓這", 7)).toBeNull();
+ expect(composerSlashTrigger("`a /让这`", 6)).toBeNull();
+ });
+ });
+
it("replaces the active token without losing text after the caret", () => {
expect(
replaceComposerSlashTrigger("/go later", { query: "go", start: 0, end: 3 }, ""),
diff --git a/src/lib/composer-commands.ts b/src/lib/composer-commands.ts
index f993d05f1f..0cbb1022ad 100644
--- a/src/lib/composer-commands.ts
+++ b/src/lib/composer-commands.ts
@@ -32,7 +32,7 @@ export interface ComposerSlashTrigger {
export function composerSlashTrigger(text: string, caretInput: number): ComposerSlashTrigger | null {
const caret = Math.max(0, Math.min(text.length, Math.floor(caretInput)));
const prefix = text.slice(0, caret);
- const match = /(?:^|\s)\/([a-z0-9-]*)$/i.exec(prefix);
+ const match = /(?:^|\s)\/([\p{L}\p{N}\p{M}-]*)$/u.exec(prefix);
if (!match) return null;
const query = match[1] ?? "";
const start = caret - query.length - 1;
diff --git a/src/lib/mentions.test.ts b/src/lib/mentions.test.ts
index 677114ec45..dd4762b017 100644
--- a/src/lib/mentions.test.ts
+++ b/src/lib/mentions.test.ts
@@ -1,9 +1,36 @@
import { describe, expect, it } from "vitest";
-import { mentionChoicesForQuery, mentionClassName, mentionRanges, mentionRowDescription } from "./mentions";
+import { mentionChoicesForQuery, mentionClassName, mentionQueryAt, mentionRanges, mentionRowDescription } from "./mentions";
const peers = [{ name: "Atlas" }, { name: "New Bot" }, { name: "New Bot 2" }, { name: "調査担当" }, { name: "Hidden", hidden: true }];
const matches = (text: string, everyone = false) => mentionRanges(text, peers, everyone).map(({ start, end }) => text.slice(start, end));
+describe("mention query at the caret", () => {
+ it.each(["(", "【", "“", "「", "《", "(", "**"])("opens after the shared opening boundary %s", (opening) => {
+ const text = `请${opening}@调`;
+ expect(mentionQueryAt(text, text.length)).toEqual({ start: text.indexOf("@"), query: "调" });
+ });
+
+ it("keeps start, whitespace, multiword names and text after the caret", () => {
+ expect(mentionQueryAt("@", 1)).toEqual({ start: 0, query: "" });
+ expect(mentionQueryAt("ask @New Bot later", 12)).toEqual({ start: 4, query: "New Bot" });
+ expect(mentionQueryAt("请\n@調査", 5)).toEqual({ start: 2, query: "調査" });
+ });
+
+ it.each(["me@调", "https://host/@调", "`@调`", "请)@调"])("keeps non-boundaries out of suggestions: %s", (text) => {
+ expect(mentionQueryAt(text, text.indexOf("@") + 2)).toBeNull();
+ });
+
+ it.each(["`(@调`", "```\n【@调\n```", " (@调"])("leaves the shared opening boundary inside code alone: %s", (text) => {
+ expect(mentionQueryAt(text, text.indexOf("@") + 2)).toBeNull();
+ });
+
+ it("retains the query length and newline limits", () => {
+ expect(mentionQueryAt(`@${"调".repeat(24)}`, 25)).toEqual({ start: 0, query: "调".repeat(24) });
+ expect(mentionQueryAt(`@${"调".repeat(25)}`, 26)).toBeNull();
+ expect(mentionQueryAt("@调\n后", 4)).toBeNull();
+ });
+});
+
describe("mention display ranges", () => {
it("keeps original casing, duplicate occurrences and longest names", () => {
expect(matches("@ATLAS, ask @New Bot 2 and @Atlas.")).toEqual(["@ATLAS", "@New Bot 2", "@Atlas"]);
diff --git a/src/lib/mentions.ts b/src/lib/mentions.ts
index 27e11acc6b..f1840cb0fc 100644
--- a/src/lib/mentions.ts
+++ b/src/lib/mentions.ts
@@ -3,6 +3,7 @@ import { MAUS_COLORS, type MausColor } from "./mascot";
import { isMentionBoundary, isMentionNameContinuation } from "../../shared/mention-boundary";
import { personTagAt, personTagNames } from "../../shared/person-mention";
import { devsTagAt } from "../../shared/devs-mention";
+import { blankCode } from "../../shared/skill-command";
/** A name the @ list offers: a bot's, or (`person`) a teammate's on a Team Cloud. */
export type MentionPeer = { name: string; hidden?: boolean; color?: MausColor; person?: true };
@@ -17,7 +18,7 @@ export function mentionQueryAt(text: string, caret: number): { start: number; qu
const upto = text.slice(0, caret);
const at = upto.lastIndexOf("@");
if (at === -1) return null;
- if (at > 0 && !/\s/.test(upto[at - 1])) return null;
+ if (!isMentionBoundary(upto, at) || blankCode(text)[at] !== "@") return null;
const query = upto.slice(at + 1);
if (query.length > 24 || query.includes("@") || query.includes("\n")) return null;
return { start: at, query };