feat: add ai gateway local core flow

This commit is contained in:
2026-05-09 16:51:28 +08:00
parent 5b20f017eb
commit c0335bd5d0
20 changed files with 2332 additions and 156 deletions
+14 -15
View File
@@ -153,19 +153,21 @@ Gateway 本地登录首期只签发短期 access token,不做 refresh token
| Method | Path | Permission | 说明 |
| --- | --- | --- | --- |
| `POST` | `/api/v1/auth/register` | `public` | 注册 Gateway 本地账号,创建或加入租户,返回 access token |
| `POST` | `/api/v1/auth/register` | `public` | 注册 Gateway 本地账号,进入默认租户;邀请码仅记录邀请关系,返回 access token |
| `POST` | `/api/v1/auth/login` | `public` | 本地账号登录,返回 access token |
注册字段:
- `username` / `email`:至少一个必填。
- `password`:至少 8 位,落库为 bcrypt hash。
- `tenantKey` / `tenantName`:可选;未传时创建个人租户
- `displayName`:可选
- `invitationCode`:可选;填写后只校验并记录邀请关系,不改变租户、用户组或角色。
安全边界:
- 普通注册首期只给 `user` 角色
- 允许注册时填写已有 `tenantKey` 只是脚手架行为;正式上线前需要租户邀请、域名校验或管理员审批
- 普通注册界面不展示、不提交租户 Key 和租户名称,单租户场景下后端创建或复用 `tenant_key=default` 的默认租户
- 普通注册首期默认给 `user` 角色;首次初始化用户可作为本地闭环的全局管理员引导账号
- 多租户加入后续通过域名校验、管理员审批或 `server-main` 同步等受控入口完成,不由普通注册邀请码决定。
- `server-main` 模式可关闭本地注册登录,只保留外部 token 入口。
### 5.2 权限等级
@@ -315,8 +317,8 @@ AI Gateway 的外部接口必须以“兼容现有 EasyAI 路由、请求 DTO、
| `GET` | `/api/v1/tenants` | `power` | 网关租户列表,支持本地租户和 server-main 同步租户 |
| `GET` | `/api/v1/tenants/:id` | `power` | 租户详情、来源、策略和同步状态 |
| `POST` | `/api/v1/tenants/sync` | `power` | 从 `server-main` 拉取或接收租户同步 |
| `GET` | `/api/v1/tenant-invitations` | `power` | 本地租户邀请码列表 |
| `POST` | `/api/v1/tenant-invitations` | `power` | 创建本地注册邀请码 |
| `GET` | `/api/v1/invitations` | `power` | 本地注册邀请码列表 |
| `POST` | `/api/v1/invitations` | `power` | 创建本地注册邀请码 |
| `GET` | `/api/v1/users` | `power` | 网关用户列表,支持本地用户和 server-main 同步用户 |
| `GET` | `/api/v1/users/:id` | `power` | 用户详情、来源、角色、用户组和同步状态 |
| `POST` | `/api/v1/users/sync` | `power` | 从 `server-main` 拉取或接收用户同步 |
@@ -375,8 +377,8 @@ Gateway 需要支持三种身份运行模式,默认配置为 `IDENTITY_MODE=hy
本地注册规则:
- 普通注册默认开放,`invitationCode` 可选;不填邀请码时创建或复用 `tenantKey` 对应的 Gateway 本地租户,未填 `tenantKey` 时生成个人租户
- 填写邀请码时必须命中 `gateway_tenant_invitations` 的有效记录,注册用户加入邀请码指定租户,并可绑定邀请码指定用户组
- 普通注册默认开放,`invitationCode` 可选;不填邀请码时创建或复用 Gateway 本地默认租户 `tenant_key=default`,不允许普通用户在注册界面填写租户 Key 或租户名称
- 填写邀请码时必须命中 `gateway_invitations` 的有效记录;邀请码只用于标识邀请关系、更新使用次数和写入用户注册 metadata,不参与租户、用户组或角色分配
- 平台凭证、provider 凭证和全局模型配置暂时只允许全局管理员维护;租户侧先只做模型使用、用量查看和策略展示。
身份解析流程:
@@ -783,17 +785,14 @@ Gateway 需要支持三种身份运行模式,默认配置为 `IDENTITY_MODE=hy
- 接入 `server-main` 时可使用 `principal_type='user' + external_user_id`,并通过 `gateway_users.source='server-main'` 反查同步副本。
- 多个组同时命中时,先按 `gateway_user_group_memberships.priority`,再按 `gateway_user_groups.priority` 合并策略。
#### 7.9.8 `gateway_tenant_invitations`
#### 7.9.8 `gateway_invitations`
本地注册邀请码。邀请码不是普通注册的必填项,但填写后必须可校验,决定用户加入哪个租户用户组。
本地注册邀请码。邀请码不是普通注册的必填项填写后必须可校验,但当前阶段只标识邀请关系,不决定用户加入哪个租户用户组或角色
| 字段 | 类型 | 约束 | 说明 |
| --- | --- | --- | --- |
| `id` | `uuid` | PK | 邀请 ID |
| `tenant_id` | `uuid` | FK | 加入的 Gateway 租户 |
| `invite_code` | `text` | unique, not null | 注册邀请码 |
| `role` | `text` | not null, default `user` | 注册后角色,默认普通用户 |
| `user_group_id` | `uuid` | FK nullable | 注册后默认用户组 |
| `max_uses` | `int` | nullable | 最大使用次数 |
| `used_count` | `int` | not null | 已使用次数 |
| `expires_at` | `timestamptz` | nullable | 过期时间 |
@@ -1838,7 +1837,7 @@ type ServerMainUploadClient interface {
| 页面 | 路由 | 权限 | 说明 |
| --- | --- | --- | --- |
| 登录 | `/login` | `public` | 用户名/邮箱 + 密码登录 Gateway 本地账号 |
| 注册 | `/register` | `public` | 创建 Gateway 本地账号;可填写租户 key / 租户名称 / 邀请码,邀请码不是必填 |
| 注册 | `/register` | `public` | 创建 Gateway 本地账号;可填写邀请码,租户字段不出现在普通注册界面 |
| 外部 Token 入口 | `/login?mode=external` | `public` | 粘贴 `server-main` access token,用于接入模式和开发调试 |
登录成功后统一获得 Gateway 可校验的 access token;本地账号 token 中包含 `source=gateway``gatewayUserId``gatewayTenantId``tenantKey`。外部 token 保留 `source=server-main` 或主服务返回的 claim。
@@ -2127,6 +2126,6 @@ apps/web/src/
已确认决策:
1. 默认身份模式就是 `hybrid`,用于同时支持本地账号和 `server-main` token / API Key。
2. 普通注册支持填写邀请码,但邀请码不是必填项;填写后按邀请码加入指定租户用户组。
2. 普通注册支持填写邀请码,但邀请码不是必填项;填写后只记录邀请关系,不决定租户用户组或角色
3. 先做本地闭环:独立模式下本地 API Key、余额、充值订单和钱包流水由 Gateway 承接。
4. 当前阶段仅允许全局管理员配置 provider / platform 凭证和全局模型,不开放租户管理员自助配置凭证。
+1 -1
View File
@@ -15,7 +15,7 @@
### 1.1 模型库
- 建立 `model_catalog_providers``base_model_catalog``model_pricing_rules` 的首批数据。
- 建立 `gateway_tenants``gateway_users``gateway_user_groups``gateway_tenant_invitations` 和用户组策略,支持独立租户/用户、可选邀请码注册、`server-main` 同步租户/用户、不同用户组的充值折扣、调用折扣和并发/限流策略。
- 建立 `gateway_tenants``gateway_users``gateway_user_groups``gateway_invitations` 和用户组策略,支持独立租户/用户、可选邀请码注册关系记录`server-main` 同步租户/用户、不同用户组的充值折扣、调用折扣和并发/限流策略。
- 建立独立模式本地闭环表:`gateway_api_keys``gateway_wallet_accounts``gateway_wallet_transactions``gateway_recharge_orders`
- 导入 OpenAI、Gemini 的基准 provider、基准模型、能力 schema、默认限流模板。
- 支持全局模型配置:模型类型、上下文、多模态能力、图片输入/输出能力、stream 支持、价格规则。