Selection button event now send an htmx request
This commit is contained in:
parent
5b7c4988d4
commit
99019595a2
104
Chat.go
104
Chat.go
@ -18,30 +18,6 @@ func ChatPageHandler(c *fiber.Ctx) error {
|
|||||||
return c.Render("chat", fiber.Map{}, "layouts/main")
|
return c.Render("chat", fiber.Map{}, "layouts/main")
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteMessageHandler(c *fiber.Ctx) error {
|
|
||||||
messageId := c.FormValue("id")
|
|
||||||
|
|
||||||
messageUUID, err := edgedb.ParseUUID(messageId)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error parsing UUID")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete all messages
|
|
||||||
err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).Execute(edgeCtx, `
|
|
||||||
WITH
|
|
||||||
messageArea := (SELECT Message FILTER .id = <uuid>$0).area
|
|
||||||
DELETE Area
|
|
||||||
FILTER .position >= messageArea.position AND .conversation = messageArea.conversation;
|
|
||||||
`, messageUUID)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error deleting messages")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.SendString(generateChatHTML(c))
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadChatHandler(c *fiber.Ctx) error {
|
func LoadChatHandler(c *fiber.Ctx) error {
|
||||||
deleteLLMtoDelete(c)
|
deleteLLMtoDelete(c)
|
||||||
|
|
||||||
@ -531,7 +507,87 @@ func generateLimitReachedChatHTML(c *fiber.Ctx) string {
|
|||||||
return htmlString
|
return htmlString
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetSelectionBtnHandler(c *fiber.Ctx) error {
|
||||||
|
messageId := c.FormValue("id")
|
||||||
|
messageUUID, err := edgedb.ParseUUID(messageId)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error parsing UUID")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var message Message
|
||||||
|
err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).QuerySingle(edgeCtx, `
|
||||||
|
SELECT Message {
|
||||||
|
id,
|
||||||
|
content,
|
||||||
|
area : {
|
||||||
|
id,
|
||||||
|
position
|
||||||
|
},
|
||||||
|
llm : {
|
||||||
|
modelInfo : {
|
||||||
|
modelID,
|
||||||
|
name,
|
||||||
|
company : {
|
||||||
|
icon,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FILTER .id = <uuid>$0;
|
||||||
|
`, &message, messageUUID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error getting message")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
templateMessage := TemplateMessage{
|
||||||
|
Icon: message.LLM.Model.Company.Icon,
|
||||||
|
Content: message.Content,
|
||||||
|
Hidden: false,
|
||||||
|
Id: message.ID.String(),
|
||||||
|
Name: message.LLM.Model.Name,
|
||||||
|
ModelID: message.LLM.Model.ModelID,
|
||||||
|
}
|
||||||
|
|
||||||
|
outBtn, err := selectBtnTmpl.Execute(map[string]interface{}{
|
||||||
|
"message": templateMessage,
|
||||||
|
"ConversationAreaId": message.Area.Position,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error generating HTML content")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
outBtn = strings.ReplaceAll(outBtn, "\n", "")
|
||||||
|
|
||||||
|
return c.SendString(outBtn)
|
||||||
|
}
|
||||||
|
|
||||||
// Button actions
|
// Button actions
|
||||||
|
func DeleteMessageHandler(c *fiber.Ctx) error {
|
||||||
|
messageId := c.FormValue("id")
|
||||||
|
|
||||||
|
messageUUID, err := edgedb.ParseUUID(messageId)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error parsing UUID")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete all messages
|
||||||
|
err = edgeGlobalClient.WithGlobals(map[string]interface{}{"ext::auth::client_token": c.Cookies("jade-edgedb-auth-token")}).Execute(edgeCtx, `
|
||||||
|
WITH
|
||||||
|
messageArea := (SELECT Message FILTER .id = <uuid>$0).area
|
||||||
|
DELETE Area
|
||||||
|
FILTER .position >= messageArea.position AND .conversation = messageArea.conversation;
|
||||||
|
`, messageUUID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error deleting messages")
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.SendString(generateChatHTML(c))
|
||||||
|
}
|
||||||
|
|
||||||
func GetEditMessageFormHandler(c *fiber.Ctx) error {
|
func GetEditMessageFormHandler(c *fiber.Ctx) error {
|
||||||
id := c.FormValue("id")
|
id := c.FormValue("id")
|
||||||
idUUID, _ := edgedb.ParseUUID(id)
|
idUUID, _ := edgedb.ParseUUID(id)
|
||||||
|
36
Request.go
36
Request.go
@ -14,7 +14,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -199,15 +198,6 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
templateMessage := TemplateMessage{
|
|
||||||
Icon: message.LLM.Model.Company.Icon,
|
|
||||||
Content: message.Content,
|
|
||||||
Hidden: false,
|
|
||||||
Id: message.ID.String(),
|
|
||||||
Name: message.LLM.Model.Name,
|
|
||||||
ModelID: message.LLM.Model.ModelID,
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the context's deadline is exceeded
|
// Check if the context's deadline is exceeded
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@ -219,16 +209,6 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error {
|
|||||||
case firstDone <- idx:
|
case firstDone <- idx:
|
||||||
// Generate the HTML content
|
// Generate the HTML content
|
||||||
|
|
||||||
outBtn, err := selectBtnTmpl.Execute(map[string]interface{}{
|
|
||||||
"message": templateMessage,
|
|
||||||
"ConversationAreaId": message.Area.Position,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error generating HTML content")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
outBtn = strings.ReplaceAll(outBtn, "\n", "")
|
|
||||||
|
|
||||||
outIcon := `<img src="` + selectedLLMs[idx].Model.Company.Icon + `" alt="User Image" id="selectedIcon-` + fmt.Sprintf("%d", message.Area.Position) + `">`
|
outIcon := `<img src="` + selectedLLMs[idx].Model.Company.Icon + `" alt="User Image" id="selectedIcon-` + fmt.Sprintf("%d", message.Area.Position) + `">`
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -241,7 +221,7 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error {
|
|||||||
sendEvent(
|
sendEvent(
|
||||||
user.ID.String(),
|
user.ID.String(),
|
||||||
"swapSelectionBtn-"+selectedLLMs[idx].ID.String(),
|
"swapSelectionBtn-"+selectedLLMs[idx].ID.String(),
|
||||||
outBtn,
|
`<hx hx-get="/selectionBtn?id=`+message.ID.String()+`" hx-trigger="load" hx-swap="outerHTML"></hx>`,
|
||||||
)
|
)
|
||||||
sendEvent(
|
sendEvent(
|
||||||
user.ID.String(),
|
user.ID.String(),
|
||||||
@ -250,24 +230,12 @@ func GenerateMultipleMessagesHandler(c *fiber.Ctx) error {
|
|||||||
)
|
)
|
||||||
}()
|
}()
|
||||||
default:
|
default:
|
||||||
out, err := selectBtnTmpl.Execute(map[string]interface{}{
|
|
||||||
"message": templateMessage,
|
|
||||||
"ConversationAreaId": message.Area.Position,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error generating HTML content")
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace newline characters to prevent premature termination
|
|
||||||
outBtn := strings.ReplaceAll(out, "\n", "")
|
|
||||||
|
|
||||||
// Send Content event
|
// Send Content event
|
||||||
go func() {
|
go func() {
|
||||||
sendEvent(
|
sendEvent(
|
||||||
user.ID.String(),
|
user.ID.String(),
|
||||||
"swapSelectionBtn-"+selectedLLMs[idx].ID.String(),
|
"swapSelectionBtn-"+selectedLLMs[idx].ID.String(),
|
||||||
outBtn,
|
`<hx hx-get="/selectionBtn?id=`+message.ID.String()+`" hx-trigger="load" hx-swap="outerHTML"></hx>`,
|
||||||
)
|
)
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,7 @@ func addFireworkMessage(c *fiber.Ctx, llm LLM, selected bool) edgedb.UUID {
|
|||||||
fmt.Println("Error fetching user profile")
|
fmt.Println("Error fetching user profile")
|
||||||
panic(err)
|
panic(err)
|
||||||
} else if len(chatCompletion.Choices) == 0 {
|
} else if len(chatCompletion.Choices) == 0 {
|
||||||
fmt.Println("No response from OpenAI")
|
fmt.Println("No response from Firework")
|
||||||
id := insertBotMessage(c, "No response from Firework", selected, llm.ID)
|
id := insertBotMessage(c, "No response from Firework", selected, llm.ID)
|
||||||
return id
|
return id
|
||||||
} else {
|
} else {
|
||||||
@ -119,7 +119,7 @@ func RequestFirework(c *fiber.Ctx, model string, messages []Message, temperature
|
|||||||
} filter .company.name = <str>$0 AND .<keys[is Setting].<setting[is User] = global currentUser
|
} filter .company.name = <str>$0 AND .<keys[is Setting].<setting[is User] = global currentUser
|
||||||
)
|
)
|
||||||
select filtered_keys.key limit 1
|
select filtered_keys.key limit 1
|
||||||
`, &apiKey, "openai")
|
`, &apiKey, "fireworks")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return FireworkChatCompletionResponse{}, fmt.Errorf("error getting OpenAI API key: %w", err)
|
return FireworkChatCompletionResponse{}, fmt.Errorf("error getting OpenAI API key: %w", err)
|
||||||
}
|
}
|
||||||
@ -176,6 +176,12 @@ func RequestFirework(c *fiber.Ctx, model string, messages []Message, temperature
|
|||||||
return FireworkChatCompletionResponse{}, fmt.Errorf("error getting model info: %w", err)
|
return FireworkChatCompletionResponse{}, fmt.Errorf("error getting model info: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(chatCompletionResponse.Choices) == 0 {
|
||||||
|
// Print the response as a JSON string
|
||||||
|
fmt.Println(string(body))
|
||||||
|
return FireworkChatCompletionResponse{}, fmt.Errorf("no response from Firework")
|
||||||
|
}
|
||||||
|
|
||||||
var inputCost float32 = float32(chatCompletionResponse.Usage.PromptTokens) * usedModelInfo.InputPrice
|
var inputCost float32 = float32(chatCompletionResponse.Usage.PromptTokens) * usedModelInfo.InputPrice
|
||||||
var outputCost float32 = float32(chatCompletionResponse.Usage.CompletionTokens) * usedModelInfo.OutputPrice
|
var outputCost float32 = float32(chatCompletionResponse.Usage.CompletionTokens) * usedModelInfo.OutputPrice
|
||||||
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
addUsage(c, inputCost, outputCost, chatCompletionResponse.Usage.PromptTokens, chatCompletionResponse.Usage.CompletionTokens, model)
|
||||||
|
1
main.go
1
main.go
@ -99,6 +99,7 @@ func main() {
|
|||||||
app.Get("/userMessage", GetUserMessageHandler)
|
app.Get("/userMessage", GetUserMessageHandler)
|
||||||
app.Post("/editMessage", EditMessageHandler)
|
app.Post("/editMessage", EditMessageHandler)
|
||||||
app.Get("/help", generateHelpChatHandler)
|
app.Get("/help", generateHelpChatHandler)
|
||||||
|
app.Get("/selectionBtn", GetSelectionBtnHandler)
|
||||||
|
|
||||||
// Settings routes
|
// Settings routes
|
||||||
app.Post("/addKeys", addKeys)
|
app.Post("/addKeys", addKeys)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user