Skip to content

Commit 0fa3e90

Browse files
committed
feat: 添加cvb压缩功能 && 修复r1模型提示词格式错误
1 parent 3c4f670 commit 0fa3e90

File tree

6 files changed

+243
-12
lines changed

6 files changed

+243
-12
lines changed

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"onCommand:codeReDesign.applyThisCvb",
2020
"onCommand:codeReDesign.analyzeCode",
2121
"onCommand:codeReDesign.uploadThisCvb",
22-
"onCommand:codeReDesign.analyzeThisCvb"
22+
"onCommand:codeReDesign.analyzeThisCvb",
23+
"onCommand:codeReDesign.compressThisCvb"
2324
],
2425
"repository": {
2526
"type": "git",
@@ -84,6 +85,13 @@
8485
"category": "CodeReDesign",
8586
"icon": "${bug}"
8687
},
88+
{
89+
"command": "codeReDesign.compressThisCvb",
90+
"title": "sidebar compress this CVB",
91+
"when": "false",
92+
"category": "CodeReDesign",
93+
"icon": "${beaker}"
94+
},
8795
{
8896
"command": "codeReDesign.showGuide",
8997
"title": "Show Guide",
@@ -128,6 +136,10 @@
128136
{
129137
"command": "codeReDesign.analyzeThisCvb",
130138
"group": "cvb@1"
139+
},
140+
{
141+
"command": "codeReDesign.compressThisCvb",
142+
"group": "cvb@1"
131143
}
132144
],
133145
"editor/context": [

prompt/rag.txt

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212

1313
������һ���򻯵�ʵ�����������ı�
14+
15+
��������CVB�Ƚϴ�ģ�͵�ע�������׶�ʧ������һ���Ľ�����
1416
1.�����ǰ������CVB����2k��С������ѹ������
1517
2.�ȱ���ÿ���ļ�������ļ�����̫��������ҳ
1618
3.��ÿ��ҳ+����һ��prompt��api�����������ϵ��°���˳���ȡ���õ�Ƭ�Σ�
@@ -28,3 +30,72 @@
2830
���Գ�ȡ��ϲ�֮��Ҫ��һ�μ��һ�³�����û�г���
2931
������ˣ�������һ�ε���ѹ��
3032

33+
---------
34+
35+
�������·����������ܣ�
36+
����ģ�ʹ��������ı�������ҵ������⣩ʱ��������ļ�����ᵼ��ģ��ע������ʧ��Ӱ�촦��Ч����Ϊ�����һ���⣬�Ľ���ķ���ͨ��ѹ���ͳ�ȡ�ؼ���Ϣ�����������ģ�͵��ı����ȣ�ͬʱȷ���������������õĺ��Ĵ�����Ϣ�������Ƿ�������ϸ��ƣ�
37+
38+
1. ѹ����������
39+
������ֵ����upload���������CVB�������ʾ�ļ�����С����4k�ַ�ʱ������ѹ�����ơ�
40+
�����飺��鱾���Ƿ���ڸ�CVB��ѹ���汾CCVB��ѹ�����CVB���������ڣ�ֱ��ʹ�û����CCVB���������ڣ�����ѹ����������CCVB��
41+
2. ѹ������
42+
�ļ���ҳ
43+
����������е�ÿ���ļ���
44+
���ļ����������糬��1k token������������߼��ṹ���纯�����ࡢģ�飩�����з֣�ȷ��ÿ��ҳ�治����2k token�����������������ԡ�
45+
�����߼��ṹ�з�ʧ�ܣ����˵������з֣�����ÿ100��һҳ����
46+
��Ϣ��ȡ
47+
��ÿ���зֺ��ҳ�棬����������󣬹���ṹ����Prompt���͸�API��Ҫ��API��˳���ȡ���ô���Ƭ�Ρ�
48+
Prompt�ṹ��
49+
����ҳ�����ݣ���ǰ�����Ĵ���Ƭ�Ρ�
50+
������������ȷ������Ŀ�꣬���硰�ҵ�ʵ�ֵ�¼���ܵĴ��벢��ȡ���滻�IJ��֡���
51+
��ȡ��׼��
52+
�������޸Ļ��滻�Ĵ��롣
53+
����ê�㸽���������ģ����鱣��ǰ��5�У���
54+
�����ڽ�������ĸ������롣
55+
�����ʽ��Ҫ��API���صĴ����֮����//...�ָ���
56+
��ȡ�鴦����
57+
ÿ����ȡ�鱣��������������Ϣ������ǰ��5�У���ȷ���ϲ����CVB�������ᡣ
58+
��API��ȡʧ�ܣ���¼ʧ��ҳ�沢������һҳ��
59+
�ϲ�����
60+
�����г�ȡ�Ĵ����ϲ�Ϊһ���µ�CVB������ԭʼ����
61+
�ںϲ���CVB�м���ע�ͣ���ȷ//...��ʡ�������ĵı�ǣ�������Ϊ����ê�㣺
62+
text
63+
Wrap
64+
Copy
65+
// ע�⣺//... ��ʾʡ�Ե������ģ������ڷָ���������������ê��
66+
�ϲ�����CVB��token���ȣ�
67+
���Գ���ģ�͵�token���ƣ�����2k token�������еڶ���ѹ���������ѹ��ǿ�ȣ���ɾ��ע�͡������Ҫ�����ȣ���
68+
�趨ѹ���������ޣ�����3�Σ�����ֹ����ѭ����
69+
3. ���¹���CVB�������ع�
70+
��ѹ�����CCVB��Ϊ���룬���ԭʼ���󣬽��д����ع��������
71+
���ع������У�ȷ��ģ����ȷʶ��//...��ǣ����⽫������Ϊê�㡣
72+
4. ����������
73+
��������
74+
���ļ���ҳʧ�ܣ����˵���С���зֵ�λ���簴���з֣���
75+
��API������Ч��յĽ������¼ʧ��ҳ�沢����������һҳ��
76+
��������
77+
���д������ҳ�棬����API���õĵȴ�ʱ�䡣
78+
�ڱ��ػ���CCVB�ļ����������ͬCVB�ظ�ѹ����
79+
����������ʱ��CCVB���棬��ʡ�洢�ռ䡣
80+
5. ������֤
81+
���ԣ�
82+
�ڲ�ͬ��ģ�Ĵ���⣨С�͡����͡���ҵ�����ϲ��Է�����
83+
����ѹ����CVB��token���ȡ���ȡ׼ȷ�ʡ��ع��ɹ��ʵ�ָ�ꡣ
84+
���ţ�
85+
���ݲ��Խ��������ҳ��С��Prompt��ƺ�ѹ��ǿ�ȣ�ȷ��������ʵ��Ӧ���е���Ч�ԡ�
86+
87+
-------------
88+
дһ��cvbѹ������ compressCvb������cvb���û�����
89+
90+
Ȼ�����cvb�������ļ�+�����ϴ�api����api��ȡ���м�ֵ��Ƭ��
91+
�м�ֵ��Ƭ�ΰ�����
92+
1.�ں����ع����������ṩ�ؼ���Ϣ����ģ�������
93+
2.���ع��Ĵ����
94+
3.��������滻�´����ʱ��Ҫ��ê�㸽����������
95+
96+
ͬһ���ļ���Ķ���м�ֵ��֮��������ʡ�Է�
97+
//...CCVB
98+
���ָ�
99+
100+
�����������ƴ�����������ʽ����cvb
101+
ֻ����������޹ش��뱻��ȡ������ʡ�Է�

src/cvbManager.ts

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import * as path from "path";
33
import * as jschardet from "jschardet"; // 编码检测库
44
import * as iconv from "iconv-lite"; // 编码转换库
55
import * as vscode from "vscode";
6-
import { generateFilenameFromRequest } from "./deepseekApi";
6+
import { generateFilenameFromRequest, callDeepSeekApi } from "./deepseekApi";
77

88
import { getLanguageFromPath } from "./languageMapping";
9+
import {getOutputChannel, getCurrentOperationController} from './extension'
910

1011
// ================== CVB 核心类 ==================
1112
export class Cvb {
@@ -693,6 +694,107 @@ function rebuildCvb(baseCvb: Cvb, mapFiles: Map<string, string>): Cvb {
693694
return cvb;
694695
}
695696

697+
export async function compressCvb(cvb: Cvb, userRequest: string): Promise<Cvb> {
698+
// 获取元数据和文件内容
699+
const metadata = cvb.getMetadata();
700+
const files = cvb.getFiles();
701+
const compressedFiles: Record<string, string> = {};
702+
703+
const outputChannel = getOutputChannel();
704+
const signal = getCurrentOperationController().signal;
705+
706+
// 遍历每个文件并压缩
707+
for (const [filePath, fileContent] of Object.entries(files)) {
708+
// 构造 API 请求内容
709+
const requestContent = `
710+
文件路径: ${filePath}
711+
712+
文件内容:
713+
\`\`\`
714+
${fileContent}
715+
\`\`\`
716+
717+
用户请求:
718+
\`\`\`
719+
${userRequest}
720+
\`\`\`
721+
722+
请从文件内容中识别并提取出有价值的代码片段,这些片段对理解代码在用户请求中的上下文非常重要。你需要关注以下几点:
723+
1. 提取出关键信息的代码块,这些代码块帮助理解用户请求中的核心上下文。比如在重构任务中,需要关注相关的函数、变量及其上下级调用等。
724+
2. 需要被处理的内容(如重构代码),应该被提取出来。
725+
3. 确定有必要作为“锚点”的代码段,以便后续处理时可以方便地替换。
726+
727+
例如:
728+
假设给定代码如下:
729+
\`\`\`
730+
function func1() {
731+
// 代码块1
732+
}
733+
734+
function func2() {
735+
// 代码块2
736+
}
737+
738+
function func3() {
739+
// 代码块3
740+
}
741+
\`\`\`
742+
743+
用户请求关注 \`func1\` 和 \`func2\`,并希望忽略 \`func3\`。你应该返回如下结果:
744+
745+
\`\`\`
746+
function func1() {
747+
// 代码块1
748+
}
749+
===SEGMENT===
750+
function func2() {
751+
// 代码块2
752+
}
753+
\`\`\`
754+
755+
注意:
756+
1. 只保留 \`func1\` 和 \`func2\`,并通过 \`===SEGMENT===\` 分隔。
757+
2. \`func3\` 被丢弃,**但其位置仍然被正确地分隔开**,以确保后续的处理不会出现问题。
758+
3. 例子里的 \`\`\` 只是为了便于说明格式清楚,你输出的时候不要有 \`\`\` 包裹代码
759+
760+
返回时,请确保**每个代码片段**都保持原始结构,不要有任何多余的文字,并且使用 \`===SEGMENT===\` 来分隔它们,而不是使用 \`\`\`code\`\`\` 或其他分隔符。
761+
762+
确保返回的格式是干净且可解析的,只包括代码片段和分隔符,不要包含任何额外的解释或注释信息!
763+
764+
`;
765+
766+
// 系统提示
767+
const systemContent = "你是一个代码分析助手。给定一个文件的内容和用户的请求,识别并提取出对理解代码在请求上下文中的有价值的代码片段。注意输出的时候不要有 \`\`\`";
768+
769+
// 调用 API
770+
const response = await callDeepSeekApi(requestContent, systemContent, outputChannel, true, undefined, signal);
771+
if (response) {
772+
// 处理 API 响应,分隔并连接有价值的代码片段
773+
const segments = response.split("===SEGMENT===").map(segment => segment.trim());
774+
const compressedContent = segments.join("\n//...CCVB\n");
775+
compressedFiles[filePath] = compressedContent;
776+
} else {
777+
// 如果 API 调用失败,保留原始内容
778+
compressedFiles[filePath] = fileContent;
779+
}
780+
}
781+
782+
// 构建新的 CVB 对象
783+
const newCvb = new Cvb();
784+
// 保留原始元数据
785+
for (const [key, value] of Object.entries(metadata)) {
786+
newCvb.setMetaData(key, value);
787+
}
788+
// 设置压缩后的文件内容
789+
for (const [filePath, content] of Object.entries(compressedFiles)) {
790+
newCvb.setFile(filePath, content);
791+
}
792+
793+
newCvb.setMetaData("用户需求", userRequest);
794+
795+
// 返回压缩后的 CVB 字符串
796+
return newCvb;
797+
}
696798
// ================== 工具函数 ==================
697799

698800
function escapeRegExp(str: string): string {

src/deepseekApi.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export async function callDeepSeekApi(
9595

9696
let systemPromot : OpenAI.ChatCompletionMessageParam = {role : "system", content: systemContent};
9797

98-
if (modelName.includes('r')) {
98+
if (/r1|reasoner/i.test(modelName)) {
9999
temperature = 0.6;
100100
systemPromot = {role : "user", content: systemContent};
101101
}
@@ -130,10 +130,7 @@ export async function callDeepSeekApi(
130130
}
131131

132132
// 如果是单个字符串,默认是 'user' 角色
133-
messages_body = [
134-
systemPromot,
135-
{ role: 'user', content: userContent },
136-
];
133+
messages_body.push({ role: 'user', content: userContent });
137134
}
138135

139136
vscode.window.showInformationMessage('开始上传DeepSeek API');

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export function activate(context: vscode.ExtensionContext) {
228228
}
229229

230230
const userPrompt = await showInputMultiLineBox({
231-
prompt: 'Enter your prompt for the refactoring',
231+
prompt: '输入你的重构方案',
232232
placeHolder: 'e.g., Refactor the code to improve readability',
233233
});
234234

@@ -329,7 +329,7 @@ export function activate(context: vscode.ExtensionContext) {
329329

330330
// 获取用户的分析需求
331331
const userRequest = await showInputMultiLineBox({
332-
prompt: 'Enter your analysis request',
332+
prompt: '输入你需要分析的需求',
333333
placeHolder: 'e.g., Analyze the code for potential bugs',
334334
});
335335

src/siderBar.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33
import * as fs from 'fs';
4-
import { applyCvbToWorkspace} from './cvbManager';
4+
import { applyCvbToWorkspace, compressCvb, Cvb} from './cvbManager';
55
import { analyzeCode } from './deepseekApi';
66
import { getCurrentOperationController, resetCurrentOperationController, clearCurrentOperationController, doUploadCommand, saveAnalyzeCodeResult} from './extension';
77
import { showInputMultiLineBox } from './UIComponents';
@@ -74,6 +74,12 @@ export function registerCvbContextMenu(context: vscode.ExtensionContext) {
7474
await analyzeThisCvb(filePath);
7575
});
7676

77+
// 注册分析 CVB 命令
78+
const compressCvbCommand = vscode.commands.registerCommand('codeReDesign.compressThisCvb', async (cvb: CvbFile) => {
79+
const filePath = cvb.resourceUri?.fsPath || "";
80+
await compressThisCvb(filePath);
81+
});
82+
7783
// 将命令添加到订阅中
7884
context.subscriptions.push(applyCvbCommand, uploadCvbCommand, analyzeCvbCommand);
7985

@@ -319,7 +325,7 @@ async function uploadThisCvb(filePath: string) {
319325
// 测试 end
320326
*/
321327
const userPrompt = await showInputMultiLineBox({
322-
prompt: 'Enter your prompt for the refactoring',
328+
prompt: '输入你的重构方案',
323329
placeHolder: 'e.g., Refactor the code to improve readability',
324330
});
325331

@@ -336,7 +342,7 @@ async function uploadThisCvb(filePath: string) {
336342
*/
337343
async function analyzeThisCvb(filePath: string) {
338344
const userRequest = await showInputMultiLineBox({
339-
prompt: 'Enter your analysis request',
345+
prompt: '输入你的分析需求',
340346
placeHolder: 'e.g., Analyze the code for potential bugs',
341347
});
342348

@@ -360,4 +366,47 @@ async function analyzeThisCvb(filePath: string) {
360366
}
361367
}
362368

369+
function getCompressedFileName(filePath: string): string {
370+
const { name, ext } = path.parse(filePath); // 使用 path.parse 获取文件名和扩展名
371+
return path.join(path.dirname(filePath), `${name}-compress${ext}`); // 拼接新的完整路径
372+
}
373+
374+
/**
375+
* 分析 CVB 文件
376+
* @param filePath .cvb 文件的路径
377+
*/
378+
async function compressThisCvb(filePath: string) {
379+
const userRequest = await showInputMultiLineBox({
380+
prompt: '输入压缩过程中需要关注的需求',
381+
placeHolder: 'e.g., Analyze the code for potential bugs',
382+
});
383+
384+
if (!userRequest) {
385+
return;
386+
}
387+
388+
const cvbContent = fs.readFileSync(filePath, 'utf-8');
389+
390+
const cvb = new Cvb(cvbContent);
391+
392+
resetCurrentOperationController();
393+
394+
const newCvb = await compressCvb(cvb, userRequest);
395+
clearCurrentOperationController();
396+
397+
if (newCvb) {
398+
vscode.window.showInformationMessage('compress cvb success!.');
399+
}
400+
else{
401+
vscode.window.showInformationMessage('compress cvb failed!.');
402+
return;
403+
}
404+
405+
newCvb.setMetaData("comressFrom", filePath);
406+
filePath = getCompressedFileName(filePath);
407+
408+
fs.writeFileSync(filePath, newCvb.toString(), 'utf-8');
409+
vscode.window.showInformationMessage(`Conversation log saved as: ${filePath}`);
410+
}
411+
363412
export function deactivate() {}

0 commit comments

Comments
 (0)