Skip to content

Commit

Permalink
Better wait
Browse files Browse the repository at this point in the history
  • Loading branch information
RXminuS committed Oct 22, 2024
1 parent c0813ed commit 7409630
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 37 deletions.
5 changes: 4 additions & 1 deletion lib/shared/src/guardrails/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export class SourcegraphGuardrailsClient implements Guardrails {
public async searchAttribution(snippet: string): Promise<Attribution | Error> {
// Short-circuit attribution search if turned off in site config.
const clientConfig = await ClientConfigSingleton.getInstance().getConfig()
if (!clientConfig?.attributionEnabled) {
if (!clientConfig) {
return new Error("Couldn't get client config.")
}
if (!clientConfig.attributionEnabled) {
return new Error('Attribution search is turned off.')
}

Expand Down
15 changes: 8 additions & 7 deletions lib/shared/src/sourcegraph-api/clientConfig.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { type Observable, interval, map } from 'observable-fns'
import { Observable, interval, map } from 'observable-fns'
import semver from 'semver'
import { authStatus } from '../auth/authStatus'
import { editorWindowIsFocused } from '../editor/editorState'
import { logDebug, logError } from '../logger'
import {
NEVER,
debounceTime,
distinctUntilChanged,
filter,
firstValueFrom,
Expand Down Expand Up @@ -73,9 +72,11 @@ export class ClientConfigSingleton {
*/
public readonly changes: Observable<CodyClientConfig | undefined | typeof pendingOperation> =
authStatus.pipe(
debounceTime(0), // wait a tick for graphqlClient's auth to be updated
switchMapReplayOperation(authStatus =>
authStatus.authenticated
switchMapReplayOperation(authStatus => {
if (authStatus.pendingValidation) {
return NEVER
}
return authStatus.authenticated
? interval(ClientConfigSingleton.REFETCH_INTERVAL).pipe(
map(() => undefined),
// Don't update if the editor is in the background, to avoid network
Expand All @@ -86,8 +87,8 @@ export class ClientConfigSingleton {
startWith(undefined),
switchMap(() => promiseFactoryToObservable(signal => this.fetchConfig(signal)))
)
: NEVER
),
: Observable.of(undefined)
}),
map(value => (isError(value) ? undefined : value)),
distinctUntilChanged()
)
Expand Down
7 changes: 5 additions & 2 deletions vscode/src/commands/execute/ask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ export interface ExecuteChatArguments extends Omit<WebviewSubmitMessage, 'text'
*/
export const executeChat = async (args: ExecuteChatArguments): Promise<ChatSession | undefined> => {
const clientConfig = await ClientConfigSingleton.getInstance().getConfig()
if (!clientConfig) {
return undefined
}
const isCommand = Boolean(args.command)
if (
(!isCommand && !clientConfig?.chatEnabled) ||
(isCommand && !clientConfig?.customCommandsEnabled)
(!isCommand && !clientConfig.chatEnabled) ||
(isCommand && !clientConfig.customCommandsEnabled)
) {
void vscode.window.showErrorMessage(
'This feature has been disabled by your Sourcegraph site admin.'
Expand Down
7 changes: 6 additions & 1 deletion vscode/src/commands/services/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ export class CommandRunner implements vscode.Disposable {

// Conditions checks
const clientConfig = await ClientConfigSingleton.getInstance().getConfig()
if (!clientConfig?.customCommandsEnabled) {
if (!clientConfig) {
this.span.addEvent('Cancel request as clientConfig is not available yet')
this.span.end()
return
}
if (!clientConfig.customCommandsEnabled) {
const disabledMsg = 'This feature has been disabled by your Sourcegraph site admin.'
void vscode.window.showErrorMessage(disabledMsg)
this.span.end()
Expand Down
57 changes: 33 additions & 24 deletions vscode/src/completions/inline-completion-item-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,12 @@ export class InlineCompletionItemProvider

stageRecorder.record('preClientConfigCheck')
const clientConfig = await ClientConfigSingleton.getInstance().getConfig()

if (clientConfig && !clientConfig.autoCompleteEnabled) {
if (!clientConfig) {
const error = new Error('ClientConfigNotFound')
this.onError(error)
throw error
}
if (!clientConfig.autoCompleteEnabled) {
// If ConfigFeatures exists and autocomplete is disabled then raise
// the error banner for autocomplete config turned off
const error = new Error('AutocompleteConfigTurnedOff')
Expand Down Expand Up @@ -989,30 +993,35 @@ export class InlineCompletionItemProvider
)
return
}

if (error.message === 'AutocompleteConfigTurnedOff') {
const errorTitle = 'Cody Autocomplete Disabled by Site Admin'
// If there's already an existing error, don't add another one.
const hasAutocompleteDisabledBanner = this.config.statusBar.hasError(
e => e.errorType === 'AutoCompleteDisabledByAdmin'
)
if (hasAutocompleteDisabledBanner) {
return
//TODO: This error handling is a bit error-prone, either create a custom error class or create a enum
switch (error.message) {
case 'ClientConfigNotFound':
// do nothing
case 'AutocompleteConfigTurnedOff': {
const errorTitle = 'Cody Autocomplete Disabled by Site Admin'
// If there's already an existing error, don't add another one.
const hasAutocompleteDisabledBanner = this.config.statusBar.hasError(
e => e.errorType === 'AutoCompleteDisabledByAdmin'
)
if (hasAutocompleteDisabledBanner) {
return
}
let shown = false
this.config.statusBar.addError({
title: errorTitle,
description: 'Contact your Sourcegraph site admin to enable autocomplete',
errorType: 'AutoCompleteDisabledByAdmin',
removeAfterSelected: false,
onShow: () => {
if (shown) {
return
}
shown = true
},
})
}
let shown = false
this.config.statusBar.addError({
title: errorTitle,
description: 'Contact your Sourcegraph site admin to enable autocomplete',
errorType: 'AutoCompleteDisabledByAdmin',
removeAfterSelected: false,
onShow: () => {
if (shown) {
return
}
shown = true
},
})
}

// TODO(philipp-spiess): Bring back this code once we have fewer uncaught errors
//
// c.f. https://sourcegraph.slack.com/archives/C05AGQYD528/p1693471486690459
Expand Down
5 changes: 4 additions & 1 deletion vscode/src/edit/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ export class EditManager implements vscode.Disposable {
telemetryMetadata,
} = args
const clientConfig = await ClientConfigSingleton.getInstance().getConfig()
if (!clientConfig?.customCommandsEnabled) {
if (!clientConfig) {
return
}
if (!clientConfig.customCommandsEnabled) {
void vscode.window.showErrorMessage(
'This feature has been disabled by your Sourcegraph site admin.'
)
Expand Down
5 changes: 4 additions & 1 deletion vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,10 @@ async function registerCodyCommands(
args?: Partial<CodyCommandArgs>
): Promise<CommandResult | undefined> => {
const clientConfig = await ClientConfigSingleton.getInstance().getConfig()
if (!clientConfig?.customCommandsEnabled) {
if (!clientConfig) {
return undefined
}
if (!clientConfig.customCommandsEnabled) {
void vscode.window.showErrorMessage(
'This feature has been disabled by your Sourcegraph site admin.'
)
Expand Down

0 comments on commit 7409630

Please sign in to comment.