Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { Autowired } from '@opensumi/di';
import {
AINativeConfigService,
ClientAppContribution,
Key,
KeybindingContribution,
KeybindingRegistry,
KeybindingScope,
} from '@opensumi/ide-core-browser';
import {
AI_MULTI_LINE_COMPLETION_ACCEPT,
AI_MULTI_LINE_COMPLETION_DISCARD,
AI_CODE_EDITS_ACCEPT,
Comment thread
Ricbet marked this conversation as resolved.
Outdated
AI_CODE_EDITS_DISCARD,
AI_CODE_EDITS_TRIGGER,
} from '@opensumi/ide-core-browser/lib/ai-native/command';
import { MultiLineEditsIsVisible } from '@opensumi/ide-core-browser/lib/contextkey/ai-native';
import { CommandContribution, CommandRegistry, Domain } from '@opensumi/ide-core-common';
import { WorkbenchEditorService } from '@opensumi/ide-editor';
import { WorkbenchEditorServiceImpl } from '@opensumi/ide-editor/lib/browser/workbench-editor.service';
import { transaction } from '@opensumi/ide-monaco/lib/common/observable';

import { IntelligentCompletionsController } from './intelligent-completions.controller';

Expand All @@ -22,8 +25,11 @@ export class IntelligentCompletionsContribution implements KeybindingContributio
@Autowired(WorkbenchEditorService)
private readonly workbenchEditorService: WorkbenchEditorServiceImpl;

@Autowired(AINativeConfigService)
private readonly aiNativeConfigService: AINativeConfigService;

