ai-chat-ui-kit 0.1.0 → 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/LICENSE +21 -0
- package/README.md +4 -7
- package/package.json +8 -4
- package/.eslintrc.cjs +0 -74
- package/.github/actions/screenshot/action.yml +0 -35
- package/.github/workflows/pages.yml +0 -46
- package/docs/README.md +0 -176
- package/docs/api/components.md +0 -344
- package/docs/api/core.md +0 -349
- package/docs/chat-style-1-minimal.html +0 -78
- package/docs/chat-style-2-neon.html +0 -74
- package/docs/chat-style-3-glass.html +0 -73
- package/docs/chat-style-4-terminal.html +0 -84
- package/docs/chat-style-5-gradient.html +0 -69
- package/docs/chat-style-6-corporate.html +0 -116
- package/docs/examples/basic-chat.md +0 -291
- package/docs/examples/custom-plugins.md +0 -431
- package/docs/examples/multi-model.md +0 -466
- package/docs/guide/api-adapters.md +0 -431
- package/docs/guide/getting-started.md +0 -244
- package/docs/guide/headless-mode.md +0 -508
- package/docs/guide/plugins.md +0 -416
- package/docs/guide/themes.md +0 -327
- package/docs/index.html +0 -256
- package/docs/theme-preview-1-minimal.html +0 -74
- package/docs/theme-preview-2-neon.html +0 -73
- package/docs/theme-preview-3-glass.html +0 -77
- package/docs/theme-preview-4-terminal.html +0 -86
- package/docs/theme-preview-5-gradient.html +0 -79
- package/docs/theme-preview-6-corporate.html +0 -71
- package/examples/index.html +0 -414
- package/examples/react-app/App.tsx +0 -131
- package/examples/react-app/index.html +0 -12
- package/examples/react-app/main.tsx +0 -15
- package/examples/react-app/package.json +0 -24
- package/examples/vue-app/index.html +0 -12
- package/examples/vue-app/package.json +0 -22
- package/examples/vue-app/src/App.vue +0 -145
- package/examples/vue-app/src/main.ts +0 -9
- package/packages/components/package.json +0 -25
- package/packages/components/src/chat/chat.css +0 -80
- package/packages/components/src/chat/chat.ts +0 -236
- package/packages/components/src/index.ts +0 -36
- package/packages/components/src/input/input.css +0 -52
- package/packages/components/src/input/input.ts +0 -116
- package/packages/components/src/markdown/markdown.css +0 -118
- package/packages/components/src/markdown/markdown.ts +0 -229
- package/packages/components/src/message/message.css +0 -56
- package/packages/components/src/message/message.ts +0 -72
- package/packages/components/src/styles/global.css +0 -43
- package/packages/components/src/tool-call/tool-call.css +0 -98
- package/packages/components/src/tool-call/tool-call.ts +0 -171
- package/packages/components/src/types.ts +0 -55
- package/packages/components/src/utils/helpers.ts +0 -128
- package/packages/components/tsconfig.json +0 -25
- package/packages/components/tsup.config.ts +0 -18
- package/packages/core/package.json +0 -47
- package/packages/core/pnpm-lock.yaml +0 -2032
- package/packages/core/pnpm-workspace.yaml +0 -2
- package/packages/core/src/api/adapters.ts +0 -717
- package/packages/core/src/api/base.ts +0 -210
- package/packages/core/src/api/index.ts +0 -54
- package/packages/core/src/index.ts +0 -93
- package/packages/core/src/parser/latex.ts +0 -274
- package/packages/core/src/parser/markdown.test.ts +0 -58
- package/packages/core/src/parser/markdown.ts +0 -206
- package/packages/core/src/parser/mermaid.ts +0 -276
- package/packages/core/src/plugins/PluginManager.ts +0 -232
- package/packages/core/src/plugins/builtin.ts +0 -406
- package/packages/core/src/store/ChatStore.ts +0 -163
- package/packages/core/src/store/ModelConfigStore.ts +0 -136
- package/packages/core/src/store/ToolCallStore.ts +0 -164
- package/packages/core/src/store/base.ts +0 -75
- package/packages/core/src/types/index.ts +0 -133
- package/packages/core/tsup.config.ts +0 -18
- package/packages/themes/package.json +0 -33
- package/packages/themes/src/corporate/index.ts +0 -52
- package/packages/themes/src/corporate/theme.css +0 -228
- package/packages/themes/src/glass/index.ts +0 -52
- package/packages/themes/src/glass/theme.css +0 -237
- package/packages/themes/src/gradient/index.ts +0 -53
- package/packages/themes/src/gradient/theme.css +0 -218
- package/packages/themes/src/index.ts +0 -13
- package/packages/themes/src/minimal/index.ts +0 -52
- package/packages/themes/src/minimal/theme.css +0 -198
- package/packages/themes/src/neon/index.ts +0 -52
- package/packages/themes/src/neon/theme.css +0 -233
- package/packages/themes/src/terminal/index.ts +0 -52
- package/packages/themes/src/terminal/theme.css +0 -235
- package/packages/themes/src/types.ts +0 -10
- package/packages/themes/src/vite-env.d.ts +0 -9
- package/packages/themes/tsup.config.ts +0 -21
- package/pnpm-workspace.yaml +0 -4
- package/tsconfig.json +0 -27
- package/vite.config.ts +0 -25
- package/vitest.config.ts +0 -28
package/docs/api/components.md
DELETED
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
# Components 包 API 参考
|
|
2
|
-
|
|
3
|
-
Components 包提供了基于 Lit 的 Web Components 封装,可直接在 HTML 中使用,也可在 React、Vue 等框架中使用。
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
pnpm add @ai-chat/components @ai-chat/themes
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## 组件列表
|
|
12
|
-
|
|
13
|
-
- [`<ai-chat>`](#ai-chat) - 聊天容器组件
|
|
14
|
-
- [`<ai-message>`](#ai-message) - 消息组件
|
|
15
|
-
- [`<ai-input>`](#ai-input) - 输入框组件
|
|
16
|
-
- [`<ai-tool-call>`](#ai-tool-call) - 工具调用组件
|
|
17
|
-
|
|
18
|
-
## `<ai-chat>`
|
|
19
|
-
|
|
20
|
-
聊天容器组件,包含所有子组件。
|
|
21
|
-
|
|
22
|
-
### 属性 (Properties)
|
|
23
|
-
|
|
24
|
-
| 属性 | 类型 | 默认值 | 说明 |
|
|
25
|
-
|------|------|--------|------|
|
|
26
|
-
| `theme` | `string` | `'default'` | 主题名称 (`'default'`, `'bubble'`, `'flat'`) |
|
|
27
|
-
| `loading` | `boolean` | `false` | 是否显示加载状态 |
|
|
28
|
-
| `placeholder` | `string` | `'输入消息...'` | 输入框占位符 |
|
|
29
|
-
| `welcomeMessage` | `string` | `''` | 欢迎消息(AI 发送) |
|
|
30
|
-
|
|
31
|
-
### 方法 (Methods)
|
|
32
|
-
|
|
33
|
-
| 方法 | 参数 | 返回值 | 说明 |
|
|
34
|
-
|------|------|--------|------|
|
|
35
|
-
| `setMessageHandler` | `(handler: (message: string) => Promise<string>)` | `void` | 设置消息处理回调 |
|
|
36
|
-
| `addMessage` | `(message: Message)` | `void` | 添加一条消息 |
|
|
37
|
-
| `clearMessages` | - | `void` | 清除所有消息 |
|
|
38
|
-
| `getMessages` | - | `Message[]` | 获取所有消息 |
|
|
39
|
-
| `updateLastMessage` | `(content: string)` | `void` | 更新最后一条消息(用于流式响应) |
|
|
40
|
-
| `applyTheme` | `(theme: ThemeConfig)` | `void` | 应用主题 |
|
|
41
|
-
|
|
42
|
-
### 事件 (Events)
|
|
43
|
-
|
|
44
|
-
| 事件名 | 事件对象 | 说明 |
|
|
45
|
-
|--------|----------|------|
|
|
46
|
-
| `message-sent` | `{ detail: { message: string } }` | 用户发送消息时触发 |
|
|
47
|
-
| `message-received` | `{ detail: { message: string } }` | 收到 AI 回复时触发 |
|
|
48
|
-
| `loading-start` | - | 开始加载时触发 |
|
|
49
|
-
| `loading-end` | - | 结束加载时触发 |
|
|
50
|
-
| `error` | `{ detail: { error: string } }` | 发生错误时触发 |
|
|
51
|
-
|
|
52
|
-
### 插槽 (Slots)
|
|
53
|
-
|
|
54
|
-
| 插槽名 | 说明 |
|
|
55
|
-
|--------|------|
|
|
56
|
-
| (默认) | 消息列表区域 |
|
|
57
|
-
| `input` | 自定义输入框区域 |
|
|
58
|
-
| `header` | 自定义头部区域 |
|
|
59
|
-
|
|
60
|
-
### 使用示例
|
|
61
|
-
|
|
62
|
-
```html
|
|
63
|
-
<!-- 基本用法 -->
|
|
64
|
-
<ai-chat></ai-chat>
|
|
65
|
-
|
|
66
|
-
<!-- 设置主题 -->
|
|
67
|
-
<ai-chat theme="bubble"></ai-chat>
|
|
68
|
-
|
|
69
|
-
<!-- 设置欢迎消息 -->
|
|
70
|
-
<ai-chat welcome-message="你好!我是 AI 助手。"></ai-chat>
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
```javascript
|
|
74
|
-
// JavaScript 交互
|
|
75
|
-
const chat = document.querySelector('ai-chat');
|
|
76
|
-
|
|
77
|
-
// 设置消息处理回调
|
|
78
|
-
chat.setMessageHandler(async (message) => {
|
|
79
|
-
// 调用您的 AI API
|
|
80
|
-
const response = await fetch('/api/chat', {
|
|
81
|
-
method: 'POST',
|
|
82
|
-
headers: { 'Content-Type': 'application/json' },
|
|
83
|
-
body: JSON.stringify({ message })
|
|
84
|
-
});
|
|
85
|
-
const data = await response.json();
|
|
86
|
-
return data.reply;
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// 添加消息
|
|
90
|
-
chat.addMessage({
|
|
91
|
-
role: 'assistant',
|
|
92
|
-
content: '你好!我是 AI 助手。'
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// 监听事件
|
|
96
|
-
chat.addEventListener('message-sent', (e) => {
|
|
97
|
-
console.log('Message sent:', e.detail.message);
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
chat.addEventListener('message-received', (e) => {
|
|
101
|
-
console.log('Message received:', e.detail.message);
|
|
102
|
-
});
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
### React 中使用
|
|
106
|
-
|
|
107
|
-
```tsx
|
|
108
|
-
import React, { useEffect, useRef } from 'react';
|
|
109
|
-
import '@ai-chat/components';
|
|
110
|
-
import '@ai-chat/themes/default';
|
|
111
|
-
|
|
112
|
-
const ChatApp: React.FC = () => {
|
|
113
|
-
const chatRef = useRef<HTMLElement>(null);
|
|
114
|
-
|
|
115
|
-
useEffect(() => {
|
|
116
|
-
if (chatRef.current) {
|
|
117
|
-
(chatRef.current as any).setMessageHandler(async (message: string) => {
|
|
118
|
-
const response = await fetch('/api/chat', {
|
|
119
|
-
method: 'POST',
|
|
120
|
-
headers: { 'Content-Type': 'application/json' },
|
|
121
|
-
body: JSON.stringify({ message })
|
|
122
|
-
});
|
|
123
|
-
const data = await response.json();
|
|
124
|
-
return data.reply;
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
}, []);
|
|
128
|
-
|
|
129
|
-
return (
|
|
130
|
-
<div style={{ height: '100vh' }}>
|
|
131
|
-
<ai-chat ref={chatRef}></ai-chat>
|
|
132
|
-
</div>
|
|
133
|
-
);
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
export default ChatApp;
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### Vue 3 中使用
|
|
140
|
-
|
|
141
|
-
```vue
|
|
142
|
-
<template>
|
|
143
|
-
<div style="height: 100vh">
|
|
144
|
-
<ai-chat ref="chatRef"></ai-chat>
|
|
145
|
-
</div>
|
|
146
|
-
</template>
|
|
147
|
-
|
|
148
|
-
<script setup lang="ts">
|
|
149
|
-
import { ref, onMounted } from 'vue';
|
|
150
|
-
import '@ai-chat/components';
|
|
151
|
-
import '@ai-chat/themes/default';
|
|
152
|
-
|
|
153
|
-
const chatRef = ref<HTMLElement>();
|
|
154
|
-
|
|
155
|
-
onMounted(() => {
|
|
156
|
-
if (chatRef.value) {
|
|
157
|
-
(chatRef.value as any).setMessageHandler(async (message: string) => {
|
|
158
|
-
const response = await fetch('/api/chat', {
|
|
159
|
-
method: 'POST',
|
|
160
|
-
headers: { 'Content-Type': 'application/json' },
|
|
161
|
-
body: JSON.stringify({ message })
|
|
162
|
-
});
|
|
163
|
-
const data = await response.json();
|
|
164
|
-
return data.reply;
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
});
|
|
168
|
-
</script>
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
## `<ai-message>`
|
|
172
|
-
|
|
173
|
-
消息组件,用于显示单条消息(通常不需要直接使用,由 `<ai-chat>` 内部管理)。
|
|
174
|
-
|
|
175
|
-
### 属性
|
|
176
|
-
|
|
177
|
-
| 属性 | 类型 | 默认值 | 说明 |
|
|
178
|
-
|------|------|--------|------|
|
|
179
|
-
| `role` | `string` | `''` | 消息角色 (`'user'`, `'assistant'`, `'system'`) |
|
|
180
|
-
| `content` | `string` | `''` | 消息内容 |
|
|
181
|
-
| `timestamp` | `number` | `0` | 消息时间戳 |
|
|
182
|
-
| `avatar` | `string` | `''` | 自定义头像 URL 或 emoji |
|
|
183
|
-
|
|
184
|
-
### 插槽
|
|
185
|
-
|
|
186
|
-
| 插槽名 | 说明 |
|
|
187
|
-
|--------|------|
|
|
188
|
-
| (默认) | 消息内容 |
|
|
189
|
-
| `avatar` | 自定义头像 |
|
|
190
|
-
|
|
191
|
-
### 使用示例
|
|
192
|
-
|
|
193
|
-
```html
|
|
194
|
-
<ai-message
|
|
195
|
-
role="user"
|
|
196
|
-
content="Hello!"
|
|
197
|
-
timestamp="1715600000000"
|
|
198
|
-
></ai-message>
|
|
199
|
-
|
|
200
|
-
<ai-message
|
|
201
|
-
role="assistant"
|
|
202
|
-
content="Hi there!"
|
|
203
|
-
></ai-message>
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
## `<ai-input>`
|
|
207
|
-
|
|
208
|
-
输入框组件(通常不需要直接使用,由 `<ai-chat>` 内部管理)。
|
|
209
|
-
|
|
210
|
-
### 属性
|
|
211
|
-
|
|
212
|
-
| 属性 | 类型 | 默认值 | 说明 |
|
|
213
|
-
|------|------|--------|------|
|
|
214
|
-
| `placeholder` | `string` | `'输入消息...'` | 占位符 |
|
|
215
|
-
| `value` | `string` | `''` | 输入框值 |
|
|
216
|
-
| `disabled` | `boolean` | `false` | 是否禁用 |
|
|
217
|
-
| `loading` | `boolean` | `false` | 是否显示加载状态 |
|
|
218
|
-
|
|
219
|
-
### 事件
|
|
220
|
-
|
|
221
|
-
| 事件名 | 说明 |
|
|
222
|
-
|--------|------|
|
|
223
|
-
| `send` | 发送消息时触发 |
|
|
224
|
-
| `input` | 输入内容变化时触发 |
|
|
225
|
-
| `keydown` | 按键时触发 |
|
|
226
|
-
|
|
227
|
-
### 插槽
|
|
228
|
-
|
|
229
|
-
| 插槽名 | 说明 |
|
|
230
|
-
|--------|------|
|
|
231
|
-
| `prefix` | 输入框前缀 |
|
|
232
|
-
| `suffix` | 输入框后缀(默认包含发送按钮) |
|
|
233
|
-
|
|
234
|
-
### 使用示例
|
|
235
|
-
|
|
236
|
-
```html
|
|
237
|
-
<ai-input
|
|
238
|
-
placeholder="输入消息..."
|
|
239
|
-
(send)="onSend($event.detail)"
|
|
240
|
-
></ai-input>
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
## `<ai-tool-call>`
|
|
244
|
-
|
|
245
|
-
工具调用组件,用于显示 AI 调用的工具(如函数调用)。
|
|
246
|
-
|
|
247
|
-
### 属性
|
|
248
|
-
|
|
249
|
-
| 属性 | 类型 | 默认值 | 说明 |
|
|
250
|
-
|------|------|--------|------|
|
|
251
|
-
| `name` | `string` | `''` | 工具名称 |
|
|
252
|
-
| `status` | `string` | `'running'` | 状态 (`'running'`, `'success'`, `'error'`) |
|
|
253
|
-
| `params` | `string` | `''` | 调用参数(JSON 字符串) |
|
|
254
|
-
| `result` | `string` | `''` | 调用结果 |
|
|
255
|
-
|
|
256
|
-
### 插槽
|
|
257
|
-
|
|
258
|
-
| 插槽名 | 说明 |
|
|
259
|
-
|--------|------|
|
|
260
|
-
| `icon` | 自定义图标 |
|
|
261
|
-
| `content` | 自定义内容 |
|
|
262
|
-
|
|
263
|
-
### 使用示例
|
|
264
|
-
|
|
265
|
-
```html
|
|
266
|
-
<ai-tool-call
|
|
267
|
-
name="get_weather"
|
|
268
|
-
status="running"
|
|
269
|
-
params='{"location": "Beijing"}'
|
|
270
|
-
></ai-tool-call>
|
|
271
|
-
|
|
272
|
-
<ai-tool-call
|
|
273
|
-
name="get_weather"
|
|
274
|
-
status="success"
|
|
275
|
-
params='{"location": "Beijing"}'
|
|
276
|
-
result='{"temperature": 25, "condition": "Sunny"}'
|
|
277
|
-
></ai-tool-call>
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
## CSS 变量
|
|
281
|
-
|
|
282
|
-
所有组件都使用 CSS 变量,可在全局或组件级别覆盖。
|
|
283
|
-
|
|
284
|
-
### 完整 CSS 变量列表
|
|
285
|
-
|
|
286
|
-
```css
|
|
287
|
-
:root {
|
|
288
|
-
/* 主色 */
|
|
289
|
-
--ai-primary: #1677ff;
|
|
290
|
-
--ai-primary-hover: #4096ff;
|
|
291
|
-
--ai-primary-active: #0958d9;
|
|
292
|
-
|
|
293
|
-
/* 背景色 */
|
|
294
|
-
--ai-bg-primary: #ffffff;
|
|
295
|
-
--ai-bg-secondary: #f5f5f5;
|
|
296
|
-
--ai-bg-chat: #f0f2f5;
|
|
297
|
-
|
|
298
|
-
/* 文本色 */
|
|
299
|
-
--ai-text-primary: rgba(0, 0, 0, 0.88);
|
|
300
|
-
--ai-text-secondary: rgba(0, 0, 0, 0.65);
|
|
301
|
-
|
|
302
|
-
/* 消息气泡 */
|
|
303
|
-
--ai-message-user-bg: #1677ff;
|
|
304
|
-
--ai-message-user-text: #ffffff;
|
|
305
|
-
--ai-message-ai-bg: #ffffff;
|
|
306
|
-
--ai-message-ai-text: rgba(0, 0, 0, 0.88);
|
|
307
|
-
|
|
308
|
-
/* 输入框 */
|
|
309
|
-
--ai-input-bg: #ffffff;
|
|
310
|
-
--ai-input-border: #d9d9d9;
|
|
311
|
-
--ai-input-focus-border: #1677ff;
|
|
312
|
-
|
|
313
|
-
/* 圆角 */
|
|
314
|
-
--ai-radius-sm: 4px;
|
|
315
|
-
--ai-radius-md: 8px;
|
|
316
|
-
--ai-radius-lg: 12px;
|
|
317
|
-
|
|
318
|
-
/* 间距 */
|
|
319
|
-
--ai-spacing-xs: 4px;
|
|
320
|
-
--ai-spacing-sm: 8px;
|
|
321
|
-
--ai-spacing-md: 12px;
|
|
322
|
-
--ai-spacing-lg: 16px;
|
|
323
|
-
}
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
### 覆盖示例
|
|
327
|
-
|
|
328
|
-
```css
|
|
329
|
-
/* 全局覆盖 */
|
|
330
|
-
:root {
|
|
331
|
-
--ai-primary: #your-color;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
/* 组件级别覆盖 */
|
|
335
|
-
ai-chat {
|
|
336
|
-
--ai-primary: #your-color;
|
|
337
|
-
}
|
|
338
|
-
```
|
|
339
|
-
|
|
340
|
-
## 下一步
|
|
341
|
-
|
|
342
|
-
- [Core 包 API](./core.md)
|
|
343
|
-
- [主题定制](../guide/themes.md)
|
|
344
|
-
- [插件开发](../guide/plugins.md)
|
package/docs/api/core.md
DELETED
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
# Core 包 API 参考
|
|
2
|
-
|
|
3
|
-
Core 包提供了 AI Chat UI Kit 的核心逻辑层,包括状态管理、API 适配器和插件系统。
|
|
4
|
-
|
|
5
|
-
## 安装
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
pnpm add @ai-chat/core
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## 导出
|
|
12
|
-
|
|
13
|
-
```typescript
|
|
14
|
-
// 主要导出
|
|
15
|
-
export { createChatStore } from './store';
|
|
16
|
-
export { createAPIAdapter } from './api';
|
|
17
|
-
export { createPluginSystem } from './plugins';
|
|
18
|
-
export { ChatStore } from './store';
|
|
19
|
-
export { APIAdapter } from './api';
|
|
20
|
-
export { Plugin } from './plugins';
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## `createChatStore(config)`
|
|
24
|
-
|
|
25
|
-
创建一个聊天状态管理实例。
|
|
26
|
-
|
|
27
|
-
### 参数
|
|
28
|
-
|
|
29
|
-
```typescript
|
|
30
|
-
interface ChatStoreConfig {
|
|
31
|
-
// 初始状态(可选)
|
|
32
|
-
initialState?: {
|
|
33
|
-
messages?: Message[];
|
|
34
|
-
isLoading?: boolean;
|
|
35
|
-
error?: string | null;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
// 消息处理回调(必填)
|
|
39
|
-
onMessage: (
|
|
40
|
-
message: string,
|
|
41
|
-
options?: { onChunk?: (chunk: string) => void }
|
|
42
|
-
) => Promise<string>;
|
|
43
|
-
|
|
44
|
-
// 插件列表(可选)
|
|
45
|
-
plugins?: ChatPlugin[];
|
|
46
|
-
|
|
47
|
-
// 全局配置(可选)
|
|
48
|
-
config?: Record<string, any>;
|
|
49
|
-
}
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
### 返回值
|
|
53
|
-
|
|
54
|
-
```typescript
|
|
55
|
-
interface ChatStore {
|
|
56
|
-
// 获取当前消息列表
|
|
57
|
-
getMessages(): Message[];
|
|
58
|
-
|
|
59
|
-
// 获取当前状态
|
|
60
|
-
getState(): ChatState;
|
|
61
|
-
|
|
62
|
-
// 发送消息
|
|
63
|
-
sendMessage(message: string): Promise<void>;
|
|
64
|
-
|
|
65
|
-
// 添加消息(不触发 AI 回复)
|
|
66
|
-
addMessage(message: Message): void;
|
|
67
|
-
|
|
68
|
-
// 更新最后一条消息(用于流式响应)
|
|
69
|
-
updateLastMessage(content: string): void;
|
|
70
|
-
|
|
71
|
-
// 清除所有消息
|
|
72
|
-
clearMessages(): void;
|
|
73
|
-
|
|
74
|
-
// 订阅状态变化
|
|
75
|
-
subscribe(listener: (state: ChatState) => void): () => void;
|
|
76
|
-
|
|
77
|
-
// 注册插件
|
|
78
|
-
registerPlugin(plugin: ChatPlugin): void;
|
|
79
|
-
|
|
80
|
-
// 启用插件
|
|
81
|
-
enablePlugin(name: string): void;
|
|
82
|
-
|
|
83
|
-
// 禁用插件
|
|
84
|
-
disablePlugin(name: string): void;
|
|
85
|
-
}
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### 示例
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
import { createChatStore } from '@ai-chat/core';
|
|
92
|
-
|
|
93
|
-
const chatStore = createChatStore({
|
|
94
|
-
initialState: {
|
|
95
|
-
messages: [
|
|
96
|
-
{ role: 'assistant', content: '你好!我是 AI 助手。' }
|
|
97
|
-
],
|
|
98
|
-
isLoading: false,
|
|
99
|
-
error: null,
|
|
100
|
-
},
|
|
101
|
-
|
|
102
|
-
onMessage: async (message, { onChunk }) => {
|
|
103
|
-
// 调用您的 AI API
|
|
104
|
-
const response = await fetch('/api/chat', {
|
|
105
|
-
method: 'POST',
|
|
106
|
-
headers: { 'Content-Type': 'application/json' },
|
|
107
|
-
body: JSON.stringify({ message })
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
const data = await response.json();
|
|
111
|
-
return data.reply;
|
|
112
|
-
},
|
|
113
|
-
|
|
114
|
-
plugins: [
|
|
115
|
-
// 插件列表
|
|
116
|
-
],
|
|
117
|
-
|
|
118
|
-
config: {
|
|
119
|
-
// 全局配置
|
|
120
|
-
},
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// 使用 store
|
|
124
|
-
chatStore.sendMessage('Hello');
|
|
125
|
-
const messages = chatStore.getMessages();
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
## `createAPIAdapter(config)`
|
|
129
|
-
|
|
130
|
-
创建一个 API 适配器实例(工厂函数)。
|
|
131
|
-
|
|
132
|
-
### 参数
|
|
133
|
-
|
|
134
|
-
```typescript
|
|
135
|
-
type createAPIAdapter = (
|
|
136
|
-
config: {
|
|
137
|
-
type: 'openai' | 'claude' | 'custom';
|
|
138
|
-
apiKey?: string;
|
|
139
|
-
model?: string;
|
|
140
|
-
baseURL?: string;
|
|
141
|
-
[key: string]: any;
|
|
142
|
-
}
|
|
143
|
-
) => APIAdapter;
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### 返回值
|
|
147
|
-
|
|
148
|
-
```typescript
|
|
149
|
-
interface APIAdapter {
|
|
150
|
-
sendMessage(request: ChatRequest): Promise<ChatResponse>;
|
|
151
|
-
sendMessageStream?(request: ChatRequest, onChunk: (chunk: string) => void): Promise<void>;
|
|
152
|
-
getHistory?(): Promise<Message[]>;
|
|
153
|
-
clearHistory?(): Promise<void>;
|
|
154
|
-
}
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
### 示例
|
|
158
|
-
|
|
159
|
-
```typescript
|
|
160
|
-
import { createAPIAdapter } from '@ai-chat/core';
|
|
161
|
-
|
|
162
|
-
// OpenAI
|
|
163
|
-
const openaiAdapter = createAPIAdapter({
|
|
164
|
-
type: 'openai',
|
|
165
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
166
|
-
model: 'gpt-4',
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
// Claude
|
|
170
|
-
const claudeAdapter = createAPIAdapter({
|
|
171
|
-
type: 'claude',
|
|
172
|
-
apiKey: process.env.CLAUDE_API_KEY,
|
|
173
|
-
model: 'claude-3-opus-20240229',
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
// 自定义
|
|
177
|
-
const customAdapter = createAPIAdapter({
|
|
178
|
-
type: 'custom',
|
|
179
|
-
baseURL: 'https://your-api.com',
|
|
180
|
-
token: 'your-token',
|
|
181
|
-
});
|
|
182
|
-
```
|
|
183
|
-
|
|
184
|
-
## `createPluginSystem()`
|
|
185
|
-
|
|
186
|
-
创建一个插件系统实例。
|
|
187
|
-
|
|
188
|
-
### 返回值
|
|
189
|
-
|
|
190
|
-
```typescript
|
|
191
|
-
interface PluginSystem {
|
|
192
|
-
register(plugin: ChatPlugin): void;
|
|
193
|
-
unregister(name: string): void;
|
|
194
|
-
enable(name: string): void;
|
|
195
|
-
disable(name: string): void;
|
|
196
|
-
getPlugin(name: string): ChatPlugin | undefined;
|
|
197
|
-
getAllPlugins(): ChatPlugin[];
|
|
198
|
-
}
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
### 示例
|
|
202
|
-
|
|
203
|
-
```typescript
|
|
204
|
-
import { createPluginSystem, ChatPlugin } from '@ai-chat/core';
|
|
205
|
-
|
|
206
|
-
const pluginSystem = createPluginSystem();
|
|
207
|
-
|
|
208
|
-
// 自定义插件
|
|
209
|
-
class MyPlugin implements ChatPlugin {
|
|
210
|
-
name = 'my-plugin';
|
|
211
|
-
|
|
212
|
-
beforeSend(message: string): string {
|
|
213
|
-
console.log('Sending:', message);
|
|
214
|
-
return message;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// 注册插件
|
|
219
|
-
pluginSystem.register(new MyPlugin());
|
|
220
|
-
|
|
221
|
-
// 使用插件系统
|
|
222
|
-
const chatStore = createChatStore({
|
|
223
|
-
// ...
|
|
224
|
-
plugins: pluginSystem.getAllPlugins(),
|
|
225
|
-
});
|
|
226
|
-
```
|
|
227
|
-
|
|
228
|
-
## 类型定义
|
|
229
|
-
|
|
230
|
-
### `Message`
|
|
231
|
-
|
|
232
|
-
```typescript
|
|
233
|
-
interface Message {
|
|
234
|
-
role: 'user' | 'assistant' | 'system';
|
|
235
|
-
content: string;
|
|
236
|
-
timestamp?: number;
|
|
237
|
-
metadata?: Record<string, any>;
|
|
238
|
-
}
|
|
239
|
-
```
|
|
240
|
-
|
|
241
|
-
### `ChatState`
|
|
242
|
-
|
|
243
|
-
```typescript
|
|
244
|
-
interface ChatState {
|
|
245
|
-
messages: Message[];
|
|
246
|
-
isLoading: boolean;
|
|
247
|
-
error: string | null;
|
|
248
|
-
}
|
|
249
|
-
```
|
|
250
|
-
|
|
251
|
-
### `ChatRequest`
|
|
252
|
-
|
|
253
|
-
```typescript
|
|
254
|
-
interface ChatRequest {
|
|
255
|
-
message: string;
|
|
256
|
-
history?: Message[];
|
|
257
|
-
model?: string;
|
|
258
|
-
temperature?: number;
|
|
259
|
-
maxTokens?: number;
|
|
260
|
-
[key: string]: any;
|
|
261
|
-
}
|
|
262
|
-
```
|
|
263
|
-
|
|
264
|
-
### `ChatResponse`
|
|
265
|
-
|
|
266
|
-
```typescript
|
|
267
|
-
interface ChatResponse {
|
|
268
|
-
content: string;
|
|
269
|
-
role: 'assistant';
|
|
270
|
-
usage?: {
|
|
271
|
-
promptTokens: number;
|
|
272
|
-
completionTokens: number;
|
|
273
|
-
totalTokens: number;
|
|
274
|
-
};
|
|
275
|
-
[key: string]: any;
|
|
276
|
-
}
|
|
277
|
-
```
|
|
278
|
-
|
|
279
|
-
### `ChatPlugin`
|
|
280
|
-
|
|
281
|
-
```typescript
|
|
282
|
-
interface ChatPlugin {
|
|
283
|
-
name: string;
|
|
284
|
-
init?: (context: PluginContext) => void | Promise<void>;
|
|
285
|
-
beforeSend?: (message: string, context: PluginContext) => string | Promise<string>;
|
|
286
|
-
afterSend?: (message: string, context: PluginContext) => void | Promise<void>;
|
|
287
|
-
afterReceive?: (message: string, context: PluginContext) => string | Promise<string>;
|
|
288
|
-
renderUI?: (container: HTMLElement, context: PluginContext) => void;
|
|
289
|
-
destroy?: () => void | Promise<void>;
|
|
290
|
-
}
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
### `PluginContext`
|
|
294
|
-
|
|
295
|
-
```typescript
|
|
296
|
-
interface PluginContext {
|
|
297
|
-
getMessages(): Message[];
|
|
298
|
-
addMessage(message: Message): void;
|
|
299
|
-
getConfig(): Record<string, any>;
|
|
300
|
-
setConfig(config: Record<string, any>): void;
|
|
301
|
-
emit(event: string, data?: any): void;
|
|
302
|
-
on(event: string, handler: (data: any) => void): void;
|
|
303
|
-
}
|
|
304
|
-
```
|
|
305
|
-
|
|
306
|
-
## 使用示例
|
|
307
|
-
|
|
308
|
-
### 完整示例
|
|
309
|
-
|
|
310
|
-
```typescript
|
|
311
|
-
import { createChatStore, createAPIAdapter } from '@ai-chat/core';
|
|
312
|
-
|
|
313
|
-
// 1. 创建 API 适配器
|
|
314
|
-
const adapter = createAPIAdapter({
|
|
315
|
-
type: 'openai',
|
|
316
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
317
|
-
model: 'gpt-3.5-turbo',
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
// 2. 创建 store
|
|
321
|
-
const chatStore = createChatStore({
|
|
322
|
-
onMessage: async (message) => {
|
|
323
|
-
const response = await adapter.sendMessage({
|
|
324
|
-
message,
|
|
325
|
-
history: chatStore.getMessages(),
|
|
326
|
-
});
|
|
327
|
-
return response.content;
|
|
328
|
-
},
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
// 3. 订阅状态变化
|
|
332
|
-
const unsubscribe = chatStore.subscribe((state) => {
|
|
333
|
-
console.log('Messages:', state.messages);
|
|
334
|
-
console.log('Loading:', state.isLoading);
|
|
335
|
-
console.log('Error:', state.error);
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
// 4. 发送消息
|
|
339
|
-
await chatStore.sendMessage('Hello!');
|
|
340
|
-
|
|
341
|
-
// 5. 清理
|
|
342
|
-
unsubscribe();
|
|
343
|
-
```
|
|
344
|
-
|
|
345
|
-
## 下一步
|
|
346
|
-
|
|
347
|
-
- [Components 包 API](./components.md)
|
|
348
|
-
- [插件开发](../guide/plugins.md)
|
|
349
|
-
- [API 适配器](../guide/api-adapters.md)
|