66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
|
|
package services
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"strings"
|
||
|
|
"sync"
|
||
|
|
|
||
|
|
"code.tczkiot.com/wlw/ai-agent/contract"
|
||
|
|
)
|
||
|
|
|
||
|
|
type platformAIService struct {
|
||
|
|
mu sync.RWMutex
|
||
|
|
provider contract.PlatformAIProvider
|
||
|
|
}
|
||
|
|
|
||
|
|
var PlatformAIService = &platformAIService{}
|
||
|
|
|
||
|
|
func SetPlatformAIProvider(provider contract.PlatformAIProvider) {
|
||
|
|
PlatformAIService.mu.Lock()
|
||
|
|
defer PlatformAIService.mu.Unlock()
|
||
|
|
PlatformAIService.provider = provider
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *platformAIService) ModelSource(ctx context.Context) (string, error) {
|
||
|
|
provider := s.current()
|
||
|
|
if provider == nil {
|
||
|
|
return contract.ModelSourceCustom, nil
|
||
|
|
}
|
||
|
|
source, err := provider.ModelSource(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
if strings.EqualFold(strings.TrimSpace(source), contract.ModelSourcePlatform) {
|
||
|
|
return contract.ModelSourcePlatform, nil
|
||
|
|
}
|
||
|
|
return contract.ModelSourceCustom, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *platformAIService) IsPlatform(ctx context.Context) (bool, error) {
|
||
|
|
source, err := s.ModelSource(ctx)
|
||
|
|
return source == contract.ModelSourcePlatform, err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *platformAIService) Config(ctx context.Context) (*contract.PlatformAIConfig, error) {
|
||
|
|
provider := s.current()
|
||
|
|
if provider == nil {
|
||
|
|
return nil, errors.New("platform AI provider is not initialized")
|
||
|
|
}
|
||
|
|
return provider.Config(ctx)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *platformAIService) Status(ctx context.Context) (*contract.PlatformAIStatus, error) {
|
||
|
|
provider := s.current()
|
||
|
|
if provider == nil {
|
||
|
|
return nil, errors.New("platform AI provider is not initialized")
|
||
|
|
}
|
||
|
|
return provider.Status(ctx)
|
||
|
|
}
|
||
|
|
|
||
|
|
func (s *platformAIService) current() contract.PlatformAIProvider {
|
||
|
|
s.mu.RLock()
|
||
|
|
defer s.mu.RUnlock()
|
||
|
|
return s.provider
|
||
|
|
}
|