Skip to content

Commit 07dcf74

Browse files
committed
feat: add Chinese documentation and update the English documentation content
1 parent cf505b5 commit 07dcf74

4 files changed

Lines changed: 381 additions & 20 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
<a href="#deploy-your-own"><strong>Deploy Your Own</strong></a> ·
1515
<a href="#running-locally"><strong>Running locally</strong></a>
1616
</p>
17+
<p align="center">
18+
Prefer Chinese docs? <a href="./README.zh-CN.md"><strong>点击查看中文版</strong></a>
19+
</p>
1720
<br/>
1821

1922
## Features

README.zh-CN.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<a href="https://chat.vercel.ai/">
2+
<img alt="Next.js 14 and App Router-ready AI chatbot." src="app/(chat)/opengraph-image.png">
3+
<h1 align="center">Chat SDK</h1>
4+
</a>
5+
6+
<p align="center">
7+
Chat SDK 是一个基于 Next.js 与 AI SDK 的免费开源模板,帮助你迅速构建强大的聊天机器人应用。
8+
</p>
9+
10+
<p align="center">
11+
<a href="https://chat-sdk.dev"><strong>查看文档</strong></a> ·
12+
<a href="#features"><strong>功能特性</strong></a> ·
13+
<a href="#model-providers"><strong>模型供应商</strong></a> ·
14+
<a href="#deploy-your-own"><strong>一键部署</strong></a> ·
15+
<a href="#running-locally"><strong>本地运行</strong></a>
16+
</p>
17+
<p align="center">
18+
English version? <a href="./README.md"><strong>Switch to README.md</strong></a>
19+
</p>
20+
<br/>
21+
22+
## 功能特性
23+
24+
- [Next.js](https://nextjs.org) App Router
25+
- 高级路由能力,带来顺滑的导航体验与更佳性能
26+
- React Server Components (RSC) 与 Server Actions,强化服务端渲染与吞吐
27+
- [AI SDK](https://ai-sdk.dev/docs/introduction)
28+
- 统一 API,方便向 LLM 生成文本、结构化对象或工具调用
29+
- 钩子函数助力搭建动态聊天与生成式 UI
30+
- 支持 xAI(默认)、OpenAI、Fireworks 等多家模型供应商
31+
- [shadcn/ui](https://ui.shadcn.com)
32+
- 基于 [Tailwind CSS](https://tailwindcss.com) 的样式系统
33+
- 使用 [Radix UI](https://radix-ui.com) 组件原语,兼顾可访问性与灵活度
34+
- 数据持久化
35+
- [Neon Serverless Postgres](https://vercel.com/marketplace/neon) 保存聊天记录与用户数据
36+
- [Vercel Blob](https://vercel.com/storage/blob) 用于高效文件存储
37+
- [Auth.js](https://authjs.dev)
38+
- 提供简洁安全的认证方案
39+
40+
## 模型供应商
41+
42+
该模板通过 [Vercel AI Gateway](https://vercel.com/docs/ai-gateway) 统一接入多家模型。默认配置使用网关路由的 [xAI](https://x.ai) 模型(如 `grok-2-vision-1212`, `grok-3-mini`)。
43+
44+
### AI Gateway 鉴权
45+
46+
**部署在 Vercel**:OIDC 令牌会自动处理鉴权。
47+
48+
**非 Vercel 部署**:需要在 `.env.local` 中设置 `AI_GATEWAY_API_KEY` 环境变量以提供网关密钥。
49+
50+
借助 [AI SDK](https://ai-sdk.dev/docs/introduction),你也可以轻松切换至 [OpenAI](https://openai.com)[Anthropic](https://anthropic.com)[Cohere](https://cohere.com/) 等更多 [官方支持的提供方](https://ai-sdk.dev/providers/ai-sdk-providers)
51+
52+
## 一键部署
53+
54+
点击下方按钮即可将 Next.js AI Chatbot 模板一键部署到 Vercel:
55+
56+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/templates/next.js/nextjs-ai-chatbot)
57+
58+
## 本地运行
59+
60+
运行前请参照 [`.env.example`](.env.example) 配置所需环境变量。推荐使用 [Vercel Environment Variables](https://vercel.com/docs/projects/environment-variables),也可直接准备 `.env` 文件。
61+
62+
> 注意:不要提交 `.env` 文件,避免暴露密钥导致他人控制你的 AI 或认证服务。
63+
64+
1. 安装 Vercel CLI:`npm i -g vercel`
65+
2. 将本地代码关联到 Vercel 与 GitHub(会创建 `.vercel` 目录):`vercel link`
66+
3. 拉取环境变量:`vercel env pull`
67+
68+
```bash
69+
pnpm install
70+
pnpm db:migrate # 初始化数据库或迁移至最新版本
71+
pnpm dev
72+
```
73+
74+
现在模板就可以在 [localhost:3000](http://localhost:3000) 访问了。
Lines changed: 152 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,172 @@
1-
# @stream-md/react
1+
# streaming-markdown-react
22

3-
React component for rendering streaming markdown content with proper handling of incomplete code blocks.
3+
React components for streaming-safe Markdown and AI chat interfaces.
4+
5+
[![npm version](https://img.shields.io/npm/v/streaming-markdown-react.svg)](https://www.npmjs.com/package/streaming-markdown-react)
6+
[![npm downloads](https://img.shields.io/npm/dm/streaming-markdown-react.svg)](https://www.npmjs.com/package/streaming-markdown-react)
7+
8+
> Prefer Chinese docs? See [README.zh-CN.md](./README.zh-CN.md).
9+
10+
## Highlights
11+
- **Streaming-safe rendering**: `useSmoothStream` queues graphemes so partially streamed Markdown never breaks code fences or inline structures.
12+
- **Shiki-powered code blocks**: `useShikiHighlight` lazy-loads themes and languages, falling back gracefully while syntax highlighting boots.
13+
- **Message-aware primitives**: `MessageItem`, `MessageBlockRenderer`, and `MessageBlockStore` model complex assistant replies (thinking, tool calls, media, etc.).
14+
- **Highly customizable**: Extend `react-markdown` via the `components` prop, swap the default `CodeBlock`, or plug in your own themes and callbacks.
15+
- **Tiny API surface**: Stream text, toggle `status`, and receive `onComplete` when everything has flushed—no heavy state machines required.
416

517
## Installation
618

719
```bash
8-
pnpm add @stream-md/react
20+
pnpm add streaming-markdown-react
21+
# or
22+
npm install streaming-markdown-react
23+
# or
24+
yarn add streaming-markdown-react
925
```
1026

11-
## Usage
27+
## Basic Usage
1228

1329
```tsx
14-
import { StreamingMarkdown } from '@stream-md/react';
30+
import { StreamingMarkdown, StreamingStatus } from 'streaming-markdown-react';
1531

16-
function ChatMessage({ content }: { content: string }) {
17-
return <StreamingMarkdown content={content} />;
32+
export function MessageBubble({
33+
text,
34+
status,
35+
}: {
36+
text: string;
37+
status: StreamingStatus;
38+
}) {
39+
return (
40+
<StreamingMarkdown
41+
status={status}
42+
className="prose prose-neutral max-w-none"
43+
onComplete={() => console.log('stream finished')}
44+
>
45+
{text}
46+
</StreamingMarkdown>
47+
);
1848
}
1949
```
2050

21-
## API
51+
Pass the latest chunked Markdown through `children`, keep `status="streaming"` until the LLM closes the stream, and use `onComplete` for follow-up UI work once every queued token is painted.
52+
53+
## Streaming Example
54+
55+
```tsx
56+
import { useState, useEffect } from 'react';
57+
import { StreamingMarkdown, StreamingStatus } from 'streaming-markdown-react';
58+
59+
export function LiveAssistantMessage({ stream }: { stream: ReadableStream<string> }) {
60+
const [text, setText] = useState('');
61+
const [status, setStatus] = useState<StreamingStatus>('streaming');
62+
63+
useEffect(() => {
64+
const reader = stream.getReader();
65+
let cancelled = false;
66+
67+
async function read() {
68+
while (!cancelled) {
69+
const { value, done } = await reader.read();
70+
if (done) {
71+
setStatus('success');
72+
break;
73+
}
74+
setText((prev) => prev + (value ?? ''));
75+
}
76+
}
77+
78+
read();
79+
return () => {
80+
cancelled = true;
81+
reader.releaseLock();
82+
};
83+
}, [stream]);
84+
85+
return (
86+
<StreamingMarkdown
87+
status={status}
88+
minDelay={12}
89+
onComplete={() => console.log('assistant block done')}
90+
>
91+
{text}
92+
</StreamingMarkdown>
93+
);
94+
}
95+
```
2296

23-
### Props
97+
`minDelay` throttles animation frames for high-throughput streams, while `status` flips to `'success'` the moment upstream tokenization ends.
2498

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
99+
## Components & Hooks
29100

30-
## Features
101+
| Export | Description |
102+
| --- | --- |
103+
| `StreamingMarkdown` | Streaming-safe Markdown renderer with GFM and overridable components. |
104+
| `StreamingStatus` | `'idle' \| 'streaming' \| 'success' \| 'error'` helper union for UI state. |
105+
| `MessageItem` | Splits assistant responses into typed blocks backed by `MessageBlockStore`. |
106+
| `MessageBlockRenderer` | Default renderer for text, thinking, tool, media, and error blocks. |
107+
| `MessageBlockStore` | Lightweight in-memory store for diffing and hydrating message blocks. |
108+
| `useSmoothStream` | Grapheme-level streaming queue powered by `Intl.Segmenter`. |
109+
| `useShikiHighlight` | Lazy-loaded Shiki highlighter with light/dark themes. |
110+
| `CodeBlock` | Default code block component; wrap or replace it for custom UI. |
31111

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
112+
## StreamingMarkdown Props
113+
114+
| Prop | Type | Description |
115+
| --- | --- | --- |
116+
| `children` | `ReactNode` | Markdown (partial or complete) to render. |
117+
| `className` | `string` | Utility classes for the container. |
118+
| `components` | `Partial<Components>` | Extend/override `react-markdown` element renderers. |
119+
| `status` | `StreamingStatus` | Controls the internal streaming lifecycle. |
120+
| `onComplete` | `() => void` | Fires once the queue drains after the stream finishes. |
121+
| `minDelay` | `number` | Minimum milliseconds between animation frames (default `10`). |
122+
| `blockId` | `string` | Reserved for coordinating multi-block updates. |
123+
124+
## Customization
125+
126+
- **Override Markdown elements**: provide a `components` map to inject callouts, alerts, or custom typography.
127+
128+
```tsx
129+
<StreamingMarkdown
130+
components={{
131+
blockquote: (props) => (
132+
<div className="rounded-lg border-l-4 border-amber-500 bg-amber-50 p-3 text-sm">
133+
{props.children}
134+
</div>
135+
),
136+
}}
137+
>
138+
{text}
139+
</StreamingMarkdown>
140+
```
141+
142+
- **Theme-aware code blocks**: use the exported `CodeBlock` or compose `useShikiHighlight` with your own chrome.
143+
144+
```tsx
145+
import { CodeBlock, useShikiHighlight } from 'streaming-markdown-react';
146+
```
147+
148+
- **Message-first UIs**: `MessageItem` and `MessageBlockRenderer` coordinate per-block rendering so chat transcripts stay in sync during streaming diffs.
149+
150+
## Type-safe Message Blocks
151+
152+
All message-related types (`Message`, `MessageBlock`, `MessageMetadata`, etc.) are exported so your AI pipeline and UI can share a single contract.
153+
154+
```ts
155+
import type { Message, MessageBlockType } from 'streaming-markdown-react';
156+
157+
const assistant: Message = {
158+
id: 'msg-1',
159+
role: 'assistant',
160+
blocks: [
161+
{
162+
id: 'block-1',
163+
type: MessageBlockType.MAIN_TEXT,
164+
content: 'Here is your SQL query...',
165+
},
166+
],
167+
};
168+
```
37169

38170
## License
39171

40-
MIT
172+
MIT © 2024-present. Feel free to use it in production or open-source projects.

0 commit comments

Comments
 (0)