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