Skip to content

feat: support register external ext api #3933

Merged
hacke2 merged 7 commits intomainfrom
feat/external-api
Aug 23, 2024
Merged

feat: support register external ext api #3933
hacke2 merged 7 commits intomainfrom
feat/external-api

Conversation

@hacke2
Copy link
Copy Markdown
Member

@hacke2 hacke2 commented Aug 13, 2024

Types

  • 🎉 New Features

Background or solution

resolve #3932

  1. register extension host api
class ExtHostBuildService implements IExtHostBuildService {
  private proxy: IMainThreadBuildService;
  constructor(protected rpcProtocol) {
    this.proxy = this.rpcProtocol.getProxy(MainThreadBuildIdentifier);
  }
  $hello = (id: string) => this.proxy.$world(id);
}

class ExtHostBuildApiExtender extends SumiApiExtender<IExtHostBuildService> {
  createRPCService = (): [identifier: ProxyIdentifier<IExtHostBuildService>, service: IExtHostBuildService] => [ExtHostBuildAPIIdentifier, new ExtHostBuildService(this.rpcProtocol)];
  createApiFactory = (service: IExtHostBuildService) => ({
    createBuildController: (id: string) => service.$hello(id),
  });
}

(async () => {
  await extProcessInit({
    sumiApiExtenders: {
      build: ExtHostBuildApiExtender,
    },
  });
})();
  1. register main thread contribution
@Domain(MainThreadExtenderContribution)
export class BrowserMainThreadExtenderContribution implements MainThreadExtenderContribution {
registerMainThreadExtender(registry: IMainThreadExtenderService<any>): void {
    registry.registerMainThreadExtender({
      identifier: MainThreadBuildIdentifier,
      serviceClass: MainThreadBuildService,
    });
  }
}
  1. implement main thread service
@Injectable({ multiple: true })
export class MainThreadBuildService extends Disposable implements IMainThreadBuildService {

  private proxy: IExtHostBuildService;

  constructor(private readonly rpcProtocol: IRPCProtocol) {
    super();
    this.proxy = this.rpcProtocol.getProxy<IExtHostBuildService>(ExtHostBuildAPIIdentifier);
  }
  $world = (id: string) => {
    console.log(id + ' world');
    return id + ' world';
  };

}

Changelog

  • feat: support register external ext api

Summary by CodeRabbit

Summary by CodeRabbit

  • 新功能

    • 引入了外部进程管理和配置接口,增强了应用的可扩展性。
    • 扩展了Sumi API,支持更动态的API创建和主线程交互。
    • 新增主线程扩展管理接口和服务,实现动态注册和管理。
  • 优化

    • 更新了API工厂函数以支持外部API,使扩展主机的API管理更灵活。
    • 通过新的配置管理策略改进了VSCode API和OpenSumi API的构建逻辑。
    • 改进了NodeExtProcessService类,增强了其功能和可访问性。
    • 更新了ExtensionLogger2类的配置管理,以更好地与扩展主机环境集成。
    • 测试文件中增加对ExtHostAppConfig的支持,增强了测试的灵活性。

@hacke2 hacke2 requested a review from life2015 August 13, 2024 08:19
@railway-app
Copy link
Copy Markdown

railway-app Bot commented Aug 13, 2024

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

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Aug 13, 2024

Walkthrough

此次变更主要集中在扩展插件 API 的集成与管理上,通过引入新的接口和模块,增强了外部进程和 API 的配置及管理能力。这些改动使得应用程序的可扩展性更强,便于与外部系统的集成,同时优化了日志记录功能,提升了整体架构的灵活性和可维护性。

Changes

