feat: enhance document loading and state management in doc-content component

- Introduced a loading state (`loadingChunks`) to manage document chunk loading more effectively.
- Updated the scroll handling logic to prevent multiple requests while loading chunks.
- Adjusted the pagination logic to ensure correct page increments based on the total document count.

This update improves the user experience by ensuring smoother document loading and better state management during scrolling interactions.
This commit is contained in:
wizardchen
2026-02-28 18:11:37 +08:00
committed by lyingbug
parent 393ebf9f92
commit 4a9977b11f
28 changed files with 351 additions and 414 deletions
+1 -1
View File
@@ -1912,7 +1912,7 @@ func (h *InitializationHandler) testMultimodalWithDocReader(
return nil, fmt.Errorf("DocReader service not configured")
}
requestID, _ := ctx.Value(types.RequestIDContextKey).(string)
requestID, _ := types.RequestIDFromContext(ctx)
readResult, err := h.documentReader.Read(ctx, &types.ReadRequest{
FileContent: imageContent,
+2 -66
View File
@@ -44,74 +44,10 @@ func NewKnowledgeHandler(
}
// validateKnowledgeBaseAccess validates access permissions to a knowledge base
// Returns the knowledge base, the knowledge base ID, effective tenant ID, permission level, and any errors encountered
// For owned KBs, effectiveTenantID is the caller's tenant ID
// For shared KBs, effectiveTenantID is the source tenant ID (owner's tenant)
// using the ":id" URL path parameter. It delegates to validateKnowledgeBaseAccessWithKBID.
func (h *KnowledgeHandler) validateKnowledgeBaseAccess(c *gin.Context) (*types.KnowledgeBase, string, uint64, types.OrgMemberRole, error) {
ctx := c.Request.Context()
// Get tenant ID from context
tenantID := c.GetUint64(types.TenantIDContextKey.String())
if tenantID == 0 {
logger.Error(ctx, "Failed to get tenant ID")
return nil, "", 0, "", errors.NewUnauthorizedError("Unauthorized")
}
// Get user ID from context (needed for shared KB permission check)
userID, userExists := c.Get(types.UserIDContextKey.String())
// Get knowledge base ID from URL path parameter
kbID := secutils.SanitizeForLog(c.Param("id"))
if kbID == "" {
logger.Error(ctx, "Knowledge base ID is empty")
return nil, "", 0, "", errors.NewBadRequestError("Knowledge base ID cannot be empty")
}
// Get knowledge base details
kb, err := h.kbService.GetKnowledgeBaseByID(ctx, kbID)
if err != nil {
logger.ErrorWithFields(ctx, err, nil)
return nil, kbID, 0, "", errors.NewInternalServerError(err.Error())
}
// Check 1: Verify tenant ownership (owner has full access)
if kb.TenantID == tenantID {
return kb, kbID, tenantID, types.OrgRoleAdmin, nil
}
// Check 2: If not owner, check organization shared access
if userExists && h.kbShareService != nil {
// Check if user has shared access through organization
permission, isShared, permErr := h.kbShareService.CheckUserKBPermission(ctx, kbID, userID.(string))
if permErr == nil && isShared {
// User has shared access, get the source tenant ID for queries
sourceTenantID, srcErr := h.kbShareService.GetKBSourceTenant(ctx, kbID)
if srcErr == nil {
logger.Infof(ctx, "User %s accessing shared KB %s with permission %s, source tenant: %d",
userID.(string), kbID, permission, sourceTenantID)
return kb, kbID, sourceTenantID, permission, nil
}
}
}
// Check 3: If not owner and no direct share, allow if user has any shared agent that can access this KB (e.g. opened from "通过智能体可见" list)
if userExists && h.agentShareService != nil {
can, err := h.agentShareService.UserCanAccessKBViaSomeSharedAgent(ctx, userID.(string), tenantID, kb)
if err == nil && can {
logger.Infof(ctx, "User %s accessing KB %s via some shared agent", userID.(string), kbID)
return kb, kbID, kb.TenantID, types.OrgRoleViewer, nil
}
}
// No permission: not owner and no shared access
logger.Warnf(
ctx,
"Permission denied to access this knowledge base, tenant ID mismatch, "+
"requested tenant ID: %d, knowledge base tenant ID: %d",
tenantID,
kb.TenantID,
)
return nil, kbID, 0, "", errors.NewForbiddenError("Permission denied to access this knowledge base")
return h.validateKnowledgeBaseAccessWithKBID(c, kbID)
}
// validateKnowledgeBaseAccessWithKBID validates access to the given knowledge base ID (e.g. from query or body).
+1 -1
View File
@@ -1225,7 +1225,7 @@ func (h *OrganizationHandler) ListSharedKnowledgeBases(c *gin.Context) {
ctx := c.Request.Context()
userID := c.GetString(types.UserIDContextKey.String())
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
sharedKBs, err := h.shareService.ListSharedKnowledgeBases(ctx, userID, tenantID)
if err != nil {
+2 -2
View File
@@ -215,7 +215,7 @@ func getFloat64(m map[string]interface{}, key string) float64 {
// It prioritizes tenant-level ConversationConfig, then falls back to config.yaml defaults
func (h *Handler) createDefaultSummaryConfig(ctx context.Context) *types.SummaryConfig {
// Try to get tenant from context
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
// Initialize with config.yaml defaults
cfg := &types.SummaryConfig{
@@ -259,7 +259,7 @@ func (h *Handler) createDefaultSummaryConfig(ctx context.Context) *types.Summary
// It prioritizes tenant-level ConversationConfig, then falls back to config.yaml defaults
func (h *Handler) fillSummaryConfigDefaults(ctx context.Context, config *types.SummaryConfig) {
// Try to get tenant from context
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
// Determine default values: tenant config first, then config.yaml
var defaultPrompt, defaultContextTemplate, defaultNoMatchPrefix string
+1 -1
View File
@@ -94,7 +94,7 @@ func (h *TagHandler) resolveTagIDWithCtx(c *gin.Context, ctx context.Context) (s
tagIDParam := secutils.SanitizeForLog(c.Param("tag_id"))
if seqID, err := strconv.ParseInt(tagIDParam, 10, 64); err == nil {
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
tag, err := h.tagRepo.GetBySeqID(ctx, tenantID, seqID)
if err != nil {
return "", errors.NewNotFoundError("标签不存在")
+10 -10
View File
@@ -439,7 +439,7 @@ type AgentConfigRequest struct {
// @Router /tenants/kv/agent-config [get]
func (h *TenantHandler) GetTenantAgentConfig(c *gin.Context) {
ctx := c.Request.Context()
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -529,7 +529,7 @@ func (h *TenantHandler) updateTenantAgentConfigInternal(c *gin.Context) {
}
// Get existing tenant
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -670,7 +670,7 @@ func (h *TenantHandler) updateTenantWebSearchConfigInternal(c *gin.Context) {
return
}
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -711,7 +711,7 @@ func (h *TenantHandler) GetTenantWebSearchConfig(c *gin.Context) {
ctx := c.Request.Context()
logger.Info(ctx, "Start getting tenant web search config")
// Get tenant
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -728,7 +728,7 @@ func (h *TenantHandler) GetTenantWebSearchConfig(c *gin.Context) {
// GetTenantParserEngineConfig returns the tenant's parser engine config (MinerU endpoint, API key, etc.).
func (h *TenantHandler) GetTenantParserEngineConfig(c *gin.Context) {
ctx := c.Request.Context()
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -753,7 +753,7 @@ func (h *TenantHandler) updateTenantParserEngineConfigInternal(c *gin.Context) {
c.Error(errors.NewValidationError("Invalid request data").WithDetails(err.Error()))
return
}
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -780,7 +780,7 @@ func (h *TenantHandler) updateTenantParserEngineConfigInternal(c *gin.Context) {
// GetTenantStorageEngineConfig returns the tenant's storage engine config (Local, MinIO, COS parameters).
func (h *TenantHandler) GetTenantStorageEngineConfig(c *gin.Context) {
ctx := c.Request.Context()
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -805,7 +805,7 @@ func (h *TenantHandler) updateTenantStorageEngineConfigInternal(c *gin.Context)
c.Error(errors.NewValidationError("Invalid request data").WithDetails(err.Error()))
return
}
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -897,7 +897,7 @@ func validateConversationConfig(req *types.ConversationConfig) error {
// @Router /tenants/kv/conversation-config [get]
func (h *TenantHandler) GetTenantConversationConfig(c *gin.Context) {
ctx := c.Request.Context()
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))
@@ -934,7 +934,7 @@ func (h *TenantHandler) updateTenantConversationInternal(c *gin.Context) {
}
// Get existing tenant
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil {
logger.Error(ctx, "Tenant is empty")
c.Error(errors.NewBadRequestError("Tenant is empty"))