@core-pilot/sdk 0.0.0-beta.3 → 0.0.0-beta.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 CHANGED
@@ -4,15 +4,17 @@
4
4
 
5
5
  它提供了开箱即用的 UI 组件、标准化的通信协议抽象(Provider)、以及完善的事件交互机制(HITL),支持与后端 AI 引擎(如 CoreAgent、Dify 等)进行无缝对接。
6
6
 
7
- ## 核心功能
7
+ ## ✨ 核心特性
8
8
 
9
- - **⚡️ 快速集成**:通过简单的配置即可挂载完整的 AI 对话界面。
10
- - **🔌 多协议支持**:内置 CoreAgent 协议支持,架构设计支持扩展 Dify 等其他 Provider
11
- - **🔄 事件驱动**:提供 `subscribeEvent` 和 `triggerEvent` 接口,实现宿主应用与 Agent 之间的双向通信(Code Bridge)。
12
- - **🔐 鉴权管理**:支持 Client/Server 模式的 Token 管理。
13
- - **🎨 样式隔离**:自带样式(需引入 CSS),支持自定义挂载点。
9
+ - **⚡️ 快速集成**:通过简单的配置即可挂载完整的 AI 对话界面
10
+ - **🔌 多协议支持**:内置 CoreAgent 协议支持,架构设计支持扩展 Dify 等其他 Provider
11
+ - **🔄 事件驱动架构**:提供 `subscribeEvent` 和 `triggerEvent` 接口,实现宿主应用与 Agent 之间的双向通信(Code Bridge
12
+ - **🔐 灵活鉴权**:支持 Client/Server 双模式的 Token 管理
13
+ - **🎨 样式隔离**:自带完整样式,支持自定义挂载点
14
+ - **📝 日志追踪**:内置日志系统,支持调试模式和远程日志上报
15
+ - **♻️ 生命周期管理**:完善的挂载、卸载、重载和销毁机制
14
16
 
15
- ## 安装
17
+ ## 📦 安装
16
18
 
17
19
  ```bash
18
20
  # npm
@@ -25,7 +27,9 @@ pnpm add @core-pilot/sdk
25
27
  yarn add @core-pilot/sdk
26
28
  ```
27
29
 
28
- ## 快速开始
30
+ ## 🚀 快速开始
31
+
32
+ ### 基础示例
29
33
 
30
34
  ```typescript
31
35
  import { createCorePilot } from '@core-pilot/sdk';
@@ -35,8 +39,12 @@ import '@core-pilot/sdk/dist/index.css'; // 引入样式
35
39
  const pilot = createCorePilot({
36
40
  // 基础配置
37
41
  appId: 'your-app-id',
38
- baseUrl: 'https://your-agent-api.com',
39
- provider: 'coreagent', // 目前支持 'coreagent' | 'dify'
42
+ provider: {
43
+ name: 'coreagent', // 目前支持 'coreagent' | 'dify'
44
+ extensions: {
45
+ user: 'default', // 可选:用户标识
46
+ }
47
+ },
40
48
 
41
49
  // 鉴权配置
42
50
  auth: {
@@ -49,137 +57,688 @@ const pilot = createCorePilot({
49
57
  selector: '#agent-container', // 宿主应用中的 DOM 节点 ID
50
58
  },
51
59
 
52
- // 调试模式
53
- debug: true,
60
+ // 可选配置
61
+ debug: true, // 开启调试日志
54
62
  });
55
63
 
56
64
  // 2. 挂载 UI
57
65
  pilot.mount();
58
66
  ```
59
67
 
60
- ## 功能指南 (按能力)
68
+ ### HTML 准备
69
+
70
+ 在你的 HTML 中准备挂载点:
71
+
72
+ ```html
73
+ <div id="agent-container" style="width: 100%; height: 600px;"></div>
74
+ ```
75
+
76
+ ## 📖 完整功能指南
61
77
 
62
- ### 1. 基础挂载与生命周期管理
78
+ ### 1. 生命周期管理
63
79
 
64
80
  SDK 提供了完整的生命周期管理方法,允许你在单页应用 (SPA) 中灵活控制 Agent 的显示与销毁。
65
81
 
82
+ #### 挂载 UI
83
+
66
84
  ```typescript
67
- // 挂载 UI 到指定节点
68
85
  pilot.mount();
86
+ ```
87
+
88
+ 将 Agent UI 渲染到指定的 DOM 节点。SDK 会在选择器指向的元素内部创建一个 `[data-corepilot-host]` 容器来挂载 Vue 应用。
69
89
 
70
- // 卸载 UI (清除 DOM 内容,但保留实例配置)
90
+ #### 卸载 UI
91
+
92
+ ```typescript
71
93
  pilot.unmount();
94
+ ```
72
95
 
73
- // 重新加载 (可传入新配置,常用于切换租户或更新 Token)
96
+ 卸载 UI 组件并清除 DOM 内容,但保留实例配置。适用于需要临时隐藏 Agent 的场景。
97
+
98
+ #### 重新加载
99
+
100
+ ```typescript
101
+ // 重新加载(可传入新配置,常用于切换租户或更新 Token)
74
102
  pilot.reload({
75
103
  auth: {
76
104
  mode: 'client',
77
105
  token: 'new-token'
106
+ },
107
+ provider: {
108
+ name: 'coreagent',
109
+ extensions: {
110
+ user: 'new-user-id'
111
+ }
78
112
  }
79
113
  });
114
+ ```
115
+
116
+ 重载会先卸载现有 UI,更新配置,然后重新挂载。适用于:
117
+ - 切换用户/租户
118
+ - 更新 Token
119
+ - 修改 Provider 配置
120
+
121
+ #### 销毁实例
80
122
 
81
- // 彻底销毁实例 (清除日志、订阅等)
123
+ ```typescript
82
124
  pilot.destroy();
83
125
  ```
84
126
 
127
+ 彻底销毁实例,清理所有资源(清除日志缓冲区、取消订阅等)。通常在应用卸载或路由切换时调用。
128
+
129
+ ---
130
+
85
131
  ### 2. 双向通信 (Code Bridge / HITL)
86
132
 
87
- 这是 SDK 的核心能力之一。通过事件机制,Agent 可以控制宿主应用(例如:Agent 请求打开某个文件),宿主应用也可以向 Agent 发送隐式指令。
133
+ 这是 SDK 的核心能力之一。通过事件机制,Agent 可以控制宿主应用(例如:Agent 请求打开某个文件),宿主应用也可以向 Agent 发送指令。
88
134
 
89
- #### 监听 Agent 事件 (Agent -> App)
135
+ #### 事件命名规范
136
+
137
+ 所有的操作 action 分为以下三种类型,及其对应的命名定义:
138
+
139
+ | 类型 | 前缀 | 说明 | 示例 |
140
+ |------|------|------|------|
141
+ | 核心业务交互 | `core_` | 通过 SDK 与后端进行交互 | `core_submit_feedback` |
142
+ | 内部组件交互 | `inner_` | 当前卡片组件纯前端交互(上一步、下一步等) | `inner_next_step` |
143
+ | 自定义事件 | `cust_` | 透传到 SDK 外部,供用户调用 | `cust_open_file` |
90
144
 
91
- Agent 需要执行本地操作(如打开 IDE 文件、跳转页面)时,会下发自定义事件。
145
+ #### 订阅 Agent 事件 (Agent → App)
92
146
 
93
- > 另外的,针对action的类型,这里做如下的规范约定:
94
- >所有的操作action分为以下三种情况,及其对应的命名定义
95
- > 1. 通过sdk 与后端进行交互部分:**core_业务场景**
96
- > 2. 当前卡片组件纯前端交互(上一步,下一步):**inner_业务场景**
97
- > 3. 透传到sdk外部,供用户调用事件:**cust_业务场景**
147
+ Agent 需要执行本地操作(如打开 IDE 文件、跳转页面)时,会下发 `cust_` 类型的自定义事件。
98
148
 
99
149
  ```typescript
100
150
  // 订阅事件
101
- pilot.subscribeEvent((event) => {
151
+ const handleAgentEvent = (event) => {
102
152
  console.log('收到 Agent 事件:', event);
103
153
 
104
154
  // 示例:处理打开文件请求
105
- if (event.type === 'open_file') {
155
+ if (event.action === 'cust_open_file') {
106
156
  const { filePath, line } = event.payload;
107
157
  myEditor.open(filePath, line);
108
158
  }
159
+
160
+ // 示例:处理跳转页面
161
+ if (event.action === 'cust_navigate') {
162
+ const { url } = event.payload;
163
+ window.location.href = url;
164
+ }
165
+ };
166
+
167
+ pilot.subscribeEvent(handleAgentEvent);
168
+
169
+ // 取消订阅
170
+ pilot.unsubscribeEvent(handleAgentEvent);
171
+ ```
172
+
173
+ > **注意**: `subscribeEvent` 支持防重复订阅,多次订阅同一个回调函数只会保留一个。
174
+
175
+ #### 触发 Agent 行为 (App → Agent)
176
+
177
+ 宿主应用可以主动触发 Agent 的某些行为,或者获取 Agent 的内部数据。
178
+
179
+ ##### 支持的事件类型
180
+
181
+ | 事件类型 | 说明 | Payload 类型 |
182
+ |---------|------|-------------|
183
+ | `get_suggestion_questions` | 获取推荐问题 | `ChatMessage` |
184
+ | `get_conversation_list` | 获取会话列表 | `ConversationRequest` |
185
+ | `get_message_history` | 获取消息历史 | `MessageHistoryRequest` |
186
+ | `submit_core_action` | 提交核心业务场景 | `HitlPayload` |
187
+ | `new_message` | 打开新对话(重置消息历史) | 无需 payload |
188
+ | `submit_message` | 提交通过对话框输入的消息 | `SubmitMessagePayload` |
189
+
190
+ ##### 示例 1:获取推荐问题
191
+
192
+ ```typescript
193
+ // 基于当前消息获取下一轮推荐问题
194
+ const currentMessage = {
195
+ messageId: 'msg-123',
196
+ role: 'ai',
197
+ content: '已为您分析完成'
198
+ };
199
+
200
+ const questions = await pilot.triggerEvent({
201
+ action: 'get_suggestion_questions',
202
+ payload: currentMessage
203
+ });
204
+
205
+ console.log('推荐问题:', questions); // ['如何优化性能?', '下一步做什么?']
206
+ ```
207
+
208
+ ##### 示例 2:获取会话列表
209
+
210
+ ```typescript
211
+ const conversations = await pilot.triggerEvent({
212
+ action: 'get_conversation_list',
213
+ payload: {
214
+ limit: '20',
215
+ user: 'default',
216
+ last_id: '',
217
+ sort_by: '-created_at' // 最新创建的在前
218
+ }
219
+ });
220
+
221
+ console.log('会话列表:', conversations);
222
+ /*
223
+ {
224
+ limit: 20,
225
+ has_more: false,
226
+ data: [
227
+ {
228
+ id: 'conv-001',
229
+ name: '项目重构讨论',
230
+ status: 'normal',
231
+ created_at: '2025-12-18T10:00:00Z',
232
+ ...
233
+ }
234
+ ]
235
+ }
236
+ */
237
+ ```
238
+
239
+ ##### 示例 3:获取消息历史
240
+
241
+ ```typescript
242
+ const history = await pilot.triggerEvent({
243
+ action: 'get_message_history',
244
+ payload: {
245
+ conversation_id: 'conv-001',
246
+ user: 'default',
247
+ first_id: '', // 从最新消息开始
248
+ limit: '50'
249
+ }
109
250
  });
251
+
252
+ console.log('消息历史:', history);
253
+ // 返回格式化后的 ChatMessage[] 数组,并自动同步到内部 store
110
254
  ```
111
255
 
112
- #### 触发 Agent 行为 (App -> Agent)
256
+ ##### 示例 4:提交核心业务场景
257
+
258
+ ```typescript
259
+ // 模拟 Agent 执行某个核心操作(如代码审查)
260
+ await pilot.triggerEvent({
261
+ action: 'submit_core_action',
262
+ payload: {
263
+ hitlData: {
264
+ action: 'core_code_review',
265
+ payload: {
266
+ filePath: '/src/components/App.tsx',
267
+ lineStart: 10,
268
+ lineEnd: 50
269
+ }
270
+ },
271
+ text: '请帮我审查这段代码',
272
+ prevMessage: lastAiMessage, // 上一条 AI 消息
273
+ inputs: {
274
+ // 额外的输入参数
275
+ language: 'typescript'
276
+ }
277
+ }
278
+ });
279
+ ```
113
280
 
114
- 宿主应用可以主动触发 Agent 的某些行为,或者获取 Agent 的内部数据。目前支持的事件类型如下:
281
+ ##### 示例 5:打开新对话
115
282
 
116
- | 事件类型 | 说明 |
117
- |------|------|
118
- | get_suggestion_questions | 获取推荐问题 |
119
- | get_conversation_list | 获取会话列表 |
120
- | get_message_history | 获取消息历史 |
121
- | submit_core_action | 提交核心业务场景 |
122
- | new_message | 打开新对话触发,用于重置消息历史 |
123
- | submit_message | 提交通过对话框输入的消息 |
283
+ ```typescript
284
+ // 重置消息历史,开始新对话
285
+ await pilot.triggerEvent({
286
+ action: 'new_message'
287
+ });
288
+ ```
124
289
 
290
+ ##### 示例 6:提交文本消息
125
291
 
126
292
  ```typescript
127
- // 触发 SDK 内部行为
128
- pilot.triggerEvent({
129
- action: 'get_suggestion_questions', // 示例:获取推荐问题
293
+ // 模拟用户输入并提交消息
294
+ await pilot.triggerEvent({
295
+ action: 'submit_message',
130
296
  payload: {
131
- // ... 具体的请求参数
297
+ text: '你好,请帮我分析这个错误',
298
+ file: {
299
+ id: 'file-001',
300
+ name: 'error.log',
301
+ url: 'https://example.com/files/error.log',
302
+ thumbnail: 'https://example.com/files/error.log.thumb'
303
+ },
304
+ inputs: {
305
+ context: 'production'
306
+ }
132
307
  }
133
- }).then(res => {
134
- console.log('Result:', res);
135
308
  });
136
309
  ```
137
310
 
311
+ ---
312
+
138
313
  ### 3. 鉴权配置 (Authentication)
139
314
 
140
- 支持两种模式:
315
+ SDK 支持两种鉴权模式,适应不同的安全需求。
316
+
317
+ #### Client Mode(客户端模式)
141
318
 
142
- - **Client Mode**: 直接在前端传入 API Key(仅用于开发或内网环境)。
143
- - **Server Mode**: 通过后端代理转发,前端不暴露 Key(推荐生产环境)。
319
+ 直接在前端传入 API Key,适用于开发环境或内网环境。
144
320
 
145
321
  ```typescript
146
- // Client Mode
147
322
  const pilot = createCorePilot({
148
- // ...
323
+ appId: 'your-app-id',
324
+ provider: { name: 'coreagent' },
149
325
  auth: {
150
326
  mode: 'client',
151
- token: 'sk-xxxxxxxx',
327
+ token: 'sk-xxxxxxxxxxxxxxxxxxxxxxxx', // 直接传入 Token
328
+ },
329
+ mount: {
330
+ selector: '#agent-container'
152
331
  }
153
332
  });
333
+ ```
334
+
335
+ > **⚠️ 安全提示**: Client Mode 会在前端暴露 API Key,仅用于开发测试或安全可控的内网环境。
336
+
337
+ #### Server Mode(服务端模式)
154
338
 
155
- // Server Mode (通常配合自定义的 AuthManager 或 Proxy Url)
156
- // 目前 SDK 内部会根据 mode 处理请求头的组装
339
+ 通过后端代理转发,前端不暴露 Key,推荐生产环境使用。
340
+
341
+ ```typescript
342
+ const pilot = createCorePilot({
343
+ appId: 'your-app-id',
344
+ provider: { name: 'coreagent' },
345
+ auth: {
346
+ mode: 'server',
347
+ // 不需要传入 token,SDK 会通过后端获取
348
+ },
349
+ mount: {
350
+ selector: '#agent-container'
351
+ }
352
+ });
157
353
  ```
158
354
 
355
+ > **实现提示**:
356
+ > - 在 Server Mode 下,SDK 会调用 `authManager.getToken()` 来获取 Token
357
+ > - 当前实现中,你需要在后端提供一个 Token 获取接口
358
+ > - 可以参考 `packages/sdk/packages/core/auth.ts` 中的 TODO 注释进行自定义实现
359
+
360
+ ---
361
+
159
362
  ### 4. 调试与日志
160
363
 
161
- 开启 `debug` 模式后,SDK 会在控制台输出详细的运行日志,方便排查连接、鉴权和事件流问题。
364
+ SDK 内置了完善的日志系统,支持本地调试和远程日志上报。
365
+
366
+ #### 开启调试模式
162
367
 
163
368
  ```typescript
164
369
  const pilot = createCorePilot({
165
- // ...
370
+ appId: 'your-app-id',
371
+ provider: { name: 'coreagent' },
372
+ auth: { mode: 'client', token: 'sk-xxx' },
373
+ mount: { selector: '#agent-container' },
374
+
166
375
  debug: true, // 开启调试日志
376
+ });
377
+ ```
378
+
379
+ 开启后,SDK 会在控制台输出详细的运行日志:
380
+ - 初始化信息
381
+ - 挂载/卸载事件
382
+ - HITL 事件提交
383
+ - SSE 连接状态
384
+ - 错误信息
385
+
386
+ #### 远程日志上报(可选)
387
+
388
+ ```typescript
389
+ const pilot = createCorePilot({
390
+ // ... 其他配置
391
+ debug: true,
167
392
  eventTracker: {
168
- url: 'https://monitor.example.com/log', // 可选:上报日志到服务器
393
+ url: 'https://monitor.example.com/log', // 日志上报地址
394
+ batchSize: 10, // 批量上报大小(默认 10)
395
+ flushInterval: 5000, // 上报间隔(毫秒,默认 5000)
169
396
  }
170
397
  });
171
398
  ```
172
399
 
173
- ## API 参考
400
+ 日志系统特性:
401
+ - **批量上报**: 积累到指定数量后批量发送,减少网络请求
402
+ - **定时刷新**: 定时发送未满批次的日志
403
+ - **销毁前刷新**: 调用 `destroy()` 时自动发送剩余日志
174
404
 
175
- ### `CorePilotOptions`
405
+ ---
176
406
 
177
- | 属性 | 类型 | 必填 | 说明 |
178
- |------|------|------|------|
179
- | `appId` | string | 是 | Agent 应用 ID |
180
- | `baseUrl` | string | 否 | API 服务基础地址 |
181
- | `provider` | `'coreagent' \| 'dify'` | 是 | 协议提供商 |
182
- | `auth` | `AuthOptions` | 是 | 鉴权配置 `{ mode, token }` |
183
- | `mount` | `MountOptions` | 是 | 挂载配置 `{ selector, container? }` |
184
- | `debug` | boolean | 否 | 是否开启调试模式 |
185
- | `hitl` | boolean | 否 | 是否启用 Human-in-the-Loop 功能 |
407
+ ### 5. Provider 配置
408
+
409
+ SDK 支持多种 AI Provider,目前内置了 CoreAgent 协议。
410
+
411
+ #### CoreAgent Provider
412
+
413
+ ```typescript
414
+ const pilot = createCorePilot({
415
+ appId: 'your-app-id',
416
+ provider: {
417
+ name: 'coreagent',
418
+ extensions: {
419
+ user: 'user-001', // 用户标识,用于会话隔离
420
+ }
421
+ },
422
+ // ... 其他配置
423
+ });
424
+ ```
425
+
426
+ #### Dify Provider(架构支持,待实现)
427
+
428
+ ```typescript
429
+ const pilot = createCorePilot({
430
+ appId: 'your-app-id',
431
+ provider: {
432
+ name: 'dify',
433
+ extensions: {
434
+ // Dify 特有配置
435
+ }
436
+ },
437
+ // ... 其他配置
438
+ });
439
+ ```
440
+
441
+ > **扩展提示**: 如需添加新的 Provider,可参考 `packages/sdk/packages/protocols/coreagent.adapter.ts` 实现 `AgentAdapter` 接口。
442
+
443
+ ---
444
+
445
+ ## 📚 API 参考
446
+
447
+ ### CorePilotOptions
448
+
449
+ | 属性 | 类型 | 必填 | 默认值 | 说明 |
450
+ |------|------|------|--------|------|
451
+ | `appId` | `string` | ✅ | - | Agent 应用 ID |
452
+ | `provider` | `AgentProvider` | ✅ | - | 协议提供商配置 |
453
+ | `auth` | `AuthOptions` | ✅ | - | 鉴权配置 |
454
+ | `mount` | `MountOptions` | ✅ | - | 挂载配置 |
455
+ | `debug` | `boolean` | ✅ | `false` | 是否开启调试模式 |
456
+ | `eventTracker` | `EventTrackerOptions` | ❌ | - | 远程日志上报配置 |
457
+
458
+ ### AgentProvider
459
+
460
+ ```typescript
461
+ type AgentProvider = {
462
+ name: 'coreagent' | 'dify';
463
+ extensions: Record<string, any>; // Provider 特有扩展配置
464
+ };
465
+ ```
466
+
467
+ ### AuthOptions
468
+
469
+ ```typescript
470
+ interface AuthOptions {
471
+ mode: 'client' | 'server';
472
+ token?: string; // client 模式下必填
473
+ }
474
+ ```
475
+
476
+ ### MountOptions
477
+
478
+ ```typescript
479
+ interface MountOptions {
480
+ selector: string; // CSS 选择器,如 '#app'
481
+ container?: HTMLElement; // 可选:限定查找范围
482
+ }
483
+ ```
484
+
485
+ ### EventTrackerOptions
486
+
487
+ ```typescript
488
+ interface EventTrackerOptions {
489
+ url: string; // 日志上报地址
490
+ batchSize?: number; // 批量上报大小(默认 10)
491
+ flushInterval?: number; // 上报间隔毫秒数(默认 5000)
492
+ }
493
+ ```
494
+
495
+ ### CorePilotInstance
496
+
497
+ 实例方法:
498
+
499
+ | 方法 | 参数 | 返回值 | 说明 |
500
+ |------|------|--------|------|
501
+ | `mount()` | - | `void` | 挂载 UI 到 DOM |
502
+ | `unmount()` | - | `void` | 卸载 UI |
503
+ | `reload(options?)` | `Partial<CorePilotOptions>` | `void` | 重载实例(可更新配置) |
504
+ | `destroy()` | - | `void` | 销毁实例(清理所有资源) |
505
+ | `triggerEvent(event)` | `TriggerEventPayload` | `Promise<any>` | 主动触发 SDK 内部事件 |
506
+ | `subscribeEvent(callback)` | `(event: any) => void` | `void` | 订阅 `cust_` 类型事件 |
507
+ | `unsubscribeEvent(callback)` | `(event: any) => void` | `void` | 取消订阅 |
508
+
509
+ ### TriggerEventPayload
510
+
511
+ ```typescript
512
+ interface TriggerEventPayload {
513
+ action:
514
+ | 'get_suggestion_questions'
515
+ | 'get_conversation_list'
516
+ | 'get_message_history'
517
+ | 'submit_core_action'
518
+ | 'new_message'
519
+ | 'submit_message';
520
+ payload?:
521
+ | ChatMessage
522
+ | ConversationRequest
523
+ | MessageHistoryRequest
524
+ | HitlPayload
525
+ | SubmitMessagePayload;
526
+ }
527
+ ```
528
+
529
+ ### ChatMessage
530
+
531
+ ```typescript
532
+ interface ChatMessage {
533
+ taskId?: string;
534
+ role: 'user' | 'ai';
535
+ messageId?: string;
536
+ conversationId?: string;
537
+ content?: string;
538
+ questions?: string[];
539
+ speed?: number;
540
+ showActions?: boolean;
541
+ feedback?: 'like' | 'dislike';
542
+ files?: Array<{
543
+ id: string;
544
+ extension: string;
545
+ name: string;
546
+ }>;
547
+ placement?: 'start' | 'end';
548
+ avatar?: string;
549
+ loading?: boolean;
550
+ maxWidth?: string;
551
+ isHistoryMessage?: boolean;
552
+ // ... 更多 UI 配置属性
553
+ }
554
+ ```
555
+
556
+ ### HitlPayload
557
+
558
+ ```typescript
559
+ interface HitlPayload {
560
+ hitlData?: {
561
+ action: string; // 如 'core_xxx', 'cust_xxx', 'inner_xxx'
562
+ payload: any;
563
+ };
564
+ text?: string; // 文本消息
565
+ prevMessage?: ChatMessage; // 上一条消息
566
+ inputs?: Record<string, any>; // 额外输入参数
567
+ }
568
+ ```
569
+
570
+ ### ConversationRequest
571
+
572
+ ```typescript
573
+ interface ConversationRequest {
574
+ limit: string; // 返回数量
575
+ user: string; // 用户标识
576
+ last_id: string; // 分页:最后一条会话 ID
577
+ sort_by: 'created_at' | 'updated_at' | '-created_at' | '-updated_at'; // 排序方式
578
+ }
579
+ ```
580
+
581
+ ### MessageHistoryRequest
582
+
583
+ ```typescript
584
+ interface MessageHistoryRequest {
585
+ conversation_id: string; // 会话 ID
586
+ user: string; // 用户标识
587
+ first_id: string; // 分页:第一条消息 ID
588
+ limit: string; // 返回数量
589
+ }
590
+ ```
591
+
592
+ ### SubmitMessagePayload
593
+
594
+ ```typescript
595
+ interface SubmitMessagePayload {
596
+ text: string; // 消息文本
597
+ file?: { // 可选:附带文件
598
+ id?: string;
599
+ name: string;
600
+ url: string;
601
+ thumbnail?: string;
602
+ };
603
+ inputs?: Record<string, any>; // 额外输入参数
604
+ }
605
+ ```
606
+
607
+ ---
608
+
609
+ ## 🏗️ 项目结构
610
+
611
+ ```
612
+ packages/sdk/
613
+ ├── packages/
614
+ │ ├── core/ # 核心模块
615
+ │ │ ├── index.ts # SDK 入口
616
+ │ │ ├── auth.ts # 鉴权管理
617
+ │ │ ├── hitl.ts # HITL 事件管理
618
+ │ │ ├── mount.ts # UI 挂载管理
619
+ │ │ └── logger.ts # 日志系统
620
+ │ ├── http/ # HTTP 通信
621
+ │ │ ├── axios.ts # HTTP 客户端
622
+ │ │ └── sse.ts # SSE 流式通信
623
+ │ ├── protocols/ # 协议适配器
624
+ │ │ ├── index.ts # 适配器工厂
625
+ │ │ └── coreagent.adapter.ts # CoreAgent 适配器
626
+ │ ├── store/ # 状态管理
627
+ │ │ └── index.ts # Zustand Store
628
+ │ ├── ui/ # UI 组件
629
+ │ │ └── vue/ # Vue 组件
630
+ │ │ └── CorePilotHost.tsx
631
+ │ └── types.ts # TypeScript 类型定义
632
+ ├── examples/ # 示例项目
633
+ │ └── vueExample/ # Vue 示例
634
+ ├── dist/ # 构建产物
635
+ ├── vite.config.ts # 构建配置
636
+ └── README.md # 本文档
637
+ ```
638
+
639
+ ---
640
+
641
+ ## 🔧 高级用法
642
+
643
+ ### 动态切换 Provider
644
+
645
+ ```typescript
646
+ // 初始化时使用 CoreAgent
647
+ const pilot = createCorePilot({
648
+ provider: { name: 'coreagent' },
649
+ // ...
650
+ });
651
+
652
+ // 运行时切换到 Dify(需要后续版本支持)
653
+ pilot.reload({
654
+ provider: { name: 'dify' },
655
+ });
656
+ ```
657
+
658
+ ---
659
+
660
+ ## 🐛 常见问题
661
+
662
+ ### 1. 挂载点找不到
663
+
664
+ **问题**: 控制台报错 `Mount point "#agent-container" not found`
665
+
666
+ **解决方案**:
667
+ - 确保 DOM 已经渲染完成后再调用 `pilot.mount()`
668
+ - 在 Vue 中使用 `nextTick` 或 `onMounted`
669
+ - 在 React 中使用 `useEffect`
670
+
671
+ ```typescript
672
+ // Vue 3
673
+ import { nextTick } from 'vue';
674
+
675
+ nextTick(() => {
676
+ pilot.mount();
677
+ });
678
+
679
+ // React
680
+ useEffect(() => {
681
+ pilot.mount();
682
+ return () => pilot.unmount();
683
+ }, []);
684
+ ```
685
+
686
+ ### 2. Token 鉴权失败
687
+
688
+ **问题**: 请求返回 401 Unauthorized
689
+
690
+ **解决方案**:
691
+ - 检查 `auth.token` 是否正确
692
+ - 确认 Token 未过期
693
+ - 验证 `appId` 与 Token 匹配
694
+
695
+ ### 3. 事件回调未触发
696
+
697
+ **问题**: `subscribeEvent` 的回调函数没有执行
698
+
699
+ **解决方案**:
700
+ - 确认 Agent 下发的事件类型是 `cust_` 前缀
701
+ - 检查回调函数是否正确订阅
702
+ - 使用 `debug: true` 查看事件流
703
+
704
+ ```typescript
705
+ // 正确的订阅方式
706
+ const handleEvent = (event) => {
707
+ console.log('Event received:', event);
708
+ };
709
+
710
+ pilot.subscribeEvent(handleEvent);
711
+
712
+ // 取消订阅时使用同一个函数引用
713
+ pilot.unsubscribeEvent(handleEvent);
714
+ ```
715
+
716
+ ---
717
+
718
+ ## 📄 License
719
+
720
+ MPL-2.0 License
721
+
722
+ <!-- ---
723
+
724
+ ## 🤝 贡献指南
725
+
726
+ 欢迎提交 Issue 和 Pull Request!
727
+
728
+ 在提交代码前,请确保:
729
+ 1. 代码通过 ESLint 检查
730
+ 2. 添加了必要的类型定义
731
+ 3. 更新了相关文档
732
+
733
+ ---
734
+
735
+ ## 📞 联系我们
736
+
737
+ 如有问题或建议,请通过以下方式联系:
738
+
739
+ - 提交 Issue: [GitHub Issues](https://github.com/your-org/code-bridge-lib/issues)
740
+ - 邮件: support@example.com
741
+
742
+ --- -->
743
+
744
+ **Happy Coding! 🚀**