ibc-ai-web-sdk 2.0.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 +607 -0
- package/dist/index.cjs.js +6256 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.esm.js +6245 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.umd.js +6262 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/index.umd.min.js +1 -0
- package/index.d.ts +96 -0
- package/miniprogram/README.md +220 -0
- package/miniprogram/ai-chat-dialog/ai-chat-dialog.js +433 -0
- package/miniprogram/ai-chat-dialog/ai-chat-dialog.json +4 -0
- package/miniprogram/ai-chat-dialog/ai-chat-dialog.wxml +82 -0
- package/miniprogram/ai-chat-dialog/ai-chat-dialog.wxss +88 -0
- package/miniprogram/config/defaultConfig.js +40 -0
- package/miniprogram/core/AIChatClient.js +326 -0
- package/miniprogram/types.ts +178 -0
- package/miniprogram/utils/errorCodes.js +25 -0
- package/miniprogram/utils/logger.js +51 -0
- package/miniprogram/utils/markdownParser.js +97 -0
- package/miniprogram/utils/messageIdGenerator.js +23 -0
- package/package.json +68 -0
- package/src/types.ts +599 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IBC AI Web SDK - 类型声明文件
|
|
3
|
+
* 为 JavaScript 项目提供 TypeScript 类型提示
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type {
|
|
7
|
+
AIChatConfig,
|
|
8
|
+
AIChatClient,
|
|
9
|
+
AIResponse,
|
|
10
|
+
Message,
|
|
11
|
+
SDKEvents,
|
|
12
|
+
ErrorCode,
|
|
13
|
+
TokenRefreshContext,
|
|
14
|
+
TokenRefreshResult,
|
|
15
|
+
TokenRefreshSuccess
|
|
16
|
+
} from './src/types'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* AI Chat Dialog 组件配置
|
|
20
|
+
*/
|
|
21
|
+
export interface AIChatDialogProps {
|
|
22
|
+
config: AIChatConfig
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* AI Chat Dialog 组件事件
|
|
27
|
+
*/
|
|
28
|
+
export interface AIChatDialogEvents extends SDKEvents {
|
|
29
|
+
/** 清除对话 */
|
|
30
|
+
'clear': void
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* AI Chat Dialog 组件方法
|
|
35
|
+
*/
|
|
36
|
+
export interface AIChatDialogMethods {
|
|
37
|
+
/** 打开对话框 */
|
|
38
|
+
open(): void
|
|
39
|
+
|
|
40
|
+
/** 关闭对话框 */
|
|
41
|
+
close(): void
|
|
42
|
+
|
|
43
|
+
/** 清除对话(开始新对话) */
|
|
44
|
+
clearChat(): void
|
|
45
|
+
|
|
46
|
+
/** 停止当前生成 */
|
|
47
|
+
stopGeneration(): Promise<void>
|
|
48
|
+
|
|
49
|
+
/** 上传附件 */
|
|
50
|
+
uploadAttachment(file: File, options?: Record<string, any>): Promise<AIResponse>
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 导入 SDK 插件
|
|
55
|
+
*/
|
|
56
|
+
export function install(Vue: any): void
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* AI Chat Dialog 组件
|
|
60
|
+
*/
|
|
61
|
+
export const AIChatDialog: any
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* AI Chat Client
|
|
65
|
+
*/
|
|
66
|
+
export const AIChatClient: {
|
|
67
|
+
new(config: AIChatConfig): AIChatClient
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function createAIChatDialog(options?: Partial<AIChatConfig>): any
|
|
71
|
+
|
|
72
|
+
export function installVuePlugin(Vue: any): void
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 默认导出
|
|
76
|
+
*/
|
|
77
|
+
export default {
|
|
78
|
+
install,
|
|
79
|
+
AIChatDialog,
|
|
80
|
+
AIChatClient,
|
|
81
|
+
createAIChatDialog,
|
|
82
|
+
installVuePlugin
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 导出类型
|
|
86
|
+
export {
|
|
87
|
+
AIChatConfig,
|
|
88
|
+
AIChatClient,
|
|
89
|
+
AIResponse,
|
|
90
|
+
Message,
|
|
91
|
+
SDKEvents,
|
|
92
|
+
ErrorCode,
|
|
93
|
+
TokenRefreshContext,
|
|
94
|
+
TokenRefreshResult,
|
|
95
|
+
TokenRefreshSuccess
|
|
96
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# ibc-ai-web-sdk - 微信小程序版本
|
|
2
|
+
|
|
3
|
+
## 目录结构
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
miniprogram/
|
|
7
|
+
├── ai-chat-dialog/ # 聊天对话框组件
|
|
8
|
+
│ ├── ai-chat-dialog.js # 组件逻辑
|
|
9
|
+
│ ├── ai-chat-dialog.json # 组件配置
|
|
10
|
+
│ ├── ai-chat-dialog.wxml # 组件模板
|
|
11
|
+
│ └── ai-chat-dialog.wxss # 组件样式
|
|
12
|
+
├── core/
|
|
13
|
+
│ └── AIChatClient.js # AI 聊天客户端(wx.request)
|
|
14
|
+
├── config/
|
|
15
|
+
│ └── defaultConfig.js # 默认配置
|
|
16
|
+
└── utils/
|
|
17
|
+
└── markdownParser.js # Markdown 解析器
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 功能特性
|
|
21
|
+
|
|
22
|
+
- ✅ 自动检测微信小程序环境
|
|
23
|
+
- ✅ 全屏模式(移动端默认全屏)
|
|
24
|
+
- ✅ 触摸事件支持(拖拽、点击)
|
|
25
|
+
- ✅ 复制功能(wx.setClipboardData)
|
|
26
|
+
- ✅ Markdown 渲染(标题、粗体、代码块等)
|
|
27
|
+
- ✅ 多轮对话(conversationId)
|
|
28
|
+
- ✅ 可选鉴权(authFn)
|
|
29
|
+
- ✅ 丰富的事件系统
|
|
30
|
+
|
|
31
|
+
## 快速开始
|
|
32
|
+
|
|
33
|
+
### 1. 复制组件到你的小程序项目
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
# 将 miniprogram 目录复制到你的小程序项目中
|
|
37
|
+
cp -r miniprogram/ your-miniprogram-project/
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### 2. 在页面中引用组件
|
|
41
|
+
|
|
42
|
+
**pages/index/index.json**
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"usingComponents": {
|
|
46
|
+
"ai-chat-dialog": "/miniprogram/ai-chat-dialog/ai-chat-dialog"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### 3. 在页面中使用
|
|
52
|
+
|
|
53
|
+
**pages/index/index.wxml**
|
|
54
|
+
```html
|
|
55
|
+
<view class="container">
|
|
56
|
+
<button bindtap="openChat">打开 AI 助手</button>
|
|
57
|
+
|
|
58
|
+
<!-- AI 聊天对话框 -->
|
|
59
|
+
<ai-chat-dialog
|
|
60
|
+
id="aiChat"
|
|
61
|
+
config="{{aiChatConfig}}"
|
|
62
|
+
bind:close="onChatClose"
|
|
63
|
+
bind:message-send="onMessageSend"
|
|
64
|
+
bind:message-receive="onMessageReceive"
|
|
65
|
+
/>
|
|
66
|
+
</view>
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
**pages/index/index.js**
|
|
70
|
+
```javascript
|
|
71
|
+
Page({
|
|
72
|
+
data: {
|
|
73
|
+
aiChatConfig: {
|
|
74
|
+
// API 配置(必须)
|
|
75
|
+
apiEndpoint: 'https://your-domain.com/v1/app/completion',
|
|
76
|
+
agentAppId: 'your-agent-app-id',
|
|
77
|
+
userId: 'user123',
|
|
78
|
+
bizType: 'miniprogram',
|
|
79
|
+
token: 'your-token-here',
|
|
80
|
+
|
|
81
|
+
// UI 配置(可选)
|
|
82
|
+
title: 'AI 智能助理',
|
|
83
|
+
welcomeText: '您好!我是 AI 智能助理',
|
|
84
|
+
welcomeDesc: '我可以帮您解答系统使用问题',
|
|
85
|
+
|
|
86
|
+
// 可选鉴权函数
|
|
87
|
+
authFn: async (token, userId) => {
|
|
88
|
+
// 调用你的鉴权接口
|
|
89
|
+
const res = await wx.request({
|
|
90
|
+
url: 'https://your-domain.com/api/check-auth',
|
|
91
|
+
method: 'POST',
|
|
92
|
+
data: { token, userId }
|
|
93
|
+
})
|
|
94
|
+
if (!res.data.valid) {
|
|
95
|
+
throw new Error('登录已过期')
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
// 事件回调
|
|
100
|
+
onLoad: (client) => {
|
|
101
|
+
console.log('SDK 加载完成', client)
|
|
102
|
+
},
|
|
103
|
+
onError: (error) => {
|
|
104
|
+
console.error('SDK 错误', error)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
// 打开聊天窗口
|
|
110
|
+
openChat() {
|
|
111
|
+
this.selectComponent('#aiChat').openDialog()
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
// 关闭聊天窗口
|
|
115
|
+
onChatClose() {
|
|
116
|
+
console.log('聊天窗口已关闭')
|
|
117
|
+
},
|
|
118
|
+
|
|
119
|
+
// 消息发送事件
|
|
120
|
+
onMessageSend(data) {
|
|
121
|
+
console.log('用户发送:', data.message)
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
// 消息接收事件
|
|
125
|
+
onMessageReceive(data) {
|
|
126
|
+
console.log('AI 回复:', data.response)
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## API 文档
|
|
132
|
+
|
|
133
|
+
### 配置项(config)
|
|
134
|
+
|
|
135
|
+
| 字段 | 类型 | 必填 | 默认值 | 说明 |
|
|
136
|
+
|------|------|------|--------|------|
|
|
137
|
+
| apiEndpoint | String | 是 | - | API 接口地址 |
|
|
138
|
+
| agentAppId | String | 是 | - | 智能体应用 ID |
|
|
139
|
+
| userId | String | 是 | - | 用户 ID |
|
|
140
|
+
| bizType | String | 是 | - | 业务类型标识 |
|
|
141
|
+
| token | String | 否 | '' | 认证 Token |
|
|
142
|
+
| timeout | Number | 否 | 60000 | 请求超时时间(毫秒) |
|
|
143
|
+
| headers | Object | 否 | {} | 自定义请求头 |
|
|
144
|
+
| extendInfo | Object | 否 | null | 扩展信息 |
|
|
145
|
+
| title | String | 否 | 'AI 智能助理' | 对话框标题 |
|
|
146
|
+
| welcomeText | String | 否 | '您好!我是 AI 智能助理' | 欢迎文本 |
|
|
147
|
+
| welcomeDesc | String | 否 | '我可以帮您解答系统使用问题' | 欢迎描述 |
|
|
148
|
+
| authFn | Function | 否 | null | 可选鉴权函数 |
|
|
149
|
+
| onLoad | Function | 否 | null | SDK 加载完成回调 |
|
|
150
|
+
| onError | Function | 否 | null | 错误回调 |
|
|
151
|
+
|
|
152
|
+
### 事件(Events)
|
|
153
|
+
|
|
154
|
+
| 事件名 | 参数 | 说明 |
|
|
155
|
+
|--------|------|------|
|
|
156
|
+
| message-send | `{ message, index }` | 用户发送消息 |
|
|
157
|
+
| message-receive | `{ response, index }` | AI 回复消息 |
|
|
158
|
+
| loading-change | `boolean` | Loading 状态变化 |
|
|
159
|
+
| close | - | 关闭对话框 |
|
|
160
|
+
| expand | `boolean` | 展开/收起 |
|
|
161
|
+
|
|
162
|
+
### 方法
|
|
163
|
+
|
|
164
|
+
通过 `this.selectComponent('#aiChat').methodName()` 调用:
|
|
165
|
+
|
|
166
|
+
| 方法名 | 说明 |
|
|
167
|
+
|--------|------|
|
|
168
|
+
| openDialog() | 打开对话框 |
|
|
169
|
+
| handleClose() | 关闭对话框 |
|
|
170
|
+
| clearChat() | 清除对话(开始新对话) |
|
|
171
|
+
|
|
172
|
+
## 响应格式
|
|
173
|
+
|
|
174
|
+
### 成功响应
|
|
175
|
+
|
|
176
|
+
```json
|
|
177
|
+
{
|
|
178
|
+
"success": true,
|
|
179
|
+
"code": 200,
|
|
180
|
+
"message": "成功",
|
|
181
|
+
"data": {
|
|
182
|
+
"content": "AI 回复内容(支持 Markdown)",
|
|
183
|
+
"conversationId": "conv_xxx",
|
|
184
|
+
"messageId": "msg_xxx"
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
### 失败响应
|
|
190
|
+
|
|
191
|
+
```json
|
|
192
|
+
{
|
|
193
|
+
"success": false,
|
|
194
|
+
"code": 40001,
|
|
195
|
+
"message": "智能体不存在或已禁用",
|
|
196
|
+
"data": null
|
|
197
|
+
}
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## 注意事项
|
|
201
|
+
|
|
202
|
+
1. **网络请求配置**
|
|
203
|
+
- 在微信开发者工具中,勾选"不校验合法域名"
|
|
204
|
+
- 生产环境需要在小程序后台配置 request 合法域名
|
|
205
|
+
|
|
206
|
+
2. **Markdown 支持**
|
|
207
|
+
- 支持标题、粗体、斜体、代码块、链接、图片等
|
|
208
|
+
- 使用 `rich-text` 组件渲染
|
|
209
|
+
|
|
210
|
+
3. **复制功能**
|
|
211
|
+
- 使用 `wx.setClipboardData()` 实现
|
|
212
|
+
- 复制成功后显示 2 秒的"已复制"提示
|
|
213
|
+
|
|
214
|
+
4. **触摸事件**
|
|
215
|
+
- 支持拖拽移动对话框(桌面端)
|
|
216
|
+
- 移动端自动全屏,禁用拖拽
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT
|