From 13e17ae38636d10636259feddd0ffbd901704378 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 17 Dec 2024 17:04:22 +0800 Subject: [PATCH] Enhance chat handling and API response in Neo - Added time-based generation of chat IDs when not provided, improving chat session management. - Updated context handling in handleChat to ensure valid chat IDs are used. - Expanded allowed HTTP methods in CORS headers to include DELETE, enhancing API flexibility. - Removed redundant chat history saving logic from GenerateChatTitle method, streamlining chat processing. --- neo/api.go | 13 ++++++++++--- neo/neo.go | 5 ----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/neo/api.go b/neo/api.go index 58320cc5..7e39f49a 100644 --- a/neo/api.go +++ b/neo/api.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strconv" "strings" + "time" "github.com/gin-gonic/gin" "github.com/google/uuid" @@ -114,8 +115,14 @@ func (neo *DSL) handleChat(c *gin.Context) { return } - // Set the context - ctx, cancel := NewContextWithCancel(sid, c.Query("chat_id"), c.Query("context")) + chatID := c.Query("chat_id") + if chatID == "" { + // Only generate new chat_id if not provided + chatID = fmt.Sprintf("chat_%d", time.Now().UnixNano()) + } + + // Set the context with validated chat_id + ctx, cancel := NewContextWithCancel(sid, chatID, c.Query("context")) defer cancel() neo.Answer(ctx, content, c) @@ -278,7 +285,7 @@ func (neo *DSL) optionsHandler(c *gin.Context) { origin := neo.getOrigin(c) if origin != "" { c.Header("Access-Control-Allow-Origin", origin) - c.Header("Access-Control-Allow-Methods", "GET, POST, OPTIONS") + c.Header("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS") c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization, Accept") c.Header("Access-Control-Allow-Credentials", "true") c.Header("Access-Control-Max-Age", "86400") // 24 hours diff --git a/neo/neo.go b/neo/neo.go index 73b1aa3f..48ef866e 100644 --- a/neo/neo.go +++ b/neo/neo.go @@ -134,11 +134,6 @@ func (neo *DSL) GenerateChatTitle(ctx Context, input string, c *gin.Context) (st message.New().Error(err).Done().Write(c.Writer) } - // Save chat history - if len(content) > 0 { - neo.saveHistory(ctx.Sid, ctx.ChatID, content, messages) - } - done <- true }()