(ctx context.Context, keyword string)
| 245 | } |
| 246 | |
| 247 | func (i *imlProviderModule) ConfiguredProviders(ctx context.Context, keyword string) ([]*ai_dto.ConfiguredProviderItem, error) { |
| 248 | // 获取已配置的AI服务商 |
| 249 | list, err := i.providerService.Search(ctx, keyword, nil, "update_at desc") |
| 250 | if err != nil { |
| 251 | return nil, fmt.Errorf("get provider list error:%v", err) |
| 252 | } |
| 253 | aiAPIMap, err := i.aiAPIService.CountMapByProvider(ctx, "", nil) |
| 254 | if err != nil { |
| 255 | return nil, fmt.Errorf("get ai api count error:%v", err) |
| 256 | } |
| 257 | keyMap, err := i.aiKeyService.CountMapByProvider(ctx, "", nil) |
| 258 | if err != nil { |
| 259 | return nil, fmt.Errorf("get ai key count error:%v", err) |
| 260 | } |
| 261 | providers := make([]*ai_dto.ConfiguredProviderItem, 0, len(list)) |
| 262 | for _, l := range list { |
| 263 | // 检查是否有默认Key |
| 264 | _, err = i.aiKeyService.DefaultKey(ctx, l.Id) |
| 265 | if err != nil { |
| 266 | if !errors.Is(err, gorm.ErrRecordNotFound) { |
| 267 | return nil, err |
| 268 | } |
| 269 | err = i.aiKeyService.Create(ctx, &ai_key.Create{ |
| 270 | ID: l.Id, |
| 271 | Name: l.Name, |
| 272 | Config: l.Config, |
| 273 | Provider: l.Id, |
| 274 | Priority: 1, |
| 275 | Status: 1, |
| 276 | ExpireTime: 0, |
| 277 | UseToken: 0, |
| 278 | Default: true, |
| 279 | }) |
| 280 | if err != nil { |
| 281 | return nil, fmt.Errorf("create default key error:%v", err) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | p, has := model_runtime.GetProvider(l.Id) |
| 286 | if !has { |
| 287 | continue |
| 288 | } |
| 289 | apiCount := aiAPIMap[l.Id] |
| 290 | defaultLLMName := "" |
| 291 | if defaultModel, has := p.GetModel(l.DefaultLLM); has { |
| 292 | defaultLLMName = defaultModel.Name() |
| 293 | } |
| 294 | providers = append(providers, &ai_dto.ConfiguredProviderItem{ |
| 295 | Id: l.Id, |
| 296 | Name: l.Name, |
| 297 | Logo: p.Logo(), |
| 298 | DefaultLLM: l.DefaultLLM, |
| 299 | DefaultLLMName: defaultLLMName, |
| 300 | Status: ai_dto.ToProviderStatus(l.Status), |
| 301 | APICount: apiCount, |
| 302 | KeyCount: keyMap[l.Id], |
| 303 | CanDelete: apiCount < 1, |
| 304 | ModelCount: int64(len(p.Models())), |
nothing calls this directly
no test coverage detected