Skip to content

Commit

Permalink
fix: updates refactor to accept instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
KevalPrajapati committed Feb 28, 2024
1 parent c09dbbb commit f4d38b2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
14 changes: 9 additions & 5 deletions vscode/src/action-managers/diff-view-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {

Expand Down
22 changes: 16 additions & 6 deletions vscode/src/action-managers/refactor-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions vscode/src/providers/chat_view_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
8 changes: 6 additions & 2 deletions vscode/src/repository/gemini-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export class GeminiRepository extends GenerationRepository {
public async refactorCode(finalstring: string, contextualCode: string | undefined, instructions: string, globalState: vscode.Memento): Promise<string | undefined> {
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 <CURSOR_SELECTION> ..... <CURSOR_SELECTION>.\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 <CURSOR_SELECTION> ..... <CURSOR_SELECTION>.\n` + '```\n' + finalstring + '\n```\n';

prompt = appendReferences(referenceEditor, prompt);
if (contextualCode) {
Expand All @@ -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',
Expand Down

0 comments on commit f4d38b2

Please sign in to comment.