189 lines
4.5 KiB
Go
189 lines
4.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"regexp"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/yuin/goldmark"
|
|
highlighting "github.com/yuin/goldmark-highlighting"
|
|
"github.com/yuin/goldmark/parser"
|
|
"github.com/yuin/goldmark/renderer/html"
|
|
)
|
|
|
|
var md goldmark.Markdown
|
|
|
|
func init() {
|
|
md = goldmark.New(goldmark.WithExtensions(highlighting.NewHighlighting(highlighting.WithStyle("monokai"))),
|
|
goldmark.WithParserOptions(
|
|
parser.WithAutoHeadingID(),
|
|
),
|
|
goldmark.WithRendererOptions(
|
|
html.WithHardWraps(),
|
|
html.WithXHTML(),
|
|
))
|
|
}
|
|
|
|
func markdownToHTML(markdownText string) string {
|
|
var buf bytes.Buffer
|
|
|
|
if err := md.Convert([]byte(markdownText), &buf); err != nil {
|
|
fmt.Println("failed to convert markdown to HTML")
|
|
panic(err)
|
|
}
|
|
|
|
return addCodeHeader(buf.String(), extractLanguages(markdownText))
|
|
}
|
|
|
|
func extractLanguages(markdownText string) []string {
|
|
// Regular expression to match code blocks and capture the language identifier
|
|
re := regexp.MustCompile("(?s)```([a-zA-Z0-9]*)\n.*?```")
|
|
|
|
matches := re.FindAllStringSubmatch(markdownText, -1)
|
|
languages := make([]string, 0, len(matches))
|
|
|
|
for _, match := range matches {
|
|
if len(match) > 1 {
|
|
languages = append(languages, match[1])
|
|
} else {
|
|
languages = append(languages, "")
|
|
}
|
|
}
|
|
|
|
return languages
|
|
}
|
|
|
|
func addCodeHeader(htmlContent string, languages []string) string {
|
|
// Regular expression pattern to match <pre> elements
|
|
pattern := `(<pre[^>]*>)`
|
|
|
|
// Compile the regular expression
|
|
re := regexp.MustCompile(pattern)
|
|
|
|
// Replace each matched <pre> element with the updated HTML
|
|
updatedHTML := re.ReplaceAllStringFunc(htmlContent, func(match string) string {
|
|
var language string
|
|
if len(languages) > 0 {
|
|
language = languages[0]
|
|
languages = languages[1:]
|
|
}
|
|
headerHTML := fmt.Sprintf(`<div class="code-header"><span>%s</span><button class="copy-button" onclick="copyToClipboardCode(this)"><i class="fa-solid fa-copy"></i></button></div>`, language)
|
|
return headerHTML + match
|
|
})
|
|
|
|
return updatedHTML
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
var userInfo User
|
|
|
|
err := edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, `
|
|
SELECT global currentUser {
|
|
setting: {
|
|
keys: {
|
|
name,
|
|
key,
|
|
company: {
|
|
name
|
|
}
|
|
}
|
|
}
|
|
}
|
|
LIMIT 1;
|
|
`, &userInfo)
|
|
if err != nil {
|
|
fmt.Println("Error getting user keys", err)
|
|
return false, false, false, false, false, false, false, false, false, false
|
|
}
|
|
|
|
openaiExists := false
|
|
anthropicExists := false
|
|
mistralExists := false
|
|
groqExists := false
|
|
nimExists := false
|
|
googleExists := false
|
|
perplexityExists := false
|
|
fireworksExists := false
|
|
togetherExists := false
|
|
deepseekExists := false
|
|
|
|
for _, key := range userInfo.Setting.Keys {
|
|
switch key.Company.Name {
|
|
case "openai":
|
|
openaiExists = true
|
|
case "anthropic":
|
|
anthropicExists = true
|
|
case "mistral":
|
|
mistralExists = true
|
|
case "groq":
|
|
groqExists = true
|
|
case "nim":
|
|
nimExists = true
|
|
case "google":
|
|
googleExists = true
|
|
case "perplexity":
|
|
perplexityExists = true
|
|
case "fireworks":
|
|
fireworksExists = true
|
|
case "together":
|
|
togetherExists = true
|
|
case "deepseek":
|
|
deepseekExists = true
|
|
}
|
|
}
|
|
|
|
return openaiExists, anthropicExists, mistralExists, groqExists, googleExists, perplexityExists, fireworksExists, nimExists, togetherExists, deepseekExists
|
|
}
|
|
|
|
func Message2RequestMessage(messages []Message, context string) []RequestMessage {
|
|
// Add context if it exists
|
|
if context != "" {
|
|
m := make([]RequestMessage, len(messages)+1)
|
|
m[0] = RequestMessage{
|
|
Role: "system",
|
|
Content: context,
|
|
}
|
|
|
|
for i, msg := range messages {
|
|
var role string
|
|
switch msg.Role {
|
|
case "user":
|
|
role = "user"
|
|
case "bot":
|
|
role = "assistant"
|
|
default:
|
|
role = "system"
|
|
}
|
|
m[i+1] = RequestMessage{
|
|
Role: role,
|
|
Content: msg.Content,
|
|
}
|
|
}
|
|
return m
|
|
} else {
|
|
m := make([]RequestMessage, len(messages))
|
|
|
|
for i, msg := range messages {
|
|
var role string
|
|
switch msg.Role {
|
|
case "user":
|
|
role = "user"
|
|
case "bot":
|
|
role = "assistant"
|
|
default:
|
|
role = "system"
|
|
}
|
|
m[i] = RequestMessage{
|
|
Role: role,
|
|
Content: msg.Content,
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
}
|