From eae1af331f46994c159abb48434e8aa9b1b1a89a Mon Sep 17 00:00:00 2001 From: mp Date: Mon, 28 Oct 2024 03:36:04 -0700 Subject: [PATCH 1/2] Create Ctrl + K --- extensions/void/src/common/ctrlK.ts | 101 ++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 extensions/void/src/common/ctrlK.ts diff --git a/extensions/void/src/common/ctrlK.ts b/extensions/void/src/common/ctrlK.ts new file mode 100644 index 00000000..ebdc9dec --- /dev/null +++ b/extensions/void/src/common/ctrlK.ts @@ -0,0 +1,101 @@ +import * as vscode from 'vscode'; +import { OnFinalMessage, OnText, sendLLMMessage, SetAbort } from "./sendLLMMessage" +import { VoidConfig } from '../sidebar/contextForConfig'; +import { findDiffs } from '../findDiffs'; +import { searchDiffChunkInstructions, writeFileWithDiffInstructions } from './systemPrompts'; +import { throttle } from 'lodash'; +import { readFileContentOfUri } from './readFileContentOfUri'; + +type Res = ((value: T) => void) + +const THRTOTLE_TIME = 100 // minimum time between edits +const LINES_PER_CHUNK = 20 // number of lines to search at a time + +const applyCtrlLChangesToFile = throttle( + ({ fileUri, newCurrentLine, oldCurrentLine, fullCompletedStr, oldFileStr, debug }: { fileUri: vscode.Uri, newCurrentLine: number, oldCurrentLine: number, fullCompletedStr: string, oldFileStr: string, debug?: string }) => { + + // write the change to the file + const WRITE_TO_FILE = ( + fullCompletedStr.split('\n').slice(0, newCurrentLine + 1).join('\n') // newFile[:newCurrentLine+1] + + oldFileStr.split('\n').slice(oldCurrentLine + 1).join('\n') // oldFile[oldCurrentLine+1:] + ) + const workspaceEdit = new vscode.WorkspaceEdit() + workspaceEdit.replace(fileUri, new vscode.Range(0, 0, Number.MAX_SAFE_INTEGER, 0), WRITE_TO_FILE) + vscode.workspace.applyEdit(workspaceEdit) + + // highlight the `newCurrentLine` in white + // highlight the remaining part of the file in gray + + }, + THRTOTLE_TIME, { trailing: true } +) + + +const applyCtrlK = async ({ fileUri, startLine, endLine, instructions, voidConfig, setAbort }: { fileUri: vscode.Uri, startLine: number, endLine: number, instructions: string, voidConfig: VoidConfig, setAbort: SetAbort }) => { + + const fileStr = await readFileContentOfUri(fileUri) + const fileLines = fileStr.split('\n') + + const prefix = fileLines.slice(startLine).join('\n') + const suffix = fileLines.slice(endLine + 1).join('\n') + const selection = fileLines.slice(startLine, endLine + 1).join('\n') + + const promptContent = `Here is the user's original selection: +\`\`\` +${selection} +\`\`\` + +The user wants to apply the following instructions to the selection: +${instructions} + +Please rewrite the selection following the user's instructions. + +Instructions to follow: +1. Follow the user's instructions +2. You may ONLY CHANGE the selection, and nothing else in the file +3. Make sure all brackets in the new selection are balanced the same was as in the original selection +3. Be careful not to duplicate or remove variables, comments, or other syntax by mistake + +Complete the following: +\`\`\` +
${prefix}
+${suffix} +`; + + + // TODO initialize stream + + // update stream + sendLLMMessage({ + messages: [{ role: 'user', content: promptContent, }], + onText: async (tokenStr, completionStr) => { + // TODO update stream + + + // apply the changes + const newCode = `${prefix}\n${completionStr}\n${suffix}` + const workspaceEdit = new vscode.WorkspaceEdit() + workspaceEdit.replace(fileUri, new vscode.Range(0, 0, Number.MAX_SAFE_INTEGER, 0), newCode) + vscode.workspace.applyEdit(workspaceEdit) + }, + onFinalMessage: (completionStr) => { + // TODO end stream + + // apply the changes + const newCode = `${prefix}\n${completionStr}\n${suffix}` + const workspaceEdit = new vscode.WorkspaceEdit() + workspaceEdit.replace(fileUri, new vscode.Range(0, 0, Number.MAX_SAFE_INTEGER, 0), newCode) + vscode.workspace.applyEdit(workspaceEdit) + }, + onError: (e) => { + console.error('Error rewriting file with diff', e); + }, + voidConfig, + setAbort, + }) + +} + + + +export { applyCtrlK } \ No newline at end of file From 6d12737fda8746df64a215e1c7f45bf5078982db Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 28 Oct 2024 03:52:19 -0700 Subject: [PATCH 2/2] add throttling --- extensions/void/src/DiffProvider.ts | 7 +++++-- extensions/void/src/common/ctrlK.ts | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/extensions/void/src/DiffProvider.ts b/extensions/void/src/DiffProvider.ts index 9191392e..794905fa 100644 --- a/extensions/void/src/DiffProvider.ts +++ b/extensions/void/src/DiffProvider.ts @@ -2,8 +2,10 @@ import * as vscode from 'vscode'; import { findDiffs } from './findDiffs'; import { Diff, DiffArea, BaseDiff, } from './common/shared_types'; import { readFileContentOfUri } from './common/readFileContentOfUri'; +import { throttle } from 'lodash'; +const THROTTLE_TIME = 100 // TODO in theory this should be disposed const greenDecoration = vscode.window.createTextEditorDecorationType({ @@ -377,7 +379,7 @@ export class DiffProvider implements vscode.CodeLensProvider { // used by us only - public async updateStream(docUriStr: string, diffArea: DiffArea, newDiffAreaCode: string) { + public updateStream = throttle(async (docUriStr: string, diffArea: DiffArea, newDiffAreaCode: string) => { const editor = vscode.window.activeTextEditor // TODO the editor should be that of `docUri` and not necessarily the current editor if (!editor) { @@ -432,12 +434,13 @@ export class DiffProvider implements vscode.CodeLensProvider { const diffareaRange = new vscode.Range(diffArea.startLine, 0, diffArea.endLine, Number.MAX_SAFE_INTEGER) workspaceEdit.replace(editor.document.uri, diffareaRange, newCode) await vscode.workspace.applyEdit(workspaceEdit) - } + }, THROTTLE_TIME) } + /* import * as vscode from 'vscode'; import { SuggestedEdit } from './findDiffs'; diff --git a/extensions/void/src/common/ctrlK.ts b/extensions/void/src/common/ctrlK.ts index ebdc9dec..703ad30c 100644 --- a/extensions/void/src/common/ctrlK.ts +++ b/extensions/void/src/common/ctrlK.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import { OnFinalMessage, OnText, sendLLMMessage, SetAbort } from "./sendLLMMessage" +import { AbortRef, OnFinalMessage, OnText, sendLLMMessage } from "./sendLLMMessage" import { VoidConfig } from '../sidebar/contextForConfig'; import { findDiffs } from '../findDiffs'; import { searchDiffChunkInstructions, writeFileWithDiffInstructions } from './systemPrompts'; @@ -31,7 +31,7 @@ const applyCtrlLChangesToFile = throttle( ) -const applyCtrlK = async ({ fileUri, startLine, endLine, instructions, voidConfig, setAbort }: { fileUri: vscode.Uri, startLine: number, endLine: number, instructions: string, voidConfig: VoidConfig, setAbort: SetAbort }) => { +const applyCtrlK = async ({ fileUri, startLine, endLine, instructions, voidConfig, abortRef }: { fileUri: vscode.Uri, startLine: number, endLine: number, instructions: string, voidConfig: VoidConfig, abortRef: AbortRef }) => { const fileStr = await readFileContentOfUri(fileUri) const fileLines = fileStr.split('\n') @@ -91,7 +91,7 @@ Complete the following: console.error('Error rewriting file with diff', e); }, voidConfig, - setAbort, + abortRef, }) }