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.
This commit is contained in:
Max 2024-12-17 17:04:22 +08:00
parent 9598d000cd
commit 13e17ae386
2 changed files with 10 additions and 8 deletions

View file

@ -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

View file

@ -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
}()