Almost working edit button

This commit is contained in:
Adrien Bouvais 2024-05-11 17:15:05 +02:00
parent b104c989a5
commit 95a58f0f4b
9 changed files with 129 additions and 85 deletions

126
Chat.go
View File

@ -24,55 +24,6 @@ func ChatPageHandler(c *fiber.Ctx) error {
return c.Render("chat", fiber.Map{"IsLogin": checkIfLogin(), "HaveKey": checkIfHaveKey()}, "layouts/main") return c.Render("chat", fiber.Map{"IsLogin": checkIfLogin(), "HaveKey": checkIfHaveKey()}, "layouts/main")
} }
func LoadModelSelectionHandler(c *fiber.Ctx) error {
openaiExists, anthropicExists, mistralExists := getExistingKeys()
var CompanyInfosAvailable []CompanyInfo
if openaiExists {
var openaiCompanyInfo CompanyInfo
for _, info := range CompanyInfos {
if info.ID == "openai" {
openaiCompanyInfo = info
break
}
}
CompanyInfosAvailable = append(CompanyInfosAvailable, openaiCompanyInfo)
}
if anthropicExists {
var anthropicCompanyInfo CompanyInfo
for _, info := range CompanyInfos {
if info.ID == "anthropic" {
anthropicCompanyInfo = info
break
}
}
CompanyInfosAvailable = append(CompanyInfosAvailable, anthropicCompanyInfo)
}
if mistralExists {
var mistralCompanyInfo CompanyInfo
for _, info := range CompanyInfos {
if info.ID == "mistral" {
mistralCompanyInfo = info
break
}
}
CompanyInfosAvailable = append(CompanyInfosAvailable, mistralCompanyInfo)
}
CheckedModels := []string{"gpt-3.5-turbo"} // Default model
out, err := pongo2.Must(pongo2.FromFile("views/partials/popover-models.html")).Execute(pongo2.Context{
"CompanyInfos": CompanyInfosAvailable,
"CheckedModels": CheckedModels,
})
if err != nil {
c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error rendering template",
})
}
return c.SendString(out)
}
func DeleteMessageHandler(c *fiber.Ctx) error { func DeleteMessageHandler(c *fiber.Ctx) error {
messageId := c.FormValue("id") messageId := c.FormValue("id")
@ -185,6 +136,7 @@ func generateChatHTML() string {
func GetMessageContentHandler(c *fiber.Ctx) error { func GetMessageContentHandler(c *fiber.Ctx) error {
messageId := c.FormValue("id") messageId := c.FormValue("id")
onlyContent := c.FormValue("onlyContent")
messageUUID, err := edgedb.ParseUUID(messageId) messageUUID, err := edgedb.ParseUUID(messageId)
if err != nil { if err != nil {
@ -207,6 +159,10 @@ func GetMessageContentHandler(c *fiber.Ctx) error {
modelID, _ := selectedMessage.ModelID.Get() modelID, _ := selectedMessage.ModelID.Get()
if onlyContent == "true" {
return c.SendString(markdownToHTML(selectedMessage.Content))
}
out := "<div class='message-header'>" out := "<div class='message-header'>"
out += "<p>" out += "<p>"
out += model2Name(modelID) out += model2Name(modelID)
@ -280,8 +236,29 @@ func generateEnterKeyChatHTML() string {
return htmlString return htmlString
} }
// Popover stuff // Buton actions
func GetEditMessageFormHandler(c *fiber.Ctx) error {
id := c.FormValue("id")
idUUID, _ := edgedb.ParseUUID(id)
var message MessageContent
err := edgeClient.QuerySingle(context.Background(), `
SELECT Message { content }
FILTER .id = <uuid>$0;
`, &message, idUUID)
if err != nil {
log.Fatal(err)
}
tmpl := pongo2.Must(pongo2.FromFile("views/partials/edit-message-form.html"))
out, err := tmpl.Execute(pongo2.Context{"Content": message.Content, "ID": id})
if err != nil {
panic(err)
}
return c.SendString(out)
}
// Popover stuff
func LoadUsageKPIHandler(c *fiber.Ctx) error { func LoadUsageKPIHandler(c *fiber.Ctx) error {
var TotalUsage float32 var TotalUsage float32
// Using the database. Get the sum of all usage.inputCost and outputCost // Using the database. Get the sum of all usage.inputCost and outputCost
@ -359,3 +336,52 @@ func LoadSettingsHandler(c *fiber.Ctx) error {
} }
return c.SendString(out) return c.SendString(out)
} }
func LoadModelSelectionHandler(c *fiber.Ctx) error {
openaiExists, anthropicExists, mistralExists := getExistingKeys()
var CompanyInfosAvailable []CompanyInfo
if openaiExists {
var openaiCompanyInfo CompanyInfo
for _, info := range CompanyInfos {
if info.ID == "openai" {
openaiCompanyInfo = info
break
}
}
CompanyInfosAvailable = append(CompanyInfosAvailable, openaiCompanyInfo)
}
if anthropicExists {
var anthropicCompanyInfo CompanyInfo
for _, info := range CompanyInfos {
if info.ID == "anthropic" {
anthropicCompanyInfo = info
break
}
}
CompanyInfosAvailable = append(CompanyInfosAvailable, anthropicCompanyInfo)
}
if mistralExists {
var mistralCompanyInfo CompanyInfo
for _, info := range CompanyInfos {
if info.ID == "mistral" {
mistralCompanyInfo = info
break
}
}
CompanyInfosAvailable = append(CompanyInfosAvailable, mistralCompanyInfo)
}
CheckedModels := []string{"gpt-3.5-turbo"} // Default model
out, err := pongo2.Must(pongo2.FromFile("views/partials/popover-models.html")).Execute(pongo2.Context{
"CompanyInfos": CompanyInfosAvailable,
"CheckedModels": CheckedModels,
})
if err != nil {
c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error rendering template",
})
}
return c.SendString(out)
}

View File

@ -55,6 +55,10 @@ type Message struct {
Conv Conversation `edgedb:"conversation"` Conv Conversation `edgedb:"conversation"`
} }
type MessageContent struct {
Content string `edgedb:"content"`
}
type Usage struct { type Usage struct {
ID edgedb.UUID `edgedb:"id"` ID edgedb.UUID `edgedb:"id"`
ModelID string `edgedb:"model_id"` ModelID string `edgedb:"model_id"`

View File

@ -41,6 +41,7 @@ func main() {
app.Post("/deleteMessage", DeleteMessageHandler) app.Post("/deleteMessage", DeleteMessageHandler)
app.Get("/generateMultipleMessages", GenerateMultipleMessages) app.Get("/generateMultipleMessages", GenerateMultipleMessages)
app.Get("/messageContent", GetMessageContentHandler) app.Get("/messageContent", GetMessageContentHandler)
app.Get("/editMessageForm", GetEditMessageFormHandler)
// Settings routes // Settings routes
app.Post("/addKeys", addKeys) app.Post("/addKeys", addKeys)
@ -56,6 +57,11 @@ func main() {
app.Get("/callback", handleCallback) app.Get("/callback", handleCallback)
app.Get("/callbackSignup", handleCallbackSignup) app.Get("/callbackSignup", handleCallbackSignup)
app.Get("/test", func(c *fiber.Ctx) error {
fmt.Println("Current User: ", getCurrentUser(), " - ", checkIfLogin())
return c.Render("test", fiber.Map{})
})
// Start server // Start server
app.Listen(":8080") app.Listen(":8080")
} }

View File

@ -172,4 +172,18 @@ svg text {
-webkit-transform: rotate(45deg); -webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg); -ms-transform: rotate(45deg);
transform: rotate(45deg); transform: rotate(45deg);
}
/* Message button styles */
.message-button {
opacity: 0;
transition: opacity 0.3s ease;
}
.message-user:hover .message-button {
opacity: 1;
}
.message-bot:hover .message-button {
opacity: 1;
} }

View File

@ -53,7 +53,7 @@ func model2Icon(model string) string {
if strings.Contains(model, "mistral") || strings.Contains(model, "mixtral") { if strings.Contains(model, "mistral") || strings.Contains(model, "mixtral") {
return "mistral" return "mistral"
} }
return "openai" return "bouvai2"
} }
func model2Name(model string) string { func model2Name(model string) string {
@ -62,7 +62,7 @@ func model2Name(model string) string {
return ModelsInfos[i].Name return ModelsInfos[i].Name
} }
} }
return "OpenAI" return "You"
} }
func getExistingKeys() (bool, bool, bool) { func getExistingKeys() (bool, bool, bool) {

View File

@ -22,6 +22,13 @@
{{embed}} {{embed}}
<script>
function updateIcon(icon, ConversationAreaId) {
var selectedIcon = document.getElementById('selectedIcon-' + ConversationAreaId);
selectedIcon.src = 'icons/' + icon + '.png';
}
</script>
<script> <script>
function copyToClipboardCode(button) { function copyToClipboardCode(button) {
// Get the code element next to the button // Get the code element next to the button

View File

@ -0,0 +1,15 @@
<div class="field">
<div class="control">
<textarea class="textarea mt-2" placeholder="Enter your message here"
style="background-color: transparent;">{{ Content }}</textarea>
</div>
</div>
<div class="field is-grouped mb-3">
<div class="control">
<button hx-get="/test" class="button is-primary is-small">Submit</button>
</div>
<div class="control">
<button hx-get="/messageContent?id={{ ID }}&onlyContent=true" hx-target="#content-{{ ID }}"
class="button is-light is-outlined is-small">Cancel</button>
</div>
</div>

View File

@ -66,22 +66,4 @@
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<style>
.message-button {
opacity: 0;
transition: opacity 0.3s ease;
}
.message-bot:hover .message-button {
opacity: 1;
}
</style>
<script>
function updateIcon(icon, ConversationAreaId) {
var selectedIcon = document.getElementById('selectedIcon-' + ConversationAreaId);
selectedIcon.src = 'icons/' + icon + '.png';
}
</script>

View File

@ -15,7 +15,7 @@
</p> </p>
</div> </div>
<div class="message-body"> <div class="message-body">
<div class="content" style="overflow-x: auto;"> <div class="content" style="overflow-x: auto;" id="content-{{ ID }}">
{{ Content|safe }} {{ Content|safe }}
</div> </div>
</div> </div>
@ -27,7 +27,8 @@
<i class="fa-solid fa-arrows-rotate"></i> <i class="fa-solid fa-arrows-rotate"></i>
</span> </span>
</button> </button>
<button id="edit-button-{{ ID }}" class="button is-small is-primary message-button is-outlined mr-2"> <button hx-get="/editMessageForm?id={{ ID }}" hx-target="#content-{{ ID }}" id="edit-button-{{ ID }}"
class="button is-small is-primary message-button is-outlined mr-5">
<span class="icon"> <span class="icon">
<i class="fa-solid fa-pen"></i> <i class="fa-solid fa-pen"></i>
</span> </span>
@ -41,15 +42,4 @@
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<style>
.message-button {
opacity: 0;
transition: opacity 0.3s ease;
}
.message-user:hover .message-button {
opacity: 1;
}
</style>