Skip to content

Commit 4e36fe8

Browse files
committed
perf(matrix): reuse prepared outbound body
1 parent 977bbe2 commit 4e36fe8

4 files changed

Lines changed: 80 additions & 7 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Matrix tests cover outbound preparation reuse before provider delivery.
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import type { PluginRuntime } from "../../runtime-api.js";
4+
import { setMatrixRuntime } from "../runtime.js";
5+
import type { MatrixClient } from "./sdk.js";
6+
7+
const bodyProjection = vi.hoisted(() => ({ count: 0 }));
8+
9+
vi.mock("./format.js", async () => {
10+
const actual = await vi.importActual<typeof import("./format.js")>("./format.js");
11+
return {
12+
...actual,
13+
markdownToMatrixBody: (markdown: string) => {
14+
bodyProjection.count += 1;
15+
return actual.markdownToMatrixBody(markdown);
16+
},
17+
};
18+
});
19+
20+
import { sendMessageMatrix } from "./send.js";
21+
22+
const runtimeStub = {
23+
channel: {
24+
text: {
25+
chunkMarkdownTextWithMode: (text: string) => [text],
26+
resolveChunkMode: () => "length",
27+
resolveMarkdownTableMode: () => "code",
28+
resolveTextChunkLimit: () => 4000,
29+
},
30+
},
31+
} as unknown as PluginRuntime;
32+
33+
describe("sendMessageMatrix preparation performance", () => {
34+
beforeEach(() => {
35+
bodyProjection.count = 0;
36+
setMatrixRuntime(runtimeStub);
37+
});
38+
39+
it("projects a single-event body once before provider delivery", async () => {
40+
const sentContent: Array<Record<string, unknown>> = [];
41+
const client = {
42+
getUserId: async () => "@bot:example.org",
43+
prepareRoomForMessageSend: async () => "m.room.message",
44+
sendMessage: async (_roomId: string, content: Record<string, unknown>) => {
45+
sentContent.push(content);
46+
return "$event";
47+
},
48+
} as unknown as MatrixClient;
49+
50+
await sendMessageMatrix("room:!room:example.org", "ordinary **Matrix** reply", {
51+
cfg: {},
52+
client,
53+
});
54+
55+
expect(bodyProjection.count).toBe(1);
56+
expect(sentContent).toHaveLength(1);
57+
expect(sentContent[0]).toMatchObject({
58+
body: "ordinary **Matrix** reply",
59+
format: "org.matrix.custom.html",
60+
});
61+
});
62+
});

