Skip to content

Commit

Permalink
Context: add three new experimental retriever strategies (#5494)
Browse files Browse the repository at this point in the history
# Context
The PR adds three major context sources useful for autocomplete and
next-edit-suggestion.

### Recent Copy retriever.

Developers often copy/cut and paste context and before pasting we show
autocomplete suggestions to the user. The PR leverages the copied
content on the clipboard by the user as a context source.
I wasn't able to find any vscode api event exposed which triggers when
user `copy` or `cut` text in the editor. This seems like an open issue:
microsoft/vscode#30066

Another alternative I think of is to use a keybinding of `Ctrl+x` and
`Ctrl+c`, but not fully sure about its implications, since this is one
of the most common shortcuts.

As a workaround, The way current PR accomplishes the same is:
1. Tracks the selection made by the user in the last `1 minutes` and
keeps tracks of upto `100` most recent selections.
3. At the time of retrieval, checks if the current clipboard content
matches the selection items in the list.

### Recent View Ports
This context source captures and utilizes the recently viewed portions
of code in the editor. It keeps track of the visible areas of code that
the developer has scrolled through or focused on within a specified time
frame.

### Diagnostics 
The Diagnostics context source leverages the diagnostic information
provided by VS Code and language servers. It collects and utilizes
information about errors, for a file as a context source.

## Test plan
1. Automated test 
- Added CI tests for each of the retrievers

2. Manual test
- Override the setting `"cody.autocomplete.experimental.graphContext":
"recent-copy"` in vscode settings.
- Observe the context events using `Autocomplete Trace View`
  • Loading branch information
hitesh-1997 authored Sep 24, 2024
1 parent fa96146 commit 588501f
Show file tree
Hide file tree
Showing 13 changed files with 1,204 additions and 5 deletions.
3 changes: 3 additions & 0 deletions vscode/src/completions/completion-provider-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class CompletionProviderConfig {
'recent-edits-1m',
'recent-edits-5m',
'recent-edits-mixed',
'recent-copy',
'diagnostics',
'recent-view-port',
]
return resolvedConfig.pipe(
mergeMap(({ configuration }) => {
Expand Down
25 changes: 23 additions & 2 deletions vscode/src/completions/context/context-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import type { ContextRetriever } from '../types'
import type { BfgRetriever } from './retrievers/bfg/bfg-retriever'
import { JaccardSimilarityRetriever } from './retrievers/jaccard-similarity/jaccard-similarity-retriever'
import { LspLightRetriever } from './retrievers/lsp-light/lsp-light-retriever'
import { RecentEditsRetriever } from './retrievers/recent-edits/recent-edits-retriever'
import { DiagnosticsRetriever } from './retrievers/recent-user-actions/diagnostics-retriever'
import { RecentCopyRetriever } from './retrievers/recent-user-actions/recent-copy'
import { RecentEditsRetriever } from './retrievers/recent-user-actions/recent-edits-retriever'
import { RecentViewPortRetriever } from './retrievers/recent-user-actions/recent-view-port'
import { loadTscRetriever } from './retrievers/tsc/load-tsc-retriever'

export type ContextStrategy =
Expand All @@ -26,6 +29,9 @@ export type ContextStrategy =
| 'recent-edits-1m'
| 'recent-edits-5m'
| 'recent-edits-mixed'
| 'recent-copy'
| 'diagnostics'
| 'recent-view-port'

export interface ContextStrategyFactory extends vscode.Disposable {
getStrategy(
Expand Down Expand Up @@ -82,6 +88,18 @@ export class DefaultContextStrategyFactory implements ContextStrategyFactory {
this.localRetriever = new JaccardSimilarityRetriever()
this.graphRetriever = new LspLightRetriever()
break
case 'recent-copy':
this.localRetriever = new RecentCopyRetriever({
maxAgeMs: 60 * 1000,
maxSelections: 100,
})
break
case 'diagnostics':
this.localRetriever = new DiagnosticsRetriever()
break
case 'recent-view-port':
this.localRetriever = new RecentViewPortRetriever()
break
case 'jaccard-similarity':
this.localRetriever = new JaccardSimilarityRetriever()
break
Expand Down Expand Up @@ -148,7 +166,10 @@ export class DefaultContextStrategyFactory implements ContextStrategyFactory {
case 'jaccard-similarity':
case 'recent-edits':
case 'recent-edits-1m':
case 'recent-edits-5m': {
case 'recent-edits-5m':
case 'recent-copy':
case 'diagnostics':
case 'recent-view-port': {
if (this.localRetriever) {
retrievers.push(this.localRetriever)
}
Expand Down
Loading

0 comments on commit 588501f

Please sign in to comment.