Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(import): keep an emoji whole when an imported name hits the length cap
kevin9327d6ab52748a7f
diff --git a/shared/import-name.test.ts b/shared/import-name.test.tsnew file mode 100644index 0000000000..cf4be6ec1f--- /dev/null+++ b/shared/import-name.test.ts@@ -0,0 +1,36 @@+import { describe, expect, it } from "vitest";++import { takeImportName } from "./import-name.ts";++const loneSurrogate = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/;++describe("takeImportName", () => {+ it("numbers a taken name and reserves the result", () => {+ const taken = new Set(["ada"]);+ expect(takeImportName("Ada", taken)).toBe("Ada 2");+ expect(takeImportName(" Ada ", taken)).toBe("Ada 3");+ expect(taken.has("ada 3")).toBe(true);+ });++ it("keeps the name within the limit", () => {+ const taken = new Set<string>();+ expect(takeImportName("a".repeat(70), taken, 60)).toHaveLength(60);+ expect(takeImportName("a".repeat(70), taken, 60)).toBe(`${"a".repeat(58)} 2`);+ });++ it("does not cut an emoji in half at the limit", () => {+ const taken = new Set<string>();+ const name = takeImportName(`${"a".repeat(59)}π`, taken, 60);+ expect(name).toBe("a".repeat(59));+ expect(loneSurrogate.test(name)).toBe(false);+ });++ it("does not cut an emoji in half to make room for the number", () => {+ const taken = new Set<string>();+ const first = takeImportName(`${"a".repeat(57)}πb`, taken, 60);+ const second = takeImportName(`${"a".repeat(57)}πb`, taken, 60);+ expect(first).toBe(`${"a".repeat(57)}πb`);+ expect(second).toBe(`${"a".repeat(57)} 2`);+ expect(loneSurrogate.test(second)).toBe(false);+ });+});diff --git a/shared/import-name.ts b/shared/import-name.tsindex f662b2b803..c29555dbd0 100644--- a/shared/import-name.ts+++ b/shared/import-name.ts@@ -1,10 +1,16 @@+/** Cut to `max` UTF-16 units without leaving the high half of a surrogate pair at the end. */+function cut(value: string, max: number): string {+ const head = value.slice(0, Math.max(0, max));+ return /[\ud800-\udbff]$/.test(head) ? head.slice(0, -1) : head;+}+/** Shared by import and preview, including names reserved by archived bots. */export function takeImportName(value: string, taken: Set<string>, maxLength = 100): string {- const base = value.trim().slice(0, maxLength);+ const base = cut(value.trim(), maxLength);let name = base;for (let n = 2; taken.has(name.toLowerCase()); n++) {const suffix = ` ${n}`;- name = `${base.slice(0, maxLength - suffix.length).trimEnd()}${suffix}`;+ name = `${cut(base, maxLength - suffix.length).trimEnd()}${suffix}`;}taken.add(name.toLowerCase());return name;