Skip to content

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): lazy-load mermaid and shiki, profile 1 in 10 sessions (#15712)

Abhimanyu Yadav82e740260a04
diff --git a/autogpt_platform/frontend/instrumentation-client.ts b/autogpt_platform/frontend/instrumentation-client.ts
index 74ab8aabae..0db1aeeef7 100644
--- a/autogpt_platform/frontend/instrumentation-client.ts
+++ b/autogpt_platform/frontend/instrumentation-client.ts
@@ -92,12 +92,12 @@ Sentry.init({
// Setting this option to true will print useful information to the console while you're setting up Sentry.
debug: false,
- // Set profilesSampleRate to 1.0 to profile every transaction.
- // Since profilesSampleRate is relative to tracesSampleRate,
- // the final profiling rate can be computed as tracesSampleRate * profilesSampleRate
- // For example, a tracesSampleRate of 0.5 and profilesSampleRate of 0.5 would
- // result in 25% of transactions being profiled (0.5*0.5=0.25)
- profilesSampleRate: 1.0,
+ // Profiling samples the main thread every 10 ms for as long as a transaction
+ // lasts (up to 30 s a page load), so profiling every transaction slowed every
+ // session down. It is relative to tracesSampleRate, so with every transaction
+ // traced, 1 in 10 is profiled. Traces stay at 100%: the backend's
+ // traces_sampler follows the browser's decision for the requests it receives.
+ profilesSampleRate: 0.1,
enableLogs: true,
});
diff --git a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts
index e3a54285ed..bcbbfe9033 100644
--- a/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts
+++ b/autogpt_platform/frontend/src/__tests__/instrumentation-client.test.ts
@@ -140,6 +140,19 @@ describe("Sentry client instrumentation", () => {
expect(beforeSend(ourWarning, {})).toBe(ourWarning);
});
+ it("traces every transaction but profiles one in ten", async () => {
+ vi.resetModules();
+ initMock.mockClear();
+ await import("../../instrumentation-client");
+
+ const { tracesSampleRate, profilesSampleRate, replaysOnErrorSampleRate } =
+ initMock.mock.calls[0][0];
+
+ expect(tracesSampleRate).toBe(1);
+ expect(profilesSampleRate).toBe(0.1);
+ expect(replaysOnErrorSampleRate).toBe(1);
+ });
+
it("tags events with the environment the deployment names for itself", async () => {
vi.resetModules();
initMock.mockClear();
diff --git a/autogpt_platform/frontend/src/components/ai-elements/message.tsx b/autogpt_platform/frontend/src/components/ai-elements/message.tsx
index 88957d904b..e350611759 100644
--- a/autogpt_platform/frontend/src/components/ai-elements/message.tsx
+++ b/autogpt_platform/frontend/src/components/ai-elements/message.tsx
@@ -15,7 +15,7 @@ import { cjk } from "@streamdown/cjk";
import { code } from "@/lib/streamdown-code-plugin";
import { createMathPlugin, type MathPlugin } from "@streamdown/math";
import { escapeCurrencyAmounts } from "@/lib/markdown-math";
-import { mermaid } from "@streamdown/mermaid";
+import { mermaid } from "@/lib/streamdown-mermaid-plugin";
import rehypeKatex from "rehype-katex";
import type { UIMessage } from "ai";
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
diff --git a/autogpt_platform/frontend/src/lib/__tests__/shiki-highlighter-lazy.test.ts b/autogpt_platform/frontend/src/lib/__tests__/shiki-highlighter-lazy.test.ts
new file mode 100644
index 0000000000..239809ce97
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/__tests__/shiki-highlighter-lazy.test.ts
@@ -0,0 +1,64 @@
+import { describe, expect, it, vi } from "vitest";
+
+const shikiLib = vi.hoisted(() => ({ loads: 0, failNextCreate: false }));
+
+vi.mock("shiki", async (importOriginal) => {
+ shikiLib.loads += 1;
+ const shiki = await importOriginal<typeof import("shiki")>();
+ return {
+ ...shiki,
+ createHighlighter: ((...args) => {
+ if (shikiLib.failNextCreate) {
+ shikiLib.failNextCreate = false;
+ return Promise.reject(new Error("grammar failed to load"));
+ }
+ return shiki.createHighlighter(...args);
+ }) as typeof shiki.createHighlighter,
+ };
+});
+
+describe("shiki highlighter loading", () => {
+ it("answers language questions without loading the highlighter", async () => {
+ const { getSupportedLanguages, isLanguageSupported, resolveLanguage } =
+ await import("../shiki-highlighter");
+
+ expect(isLanguageSupported("py")).toBe(true);
+ expect(isLanguageSupported("not-a-language")).toBe(false);
+ expect(resolveLanguage("ts")).toBe("typescript");
+ expect(getSupportedLanguages()).toContain("python");
+ expect(shikiLib.loads).toBe(0);
+ });
+
+ it("tries again after a failed load instead of keeping the failure", async () => {
+ const { getShikiHighlighter } = await import("../shiki-highlighter");
+
+ shikiLib.failNextCreate = true;
+ await expect(getShikiHighlighter()).rejects.toThrow("grammar failed");
+
+ await expect(getShikiHighlighter()).resolves.toBeDefined();
+ });
+
+ it("loads the highlighter once, on first use", async () => {
+ const { getShikiHighlighter, SHIKI_THEMES } = await import(
+ "../shiki-highlighter"
+ );
+
+ const [highlighter, again] = await Promise.all([
+ getShikiHighlighter(),
+ getShikiHighlighter(),
+ ]);
+
+ expect(again).toBe(highlighter);
+ expect(shikiLib.loads).toBe(1);
+ const { tokens } = highlighter.codeToTokens("const answer = 42;", {
+ lang: "typescript",
+ theme: SHIKI_THEMES[0],
+ });
+ expect(tokens[0].map((token) => token.content).join("")).toBe(
+ "const answer = 42;",
+ );
+ expect(new Set(tokens[0].map((token) => token.color)).size).toBeGreaterThan(
+ 1,
+ );
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/__tests__/streamdown-mermaid-plugin-retry.test.ts b/autogpt_platform/frontend/src/lib/__tests__/streamdown-mermaid-plugin-retry.test.ts
new file mode 100644
index 0000000000..e03cc60f81
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/__tests__/streamdown-mermaid-plugin-retry.test.ts
@@ -0,0 +1,44 @@
+import { describe, expect, it, vi } from "vitest";
+
+const mermaidPlugin = vi.hoisted(() => ({
+ attempts: 0,
+ getMermaid: vi.fn(),
+}));
+
+// The first fetch of the chunk fails, as it would offline or mid-deploy.
+vi.mock("@streamdown/mermaid", () => {
+ mermaidPlugin.attempts += 1;
+ if (mermaidPlugin.attempts === 1) throw new Error("chunk failed to load");
+ return {
+ mermaid: {
+ name: "mermaid",
+ type: "diagram",
+ language: "mermaid",
+ getMermaid(config?: unknown) {
+ mermaidPlugin.getMermaid(config);
+ return {
+ initialize: vi.fn(),
+ render: async () => ({ svg: "<svg></svg>" }),
+ };
+ },
+ },
+ };
+});
+
+describe("lazy mermaid plugin after a failed load", () => {
+ it("fails that diagram and loads mermaid again for the next one", async () => {
+ const { mermaid } = await import("../streamdown-mermaid-plugin");
+
+ await expect(
+ mermaid.getMermaid().render("first", "graph TD\n A --> B"),
+ ).rejects.toThrow();
+
+ const instance = mermaid.getMermaid();
+ instance.initialize({ theme: "forest" });
+ await expect(
+ instance.render("second", "graph TD\n A --> B"),
+ ).resolves.toEqual({ svg: "<svg></svg>" });
+ expect(mermaidPlugin.attempts).toBe(2);
+ expect(mermaidPlugin.getMermaid).toHaveBeenCalledWith({ theme: "forest" });
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/__tests__/streamdown-mermaid-plugin.test.tsx b/autogpt_platform/frontend/src/lib/__tests__/streamdown-mermaid-plugin.test.tsx
new file mode 100644
index 0000000000..d932e0bc19
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/__tests__/streamdown-mermaid-plugin.test.tsx
@@ -0,0 +1,111 @@
+import { cleanup, render, waitFor } from "@testing-library/react";
+import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
+
+const mermaidPlugin = vi.hoisted(() => ({
+ loads: 0,
+ getMermaid: vi.fn(),
+ render: vi.fn(async (_id: string, _source: string) => ({
+ svg: '<svg data-testid="diagram"><text>A to B</text></svg>',
+ })),
+}));
+
+// @streamdown/mermaid imports the whole mermaid library at the top of its
+// module, so loading it is loading mermaid.
+vi.mock("@streamdown/mermaid", () => {
+ mermaidPlugin.loads += 1;
+ return {
+ mermaid: {
+ name: "mermaid",
+ type: "diagram",
+ language: "mermaid",
+ getMermaid(config?: unknown) {
+ mermaidPlugin.getMermaid(config);
+ return { initialize: vi.fn(), render: mermaidPlugin.render };
+ },
+ },
+ };
+});
+
+// Streamdown draws a diagram once its block scrolls into view.
+class VisibleIntersectionObserver {
+ constructor(
+ private readonly callback: (
+ entries: { isIntersecting: boolean }[],
+ observer: VisibleIntersectionObserver,
+ ) => void,
+ ) {}
+ observe() {
+ this.callback([{ isIntersecting: true }], this);
+ }
+ unobserve() {}
+ disconnect() {}
+ takeRecords() {
+ return [];
+ }
+}
+
+const DIAGRAM_REPLY = [
+ "Here is the flow.",
+ "",
+ "```mermaid",
+ "graph TD",
+ " A --> B",
+ "```",
+].join("\n");
+
+describe("Streamdown's mermaid plugin in chat replies", () => {
+ beforeAll(() => {
+ vi.stubGlobal("IntersectionObserver", VisibleIntersectionObserver);
+ });
+
+ afterEach(() => {
+ cleanup();
+ });
+
+ it("does not load mermaid for a reply without a diagram", async () => {
+ const { MessageResponse } = await import(
+ "@/components/ai-elements/message"
+ );
+ const { container } = render(
+ <MessageResponse>{"Just **prose** here."}</MessageResponse>,
+ );
+
+ await waitFor(() => expect(container.textContent).toContain("prose"));
+ expect(mermaidPlugin.loads).toBe(0);
+ });
+
+ it("loads mermaid and draws the diagram once a reply holds one", async () => {
+ const { MessageResponse } = await import(
+ "@/components/ai-elements/message"
+ );
+ const { findByTestId } = render(
+ <MessageResponse>{DIAGRAM_REPLY}</MessageResponse>,
+ );
+
+ expect(await findByTestId("diagram", {}, { timeout: 5000 })).toBeTruthy();
+ expect(mermaidPlugin.loads).toBe(1);
+ expect(mermaidPlugin.render).toHaveBeenCalledWith(
+ expect.any(String),
+ expect.stringContaining("graph TD\n A --> B"),
+ );
+ });
+
+ it("loads mermaid only once and passes Streamdown's config through", async () => {
+ const { mermaid } = await import("../streamdown-mermaid-plugin");
+ const config = { theme: "dark" as const };
+
+ const [first, second] = await Promise.all([
+ mermaid.getMermaid(config).render("first", "graph TD\n A --> B"),
+ mermaid.getMermaid().render("second", "graph LR\n C --> D"),
+ ]);
+
+ expect(mermaidPlugin.loads).toBe(1);
+ expect(mermaidPlugin.getMermaid).toHaveBeenCalledWith(config);
+ expect(mermaidPlugin.render).toHaveBeenCalledWith(
+ "second",
+ "graph LR\n C --> D",
+ );
+ expect(first.svg).toContain("diagram");
+ expect(second.svg).toContain("diagram");
+ });
+});
diff --git a/autogpt_platform/frontend/src/lib/shiki-highlighter.ts b/autogpt_platform/frontend/src/lib/shiki-highlighter.ts
index f6bbcb9b94..6b5e9b1c35 100644
--- a/autogpt_platform/frontend/src/lib/shiki-highlighter.ts
+++ b/autogpt_platform/frontend/src/lib/shiki-highlighter.ts
@@ -1,11 +1,5 @@
-import {
- bundledLanguages,
- bundledLanguagesInfo,
- createHighlighter,
- type BundledLanguage,
- type BundledTheme,
- type HighlighterGeneric,
-} from "shiki";
+import type { BundledLanguage, BundledTheme, HighlighterGeneric } from "shiki";
+import { bundledLanguages, bundledLanguagesInfo } from "shiki/langs";
export type { BundledLanguage, BundledTheme };
@@ -45,13 +39,19 @@ export function getShikiHighlighter(): Promise<
HighlighterGeneric<BundledLanguage, BundledTheme>
> {
if (!highlighterPromise) {
- highlighterPromise = createHighlighter({
- themes: SHIKI_THEMES,
- langs: PRELOAD_LANGUAGES,
- }).catch((err) => {
- highlighterPromise = null;
- throw err;
- });
+ // The highlighter's engine stays out of the first load: it is fetched with
+ // the first code block. The language table above only holds lazy imports.
+ highlighterPromise = import("shiki")
+ .then(({ createHighlighter }) =>
+ createHighlighter({
+ themes: SHIKI_THEMES,
+ langs: PRELOAD_LANGUAGES,
+ }),
+ )
+ .catch((err) => {
+ highlighterPromise = null;
+ throw err;
+ });
}
return highlighterPromise;
}
diff --git a/autogpt_platform/frontend/src/lib/streamdown-mermaid-plugin.ts b/autogpt_platform/frontend/src/lib/streamdown-mermaid-plugin.ts
new file mode 100644
index 0000000000..1a90b0da6c
--- /dev/null
+++ b/autogpt_platform/frontend/src/lib/streamdown-mermaid-plugin.ts
@@ -0,0 +1,39 @@
+import type { DiagramPlugin } from "streamdown";
+
+type MermaidConfig = Parameters<DiagramPlugin["getMermaid"]>[0];
+
+let pluginPromise: Promise<DiagramPlugin> | null = null;
+
+// @streamdown/mermaid imports all of mermaid at the top of its module, which put
+// the library in every chat's first load. It is fetched the first time a reply
+// actually draws a diagram, and Streamdown only ever awaits `render`.
+function loadMermaidPlugin(): Promise<DiagramPlugin> {
+ if (!pluginPromise) {
+ pluginPromise = import("@streamdown/mermaid").then(
+ (module) => module.mermaid,
+ (error: unknown) => {
+ pluginPromise = null;
+ throw error;
+ },
+ );
+ }
+ return pluginPromise;
+}
+
+export const mermaid: DiagramPlugin = {
+ name: "mermaid",
+ type: "diagram",
+ language: "mermaid",
+ getMermaid(config?: MermaidConfig) {
+ let pendingConfig = config;
+ return {
+ initialize(nextConfig) {
+ pendingConfig = nextConfig;
+ },
+ async render(id, source) {
+ const plugin = await loadMermaidPlugin();
+ return plugin.getMermaid(pendingConfig).render(id, source);
+ },
+ };
+ },
+};
diff --git a/autogpt_platform/frontend/src/playwright/copilot-markdown-happy-path.spec.ts b/autogpt_platform/frontend/src/playwright/copilot-markdown-happy-path.spec.ts
new file mode 100644
index 0000000000..2cc0a9fc15
--- /dev/null
+++ b/autogpt_platform/frontend/src/playwright/copilot-markdown-happy-path.spec.ts
@@ -0,0 +1,96 @@
+import { expect, test } from "./coverage-fixture";
+import { E2E_AUTH_STATES } from "./credentials/accounts";
+import { CopilotPage } from "./pages/copilot.page";
+
+test.use({ storageState: E2E_AUTH_STATES.marketplace });
+
+const DIAGRAM_REPLY = [
+ "Here is how the steps connect:",
+ "",
+ "```mermaid",
+ "graph TD",
+ " Fetch[Fetch rows] --> Clean[Clean rows]",
+ "```",
+].join("\n");
+
+const FORMULA_REPLY = "The total is $\\sum_{i=1}^{n} x_i$ for every row.";
+
+function historyRow(
+ sessionId: string,
+ sequence: number,
+ role: "user" | "assistant",
+ content: string,
+) {
+ return {
+ id: `markdown-e2e-${sequence}`,
+ session_id: sessionId,
+ role,
+ content,
+ tool_calls: null,
+ tool_call_id: null,
+ sequence,
+ duration_ms: role === "assistant" ? 1200 : null,
+ created_at: new Date(Date.UTC(2026, 0, 5, 9, 0, sequence)).toISOString(),
+ metadata: {},
+ };
+}
+
+test("copilot happy path: a reply's diagram and formula render after their plugins load", async ({
+ page,
+}) => {
+ test.setTimeout(120000);
+
+ const copilotPage = new CopilotPage(page);
+ await copilotPage.open();
+ const sessionId = await copilotPage.createSessionViaApi();
+
+ // The chat's history comes from the real session, with two finished turns
+ // added: one reply holds a mermaid diagram, the other a formula.
+ await page.route(
+ (url) => url.pathname === `/api/proxy/api/chat/sessions/${sessionId}`,
+ async (route) => {
+ if (route.request().method() !== "GET") return route.fallback();
+ const response = await route.fetch();
+ const session = await response.json();
+ const messages = [
+ historyRow(sessionId, 0, "user", "Draw the cleanup steps"),
+ historyRow(sessionId, 1, "assistant", DIAGRAM_REPLY),
+ historyRow(sessionId, 2, "user", "And the total?"),
+ historyRow(sessionId, 3, "assistant", FORMULA_REPLY),
+ ];
+ await route.fulfill({
+ response,
+ json: {
+ ...session,
+ messages,
+ oldest_sequence: 0,
+ has_more_messages: false,
+ },
+ });
+ },
+ );
+
+ await copilotPage.open(sessionId);
+ await copilotPage.waitForChatInput();
+
+ const diagram = page
+ .locator('[data-streamdown="mermaid"] svg[id^="mermaid-"]')
+ .first();
+ await diagram.scrollIntoViewIfNeeded({ timeout: 30000 });
+ await expect(
+ diagram,
+ "the mermaid block must be drawn as an SVG diagram",
+ ).toBeVisible({ timeout: 30000 });
+ await expect(diagram).toContainText("Fetch rows");
+ await expect(diagram).toContainText("Clean rows");
+
+ const formula = page.locator(".katex").first();
+ await formula.scrollIntoViewIfNeeded({ timeout: 15000 });
+ await expect(
+ formula,
+ "inline $…$ math must render through KaTeX",
+ ).toBeVisible({ timeout: 15000 });
+ await expect(
+ page.locator('.katex annotation[encoding="application/x-tex"]').first(),
+ ).toHaveText("\\sum_{i=1}^{n} x_i");
+});