@@ -1535,6 +1535,48 @@ Note: This only deletes the file index, not your development memory contexts.`,
15351535 }
15361536 }
15371537
1538+ // === 多项目场景验证 (v2.5.0) ===
1539+ if ( inferredProjectPath ) {
1540+ try {
1541+ // 获取所有项目进行对比
1542+ const allProjects = this . db . getAllProjects ( ) ;
1543+ if ( allProjects . length > 1 ) {
1544+ const currentProject = allProjects . find (
1545+ ( p : any ) => p . path === inferredProjectPath
1546+ ) ;
1547+ if ( currentProject ) {
1548+ // 检查是否有多个最近活跃的项目
1549+ const recentProjects = allProjects
1550+ . filter ( ( p : any ) => {
1551+ const lastAccess = new Date ( p . last_accessed || 0 ) ;
1552+ const daysSince = ( Date . now ( ) - lastAccess . getTime ( ) ) / ( 1000 * 60 * 60 * 24 ) ;
1553+ return daysSince < 7 ; // 最近7天访问过的项目
1554+ } )
1555+ . sort ( ( a : any , b : any ) => {
1556+ const aTime = new Date ( a . last_accessed || 0 ) . getTime ( ) ;
1557+ const bTime = new Date ( b . last_accessed || 0 ) . getTime ( ) ;
1558+ return bTime - aTime ; // 按访问时间倒序
1559+ } ) ;
1560+
1561+ if ( recentProjects . length > 1 && currentProject . id !== recentProjects [ 0 ] . id ) {
1562+ console . warn (
1563+ `[DevMind] ⚠️ 多项目检测: 检测到 ${ recentProjects . length } 个最近活跃项目,当前操作将记录到: ${ currentProject . name } `
1564+ ) ;
1565+ console . warn (
1566+ `[DevMind] 💡 建议: 在多项目开发时,请在 record_context 中明确指定 project_path 参数以避免混淆`
1567+ ) ;
1568+ autoSessionMeta . multi_project_warning = true ;
1569+ autoSessionMeta . current_project = currentProject . name ;
1570+ autoSessionMeta . recent_projects = recentProjects . map ( ( p : any ) => p . name ) ;
1571+ }
1572+ }
1573+ }
1574+ } catch ( error ) {
1575+ // 静默失败,不影响正常流程
1576+ console . warn ( "[DevMind] Multi-project validation failed:" , error ) ;
1577+ }
1578+ }
1579+
15381580 if ( ! sessionId && inferredProjectPath ) {
15391581 // 尝试获取活跃会话
15401582 const currentSessionId = await this . sessionManager . getCurrentSession (
@@ -2932,24 +2974,51 @@ Note: This only deletes the file index, not your development memory contexts.`,
29322974 console . error ( "[AI Enhancement] Query enhancement failed:" , error ) ;
29332975 }
29342976
2935- // 获取用于搜索的contexts
2977+ // 获取用于搜索的contexts(开发记忆)
29362978 const allContexts = this . db . getContextsForVectorSearch (
29372979 projectId ,
29382980 args . session_id
29392981 ) ;
29402982
2941- if ( allContexts . length === 0 ) {
2983+ // 获取代码库索引文件
2984+ const allFileIndex = this . db . getFileIndexForVectorSearch (
2985+ projectId ,
2986+ args . session_id
2987+ ) ;
2988+
2989+ // 转换 file_index 为兼容格式以便搜索
2990+ const fileIndexAsContexts = allFileIndex . map ( ( file ) => ( {
2991+ id : file . id ,
2992+ session_id : file . session_id ,
2993+ project_id : file . project_id ,
2994+ content : file . content ,
2995+ type : "code" as ContextType , // 使用 ContextType.CODE 表示代码文件
2996+ tags : file . tags ,
2997+ file_path : file . file_path ,
2998+ created_at : file . indexed_at ,
2999+ updated_at : file . modified_time ,
3000+ quality_score : 0.95 , // 提升代码文件优先级,确保"如何实现"类查询优先返回代码
3001+ embedding_text : undefined , // 文件没有预生成的embedding
3002+ metadata : file . metadata ,
3003+ } ) ) ;
3004+
3005+ // 合并开发记忆和代码库索引
3006+ const allSearchData = [ ...allContexts , ...fileIndexAsContexts ] ;
3007+
3008+ if ( allSearchData . length === 0 ) {
29423009 return {
29433010 content : [
29443011 {
29453012 type : "text" ,
2946- text : "No contexts with embeddings found. Try running generate_embeddings first." ,
3013+ text : "No contexts or codebase files found. Try running generate_embeddings first or index your codebase ." ,
29473014 } ,
29483015 ] ,
29493016 isError : false ,
29503017 _meta : {
29513018 query : args . query ,
29523019 results : [ ] ,
3020+ contexts_count : 0 ,
3021+ files_count : 0 ,
29533022 } ,
29543023 } ;
29553024 }
@@ -2958,7 +3027,7 @@ Note: This only deletes the file index, not your development memory contexts.`,
29583027 const searchParams = {
29593028 query : args . query ,
29603029 use_semantic_search : true ,
2961- limit : args . limit || 10 ,
3030+ limit : args . limit || 20 , // 增加默认限制以包含更多结果
29623031 similarity_threshold :
29633032 args . similarity_threshold ||
29643033 this . config . vector_search ?. similarity_threshold ||
@@ -2967,18 +3036,18 @@ Note: This only deletes the file index, not your development memory contexts.`,
29673036 args . hybrid_weight || this . config . vector_search ?. hybrid_weight || 0.7 ,
29683037 } ;
29693038
2970- // 获取关键词搜索结果作为基线
3039+ // 获取关键词搜索结果作为基线(仅针对开发记忆)
29713040 const keywordResults = this . db . searchContexts (
29723041 enhancedQuery ,
29733042 projectId ,
29743043 searchParams . limit
29753044 ) ;
29763045
2977- // 执行混合搜索
3046+ // 执行混合搜索(搜索所有数据:记忆 + 代码库)
29783047 let results = await this . vectorSearch . hybridSearch (
29793048 enhancedQuery ,
29803049 keywordResults ,
2981- allContexts ,
3050+ allSearchData ,
29823051 searchParams
29833052 ) ;
29843053
@@ -3197,10 +3266,13 @@ Note: This only deletes the file index, not your development memory contexts.`,
31973266 query : args . query ,
31983267 enhanced_query : enhancedQuery ,
31993268 total_contexts_searched : allContexts . length ,
3269+ total_files_searched : allFileIndex . length ,
32003270 results_count : formattedResults . length ,
32013271 results : formattedResults ,
32023272 search_params : searchParams ,
32033273 query_enhancement : queryEnhancementMeta ,
3274+ contexts_count : allContexts . length ,
3275+ files_count : allFileIndex . length ,
32043276 } ,
32053277 } ;
32063278 } catch ( error ) {
0 commit comments