文件 变更摘要
packages/extension/src/common/ext.process.ts 新增多个接口和符号以管理外部进程配置,增强日志服务和命令处理能力。
packages/extension/src/common/sumi/index.ts 扩展 API 接口,新增 SumiApiExtender 以支持动态 API 创建和 RPC 注册。
packages/extension/src/hosted/api/sumi/ext.host.api.impl.ts 修改 createAPIFactory 函数,新增 sumiApiExtenders 参数以集成外部 API。
packages/extension/src/hosted/ext.host.ts 修改配置管理逻辑,改用 ExtHostAppConfig 替代 AppConfig,提升 API 的扩展性。
packages/extension/src/browser/extension-node.service.ts 修改 NodeExtProcessService,增强对主线程扩展的支持,新增 API 工厂创建方法。
packages/extension/src/browser/index.ts 更新模块以集成新服务,增强主线程扩展的功能。
packages/extension/src/common/main.thread.extender.ts 新增接口和类以管理主线程扩展,有助于动态注册服务。

Assessment against linked issues

目标 已解决 解释
提供集成测扩展插件 API 的方式 ( #3932 )
增强外部 API 的配置与管理能力 ( #3932 )
允许插件与外部服务进行更灵活的交互 ( #3932 ) 变更虽然增加了 API 的灵活性,但具体的外部服务交互细节尚不明确。
简化日志配置管理 ( #3932 )
提高代码可维护性 ( #3932 )

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 2f834f9 and 0cd4034.

Files selected for processing (2)
  • packages/extension/tests/browser/main.thread.extensions.test.ts (2 hunks)
  • packages/extension/tests/hosted/api/vscode/ext.host.test.ts (2 hunks)
Additional comments not posted (4)
packages/extension/__tests__/hosted/api/vscode/ext.host.test.ts (2)

13-13: 导入 ExtHostAppConfig 看起来不错。

这个更改是为了新的配置设置,确保 ExtHostAppConfig 已正确定义和使用。


27-27: 更改配置 token 为 ExtHostAppConfig 看起来不错。

此更改与新的配置设置保持一致。请确保所有使用此 token 的依赖项都已相应更新。

运行以下脚本以验证使用此 token 的依赖项:

Verification successful

ExtHostAppConfig token 的使用已在代码库中一致更新。

搜索结果表明,ExtHostAppConfig token 在不同文件和上下文中使用一致。这表明从之前的 token 到 ExtHostAppConfig 的过渡已在整个代码库中实现,没有发现任何不一致或遗漏的更新。

  • 使用 ExtHostAppConfig 的文件包括:
    • packages/extension/__tests__/browser/main.thread.extensions.test.ts
    • packages/extension/__tests__/hosted/api/vscode/ext.host.test.ts
    • packages/extension/src/hosted/ext.process-base.ts
    • packages/extension/src/hosted/extension-log2.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all dependencies using the `ExtHostAppConfig` token.

# Test: Search for the token usage. Expect: All occurrences should use `ExtHostAppConfig`.
rg --type ts -A 5 $'token: ExtHostAppConfig'

Length of output: 2585

packages/extension/__tests__/browser/main.thread.extensions.test.ts (2)

32-32: 导入 ExtHostAppConfig 看起来不错。

这个更改是为了新的配置设置,确保 ExtHostAppConfig 已正确定义和使用。


73-76: 添加 ExtHostAppConfig token 看起来不错。

此添加是新的配置设置的一部分。请确保它在测试套件中已正确集成和使用。

运行以下脚本以验证 token 的使用:

Verification successful

ExtHostAppConfig token 已正确集成

ExtHostAppConfig token 在多个文件中使用,包括测试文件和源文件,表明它已正确集成到系统中。

  • packages/extension/__tests__/browser/main.thread.extensions.test.ts
  • packages/extension/__tests__/hosted/api/vscode/ext.host.test.ts
  • packages/extension/src/hosted/extension-log2.ts
  • packages/extension/src/hosted/ext.process-base.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all dependencies using the `ExtHostAppConfig` token.

# Test: Search for the token usage. Expect: All occurrences should use `ExtHostAppConfig`.
rg --type ts -A 5 $'token: ExtHostAppConfig'

Length of output: 2585


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 Aug 13, 2024
Copy link
Copy Markdown
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 (4)
packages/extension/src/hosted/extension-log2.ts (1)

23-24: 注释说明

注释解释了代码的设计选择,提供了有用的上下文信息。

考虑将注释翻译为英文,以便于非中文开发者理解。

- // 这块不是非常合理,插件进程引用了 Node 主进程的代码,先改为多例方式防止 AppConfig 找不到的问题
+ // This is not very reasonable, the plugin process references the Node main process code, temporarily changed to multi-instance to prevent AppConfig from not being found.
packages/logs-core/src/node/log-manager.ts (1)

29-29: 确保 appConfig 属性的安全访问

LogServiceManager 中,appConfig 的属性 logDirlogLevel 被直接使用,没有进行空值检查或提供默认值。建议添加相应的检查或默认值,以避免潜在的运行时错误。

  • logDir: 需要检查是否为 null 或 undefined。
  • logLevel: 需要检查是否为 null 或 undefined。

请确保在使用这些属性时进行适当的验证,以提高代码的健壮性。

Analysis chain

确保 appConfig 的安全使用

appConfig 现在是可选的,这意味着在使用 appConfig 的属性之前需要进行检查,以避免运行时错误。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify safe usage of `appConfig` in `LogServiceManager`.

# Test: Search for `appConfig` usage. Expect: Presence of null checks or default values.
rg --type typescript -A 3 $'this\.appConfig\.' packages/logs-core/src/node/log-manager.ts

Length of output: 125


Script:

#!/bin/bash
# Description: Verify safe usage of `appConfig` in `LogServiceManager`.

# Test: Search for `appConfig` usage. Expect: Presence of null checks or default values.
rg --type ts -A 3 $'this\.appConfig\.' packages/logs-core/src/node/log-manager.ts

Length of output: 383

packages/extension/src/hosted/api/sumi/ext.host.api.impl.ts (1)

62-71: 建议在使用 createApiFactory 之前进行检查

在处理 externalSumiExtApi 时,虽然已经对 identifier 进行了存在性检查,但 createApiFactory 未进行类似的检查。建议在调用 createApiFactory 之前,确保其存在以避免潜在的未定义错误。

  • 代码位置: packages/extension/src/hosted/api/sumi/ext.host.api.impl.ts, 行 62-71
Analysis chain

验证 externalSumiExtApi 的处理逻辑

在处理 externalSumiExtApi 时,确保 identifiercreateApiFactory 的存在性,以避免潜在的未定义错误。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify handling of `externalSumiExtApi` keys for presence of `identifier` and `createApiFactory`.

# Test: Search for `externalSumiExtApi` key usage. Expect: Checks for `identifier` and `createApiFactory`.
rg --type typescript -A 3 $'externalSumiExtApi\[key\]\.' packages/extension/src/hosted/api/sumi/ext.host.api.impl.ts

Length of output: 152


Script:

#!/bin/bash
# Description: Verify handling of `externalSumiExtApi` keys for presence of `identifier` and `createApiFactory`.

# Test: Search for `externalSumiExtApi` key usage and ensure checks for `identifier` and `createApiFactory`.
rg -A 3 'externalSumiExtApi\[key\]\.' packages/extension/src/hosted/api/sumi/ext.host.api.impl.ts

Length of output: 333

packages/extension/src/hosted/api/vscode/ext.host.command.ts (1)

15-15: 导入路径更改验证

IBuiltInCommand 的导入路径更改为 ../../../common/ext.process 是正确的,因为该接口在此位置定义。然而,tools/dev-tool/src/ext-host.ts 文件中仍然使用旧路径 @opensumi/ide-extension/lib/hosted/ext.process-base,这可能导致错误或不一致。请检查并更新此文件中的导入路径。

  • tools/dev-tool/src/ext-host.ts: 旧路径 @opensumi/ide-extension/lib/hosted/ext.process-base 需要更新。
Analysis chain

验证导入路径更改的影响。

IBuiltInCommand 的导入路径从 ../../ext.process-base 更改为 ../../../common/ext.process。请确保新路径正确,并验证此更改是否影响到文件中的其他功能。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of the import path change for `IBuiltInCommand`.

# Test: Search for all usages of `IBuiltInCommand` in the repository to ensure the new path is correct.
rg --type ts 'IBuiltInCommand'

Length of output: 677

Comment thread packages/logs-core/src/node/log-manager.ts
@life2015
Copy link
Copy Markdown
Member

实现逻辑我看起来OK

这个单测挂了 packages/extension/tests/hosted/api/vscode/ext.host.test.ts

Copy link
Copy Markdown
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/extension/src/common/sumi/index.ts (1)

39-50: 设计良好但需注意文档。

SumiApiExtender类设计用于扩展API,使用了抽象类和可选方法。建议为createRPCServicecreateApiFactory方法添加详细的文档注释,以便开发者更好地理解其用途和使用方式。

  /**
   * create rpc service when main thread could call
   */
  createRPCService?: () => [identifier: ProxyIdentifier<T>, service: T];

  /**
   * api factory
   * can use rpc service to call main thread
   */
  createApiFactory: (service?: T) => any;

@life2015
Copy link
Copy Markdown
Member

是否要在 startup 里面加入一些简答示例代码?

@life2015
Copy link
Copy Markdown
Member

/next

@opensumi
Copy link
Copy Markdown
Contributor

opensumi Bot commented Aug 15, 2024

🎉 PR Next publish successful!

3.2.4-next-1723688727.0

@hacke2 hacke2 changed the title [WIP] feat: support register external ext api feat: support register external ext api Aug 20, 2024
@life2015
Copy link
Copy Markdown
Member

LGTM

life2015
life2015 previously approved these changes Aug 22, 2024
Copy link
Copy Markdown
Member

@life2015 life2015 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
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

Comment thread packages/extension/src/hosted/extension-log2.ts Outdated
bytemain
bytemain previously approved these changes Aug 22, 2024
@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 23, 2024

Codecov Report

Attention: Patch coverage is 38.98305% with 36 lines in your changes missing coverage. Please review.

Project coverage is 54.88%. Comparing base (62ae96c) to head (0cd4034).
Report is 2 commits behind head on main.

Files Patch % Lines
...es/extension/src/browser/extension-node.service.ts 20.00% 12 Missing ⚠️
...kages/extension/src/common/main.thread.extender.ts 35.71% 8 Missing and 1 partial ⚠️
...extension/src/hosted/api/sumi/ext.host.api.impl.ts 33.33% 7 Missing and 1 partial ⚠️
packages/extension/src/hosted/ext.process-base.ts 0.00% 3 Missing ⚠️
packages/extension/src/hosted/extension-log2.ts 0.00% 3 Missing ⚠️
packages/extension/src/common/sumi/index.ts 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3933      +/-   ##
==========================================
- Coverage   54.89%   54.88%   -0.01%     
==========================================
  Files        1566     1568       +2     
  Lines       95573    95632      +59     
  Branches    19602    19608       +6     
==========================================
+ Hits        52461    52488      +27     
- Misses      35794    35825      +31     
- Partials     7318     7319       +1     
Flag Coverage Δ
jsdom 50.35% <35.59%> (-0.01%) ⬇️
node 15.47% <3.38%> (-0.02%) ⬇️

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
Copy Markdown
Member

@life2015 life2015 left a comment

Choose a reason for hiding this comment

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

LGTM

@hacke2 hacke2 merged commit 2eb234b into main Aug 23, 2024
@hacke2 hacke2 deleted the feat/external-api branch August 23, 2024 06:16
Ricbet pushed a commit that referenced this pull request Aug 27, 2024
* refactor: replace AppConfig with ExtHostAppConfig

* feat: support register external api to exthost

* refactor: abstract external extension API

* feat: support reguster main thread extender

* fix: fix lint

* fix: missing ExtHostAppConfig for test
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.

[FEATURE] 提供一个集成测扩展插件 API 的方式

3 participants