@oakkles/llm-ui-react 0.1.1

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,203 @@
1
+ <div align="center">
2
+
3
+ # @oakkles/llm-ui-react
4
+
5
+ 面向 AI / LLM 产品的 React 组件库,用一套可组合组件快速搭建 Chat、Agent、知识库问答和流式生成界面。
6
+
7
+ [![npm](https://img.shields.io/npm/v/@oakkles/llm-ui-react?color=0f172a&label=npm)](https://www.npmjs.com/package/@oakkles/llm-ui-react)
8
+ ![React](https://img.shields.io/badge/React-18%2B-61DAFB?logo=react&logoColor=white)
9
+ ![TypeScript](https://img.shields.io/badge/TypeScript-ready-3178C6?logo=typescript&logoColor=white)
10
+ ![Storybook](https://img.shields.io/badge/Storybook-docs-FF4785?logo=storybook&logoColor=white)
11
+ ![Package](https://img.shields.io/badge/package-ESM%20%2B%20CJS-111827)
12
+ ![License](https://img.shields.io/npm/l/@oakkles/llm-ui-react)
13
+
14
+ </div>
15
+
16
+ ## 特性
17
+
18
+ | 能力 | 说明 |
19
+ | ---------- | ------------------------------------------------------------------------ |
20
+ | 对话界面 | `Bubble`、`MessageList`、`ConversationList` 组合出完整 Chat UI |
21
+ | 输入体验 | `Sender` 支持受控输入、发送、取消、前缀操作和模型切换入口 |
22
+ | 流式生成 | `useStream`、`mockStream` 和 Markdown streaming 状态适配流式回复 |
23
+ | 富文本输出 | `Mark`、`CodeHighlighter` 支持 Markdown、GFM、代码块和语法高亮 |
24
+ | Agent 过程 | `Think`、`Thought`、`Citation` 展示思考过程、步骤和引用来源 |
25
+ | 主题系统 | 基于 `data-theme` 和 CSS Variables,内置 light / dark 主题和柔和切换动画 |
26
+ | AI Ready | Demo 可接入 `/api/chat` 这类后端接口,安全使用真实模型能力 |
27
+
28
+ ## 安装
29
+
30
+ ```bash
31
+ pnpm add @oakkles/llm-ui-react
32
+ ```
33
+
34
+ 也可以使用 npm:
35
+
36
+ ```bash
37
+ npm install @oakkles/llm-ui-react
38
+ ```
39
+
40
+ ## 快速开始
41
+
42
+ 导入组件和样式:
43
+
44
+ ```tsx
45
+ import { ConfigProvider, MessageList, Sender } from '@oakkles/llm-ui-react'
46
+ import '@oakkles/llm-ui-react/style.css'
47
+
48
+ const messages = [
49
+ {
50
+ id: 'hello-user',
51
+ role: 'user',
52
+ content: '帮我总结今天的发布风险。',
53
+ },
54
+ {
55
+ id: 'hello-assistant',
56
+ role: 'assistant',
57
+ content: '可以从接口稳定性、文档一致性和回归覆盖三个方向拆分。',
58
+ },
59
+ ] as const
60
+
61
+ export function Chat() {
62
+ return (
63
+ <ConfigProvider theme={{ mode: 'light' }} locale="zh-CN">
64
+ <MessageList messages={messages} />
65
+ <Sender onSend={(message) => console.log(message)} />
66
+ </ConfigProvider>
67
+ )
68
+ }
69
+ ```
70
+
71
+ ## 真实 AI Demo 接入
72
+
73
+ 组件库可以发布静态 Demo,也可以在部署到 Vercel 后接入真实 AI 能力。推荐结构是:
74
+
75
+ ```txt
76
+ 浏览器 Demo
77
+ -> fetch('/api/chat')
78
+ -> Vercel Serverless Function
79
+ -> OpenAI-compatible 模型服务
80
+ ```
81
+
82
+ 不要把模型 API Key 放进前端代码、Storybook stories 或任何 `VITE_*` 环境变量。Key 应该只存在于 Vercel 服务端环境变量中,例如:
83
+
84
+ ```txt
85
+ DEEPSEEK_API_KEY=your-server-side-key
86
+ DEEPSEEK_MODEL=deepseek-chat
87
+ ```
88
+
89
+ 前端 Demo 只调用自己的后端接口:
90
+
91
+ ```ts
92
+ async function* requestAI(message: string) {
93
+ const response = await fetch('/api/chat', {
94
+ method: 'POST',
95
+ headers: { 'Content-Type': 'application/json' },
96
+ body: JSON.stringify({ message }),
97
+ })
98
+
99
+ const reader = response.body?.getReader()
100
+ const decoder = new TextDecoder()
101
+
102
+ if (!reader) return
103
+
104
+ while (true) {
105
+ const { done, value } = await reader.read()
106
+ if (done) break
107
+ yield decoder.decode(value)
108
+ }
109
+ }
110
+ ```
111
+
112
+ 这样 npm 包保持安全、可复用,Vercel 上的在线 Demo 仍然可以拥有真实 AI 回复。
113
+
114
+ ## 组件矩阵
115
+
116
+ | 分类 | 组件 |
117
+ | --------------- | -------------------------------------- |
118
+ | 基础上下文 | `ConfigProvider` |
119
+ | 消息展示 | `Bubble`、`MessageList` |
120
+ | 输入与发送 | `Sender` |
121
+ | Markdown 与代码 | `Mark`、`CodeHighlighter` |
122
+ | 会话管理 | `ConversationList`、`ConversationItem` |
123
+ | 快捷操作 | `Prompts`、`Actions` |
124
+ | 推理与引用 | `Think`、`Thought`、`Citation` |
125
+ | 通知反馈 | `Notification`、`NotificationStack` |
126
+
127
+ ## Hooks 与工具
128
+
129
+ | API | 用途 |
130
+ | ------------------- | ---------------------------------- |
131
+ | `useConfig` | 读取全局配置 |
132
+ | `useLocale` | 读取当前语言包 |
133
+ | `useTheme` | 读取和切换主题 |
134
+ | `useStream` | 管理流式文本状态 |
135
+ | `useVirtualList` | 虚拟列表能力 |
136
+ | `mockStream` | 本地模拟流式输出 |
137
+ | `streamToGenerator` | 将 Web Stream 转为 async generator |
138
+ | `generatorToStream` | 将 async generator 转为 Web Stream |
139
+ | `sanitizeMarkdown` | 修复 streaming 阶段不完整 Markdown |
140
+
141
+ ## 主题
142
+
143
+ 主题通过 `data-theme` 属性和 CSS Variables 驱动:
144
+
145
+ ```ts
146
+ document.documentElement.setAttribute('data-theme', 'dark')
147
+ ```
148
+
149
+ 也可以通过 `ConfigProvider` 和 `useTheme` 控制:
150
+
151
+ ```tsx
152
+ <ConfigProvider theme={{ mode: 'dark' }} locale="zh-CN">
153
+ <App />
154
+ </ConfigProvider>
155
+ ```
156
+
157
+ 内置主题文件:
158
+
159
+ ```txt
160
+ src/styles/themes/light.css
161
+ src/styles/themes/dark.css
162
+ src/styles/tokens.css
163
+ ```
164
+
165
+ 所有变量都使用 `--llm-` 前缀,便于外部覆盖。
166
+
167
+ ## 本地开发
168
+
169
+ ```bash
170
+ pnpm install
171
+ pnpm storybook
172
+ ```
173
+
174
+ 常用命令:
175
+
176
+ ```bash
177
+ pnpm lint # ESLint 检查
178
+ pnpm build # 构建库代码、类型声明和 CSS
179
+ pnpm test:storybook # 运行 Storybook browser tests
180
+ pnpm build-storybook # 构建 Storybook 静态站点
181
+ ```
182
+
183
+ ## 发布产物
184
+
185
+ 库构建输出到 `dist/`:
186
+
187
+ ```txt
188
+ dist/index.js # ESM
189
+ dist/index.cjs # CommonJS
190
+ dist/index.d.ts # 类型声明
191
+ dist/style.css # 组件样式
192
+ ```
193
+
194
+ 用户应同时导入组件入口和样式入口:
195
+
196
+ ```tsx
197
+ import { Bubble } from '@oakkles/llm-ui-react'
198
+ import '@oakkles/llm-ui-react/style.css'
199
+ ```
200
+
201
+ ## 在线文档
202
+
203
+ Storybook / Vercel Demo:Coming soon