Skip to content
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

feat(autoedits): make it reactive to config changes #6537

Merged
merged 11 commits into from
Jan 8, 2025
2 changes: 1 addition & 1 deletion agent/src/AgentWorkspaceConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('AgentWorkspaceConfiguration', () => {
expect(config.get('cody.telemetry.level')).toBe('agent')
// clientName undefined because custom JSON specified telemetry with level alone.
expect(config.get('cody.telemetry.clientName')).toBe('test-client')
expect(config.get('cody.autocomplete.enabled')).toBe(true)
expect(config.get('cody.suggestions.mode')).toBe('autocomplete')
expect(config.get('cody.autocomplete.advanced.provider')).toBe('anthropic')
expect(config.get('cody.autocomplete.advanced.model')).toBe('claude-2')
expect(config.get('cody.advanced.agent.running')).toBe(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ConfigUtils {
"""
{
"cody.debug": true,
"cody.autocomplete.enabled": true
"cody.suggestions.mode": "autocomplete"
}
"""
val result = ConfigUtil.getCustomConfiguration(mockProject, input)
Expand All @@ -39,7 +39,7 @@ class ConfigUtils {
"""
{
"cody.debug": true,
"cody.autocomplete.enabled": true,
"cody.suggestions.mode": "autocomplete"
}
"""
val result = ConfigUtil.getCustomConfiguration(mockProject, input)
Expand All @@ -55,7 +55,7 @@ class ConfigUtils {
{
// This is a comment
"cody.debug": true,
"cody.autocomplete.enabled": true,
"cody.suggestions.mode": "autocomplete"
}
"""
val result = ConfigUtil.getCustomConfiguration(mockProject, input)
Expand Down
18 changes: 18 additions & 0 deletions lib/shared/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ export enum CodyIDE {
StandaloneWeb = 'StandaloneWeb',
}

/**
* These values must match the enum values in cody.suggestions.mode in vscode/package.json
*/
export enum CodyAutoSuggestionMode {
/**
* The suggestion mode where suggestions come from the OpenAI completions API. This is the default mode.
*/
Autocomplete = 'autocomplete',
/**
* The suggestion mode where suggestions come from the Cody AI agent chat API.
*/
Autoedits = 'auto-edits (Experimental)',
/**
* Disable Cody suggestions altogether.
*/
Off = 'off',
}

export type AutocompleteProviderID = keyof typeof AUTOCOMPLETE_PROVIDER_ID

export const AUTOCOMPLETE_PROVIDER_ID = {
Expand Down
25 changes: 15 additions & 10 deletions vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,13 @@
"command": "cody.autocomplete.openTraceView",
"category": "Cody",
"title": "Open Autocomplete Trace View",
"enablement": "cody.activated && config.cody.autocomplete.enabled"
"enablement": "cody.activated && config.cody.suggestions.mode === 'autocomplete'"
},
{
"command": "cody.autocomplete.manual-trigger",
"category": "Cody",
"title": "Trigger Autocomplete at Cursor",
"enablement": "cody.activated && config.cody.autocomplete.enabled && !editorReadonly && !editorHasSelection && !inlineSuggestionsVisible"
"enablement": "cody.activated && config.cody.suggestions.mode === 'autocomplete' && !editorReadonly && !editorHasSelection && !inlineSuggestionsVisible"
},
{
"command": "cody.chat.signIn",
Expand Down Expand Up @@ -578,7 +578,7 @@
{
"command": "cody.command.autoedits-manual-trigger",
"title": "Autoedits Manual Trigger",
"enablement": "cody.activated && config.cody.experimental.autoedits.enabled"
"enablement": "cody.activated && config.cody.suggestions.mode == 'auto-edits (Experimental)'"
}
],
"keybindings": [
Expand Down Expand Up @@ -687,7 +687,7 @@
{
"command": "cody.autocomplete.manual-trigger",
"key": "alt+\\",
"when": "editorTextFocus && !editorHasSelection && config.cody.autocomplete.enabled && !inlineSuggestionsVisible"
"when": "editorTextFocus && !editorHasSelection && config.cody.suggestions.mode === 'autocomplete' && !inlineSuggestionsVisible"
},
{
"command": "cody.fixup.acceptNearest",
Expand Down Expand Up @@ -739,7 +739,7 @@
{
"command": "cody.supersuggest.testExample",
"key": "ctrl+alt+enter",
"when": "cody.activated && config.cody.experimental.autoedits.enabled"
"when": "cody.activated && config.cody.suggestions.mode == 'auto-edits (Experimental)'"
}
],
"submenus": [
Expand Down Expand Up @@ -1001,11 +1001,16 @@
}
]
},
"cody.autocomplete.enabled": {
"order": 5,
"type": "boolean",
"markdownDescription": "Enables code autocompletions.",
"default": true
"cody.suggestions.mode": {
"type": "string",
"enum": ["autocomplete", "auto-edits (Experimental)", "off"],
"enumDescriptions": [
"Suggests standard code completions as you type.",
"Suggests advanced context-aware code edits as you navigate the codebase. Experimental feature for Pro and Enterprise users.",
"Disables all suggestions."
],
"default": "autocomplete",
"markdownDescription": "Controls the suggestion mode for Cody"
},
"cody.autocomplete.triggerDelay": {
"order": 5,
Expand Down
56 changes: 56 additions & 0 deletions vscode/src/autoedits/create-autoedits-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Observable, map } from 'observable-fns'
import * as vscode from 'vscode'

import {
type AuthenticatedAuthStatus,
type ChatClient,
NEVER,
type PickResolvedConfiguration,
type UnauthenticatedAuthStatus,
createDisposables,
skipPendingOperation,
} from '@sourcegraph/cody-shared'
import { AutoeditsProvider } from './autoedits-provider'
import { autoeditsOutputChannelLogger } from './output-channel-logger'

interface AutoeditsItemProviderArgs {
config: PickResolvedConfiguration<{ configuration: true }>
authStatus: UnauthenticatedAuthStatus | Pick<AuthenticatedAuthStatus, 'authenticated' | 'endpoint'>
chatClient: ChatClient
}

export function createAutoEditsProvider({
config: { configuration },
authStatus,
chatClient,
}: AutoeditsItemProviderArgs): Observable<void> {
if (!configuration.experimentalAutoeditsEnabled) {
return NEVER
}

if (!authStatus.authenticated) {
if (!authStatus.pendingValidation) {
autoeditsOutputChannelLogger.logDebug('createProvider', 'You are not signed in.')
}
return NEVER
}

return Observable.of(undefined).pipe(
skipPendingOperation(),
createDisposables(() => {
const provider = new AutoeditsProvider(chatClient)
return [
vscode.commands.registerCommand('cody.command.autoedits-manual-trigger', async () => {
await vscode.commands.executeCommand('editor.action.inlineSuggest.hide')
await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger')
}),
vscode.languages.registerInlineCompletionItemProvider(
[{ scheme: 'file', language: '*' }, { notebookType: '*' }],
provider
),
provider,
]
}),
map(() => undefined)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function createInlineCompletionItemProvider({
) {
throw new Error(
'The setting `config.autocomplete` evaluated to `false`. It must be true when running inside the agent. ' +
'To fix this problem, make sure that the setting cody.autocomplete.enabled has the value true.'
'To fix this problem, make sure that the setting cody.suggestions.mode has the value autocomplete.'
)
}
return NEVER
Expand Down
15 changes: 9 additions & 6 deletions vscode/src/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { assert, describe, expect, it } from 'vitest'
import type * as vscode from 'vscode'

import { type ClientConfiguration, OLLAMA_DEFAULT_URL, ps } from '@sourcegraph/cody-shared'
import {
type ClientConfiguration,
CodyAutoSuggestionMode,
OLLAMA_DEFAULT_URL,
ps,
} from '@sourcegraph/cody-shared'

import type { ChatModelProviderConfig } from '@sourcegraph/cody-shared/src/models/sync'
import { getConfiguration } from './configuration'
Expand All @@ -28,8 +33,8 @@ describe('getConfiguration', () => {
'Cache-Control': 'no-cache',
'Proxy-Authenticate': 'Basic',
}
case 'cody.autocomplete.enabled':
return false
case 'cody.suggestions.mode':
return CodyAutoSuggestionMode.Off
case 'cody.autocomplete.languages':
return { '*': true }
case 'cody.commandCodeLenses':
Expand Down Expand Up @@ -95,8 +100,6 @@ describe('getConfiguration', () => {
return false
case 'cody.experimental.supercompletions':
return false
case 'cody.experimental.autoedits.enabled':
return undefined
case 'cody.experimental.autoedits-renderer-testing':
return false
case 'cody.experimental.autoedits.config.override':
Expand Down Expand Up @@ -166,7 +169,7 @@ describe('getConfiguration', () => {
agenticContextExperimentalShell: false,
agenticContextExperimentalOptions: { shell: { allow: ['git'] } },
experimentalSupercompletions: false,
experimentalAutoeditsEnabled: undefined,
experimentalAutoeditsEnabled: false,
experimentalAutoeditsConfigOverride: undefined,
experimentalAutoeditsRendererTesting: false,
experimentalMinionAnthropicKey: undefined,
Expand Down
10 changes: 8 additions & 2 deletions vscode/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as vscode from 'vscode'

import {
type ClientConfiguration,
CodyAutoSuggestionMode,
type CodyIDE,
OLLAMA_DEFAULT_URL,
type PickResolvedConfiguration,
Expand Down Expand Up @@ -46,6 +47,11 @@ export function getConfiguration(
debugRegex = /.*/
}

const codyAutoSuggestionsMode = config.get<string>(
CONFIG_KEY.suggestionsMode,
CodyAutoSuggestionMode.Autocomplete
)

return {
net: {
mode: config.get<string | null | undefined>(CONFIG_KEY.netMode, undefined),
Expand All @@ -68,7 +74,7 @@ export function getConfiguration(
debugVerbose: config.get<boolean>(CONFIG_KEY.debugVerbose, false),
debugFilter: debugRegex,
telemetryLevel: config.get<'all' | 'off'>(CONFIG_KEY.telemetryLevel, 'all'),
autocomplete: config.get(CONFIG_KEY.autocompleteEnabled, true),
autocomplete: codyAutoSuggestionsMode === CodyAutoSuggestionMode.Autocomplete,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we migrate settings from people with the old configuration field in their settings.json? If I have "cody.autocomplete.enabled": false in my settings I expect CONFIG_KEY.suggestionsMode to be false too when I upgrade to the new version. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, sorry I missed this.
Should we add a manual override here ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we programmatically migrate the current setting value to a new field? For example, if autocomplete.enabled is set to false, suggestions.mode should be manually updated to off. Also, let's create an issue to follow up and remove this code later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

autocompleteLanguages: config.get(CONFIG_KEY.autocompleteLanguages, {
'*': true,
}),
Expand Down Expand Up @@ -119,7 +125,7 @@ export function getConfiguration(
experimentalTracing: getHiddenSetting('experimental.tracing', false),

experimentalSupercompletions: getHiddenSetting('experimental.supercompletions', false),
experimentalAutoeditsEnabled: getHiddenSetting('experimental.autoedits.enabled', undefined),
experimentalAutoeditsEnabled: codyAutoSuggestionsMode === CodyAutoSuggestionMode.Autoedits,
experimentalAutoeditsConfigOverride: getHiddenSetting(
'experimental.autoedits.config.override',
undefined
Expand Down
45 changes: 21 additions & 24 deletions vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import { isReinstalling } from '../uninstall/reinstall'
import type { CommandResult } from './CommandResult'
import { showAccountMenu } from './auth/account-menu'
import { showSignInMenu, showSignOutMenu, tokenCallbackHandler } from './auth/auth'
import { AutoeditsProvider } from './autoedits/autoedits-provider'
import { createAutoEditsProvider } from './autoedits/create-autoedits-provider'
import { autoeditsOutputChannelLogger } from './autoedits/output-channel-logger'
import { registerAutoEditTestRenderCommand } from './autoedits/renderer/mock-renderer'
import type { MessageProviderOptions } from './chat/MessageProvider'
import { ChatsController, CodyChatEditorViewType } from './chat/chat-view/ChatsController'
Expand Down Expand Up @@ -707,30 +708,26 @@ function registerAutoEdits(chatClient: ChatClient, disposables: vscode.Disposabl
)
)
.pipe(
map(([config, authStatus, autoeditEnabled]) => {
if (shouldEnableExperimentalAutoedits(config, autoeditEnabled, authStatus)) {
const provider = new AutoeditsProvider(chatClient)
const completionRegistration =
vscode.languages.registerInlineCompletionItemProvider(
[{ scheme: 'file', language: '*' }, { notebookType: '*' }],
provider
)

// Command used to trigger autoedits manually via command palette and is also used by e2e test
vscode.commands.registerCommand(
'cody.command.autoedits-manual-trigger',
async () => {
await vscode.commands.executeCommand(
'editor.action.inlineSuggest.hide'
)
await vscode.commands.executeCommand(
'editor.action.inlineSuggest.trigger'
)
}
)
return vscode.Disposable.from(provider, completionRegistration)
distinctUntilChanged((a, b) => {
return (
isEqual(a[0].configuration, b[0].configuration) &&
isEqual(a[1], b[1]) &&
isEqual(a[2], b[2])
)
}),
Comment on lines +714 to +720
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭

Copy link
Contributor Author

@hitesh-1997 hitesh-1997 Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simply copied from registerAutocomplete anything wrong with this approach 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😭😭😭

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think there's a TODO to address it. We should look into it at some point.

switchMap(([config, authStatus, autoeditEnabled]) => {
if (!shouldEnableExperimentalAutoedits(config, autoeditEnabled, authStatus)) {
return NEVER
}
return []
return createAutoEditsProvider({
config,
authStatus,
chatClient,
})
}),
catchError(error => {
autoeditsOutputChannelLogger.logError('registerAutoedits', 'Error', error)
return NEVER
})
)
.subscribe({})
Expand Down
2 changes: 1 addition & 1 deletion vscode/src/testutils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ export const DEFAULT_VSCODE_SETTINGS = {
},
commandCodeLenses: false,
experimentalSupercompletions: false,
experimentalAutoeditsEnabled: undefined,
experimentalAutoeditsEnabled: false,
experimentalAutoeditsRendererTesting: false,
experimentalAutoeditsConfigOverride: undefined,
experimentalMinionAnthropicKey: undefined,
Expand Down
3 changes: 2 additions & 1 deletion vscode/test/e2e/auto-edits.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from '@playwright/test'
import type { Frame, Page } from '@playwright/test'
import { CodyAutoSuggestionMode } from '@sourcegraph/cody-shared'
import * as mockServer from '../fixtures/mock-server'
import { sidebarExplorer, sidebarSignin } from './common'
import {
Expand Down Expand Up @@ -66,7 +67,7 @@ const test = baseTest
.extend<DotcomUrlOverride>({ dotcomUrl: mockServer.SERVER_URL })
.extend<ExtraWorkspaceSettings>({
extraWorkspaceSettings: {
'cody.experimental.autoedits.enabled': true,
'cody.suggestions.mode': CodyAutoSuggestionMode.Autoedits,
'cody.experimental.autoedits.use-mock-responses': true,
},
})
Expand Down
4 changes: 2 additions & 2 deletions web/lib/agent/agent.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FILE_CONTEXT_MENTION_PROVIDER } from '@sourcegraph/cody-shared'
import { CodyAutoSuggestionMode, FILE_CONTEXT_MENTION_PROVIDER } from '@sourcegraph/cody-shared'
import type { ClientInfo, ServerInfo } from 'cody-ai/src/jsonrpc/agent-protocol'
import {
BrowserMessageReader,
Expand Down Expand Up @@ -86,7 +86,7 @@ export async function createAgentClient({
...(customHeaders ?? {}),
},
customConfiguration: {
'cody.autocomplete.enabled': false,
'cody.suggestions.mode': CodyAutoSuggestionMode.Off,
'cody.experimental.urlContext': true,
// Will be replaced with vite in build time
// @ts-ignore
Expand Down
Loading