Skip to content

Commit b9eec98

Browse files
committed
fix: 读取文件的tool函数限制长度
1 parent 71bd061 commit b9eec98

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/apiTools.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { CONFIG as RAG_CONFIG } from './ragService';
1818
import { parseString } from 'xml2js';
1919
import ExcelJS from 'exceljs';
2020
import pdfParse from 'pdf-parse';
21+
import {getLanguageFromPath} from './languageMapping';
2122

2223
/** 定义工具的接口 */
2324
export 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
}

0 commit comments

Comments
 (0)