Skip to content

A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.

TypeScript

fix: keep surrogate numeric character references from failing the mail list (#183)

Charan Rathorea4944f4c471d
diff --git a/packages/integrations/src/google.ts b/packages/integrations/src/google.ts
index 9231493..e7982db 100644
--- a/packages/integrations/src/google.ts
+++ b/packages/integrations/src/google.ts
@@ -188,7 +188,10 @@ function decodeSnippet(value: string): string {
entity[1].toLowerCase() === "x"
? Number.parseInt(entity.slice(2), 16)
: Number.parseInt(entity.slice(1), 10);
- return code >= 0 && code <= 0x10ffff ? String.fromCodePoint(code) : original;
+ // Surrogates are not scalar values: fromCodePoint throws on them, and one
+ // such reference in a snippet must not reject the whole message list.
+ const scalar = code >= 0 && code <= 0x10ffff && !(code >= 0xd800 && code <= 0xdfff);
+ return scalar ? String.fromCodePoint(code) : original;
},
);
}
diff --git a/tests/google.test.ts b/tests/google.test.ts
index 441d0c8..6f16a34 100644
--- a/tests/google.test.ts
+++ b/tests/google.test.ts
@@ -221,6 +221,19 @@ test("header decoding preserves spaces encoded inside words and next to plain te
assert.equal((await client.getThread("thread1"))[0].subject, "Re: Visit résumé notes");
});
+test("a numeric character reference to a surrogate stays literal instead of failing the list", async () => {
+ // String.fromCodePoint throws on surrogate code points; a sender can put the
+ // literal text &#xD800; in a snippet, and one such message must not reject
+ // the whole list (Promise.all over per-message parses).
+ const client = clientWith((request) =>
+ new URL(request.url).pathname.endsWith("/messages")
+ ? json({ messages: [{ id: "msg1" }] })
+ : json({ id: "msg1", threadId: "thread1", snippet: "Entity notes: &#xD800; and &#55296;" }),
+ );
+ const [mail] = await client.listMail();
+ assert.equal(mail.body, "Entity notes: &#xD800; and &#55296;");
+});
+
test("a body charset TextDecoder does not know falls back to UTF-8 instead of failing the list", async () => {
const charsets: Record<string, string> = { known: "utf-8", unknown: "unknown-8bit" };
const bodies: Record<string, string> = { known: "Opening hours", unknown: "Café tickets" };