0.11.3: 旗舰版第四轮完善(统一审批中心/工具治理/平台环境变量/数字员工入口/个人渠道/报表多维/租户配额)

- 统一审批中心:模型/资源/渠道/工具四类申请聚合审批,通过自动开通
  (marketplace 安装/渠道授权),outbox 双向站内信;门户可发起/撤回。
- 工具治理:rate_limit_rpm(固定窗口原子 upsert,多实例共享)+ approval_required
  (首次调用自动发起审批,批准前一律拒绝)。
- 平台环境变量:平台级注入 skill/MCP 运行时,个人可覆盖;系统管理员可写。
- 数字员工会话入口:门户列表/对话/调用记录,复用用户运行时凭据。
- 个人渠道:webhook 入站令牌 SHA-256 摘要 + constant-time 校验,绑定已批准
  模型,用量归属用户 Key。
- 报表多维:工具调用/审批授权/安全事件三组统计端点与页面。
- 租户配额:部门 Key/月 Token 上限,运行时凭据开通强制校验,概览展示用量。
- 迁移 000042-000045;修复渠道空 API Key NOT NULL 违约与 inet 扫描;
  25 包测试通过,前后端构建通过,端到端验证完成。
This commit is contained in:
LLMGuardX Dev
2026-08-13 13:41:22 +08:00
parent e31cc54b8e
commit 87c2b04174
40 changed files with 2416 additions and 111 deletions
+60
View File
@@ -0,0 +1,60 @@
-- 000042_governance.sql — 治理增强:工具限流与审批、资源/渠道权限申请(全类型审批流)。
-- 工具治理:调用频率上限(0 = 不限)与审批标记(approval_required 工具首次调用需管理员审批)。
ALTER TABLE gateway.tool_definitions
ADD COLUMN IF NOT EXISTS rate_limit_rpm integer NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS approval_required boolean NOT NULL DEFAULT false;
COMMENT ON COLUMN gateway.tool_definitions.rate_limit_rpm IS
'Per-tool rate limit in calls per minute, 0 = unlimited. Enforced with an atomic fixed-window upsert so multiple gateway replicas share the same budget.';
COMMENT ON COLUMN gateway.tool_definitions.approval_required IS
'When true the tool cannot run until an admin approves it; the first invocation creates a tool_approval_requests entry.';
-- 工具限流固定窗口计数(PostgreSQL 原子 upsert,多实例共享)。
CREATE TABLE IF NOT EXISTS gateway.tool_rate_usage (
tool_id uuid NOT NULL REFERENCES gateway.tool_definitions(id) ON DELETE CASCADE,
window_start timestamptz NOT NULL,
call_count bigint NOT NULL DEFAULT 0,
PRIMARY KEY (tool_id, window_start)
);
-- 工具审批申请:每工具至多一个待审项;审批通过后该工具可被调用。
CREATE TABLE IF NOT EXISTS gateway.tool_approval_requests (
id uuid PRIMARY KEY,
tool_id uuid NOT NULL REFERENCES gateway.tool_definitions(id) ON DELETE CASCADE,
requester_kind varchar(16) NOT NULL DEFAULT 'system',
requester_id text NOT NULL DEFAULT '',
reason text NOT NULL DEFAULT '' CHECK (length(reason) <= 4000),
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected')),
decided_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
decision_note text NOT NULL DEFAULT '' CHECK (length(decision_note) <= 4000),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
decided_at timestamptz
);
CREATE UNIQUE INDEX IF NOT EXISTS tool_approval_requests_one_pending_idx
ON gateway.tool_approval_requests (tool_id) WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS tool_approval_requests_status_time_idx
ON gateway.tool_approval_requests (status, created_at DESC);
-- 资源/渠道权限申请:门户用户申请 mcp/skill/数字员工/渠道的使用权限,管理员审批后
-- 自动开通(marketplace 安装或渠道部门授权)。
CREATE TABLE IF NOT EXISTS gateway.resource_access_requests (
id uuid PRIMARY KEY,
portal_user_id uuid NOT NULL REFERENCES gateway.portal_users(id) ON DELETE CASCADE,
resource_type varchar(24) NOT NULL CHECK (resource_type IN ('mcp_server', 'skill', 'digital_employee', 'channel')),
resource_code text NOT NULL CHECK (length(resource_code) BETWEEN 1 AND 128),
reason text NOT NULL DEFAULT '' CHECK (length(reason) <= 4000),
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled')),
decision_note text NOT NULL DEFAULT '' CHECK (length(decision_note) <= 4000),
decided_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
decided_at timestamptz,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE UNIQUE INDEX IF NOT EXISTS resource_access_requests_one_pending_idx
ON gateway.resource_access_requests (portal_user_id, resource_type, resource_code)
WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS resource_access_requests_status_time_idx
ON gateway.resource_access_requests (status, created_at DESC);
+11
View File
@@ -0,0 +1,11 @@
-- 000043_platform_env_vars.sql — 平台级环境变量(skill/mcp 运行时注入)。
-- 优先级:平台变量 < 个人变量(个人可覆盖平台默认值)。
CREATE TABLE IF NOT EXISTS gateway.platform_env_vars (
key varchar(128) PRIMARY KEY CHECK (key ~ '^[A-Za-z_][A-Za-z0-9_]*$'),
encrypted_value bytea NOT NULL,
value_kek_version integer NOT NULL,
description text NOT NULL DEFAULT '' CHECK (length(description) <= 512),
updated_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
+21
View File
@@ -0,0 +1,21 @@
-- 000044_personal_channels.sql — 个人渠道:门户用户自建 webhook 渠道,绑定已批准模型,
-- 入站消息用用户运行时凭据应答,用量归属用户自己的 Key。
CREATE TABLE IF NOT EXISTS gateway.personal_channels (
id uuid PRIMARY KEY,
portal_user_id uuid NOT NULL REFERENCES gateway.portal_users(id) ON DELETE CASCADE,
code varchar(64) NOT NULL,
name varchar(128) NOT NULL,
kind varchar(16) NOT NULL DEFAULT 'webhook' CHECK (kind IN ('webhook')),
inbound_token_hash varchar(64) NOT NULL,
provider_code text NOT NULL DEFAULT '',
model text NOT NULL CHECK (length(model) BETWEEN 1 AND 512),
enabled boolean NOT NULL DEFAULT true,
last_used_at timestamptz,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (code)
);
CREATE INDEX IF NOT EXISTS personal_channels_user_idx
ON gateway.personal_channels (portal_user_id, created_at DESC);
+11
View File
@@ -0,0 +1,11 @@
-- 000045_tenant_quotas.sql — 租户(部门)级配额:Key 数量上限与月 Token 上限,
-- 0 = 不限。平台管理员多租户管理的基础:每个部门即一个租户,配额在创建时强制。
ALTER TABLE gateway.departments
ADD COLUMN IF NOT EXISTS max_api_keys integer NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS max_monthly_tokens bigint NOT NULL DEFAULT 0;
COMMENT ON COLUMN gateway.departments.max_api_keys IS
'Maximum gateway API keys bound to this tenant (department), 0 = unlimited.';
COMMENT ON COLUMN gateway.departments.max_monthly_tokens IS
'Maximum monthly token usage for this tenant, 0 = unlimited.';