@glodon-aiot/bot-client-ui 0.0.0-snapshot-20250729081352

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,403 @@
1
+ # 安装依赖
2
+
3
+ ## npm
4
+
5
+ `npm install @glodon-aiot/bot-client-ui`
6
+
7
+ ## yarn
8
+
9
+ `yarn add @glodon-aiot/bot-client-ui`
10
+
11
+ ## 从cdn引入cjs文件
12
+ 一些项目无法处理esm,请使用以下方式引入依赖。BotClientUI将被注册在window对象上。
13
+ `<script src="https://cv-cdn.obs.cn-north-4.myhuaweicloud.com/glodon/libs/bot-client-ui/3.3.0/bot-client-ui.umd.cjs"></script>`
14
+
15
+ # 快速开始
16
+
17
+ ## 初始化
18
+
19
+ ```javascript
20
+ import BotClientUI from '@glodon-aiot/bot-client-ui';
21
+
22
+ new BotClientUI({
23
+ token: 'aiot_resource_token_goes_here',
24
+ getContainer: () => document.getElementById('client-ui'),
25
+ errorHandlers: {
26
+ token: (error) => {
27
+ console.error(error);
28
+ },
29
+ },
30
+ });
31
+ ```
32
+
33
+ ## 在线预览:
34
+
35
+ - 对话型应用 Demo [https://code.juejin.cn/pen/7494170871938940966](https://code.juejin.cn/pen/7494170871938940966)
36
+ - Agent Demo [https://code.juejin.cn/pen/7372449505322467363](https://code.juejin.cn/pen/7372449505322467363)
37
+
38
+ # BotClientUI 构造配置
39
+
40
+ ## 通用配置
41
+
42
+ | 名称 | 类型 | 必填 | 默认值 | 说明 | 支持版本 |
43
+ | -------------------------------------------------- | ---------------------------- | ---- | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -------- |
44
+ | token | string | 是 | - | aiot 行业 AI 平台的 AI 应用的 resource token。详情查看[认证说明](https://glodon-cv-help.yuque.com/lzh2bp/eszzb5/evk1gvh3pqaexe74?singleDoc# 《认证说明》/cwyipHfE1wRR7nhN#post-v1-resourceToken) | |
45
+ | apiUrl | string | 否 | `https://copilot.glodon.com/api/cvforce` | 拼接规则:`${origin}/api/cvforce`,其中`origin`是部署服务的源地址。 | 3.0.8 |
46
+ | getContainer | () => Element | 否 | () => document.body | 容器元素获取方法,默认在 document.body 中渲染 | |
47
+ | mode | 'float' \| 'inlay' | 否 | 'float' | 悬浮按钮模式,或者嵌入模式 | |
48
+ | header | Element \| string \| boolean | 否 | 应用名称 | 应用名称 | |
49
+ | footer | Element \| string \| boolean | 否 | | | |
50
+ | sider | boolean | 否 | | inlay 模式可用。<br/>是否展示左侧会话列表 | |
51
+ | icon | string | 否 | | 影响greeting和triggerButton的icon | |
52
+ | size | SizeConfig | 否 | {width: 450} | float 模式下:窗体大小 | |
53
+ | open | boolean | 否 | false | float 模式下:默认打开状态 | |
54
+ | triggerButton | TriggerButtonConfig | 否 | {visible: true} | triggerButton 设置 | |
55
+ | features | Feature | 否 | FEATURE_DEFAULT | 功能配置 | |
56
+ | debug | boolean | 否 | false | 1. 控制 log 输出。<br/>2. promptValues 变为非必填项;<br/>3. Agent 类型应用下,控制是否开启,debug 模式,由于收到权限限制,可能会配置失败。 | |
57
+ | eventListeners<font style="color:#1DC0C9;"></font> | EventListeners | | | 事件监听器配置 | |
58
+ | errorHandlers | ErrorHandlers | 否 | - | errorHandlers.token 是 token 验证失败后的处理器;<br/>errorHandlers.promptValues 非 debug 模式下,检测空值 | |
59
+
60
+ ## 只适用于 Agent 应用的配置
61
+
62
+ | 名称 | 类型 | 必填 | 默认值 | 说明 |
63
+ | ------- | ------------- | ---- | ------ | ----------------------------------------------------------------------------------------------------------------- |
64
+ | plugins | PluginsConfig | 否 | - | Agent 应用适用。<br/>插件列表,`pluginCode`与`functionCode`要与平台中注册的插件标识和 API 名称保持一致 |
65
+ | mcp | McpConfig | 否 | - | Agent 应用适用。<br/>这里注册的 MCP 作用到每个会话中。<br/>如果希望特定会话才可注册 MCP,则在会话加载后动态注册。 |
66
+
67
+ # BotClientUI 实例 API
68
+
69
+ | 名称 | 类型 | 说明 |
70
+ | -------------- | -------------------------------------------- | ------------------ |
71
+ | reload | `() => void` | |
72
+ | activeSession | `Session` | 当前活跃的会话信息 |
73
+ | input | `(message:string) => void` | 填充输入框内容 |
74
+ | getMessageList | `(sessionId?: string) => Promise<Message[]>` | 获取历史对话列表 |
75
+
76
+ # 类型定义
77
+
78
+ ## 基础类型
79
+
80
+ ```typescript
81
+ interface SizeConfig {
82
+ width?: number | string;
83
+ height?: number | string;
84
+ maxWidth?: number | string;
85
+ maxHeight?: number | string;
86
+ minWidth?: number | string;
87
+ minHeight?: number | string;
88
+ }
89
+
90
+ interface TriggerButtonConfig {
91
+ style: string;
92
+ visible: boolean;
93
+ }
94
+
95
+ interface SessionInfo {
96
+ id: string;
97
+ name: string;
98
+ }
99
+
100
+ interface Message {
101
+ messageId: string;
102
+ content: string;
103
+ role: ChatMessageRole;
104
+ sessionId: string;
105
+ reference: IReference[];
106
+ searchReference: ISearchRefernce[];
107
+ messageTime: string;
108
+ }
109
+
110
+ enum ChatMessageRole {
111
+ Robot = 1,
112
+ User = 2,
113
+ System = 3,
114
+ }
115
+
116
+ interface IReference {
117
+ text: string;
118
+ docId: string;
119
+ fileId: string;
120
+ fileName: string;
121
+ knowledgeId: string;
122
+ knowledgeName: string;
123
+ score: number;
124
+ nickname: string;
125
+ knowledgeSourceName: string;
126
+ }
127
+
128
+ interface ISearchRefernce {
129
+ link: string;
130
+ title: string;
131
+ snippet: string;
132
+ }
133
+
134
+ interface SessionPrompt {
135
+ id?: string;
136
+ key: string;
137
+ name?: string;
138
+ value: string;
139
+ status?: PromptStatus;
140
+ }
141
+
142
+ interface Knowledge {
143
+ id: string;
144
+ }
145
+ ```
146
+
147
+ ## 功能配置
148
+
149
+ ```typescript
150
+ interface Feature {
151
+ // 问候
152
+ greeting?: boolean | {
153
+ icon?: string;
154
+ text?: string; // 问候语
155
+ questions?: boolean; // 推荐问题
156
+ };
157
+ // toolbar中的"会话历史记录"
158
+ sessionList?: boolean;
159
+ // toolbar中的"新会话"
160
+ newSession?: boolean; // dialog only
161
+ // toolbar中的"上传文件"
162
+ fileUpload?: boolean; // dialog only
163
+ // toolbar中的"上传图片"
164
+ imageUpload?: boolean; // agent only
165
+ // 问题气泡
166
+ userMessageBox?: {
167
+ // dialog only
168
+ copy?: boolean;
169
+ editAgain?: boolean;
170
+ };
171
+ // 回答气泡
172
+ botMessageBox?: {
173
+ // dialog only
174
+ // 参考列表前置
175
+ referenceFirst?: boolean;
176
+ // 参考列表元素设置
177
+ reference?: {
178
+ // 是否可查看参考源文件
179
+ file?: boolean;
180
+ // 是否展开参考内容
181
+ itemExpanded?: boolean;
182
+ };
183
+ // 是否展示相关问题
184
+ relatedQuesions: boolean;
185
+ // 回答气泡底部设置
186
+ footer: {
187
+ items: {
188
+ componentName: 'AnswerAgain' | 'Copy' | 'Comments';
189
+ position: 'left' | 'right';
190
+ }[];
191
+ };
192
+ };
193
+ prompts?: boolean;
194
+ // toolbar中的网络访问
195
+ connectNetwork?: boolean | { visible: boolean; default?: boolean; disabled?: boolean };
196
+ // 参考知识库设置
197
+ knowledges?: boolean | { visible: boolean; default: Knowledge[]; disabled?: boolean };
198
+ // 用户指令变量设置
199
+ promptVariables?: boolean | { visible: boolean; default: SessionPrompt[] };
200
+ }
201
+ ```
202
+
203
+ ## 事件与错误处理
204
+
205
+ ```typescript
206
+ interface EventListeners {
207
+ comment: (payload: { applicationId: string; sessionId: string; comment: Comment }) => void;
208
+ sessionload: (payload: {
209
+ applicationId: string;
210
+ sessionId: string;
211
+ session: SessionInfo;
212
+ sessionInstance: Session;
213
+ }) => void;
214
+ message: (payload: { applicationId: string; sessionId: string; message: Message }) => void;
215
+ beforemessagesend: (payload: {
216
+ applicationId: string;
217
+ sessionId: string;
218
+ message: Message;
219
+ }) => boolean | Partil<Message> | Promise<boolean | Partil<Message>>;
220
+ }
221
+
222
+ interface ErrorHandlers {
223
+ token: (error: any) => void;
224
+ promptValues?: (error: any, values: { key: string; value: string }[]) => void;
225
+ }
226
+ ```
227
+
228
+ ## Agent 应用配置
229
+
230
+ ### 插件配置
231
+
232
+ ```typescript
233
+ interface PluginsConfig {
234
+ [pluginCode: string]: {
235
+ [functionCode: string]: Function;
236
+ };
237
+ }
238
+
239
+ interface McpServer {
240
+ type: 'sse';
241
+ url: string;
242
+ headers?: Record<string, string>;
243
+ }
244
+
245
+ interface McpConfig {
246
+ mcpServers: {
247
+ [key: string]: McpServer;
248
+ };
249
+ }
250
+ ```
251
+
252
+ ### MCP 配置
253
+
254
+ ```typescript
255
+ interface McpServer {
256
+ type: 'sse';
257
+ url: string;
258
+ headers?: Record<string, string>;
259
+ }
260
+
261
+ interface McpConfig {
262
+ mcpServers: {
263
+ [key: string]: McpServer;
264
+ };
265
+ }
266
+ ```
267
+
268
+ # Agent 类型应用的特别说明
269
+
270
+ ## 客户端插件使用
271
+
272
+ 由于该类型应用一般被当作某个软件的助手使用,需要主体软件在浏览器上注册一些方法,作为与浏览器交互的桥梁。
273
+
274
+ 例如,有客户端`Plugin A`,注册插件如下代码。
275
+
276
+ 其中,`pluginACode`是插件标识,可以在[行业 AI 平台](https://copilot.glodon.com/)的【插件管理 - 插件详情】中获得,不支持自定义。`functionCode`对应的是`API名称`,可自定义。
277
+
278
+ ```javascript
279
+ window.pluginACode = {
280
+ functionCode1: (params, cb) => {
281
+ console.log(`模拟pluginA function1 执行\n执行入参\n${JSON.stringify(arguments)}`);
282
+ setTimeout(() => {
283
+ console.log(`模拟pluginA function1 执行完成`);
284
+ cb({
285
+ code: 0,
286
+ message: 'function1运行成功',
287
+ data: {
288
+ a: 'a',
289
+ b: 2,
290
+ },
291
+ });
292
+ }, 5000);
293
+ },
294
+ functionCode2: (params, cb) => {
295
+ console.log(`模拟pluginA function2 执行\n执行入参\n${JSON.stringify(arguments)}`);
296
+ setTimeout(() => {
297
+ console.log(`模拟pluginA function2 执行完成`);
298
+ cb({
299
+ code: 0,
300
+ message: 'function2运行成功',
301
+ data: {
302
+ a: 'a',
303
+ b: 2,
304
+ },
305
+ });
306
+ }, 10000);
307
+ },
308
+ };
309
+ ```
310
+
311
+ 创建 Agent 应用 UI 的时候,配置`plugins`属性。完整代码如下:
312
+
313
+ ```javascript
314
+ import BotClientUI from '@glodon-aiot/bot-client-ui';
315
+
316
+ new BotClientUI({
317
+ token: 'aiot_resource_token_goes_here',
318
+ getContainer: () => document.getElementById('agent-ui'),
319
+ mode: 'inlay',
320
+ plugins: {
321
+ pluginACode: window.pluginACode,
322
+ },
323
+ errorHandlers: {
324
+ token: (error) => {
325
+ console.error(error);
326
+ },
327
+ },
328
+ });
329
+ ```
330
+
331
+ ## MCP 注册
332
+
333
+ MCP Server 信息可以在[行业 AI 平台](https://copilot.glodon.com/)的【MCP 管理 - MCP 详情】中获得。
334
+
335
+ ### 全局作用的 MCP 注册
336
+
337
+ 全局作用的 MCP 注册的完整代码如下:
338
+
339
+ ```javascript
340
+ import BotClientUI from '@glodon-aiot/bot-client-ui';
341
+
342
+ new BotClientUI({
343
+ token: 'aiot_resource_token_goes_here',
344
+ getContainer: () => document.getElementById('agent-ui'),
345
+ mode: 'inlay',
346
+ mcp: {
347
+ mcpServers: {
348
+ mcpServer_ID: {
349
+ type: 'sse',
350
+ url: 'https://copilot.glodon.com/proxy/XXXX/mcp/XXXX/mcp-mcpServer_ID/XXXX/sse',
351
+ },
352
+ },
353
+ },
354
+ errorHandlers: {
355
+ token: (error) => {
356
+ console.error(error);
357
+ },
358
+ },
359
+ });
360
+ ```
361
+
362
+ ### 为当前会话注册 MCP
363
+
364
+ 为当前会话注册 MCP 的完整代码如下:
365
+
366
+ ```javascript
367
+ import BotClientUI from '@glodon-aiot/bot-client-ui';
368
+
369
+ const botClientUI = new BotClientUI({
370
+ token: 'aiot_resource_token_goes_here',
371
+ getContainer: () => document.getElementById('agent-ui'),
372
+ mode: 'inlay',
373
+ errorHandlers: {
374
+ token: (error) => {
375
+ console.error(error);
376
+ },
377
+ },
378
+ eventListeners: {
379
+ sessionload: ({ sessionInstance }) => {
380
+ if (sessionInstance) {
381
+ // MCP注册方法
382
+ const addMCP = () => {
383
+ sessionInstance.addMcpServer({
384
+ mcpServers: {
385
+ mcpServer_ID: {
386
+ type: 'sse',
387
+ url: 'https://copilot.glodon.com/proxy/XXXX/mcp/XXXX/mcp-mcpServer_ID/XXXX/sse',
388
+ },
389
+ },
390
+ });
391
+ };
392
+
393
+ // 只能在ready事件发生后才可以注册MCP
394
+ sessionInstance.addEventListener('ready', addMCP);
395
+ // session不活跃之后移除事件监听
396
+ sessionInstance.addEventListener('inactivated', () =>
397
+ sessionInstance.removeEventListener('ready', addMCP)
398
+ );
399
+ }
400
+ },
401
+ },
402
+ });
403
+ ```