streaming-markdown-react 0.1.4
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.
- package/LICENSE +13 -0
- package/README.md +40 -0
- package/dist/index.d.mts +151 -0
- package/dist/index.d.ts +151 -0
- package/dist/index.js +537 -0
- package/dist/index.mjs +490 -0
- package/package.json +59 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
CodeBlock: () => CodeBlock,
|
|
34
|
+
MessageBlockRenderer: () => MessageBlockRenderer,
|
|
35
|
+
MessageBlockStatus: () => MessageBlockStatus,
|
|
36
|
+
MessageBlockStore: () => MessageBlockStore,
|
|
37
|
+
MessageBlockType: () => MessageBlockType,
|
|
38
|
+
MessageItem: () => MessageItem,
|
|
39
|
+
MessageStatus: () => MessageStatus,
|
|
40
|
+
StreamingMarkdown: () => StreamingMarkdown,
|
|
41
|
+
messageBlockStore: () => messageBlockStore,
|
|
42
|
+
useShikiHighlight: () => useShikiHighlight,
|
|
43
|
+
useSmoothStream: () => useSmoothStream
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(index_exports);
|
|
46
|
+
|
|
47
|
+
// src/types/message.ts
|
|
48
|
+
var MessageStatus = /* @__PURE__ */ ((MessageStatus2) => {
|
|
49
|
+
MessageStatus2["IDLE"] = "idle";
|
|
50
|
+
MessageStatus2["STREAMING"] = "streaming";
|
|
51
|
+
MessageStatus2["SUCCESS"] = "success";
|
|
52
|
+
MessageStatus2["ERROR"] = "error";
|
|
53
|
+
return MessageStatus2;
|
|
54
|
+
})(MessageStatus || {});
|
|
55
|
+
var MessageBlockStatus = /* @__PURE__ */ ((MessageBlockStatus2) => {
|
|
56
|
+
MessageBlockStatus2["IDLE"] = "idle";
|
|
57
|
+
MessageBlockStatus2["STREAMING"] = "streaming";
|
|
58
|
+
MessageBlockStatus2["SUCCESS"] = "success";
|
|
59
|
+
MessageBlockStatus2["ERROR"] = "error";
|
|
60
|
+
return MessageBlockStatus2;
|
|
61
|
+
})(MessageBlockStatus || {});
|
|
62
|
+
var MessageBlockType = /* @__PURE__ */ ((MessageBlockType2) => {
|
|
63
|
+
MessageBlockType2["MAIN_TEXT"] = "main_text";
|
|
64
|
+
MessageBlockType2["CODE"] = "code";
|
|
65
|
+
MessageBlockType2["IMAGE"] = "image";
|
|
66
|
+
MessageBlockType2["FILE"] = "file";
|
|
67
|
+
MessageBlockType2["TOOL"] = "tool";
|
|
68
|
+
MessageBlockType2["CITATION"] = "citation";
|
|
69
|
+
MessageBlockType2["TRANSLATION"] = "translation";
|
|
70
|
+
MessageBlockType2["THINKING"] = "thinking";
|
|
71
|
+
MessageBlockType2["VIDEO"] = "video";
|
|
72
|
+
MessageBlockType2["ERROR"] = "error";
|
|
73
|
+
MessageBlockType2["UNKNOWN"] = "unknown";
|
|
74
|
+
return MessageBlockType2;
|
|
75
|
+
})(MessageBlockType || {});
|
|
76
|
+
|
|
77
|
+
// src/store/messageBlocks.ts
|
|
78
|
+
var MessageBlockStore = class {
|
|
79
|
+
constructor() {
|
|
80
|
+
this.blocks = /* @__PURE__ */ new Map();
|
|
81
|
+
}
|
|
82
|
+
upsert(input) {
|
|
83
|
+
const list = Array.isArray(input) ? input : [input];
|
|
84
|
+
for (const block of list) {
|
|
85
|
+
this.blocks.set(block.id, block);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
selectById(id) {
|
|
89
|
+
return this.blocks.get(id);
|
|
90
|
+
}
|
|
91
|
+
selectMany(ids) {
|
|
92
|
+
return ids.map((id) => this.blocks.get(id)).filter((block) => Boolean(block));
|
|
93
|
+
}
|
|
94
|
+
remove(input) {
|
|
95
|
+
const list = Array.isArray(input) ? input : [input];
|
|
96
|
+
for (const blockId of list) {
|
|
97
|
+
this.blocks.delete(blockId);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
clear() {
|
|
101
|
+
this.blocks.clear();
|
|
102
|
+
}
|
|
103
|
+
selectAll() {
|
|
104
|
+
return Array.from(this.blocks.values());
|
|
105
|
+
}
|
|
106
|
+
toJSON() {
|
|
107
|
+
return Object.fromEntries(this.blocks.entries());
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var messageBlockStore = new MessageBlockStore();
|
|
111
|
+
|
|
112
|
+
// src/components/Markdown/StreamingMarkdown.tsx
|
|
113
|
+
var import_react3 = require("react");
|
|
114
|
+
var import_react_markdown = __toESM(require("react-markdown"));
|
|
115
|
+
var import_remark_gfm = __toESM(require("remark-gfm"));
|
|
116
|
+
|
|
117
|
+
// src/hooks/useSmoothStream.ts
|
|
118
|
+
var import_react = require("react");
|
|
119
|
+
var segmenter = new Intl.Segmenter(["en", "zh", "ja", "ru", "fr", "de"], {
|
|
120
|
+
granularity: "grapheme"
|
|
121
|
+
});
|
|
122
|
+
function useSmoothStream({
|
|
123
|
+
onUpdate,
|
|
124
|
+
streamDone,
|
|
125
|
+
minDelay = 10,
|
|
126
|
+
initialText = "",
|
|
127
|
+
onComplete
|
|
128
|
+
}) {
|
|
129
|
+
const chunkQueueRef = (0, import_react.useRef)([]);
|
|
130
|
+
const displayedTextRef = (0, import_react.useRef)(initialText);
|
|
131
|
+
const lastUpdateTimeRef = (0, import_react.useRef)(0);
|
|
132
|
+
const animationFrameRef = (0, import_react.useRef)(null);
|
|
133
|
+
const completedRef = (0, import_react.useRef)(false);
|
|
134
|
+
const onUpdateRef = (0, import_react.useRef)(onUpdate);
|
|
135
|
+
const onCompleteRef = (0, import_react.useRef)(onComplete);
|
|
136
|
+
const streamDoneRef = (0, import_react.useRef)(streamDone);
|
|
137
|
+
const minDelayRef = (0, import_react.useRef)(minDelay);
|
|
138
|
+
(0, import_react.useEffect)(() => {
|
|
139
|
+
onUpdateRef.current = onUpdate;
|
|
140
|
+
}, [onUpdate]);
|
|
141
|
+
(0, import_react.useEffect)(() => {
|
|
142
|
+
onCompleteRef.current = onComplete;
|
|
143
|
+
}, [onComplete]);
|
|
144
|
+
const renderLoopRef = (0, import_react.useRef)(null);
|
|
145
|
+
(0, import_react.useEffect)(() => {
|
|
146
|
+
streamDoneRef.current = streamDone;
|
|
147
|
+
if (streamDone) {
|
|
148
|
+
if (chunkQueueRef.current.length === 0 && !completedRef.current) {
|
|
149
|
+
completedRef.current = true;
|
|
150
|
+
onCompleteRef.current?.();
|
|
151
|
+
} else if (!animationFrameRef.current && renderLoopRef.current) {
|
|
152
|
+
animationFrameRef.current = requestAnimationFrame(renderLoopRef.current);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}, [streamDone]);
|
|
156
|
+
(0, import_react.useEffect)(() => {
|
|
157
|
+
minDelayRef.current = minDelay;
|
|
158
|
+
}, [minDelay]);
|
|
159
|
+
(0, import_react.useEffect)(() => {
|
|
160
|
+
if (initialText) {
|
|
161
|
+
onUpdateRef.current(initialText);
|
|
162
|
+
}
|
|
163
|
+
}, [initialText]);
|
|
164
|
+
const renderLoop = (0, import_react.useCallback)((currentTime) => {
|
|
165
|
+
const queue = chunkQueueRef.current;
|
|
166
|
+
const hasQueue = queue.length > 0;
|
|
167
|
+
if (!hasQueue) {
|
|
168
|
+
if (streamDoneRef.current && !completedRef.current) {
|
|
169
|
+
completedRef.current = true;
|
|
170
|
+
onCompleteRef.current?.();
|
|
171
|
+
animationFrameRef.current = null;
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
animationFrameRef.current = null;
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
if (currentTime - lastUpdateTimeRef.current < minDelayRef.current) {
|
|
178
|
+
animationFrameRef.current = requestAnimationFrame(renderLoop);
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
lastUpdateTimeRef.current = currentTime;
|
|
182
|
+
let count = Math.max(1, Math.floor(queue.length / 5));
|
|
183
|
+
if (streamDoneRef.current) {
|
|
184
|
+
count = queue.length;
|
|
185
|
+
}
|
|
186
|
+
const next = queue.splice(0, count).join("");
|
|
187
|
+
displayedTextRef.current += next;
|
|
188
|
+
onUpdateRef.current(displayedTextRef.current);
|
|
189
|
+
animationFrameRef.current = requestAnimationFrame(renderLoop);
|
|
190
|
+
}, []);
|
|
191
|
+
(0, import_react.useEffect)(() => {
|
|
192
|
+
renderLoopRef.current = renderLoop;
|
|
193
|
+
}, [renderLoop]);
|
|
194
|
+
const addChunk = (0, import_react.useCallback)(
|
|
195
|
+
(chunk) => {
|
|
196
|
+
if (!chunk) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const segments = Array.from(segmenter.segment(chunk)).map((s) => s.segment);
|
|
200
|
+
chunkQueueRef.current.push(...segments);
|
|
201
|
+
if (!animationFrameRef.current) {
|
|
202
|
+
animationFrameRef.current = requestAnimationFrame(renderLoop);
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
[renderLoop]
|
|
206
|
+
);
|
|
207
|
+
const reset = (0, import_react.useCallback)((newText = "") => {
|
|
208
|
+
if (animationFrameRef.current) {
|
|
209
|
+
cancelAnimationFrame(animationFrameRef.current);
|
|
210
|
+
animationFrameRef.current = null;
|
|
211
|
+
}
|
|
212
|
+
chunkQueueRef.current = [];
|
|
213
|
+
displayedTextRef.current = newText;
|
|
214
|
+
completedRef.current = false;
|
|
215
|
+
onUpdateRef.current(newText);
|
|
216
|
+
}, []);
|
|
217
|
+
(0, import_react.useEffect)(
|
|
218
|
+
() => () => {
|
|
219
|
+
if (animationFrameRef.current) {
|
|
220
|
+
cancelAnimationFrame(animationFrameRef.current);
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
[]
|
|
224
|
+
);
|
|
225
|
+
return { addChunk, reset };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// src/hooks/useShikiHighlight.ts
|
|
229
|
+
var import_react2 = require("react");
|
|
230
|
+
var import_core = require("shiki/core");
|
|
231
|
+
var import_javascript = require("shiki/engine/javascript");
|
|
232
|
+
var highlighterPromise;
|
|
233
|
+
async function getHighlighter() {
|
|
234
|
+
if (!highlighterPromise) {
|
|
235
|
+
highlighterPromise = (0, import_core.createHighlighterCore)({
|
|
236
|
+
themes: [
|
|
237
|
+
import("shiki/themes/github-light.mjs"),
|
|
238
|
+
import("shiki/themes/github-dark.mjs")
|
|
239
|
+
],
|
|
240
|
+
langs: [
|
|
241
|
+
import("shiki/langs/javascript.mjs"),
|
|
242
|
+
import("shiki/langs/typescript.mjs"),
|
|
243
|
+
import("shiki/langs/tsx.mjs"),
|
|
244
|
+
import("shiki/langs/jsx.mjs"),
|
|
245
|
+
import("shiki/langs/python.mjs"),
|
|
246
|
+
import("shiki/langs/java.mjs"),
|
|
247
|
+
import("shiki/langs/json.mjs"),
|
|
248
|
+
import("shiki/langs/html.mjs"),
|
|
249
|
+
import("shiki/langs/css.mjs"),
|
|
250
|
+
import("shiki/langs/bash.mjs"),
|
|
251
|
+
import("shiki/langs/shell.mjs"),
|
|
252
|
+
import("shiki/langs/sql.mjs"),
|
|
253
|
+
import("shiki/langs/markdown.mjs"),
|
|
254
|
+
import("shiki/langs/go.mjs"),
|
|
255
|
+
import("shiki/langs/rust.mjs"),
|
|
256
|
+
import("shiki/langs/c.mjs"),
|
|
257
|
+
import("shiki/langs/cpp.mjs"),
|
|
258
|
+
import("shiki/langs/csharp.mjs"),
|
|
259
|
+
import("shiki/langs/php.mjs"),
|
|
260
|
+
import("shiki/langs/ruby.mjs"),
|
|
261
|
+
import("shiki/langs/swift.mjs"),
|
|
262
|
+
import("shiki/langs/kotlin.mjs"),
|
|
263
|
+
import("shiki/langs/yaml.mjs"),
|
|
264
|
+
import("shiki/langs/xml.mjs"),
|
|
265
|
+
import("shiki/langs/dockerfile.mjs")
|
|
266
|
+
],
|
|
267
|
+
engine: (0, import_javascript.createJavaScriptRegexEngine)()
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
return highlighterPromise;
|
|
271
|
+
}
|
|
272
|
+
function useShikiHighlight({
|
|
273
|
+
code,
|
|
274
|
+
language = "text",
|
|
275
|
+
theme = "light"
|
|
276
|
+
}) {
|
|
277
|
+
const [html, setHtml] = (0, import_react2.useState)("");
|
|
278
|
+
const [isLoading, setIsLoading] = (0, import_react2.useState)(true);
|
|
279
|
+
const [error, setError] = (0, import_react2.useState)(null);
|
|
280
|
+
(0, import_react2.useEffect)(() => {
|
|
281
|
+
let cancelled = false;
|
|
282
|
+
async function highlight() {
|
|
283
|
+
try {
|
|
284
|
+
setIsLoading(true);
|
|
285
|
+
setError(null);
|
|
286
|
+
const highlighter = await getHighlighter();
|
|
287
|
+
if (cancelled) return;
|
|
288
|
+
const normalizedLang = language.toLowerCase();
|
|
289
|
+
if (cancelled) return;
|
|
290
|
+
const themeName = theme === "dark" ? "github-dark" : "github-light";
|
|
291
|
+
const highlighted = highlighter.codeToHtml(code, {
|
|
292
|
+
lang: normalizedLang,
|
|
293
|
+
theme: themeName
|
|
294
|
+
});
|
|
295
|
+
if (!cancelled) {
|
|
296
|
+
setHtml(highlighted);
|
|
297
|
+
setIsLoading(false);
|
|
298
|
+
}
|
|
299
|
+
} catch (err) {
|
|
300
|
+
if (!cancelled) {
|
|
301
|
+
setError(err instanceof Error ? err : new Error(String(err)));
|
|
302
|
+
setIsLoading(false);
|
|
303
|
+
setHtml(`<pre><code>${escapeHtml(code)}</code></pre>`);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
highlight();
|
|
308
|
+
return () => {
|
|
309
|
+
cancelled = true;
|
|
310
|
+
};
|
|
311
|
+
}, [code, language, theme]);
|
|
312
|
+
return { html, isLoading, error };
|
|
313
|
+
}
|
|
314
|
+
function escapeHtml(unsafe) {
|
|
315
|
+
return unsafe.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// src/components/Markdown/CodeBlock.tsx
|
|
319
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
320
|
+
function CodeBlock({
|
|
321
|
+
code,
|
|
322
|
+
language = "text",
|
|
323
|
+
className,
|
|
324
|
+
theme = "light"
|
|
325
|
+
}) {
|
|
326
|
+
const { html, isLoading } = useShikiHighlight({ code, language, theme });
|
|
327
|
+
if (isLoading) {
|
|
328
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("pre", { className, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("code", { "data-language": language, children: code }) });
|
|
329
|
+
}
|
|
330
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
331
|
+
"div",
|
|
332
|
+
{
|
|
333
|
+
dangerouslySetInnerHTML: { __html: html },
|
|
334
|
+
className,
|
|
335
|
+
style: {
|
|
336
|
+
borderRadius: "8px",
|
|
337
|
+
overflow: "auto"
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// src/components/Markdown/StreamingMarkdown.tsx
|
|
344
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
345
|
+
function StreamingMarkdown({
|
|
346
|
+
children,
|
|
347
|
+
className,
|
|
348
|
+
components: customComponents,
|
|
349
|
+
status = "idle",
|
|
350
|
+
onComplete,
|
|
351
|
+
minDelay = 10
|
|
352
|
+
}) {
|
|
353
|
+
const markdown = typeof children === "string" ? children : String(children || "");
|
|
354
|
+
const [displayedText, setDisplayedText] = (0, import_react3.useState)(status !== "streaming" ? markdown : "");
|
|
355
|
+
const previousChildrenRef = (0, import_react3.useRef)(status !== "streaming" ? markdown : "");
|
|
356
|
+
console.log("StreamingMarkdown children:", markdown);
|
|
357
|
+
const { addChunk, reset } = useSmoothStream({
|
|
358
|
+
onUpdate: setDisplayedText,
|
|
359
|
+
streamDone: status !== "streaming",
|
|
360
|
+
minDelay,
|
|
361
|
+
initialText: status !== "streaming" ? markdown : "",
|
|
362
|
+
onComplete
|
|
363
|
+
});
|
|
364
|
+
(0, import_react3.useEffect)(() => {
|
|
365
|
+
const currentContent = markdown;
|
|
366
|
+
const previousContent = previousChildrenRef.current;
|
|
367
|
+
if (currentContent !== previousContent) {
|
|
368
|
+
if (currentContent.startsWith(previousContent)) {
|
|
369
|
+
const delta = currentContent.slice(previousContent.length);
|
|
370
|
+
addChunk(delta);
|
|
371
|
+
} else {
|
|
372
|
+
reset(currentContent);
|
|
373
|
+
}
|
|
374
|
+
previousChildrenRef.current = currentContent;
|
|
375
|
+
}
|
|
376
|
+
}, [markdown, addChunk, reset]);
|
|
377
|
+
const components = (0, import_react3.useMemo)(() => {
|
|
378
|
+
const baseComponents = {
|
|
379
|
+
code: (props) => {
|
|
380
|
+
const { node, inline, className: className2, children: children2, ...rest } = props;
|
|
381
|
+
if (inline) {
|
|
382
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("code", { className: className2, ...rest, children: children2 });
|
|
383
|
+
}
|
|
384
|
+
const match = /language-(\w+)/.exec(className2 || "");
|
|
385
|
+
const language = match ? match[1] : void 0;
|
|
386
|
+
const code = String(children2).replace(/\n$/, "");
|
|
387
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(CodeBlock, { code, language, className: className2 });
|
|
388
|
+
},
|
|
389
|
+
pre: (props) => {
|
|
390
|
+
const { children: children2, ...rest } = props;
|
|
391
|
+
if (children2?.type === CodeBlock) {
|
|
392
|
+
return children2;
|
|
393
|
+
}
|
|
394
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("pre", { style: { overflow: "visible" }, ...rest, children: children2 });
|
|
395
|
+
},
|
|
396
|
+
p: (props) => {
|
|
397
|
+
const hasCodeBlock = props?.node?.children?.some(
|
|
398
|
+
(child) => child.tagName === "pre" || child.tagName === "code"
|
|
399
|
+
);
|
|
400
|
+
if (hasCodeBlock) {
|
|
401
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: props.children });
|
|
402
|
+
}
|
|
403
|
+
const hasImage = props?.node?.children?.some((child) => child.tagName === "img");
|
|
404
|
+
if (hasImage) return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...props });
|
|
405
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("p", { ...props });
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
if (/<style\b[^>]*>/i.test(markdown)) {
|
|
409
|
+
baseComponents.style = (props) => /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { ...props });
|
|
410
|
+
}
|
|
411
|
+
return { ...baseComponents, ...customComponents };
|
|
412
|
+
}, [markdown, customComponents]);
|
|
413
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
414
|
+
import_react_markdown.default,
|
|
415
|
+
{
|
|
416
|
+
className,
|
|
417
|
+
remarkPlugins: [import_remark_gfm.default],
|
|
418
|
+
components,
|
|
419
|
+
children: displayedText
|
|
420
|
+
}
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// src/components/Message/MessageBlockRenderer.tsx
|
|
425
|
+
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
426
|
+
function MessageBlockRenderer({ block, className }) {
|
|
427
|
+
switch (block.type) {
|
|
428
|
+
case "main_text" /* MAIN_TEXT */:
|
|
429
|
+
case "translation" /* TRANSLATION */:
|
|
430
|
+
case "thinking" /* THINKING */:
|
|
431
|
+
case "error" /* ERROR */:
|
|
432
|
+
case "unknown" /* UNKNOWN */: {
|
|
433
|
+
const textBlock = block;
|
|
434
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
435
|
+
StreamingMarkdown,
|
|
436
|
+
{
|
|
437
|
+
className,
|
|
438
|
+
status: block.status === "streaming" /* STREAMING */ ? "streaming" : "success",
|
|
439
|
+
children: textBlock.content
|
|
440
|
+
}
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
case "code" /* CODE */: {
|
|
444
|
+
const codeBlock = block;
|
|
445
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("pre", { children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("code", { children: codeBlock.content }) }) });
|
|
446
|
+
}
|
|
447
|
+
case "image" /* IMAGE */:
|
|
448
|
+
case "video" /* VIDEO */:
|
|
449
|
+
case "file" /* FILE */: {
|
|
450
|
+
const mediaBlock = block;
|
|
451
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("a", { href: mediaBlock.url, target: "_blank", rel: "noopener noreferrer", children: mediaBlock.name ?? "Media File" }) });
|
|
452
|
+
}
|
|
453
|
+
case "tool" /* TOOL */:
|
|
454
|
+
case "citation" /* CITATION */: {
|
|
455
|
+
const toolBlock = block;
|
|
456
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("pre", { children: JSON.stringify(toolBlock.payload ?? {}, null, 2) }) });
|
|
457
|
+
}
|
|
458
|
+
default:
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
// src/components/Message/MessageItem.tsx
|
|
464
|
+
var import_react4 = require("react");
|
|
465
|
+
var import_react_markdown2 = __toESM(require("react-markdown"));
|
|
466
|
+
var import_remark_gfm2 = __toESM(require("remark-gfm"));
|
|
467
|
+
|
|
468
|
+
// src/utils/markdown/splitMarkdownIntoBlocks.ts
|
|
469
|
+
var blockIdCounter = 0;
|
|
470
|
+
function generateBlockId() {
|
|
471
|
+
blockIdCounter += 1;
|
|
472
|
+
return `block-${Date.now()}-${blockIdCounter}`;
|
|
473
|
+
}
|
|
474
|
+
function splitMarkdownIntoBlocks({
|
|
475
|
+
messageId,
|
|
476
|
+
markdown,
|
|
477
|
+
status = "idle" /* IDLE */
|
|
478
|
+
}) {
|
|
479
|
+
if (!markdown.trim()) {
|
|
480
|
+
return [];
|
|
481
|
+
}
|
|
482
|
+
const block = {
|
|
483
|
+
id: generateBlockId(),
|
|
484
|
+
messageId,
|
|
485
|
+
type: "main_text" /* MAIN_TEXT */,
|
|
486
|
+
status,
|
|
487
|
+
content: markdown,
|
|
488
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
489
|
+
};
|
|
490
|
+
return [block];
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
// src/components/Message/MessageItem.tsx
|
|
494
|
+
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
495
|
+
function MessageItem({
|
|
496
|
+
children,
|
|
497
|
+
role = "assistant",
|
|
498
|
+
messageId,
|
|
499
|
+
className,
|
|
500
|
+
blockClassName
|
|
501
|
+
}) {
|
|
502
|
+
const markdown = typeof children === "string" ? children : String(children ?? "");
|
|
503
|
+
if (role === "user") {
|
|
504
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className, "data-role": "user", children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_react_markdown2.default, { remarkPlugins: [import_remark_gfm2.default], children: markdown }) });
|
|
505
|
+
}
|
|
506
|
+
const generatedId = (0, import_react4.useId)();
|
|
507
|
+
const messageIdRef = messageId ?? generatedId;
|
|
508
|
+
const blocks = (0, import_react4.useMemo)(() => {
|
|
509
|
+
const allBlocks = messageBlockStore.selectAll();
|
|
510
|
+
const oldBlockIds = allBlocks.filter((b) => b.messageId === messageIdRef).map((b) => b.id);
|
|
511
|
+
if (oldBlockIds.length > 0) {
|
|
512
|
+
messageBlockStore.remove(oldBlockIds);
|
|
513
|
+
}
|
|
514
|
+
const newBlocks = splitMarkdownIntoBlocks({
|
|
515
|
+
messageId: messageIdRef,
|
|
516
|
+
markdown,
|
|
517
|
+
status: "idle" /* IDLE */
|
|
518
|
+
});
|
|
519
|
+
messageBlockStore.upsert(newBlocks);
|
|
520
|
+
return newBlocks;
|
|
521
|
+
}, [markdown, messageIdRef]);
|
|
522
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className, "data-message-id": messageIdRef, "data-role": role, children: blocks.map((block) => /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(MessageBlockRenderer, { block, className: blockClassName }, block.id)) });
|
|
523
|
+
}
|
|
524
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
525
|
+
0 && (module.exports = {
|
|
526
|
+
CodeBlock,
|
|
527
|
+
MessageBlockRenderer,
|
|
528
|
+
MessageBlockStatus,
|
|
529
|
+
MessageBlockStore,
|
|
530
|
+
MessageBlockType,
|
|
531
|
+
MessageItem,
|
|
532
|
+
MessageStatus,
|
|
533
|
+
StreamingMarkdown,
|
|
534
|
+
messageBlockStore,
|
|
535
|
+
useShikiHighlight,
|
|
536
|
+
useSmoothStream
|
|
537
|
+
});
|