AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
perf(frontend/copilot): highlight a streaming code block incrementally (#15688)
Abhimanyu Yadav52cbe4f9cef1
diff --git a/autogpt_platform/frontend/scripts/perf/__tests__/fixtures.test.ts b/autogpt_platform/frontend/scripts/perf/__tests__/fixtures.test.tsindex 0942c2ecef..aa04a9915b 100644--- a/autogpt_platform/frontend/scripts/perf/__tests__/fixtures.test.ts+++ b/autogpt_platform/frontend/scripts/perf/__tests__/fixtures.test.ts@@ -11,6 +11,7 @@ import { FIXTURES_DIR } from "../lib/paths";import { splitFrames } from "../lib/stream-server";import {REPLY_20_LINES,+ REPLY_LONG_CODE,buildReplyStream,buildSessionRows,} from "../fixtures/generate";@@ -53,6 +54,9 @@ describe("copilot fixtures", () => {expect(fs.readFileSync(path.join(FIXTURES_DIR, "reply-20-lines.sse"), "utf8"),).toBe(buildReplyStream(REPLY_20_LINES, "perf-reply-20-lines"));+ expect(+ fs.readFileSync(path.join(FIXTURES_DIR, "reply-long-code.sse"), "utf8"),+ ).toBe(buildReplyStream(REPLY_LONG_CODE, "perf-reply-long-code"));});});diff --git a/autogpt_platform/frontend/scripts/perf/fixtures/generate.ts b/autogpt_platform/frontend/scripts/perf/fixtures/generate.tsindex ceb5f9bd82..229f29b3fd 100644--- a/autogpt_platform/frontend/scripts/perf/fixtures/generate.ts+++ b/autogpt_platform/frontend/scripts/perf/fixtures/generate.ts@@ -1,6 +1,6 @@/**- * Writes the synthetic copilot fixtures: a 200-row chat session and a- * 20-line streamed reply. Deterministic, so re-running produces the same+ * Writes the synthetic copilot fixtures: a 200-row chat session, a+ * 20-line streamed reply and a reply that is mostly one long code block. Deterministic, so re-running produces the same* files. To capture real wire data from a running stack instead, use* scripts/perf/record.ts.*@@ -185,6 +185,51 @@ export const REPLY_20_LINES = ["Let me know if you want me to set up the daily check for you.",].join("\n");+const LONG_CODE_SECTIONS = [+ "signups",+ "refunds",+ "renewals",+ "upgrades",+ "trials",+ "invites",+ "exports",+ "payouts",+ "reviews",+ "tickets",+];++function longCodeSection(name: string, index: number) {+ const title = name[0].toUpperCase() + name.slice(1);+ return [+ "/**",+ ` * Counts the ${name} events and reports the busiest day.`,+ " */",+ `export function count${title}(events: { type: string; day: string }[]) {`,+ ` const matching = events.filter((event) => event.type === "${name}");`,+ " const byDay = new Map<string, number>();",+ " for (const event of matching) {",+ " byDay.set(event.day, (byDay.get(event.day) ?? 0) + 1);",+ " }",+ ` const label = \`${name}: \${matching.length} events\`; // section ${index}`,+ " return { total: matching.length, byDay, label };",+ "}",+ "",+ ];+}++/** A reply that is mostly one long code block, so every flush lands in it. */+export const REPLY_LONG_CODE = [+ "Here is the full report script.",+ "",+ "```ts",+ 'import { readFileSync } from "fs";',+ "",+ ...LONG_CODE_SECTIONS.flatMap(longCodeSection),+ "```",+ "",+ "Run it once a day.",+].join("\n");+export function buildReplyStream(text: string, messageID: string) {const words = text.match(/\S+\s*|\s+/g) ?? [];const deltas: string[] = [];@@ -224,7 +269,14 @@ function main() {replyFile,buildReplyStream(REPLY_20_LINES, "perf-reply-20-lines"),);- console.log(`wrote ${sessionFile}\nwrote ${replyFile}`);+ const longCodeFile = path.join(FIXTURES_DIR, "reply-long-code.sse");+ fs.writeFileSync(+ longCodeFile,+ buildReplyStream(REPLY_LONG_CODE, "perf-reply-long-code"),+ );+ console.log(+ `wrote ${sessionFile}\nwrote ${replyFile}\nwrote ${longCodeFile}`,+ );}if (require.main === module) main();diff --git a/autogpt_platform/frontend/scripts/perf/fixtures/reply-long-code.sse b/autogpt_platform/frontend/scripts/perf/fixtures/reply-long-code.ssenew file mode 100644index 0000000000..18f1180f0b--- /dev/null+++ b/autogpt_platform/frontend/scripts/perf/fixtures/reply-long-code.sse@@ -0,0 +1,1372 @@+data: {"type":"start","messageId":"perf-reply-long-code"}++data: {"type":"start-step"}++data: {"type":"text-start","id":"perf-text-1"}++data: {"type":"text-delta","id":"perf-text-1","delta":"Here "}++data: {"type":"text-delta","id":"perf-text-1","delta":"is "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"full "}++data: {"type":"text-delta","id":"perf-text-1","delta":"report "}++data: {"type":"text-delta","id":"perf-text-1","delta":"script.\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"```ts\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"import "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"readFileSync "}++data: {"type":"text-delta","id":"perf-text-1","delta":"} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"from "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"fs\";\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"signups "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countSignups(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"signups\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`signups: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"refunds "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countRefunds(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"refunds\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`refunds: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"renewals "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countRenewals(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"renewals\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`renewals: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"2\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"upgrades "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countUpgrades(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"upgrades\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`upgrades: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"3\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"trials "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countTrials(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"trials\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`trials: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"4\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"invites "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countInvites(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"invites\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`invites: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"5\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"exports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countExports(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"exports\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`exports: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"6\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"payouts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countPayouts(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"payouts\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`payouts: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"7\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reviews "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countReviews(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"reviews\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`reviews: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"8\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"/**\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"* "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Counts "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"tickets "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events "}++data: {"type":"text-delta","id":"perf-text-1","delta":"and "}++data: {"type":"text-delta","id":"perf-text-1","delta":"reports "}++data: {"type":"text-delta","id":"perf-text-1","delta":"the "}++data: {"type":"text-delta","id":"perf-text-1","delta":"busiest "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day.\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"*/\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"export "}++data: {"type":"text-delta","id":"perf-text-1","delta":"function "}++data: {"type":"text-delta","id":"perf-text-1","delta":"countTickets(events: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"type: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"string "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}[]) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events.filter((event) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=> "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event.type "}++data: {"type":"text-delta","id":"perf-text-1","delta":"=== "}++data: {"type":"text-delta","id":"perf-text-1","delta":"\"tickets\");\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"new "}++data: {"type":"text-delta","id":"perf-text-1","delta":"Map<string, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"number>();\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"for "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"event "}++data: {"type":"text-delta","id":"perf-text-1","delta":"of "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay.set(event.day, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"(byDay.get(event.day) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"?? "}++data: {"type":"text-delta","id":"perf-text-1","delta":"0) "}++data: {"type":"text-delta","id":"perf-text-1","delta":"+ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"1);\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"const "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"= "}++data: {"type":"text-delta","id":"perf-text-1","delta":"`tickets: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"${matching.length} "}++data: {"type":"text-delta","id":"perf-text-1","delta":"events`; "}++data: {"type":"text-delta","id":"perf-text-1","delta":"// "}++data: {"type":"text-delta","id":"perf-text-1","delta":"section "}++data: {"type":"text-delta","id":"perf-text-1","delta":"9\n "}++data: {"type":"text-delta","id":"perf-text-1","delta":"return "}++data: {"type":"text-delta","id":"perf-text-1","delta":"{ "}++data: {"type":"text-delta","id":"perf-text-1","delta":"total: "}++data: {"type":"text-delta","id":"perf-text-1","delta":"matching.length, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"byDay, "}++data: {"type":"text-delta","id":"perf-text-1","delta":"label "}++data: {"type":"text-delta","id":"perf-text-1","delta":"};\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"}\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"```\n\n"}++data: {"type":"text-delta","id":"perf-text-1","delta":"Run "}++data: {"type":"text-delta","id":"perf-text-1","delta":"it "}++data: {"type":"text-delta","id":"perf-text-1","delta":"once "}++data: {"type":"text-delta","id":"perf-text-1","delta":"a "}++data: {"type":"text-delta","id":"perf-text-1","delta":"day."}++data: {"type":"text-end","id":"perf-text-1"}++data: {"type":"finish-step"}++data: {"type":"finish"}++data: [DONE]+diff --git a/autogpt_platform/frontend/scripts/perf/lib/copilot-page.ts b/autogpt_platform/frontend/scripts/perf/lib/copilot-page.tsindex d8812306e3..d28f3311fa 100644--- a/autogpt_platform/frontend/scripts/perf/lib/copilot-page.ts+++ b/autogpt_platform/frontend/scripts/perf/lib/copilot-page.ts@@ -15,26 +15,31 @@ const READY_TIMEOUT_MS = 60_000;interface OpenArgs {ctx: ScenarioContext;paceMs?: number;+ sessionID?: string;+ replyFixture?: string;}/**- * Serves the three fixture sessions, opens the 200-message one and waits- * until its newest message and the composer are on screen.+ * Serves the three fixture sessions, opens the 200-message one (or+ * `sessionID`) and waits until its newest message and the composer are on+ * screen.*/export async function openLongSession({ctx,paceMs = LIVE_PACE_MS,+ sessionID = SESSION_L,+ replyFixture = REPLY_FIXTURE,}: OpenArgs) {const fixture = createCopilotFixture({userID: ctx.userID,sessions: COPILOT_SESSIONS,stream: ctx.stream,- replyFixture: REPLY_FIXTURE,+ replyFixture,paceMs,});await installRoutes(ctx.page, fixture.handlers);const copilot = new CopilotPage(ctx.page);- await copilot.open(SESSION_L);+ await copilot.open(sessionID);await copilot.getChatInput().waitFor({ state: "visible", timeout: READY_TIMEOUT_MS });diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/copilot-stream-code.ts b/autogpt_platform/frontend/scripts/perf/scenarios/copilot-stream-code.tsnew file mode 100644index 0000000000..680a3afb10--- /dev/null+++ b/autogpt_platform/frontend/scripts/perf/scenarios/copilot-stream-code.ts@@ -0,0 +1,35 @@+import { SESSION_S } from "../lib/copilot-fixture";+import { openLongSession, sendMessage } from "../lib/copilot-page";+import type { Scenario } from "../lib/scenario";++const LONG_CODE_FIXTURE = "reply-long-code.sse";+const LAST_LINE = "Run it once a day.";+/** A page that highlights slowly renders the reply well after it arrived. */+const RENDERED_TIMEOUT_MS = 120_000;++export const copilotStreamCode: Scenario = {+ name: "copilot-stream-code",+ description:+ 'Send "go" in the 10-message chat and stream a reply that is one 130-line code block at ~33 chunks/s.',+ readyWhen:+ "the newest user message and the composer are visible, then network idle + 1 s",+ windowMs: 1_000,+ async setup(ctx) {+ const { fixture, copilot } = await openLongSession({+ ctx,+ sessionID: SESSION_S,+ replyFixture: LONG_CODE_FIXTURE,+ });+ return {+ async run() {+ await sendMessage(copilot, "go");+ // Wait for the last line on screen, not just the stream: the window+ // must cover rendering the whole reply on both sides of a comparison.+ await ctx.page+ .getByText(LAST_LINE, { exact: true })+ .waitFor({ state: "visible", timeout: RENDERED_TIMEOUT_MS });+ await fixture.waitForTurn(1);+ },+ };+ },+};diff --git a/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts b/autogpt_platform/frontend/scripts/perf/scenarios/registry.tsindex a8d66ad8e8..ba9a315452 100644--- a/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts+++ b/autogpt_platform/frontend/scripts/perf/scenarios/registry.ts@@ -4,6 +4,7 @@ import { builderDragRunning } from "./builder-drag-running";import { builderRunEvents } from "./builder-run-events";import { copilotBackgroundStream } from "./copilot-background-stream";import { copilotIdle } from "./copilot-idle";+import { copilotStreamCode } from "./copilot-stream-code";import { copilotStreamReply } from "./copilot-stream-reply";import { copilotSwitchThreads } from "./copilot-switch-threads";import { copilotType } from "./copilot-type";@@ -18,6 +19,7 @@ export const SCENARIOS: Scenario[] = [copilotSwitchThreads,builderDragRunning,libraryRunning,+ copilotStreamCode,builderConnectEdge,libraryRunningEvents,builderRunEvents,diff --git a/autogpt_platform/frontend/src/lib/__tests__/streamdown-code-plugin-render.test.tsx b/autogpt_platform/frontend/src/lib/__tests__/streamdown-code-plugin-render.test.tsxnew file mode 100644index 0000000000..b05a76d844--- /dev/null+++ b/autogpt_platform/frontend/src/lib/__tests__/streamdown-code-plugin-render.test.tsx@@ -0,0 +1,73 @@+import { render, waitFor } from "@testing-library/react";+import { Streamdown } from "streamdown";+import { describe, expect, it } from "vitest";++import { code } from "../streamdown-code-plugin";+import { getShikiHighlighter, SHIKI_THEMES } from "../shiki-highlighter";++const SOURCE = [+ "/**",+ " * Rendered while it streams.",+ " */",+ "export function total(rows: { amount: number }[]) {",+ " const label = `rows: ${rows.length}`;",+ " return rows.reduce((sum, row) => sum + row.amount, 0);",+ "}",+].join("\n");++function markdown(body: string) {+ return `Here is the script.\n\n\`\`\`ts\n${body}`;+}++function renderedLines(container: HTMLElement) {+ const body = container.querySelector('[data-streamdown="code-block-body"]');+ return Array.from(body?.querySelectorAll("code > span") ?? []).map((line) =>+ Array.from(line.children).map((token) => ({+ content: token.textContent,+ color: (token as HTMLElement).style.color,+ })),+ );+}++async function expectedLines(source: string) {+ const highlighter = await getShikiHighlighter();+ return highlighter+ .codeToTokens(source, {+ lang: "typescript",+ themes: { light: SHIKI_THEMES[0], dark: SHIKI_THEMES[1] },+ })+ .tokens.map((line) =>+ line.map((token) => ({+ content: token.content,+ color: token.htmlStyle?.color ?? token.color,+ })),+ );+}++describe("Streamdown with the shiki code plugin", () => {+ it("shows the fully highlighted block once a streamed code block is complete", async () => {+ const { container, rerender } = render(+ <Streamdown plugins={{ code }} isAnimating>+ {markdown("")}+ </Streamdown>,+ );++ for (let end = 1; end <= SOURCE.length; end += 7) {+ rerender(+ <Streamdown plugins={{ code }} isAnimating>+ {markdown(SOURCE.slice(0, end))}+ </Streamdown>,+ );+ }+ rerender(+ <Streamdown plugins={{ code }} isAnimating={false}>+ {`${markdown(SOURCE)}\n\`\`\`\n\nDone.`}+ </Streamdown>,+ );++ // Streamdown hands the plugin the fence body with its closing newline.+ const expected = await expectedLines(`${SOURCE}\n`);+ await waitFor(() => expect(renderedLines(container)).toEqual(expected));+ expect(expected.flat().every((token) => token.color)).toBe(true);+ });+});diff --git a/autogpt_platform/frontend/src/lib/__tests__/streamdown-code-plugin.test.ts b/autogpt_platform/frontend/src/lib/__tests__/streamdown-code-plugin.test.tsnew file mode 100644index 0000000000..da3c70ed07--- /dev/null+++ b/autogpt_platform/frontend/src/lib/__tests__/streamdown-code-plugin.test.ts@@ -0,0 +1,263 @@+import type { CodeHighlighterPlugin } from "streamdown";+import { afterEach, describe, expect, it, vi } from "vitest";++import { createSingletonCodePlugin } from "../streamdown-code-plugin";+import {+ type BundledLanguage,+ getShikiHighlighter,+ SHIKI_THEMES,+} from "../shiki-highlighter";++type HighlightResult = NonNullable<+ ReturnType<CodeHighlighterPlugin["highlight"]>+>;++const plugin = createSingletonCodePlugin();++function highlight(code: string, language: string) {+ return new Promise<HighlightResult>((resolve) => {+ const result = plugin.highlight(+ { code, language: language as BundledLanguage, themes: SHIKI_THEMES },+ resolve,+ );+ if (result) resolve(result);+ });+}++async function highlightFromScratch(code: string, lang: BundledLanguage) {+ const highlighter = await getShikiHighlighter();+ const result = highlighter.codeToTokens(code, {+ lang,+ themes: { light: SHIKI_THEMES[0], dark: SHIKI_THEMES[1] },+ });+ return {+ tokens: result.tokens.map((line) =>+ line.map((token) => ({+ content: token.content,+ color: token.color,+ htmlStyle: token.htmlStyle,+ })),+ ),+ fg: result.fg,+ bg: result.bg,+ };+}++function prefixes(code: string, count: number) {+ const step = Math.max(1, Math.ceil(code.length / count));+ const result: string[] = [];+ for (let end = step; end < code.length; end += step) {+ result.push(code.slice(0, end));+ }+ result.push(code);+ return result;+}++async function spyOnTokenizer() {+ const highlighter = await getShikiHighlighter();+ return vi.spyOn(highlighter, "codeToTokens");+}++function tokenizedChars(spy: Awaited<ReturnType<typeof spyOnTokenizer>>) {+ return spy.mock.calls.reduce((sum, [code]) => sum + code.length, 0);+}++const SAMPLES: { lang: BundledLanguage; code: string }[] = [+ {+ lang: "typescript",+ code: [+ "/**",+ " * A block comment that spans",+ " * several lines.",+ " */",+ "export function greet(name: string) {",+ " const message = `Hello, ${name}!",+ " Welcome back.`;",+ ' return message + " " + "done";',+ "}",+ "",+ "",+ "const value = 42; // trailing comment",+ ].join("\n"),+ },+ {+ lang: "python",+ code: [+ "def greet(name):",+ ' """',+ " A docstring that spans",+ " several lines.",+ ' """',+ " return f'Hello, {name}!'",+ "",+ "class Point:",+ " x: int = 0",+ ].join("\n"),+ },+ {+ lang: "html",+ code: [+ "<html>",+ " <style>",+ " body { color: red; }",+ " </style>",+ " <script>",+ " const answer = 42;",+ " </script>",+ " <!-- a comment",+ " that keeps going -->",+ "</html>",+ ].join("\n"),+ },+ {+ lang: "markdown",+ code: [+ "# Title",+ "",+ "```python",+ "print('nested')",+ "```",+ "",+ "Some *text* after.",+ ].join("\n"),+ },+ {+ lang: "json",+ code: '{\r\n "a": [1, 2, 3],\r\n "b": { "c": "d" }\r\n}\r\n',+ },+];++// Streamdown hands the plugin an open fence's body with a newline after the+// partial last line, so a still-growing line already ends in "\n".+const SHAPES = [+ { shape: "raw prefixes", wrap: (text: string) => text },+ { shape: "Streamdown fence bodies", wrap: (text: string) => `${text}\n` },+];++const CASES = SAMPLES.flatMap((sample) =>+ SHAPES.map((shape) => ({ ...sample, ...shape })),+);++afterEach(() => {+ vi.restoreAllMocks();+});++describe("streamdown code plugin while a block streams", () => {+ it.each(CASES)(+ "gives every streamed version of a $lang block ($shape) the same tokens as highlighting it from scratch",+ async ({ lang, code, wrap }) => {+ for (let end = 1; end <= code.length; end++) {+ const partial = wrap(code.slice(0, end));+ const streamed = await highlight(partial, lang);+ expect(streamed).toEqual(await highlightFromScratch(partial, lang));+ }+ },+ 60_000,+ );++ it("gives each of many in-flight versions its own tokens", async () => {+ const code = SAMPLES[0].code.replace("greet", "greetInFlight");+ const versions = prefixes(code, 30);+ const results = await Promise.all(+ versions.map((version) => highlight(version, "typescript")),+ );+ for (const [index, version] of versions.entries()) {+ expect(results[index]).toEqual(+ await highlightFromScratch(version, "typescript"),+ );+ }+ });++ it.each(SHAPES)(+ "tokenizes each line of a growing block about once, not once per flush ($shape)",+ async ({ shape, wrap }) => {+ const lines = Array.from(+ { length: 80 },+ (_, i) => `const value${i} = compute(${i}, "${shape} line ${i}");`,+ );+ const code = lines.join("\n");+ const spy = await spyOnTokenizer();++ for (const version of prefixes(code, 50)) {+ await highlight(wrap(version), "ts");+ }++ // Re-tokenizing every version from scratch would cost ~25x the block.+ expect(tokenizedChars(spy)).toBeLessThan(code.length * 2);+ expect(await highlight(wrap(code), "ts")).toEqual(+ await highlightFromScratch(wrap(code), "typescript"),+ );+ },+ );++ it.each(SHAPES)(+ "keeps a finished block's tokens cached through 600 updates of another block ($shape)",+ async ({ shape, wrap }) => {+ const closed = wrap(`print("finished before the next one: ${shape}")`);+ const closedResult = await highlight(closed, "python");++ // 60 lines of 10 words, one word per update, as the copilot streams.+ let done = "";+ for (let line = 0; line < 60; line++) {+ const words = Array.from({ length: 10 }, (_, i) => `w${line}_${i}`);+ for (let count = 1; count <= words.length; count++) {+ const partial = words.slice(0, count).join(" ");+ await highlight(wrap(`${done}${partial}`), "javascript");+ }+ done = `${done}${words.join(" ")}\n`;+ }++ const spy = await spyOnTokenizer();+ const again = plugin.highlight(+ {+ code: closed,+ language: "python" as BundledLanguage,+ themes: SHIKI_THEMES,+ },+ () => undefined,+ );+ expect(again).toBe(closedResult);+ expect(spy).not.toHaveBeenCalled();+ },+ 60_000,+ );++ it("still returns the cached tokens of an earlier version requested again", async () => {+ const first = "const shared = 1;\n";+ const second = `${first}const extended = 2;`;+ const firstResult = await highlight(first, "typescript");+ await highlight(second, "typescript");++ const again = plugin.highlight(+ {+ code: first,+ language: "typescript" as BundledLanguage,+ themes: SHIKI_THEMES,+ },+ () => undefined,+ );+ expect(again).toBe(firstResult);+ });++ it("falls back to a full highlight when resuming from a checkpoint fails", async () => {+ const code = "const resumed = 1;\nconst after = 2;\n";+ await highlight(code, "typescript");+ const highlighter = await getShikiHighlighter();+ const original = highlighter.codeToTokens.bind(highlighter);+ const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);+ vi.spyOn(highlighter, "codeToTokens").mockImplementation(+ (input, options) => {+ if ("grammarState" in options && options.grammarState) {+ throw new Error("bad grammar state");+ }+ return original(input, options);+ },+ );++ const longer = `${code}const more = 3;`;+ expect(await highlight(longer, "typescript")).toEqual(+ await highlightFromScratch(longer, "typescript"),+ );+ expect(warn).toHaveBeenCalled();+ });+});diff --git a/autogpt_platform/frontend/src/lib/streamdown-code-plugin.ts b/autogpt_platform/frontend/src/lib/streamdown-code-plugin.tsindex 52ccc5afed..00cd631f5a 100644--- a/autogpt_platform/frontend/src/lib/streamdown-code-plugin.ts+++ b/autogpt_platform/frontend/src/lib/streamdown-code-plugin.ts@@ -1,3 +1,4 @@+import type { GrammarState } from "shiki";import type { CodeHighlighterPlugin } from "streamdown";import {@@ -21,39 +22,220 @@ interface HighlightResult {}type HighlightCallback = (result: HighlightResult) => void;+type Highlighter = Awaited<ReturnType<typeof getShikiHighlighter>>;+type HighlightedLine = HighlightResult["tokens"][number];++/**+ * Where tokenizing stopped for a block that may still be growing: the tokens+ * of every line before its last one, and the grammar state after them. A+ * longer version of the same block only tokenizes what comes after `text`.+ * `lastCode` is the latest version highlighted from here.+ */+interface StreamCheckpoint {+ lang: string;+ themesKey: string;+ text: string;+ lines: HighlightedLine[];+ grammarState: GrammarState | undefined;+ lastCacheKey: string;+ lastCode: string;+}const MAX_CACHE_SIZE = 500;+const MAX_PARTIAL_CACHE_SIZE = 50;+const MAX_CHECKPOINTS = 20;const tokenCache = new Map<string, HighlightResult>();+// Versions of a block that a longer version has replaced. Kept apart so a+// streaming block cannot push other blocks' tokens out of `tokenCache`.+const partialCache = new Map<string, HighlightResult>();+const checkpoints: StreamCheckpoint[] = [];const pendingCallbacks = new Map<string, Set<HighlightCallback>>();const inFlightLanguageLoads = new Map<string, Promise<void>>();-function simpleHash(str: string): string {- let hash = 0;- for (let i = 0; i < str.length; i++) {- const char = str.charCodeAt(i);- hash = (hash << 5) - hash + char;- hash = hash & hash;- }- return hash.toString(36);-}-function getCacheKey(code: string,lang: string,themes: readonly string[],): string {- return `${lang}:${themes.join(",")}:${simpleHash(code)}`;+ return `${lang}:${themes.join(",")}:${code}`;}-function evictOldestIfNeeded(): void {- if (tokenCache.size > MAX_CACHE_SIZE) {- const oldestKey = tokenCache.keys().next().value;- if (oldestKey) {- tokenCache.delete(oldestKey);+function setBounded(+ cache: Map<string, HighlightResult>,+ key: string,+ value: HighlightResult,+ maxSize: number,+): void {+ cache.set(key, value);+ if (cache.size > maxSize) {+ const oldestKey = cache.keys().next().value;+ if (oldestKey !== undefined) {+ cache.delete(oldestKey);}}}+function getCachedResult(cacheKey: string): HighlightResult | undefined {+ return tokenCache.get(cacheKey) ?? partialCache.get(cacheKey);+}++function demoteToPartial(cacheKey: string): void {+ const result = tokenCache.get(cacheKey);+ if (!result) return;+ tokenCache.delete(cacheKey);+ setBounded(partialCache, cacheKey, result, MAX_PARTIAL_CACHE_SIZE);+}++function findCheckpoint(+ code: string,+ lang: string,+ themesKey: string,+): StreamCheckpoint | undefined {+ let best: StreamCheckpoint | undefined;+ for (const checkpoint of checkpoints) {+ if (+ checkpoint.lang === lang &&+ checkpoint.themesKey === themesKey &&+ (!best || checkpoint.text.length > best.text.length) &&+ code.startsWith(checkpoint.text)+ ) {+ best = checkpoint;+ }+ }+ return best;+}++function rememberCheckpoint(checkpoint: StreamCheckpoint): void {+ const index = checkpoints.indexOf(checkpoint);+ if (index !== -1) checkpoints.splice(index, 1);+ checkpoints.unshift(checkpoint);+ if (checkpoints.length > MAX_CHECKPOINTS) checkpoints.pop();+}++function toHighlightedLines(+ tokens: ReturnType<Highlighter["codeToTokens"]>["tokens"],+): HighlightedLine[] {+ return tokens.map((line) =>+ line.map((token) => ({+ content: token.content,+ color: token.color,+ htmlStyle: token.htmlStyle,+ })),+ );+}++interface TokenizeArgs {+ highlighter: Highlighter;+ code: string;+ lang: BundledLanguage;+ themes: readonly string[];+ cacheKey: string;+}++function tokenizeFully({+ highlighter,+ code,+ lang,+ themes,+}: TokenizeArgs): HighlightResult {+ const shikiResult = highlighter.codeToTokens(code, {+ lang,+ themes: { light: themes[0], dark: themes[1] },+ });+ return {+ tokens: toHighlightedLines(shikiResult.tokens),+ fg: shikiResult.fg,+ bg: shikiResult.bg,+ };+}++/**+ * Tokenizes `code` starting from the longest checkpoint it extends, so a+ * block that grows with every streamed chunk costs only its new lines.+ * TextMate grammars tokenize line by line from the previous line's state,+ * so the result is the same as tokenizing the whole block again.+ */+function tokenizeIncrementally({+ highlighter,+ code,+ lang,+ themes,+ cacheKey,+}: TokenizeArgs): HighlightResult {+ const themesKey = themes.join(",");+ const checkpoint = findCheckpoint(code, lang, themesKey);+ const start = checkpoint?.text.length ?? 0;+ const rest = code.slice(start);+ // The last line can still grow even when it ends in a newline: Streamdown+ // passes an open fence's body with a newline after the partial line.+ const lastNewline = rest.replace(/\n$/, "").lastIndexOf("\n");+ const options = {+ lang,+ themes: { light: themes[0], dark: themes[1] },+ };++ let lines = checkpoint?.lines ?? [];+ let grammarState = checkpoint?.grammarState;++ if (lastNewline !== -1) {+ // Shiki splits lines on /\r?\n/, so a CRLF line must not keep its \r.+ const completeLines = rest.slice(0, lastNewline).replace(/\r$/, "");+ const complete = highlighter.codeToTokens(completeLines, {+ ...options,+ grammarState,+ });+ lines = lines.concat(toHighlightedLines(complete.tokens));+ grammarState = complete.grammarState;+ }++ const tail = highlighter.codeToTokens(rest.slice(lastNewline + 1), {+ ...options,+ grammarState,+ });++ if (+ checkpoint &&+ checkpoint.lastCode !== code &&+ code.startsWith(checkpoint.lastCode.replace(/\n$/, ""))+ ) {+ demoteToPartial(checkpoint.lastCacheKey);+ }++ const next: StreamCheckpoint = checkpoint ?? {+ lang,+ themesKey,+ text: "",+ lines: [],+ grammarState: undefined,+ lastCacheKey: cacheKey,+ lastCode: code,+ };+ if (lastNewline !== -1 && grammarState) {+ next.text = code.slice(0, start + lastNewline + 1);+ next.lines = lines;+ next.grammarState = grammarState;+ }+ next.lastCacheKey = cacheKey;+ next.lastCode = code;+ rememberCheckpoint(next);++ return {+ tokens: lines.concat(toHighlightedLines(tail.tokens)),+ fg: tail.fg,+ bg: tail.bg,+ };+}++function tokenize(args: TokenizeArgs): HighlightResult {+ try {+ return tokenizeIncrementally(args);+ } catch (error) {+ checkpoints.length = 0;+ console.warn("[Shiki] Incremental highlight failed, retrying:", error);+ return tokenizeFully(args);+ }+}+export function createSingletonCodePlugin(): CodeHighlighterPlugin {return {name: "shiki",@@ -75,8 +257,9 @@ export function createSingletonCodePlugin(): CodeHighlighterPlugin {const lang = resolveLanguage(language);const cacheKey = getCacheKey(code, lang, themes);- if (tokenCache.has(cacheKey)) {- return tokenCache.get(cacheKey)!;+ const cached = getCachedResult(cacheKey);+ if (cached) {+ return cached;}if (callback) {@@ -107,25 +290,15 @@ export function createSingletonCodePlugin(): CodeHighlighterPlugin {highlighter.getLoadedLanguages().includes(lang) ? lang : "text") as BundledLanguage;- const shikiResult = highlighter.codeToTokens(code, {+ const result = tokenize({+ highlighter,+ code,lang: finalLang,- themes: { light: themes[0], dark: themes[1] },+ themes,+ cacheKey,});- const result: HighlightResult = {- tokens: shikiResult.tokens.map((line) =>- line.map((token) => ({- content: token.content,- color: token.color,- htmlStyle: token.htmlStyle,- })),- ),- fg: shikiResult.fg,- bg: shikiResult.bg,- };-- evictOldestIfNeeded();- tokenCache.set(cacheKey, result);+ setBounded(tokenCache, cacheKey, result, MAX_CACHE_SIZE);const callbacks = pendingCallbacks.get(cacheKey);if (callbacks) {diff --git a/docs/engineering/frontend-perf-baseline.md b/docs/engineering/frontend-perf-baseline.mdindex ccbfdfbdb0..57331664a3 100644--- a/docs/engineering/frontend-perf-baseline.md+++ b/docs/engineering/frontend-perf-baseline.md@@ -485,3 +485,51 @@ Added with SECRT-3003: measured on `dev` app code at `a1578ad9a` on a similar 16| `TooltipContent props:children` | 1,344 || `Primitive.button.Slot props:children,onPointerMove,onPointerLeave,onPointerDown,+4` | 1,217 || `Primitive.button.SlotClone props:onPointerMove,onPointerLeave,onPointerDown,onFocus,+4` | 1,217 |++## copilot-stream-code · baseline — median of 5 runs++Added after the table above was saved: `dev` at `a1578ad9a`, same machine type, 1 warm-up and 5 saved runs. The reply is one 130-line code block, so most of the time is the transcript re-rendering on every streamed word, plus Shiki highlighting the open block on each flush.++| metric | median | range | spread |+| --- | --- | --- | --- |+| commits | 2,573 | 2,565 – 2,588 | 0.9% |+| total renders | 579,560 | 576,640 – 580,000 | 0.6% |+| renders per commit | 224.5 | 224.1 – 226.1 | 0.9% |+| max renders in one commit | 1,010 | 1,010 – 1,010 | 0.0% |+| script time (ms) | 46,940 | 46,115 – 50,794 | 10.0% |+| task time (ms) | 55,268 | 54,101 – 58,986 | 8.8% |+| layout time (ms) | 461 | 428 – 486 | 12.8% |+| style recalc time (ms) | 1,616 | 1,523 – 1,690 | 10.3% |+| long tasks (≥ 50 ms) | 377 | 350 – 407 | 15.1% |+| long task total (ms) | 33,039 | 31,008 – 37,272 | 19.0% |+| heap used after GC (MB) | 70.8 | 70.4 – 73.5 | 4.3% |++**Top components (median renders)**++| name | renders |+| --- | --- |+| `Tooltip` | 35,872 |+| `TooltipContent` | 35,871 |+| `Presence` | 24,642 |+| `Popper` | 20,406 |+| `PopperAnchor` | 20,406 |+| `PopperProvider` | 20,406 |+| `Primitive.button` | 20,406 |+| `Primitive.button.Slot` | 20,406 |+| `Primitive.button.SlotClone` | 20,406 |+| `Primitive.div` | 20,406 |++**Top render causes (median renders)**++| name | renders |+| --- | --- |+| `Tooltip props:children` | 34,456 |+| `Presence props:children` | 24,631 |+| `Primitive.div props:children` | 20,397 |+| `Primitive.div.Slot props:children` | 20,397 |+| `Primitive.div.SlotClone props:children` | 20,397 |+| `TooltipProvider props:children` | 20,388 |+| `HugeiconsIcon parent-same-props<-Icon` | 19,744 |+| `PopperAnchor props:children` | 19,676 |+| `Popper props:children` | 19,667 |+| `PopperProvider props:children` | 19,667 |diff --git a/docs/engineering/frontend-perf-measurement.md b/docs/engineering/frontend-perf-measurement.mdindex 488ebecf9d..0a6a00ff5c 100644--- a/docs/engineering/frontend-perf-measurement.md+++ b/docs/engineering/frontend-perf-measurement.md@@ -108,6 +108,7 @@ Names come from the component function. Third-party code bundled already minifie| `copilot-switch-threads` | Open the 200-message chat and list the 200 / 50 / 10-message chats in the sidebar | Click 50 → 10 → 200, three rounds, 2 s on each | Until done || `builder-drag-running` | Open a 50-node workflow whose run is RUNNING (the page polls it every second) | Drag one node for 2 s with 60 pointer moves | 2 s || `library-running` | Open the library with 20 workflows, all with a RUNNING run | Nothing | 30 s |+| `copilot-stream-code` | Open the 10-message chat | Type "go", press Enter, stream a reply that is one 130-line code block (`fixtures/reply-long-code.sse`) at about 33 chunks a second | Until the last line of the reply is on screen and the page has refetched the session, + 1 s || `builder-connect-edge` | Open the same 50-node workflow with no run (nothing polls) | Drag one new edge from StoreValue node 4's `output` to Calculator node 5's `a` input, 20 pointer moves over 0.6 s | 2 s after the drop || `library-running-events` | Open the same library at `/library?sort=updatedAt`, with the websocket mocked | Push a `graph_execution_event` for one of the 20 runs every 250 ms, 80 in all; print the `subscribe_graph_executions` frames and `GET /executions` requests seen | 20 s of events + 2 s || `builder-run-events` | Open the same 50-node workflow with its RUNNING run; the harness answers the websocket subscribe for that run itself | 40 node execution events over the websocket, one every 50 ms: the 10 unfinished nodes go RUNNING then COMPLETED, each event sent twice. The execution GET returns what has been sent so far, as the backend would | 2 s after the last event |@@ -155,6 +156,7 @@ The copilot scenarios do not need an LLM:- `fixtures/session-200.json`: 200 message rows (text, code blocks, tables, lists and tool calls). The 50- and 10-message chats are its first 50 and 10 rows.- `fixtures/reply-20-lines.sse`: one streamed reply in the backend's SSE format, one word per `text-delta`.+- `fixtures/reply-long-code.sse`: a reply that is mostly one 130-line TypeScript block, streamed the same way, so nearly every chunk lands inside an open code fence.- `lib/stream-server.ts`: a small local HTTP server that replays the `.sse` file one frame every N ms. The stream request is redirected there with `route.continue({ url })`, so the reply arrives at a steady, repeatable pace. The copilot's own word-by-word smoothing (`copilotStreamSmoothing.ts`) then runs on top, as it does in production.- `lib/copilot-fixture.ts` keeps state: once a streamed turn ends, the session GET returns the new user and assistant rows, as the backend would. Without that, the page's refetch after each turn would wipe the reply it just streamed.