feat: align controlled AI sessions and MVP controls
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
"senlinai-agent/backend/internal/models"
|
||||
)
|
||||
@@ -25,6 +26,11 @@ type SelectedKey struct {
|
||||
KeyType string
|
||||
}
|
||||
|
||||
var (
|
||||
ErrAIKeyMissing = errors.New("no ai key available")
|
||||
ErrAIRateLimited = errors.New("ai rate limit exceeded")
|
||||
)
|
||||
|
||||
func NewGateway(systemKey string) *Gateway {
|
||||
return NewGatewayWithSecret(systemKey, "development-ai-key-secret-change-me")
|
||||
}
|
||||
@@ -47,15 +53,19 @@ func (g *Gateway) SaveUserKey(userID uint, provider string, apiKey string) error
|
||||
|
||||
func (g *Gateway) SelectKey(userID uint) (SelectedKey, error) {
|
||||
var userKey models.SenlinAgentAIKey
|
||||
if err := models.DBService.Where("user_id = ?", userID).First(&userKey).Error; err == nil {
|
||||
err := models.DBService.Where("user_id = ?", userID).First(&userKey).Error
|
||||
if err == nil {
|
||||
apiKey, err := decryptAPIKey(userKey.EncryptedAPIKey, g.encryptionSecret)
|
||||
if err != nil {
|
||||
return SelectedKey{}, err
|
||||
}
|
||||
return SelectedKey{Provider: userKey.Provider, APIKey: apiKey, KeyType: "user"}, nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return SelectedKey{}, err
|
||||
}
|
||||
if g.systemKey == "" {
|
||||
return SelectedKey{}, errors.New("no ai key available")
|
||||
return SelectedKey{}, ErrAIKeyMissing
|
||||
}
|
||||
return SelectedKey{Provider: "openai", APIKey: g.systemKey, KeyType: "system"}, nil
|
||||
}
|
||||
@@ -82,7 +92,7 @@ func (g *Gateway) CheckRateLimit(userID uint, action string, limit int, window t
|
||||
return err
|
||||
}
|
||||
if count >= int64(limit) {
|
||||
return errors.New("ai rate limit exceeded")
|
||||
return ErrAIRateLimited
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user