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 ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2024 Vercel, Inc.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # @stream-md/react
2
+
3
+ React component for rendering streaming markdown content with proper handling of incomplete code blocks.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @stream-md/react
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```tsx
14
+ import { StreamingMarkdown } from '@stream-md/react';
15
+
16
+ function ChatMessage({ content }: { content: string }) {
17
+ return <StreamingMarkdown content={content} />;
18
+ }
19
+ ```
20
+
21
+ ## API
22
+
23
+ ### Props
24
+
25
+ - `content: string` - Markdown content to render (can be incomplete during streaming)
26
+ - `className?: string` - Optional CSS class name
27
+ - `components?: Partial<Components>` - Custom react-markdown components
28
+ - `onComplete?: () => void` - Callback when streaming completes
29
+
30
+ ## Features
31
+
32
+ - ✅ Handles incomplete code blocks during streaming
33
+ - ✅ GitHub Flavored Markdown (tables, strikethrough, etc.)
34
+ - ✅ Type-safe with TypeScript
35
+ - ✅ Zero runtime overhead
36
+ - ✅ Customizable rendering via components prop
37
+
38
+ ## License
39
+
40
+ MIT
@@ -0,0 +1,151 @@
1
+ import { ReactNode } from 'react';
2
+ import { Components } from 'react-markdown';
3
+
4
+ type MessageRole = 'system' | 'user' | 'assistant' | 'tool' | (string & {});
5
+ declare enum MessageStatus {
6
+ IDLE = "idle",
7
+ STREAMING = "streaming",
8
+ SUCCESS = "success",
9
+ ERROR = "error"
10
+ }
11
+ declare enum MessageBlockStatus {
12
+ IDLE = "idle",
13
+ STREAMING = "streaming",
14
+ SUCCESS = "success",
15
+ ERROR = "error"
16
+ }
17
+ declare enum MessageBlockType {
18
+ MAIN_TEXT = "main_text",
19
+ CODE = "code",
20
+ IMAGE = "image",
21
+ FILE = "file",
22
+ TOOL = "tool",
23
+ CITATION = "citation",
24
+ TRANSLATION = "translation",
25
+ THINKING = "thinking",
26
+ VIDEO = "video",
27
+ ERROR = "error",
28
+ UNKNOWN = "unknown"
29
+ }
30
+ interface MessageMetadata {
31
+ [key: string]: unknown;
32
+ }
33
+ interface MessageBlockMetadata {
34
+ [key: string]: unknown;
35
+ }
36
+ interface MessageBlockBase {
37
+ id: string;
38
+ messageId: string;
39
+ type: MessageBlockType;
40
+ status: MessageBlockStatus;
41
+ createdAt: string;
42
+ updatedAt?: string;
43
+ metadata?: MessageBlockMetadata;
44
+ }
45
+ interface TextMessageBlock extends MessageBlockBase {
46
+ content: string;
47
+ type: MessageBlockType.MAIN_TEXT | MessageBlockType.CODE | MessageBlockType.TRANSLATION | MessageBlockType.THINKING | MessageBlockType.ERROR | MessageBlockType.UNKNOWN;
48
+ }
49
+ interface MediaMessageBlock extends MessageBlockBase {
50
+ type: MessageBlockType.IMAGE | MessageBlockType.VIDEO | MessageBlockType.FILE;
51
+ url: string;
52
+ previewUrl?: string;
53
+ name?: string;
54
+ mimeType?: string;
55
+ size?: number;
56
+ content?: string;
57
+ }
58
+ interface ToolMessageBlock extends MessageBlockBase {
59
+ type: MessageBlockType.TOOL | MessageBlockType.CITATION;
60
+ content?: string;
61
+ payload?: Record<string, unknown>;
62
+ }
63
+ type MessageBlock = TextMessageBlock | MediaMessageBlock | ToolMessageBlock;
64
+ interface Message {
65
+ id: string;
66
+ role: MessageRole;
67
+ blockIds: string[];
68
+ status: MessageStatus;
69
+ createdAt: string;
70
+ updatedAt?: string;
71
+ askId?: string;
72
+ metadata?: MessageMetadata;
73
+ }
74
+ type MessageMap = Record<string, Message>;
75
+ type MessageBlockMap = Record<string, MessageBlock>;
76
+
77
+ declare class MessageBlockStore {
78
+ private blocks;
79
+ upsert(block: MessageBlock): void;
80
+ upsert(blocks: MessageBlock[]): void;
81
+ selectById(id: string): MessageBlock | undefined;
82
+ selectMany(ids: string[]): MessageBlock[];
83
+ remove(id: string): void;
84
+ remove(ids: string[]): void;
85
+ clear(): void;
86
+ selectAll(): MessageBlock[];
87
+ toJSON(): Record<string, MessageBlock>;
88
+ }
89
+ declare const messageBlockStore: MessageBlockStore;
90
+
91
+ type StreamingStatus = 'idle' | 'streaming' | 'success' | 'error';
92
+ interface StreamingMarkdownProps {
93
+ children?: ReactNode;
94
+ className?: string;
95
+ components?: Partial<Components>;
96
+ status?: StreamingStatus;
97
+ onComplete?: () => void;
98
+ minDelay?: number;
99
+ blockId?: string;
100
+ }
101
+ declare function StreamingMarkdown({ children, className, components: customComponents, status, onComplete, minDelay, }: StreamingMarkdownProps): ReactNode;
102
+
103
+ interface CodeBlockProps {
104
+ code: string;
105
+ language?: string;
106
+ className?: string;
107
+ theme?: 'light' | 'dark';
108
+ showLineNumbers?: boolean;
109
+ }
110
+ declare function CodeBlock({ code, language, className, theme, }: CodeBlockProps): ReactNode;
111
+
112
+ interface MessageBlockRendererProps {
113
+ block: MessageBlock;
114
+ className?: string;
115
+ }
116
+ declare function MessageBlockRenderer({ block, className }: MessageBlockRendererProps): ReactNode;
117
+
118
+ interface MessageItemProps {
119
+ children?: ReactNode;
120
+ role?: MessageRole;
121
+ messageId?: string;
122
+ className?: string;
123
+ blockClassName?: string;
124
+ }
125
+ declare function MessageItem({ children, role, messageId, className, blockClassName, }: MessageItemProps): ReactNode;
126
+
127
+ interface UseSmoothStreamOptions {
128
+ onUpdate: (text: string) => void;
129
+ streamDone: boolean;
130
+ minDelay?: number;
131
+ initialText?: string;
132
+ onComplete?: () => void;
133
+ }
134
+ declare function useSmoothStream({ onUpdate, streamDone, minDelay, initialText, onComplete, }: UseSmoothStreamOptions): {
135
+ addChunk: (chunk: string) => void;
136
+ reset: (newText?: string) => void;
137
+ };
138
+
139
+ interface UseShikiHighlightOptions {
140
+ code: string;
141
+ language?: string;
142
+ theme?: 'light' | 'dark';
143
+ }
144
+ interface UseShikiHighlightResult {
145
+ html: string;
146
+ isLoading: boolean;
147
+ error: Error | null;
148
+ }
149
+ declare function useShikiHighlight({ code, language, theme, }: UseShikiHighlightOptions): UseShikiHighlightResult;
150
+
151
+ export { CodeBlock, type CodeBlockProps, type MediaMessageBlock, type Message, type MessageBlock, type MessageBlockMap, type MessageBlockMetadata, MessageBlockRenderer, type MessageBlockRendererProps, MessageBlockStatus, MessageBlockStore, MessageBlockType, MessageItem, type MessageItemProps, type MessageMap, type MessageMetadata, type MessageRole, MessageStatus, StreamingMarkdown, type StreamingMarkdownProps, type StreamingStatus, type TextMessageBlock, type ToolMessageBlock, type UseSmoothStreamOptions, messageBlockStore, useShikiHighlight, useSmoothStream };
@@ -0,0 +1,151 @@
1
+ import { ReactNode } from 'react';
2
+ import { Components } from 'react-markdown';
3
+
4
+ type MessageRole = 'system' | 'user' | 'assistant' | 'tool' | (string & {});
5
+ declare enum MessageStatus {
6
+ IDLE = "idle",
7
+ STREAMING = "streaming",
8
+ SUCCESS = "success",
9
+ ERROR = "error"
10
+ }
11
+ declare enum MessageBlockStatus {
12
+ IDLE = "idle",
13
+ STREAMING = "streaming",
14
+ SUCCESS = "success",
15
+ ERROR = "error"
16
+ }
17
+ declare enum MessageBlockType {
18
+ MAIN_TEXT = "main_text",
19
+ CODE = "code",
20
+ IMAGE = "image",
21
+ FILE = "file",
22
+ TOOL = "tool",
23
+ CITATION = "citation",
24
+ TRANSLATION = "translation",
25
+ THINKING = "thinking",
26
+ VIDEO = "video",
27
+ ERROR = "error",
28
+ UNKNOWN = "unknown"
29
+ }
30
+ interface MessageMetadata {
31
+ [key: string]: unknown;
32
+ }
33
+ interface MessageBlockMetadata {
34
+ [key: string]: unknown;
35
+ }
36
+ interface MessageBlockBase {
37
+ id: string;
38
+ messageId: string;
39
+ type: MessageBlockType;
40
+ status: MessageBlockStatus;
41
+ createdAt: string;
42
+ updatedAt?: string;
43
+ metadata?: MessageBlockMetadata;
44
+ }
45
+ interface TextMessageBlock extends MessageBlockBase {
46
+ content: string;
47
+ type: MessageBlockType.MAIN_TEXT | MessageBlockType.CODE | MessageBlockType.TRANSLATION | MessageBlockType.THINKING | MessageBlockType.ERROR | MessageBlockType.UNKNOWN;
48
+ }
49
+ interface MediaMessageBlock extends MessageBlockBase {
50
+ type: MessageBlockType.IMAGE | MessageBlockType.VIDEO | MessageBlockType.FILE;
51
+ url: string;
52
+ previewUrl?: string;
53
+ name?: string;
54
+ mimeType?: string;
55
+ size?: number;
56
+ content?: string;
57
+ }
58
+ interface ToolMessageBlock extends MessageBlockBase {
59
+ type: MessageBlockType.TOOL | MessageBlockType.CITATION;
60
+ content?: string;
61
+ payload?: Record<string, unknown>;
62
+ }
63
+ type MessageBlock = TextMessageBlock | MediaMessageBlock | ToolMessageBlock;
64
+ interface Message {
65
+ id: string;
66
+ role: MessageRole;
67
+ blockIds: string[];
68
+ status: MessageStatus;
69
+ createdAt: string;
70
+ updatedAt?: string;
71
+ askId?: string;
72
+ metadata?: MessageMetadata;
73
+ }
74
+ type MessageMap = Record<string, Message>;
75
+ type MessageBlockMap = Record<string, MessageBlock>;
76
+
77
+ declare class MessageBlockStore {
78
+ private blocks;
79
+ upsert(block: MessageBlock): void;
80
+ upsert(blocks: MessageBlock[]): void;
81
+ selectById(id: string): MessageBlock | undefined;
82
+ selectMany(ids: string[]): MessageBlock[];
83
+ remove(id: string): void;
84
+ remove(ids: string[]): void;
85
+ clear(): void;
86
+ selectAll(): MessageBlock[];
87
+ toJSON(): Record<string, MessageBlock>;
88
+ }
89
+ declare const messageBlockStore: MessageBlockStore;
90
+
91
+ type StreamingStatus = 'idle' | 'streaming' | 'success' | 'error';
92
+ interface StreamingMarkdownProps {
93
+ children?: ReactNode;
94
+ className?: string;
95
+ components?: Partial<Components>;
96
+ status?: StreamingStatus;
97
+ onComplete?: () => void;
98
+ minDelay?: number;
99
+ blockId?: string;
100
+ }
101
+ declare function StreamingMarkdown({ children, className, components: customComponents, status, onComplete, minDelay, }: StreamingMarkdownProps): ReactNode;
102
+
103
+ interface CodeBlockProps {
104
+ code: string;
105
+ language?: string;
106
+ className?: string;
107
+ theme?: 'light' | 'dark';
108
+ showLineNumbers?: boolean;
109
+ }
110
+ declare function CodeBlock({ code, language, className, theme, }: CodeBlockProps): ReactNode;
111
+
112
+ interface MessageBlockRendererProps {
113
+ block: MessageBlock;
114
+ className?: string;
115
+ }
116
+ declare function MessageBlockRenderer({ block, className }: MessageBlockRendererProps): ReactNode;
117
+
118
+ interface MessageItemProps {
119
+ children?: ReactNode;
120
+ role?: MessageRole;
121
+ messageId?: string;
122
+ className?: string;
123
+ blockClassName?: string;
124
+ }
125
+ declare function MessageItem({ children, role, messageId, className, blockClassName, }: MessageItemProps): ReactNode;
126
+
127
+ interface UseSmoothStreamOptions {
128
+ onUpdate: (text: string) => void;
129
+ streamDone: boolean;
130
+ minDelay?: number;
131
+ initialText?: string;
132
+ onComplete?: () => void;
133
+ }
134
+ declare function useSmoothStream({ onUpdate, streamDone, minDelay, initialText, onComplete, }: UseSmoothStreamOptions): {
135
+ addChunk: (chunk: string) => void;
136
+ reset: (newText?: string) => void;
137
+ };
138
+
139
+ interface UseShikiHighlightOptions {
140
+ code: string;
141
+ language?: string;
142
+ theme?: 'light' | 'dark';
143
+ }
144
+ interface UseShikiHighlightResult {
145
+ html: string;
146
+ isLoading: boolean;
147
+ error: Error | null;
148
+ }
149
+ declare function useShikiHighlight({ code, language, theme, }: UseShikiHighlightOptions): UseShikiHighlightResult;
150
+
151
+ export { CodeBlock, type CodeBlockProps, type MediaMessageBlock, type Message, type MessageBlock, type MessageBlockMap, type MessageBlockMetadata, MessageBlockRenderer, type MessageBlockRendererProps, MessageBlockStatus, MessageBlockStore, MessageBlockType, MessageItem, type MessageItemProps, type MessageMap, type MessageMetadata, type MessageRole, MessageStatus, StreamingMarkdown, type StreamingMarkdownProps, type StreamingStatus, type TextMessageBlock, type ToolMessageBlock, type UseSmoothStreamOptions, messageBlockStore, useShikiHighlight, useSmoothStream };