Major rewrite of the Go backend to support:
- Three API format imports: openai, anthropic, compatible
- Three protocol conversions: Chat Completions, Responses, Messages
- Hub-and-spoke architecture with Chat as intermediate format
Deleted:
- opencat.go (old entry)
- store/, team/, pkg/team/, pkg/store/ (old data layer)
- internal/model/, internal/consts/ (old types)
- internal/service/team/, internal/controller/team/ (old handlers)
- llm/ (removed LLM client library, pure proxy mode)
- dist/, assets/ (old build artifacts)
Added:
- internal/store/ — 9 GORM models + multi-DB support
- internal/pkg/ — crypto (AES-GCM), apikey, jwt, ratelimit, resp, tokenizer
- internal/channel/ — channel selection, weighted LB, health checks
- internal/proxy/convert/ — 6 protocol conversion functions + SSE streaming
- internal/proxy/ — gateway with request dispatch and upstream selection
- internal/usage/ — async usage recorder with batch writes
- internal/api/ — management API (auth, users, keys, channels, models)
- Makefile for build/test/deploy
Fixed API to match frontend expectations:
- Login response wraps token in { data: { token } }
- GET /api/profile route added
- Profile response wraps user in { code, data }
- Role returned as number (10=admin, 1=user)
85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"opencatd-open/internal/channel"
|
|
"opencatd-open/internal/dao"
|
|
"opencatd-open/internal/store"
|
|
"opencatd-open/internal/pkg/crypto"
|
|
)
|
|
|
|
type ChannelServiceImpl struct {
|
|
channelDAO *dao.ChannelDAO
|
|
channelSvc *channel.Service
|
|
}
|
|
|
|
func NewChannelService(channelDAO *dao.ChannelDAO, channelSvc *channel.Service) *ChannelServiceImpl {
|
|
return &ChannelServiceImpl{
|
|
channelDAO: channelDAO,
|
|
channelSvc: channelSvc,
|
|
}
|
|
}
|
|
|
|
func (s *ChannelServiceImpl) Create(ctx context.Context, ch *store.Channel) error {
|
|
return s.channelDAO.Create(ch)
|
|
}
|
|
|
|
func (s *ChannelServiceImpl) GetByID(ctx context.Context, id uint64) (*store.Channel, error) {
|
|
return s.channelDAO.GetByID(id)
|
|
}
|
|
|
|
func (s *ChannelServiceImpl) List(ctx context.Context, limit, offset int) ([]*store.Channel, int64, error) {
|
|
return s.channelDAO.List(limit, offset)
|
|
}
|
|
|
|
func (s *ChannelServiceImpl) ListEnabled(ctx context.Context) ([]*store.Channel, error) {
|
|
return s.channelDAO.ListEnabled()
|
|
}
|
|
|
|
func (s *ChannelServiceImpl) Update(ctx context.Context, ch *store.Channel) error {
|
|
return s.channelDAO.Update(ch)
|
|
}
|
|
|
|
func (s *ChannelServiceImpl) Delete(ctx context.Context, id uint64) error {
|
|
return s.channelDAO.Delete(id)
|
|
}
|
|
|
|
// GetAPIKey decrypts the channel's API key
|
|
func (s *ChannelServiceImpl) GetAPIKey(ctx context.Context, channelID uint64) (string, error) {
|
|
ch, err := s.channelDAO.GetByID(channelID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return crypto.Decrypt(ch.APIKeyEnc)
|
|
}
|
|
|
|
// SelectForModel selects the best channel for a model
|
|
func (s *ChannelServiceImpl) SelectForModel(ctx context.Context, modelName string) (*store.Channel, error) {
|
|
return s.channelSvc.SelectChannel(ctx, modelName)
|
|
}
|
|
|
|
// BindModels binds models to a channel
|
|
func (s *ChannelServiceImpl) BindModels(ctx context.Context, channelID uint64, bindings []store.ChannelModelBinding) error {
|
|
return s.channelDAO.BindModels(channelID, bindings)
|
|
}
|
|
|
|
// GetChannelModels returns models bound to a channel
|
|
func (s *ChannelServiceImpl) GetChannelModels(ctx context.Context, channelID uint64) ([]store.ChannelModelBinding, error) {
|
|
return s.channelDAO.GetChannelModels(channelID)
|
|
}
|
|
|
|
// GetModelChannels returns channels for a model
|
|
func (s *ChannelServiceImpl) GetModelChannels(ctx context.Context, modelName string) ([]*store.Channel, error) {
|
|
return s.channelDAO.GetEnabledChannelsByModel(modelName)
|
|
}
|
|
|
|
// RecordSuccess records a successful request
|
|
func (s *ChannelServiceImpl) RecordSuccess(channelID uint64) {
|
|
s.channelSvc.RecordSuccess(channelID)
|
|
}
|
|
|
|
// RecordFailure records a failed request
|
|
func (s *ChannelServiceImpl) RecordFailure(channelID uint64) {
|
|
s.channelSvc.RecordFailure(channelID)
|
|
}
|