From a896f226e76134977a29b965e750ef78df0d9b7a Mon Sep 17 00:00:00 2001 From: wadhia-yash Date: Tue, 27 Feb 2024 19:56:25 +0530 Subject: [PATCH] feat(Refactor user message bubble): When user uses refactoring command we can show the prompt which user has entered in the user message bubbles --- user_config.json | 1 + vscode/media/onboarding/onboarding.js | 89 ++++++++++--------- vscode/src/providers/chat_view_provider.ts | 2 + .../refactor/refactor_from_instructions.ts | 3 +- vscode/src/tools/reference/add_reference.ts | 1 + 5 files changed, 55 insertions(+), 41 deletions(-) create mode 100644 user_config.json diff --git a/user_config.json b/user_config.json new file mode 100644 index 00000000..04b6dbd1 --- /dev/null +++ b/user_config.json @@ -0,0 +1 @@ +{"version":"0.2.1"} \ No newline at end of file diff --git a/vscode/media/onboarding/onboarding.js b/vscode/media/onboarding/onboarding.js index 3a39ec8b..f0f014ba 100644 --- a/vscode/media/onboarding/onboarding.js +++ b/vscode/media/onboarding/onboarding.js @@ -509,20 +509,21 @@ class CommandDeck { }); sendButton.addEventListener("click", (event) => { - let prompt = textInput.textContent; - - for (const chip in chipsData) { - if (prompt.includes(chip)) { - prompt = prompt.replace(chip, chipsData[chip].referenceContent); - } - } - - vscode.postMessage({ type: "prompt", value: prompt }); - googleApiKeyHeader.classList.add("hidden"); - if (onboardingCompleted) { - textInput.textContent = ''; - } - adjustHeight(); + // let prompt = textInput.textContent; + + // for (const chip in chipsData) { + // if (prompt.includes(chip)) { + // prompt = prompt.replace(chip, chipsData[chip].referenceContent); + // } + // } + + // vscode.postMessage({ type: "prompt", value: prompt }); + // googleApiKeyHeader.classList.add("hidden"); + // if (onboardingCompleted) { + // textInput.textContent = ''; + // } + // adjustHeight(); + submitResponse(); }); textInput.addEventListener("paste", (event) => { @@ -561,6 +562,39 @@ function addToolTipsById() { }); } +function submitResponse() { + const textRefactor = document.getElementById("text-to-refactor-span"); + if (textRefactor) { + textRefactor.remove(); + } + let prompt = textInput.textContent; + if (!prompt.startsWith('/')) { + for (const chip in chipsData) { + if (prompt.includes(chip)) { + prompt = prompt.replace(chip, chipsData[chip].referenceContent); + } + } + } + if (!prompt.startsWith('/')) { + vscode.postMessage({ type: "prompt", value: prompt }); + } else { + for (const chip in chipsData) { + if (prompt.includes(chip)) { + prompt = prompt.replace(chip, chipsData[chip].referenceContent); + } + } + vscode.postMessage({ + type: "action", + value: JSON.stringify({ + 'message': prompt, + 'chipsData': chipsData, + }), + }); + } + textInput.textContent = ""; + adjustHeight(); +} + function handleSubmit(event) { const resolveFn = async (query, type) => { // Array to store possible matches @@ -617,32 +651,7 @@ function handleSubmit(event) { if (event.key === "Enter" && !event.shiftKey && commandDeck.menuRef?.hidden) { event.preventDefault(); - const textRefactor = document.getElementById("text-to-refactor-span"); - if (textRefactor) { - textRefactor.remove(); - } - let prompt = textInput.textContent; - if (!prompt.startsWith('/')) { - for (const chip in chipsData) { - if (prompt.includes(chip)) { - prompt = prompt.replace(chip, chipsData[chip].referenceContent); - } - } - } - if (!prompt.startsWith('/')) { - vscode.postMessage({ type: "prompt", value: prompt }); - } else { - debugger; - vscode.postMessage({ - type: "action", - value: JSON.stringify({ - 'message': prompt, - 'chipsData': chipsData, - }), - }); - } - textInput.textContent = ""; - adjustHeight(); + submitResponse(); } if (event.key === "Backspace") { diff --git a/vscode/src/providers/chat_view_provider.ts b/vscode/src/providers/chat_view_provider.ts index f7cf7381..0235486a 100644 --- a/vscode/src/providers/chat_view_provider.ts +++ b/vscode/src/providers/chat_view_provider.ts @@ -158,6 +158,8 @@ export class FlutterGPTViewProvider implements vscode.WebviewViewProvider { const chipsData = data.chipsData; data.message = data.message.replace(`/${actionType}`, '').trim(); 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); this._view?.webview.postMessage({ type: 'hideLoadingIndicator' }); diff --git a/vscode/src/tools/refactor/refactor_from_instructions.ts b/vscode/src/tools/refactor/refactor_from_instructions.ts index 416d0bdb..5548bbde 100644 --- a/vscode/src/tools/refactor/refactor_from_instructions.ts +++ b/vscode/src/tools/refactor/refactor_from_instructions.ts @@ -46,6 +46,7 @@ export async function refactorCode(generationRepository: GenerationRepository, g const workspaceFolders = vscode.workspace.workspaceFolders; let relativePath = editor.document.fileName; + const referenceContent = editor.document.getText(editor.selection); if (workspaceFolders && workspaceFolders.length > 0) { const workspaceRoot = workspaceFolders[0].uri.fsPath; relativePath = path.relative(workspaceRoot, editor.document.fileName); @@ -62,7 +63,7 @@ export async function refactorCode(generationRepository: GenerationRepository, g }); flutterGPTViewProvider.postMessageToWebview({ type: 'addToReference', value: JSON.stringify({ - relativePath: relativePath.trim(), referenceContent: '', referenceData: { + relativePath: relativePath.trim(), referenceContent: `\`\n${relativePath.trim()}\n\`\n\`\`\`\n${referenceContent.toString()}\n\`\`\`\n`, referenceData: { 'selection': { 'start': { 'line': replaceRange.start.line, diff --git a/vscode/src/tools/reference/add_reference.ts b/vscode/src/tools/reference/add_reference.ts index 101638ec..59b94664 100644 --- a/vscode/src/tools/reference/add_reference.ts +++ b/vscode/src/tools/reference/add_reference.ts @@ -26,6 +26,7 @@ export async function addToReference(globalState: vscode.Memento, flutterGPTView relativePath = path.relative(workspaceRoot, editor.document.fileName); } + debugger; flutterGPTViewProvider.postMessageToWebview({ type: 'addToReference', value: JSON.stringify({ relativePath: relativePath.trim(), referenceContent: `\`\n${relativePath.trim()}\n\`\n\`\`\`\n${referenceContent.toString()}\n\`\`\`\n`, referenceData: {