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)
125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package proxy
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"opencatd-open/internal/channel"
|
|
"opencatd-open/internal/dao"
|
|
"opencatd-open/internal/store"
|
|
"opencatd-open/pkg/config"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Proxy struct {
|
|
ctx context.Context
|
|
cfg *config.Config
|
|
db *gorm.DB
|
|
wg *sync.WaitGroup
|
|
httpClient *http.Client
|
|
|
|
userDAO *dao.UserDAO
|
|
apiKeyDAO *dao.ApiKeyDAO
|
|
usageDAO *dao.UsageDAO
|
|
dailyDAO *dao.DailyUsageDAO
|
|
channelSvc *channel.Service
|
|
}
|
|
|
|
func NewProxy(ctx context.Context, cfg *config.Config, db *gorm.DB, wg *sync.WaitGroup, userDAO *dao.UserDAO, apiKeyDAO *dao.ApiKeyDAO, usageDAO *dao.UsageDAO, dailyDAO *dao.DailyUsageDAO) *Proxy {
|
|
client := http.DefaultClient
|
|
if os.Getenv("LOCAL_PROXY") != "" {
|
|
proxyUrl, err := url.Parse(os.Getenv("LOCAL_PROXY"))
|
|
if err == nil {
|
|
tr := &http.Transport{
|
|
Proxy: http.ProxyURL(proxyUrl),
|
|
}
|
|
client.Transport = tr
|
|
}
|
|
}
|
|
|
|
np := &Proxy{
|
|
ctx: ctx,
|
|
cfg: cfg,
|
|
db: db,
|
|
wg: wg,
|
|
httpClient: client,
|
|
userDAO: userDAO,
|
|
apiKeyDAO: apiKeyDAO,
|
|
usageDAO: usageDAO,
|
|
dailyDAO: dailyDAO,
|
|
}
|
|
|
|
return np
|
|
}
|
|
|
|
// SetChannelService sets the channel service (called after construction)
|
|
func (p *Proxy) SetChannelService(svc *channel.Service) {
|
|
p.channelSvc = svc
|
|
}
|
|
|
|
func (p *Proxy) HandleProxy(c *gin.Context) {
|
|
path := c.Request.URL.Path
|
|
switch {
|
|
case path == "/v1/chat/completions":
|
|
// TODO: Phase 3 - implement chat completions handler
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "chat completions not yet implemented"})
|
|
case strings.HasPrefix(path, "/v1/messages"):
|
|
// TODO: Phase 3 - implement messages handler
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "messages not yet implemented"})
|
|
case path == "/v1/responses":
|
|
// TODO: Phase 3 - implement responses handler
|
|
c.JSON(http.StatusNotImplemented, gin.H{"error": "responses not yet implemented"})
|
|
default:
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "unknown endpoint"})
|
|
}
|
|
}
|
|
|
|
func (p *Proxy) HandleModels(c *gin.Context) {
|
|
// TODO: Phase 3 - implement models list
|
|
c.JSON(http.StatusOK, gin.H{"object": "list", "data": []interface{}{}})
|
|
}
|
|
|
|
func (p *Proxy) GetDB() *gorm.DB {
|
|
return p.db
|
|
}
|
|
|
|
// SelectChannel selects the best channel for a model
|
|
func (p *Proxy) SelectChannel(modelName string) (*store.Channel, error) {
|
|
if p.channelSvc == nil {
|
|
return nil, fmt.Errorf("channel service not initialized")
|
|
}
|
|
return p.channelSvc.SelectChannel(p.ctx, modelName)
|
|
}
|
|
|
|
// RecordSuccess records a successful request
|
|
func (p *Proxy) RecordSuccess(channelID uint64) {
|
|
if p.channelSvc != nil {
|
|
p.channelSvc.RecordSuccess(channelID)
|
|
}
|
|
}
|
|
|
|
// RecordFailure records a failed request
|
|
func (p *Proxy) RecordFailure(channelID uint64) {
|
|
if p.channelSvc != nil {
|
|
p.channelSvc.RecordFailure(channelID)
|
|
}
|
|
}
|
|
|
|
// SendUsagePlaceholder placeholder for usage processing
|
|
func (p *Proxy) SendUsagePlaceholder(model string, userID uint64, promptTokens, completionTokens int) {
|
|
log.Printf("Usage: model=%s user=%d prompt=%d completion=%d", model, userID, promptTokens, completionTokens)
|
|
}
|
|
|
|
// Placeholder to keep the file compilable
|
|
var _ = json.Marshal
|
|
var _ = io.ReadAll
|