@rtc-agent/component 0.2.6-rc.0 → 0.2.6-rc.2

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
@@ -80,6 +80,39 @@ const agent = createRtcAgent({ /* ... */ });
80
80
 
81
81
  ## 用法
82
82
 
83
+ ### 工厂函数(推荐)
84
+
85
+ ```ts
86
+ import { createRtcAgent } from '@rtc-agent/component';
87
+
88
+ const agent = createRtcAgent({
89
+ appLabel: 'My Assistant',
90
+ server: { url: 'https://api.example.com' },
91
+ workerUrl: '/rtc-agent/shared-worker.js',
92
+ auth: {
93
+ getToken: () => localStorage.getItem('token'),
94
+ refreshToken: async () => {
95
+ const res = await fetch('/api/refresh');
96
+ const data = await res.json();
97
+ return { accessToken: data.token };
98
+ },
99
+ userId: 'user-123',
100
+ },
101
+ on: {
102
+ ready: () => console.log('RTC Agent ready'),
103
+ themeChange: ({ theme }) => console.log('Theme:', theme),
104
+ toolCallStart: ({ path, params }) => console.log('Tool call:', path),
105
+ },
106
+ });
107
+
108
+ document.body.appendChild(agent);
109
+
110
+ // 不再需要时销毁
111
+ agent.destroy();
112
+ ```
113
+
114
+ ### HTML 元素
115
+
83
116
  ```html
84
117
  <script type="module">
85
118
  import '@rtc-agent/component';
@@ -115,13 +148,33 @@ RTC Agent 使用 SharedWorker + Comlink 架构实现多 Tab 共享:
115
148
 
116
149
  ### 公开事件
117
150
 
118
- 所有公开事件遵循 `rtc-<domain>-<action>-<past-tense>` 命名模式:
151
+ 所有公开事件遵循 `rtc-agent-<event>` 命名模式:
152
+
153
+ **生命周期事件:**
119
154
 
120
155
  - `rtc-agent-ready` — 组件就绪
156
+ - `rtc-agent-beforeDestroy` — 组件即将销毁
157
+ - `rtc-agent-themeChange` — 主题变化
158
+
159
+ **消息拦截事件:**
160
+
161
+ - `rtc-agent-beforeMessageSend` — 消息发送前(可取消或修改)
162
+
163
+ **工具调用事件:**
164
+
165
+ - `rtc-agent-toolCallStart` — 工具调用开始
166
+ - `rtc-agent-toolCallSuccess` — 工具调用成功
167
+ - `rtc-agent-toolCallError` — 工具调用失败
168
+ - `rtc-agent-toolCallProgress` — 工具调用进度
169
+
170
+ **会话和消息事件:**
171
+
121
172
  - `rtc-session-created` / `rtc-session-switched`
122
173
  - `rtc-message-sent`
174
+
175
+ **认证事件:**
176
+
123
177
  - `rtc-auth-login-requested`
124
- - `rtc-window-minimize` / `rtc-window-maximize` / `rtc-window-restore`
125
178
 
126
179
  ### 控制器访问
127
180
 
@@ -16,7 +16,7 @@
16
16
  * }
17
17
  */
18
18
 
19
- import { existsSync, mkdirSync, copyFileSync, readFileSync, readdirSync, writeFileSync } from 'fs';
19
+ import { existsSync, mkdirSync, copyFileSync, readFileSync, readdirSync, writeFileSync, unlinkSync } from 'fs';
20
20
  import { join, dirname } from 'path';
21
21
  import { fileURLToPath } from 'url';
22
22
 
@@ -92,7 +92,23 @@ function copyWorkerFiles(targetDir) {
92
92
  console.log(`📁 Created directory: ${targetDir}`);
93
93
  }
94
94
 
95
- // Find and copy shared-worker files
95
+ // Clean up old hashed worker files
96
+ const existingFiles = existsSync(targetDir) ? readdirSync(targetDir) : [];
97
+ const oldWorkerFiles = existingFiles.filter(
98
+ f => f.startsWith('shared-worker-') && f.endsWith('.js') && f !== 'shared-worker.js'
99
+ );
100
+
101
+ for (const oldFile of oldWorkerFiles) {
102
+ const oldPath = join(targetDir, oldFile);
103
+ try {
104
+ unlinkSync(oldPath);
105
+ console.log(`🗑️ Removed old: ${oldFile}`);
106
+ } catch (err) {
107
+ console.warn(`⚠️ Could not remove ${oldFile}: ${err.message}`);
108
+ }
109
+ }
110
+
111
+ // Find the main worker file in dist
96
112
  const distFiles = readdirSync(sourceDir);
97
113
  const workerFiles = distFiles.filter(f => f.startsWith('shared-worker') && f.endsWith('.js'));
98
114
 
@@ -102,28 +118,19 @@ function copyWorkerFiles(targetDir) {
102
118
  return 0;
103
119
  }
104
120
 
105
- let copiedCount = 0;
106
- let mainWorkerFile = null;
121
+ // Find the hashed worker file
122
+ const hashedWorkerFile = workerFiles.find(f => f !== 'shared-worker.js' && f.match(/shared-worker-[A-Za-z0-9]+\.js/));
107
123
 
108
- for (const file of workerFiles) {
109
- const sourcePath = join(sourceDir, file);
110
- const targetPath = join(targetDir, file);
111
- copyFileSync(sourcePath, targetPath);
112
- console.log(`✅ Copied ${file} → ${targetDir}`);
113
- copiedCount++;
114
-
115
- // Track the main worker file (without .map or other extensions)
116
- if (file.endsWith('.js') && !file.includes('.map')) {
117
- mainWorkerFile = file;
118
- }
124
+ if (!hashedWorkerFile) {
125
+ console.warn('⚠️ Warning: No hashed worker file found');
126
+ return 0;
119
127
  }
120
128
 
121
- // Create a stable symlink or copy without hash for easy reference
122
- if (mainWorkerFile && mainWorkerFile !== 'shared-worker.js') {
123
- const stablePath = join(targetDir, 'shared-worker.js');
124
- copyFileSync(join(targetDir, mainWorkerFile), stablePath);
125
- console.log(`✅ Created stable link: shared-worker.js → ${mainWorkerFile}`);
126
- }
129
+ // Copy as shared-worker.js (stable name, no hash)
130
+ const sourcePath = join(sourceDir, hashedWorkerFile);
131
+ const targetPath = join(targetDir, 'shared-worker.js');
132
+ copyFileSync(sourcePath, targetPath);
133
+ console.log(`✅ Copied ${hashedWorkerFile} → shared-worker.js`);
127
134
 
128
135
  // Create manifest.json with version info
129
136
  const packageJsonPath = join(__dirname, '..', 'package.json');
@@ -131,7 +138,7 @@ function copyWorkerFiles(targetDir) {
131
138
 
132
139
  const manifest = {
133
140
  version: packageJson.version,
134
- workerFile: mainWorkerFile,
141
+ workerFile: 'shared-worker.js',
135
142
  stableWorkerUrl: '/rtc-agent/shared-worker.js',
136
143
  timestamp: new Date().toISOString(),
137
144
  };
@@ -142,7 +149,7 @@ function copyWorkerFiles(targetDir) {
142
149
  );
143
150
  console.log(`✅ Created manifest.json (version ${packageJson.version})`);
144
151
 
145
- return copiedCount;
152
+ return 1;
146
153
  }
147
154
 
148
155
  /**