-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a function to find optimizations in code
- Loading branch information
1 parent
cf9911b
commit ae83743
Showing
9 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { CHARS_PER_TOKEN, MAX_AVAILABLE_PROMPT_LENGTH, MAX_RECIPE_INPUT_TOKENS } from '../../prompt/constants' | ||
import { truncateText } from '../../prompt/truncation' | ||
import { Interaction } from '../transcript/interaction' | ||
|
||
import { getNormalizedLanguageName } from './helpers' | ||
import { Recipe, RecipeContext, RecipeID } from './recipe' | ||
|
||
export class OptimizeCode implements Recipe { | ||
public id: RecipeID = 'optimize-code' | ||
|
||
public async getInteraction(_humanChatInput: string, context: RecipeContext): Promise<Interaction | null> { | ||
const selection = context.editor.getActiveTextEditorSelectionOrEntireFile() | ||
if (!selection) { | ||
await context.editor.showWarningMessage('No code selected. Please select some code and try again.') | ||
return Promise.resolve(null) | ||
} | ||
|
||
const languageName = getNormalizedLanguageName(selection.fileName) | ||
const promptPrefix = `Find code smells, potential bugs, and unhandled errors in my ${languageName} code:` | ||
const promptSuffix = `List maximum five of them as a list (if you have more in mind, mention that these are the top five), with a short context, reasoning, and suggestion on each. | ||
If you have no ideas because the code looks fine, feel free to say that it already looks fine.` | ||
|
||
// Use the whole context window for the prompt because we're attaching no files | ||
const maxTokenCount = | ||
MAX_AVAILABLE_PROMPT_LENGTH - (promptPrefix.length + promptSuffix.length) / CHARS_PER_TOKEN | ||
const truncatedSelectedText = truncateText( | ||
selection.selectedText, | ||
Math.min(maxTokenCount, MAX_RECIPE_INPUT_TOKENS) | ||
) | ||
const promptMessage = `${promptPrefix}\n\n\`\`\`\n${truncatedSelectedText}\n\`\`\`\n\n${promptSuffix}` | ||
|
||
const displayText = `Find all the optimizations in the following code: \n\`\`\`\n${selection.selectedText}\n\`\`\`` | ||
|
||
const assistantResponsePrefix = '' | ||
return new Interaction( | ||
{ speaker: 'human', text: promptMessage, displayText }, | ||
{ | ||
speaker: 'assistant', | ||
prefix: assistantResponsePrefix, | ||
text: assistantResponsePrefix, | ||
}, | ||
new Promise(resolve => resolve([])), | ||
[] | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters