Skip to content

Commit 06548ca

Browse files
committed
feat:压缩改为并发,加快处理速度
1 parent 9ffee2f commit 06548ca

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

src/cvbManager.ts

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -888,16 +888,17 @@ export async function compressCvb(cvb: Cvb, userRequest: string): Promise<Cvb> {
888888
const metadata = cvb.getMetadata();
889889
const files = cvb.getFiles();
890890
const compressedFiles: Record<string, string> = {};
891+
const MAX_CONCURRENT = 5; // 设置最大并行数量为5
891892

892893
const outputChannel = getOutputChannel();
893894
const signal = getCurrentOperationController().signal;
894895

895896
outputChannel.appendLine("compress task start");
896897

897-
// 遍历每个文件并压缩
898-
for (const [filePath, fileContent] of Object.entries(files)) {
899-
// 构造 API 请求内容
900-
const requestContent = `
898+
// 将文件处理任务放入队列
899+
const fileEntries = Object.entries(files);
900+
const processFile = async ([filePath, fileContent]: [string, string]) => {
901+
const requestContent = `
901902
文件路径: ${filePath}
902903
903904
文件内容:
@@ -952,44 +953,65 @@ function func2() {
952953
返回时,请确保**每个代码片段**都保持原始结构,不要有任何多余的文字,并且使用 \`===SEGMENT===\` 来分隔它们,而不是使用 \`\`\`code\`\`\` 或其他分隔符。
953954
954955
确保返回的格式是干净且可解析的,只包括代码片段和分隔符,不要包含任何额外的解释或注释信息!
955-
956-
`;
956+
`;
957957

958-
// 系统提示
959-
const systemContent = "你是一个代码分析助手。给定一个文件的内容和用户的请求,识别并提取出对理解代码在请求上下文中的有价值的代码片段。注意输出的时候不要有 \`\`\`";
958+
const systemContent = "你是一个代码分析助手。给定一个文件的内容和用户的请求,识别并提取出对理解代码在请求上下文中的有价值的代码片段。注意输出的时候不要有 \`\`\`";
960959

961-
outputChannel.appendLine(`compress processing .. ${filePath}`);
962-
// 调用 API
960+
outputChannel.appendLine(`compress processing .. ${filePath}`);
961+
try {
963962
const response = await callDeepSeekApi(requestContent, systemContent, undefined, true, undefined, signal, true);
964963
if (response) {
965-
// 处理 API 响应,分隔并连接有价值的代码片段
966-
const segments = response.split("===SEGMENT===").map(segment => segment.trim());
967-
const compressedContent = segments.join("\n//...CCVB\n");
968-
compressedFiles[filePath] = compressedContent;
969-
970-
outputChannel.appendLine(`compress processing .. ${filePath} [success]`);
964+
const segments = response.split("===SEGMENT===").map(segment => segment.trim());
965+
const compressedContent = segments.join("\n//...CCVB\n");
966+
compressedFiles[filePath] = compressedContent;
967+
outputChannel.appendLine(`compress processing .. ${filePath} [success]`);
971968
} else {
972-
// 如果 API 调用失败,保留原始内容
973-
outputChannel.appendLine(`compress processing .. ${filePath} [failed]`);
969+
outputChannel.appendLine(`compress processing .. ${filePath} [failed]`);
974970
}
975-
}
971+
} catch (error) {
972+
outputChannel.appendLine(`compress processing .. ${filePath} [failed: ${error}]`);
973+
}
974+
};
975+
976+
// 创建并行处理队列
977+
const processQueue = async () => {
978+
const activePromises: Promise<void>[] = [];
979+
980+
for (const entry of fileEntries) {
981+
// 当达到最大并行数时,等待任意一个任务完成
982+
if (activePromises.length >= MAX_CONCURRENT) {
983+
await Promise.race(activePromises);
984+
}
985+
986+
// 创建新任务
987+
const promise = processFile(entry).then(() => {
988+
// 任务完成后从活动promise数组中移除
989+
const index = activePromises.indexOf(promise);
990+
if (index !== -1) {
991+
activePromises.splice(index, 1);
992+
}
993+
});
994+
995+
activePromises.push(promise);
996+
}
997+
998+
// 等待所有剩余任务完成
999+
await Promise.all(activePromises);
1000+
};
1001+
1002+
await processQueue();
9761003

9771004
outputChannel.appendLine("compress task finish");
9781005

979-
// 构建新的 CVB 对象
9801006
const newCvb = new Cvb();
981-
// 保留原始元数据
9821007
for (const [key, value] of Object.entries(metadata)) {
983-
newCvb.setMetaData(key, value);
1008+
newCvb.setMetaData(key, value);
9841009
}
985-
// 设置压缩后的文件内容
9861010
for (const [filePath, content] of Object.entries(compressedFiles)) {
987-
newCvb.setFile(filePath, content);
1011+
newCvb.setFile(filePath, content);
9881012
}
9891013

9901014
newCvb.setMetaData("用户需求", userRequest);
991-
992-
// 返回压缩后的 CVB 字符串
9931015
return newCvb;
9941016
}
9951017
// ================== 工具函数 ==================

0 commit comments

Comments
 (0)