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)
106 lines
3.2 KiB
Go
106 lines
3.2 KiB
Go
package dao
|
|
|
|
import (
|
|
"opencatd-open/internal/store"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ChannelDAO struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewChannelDAO(db *gorm.DB) *ChannelDAO {
|
|
return &ChannelDAO{db: db}
|
|
}
|
|
|
|
func (d *ChannelDAO) Create(channel *store.Channel) error {
|
|
return d.db.Create(channel).Error
|
|
}
|
|
|
|
func (d *ChannelDAO) GetByID(id uint64) (*store.Channel, error) {
|
|
var channel store.Channel
|
|
err := d.db.First(&channel, id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &channel, nil
|
|
}
|
|
|
|
func (d *ChannelDAO) GetByName(name string) (*store.Channel, error) {
|
|
var channel store.Channel
|
|
err := d.db.Where("name = ?", name).First(&channel).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &channel, nil
|
|
}
|
|
|
|
func (d *ChannelDAO) List(limit, offset int) ([]*store.Channel, int64, error) {
|
|
var channels []*store.Channel
|
|
var total int64
|
|
d.db.Model(&store.Channel{}).Count(&total)
|
|
err := d.db.Limit(limit).Offset(offset).Order("priority DESC, weight DESC").Find(&channels).Error
|
|
return channels, total, err
|
|
}
|
|
|
|
func (d *ChannelDAO) ListEnabled() ([]*store.Channel, error) {
|
|
var channels []*store.Channel
|
|
err := d.db.Where("enabled = ?", true).Order("priority DESC, weight DESC").Find(&channels).Error
|
|
return channels, err
|
|
}
|
|
|
|
func (d *ChannelDAO) Update(channel *store.Channel) error {
|
|
return d.db.Save(channel).Error
|
|
}
|
|
|
|
func (d *ChannelDAO) Delete(id uint64) error {
|
|
return d.db.Delete(&store.Channel{}, id).Error
|
|
}
|
|
|
|
// BindModels binds models to a channel (replaces existing bindings)
|
|
func (d *ChannelDAO) BindModels(channelID uint64, bindings []store.ChannelModelBinding) error {
|
|
return d.db.Transaction(func(tx *gorm.DB) error {
|
|
// Delete existing bindings
|
|
if err := tx.Where("channel_id = ?", channelID).Delete(&store.ChannelModelBinding{}).Error; err != nil {
|
|
return err
|
|
}
|
|
// Create new bindings
|
|
for i := range bindings {
|
|
bindings[i].ChannelID = channelID
|
|
}
|
|
return tx.Create(&bindings).Error
|
|
})
|
|
}
|
|
|
|
// GetChannelModels returns all models bound to a channel
|
|
func (d *ChannelDAO) GetChannelModels(channelID uint64) ([]store.ChannelModelBinding, error) {
|
|
var bindings []store.ChannelModelBinding
|
|
err := d.db.Where("channel_id = ?", channelID).Find(&bindings).Error
|
|
return bindings, err
|
|
}
|
|
|
|
// GetModelChannels returns all channels that support a given model (by model name)
|
|
func (d *ChannelDAO) GetModelChannels(modelName string) ([]store.ChannelModelBinding, error) {
|
|
var bindings []store.ChannelModelBinding
|
|
err := d.db.
|
|
Joins("JOIN channels ON channels.id = channel_model_bindings.channel_id").
|
|
Joins("JOIN models ON models.id = channel_model_bindings.model_id").
|
|
Where("models.name = ? AND channels.enabled = ?", modelName, true).
|
|
Find(&bindings).Error
|
|
return bindings, err
|
|
}
|
|
|
|
// GetEnabledChannelsByModel returns enabled channels for a model, ordered by priority/weight
|
|
func (d *ChannelDAO) GetEnabledChannelsByModel(modelName string) ([]*store.Channel, error) {
|
|
var channels []*store.Channel
|
|
err := d.db.
|
|
Distinct("channels.*").
|
|
Joins("JOIN channel_model_bindings ON channel_model_bindings.channel_id = channels.id").
|
|
Joins("JOIN models ON models.id = channel_model_bindings.model_id").
|
|
Where("models.name = ? AND channels.enabled = ?", modelName, true).
|
|
Order("channels.priority DESC, channels.weight DESC").
|
|
Find(&channels).Error
|
|
return channels, err
|
|
}
|