Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(ts): port the From: header rules the Python side already has

PerryLink584277f843b0
diff --git a/laya-ts/src/email.ts b/laya-ts/src/email.ts
index 09c1cc24..32614400 100644
--- a/laya-ts/src/email.ts
+++ b/laya-ts/src/email.ts
@@ -5,13 +5,28 @@ const QUOTE_HEADERS: RegExp[] = [
/^\s*-{2,}\s*(Original|Forwarded) Message\s*-{2,}/i,
/^\s*-{2,}\s*(Mensagem (original|encaminhada)|Mensaje (original|reenviado))\s*-{2,}/i,
/^\s*_{8,}\s*$/,
- /^\s*From:\s.+$/i,
+ // `From:` opens ordinary prose too ("From: my side the integration works, but please
+ // refund..."), and a reply header always carries the sender, so the header is only recognised
+ // when an address follows -- the same rule as `De:` below. A bare `From: Name` header is
+ // caught by HEADER_FROM_NAME/HEADER_NEXT instead, which need the header's own `Sent:`/`Date:`
+ // line to tell it apart from a sentence.
+ /^\s*From:\s.*[@<]/i,
+ // `De:` also opens ordinary Portuguese/Spanish lines ("De: 10/09 a 15/09"), so the Outlook
+ // header is only recognised when it carries an address
/^\s*De:\s.*[@<]/i,
];
const ATTRIBUTION_TAIL = /^.{0,120}\S@\S+\s+(wrote|escreveu|escribi[óo]):\s*$/i;
const ATTRIBUTION_HEAD = /^\s*(On|Em|El) (?=.*\d)/i;
-const HEADER_FROM_NAME = /^\s*De:\s+\S/i;
-const HEADER_NEXT = /^\s*(Enviad[oa]( em| el)?:\s|(Data|Fecha):\s.*\d{4})/i;
+// Exchange often leaves the address out of Outlook's reply header ("De: Maria Souza"), so a bare
+// `De:` only cuts when the header's own `Enviado:` line, or a dated `Data:`/`Fecha:` line, follows
+// it. `Para:` is not enough: "De: 10/09 / Para: 15/09" is how a leave request reads.
+//
+// The same is true of a bare English `From: Maria Souza`, which is why the marker above needs
+// this rule: the English client lines are the translations of the two `De:` neighbours. A line
+// that only looks like prose still has to be told apart from a header by its neighbours, so the
+// English pair is "From: <name>" followed by "Sent:"/"Date:".
+const HEADER_FROM_NAME = /^\s*(De|From):\s+\S/i;
+const HEADER_NEXT = /^\s*(Enviad[oa]( em| el)?:\s|Sent:\s|(Data|Fecha|Date):\s.*\d{4})/i;
// A closing line is the closing word plus punctuation and at most a name. Anything else on
// the line is a sentence, and the case of the next word is what separates the two: a name is
// capitalised, "for" in "Thanks for the quick reply." is not. JS regexes have no scoped
diff --git a/laya-ts/tests/email-presets.test.ts b/laya-ts/tests/email-presets.test.ts
index 3df2caac..02e8740b 100644
--- a/laya-ts/tests/email-presets.test.ts
+++ b/laya-ts/tests/email-presets.test.ts
@@ -31,6 +31,35 @@ describe("email+presets", () => {
const out = cleanEmailBody("Please review the draft when you can.\nIt is two pages.\nWarmest regards,\nŁukasz");
expect(out).toBe("Please review the draft when you can.\nIt is two pages.");
});
+ // `From:` opens ordinary prose as well as a reply header, and a reply header always carries
+ // the sender, so the marker only cuts when an address follows -- Python's `From:\s.*[@<]`.
+ // The looser `From:\s.+$` this port shipped with deleted the rest of every request whose body
+ // happened to contain a line starting "From: ", which is issue #338 on the Python side.
+ it("keeps a request whose prose line starts with From: (Python parity)", () => {
+ const body = "Hi team, the export failed again this morning.\n"
+ + "From: my side the integration works, but the downstream job still times out.\n"
+ + "Could you take a look before Friday?";
+ expect(cleanEmailBody(body)).toBe(body);
+ });
+ it("still cuts a From: line that carries an address", () => {
+ const out = cleanEmailBody("Please refund my order\n\nFrom: Bob <[email protected]>\nold text");
+ expect(out).toContain("Please refund my order");
+ expect(out).not.toContain("old text");
+ });
+ // A bare `From: Name` header has no address, so it is told apart from a sentence by its
+ // neighbours: the English client lines are the translations of the `De:` pair below.
+ it("cuts a bare From: name header followed by Sent:", () => {
+ const out = cleanEmailBody("Please refund order 123.\nFrom: Maria Souza\nSent: Monday\nold");
+ expect(out).toBe("Please refund order 123.");
+ });
+ it("does not cut a bare From: name with no header after it", () => {
+ const body = "Please refund order 123.\nFrom: Maria Souza\nPlease help with my refund.";
+ expect(cleanEmailBody(body)).toBe(body);
+ });
+ it("cuts a De: name header followed by a dated Date: (Python parity)", () => {
+ const out = cleanEmailBody("Please refund order 123.\nDe: Maria Souza\nDate: 12/09/2026\nold");
+ expect(out).toBe("Please refund order 123.");
+ });
it("cuts thanks in advance plus a two-word name", () => {
const out = cleanEmailBody("Please review the draft when you can.\nIt is two pages.\nThanks in advance,\nPriya Nair");
expect(out).toBe("Please review the draft when you can.\nIt is two pages.");