@glodon-aiot/bot-client-ui 2.4.8 → 3.0.4
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 +270 -1
- package/dist/bot-client-ui.js +108768 -65775
- package/dist/bot-client-ui.umd.cjs +628 -225
- package/dist/example/knowledges.d.ts +194 -0
- package/package.json +8 -5
package/README.md
CHANGED
|
@@ -1 +1,270 @@
|
|
|
1
|
-
|
|
1
|
+
# 安装依赖
|
|
2
|
+
## 安装依赖
|
|
3
|
+
### npm
|
|
4
|
+
`npm install @glodon-aiot/bot-client-ui`
|
|
5
|
+
|
|
6
|
+
### yarn
|
|
7
|
+
`yarn add @glodon-aiot/bot-client-ui`
|
|
8
|
+
|
|
9
|
+
# 配置
|
|
10
|
+
## 初始化
|
|
11
|
+
```javascript
|
|
12
|
+
import BotClientUI from '@glodon-aiot/bot-client-ui';
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
new BotClientUI({
|
|
16
|
+
token: 'aiot_resource_token_goes_here',
|
|
17
|
+
getContainer: () => document.getElementById('client-ui'),
|
|
18
|
+
mode: 'inlay',
|
|
19
|
+
errorHandlers: {
|
|
20
|
+
token: (error) => {
|
|
21
|
+
console.error(error);
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
// 弃用之前的create方式
|
|
26
|
+
// // dialog - 对话型应用 | agent - Agent应用
|
|
27
|
+
// // 根据平台上应用的类型来创建对应类型的BotClient
|
|
28
|
+
// const BotClient = botClientUI.create("agent"); // 接受 agent 或者 dialog
|
|
29
|
+
|
|
30
|
+
// new BotClient({
|
|
31
|
+
// token: 'aiot_resource_token_goes_here',
|
|
32
|
+
// getContainer: () => document.getElementById('agent-ui'),
|
|
33
|
+
// mode: 'inlay',
|
|
34
|
+
// errorHandlers: {
|
|
35
|
+
// token: (error) => {
|
|
36
|
+
// console.error(error);
|
|
37
|
+
// },
|
|
38
|
+
// },
|
|
39
|
+
// })
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## 创建一个客户端程序的助手
|
|
43
|
+
```javascript
|
|
44
|
+
import BotClientUI from '@glodon-aiot/bot-client-ui';
|
|
45
|
+
|
|
46
|
+
new BotClientUI({
|
|
47
|
+
token: 'aiot_resource_token_goes_here',
|
|
48
|
+
getContainer: () => document.getElementById('agent-ui'),
|
|
49
|
+
mode: 'inlay',
|
|
50
|
+
plugins: {
|
|
51
|
+
pluginACode: window.pluginACode
|
|
52
|
+
},
|
|
53
|
+
errorHandlers: {
|
|
54
|
+
token: (error) => {
|
|
55
|
+
console.error(error);
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
// 弃用之前的create方式
|
|
61
|
+
// dialog - 对话型应用 | agent - Agent应用
|
|
62
|
+
// 根据平台上应用的类型来创建对应类型的BotClient
|
|
63
|
+
// const BotClient = botClientUI.create("agent"); // 接受 agent | dialog
|
|
64
|
+
|
|
65
|
+
// new BotClient({
|
|
66
|
+
// token: 'aiot_resource_token_goes_here',
|
|
67
|
+
// getContainer: () => document.getElementById('agent-ui'),
|
|
68
|
+
// mode: 'inlay',
|
|
69
|
+
// plugins: {
|
|
70
|
+
// pluginACode: window.pluginACode
|
|
71
|
+
// },
|
|
72
|
+
// errorHandlers: {
|
|
73
|
+
// token: (error) => {
|
|
74
|
+
// console.error(error);
|
|
75
|
+
// },
|
|
76
|
+
// },
|
|
77
|
+
// })
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
## Demo工程
|
|
83
|
+
1. 完整的demo代码:<font style="color:#1DC0C9;">(待完善) </font>
|
|
84
|
+
|
|
85
|
+
:::info
|
|
86
|
+
- [ ] demo工程 [@阿如娜](undefined/aruna-1oru6)
|
|
87
|
+
|
|
88
|
+
:::
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
2. 在线预览:
|
|
93
|
+
1. 对话型应用Demo [https://code.juejin.cn/pen/7436299391344607286](https://code.juejin.cn/pen/7436299391344607286)
|
|
94
|
+
2. Agent Demo [https://code.juejin.cn/pen/7372449505322467363](https://code.juejin.cn/pen/7372449505322467363)
|
|
95
|
+
|
|
96
|
+
# 配置
|
|
97
|
+
## 通用配置
|
|
98
|
+
| 名称 | 类型 | 必填 | 默认值 | 说明 |
|
|
99
|
+
| --- | --- | --- | --- | --- |
|
|
100
|
+
| token | string | 是 | - | aiot 行业AI平台的AI应用的resource token。详情查看[链接](https://www.reapi.com/gallery/xAkNoUQjAREnxtgJ/cwyipHfE1wRR7nhN#post-v1-resourceToken) |
|
|
101
|
+
| getContainer | () => Element | 否 | document.body | |
|
|
102
|
+
| mode | 'float' | 'inlay' | 否 | 'float' | 悬浮按钮模式,或者嵌入模式 |
|
|
103
|
+
| header | Element|string | boolean | 否 | 应用名称 | 应用名称 |
|
|
104
|
+
| footer | Element|string | boolean | 否 | | |
|
|
105
|
+
| size | {<br/> width?: number | string;<br/> height?: number | string;<br/> maxWidth?: number | string;<br/> maxHeight?: number | string;<br/> minWidth?: number | string;<br/> minHeight?: number | string;<br/>} | 否 | {width: 450} | float模式下:窗体大小 |
|
|
106
|
+
| open | boolean | 否 | false | float模式下:默认打开状态 |
|
|
107
|
+
| triggerButton | ```typescript { style: string; visible: boolean; } ``` | 否 | {visible: true} | triggerButton设置 |
|
|
108
|
+
| history(<font style="color:#DF2A3F;">即将弃用</font>) | boolean | 否 | true | 是否展示session列表, features.sessionList代替 |
|
|
109
|
+
| features <font style="color:#1DC0C9;">(待完成)</font> | ```typescript interface Feature { greeting?: boolean; sessionList?: boolean; newSession?: boolean;// dialog only fileUpload?: boolean;// dialog only imageUpload?: boolean;//agent only userMessageBox?: {// dialog only copy?: boolean; editAgain?: boolean; }; botMessageBox?: {// dialog only referenceFirst?: boolean; reference?: { file?: boolean; itemExpanded?: boolean; }; relatedQuesions: boolean; footer: { items: { componentName: 'AnswerAgain' | 'Copy' | 'Comments'; position: 'left' | 'right' }[]; }; }; prompts?: boolean; knowledges?: boolean | {visible: boolean, default: Knowledge[]}; promptVariables?: boolean | {visible: boolean, default: SessionPrompt[]}; footer?: HTMLDivElement | string | boolean; } ``` | 否 | ```javascript const FEATURE_DEFAULT = { greeting: true, sessionList: true, newSession: true, fileUpload: true,//dialog only imageUpload: true,//agent only userMessageBox: { copy: true, editAgain: true }, botMessageBox: { referenceFirst: false,//dialog only reference: { file: true, itemExpanded: false }, relatedQuesions: true,// dialog only footer: { items: [{ componentName: 'AnswerAgain', position: 'left', iconOnly: true, visible: true }, { componentName: 'Copy', position: 'right' iconOnly: true, visible: true }, { componentName: 'Comments', position: 'right' iconOnly: true, visible: true }] } }, prompts: true, knowledges: true, promptVariables: true } ``` | 功能配置 |
|
|
110
|
+
| debug | boolean | 否 | false | 1. 控制log输出。<br/>2. promptValues变为非必填项;<br/>3. Agent类型应用下,控制是否开启,debug模式,由于收到权限限制,可能会配置失败。 |
|
|
111
|
+
| eventListeners<font style="color:#1DC0C9;">(待完成)</font> | ```typescript { comment: (payload: { applicationId: string, sessionId: string, comment: Comment }) => void; sessionload: (payload: { applicationId: string, sessionId: string, session: SessionInfo,sessionInstance: Session }) => void; message: (payload: { applicationId: string, sessionId: string, message: Message }) => void; } ``` | | | comment, 用户提交评论<br/>sessionload,加载会话, sessionInstance,请参考`[Session](https://glodon-cv-help.yuque.com/lzh2bp/vit0x5/mgtsbugixgqppqa7#p2B8C)`API。<br/>message,对话内容<br/> |
|
|
112
|
+
| errorHandlers | { <br/>token: (error) => void, promptValues:(error, {key:string,value: string}[]) => void<br/>} | 否 | - | errorHandlers.token 是token验证失败后的处理器;<br/>errorHandlers.promptValues 非debug模式下,检测空值 |
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
## 只适用于对话应用的配置(<font style="color:#DF2A3F;">即将弃用</font>)
|
|
118
|
+
| 名称 | 类型 | 必填 | 默认值 | 说明 |
|
|
119
|
+
| --- | --- | --- | --- | --- |
|
|
120
|
+
| transform(<font style="color:#DF2A3F;">即将弃用, features代替</font>) | ```typescript { greating?: boolean; sessionList?: boolean; sessionOpen?: boolean; fileUpload?: boolean; connectNetwork?: boolean; userCopy?: boolean; userEdit?: boolean; userPrompt?: booleam prompt?: boolean; relate?: boolean; again?: boolean; metadata?: boolen; reference?: { iconVisible?: boolean, referenceFirst?: boolean, isExpand?: boolean, }, }, } ``` | 否 | ```plain { greating: true, // 问候语 sessionList: true, // 会话历史记录 sessionOpen: true, // 打开新会话 fileUpload: true, // 文件上传 connectNetwork: true, // 联网回答 userCopy: true, // 复制 userEdit: true, // 编辑 userPrompt: true, // 添加到prompt prompt: true, // 常用prompt relate: true, // 相关问题 again: true, // 再次回答 metadata: true, // 參考名称来源 reference: { iconVisible: true, // 参考来源icon referenceFirst: false, // 配置参考来源无联网模式下引用顺序 isExpand: false, // 配置参考来源在物联网模式下展开 }, }, } ``` | 对话应用适用,是否支持去除功能 <font style="color:#DF2A3F;">(connectNetwork /metadata 待确认?)</font> |
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## 只适用于Agent应用的配置
|
|
126
|
+
| 名称 | 类型 | 必填 | 默认值 | 说明 |
|
|
127
|
+
| --- | --- | --- | --- | --- |
|
|
128
|
+
| plugins | {[pluginCode:string]:{ [function Code:string]: Function }} | 否 | - | Agent应用适用。<br/>插件列表,`pluginCode`与`functionCode`要与平台中注册的插件标识和API名称保持一致 |
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# API
|
|
132
|
+
| 名称 | 类型 | 说明 |
|
|
133
|
+
| --- | --- | --- |
|
|
134
|
+
| create | (appType: "agent" | "dialog") => `AgentClientUI` | `DialogClientUI` | 创建指定类型的应用UI,配置说明参考[本文第2部分](#qIfRd) |
|
|
135
|
+
| reload | `() => void` | |
|
|
136
|
+
| activeSession | `Session` | 当前活跃的会话信息 |
|
|
137
|
+
| input | `(message:string) => void` | 填充输入框内容 |
|
|
138
|
+
| getMessageList | `(sessionId?: string) => Promise<Message[]>` | 获取历史对话列表 |
|
|
139
|
+
| on<font style="color:#1DC0C9;">(待完成)</font> | | |
|
|
140
|
+
| off<font style="color:#1DC0C9;">(待完成)</font> | | |
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
## 数据结构
|
|
146
|
+
```typescript
|
|
147
|
+
interface SessionInfo {
|
|
148
|
+
id: string;
|
|
149
|
+
name: string;
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
interface Message {
|
|
157
|
+
messageId: string;
|
|
158
|
+
content: string;
|
|
159
|
+
role: ChatMessageRole;
|
|
160
|
+
sessionId: string;
|
|
161
|
+
reference: IReference[];
|
|
162
|
+
searchReference: ISearchRefernce[];
|
|
163
|
+
messageTime: string;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
enum ChatMessageRole {
|
|
167
|
+
Robot = 1,
|
|
168
|
+
User = 2,
|
|
169
|
+
System = 3
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
interface IReference {
|
|
173
|
+
text: string;
|
|
174
|
+
docId: string;
|
|
175
|
+
fileId: string;
|
|
176
|
+
fileName: string;
|
|
177
|
+
knowledgeId: string;
|
|
178
|
+
knowledgeName: string;
|
|
179
|
+
score: number;
|
|
180
|
+
nickname: string;
|
|
181
|
+
knowledgeSourceName: string;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
interface ISearchRefernce {
|
|
185
|
+
link: string;
|
|
186
|
+
title: string;
|
|
187
|
+
snippet: string;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
interface SessionPrompt {
|
|
191
|
+
id?: string;
|
|
192
|
+
key: string;
|
|
193
|
+
name?: string;
|
|
194
|
+
value: string;
|
|
195
|
+
status?: PromptStatus;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
interface Knowledge {
|
|
199
|
+
id: string;
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
# Features
|
|
204
|
+
功能与配置关系如下图所示。
|
|
205
|
+
|
|
206
|
+
当前支持的配置项;
|
|
207
|
+
|
|
208
|
+
有这个标识的配置项是还在计划中。
|
|
209
|
+
|
|
210
|
+

|
|
211
|
+
|
|
212
|
+
# 客户端Agent类型应用的特别说明
|
|
213
|
+
由于该类型应用一般被当作某个软件的助手使用,需要主体软件在浏览器上注册一些方法,作为与浏览器交互的桥梁。
|
|
214
|
+
|
|
215
|
+
例如,有客户端`Plugin A`,注册插件如下代码。
|
|
216
|
+
|
|
217
|
+
其中,`pluginACode`是`<font style="color:rgba(0, 0, 0, 0.85);">插件标识</font>`,可以在[行业AI平台](https://copilot.glodon.com/)的【插件管理 - 插件详情】中获得,不支持自定义。`functionCode`对应的是`API名称`,可自定义。
|
|
218
|
+
|
|
219
|
+
```javascript
|
|
220
|
+
window.pluginACode = {
|
|
221
|
+
functionCode1: (params,cb) => {
|
|
222
|
+
console.log(`模拟pluginA function1 执行\n执行入参\n${JSON.stringify(arguments)}`);
|
|
223
|
+
setTimeout(() => {
|
|
224
|
+
console.log(`模拟pluginA function1 执行完成`);
|
|
225
|
+
cb({
|
|
226
|
+
code: 0,
|
|
227
|
+
message: 'function1运行成功',
|
|
228
|
+
data: {
|
|
229
|
+
a: 'a',
|
|
230
|
+
b: 2,
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
}, 5000)
|
|
234
|
+
},
|
|
235
|
+
functionCode2: (params,cb) => {
|
|
236
|
+
console.log(`模拟pluginA function2 执行\n执行入参\n${JSON.stringify(arguments)}`);
|
|
237
|
+
setTimeout(() => {
|
|
238
|
+
console.log(`模拟pluginA function2 执行完成`);
|
|
239
|
+
cb({
|
|
240
|
+
code: 0,
|
|
241
|
+
message: 'function2运行成功',
|
|
242
|
+
data: {
|
|
243
|
+
a: 'a',
|
|
244
|
+
b: 2,
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
}, 10000)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
创建Agent应用UI的时候,配置`plugins`属性。完整代码如下:
|
|
253
|
+
|
|
254
|
+
```javascript
|
|
255
|
+
import BotClientUI from '@glodon-aiot/bot-client-ui';
|
|
256
|
+
|
|
257
|
+
new BotClientUI({
|
|
258
|
+
token: 'aiot_resource_token_goes_here',
|
|
259
|
+
getContainer: () => document.getElementById('agent-ui'),
|
|
260
|
+
mode: 'inlay',
|
|
261
|
+
plugins: {
|
|
262
|
+
pluginACode: window.pluginACode
|
|
263
|
+
},
|
|
264
|
+
errorHandlers: {
|
|
265
|
+
token: (error) => {
|
|
266
|
+
console.error(error);
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
})
|
|
270
|
+
```
|