0.11.8: 优化方向落地(流式聊天/操作审计/列表分页/测试补齐)
P1-聊天 SSE 流式响应:
- 新增 POST /chat/sessions/{id}/messages/stream:网关 text/event-stream 实时
透传,流结束整轮落库(哈希链),上游忽略 stream 返回普通 JSON 时自动转
SSE 事件,非 2xx 错误缓冲后走统一错误处理(不落 header);
- 前端 fetch+ReadableStream 解析 SSE,占位气泡实时填充,支持停止生成
(AbortController),切会话丢弃迟到增量防串扰。
P2-管理操作审计(admin_op_logs):
- 新表+oplog 包(同步写,失败不阻塞业务);管理端查询端点
GET /api/v1/admin/op-logs(操作者/类型过滤+分页,audit:read);
- 埋点:渠道 save/delete/grant(幂等重复不重复记)/revoke_grant、账号
create/update、角色 CRUD、API Key create/revoke/limits、工具
save/delete、审批决定(资源/工具)、模型配额;管理端「操作审计」菜单。
P2-列表分页与安全上限:
- 用户/管理员列表 q+limit+offset 分页(默认 50 上限 200),渠道授权弹窗
改远程搜索,不再全量拉取 portal-users;api_keys/channels List 加
LIMIT 200 防全表扫描。
健壮性:
- ChatModels/approvedModel 对 decided_at 为 NULL 的历史批准记录
COALESCE 兜底,修复 NULL scan 报错;
- docker-compose 补 ALLOW_PRIVATE_PROVIDER_URLS 透传(默认 false)。
测试:
- portal: 流式解析/错误提取/stream writer 模式单测,会话生命周期/哈希链
完整性/200 条上限/busy 租约回收集成测试;
- channel: CRUD+加解密+部门可见性+授权撤销+幂等+审计落库集成测试。
全部通过;全量 go vet 干净。
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
platformid "aigateway.local/core/internal/platform/id"
|
||||
"aigateway.local/core/internal/platform/oplog"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
@@ -23,7 +24,7 @@ func (r *Repository) List(ctx context.Context) ([]Record, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id::text, tenant_id::text, name, key_prefix, scopes, enabled,
|
||||
requests_per_minute, monthly_request_quota, monthly_token_quota, expires_at, last_used_at, created_at
|
||||
FROM gateway.api_keys ORDER BY created_at DESC`)
|
||||
FROM gateway.api_keys ORDER BY created_at DESC LIMIT 200`)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrStore, err)
|
||||
}
|
||||
@@ -77,6 +78,10 @@ func (r *Repository) Create(ctx context.Context, name string, scopes []string, r
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return Record{}, "", fmt.Errorf("%w: %v", ErrStore, err)
|
||||
}
|
||||
oplog.Record(ctx, r.pool, nil, actorID, "", "api_key.create", "api_key", id, map[string]any{
|
||||
"name": name, "key_prefix": prefix, "requests_per_minute": requestsPerMinute,
|
||||
"monthly_request_quota": monthlyRequestQuota, "monthly_token_quota": monthlyTokenQuota,
|
||||
})
|
||||
return record, secret, nil
|
||||
}
|
||||
|
||||
@@ -131,6 +136,7 @@ func (r *Repository) Revoke(ctx context.Context, id, actorID string) ([]byte, er
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrStore, err)
|
||||
}
|
||||
oplog.Record(ctx, r.pool, nil, actorID, "", "api_key.revoke", "api_key", id, nil)
|
||||
return hash, nil
|
||||
}
|
||||
|
||||
@@ -175,5 +181,8 @@ func (r *Repository) UpdateLimits(ctx context.Context, id string, requestsPerMin
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return Record{}, nil, fmt.Errorf("%w: %v", ErrStore, err)
|
||||
}
|
||||
oplog.Record(ctx, r.pool, nil, actorID, "", "api_key.limits_update", "api_key", id, map[string]any{
|
||||
"requests_per_minute": requestsPerMinute, "monthly_request_quota": monthlyRequestQuota, "monthly_token_quota": monthlyTokenQuota,
|
||||
})
|
||||
return record, record.KeyHash, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user