‎extensions/matrix/src/matrix/send.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,13 @@ export async function sendMessageMatrix(
227227
: null;
228228
let plannedEvents: MatrixPreparedEvent[] | undefined = storedPlan?.events;
229229
if (!plannedEvents) {
230-
const { chunks, tableMode } = chunkMatrixText(messageText, {
230+
const preparedText = chunkMatrixText(messageText, {
231231
cfg,
232232
accountId: opts.accountId,
233233
preserveWhitespace: true,
234234
});
235+
const { chunks, convertedText, preparedBody, fitsInSingleEvent, tableMode } = preparedText;
236+
const singleEventBody = fitsInSingleEvent ? preparedBody : undefined;
235237
const relation = threadId
236238
? buildThreadRelation(threadId, opts.replyToId)
237239
: buildReplyRelation(opts.replyToId);
@@ -301,6 +303,7 @@ export async function sendMessageMatrix(
301303
client,
302304
content,
303305
markdown: captionMarkdown,
306+
preparedBody: captionMarkdown === convertedText ? singleEventBody : undefined,
304307
tableMode,
305308
});
306309
prepareContent(content, receiptKind);
@@ -315,6 +318,7 @@ export async function sendMessageMatrix(
315318
client,
316319
content: followup,
317320
markdown: chunk,
321+
preparedBody: chunk === convertedText ? singleEventBody : undefined,
318322
tableMode,
319323
});
320324
prepareContent(followup, "text");
@@ -329,6 +333,7 @@ export async function sendMessageMatrix(
329333
client,
330334
content,
331335
markdown: chunk,
336+
preparedBody: chunk === convertedText ? singleEventBody : undefined,
332337
tableMode,
333338
});
334339
prepareContent(content, "text");
@@ -522,6 +527,7 @@ export async function sendSingleTextMessageMatrix(
522527
const {
523528
trimmedText,
524529
convertedText,
530+
preparedBody,
525531
singleEventLimit,
526532
eventTextLength,
527533
fitsInSingleEvent,
@@ -561,6 +567,7 @@ export async function sendSingleTextMessageMatrix(
561567
client,
562568
content,
563569
markdown: convertedText,
570+
preparedBody,
564571
includeMentions: opts.includeMentions,
565572
tableMode,
566573
});
@@ -631,7 +638,7 @@ export async function editMessageMatrix(
631638
async (client) => {
632639
const resolvedRoom = await resolveMatrixRoomId(client, roomId);
633640
const cfg = requireRuntimeConfig(opts.cfg, "Matrix message edit") as CoreConfig;
634-
const { convertedText, tableMode } = prepareMatrixSingleText(newText, {
641+
const { convertedText, preparedBody, tableMode } = prepareMatrixSingleText(newText, {
635642
cfg,
636643
accountId: opts.accountId,
637644
preserveWhitespace: true,
@@ -646,6 +653,7 @@ export async function editMessageMatrix(
646653
client,
647654
content: newContent,
648655
markdown: convertedText,
656+
preparedBody,
649657
includeMentions: opts.includeMentions,
650658
tableMode,
651659
});

‎extensions/matrix/src/matrix/send/chunking.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
type MatrixPreparedSingleText = {
2626
trimmedText: string;
2727
convertedText: string;
28+
preparedBody: string;
2829
singleEventLimit: number;
2930
eventTextLength: number;
3031
fitsInSingleEvent: boolean;
@@ -194,13 +195,12 @@ export function prepareMatrixSingleText(
194195
MATRIX_FORMAT_PROFILE.chunk.limit,
195196
),
196197
);
197-
const eventTextLength = Math.max(
198-
convertedText.length,
199-
markdownToMatrixBody(convertedText).length,
200-
);
198+
const preparedBody = markdownToMatrixBody(convertedText);
199+
const eventTextLength = Math.max(convertedText.length, preparedBody.length);
201200
return {
202201
trimmedText,
203202
convertedText,
203+
preparedBody,
204204
singleEventLimit,
205205
eventTextLength,
206206
fitsInSingleEvent: eventTextLength <= singleEventLimit,

‎extensions/matrix/src/matrix/send/formatting.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ const getCore = () => getMatrixRuntime();
2727
async function renderMatrixFormattedContent(params: {
2828
client: MatrixClient;
2929
markdown?: string | null;
30+
preparedBody?: string;
3031
includeMentions?: boolean;
3132
tableMode?: MarkdownTableMode;
3233
}): Promise<{ body: string; html?: string; mentions?: MatrixMentions }> {
3334
const markdown = params.markdown ?? "";
34-
const body = markdownToMatrixBody(markdown);
35+
const body = params.preparedBody ?? markdownToMatrixBody(markdown);
3536
if (params.includeMentions === false) {
3637
const html = markdownToMatrixHtml(markdown, { tableMode: params.tableMode }).trimEnd();
3738
return { body, html: html || undefined };
@@ -68,12 +69,14 @@ export async function enrichMatrixFormattedContent(params: {
6869
client: MatrixClient;
6970
content: MatrixFormattedContent;
7071
markdown?: string | null;
72+
preparedBody?: string;
7173
includeMentions?: boolean;
7274
tableMode?: MarkdownTableMode;
7375
}): Promise<void> {
7476
const { body, html, mentions } = await renderMatrixFormattedContent({
7577
client: params.client,
7678
markdown: params.markdown,
79+
preparedBody: params.preparedBody,
7780
includeMentions: params.includeMentions,
7881
tableMode: params.tableMode,
7982
});

0 commit comments

Comments
 (0)