From 05b9757b69d9ff5a31ef08b4cd6a27186ea52558 Mon Sep 17 00:00:00 2001 From: sahari Date: Thu, 11 Jul 2024 21:24:40 -0700 Subject: [PATCH] feat: ability to delete chats --- app/config.go | 25 +++++++++++++++++++++++++ app/database.go | 12 ++++++++++++ app/message.go | 16 ++++++++++++++++ app/run.go | 11 ----------- app/server.go | 27 +-------------------------- templates/index.html | 2 +- templates/sidebar.html | 29 +++++++++++++++++++++++++---- 7 files changed, 80 insertions(+), 42 deletions(-) create mode 100644 app/config.go diff --git a/app/config.go b/app/config.go new file mode 100644 index 0000000..450d3b5 --- /dev/null +++ b/app/config.go @@ -0,0 +1,25 @@ +package app + +type ModelConfig struct { + Platform string + ModelID string + + Temperature float64 + TopP float64 + TopK int + MaxTokens int + + Region string // used for AWS models +} + +func NewModelConfig(platform, modelID, region string, temp, topP float64, topK, maxTokens int) ModelConfig { + return ModelConfig{ + Platform: platform, + ModelID: modelID, + Temperature: temp, + TopP: topP, + TopK: topK, + MaxTokens: maxTokens, + Region: region, + } +} diff --git a/app/database.go b/app/database.go index 48f6ca4..a4c6547 100644 --- a/app/database.go +++ b/app/database.go @@ -122,6 +122,18 @@ func createNewConversation() (int, error) { return conversationID, nil } +func deleteConversation(conversationID int) error { + // Delete associated messages first + _, err := db.Exec("DELETE FROM messages WHERE conversation_id = ?", conversationID) + if err != nil { + return err + } + + // Delete the conversation + _, err = db.Exec("DELETE FROM conversations WHERE id = ?", conversationID) + return err +} + type Message struct { ID int `json:"id"` ConversationID int `json:"conversation_id"` diff --git a/app/message.go b/app/message.go index b4d47aa..afde69e 100644 --- a/app/message.go +++ b/app/message.go @@ -106,3 +106,19 @@ func getAllConversationsHandler(c *gin.Context) { "Conversations": conversations, }) } + +func deleteChat(c *gin.Context) { + conversationID, err := strconv.Atoi(c.Param("id")) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid conversation ID"}) + return + } + + err = deleteConversation(conversationID) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete conversation"}) + return + } + + c.JSON(http.StatusOK, gin.H{"status": "Conversation deleted"}) +} diff --git a/app/run.go b/app/run.go index 528bce7..d6b361c 100644 --- a/app/run.go +++ b/app/run.go @@ -104,17 +104,6 @@ func (mc *ModelConfig) runModel(ctx context.Context) gin.HandlerFunc { }) } - // Fetch updated list of conversations - conversations, err := getAllConversations() - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve conversations"}) - return - } - - c.HTML(http.StatusOK, "sidebar.html", gin.H{ - "Conversations": conversations, - }) - c.HTML(http.StatusOK, "chat.html", gin.H{ "ChatMessages": chatMessages, "Platform": platform, diff --git a/app/server.go b/app/server.go index 80adcc1..0517981 100644 --- a/app/server.go +++ b/app/server.go @@ -9,32 +9,6 @@ import ( "github.com/gin-gonic/gin" ) -var completion string - -type ModelConfig struct { - Platform string - ModelID string - - Temperature float64 - TopP float64 - TopK int - MaxTokens int - - Region string // used for AWS models -} - -func NewModelConfig(platform, modelID, region string, temp, topP float64, topK, maxTokens int) ModelConfig { - return ModelConfig{ - Platform: platform, - ModelID: modelID, - Temperature: temp, - TopP: topP, - TopK: topK, - MaxTokens: maxTokens, - Region: region, - } -} - func (mc *ModelConfig) Serve(ctx context.Context) { router := gin.Default() router.LoadHTMLGlob("templates/*") @@ -50,6 +24,7 @@ func (mc *ModelConfig) Serve(ctx context.Context) { router.POST("/select-model", selectModel) router.POST("/run", mc.runModel(ctx)) router.POST("/new-conversation", createConversation) + router.DELETE("/conversation/:id/delete", deleteChat) if err := router.Run(":31000"); err != nil { log.Fatal(err) diff --git a/templates/index.html b/templates/index.html index 99bebab..f70355a 100644 --- a/templates/index.html +++ b/templates/index.html @@ -40,7 +40,7 @@ hx-swap="innerHTML" >
- {{.Message}} + {{.Message}} {{end}}