Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(documents): use plain mail bodies when HTML is empty

Afloataee1e493b1c6
diff --git a/apps/node-services/AGENTS.md b/apps/node-services/AGENTS.md
index 13238597..975e20fb 100644
--- a/apps/node-services/AGENTS.md
+++ b/apps/node-services/AGENTS.md
@@ -73,6 +73,10 @@
## Structure
+- MIME document normalization prefers extractable HTML, then the recorded
+ plain-text alternative when HTML has no extractable body. Headers alone do
+ not make an empty message indexable.
+
- `tests/<role>/` mirrors `src/<role>/`; host-level tests such as
`tests/main.test.ts` and `tests/http-host.test.ts` stay flat under `tests/`.
- Role-specific rules live in `src/agent/AGENTS.md`, `src/llm-wiki/AGENTS.md`,
diff --git a/apps/node-services/src/document-normalizer/document-mail-extract.ts b/apps/node-services/src/document-normalizer/document-mail-extract.ts
index 4a2a57f8..5c05d523 100644
--- a/apps/node-services/src/document-normalizer/document-mail-extract.ts
+++ b/apps/node-services/src/document-normalizer/document-mail-extract.ts
@@ -12,9 +12,10 @@ export async function extractMailMarkdown(source: Uint8Array): Promise<string> {
maxHtmlLengthToParse: 32 * 1024 * 1024,
});
if (mail.headers.size === 0) throw new Error("Missing MIME headers");
- const body = mail.html
+ let body = mail.html
? await extractOfficeMarkdown(Buffer.from(mail.html), "html")
: (mail.text ?? "");
+ if (!body.trim()) body = mail.text ?? "";
// Subject/header-only messages are not a substitute for an extractable body.
if (!body.trim())
throw new DocumentNormalizationError("DOCUMENT_SOURCE_EMPTY");
diff --git a/apps/node-services/tests/document-normalizer/document-mail-extract.test.ts b/apps/node-services/tests/document-normalizer/document-mail-extract.test.ts
new file mode 100644
index 00000000..7295d3ab
--- /dev/null
+++ b/apps/node-services/tests/document-normalizer/document-mail-extract.test.ts
@@ -0,0 +1,73 @@
+import { describe, expect, it } from "vitest";
+
+import { normalizeDocumentContent } from "../../src/document-normalizer/document-normalizer-core.js";
+
+function message(plain: string, html: string): Uint8Array {
+ return Buffer.from(
+ [
+ "MIME-Version: 1.0",
+ "Subject: Project update",
+ "From: Sender <[email protected]>",
+ 'Content-Type: multipart/alternative; boundary="body-alternatives"',
+ "",
+ "--body-alternatives",
+ "Content-Type: text/plain; charset=utf-8",
+ "Content-Transfer-Encoding: base64",
+ "",
+ Buffer.from(plain).toString("base64"),
+ "--body-alternatives",
+ "Content-Type: text/html; charset=utf-8",
+ "Content-Transfer-Encoding: base64",
+ "",
+ Buffer.from(html).toString("base64"),
+ "--body-alternatives--",
+ "",
+ ].join("\r\n"),
+ );
+}
+
+async function extract(plain: string, html: string): Promise<string> {
+ const bytes = await normalizeDocumentContent({
+ filename: "update.eml",
+ mediaType: "message/rfc822",
+ source: message(plain, html),
+ });
+ return new TextDecoder().decode(bytes);
+}
+
+describe("MIME body alternatives", () => {
+ it.each([
+ ["empty HTML", "<html><body> </body></html>"],
+ ["image-only HTML", '<html><body><img src="cid:logo"></body></html>'],
+ ])(
+ "uses the recorded plain body after extracting %s",
+ async (_name, html) => {
+ const markdown = await extract(
+ "Approved budget: 1250 €.\nShip on Friday.",
+ html,
+ );
+ expect(markdown).toContain("Approved budget: 1250 €.");
+ expect(markdown).toContain("Ship on Friday.");
+ expect(markdown).toContain("Project update");
+ expect(markdown).toContain("[email protected]");
+ expect(markdown).not.toContain("cid:logo");
+ },
+ );
+
+ it("prefers extractable HTML over its plain alternative", async () => {
+ const markdown = await extract(
+ "Plain alternative only",
+ "<p>HTML body with <strong>formatting</strong></p>",
+ );
+ expect(markdown).toContain("HTML body with **formatting**");
+ expect(markdown).not.toContain("Plain alternative only");
+ });
+
+ it("still rejects header-only mail when neither body has text", async () => {
+ await expect(
+ extract(" \n", "<html><body> </body></html>"),
+ ).rejects.toMatchObject({
+ code: "DOCUMENT_SOURCE_EMPTY",
+ });
+ });
+});