Jade/utils.go

147 lines
3.3 KiB
Go

package main
import (
"bytes"
"fmt"
"regexp"
"github.com/yuin/goldmark"
highlighting "github.com/yuin/goldmark-highlighting"
)
func markdownToHTML(markdownText string) string {
var buf bytes.Buffer
md := goldmark.New(
goldmark.WithExtensions(
highlighting.NewHighlighting(highlighting.WithStyle("monokai")),
),
)
if err := md.Convert([]byte(markdownText), &buf); err != nil {
panic(err) // Handle the error appropriately
}
return addCopyButtonsToCode(buf.String())
}
func addCopyButtonsToCode(htmlContent string) string {
buttonHTML := `<button class="copy-button button is-small is-primary is-outlined" onclick="copyToClipboardCode(this)"><i class="fa-solid fa-copy"></i></button>`
// Regular expression pattern to match <pre> elements and insert the button right before <code>
pattern := `(<pre[^>]*>)`
// Compile the regular expression
re := regexp.MustCompile(pattern)
// Replace each matched <pre> element with the updated HTML
updatedHTML := re.ReplaceAllString(htmlContent, "$1"+buttonHTML)
return updatedHTML
}
func model2Icon(model string) string {
for i := range ModelsInfos {
if ModelsInfos[i].ID == model {
return ModelsInfos[i].Icon
}
}
return "bouvai2"
}
func model2Name(model string) string {
for i := range ModelsInfos {
if ModelsInfos[i].ID == model {
return ModelsInfos[i].Name
}
}
return "You"
}
func getExistingKeys() (bool, bool, bool, bool) {
if edgeClient == nil {
return false, false, false, false
}
var openaiExists bool
var anthropicExists bool
var mistralExists bool
var groqExists bool
err := edgeClient.QuerySingle(edgeCtx, `
select exists (
select global currentUser.setting.keys
filter .company = "openai"
);
`, &openaiExists)
if err != nil {
fmt.Println("Error in edgedb.QuerySingle checking for openai: ", err)
openaiExists = false
}
err = edgeClient.QuerySingle(edgeCtx, `
select exists (
select global currentUser.setting.keys
filter .company = "anthropic"
);
`, &anthropicExists)
if err != nil {
fmt.Println("Error in edgedb.QuerySingle checking for anthropic: ", err)
anthropicExists = false
}
err = edgeClient.QuerySingle(edgeCtx, `
select exists (
select global currentUser.setting.keys
filter .company = "mistral"
);
`, &mistralExists)
if err != nil {
fmt.Println("Error in edgedb.QuerySingle checking for mistral: ", err)
mistralExists = false
}
err = edgeClient.QuerySingle(edgeCtx, `
select exists (
select global currentUser.setting.keys
filter .company = "groq"
);
`, &groqExists)
if err != nil {
fmt.Println("Error in edgedb.QuerySingle checking for mistral: ", err)
groqExists = false
}
return openaiExists, anthropicExists, mistralExists, groqExists
}
func removeDuplicate(s []string) []string {
m := make(map[string]bool)
var result []string
for _, str := range s {
if !m[str] {
m[str] = true
result = append(result, str)
}
}
return result
}
func ChangeRoleBot2Assistant(messages []Message) []Message {
openaiMessages := make([]Message, len(messages))
for i, msg := range messages {
var role string
switch msg.Role {
case "user":
role = "user"
case "bot":
role = "assistant"
default:
role = "system"
}
openaiMessages[i] = Message{
Role: role,
Content: msg.Content,
}
}
return openaiMessages
}