feat(admin): 模型管理 - 支持从上游渠道拉取模型目录 (#36)

以 main 为基线仅补齐拉取模型能力:管理员可一键导入缺失模型(默认停用待定价),并提供登录态后端代理目录接口避免 CORS。

Co-authored-by: ddcat666 <2695092045@qq.com>
This commit is contained in:
yepandddcat666 authored and GitHub committed 2026-07-23 22:04:54 +08:00
1 parent 61d12ec3ff
commit 243c43f6c2
11 files changed
+293 -7

No files matched your search

@@ -1,6 +1,7 @@
package service
import (
"context"
"encoding/json"
"strings"
@@ -17,6 +18,12 @@ type ChannelModelRequest struct {
Enabled *bool `json:"enabled"`
}
// AdminChannelModelFetchResult 是管理员从上游拉目录后的汇总:models 为去重后的标识,added 为本次新建条数。
type AdminChannelModelFetchResult struct {
Models []string `json:"models"`
Added int64 `json:"added"`
}
func (s *Service) EnsureSystemChannelModels() error {
channels, err := s.repo.SystemChannels(true)
if err != nil {
@@ -46,6 +53,42 @@ func (s *Service) AdminChannelModels(actor *model.User, channelID string) ([]mod
return s.ensureChannelModels(channelID, true)
}
func (s *Service) FetchAdminChannelModels(ctx context.Context, actor *model.User, channelID string) (*AdminChannelModelFetchResult, error) {
if err := s.RequireAdmin(actor); err != nil {
return nil, err
}
channel, err := s.repo.AdminSystemChannel(channelID)
if err != nil {
return nil, err
}
// 使用服务端保存的渠道密钥请求上游,避免密钥为了拉目录再次经过浏览器。
models, err := s.FetchChannelModels(ctx, actor, ChannelModelsRequest{BaseURL: channel.BaseURL, APIKey: channel.APIKey, APIFormat: channel.APIFormat})
if err != nil {
return nil, err
}
existing, err := s.repo.ChannelModels(channelID, true)
if err != nil {
return nil, err
}
known := make(map[string]struct{}, len(existing))
for _, item := range existing {
known[item.ModelKey] = struct{}{}
}
missing := make([]model.ChannelModel, 0, len(models))
for _, name := range models {
if _, ok := known[name]; ok {
continue
}
// 自动发现不能绕过定价边界;新模型由管理员定价后再手动启用。
missing = append(missing, model.ChannelModel{ID: newID(), ChannelID: channelID, ModelKey: name, DisplayName: name, Capability: capabilityForChannel(*channel), BillingMode: "fixed_request", Enabled: false, PriceVersion: 1})
}
added, err := s.repo.CreateMissingChannelModels(missing)
if err != nil {
return nil, err
}
return &AdminChannelModelFetchResult{Models: models, Added: added}, nil
}
func (s *Service) SaveAdminChannelModel(actor *model.User, channelID string, id string, req ChannelModelRequest) (*model.ChannelModel, error) {
if err := s.RequireAdmin(actor); err != nil {
return nil, err