diff --git a/frontend/src/api/agent/index.ts b/frontend/src/api/agent/index.ts index 71254f59..04572426 100644 --- a/frontend/src/api/agent/index.ts +++ b/frontend/src/api/agent/index.ts @@ -26,6 +26,11 @@ export interface CustomAgentConfig { kb_selection_mode?: 'all' | 'selected' | 'none'; knowledge_bases?: string[]; + // ===== 文件类型限制 ===== + // 支持的文件类型(如 ["csv", "xlsx", "xls"]) + // 为空表示支持所有文件类型 + supported_file_types?: string[]; + // ===== 网络搜索设置 ===== web_search_enabled?: boolean; web_search_max_results?: number; @@ -85,7 +90,7 @@ export interface UpdateAgentRequest { config?: CustomAgentConfig; } -// 内置智能体 ID +// 内置智能体 ID(常用的保留常量,便于代码引用) export const BUILTIN_QUICK_ANSWER_ID = 'builtin-quick-answer'; export const BUILTIN_SMART_REASONING_ID = 'builtin-smart-reasoning'; @@ -98,12 +103,6 @@ export const BUILTIN_AGENT_NORMAL_ID = BUILTIN_QUICK_ANSWER_ID; // Deprecated: Use BUILTIN_SMART_REASONING_ID instead export const BUILTIN_AGENT_AGENT_ID = BUILTIN_SMART_REASONING_ID; -// 所有内置智能体 ID 列表(便于扩展) -export const BUILTIN_AGENT_IDS = [ - BUILTIN_QUICK_ANSWER_ID, - BUILTIN_SMART_REASONING_ID, -] as const; - // 获取智能体列表(包括内置智能体) export function listAgents() { return get<{ data: CustomAgent[] }>('/api/v1/agents'); @@ -134,9 +133,9 @@ export function copyAgent(id: string) { return post<{ data: CustomAgent }>(`/api/v1/agents/${id}/copy`); } -// 判断是否为内置智能体 +// 判断是否为内置智能体(通过 agent.is_builtin 字段或 ID 前缀判断) export function isBuiltinAgent(agentId: string): boolean { - return (BUILTIN_AGENT_IDS as readonly string[]).includes(agentId); + return agentId.startsWith('builtin-'); } // 占位符定义 diff --git a/frontend/src/api/knowledge-base/index.ts b/frontend/src/api/knowledge-base/index.ts index 7c5f1cb3..15aa4c28 100644 --- a/frontend/src/api/knowledge-base/index.ts +++ b/frontend/src/api/knowledge-base/index.ts @@ -232,6 +232,10 @@ export function getFAQImportProgress(taskId: string) { return get(`/api/v1/faq/import/progress/${taskId}`); } -export function searchKnowledge(keyword: string, offset = 0, limit = 20) { - return get(`/api/v1/knowledge/search?keyword=${encodeURIComponent(keyword)}&offset=${offset}&limit=${limit}`); +export function searchKnowledge(keyword: string, offset = 0, limit = 20, fileTypes?: string[]) { + let url = `/api/v1/knowledge/search?keyword=${encodeURIComponent(keyword)}&offset=${offset}&limit=${limit}`; + if (fileTypes && fileTypes.length > 0) { + url += `&file_types=${encodeURIComponent(fileTypes.join(','))}`; + } + return get(url); } diff --git a/frontend/src/components/AgentSelector.vue b/frontend/src/components/AgentSelector.vue index f063c311..9945dabe 100644 --- a/frontend/src/components/AgentSelector.vue +++ b/frontend/src/components/AgentSelector.vue @@ -33,9 +33,16 @@ :class="{ 'selected': currentAgentId === agent.id }" @click="selectAgent(agent)" > -
+ +
+
{{ agent.avatar }}
+
+ +
{{ agent.name }}
-
+ +
+
{{ agent.avatar }}
+
+ +
{{ agent.name }} {{ $t('agent.selector.current') }} @@ -170,40 +184,29 @@ const emit = defineEmits<{ const agents = ref([]); const dropdownStyle = ref>({}); -// 内置智能体 +// 内置智能体(从 API 获取,对特定 ID 使用本地化名称) const builtinAgents = computed(() => { - const defaults = [ - { - id: BUILTIN_QUICK_ANSWER_ID, - name: t('input.normalMode'), - description: t('input.normalModeDesc'), - is_builtin: true, - config: { agent_mode: 'quick-answer' as const } - }, - { - id: BUILTIN_SMART_REASONING_ID, - name: t('input.agentMode'), - description: t('input.agentModeDesc'), - is_builtin: true, - config: { agent_mode: 'smart-reasoning' as const } - } - ]; - - return defaults.map(def => { - const apiAgent = agents.value.find(a => a.id === def.id); - if (apiAgent) { + // 从 API 获取的内置智能体 + const apiBuiltins = agents.value.filter(a => a.is_builtin); + + // 对特定内置智能体使用本地化名称和描述 + return apiBuiltins.map(agent => { + if (agent.id === BUILTIN_QUICK_ANSWER_ID) { return { - ...apiAgent, - name: def.name, - description: def.description, - config: { - ...(apiAgent.config || {}), - agent_mode: def.config.agent_mode - } + ...agent, + name: t('input.normalMode'), + description: t('input.normalModeDesc'), + }; + } else if (agent.id === BUILTIN_SMART_REASONING_ID) { + return { + ...agent, + name: t('input.agentMode'), + description: t('input.agentModeDesc'), }; } - return def; - }) as CustomAgent[]; + // 其他内置智能体使用 API 返回的名称和描述 + return agent; + }); }); // 自定义智能体 @@ -466,6 +469,18 @@ onMounted(() => { } } +.builtin-avatar { + display: flex; + align-items: center; + justify-content: center; + width: 24px; + height: 24px; + border-radius: 6px; + flex-shrink: 0; + font-size: 16px; + background: var(--td-bg-color-secondarycontainer, #f5f5f5); +} + .check-icon { width: 14px; height: 14px; @@ -491,6 +506,12 @@ onMounted(() => { width: 28px; height: 28px; } + + .builtin-avatar { + width: 28px; + height: 28px; + font-size: 18px; + } } .agent-tooltip-title { diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index 583459aa..74b72726 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -129,6 +129,12 @@ const agentModelId = computed(() => { return currentAgentConfig.value?.model_id || null; }); +// 智能体支持的文件类型(空数组表示支持所有类型) +const agentSupportedFileTypes = computed(() => { + if (!hasAgentConfig.value) return []; + return currentAgentConfig.value?.supported_file_types || []; +}); + // 模型选择是否被智能体锁定 - 已移除锁定逻辑,允许用户自由切换模型 const isModelLockedByAgent = computed(() => { return false; @@ -355,9 +361,12 @@ const loadConversationConfig = async () => { const response = await getConversationConfig(); conversationConfig.value = response.data; const modelId = response.data?.summary_model_id || ''; + + // 保留当前已选择的模型(如果有),避免覆盖从其他页面传递的模型选择 + const currentSelectedModel = settingsStore.conversationModels.selectedChatModelId; settingsStore.updateConversationModels({ summaryModelId: modelId, - selectedChatModelId: modelId, + selectedChatModelId: currentSelectedModel || modelId, // 优先保留当前选择 rerankModelId: response.data?.rerank_model_id || '', }); if (!selectedModelId.value) { @@ -615,7 +624,9 @@ const loadMentionItems = async (q: string, resetIndex = true, append = false) => if (shouldLoadFiles) { mentionLoading.value = true; try { - const res: any = await searchKnowledge(q || '', mentionOffset.value, MENTION_PAGE_SIZE); + // 将文件类型过滤传递给后端 + const fileTypesParam = agentSupportedFileTypes.value.length > 0 ? agentSupportedFileTypes.value : undefined; + const res: any = await searchKnowledge(q || '', mentionOffset.value, MENTION_PAGE_SIZE, fileTypesParam); console.log('[Mention] searchKnowledge response:', res); if (res.data && Array.isArray(res.data)) { let files = res.data; diff --git a/frontend/src/views/agent/AgentEditorModal.vue b/frontend/src/views/agent/AgentEditorModal.vue index d697405a..d6f7e07a 100644 --- a/frontend/src/views/agent/AgentEditorModal.vue +++ b/frontend/src/views/agent/AgentEditorModal.vue @@ -617,6 +617,30 @@
+ +
+
+ +

限制可选择的文件类型,留空表示支持所有类型

+
+
+ + + +
+
+
@@ -978,6 +1002,9 @@ const defaultTemperature = ref(0.7); // 知识库相关工具列表 const knowledgeBaseTools = ['grep_chunks', 'knowledge_search', 'list_knowledge_chunks', 'query_knowledge_graph', 'get_document_info', 'database_query']; +// 初始化标志,防止初始化时触发 watch 自动添加工具 +const isInitializing = ref(false); + // 知识库选择模式:all=全部, selected=指定, none=不使用 const kbSelectionMode = ref<'all' | 'selected' | 'none'>('none'); @@ -1022,6 +1049,17 @@ const availableTools = computed(() => { })); }); +// 可用文件类型列表 +const availableFileTypes = [ + { value: 'pdf', label: 'PDF', description: 'PDF 文档' }, + { value: 'docx', label: 'Word', description: 'Word 文档 (.docx/.doc)' }, + { value: 'txt', label: '文本', description: '纯文本文件 (.txt)' }, + { value: 'md', label: 'Markdown', description: 'Markdown 文档' }, + { value: 'csv', label: 'CSV', description: '逗号分隔值文件' }, + { value: 'xlsx', label: 'Excel', description: 'Excel 表格 (.xlsx/.xls)' }, + { value: 'jpg', label: '图片', description: '图片文件 (.jpg/.jpeg/.png)' }, +]; + // 占位符相关 - 从 API 获取 const placeholderData = ref<{ system_prompt: PlaceholderDefinition[]; @@ -1145,6 +1183,8 @@ const defaultFormData = { // 知识库设置 kb_selection_mode: 'none' as 'all' | 'selected' | 'none', knowledge_bases: [] as string[], + // 文件类型限制 + supported_file_types: [] as string[], // FAQ 策略设置 faq_priority_enabled: true, // 是否启用 FAQ 优先策略 faq_direct_answer_threshold: 0.9, // FAQ 直接回答阈值(相似度高于此值直接使用 FAQ 答案) @@ -1227,6 +1267,7 @@ watch(() => props.visible, async (val) => { if (!agentData.config.knowledge_bases) agentData.config.knowledge_bases = []; if (!agentData.config.allowed_tools) agentData.config.allowed_tools = []; if (!agentData.config.mcp_services) agentData.config.mcp_services = []; + if (!agentData.config.supported_file_types) agentData.config.supported_file_types = []; // 兼容旧数据:如果没有 agent_mode 字段,根据 allowed_tools 推断 if (!agentData.config.agent_mode) { @@ -1234,10 +1275,16 @@ watch(() => props.visible, async (val) => { agentData.config.agent_mode = isAgent ? 'smart-reasoning' : 'quick-answer'; } + // 设置初始化标志,防止 watch 自动添加工具 + isInitializing.value = true; formData.value = agentData; // 初始化知识库选择模式 initKbSelectionMode(); initMcpSelectionMode(); + // 初始化完成后重置标志 + nextTick(() => { + isInitializing.value = false; + }); // 内置智能体:如果提示词为空,填入系统默认值 if (agentData.is_builtin) { fillBuiltinAgentDefaults(); @@ -1390,7 +1437,8 @@ watch(hasKnowledgeBase, (hasKB, oldHasKB) => { currentSection.value = 'basic'; } - if (!isAgentMode.value) return; // 只在Agent模式下处理工具 + // 初始化期间或非 Agent 模式下不自动调整工具 + if (isInitializing.value || !isAgentMode.value) return; if (hasKB && !oldHasKB) { // 从无知识库变为有知识库,自动添加知识库相关工具 diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.go index 4640c8fd..209dac65 100644 --- a/internal/application/repository/knowledge.go +++ b/internal/application/repository/knowledge.go @@ -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). diff --git a/internal/application/service/knowledge.go b/internal/application/service/knowledge.go index 3ab77270..e9337786 100644 --- a/internal/application/service/knowledge.go +++ b/internal/application/service/knowledge.go @@ -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) } diff --git a/internal/handler/knowledge.go b/internal/handler/knowledge.go index 2c31080e..c46f1606 100644 --- a/internal/handler/knowledge.go +++ b/internal/handler/knowledge.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "strconv" + "strings" "github.com/Tencent/WeKnora/internal/errors" "github.com/Tencent/WeKnora/internal/logger" @@ -791,11 +792,12 @@ func (h *KnowledgeHandler) UpdateImageInfo(c *gin.Context) { // @Tags Knowledge // @Accept json // @Produce json -// @Param keyword query string false "Keyword to search" -// @Param offset query int false "Offset for pagination" -// @Param limit query int false "Limit for pagination (default 20)" -// @Success 200 {object} map[string]interface{} "Search results" -// @Failure 400 {object} errors.AppError "Invalid request" +// @Param keyword query string false "Keyword to search" +// @Param offset query int false "Offset for pagination" +// @Param limit query int false "Limit for pagination (default 20)" +// @Param file_types query string false "Comma-separated file extensions to filter (e.g., csv,xlsx)" +// @Success 200 {object} map[string]interface{} "Search results" +// @Failure 400 {object} errors.AppError "Invalid request" // @Security Bearer // @Security ApiKeyAuth // @Router /knowledge/search [get] @@ -805,8 +807,19 @@ func (h *KnowledgeHandler) SearchKnowledge(c *gin.Context) { offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0")) limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20")) + // Parse file_types parameter (comma-separated) + var fileTypes []string + if fileTypesStr := c.Query("file_types"); fileTypesStr != "" { + for _, ft := range strings.Split(fileTypesStr, ",") { + ft = strings.TrimSpace(ft) + if ft != "" { + fileTypes = append(fileTypes, ft) + } + } + } + // Retrieve knowledge entries (empty keyword returns recent files) - knowledges, hasMore, err := h.kgService.SearchKnowledge(ctx, keyword, offset, limit) + knowledges, hasMore, err := h.kgService.SearchKnowledge(ctx, keyword, offset, limit, fileTypes) if err != nil { logger.ErrorWithFields(ctx, err, nil) c.Error(errors.NewInternalServerError("Failed to search knowledge").WithDetails(err.Error())) diff --git a/internal/types/custom_agent.go b/internal/types/custom_agent.go index 78af9819..a74b739f 100644 --- a/internal/types/custom_agent.go +++ b/internal/types/custom_agent.go @@ -98,6 +98,12 @@ type CustomAgentConfig struct { // Associated knowledge base IDs (only used when KBSelectionMode is "selected") KnowledgeBases []string `yaml:"knowledge_bases" json:"knowledge_bases"` + // ===== File Type Restriction Settings ===== + // Supported file types for this agent (e.g., ["csv", "xlsx", "xls"]) + // Empty means all file types are supported + // When set, only files with matching extensions can be used with this agent + SupportedFileTypes []string `yaml:"supported_file_types" json:"supported_file_types"` + // ===== FAQ Strategy Settings ===== // Whether FAQ priority strategy is enabled (FAQ answers prioritized over document chunks) FAQPriorityEnabled bool `yaml:"faq_priority_enabled" json:"faq_priority_enabled"` @@ -297,6 +303,83 @@ func GetBuiltinSmartReasoningAgent(tenantID uint64) *CustomAgent { } } +// GetBuiltinDataAnalystAgent returns the built-in data analyst agent +// This agent specializes in analyzing CSV/Excel data using SQL queries via DuckDB +func GetBuiltinDataAnalystAgent(tenantID uint64) *CustomAgent { + return &CustomAgent{ + ID: BuiltinDataAnalystID, + Name: "数据分析师", + Description: "专业数据分析智能体,支持 CSV/Excel 文件的 SQL 查询与统计分析", + Avatar: "📊", + IsBuiltin: true, + TenantID: tenantID, + Config: CustomAgentConfig{ + AgentMode: AgentModeSmartReasoning, + SystemPrompt: `### Role +You are WeKnora Data Analyst, an intelligent data analysis assistant powered by DuckDB. You specialize in analyzing structured data from CSV and Excel files using SQL queries. + +### Mission +Help users explore, analyze, and derive insights from their tabular data through intelligent SQL query generation and execution. + +### Critical Constraints +1. **Schema First:** ALWAYS call data_schema before writing any SQL query to understand the table structure. +2. **Read-Only:** Only SELECT queries allowed. INSERT, UPDATE, DELETE, CREATE, DROP are forbidden. +3. **Iterative Refinement:** If a query fails, analyze the error and refine your approach. + +### Workflow +1. **Understand:** Call data_schema to get table name, columns, types, and row count. +2. **Plan:** For complex questions, use todo_write to break into sub-queries. +3. **Query:** Call data_analysis with the knowledge_id and SQL query. +4. **Analyze:** Interpret results and provide insights. + +### SQL Best Practices for DuckDB +- Use double quotes for identifiers: SELECT "Column Name" FROM "table_name" +- Aggregate functions: COUNT(*), SUM(), AVG(), MIN(), MAX(), MEDIAN(), STDDEV() +- String matching: LIKE, ILIKE (case-insensitive), REGEXP +- Use LIMIT to prevent overwhelming output (default to 100 rows max) + +### Tool Guidelines +- **data_schema:** ALWAYS use first. Required before any query. +- **data_analysis:** Execute SQL queries. Only SELECT queries allowed. +- **thinking:** Plan complex analyses, debug query issues. +- **todo_write:** Track multi-step analysis tasks. + +### Output Standards +- Present results in well-formatted tables or summaries +- Provide actionable insights, not just raw numbers +- Relate findings back to the user's original question + +Current Time: {{current_time}} +`, + Temperature: 0.3, // Lower temperature for precise SQL generation + MaxCompletionTokens: 4096, + MaxIterations: 30, + KBSelectionMode: "all", + // Only support CSV and Excel files for data analysis + // Use standard values (xlsx), backend will auto-include xls via alias + SupportedFileTypes: []string{"csv", "xlsx"}, + // Core tools for data analysis + AllowedTools: []string{ + "thinking", + "todo_write", + "data_schema", // Get table schema information + "data_analysis", // Execute SQL queries on data + }, + WebSearchEnabled: false, // Data analysis doesn't need web search + WebSearchMaxResults: 0, + ReflectionEnabled: true, // Enable reflection for query optimization + MultiTurnEnabled: true, + HistoryTurns: 10, // More history for iterative analysis + // Retrieval strategy (minimal, as we focus on data tools) + EmbeddingTopK: 5, + KeywordThreshold: 0.3, + VectorThreshold: 0.5, + RerankTopK: 5, + RerankThreshold: 0.3, + }, + } +} + // Deprecated: Use GetBuiltinQuickAnswerAgent instead func GetBuiltinNormalAgent(tenantID uint64) *CustomAgent { return GetBuiltinQuickAnswerAgent(tenantID) @@ -311,6 +394,7 @@ func GetBuiltinAgentAgent(tenantID uint64) *CustomAgent { var BuiltinAgentRegistry = map[string]func(uint64) *CustomAgent{ BuiltinQuickAnswerID: GetBuiltinQuickAnswerAgent, BuiltinSmartReasoningID: GetBuiltinSmartReasoningAgent, + BuiltinDataAnalystID: GetBuiltinDataAnalystAgent, } // builtinAgentIDsOrdered defines the fixed display order of built-in agents diff --git a/internal/types/interfaces/knowledge.go b/internal/types/interfaces/knowledge.go index ac1bf388..b4f0d80d 100644 --- a/internal/types/interfaces/knowledge.go +++ b/internal/types/interfaces/knowledge.go @@ -126,7 +126,8 @@ type KnowledgeService interface { // GetFAQImportProgress retrieves the progress of an FAQ import task GetFAQImportProgress(ctx context.Context, taskID string) (*types.FAQImportProgress, error) // SearchKnowledge searches knowledge items by keyword across the tenant. - 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"]) + SearchKnowledge(ctx context.Context, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error) } // KnowledgeRepository defines the interface for knowledge repositories. @@ -165,5 +166,6 @@ type KnowledgeRepository interface { // CountKnowledgeByStatus counts the number of knowledge items with the specified parse status. CountKnowledgeByStatus(ctx context.Context, tenantID uint64, kbID string, parseStatuses []string) (int64, error) // SearchKnowledge searches knowledge items by keyword across the tenant. - SearchKnowledge(ctx context.Context, tenantID uint64, keyword string, offset, limit int) ([]*types.Knowledge, bool, error) + // fileTypes: optional list of file extensions to filter by (e.g., ["csv", "xlsx"]) + SearchKnowledge(ctx context.Context, tenantID uint64, keyword string, offset, limit int, fileTypes []string) ([]*types.Knowledge, bool, error) }