Skip to content

Commit

Permalink
Adding a function to find optimizations in code
Browse files Browse the repository at this point in the history
  • Loading branch information
arafatkatze committed Jul 28, 2023
1 parent cf9911b commit ae83743
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/shared/src/chat/recipes/agent-recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FindCodeSmells } from './find-code-smells'
import { GenerateDocstring } from './generate-docstring'
import { GenerateTest } from './generate-test'
import { ImproveVariableNames } from './improve-variable-names'
import { OptimizeCode } from './optimize-code'
import { Recipe, RecipeID } from './recipe'
import { TranslateToLanguage } from './translate'

Expand Down Expand Up @@ -36,6 +37,7 @@ function init(): void {
new ImproveVariableNames(),
new TranslateToLanguage(),
new FindCodeSmells(),
new OptimizeCode(),
]

for (const recipe of recipes) {
Expand Down
2 changes: 2 additions & 0 deletions lib/shared/src/chat/recipes/browser-recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { FindCodeSmells } from './find-code-smells'
import { GenerateDocstring } from './generate-docstring'
import { GenerateTest } from './generate-test'
import { ImproveVariableNames } from './improve-variable-names'
import { OptimizeCode } from './optimize-code'
import { Recipe, RecipeID } from './recipe'
import { TranslateToLanguage } from './translate'

Expand Down Expand Up @@ -36,6 +37,7 @@ function init(): void {
new ImproveVariableNames(),
new TranslateToLanguage(),
new FindCodeSmells(),
new OptimizeCode(),
]

for (const recipe of recipes) {
Expand Down
46 changes: 46 additions & 0 deletions lib/shared/src/chat/recipes/optimize-code.ts
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([])),
[]
)
}
}
1 change: 1 addition & 0 deletions lib/shared/src/chat/recipes/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type RecipeID =
| 'pr-description'
| 'release-notes'
| 'translate-to-language'
| 'optimize-code'

export interface Recipe {
id: RecipeID
Expand Down
13 changes: 13 additions & 0 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@
"category": "Ask Cody",
"title": "Find Code Smells"
},
{
"command": "cody.recipe.optimize-code",
"category": "Ask Cody",
"title": "Optimize Code"
},
{
"command": "cody.auth.signout",
"category": "Cody",
Expand Down Expand Up @@ -538,6 +543,10 @@
"command": "cody.recipe.find-code-smells",
"when": "cody.activated && editorHasSelection"
},
{
"command": "cody.recipe.optimize-code",
"when": "cody.activated && editorHasSelection"
},
{
"command": "cody.focus",
"title": "Cody: Sign In",
Expand Down Expand Up @@ -635,6 +644,10 @@
"command": "cody.recipe.find-code-smells",
"when": "cody.activated"
},
{
"command": "cody.recipe.optimize-code",
"when": "cody.activated"
},
{
"command": "cody.focus",
"when": "!cody.activated"
Expand Down
1 change: 1 addition & 0 deletions vscode/src/chat/ChatViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class ChatViewProvider extends MessageProvider implements vscode.WebviewV
switch (message.action) {
case 'explain-code-high-level':
case 'find-code-smells':
case 'optimize-code':
case 'generate-unit-test':
void this.executeRecipe(message.action)
break
Expand Down
1 change: 1 addition & 0 deletions vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ const register = async (
vscode.commands.registerCommand('cody.recipe.find-code-smells', () =>
executeRecipeInSidebar('find-code-smells')
),
vscode.commands.registerCommand('cody.recipe.optimize-code', () => executeRecipeInSidebar('optimize-code')),
vscode.commands.registerCommand('cody.recipe.context-search', () => executeRecipeInSidebar('context-search')),

// Register URI Handler (vscode://sourcegraph.cody-ai)
Expand Down
1 change: 1 addition & 0 deletions vscode/webviews/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ To get started, select some code and run one of Cody's recipes:"
gettingStartedButtons={[
{ label: 'Explain code (high level)', action: 'explain-code-high-level', onClick: onChatButtonClick },
{ label: 'Smell code', action: 'find-code-smells', onClick: onChatButtonClick },
{ label: 'Optimize code', action: 'optimize-code', onClick: onChatButtonClick },
{ label: 'Generate a unit test', action: 'generate-unit-test', onClick: onChatButtonClick },
]}
ChatButtonComponent={ChatButton}
Expand Down
1 change: 1 addition & 0 deletions vscode/webviews/Recipes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const recipesList = {
'context-search': 'Codebase context search',
'release-notes': 'Generate release notes',
'pr-description': 'Generate pull request description',
'optimize-code': 'Optimize code',
}

export const Recipes: React.FunctionComponent<{
Expand Down

0 comments on commit ae83743

Please sign in to comment.