Skip to content

Commit

Permalink
chore: polish AI code completion
Browse files Browse the repository at this point in the history
- removes code completion item provider as it blocks the completion
  popup
- reworks the code completion prompt so that it can be easier
  customized
- catches abort errors in the Open AI Api which are triggered very
  often when using code completion. This avoids spamming the log with
  "non-errors".
  • Loading branch information
sdirix authored and planger committed Sep 19, 2024
1 parent b8ecd13 commit 5f6b21e
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { ILogger } from '@theia/core';
import { ContainerModule } from '@theia/core/shared/inversify';
import { CodeCompletionAgent, CodeCompletionAgentImpl } from '../common/code-completion-agent';
import { AICodeCompletionProvider } from './ai-code-completion-provider';
import { AIFrontendApplicationContribution } from './ai-code-frontend-application-contribution';
import { FrontendApplicationContribution, PreferenceContribution } from '@theia/core/lib/browser';
import { Agent } from '@theia/ai-core';
Expand All @@ -32,7 +31,6 @@ export default new ContainerModule(bind => {
bind(CodeCompletionAgentImpl).toSelf().inSingletonScope();
bind(CodeCompletionAgent).toService(CodeCompletionAgentImpl);
bind(Agent).toService(CodeCompletionAgentImpl);
bind(AICodeCompletionProvider).toSelf().inSingletonScope();
bind(AICodeInlineCompletionsProvider).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).to(AIFrontendApplicationContribution).inSingletonScope();
bind(PreferenceContribution).toConstantValue({ schema: AICodeCompletionPreferencesSchema });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,11 @@
import { PreferenceSchema } from '@theia/core/lib/browser/preferences/preference-contribution';
import { AI_CORE_PREFERENCES_TITLE } from '@theia/ai-core/lib/browser/ai-core-preferences';

export const PREF_AI_CODE_COMPLETION_ENABLE = 'ai-features.code-completion.enable';
export const PREF_AI_CODE_COMPLETION_PRECOMPUTE = 'ai-features.code-completion.precompute';
export const PREF_AI_INLINE_COMPLETION_ENABLE = 'ai-features.code-completion-inline.enable';

export const AICodeCompletionPreferencesSchema: PreferenceSchema = {
type: 'object',
properties: {
[PREF_AI_CODE_COMPLETION_ENABLE]: {
title: AI_CORE_PREFERENCES_TITLE,
type: 'boolean',
description: 'Enable AI completion items within any (Monaco) editor.',
default: false
},
[PREF_AI_CODE_COMPLETION_PRECOMPUTE]: {
title: AI_CORE_PREFERENCES_TITLE,
type: 'boolean',
description: 'Precompute AI completion items. This will improve completion previews, however it will trigger many more requests and will take longer to complete.',
default: false
},
[PREF_AI_INLINE_COMPLETION_ENABLE]: {
title: AI_CORE_PREFERENCES_TITLE,
type: 'boolean',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@
import * as monaco from '@theia/monaco-editor-core';

import { FrontendApplicationContribution, PreferenceService } from '@theia/core/lib/browser';
import { AICodeCompletionProvider } from './ai-code-completion-provider';
import { inject, injectable } from '@theia/core/shared/inversify';
import { AIActivationService } from '@theia/ai-core/lib/browser';
import { Disposable } from '@theia/core';
import { AICodeInlineCompletionsProvider } from './ai-code-inline-completion-provider';
import { PREF_AI_CODE_COMPLETION_ENABLE, PREF_AI_INLINE_COMPLETION_ENABLE } from './ai-code-completion-preference';
import { PREF_AI_INLINE_COMPLETION_ENABLE } from './ai-code-completion-preference';

@injectable()
export class AIFrontendApplicationContribution implements FrontendApplicationContribution {
@inject(AICodeCompletionProvider)
protected codeCompletionProvider: AICodeCompletionProvider;

@inject(AICodeInlineCompletionsProvider)
private inlineCodeCompletionProvider: AICodeInlineCompletionsProvider;

Expand All @@ -42,7 +38,6 @@ export class AIFrontendApplicationContribution implements FrontendApplicationCon

onDidInitializeLayout(): void {
this.preferenceService.ready.then(() => {
this.handlePreference(PREF_AI_CODE_COMPLETION_ENABLE, enable => this.handleCodeCompletions(enable));
this.handlePreference(PREF_AI_INLINE_COMPLETION_ENABLE, enable => this.handleInlineCompletions(enable));
});
}
Expand All @@ -63,10 +58,6 @@ export class AIFrontendApplicationContribution implements FrontendApplicationCon
});
}

protected handleCodeCompletions(enable: boolean): Disposable {
return enable ? monaco.languages.registerCompletionItemProvider({ scheme: 'file' }, this.codeCompletionProvider) : Disposable.NULL;
}

protected handleInlineCompletions(enable: boolean): Disposable {
return enable ? monaco.languages.registerInlineCompletionsProvider({ scheme: 'file' }, this.inlineCodeCompletionProvider) : Disposable.NULL;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,36 @@ import * as monaco from '@theia/monaco-editor-core';

import { inject, injectable } from '@theia/core/shared/inversify';
import { CodeCompletionAgent } from '../common/code-completion-agent';
import { CompletionTriggerKind } from '@theia/core/shared/vscode-languageserver-protocol';
import { AgentService } from '@theia/ai-core';

@injectable()
export class AICodeInlineCompletionsProvider implements monaco.languages.InlineCompletionsProvider {
export class AICodeInlineCompletionsProvider
implements monaco.languages.InlineCompletionsProvider {
@inject(CodeCompletionAgent)
protected readonly agent: CodeCompletionAgent;
@inject(AgentService)
private readonly agentService: AgentService;

async provideInlineCompletions(model: monaco.editor.ITextModel, position: monaco.Position,
context: monaco.languages.InlineCompletionContext, token: monaco.CancellationToken): Promise<monaco.languages.InlineCompletions | undefined> {
async provideInlineCompletions(
model: monaco.editor.ITextModel,
position: monaco.Position,
context: monaco.languages.InlineCompletionContext,
token: monaco.CancellationToken
): Promise<monaco.languages.InlineCompletions | undefined> {
if (!this.agentService.isEnabled(this.agent.id)) {
return;
return undefined;
}
if (this.agent.provideInlineCompletions) {
return this.agent.provideInlineCompletions(model, position, context, token);
}
// map from regular completion items
const items = await this.agent.provideCompletionItems(model, position, { ...context, triggerKind: CompletionTriggerKind.Invoked }, token);
return {
items: items?.suggestions.map(suggestion => ({ insertText: suggestion.insertText })) ?? []
};
return this.agent.provideInlineCompletions(
model,
position,
context,
token
);
}

freeInlineCompletions(completions: monaco.languages.InlineCompletions<monaco.languages.InlineCompletion>): void {
freeInlineCompletions(
completions: monaco.languages.InlineCompletions<monaco.languages.InlineCompletion>
): void {
// nothing to do
}
}
18 changes: 0 additions & 18 deletions packages/ai-code-completion/src/browser/index.ts

This file was deleted.

Loading

0 comments on commit 5f6b21e

Please sign in to comment.