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.
fix(frontend): use singleton Shiki highlighter for code syntax highlighting (#12144)
Bently905373a712b7
diff --git a/autogpt_platform/frontend/package.json b/autogpt_platform/frontend/package.jsonindex 5988e59c90..80fb46beff 100644--- a/autogpt_platform/frontend/package.json+++ b/autogpt_platform/frontend/package.json@@ -62,7 +62,6 @@"@rjsf/validator-ajv8": "6.1.2","@sentry/nextjs": "10.27.0","@streamdown/cjk": "1.0.1",- "@streamdown/code": "1.0.1","@streamdown/math": "1.0.1","@streamdown/mermaid": "1.0.1","@supabase/ssr": "0.7.0",@@ -116,6 +115,7 @@"remark-gfm": "4.0.1","remark-math": "6.0.0","shepherd.js": "14.5.1",+ "shiki": "^3.21.0","sonner": "2.0.7","streamdown": "2.1.0","tailwind-merge": "2.6.0",diff --git a/autogpt_platform/frontend/pnpm-lock.yaml b/autogpt_platform/frontend/pnpm-lock.yamlindex 468e2f312d..4d76cd0cb9 100644--- a/autogpt_platform/frontend/pnpm-lock.yaml+++ b/autogpt_platform/frontend/pnpm-lock.yaml@@ -108,9 +108,6 @@ importers:'@streamdown/cjk':specifier: 1.0.1version: 1.0.1(@types/[email protected])([email protected])([email protected])([email protected])([email protected])- '@streamdown/code':- specifier: 1.0.1- version: 1.0.1([email protected])'@streamdown/math':specifier: 1.0.1version: 1.0.1([email protected])@@ -270,6 +267,9 @@ importers:shepherd.js:specifier: 14.5.1version: 14.5.1+ shiki:+ specifier: ^3.21.0+ version: 3.21.0sonner:specifier: 2.0.7@@ -3307,11 +3307,6 @@ packages:peerDependencies:react: ^18.0.0 || ^19.0.0- '@streamdown/[email protected]':- resolution: {integrity: sha512-U9LITfQ28tZYAoY922jdtw1ryg4kgRBdURopqK9hph7G2fBUwPeHthjH7SvaV0fvFv7EqjqCzARJuWUljLe9Ag==}- peerDependencies:- react: ^18.0.0 || ^19.0.0-'@streamdown/[email protected]':resolution: {integrity: sha512-R9WdHbpERiRU7WeO7oT1aIbnLJ/jraDr89F7X9x2OM//Y8G8UMATRnLD/RUwg4VLr8Nu7QSIJ0Pa8lXd2meM4Q==}peerDependencies:@@ -11907,11 +11902,6 @@ snapshots:- micromark-util-types- unified- '@streamdown/[email protected]([email protected])':- dependencies:- react: 18.3.1- shiki: 3.21.0-'@streamdown/[email protected]([email protected])':dependencies:katex: 0.16.28diff --git a/autogpt_platform/frontend/src/components/ai-elements/message.tsx b/autogpt_platform/frontend/src/components/ai-elements/message.tsxindex 5cc330e57c..cc11c2f01b 100644--- a/autogpt_platform/frontend/src/components/ai-elements/message.tsx+++ b/autogpt_platform/frontend/src/components/ai-elements/message.tsx@@ -10,7 +10,7 @@ import {} from "@/components/ui/tooltip";import { cn } from "@/lib/utils";import { cjk } from "@streamdown/cjk";-import { code } from "@streamdown/code";+import { code } from "@/lib/streamdown-code-plugin";import { math } from "@streamdown/math";import { mermaid } from "@streamdown/mermaid";import type { UIMessage } from "ai";diff --git a/autogpt_platform/frontend/src/lib/shiki-highlighter.ts b/autogpt_platform/frontend/src/lib/shiki-highlighter.tsnew file mode 100644index 0000000000..f6bbcb9b94--- /dev/null+++ b/autogpt_platform/frontend/src/lib/shiki-highlighter.ts@@ -0,0 +1,70 @@+import {+ bundledLanguages,+ bundledLanguagesInfo,+ createHighlighter,+ type BundledLanguage,+ type BundledTheme,+ type HighlighterGeneric,+} from "shiki";++export type { BundledLanguage, BundledTheme };++const LANGUAGE_ALIASES: Record<string, string> = Object.fromEntries(+ bundledLanguagesInfo.flatMap((lang) =>+ (lang.aliases ?? []).map((alias) => [alias, lang.id]),+ ),+);++const SUPPORTED_LANGUAGES = new Set(Object.keys(bundledLanguages));++const PRELOAD_LANGUAGES: BundledLanguage[] = [+ "javascript",+ "typescript",+ "python",+ "json",+ "bash",+ "yaml",+ "markdown",+ "html",+ "css",+ "sql",+ "tsx",+ "jsx",+];++export const SHIKI_THEMES: [BundledTheme, BundledTheme] = [+ "github-light",+ "github-dark",+];++let highlighterPromise: Promise<+ HighlighterGeneric<BundledLanguage, BundledTheme>+> | null = null;++export function getShikiHighlighter(): Promise<+ HighlighterGeneric<BundledLanguage, BundledTheme>+> {+ if (!highlighterPromise) {+ highlighterPromise = createHighlighter({+ themes: SHIKI_THEMES,+ langs: PRELOAD_LANGUAGES,+ }).catch((err) => {+ highlighterPromise = null;+ throw err;+ });+ }+ return highlighterPromise;+}++export function resolveLanguage(lang: string): string {+ const normalized = lang.trim().toLowerCase();+ return LANGUAGE_ALIASES[normalized] ?? normalized;+}++export function isLanguageSupported(lang: string): boolean {+ return SUPPORTED_LANGUAGES.has(resolveLanguage(lang));+}++export function getSupportedLanguages(): BundledLanguage[] {+ return Array.from(SUPPORTED_LANGUAGES) as BundledLanguage[];+}diff --git a/autogpt_platform/frontend/src/lib/streamdown-code-plugin.ts b/autogpt_platform/frontend/src/lib/streamdown-code-plugin.tsnew file mode 100644index 0000000000..52ccc5afed--- /dev/null+++ b/autogpt_platform/frontend/src/lib/streamdown-code-plugin.ts@@ -0,0 +1,159 @@+import type { CodeHighlighterPlugin } from "streamdown";++import {+ type BundledLanguage,+ type BundledTheme,+ getShikiHighlighter,+ getSupportedLanguages,+ isLanguageSupported,+ resolveLanguage,+ SHIKI_THEMES,+} from "./shiki-highlighter";++interface HighlightResult {+ tokens: {+ content: string;+ color?: string;+ htmlStyle?: Record<string, string>;+ }[][];+ fg?: string;+ bg?: string;+}++type HighlightCallback = (result: HighlightResult) => void;++const MAX_CACHE_SIZE = 500;+const tokenCache = new Map<string, HighlightResult>();+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)}`;+}++function evictOldestIfNeeded(): void {+ if (tokenCache.size > MAX_CACHE_SIZE) {+ const oldestKey = tokenCache.keys().next().value;+ if (oldestKey) {+ tokenCache.delete(oldestKey);+ }+ }+}++export function createSingletonCodePlugin(): CodeHighlighterPlugin {+ return {+ name: "shiki",+ type: "code-highlighter",++ supportsLanguage(lang: BundledLanguage): boolean {+ return isLanguageSupported(lang);+ },++ getSupportedLanguages(): BundledLanguage[] {+ return getSupportedLanguages();+ },++ getThemes(): [BundledTheme, BundledTheme] {+ return SHIKI_THEMES;+ },++ highlight({ code, language, themes }, callback) {+ const lang = resolveLanguage(language);+ const cacheKey = getCacheKey(code, lang, themes);++ if (tokenCache.has(cacheKey)) {+ return tokenCache.get(cacheKey)!;+ }++ if (callback) {+ if (!pendingCallbacks.has(cacheKey)) {+ pendingCallbacks.set(cacheKey, new Set());+ }+ pendingCallbacks.get(cacheKey)!.add(callback);+ }++ getShikiHighlighter()+ .then(async (highlighter) => {+ const loadedLanguages = highlighter.getLoadedLanguages();++ if (!loadedLanguages.includes(lang) && isLanguageSupported(lang)) {+ let loadPromise = inFlightLanguageLoads.get(lang);+ if (!loadPromise) {+ loadPromise = highlighter+ .loadLanguage(lang as BundledLanguage)+ .finally(() => {+ inFlightLanguageLoads.delete(lang);+ });+ inFlightLanguageLoads.set(lang, loadPromise);+ }+ await loadPromise;+ }++ const finalLang = (+ highlighter.getLoadedLanguages().includes(lang) ? lang : "text"+ ) as BundledLanguage;++ const shikiResult = highlighter.codeToTokens(code, {+ lang: finalLang,+ themes: { light: themes[0], dark: themes[1] },+ });++ 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);++ const callbacks = pendingCallbacks.get(cacheKey);+ if (callbacks) {+ callbacks.forEach((cb) => {+ cb(result);+ });+ pendingCallbacks.delete(cacheKey);+ }+ })+ .catch((error) => {+ console.error("[Shiki] Failed to highlight code:", error);++ const fallback: HighlightResult = {+ tokens: code.split("\n").map((line) => [{ content: line }]),+ };++ const callbacks = pendingCallbacks.get(cacheKey);+ if (callbacks) {+ callbacks.forEach((cb) => {+ cb(fallback);+ });+ pendingCallbacks.delete(cacheKey);+ }+ });++ return null;+ },+ };+}++export const code = createSingletonCodePlugin();