refactor: 项目更名 WeKnora → Academy
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s

- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
This commit is contained in:
runny committed 2026-11-13 20:52:51 +08:00
1 parent 9097ee699d
commit 9058149576
3136 files changed
+18018 -18004

No files matched your search

+29 -29
View File
@@ -1,6 +1,6 @@
# 网页嵌入(Embed Channel)
嵌入渠道用于在官网或帮助中心提供知识库问答挂件。创建渠道并绑定智能体后,将生成的脚本加入网页,访客无需 WeKnora 账号即可对话。访客的检索范围和模型完全由渠道绑定的智能体决定:服务端会丢弃请求里的知识库、文档、标签、@提及、技能和模型覆盖。
嵌入渠道用于在官网或帮助中心提供知识库问答挂件。创建渠道并绑定智能体后,将生成的脚本加入网页,访客无需 Academy 账号即可对话。访客的检索范围和模型完全由渠道绑定的智能体决定:服务端会丢弃请求里的知识库、文档、标签、@提及、技能和模型覆盖。
在「设置 → 网页嵌入」新建渠道,绑定智能体并设置允许嵌入的域名,然后复制接入代码。公开使用前应配置域名白名单和限流,限制访问来源与请求量。
@@ -15,13 +15,13 @@
## 前端挂件接入
挂件 SDK 是一个无依赖的 loader 脚本 `frontend/public/weknora-widget.js`(部署后从 WeKnora 服务根路径提供),负责渲染悬浮按钮 + iframe 面板,iframe 指向嵌入页 SPA `/embed/{channel_id}`(入口 `frontend/src/embed-main.ts`)。
挂件 SDK 是一个无依赖的 loader 脚本 `frontend/public/academy-widget.js`(部署后从 Academy 服务根路径提供),负责渲染悬浮按钮 + iframe 面板,iframe 指向嵌入页 SPA `/embed/{channel_id}`(入口 `frontend/src/embed-main.ts`)。
### 方式一:静态 Token 模式(token 对访客可见) {#方式一-静态-token-模式-最简单-token-暴露在页面}
```html
<script
src="https://your-weknora.example.com/weknora-widget.js"
src="https://your-academy.example.com/academy-widget.js"
data-channel="你的渠道UUID"
data-token="em_你的publish_token"
data-position="bottom-right"
@@ -38,37 +38,37 @@ publish token 只保存在业务后端,页面通过 `data-token-endpoint` 指
```html
<script
src="https://your-weknora.example.com/weknora-widget.js"
src="https://your-academy.example.com/academy-widget.js"
data-channel="你的渠道UUID"
data-token-endpoint="https://your-backend.example.com/weknora/embed-token"
data-token-endpoint="https://your-backend.example.com/academy/embed-token"
data-position="bottom-right"
></script>
```
业务后端实现该 endpoint:服务端持有 `em_` token,调用 `POST /api/v1/embed/{channel_id}/exchange` 换取 `ems_` 短效 token 并返回 `{ "token": "ems_...", "expiresIn": 1800 }`。挂件会在约 80% TTL 时(不早于 30 秒)自动刷新 token(见 `weknora-widget.js` 中的 `scheduleRefresh`)。**publish token 永不到达浏览器。**
业务后端实现该 endpoint:服务端持有 `em_` token,调用 `POST /api/v1/embed/{channel_id}/exchange` 换取 `ems_` 短效 token 并返回 `{ "token": "ems_...", "expiresIn": 1800 }`。挂件会在约 80% TTL 时(不早于 30 秒)自动刷新 token(见 `academy-widget.js` 中的 `scheduleRefresh`)。**publish token 永不到达浏览器。**
换取接口必须先验证业务侧 Session/JWT 的有效性和访客访问权;仅判断 Cookie 或 Authorization 头是否存在不构成身份验证。服务端调用 exchange 时须手动发送与渠道白名单一致的业务宿主 `Origin`,例如 `Origin: https://shop.example.com`;不要把 WeKnora 的管理 Token 暴露给访客。
换取接口必须先验证业务侧 Session/JWT 的有效性和访客访问权;仅判断 Cookie 或 Authorization 头是否存在不构成身份验证。服务端调用 exchange 时须手动发送与渠道白名单一致的业务宿主 `Origin`,例如 `Origin: https://shop.example.com`;不要把 Academy 的管理 Token 暴露给访客。
其余可选属性:`data-base-url`(默认从 script src 推导)、`data-width` / `data-height`(面板尺寸,默认 400×600)、`data-sandbox`(iframe sandbox 策略;跨域嵌入时自动加 `allow-scripts allow-forms allow-popups allow-modals allow-same-origin`)。
### 方式三:编程式 API
```html
<script src="https://your-weknora.example.com/weknora-widget.js"></script>
<script src="https://your-academy.example.com/academy-widget.js"></script>
<script>
WeKnora.init({
Academy.init({
channel: '渠道UUID',
tokenEndpoint: 'https://your-backend.example.com/weknora/embed-token', // 或 token: 'em_...'
tokenEndpoint: 'https://your-backend.example.com/academy/embed-token', // 或 token: 'em_...'
position: 'bottom-right',
primaryColor: '#07C05F',
title: 'AI Assistant',
baseUrl: 'https://your-weknora.example.com',
baseUrl: 'https://your-academy.example.com',
});
WeKnora.setContext({ userId: 'u_123', page: location.pathname }); // 上下文随每次提问注入
WeKnora.setLocale('en-US');
WeKnora.openWithQuery('如何重置密码?'); // 打开面板并自动发送提问
WeKnora.on('ready', () => console.log('widget ready'));
// 其他:WeKnora.open() / close() / toggle() / destroy() / off(event, fn)
Academy.setContext({ userId: 'u_123', page: location.pathname }); // 上下文随每次提问注入
Academy.setLocale('en-US');
Academy.openWithQuery('如何重置密码?'); // 打开面板并自动发送提问
Academy.on('ready', () => console.log('widget ready'));
// 其他:Academy.open() / close() / toggle() / destroy() / off(event, fn)
</script>
```
@@ -77,7 +77,7 @@ publish token 只保存在业务后端,页面通过 `data-token-endpoint` 指
也可以不用 loader,直接内嵌 iframe(此时需要通过 URL/postMessage 提供 token,通常建议使用 loader):
```html
<iframe src="https://your-weknora.example.com/embed/渠道UUID"
<iframe src="https://your-academy.example.com/embed/渠道UUID"
width="400" height="600" style="border:none"></iframe>
```
@@ -85,8 +85,8 @@ publish token 只保存在业务后端,页面通过 `data-token-endpoint` 指
宿主页(loader)与 iframe 内嵌入页之间通过 `postMessage` 通信,双方都做严格的 Origin 校验(loader 只向推导出的 `embedOrigin` 发消息,绝不使用 `*`;嵌入页对首个可信消息做 origin 固定 —— 见 `frontend/src/composables/useEmbedBridge.ts`):
- 宿主 → iframe(`source: "weknora-host"`):`provide_token`(下发 token)、`set_context`、`set_locale`、`open_with_query`;
- iframe → 宿主(`source: "weknora-embed"`):`ready`、`bootstrap_request`(请求 token)、`message_sent`、`message_received`。
- 宿主 → iframe(`source: "academy-host"`):`provide_token`(下发 token)、`set_context`、`set_locale`、`open_with_query`;
- iframe → 宿主(`source: "academy-embed"`):`ready`、`bootstrap_request`(请求 token)、`message_sent`、`message_received`。
## 鉴权与匿名会话
@@ -109,7 +109,7 @@ publish token 只保存在业务后端,页面通过 `data-token-endpoint` 指
### 宿主来源与部署
A 网站嵌入 B 的 WeKnora 时,白名单填 A。标准 Nginx 使用 `/api/v1/embed-frame-policy` 获取渠道策略(无需 token,仅返回 CSP,不返回渠道配置),并在 `/embed/:channelId` 的 HTML 响应中设置 `frame-ancestors`;Lite 使用同一策略。该页面不缓存,策略获取失败时不返回嵌入 HTML。
A 网站嵌入 B 的 Academy 时,白名单填 A。标准 Nginx 使用 `/api/v1/embed-frame-policy` 获取渠道策略(无需 token,仅返回 CSP,不返回渠道配置),并在 `/embed/:channelId` 的 HTML 响应中设置 `frame-ancestors`;Lite 使用同一策略。该页面不缓存,策略获取失败时不返回嵌入 HTML。
升级时,过去仅填 B 的渠道需改填实际宿主 A,并同时更新前后端。自定义反向代理需保留 CSP、原始 Host(含端口)、协议及 `Sec-Fetch-Site`。白名单限制浏览器嵌入,不能代替访客认证或阻止持有 token 的非浏览器客户端;此类访问控制使用安全模式和限流。
@@ -119,7 +119,7 @@ A 网站嵌入 B 的 WeKnora 时,白名单填 A。标准 Nginx 使用 `/api/v1
管理端通过 `frontend/public/config.js` 的 `window.__RUNTIME_CONFIG__.EMBED_BASE_URL` 指定 embed 源站,或使用构建期 `VITE_EMBED_BASE_URL`;留空则跟随当前页面 origin。修改后确认生成的 Widget/iframe 代码指向新地址。
独立 Nginx server 只提供 `/embed/*`、`/weknora-widget.js`、`/assets/*` 和必要的后端代理,不挂管理端 `index.html`。保留标准 `frontend/nginx.conf` 中 `/embed/` 的 `auth_request`、内部 `/_embed-frame-policy` 和 CSP 响应头:获取策略失败时必须拒绝返回页面,网关/CDN 不应缓存或丢弃该策略。
独立 Nginx server 只提供 `/embed/*`、`/academy-widget.js`、`/assets/*` 和必要的后端代理,不挂管理端 `index.html`。保留标准 `frontend/nginx.conf` 中 `/embed/` 的 `auth_request`、内部 `/_embed-frame-policy` 和 CSP 响应头:获取策略失败时必须拒绝返回页面,网关/CDN 不应缓存或丢弃该策略。
白名单仍填写实际业务宿主 `https://shop.example.com`,安全模式 exchange 声明相同 Origin;不需要为正常同源聊天请求额外加入 embed 源站。Widget 在宿主与 embed 不同源时自动添加 iframe sandbox。验证时从实际宿主打开页面,检查 iframe、API、CSP 和生成代码,不能只在管理端预览。
@@ -157,8 +157,8 @@ A 网站嵌入 B 的 WeKnora 时,白名单填 A。标准 Nginx 使用 `/api/v1
安全与投递语义:
- 配置了 `webhook_secret` 时附带签名头 `X-WeKnora-Signature: sha256=<hex(HMAC-SHA256(secret, raw_body))>`;
- URL 必须 HTTPS,出站请求走 SSRF 安全客户端(每次重定向重新校验,最多 5 跳),超时 5 秒,User-Agent 为 `WeKnora-Embed-Webhook/1.0`;
- 配置了 `webhook_secret` 时附带签名头 `X-Academy-Signature: sha256=<hex(HMAC-SHA256(secret, raw_body))>`;
- URL 必须 HTTPS,出站请求走 SSRF 安全客户端(每次重定向重新校验,最多 5 跳),超时 5 秒,User-Agent 为 `Academy-Embed-Webhook/1.0`;
- 异步 best-effort 投递,失败仅记录日志、**不重试**;
- 前端也可通过 `POST /api/v1/embed/:channel_id/sessions/:session_id/events` 显式转发事件。
@@ -203,7 +203,7 @@ type EmbedChannel struct {
| `name` | string | — | 渠道显示名称 |
| `enabled` | bool | `true` | 渠道开关,关闭后所有公开接口拒绝访问 |
| `agent_id` | string | `builtin-quick-answer` | 绑定的 Agent,决定知识库范围与对话能力 |
| `allowed_origins` | string[] | — | **必填至少一项,填写嵌入宿主 A,不是 WeKnora 地址 B**。支持三种形式:完整 `http(s)://` Origin(不含路径、查询参数)、子域名通配 `*.example.com`(只匹配子域,不含 `example.com` 本身;未写端口时匹配任意端口)、全通配 `*`(`GIN_MODE=release` 时拒绝保存,仅供开发) |
| `allowed_origins` | string[] | — | **必填至少一项,填写嵌入宿主 A,不是 Academy 地址 B**。支持三种形式:完整 `http(s)://` Origin(不含路径、查询参数)、子域名通配 `*.example.com`(只匹配子域,不含 `example.com` 本身;未写端口时匹配任意端口)、全通配 `*`(`GIN_MODE=release` 时拒绝保存,仅供开发) |
| `welcome_message` | string | 空 | 打开挂件时的欢迎语 |
| `rate_limit_per_minute` | int | `30` | 单 IP 每分钟请求上限 |
| `rate_limit_per_day` | int | `10000` | 渠道级每日请求总量上限 |
@@ -271,17 +271,17 @@ embed := r.Group("/api/v1/embed/:channel_id", middleware.EmbedAuth(embedService,
sequenceDiagram
autonumber
participant Visitor as "访客浏览器"
participant Host as "宿主页面 (weknora-widget.js)"
participant Host as "宿主页面 (academy-widget.js)"
participant Backend as "业务后端 (安全模式可选)"
participant Iframe as "嵌入页 SPA (/embed/:channel_id)"
participant API as "WeKnora API (/api/v1/embed/:channel_id)"
participant API as "Academy API (/api/v1/embed/:channel_id)"
participant Webhook as "站长 Webhook"
Visitor->>Host: 加载页面, script 标签自动初始化
Host->>Iframe: 创建 iframe (悬浮面板)
Iframe-->>Host: postMessage "bootstrap_request"
alt 安全模式 (data-token-endpoint)
Host->>Backend: GET /weknora/embed-token
Host->>Backend: GET /academy/embed-token
Backend->>API: POST /exchange (Authorization: Embed em_...)
API-->>Backend: "{ session_token: ems_..., expires_in: 1800 }"
Backend-->>Host: "{ token: ems_... }"
@@ -296,7 +296,7 @@ sequenceDiagram
Note over Iframe: session id + sig 存入 localStorage
Visitor->>Iframe: 输入问题
Iframe->>API: POST /agent-chat/:session_id (X-Embed-Session: sig)
API-->>Webhook: 异步 POST message_sent (X-WeKnora-Signature)
API-->>Webhook: 异步 POST message_sent (X-Academy-Signature)
API-->>Iframe: SSE 流式回复
API-->>Webhook: 异步 POST message_received
Iframe-->>Host: postMessage "message_received"
@@ -324,6 +324,6 @@ sequenceDiagram
| 鉴权中间件 | `internal/middleware/embed_auth.go` |
| 路由注册 | `internal/router/routes_agent.go`(`RegisterEmbedPublicRoutes` / `RegisterEmbedChannelRoutes` / 嵌入页 CSP 策略) |
| 宿主来源规则 | `internal/embedpolicy/origin.go` |
| 挂件加载器(SDK) | `frontend/public/weknora-widget.js` |
| 挂件加载器(SDK) | `frontend/public/academy-widget.js` |
| 嵌入页 SPA 入口 | `frontend/src/embed-main.ts`、`frontend/src/composables/useEmbedBridge.ts`、`useEmbedChatSession.ts` |
| 数据库迁移 | `migrations/versioned/000060_embed_channels.up.sql` |