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

Show progress while calculating AI code completion #14537

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 83 additions & 73 deletions packages/ai-code-completion/src/common/code-completion-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Agent, AgentSpecificVariables, CommunicationRecordingService, getTextOfResponse,
LanguageModelRegistry, LanguageModelRequest, LanguageModelRequirement, PromptService, PromptTemplate
} from '@theia/ai-core/lib/common';
import { generateUuid, ILogger } from '@theia/core';
import { generateUuid, ILogger, ProgressService } from '@theia/core';
import { inject, injectable, named } from '@theia/core/shared/inversify';
import * as monaco from '@theia/monaco-editor-core';

Expand All @@ -36,82 +36,89 @@ export class CodeCompletionAgentImpl implements CodeCompletionAgent {
context: monaco.languages.InlineCompletionContext,
token: monaco.CancellationToken
): Promise<monaco.languages.InlineCompletions | undefined> {
const languageModel =
await this.languageModelRegistry.selectLanguageModel({
agent: this.id,
...this.languageModelRequirements[0],
const progress = await this.progressService.showProgress(
martin-fleck-at marked this conversation as resolved.
Show resolved Hide resolved
{ text: 'Calculating AI code completion...', options: { location: 'window' } }
);
try {
const languageModel =
martin-fleck-at marked this conversation as resolved.
Show resolved Hide resolved
await this.languageModelRegistry.selectLanguageModel({
agent: this.id,
...this.languageModelRequirements[0],
});
if (!languageModel) {
this.logger.error(
'No language model found for code-completion-agent'
);
return undefined;
}

// Get text until the given position
const prefix = model.getValueInRange({
startLineNumber: 1,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column,
});
if (!languageModel) {
this.logger.error(
'No language model found for code-completion-agent'
);
return undefined;
}

// Get text until the given position
const prefix = model.getValueInRange({
startLineNumber: 1,
startColumn: 1,
endLineNumber: position.lineNumber,
endColumn: position.column,
});

// Get text after the given position
const suffix = model.getValueInRange({
startLineNumber: position.lineNumber,
startColumn: position.column,
endLineNumber: model.getLineCount(),
endColumn: model.getLineMaxColumn(model.getLineCount()),
});

const file = model.uri.toString(false);
const language = model.getLanguageId();

if (token.isCancellationRequested) {
return undefined;
}
const prompt = await this.promptService
.getPrompt('code-completion-prompt', { prefix, suffix, file, language })
.then(p => p?.text);
if (!prompt) {
this.logger.error('No prompt found for code-completion-agent');
return undefined;
}
// Get text after the given position
const suffix = model.getValueInRange({
startLineNumber: position.lineNumber,
startColumn: position.column,
endLineNumber: model.getLineCount(),
endColumn: model.getLineMaxColumn(model.getLineCount()),
});

// since we do not actually hold complete conversions, the request/response pair is considered a session
const sessionId = generateUuid();
const requestId = generateUuid();
const request: LanguageModelRequest = {
messages: [{ type: 'text', actor: 'user', query: prompt }],
};
if (token.isCancellationRequested) {
return undefined;
}
this.recordingService.recordRequest({
agentId: this.id,
sessionId,
requestId,
request: prompt,
});
const response = await languageModel.request(request, token);
if (token.isCancellationRequested) {
return undefined;
}
const completionText = await getTextOfResponse(response);
if (token.isCancellationRequested) {
return undefined;
const file = model.uri.toString(false);
const language = model.getLanguageId();

if (token.isCancellationRequested) {
return undefined;
}
const prompt = await this.promptService
.getPrompt('code-completion-prompt', { prefix, suffix, file, language })
.then(p => p?.text);
if (!prompt) {
this.logger.error('No prompt found for code-completion-agent');
return undefined;
}

// since we do not actually hold complete conversions, the request/response pair is considered a session
const sessionId = generateUuid();
const requestId = generateUuid();
const request: LanguageModelRequest = {
messages: [{ type: 'text', actor: 'user', query: prompt }],
};
if (token.isCancellationRequested) {
return undefined;
}
this.recordingService.recordRequest({
agentId: this.id,
sessionId,
requestId,
request: prompt,
});
const response = await languageModel.request(request, token);
if (token.isCancellationRequested) {
return undefined;
}
const completionText = await getTextOfResponse(response);
if (token.isCancellationRequested) {
return undefined;
}
this.recordingService.recordResponse({
agentId: this.id,
sessionId,
requestId,
response: completionText,
});

return {
items: [{ insertText: completionText }],
enableForwardStability: true,
};
} finally {
progress.cancel();
}
this.recordingService.recordResponse({
agentId: this.id,
sessionId,
requestId,
response: completionText,
});

return {
items: [{ insertText: completionText }],
enableForwardStability: true,
};
}

@inject(ILogger)
Expand All @@ -127,6 +134,9 @@ export class CodeCompletionAgentImpl implements CodeCompletionAgent {
@inject(CommunicationRecordingService)
protected recordingService: CommunicationRecordingService;

@inject(ProgressService)
protected progressService: ProgressService;

id = 'Code Completion';
name = 'Code Completion';
description =
Expand Down
Loading