@kweaver-ai/chatkit 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 +528 -0
- package/dist/chatkit.cjs.js +66 -0
- package/dist/chatkit.es.js +2966 -0
- package/dist/components/base/ChatKitBase.d.ts +261 -0
- package/dist/components/base/assistant/AssistantBase.d.ts +17 -0
- package/dist/components/base/assistant/Header.d.ts +20 -0
- package/dist/components/base/assistant/InputArea.d.ts +29 -0
- package/dist/components/base/assistant/MessageItem.d.ts +15 -0
- package/dist/components/base/assistant/MessageList.d.ts +15 -0
- package/dist/components/base/assistant/Prologue.d.ts +18 -0
- package/dist/components/base/assistant/blocks/MarkdownBlock.d.ts +15 -0
- package/dist/components/base/assistant/blocks/TextBlock.d.ts +15 -0
- package/dist/components/base/assistant/blocks/ToolBlock.d.ts +16 -0
- package/dist/components/base/assistant/blocks/ToolDrawer.d.ts +26 -0
- package/dist/components/base/assistant/blocks/WebSearchBlock.d.ts +16 -0
- package/dist/components/base/assistant/blocks/index.d.ts +9 -0
- package/dist/components/base/copilot/CopilotBase.d.ts +16 -0
- package/dist/components/base/copilot/Header.d.ts +22 -0
- package/dist/components/base/copilot/InputArea.d.ts +29 -0
- package/dist/components/base/copilot/MessageItem.d.ts +15 -0
- package/dist/components/base/copilot/MessageList.d.ts +15 -0
- package/dist/components/base/copilot/Prologue.d.ts +18 -0
- package/dist/components/base/copilot/blocks/MarkdownBlock.d.ts +15 -0
- package/dist/components/base/copilot/blocks/TextBlock.d.ts +15 -0
- package/dist/components/base/copilot/blocks/ToolBlock.d.ts +16 -0
- package/dist/components/base/copilot/blocks/ToolDrawer.d.ts +26 -0
- package/dist/components/base/copilot/blocks/WebSearchBlock.d.ts +16 -0
- package/dist/components/base/copilot/blocks/index.d.ts +9 -0
- package/dist/components/coze/Copilot.d.ts +102 -0
- package/dist/components/dip/Assistant.d.ts +22 -0
- package/dist/components/dip/Copilot.d.ts +22 -0
- package/dist/components/dip/DIPBase.d.ts +310 -0
- package/dist/components/icons/ClockIcon.d.ts +6 -0
- package/dist/components/icons/CloseIcon.d.ts +6 -0
- package/dist/components/icons/SendIcon.d.ts +13 -0
- package/dist/components/icons/StopIcon.d.ts +6 -0
- package/dist/components/icons/index.d.ts +7 -0
- package/dist/icons/assistant.svg +14 -0
- package/dist/icons/close.svg +6 -0
- package/dist/icons/expand.svg +3 -0
- package/dist/icons/more.svg +3 -0
- package/dist/icons/new.svg +10 -0
- package/dist/icons/send.svg +4 -0
- package/dist/index.d.ts +20 -0
- package/dist/types/index.d.ts +285 -0
- package/dist/utils/mixins.d.ts +18 -0
- package/package.json +66 -0
package/README.md
ADDED
|
@@ -0,0 +1,528 @@
|
|
|
1
|
+
# ChatKit
|
|
2
|
+
|
|
3
|
+
ChatKit 是一个 AI 对话组件。Web 应用开发者可以将 ChatKit 集成到自己的前端代码中,并通过传入与用户输入有关的上下文对象,实现对 Web 应用中有关的信息发起 AI 对话。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- **多轮对话**: 支持基于会话 ID 的多轮对话,保持上下文连贯性
|
|
8
|
+
- **会话管理**: 支持创建新会话、加载历史会话、删除会话等操作
|
|
9
|
+
- **历史会话**: 支持查看历史会话列表,加载历史对话内容
|
|
10
|
+
- **应用上下文**: 支持注入应用上下文,让 AI 理解用户操作背景
|
|
11
|
+
- **流式响应**: 支持 SSE 流式响应,实现打字机效果
|
|
12
|
+
- **Markdown 渲染**: AI 助手消息支持 Markdown 格式渲染
|
|
13
|
+
- **Web 搜索**: 支持显示 Web 搜索结果
|
|
14
|
+
- **多平台适配**: 支持扣子(Coze)、AISHU DIP 平台等
|
|
15
|
+
- **TypeScript Mixin**: 使用 Mixin 模式实现多重继承,代码复用性强
|
|
16
|
+
|
|
17
|
+
## 项目结构
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
chatkit/
|
|
21
|
+
├── src/ # ChatKit 组件源码
|
|
22
|
+
│ ├── components/ # React 组件
|
|
23
|
+
│ │ ├── base/ # 平台无关的基础组件
|
|
24
|
+
│ │ │ ├── ChatKitBase.tsx # 核心基类组件
|
|
25
|
+
│ │ │ ├── copilot/ # Copilot 模式组件
|
|
26
|
+
│ │ │ │ ├── CopilotBase.tsx # Copilot 基类
|
|
27
|
+
│ │ │ │ ├── Header.tsx # Copilot 头部
|
|
28
|
+
│ │ │ │ ├── MessageList.tsx # Copilot 消息列表
|
|
29
|
+
│ │ │ │ ├── MessageItem.tsx # Copilot 消息项
|
|
30
|
+
│ │ │ │ ├── InputArea.tsx # Copilot 输入区域
|
|
31
|
+
│ │ │ │ ├── Prologue.tsx # Copilot 开场白
|
|
32
|
+
│ │ │ │ └── blocks/ # Copilot 消息块组件
|
|
33
|
+
│ │ │ └── assistant/ # Assistant 模式组件
|
|
34
|
+
│ │ │ ├── AssistantBase.tsx # Assistant 基类
|
|
35
|
+
│ │ │ ├── Header.tsx # Assistant 头部
|
|
36
|
+
│ │ │ ├── MessageList.tsx # Assistant 消息列表
|
|
37
|
+
│ │ │ ├── MessageItem.tsx # Assistant 消息项
|
|
38
|
+
│ │ │ ├── InputArea.tsx # Assistant 输入区域
|
|
39
|
+
│ │ │ ├── Prologue.tsx # Assistant 开场白
|
|
40
|
+
│ │ │ └── blocks/ # Assistant 消息块组件
|
|
41
|
+
│ │ ├── coze/ # 扣子平台适配
|
|
42
|
+
│ │ │ └── Copilot.tsx # 扣子 Copilot 组件
|
|
43
|
+
│ │ ├── dip/ # AISHU DIP 平台适配
|
|
44
|
+
│ │ │ ├── DIPBase.tsx # DIP Mixin (实现 API 层)
|
|
45
|
+
│ │ │ ├── Copilot.tsx # DIP Copilot 组件
|
|
46
|
+
│ │ │ └── Assistant.tsx # DIP Assistant 组件
|
|
47
|
+
│ │ └── icons/ # 图标组件
|
|
48
|
+
│ ├── utils/ # 工具函数
|
|
49
|
+
│ │ └── mixins.ts # TypeScript Mixin 工具
|
|
50
|
+
│ ├── types/ # TypeScript 类型定义
|
|
51
|
+
│ │ └── index.ts # 类型定义文件
|
|
52
|
+
│ ├── styles/ # 样式文件
|
|
53
|
+
│ │ └── index.css # 全局样式
|
|
54
|
+
│ └── index.ts # 导出入口
|
|
55
|
+
├── examples/ # Demo 示例应用
|
|
56
|
+
│ ├── chatkit_coze/ # 扣子 Demo
|
|
57
|
+
│ ├── chatkit_data_agent/ # DIP Demo
|
|
58
|
+
│ └── src/ # Demo 入口
|
|
59
|
+
├── openapi/ # OpenAPI 规范
|
|
60
|
+
│ ├── coze/ # Coze API 规范
|
|
61
|
+
│ │ ├── coze.paths.yaml
|
|
62
|
+
│ │ └── coze.schemas.yaml
|
|
63
|
+
│ └── adp/ # AISHU DIP API 规范
|
|
64
|
+
│ └── agent-app/
|
|
65
|
+
│ ├── agent-app.paths.yaml
|
|
66
|
+
│ └── agent-app.schemas.yaml
|
|
67
|
+
├── design/ # 设计文档
|
|
68
|
+
│ ├── ChatKit.pdf # ChatKit 总体设计
|
|
69
|
+
│ ├── ChatKit for DIP.pdf # DIP 平台设计文档
|
|
70
|
+
│ └── 模块设计.pdf # 模块设计文档
|
|
71
|
+
└── package.json
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## 快速开始
|
|
75
|
+
|
|
76
|
+
### 1. 安装依赖
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npm install
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### 2. 启动开发服务器
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
npm run dev
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
访问 http://localhost:5173/ 查看 demo 应用。
|
|
89
|
+
|
|
90
|
+
### 3. 构建项目
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
npm run build
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### 4. 配置 Coze 接口
|
|
97
|
+
|
|
98
|
+
- 编辑 `examples/chatkit_coze/config.ts`,填入你的 `baseUrl`、`botId`、`apiToken` 和 `userId`。
|
|
99
|
+
- 需要使用 https://www.coze.cn/ 的 Personal Access Token,并确保 Bot ID 与 API Token 均可用。
|
|
100
|
+
|
|
101
|
+
### 5. 配置 AISHU DIP 接口
|
|
102
|
+
|
|
103
|
+
- 编辑 `examples/chatkit_data_agent/config.ts`,填入你的 `baseUrl`、`agentKey`、`token` 和 `businessDomain`。
|
|
104
|
+
- `token` 需要包含 Bearer 前缀,例如:`Bearer ory_at_xxx`。
|
|
105
|
+
|
|
106
|
+
## 使用方法
|
|
107
|
+
|
|
108
|
+
### 使用 Coze Copilot (扣子平台)
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
import React, { useRef } from 'react';
|
|
112
|
+
import { ChatKitCoze } from '@dip/chatkit';
|
|
113
|
+
|
|
114
|
+
function App() {
|
|
115
|
+
const chatKitRef = useRef<ChatKitCoze>(null);
|
|
116
|
+
|
|
117
|
+
// 注入应用上下文
|
|
118
|
+
const injectContext = () => {
|
|
119
|
+
chatKitRef.current?.injectApplicationContext({
|
|
120
|
+
title: '故障节点',
|
|
121
|
+
data: { node_id: 'node-uuid-1' },
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// 创建新会话
|
|
126
|
+
const createNewConversation = () => {
|
|
127
|
+
chatKitRef.current?.createConversation();
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// 发送消息
|
|
131
|
+
const sendMessage = () => {
|
|
132
|
+
chatKitRef.current?.send('帮我分析这个故障', {
|
|
133
|
+
title: '中心节点',
|
|
134
|
+
data: { node_id: 'node-uuid-1' },
|
|
135
|
+
});
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return (
|
|
139
|
+
<div>
|
|
140
|
+
<button onClick={injectContext}>添加上下文</button>
|
|
141
|
+
<button onClick={createNewConversation}>新建会话</button>
|
|
142
|
+
<button onClick={sendMessage}>发送消息</button>
|
|
143
|
+
<ChatKitCoze
|
|
144
|
+
ref={chatKitRef}
|
|
145
|
+
botId="你的Bot ID"
|
|
146
|
+
apiToken="你的API Token"
|
|
147
|
+
title="Copilot"
|
|
148
|
+
visible={true}
|
|
149
|
+
/>
|
|
150
|
+
</div>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### 使用 DIP Copilot (AISHU DIP 平台)
|
|
156
|
+
|
|
157
|
+
```tsx
|
|
158
|
+
import React, { useRef, useState } from 'react';
|
|
159
|
+
import { Copilot, type ApplicationContext } from '@dip/chatkit';
|
|
160
|
+
|
|
161
|
+
function App() {
|
|
162
|
+
const [showChat, setShowChat] = useState(false);
|
|
163
|
+
const chatKitRef = useRef<Copilot>(null);
|
|
164
|
+
|
|
165
|
+
// Token 刷新函数
|
|
166
|
+
const refreshToken = async (): Promise<string> => {
|
|
167
|
+
// 调用您的 token 刷新接口
|
|
168
|
+
const response = await fetch('/api/refresh-token');
|
|
169
|
+
const data = await response.json();
|
|
170
|
+
return data.token;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// 注入上下文示例
|
|
174
|
+
const injectContext = () => {
|
|
175
|
+
chatKitRef.current?.injectApplicationContext({
|
|
176
|
+
title: '故障节点',
|
|
177
|
+
data: { node_id: 'node-uuid-1' },
|
|
178
|
+
});
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// 发送消息示例
|
|
182
|
+
const sendMessage = async () => {
|
|
183
|
+
const context: ApplicationContext = {
|
|
184
|
+
title: '中心节点',
|
|
185
|
+
data: { node_id: 'node-uuid-1' },
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
await chatKitRef.current?.send(
|
|
189
|
+
'节点故障,帮我分析可能的原因并给出解决方案',
|
|
190
|
+
context
|
|
191
|
+
);
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
return (
|
|
195
|
+
<div>
|
|
196
|
+
<button onClick={injectContext}>添加上下文</button>
|
|
197
|
+
<button onClick={sendMessage}>发送消息</button>
|
|
198
|
+
<button onClick={() => setShowChat(!showChat)}>
|
|
199
|
+
{showChat ? '关闭' : '打开'}聊天
|
|
200
|
+
</button>
|
|
201
|
+
|
|
202
|
+
{showChat && (
|
|
203
|
+
<Copilot
|
|
204
|
+
ref={chatKitRef}
|
|
205
|
+
title="DIP Copilot"
|
|
206
|
+
visible={showChat}
|
|
207
|
+
onClose={() => setShowChat(false)}
|
|
208
|
+
baseUrl="https://dip.aishu.cn/api/agent-app/v1"
|
|
209
|
+
agentKey="你的Agent Key"
|
|
210
|
+
token="Bearer your-token"
|
|
211
|
+
refreshToken={refreshToken}
|
|
212
|
+
businessDomain="bd_public"
|
|
213
|
+
/>
|
|
214
|
+
)}
|
|
215
|
+
</div>
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### 使用 DIP Assistant (AISHU DIP 平台)
|
|
221
|
+
|
|
222
|
+
```tsx
|
|
223
|
+
import React, { useRef } from 'react';
|
|
224
|
+
import { Assistant, type ConversationHistory } from '@dip/chatkit';
|
|
225
|
+
|
|
226
|
+
function App() {
|
|
227
|
+
const chatKitRef = useRef<Assistant>(null);
|
|
228
|
+
|
|
229
|
+
// Token 刷新函数
|
|
230
|
+
const refreshToken = async (): Promise<string> => {
|
|
231
|
+
const response = await fetch('/api/refresh-token');
|
|
232
|
+
const data = await response.json();
|
|
233
|
+
return data.token;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
// 获取历史会话列表
|
|
237
|
+
const getHistoryConversations = async () => {
|
|
238
|
+
const conversations = await chatKitRef.current?.getConversations(1, 10);
|
|
239
|
+
console.log('历史会话列表:', conversations);
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
// 加载指定会话
|
|
243
|
+
const loadConversation = async (conversationId: string) => {
|
|
244
|
+
await chatKitRef.current?.loadConversation(conversationId);
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// 删除会话
|
|
248
|
+
const deleteConversation = async (conversationId: string) => {
|
|
249
|
+
await chatKitRef.current?.deleteConversation(conversationId);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
<div className="h-screen">
|
|
254
|
+
<Assistant
|
|
255
|
+
ref={chatKitRef}
|
|
256
|
+
title="DIP Assistant"
|
|
257
|
+
visible={true}
|
|
258
|
+
baseUrl="https://dip.aishu.cn/api/agent-app/v1"
|
|
259
|
+
agentKey="你的Agent Key"
|
|
260
|
+
token="Bearer your-token"
|
|
261
|
+
refreshToken={refreshToken}
|
|
262
|
+
businessDomain="bd_public"
|
|
263
|
+
/>
|
|
264
|
+
</div>
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
## API 文档
|
|
270
|
+
|
|
271
|
+
### ChatKitBase 基类
|
|
272
|
+
|
|
273
|
+
ChatKitBase 是 AI 对话组件的核心基类。开发者不能直接挂载 ChatKitBase,而是需要使用子类(如 Coze Copilot、DIP Copilot、DIP Assistant)。
|
|
274
|
+
|
|
275
|
+
#### 属性 (Props)
|
|
276
|
+
|
|
277
|
+
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|
|
278
|
+
|--------|------|------|--------|------|
|
|
279
|
+
| conversationID | `string` | 否 | `''` | 会话 ID |
|
|
280
|
+
| defaultApplicationContext | `ApplicationContext` | 否 | - | 默认的应用上下文 |
|
|
281
|
+
| title | `string` | 否 | - | 组件标题 |
|
|
282
|
+
| visible | `boolean` | 否 | `true` | 是否显示组件 |
|
|
283
|
+
| onClose | `() => void` | 否 | - | 关闭组件的回调函数 |
|
|
284
|
+
| token | `string` | 否 | - | 访问令牌 |
|
|
285
|
+
| refreshToken | `() => Promise<string>` | 否 | - | Token 刷新函数 |
|
|
286
|
+
|
|
287
|
+
#### 公开方法
|
|
288
|
+
|
|
289
|
+
| 方法名 | 参数 | 返回值 | 说明 |
|
|
290
|
+
|--------|------|--------|------|
|
|
291
|
+
| createConversation | `()` | `Promise<void>` | 创建新的会话,清除现有消息 |
|
|
292
|
+
| loadConversation | `(conversationId: string)` | `Promise<void>` | 加载指定 ID 的历史会话 |
|
|
293
|
+
| send | `(text: string, ctx?: ApplicationContext, conversationID?: string)` | `Promise<ChatMessage>` | 发送消息,支持传入上下文和会话ID |
|
|
294
|
+
| injectApplicationContext | `(ctx: ApplicationContext)` | `void` | 向 ChatKit 注入应用上下文 |
|
|
295
|
+
| removeApplicationContext | `()` | `void` | 移除注入的应用上下文 |
|
|
296
|
+
| getConversations | `(page?: number, size?: number)` | `Promise<ConversationHistory[]>` | 获取历史会话列表 |
|
|
297
|
+
| getConversationMessages | `(conversationId: string)` | `Promise<ChatMessage[]>` | 获取指定会话的消息列表 |
|
|
298
|
+
| deleteConversation | `(conversationId: string)` | `Promise<void>` | 删除指定会话 |
|
|
299
|
+
| terminateConversation | `(conversationId: string)` | `Promise<void>` | 终止指定会话 |
|
|
300
|
+
|
|
301
|
+
### ChatKitCoze (Coze Copilot)
|
|
302
|
+
|
|
303
|
+
扣子(Coze)平台适配组件。
|
|
304
|
+
|
|
305
|
+
#### 额外属性
|
|
306
|
+
|
|
307
|
+
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|
|
308
|
+
|--------|------|------|--------|------|
|
|
309
|
+
| botId | `string` | 是 | - | 扣子 Bot ID |
|
|
310
|
+
| apiToken | `string` | 是 | - | 扣子 API Token |
|
|
311
|
+
| baseUrl | `string` | 否 | `'https://api.coze.cn'` | 扣子 API 基础 URL |
|
|
312
|
+
| userId | `string` | 否 | `'chatkit-user'` | 用户 ID |
|
|
313
|
+
|
|
314
|
+
### Copilot (DIP 平台)
|
|
315
|
+
|
|
316
|
+
AISHU DIP 平台的 Copilot 模式组件。侧边跟随的 AI 助手,为应用提供辅助对话。
|
|
317
|
+
|
|
318
|
+
#### 额外属性
|
|
319
|
+
|
|
320
|
+
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|
|
321
|
+
|--------|------|------|--------|------|
|
|
322
|
+
| baseUrl | `string` | 是 | - | DIP 服务端基础地址 |
|
|
323
|
+
| agentKey | `string` | 是 | - | Agent Key |
|
|
324
|
+
| token | `string` | 是 | - | 访问令牌(需包含 Bearer 前缀) |
|
|
325
|
+
| businessDomain | `string` | 否 | `'bd_public'` | 业务域 |
|
|
326
|
+
| agentVersion | `string` | 否 | `'latest'` | Agent 版本 |
|
|
327
|
+
| executorVersion | `string` | 否 | `'v2'` | 智能体执行引擎版本 |
|
|
328
|
+
| refreshToken | `() => Promise<string>` | 否 | - | Token 刷新函数 |
|
|
329
|
+
|
|
330
|
+
### Assistant (DIP 平台)
|
|
331
|
+
|
|
332
|
+
AISHU DIP 平台的 Assistant 模式组件。作为主交互入口,是应用的主体。支持历史会话管理。
|
|
333
|
+
|
|
334
|
+
#### 额外属性
|
|
335
|
+
|
|
336
|
+
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|
|
337
|
+
|--------|------|------|--------|------|
|
|
338
|
+
| baseUrl | `string` | 是 | - | DIP 服务端基础地址 |
|
|
339
|
+
| agentKey | `string` | 是 | - | Agent Key |
|
|
340
|
+
| token | `string` | 是 | - | 访问令牌(需包含 Bearer 前缀) |
|
|
341
|
+
| businessDomain | `string` | 否 | `'bd_public'` | 业务域 |
|
|
342
|
+
| agentVersion | `string` | 否 | `'latest'` | Agent 版本 |
|
|
343
|
+
| executorVersion | `string` | 否 | `'v2'` | 智能体执行引擎版本 |
|
|
344
|
+
| refreshToken | `() => Promise<string>` | 否 | - | Token 刷新函数 |
|
|
345
|
+
|
|
346
|
+
### 类型定义
|
|
347
|
+
|
|
348
|
+
#### ChatMessage
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
interface ChatMessage {
|
|
352
|
+
/** 消息 ID */
|
|
353
|
+
messageId: string;
|
|
354
|
+
/** 发送该消息的角色 */
|
|
355
|
+
role: Role;
|
|
356
|
+
/** 消息类型(已废弃,保留用于向后兼容) */
|
|
357
|
+
type?: ChatMessageType;
|
|
358
|
+
/** 消息内容,由多个消息块组成 */
|
|
359
|
+
content: Array<TextBlock | MarkdownBlock | WebSearchBlock>;
|
|
360
|
+
/** 与该消息关联的应用上下文(可选)*/
|
|
361
|
+
applicationContext?: ApplicationContext;
|
|
362
|
+
}
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
#### ContentBlock
|
|
366
|
+
|
|
367
|
+
```typescript
|
|
368
|
+
// 文本块
|
|
369
|
+
interface TextBlock {
|
|
370
|
+
type: BlockType.TEXT;
|
|
371
|
+
content: string;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Markdown 块
|
|
375
|
+
interface MarkdownBlock {
|
|
376
|
+
type: BlockType.MARKDOWN;
|
|
377
|
+
content: string;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Web 搜索块
|
|
381
|
+
interface WebSearchBlock {
|
|
382
|
+
type: BlockType.WEB_SEARCH;
|
|
383
|
+
content: WebSearchQuery;
|
|
384
|
+
}
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
#### ApplicationContext
|
|
388
|
+
|
|
389
|
+
```typescript
|
|
390
|
+
interface ApplicationContext {
|
|
391
|
+
/** 显示在输入框上方的应用上下文标题 */
|
|
392
|
+
title: string;
|
|
393
|
+
/** 该应用上下文实际包含的数据 */
|
|
394
|
+
data: any;
|
|
395
|
+
}
|
|
396
|
+
```
|
|
397
|
+
|
|
398
|
+
#### ConversationHistory
|
|
399
|
+
|
|
400
|
+
```typescript
|
|
401
|
+
interface ConversationHistory {
|
|
402
|
+
/** 会话 ID */
|
|
403
|
+
conversationID: string;
|
|
404
|
+
/** 会话标题 */
|
|
405
|
+
title: string;
|
|
406
|
+
/** 会话创建时间(Unix 时间戳) */
|
|
407
|
+
created_at: number;
|
|
408
|
+
/** 会话最后更新时间(Unix 时间戳) */
|
|
409
|
+
updated_at: number;
|
|
410
|
+
/** 最新会话消息下标 */
|
|
411
|
+
message_index?: number;
|
|
412
|
+
/** 最新已读的会话消息下标 */
|
|
413
|
+
read_message_index?: number;
|
|
414
|
+
}
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
#### RoleType
|
|
418
|
+
|
|
419
|
+
```typescript
|
|
420
|
+
enum RoleType {
|
|
421
|
+
/** 用户 */
|
|
422
|
+
USER = 'User',
|
|
423
|
+
/** AI 助手 */
|
|
424
|
+
ASSISTANT = 'Assistant'
|
|
425
|
+
}
|
|
426
|
+
```
|
|
427
|
+
|
|
428
|
+
#### BlockType
|
|
429
|
+
|
|
430
|
+
```typescript
|
|
431
|
+
enum BlockType {
|
|
432
|
+
/** 文本类型 */
|
|
433
|
+
TEXT = 'Text',
|
|
434
|
+
/** Markdown 类型 */
|
|
435
|
+
MARKDOWN = 'Markdown',
|
|
436
|
+
/** Web 搜索类型 */
|
|
437
|
+
WEB_SEARCH = 'WebSearch'
|
|
438
|
+
}
|
|
439
|
+
```
|
|
440
|
+
|
|
441
|
+
### ChatKitInterface 接口
|
|
442
|
+
|
|
443
|
+
如果需要创建自定义的 ChatKit 子类,需要实现 ChatKitInterface 接口中的方法:
|
|
444
|
+
|
|
445
|
+
```typescript
|
|
446
|
+
interface ChatKitInterface {
|
|
447
|
+
/** 获取开场白和预置问题 */
|
|
448
|
+
getOnboardingInfo(): Promise<OnboardingInfo>;
|
|
449
|
+
|
|
450
|
+
/** 新建会话,返回会话 ID */
|
|
451
|
+
generateConversation(): Promise<string>;
|
|
452
|
+
|
|
453
|
+
/** 向后端发送消息 */
|
|
454
|
+
sendMessage(
|
|
455
|
+
text: string,
|
|
456
|
+
ctx: ApplicationContext,
|
|
457
|
+
conversationID?: string
|
|
458
|
+
): Promise<ChatMessage>;
|
|
459
|
+
|
|
460
|
+
/** 解析 EventStreamMessage 并增量更新 AssistantMessage */
|
|
461
|
+
reduceAssistantMessage<T = any, K = any>(
|
|
462
|
+
eventMessage: T,
|
|
463
|
+
prev: K,
|
|
464
|
+
messageId: string
|
|
465
|
+
): K;
|
|
466
|
+
|
|
467
|
+
/** 检查是否需要刷新 token */
|
|
468
|
+
shouldRefreshToken(status: number, error: any): boolean;
|
|
469
|
+
|
|
470
|
+
/** 终止会话 */
|
|
471
|
+
terminateConversation(conversationId: string): Promise<void>;
|
|
472
|
+
|
|
473
|
+
/** 获取历史会话列表 */
|
|
474
|
+
getConversations(page?: number, size?: number): Promise<ConversationHistory[]>;
|
|
475
|
+
|
|
476
|
+
/** 获取指定会话的消息列表 */
|
|
477
|
+
getConversationMessages(conversationId: string): Promise<ChatMessage[]>;
|
|
478
|
+
|
|
479
|
+
/** 删除指定会话 */
|
|
480
|
+
deleteConversation(conversationID: string): Promise<void>;
|
|
481
|
+
}
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
## 架构设计
|
|
485
|
+
|
|
486
|
+
### Mixin 模式
|
|
487
|
+
|
|
488
|
+
ChatKit 使用 TypeScript Mixin 模式实现多重继承,实现了代码的高度复用:
|
|
489
|
+
|
|
490
|
+
```
|
|
491
|
+
┌─────────────────────────────────────────────────┐
|
|
492
|
+
│ Copilot │
|
|
493
|
+
│ (DIP Copilot 组件) │
|
|
494
|
+
└─────────────┬───────────────────────┬───────────┘
|
|
495
|
+
│ │
|
|
496
|
+
│ │
|
|
497
|
+
┌─────────▼──────────┐ ┌────────▼──────────┐
|
|
498
|
+
│ CopilotBase │ │ DIPBaseMixin │
|
|
499
|
+
│ (交互逻辑) │ │ (API 实现) │
|
|
500
|
+
└─────────┬──────────┘ └────────┬──────────┘
|
|
501
|
+
│ │
|
|
502
|
+
└───────────┬───────────┘
|
|
503
|
+
│
|
|
504
|
+
┌───────▼────────┐
|
|
505
|
+
│ ChatKitBase │
|
|
506
|
+
│ (核心基类) │
|
|
507
|
+
└────────────────┘
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
### 组件层次
|
|
511
|
+
|
|
512
|
+
- **ChatKitBase**: 核心基类,定义标准交互逻辑和状态管理
|
|
513
|
+
- **CopilotBase / AssistantBase**: 不同模式的交互界面实现
|
|
514
|
+
- **DIPBaseMixin / CozeBaseMixin**: 平台特定的 API 实现
|
|
515
|
+
- **Copilot / Assistant**: 最终导出的组件,组合基类和 Mixin
|
|
516
|
+
|
|
517
|
+
## 技术栈
|
|
518
|
+
|
|
519
|
+
- **TypeScript** - 类型安全的 JavaScript
|
|
520
|
+
- **React** - UI 框架
|
|
521
|
+
- **Tailwind CSS** - 样式框架
|
|
522
|
+
- **Vite** - 构建工具
|
|
523
|
+
- **react-markdown** - Markdown 渲染
|
|
524
|
+
- **remark-gfm** - GitHub Flavored Markdown 支持
|
|
525
|
+
|
|
526
|
+
## 协议
|
|
527
|
+
|
|
528
|
+
MIT
|