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

feat: support multi inline completion #3874

Merged
merged 12 commits into from
Aug 19, 2024
Merged

Conversation

Ricbet
Copy link
Member

@Ricbet Ricbet commented Jul 23, 2024

Types

  • 🎉 New Features

Background or solution

  • 新增 registerIntelligentCompletionFeature 模块 API

用法示例

registerIntelligentCompletionFeature(registry: IIntelligentCompletionsRegistry): void {
  registry.registerIntelligentCompletionProvider(async (editor, position, token) => {
    return {
      items: [
        {
          content: 'completion value',
          range: {
            startLineNumber: position.lineNumber,
            startColumn: 1,
            endLineNumber: position.lineNumber + 3,
            endColumn: model?.getLineMaxColumn(position.lineNumber + 3),
          },
        },
      ],
      enableMultiLine: true
    };
  });
}

当只返回 content 时为内敛补全,当存在 belowRadius 或 aboveRadius 时为多行补全

  • 根据返回的 content 内容以及光标位置决定是否显示补全内容
  • 重构旧的 inline completion 模块,将其补全的触发逻辑与 intelligent completion 的触发逻辑融合
  • 删除 IAIMiddleware API,其功能可以通过 registerIntelligentCompletionFeature API 代替
multi_line.mp4

Changelog

Summary by CodeRabbit

Summary by CodeRabbit

  • 新功能

    • 引入智能补全功能,提升代码编辑器的智能建议能力。
    • 添加内联装饰模型,直观展示代码变更。
    • 实现智能补全差异计算算法,增强文本比较功能。
    • 允许动态插入随机字符串,丰富用户编辑体验。
    • 支持多行补全的可见性管理,优化用户输入体验。
  • 文档

    • 新增接口和类型,增强智能补全的扩展性和灵活性。

@Ricbet Ricbet self-assigned this Jul 23, 2024
Copy link

railway-app bot commented Jul 23, 2024

This PR was not deployed automatically as @Ricbet does not have access to the Railway project.

In order to get automatic PR deploys, please add @Ricbet to the project inside the project settings page.

Copy link
Contributor

coderabbitai bot commented Jul 23, 2024

Walkthrough

此次更改显著增强了 AI Native 功能,特别是在编辑器中引入了智能补全特性。通过新注册的智能补全处理程序和相应的装饰模型,用户可以在编程时获得更智能、上下文相关的代码建议。同时,重构的控制流简化了贡献注册过程,提高了代码的可维护性和可读性,整体提升了用户体验。

Changes

文件 更改摘要
packages/ai-native/src/browser/... 引入智能补全功能,增强 AINativeBrowserContributionAIEditorContribution,添加私有成员和新处理程序;重构 registerFeature 方法。
packages/ai-native/src/browser/... 新增 MultiLineDecorationModel 类,管理和渲染代码编辑器中的多行内联装饰,改善用户体验。
packages/ai-native/src/browser/... 新增 MultiLineDiffComputer 类,实现智能补全的文本差异计算算法,增强文本对比能力。
packages/ai-native/src/browser/... 引入 IntelligentCompletionsRegistry 类,管理智能补全提供者的注册,提升系统的可扩展性。
packages/ai-native/src/browser/... 新增 IntelligentCompletionsHandler 类,通过智能补全处理程序增强 Monaco 编辑器的代码补全功能。
packages/ai-native/src/browser/... 新增接口 IIntelligentCompletionsResultIIntelligentCompletionItem,用于智能补全结果的结构化处理。
packages/core-common/src/types/... 新增常量 IntelligentCompletionsRegistryToken,扩展智能补全功能的服务注册。
packages/startup/entry/sample-... AINativeContribution 类中新增 registerIntelligentCompletionFeature 方法,支持随机字符串插入的智能补全功能。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Editor
    participant IntelligentCompletionsHandler
    participant MultiLineDiffComputer
    participant MultiLineDecorationModel

    User->>Editor: 输入代码
    Editor->>IntelligentCompletionsHandler: 更新游标位置
    IntelligentCompletionsHandler->>MultiLineDiffComputer: 计算差异
    MultiLineDiffComputer->>IntelligentCompletionsHandler: 返回差异结果
    IntelligentCompletionsHandler->>MultiLineDecorationModel: 更新内联装饰
    MultiLineDecorationModel->>Editor: 在编辑器中显示补全建议
Loading

Possibly related issues


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 6b35cae and b0dad76.