registerCommands(commands: CommandRegistry): void {
commands.registerCommand(AI_MULTI_LINE_COMPLETION_DISCARD, {
commands.registerCommand(AI_CODE_EDITS_DISCARD, {
execute: () => {
const editor = this.workbenchEditorService.currentCodeEditor;
if (editor) {
Expand All @@ -32,31 +38,49 @@ export class IntelligentCompletionsContribution implements KeybindingContributio
},
});

commands.registerCommand(AI_MULTI_LINE_COMPLETION_ACCEPT, {
commands.registerCommand(AI_CODE_EDITS_ACCEPT, {
execute: () => {
const editor = this.workbenchEditorService.currentCodeEditor;
if (editor) {
IntelligentCompletionsController.get(editor.monacoEditor)?.accept.get();
}
},
});

commands.registerCommand(AI_CODE_EDITS_TRIGGER, {
execute: () => {
const editor = this.workbenchEditorService.currentCodeEditor;
if (editor) {
transaction((tx) => {
IntelligentCompletionsController.get(editor.monacoEditor)?.trigger(tx);
});
}
},
});
}

registerKeybindings(keybindings: KeybindingRegistry): void {
const { codeEdits } = this.aiNativeConfigService;

keybindings.registerKeybinding({
command: AI_MULTI_LINE_COMPLETION_DISCARD.id,
command: AI_CODE_EDITS_DISCARD.id,
keybinding: Key.ESCAPE.code,
when: MultiLineEditsIsVisible.raw,
priority: 100,
});

keybindings.registerKeybinding(
{
command: AI_MULTI_LINE_COMPLETION_ACCEPT.id,
command: AI_CODE_EDITS_ACCEPT.id,
keybinding: Key.TAB.code,
when: MultiLineEditsIsVisible.raw,
},
KeybindingScope.USER,
);

keybindings.registerKeybinding({
command: AI_CODE_EDITS_TRIGGER.id,
keybinding: codeEdits.triggerKeybinding,
});
Comment thread
Ricbet marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import {
import { Emitter, ICodeEditor, ICursorPositionChangedEvent, IRange, ITextModel, Range } from '@opensumi/ide-monaco';
import {
IObservable,
IObservableSignal,
ISettableObservable,
ITransaction,
autorun,
autorunWithStoreHandleChanges,
derived,
observableSignal,
observableValue,
transaction,
} from '@opensumi/ide-monaco/lib/common/observable';
Expand Down Expand Up @@ -85,11 +88,13 @@ export class IntelligentCompletionsController extends BaseAIMonacoEditorControll
private aiNativeContextKey: AINativeContextKey;
private rewriteWidget: RewriteWidget | null;
private whenMultiLineEditsVisibleDisposable: Disposable;
private codeEditsTriggerSignal: IObservableSignal<void>;

public mount(): IDisposable {
this.handlerAlwaysVisiblePreference();

this.codeEditsResult = observableValue<CodeEditsResultValue | undefined>(this, undefined);
this.codeEditsTriggerSignal = observableSignal(this);

this.whenMultiLineEditsVisibleDisposable = new Disposable();
this.multiLineDecorationModel = new MultiLineDecorationModel(this.monacoEditor);
Expand Down Expand Up @@ -393,6 +398,10 @@ export class IntelligentCompletionsController extends BaseAIMonacoEditorControll
this.hide();
});

public trigger(tx: ITransaction): void {
this.codeEditsTriggerSignal.trigger(tx);
}

private registerFeature(monacoEditor: ICodeEditor): void {
this.featureDisposable.addDispose(
Event.any<any>(
Expand Down Expand Up @@ -432,16 +441,19 @@ export class IntelligentCompletionsController extends BaseAIMonacoEditorControll
autorunWithStoreHandleChanges(
{
createEmptyChangeSummary: () => ({}),
handleChange: (context, changeSummary) => {
handleChange: (context) => {
if (context.didChange(this.codeEditsSourceCollection.codeEditsContextBean)) {
// 如果上一次补全结果还在,则不重复请求
const isVisible = this.aiNativeContextKey.multiLineEditsIsVisible.get();
return !isVisible;
} else if (context.didChange(this.codeEditsTriggerSignal)) {
return true;
}
return false;
},
},
async (reader, _, store) => {
this.codeEditsTriggerSignal.read(reader);
const context = this.codeEditsSourceCollection.codeEditsContextBean.read(reader);

const provider = this.intelligentCompletionsRegistry.getCodeEditsProvider();
Expand Down
22 changes: 21 additions & 1 deletion packages/core-browser/src/ai-native/ai-config.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Autowired, Injectable } from '@opensumi/di';
import { IAINativeCapabilities, IAINativeConfig, IAINativeInlineChatConfig } from '@opensumi/ide-core-common';
import {
IAINativeCapabilities,
IAINativeCodeEditsConfig,
IAINativeConfig,
IAINativeInlineChatConfig,
} from '@opensumi/ide-core-common';

import { AILogoAvatar } from '../components/ai-native';
import { LayoutViewSizeConfig } from '../layout/constants';
Expand Down Expand Up @@ -28,6 +33,10 @@ const DEFAULT_INLINE_CHAT_CONFIG: Required<IAINativeInlineChatConfig> = {
logo: AILogoAvatar,
};

const DEFAULT_CODE_EDITS_CONFIG: Required<IAINativeCodeEditsConfig> = {
triggerKeybinding: 'alt+\\',
};

@Injectable()
export class AINativeConfigService implements IAINativeConfig {
@Autowired(AppConfig)
Expand All @@ -40,6 +49,7 @@ export class AINativeConfigService implements IAINativeConfig {

private internalCapabilities = DEFAULT_CAPABILITIES;
private internalInlineChat = DEFAULT_INLINE_CHAT_CONFIG;
private internalCodeEdits = DEFAULT_CODE_EDITS_CONFIG;

public get capabilities(): Required<IAINativeCapabilities> {
if (!this.aiModuleLoaded) {
Expand All @@ -65,6 +75,16 @@ export class AINativeConfigService implements IAINativeConfig {
return this.internalInlineChat;
}

public get codeEdits(): Required<IAINativeCodeEditsConfig> {
const { AINativeConfig } = this.appConfig;

if (AINativeConfig?.codeEdits) {
return { ...this.internalCodeEdits, ...AINativeConfig.codeEdits };
}

return this.internalCodeEdits;
}

setAINativeModuleLoaded(value: boolean): void {
this.aiModuleLoaded = value;
}
Expand Down
12 changes: 8 additions & 4 deletions packages/core-browser/src/ai-native/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ export const AI_CODE_ACTION = {
id: 'ai.code.action',
};

export const AI_MULTI_LINE_COMPLETION_DISCARD = {
id: 'ai.multiLine.completion.discard',
export const AI_CODE_EDITS_DISCARD = {
id: 'ai.codeEdits.discard',
};

export const AI_MULTI_LINE_COMPLETION_ACCEPT = {
id: 'ai.multiLine.completion.accept',
export const AI_CODE_EDITS_ACCEPT = {
id: 'ai.codeEdits.accept',
};

export const AI_CODE_EDITS_TRIGGER = {
id: 'ai.codeEdits.trigger',
};
11 changes: 11 additions & 0 deletions packages/core-common/src/types/ai-native/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ export interface IAINativeInlineChatConfig {
logo?: string | React.ReactNode | React.ComponentType<any>;
}

export interface IAINativeCodeEditsConfig {
/**
* 触发 code edits 的快捷键
*/
triggerKeybinding?: string;
}

export interface IAINativeConfig {
capabilities?: IAINativeCapabilities;
/**
Expand All @@ -91,6 +98,10 @@ export interface IAINativeConfig {
* inline chat 配置
*/
inlineChat?: IAINativeInlineChatConfig;
/**
* code edits 配置
*/
codeEdits?: IAINativeCodeEditsConfig;
}

export enum ECompletionType {
Expand Down