From f6cad40a4498ed6f101f835b4f130df200184428 Mon Sep 17 00:00:00 2001 From: mp Date: Fri, 18 Oct 2024 19:18:11 -0700 Subject: [PATCH] Finish speculative draft --- extensions/void/package-lock.json | 9 +++--- extensions/void/package.json | 2 +- extensions/void/src/common/ctrlL.ts | 45 +++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/extensions/void/package-lock.json b/extensions/void/package-lock.json index a7f8924e..b0076339 100644 --- a/extensions/void/package-lock.json +++ b/extensions/void/package-lock.json @@ -37,7 +37,7 @@ "marked": "^14.1.0", "ollama": "^0.5.9", "postcss": "^8.4.41", - "posthog-js": "^1.174.0", + "posthog-js": "^1.174.2", "react": "^18.3.1", "react-dom": "^18.3.1", "react-markdown": "^9.0.1", @@ -6345,11 +6345,10 @@ "license": "MIT" }, "node_modules/posthog-js": { - "version": "1.174.0", - "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.174.0.tgz", - "integrity": "sha512-60qCn8bloCxVc3oBQ/JP77J40J7UD+cRGUfYXJdsqjUH82s2wmCx4MicuNrcn9Hd2dHM25nXmOAMLO5iwSq9gg==", + "version": "1.174.2", + "resolved": "https://registry.npmjs.org/posthog-js/-/posthog-js-1.174.2.tgz", + "integrity": "sha512-UgS7eRcDVvVz2XSJ09NMX8zBcdpFnPayfiWDNF3xEbJTsIu1GipkkYNrVlsWlq8U1PIrviNm6i0Dyq8daaxssw==", "dev": true, - "license": "MIT", "dependencies": { "core-js": "^3.38.1", "fflate": "^0.4.8", diff --git a/extensions/void/package.json b/extensions/void/package.json index 32d9b22e..5ce9928b 100644 --- a/extensions/void/package.json +++ b/extensions/void/package.json @@ -133,7 +133,7 @@ "marked": "^14.1.0", "ollama": "^0.5.9", "postcss": "^8.4.41", - "posthog-js": "^1.174.0", + "posthog-js": "^1.174.2", "react": "^18.3.1", "react-dom": "^18.3.1", "react-markdown": "^9.0.1", diff --git a/extensions/void/src/common/ctrlL.ts b/extensions/void/src/common/ctrlL.ts index 7f2bd3e5..60649218 100644 --- a/extensions/void/src/common/ctrlL.ts +++ b/extensions/void/src/common/ctrlL.ts @@ -549,7 +549,7 @@ ${newFileStr} voidConfig: { ...voidConfig, default: { - // set `max_tokens` = (number of expected tokens) + (number of extra tokens) + // set `maxTokens` = (number of expected tokens) + (number of extra tokens) maxTokens: Math.round((diff.split('\n').filter(l => !l.startsWith('-')).length) + EXTRA_TOKENS) + '' } }, @@ -561,9 +561,11 @@ ${newFileStr} } -const shouldApplyDiffToLine = async ({ diff, fileStr, lineStr, voidConfig, setAbort }: { diff: string, fileStr: string, lineStr: string, voidConfig: VoidConfig, setAbort: SetAbort }) => { +const shouldApplyDiffFn = async ({ diff, fileStr, speculationStr, type, voidConfig, setAbort }: { diff: string, fileStr: string, speculationStr: string, type: 'line' | 'chunk', voidConfig: VoidConfig, setAbort: SetAbort }) => { - const promptContent = `DIFF + const promptContent = ( + // the speculation is a line + type === 'line' ? `DIFF \`\`\` ${diff} \`\`\` @@ -574,21 +576,46 @@ ${fileStr} \`\`\` SELECTION -\`\`\`${lineStr}\`\`\` +\`\`\`${speculationStr}\`\`\` Return \`true\` if this line should be modified, and \`false\` if it should not be modified. ` + // the speculation is a chunk + : `DIFF +\`\`\` +${diff} +\`\`\` + +FILES +\`\`\` +${fileStr} +\`\`\` + +SELECTION +\`\`\` +${speculationStr} +\`\`\` + +Return \`true\` if this any part of the chunk should be modified, and \`false\` if it should not be modified. +`) // create new promise let res: Res = () => { } const promise = new Promise((resolve, reject) => { res = resolve }) sendLLMMessage({ - messages: [{ role: 'assistant', content: searchDiffLineInstructions, }, { role: 'assistant', content: promptContent, }], + messages: [ + { + role: 'assistant', + content: type === 'line' ? searchDiffLineInstructions : searchDiffChunkInstructions, + }, { + role: 'assistant', + content: promptContent, + }], onText: () => { }, onFinalMessage: (finalMessage) => { const containsTrue = finalMessage - .slice(-10) + .slice(-10) // check for `true` in last 10 characters .toLowerCase() .includes('true') res(containsTrue) @@ -598,7 +625,7 @@ Return \`true\` if this line should be modified, and \`false\` if it should not console.error('Error applying diff to line') }, voidConfig, - setAbort + setAbort, }) return promise @@ -627,7 +654,7 @@ const applyDiffLazily = async ({ fileUri, fileStr, diff, voidConfig, setAbort }: const chunkStr = chunkLines.join('\n'); // ask LLM if we should apply the diff to the chunk - let shouldApplyDiff = await shouldApplyDiffToChunk({ chunkStr, diff, fileUri, setAbort }) + let shouldApplyDiff = await shouldApplyDiffFn({ speculationStr: chunkStr, type: 'chunk', diff, fileStr, voidConfig, setAbort }) if (!shouldApplyDiff) { // should not change the chunk completedLines.push(chunkStr); // TODO update highlighting here @@ -638,7 +665,7 @@ const applyDiffLazily = async ({ fileUri, fileStr, diff, voidConfig, setAbort }: for (const lineStr of chunkLines) { // ask LLM if we should apply the diff to the line - let shouldApplyDiff = await shouldApplyDiffToLine({ diff, fileStr, lineStr, voidConfig, setAbort }) + let shouldApplyDiff = await shouldApplyDiffFn({ speculationStr: lineStr, type: 'line', diff, fileStr, voidConfig, setAbort }) if (!shouldApplyDiff) { // should not change the line completedLines.push(lineStr); // TODO update highlighting here