feat: 添加智能体文件类型限制,内置数据分析师智能体

This commit is contained in:
wizardchen
2025-12-31 18:18:22 +08:00
committed by lyingbug
parent 884153aeb8
commit d72b6997cb
10 changed files with 290 additions and 56 deletions
@@ -3,6 +3,7 @@ package repository
import (
"context"
"errors"
"strings"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -302,6 +303,7 @@ func (r *knowledgeRepository) SearchKnowledge(
tenantID uint64,
keyword string,
offset, limit int,
fileTypes []string,
) ([]*types.Knowledge, bool, error) {
// Use raw query to properly map knowledge_base_name
type KnowledgeWithKBName struct {
@@ -323,6 +325,55 @@ func (r *knowledgeRepository) SearchKnowledge(
query = query.Where("knowledges.file_name LIKE ? ", "%"+keyword+"%")
}
// If fileTypes is provided, filter by file extension
if len(fileTypes) > 0 {
// Build file extension patterns (e.g., "%.csv", "%.xlsx")
seen := make(map[string]bool)
var uniquePatterns []string
for _, ft := range fileTypes {
ft = strings.ToLower(strings.TrimPrefix(ft, "."))
pattern := "%." + ft
if !seen[pattern] {
seen[pattern] = true
uniquePatterns = append(uniquePatterns, pattern)
}
// Handle common aliases
var aliases []string
switch ft {
case "xlsx":
aliases = []string{"%.xls"}
case "xls":
aliases = []string{"%.xlsx"}
case "docx":
aliases = []string{"%.doc"}
case "doc":
aliases = []string{"%.docx"}
case "jpg":
aliases = []string{"%.jpeg", "%.png"}
case "jpeg":
aliases = []string{"%.jpg", "%.png"}
case "png":
aliases = []string{"%.jpg", "%.jpeg"}
}
for _, alias := range aliases {
if !seen[alias] {
seen[alias] = true
uniquePatterns = append(uniquePatterns, alias)
}
}
}
// Build OR conditions for file extensions
if len(uniquePatterns) > 0 {
orConditions := make([]string, len(uniquePatterns))
args := make([]interface{}, len(uniquePatterns))
for i, p := range uniquePatterns {
orConditions[i] = "LOWER(knowledges.file_name) LIKE ?"
args[i] = p
}
query = query.Where("("+strings.Join(orConditions, " OR ")+")", args...)
}
}
// Fetch limit+1 to check if there are more results
err := query.Order("knowledges.created_at DESC").
Offset(offset).
+3 -2
View File
@@ -6041,10 +6041,11 @@ func (s *knowledgeService) getOrCreateTagInTarget(
}
// SearchKnowledge searches knowledge items by keyword across the tenant
func (s *knowledgeService) SearchKnowledge(ctx context.Context, keyword string, offset, limit int) ([]*types.Knowledge, bool, error) {
// fileTypes: optional list of file extensions to filter by (e.g., ["csv", "xlsx"])
func (s *knowledgeService) SearchKnowledge(ctx context.Context, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error) {
tenantID, ok := ctx.Value(types.TenantIDContextKey).(uint64)
if !ok {
return nil, false, werrors.NewUnauthorizedError("Tenant ID not found in context")
}
return s.repo.SearchKnowledge(ctx, tenantID, keyword, offset, limit)
return s.repo.SearchKnowledge(ctx, tenantID, keyword, offset, limit, fileTypes)
}