Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
757 changes: 751 additions & 6 deletions backend/docs/docs.go

Large diffs are not rendered by default.

757 changes: 751 additions & 6 deletions backend/docs/swagger.json

Large diffs are not rendered by default.

493 changes: 493 additions & 0 deletions backend/docs/swagger.yaml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func NewApp() (*App, error) {
llmClient := llm.NewClient(trustedOutboundPolicy)
mcpClient := mcp.NewClient(trustedOutboundPolicy)
mediaArtifactClient := mediaartifact.New(strictOutboundPolicy)
channelService := channel.NewServiceWithRuntime(runtimeCfg, channelRepo, channelCache, llmClient)
channelService := channel.NewServiceWithRuntime(runtimeCfg, channelRepo, channelRepo, channelCache, llmClient)
channelService.SetLogger(log)
channelService.SetBillingModelPricingFilter(billingService)
channelService.SetPermissionGroupRepo(channelRepo)
Expand Down
27 changes: 27 additions & 0 deletions backend/internal/application/channel/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ type ModelView struct {
ID uint
PlatformModelName string
Vendor string
VendorName string
VendorIcon string
DisplayGroupID *uint
DisplayGroupName string
DisplayGroupIcon string
KindsJSON string
Icon string
CapabilitiesJSON string
Expand All @@ -156,6 +161,28 @@ type ModelView struct {
UpdatedAt string
}

// ModelVendorView 表示技术厂商目录展示数据。
type ModelVendorView struct {
ID uint
Key string
Name string
Icon string
BuiltIn bool
SortOrder int
CreatedAt string
UpdatedAt string
}

// ModelDisplayGroupView 表示自定义模型展示分组数据。
type ModelDisplayGroupView struct {
ID uint
Name string
Icon string
SortOrder int
CreatedAt string
UpdatedAt string
}

// UpstreamModelView 上游模型路由绑定展示数据(内部传输,不携带序列化标记)。
type UpstreamModelView struct {
ID uint
Expand Down
12 changes: 12 additions & 0 deletions backend/internal/application/channel/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ var (
ErrSystemPromptTooLong = errors.New("system prompt too long")
// ErrInvalidModelOrder 模型排序参数无效。
ErrInvalidModelOrder = errors.New("invalid model order")
// ErrModelVendorNotFound 技术厂商不存在。
ErrModelVendorNotFound = errors.New("model vendor not found")
// ErrModelVendorConflict 技术厂商 key 重复。
ErrModelVendorConflict = errors.New("model vendor conflict")
// ErrInvalidModelVendor 技术厂商参数无效。
ErrInvalidModelVendor = errors.New("invalid model vendor")
// ErrModelDisplayGroupNotFound 展示分组不存在。
ErrModelDisplayGroupNotFound = errors.New("model display group not found")
// ErrModelDisplayGroupConflict 展示分组名称重复。
ErrModelDisplayGroupConflict = errors.New("model display group conflict")
// ErrInvalidModelDisplayGroup 展示分组参数无效。
ErrInvalidModelDisplayGroup = errors.New("invalid model display group")
// ErrInvalidPermissionGroupModels 模型权限组参数无效。
ErrInvalidPermissionGroupModels = errors.New("invalid permission group models")
// ErrPermissionGroupRepoUnavailable 权限组仓储未注入。
Expand Down
29 changes: 29 additions & 0 deletions backend/internal/application/channel/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type UpdateUpstreamInput struct {
type CreateModelInput struct {
PlatformModelName string
Vendor string
DisplayGroupID uint
KindsJSON string
Icon string
CapabilitiesJSON string
Expand All @@ -61,6 +62,7 @@ type CreateModelInput struct {
type UpdateModelInput struct {
PlatformModelName *string
Vendor *string
DisplayGroupID *uint
KindsJSON *string
Icon *string
CapabilitiesJSON *string
Expand All @@ -74,6 +76,33 @@ type UpdateModelInput struct {
CbWindowMin *int
}

// CreateModelVendorInput 定义创建技术厂商入参。
type CreateModelVendorInput struct {
Key string
Name string
Icon string
}

// UpdateModelVendorInput 定义更新技术厂商展示信息入参。
type UpdateModelVendorInput struct {
Name *string
Icon *string
}

// CreateModelDisplayGroupInput 定义创建模型展示分组入参。
type CreateModelDisplayGroupInput struct {
Name string
Icon string
ModelIDs []uint
}

// UpdateModelDisplayGroupInput 定义更新模型展示分组入参。
type UpdateModelDisplayGroupInput struct {
Name *string
Icon *string
ModelIDs *[]uint
}

// UpsertUpstreamModelInput 定义上游真实模型与平台路由保存入参。
type UpsertUpstreamModelInput struct {
RouteID uint
Expand Down
16 changes: 9 additions & 7 deletions backend/internal/application/channel/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (s *Service) isModelAccessible(ctx context.Context, platformModelID uint, u
type Service struct {
cfg *config.Runtime
repo repository.ChannelRepository
presentationRepo repository.ModelPresentationRepository
cache repository.ChannelCacheRepository
llmClient *llm.Client
modelPricingFilter billingModelPricingFilter
Expand Down Expand Up @@ -192,17 +193,18 @@ const (
var localAPIKeyCounters sync.Map

// NewService 创建服务。
func NewService(cfg config.Config, repo repository.ChannelRepository, cache repository.ChannelCacheRepository, llmClient *llm.Client) *Service {
return NewServiceWithRuntime(config.NewRuntime(cfg), repo, cache, llmClient)
func NewService(cfg config.Config, repo repository.ChannelRepository, presentationRepo repository.ModelPresentationRepository, cache repository.ChannelCacheRepository, llmClient *llm.Client) *Service {
return NewServiceWithRuntime(config.NewRuntime(cfg), repo, presentationRepo, cache, llmClient)
}

// NewServiceWithRuntime 创建使用运行时配置容器的服务。
func NewServiceWithRuntime(cfg *config.Runtime, repo repository.ChannelRepository, cache repository.ChannelCacheRepository, llmClient *llm.Client) *Service {
func NewServiceWithRuntime(cfg *config.Runtime, repo repository.ChannelRepository, presentationRepo repository.ModelPresentationRepository, cache repository.ChannelCacheRepository, llmClient *llm.Client) *Service {
return &Service{
cfg: cfg,
repo: repo,
cache: cache,
llmClient: llmClient,
cfg: cfg,
repo: repo,
presentationRepo: presentationRepo,
cache: cache,
llmClient: llmClient,
}
}

Expand Down
46 changes: 39 additions & 7 deletions backend/internal/application/channel/service_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ func cloneModelViews(items []ModelView) []ModelView {
}
results := make([]ModelView, 0, len(items))
for _, item := range items {
if item.DisplayGroupID != nil {
displayGroupID := *item.DisplayGroupID
item.DisplayGroupID = &displayGroupID
}
if item.Pricing != nil {
pricing := *item.Pricing
if len(pricing.Tiers) > 0 {
Expand Down Expand Up @@ -347,12 +351,25 @@ func (s *Service) CreateModel(ctx context.Context, input CreateModelInput) (*Mod
return nil, err
}
cbPolicyMode := normalizeModelCircuitPolicyMode(input.CbPolicyMode)
vendor, err := s.resolvePlatformModelVendor(ctx, input.Vendor, platformModelName)
if err != nil {
return nil, err
}
if err := s.validateModelDisplayGroup(ctx, input.DisplayGroupID); err != nil {
return nil, err
}
var displayGroupID *uint
if input.DisplayGroupID > 0 {
value := input.DisplayGroupID
displayGroupID = &value
}

item := &domainchannel.PlatformModel{
PlatformModelName: platformModelName,
Vendor: normalizeModelVendor(input.Vendor, platformModelName),
Vendor: vendor,
DisplayGroupID: displayGroupID,
KindsJSON: kindsJSON,
Icon: normalizeModelIcon(input.Icon, input.Vendor, platformModelName),
Icon: normalizeModelIcon(input.Icon, vendor, platformModelName),
CapabilitiesJSON: strings.TrimSpace(input.CapabilitiesJSON),
SystemPrompt: systemPrompt,
AccessScope: accessScope,
Expand All @@ -370,8 +387,7 @@ func (s *Service) CreateModel(ctx context.Context, input CreateModelInput) (*Mod
return nil, err
}
s.InvalidateModelCatalog()
view := toModelView(repository.ChannelModelListRow{PlatformModel: *item})
return &view, nil
return s.getModelViewByID(ctx, item.ID)
}

// UpdateModel 更新平台模型目录项。
Expand All @@ -381,7 +397,10 @@ func (s *Service) UpdateModel(ctx context.Context, modelID uint, input UpdateMod
return nil, err
}

nextVendor := normalizeModelVendor(current.Vendor, current.PlatformModelName)
nextVendor, err := normalizeModelVendorKey(current.Vendor)
if err != nil {
return nil, err
}
nextPlatformModelName := current.PlatformModelName

update := repository.UpdateChannelModelInput{}
Expand All @@ -393,9 +412,19 @@ func (s *Service) UpdateModel(ctx context.Context, modelID uint, input UpdateMod
update.PlatformModelName = &nextPlatformModelName
}
if input.Vendor != nil {
nextVendor = normalizeModelVendor(*input.Vendor, nextPlatformModelName)
nextVendor, err = s.resolvePlatformModelVendor(ctx, *input.Vendor, nextPlatformModelName)
if err != nil {
return nil, err
}
update.Vendor = &nextVendor
}
if input.DisplayGroupID != nil {
if err := s.validateModelDisplayGroup(ctx, *input.DisplayGroupID); err != nil {
return nil, err
}
value := *input.DisplayGroupID
update.DisplayGroupID = &value
}
if input.KindsJSON != nil {
kindsJSON, err := normalizeKindsJSON(*input.KindsJSON)
if err != nil {
Expand Down Expand Up @@ -453,7 +482,10 @@ func (s *Service) UpdateModel(ctx context.Context, modelID uint, input UpdateMod
update.CbWindowMin = &value
}
if input.Vendor == nil && input.PlatformModelName != nil {
autoVendor := normalizeModelVendor("", nextPlatformModelName)
autoVendor, err := s.resolvePlatformModelVendor(ctx, "", nextPlatformModelName)
if err != nil {
return nil, err
}
if autoVendor != nextVendor {
update.Vendor = &autoVendor
nextVendor = autoVendor
Expand Down
Loading
Loading