Skip to content

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

Keep Chinese sentence boundaries and surrogate pairs in SOUL summaries

focuspoints9dc7e2f57323
diff --git a/src/components/BotSettingsDialog.simple.test.ts b/src/components/BotSettingsDialog.simple.test.ts
index b4e429c937..c5ab8e8e19 100644
--- a/src/components/BotSettingsDialog.simple.test.ts
+++ b/src/components/BotSettingsDialog.simple.test.ts
@@ -654,6 +654,15 @@ describe("the Details tab", () => {
});
describe("the Bot settings page", () => {
+ it.each(["整理文件。", "整理檔案。"])("migrates the full SOUL while retaining only %s as the description", (first) => {
+ const description = `${first}${"遵守安全要求。".repeat(80)}`;
+ const rendered = capture(() => SoulField({ bot: makeBot({ description, soul: "" }), onPatch: patch }));
+ const migrate = rendered.nodes.find((node) => node.type === "button" && node.props.children === "Move instructions into SOUL.md");
+ expect(migrate).toBeDefined();
+ click(migrate!);
+ expect(patch).toHaveBeenCalledExactlyOnceWith({ soul: description, description: first });
+ });
+
it("holds Instructions, Before it acts, Backup AIs, Call mode, Apps, Job and Hide, with every title on one edge", () => {
const rendered = settingsPage(makeBot({ defaultBot: {} }));
const order = ['data-panel-edit="soul"', 'data-panel-row="approval"', 'data-panel-row="backups"', 'data-panel-row="call-mode"', 'data-panel-page="apps"', 'data-panel-row="job"', 'data-panel-row="default-bot-hide"']
diff --git a/src/lib/soul.test.ts b/src/lib/soul.test.ts
index 3b5e68e334..cb84299161 100644
--- a/src/lib/soul.test.ts
+++ b/src/lib/soul.test.ts
@@ -18,6 +18,27 @@ describe("firstSentence", () => {
expect(firstSentence("No punctuation here")).toBe("No punctuation here");
});
+ it.each(["。", "!", "?"])("ends a Chinese sentence at %s without requiring whitespace", (ending) => {
+ expect(firstSentence(` 整理文件${ending}不要制造噪音。`)).toBe(`整理文件${ending}`);
+ expect(firstSentence(` 整理檔案${ending}不要製造雜訊。`)).toBe(`整理檔案${ending}`);
+ });
+
+ it("keeps the existing English punctuation boundaries", () => {
+ expect(firstSentence("Use v1.2, e.g. for files. Next.")).toBe("Use v1.2, e.g.");
+ expect(firstSentence("No.space!inside?here")).toBe("No.space!inside?here");
+ expect(firstSentence("Files bugs?! Never noise.")).toBe("Files bugs?!");
+ });
+
+ it("caps UTF-16 length without splitting an astral Han character or emoji", () => {
+ for (const astral of ["𠀀", "🐭"]) {
+ const prefix = "文".repeat(199);
+ expect(firstSentence(`${prefix}${astral}继续`)).toBe(prefix);
+ const complete = `${"文".repeat(198)}${astral}`;
+ expect(firstSentence(`${complete}继续`)).toBe(complete);
+ expect(firstSentence(`${astral}。下一句`, 1)).toBe("");
+ }
+ });
+
it("stops at the first line break and caps the length", () => {
expect(firstSentence("Line one\nLine two.")).toBe("Line one");
expect(firstSentence("a".repeat(500), 200)).toHaveLength(200);
diff --git a/src/lib/soul.ts b/src/lib/soul.ts
index 80ff7fcaef..9cfbc300a2 100644
--- a/src/lib/soul.ts
+++ b/src/lib/soul.ts
@@ -4,9 +4,9 @@ export function utf8Bytes(text: string): number {
export function firstSentence(text: string, max = 200): string {
const line = text.trim().split("\n")[0] ?? "";
- const match = line.match(/^.*?[.!?](?=\s|$)/);
+ const match = line.match(/^.*?(?:[。!?]|[.!?](?=\s|$))/u);
const sentence = (match ? match[0] : line).trim();
- return sentence.slice(0, max);
+ return sentence.slice(0, max).replace(/[\uD800-\uDBFF]$/u, "");
}
export function soulPatchFor(value: string, limit: number): { soul: string } | null {