Files selected for processing (5)
  • packages/ai-native/src/browser/contrib/inline-completions/inline-completions.handler.ts (5 hunks)
  • packages/ai-native/src/browser/contrib/inline-completions/model/inlineCompletionRequestTask.ts (1 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts (1 hunks)
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.ts (1 hunks)
  • packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/ai-native/src/browser/contrib/inline-completions/model/inlineCompletionRequestTask.ts
  • packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.ts
Additional context used
Biome
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts

[error] 55-56: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

Additional comments not posted (19)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts (7)

45-63: 建议添加错误处理和日志记录。

fetchProvider 方法中缺少错误处理,可能导致未捕获的异常。建议在异步操作中添加错误处理和日志记录。

+  try {
    const provider = this.intelligentCompletionsRegistry.getProvider();
    if (!provider) {
      return;
    }

    const position = this.monacoEditor.getPosition()!;
    const intelligentCompletionModel = await provider(this.monacoEditor, position, bean, this.cancelIndicator.token);

    if (
      intelligentCompletionModel &&
      intelligentCompletionModel.enableMultiLine &&
      intelligentCompletionModel.items.length > 0
    ) {
      return this.applyInlineDecorations(intelligentCompletionModel);
    }

    return intelligentCompletionModel;
+  } catch (error) {
+    console.error('Error in fetchProvider:', error);
+  }
Tools
Biome

[error] 55-56: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


65-97: 建议添加错误处理和日志记录。

applyInlineDecorations 方法中缺少错误处理,可能导致未处理的异常。建议在操作中添加错误处理和日志记录。

+  try {
    const { items } = completionModel;

    const position = this.monacoEditor.getPosition()!;
    const model = this.monacoEditor.getModel();
    const { range, insertText } = items[0];

    // 如果只是开启了 enableMultiLine 而没有传递 range ,则不显示 multi line
    if (!range) {
      return completionModel;
    }

    const originalContent = model?.getValueInRange(range);

    const diffComputerResult = this.multiLineDiffComputer.diff(originalContent!, insertText.toString());

    if (diffComputerResult) {
      const inlineModifications = this.multiLineDecorationModel.applyInlineDecorations(
        this.monacoEditor,
        diffComputerResult,
        position.lineNumber,
        position,
      );

      if (inlineModifications) {
        this.aiNativeContextKey.multiLineCompletionsIsVisible.set(true);
        this.multiLineDecorationModel.updateLineModificationDecorations(inlineModifications);
      } else {
        this.aiNativeContextKey.multiLineCompletionsIsVisible.reset();
        this.multiLineDecorationModel.clearDecorations();
      }
    }
+  } catch (error) {
+    console.error('Error in applyInlineDecorations:', error);
+  }

99-103: 代码看起来不错!

hide 方法实现简单且有效。


105-120: 代码看起来不错!

accept 方法结构良好,逻辑清晰。


122-129: 代码看起来不错!

registerFeature 方法有效地初始化了编辑器和相关模型。


30-33: 代码看起来不错!

cancelToken 方法简单明了,功能实现有效。


20-130: 类结构良好!

IntelligentCompletionsHandler 类组织良好,使用了依赖注入,符合良好的编码实践。

Tools
Biome

[error] 55-56: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

packages/ai-native/src/browser/contrib/inline-completions/inline-completions.handler.ts (5)

Line range hint 34-73: 建议添加错误处理和日志记录。

registerInlineCompletionFeature 方法中缺少错误处理,建议在事件处理和异步操作中添加错误处理和日志记录。

+  try {
    const { monacoEditor } = editor;
    // 判断用户是否选择了一块区域或者移动光标 取消掉请补全求
    const selectionChange = () => {
      this.aiCompletionsService.hideStatusBarItem();
      const selection = monacoEditor.getSelection();
      if (!selection) {
        return;
      }

      // 判断是否选中区域
      if (selection.startLineNumber !== selection.endLineNumber || selection.startColumn !== selection.endColumn) {
        this.aiInlineCompletionsProvider.cancelRequest();
      }
    };

    const debouncedSelectionChange = debounce(selectionChange, 50, {
      maxWait: 200,
      leading: true,
      trailing: true,
    });

    this.disposables.push(
      this.eventBus.on(EditorSelectionChangeEvent, (e) => {
        if (e.payload.source === 'mouse') {
          debouncedSelectionChange();
        } else {
          debouncedSelectionChange.cancel();
          selectionChange();
        }
      }),
      monacoEditor.onDidChangeModelContent((e) => {
        const changes = e.changes;
        for (const change of changes) {
          if (change.text === '') {
            this.aiInlineCompletionsProvider.isDelEvent = true;
            this.aiInlineCompletionsProvider.cancelRequest();
          } else {
            this.aiInlineCompletionsProvider.isDelEvent = false;
          }
        }
      }),
      monacoEditor.onDidBlurEditorText(() => {
        this.commandService.executeCommand(AI_INLINE_COMPLETION_VISIBLE.id, false);
      }),
    );

    return this;
+  } catch (error) {
+    console.error('Error in registerInlineCompletionFeature:', error);
+  }

Line range hint 75-100: 代码看起来不错!

mountEditor 方法结构良好,逻辑清晰。


Line range hint 102-153: 优化条件检查。

doContribute 方法中,建议使用可选链来优化条件检查,以提高代码的可读性和安全性。

-        if (completionsResult && completionsResult.items.some((i) => isMultiLineCompletion(i))) {
+        if (completionsResult?.items.some((i) => isMultiLineCompletion(i))) {

Line range hint 22-154: 类结构良好!

InlineCompletionHandler 类组织良好,集成了智能补全功能,符合良好的编码实践。


Line range hint 1-21: 导入模块良好!

导入的模块组织良好,与功能相关。

packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts (7)

405-475: 改进错误处理:捕获所有错误并记录日志。

建议在 registerIntelligentCompletionFeature 方法中捕获所有错误并记录日志,以便更好地调试和维护代码。

if (error.name === 'AbortError') {
  return { items: [] };
}
+ this.logger.error('Error in intelligent completion provider', error);
throw error;

Line range hint 70-197: 代码看起来不错!

registerInlineChatFeature 方法有效地注册了多种操作的内联聊天功能。


Line range hint 199-287: 代码看起来不错!

registerChatFeature 方法有效地注册了聊天功能,包括欢迎消息和命令。


Line range hint 289-373: 改进错误处理:捕获所有错误并记录日志。

建议在 registerResolveConflictFeature 方法中捕获所有错误并记录日志,以便更好地调试和维护代码。

if (error.name === 'AbortError') {
  return new CancelResponse();
}
+ this.logger.error('Error in resolve conflict provider', error);
throw error;

Line range hint 375-402: 代码看起来不错!

registerRenameProvider 方法有效地注册了重命名建议提供者。


404-404: 代码看起来不错!

registerTerminalProvider 方法有效地注册了终端命令建议提供者。


Line range hint 1-68: 导入模块良好!

导入的模块组织良好,与功能相关。


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@opensumi opensumi bot added the 🎨 feature feature required label Jul 23, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range, codebase verification and nitpick comments (1)
packages/ai-native/src/browser/contrib/intelligent-completions/diff-computer.ts (1)

54-184: 方法 diff 已正确实现,但建议添加注释以提高理解。

该方法计算两个字符串之间的差异,逻辑复杂但正确。建议添加注释以提高代码的可读性和理解。

+    // Clone an element
     const cloneElement = (element: IElement): IElement => ({
       newPos: element.newPos,
       changeResult: [...element.changeResult],
     });
+    // Join content array into a single string
     const join = (content: string[]) => content.join(empty);
+    // Process elements to compute the final diff result
     const processElements = (changeResult: IResultWithCount[], modified: string[], original: string[]) => {
       const modifiedLength = changeResult.length;

Copy link

codecov bot commented Jul 23, 2024

Codecov Report

Attention: Patch coverage is 43.68071% with 254 lines in your changes missing coverage. Please review.

Project coverage is 54.88%. Comparing base (71469b4) to head (b0dad76).
Report is 1 commits behind head on main.

Files Patch % Lines
...e-completions/model/inlineCompletionRequestTask.ts 0.00% 95 Missing and 12 partials ⚠️
...ent-completions/intelligent-completions.handler.ts 0.00% 57 Missing and 6 partials ⚠️
...b/intelligent-completions/multi-line.decoration.ts 72.22% 33 Missing and 7 partials ⚠️
...line-completions/service/ai-completions.service.ts 0.00% 15 Missing and 2 partials ⚠️
...etions/intelligent-completions.feature.registry.ts 0.00% 7 Missing ⚠️
...ser/contrib/inline-completions/completeProvider.ts 0.00% 5 Missing ⚠️
...b/inline-completions/inline-completions.handler.ts 0.00% 5 Missing ⚠️
packages/ai-native/src/browser/index.ts 0.00% 3 Missing ⚠️
...r/contrib/intelligent-completions/diff-computer.ts 97.87% 2 Missing ⚠️
packages/core-browser/src/ai-native/command.ts 0.00% 2 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3874      +/-   ##
==========================================
+ Coverage   54.80%   54.88%   +0.07%     
==========================================
  Files        1561     1566       +5     
  Lines       95260    95564     +304     
  Branches    19533    19599      +66     
==========================================
+ Hits        52210    52453     +243     
- Misses      35737    35793      +56     
- Partials     7313     7318       +5     
Flag Coverage Δ
jsdom 50.34% <43.68%> (+0.09%) ⬆️
node 15.48% <0.22%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (3)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts (1)

87-121: 建议添加注释以提高可读性。

虽然 registerFeature 函数实现良好,但添加注释可以提高代码的可读性。

  public registerFeature(editor: IEditor): IDisposable {
    this.editor = editor;
    const { monacoEditor } = editor;

    this.multiLineDecorationModel = new MultiLineDecorationModel(monacoEditor);

    const stop = () => {
      this.cancelToken();
      this.multiLineDecorationModel.clearDecorations();
    };

    /**
     * 触发时机与 inline completion 保持一致
     */
    this.addDispose([
      monacoEditor.onDidType(() => {
        this.update();
      }),

      Event.any<any>(
        monacoEditor.onDidChangeModel,
        monacoEditor.onDidBlurEditorWidget,
      )(() => {
        stop();
      }),

      monacoEditor.onDidChangeCursorSelection((event: ICursorSelectionChangedEvent) => {
        if (event.reason === CursorChangeReason.Explicit || event.source === TextEditorSelectionSource.PROGRAMMATIC) {
          stop();
        }
      }),
    ]);

    return this;
  }
packages/ai-native/src/browser/contrib/intelligent-completions/multi-line.decoration.ts (1)

95-316: 建议添加注释以提高可读性。

虽然这些函数实现良好,但添加注释可以提高代码的可读性。

  private processLineModifications(
    waitAddModificationsLines: IModificationsInline[],
    eol: string,
    previous: IDiffChangeResult,
    next?: IDiffChangeResult,
  ) {
    const lines = this.combineContinuousMods(waitAddModificationsLines);
    const len = lines.length;

    const fullLineMods: string[] = [];
    const inlineMods: IProcessModificationsInline[] = [];

    if (len === 0) {
      return {
        fullLineMods,
        inlineMods,
      };
    }

    const firstLine = lines[0];
    const lastLine = lines[len - 1];

    if (len === 1) {
      if (!isUndefined(previous) && previous.value !== eol) {
        inlineMods.push({
          status: EProcessStatus.beginning,
          newValue: previous.value + firstLine,
          oldValue: previous.value,
        });
      } else if (!isUndefined(next) && next.value !== eol) {
        inlineMods.push({
          status: EProcessStatus.end,
          newValue: lastLine + next.value,
          oldValue: next.value,
        });
      } else {
        fullLineMods.push(firstLine);
      }

      return {
        fullLineMods,
        inlineMods,
      };
    }

    if (isUndefined(previous) || previous.value === eol) {
      fullLineMods.push(firstLine);
    } else {
      inlineMods.push({
        status: EProcessStatus.beginning,
        newValue: previous.value + firstLine,
        oldValue: previous.value,
      });
    }

    if (len > 2) {
      const middleLines = lines.slice(1, -1);
      for (const line of middleLines) {
        fullLineMods.push(line);
      }
    }

    if (isUndefined(next) || next.value === eol) {
      fullLineMods.push(lastLine);
    } else {
      inlineMods.push({
        status: EProcessStatus.end,
        newValue: lastLine + next.value,
        oldValue: next.value,
      });
    }

    return {
      fullLineMods,
      inlineMods,
    };
  }

  public clearDecorations(): void {
    this.ghostTextDecorations.clear();
  }

  public updateLineModificationDecorations(modifications: IModificationsInline[]) {
    if (modifications.length === 0) {
      this.clearDecorations();
      return;
    }

    const decorations: IModelDeltaDecoration[] = modifications.map((modification) => {
      let content: string;

      if (modification.newValue.startsWith(modification.oldValue)) {
        content = modification.newValue.slice(modification.oldValue.length);
      } else {
        const oldValueIndex = modification.newValue.indexOf(modification.oldValue);
        content = oldValueIndex !== -1 ? modification.newValue.slice(0, oldValueIndex) : modification.newValue;
      }

      return {
        range: Range.fromPositions(new Position(modification.lineNumber!, modification.column!)),
        options: {
          description: GHOST_TEXT,
          showIfCollapsed: true,
          after: {
            content,
            inlineClassName: GHOST_TEXT_DESCRIPTION,
          },
        },
      };
    });

    this.ghostTextDecorations.set(decorations);
  }

  public applyInlineDecorations(
    editor: ICodeEditor,
    changes: IDiffChangeResult[],
    startLine: number,
    cursorPosition: IPosition,
  ): IModificationsInline[] | undefined {
    startLine = Math.max(startLine - 1, 0);

    const model = editor.getModel();
    if (!model) {
      return;
    }

    const eol = model.getEOL();

    changes = this.splitDiffChanges(changes, eol);
    changes.unshift({
      value: eol,
      added: undefined,
      removed: undefined,
    });

    const currentLineText = model.getLineContent(cursorPosition.lineNumber);
    const resultModifications: IModificationsInline[] = [];

    let lastChange: IDiffChangeResult;
    let waitAddModificationsLines: IModificationsInline[] = [];
    let columnNumber = 1;

    const processChange = (change: IDiffChangeResult | undefined) => {
      const { fullLineMods, inlineMods } = this.processLineModifications(
        waitAddModificationsLines,
        eol,
        lastChange,
        change,
      );

      inlineMods.forEach((mod) =>
        resultModifications.push({
          lineNumber: mod.status === EProcessStatus.beginning ? startLine : startLine + 1,
          column: mod.status === EProcessStatus.beginning ? columnNumber : 1,
          newValue: mod.newValue,
          oldValue: mod.oldValue,
        }),
      );

      return {
        fullLineMods,
        inlineMods,
      };
    };

    let currentLineIndex = startLine;
    let previousValue = empty;
    let isEmptyLine = currentLineText.trim() === empty;

    for (const change of changes) {
      if (change.added) {
        const isEolLine = change.value === eol;

        if (isEolLine) {
          previousValue = empty;
        }

        waitAddModificationsLines.push({
          isEolLine,
          lineNumber: startLine,
          newValue: isEolLine ? empty : change.value,
          oldValue: isEolLine ? empty : previousValue,
        });
      } else {
        const { inlineMods } = processChange(change);

        if (startLine === cursorPosition.lineNumber && inlineMods.length > 0) {
          isEmptyLine = false;

          /**
           * 如果光标位置在首个 diff 结果的后面,则不显示多行补全
           */
          if (startLine < cursorPosition.lineNumber && columnNumber < cursorPosition.column) {
            return;
          }
        }

        lastChange = change;
        waitAddModificationsLines = [];

        // 如果 change 的值是 eol,则开启新的一行进行计算
        if (change.value === eol) {
          currentLineIndex++;
          columnNumber = 1;
          previousValue = empty;
        } else {
          startLine = Math.max(startLine, currentLineIndex);
          columnNumber += change.value.length;
          previousValue += change.value;
        }
      }
    }

    const { fullLineMods } = processChange(undefined);

    if (!isEmptyLine && startLine < cursorPosition.lineNumber && fullLineMods.length > 0) {
      return;
    }

    return resultModifications;
  }
packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts (1)

409-453: 建议添加注释以提高可读性。

虽然 registerIntelligentCompletionFeature 方法实现良好,但添加注释可以提高代码的可读性。

  registerIntelligentCompletionFeature(registry: IIntelligentCompletionsRegistry): void {
    registry.registerIntelligentCompletionProvider(async (editor, position, token) => {
      const model = editor.getModel()!;
      let value = model.getValueInRange({
        startLineNumber: position.lineNumber,
        startColumn: 1,
        endLineNumber: position.lineNumber + 3,
        endColumn: model?.getLineMaxColumn(position.lineNumber + 3),
      });

      /**
       * mock randown
       */
      const getRandomString = (length) => {
        const characters = 'opensumi';
        let result = '';
        for (let i = 0; i < length; i++) {
          result += characters.charAt(Math.floor(Math.random() * characters.length));
        }
        return result;
      };

      const insertRandomStrings = (originalString) => {
        const numberOfInserts = Math.floor(Math.random() * 3) + 1;
        let modifiedString = originalString;
        for (let i = 0; i < numberOfInserts; i++) {
          const randomString = getRandomString(Math.floor(Math.random() * 2) + 1);
          const position = Math.floor(Math.random() * (modifiedString.length + 1));
          modifiedString = modifiedString.slice(0, position) + randomString + modifiedString.slice(position);
        }
        return modifiedString;
      };

      return {
        items: [
          {
            // content: 'export class Personsss {\n  name: string;\n  ages: n__open__umber;\n}',
            content: insertRandomStrings(value),
            belowRadius

</blockquote></details>

</blockquote></details>



<!-- This is an auto-generated comment by CodeRabbit for review status -->

@Ricbet Ricbet force-pushed the feat/support-inline-completion-api branch from 1879580 to dd9f1b4 Compare July 29, 2024 06:41
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range, codebase verification and nitpick comments (3)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.contribution.ts (1)

1-13: 确保所有导入的模块和常量都被正确使用。

请确认所有导入的模块和常量在文件中都有使用,避免不必要的依赖。

packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.handler.ts (1)

1-18: 确保所有导入的模块和常量都被正确使用。

请确认所有导入的模块和常量在文件中都有使用,避免不必要的依赖。

packages/ai-native/src/browser/contrib/inline-completions/inline-completions.handler.ts (1)

Line range hint 1-13:
确保所有导入的模块和常量都被正确使用。

请确认所有导入的模块和常量在文件中都有使用,避免不必要的依赖。

@Ricbet Ricbet force-pushed the feat/support-inline-completion-api branch from dd9f1b4 to 8bd600c Compare July 31, 2024 03:19
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

@Ricbet Ricbet changed the title WIP: feat/support multi inline completion feat/support multi inline completion Jul 31, 2024
@Ricbet Ricbet changed the title feat/support multi inline completion feat: support multi inline completion Jul 31, 2024
@Ricbet Ricbet force-pushed the feat/support-inline-completion-api branch from d59ffcc to a052b60 Compare August 1, 2024 02:18
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
packages/core-node/src/ai-native/base-back.service.ts (1)

需要更新或移除引用 requestCompletionreportCompletion 的代码

BaseAIBackService 类中移除 requestCompletionreportCompletion 方法后,以下文件中仍然存在对这些方法的引用或声明,需要进行相应的更新或移除:

  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts

    • requestCompletion 方法引用
    • reportCompletion 方法引用
  • packages/core-common/src/types/ai-native/index.ts

    • requestCompletion 方法声明
    • reportCompletion 方法声明

请确保所有依赖这些方法的代码已经被更新或移除,以避免运行时错误。

Analysis chain

Line range hint 1-1:
移除方法 requestCompletionreportCompletion

BaseAIBackService 类中移除了 requestCompletionreportCompletion 方法,这意味着该服务不再处理 AI 补全请求和报告。这可能会影响依赖这些方法的组件或服务。

请确保所有依赖这些方法的代码已经被更新或移除。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if `requestCompletion` and `reportCompletion` methods are called in the codebase.

# Test: Search for the method usage. Expect: No occurrences of the removed methods.
rg --type ts -A 5 'requestCompletion|reportCompletion'

Length of output: 4013


Script:

#!/bin/bash
# Search for the implementation of `requestCompletion` and `reportCompletion` in the mentioned files
rg 'requestCompletion' packages/startup/entry/sample-modules/ai-native/ai.back.service.ts
rg 'reportCompletion' packages/startup/entry/sample-modules/ai-native/ai.back.service.ts

# Search for the implementation of `requestCompletion` and `reportCompletion` in the mentioned files
rg 'requestCompletion' packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts
rg 'reportCompletion' packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts

# Search for the implementation of `requestCompletion` and `reportCompletion` in the mentioned files
rg 'requestCompletion' packages/core-common/src/types/ai-native/index.ts
rg 'reportCompletion' packages/core-common/src/types/ai-native/index.ts

Length of output: 973

@Ricbet Ricbet force-pushed the feat/support-inline-completion-api branch from a052b60 to 1c33fef Compare August 2, 2024 06:26
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range, codebase verification and nitpick comments (4)
packages/core-node/src/ai-native/base-back.service.ts (1)

验证移除 reportCompletion 方法的影响

reportCompletion 方法在以下文件中有使用:

  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts
  • packages/core-common/src/types/ai-native/index.ts

请确保在移除该方法后,更新这些文件中的相关代码以保持代码库的功能完整性。

Analysis chain

Line range hint 1-1:
验证移除 reportCompletion 方法的影响

reportCompletion 方法的移除简化了类接口,但可能会影响依赖此方法的组件。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of removing `reportCompletion` method.

# Test: Search for the usage of `reportCompletion` method. Expect: No occurrences.
rg --type python -A 5 $'reportCompletion'

Length of output: 71


Script:

#!/bin/bash
# Description: Verify the impact of removing `reportCompletion` method.

# Test: Search for the usage of `reportCompletion` method. Expect: No occurrences.
rg --type typescript -A 5 $'reportCompletion'

Length of output: 79


Script:

#!/bin/bash
# Description: Verify the impact of removing `reportCompletion` method.

# Test: Search for the usage of `reportCompletion` method. Expect: No occurrences.
rg --type ts -A 5 $'reportCompletion'

Length of output: 1930

packages/startup/entry/sample-modules/ai-native/ai-native.contribution.ts (3)

419-421: 改进代码:取消控制器的使用

取消控制器的使用是一个好的实践,但可以考虑将其封装到一个单独的函数中,以提高代码的可读性和可维护性。


426-437: 改进注释:更详细地描述函数功能

当前注释过于简单,建议提供更详细的描述,以便其他开发人员更容易理解代码的意图。

- /**
- * mock randown
- */
+ /**
+ * 生成指定长度的随机字符串
+ * @param {number} length - 随机字符串的长度
+ * @returns {string} - 生成的随机字符串
+ */

449-457: 改进代码:封装异步操作

将异步操作封装到一个单独的函数中,以提高代码的可读性和可维护性。

+ const delay = (ms, signal) => new Promise((resolve, reject) => {
+   const timeout = setTimeout(resolve, ms);
+   signal.addEventListener('abort', () => {
+     clearTimeout(timeout);
+     reject(new DOMException('Aborted', 'AbortError'));
+   });
+ });

try {
  await delay(1000, signal);

@Ricbet Ricbet force-pushed the feat/support-inline-completion-api branch from 1c33fef to 3022944 Compare August 13, 2024 09:51
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

Outside diff range, codebase verification and nitpick comments (8)
packages/ai-native/src/browser/contrib/intelligent-completions/intelligent-completions.feature.registry.ts (1)

1-2: 考虑模块化导入。

如果 @opensumi/di@opensumi/ide-core-common 提供了更多的导出项,可以考虑只导入需要的模块以优化性能。

packages/core-node/src/ai-native/base-back.service.ts (1)

移除方法的影响需要验证

requestCompletionreportCompletion 方法在多个文件中仍有引用。请确保所有依赖这些方法的组件已根据新的架构进行更新。

  • packages/startup/entry/sample-modules/ai-native/ai.back.service.ts 中的 requestCompletion
  • packages/core-common/src/types/ai-native/index.ts 中的 requestCompletionreportCompletion
  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts 中的相关引用

请检查这些文件并更新依赖关系以适应方法的移除。

Analysis chain

Line range hint 10-10:
移除 requestCompletionreportCompletion 方法。

这些方法的移除可能会影响依赖它们的其他组件。请确保相关依赖已更新以适应新的架构。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of removing `requestCompletion` and `reportCompletion`.

# Test: Search for any usage of the removed methods. Expect: No occurrences.
rg --type ts 'requestCompletion|reportCompletion'

Length of output: 1007

packages/core-common/src/types/ai-native/index.ts (3)

96-98: 需要迁移到新接口 IIntelligentCompletionsResult

在多个文件中仍然发现了对已过时接口 IAICompletionResultModel 的引用。建议将这些引用迁移到新的接口 IIntelligentCompletionsResult

  • packages/core-common/src/types/ai-native/index.ts
  • packages/ai-native/src/browser/types.ts
  • packages/ai-native/src/browser/contrib/inline-completions/model/inlineCompletionRequestTask.ts
  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts

请确保更新所有相关代码以使用新的接口。

Analysis chain

标记为过时的接口

IAICompletionResultModel 接口已标记为过时,建议使用 IIntelligentCompletionsResult。确保所有相关代码都迁移到新的接口。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of deprecated `IAICompletionResultModel` interface.

# Test: Search for references to `IAICompletionResultModel`. Expect: No references or only in deprecated contexts.
rg --type typescript 'IAICompletionResultModel'

Length of output: 82


Script:

#!/bin/bash
# Description: Verify usage of deprecated `IAICompletionResultModel` interface.

# Test: Search for references to `IAICompletionResultModel`. Expect: No references or only in deprecated contexts.
rg 'IAICompletionResultModel'

Length of output: 1110


167-169: 发现对过时方法 requestCompletion 的引用

在代码库中仍然存在对 requestCompletion 方法的引用。请确保将这些引用更新为使用新的 registerIntelligentCompletionFeature API。

  • packages/startup/entry/sample-modules/ai-native/ai.back.service.ts: 定义了 requestCompletion 方法。
  • packages/core-common/src/types/ai-native/index.ts: 声明了 requestCompletion 方法。
  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts: 使用了 requestCompletion 方法。

请更新这些位置以使用新的 API。

Analysis chain

标记为过时的方法

requestCompletion 方法已标记为过时,建议使用 registerIntelligentCompletionFeature API。确保所有调用此方法的代码都更新为使用新的 API。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of deprecated `requestCompletion` method.

# Test: Search for references to `requestCompletion`. Expect: No references or only in deprecated contexts.
rg --type typescript 'requestCompletion'

Length of output: 75


Script:

#!/bin/bash
# Description: Verify usage of deprecated `requestCompletion` method.

# Test: Search for references to `requestCompletion`. Expect: No references or only in deprecated contexts.
rg --type ts 'requestCompletion'

Length of output: 576


174-176: reportCompletion 方法仍在使用

尽管 reportCompletion 方法已被标记为过时,但它仍在以下位置使用:

  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts: 在条件检查和方法调用中。

建议检查是否有计划中的替代方案或功能调整,以便在未来版本中移除对该方法的依赖。请提供替代方案或更新文档以指导开发者进行迁移。

Analysis chain

标记为过时的方法

reportCompletion 方法已标记为过时,但没有提供具体的替代方案。需要确认是否有计划中的替代方案或功能调整。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of deprecated `reportCompletion` method.

# Test: Search for references to `reportCompletion`. Expect: No references or only in deprecated contexts.
rg --type typescript 'reportCompletion'

Length of output: 74


Script:

#!/bin/bash
# Description: Verify usage of deprecated `reportCompletion` method.

# Test: Search for references to `reportCompletion`. Expect: No references or only in deprecated contexts.
rg --type ts 'reportCompletion'

Length of output: 443

packages/ai-native/src/browser/types.ts (1)

259-261: 过时类型仍在使用

IProvideInlineCompletionsSignature 类型仍然在 packages/ai-native/src/browser/types.ts 中定义并使用。请确保所有相关代码迁移到新的 registerIntelligentCompletionProvider API。

  • packages/ai-native/src/browser/types.ts: IProvideInlineCompletionsSignature 类型定义及使用
Analysis chain

标记为过时的类型

IProvideInlineCompletionsSignature 类型已标记为过时,建议使用 registerIntelligentCompletionProvider API。确保所有相关代码都迁移到新的 API。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of deprecated `IProvideInlineCompletionsSignature` type.

# Test: Search for references to `IProvideInlineCompletionsSignature`. Expect: No references or only in deprecated contexts.
rg --type typescript 'IProvideInlineCompletionsSignature'

Length of output: 92


Script:

#!/bin/bash
# Description: Verify usage of deprecated `IProvideInlineCompletionsSignature` type.

# Test: Search for references to `IProvideInlineCompletionsSignature`. Expect: No references or only in deprecated contexts.
rg 'IProvideInlineCompletionsSignature' --glob '*.ts' -A 3

Length of output: 523

packages/ai-native/src/browser/contrib/inline-completions/model/inlineCompletionRequestTask.ts (1)

186-264: 提高代码可读性:在 pushLineCompletions 方法中使用更具描述性的变量名

建议使用更具描述性的变量名来提高代码的可读性。例如,将 codeModel 改为 completionItem

- for (const codeModel of completeResult.items) {
-   const contentText = codeModel.insertText.toString();
+ for (const completionItem of completeResult.items) {
+   const contentText = completionItem.insertText.toString();
packages/ai-native/src/browser/contrib/intelligent-completions/multi-line.decoration.ts (1)

73-88: 重命名方法以提高可读性

建议将 combineContinuousMods 方法重命名为 combineContinuousModifications 以提高代码可读性。

- private combineContinuousMods(changes: IModificationsInline[]) {
+ private combineContinuousModifications(changes: IModificationsInline[]) {

@Ricbet Ricbet force-pushed the feat/support-inline-completion-api branch from 3022944 to c3829a2 Compare August 14, 2024 08:11
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
packages/core-node/src/ai-native/base-back.service.ts (1)

确认删除方法的影响

在代码库中发现了 requestCompletionreportCompletion 方法的使用。这些方法的移除可能会影响依赖它们的功能。请手动检查以下文件以确保没有未处理的依赖项或调用:

  • packages/startup/entry/sample-modules/ai-native/ai.back.service.ts
  • packages/core-common/src/types/ai-native/index.ts
  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts
Analysis chain

Line range hint 10-20:
确认删除方法的影响

requestCompletionreportCompletion 方法被移除,可能会影响依赖这些方法的组件或服务。请确保在代码库中没有未处理的依赖项或调用。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify no remaining dependencies on removed methods in the codebase.

# Test: Search for any usage of the removed methods. Expect: No occurrences.
rg --type ts 'requestCompletion|reportCompletion'

Length of output: 1007

@Ricbet Ricbet force-pushed the feat/support-inline-completion-api branch from c3829a2 to 6278f4d Compare August 15, 2024 06:15
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
packages/core-node/src/ai-native/base-back.service.ts (1)

移除方法的影响需要验证

requestCompletionreportCompletion 方法在以下文件中仍有引用,可能会影响代码库的功能:

  • packages/startup/entry/sample-modules/ai-native/ai.back.service.ts
  • packages/core-common/src/types/ai-native/index.ts
  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts

请确保这些方法的移除不会破坏相关功能,或者用新的实现替代它们。

Analysis chain

Line range hint 1-22: 请验证移除的方法对代码库的影响。

requestCompletionreportCompletion 方法的移除可能会影响依赖这些方法的其他组件。请确保在整个代码库中没有对这些方法的调用,或者已经用新的实现替代。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that removed methods are not called elsewhere in the codebase.

# Test: Search for any usage of the removed methods. Expect: No occurrences found.
rg --type typescript 'requestCompletion|reportCompletion'

Length of output: 94


Script:

#!/bin/bash
# Description: Verify that removed methods are not called elsewhere in the codebase.

# Test: Search for any usage of the removed methods in TypeScript files. Expect: No occurrences found.
rg 'requestCompletion|reportCompletion' --glob '*.ts'

Length of output: 1011

@Ricbet Ricbet force-pushed the feat/support-inline-completion-api branch from af9889f to 6b35cae Compare August 19, 2024 06:54
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range, codebase verification and nitpick comments (1)
packages/core-node/src/ai-native/base-back.service.ts (1)

确认移除方法的影响

在代码库中仍然存在对 requestCompletionreportCompletion 方法的引用。请确保更新或移除这些引用,以防止代码错误。

  • packages/startup/entry/sample-modules/ai-native/ai.back.service.ts
  • packages/core-common/src/types/ai-native/index.ts
  • packages/ai-native/src/browser/contrib/inline-completions/service/ai-completions.service.ts

请手动验证这些更改对代码库的影响。

Analysis chain

Line range hint 10-10: 确认移除方法的影响。

requestCompletionreportCompletion 方法的移除可能会影响依赖这些方法的其他组件。请确保在代码库中已更新相关调用。

Also applies to: 14-14

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the removal of `requestCompletion` and `reportCompletion` methods does not break the codebase.

# Test: Search for any remaining references to the removed methods.
rg --type typescript 'requestCompletion|reportCompletion'

Length of output: 94


Script:

#!/bin/bash
# Description: Verify that the removal of `requestCompletion` and `reportCompletion` methods does not break the codebase.

# Test: Search for any remaining references to the removed methods using the correct file extension for TypeScript.
rg 'requestCompletion|reportCompletion' --glob '*.ts'

Length of output: 1011

@Ricbet Ricbet merged commit b127cf9 into main Aug 19, 2024
13 checks passed
@Ricbet Ricbet deleted the feat/support-inline-completion-api branch August 19, 2024 09:27
Ricbet added a commit that referenced this pull request Aug 27, 2024
* chore:  implement intelligent completions api

* chore: implement diff algorithm

* feat: handle diff result decoration render

* chore: improve trigger

* chore: add tests

* feat: support accept & esc

* refactor: inline completion provider

* feat: add context bean

* fix: ci

* fix: ci

* feat: improve code

* chore: use enableMultiLine
@coderabbitai coderabbitai bot mentioned this pull request Oct 25, 2024
1 task
@coderabbitai coderabbitai bot mentioned this pull request Nov 6, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🎨 feature feature required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants