@huyooo/ai-chat-frontend-react 0.2.17 → 0.2.19

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.
Files changed (2) hide show
  1. package/package.json +4 -4
  2. package/README.md +0 -383
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huyooo/ai-chat-frontend-react",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
4
4
  "description": "AI Chat Frontend - React components with adapter pattern",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -31,8 +31,8 @@
31
31
  "react-dom": ">=18.0.0"
32
32
  },
33
33
  "dependencies": {
34
- "@huyooo/ai-chat-bridge-electron": "^0.2.17",
35
- "@huyooo/ai-chat-shared": "^0.2.17",
34
+ "@huyooo/ai-chat-bridge-electron": "^0.2.19",
35
+ "@huyooo/ai-chat-shared": "^0.2.19",
36
36
  "@iconify/react": "^5.0.2"
37
37
  },
38
38
  "devDependencies": {
@@ -51,7 +51,7 @@
51
51
  "frontend"
52
52
  ],
53
53
  "author": "huyooo",
54
- "license": "MIT",
54
+ "license": "SEE LICENSE IN LICENSE",
55
55
  "publishConfig": {
56
56
  "access": "public"
57
57
  }
package/README.md DELETED
@@ -1,383 +0,0 @@
1
- # @huyooo/ai-chat-frontend-react
2
-
3
- AI Chat 前端组件库 - React 版本。
4
-
5
- > 📖 **深入了解**:查看 [技术架构文档](../../ARCHITECTURE.md) 了解流式渲染、Content Parts 架构、自定义 UI 注入等核心原理。
6
-
7
- ## 安装
8
-
9
- ```bash
10
- npm install @huyooo/ai-chat-frontend-react
11
- ```
12
-
13
- ## 快速开始
14
-
15
- ```tsx
16
- import { ChatPanel } from '@huyooo/ai-chat-frontend-react'
17
- import '@huyooo/ai-chat-frontend-react/style.css'
18
- import { createElectronAdapter } from '@huyooo/ai-chat-bridge-electron/renderer'
19
-
20
- const adapter = createElectronAdapter()
21
-
22
- function App() {
23
- return <ChatPanel adapter={adapter} />
24
- }
25
- ```
26
-
27
- ## 组件
28
-
29
- ### ChatPanel
30
-
31
- 主聊天面板组件。
32
-
33
- ```tsx
34
- <ChatPanel
35
- adapter={adapter}
36
- workingDir="/path/to/dir"
37
- defaultModel="anthropic/claude-opus-4.5"
38
- defaultMode="agent"
39
- models={modelList}
40
- hideHeader={false}
41
- welcomeConfig={welcomeConfig}
42
- partRenderers={partRenderers}
43
- onClose={handleClose}
44
- />
45
- ```
46
-
47
- #### Props
48
-
49
- | 属性 | 类型 | 默认值 | 说明 |
50
- |------|------|--------|------|
51
- | `adapter` | `ChatAdapter` | 必填 | 通信适配器 |
52
- | `workingDir` | `string` | - | 工作目录 |
53
- | `defaultModel` | `string` | `'anthropic/claude-opus-4.5'` | 默认模型 |
54
- | `defaultMode` | `ChatMode` | `'agent'` | 默认模式 |
55
- | `models` | `ModelOption[]` | `[]` | 可用模型列表 |
56
- | `hideHeader` | `boolean` | `false` | 是否隐藏标题栏 |
57
- | `welcomeConfig` | `Partial<WelcomeConfig>` | - | 欢迎页配置 |
58
- | `stepsExpandedType` | `'open' \| 'close' \| 'auto'` | `'auto'` | 执行步骤展开模式 |
59
- | `partRenderers` | `PartRenderers` | `{}` | 自定义 Part 类型渲染器 |
60
- | `autoRunConfig` | `AutoRunConfig` | - | 自动运行配置 |
61
- | `onClose` | `() => void` | - | 关闭回调 |
62
- | `onToolComplete` | `(event: ToolCompleteEvent) => void` | - | 工具执行完成回调 |
63
-
64
- #### stepsExpandedType 说明
65
-
66
- 控制工具执行步骤的默认展开/折叠行为:
67
-
68
- | 值 | 说明 |
69
- |---|------|
70
- | `'open'` | 始终展开所有步骤 |
71
- | `'close'` | 始终折叠所有步骤 |
72
- | `'auto'` | 正在执行的步骤展开,执行完毕自动折叠 |
73
-
74
- #### autoRunConfig 说明
75
-
76
- 自动运行配置用于控制 Agent 的工具执行行为:
77
-
78
- ```typescript
79
- interface AutoRunConfig {
80
- mode?: 'run-everything' | 'manual'; // 自动运行模式
81
- }
82
- ```
83
-
84
- **配置项说明:**
85
-
86
- - **mode**: 自动运行模式
87
- - `'run-everything'`: 运行所有内容(自动执行)
88
- - `'manual'`: 手动批准模式(每次执行前询问)
89
-
90
- **使用示例:**
91
-
92
- ```tsx
93
- <ChatPanel
94
- adapter={adapter}
95
- workingDir={workingDir}
96
- autoRunConfig={{
97
- mode: 'run-everything',
98
- }}
99
- />
100
- ```
101
-
102
- ### WelcomeMessage
103
-
104
- 欢迎页组件,支持自定义配置。
105
-
106
- ```tsx
107
- <WelcomeMessage config={welcomeConfig} onQuickAction={handleQuickAction} />
108
- ```
109
-
110
- ## 欢迎页配置
111
-
112
- 可以通过 `welcomeConfig` 自定义欢迎页的标题、功能列表和快捷任务。
113
-
114
- ```typescript
115
- import type { WelcomeConfig } from '@huyooo/ai-chat-frontend-react'
116
-
117
- const welcomeConfig: Partial<WelcomeConfig> = {
118
- // 标题区域
119
- title: '文件助手',
120
- subtitle: '智能文件管理 · 代码分析',
121
- icon: 'lucide:folder',
122
-
123
- // 功能列表
124
- features: [
125
- { name: '执行命令', icon: 'lucide:terminal' },
126
- { name: '文件分析', icon: 'lucide:file-search' },
127
- { name: '代码生成', icon: 'lucide:code' },
128
- ],
129
-
130
- // 快捷任务
131
- tasks: [
132
- {
133
- name: '列出文件',
134
- desc: '查看目录内容',
135
- prompt: '列出当前目录的文件',
136
- icon: 'lucide:list',
137
- },
138
- {
139
- name: '代码分析',
140
- desc: '分析代码结构',
141
- prompt: '分析这个项目的代码结构',
142
- icon: 'lucide:code',
143
- },
144
- ],
145
- }
146
- ```
147
-
148
- ### 配置接口
149
-
150
- ```typescript
151
- interface WelcomeConfig {
152
- /** 标题 */
153
- title: string
154
- /** 副标题 */
155
- subtitle: string
156
- /** 图标 (Iconify 格式) */
157
- icon: string
158
- /** 功能列表 */
159
- features: WelcomeFeature[]
160
- /** 快捷任务 */
161
- tasks: WelcomeTask[]
162
- }
163
-
164
- interface WelcomeFeature {
165
- name: string
166
- icon: string
167
- }
168
-
169
- interface WelcomeTask {
170
- name: string
171
- desc: string
172
- prompt: string
173
- icon: string
174
- }
175
- ```
176
-
177
- ### 默认配置
178
-
179
- 如果不传 `welcomeConfig`,将使用内置的默认配置。也可以只覆盖部分字段:
180
-
181
- ```typescript
182
- // 只修改标题,其他使用默认
183
- const welcomeConfig = {
184
- title: '我的 AI 助手',
185
- subtitle: '智能对话工具',
186
- }
187
- ```
188
-
189
- 可以导入默认配置作为基础:
190
-
191
- ```typescript
192
- import { defaultWelcomeConfig } from '@huyooo/ai-chat-frontend-react'
193
-
194
- const welcomeConfig = {
195
- ...defaultWelcomeConfig,
196
- title: '自定义标题',
197
- }
198
- ```
199
-
200
- ## 自定义 Part 渲染器(新架构)
201
-
202
- 新架构使用 `partRenderers` 为特定的 Part 类型注入自定义渲染组件。工具执行结果会直接生成对应类型的 Part(如 `weather`),前端根据 `part.type` 选择渲染组件。
203
-
204
- ### 工作原理
205
-
206
- ```
207
- 工具执行完成
208
-
209
- 后端发送 tool_call_result { id, name, result, resultType: 'weather' }
210
-
211
- 前端 useChat 处理:
212
- 1. 更新 tool_call 状态为 done
213
- 2. 根据 resultType 创建 { type: 'weather', ...result } Part
214
-
215
- PartsRenderer 渲染:
216
- - 检查 partRenderers['weather']
217
- - 找到 → 使用自定义 WeatherCard 渲染
218
- - 没找到 → 显示 JSON fallback
219
- ```
220
-
221
- ### 使用示例
222
-
223
- ```tsx
224
- import { useMemo } from 'react'
225
- import { ChatPanel, PartRenderers } from '@huyooo/ai-chat-frontend-react'
226
- import { CustomWeatherCard } from './components/CustomWeatherCard'
227
-
228
- function App() {
229
- // Part 类型 -> 渲染组件映射
230
- const partRenderers = useMemo<PartRenderers>(() => ({
231
- weather: CustomWeatherCard, // weather 类型使用自定义卡片
232
- // stock: CustomStockCard,
233
- }), [])
234
-
235
- return (
236
- <ChatPanel
237
- adapter={adapter}
238
- partRenderers={partRenderers}
239
- />
240
- )
241
- }
242
- ```
243
-
244
- ### 创建自定义渲染组件
245
-
246
- 自定义组件直接接收 Part 的所有字段作为 props:
247
-
248
- ```tsx
249
- // CustomWeatherCard.tsx
250
- import { FC } from 'react'
251
- import './CustomWeatherCard.css'
252
-
253
- interface WeatherPartProps {
254
- type: 'weather' // Part 类型
255
- city: string
256
- temperature: number
257
- condition: string
258
- humidity?: number
259
- wind?: string
260
- reportTime?: string
261
- province?: string
262
- }
263
-
264
- export const CustomWeatherCard: FC<WeatherPartProps> = ({
265
- city,
266
- temperature,
267
- condition,
268
- humidity,
269
- wind,
270
- }) => {
271
- return (
272
- <div className="weather-card">
273
- <div className="city">{city}</div>
274
- <div className="temp">{temperature}°C</div>
275
- <div className="condition">{condition}</div>
276
- <div className="details">
277
- {humidity && <span>湿度 {humidity}%</span>}
278
- {wind && <span>{wind}</span>}
279
- </div>
280
- </div>
281
- )
282
- }
283
- ```
284
-
285
- ### 内置 Part 类型
286
-
287
- | Part 类型 | 说明 | 内置渲染器 |
288
- |-----------|------|-----------|
289
- | `text` | 文本内容 | TextPart(支持 Markdown) |
290
- | `thinking` | 思考过程 | ThinkingPart(可折叠卡片) |
291
- | `search` | 搜索结果 | SearchPart |
292
- | `tool_call` | 工具执行状态 | ToolCallPart(仅显示状态) |
293
- | `image` | 图片 | ImagePart |
294
- | `error` | 错误信息 | ErrorPart |
295
-
296
- ### 扩展 Part 类型
297
-
298
- 可以通过工具的 `resultType` 定义新的 Part 类型:
299
-
300
- ```typescript
301
- // 后端工具定义
302
- export function getWeatherTool(): ToolPlugin {
303
- return tool({
304
- name: 'get_weather',
305
- description: '获取城市天气',
306
- parameters: { city: { type: 'string' } },
307
- resultType: 'weather', // ← 指定结果 Part 类型
308
- execute: async (args, context) => {
309
- // 返回值会自动转换为 { type: 'weather', ...result }
310
- return { city: args.city, temperature: 25, condition: '晴' }
311
- }
312
- })
313
- }
314
- ```
315
-
316
- ## useChat
317
-
318
- Hook 函数,用于自定义 UI。
319
-
320
- ```typescript
321
- import { useChat } from '@huyooo/ai-chat-frontend-react'
322
-
323
- const {
324
- messages,
325
- isLoading,
326
- sendMessage,
327
- regenerateMessage,
328
- abort,
329
- clearHistory,
330
- } = useChat({ adapter })
331
- ```
332
-
333
- ## 导出
334
-
335
- ```typescript
336
- // 主组件
337
- export { ChatPanel, WelcomeMessage, ChatInput, ChatHeader, MessageBubble }
338
-
339
- // Part 渲染组件
340
- export { PartsRenderer }
341
- export {
342
- TextPartComponent,
343
- ThinkingPartComponent,
344
- SearchPartComponent,
345
- ToolCallPartComponent,
346
- ImagePartComponent,
347
- ErrorPartComponent,
348
- }
349
-
350
- // Context Provider
351
- export { PartRenderersProvider, PartRenderersContext }
352
-
353
- // Hook
354
- export { useChat }
355
-
356
- // 类型
357
- export type {
358
- WelcomeConfig,
359
- WelcomeFeature,
360
- WelcomeTask,
361
- PartRenderers,
362
- PartRendererProps,
363
- ContentPart,
364
- TextPart,
365
- ThinkingPart,
366
- SearchPart,
367
- ToolCallPart,
368
- ImagePart,
369
- ErrorPart,
370
- WeatherData,
371
- SearchResultItem,
372
- }
373
-
374
- // 工具函数 (来自 @huyooo/ai-chat-shared)
375
- export { parseContent, highlightCode, getLanguageDisplayName }
376
-
377
- // 默认配置
378
- export { defaultWelcomeConfig }
379
- ```
380
-
381
- ## License
382
-
383
- MIT