Skip to content

Commit 8ffb6eb

Browse files
committed
feat: 增加中途中断指令
1 parent f2175ec commit 8ffb6eb

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ dist/
1717
Thumbs.db
1818
tmp/*
1919
.CodeReDesignWorkSpace/*
20+
*.vsix

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ ctrl + shift + p 打开指令菜单(Command Palette),有以下几个指令
3939
如果查看后觉得没问题,可以用这个指令
4040
将这个 CVB 格式文件展开覆盖本地文件
4141

42-
42+
4. **codeReDesign.stopOperation** (中断处理)
43+
中断正在执行的uploadCvb操作
4344
---
4445

4546
# CodeReDesign
@@ -82,3 +83,6 @@ Press `ctrl + shift + p` to open the Command Palette, where the following comman
8283
3. **codeReDesign.applyCvb** (Apply CVB file)
8384
If you review the changes and find them satisfactory, you can use this command.
8485
It will unpack and overwrite the local files with the changes from the CVB file.
86+
87+
4. **codeReDesign.stopOperation** (stop Upload CVB)
88+
stop processing Operation

package-lock.json

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
{
4040
"command": "codeReDesign.applyThisCvb",
4141
"title": "CodeReDesign: Apply this CVB to Workspace"
42+
},
43+
{
44+
"command": "codeReDesign.stopOperation",
45+
"title": "CodeReDesign: Stop Operation"
4246
}
4347
],
4448
"views": {
@@ -105,6 +109,6 @@
105109
"dependencies": {
106110
"iconv-lite": "^0.6.3",
107111
"jschardet": "^3.1.4",
108-
"openai": "^4.78.1"
112+
"openai": "^4.81.0"
109113
}
110114
}

src/deepseekApi.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ const apiBaseURL = "https://api.deepseek.com";
3232
* @param outputChannel 输出通道,用于实时显示流式内容
3333
* @param streamMode 是否启用流式模式
3434
* @param endstring 结束字符串,用于检查输出是否包含特定字符串
35+
* @param abortSignal 用于中断请求的信号
3536
* @returns API 返回的完整内容
3637
*/
3738
async function callDeepSeekApi(
3839
userContent: string,
3940
systemContent: string = 'You are a helpful assistant.',
4041
outputChannel?: vscode.OutputChannel,
4142
streamMode: boolean = true,
42-
endstring?: string
43+
endstring?: string,
44+
abortSignal?: AbortSignal
4345
): Promise<string | null> {
4446
const apiKey = getDeepSeekApiKey();
4547
if (!apiKey) {
@@ -85,6 +87,9 @@ async function callDeepSeekApi(
8587

8688
if (streamMode) {
8789
for await (const chunk of response as AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>) {
90+
if (abortSignal?.aborted) {
91+
throw new Error('operation stop by user');
92+
}
8893
const content = chunk.choices[0]?.delta?.content || '';
8994
chunkResponse += content;
9095
if (outputChannel) {
@@ -128,6 +133,10 @@ async function callDeepSeekApi(
128133
return fullResponse;
129134

130135
} catch (error) {
136+
if (error instanceof Error && error.message === 'operation stop by user') {
137+
vscode.window.showInformationMessage('operation stop by user');
138+
return null;
139+
}
131140
vscode.window.showErrorMessage('API调用失败: ' + (error as Error).message);
132141
return null;
133142
}
@@ -138,12 +147,14 @@ async function callDeepSeekApi(
138147
* @param cvbContent CVB 文件内容
139148
* @param userRequest 用户输入的重构需求
140149
* @param outputChannel 输出通道,用于实时显示流式内容
150+
* @param abortSignal 用于中断请求的信号
141151
* @returns API 返回的完整 CVB 内容
142152
*/
143153
export async function queryCodeReDesign(
144154
cvbContent: string,
145155
userRequest: string,
146-
outputChannel: vscode.OutputChannel
156+
outputChannel: vscode.OutputChannel,
157+
abortSignal?: AbortSignal
147158
): Promise<string | null> {
148159
const requestContent = `
149160
@@ -170,7 +181,7 @@ ${userRequest}
170181
请输出CVB格式的代码:
171182
`;
172183

173-
return callDeepSeekApi(requestContent, undefined, outputChannel, true, '## END_CVB'); // 添加结束字符串
184+
return callDeepSeekApi(requestContent, undefined, outputChannel, true, '## END_CVB', abortSignal); // 添加结束字符串
174185
}
175186

176187
/**

src/extension.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ export function activate(context: vscode.ExtensionContext) {
4141
vscode.window.showInformationMessage(`CVB file generated at: ${cvbFilePath}`);
4242
});
4343

44+
// 用于存储当前的上传操作
45+
let currentOperationController: AbortController | null = null;
46+
4447
// 注册命令:上传 CVB 并调用 API
4548
let uploadCvbCommand = vscode.commands.registerCommand('codeReDesign.uploadCvb', async () => {
4649
const workspaceFolders = vscode.workspace.workspaceFolders;
@@ -88,13 +91,27 @@ export function activate(context: vscode.ExtensionContext) {
8891
const cvbFilePath = path.join(tmpDir, selectedCvbFile);
8992
const cvbContent = fs.readFileSync(cvbFilePath, 'utf-8');
9093

91-
const apiResponse = await queryCodeReDesign(cvbContent, userPrompt, outputChannel);
94+
// 创建新的 AbortController
95+
currentOperationController = new AbortController();
96+
const apiResponse = await queryCodeReDesign(cvbContent, userPrompt, outputChannel, currentOperationController.signal);
9297
if (apiResponse) {
9398
const { cvbContent: newCvbContent, metadata, files } = parseCvb(apiResponse);
9499
const newCvbFilePath = path.join(tmpDir, fileName);
95100
fs.writeFileSync(newCvbFilePath, newCvbContent, 'utf-8');
96101
vscode.window.showInformationMessage(`API response saved as CVB file: ${newCvbFilePath}`);
97102
}
103+
currentOperationController = null;
104+
});
105+
106+
// 注册命令:中断当前的上传操作
107+
let stopOperation = vscode.commands.registerCommand('codeReDesign.stopOperation', () => {
108+
if (currentOperationController) {
109+
currentOperationController.abort();
110+
currentOperationController = null;
111+
vscode.window.showInformationMessage('Stop operation.');
112+
} else {
113+
vscode.window.showInformationMessage('No operation in progress.');
114+
}
98115
});
99116

100117
// 注册命令:应用 CVB 到工作目录
@@ -135,7 +152,7 @@ export function activate(context: vscode.ExtensionContext) {
135152
}
136153
});
137154

138-
context.subscriptions.push(generateCvbCommand, uploadCvbCommand, applyCvbCommand, outputChannel);
155+
context.subscriptions.push(generateCvbCommand, uploadCvbCommand, applyCvbCommand, stopOperation, outputChannel);
139156

140157
setupCvbAsMarkdown(context);
141158

0 commit comments

Comments
 (0)