@cerebriline/ui 0.0.0-stage → 4.100.313

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (222) hide show
  1. package/ADOPTION.md +833 -0
  2. package/LICENSE +201 -0
  3. package/RADIX-COLORS-LICENSE +21 -0
  4. package/README.md +427 -2
  5. package/components/agent-approval-card.css +70 -0
  6. package/components/agent-approval-card.tsx +109 -0
  7. package/components/agent-ask-question.css +103 -0
  8. package/components/agent-ask-question.tsx +276 -0
  9. package/components/agent-aurora.css +228 -0
  10. package/components/agent-aurora.tsx +96 -0
  11. package/components/agent-changes.css +13 -0
  12. package/components/agent-changes.tsx +167 -0
  13. package/components/agent-chat/agent-chat.css +556 -0
  14. package/components/agent-chat/index.tsx +1037 -0
  15. package/components/agent-chat/tool-diff.tsx +165 -0
  16. package/components/agent-chat/tool-summary/agents.ts +114 -0
  17. package/components/agent-chat/tool-summary/diff.ts +152 -0
  18. package/components/agent-chat/tool-summary/index.ts +928 -0
  19. package/components/agent-chat/tool-summary/parsers.ts +611 -0
  20. package/components/agent-chat/tool-summary/paths.ts +84 -0
  21. package/components/agent-chat/tool-summary/team.ts +401 -0
  22. package/components/agent-command-output.tsx +63 -0
  23. package/components/agent-composer.tsx +263 -0
  24. package/components/agent-context-usage.tsx +181 -0
  25. package/components/agent-conversation-layout.tsx +139 -0
  26. package/components/agent-hero-heading.css +53 -0
  27. package/components/agent-hero-heading.tsx +45 -0
  28. package/components/agent-image-lightbox.tsx +48 -0
  29. package/components/agent-prompt-queue.css +20 -0
  30. package/components/agent-prompt-queue.tsx +350 -0
  31. package/components/agent-pull-request-bar.css +13 -0
  32. package/components/agent-pull-request-bar.tsx +346 -0
  33. package/components/agent-quick-actions.css +19 -0
  34. package/components/agent-quick-actions.tsx +70 -0
  35. package/components/agent-session-row.tsx +183 -0
  36. package/components/agent-welcome-hero-pointer.ts +199 -0
  37. package/components/agent-welcome-hero.css +205 -0
  38. package/components/agent-welcome-hero.tsx +68 -0
  39. package/components/attachment-drop-zone.tsx +154 -0
  40. package/components/badge.tsx +21 -0
  41. package/components/button.tsx +324 -0
  42. package/components/generated-media.tsx +173 -0
  43. package/components/github-icon.tsx +19 -0
  44. package/components/index.ts +133 -0
  45. package/components/markdown.css +490 -0
  46. package/components/markdown.ts +365 -0
  47. package/components/mermaid-block.tsx +722 -0
  48. package/components/mermaid-diagram.ts +1260 -0
  49. package/components/mermaid-dom.ts +301 -0
  50. package/components/message-scroller.css +118 -0
  51. package/components/message-scroller.tsx +114 -0
  52. package/components/search-combobox.css +67 -0
  53. package/components/search-combobox.tsx +433 -0
  54. package/components/session-status.css +19 -0
  55. package/components/session-status.tsx +39 -0
  56. package/components/switch.css +221 -0
  57. package/components/switch.tsx +48 -0
  58. package/components.css +16 -0
  59. package/dist/components/agent-approval-card.d.ts +14 -0
  60. package/dist/components/agent-approval-card.d.ts.map +1 -0
  61. package/dist/components/agent-approval-card.js +12 -0
  62. package/dist/components/agent-approval-card.js.map +1 -0
  63. package/dist/components/agent-ask-question.d.ts +18 -0
  64. package/dist/components/agent-ask-question.d.ts.map +1 -0
  65. package/dist/components/agent-ask-question.js +101 -0
  66. package/dist/components/agent-ask-question.js.map +1 -0
  67. package/dist/components/agent-aurora.d.ts +3 -0
  68. package/dist/components/agent-aurora.d.ts.map +1 -0
  69. package/dist/components/agent-aurora.js +60 -0
  70. package/dist/components/agent-aurora.js.map +1 -0
  71. package/dist/components/agent-changes.d.ts +26 -0
  72. package/dist/components/agent-changes.d.ts.map +1 -0
  73. package/dist/components/agent-changes.js +19 -0
  74. package/dist/components/agent-changes.js.map +1 -0
  75. package/dist/components/agent-chat/disclosure.d.ts +25 -0
  76. package/dist/components/agent-chat/disclosure.d.ts.map +1 -0
  77. package/dist/components/agent-chat/disclosure.js +39 -0
  78. package/dist/components/agent-chat/disclosure.js.map +1 -0
  79. package/dist/components/agent-chat/index.d.ts +140 -0
  80. package/dist/components/agent-chat/index.d.ts.map +1 -0
  81. package/dist/components/agent-chat/index.js +379 -0
  82. package/dist/components/agent-chat/index.js.map +1 -0
  83. package/dist/components/agent-chat/tool-diff.d.ts +32 -0
  84. package/dist/components/agent-chat/tool-diff.d.ts.map +1 -0
  85. package/dist/components/agent-chat/tool-diff.js +97 -0
  86. package/dist/components/agent-chat/tool-diff.js.map +1 -0
  87. package/dist/components/agent-chat/tool-summary/agents.d.ts +10 -0
  88. package/dist/components/agent-chat/tool-summary/agents.d.ts.map +1 -0
  89. package/dist/components/agent-chat/tool-summary/agents.js +100 -0
  90. package/dist/components/agent-chat/tool-summary/agents.js.map +1 -0
  91. package/dist/components/agent-chat/tool-summary/diff.d.ts +22 -0
  92. package/dist/components/agent-chat/tool-summary/diff.d.ts.map +1 -0
  93. package/dist/components/agent-chat/tool-summary/diff.js +127 -0
  94. package/dist/components/agent-chat/tool-summary/diff.js.map +1 -0
  95. package/dist/components/agent-chat/tool-summary/index.d.ts +145 -0
  96. package/dist/components/agent-chat/tool-summary/index.d.ts.map +1 -0
  97. package/dist/components/agent-chat/tool-summary/index.js +663 -0
  98. package/dist/components/agent-chat/tool-summary/index.js.map +1 -0
  99. package/dist/components/agent-chat/tool-summary/parsers.d.ts +114 -0
  100. package/dist/components/agent-chat/tool-summary/parsers.d.ts.map +1 -0
  101. package/dist/components/agent-chat/tool-summary/parsers.js +502 -0
  102. package/dist/components/agent-chat/tool-summary/parsers.js.map +1 -0
  103. package/dist/components/agent-chat/tool-summary/paths.d.ts +10 -0
  104. package/dist/components/agent-chat/tool-summary/paths.d.ts.map +1 -0
  105. package/dist/components/agent-chat/tool-summary/paths.js +88 -0
  106. package/dist/components/agent-chat/tool-summary/paths.js.map +1 -0
  107. package/dist/components/agent-chat/tool-summary/team.d.ts +16 -0
  108. package/dist/components/agent-chat/tool-summary/team.d.ts.map +1 -0
  109. package/dist/components/agent-chat/tool-summary/team.js +283 -0
  110. package/dist/components/agent-chat/tool-summary/team.js.map +1 -0
  111. package/dist/components/agent-command-output.d.ts +14 -0
  112. package/dist/components/agent-command-output.d.ts.map +1 -0
  113. package/dist/components/agent-command-output.js +22 -0
  114. package/dist/components/agent-command-output.js.map +1 -0
  115. package/dist/components/agent-composer.d.ts +34 -0
  116. package/dist/components/agent-composer.d.ts.map +1 -0
  117. package/dist/components/agent-composer.js +65 -0
  118. package/dist/components/agent-composer.js.map +1 -0
  119. package/dist/components/agent-context-usage.d.ts +28 -0
  120. package/dist/components/agent-context-usage.d.ts.map +1 -0
  121. package/dist/components/agent-context-usage.js +51 -0
  122. package/dist/components/agent-context-usage.js.map +1 -0
  123. package/dist/components/agent-conversation-layout.d.ts +30 -0
  124. package/dist/components/agent-conversation-layout.d.ts.map +1 -0
  125. package/dist/components/agent-conversation-layout.js +24 -0
  126. package/dist/components/agent-conversation-layout.js.map +1 -0
  127. package/dist/components/agent-hero-heading.d.ts +2 -0
  128. package/dist/components/agent-hero-heading.d.ts.map +1 -0
  129. package/dist/components/agent-hero-heading.js +20 -0
  130. package/dist/components/agent-hero-heading.js.map +1 -0
  131. package/dist/components/agent-image-lightbox.d.ts +11 -0
  132. package/dist/components/agent-image-lightbox.d.ts.map +1 -0
  133. package/dist/components/agent-image-lightbox.js +8 -0
  134. package/dist/components/agent-image-lightbox.js.map +1 -0
  135. package/dist/components/agent-prompt-queue.d.ts +14 -0
  136. package/dist/components/agent-prompt-queue.d.ts.map +1 -0
  137. package/dist/components/agent-prompt-queue.js +114 -0
  138. package/dist/components/agent-prompt-queue.js.map +1 -0
  139. package/dist/components/agent-pull-request-bar.d.ts +72 -0
  140. package/dist/components/agent-pull-request-bar.d.ts.map +1 -0
  141. package/dist/components/agent-pull-request-bar.js +90 -0
  142. package/dist/components/agent-pull-request-bar.js.map +1 -0
  143. package/dist/components/agent-quick-actions.d.ts +14 -0
  144. package/dist/components/agent-quick-actions.d.ts.map +1 -0
  145. package/dist/components/agent-quick-actions.js +12 -0
  146. package/dist/components/agent-quick-actions.js.map +1 -0
  147. package/dist/components/agent-session-row.d.ts +45 -0
  148. package/dist/components/agent-session-row.d.ts.map +1 -0
  149. package/dist/components/agent-session-row.js +34 -0
  150. package/dist/components/agent-session-row.js.map +1 -0
  151. package/dist/components/agent-welcome-hero-pointer.d.ts +3 -0
  152. package/dist/components/agent-welcome-hero-pointer.d.ts.map +1 -0
  153. package/dist/components/agent-welcome-hero-pointer.js +169 -0
  154. package/dist/components/agent-welcome-hero-pointer.js.map +1 -0
  155. package/dist/components/agent-welcome-hero.d.ts +9 -0
  156. package/dist/components/agent-welcome-hero.d.ts.map +1 -0
  157. package/dist/components/agent-welcome-hero.js +14 -0
  158. package/dist/components/agent-welcome-hero.js.map +1 -0
  159. package/dist/components/attachment-drop-zone.d.ts +15 -0
  160. package/dist/components/attachment-drop-zone.d.ts.map +1 -0
  161. package/dist/components/attachment-drop-zone.js +64 -0
  162. package/dist/components/attachment-drop-zone.js.map +1 -0
  163. package/dist/components/badge.d.ts +5 -0
  164. package/dist/components/badge.d.ts.map +1 -0
  165. package/dist/components/badge.js +7 -0
  166. package/dist/components/badge.js.map +1 -0
  167. package/dist/components/button.d.ts +27 -0
  168. package/dist/components/button.d.ts.map +1 -0
  169. package/dist/components/button.js +209 -0
  170. package/dist/components/button.js.map +1 -0
  171. package/dist/components/generated-media.d.ts +12 -0
  172. package/dist/components/generated-media.d.ts.map +1 -0
  173. package/dist/components/generated-media.js +105 -0
  174. package/dist/components/generated-media.js.map +1 -0
  175. package/dist/components/github-icon.d.ts +3 -0
  176. package/dist/components/github-icon.d.ts.map +1 -0
  177. package/dist/components/github-icon.js +6 -0
  178. package/dist/components/github-icon.js.map +1 -0
  179. package/dist/components/index.d.ts +24 -0
  180. package/dist/components/index.d.ts.map +1 -0
  181. package/dist/components/index.js +25 -0
  182. package/dist/components/index.js.map +1 -0
  183. package/dist/components/markdown.d.ts +48 -0
  184. package/dist/components/markdown.d.ts.map +1 -0
  185. package/dist/components/markdown.js +264 -0
  186. package/dist/components/markdown.js.map +1 -0
  187. package/dist/components/mermaid-block.d.ts +14 -0
  188. package/dist/components/mermaid-block.d.ts.map +1 -0
  189. package/dist/components/mermaid-block.js +339 -0
  190. package/dist/components/mermaid-block.js.map +1 -0
  191. package/dist/components/mermaid-diagram.d.ts +264 -0
  192. package/dist/components/mermaid-diagram.d.ts.map +1 -0
  193. package/dist/components/mermaid-diagram.js +999 -0
  194. package/dist/components/mermaid-diagram.js.map +1 -0
  195. package/dist/components/mermaid-dom.d.ts +51 -0
  196. package/dist/components/mermaid-dom.d.ts.map +1 -0
  197. package/dist/components/mermaid-dom.js +246 -0
  198. package/dist/components/mermaid-dom.js.map +1 -0
  199. package/dist/components/message-scroller.d.ts +23 -0
  200. package/dist/components/message-scroller.d.ts.map +1 -0
  201. package/dist/components/message-scroller.js +31 -0
  202. package/dist/components/message-scroller.js.map +1 -0
  203. package/dist/components/search-combobox.d.ts +44 -0
  204. package/dist/components/search-combobox.d.ts.map +1 -0
  205. package/dist/components/search-combobox.js +191 -0
  206. package/dist/components/search-combobox.js.map +1 -0
  207. package/dist/components/session-status.d.ts +9 -0
  208. package/dist/components/session-status.d.ts.map +1 -0
  209. package/dist/components/session-status.js +11 -0
  210. package/dist/components/session-status.js.map +1 -0
  211. package/dist/components/switch.d.ts +7 -0
  212. package/dist/components/switch.d.ts.map +1 -0
  213. package/dist/components/switch.js +14 -0
  214. package/dist/components/switch.js.map +1 -0
  215. package/package.json +177 -5
  216. package/theme/base.css +83 -0
  217. package/theme/component-theme.css +126 -0
  218. package/theme/index.css +3 -0
  219. package/theme/palette.css +347 -0
  220. package/theme/scoped-tokens.css +503 -0
  221. package/theme/theme.css +90 -0
  222. package/theme/tokens.css +165 -0
