快速体验
适合:想用最少的改动体验 RTC Agent 的能力。你只需要一个静态页面(哪怕只是一个 HTML 文件),不需要写后端 API。
- ✅ 完成快速入门
- GitHub 账号(用于登录体验)
Step 1: 配置 GitHub OAuth2
Section titled “Step 1: 配置 GitHub OAuth2”1.1 创建 GitHub OAuth App
Section titled “1.1 创建 GitHub OAuth App”- 访问 GitHub Developer Settings
- 点击 New OAuth App
- 填写信息:
- Application name: 任意名称,如
RTC Agent Demo - Homepage URL:
http://localhost:28080(Server 地址) - Authorization callback URL:
http://localhost:28080/auth/callback.html
- Application name: 任意名称,如
- 点击 Register application
- 生成一个新的 Client Secret
💡 端口说明:以上示例假设你的网站运行在
localhost:28080(与 Server 同端口)。如果你的应用运行在其他端口(如3000、5173),请将 GitHub OAuth 配置和下文代码中的localhost:28080替换为你的实际地址。生产环境请替换为https://your-app.com。
1.2 配置 Server
Section titled “1.2 配置 Server”编辑 .env:
PROVIDERS__GITHUB__ENABLED=truePROVIDERS__GITHUB__CLIENT_ID=你的 Client IDPROVIDERS__GITHUB__CLIENT_SECRET=你的 Client Secret💡 配置文件说明:Docker Compose 部署时,Server 配置使用
etc/config.docker.yaml,Admin Server 使用etc/admin.docker.yaml(自动挂载到容器内)。如需修改高级配置(如 LLM 模型、CORS 等),直接编辑这两个文件。本地开发时则使用etc/config.yaml.example(复制为config.yaml)和etc/admin.yaml。
重启 Server:
docker compose restart server1.3 创建 OAuth 回调页面
Section titled “1.3 创建 OAuth 回调页面”在你的宿主应用(不是 server 仓库)中创建一个回调页面。例如,如果你的网站运行在 http://localhost:28080,创建 public/auth/callback.html:
<!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8"> <title>授权完成</title> <style> body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; display: flex; align-items: center; justify-content: center; min-height: 100vh; margin: 0; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); } .container { background: white; border-radius: 16px; padding: 48px; text-align: center; max-width: 400px; } .icon { font-size: 48px; margin-bottom: 16px; } </style></head><body> <div class="container"> <div class="icon" id="icon">⏳</div> <h1 id="title">处理中...</h1> <p id="message">请稍候,正在完成授权</p> </div>
<script> (function() { var params = new URLSearchParams(window.location.search); var code = params.get('code'); var state = params.get('state'); var error = params.get('error');
var icon = document.getElementById('icon'); var title = document.getElementById('title'); var message = document.getElementById('message');
if (error) { icon.textContent = '✕'; title.textContent = '授权失败'; message.textContent = error; return; }
if (code && state) { var target = window.opener || window.parent; if (target) { target.postMessage({ type: 'oauth-callback', code: code, state: state, }, '*'); // 生产环境建议替换为具体的 origin,如 'https://your-app.com' icon.textContent = '✓'; title.textContent = '授权成功'; message.textContent = '正在返回应用...'; setTimeout(function() { window.close(); }, 2000); } } })(); </script></body></html>💡 文件位置:这个文件应该放在你的宿主应用的静态资源目录中(如
public/),确保可以通过http://localhost:28080/auth/callback.html访问。
Step 2: 嵌入前端组件
Section titled “Step 2: 嵌入前端组件”选择一种方式将 RTC Agent 嵌入你的页面:
方式 A:CDN 接入(最简单)
Section titled “方式 A:CDN 接入(最简单)”在你的 HTML 页面中添加:
<!-- 引入组件 --><script type="module" src="https://cdn.jsdelivr.net/npm/@rtc-agent/component@latest/dist/index.js"></script>
<!-- 使用工厂函数配置 --><script type="module"> import { createRtcAgent } from 'https://cdn.jsdelivr.net/npm/@rtc-agent/component@latest/dist/index.js';
const agent = createRtcAgent({ appLabel: '我的 AI 助手', server: { url: 'http://localhost:28080', redirectUri: '/auth/callback.html', // Step 1.3 创建的回调页面 }, workerURL: '/rtc-agent/shared-worker.js', // SharedWorker 文件路径,详见下方说明 });
document.body.appendChild(agent);</script>刷新页面即可看到右下角的 AI 助手气泡。
💡 SharedWorker 文件:CDN 方式也需要 SharedWorker 文件。从 GitHub 下载:shared-worker.js,放到你网站的
public/rtc-agent/shared-worker.js即可。
方式 B:NPM 接入
Section titled “方式 B:NPM 接入”安装依赖:
pnpm add @rtc-agent/component配置 SharedWorker:
在 package.json 中添加 postinstall 脚本:
{ "scripts": { "postinstall": "rtc-agent-setup" }}⚠️ 重要:每次升级
@rtc-agent/component时,需要重新运行rtc-agent-setup来更新 SharedWorker 文件。使用postinstall可以自动化这个过程,也可以手动运行npx rtc-agent-setup。
首次运行:
npx rtc-agent-setup使用组件:
import { createRtcAgent } from '@rtc-agent/component';
const agent = createRtcAgent({ appLabel: '我的 AI 助手', server: { url: 'http://localhost:28080' }, workerURL: '/rtc-agent/shared-worker.js',});
document.body.appendChild(agent);Step 3: 验证
Section titled “Step 3: 验证”打开你的页面,点击右下角的 AI 助手气泡:
- 点击 登录 按钮
- 使用 GitHub 账号授权
- 开始与 AI 对话!
| 示例 | 接入方式 | 认证 | 说明 | 代码 |
|---|---|---|---|---|
| 官方文档站 | CDN | GitHub OAuth2 | Astro + Starlight 文档站 | astro.config.mjs |
| Mermaid Live Editor | NPM | GitHub OAuth2 | SvelteKit 图表编辑器 | +layout.svelte |
GitHub OAuth 授权后无法登录?
- 检查
.env中PROVIDERS__GITHUB__ENABLED=true且 Client ID/Secret 正确 - 确认 GitHub OAuth App 的回调地址与
redirectUri配置一致 - 重启 Server:
docker compose restart server
前端页面没有显示 AI 助手气泡?
- 检查浏览器控制台是否有 JavaScript 错误
- 确认 CDN 脚本加载成功:
https://cdn.jsdelivr.net/npm/@rtc-agent/component@latest/dist/index.js - 确认
server.url指向正确的 Server 地址
SharedWorker 404 错误(NPM 方式)?
- 确认已运行
npx rtc-agent-setup - 检查
public/rtc-agent/shared-worker.js文件是否存在 - 确认
workerURL路径与实际文件位置匹配
- Web Component API — 了解全部配置选项
- Function 注册 — 让 AI 调用你的业务 API
- Scenario 编写 — 给 AI 提供业务上下文
- 真实接入 — 将 RTC Agent 集成到生产应用