From a29543e4a715a72b89ad74969857b993ad7e462a Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 25 Oct 2024 01:33:54 -0700 Subject: [PATCH] Promisify --- extensions/void/src/common/ctrlL.ts | 142 ++++++++++++++-------------- 1 file changed, 72 insertions(+), 70 deletions(-) diff --git a/extensions/void/src/common/ctrlL.ts b/extensions/void/src/common/ctrlL.ts index 02b0723d..aa75c835 100644 --- a/extensions/void/src/common/ctrlL.ts +++ b/extensions/void/src/common/ctrlL.ts @@ -66,78 +66,81 @@ ${completedStr} \`\`\` ` // create a promise that can be awaited - let res: Res = () => { } - const promise = new Promise((resolve, reject) => { res = resolve }) + const promise = new Promise((resolve, reject) => { - // get the abort method - let _abort = () => { } - let did_abort = false + // get the abort method + let _abort = () => { } + let did_abort = false - // make LLM complete the file to include the diff - sendLLMMessage({ - messages: [{ role: 'system', content: writeFileWithDiffInstructions, }, { role: 'user', content: promptContent, }], - onText: (tokenStr, deltaStr) => { - if (did_abort) return; + sendLLMMessage({ + messages: [{ role: 'system', content: writeFileWithDiffInstructions, }, { role: 'user', content: promptContent, }], + onText: (tokenStr, deltaStr) => { - const fullCompletedStr = completedStr + deltaStr + if (did_abort) return; - // diff `originalFileStr` and `newFileStr` - const diffs = findDiffs(oldFileStr, fullCompletedStr) - const lastDiff = diffs[diffs.length - 1] - const oldLineAfterLastDiff = lastDiff.deletedRange.end.line + 1 - const newLineAfterLastDiff = lastDiff.insertedRange.end.line + 1 + const fullCompletedStr = completedStr + deltaStr - // check if we've generated a diff - const didGenerateDiff = newLineAfterLastDiff > next + // diff `originalFileStr` and `newFileStr` + const diffs = findDiffs(oldFileStr, fullCompletedStr) + const lastDiff = diffs[diffs.length - 1] + const oldLineAfterLastDiff = lastDiff.originalEndLine + 1 + const newLineAfterLastDiff = lastDiff.endLine + 1 - // get the line we are currently generating `newCurrentLine`; make sure it never goes past the last diff we've generated - // - if `deltaStr` contains a diff, then _next = newLineAfterLastDiff - 1 - // - if it does not contain a diff, then _next = next + deltaStr.split('\n').length - 1 - const newCurrentLine = didGenerateDiff ? newLineAfterLastDiff - 1 : next + deltaStr.split('\n').length - 1 - const oldCurrentLine = didGenerateDiff ? oldLineAfterLastDiff - 1 : oldNext + (newCurrentLine - next) + // check if we've generated a diff + const didGenerateDiff = newLineAfterLastDiff > next - // 1. Apply the changes and modify highlighting + // get the line we are currently generating `newCurrentLine`; make sure it never goes past the last diff we've generated + // - if `deltaStr` contains a diff, then _next = newLineAfterLastDiff - 1 + // - if it does not contain a diff, then _next = next + deltaStr.split('\n').length - 1 + const newCurrentLine = didGenerateDiff ? newLineAfterLastDiff - 1 : next + deltaStr.split('\n').length - 1 + const oldCurrentLine = didGenerateDiff ? oldLineAfterLastDiff - 1 : oldNext + (newCurrentLine - next) - applyCtrlLChangesToFile({ fileUri, newCurrentLine, oldCurrentLine, fullCompletedStr, oldFileStr }) + // 1. Apply the changes and modify highlighting - // 2. Check for early stopping - // the conditions for early stopping are: - // - we have generated a diff - // - there is matchup with the original file after the diff - const isMatchupAfterDiff = fullCompletedStr.split('\n').slice(newLineAfterLastDiff).join('\n').length > NUM_MATCHUP_TOKENS - if (didGenerateDiff && isMatchupAfterDiff) { + applyCtrlLChangesToFile({ fileUri, newCurrentLine, oldCurrentLine, fullCompletedStr, oldFileStr }) - // resolve the promise - res({ next: newCurrentLine + 1, oldNext: oldCurrentLine + 1, }); + // 2. Check for early stopping + // the conditions for early stopping are: + // - we have generated a diff + // - there is matchup with the original file after the diff + const isMatchupAfterDiff = fullCompletedStr.split('\n').slice(newLineAfterLastDiff).join('\n').length > NUM_MATCHUP_TOKENS + if (didGenerateDiff && isMatchupAfterDiff) { - // abort the LLM call - _abort() - did_abort = true + // resolve the promise + resolve({ next: newCurrentLine + 1, oldNext: oldCurrentLine + 1, }); - } else { + // abort the LLM call + _abort() + did_abort = true - } + } else { + + } - }, - onFinalMessage: (deltaStr) => { + }, + onFinalMessage: (deltaStr) => { - const newCompletedStr = completedStr + deltaStr + const newCompletedStr = completedStr + deltaStr - applyCtrlLChangesToFile({ fileUri, newCurrentLine: Number.MAX_SAFE_INTEGER, oldCurrentLine: Number.MAX_SAFE_INTEGER, fullCompletedStr: newCompletedStr, oldFileStr, debug: 'FINAL' }) + applyCtrlLChangesToFile({ fileUri, newCurrentLine: Number.MAX_SAFE_INTEGER, oldCurrentLine: Number.MAX_SAFE_INTEGER, fullCompletedStr: newCompletedStr, oldFileStr, debug: 'FINAL' }) - res({ isFinished: true }); - }, - onError: (e) => { - res({ isFinished: true }); - console.error('Error rewriting file with diff', e); - }, - voidConfig, - setAbort: (a) => { setAbort(a); _abort = a; }, + resolve({ isFinished: true }); + }, + onError: (e) => { + resolve({ isFinished: true }); + console.error('Error rewriting file with diff', e); + }, + voidConfig, + setAbort: (a) => { setAbort(a); _abort = a; }, + }) }) + + + return promise } @@ -164,28 +167,27 @@ Return \`true\` if ANY part of the chunk should be modified, and \`false\` if it ` // create new promise - let res: Res = () => { } - const promise = new Promise((resolve, reject) => { res = resolve }) + const promise = new Promise((resolve, reject) => { + // send message to LLM + sendLLMMessage({ + messages: [{ role: 'system', content: searchDiffChunkInstructions, }, { role: 'user', content: promptContent, }], + onFinalMessage: (finalMessage) => { - // send message to LLM - sendLLMMessage({ - messages: [{ role: 'system', content: searchDiffChunkInstructions, }, { role: 'user', content: promptContent, }], - onFinalMessage: (finalMessage) => { + const containsTrue = finalMessage + .slice(-10) // check for `true` in last 10 characters + .toLowerCase() + .includes('true') - const containsTrue = finalMessage - .slice(-10) // check for `true` in last 10 characters - .toLowerCase() - .includes('true') - - res(containsTrue) - }, - onError: (e) => { - res(false); - console.error('Error in shouldApplyDiff: ', e) - }, - onText: () => { }, - voidConfig, - setAbort, + resolve(containsTrue) + }, + onError: (e) => { + resolve(false); + console.error('Error in shouldApplyDiff: ', e) + }, + onText: () => { }, + voidConfig, + setAbort, + }) }) // return the promise