mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-06-29 23:41:12 +08:00
feat[Go]: implement Create-Chat/Session, Delete-Session (#16386)
### What problem does this PR solve?
As title:
implement:
```go
chats.POST("", r.chatHandler.Create)
chats.POST("/:chat_id/sessions", r.chatSessionHandler.CreateSession)
chats.DELETE("/:chat_id/sessions", r.chatSessionHandler.DeleteSessions)
```
bug fixed:
f80d4c7843/internal/handler/chat.go (L84)
↓
```go
result, err := h.chatService.ListChats(userID, "1", keywords, page, pageSize, orderby, desc)
```
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [x] Refactoring
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"ragflow/internal/common"
|
||||
"strconv"
|
||||
@@ -81,7 +82,7 @@ func (h *ChatHandler) ListChats(c *gin.Context) {
|
||||
}
|
||||
|
||||
// List chats - default to valid status "1" (same as Python StatusEnum.VALID.value)
|
||||
result, err := h.chatService.ListChats(userID, keywords, "1", page, pageSize, orderby, desc)
|
||||
result, err := h.chatService.ListChats(userID, "1", keywords, page, pageSize, orderby, desc)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
@@ -97,6 +98,46 @@ func (h *ChatHandler) ListChats(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// Create creates a chat.
|
||||
// @Summary Create Chat
|
||||
// @Description Create a chat, aligned with Python POST /api/v1/chats.
|
||||
// @Tags chat
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body service.CreateChatRequest true "chat configuration"
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Router /api/v1/chats [post]
|
||||
func (h *ChatHandler) Create(c *gin.Context) {
|
||||
user, errorCode, errorMessage := GetUser(c)
|
||||
if errorCode != common.CodeSuccess {
|
||||
jsonError(c, errorCode, errorMessage)
|
||||
return
|
||||
}
|
||||
|
||||
var req map[string]interface{}
|
||||
decoder := json.NewDecoder(c.Request.Body)
|
||||
decoder.UseNumber()
|
||||
if err := decoder.Decode(&req); err != nil {
|
||||
jsonError(c, common.CodeArgumentError, err.Error())
|
||||
return
|
||||
}
|
||||
if req == nil {
|
||||
req = map[string]interface{}{}
|
||||
}
|
||||
|
||||
result, code, err := h.chatService.Create(user.ID, req)
|
||||
if err != nil {
|
||||
jsonError(c, code, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": common.CodeSuccess,
|
||||
"data": result,
|
||||
"message": "success",
|
||||
})
|
||||
}
|
||||
|
||||
// ListChatsNext list chats with advanced filtering and pagination
|
||||
// @Summary List Chats Next
|
||||
// @Description Get list of chats with filtering, pagination and sorting (equivalent to list_dialogs_next)
|
||||
|
||||
@@ -17,11 +17,13 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"ragflow/internal/common"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -349,6 +351,98 @@ func (h *ChatSessionHandler) GetSession(c *gin.Context) {
|
||||
jsonResponse(c, common.CodeSuccess, result, "success")
|
||||
}
|
||||
|
||||
// CreateSession create a session in a dialog
|
||||
func (h *ChatSessionHandler) CreateSession(c *gin.Context) {
|
||||
user, errorCode, errorMessage := GetUser(c)
|
||||
if errorCode != common.CodeSuccess {
|
||||
jsonError(c, errorCode, errorMessage)
|
||||
return
|
||||
}
|
||||
|
||||
userID := strings.TrimSpace(user.ID)
|
||||
if userID == "" {
|
||||
jsonError(c, common.CodeBadRequest, "user_id is required")
|
||||
return
|
||||
}
|
||||
|
||||
chatID := strings.TrimSpace(c.Param("chat_id"))
|
||||
if chatID == "" {
|
||||
jsonError(c, common.CodeBadRequest, "chat_id is required")
|
||||
return
|
||||
}
|
||||
|
||||
req := map[string]interface{}{}
|
||||
if err := json.NewDecoder(c.Request.Body).Decode(&req); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
req = map[string]interface{}{}
|
||||
} else {
|
||||
jsonError(c, common.CodeArgumentError, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
if req == nil {
|
||||
req = map[string]interface{}{}
|
||||
}
|
||||
|
||||
result, code, err := h.chatSessionService.CreateSession(userID, chatID, req)
|
||||
if err != nil {
|
||||
if code == common.CodeAuthenticationError {
|
||||
jsonResponse(c, code, false, err.Error())
|
||||
return
|
||||
}
|
||||
jsonError(c, code, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse(c, common.CodeSuccess, result, "success")
|
||||
}
|
||||
|
||||
// DeleteSessions delete a session in a dialog
|
||||
func (h *ChatSessionHandler) DeleteSessions(c *gin.Context) {
|
||||
user, errorCode, errorMessage := GetUser(c)
|
||||
if errorCode != common.CodeSuccess {
|
||||
jsonError(c, errorCode, errorMessage)
|
||||
return
|
||||
}
|
||||
|
||||
chatID := strings.TrimSpace(c.Param("chat_id"))
|
||||
if chatID == "" {
|
||||
jsonError(c, common.CodeBadRequest, "chat_id is required")
|
||||
return
|
||||
}
|
||||
|
||||
userID := strings.TrimSpace(user.ID)
|
||||
if userID == "" {
|
||||
jsonError(c, common.CodeBadRequest, "user_id is required")
|
||||
return
|
||||
}
|
||||
|
||||
req := map[string]interface{}{}
|
||||
if err := json.NewDecoder(c.Request.Body).Decode(&req); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
req = map[string]interface{}{}
|
||||
} else {
|
||||
jsonError(c, common.CodeArgumentError, err.Error())
|
||||
return
|
||||
}
|
||||
}
|
||||
if req == nil {
|
||||
req = map[string]interface{}{}
|
||||
}
|
||||
|
||||
result, message, code, err := h.chatSessionService.DeleteSessions(userID, chatID, req)
|
||||
if err != nil {
|
||||
if code == common.CodeAuthenticationError {
|
||||
jsonResponse(c, code, false, err.Error())
|
||||
return
|
||||
}
|
||||
jsonError(c, code, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse(c, common.CodeSuccess, result, message)
|
||||
}
|
||||
|
||||
func (h *ChatSessionHandler) UpdateSession(c *gin.Context) {
|
||||
user, errorCode, errorMessage := GetUser(c)
|
||||
if errorCode != common.CodeSuccess {
|
||||
|
||||
Reference in New Issue
Block a user