File tree Expand file tree Collapse file tree 1 file changed +18
-2
lines changed
Expand file tree Collapse file tree 1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ import { CONFIG as RAG_CONFIG } from './ragService';
1818import { parseString } from 'xml2js' ;
1919import ExcelJS from 'exceljs' ;
2020import pdfParse from 'pdf-parse' ;
21+ import { getLanguageFromPath } from './languageMapping' ;
2122
2223/** 定义工具的接口 */
2324export interface Tool {
@@ -592,8 +593,23 @@ export const readTextFile: Tool = {
592593 } ,
593594 function : async ( args : { filePath : string } ) => {
594595 try {
595- const content = await fs . readFile ( args . filePath , 'utf-8' ) ;
596- return content ;
596+ let content = await fs . readFile ( args . filePath , 'utf-8' ) ;
597+ const language = getLanguageFromPath ( args . filePath ) ;
598+ const PRE = `${ args . filePath } \n\`\`\`${ language } \n` ;
599+ const POST = `\n\`\`\`` ;
600+
601+ // 计算可用内容长度(预留注释和格式空间)
602+ const MAX_LEN = 2048 ;
603+ const reservedSpace = PRE . length + POST . length + 30 ; // 30为截断注释长度
604+ let maxContentLen = MAX_LEN - reservedSpace ;
605+
606+ // 处理内容截断
607+ let truncated = content . length > maxContentLen ;
608+ if ( truncated ) {
609+ content = content . substring ( 0 , maxContentLen ) + '\n// ...(内容已截断,超过2K限制)' ;
610+ }
611+
612+ return PRE + content + POST ;
597613 } catch ( error : any ) {
598614 return `读取文件失败: ${ error . message } ` ;
599615 }
You can’t perform that action at this time.
0 commit comments