@v0-sdk/react 0.1.0

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/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # @v0-sdk/react
2
+
3
+ React components for rendering content from the v0 Platform API.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @v0-sdk/react
9
+ # or
10
+ yarn add @v0-sdk/react
11
+ # or
12
+ pnpm add @v0-sdk/react
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Basic Usage
18
+
19
+ ```tsx
20
+ import { V0MessageRenderer } from '@v0-sdk/react'
21
+
22
+ function ChatMessage({ apiResponse }) {
23
+ // Parse the content from the API response
24
+ const content = JSON.parse(apiResponse.content)
25
+
26
+ return (
27
+ <V0MessageRenderer
28
+ content={content}
29
+ messageId={apiResponse.id}
30
+ role={apiResponse.role}
31
+ />
32
+ )
33
+ }
34
+ ```
35
+
36
+ ### With Streaming
37
+
38
+ ```tsx
39
+ import { V0MessageRenderer } from '@v0-sdk/react'
40
+
41
+ function StreamingMessage({ apiResponse, isStreaming }) {
42
+ const content = JSON.parse(apiResponse.content)
43
+
44
+ return (
45
+ <V0MessageRenderer
46
+ content={content}
47
+ messageId={apiResponse.id}
48
+ role={apiResponse.role}
49
+ streaming={isStreaming}
50
+ isLastMessage={true}
51
+ />
52
+ )
53
+ }
54
+ ```
55
+
56
+ ### Custom Styling
57
+
58
+ ```tsx
59
+ import { V0MessageRenderer } from '@v0-sdk/react'
60
+
61
+ function StyledMessage({ apiResponse }) {
62
+ const content = JSON.parse(apiResponse.content)
63
+
64
+ return (
65
+ <V0MessageRenderer
66
+ content={content}
67
+ messageId={apiResponse.id}
68
+ role={apiResponse.role}
69
+ className="my-custom-message-styles"
70
+ />
71
+ )
72
+ }
73
+ ```
74
+
75
+ ## API Reference
76
+
77
+ ### V0MessageRenderer
78
+
79
+ The main component for rendering v0 Platform API message content.
80
+
81
+ #### Props
82
+
83
+ | Prop | Type | Default | Description |
84
+ | --------------- | --------------------------------------------- | ------------- | ---------------------------------------------------------------------------- |
85
+ | `content` | `MessageBinaryFormat` | **required** | The parsed content from the v0 Platform API (JSON.parse the 'content' field) |
86
+ | `messageId` | `string` | `'unknown'` | Optional message ID for tracking purposes |
87
+ | `role` | `'user' \| 'assistant' \| 'system' \| 'tool'` | `'assistant'` | Role of the message sender |
88
+ | `streaming` | `boolean` | `false` | Whether the message is currently being streamed |
89
+ | `isLastMessage` | `boolean` | `false` | Whether this is the last message in the conversation |
90
+ | `className` | `string` | `undefined` | Custom className for styling |
91
+
92
+ ### Types
93
+
94
+ #### MessageBinaryFormat
95
+
96
+ ```typescript
97
+ type MessageBinaryFormat = [number, ...any[]][]
98
+ ```
99
+
100
+ The binary format for message content as returned by the v0 Platform API. Each row is a tuple where the first element is the type and the rest are data.
101
+
102
+ #### V0MessageRendererProps
103
+
104
+ ```typescript
105
+ interface V0MessageRendererProps {
106
+ content: MessageBinaryFormat
107
+ messageId?: string
108
+ role?: 'user' | 'assistant' | 'system' | 'tool'
109
+ streaming?: boolean
110
+ isLastMessage?: boolean
111
+ className?: string
112
+ }
113
+ ```
114
+
115
+ ## Features
116
+
117
+ ### Supported Content Types
118
+
119
+ - **Markdown/Text Content**: Paragraphs, headings, lists, links, emphasis, code spans, etc.
120
+ - **Code Blocks**: Syntax-highlighted code blocks with support for 25+ programming languages
121
+ - **Mathematical Expressions**: Inline and block math using KaTeX
122
+ - **Extensible**: Easy to extend with additional content types
123
+
124
+ ### Syntax Highlighting
125
+
126
+ Code blocks are automatically syntax-highlighted using Prism.js with support for:
127
+
128
+ - JavaScript/TypeScript
129
+ - Python, Java, C/C++, C#
130
+ - PHP, Ruby, Go, Rust
131
+ - Swift, Kotlin, Scala
132
+ - SQL, JSON, YAML
133
+ - Markdown, CSS/SCSS
134
+ - Bash/Shell scripts
135
+ - And many more...
136
+
137
+ ### Math Rendering
138
+
139
+ Mathematical expressions are rendered using KaTeX:
140
+
141
+ - Inline math: `$E = mc^2$`
142
+ - Block math: `$$\int_{-\infty}^{\infty} e^{-x^2} dx = \sqrt{\pi}$$`
143
+
144
+ ## CSS Styling
145
+
146
+ The component uses Tailwind CSS classes by default. You can:
147
+
148
+ 1. **Use Tailwind CSS**: The component works out of the box with Tailwind
149
+ 2. **Custom CSS**: Override styles using the `className` prop
150
+ 3. **CSS Modules**: Import your own styles and pass via `className`
151
+
152
+ ### Required CSS
153
+
154
+ Make sure to include KaTeX CSS for math rendering:
155
+
156
+ ```css
157
+ @import 'katex/dist/katex.min.css';
158
+ ```
159
+
160
+ Or import it in your JavaScript:
161
+
162
+ ```javascript
163
+ import 'katex/dist/katex.min.css'
164
+ ```
165
+
166
+ ## Examples
167
+
168
+ ### Complete Chat Interface
169
+
170
+ ```tsx
171
+ import { V0MessageRenderer } from '@v0-sdk/react'
172
+ import 'katex/dist/katex.min.css'
173
+
174
+ function ChatInterface({ messages }) {
175
+ return (
176
+ <div className="chat-container">
177
+ {messages.map((message, index) => {
178
+ const content = JSON.parse(message.content)
179
+ const isLast = index === messages.length - 1
180
+
181
+ return (
182
+ <div key={message.id} className="message">
183
+ <div className="message-header">
184
+ <span className="role">{message.role}</span>
185
+ <span className="timestamp">{message.createdAt}</span>
186
+ </div>
187
+ <V0MessageRenderer
188
+ content={content}
189
+ messageId={message.id}
190
+ role={message.role}
191
+ isLastMessage={isLast}
192
+ className="message-content"
193
+ />
194
+ </div>
195
+ )
196
+ })}
197
+ </div>
198
+ )
199
+ }
200
+ ```
201
+
202
+ ### Error Handling
203
+
204
+ ```tsx
205
+ import { V0MessageRenderer } from '@v0-sdk/react'
206
+
207
+ function SafeMessageRenderer({ apiResponse }) {
208
+ try {
209
+ const content = JSON.parse(apiResponse.content)
210
+
211
+ return (
212
+ <V0MessageRenderer
213
+ content={content}
214
+ messageId={apiResponse.id}
215
+ role={apiResponse.role}
216
+ />
217
+ )
218
+ } catch (error) {
219
+ console.error('Failed to parse message content:', error)
220
+
221
+ return <div className="error-message">Failed to render message content</div>
222
+ }
223
+ }
224
+ ```
225
+
226
+ ## Requirements
227
+
228
+ - React 18+ or React 19+
229
+ - Modern browser with ES2020+ support
230
+
231
+ ## License
232
+
233
+ MIT