mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-23 08:56:42 +08:00
feat(go-api): migrate searchbot share detail endpoint to go (#16124)
## Summary - add public Go route for `/api/v1/searchbots/detail` - implement beta-token auth flow for shared search access - add tenant-based access check for shared search apps - add joined search detail query for the share response - align Go response shape with the current Python runtime behavior - add DAO / service / handler tests for the new endpoint
This commit is contained in:
@@ -45,7 +45,6 @@ type ChunkRetriever interface {
|
||||
RetrievalTest(req *service.RetrievalTestRequest, userID string) (*service.RetrievalTestResponse, error)
|
||||
}
|
||||
|
||||
|
||||
// streamingLLM abstracts streaming chat for the Ask endpoint.
|
||||
// The returned channel delivers raw text deltas from the LLM.
|
||||
// Implementations should respect ctx cancellation to prevent goroutine leaks.
|
||||
@@ -55,9 +54,9 @@ type streamingLLM interface {
|
||||
|
||||
// SearchBotAskRequest is the request body for POST /api/v1/searchbots/ask.
|
||||
type SearchBotAskRequest struct {
|
||||
Question string `json:"question" binding:"required"`
|
||||
KbIDs common.StringSlice `json:"kb_ids" binding:"required"`
|
||||
SearchID string `json:"search_id,omitempty"`
|
||||
Question string `json:"question" binding:"required"`
|
||||
KbIDs common.StringSlice `json:"kb_ids" binding:"required"`
|
||||
SearchID string `json:"search_id,omitempty"`
|
||||
}
|
||||
|
||||
// SearchBotRealLLM wraps ModelProviderService to implement searchbotLLM.
|
||||
@@ -111,21 +110,21 @@ func chatStreamWithContext(ctx context.Context, chatModel *modelModule.ChatModel
|
||||
|
||||
// SearchBotRetrievalTestRequest is the request body for POST /api/v1/searchbots/retrieval_test.
|
||||
type SearchBotRetrievalTestRequest struct {
|
||||
KbIDs common.StringSlice `json:"kb_ids" binding:"required"`
|
||||
Question string `json:"question" binding:"required"`
|
||||
Page *int `json:"page,omitempty"`
|
||||
Size *int `json:"size,omitempty"`
|
||||
DocIDs []string `json:"doc_ids,omitempty"`
|
||||
UseKG *bool `json:"use_kg,omitempty"`
|
||||
TopK *int `json:"top_k,omitempty"`
|
||||
CrossLanguages []string `json:"cross_languages,omitempty"`
|
||||
SearchID *string `json:"search_id,omitempty"`
|
||||
KbIDs common.StringSlice `json:"kb_ids" binding:"required"`
|
||||
Question string `json:"question" binding:"required"`
|
||||
Page *int `json:"page,omitempty"`
|
||||
Size *int `json:"size,omitempty"`
|
||||
DocIDs []string `json:"doc_ids,omitempty"`
|
||||
UseKG *bool `json:"use_kg,omitempty"`
|
||||
TopK *int `json:"top_k,omitempty"`
|
||||
CrossLanguages []string `json:"cross_languages,omitempty"`
|
||||
SearchID *string `json:"search_id,omitempty"`
|
||||
MetaDataFilter map[string]interface{} `json:"meta_data_filter,omitempty"`
|
||||
TenantRerankID *string `json:"tenant_rerank_id,omitempty"`
|
||||
RerankID *string `json:"rerank_id,omitempty"`
|
||||
Keyword *bool `json:"keyword,omitempty"`
|
||||
SimilarityThreshold *float64 `json:"similarity_threshold,omitempty"`
|
||||
VectorSimilarityWeight *float64 `json:"vector_similarity_weight,omitempty"`
|
||||
TenantRerankID *string `json:"tenant_rerank_id,omitempty"`
|
||||
RerankID *string `json:"rerank_id,omitempty"`
|
||||
Keyword *bool `json:"keyword,omitempty"`
|
||||
SimilarityThreshold *float64 `json:"similarity_threshold,omitempty"`
|
||||
VectorSimilarityWeight *float64 `json:"vector_similarity_weight,omitempty"`
|
||||
// TODO: wire highlight to nlp Retrieval when engine supports highlightFields
|
||||
// Python: bot_api.py → retrieval(highlight=req.get("highlight"))
|
||||
// → search.py highlightFields → ES get_highlight()
|
||||
@@ -140,9 +139,10 @@ type SearchBotRequest struct {
|
||||
}
|
||||
|
||||
// SearchBotHandler handles searchbot endpoints:
|
||||
// POST /api/v1/searchbots/related_questions
|
||||
// POST /api/v1/searchbots/retrieval_test
|
||||
// POST /api/v1/searchbots/ask
|
||||
//
|
||||
// POST /api/v1/searchbots/related_questions
|
||||
// POST /api/v1/searchbots/retrieval_test
|
||||
// POST /api/v1/searchbots/ask
|
||||
type SearchBotHandler struct {
|
||||
searchSvc *service.SearchService
|
||||
tenantSvc *service.TenantService
|
||||
@@ -404,6 +404,39 @@ func (h *SearchBotHandler) Ask(c *gin.Context) {
|
||||
|
||||
}
|
||||
|
||||
// SearchbotDetail returns the public share-page bootstrap payload for a
|
||||
// search app. The route is mounted under apiNoAuth but still requires a beta
|
||||
// token, matching Python's AUTH_BETA flow.
|
||||
func (h *SearchBotHandler) SearchbotDetail(c *gin.Context) {
|
||||
searchID := strings.TrimSpace(c.Query("search_id"))
|
||||
if searchID == "" {
|
||||
jsonError(c, common.CodeArgumentError, "search_id is required")
|
||||
return
|
||||
}
|
||||
|
||||
userSvc := service.NewUserService()
|
||||
user, code, err := userSvc.GetUserByBetaAPIToken(c.GetHeader("Authorization"))
|
||||
if err != nil {
|
||||
jsonError(c, code, "Authentication error: API key is invalid!")
|
||||
return
|
||||
}
|
||||
|
||||
detail, err := h.searchSvc.GetSearchShareDetail(user.ID, searchID)
|
||||
if err != nil {
|
||||
switch err.Error() {
|
||||
case "has no permission for this operation":
|
||||
jsonError(c, common.CodeOperatingError, "Has no permission for this operation.")
|
||||
case "can't find this Search App!":
|
||||
jsonError(c, common.CodeDataError, "Can't find this Search App!")
|
||||
default:
|
||||
jsonInternalError(c, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
jsonResponse(c, common.CodeSuccess, detail, "success")
|
||||
}
|
||||
|
||||
// ---- SSE helpers ----
|
||||
|
||||
type ssePayload struct {
|
||||
@@ -416,11 +449,11 @@ type ssePayload struct {
|
||||
// The Reference field is always present (non-nil) so the frontend can safely
|
||||
// access .chunks or .reduce without a null guard.
|
||||
type askSSEData struct {
|
||||
Answer string `json:"answer"`
|
||||
Reference interface{} `json:"reference"`
|
||||
Final bool `json:"final"`
|
||||
StartToThink bool `json:"start_to_think,omitempty"`
|
||||
EndToThink bool `json:"end_to_think,omitempty"`
|
||||
Answer string `json:"answer"`
|
||||
Reference interface{} `json:"reference"`
|
||||
Final bool `json:"final"`
|
||||
StartToThink bool `json:"start_to_think,omitempty"`
|
||||
EndToThink bool `json:"end_to_think,omitempty"`
|
||||
}
|
||||
|
||||
func sseAnswer(answer string, refs interface{}, final bool) string {
|
||||
@@ -515,7 +548,7 @@ func toRetrievalServiceRequest(h *SearchBotRetrievalTestRequest) *service.Retrie
|
||||
// ptrFloat64 returns a pointer to a float64 value.
|
||||
func ptrFloat64(v float64) *float64 { return &v }
|
||||
|
||||
func intPtr(v int) *int { return &v }
|
||||
func intPtr(v int) *int { return &v }
|
||||
func floatPtr(v float64) *float64 { return &v }
|
||||
|
||||
// applyRetrievalDefaults fills in default values for optional fields,
|
||||
|
||||
315
internal/handler/searchbot_detail_test.go
Normal file
315
internal/handler/searchbot_detail_test.go
Normal file
@@ -0,0 +1,315 @@
|
||||
//
|
||||
// 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 (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"ragflow/internal/common"
|
||||
"ragflow/internal/dao"
|
||||
"ragflow/internal/entity"
|
||||
"ragflow/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func setupSearchbotDetailHandlerDB(t *testing.T) {
|
||||
t.Helper()
|
||||
|
||||
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{
|
||||
TranslateError: true,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to open sqlite: %v", err)
|
||||
}
|
||||
|
||||
if err := db.AutoMigrate(&entity.User{}, &entity.UserTenant{}, &entity.Search{}, &entity.APIToken{}); err != nil {
|
||||
t.Fatalf("failed to migrate: %v", err)
|
||||
}
|
||||
|
||||
orig := dao.DB
|
||||
dao.DB = db
|
||||
t.Cleanup(func() {
|
||||
dao.DB = orig
|
||||
})
|
||||
}
|
||||
|
||||
func newSearchbotDetailRouter() *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
h := NewSearchBotHandler(service.NewSearchService(), nil, nil, nil)
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/searchbots/detail", h.SearchbotDetail)
|
||||
return r
|
||||
}
|
||||
|
||||
func TestSearchbotDetailSuccess(t *testing.T) {
|
||||
setupSearchbotDetailHandlerDB(t)
|
||||
|
||||
status := "1"
|
||||
accessToken := "access-token"
|
||||
tenantAvatar := "tenant-avatar"
|
||||
beta := "beta-token"
|
||||
searchAvatar := "search-avatar"
|
||||
|
||||
if err := dao.DB.Create(&entity.User{
|
||||
ID: "tenant-1",
|
||||
Email: "tenant@example.com",
|
||||
Nickname: "Tenant",
|
||||
Avatar: &tenantAvatar,
|
||||
AccessToken: &accessToken,
|
||||
IsAuthenticated: "1",
|
||||
IsActive: "1",
|
||||
IsAnonymous: "0",
|
||||
Status: &status,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
if err := dao.DB.Create(&entity.UserTenant{
|
||||
ID: "ut-1",
|
||||
UserID: "tenant-1",
|
||||
TenantID: "tenant-1",
|
||||
Role: "owner",
|
||||
InvitedBy: "tenant-1",
|
||||
Status: &status,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create user_tenant: %v", err)
|
||||
}
|
||||
|
||||
if err := dao.DB.Create(&entity.APIToken{
|
||||
TenantID: "tenant-1",
|
||||
Token: "token-1",
|
||||
Beta: &beta,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create api token: %v", err)
|
||||
}
|
||||
|
||||
if err := dao.DB.Create(&entity.Search{
|
||||
ID: "search-1",
|
||||
Avatar: &searchAvatar,
|
||||
TenantID: "tenant-1",
|
||||
Name: "Search App",
|
||||
CreatedBy: "tenant-1",
|
||||
SearchConfig: entity.JSONMap{"summary": true},
|
||||
Status: &status,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create search: %v", err)
|
||||
}
|
||||
|
||||
r := newSearchbotDetailRouter()
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/searchbots/detail?search_id=search-1", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+beta)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, body=%s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal: %v", err)
|
||||
}
|
||||
if resp["code"] != float64(common.CodeSuccess) {
|
||||
t.Fatalf("code = %v, want %v", resp["code"], common.CodeSuccess)
|
||||
}
|
||||
data := resp["data"].(map[string]interface{})
|
||||
if data["id"] != "search-1" {
|
||||
t.Fatalf("id = %v, want search-1", data["id"])
|
||||
}
|
||||
if _, ok := data["avatar"]; !ok {
|
||||
t.Fatal("expected avatar key in response")
|
||||
}
|
||||
if data["avatar"] != searchAvatar {
|
||||
t.Fatalf("avatar = %v, want %q", data["avatar"], searchAvatar)
|
||||
}
|
||||
if _, ok := data["nickname"]; ok {
|
||||
t.Fatalf("did not expect nickname in response, got %v", data["nickname"])
|
||||
}
|
||||
if _, ok := data["tenant_avatar"]; ok {
|
||||
t.Fatalf("did not expect tenant_avatar in response, got %v", data["tenant_avatar"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchbotDetailRejectsMissingSearchID(t *testing.T) {
|
||||
setupSearchbotDetailHandlerDB(t)
|
||||
|
||||
r := newSearchbotDetailRouter()
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/searchbots/detail", nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
var resp map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal: %v", err)
|
||||
}
|
||||
if resp["code"] != float64(common.CodeArgumentError) {
|
||||
t.Fatalf("code = %v, want %v", resp["code"], common.CodeArgumentError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchbotDetailRejectsInvalidBetaToken(t *testing.T) {
|
||||
setupSearchbotDetailHandlerDB(t)
|
||||
|
||||
r := newSearchbotDetailRouter()
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/searchbots/detail?search_id=search-1", nil)
|
||||
req.Header.Set("Authorization", "Bearer missing")
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
var resp map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal: %v", err)
|
||||
}
|
||||
if resp["code"] != float64(common.CodeUnauthorized) {
|
||||
t.Fatalf("code = %v, want %v", resp["code"], common.CodeUnauthorized)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchbotDetailRejectsUnauthorizedSearchAccess(t *testing.T) {
|
||||
setupSearchbotDetailHandlerDB(t)
|
||||
|
||||
status := "1"
|
||||
beta := "beta-token"
|
||||
accessToken := "access-token"
|
||||
|
||||
for _, u := range []entity.User{
|
||||
{
|
||||
ID: "tenant-1",
|
||||
Email: "tenant1@example.com",
|
||||
Nickname: "Tenant1",
|
||||
IsAuthenticated: "1",
|
||||
IsActive: "1",
|
||||
IsAnonymous: "0",
|
||||
Status: &status,
|
||||
},
|
||||
{
|
||||
ID: "user-2",
|
||||
Email: "user2@example.com",
|
||||
Nickname: "User2",
|
||||
AccessToken: &accessToken,
|
||||
IsAuthenticated: "1",
|
||||
IsActive: "1",
|
||||
IsAnonymous: "0",
|
||||
Status: &status,
|
||||
},
|
||||
} {
|
||||
user := u
|
||||
if err := dao.DB.Create(&user).Error; err != nil {
|
||||
t.Fatalf("failed to create user %s: %v", user.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := dao.DB.Create(&entity.UserTenant{
|
||||
ID: "ut-1",
|
||||
UserID: "user-2",
|
||||
TenantID: "user-2",
|
||||
Role: "owner",
|
||||
InvitedBy: "user-2",
|
||||
Status: &status,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create user_tenant: %v", err)
|
||||
}
|
||||
|
||||
if err := dao.DB.Create(&entity.APIToken{
|
||||
TenantID: "user-2",
|
||||
Token: "token-1",
|
||||
Beta: &beta,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create api token: %v", err)
|
||||
}
|
||||
|
||||
if err := dao.DB.Create(&entity.Search{
|
||||
ID: "search-1",
|
||||
TenantID: "tenant-1",
|
||||
Name: "Search App",
|
||||
CreatedBy: "tenant-1",
|
||||
SearchConfig: entity.JSONMap{"summary": true},
|
||||
Status: &status,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create search: %v", err)
|
||||
}
|
||||
|
||||
r := newSearchbotDetailRouter()
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/searchbots/detail?search_id=search-1", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+beta)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
var resp map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal: %v", err)
|
||||
}
|
||||
if resp["code"] != float64(common.CodeOperatingError) {
|
||||
t.Fatalf("code = %v, want %v", resp["code"], common.CodeOperatingError)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchbotDetailDoesNotExposeInternalErrorText(t *testing.T) {
|
||||
setupSearchbotDetailHandlerDB(t)
|
||||
|
||||
status := "1"
|
||||
accessToken := "access-token"
|
||||
beta := "beta-token"
|
||||
if err := dao.DB.Create(&entity.User{
|
||||
ID: "tenant-1",
|
||||
Email: "tenant@example.com",
|
||||
Nickname: "Tenant",
|
||||
AccessToken: &accessToken,
|
||||
IsAuthenticated: "1",
|
||||
IsActive: "1",
|
||||
IsAnonymous: "0",
|
||||
Status: &status,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
if err := dao.DB.Create(&entity.APIToken{
|
||||
TenantID: "tenant-1",
|
||||
Token: "token-1",
|
||||
Beta: &beta,
|
||||
}).Error; err != nil {
|
||||
t.Fatalf("failed to create api token: %v", err)
|
||||
}
|
||||
if err := dao.DB.Exec("DROP TABLE user_tenant").Error; err != nil {
|
||||
t.Fatalf("failed to drop user_tenant table: %v", err)
|
||||
}
|
||||
|
||||
r := newSearchbotDetailRouter()
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", "/api/v1/searchbots/detail?search_id=search-1", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+beta)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
body := w.Body.String()
|
||||
if strings.Contains(body, "no such table") {
|
||||
t.Fatalf("response leaked internal error text: %s", body)
|
||||
}
|
||||
|
||||
var resp map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
|
||||
t.Fatalf("failed to unmarshal: %v", err)
|
||||
}
|
||||
if resp["code"] != float64(common.CodeServerError) {
|
||||
t.Fatalf("code = %v, want %v", resp["code"], common.CodeServerError)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user