-
Notifications
You must be signed in to change notification settings - Fork 291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding autoedits support #5845
Merged
Merged
adding autoedits support #5845
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7230317
adding autoedits support
hitesh-1997 f63e4eb
fix prompt templates as per fine-tuning
hitesh-1997 e1caec2
fix ci test cases
hitesh-1997 53bb858
logs diff at the cursor position
hitesh-1997 602822d
add info on lines
hitesh-1997 85d7d93
display diff with hover provider
abeatrix eb3f0a1
change model with fix indentation error
hitesh-1997 9a01733
use config from the vscode settings
hitesh-1997 6a3113e
minor fix
hitesh-1997 e488280
minor fix
hitesh-1997 890b3fb
add decorations for rendering
hitesh-1997 0d7dc7a
address pr comments
hitesh-1997 8d28c07
Merge branch 'main' into hitesh/autoedits-integeration
hitesh-1997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,107 @@ | ||
import { type AutoEditsTokenLimit, type DocumentContext, logDebug } from '@sourcegraph/cody-shared' | ||
import { Observable } from 'observable-fns' | ||
import * as vscode from 'vscode' | ||
import { ContextMixer } from '../completions/context/context-mixer' | ||
import { DefaultContextStrategyFactory } from '../completions/context/context-strategy' | ||
import { getCurrentDocContext } from '../completions/get-current-doc-context' | ||
import { getConfiguration } from '../configuration' | ||
import { DeepSeekPromptProvider, OpenAIPromptProvider, type PromptProvider } from './prompt-provider' | ||
import { AutoEditsRenderer } from './renderer' | ||
|
||
const AUTOEDITS_CONTEXT_STRATEGY = 'auto-edits' | ||
|
||
export interface AutoEditsProviderOptions { | ||
document: vscode.TextDocument | ||
position: vscode.Position | ||
} | ||
|
||
export class AutoeditsProvider implements vscode.Disposable { | ||
private disposables: vscode.Disposable[] = [] | ||
private contextMixer: ContextMixer = new ContextMixer( | ||
new DefaultContextStrategyFactory(Observable.of(AUTOEDITS_CONTEXT_STRATEGY)), | ||
false | ||
) | ||
private autoEditsTokenLimit: AutoEditsTokenLimit | undefined | ||
private provider: PromptProvider | undefined | ||
private model: string | undefined | ||
private apiKey: string | undefined | ||
private renderer: AutoEditsRenderer = new AutoEditsRenderer() | ||
|
||
constructor() { | ||
const config = getConfiguration().experimentalAutoedits | ||
if (config === undefined) { | ||
logDebug('AutoEdits', 'No Configuration found in the settings') | ||
return | ||
} | ||
this.initizlizePromptProvider(config.provider) | ||
this.autoEditsTokenLimit = config.tokenLimit as AutoEditsTokenLimit | ||
this.model = config.model | ||
this.apiKey = config.apiKey | ||
this.disposables.push( | ||
this.contextMixer, | ||
this.renderer, | ||
vscode.commands.registerCommand('cody.experimental.suggest', () => this.getAutoedit()) | ||
) | ||
} | ||
|
||
private initizlizePromptProvider(provider: string) { | ||
if (provider === 'openai') { | ||
this.provider = new OpenAIPromptProvider() | ||
} else if (provider === 'deepseek') { | ||
this.provider = new DeepSeekPromptProvider() | ||
} else { | ||
logDebug('AutoEdits', `provider ${provider} not supported`) | ||
} | ||
} | ||
|
||
public getAutoedit() { | ||
this.predictAutoeditAtDocAndPosition({ | ||
document: vscode.window.activeTextEditor!.document, | ||
position: vscode.window.activeTextEditor!.selection.active, | ||
}) | ||
} | ||
|
||
public async predictAutoeditAtDocAndPosition(options: AutoEditsProviderOptions) { | ||
if (!this.provider || !this.autoEditsTokenLimit || !this.model || !this.apiKey) { | ||
logDebug('AutoEdits', 'No Provider or Token Limit found in the settings') | ||
return | ||
} | ||
const start = Date.now() | ||
const docContext = this.getDocContext(options.document, options.position) | ||
const { context } = await this.contextMixer.getContext({ | ||
document: options.document, | ||
position: options.position, | ||
docContext: docContext, | ||
maxChars: 100000, | ||
}) | ||
const { codeToReplace, promptResponse: prompt } = this.provider.getPrompt( | ||
docContext, | ||
options.document, | ||
context, | ||
this.autoEditsTokenLimit | ||
) | ||
const response = await this.provider.getModelResponse(this.model, this.apiKey, prompt) | ||
const timeToResponse = Date.now() - start | ||
logDebug('AutoEdits: (Time LLM Query):', timeToResponse.toString()) | ||
await this.renderer.render(options, codeToReplace, response) | ||
} | ||
|
||
private getDocContext(document: vscode.TextDocument, position: vscode.Position): DocumentContext { | ||
return getCurrentDocContext({ | ||
document, | ||
position, | ||
maxPrefixLength: convertTokensToChars(this.autoEditsTokenLimit?.prefixTokens ?? 0), | ||
maxSuffixLength: convertTokensToChars(this.autoEditsTokenLimit?.suffixTokens ?? 0), | ||
}) | ||
} | ||
|
||
public dispose() { | ||
for (const disposable of this.disposables) { | ||
disposable.dispose() | ||
} | ||
} | ||
} | ||
|
||
function convertTokensToChars(tokens: number) { | ||
return tokens * 4 | ||
} | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
tokensToChars
from@sourcegraph/cody-shared
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done