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
@@ -109,7 +109,7 @@ func (s *agentService) CreateAgentEngine(
// Register MCP tools from enabled services for this tenant
tenantID := uint64(0)
if tid, ok := ctx.Value(types.TenantIDContextKey).(uint64); ok {
if tid, ok := types.TenantIDFromContext(ctx); ok {
tenantID = tid
}
if tenantID > 0 && s.mcpServiceService != nil && s.mcpManager != nil {
@@ -550,7 +550,7 @@ func (s *agentService) getSelectedDocumentInfos(ctx context.Context, knowledgeID
// Get tenant ID from context
tenantID := uint64(0)
if tid, ok := ctx.Value(types.TenantIDContextKey).(uint64); ok {
if tid, ok := types.TenantIDFromContext(ctx); ok {
tenantID = tid
}
@@ -104,12 +104,12 @@ func (p *MemoryPlugin) handleStorage(
{Role: "user", Content: chatManage.Query},
{Role: "assistant", Content: chatManage.ChatResponse.Content},
}
// Capture UserID and SessionID for goroutine
userID := chatManage.UserID
sessionID := chatManage.SessionID
bgCtx := context.WithoutCancel(ctx)
go func() {
if err := p.memoryService.AddEpisode(ctx, userID, sessionID, messages); err != nil {
logger.Errorf(ctx, "failed to add episode: %v", err)
if err := p.memoryService.AddEpisode(bgCtx, userID, sessionID, messages); err != nil {
logger.Errorf(bgCtx, "failed to add episode: %v", err)
}
}()
return nil
@@ -118,11 +118,11 @@ func (p *MemoryPlugin) handleStorage(
// If streaming, subscribe to events
if chatManage.EventBus != nil {
var fullResponse string
// Capture UserID and SessionID for closure
userID := chatManage.UserID
sessionID := chatManage.SessionID
bgCtx := context.WithoutCancel(ctx)
chatManage.EventBus.On(types.EventType(event.EventAgentFinalAnswer), func(ctx context.Context, evt types.Event) error {
chatManage.EventBus.On(types.EventType(event.EventAgentFinalAnswer), func(_ context.Context, evt types.Event) error {
data, ok := evt.Data.(event.AgentFinalAnswerData)
if !ok {
return nil
@@ -134,8 +134,8 @@ func (p *MemoryPlugin) handleStorage(
{Role: "assistant", Content: fullResponse},
}
go func() {
if err := p.memoryService.AddEpisode(ctx, userID, sessionID, messages); err != nil {
logger.Errorf(ctx, "failed to add episode: %v", err)
if err := p.memoryService.AddEpisode(bgCtx, userID, sessionID, messages); err != nil {
logger.Errorf(bgCtx, "failed to add episode: %v", err)
}
}()
}
@@ -220,7 +220,7 @@ func (p *PluginMerge) populateFAQAnswers(
return results
}
tenantID, _ := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, _ := types.TenantIDFromContext(ctx)
if tenantID == 0 && chatManage != nil {
tenantID = chatManage.TenantID
}
@@ -351,7 +351,7 @@ func (p *PluginMerge) expandShortContextWithNeighbors(
return results
}
tenantID, _ := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, _ := types.TenantIDFromContext(ctx)
if tenantID == 0 && chatManage != nil {
tenantID = chatManage.TenantID
}
@@ -503,7 +503,7 @@ func (p *PluginSearch) searchWebIfEnabled(ctx context.Context, chatManage *types
if !chatManage.WebSearchEnabled || p.webSearchService == nil || p.tenantService == nil {
return nil
}
tenant := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
if tenant == nil || tenant.WebSearchConfig == nil || tenant.WebSearchConfig.Provider == "" {
pipelineWarn(ctx, "Search", "web_config_missing", map[string]interface{}{
"tenant_id": chatManage.TenantID,
@@ -138,7 +138,7 @@ func (p *PluginSearchEntity) OnEvent(ctx context.Context,
logger.Infof(ctx, "No new chunk found")
return next()
}
chunks, err := p.chunkRepo.ListChunksByID(ctx, ctx.Value(types.TenantIDContextKey).(uint64), chunkIDs)
chunks, err := p.chunkRepo.ListChunksByID(ctx, types.MustTenantIDFromContext(ctx), chunkIDs)
if err != nil {
logger.Errorf(ctx, "Failed to list chunks, session_id: %s, error: %v", chatManage.SessionID, err)
return next()
@@ -149,7 +149,7 @@ func (p *PluginSearchEntity) OnEvent(ctx context.Context,
}
knowledges, err := p.knowledgeRepo.GetKnowledgeBatch(
ctx,
ctx.Value(types.TenantIDContextKey).(uint64),
types.MustTenantIDFromContext(ctx),
knowledgeIDs,
)
if err != nil {
+9 -9
View File
@@ -86,7 +86,7 @@ func (s *chunkService) CreateChunks(ctx context.Context, chunks []*types.Chunk)
// - *types.Chunk: Retrieved chunk if found
// - error: Any error encountered during retrieval
func (s *chunkService) GetChunkByID(ctx context.Context, id string) (*types.Chunk, error) {
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Getting chunk by ID, ID: %s, tenant ID: %d", id, tenantID)
chunk, err := s.chunkRepository.GetChunkByID(ctx, tenantID, id)
if err != nil {
@@ -126,7 +126,7 @@ func (s *chunkService) ListChunksByKnowledgeID(ctx context.Context, knowledgeID
logger.Info(ctx, "Start listing chunks by knowledge ID")
logger.Infof(ctx, "Knowledge ID: %s", knowledgeID)
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Tenant ID: %d", tenantID)
chunks, err := s.chunkRepository.ListChunksByKnowledgeID(ctx, tenantID, knowledgeID)
@@ -155,7 +155,7 @@ func (s *chunkService) ListChunksByKnowledgeID(ctx context.Context, knowledgeID
func (s *chunkService) ListPagedChunksByKnowledgeID(ctx context.Context,
knowledgeID string, page *types.Pagination, chunkType []types.ChunkType,
) (*types.PageResult, error) {
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
chunks, total, err := s.chunkRepository.ListPagedChunksByKnowledgeID(
ctx,
tenantID,
@@ -236,7 +236,7 @@ func (s *chunkService) UpdateChunks(ctx context.Context, chunks []*types.Chunk)
// Returns:
// - error: Any error encountered during deletion
func (s *chunkService) DeleteChunk(ctx context.Context, id string) error {
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
err := s.chunkRepository.DeleteChunk(ctx, tenantID, id)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
@@ -263,7 +263,7 @@ func (s *chunkService) DeleteChunks(ctx context.Context, ids []string) error {
logger.Info(ctx, "Start deleting chunks in batch")
logger.Infof(ctx, "Deleting %d chunks", len(ids))
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Tenant ID: %d", tenantID)
err := s.chunkRepository.DeleteChunks(ctx, tenantID, ids)
@@ -291,7 +291,7 @@ func (s *chunkService) DeleteChunksByKnowledgeID(ctx context.Context, knowledgeI
logger.Info(ctx, "Start deleting all chunks by knowledge ID")
logger.Infof(ctx, "Knowledge ID: %s", knowledgeID)
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Tenant ID: %d", tenantID)
err := s.chunkRepository.DeleteChunksByKnowledgeID(ctx, tenantID, knowledgeID)
@@ -311,7 +311,7 @@ func (s *chunkService) DeleteByKnowledgeList(ctx context.Context, ids []string)
logger.Info(ctx, "Start deleting all chunks by knowledge IDs")
logger.Infof(ctx, "Knowledge IDs: %v", ids)
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Tenant ID: %d", tenantID)
err := s.chunkRepository.DeleteByKnowledgeList(ctx, tenantID, ids)
@@ -353,7 +353,7 @@ func (s *chunkService) ListChunkByParentID(
func (s *chunkService) DeleteGeneratedQuestion(ctx context.Context, chunkID string, questionID string) error {
logger.Infof(ctx, "Deleting generated question, chunk ID: %s, question ID: %s", chunkID, questionID)
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// 1. Get the chunk
chunk, err := s.chunkRepository.GetChunkByID(ctx, tenantID, chunkID)
@@ -404,7 +404,7 @@ func (s *chunkService) DeleteGeneratedQuestion(ctx context.Context, chunkID stri
// The source_id format is: {chunk_id}-{question_id}
sourceID := fmt.Sprintf("%s-%s", chunkID, questionID)
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenantInfo, _ := types.TenantInfoFromContext(ctx)
retrieveEngine, err := retriever.NewCompositeRetrieveEngine(s.retrieveEngine, tenantInfo.GetEffectiveEngines())
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
+6 -6
View File
@@ -46,7 +46,7 @@ func (s *customAgentService) CreateAgent(ctx context.Context, agent *types.Custo
}
// Get tenant ID from context
tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
return nil, ErrInvalidTenantID
}
@@ -90,7 +90,7 @@ func (s *customAgentService) GetAgentByID(ctx context.Context, id string) (*type
}
// Get tenant ID from context
tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
return nil, ErrInvalidTenantID
}
@@ -142,7 +142,7 @@ func (s *customAgentService) GetAgentByIDAndTenant(ctx context.Context, id strin
// ListAgents lists all agents for the current tenant (including built-in agents)
func (s *customAgentService) ListAgents(ctx context.Context) ([]*types.CustomAgent, error) {
tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
return nil, ErrInvalidTenantID
}
@@ -204,7 +204,7 @@ func (s *customAgentService) UpdateAgent(ctx context.Context, agent *types.Custo
}
// Get tenant ID from context
tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
return nil, ErrInvalidTenantID
}
@@ -330,7 +330,7 @@ func (s *customAgentService) DeleteAgent(ctx context.Context, id string) error {
}
// Get tenant ID from context
tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
return ErrInvalidTenantID
}
@@ -370,7 +370,7 @@ func (s *customAgentService) CopyAgent(ctx context.Context, id string) (*types.C
}
// Get tenant ID from context
tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
return nil, ErrInvalidTenantID
}
+2 -2
View File
@@ -109,7 +109,7 @@ func (e *EvaluationService) EvaluationResult(ctx context.Context, taskID string)
return nil, err
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(
ctx,
"Checking tenant ID match, task tenant ID: %d, current tenant ID: %d",
@@ -138,7 +138,7 @@ func (e *EvaluationService) Evaluation(ctx context.Context,
datasetID, knowledgeBaseID, chatModelID, rerankModelID)
// Get tenant ID from context for multi-tenancy support
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Tenant ID: %d", tenantID)
// Handle knowledge base creation if not provided
+34 -51
View File
@@ -7,6 +7,7 @@ import (
"io"
"mime/multipart"
"path/filepath"
"strings"
"time"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -89,6 +90,27 @@ func CheckMinioConnectivity(ctx context.Context, endpoint, accessKeyID, secretAc
return svc.CheckConnectivity(ctx)
}
// parseMinioFilePath extracts the object name from a "minio://bucket/key" path,
// validates the bucket matches this service instance, and checks for path traversal.
func (s *minioFileService) parseMinioFilePath(filePath string) (string, error) {
const prefix = "minio://"
if !strings.HasPrefix(filePath, prefix) {
return "", fmt.Errorf("invalid MinIO file path: %s", filePath)
}
rest := strings.TrimPrefix(filePath, prefix)
parts := strings.SplitN(rest, "/", 2)
if len(parts) != 2 || parts[0] == "" || parts[1] == "" {
return "", fmt.Errorf("invalid MinIO file path: %s", filePath)
}
if parts[0] != s.bucketName {
return "", fmt.Errorf("bucket mismatch in path: got %s, want %s", parts[0], s.bucketName)
}
if err := utils.SafeObjectKey(parts[1]); err != nil {
return "", fmt.Errorf("invalid file path: %w", err)
}
return parts[1], nil
}
// SaveFile saves a file to MinIO
func (s *minioFileService) SaveFile(ctx context.Context,
file *multipart.FileHeader, tenantID uint64, knowledgeID string,
@@ -118,55 +140,28 @@ func (s *minioFileService) SaveFile(ctx context.Context,
// GetFile gets a file from MinIO
func (s *minioFileService) GetFile(ctx context.Context, filePath string) (io.ReadCloser, error) {
// Parse MinIO path
// Format: minio://bucketName/objectName
if len(filePath) < 9 || filePath[:8] != "minio://" {
return nil, fmt.Errorf("invalid MinIO file path: %s", filePath)
objectName, err := s.parseMinioFilePath(filePath)
if err != nil {
return nil, err
}
// Extract object name
objectName := filePath[9+len(s.bucketName):]
if len(objectName) > 0 && objectName[0] == '/' {
objectName = objectName[1:]
}
if err := utils.SafeObjectKey(objectName); err != nil {
return nil, fmt.Errorf("invalid file path: %w", err)
}
// Get object
obj, err := s.client.GetObject(ctx, s.bucketName, objectName, minio.GetObjectOptions{})
if err != nil {
return nil, fmt.Errorf("failed to get file from MinIO: %w", err)
}
return obj, nil
}
// DeleteFile deletes a file
func (s *minioFileService) DeleteFile(ctx context.Context, filePath string) error {
// Parse MinIO path
// Format: minio://bucketName/objectName
if len(filePath) < 9 || filePath[:8] != "minio://" {
return fmt.Errorf("invalid MinIO file path: %s", filePath)
}
// Extract object name
objectName := filePath[9+len(s.bucketName):]
if len(objectName) > 0 && objectName[0] == '/' {
objectName = objectName[1:]
}
if err := utils.SafeObjectKey(objectName); err != nil {
return fmt.Errorf("invalid file path: %w", err)
}
// Delete object
err := s.client.RemoveObject(ctx, s.bucketName, objectName, minio.RemoveObjectOptions{
GovernanceBypass: true,
})
objectName, err := s.parseMinioFilePath(filePath)
if err != nil {
return err
}
if err := s.client.RemoveObject(ctx, s.bucketName, objectName, minio.RemoveObjectOptions{
GovernanceBypass: true,
}); err != nil {
return fmt.Errorf("failed to delete file: %w", err)
}
return nil
}
@@ -194,25 +189,13 @@ func (s *minioFileService) SaveBytes(ctx context.Context, data []byte, tenantID
// GetFileURL returns a presigned download URL for the file
func (s *minioFileService) GetFileURL(ctx context.Context, filePath string) (string, error) {
// Parse MinIO path
if len(filePath) < 9 || filePath[:8] != "minio://" {
return "", fmt.Errorf("invalid MinIO file path: %s", filePath)
objectName, err := s.parseMinioFilePath(filePath)
if err != nil {
return "", err
}
// Extract object name
objectName := filePath[9+len(s.bucketName):]
if len(objectName) > 0 && objectName[0] == '/' {
objectName = objectName[1:]
}
if err := utils.SafeObjectKey(objectName); err != nil {
return "", fmt.Errorf("invalid file path: %w", err)
}
// Generate presigned URL (valid for 24 hours)
presignedURL, err := s.client.PresignedGetObject(ctx, s.bucketName, objectName, 24*time.Hour, nil)
if err != nil {
return "", fmt.Errorf("failed to generate presigned URL: %w", err)
}
return presignedURL.String(), nil
}
@@ -4,8 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
@@ -16,6 +14,7 @@ import (
"github.com/Tencent/WeKnora/internal/models/vlm"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
secutils "github.com/Tencent/WeKnora/internal/utils"
"github.com/google/uuid"
"github.com/hibiken/asynq"
)
@@ -355,17 +354,5 @@ func (s *ImageMultimodalService) resolveVLM(ctx context.Context, kbID string) (v
// downloadImageFromURL downloads image bytes from an HTTP(S) URL.
func downloadImageFromURL(imageURL string) ([]byte, error) {
if !strings.HasPrefix(imageURL, "http://") && !strings.HasPrefix(imageURL, "https://") {
return nil, fmt.Errorf("unsupported URL scheme: %s", imageURL)
}
client := &http.Client{Timeout: 60 * time.Second}
resp, err := client.Get(imageURL)
if err != nil {
return nil, fmt.Errorf("HTTP GET: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP %d for %s", resp.StatusCode, imageURL)
}
return io.ReadAll(resp.Body)
return secutils.DownloadBytes(imageURL)
}
+10 -10
View File
@@ -82,7 +82,7 @@ func (s *knowledgeBaseService) CreateKnowledgeBase(ctx context.Context,
kb.ID = uuid.New().String()
}
kb.CreatedAt = time.Now()
kb.TenantID = ctx.Value(types.TenantIDContextKey).(uint64)
kb.TenantID = types.MustTenantIDFromContext(ctx)
kb.UpdatedAt = time.Now()
kb.EnsureDefaults()
@@ -158,7 +158,7 @@ func (s *knowledgeBaseService) GetKnowledgeBasesByIDsOnly(ctx context.Context, i
// ListKnowledgeBases returns all knowledge bases for a tenant
func (s *knowledgeBaseService) ListKnowledgeBases(ctx context.Context) ([]*types.KnowledgeBase, error) {
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
kbs, err := s.repo.ListKnowledgeBasesByTenantID(ctx, tenantID)
if err != nil {
@@ -324,8 +324,8 @@ func (s *knowledgeBaseService) DeleteKnowledgeBase(ctx context.Context, id strin
logger.Infof(ctx, "Deleting knowledge base, ID: %s", id)
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenantID := types.MustTenantIDFromContext(ctx)
tenantInfo, _ := types.TenantInfoFromContext(ctx)
// Step 1: Delete the knowledge base record first (mark as deleted)
logger.Infof(ctx, "Deleting knowledge base from database")
@@ -543,7 +543,7 @@ func (s *knowledgeBaseService) SetEmbeddingModel(ctx context.Context, id string,
func (s *knowledgeBaseService) CopyKnowledgeBase(ctx context.Context,
srcKB string, dstKB string,
) (*types.KnowledgeBase, *types.KnowledgeBase, error) {
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// Load source KB with tenant scope to prevent cross-tenant cloning
sourceKB, err := s.repo.GetKnowledgeBaseByIDAndTenant(ctx, srcKB, tenantID)
if err != nil {
@@ -593,8 +593,8 @@ func (s *knowledgeBaseService) HybridSearch(ctx context.Context,
) ([]*types.SearchResult, error) {
logger.Infof(ctx, "Hybrid search parameters, knowledge base ID: %s, query text: %s", id, params.QueryText)
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
currentTenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantInfo, _ := types.TenantInfoFromContext(ctx)
currentTenantID := types.MustTenantIDFromContext(ctx)
// Create a composite retrieval engine with tenant's configured retrievers
retrieveEngine, err := retriever.NewCompositeRetrieveEngine(s.retrieveEngine, tenantInfo.GetEffectiveEngines())
@@ -880,7 +880,7 @@ func (s *knowledgeBaseService) iterativeRetrieveWithDeduplication(ctx context.Co
filteredOutChunks := make(map[string]struct{})
queryTextLower := strings.ToLower(strings.TrimSpace(queryText))
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
for i := 0; i < maxIterations; i++ {
// Update TopK in retrieve params
@@ -1017,7 +1017,7 @@ func (s *knowledgeBaseService) filterByNegativeQuestions(ctx context.Context,
return chunks
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// Collect chunk IDs
chunkIDs := make([]string, 0, len(chunks))
@@ -1104,7 +1104,7 @@ func (s *knowledgeBaseService) processSearchResults(ctx context.Context,
return nil, nil
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// Prepare data structures for efficient processing
var knowledgeIDs []string
+5 -5
View File
@@ -60,7 +60,7 @@ func (s *messageService) CreateMessage(ctx context.Context, message *types.Messa
logger.Infof(ctx, "Creating message for session ID: %s", message.SessionID)
// Check if the session exists to validate the message belongs to a valid session
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Checking if session exists, tenant ID: %d, session ID: %s", tenantID, message.SessionID)
_, err := s.sessionRepo.Get(ctx, tenantID, message.SessionID)
if err != nil {
@@ -94,7 +94,7 @@ func (s *messageService) GetMessage(ctx context.Context, sessionID string, messa
logger.Infof(ctx, "Getting message, session ID: %s, message ID: %s", sessionID, messageID)
// Verify the session exists before attempting to retrieve the message
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Checking if session exists, tenant ID: %d", tenantID)
_, err := s.sessionRepo.Get(ctx, tenantID, sessionID)
if err != nil {
@@ -132,7 +132,7 @@ func (s *messageService) GetMessagesBySession(ctx context.Context,
logger.Infof(ctx, "Getting messages for session ID: %s, page: %d, pageSize: %d", sessionID, page, pageSize)
// Verify the session exists before retrieving messages
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Checking if session exists, tenant ID: %d", tenantID)
_, err := s.sessionRepo.Get(ctx, tenantID, sessionID)
if err != nil {
@@ -253,7 +253,7 @@ func (s *messageService) UpdateMessage(ctx context.Context, message *types.Messa
logger.Infof(ctx, "Updating message, ID: %s, session ID: %s", message.ID, message.SessionID)
// Verify the session exists before updating the message
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Checking if session exists, tenant ID: %d", tenantID)
_, err := s.sessionRepo.Get(ctx, tenantID, message.SessionID)
if err != nil {
@@ -288,7 +288,7 @@ func (s *messageService) DeleteMessage(ctx context.Context, sessionID string, me
logger.Infof(ctx, "Deleting message, session ID: %s, message ID: %s", sessionID, messageID)
// Verify the session exists before deleting the message
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Checking if session exists, tenant ID: %d", tenantID)
_, err := s.sessionRepo.Get(ctx, tenantID, sessionID)
if err != nil {
+6 -6
View File
@@ -104,7 +104,7 @@ func (s *modelService) GetModelByID(ctx context.Context, id string) (*types.Mode
return nil, errors.New("model ID cannot be empty")
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// Fetch model from repository
model, err := s.repo.GetByID(ctx, tenantID, id)
@@ -148,7 +148,7 @@ func (s *modelService) GetModelByID(ctx context.Context, id string) (*types.Mode
func (s *modelService) ListModels(ctx context.Context) ([]*types.Model, error) {
logger.Info(ctx, "Start listing models")
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Listing models for tenant ID: %d", tenantID)
// List models from repository with no additional filters
@@ -170,7 +170,7 @@ func (s *modelService) UpdateModel(ctx context.Context, model *types.Model) erro
logger.Infof(ctx, "Updating model ID: %s, name: %s", model.ID, model.Name)
// Check if the model is builtin - builtin models cannot be updated
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
existingModel, err := s.repo.GetByID(ctx, tenantID, model.ID)
if err != nil {
logger.ErrorWithFields(ctx, err, map[string]interface{}{
@@ -202,7 +202,7 @@ func (s *modelService) DeleteModel(ctx context.Context, id string) error {
logger.Info(ctx, "Start deleting model")
logger.Infof(ctx, "Deleting model ID: %s", id)
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Tenant ID: %d", tenantID)
// Check if the model is builtin - builtin models cannot be deleted
@@ -368,7 +368,7 @@ func (s *modelService) GetChatModel(ctx context.Context, modelId string) (chat.C
return nil, errors.New("model ID cannot be empty")
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// Get the model directly from repository to avoid status checks
model, err := s.repo.GetByID(ctx, tenantID, modelId)
@@ -412,7 +412,7 @@ func (s *modelService) GetVLMModel(ctx context.Context, modelId string) (vlm.VLM
return nil, errors.New("model ID cannot be empty")
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
model, err := s.repo.GetByID(ctx, tenantID, modelId)
if err != nil {
+13 -13
View File
@@ -114,7 +114,7 @@ func (s *sessionService) GetSession(ctx context.Context, id string) (*types.Sess
}
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Retrieving session, ID: %s, tenant ID: %d", id, tenantID)
// Get session from repository
@@ -134,7 +134,7 @@ func (s *sessionService) GetSession(ctx context.Context, id string) (*types.Sess
// GetSessionsByTenant retrieves all sessions for the current tenant
func (s *sessionService) GetSessionsByTenant(ctx context.Context) ([]*types.Session, error) {
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
logger.Infof(ctx, "Retrieving all sessions for tenant, tenant ID: %d", tenantID)
// Get sessions from repository
@@ -157,7 +157,7 @@ func (s *sessionService) GetPagedSessionsByTenant(ctx context.Context,
pagination *types.Pagination,
) (*types.PageResult, error) {
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// Get paged sessions from repository
sessions, total, err := s.sessionRepo.GetPagedByTenantID(ctx, tenantID, pagination)
if err != nil {
@@ -203,7 +203,7 @@ func (s *sessionService) DeleteSession(ctx context.Context, id string) error {
}
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// Cleanup temporary KB stored in Redis for this session
if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, id); err != nil {
@@ -236,7 +236,7 @@ func (s *sessionService) BatchDeleteSessions(ctx context.Context, ids []string)
}
// Get tenant ID from context
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
// Cleanup associated resources for each session
for _, id := range ids {
@@ -643,7 +643,7 @@ func (s *sessionService) KnowledgeQA(
)
// Get UserID from context
userID, _ := ctx.Value(types.UserIDContextKey).(string)
userID, _ := types.UserIDFromContext(ctx)
chatManage := &types.ChatManage{
Query: query,
@@ -790,7 +790,7 @@ func (s *sessionService) selectChatModelID(
) (string, error) {
// If no knowledge base IDs but have knowledge IDs, derive KB IDs from knowledge IDs (include shared KB files)
if len(knowledgeBaseIDs) == 0 && len(knowledgeIDs) > 0 {
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
knowledgeList, err := s.knowledgeService.GetKnowledgeBatchWithSharedAccess(ctx, tenantID, knowledgeIDs)
if err != nil {
logger.Warnf(ctx, "Failed to get knowledge batch for model selection: %v", err)
@@ -899,7 +899,7 @@ func (s *sessionService) resolveKnowledgeBasesFromAgent(
// unrelated KBs from other organisations into the agent's retrieval scope.
isSharedAgent := sessionTenantID != 0 && sessionTenantID != customAgent.TenantID
if !isSharedAgent {
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
userIDVal := ctx.Value(types.UserIDContextKey)
if userIDVal != nil {
if userID, ok := userIDVal.(string); ok && userID != "" && s.kbShareService != nil {
@@ -1021,7 +1021,7 @@ func (s *sessionService) buildSearchTargets(
kbByID[kb.ID] = kb
}
}
userID, _ := ctx.Value(types.UserIDContextKey).(string)
userID, _ := types.UserIDFromContext(ctx)
for _, kbID := range knowledgeBaseIDs {
fullKBSet[kbID] = true
kb := kbByID[kbID]
@@ -1114,7 +1114,7 @@ func (s *sessionService) KnowledgeQAByEvent(ctx context.Context,
// Set up tracing attributes
logger.Infof(ctx, "Trigger event list: %v", methods)
span.SetAttributes(
attribute.String("request_id", ctx.Value(types.RequestIDContextKey).(string)),
attribute.String("request_id", func() string { id, _ := types.RequestIDFromContext(ctx); return id }()),
attribute.String("query", chatManage.Query),
attribute.String("method", strings.Join(methods, ",")),
)
@@ -1163,7 +1163,7 @@ func (s *sessionService) SearchKnowledge(ctx context.Context,
knowledgeBaseIDs, knowledgeIDs, query)
// Get tenant ID from context
tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID, ok := types.TenantIDFromContext(ctx)
if !ok {
logger.Error(ctx, "Failed to get tenant ID from context")
return nil, fmt.Errorf("tenant ID not found in context")
@@ -1181,7 +1181,7 @@ func (s *sessionService) SearchKnowledge(ctx context.Context,
}
// Create default retrieval parameters
userID, _ := ctx.Value(types.UserIDContextKey).(string)
userID, _ := types.UserIDFromContext(ctx)
chatManage := &types.ChatManage{
Query: query,
RewriteQuery: query,
@@ -1528,7 +1528,7 @@ func (s *sessionService) getContextManagerForSession(
chatModel chat.Chat,
) interfaces.ContextManager {
// Get tenant to access global context configuration
tenant, _ := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenant, _ := types.TenantInfoFromContext(ctx)
// Determine which context config to use: tenant-level or default
var contextConfig *types.ContextConfig
if tenant != nil && tenant.ContextConfig != nil {
+4 -4
View File
@@ -73,7 +73,7 @@ func (s *knowledgeTagService) ListTags(
}
// Check access permission
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
if kb.TenantID != tenantID {
// Get user ID from context
userIDVal := ctx.Value(types.UserIDContextKey)
@@ -192,7 +192,7 @@ func (s *knowledgeTagService) UpdateTag(
if id == "" {
return nil, werrors.NewBadRequestError("标签ID不能为空")
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
tag, err := s.repo.GetByID(ctx, tenantID, id)
if err != nil {
return nil, err
@@ -225,7 +225,7 @@ func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bo
if id == "" {
return werrors.NewBadRequestError("标签ID不能为空")
}
tenantID := ctx.Value(types.TenantIDContextKey).(uint64)
tenantID := types.MustTenantIDFromContext(ctx)
tag, err := s.repo.GetByID(ctx, tenantID, id)
if err != nil {
return err
@@ -243,7 +243,7 @@ func (s *knowledgeTagService) DeleteTag(ctx context.Context, id string, force bo
}
// Get tenant info for effective engines
tenantInfo := ctx.Value(types.TenantInfoContextKey).(*types.Tenant)
tenantInfo, _ := types.TenantInfoFromContext(ctx)
// Helper function to delete chunks and enqueue index deletion task
deleteChunksAndEnqueueIndexDelete := func() error {