226 lines
5.5 KiB
Go
226 lines
5.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/flosch/pongo2"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
"github.com/gofiber/template/django/v3"
|
|
)
|
|
|
|
var userTmpl *pongo2.Template
|
|
var botTmpl *pongo2.Template
|
|
|
|
func main() {
|
|
botTmpl = pongo2.Must(pongo2.FromFile("views/partials/message-bot.html"))
|
|
userTmpl = pongo2.Must(pongo2.FromFile("views/partials/message-user.html"))
|
|
|
|
// Import HTML using django engine/template
|
|
engine := django.New("./views", ".html")
|
|
|
|
// Create new Fiber instance. Can use any framework. I use fiber for speed and simplicity
|
|
app := fiber.New(fiber.Config{
|
|
Views: engine,
|
|
AppName: "JADE 2.0",
|
|
EnablePrintRoutes: true,
|
|
})
|
|
|
|
// Add default logger
|
|
app.Use(logger.New())
|
|
|
|
// Add static files
|
|
app.Static("/", "./static")
|
|
|
|
// Main routes
|
|
app.Get("/", ChatPageHandler)
|
|
app.Get("/loadChat", LoadChatHandler)
|
|
|
|
// Chat routes
|
|
app.Post("/requestMultipleMessages", RequestMultipleMessages)
|
|
app.Post("/deleteMessage", DeleteMessageHandler)
|
|
app.Get("/generateMultipleMessages", GenerateMultipleMessages)
|
|
app.Get("/messageContent", GetMessageContentHandler)
|
|
|
|
// Settings routes
|
|
app.Post("/addKeys", addKeys)
|
|
|
|
// Popovers
|
|
app.Get("/loadModelSelection", LoadModelSelectionHandler)
|
|
app.Get("/loadUsageKPI", LoadUsageKPIHandler)
|
|
app.Get("/loadSettings", LoadSettingsHandler)
|
|
|
|
// Authentication
|
|
app.Get("/signin", handleUiSignIn)
|
|
app.Get("/signout", handleSignOut)
|
|
app.Get("/callback", handleCallback)
|
|
app.Get("/callbackSignup", handleCallbackSignup)
|
|
|
|
// Start server
|
|
app.Listen(":8080")
|
|
}
|
|
|
|
func addKeys(c *fiber.Ctx) error {
|
|
openaiKey := c.FormValue("openai_key")
|
|
anthropicKey := c.FormValue("anthropic_key")
|
|
var Exists bool
|
|
|
|
// Handle OpenAI key
|
|
if openaiKey != "" {
|
|
// Check if the OpenAI key already exists
|
|
err := edgeClient.QuerySingle(edgeCtx, `
|
|
select exists (
|
|
select global currentUser.setting.keys
|
|
filter .company = "openai" AND .key = <str>$0
|
|
);
|
|
`, &Exists, openaiKey)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addOpenaiKey: ", err)
|
|
return c.SendString("")
|
|
}
|
|
if Exists {
|
|
fmt.Println("OpenAI key already exists")
|
|
return c.SendString("")
|
|
}
|
|
|
|
fmt.Println("OpenAI API key: ", openaiKey)
|
|
|
|
if !TestOpenaiKey(openaiKey) {
|
|
fmt.Println("Invalid OpenAI API Key")
|
|
return handleInvalidKey(c, "OpenAI API Key")
|
|
}
|
|
|
|
// Check if the company key already exists
|
|
err = edgeClient.QuerySingle(edgeCtx, `
|
|
select exists (
|
|
select global currentUser.setting.keys
|
|
filter .company = "openai"
|
|
);
|
|
`, &Exists)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addOpenaiKey")
|
|
fmt.Println(err)
|
|
}
|
|
|
|
if Exists {
|
|
fmt.Println("Company key already exists")
|
|
err = edgeClient.Execute(edgeCtx, `
|
|
UPDATE Key filter .company = <str>$0 AND .key = <str>$1
|
|
SET {
|
|
key := <str>$1,
|
|
}
|
|
`, "openai", openaiKey)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addOpenaiKey")
|
|
fmt.Println(err)
|
|
}
|
|
} else {
|
|
fmt.Println("OpenAI API key: ", openaiKey)
|
|
|
|
err = edgeClient.Execute(edgeCtx, `
|
|
UPDATE global currentUser.setting
|
|
SET {
|
|
keys += (
|
|
INSERT Key {
|
|
company := <str>$0,
|
|
key := <str>$1,
|
|
name := <str>$2,
|
|
}
|
|
)
|
|
}`, "openai", openaiKey, "OpenAI API Key")
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addOpenaiKey")
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle Anthropic key
|
|
if anthropicKey != "" {
|
|
// Check if the OpenAI key already exists
|
|
err := edgeClient.QuerySingle(edgeCtx, `
|
|
select exists (
|
|
select global currentUser.setting.keys
|
|
filter .company = "anthropic" AND .key = <str>$0
|
|
);
|
|
`, &Exists, openaiKey)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addAnthropicKey: ", err)
|
|
return c.SendString("")
|
|
}
|
|
if Exists {
|
|
fmt.Println("Anthropic key already exists")
|
|
return c.SendString("")
|
|
}
|
|
|
|
if !TestAnthropicKey(anthropicKey) {
|
|
fmt.Println("Invalid Anthropic API Key")
|
|
return handleInvalidKey(c, "Anthropic API Key")
|
|
}
|
|
|
|
// Check if the company key already exists
|
|
err = edgeClient.QuerySingle(edgeCtx, `
|
|
select exists (
|
|
select global currentUser.setting.keys
|
|
filter .company = "anthropic"
|
|
);
|
|
`, &Exists)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addAnthropicKey")
|
|
fmt.Println(err)
|
|
}
|
|
|
|
if Exists {
|
|
fmt.Println("Company key already exists")
|
|
err = edgeClient.Execute(edgeCtx, `
|
|
UPDATE Key filter .company = "anthropic" AND .key = <str>$0
|
|
SET {
|
|
key := <str>$0,
|
|
}
|
|
`, anthropicKey)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addAnthropicKey")
|
|
fmt.Println(err)
|
|
}
|
|
} else {
|
|
fmt.Println("OpenAI API key: ", openaiKey)
|
|
|
|
err = edgeClient.Execute(edgeCtx, `
|
|
UPDATE global currentUser.setting
|
|
SET {
|
|
keys += (
|
|
INSERT Key {
|
|
company := "anthropic",
|
|
key := <str>$0,
|
|
name := "Anthropic API Key",
|
|
}
|
|
)
|
|
}`, anthropicKey)
|
|
if err != nil {
|
|
fmt.Println("Error in edgedb.QuerySingle: in addAnthropicKey")
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return c.SendString("<script>window.location.reload()</script>")
|
|
}
|
|
|
|
func handleInvalidKey(c *fiber.Ctx, keyType string) error {
|
|
NextMessages := []NextMessage{}
|
|
nextMsg := NextMessage{
|
|
Icon: "bouvai2",
|
|
Content: "<br>" + markdownToHTML(fmt.Sprintf("Invalid %s", keyType)),
|
|
Hidden: false,
|
|
Id: "0",
|
|
Name: "JADE",
|
|
}
|
|
NextMessages = append(NextMessages, nextMsg)
|
|
|
|
botOut, err := botTmpl.Execute(pongo2.Context{"Messages": NextMessages, "ConversationAreaId": 0})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return c.SendString(botOut)
|
|
}
|