diff --git a/Chat.go b/Chat.go index e406527..adca8e1 100644 --- a/Chat.go +++ b/Chat.go @@ -934,7 +934,7 @@ func LoadSettingsHandler(c *fiber.Ctx) error { stripeSubLink := "https://billing.stripe.com/p/login/6oE6sc0PTfvq1Hi288?prefilled_email=" + user.Email - openaiExists, anthropicExists, mistralExists, groqExists, gooseaiExists, googleExists, perplexityExists, fireworksExists, nimExists, togetherExists, deepseekExists := getExistingKeysNew(c) + openaiExists, anthropicExists, mistralExists, groqExists, googleExists, perplexityExists, fireworksExists, nimExists, togetherExists, deepseekExists := getExistingKeysNew(c) isPremium, isBasic := IsCurrentUserSubscribed(c) out, err := settingPopoverTmpl.Execute(pongo2.Context{ @@ -943,14 +943,13 @@ func LoadSettingsHandler(c *fiber.Ctx) error { "AnthropicExists": anthropicExists, "MistralExists": mistralExists, "GroqExists": groqExists, - "GooseaiExists": gooseaiExists, "GoogleExists": googleExists, "NimExists": nimExists, "PerplexityExists": perplexityExists, "FireworksExists": fireworksExists, "TogetherExists": togetherExists, "DeepseekExists": deepseekExists, - "AnyExists": fireworksExists || openaiExists || anthropicExists || mistralExists || groqExists || gooseaiExists || googleExists || perplexityExists || nimExists || togetherExists || deepseekExists, + "AnyExists": fireworksExists || openaiExists || anthropicExists || mistralExists || groqExists || googleExists || perplexityExists || nimExists || togetherExists || deepseekExists, "isPremium": isPremium, "isBasic": isBasic, "StripeSubLink": stripeSubLink, diff --git a/MyUtils.go b/MyUtils.go index d8124a5..5878688 100644 --- a/MyUtils.go +++ b/MyUtils.go @@ -75,10 +75,9 @@ func addCodeHeader(htmlContent string, languages []string) string { return updatedHTML } -func getExistingKeysNew(c *fiber.Ctx) (bool, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool) { - // TODO: Optimize to use only one query +func getExistingKeysNew(c *fiber.Ctx) (bool, bool, bool, bool, bool, bool, bool, bool, bool, bool) { if edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}) == nil { - return false, false, false, false, false, false, false, false, false, false, false + return false, false, false, false, false, false, false, false, false, false } var userInfo User @@ -99,14 +98,13 @@ func getExistingKeysNew(c *fiber.Ctx) (bool, bool, bool, bool, bool, bool, bool, `, &userInfo) if err != nil { fmt.Println("Error getting user keys", err) - return false, false, false, false, false, false, false, false, false, false, false + return false, false, false, false, false, false, false, false, false, false } openaiExists := false anthropicExists := false mistralExists := false groqExists := false - gooseaiExists := false nimExists := false googleExists := false perplexityExists := false @@ -124,8 +122,6 @@ func getExistingKeysNew(c *fiber.Ctx) (bool, bool, bool, bool, bool, bool, bool, mistralExists = true case "groq": groqExists = true - case "gooseai": - gooseaiExists = true case "nim": nimExists = true case "google": @@ -141,7 +137,7 @@ func getExistingKeysNew(c *fiber.Ctx) (bool, bool, bool, bool, bool, bool, bool, } } - return openaiExists, anthropicExists, mistralExists, groqExists, gooseaiExists, googleExists, perplexityExists, fireworksExists, nimExists, togetherExists, deepseekExists + return openaiExists, anthropicExists, mistralExists, groqExists, googleExists, perplexityExists, fireworksExists, nimExists, togetherExists, deepseekExists } func Message2RequestMessage(messages []Message, context string) []RequestMessage { diff --git a/RequestMistral.go b/RequestMistral.go index c185c9a..19cdc31 100644 --- a/RequestMistral.go +++ b/RequestMistral.go @@ -39,7 +39,6 @@ func TestMistralKey(apiKey string) bool { } req.Header.Set("Content-Type", "application/json") - req.Header.Set("Accept", "application/json") req.Header.Set("Authorization", "Bearer "+apiKey) client := &http.Client{} diff --git a/main.go b/main.go index 5a73792..9dcee5d 100644 --- a/main.go +++ b/main.go @@ -155,37 +155,41 @@ func addKeys(c *fiber.Ctx) error { "deepseek": TestDeepseekKey, } + openaiExists, anthropicExists, mistralExists, groqExists, googleExists, perplexityExists, fireworksExists, nimExists, togetherExists, deepseekExists := getExistingKeysNew(c) + + keyExists := map[string]bool{ + "openai": openaiExists, + "anthropic": anthropicExists, + "mistral": mistralExists, + "groq": groqExists, + "google": googleExists, + "nim": nimExists, + "perplexity": perplexityExists, + "fireworks": fireworksExists, + "together": togetherExists, + "deepseek": deepseekExists, + } + for company, key := range keys { if key != "" { if !testFunctions[company](key) { return c.SendString(fmt.Sprintf("Invalid %s API Key\n", company)) } - var Exists bool - err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, ` - select exists ( - select global currentUser.setting.keys - filter .company.name = $0 - ); - `, &Exists, company) - if err != nil { - fmt.Println("Error checking if key exists") - panic(err) - } - - if Exists { - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).Execute(edgeCtx, ` - UPDATE Key filter .company.name = $0 AND .key = $1 - SET { - key := $1, - } - `, company, key) + if keyExists[company] { + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).Execute(edgeCtx, ` + UPDATE Key + FILTER .company.name = $0 AND .$1, + } + `, company, key) if err != nil { fmt.Println("Error updating key") panic(err) } } else { - err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).Execute(edgeCtx, ` + err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).Execute(edgeCtx, ` WITH c := (SELECT Company FILTER .name = $0 LIMIT 1) UPDATE global currentUser.setting