@@ -0,0 +1,365 @@
1
+ import type { MermaidConfig } from "mermaid";
2
+ import type {
3
+ HighlighterCore,
4
+ LanguageRegistration,
5
+ ThemeRegistration,
6
+ } from "shiki/core";
7
+ import type {
8
+ CodeHighlighterPlugin,
9
+ ControlsConfig,
10
+ DiagramPlugin,
11
+ } from "streamdown";
12
+ import {
13
+ buildMermaidConfig,
14
+ createDefaultMermaidConfig,
15
+ createMermaidService,
16
+ defaultMermaidLoader,
17
+ type LazyMermaidInstance,
18
+ type MermaidModuleLoader,
19
+ } from "./mermaid-diagram.js";
20
+ import { readMermaidTheme } from "./mermaid-dom.js";
21
+
22
+ /**
23
+ * Shared Streamdown configuration for agent chat Markdown, so every product
24
+ * renders assistant prose the same way. Products keep their own Streamdown
25
+ * wrapper (link policy, image policy, extra plugins) and pass these in:
26
+ *
27
+ * <Streamdown
28
+ * className="cline-markdown"
29
+ * controls={agentMarkdownControls}
30
+ * plugins={{ code: markdownCodeHighlighter }}
31
+ * >
32
+ *
33
+ * Pair with `@cerebriline/ui/components/markdown.css` for the matching visual
34
+ * treatment (single quiet code blocks with a hover copy control, chat-scale
35
+ * headings, table cards).
36
+ *
37
+ * Runtime requirements (optional peer dependencies): `streamdown`, `shiki`,
38
+ * `@shikijs/langs`, and `@shikijs/themes`.
39
+ */
40
+
41
+ type HighlightResult = NonNullable<
42
+ ReturnType<CodeHighlighterPlugin["highlight"]>
43
+ >;
44
+
45
+ /** Copy on code blocks; no downloads; no mermaid/table chrome. The matching
46
+ * CSS hides the code-block header and reveals the copy control on hover. */
47
+ export const agentMarkdownControls = {
48
+ code: { copy: true, download: false },
49
+ mermaid: false,
50
+ table: false,
51
+ } satisfies ControlsConfig;
52
+
53
+ /** Desktop/host opt-in controls for interactive Mermaid diagrams. The default
54
+ * controls above stay diagram-free so existing consumers don't change unless
55
+ * they also register a Mermaid DiagramPlugin. */
56
+ export const agentMarkdownControlsWithMermaid = {
57
+ ...agentMarkdownControls,
58
+ mermaid: {
59
+ copy: true,
60
+ download: true,
61
+ fullscreen: true,
62
+ panZoom: true,
63
+ },
64
+ } satisfies ControlsConfig;
65
+
66
+ export {
67
+ buildMermaidConfig,
68
+ buildMermaidThemeVariables,
69
+ createDefaultMermaidConfig,
70
+ DIAGRAM_LINK_HREF_ATTRIBUTE,
71
+ type MermaidColorMode,
72
+ type MermaidColorResolver,
73
+ type MermaidModuleLoader,
74
+ type MermaidThemeOptions,
75
+ type MermaidThemeTokens,
76
+ neutralizeDiagramLinks,
77
+ } from "./mermaid-diagram.js";
78
+
79
+ /** Base-theme defaults from the palette (light); hosts with a DOM resolve the
80
+ * live design tokens instead, see `resolveThemedMermaidConfig`. */
81
+ export const DEFAULT_MERMAID_CONFIG: MermaidConfig =
82
+ createDefaultMermaidConfig("light");
83
+
84
+ /**
85
+ * Creates a Streamdown Mermaid *diagram plugin* (Streamdown's built-in block
86
+ * chrome). Hosts that want the owned block (filename header, PNG/MMD
87
+ * downloads, themed PNGs) register `createMermaidRenderer` from
88
+ * `@cerebriline/ui/components/mermaid-block` instead.
89
+ *
90
+ * The default config is resolved from the live Cline design tokens at render
91
+ * time (falling back to the palette without a DOM) and re-applied when the
92
+ * resolved theme changes; configs supplied through `getMermaid(config)` layer
93
+ * on top, with `securityLevel` always forced to `strict`.
94
+ */
95
+ export function createLazyMermaidPlugin(
96
+ loader: MermaidModuleLoader = defaultMermaidLoader,
97
+ ): DiagramPlugin {
98
+ const service = createMermaidService(loader);
99
+ let overrides: MermaidConfig = {};
100
+ let cached: { config: MermaidConfig; key: string } | undefined;
101
+
102
+ const instance: LazyMermaidInstance = {
103
+ initialize(nextConfig: MermaidConfig) {
104
+ overrides = { ...overrides, ...nextConfig };
105
+ cached = undefined;
106
+ },
107
+ render(id: string, source: string) {
108
+ const theme = readMermaidTheme();
109
+ // One config object per theme key so the shared service only calls
110
+ // `initialize` when the theme or the overrides change.
111
+ if (cached?.key !== theme.key) {
112
+ cached = {
113
+ config: {
114
+ ...buildMermaidConfig(theme.tokens, theme.mode, {
115
+ fontFamily: theme.fontFamily,
116
+ }),
117
+ ...overrides,
118
+ securityLevel: "strict",
119
+ },
120
+ key: theme.key,
121
+ };
122
+ }
123
+ return service.render(id, source, cached.config);
124
+ },
125
+ };
126
+
127
+ return {
128
+ getMermaid(nextConfig?: MermaidConfig) {
129
+ if (nextConfig) instance.initialize(nextConfig);
130
+ return instance;
131
+ },
132
+ language: "mermaid",
133
+ name: "mermaid",
134
+ type: "diagram",
135
+ };
136
+ }
137
+
138
+ export const SUPPORTED_MARKDOWN_LANGUAGES = [
139
+ "bash",
140
+ "css",
141
+ "diff",
142
+ "html",
143
+ "javascript",
144
+ "json",
145
+ "jsonc",
146
+ "jsx",
147
+ "markdown",
148
+ "python",
149
+ "shellscript",
150
+ "tsx",
151
+ "typescript",
152
+ "yaml",
153
+ ] as const;
154
+
155
+ type SupportedMarkdownLanguage = (typeof SUPPORTED_MARKDOWN_LANGUAGES)[number];
156
+
157
+ const SUPPORTED_LANGUAGE_SET = new Set<string>(SUPPORTED_MARKDOWN_LANGUAGES);
158
+
159
+ const LANGUAGE_ALIASES: Record<string, SupportedMarkdownLanguage> = {
160
+ cjs: "javascript",
161
+ console: "shellscript",
162
+ htm: "html",
163
+ js: "javascript",
164
+ json5: "jsonc",
165
+ md: "markdown",
166
+ mjs: "javascript",
167
+ py: "python",
168
+ sh: "shellscript",
169
+ shell: "shellscript",
170
+ ts: "typescript",
171
+ yml: "yaml",
172
+ };
173
+
174
+ const LANGUAGE_LOADERS: Record<
175
+ SupportedMarkdownLanguage,
176
+ () => Promise<LanguageRegistration[]>
177
+ > = {
178
+ bash: () => import("@shikijs/langs/bash").then((module) => module.default),
179
+ css: () => import("@shikijs/langs/css").then((module) => module.default),
180
+ diff: () => import("@shikijs/langs/diff").then((module) => module.default),
181
+ html: () => import("@shikijs/langs/html").then((module) => module.default),
182
+ javascript: () =>
183
+ import("@shikijs/langs/javascript").then((module) => module.default),
184
+ json: () => import("@shikijs/langs/json").then((module) => module.default),
185
+ jsonc: () => import("@shikijs/langs/jsonc").then((module) => module.default),
186
+ jsx: () => import("@shikijs/langs/jsx").then((module) => module.default),
187
+ markdown: () =>
188
+ import("@shikijs/langs/markdown").then((module) => module.default),
189
+ python: () =>
190
+ import("@shikijs/langs/python").then((module) => module.default),
191
+ shellscript: () =>
192
+ import("@shikijs/langs/shellscript").then((module) => module.default),
193
+ tsx: () => import("@shikijs/langs/tsx").then((module) => module.default),
194
+ typescript: () =>
195
+ import("@shikijs/langs/typescript").then((module) => module.default),
196
+ yaml: () => import("@shikijs/langs/yaml").then((module) => module.default),
197
+ };
198
+
199
+ const LIGHT_THEME = "github-light";
200
+ const DARK_THEME = "github-dark";
201
+ const MAX_CACHED_RESULTS = 256;
202
+
203
+ let highlighterPromise: Promise<HighlighterCore> | undefined;
204
+ let themesPromise: Promise<void> | undefined;
205
+ const languagePromises = new Map<SupportedMarkdownLanguage, Promise<void>>();
206
+ const resultCache = new Map<string, HighlightResult>();
207
+ const pendingHighlights = new Map<string, Promise<HighlightResult>>();
208
+ const loggedFailures = new Set<string>();
209
+
210
+ function normalizeLanguage(language: string): SupportedMarkdownLanguage | null {
211
+ const normalized = language.trim().toLowerCase();
212
+ if (!normalized) return null;
213
+ const aliased = LANGUAGE_ALIASES[normalized] ?? normalized;
214
+ return SUPPORTED_LANGUAGE_SET.has(aliased)
215
+ ? (aliased as SupportedMarkdownLanguage)
216
+ : null;
217
+ }
218
+
219
+ function getHighlighter(): Promise<HighlighterCore> {
220
+ if (!highlighterPromise) {
221
+ highlighterPromise = Promise.all([
222
+ import("shiki/core"),
223
+ import("shiki/engine/javascript"),
224
+ ]).then(([core, engine]) =>
225
+ core.createHighlighterCore({
226
+ engine: engine.createJavaScriptRegexEngine({ forgiving: true }),
227
+ }),
228
+ );
229
+ }
230
+ return highlighterPromise;
231
+ }
232
+
233
+ function ensureThemes(highlighter: HighlighterCore): Promise<void> {
234
+ if (!themesPromise) {
235
+ themesPromise = Promise.all([
236
+ import("@shikijs/themes/github-light").then((module) => module.default),
237
+ import("@shikijs/themes/github-dark").then((module) => module.default),
238
+ ]).then((themes: ThemeRegistration[]) => highlighter.loadTheme(...themes));
239
+ }
240
+ return themesPromise;
241
+ }
242
+
243
+ function ensureLanguage(
244
+ highlighter: HighlighterCore,
245
+ language: SupportedMarkdownLanguage,
246
+ ): Promise<void> {
247
+ const existing = languagePromises.get(language);
248
+ if (existing) return existing;
249
+
250
+ const loading = LANGUAGE_LOADERS[language]().then((registrations) =>
251
+ highlighter.loadLanguage(...registrations),
252
+ );
253
+ languagePromises.set(language, loading);
254
+ return loading;
255
+ }
256
+
257
+ function rawHighlight(code: string): HighlightResult {
258
+ return {
259
+ bg: "transparent",
260
+ fg: "inherit",
261
+ tokens: code.split("\n").map((line) =>
262
+ line
263
+ ? [
264
+ {
265
+ bgColor: "transparent",
266
+ color: "inherit",
267
+ content: line,
268
+ htmlStyle: {},
269
+ offset: 0,
270
+ },
271
+ ]
272
+ : [],
273
+ ),
274
+ };
275
+ }
276
+
277
+ function cacheResult(key: string, result: HighlightResult): void {
278
+ resultCache.delete(key);
279
+ resultCache.set(key, result);
280
+ if (resultCache.size <= MAX_CACHED_RESULTS) return;
281
+
282
+ const oldestKey = resultCache.keys().next().value;
283
+ if (oldestKey !== undefined) resultCache.delete(oldestKey);
284
+ }
285
+
286
+ function reportHighlightFailure(
287
+ language: SupportedMarkdownLanguage,
288
+ error: unknown,
289
+ ): void {
290
+ if (loggedFailures.has(language)) return;
291
+ loggedFailures.add(language);
292
+ console.warn(
293
+ `Syntax highlighting unavailable for ${language}; rendering plain code.`,
294
+ error,
295
+ );
296
+ }
297
+
298
+ function loadHighlight(
299
+ code: string,
300
+ language: SupportedMarkdownLanguage,
301
+ ): Promise<HighlightResult> {
302
+ const cacheKey = `${language}\0${code}`;
303
+ const cached = resultCache.get(cacheKey);
304
+ if (cached) return Promise.resolve(cached);
305
+
306
+ const pending = pendingHighlights.get(cacheKey);
307
+ if (pending) return pending;
308
+
309
+ const loading = getHighlighter()
310
+ .then(async (highlighter) => {
311
+ await ensureThemes(highlighter);
312
+ await ensureLanguage(highlighter, language);
313
+ const result = highlighter.codeToTokens(code, {
314
+ lang: language,
315
+ themes: {
316
+ dark: DARK_THEME,
317
+ light: LIGHT_THEME,
318
+ },
319
+ });
320
+ return {
321
+ bg: result.bg,
322
+ fg: result.fg,
323
+ rootStyle: result.rootStyle,
324
+ tokens: result.tokens,
325
+ } satisfies HighlightResult;
326
+ })
327
+ .catch((error: unknown) => {
328
+ reportHighlightFailure(language, error);
329
+ return rawHighlight(code);
330
+ })
331
+ .then((result) => {
332
+ cacheResult(cacheKey, result);
333
+ return result;
334
+ })
335
+ .finally(() => {
336
+ pendingHighlights.delete(cacheKey);
337
+ });
338
+
339
+ pendingHighlights.set(cacheKey, loading);
340
+ return loading;
341
+ }
342
+
343
+ // Annotated (not `satisfies`) so the emitted declaration names the public
344
+ // CodeHighlighterPlugin type instead of streamdown's unexported internals.
345
+ export const markdownCodeHighlighter: CodeHighlighterPlugin = {
346
+ getSupportedLanguages: () => [...SUPPORTED_MARKDOWN_LANGUAGES],
347
+ getThemes: () => [LIGHT_THEME, DARK_THEME],
348
+ highlight: ({ code, language }, callback) => {
349
+ const normalizedLanguage = normalizeLanguage(language);
350
+ if (!normalizedLanguage) return rawHighlight(code);
351
+
352
+ const cacheKey = `${normalizedLanguage}\0${code}`;
353
+ const cached = resultCache.get(cacheKey);
354
+ if (cached) return cached;
355
+
356
+ const loading = loadHighlight(code, normalizedLanguage);
357
+ if (callback) {
358
+ void loading.then((result) => callback(result));
359
+ }
360
+ return null;
361
+ },
362
+ name: "shiki",
363
+ supportsLanguage: (language) => normalizeLanguage(language) !== null,
364
+ type: "code-highlighter",
365
+ };