Skip to content

Commit 6cd946d

Browse files
committed
feat: 增加历史聊天续聊的功能
1 parent 51ddb4e commit 6cd946d

File tree

3 files changed

+86
-5
lines changed

3 files changed

+86
-5
lines changed

package.json

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
"onCommand:codeReDesign.analyzeThisCvb",
2323
"onCommand:codeReDesign.summaryThisCvb",
2424
"onCommand:codeReDesign.analyzeSingleFile",
25-
"onCommand:codeReDesign.redesignSingleFile"
25+
"onCommand:codeReDesign.redesignSingleFile",
26+
27+
"onCommand:codeReDesign.startChat",
28+
"onCommand:codeReDesign.continueChat"
2629
],
2730
"repository": {
2831
"type": "git",
@@ -66,6 +69,12 @@
6669
"category": "CodeReDesign",
6770
"icon": "${chat}"
6871
},
72+
{
73+
"command": "codeReDesign.continueChat",
74+
"title": "continue Chat",
75+
"category": "CodeReDesign",
76+
"icon": "${chat}"
77+
},
6978
{
7079
"command": "codeReDesign.applyThisCvb",
7180
"title": "sidebar Apply this CVB to Workspace",
@@ -146,22 +155,27 @@
146155
{
147156
"command": "codeReDesign.applyThisCvb",
148157
"group": "cvb@1",
149-
"when": "viewItem == cvbFile "
158+
"when": "viewItem == cvbFile"
150159
},
151160
{
152161
"command": "codeReDesign.redesignThisCvb",
153162
"group": "cvb@1",
154-
"when": "viewItem == cvbFile "
163+
"when": "viewItem == cvbFile"
155164
},
156165
{
157166
"command": "codeReDesign.analyzeThisCvb",
158167
"group": "cvb@1",
159-
"when": "viewItem == cvbFile "
168+
"when": "viewItem == cvbFile"
160169
},
161170
{
162171
"command": "codeReDesign.summaryThisCvb",
163172
"group": "cvb@1",
164-
"when": "viewItem == cvbFile "
173+
"when": "viewItem == cvbFile"
174+
},
175+
{
176+
"command": "codeReDesign.continueChat",
177+
"group": "cvb@1",
178+
"when": "viewItem == chatFile"
165179
}
166180
],
167181
"editor/context": [

src/chatPanel.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,66 @@ export class ChatPanel {
348348
}
349349
}
350350

351+
public static async loadFromFile(context: vscode.ExtensionContext, filePath: string) {
352+
const content = fs.readFileSync(filePath, 'utf-8');
353+
const conversation = this.parseChatContent(content);
354+
355+
this.createOrShow(context);
356+
357+
ChatPanel.currentPanel!.chatFilePath = filePath;
358+
ChatPanel.currentPanel!.conversation = conversation;
359+
ChatPanel.currentPanel!.userMessageIndex = conversation.filter(m => m.role === 'user').length;
360+
361+
// 将历史记录发送到webview
362+
conversation.forEach((msg, index) => {
363+
ChatPanel.currentPanel!.panel.webview.postMessage({
364+
role: msg.role,
365+
content: msg.content,
366+
index: msg.role === 'user' ? index : undefined
367+
});
368+
});
369+
}
370+
371+
private static parseChatContent(content: string): Array<{role: string, content: string}> {
372+
const conversation = [];
373+
const lines = content.split('\n');
374+
let currentRole = '';
375+
let currentContent = [];
376+
377+
for (const line of lines) {
378+
if (line.startsWith('@user:')) {
379+
if (currentRole) {
380+
conversation.push({
381+
role: currentRole,
382+
content: currentContent.join('\n').trim()
383+
});
384+
}
385+
currentRole = 'user';
386+
currentContent = [];
387+
} else if (line.startsWith('@AI:')) {
388+
if (currentRole) {
389+
conversation.push({
390+
role: currentRole,
391+
content: currentContent.join('\n').trim()
392+
});
393+
}
394+
currentRole = 'model';
395+
currentContent = [];
396+
} else {
397+
currentContent.push(line);
398+
}
399+
}
400+
401+
if (currentRole) {
402+
conversation.push({
403+
role: currentRole,
404+
content: currentContent.join('\n').trim()
405+
});
406+
}
407+
408+
return conversation;
409+
}
410+
351411
public dispose(): void {
352412
ChatPanel.currentPanel = undefined;
353413
this.panel.dispose();

src/siderBar.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getCurrentOperationController, resetCurrentOperationController, clearC
77
import { showInputMultiLineBox } from './UIComponents';
88
import {getOutputChannel} from './extension';
99
import {SOURCE_FILE_EXTENSIONS_WITH_DOT} from './languageMapping';
10+
import { ChatPanel } from './chatPanel';
1011

1112
class ChatPreviewFileSystemProvider implements vscode.FileSystemProvider {
1213
private content: Uint8Array = new Uint8Array();
@@ -95,6 +96,12 @@ export function registerCvbContextMenu(context: vscode.ExtensionContext) {
9596
});
9697
context.subscriptions.push(redesignSingleFileCommand);
9798

99+
const loadChatHistoryCommand = vscode.commands.registerCommand('codeReDesign.continueChat', async (history: ChatFile) => {
100+
const filePath = history.resourceUri?.fsPath || "";
101+
ChatPanel.loadFromFile(context, filePath);
102+
});
103+
context.subscriptions.push(loadChatHistoryCommand);
104+
98105
// 注册 TreeDataProvider
99106
const cvbViewProvider = new CvbViewProvider();
100107
vscode.window.registerTreeDataProvider('codeReDesign.cvbView', cvbViewProvider);

0 commit comments

Comments
 (0)