@rtc-agent/component 0.2.5 → 0.2.6-rc.1
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 +60 -0
- package/bin/rtc-agent-setup.js +196 -0
- package/dist/assets/{shared-worker-BEaT-a-g.js → shared-worker-D2gLjnH6.js} +85 -85
- package/dist/{en-US-UrEM8FV8.js → en-US-BGIY9iSK.js} +1 -1
- package/dist/{index-_sFAoDQb.js → index-DF_oHmgJ.js} +1 -1
- package/dist/{index-CoqPDxYj.js → index-KpcYA2oO.js} +5263 -4874
- package/dist/index.js +13 -12
- package/dist/index.umd.js +200 -200
- package/package.json +10 -5
package/README.md
CHANGED
|
@@ -2,6 +2,55 @@
|
|
|
2
2
|
|
|
3
3
|
基于 Lit 的 Web Component 组件库,提供浮窗式 RTC Agent 交互界面。
|
|
4
4
|
|
|
5
|
+
## 快速开始
|
|
6
|
+
|
|
7
|
+
### 安装
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @rtc-agent/component
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### 使用工厂函数(推荐)
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createRtcAgent } from '@rtc-agent/component';
|
|
17
|
+
|
|
18
|
+
const agent = createRtcAgent({
|
|
19
|
+
appLabel: 'My Assistant',
|
|
20
|
+
server: { url: 'https://api.example.com' },
|
|
21
|
+
auth: {
|
|
22
|
+
getToken: () => localStorage.getItem('token'),
|
|
23
|
+
refreshToken: async () => {
|
|
24
|
+
const res = await fetch('/api/refresh');
|
|
25
|
+
const data = await res.json();
|
|
26
|
+
return { accessToken: data.token };
|
|
27
|
+
},
|
|
28
|
+
userId: 'user-123',
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
document.body.appendChild(agent);
|
|
33
|
+
|
|
34
|
+
// 销毁
|
|
35
|
+
agent.destroy();
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
完整 API 文档见 [API.md](./API.md),更多示例见 [EXAMPLES.md](./EXAMPLES.md)。
|
|
39
|
+
|
|
40
|
+
### 使用 HTML 元素
|
|
41
|
+
|
|
42
|
+
```html
|
|
43
|
+
<script type="module">
|
|
44
|
+
import '@rtc-agent/component';
|
|
45
|
+
</script>
|
|
46
|
+
|
|
47
|
+
<rtc-agent
|
|
48
|
+
theme="system"
|
|
49
|
+
app-label="RTC Agent"
|
|
50
|
+
scenarios-url="./scenarios/"
|
|
51
|
+
></rtc-agent>
|
|
52
|
+
```
|
|
53
|
+
|
|
5
54
|
## 导出
|
|
6
55
|
|
|
7
56
|
### 组件
|
|
@@ -18,6 +67,17 @@ import { RtcAgent } from '@rtc-agent/component';
|
|
|
18
67
|
const agent = document.querySelector<RtcAgent>('rtc-agent')!;
|
|
19
68
|
```
|
|
20
69
|
|
|
70
|
+
### 工厂函数
|
|
71
|
+
|
|
72
|
+
- `createRtcAgent` — 声明式创建 `<rtc-agent>` 实例
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { createRtcAgent } from '@rtc-agent/component';
|
|
76
|
+
const agent = createRtcAgent({ /* ... */ });
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
详细配置见 [API.md](./API.md)。
|
|
80
|
+
|
|
21
81
|
## 用法
|
|
22
82
|
|
|
23
83
|
```html
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* RTC Agent SharedWorker Setup Tool
|
|
5
|
+
*
|
|
6
|
+
* Automatically copies the SharedWorker file to the host application's public directory.
|
|
7
|
+
* This solves the NPM distribution problem where the worker file inside node_modules
|
|
8
|
+
* cannot be accessed by the browser.
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* npx rtc-agent-setup
|
|
12
|
+
*
|
|
13
|
+
* Or in package.json:
|
|
14
|
+
* "scripts": {
|
|
15
|
+
* "postinstall": "rtc-agent-setup"
|
|
16
|
+
* }
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { existsSync, mkdirSync, copyFileSync, readFileSync, readdirSync, writeFileSync, unlinkSync } from 'fs';
|
|
20
|
+
import { join, dirname } from 'path';
|
|
21
|
+
import { fileURLToPath } from 'url';
|
|
22
|
+
|
|
23
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
24
|
+
const __dirname = dirname(__filename);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Detect project type based on dependencies
|
|
28
|
+
*/
|
|
29
|
+
function detectProjectType(cwd) {
|
|
30
|
+
const packageJsonPath = join(cwd, 'package.json');
|
|
31
|
+
if (!existsSync(packageJsonPath)) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
|
|
36
|
+
const allDeps = {
|
|
37
|
+
...packageJson.dependencies,
|
|
38
|
+
...packageJson.devDependencies,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Detect SvelteKit first (more specific, also uses Vite)
|
|
42
|
+
if (allDeps?.['@sveltejs/kit']) {
|
|
43
|
+
return 'sveltekit';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Detect Vite (generic)
|
|
47
|
+
if (allDeps?.vite) {
|
|
48
|
+
return 'vite';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Detect Webpack
|
|
52
|
+
if (allDeps?.webpack) {
|
|
53
|
+
return 'webpack';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return 'generic';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get target directory based on project type
|
|
61
|
+
*/
|
|
62
|
+
function getTargetDir(projectType, cwd) {
|
|
63
|
+
switch (projectType) {
|
|
64
|
+
case 'sveltekit':
|
|
65
|
+
// SvelteKit uses 'static' directory for static assets
|
|
66
|
+
return join(cwd, 'static', 'rtc-agent');
|
|
67
|
+
case 'vite':
|
|
68
|
+
// Plain Vite projects typically use 'public' directory
|
|
69
|
+
return join(cwd, 'public', 'rtc-agent');
|
|
70
|
+
case 'webpack':
|
|
71
|
+
return join(cwd, 'static', 'rtc-agent');
|
|
72
|
+
default:
|
|
73
|
+
return join(cwd, 'public', 'rtc-agent');
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Copy SharedWorker files to target directory
|
|
79
|
+
*/
|
|
80
|
+
function copyWorkerFiles(targetDir) {
|
|
81
|
+
const sourceDir = join(__dirname, '..', 'dist', 'assets');
|
|
82
|
+
|
|
83
|
+
if (!existsSync(sourceDir)) {
|
|
84
|
+
console.error('❌ Error: Cannot find @rtc-agent/component dist files');
|
|
85
|
+
console.error(' Please ensure the package is built correctly');
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Create target directory
|
|
90
|
+
if (!existsSync(targetDir)) {
|
|
91
|
+
mkdirSync(targetDir, { recursive: true });
|
|
92
|
+
console.log(`📁 Created directory: ${targetDir}`);
|
|
93
|
+
}
|
|
94
|
+
|
|
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
|
|
112
|
+
const distFiles = readdirSync(sourceDir);
|
|
113
|
+
const workerFiles = distFiles.filter(f => f.startsWith('shared-worker') && f.endsWith('.js'));
|
|
114
|
+
|
|
115
|
+
if (workerFiles.length === 0) {
|
|
116
|
+
console.warn('⚠️ Warning: No shared-worker files found in dist/assets');
|
|
117
|
+
console.log(' Available files:', distFiles.join(', '));
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
|
|
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/));
|
|
123
|
+
|
|
124
|
+
if (!hashedWorkerFile) {
|
|
125
|
+
console.warn('⚠️ Warning: No hashed worker file found');
|
|
126
|
+
return 0;
|
|
127
|
+
}
|
|
128
|
+
|
|
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`);
|
|
134
|
+
|
|
135
|
+
// Create manifest.json with version info
|
|
136
|
+
const packageJsonPath = join(__dirname, '..', 'package.json');
|
|
137
|
+
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
|
|
138
|
+
|
|
139
|
+
const manifest = {
|
|
140
|
+
version: packageJson.version,
|
|
141
|
+
workerFile: 'shared-worker.js',
|
|
142
|
+
stableWorkerUrl: '/rtc-agent/shared-worker.js',
|
|
143
|
+
timestamp: new Date().toISOString(),
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
writeFileSync(
|
|
147
|
+
join(targetDir, 'manifest.json'),
|
|
148
|
+
JSON.stringify(manifest, null, 2)
|
|
149
|
+
);
|
|
150
|
+
console.log(`✅ Created manifest.json (version ${packageJson.version})`);
|
|
151
|
+
|
|
152
|
+
return 1;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Main entry point
|
|
157
|
+
*/
|
|
158
|
+
async function main() {
|
|
159
|
+
console.log('🔧 RTC Agent SharedWorker Setup\n');
|
|
160
|
+
|
|
161
|
+
const cwd = process.cwd();
|
|
162
|
+
const projectType = detectProjectType(cwd);
|
|
163
|
+
|
|
164
|
+
if (!projectType) {
|
|
165
|
+
console.error('❌ Error: No package.json found in current directory');
|
|
166
|
+
process.exit(1);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
console.log(`📦 Detected project type: ${projectType}`);
|
|
170
|
+
|
|
171
|
+
const targetDir = getTargetDir(projectType, cwd);
|
|
172
|
+
console.log(`📂 Target directory: ${targetDir}\n`);
|
|
173
|
+
|
|
174
|
+
const copiedFiles = copyWorkerFiles(targetDir);
|
|
175
|
+
|
|
176
|
+
if (copiedFiles > 0) {
|
|
177
|
+
console.log('\n✅ Setup complete!');
|
|
178
|
+
console.log('\nNext steps:');
|
|
179
|
+
console.log(' 1. SharedWorker files are now available in your public directory');
|
|
180
|
+
console.log(' 2. Configure createRtcAgent() to use the worker:');
|
|
181
|
+
console.log(' createRtcAgent({');
|
|
182
|
+
console.log(' workerUrl: \'/rtc-agent/shared-worker.js\'');
|
|
183
|
+
console.log(' })');
|
|
184
|
+
console.log(' 3. When upgrading @rtc-agent/component, run:');
|
|
185
|
+
console.log(' npx rtc-agent-setup');
|
|
186
|
+
console.log(' This will update the worker files and manifest.json');
|
|
187
|
+
} else {
|
|
188
|
+
console.log('\n⚠️ Setup completed with warnings');
|
|
189
|
+
console.log(' Please check if @rtc-agent/component is installed correctly');
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
main().catch(err => {
|
|
194
|
+
console.error('❌ Setup failed:', err);
|
|
195
|
+
process.exit(1);
|
|
196
|
+
});
|