diff --git a/vscode/src/action-managers/diff-view-agent.ts b/vscode/src/action-managers/diff-view-agent.ts index fdbed285..5ad683e3 100644 --- a/vscode/src/action-managers/diff-view-agent.ts +++ b/vscode/src/action-managers/diff-view-agent.ts @@ -5,17 +5,21 @@ export class DiffViewAgent { const chip = data.chip; const optimizedCode = data.optimizedCode; const originalCodeUri = data.originalCodeUri; - const editorUri = chip.referenceData.editor; - const document = vscode.workspace.textDocuments.find(function (e) { - console.log(e.uri.toString(), editorUri); - return e.uri.toString() === editorUri; + let document = vscode.workspace.textDocuments.find(function (e) { + console.log(e.uri.toString(), originalCodeUri); + return e.uri.toString() === originalCodeUri; }); const selection = chip.referenceData.selection; const range: vscode.Range = new vscode.Range(new vscode.Position(selection.start.line, selection.start.character), new vscode.Position(selection.end.line, selection.end.character)); if (!document) { - return; + // if document is not founds, open the document + let uri = vscode.Uri.parse(originalCodeUri); + document = await vscode.workspace.openTextDocument(uri); + if (!document) { + return; + } } if (userChoice === 'accept') { diff --git a/vscode/src/action-managers/refactor-agent.ts b/vscode/src/action-managers/refactor-agent.ts index 4ca619e7..f15c71f5 100644 --- a/vscode/src/action-managers/refactor-agent.ts +++ b/vscode/src/action-managers/refactor-agent.ts @@ -8,15 +8,25 @@ export class RefactorActionManager { constructor() { } - static async handleRequest(chipsData: any, data: any, aiRepo: GeminiRepository, context: vscode.ExtensionContext, analyzer: ILspAnalyzer, flutterGPTViewProvider: FlutterGPTViewProvider) { - - const chip = Object.values(chipsData).values().next().value; - data.message = data.message.replace(chip.chipId, ''); + static async handleRequest(chips: any, chipIds: string[], data: any, aiRepo: GeminiRepository, context: vscode.ExtensionContext, analyzer: ILspAnalyzer, flutterGPTViewProvider: FlutterGPTViewProvider) { + var instructions = data.instructions as string; + for (const chip of chipIds) { + if (instructions.includes(chip)) { + instructions = instructions.replace(chip, ''); + } + } + const chip = Object.values(chips).values().next().value; + // Assuming first chip is code to refactor const editorUri = chip.referenceData.editor; - const editor = vscode.window.visibleTextEditors.find(e => e.document.uri.toString() === editorUri); + let editor = vscode.window.visibleTextEditors.find(e => e.document.uri.toString() === editorUri); + if (!editor) { + let uri = vscode.Uri.parse(editorUri); + let document = await vscode.workspace.openTextDocument(uri); + editor = await vscode.window.showTextDocument(document); + } const selection = chip.referenceData.selection; const range: vscode.Range = new vscode.Range(new vscode.Position(selection.start.line, selection.start.character), new vscode.Position(selection.end.line, selection.end.character)); - const optimizedCode = await refactorCode(aiRepo!, context.globalState, range, analyzer, undefined, context, flutterGPTViewProvider, editor, data.message, false); + const optimizedCode = await refactorCode(aiRepo!, context.globalState, range, analyzer, undefined, context, flutterGPTViewProvider, editor, instructions.trim(), false); return { role: "dash", parts: 'Do you want to merge these changes?', messageId: "", data: { 'chip': chip, diff --git a/vscode/src/providers/chat_view_provider.ts b/vscode/src/providers/chat_view_provider.ts index 0235486a..7303c06f 100644 --- a/vscode/src/providers/chat_view_provider.ts +++ b/vscode/src/providers/chat_view_provider.ts @@ -155,13 +155,15 @@ export class FlutterGPTViewProvider implements vscode.WebviewViewProvider { private async handleAction(input: string) { const data = JSON.parse(input); const actionType = data.message.startsWith('/') ? data.message.split('\u00A0')[0].substring(1) : ''; - const chipsData = data.chipsData; + const chipsData: object = data.chipsData; data.message = data.message.replace(`/${actionType}`, '').trim(); + data.instructions = data.instructions.replace(`/${actionType}`, '').trim(); + const chipIds: string[] = data.chipId; if (actionType === 'refactor') { this._publicConversationHistory.push({ role: 'user', parts: data.message }); this._view?.webview.postMessage({ type: 'displayMessages', value: this._publicConversationHistory }); this._view?.webview.postMessage({ type: 'showLoadingIndicator' }); - const result = await RefactorActionManager.handleRequest(chipsData, data, this.aiRepo!, this.context, this.analyzer!, this); + const result = await RefactorActionManager.handleRequest(chipsData, chipIds, data, this.aiRepo!, this.context, this.analyzer!, this); this._view?.webview.postMessage({ type: 'hideLoadingIndicator' }); this._publicConversationHistory.push(result); this._view?.webview.postMessage({ type: 'displayMessages', value: this._publicConversationHistory }); diff --git a/vscode/src/repository/gemini-repository.ts b/vscode/src/repository/gemini-repository.ts index 16b8ccad..1426a59b 100644 --- a/vscode/src/repository/gemini-repository.ts +++ b/vscode/src/repository/gemini-repository.ts @@ -399,7 +399,7 @@ export class GeminiRepository extends GenerationRepository { public async refactorCode(finalstring: string, contextualCode: string | undefined, instructions: string, globalState: vscode.Memento): Promise { let referenceEditor = getReferenceEditor(globalState); let prompt = 'You are a Flutter/Dart assistant helping user modify code within their editor window.'; - prompt += `Modification instructions from user: ${instructions}. Please find the editor file code. To represent the selected code, we have it highlighted with ..... .\n` + '```\n' + finalstring + '\n```\n'; + prompt += `Modification instructions from user:\n${instructions}.\n\nPlease find the editor file code. To represent the selected code, we have it highlighted with ..... .\n` + '```\n' + finalstring + '\n```\n'; prompt = appendReferences(referenceEditor, prompt); if (contextualCode) { @@ -409,7 +409,11 @@ export class GeminiRepository extends GenerationRepository { 1. Describe the selected piece of code. 2. What is the intent of user's modification? 3. How do you plan to achieve that? [Don't output code yet] - 4. Output the modified code to be be programatically replaced in the editor in place of the CURSOR_SELECTION. Since this is without human review, you need to output the precise CURSOR_SELECTION`; + 4. Output the modified code to be be programatically replaced in the editor in place of the CURSOR_SELECTION. Since this is without human review, you need to output the precise CURSOR_SELECTION + + IMPORTANT NOTE: Please make sure to output the modified code in a single code block. + Do not just give explanation prose but also give the final code at last. + `; console.log(prompt); const result = await this.getCompletion([{ 'role': 'user',