diff --git a/packages/ai-native/src/browser/ai-core.contribution.ts b/packages/ai-native/src/browser/ai-core.contribution.ts index 371ca2f525..21faba52ec 100644 --- a/packages/ai-native/src/browser/ai-core.contribution.ts +++ b/packages/ai-native/src/browser/ai-core.contribution.ts @@ -365,6 +365,10 @@ export class AINativeBrowserContribution id: AINativeSettingSectionsId.CodeEditsLineChange, localized: 'preference.ai.native.codeEdits.lineChange', }, + { + id: AINativeSettingSectionsId.CodeEditsTyping, + localized: 'preference.ai.native.codeEdits.typing', + }, ], }); } diff --git a/packages/ai-native/src/browser/contrib/intelligent-completions/index.ts b/packages/ai-native/src/browser/contrib/intelligent-completions/index.ts index 214c4c86e7..44bd3cf4dc 100644 --- a/packages/ai-native/src/browser/contrib/intelligent-completions/index.ts +++ b/packages/ai-native/src/browser/contrib/intelligent-completions/index.ts @@ -1,5 +1,5 @@ import { Disposable, ECodeEditsSourceTyping } from '@opensumi/ide-core-common'; -import { IPosition, IRange, InlineCompletion } from '@opensumi/ide-monaco'; +import { IModelContentChangedEvent, IPosition, IRange, InlineCompletion } from '@opensumi/ide-monaco'; import type { ILineChangeData } from './source/line-change.source'; import type { ILinterErrorData } from './source/lint-error.source'; @@ -14,7 +14,8 @@ export interface IIntelligentCompletionsResult { export type ICodeEditsContextBean = | { typing: ECodeEditsSourceTyping.LinterErrors; position: IPosition; data: ILinterErrorData } - | { typing: ECodeEditsSourceTyping.LineChange; position: IPosition; data: ILineChangeData }; + | { typing: ECodeEditsSourceTyping.LineChange; position: IPosition; data: ILineChangeData } + | { typing: ECodeEditsSourceTyping.Typing; position: IPosition; data: IModelContentChangedEvent }; export interface ICodeEdit { /** diff --git a/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts b/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts index b84ea4d1d7..feb727cf9f 100644 --- a/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts +++ b/packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.controller.ts @@ -47,6 +47,7 @@ import { IntelligentCompletionsRegistry } from './intelligent-completions.featur import { CodeEditsSourceCollection } from './source/base'; import { LineChangeCodeEditsSource } from './source/line-change.source'; import { LintErrorCodeEditsSource } from './source/lint-error.source'; +import { TypingCodeEditsSource } from './source/typing.source'; import { CodeEditsResultValue } from './index'; @@ -95,7 +96,7 @@ export class IntelligentCompletionsController extends BaseAIMonacoEditorControll this.additionsDeletionsDecorationModel = new AdditionsDeletionsDecorationModel(this.monacoEditor); this.aiNativeContextKey = this.injector.get(AINativeContextKey, [this.monacoEditor.contextKeyService]); this.codeEditsSourceCollection = this.injector.get(CodeEditsSourceCollection, [ - [LintErrorCodeEditsSource, LineChangeCodeEditsSource], + [LintErrorCodeEditsSource, LineChangeCodeEditsSource, TypingCodeEditsSource], this.monacoEditor, ]); diff --git a/packages/ai-native/src/browser/contrib/intelligent-completions/source/typing.source.ts b/packages/ai-native/src/browser/contrib/intelligent-completions/source/typing.source.ts new file mode 100644 index 0000000000..547a906571 --- /dev/null +++ b/packages/ai-native/src/browser/contrib/intelligent-completions/source/typing.source.ts @@ -0,0 +1,36 @@ +import { Injectable } from '@opensumi/di'; +import { AINativeSettingSectionsId, ECodeEditsSourceTyping, IDisposable } from '@opensumi/ide-core-common'; +import { IModelContentChangedEvent, Position } from '@opensumi/ide-monaco'; + +import { BaseCodeEditsSource } from './base'; + +@Injectable({ multiple: true }) +export class TypingCodeEditsSource extends BaseCodeEditsSource { + public priority = 0; + + public mount(): IDisposable { + this.addDispose( + this.monacoEditor.onDidChangeModelContent((event: IModelContentChangedEvent) => { + const position = this.monacoEditor.getPosition(); + if (position) { + this.doTrigger(position, event); + } + }), + ); + return this; + } + + protected async doTrigger(position: Position, data: IModelContentChangedEvent) { + const isTypingEnabled = this.preferenceService.getValid(AINativeSettingSectionsId.CodeEditsTyping, false); + + if (!isTypingEnabled || !this.model) { + return; + } + + this.setBean({ + typing: ECodeEditsSourceTyping.Typing, + position, + data, + }); + } +} diff --git a/packages/ai-native/src/browser/preferences/schema.ts b/packages/ai-native/src/browser/preferences/schema.ts index d41812f9ee..17704b99d4 100644 --- a/packages/ai-native/src/browser/preferences/schema.ts +++ b/packages/ai-native/src/browser/preferences/schema.ts @@ -58,5 +58,9 @@ export const aiNativePreferenceSchema: PreferenceSchema = { type: 'boolean', default: false, }, + [AINativeSettingSectionsId.CodeEditsTyping]: { + type: 'boolean', + default: false, + }, }, }; diff --git a/packages/core-common/src/settings/ai-native.ts b/packages/core-common/src/settings/ai-native.ts index a034cd9f52..0eb624b40b 100644 --- a/packages/core-common/src/settings/ai-native.ts +++ b/packages/core-common/src/settings/ai-native.ts @@ -22,6 +22,7 @@ export enum AINativeSettingSectionsId { */ CodeEditsLintErrors = 'ai.native.codeEdits.lintErrors', CodeEditsLineChange = 'ai.native.codeEdits.lineChange', + CodeEditsTyping = 'ai.native.codeEdits.typing', } export const AI_NATIVE_SETTING_GROUP_ID = 'AI-Native'; export const AI_NATIVE_SETTING_GROUP_TITLE = 'AI Native'; diff --git a/packages/core-common/src/types/ai-native/index.ts b/packages/core-common/src/types/ai-native/index.ts index 65aafb7494..2ff367ff7a 100644 --- a/packages/core-common/src/types/ai-native/index.ts +++ b/packages/core-common/src/types/ai-native/index.ts @@ -354,5 +354,6 @@ export interface IHistoryChatMessage extends IChatMessage { export enum ECodeEditsSourceTyping { LinterErrors = 'lint_errors', LineChange = 'line_change', + Typing = 'typing', } // ## Code Edits ends ## diff --git a/packages/i18n/src/common/en-US.lang.ts b/packages/i18n/src/common/en-US.lang.ts index 15a56e65bd..b146abe17c 100644 --- a/packages/i18n/src/common/en-US.lang.ts +++ b/packages/i18n/src/common/en-US.lang.ts @@ -1504,8 +1504,10 @@ export const localizationBundle = { 'preference.ai.native.intelligentCompletions.alwaysVisible': 'Whether to always show intelligent completions', 'preference.ai.native.codeEdits.title': 'Code Edits', - 'preference.ai.native.codeEdits.lintErrors': 'Whether to enable intelligent rewriting of Lint Errors', - 'preference.ai.native.codeEdits.lineChange': 'Whether to enable intelligent rewriting of Line Change', + 'preference.ai.native.codeEdits.lintErrors': 'Whether to trigger intelligent rewriting when Lint Error occurs', + 'preference.ai.native.codeEdits.lineChange': + 'Whether to trigger intelligent rewriting when the cursor line number changes', + 'preference.ai.native.codeEdits.typing': 'Whether to trigger intelligent rewriting when the content changes', // #endregion AI Native // #endregion merge editor diff --git a/packages/i18n/src/common/zh-CN.lang.ts b/packages/i18n/src/common/zh-CN.lang.ts index 38528ade5d..01f0c1ea45 100644 --- a/packages/i18n/src/common/zh-CN.lang.ts +++ b/packages/i18n/src/common/zh-CN.lang.ts @@ -1271,8 +1271,9 @@ export const localizationBundle = { 'preference.ai.native.intelligentCompletions.alwaysVisible': '是否总是展示智能补全', 'preference.ai.native.codeEdits.title': '智能改写', - 'preference.ai.native.codeEdits.lintErrors': '是否开启对 Lint Error 类型的智能改写', - 'preference.ai.native.codeEdits.lineChange': '是否开启对 Line Change 类型的智能改写', + 'preference.ai.native.codeEdits.lintErrors': '是否在发生 Lint Error 时触发智能改写', + 'preference.ai.native.codeEdits.lineChange': '是否在光标行号发生变化时触发智能改写', + 'preference.ai.native.codeEdits.typing': '是否在内容发生变化时触发智能改写', // #endregion AI Native 'webview.webviewTagUnavailable': '非 Electron 环境不支持 webview 标签,请使用 iframe 标签',