2026-03-04 19:17:16 +08:00
|
|
|
//
|
|
|
|
|
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
"encoding/json"
|
2026-06-24 17:34:01 +08:00
|
|
|
"errors"
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
"fmt"
|
2026-03-04 19:17:16 +08:00
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2026-03-11 11:23:13 +08:00
|
|
|
"ragflow/internal/common"
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
"strconv"
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
"strings"
|
2026-03-04 19:17:16 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
|
|
"ragflow/internal/service"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ChatSessionHandler chat session (conversation) handler
|
|
|
|
|
type ChatSessionHandler struct {
|
|
|
|
|
chatSessionService *service.ChatSessionService
|
|
|
|
|
userService *service.UserService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewChatSessionHandler create chat session handler
|
|
|
|
|
func NewChatSessionHandler(chatSessionService *service.ChatSessionService, userService *service.UserService) *ChatSessionHandler {
|
|
|
|
|
return &ChatSessionHandler{
|
|
|
|
|
chatSessionService: chatSessionService,
|
|
|
|
|
userService: userService,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ListChatSessions list chat sessions for a dialog
|
|
|
|
|
// @Summary List Chat Sessions
|
|
|
|
|
// @Description Get list of chat sessions for a specific dialog
|
|
|
|
|
// @Tags chat_session
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
2026-05-07 17:14:22 +08:00
|
|
|
// @Param chat_id query string true "chat ID"
|
2026-03-04 19:17:16 +08:00
|
|
|
// @Success 200 {object} service.ListChatSessionsResponse
|
2026-05-07 17:14:22 +08:00
|
|
|
// @Router /api/v1/chats/<chat_id>/sessions [get]
|
2026-03-04 19:17:16 +08:00
|
|
|
func (h *ChatSessionHandler) ListChatSessions(c *gin.Context) {
|
2026-03-11 11:23:13 +08:00
|
|
|
user, errorCode, errorMessage := GetUser(c)
|
|
|
|
|
if errorCode != common.CodeSuccess {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, errorCode, errorMessage)
|
2026-03-04 19:17:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
userID := user.ID
|
|
|
|
|
|
2026-05-07 17:14:22 +08:00
|
|
|
// Get chat_id from query parameter
|
|
|
|
|
chatID := c.Param("chat_id")
|
|
|
|
|
if chatID == "" {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithHttpCodeData(c, http.StatusBadRequest, 400, nil, "chat_id is required")
|
2026-03-04 19:17:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
// Mirror Python's list_sessions query handling: invalid/negative page
|
|
|
|
|
// values fall back to the default; page_size 0 yields an empty list and a
|
|
|
|
|
// negative page_size disables pagination.
|
|
|
|
|
page := 1
|
|
|
|
|
if pageStr := c.Query("page"); pageStr != "" {
|
|
|
|
|
if p, err := strconv.Atoi(pageStr); err == nil && p > 0 {
|
|
|
|
|
page = p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pageSize := 30
|
|
|
|
|
if pageSizeStr := c.Query("page_size"); pageSizeStr != "" {
|
|
|
|
|
if ps, err := strconv.Atoi(pageSizeStr); err == nil {
|
|
|
|
|
pageSize = ps
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
orderby := "create_time"
|
|
|
|
|
if queryOrderby := c.Query("orderby"); queryOrderby != "" {
|
|
|
|
|
switch queryOrderby {
|
|
|
|
|
case "create_time", "update_time", "name":
|
|
|
|
|
orderby = queryOrderby
|
|
|
|
|
default:
|
|
|
|
|
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, fmt.Sprintf("invalid orderby field: %s", queryOrderby))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
desc := true
|
|
|
|
|
if descStr := c.Query("desc"); descStr != "" {
|
|
|
|
|
desc = !strings.EqualFold(descStr, "false")
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 19:17:16 +08:00
|
|
|
// Call service to list chat sessions
|
2026-07-23 12:15:58 +08:00
|
|
|
ctx := c.Request.Context()
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
result, err := h.chatSessionService.ListChatSessions(ctx, userID, chatID, c.Query("id"), c.Query("name"), orderby, desc, page, pageSize)
|
2026-03-04 19:17:16 +08:00
|
|
|
if err != nil {
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
// Mirror Python: ownership failures return code 109 "No authorization."
|
|
|
|
|
if strings.Contains(err.Error(), "No authorization") {
|
|
|
|
|
common.ResponseWithCodeData(c, common.CodeAuthenticationError, false, "No authorization.")
|
2026-03-04 19:17:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, 500, nil, err.Error())
|
2026-03-04 19:17:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 18:14:05 +08:00
|
|
|
common.SuccessWithData(c, result.Sessions, "success")
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-01 15:52:52 +08:00
|
|
|
type ChatCompletionsRequest struct {
|
|
|
|
|
ChatID string `json:"chat_id,omitempty"`
|
|
|
|
|
SessionID string `json:"session_id,omitempty"`
|
|
|
|
|
ConversationID string `json:"conversation_id,omitempty"`
|
|
|
|
|
Messages []map[string]interface{} `json:"messages,omitempty"`
|
|
|
|
|
Question string `json:"question,omitempty"`
|
|
|
|
|
Files []interface{} `json:"files,omitempty"`
|
|
|
|
|
LLMID string `json:"llm_id,omitempty"`
|
|
|
|
|
PassAllHistoryMessages *bool `json:"pass_all_history_messages,omitempty"`
|
|
|
|
|
PassAllHistory *bool `json:"pass_all_history,omitempty"`
|
2026-07-31 19:12:24 +08:00
|
|
|
StoreHistoryMessages *bool `json:"store_history_messages,omitempty"`
|
|
|
|
|
StoreHistory *bool `json:"store_history,omitempty"`
|
2026-07-01 15:52:52 +08:00
|
|
|
Legacy bool `json:"legacy,omitempty"`
|
|
|
|
|
Stream *bool `json:"stream"`
|
|
|
|
|
Temperature *float64 `json:"temperature,omitempty"`
|
|
|
|
|
TopP *float64 `json:"top_p,omitempty"`
|
|
|
|
|
FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"`
|
|
|
|
|
PresencePenalty *float64 `json:"presence_penalty,omitempty"`
|
|
|
|
|
MaxTokens *int `json:"max_tokens,omitempty"`
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-01 15:52:52 +08:00
|
|
|
// ChatCompletions chat completion
|
2026-03-04 19:17:16 +08:00
|
|
|
// @Summary Chat Completion
|
2026-07-01 15:52:52 +08:00
|
|
|
// @Description Send messages to the chat model and get a response.
|
|
|
|
|
// @Description Default is streaming (text/event-stream); set stream:false for JSON.
|
2026-03-04 19:17:16 +08:00
|
|
|
// @Tags chat_session
|
|
|
|
|
// @Accept json
|
2026-07-01 15:52:52 +08:00
|
|
|
// @Produce json, text/event-stream
|
|
|
|
|
// @Param request body ChatCompletionsRequest true "chat completion request"
|
|
|
|
|
// @Success 200 {object} map[string]interface{} "Non-streaming JSON response"
|
|
|
|
|
// @Success 200 {string} text/event-stream "Streaming SSE response"
|
|
|
|
|
// @Router /api/v1/chat/completions [post]
|
|
|
|
|
func (h *ChatSessionHandler) ChatCompletions(c *gin.Context) {
|
2026-03-11 11:23:13 +08:00
|
|
|
user, errorCode, errorMessage := GetUser(c)
|
|
|
|
|
if errorCode != common.CodeSuccess {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, errorCode, errorMessage)
|
2026-03-04 19:17:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
userID := user.ID
|
|
|
|
|
|
2026-07-01 15:52:52 +08:00
|
|
|
var rawBody map[string]interface{}
|
|
|
|
|
if err := c.ShouldBindJSON(&rawBody); err != nil {
|
2026-07-10 17:02:25 +08:00
|
|
|
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
|
2026-03-04 19:17:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 15:52:52 +08:00
|
|
|
var req ChatCompletionsRequest
|
|
|
|
|
b, err := json.Marshal(rawBody)
|
|
|
|
|
if err != nil {
|
2026-07-10 17:02:25 +08:00
|
|
|
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
|
2026-07-01 15:52:52 +08:00
|
|
|
return
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
if err = json.Unmarshal(b, &req); err != nil {
|
2026-07-10 17:02:25 +08:00
|
|
|
common.ErrorWithCode(c, common.CodeBadRequest, err.Error())
|
2026-07-01 15:52:52 +08:00
|
|
|
return
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
2026-07-01 15:52:52 +08:00
|
|
|
|
|
|
|
|
// Normalize session_id / conversation_id
|
|
|
|
|
sessionID := req.SessionID
|
|
|
|
|
if sessionID == "" {
|
|
|
|
|
sessionID = req.ConversationID
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
2026-07-01 15:52:52 +08:00
|
|
|
|
|
|
|
|
// Build generation config
|
|
|
|
|
genConfig := make(map[string]interface{})
|
|
|
|
|
if req.Temperature != nil {
|
|
|
|
|
genConfig["temperature"] = *req.Temperature
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
2026-07-01 15:52:52 +08:00
|
|
|
if req.TopP != nil {
|
|
|
|
|
genConfig["top_p"] = *req.TopP
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
2026-07-01 15:52:52 +08:00
|
|
|
if req.FrequencyPenalty != nil {
|
|
|
|
|
genConfig["frequency_penalty"] = *req.FrequencyPenalty
|
2026-04-29 14:44:12 +08:00
|
|
|
}
|
2026-07-01 15:52:52 +08:00
|
|
|
if req.PresencePenalty != nil {
|
|
|
|
|
genConfig["presence_penalty"] = *req.PresencePenalty
|
|
|
|
|
}
|
|
|
|
|
if req.MaxTokens != nil {
|
|
|
|
|
genConfig["max_tokens"] = *req.MaxTokens
|
2026-04-29 14:44:12 +08:00
|
|
|
}
|
2026-03-04 19:17:16 +08:00
|
|
|
|
2026-07-01 15:52:52 +08:00
|
|
|
// Resolve pass_all_history from either alias
|
|
|
|
|
passAllHistory := false
|
|
|
|
|
if req.PassAllHistory != nil {
|
|
|
|
|
passAllHistory = *req.PassAllHistory
|
|
|
|
|
}
|
|
|
|
|
if req.PassAllHistoryMessages != nil {
|
|
|
|
|
passAllHistory = *req.PassAllHistoryMessages
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
|
2026-07-31 19:12:24 +08:00
|
|
|
// Resolve store_history from either alias (default true, mirrors Python)
|
|
|
|
|
storeHistory := true
|
|
|
|
|
if req.StoreHistory != nil {
|
|
|
|
|
storeHistory = *req.StoreHistory
|
|
|
|
|
}
|
|
|
|
|
if req.StoreHistoryMessages != nil {
|
|
|
|
|
storeHistory = *req.StoreHistoryMessages
|
|
|
|
|
}
|
|
|
|
|
if !storeHistory && !passAllHistory {
|
|
|
|
|
common.ErrorWithCode(c, common.CodeDataError, "`pass_all_history_messages` must be true when `store_history_messages` is false.")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 15:52:52 +08:00
|
|
|
// Remove known keys from rawBody; what remains is passthrough kwargs
|
|
|
|
|
knownKeys := []string{
|
|
|
|
|
"chat_id", "session_id", "conversation_id",
|
|
|
|
|
"messages", "question", "files",
|
|
|
|
|
"llm_id",
|
|
|
|
|
"pass_all_history_messages", "pass_all_history",
|
2026-07-31 19:12:24 +08:00
|
|
|
"store_history_messages", "store_history",
|
2026-07-01 15:52:52 +08:00
|
|
|
"legacy", "stream",
|
|
|
|
|
"temperature", "top_p", "frequency_penalty", "presence_penalty", "max_tokens",
|
|
|
|
|
}
|
|
|
|
|
for _, key := range knownKeys {
|
|
|
|
|
delete(rawBody, key)
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
2026-07-01 15:52:52 +08:00
|
|
|
kwargs := rawBody
|
2026-03-04 19:17:16 +08:00
|
|
|
|
2026-07-01 15:52:52 +08:00
|
|
|
// Determine stream mode
|
|
|
|
|
streamMode := true
|
|
|
|
|
if req.Stream != nil {
|
|
|
|
|
streamMode = *req.Stream
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if streamMode {
|
2026-06-12 09:49:34 -03:00
|
|
|
disableWriteDeadlineForSSE(c)
|
2026-03-04 19:17:16 +08:00
|
|
|
c.Header("Content-Type", "text/event-stream")
|
|
|
|
|
c.Header("Cache-Control", "no-cache")
|
|
|
|
|
c.Header("Connection", "keep-alive")
|
|
|
|
|
c.Header("X-Accel-Buffering", "no")
|
|
|
|
|
|
2026-07-01 15:52:52 +08:00
|
|
|
streamChan := make(chan string, 32)
|
2026-06-09 20:07:45 -07:00
|
|
|
reqCtx := c.Request.Context()
|
2026-03-04 19:17:16 +08:00
|
|
|
go func() {
|
|
|
|
|
defer close(streamChan)
|
2026-07-01 15:52:52 +08:00
|
|
|
_, _ = h.chatSessionService.ChatCompletions(
|
|
|
|
|
reqCtx, userID,
|
|
|
|
|
req.ChatID, sessionID,
|
|
|
|
|
req.Messages, req.Question, req.Files,
|
|
|
|
|
req.LLMID, genConfig, kwargs,
|
2026-07-31 19:12:24 +08:00
|
|
|
passAllHistory, storeHistory, req.Legacy,
|
2026-07-01 15:52:52 +08:00
|
|
|
true, streamChan,
|
|
|
|
|
)
|
2026-03-04 19:17:16 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
c.Stream(func(w io.Writer) bool {
|
|
|
|
|
data, ok := <-streamChan
|
|
|
|
|
if !ok {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
c.Writer.Write([]byte(data))
|
|
|
|
|
return true
|
|
|
|
|
})
|
|
|
|
|
} else {
|
2026-07-06 18:14:05 +08:00
|
|
|
var result map[string]interface{}
|
|
|
|
|
result, err = h.chatSessionService.ChatCompletions(
|
2026-07-01 15:52:52 +08:00
|
|
|
c.Request.Context(), userID,
|
|
|
|
|
req.ChatID, sessionID,
|
|
|
|
|
req.Messages, req.Question, req.Files,
|
|
|
|
|
req.LLMID, genConfig, kwargs,
|
2026-07-31 19:12:24 +08:00
|
|
|
passAllHistory, storeHistory, req.Legacy,
|
2026-07-01 15:52:52 +08:00
|
|
|
false, nil,
|
|
|
|
|
)
|
2026-03-04 19:17:16 +08:00
|
|
|
if err != nil {
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
var codedErr *common.CodedError
|
|
|
|
|
if errors.As(err, &codedErr) {
|
|
|
|
|
common.ErrorWithCode(c, codedErr.Code, codedErr.Message)
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, 500, nil, err.Error())
|
2026-03-04 19:17:16 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
common.SuccessWithData(c, result, "")
|
2026-03-04 19:17:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-06-24 17:34:01 +08:00
|
|
|
|
|
|
|
|
func (h *ChatSessionHandler) GetSession(c *gin.Context) {
|
|
|
|
|
user, errorCode, errorMessage := GetUser(c)
|
|
|
|
|
if errorCode != common.CodeSuccess {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, errorCode, errorMessage)
|
2026-06-24 17:34:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID := user.ID
|
|
|
|
|
chatID, sessionID := c.Param("chat_id"), c.Param("session_id")
|
|
|
|
|
|
2026-07-23 12:15:58 +08:00
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
result, code, err := h.chatSessionService.GetSession(ctx, userID, chatID, sessionID)
|
2026-06-24 17:34:01 +08:00
|
|
|
if err != nil {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, code, err.Error())
|
2026-06-24 17:34:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
common.SuccessWithData(c, result, "success")
|
2026-06-24 17:34:01 +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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
// CreateSession create a session in a dialog
|
|
|
|
|
func (h *ChatSessionHandler) CreateSession(c *gin.Context) {
|
|
|
|
|
user, errorCode, errorMessage := GetUser(c)
|
|
|
|
|
if errorCode != common.CodeSuccess {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, errorCode, errorMessage)
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID := strings.TrimSpace(user.ID)
|
|
|
|
|
if userID == "" {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "user_id is required")
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chatID := strings.TrimSpace(c.Param("chat_id"))
|
|
|
|
|
if chatID == "" {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "chat_id is required")
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
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 {
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
// Mirror Python's malformed-JSON contract message.
|
|
|
|
|
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Malformed JSON syntax: Missing commas/brackets or invalid encoding")
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if req == nil {
|
|
|
|
|
req = map[string]interface{}{}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 12:15:58 +08:00
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
result, code, err := h.chatSessionService.CreateSession(ctx, userID, chatID, req)
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
if err != nil {
|
|
|
|
|
if code == common.CodeAuthenticationError {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, code, false, err.Error())
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, code, err.Error())
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 18:14:05 +08:00
|
|
|
common.SuccessWithData(c, result, "success")
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteSessions delete a session in a dialog
|
|
|
|
|
func (h *ChatSessionHandler) DeleteSessions(c *gin.Context) {
|
|
|
|
|
user, errorCode, errorMessage := GetUser(c)
|
|
|
|
|
if errorCode != common.CodeSuccess {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, errorCode, errorMessage)
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chatID := strings.TrimSpace(c.Param("chat_id"))
|
|
|
|
|
if chatID == "" {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "chat_id is required")
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID := strings.TrimSpace(user.ID)
|
|
|
|
|
if userID == "" {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, common.CodeBadRequest, nil, "user_id is required")
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
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 {
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
// Mirror Python's malformed-JSON contract message.
|
|
|
|
|
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Malformed JSON syntax: Missing commas/brackets or invalid encoding")
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if req == nil {
|
|
|
|
|
req = map[string]interface{}{}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 12:15:58 +08:00
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
result, message, code, err := h.chatSessionService.DeleteSessions(ctx, userID, chatID, req)
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
if err != nil {
|
|
|
|
|
if code == common.CodeAuthenticationError {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, code, false, err.Error())
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, code, err.Error())
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 18:14:05 +08:00
|
|
|
common.SuccessWithData(c, result, message)
|
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:
https://github.com/infiniflow/ragflow/blob/f80d4c784368853ec727231b6891860629973519/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
2026-06-26 19:23:45 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-24 17:34:01 +08:00
|
|
|
func (h *ChatSessionHandler) UpdateSession(c *gin.Context) {
|
|
|
|
|
user, errorCode, errorMessage := GetUser(c)
|
|
|
|
|
if errorCode != common.CodeSuccess {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, errorCode, errorMessage)
|
2026-06-24 17:34:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID := user.ID
|
|
|
|
|
chatID, sessionID := c.Param("chat_id"), c.Param("session_id")
|
|
|
|
|
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
// An empty payload is a valid no-op (mirrors Python's update_session).
|
2026-06-24 17:34:01 +08:00
|
|
|
req := map[string]any{}
|
Test: release Go-proxy RESTful contract tests verified passing in Go mode (#17468)
### Summary
Aligns Go and Python error codes/messages so both backends honor the
same RESTful API contract, removing implementation-specific error leaks
(MySQL errors, `ValueError`, `AttributeError`, Gin validator format) in
favor of clean business error codes.
**Chat list** — invalid `orderby` now returns code 101 (was: raw Python
`AttributeError` code 100); invalid `page`/`page_size` values fall back
to defaults (was: raw `ValueError`/`ProgrammingError` code 100).
**Dataset create/update/delete** — adds UUID validation (101),
extra-field rejection (101), duplicate-id detection (101), content-type
/ JSON-syntax / object-shape checks (101), and "lacks permission" for
nonexistent datasets (IDOR). Create auto-deduplicates dataset names.
Pagerank updates tolerate a missing ES index. List response includes
`parser_config` and `pagerank`.
**Session list/update** — adds filtering, sorting, and pagination
support. Empty payloads are valid no-ops. Authorization errors map to
code 109.
**Chunk list** — doc object uses Python key names (`chunk_count`,
`dataset_id`, `chunk_method`, run text status). Add validates list
element types.
**Document update** — adds `chunk_method` alias, pydantic-style Field
error messages, metadata index auto-create with refresh, and "These
documents do not belong to dataset" messages. List validates
`metadata_condition` and reports ownership errors for unmatched name/id
filters.
**Search completion** — `kb_ids` ownership failure returns code 102
instead of 109.
Released 31 contract tests from `GO_ONLY_SKIPS` (all verified passing on
both Go and Python backends with real LLM keys).
2026-07-30 19:58:49 +08:00
|
|
|
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Invalid request: "+err.Error())
|
2026-06-24 17:34:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-23 12:15:58 +08:00
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
result, code, err := h.chatSessionService.UpdateSession(ctx, userID, chatID, sessionID, req)
|
2026-06-24 17:34:01 +08:00
|
|
|
if err != nil {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, code, err.Error())
|
2026-06-24 17:34:01 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
common.SuccessWithData(c, result, "success")
|
2026-06-24 17:34:01 +08:00
|
|
|
}
|
2026-07-02 10:33:27 +08:00
|
|
|
|
|
|
|
|
func (h *ChatSessionHandler) DeleteSessionMessage(c *gin.Context) {
|
|
|
|
|
user, errorCode, errorMessage := GetUser(c)
|
|
|
|
|
if errorCode != common.CodeSuccess {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, errorCode, errorMessage)
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
userID := user.ID
|
|
|
|
|
chatID, sessionID, msgID := c.Param("chat_id"), c.Param("session_id"), c.Param("msg_id")
|
|
|
|
|
|
2026-07-23 12:15:58 +08:00
|
|
|
ctx := c.Request.Context()
|
|
|
|
|
result, code, err := h.chatSessionService.DeleteSessionMessage(ctx, userID, chatID, sessionID, msgID)
|
2026-07-02 10:33:27 +08:00
|
|
|
if err != nil {
|
|
|
|
|
if code == common.CodeAuthenticationError {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, code, false, err.Error())
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, code, err.Error())
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
common.SuccessWithData(c, result, "success")
|
2026-07-02 10:33:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ChatSessionHandler) UpdateMessageFeedback(c *gin.Context) {
|
|
|
|
|
user, errorCode, errorMessage := GetUser(c)
|
|
|
|
|
if errorCode != common.CodeSuccess {
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, errorCode, errorMessage)
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userID := user.ID
|
|
|
|
|
chatID, sessionID, msgID := c.Param("chat_id"), c.Param("session_id"), c.Param("msg_id")
|
|
|
|
|
|
|
|
|
|
req := map[string]interface{}{}
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
if errors.Is(err, io.EOF) {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, common.CodeArgumentError, nil,
|
|
|
|
|
"Request body cannot be empty")
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Invalid request: "+err.Error())
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(req) == 0 {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Request body cannot be empty")
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result, code, err := h.chatSessionService.UpdateMessageFeedback(c.Request.Context(), userID, chatID, sessionID, msgID, req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if code == common.CodeAuthenticationError {
|
2026-07-06 18:14:05 +08:00
|
|
|
common.ResponseWithCodeData(c, code, false, err.Error())
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-10 14:26:54 +08:00
|
|
|
common.ErrorWithCode(c, code, err.Error())
|
2026-07-02 10:33:27 +08:00
|
|
|
return
|
|
|
|
|
}
|
2026-07-06 18:14:05 +08:00
|
|
|
common.SuccessWithData(c, result, "success")
|
2026-07-02 10:33:27 +08:00
|
|
|
}
|