Skip to content

Conversation

@yusheng929
Copy link
Contributor

@yusheng929 yusheng929 commented Dec 22, 2025

Summary by Sourcery

规范 OneBot 的 HTTP 和 WebSocket API 调用,在请求、错误报告和触发的事件中统一使用格式化后的动作名称。

Bug Fixes:

  • 确保 WebSocket API 的请求、超时以及响应事件使用格式化后的动作名称,而不是原始动作字符串。
  • 确保 HTTP API 端点、错误处理和 SEND_API 事件使用格式化后的动作名称来替代原始动作名称。
Original summary in English

Summary by Sourcery

Normalize OneBot HTTP and WebSocket API calls to use the formatted action name consistently in requests, error reporting, and emitted events.

Bug Fixes:

  • Ensure WebSocket API requests, timeouts, and response events use the formatted action name instead of the raw action string.
  • Ensure HTTP API endpoints, error handling, and SEND_API events use the formatted action name in place of the raw action.

Summary by CodeRabbit

  • Bug Fixes
    • Standardized action string formatting across API and WebSocket communication. Request payloads, emitted events, timeout handling, and error messages now use the normalized action, improving consistency and clarity in logs and error reporting.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 22, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

Walkthrough

Action strings are now normalized using _formatAction in both HTTP and WebSocket code paths; the formatted action is used for request construction, payload replacement, event emission, and error messages instead of the original action.

Changes

Cohort / File(s) Summary
HTTP API normalization
packages/onebot/src/http/http.ts
sendApi now calls _formatAction and uses the formatted action to build the request host, emit events, and populate thrown API errors.
WebSocket API normalization
packages/onebot/src/ws/base.ts
Compute realAction = _formatAction(action) and replace the action in payloads; use realAction for timeout/error formatting and for SEND_API / RESPONSE event emission.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Verify _formatAction is deterministic and consistent across both modules.
  • Confirm all event emissions, error messages, timeouts, and request payloads reference the formatted action only.
  • Check for any remaining references to the original, unformatted action in edge/error paths and logs.

Poem

🐰 I hop through code, polish each action bright,

I format the strings till they read just right.
In HTTP and WebSocket I tidy the trail,
No loose ends drifting, no bugs left to pale.
A tidy little hop — and the system's set sail.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the bug fix for action string replacement issues across OneBot interfaces, matching the core changes in both HTTP and WebSocket modules.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch ys-dev

📜 Recent review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4ef48d0 and dd37ed6.

📒 Files selected for processing (1)
  • packages/onebot/src/ws/base.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/onebot/src/ws/base.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Sourcery review
  • GitHub Check: publish-temp

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 22, 2025

审阅者指南(在小型 PR 中折叠)

审阅者指南

确保 WebSocket 和 HTTP 的 OneBot API 调用在发送请求、触发事件以及格式化错误之前,都一致地应用内部的 _formatAction 转换,使最终的 action 名称与下游使用方的预期保持一致。

已格式化的 OneBot WebSocket API 调用时序图

sequenceDiagram
  participant Caller
  participant OneBotWsBase
  participant Socket

  Caller->>OneBotWsBase: callApi(action, params, timeout)
  activate OneBotWsBase
  OneBotWsBase->>OneBotWsBase: _formatAction(action)
  Note right of OneBotWsBase: realAction computed from action
  OneBotWsBase->>OneBotWsBase: JSON.stringify({ echo, action: realAction, params })
  OneBotWsBase->>OneBotWsBase: setTimeout(reject with _formatApiError(realAction, request, timeout))
  OneBotWsBase-->>Caller: (Promise pending)

  OneBotWsBase->>OneBotWsBase: emit(OneBotEventKey.SEND_API, { echo, action: realAction, params, request })
  OneBotWsBase->>Socket: send(request)

  Socket-->>OneBotWsBase: response for echo
  OneBotWsBase->>OneBotWsBase: clearTimeout
  alt status === ok
    OneBotWsBase-->>Caller: resolve(data.data)
  else status !== ok
    OneBotWsBase->>OneBotWsBase: _formatApiError(realAction, request, data)
    OneBotWsBase-->>Caller: reject(error)
  end

  OneBotWsBase->>OneBotWsBase: emit(OneBotEventKey.RESPONSE, { echo, action: realAction, params, request, data })
  deactivate OneBotWsBase
Loading

已格式化的 OneBot HTTP API 调用时序图

sequenceDiagram
  participant Caller
  participant OneBotHttp
  participant HttpClient as http

  Caller->>OneBotHttp: callApi(action, params, timeout)
  activate OneBotHttp
  OneBotHttp->>OneBotHttp: _formatAction(action)
  Note right of OneBotHttp: realAction computed from action
  OneBotHttp->>OneBotHttp: host = httpHost + '/' + realAction
  OneBotHttp->>OneBotHttp: request = JSON.stringify(params)
  OneBotHttp->>OneBotHttp: emit(OneBotEventKey.SEND_API, { action: realAction, params, request, echo: '' })

  OneBotHttp->>HttpClient: post(host, request, { timeout, headers })
  HttpClient-->>OneBotHttp: data

  alt data.data.status === ok
    OneBotHttp-->>Caller: return data.data.data
  else data.data.status !== ok
    OneBotHttp->>OneBotHttp: _formatApiError(realAction, request, data.data)
    OneBotHttp-->>Caller: throw error
  end
  deactivate OneBotHttp
Loading

OneBotWsBase 与 OneBotHttp 动作格式化类图

classDiagram
  class OneBotCore {
    <<abstract>>
    - _options: OneBotOptions
    + _formatAction(action: string) string
    + _formatApiError(action: string, request: string, data: any) Error
  }

  class OneBotWsBase {
    <<abstract>>
    - _socket: WebSocket
    - echo: number
    + callApi<T>(action: keyof OneBotApi, params: OneBotApi[T][params], timeout: number) Promise<OneBotApi[T][response]>
  }

  class OneBotHttp {
    - _options: OneBotOptions
    + callApi<T>(action: keyof OneBotApi, params: OneBotApi[T][params], timeout: number) Promise<OneBotApi[T][response]>
  }

  class OneBotOptions {
    + httpHost: string
    + timeout: number
    + headers: any
  }

  OneBotCore <|-- OneBotWsBase
  OneBotCore <|-- OneBotHttp
  OneBotHttp o-- OneBotOptions
  OneBotWsBase o-- OneBotOptions

  class OneBotEventKey {
    <<enumeration>>
    SEND_API
    RESPONSE
  }

  OneBotWsBase ..> OneBotEventKey : emits
  OneBotHttp ..> OneBotEventKey : emits

  class OneBotApi {
  }

  OneBotWsBase ..> OneBotApi
  OneBotHttp ..> OneBotApi
Loading

文件级变更

Change Details Files
规范化并在所有 WebSocket 请求负载、事件和错误处理中使用格式化后的 action 名称。
  • 在发送请求前,将传入的 action 通过 _formatAction 处理后引入一个 realAction 变量。
  • 在序列化 WebSocket 请求体时使用 realAction 而不是原始的 action
  • 触发 SEND_APIRESPONSE 事件时,将 action 设置为 realAction,以便事件消费者看到的是实际生效的 action 名称。
  • 在通过 _formatApiError 构造超时和响应错误时使用 realAction,使错误上下文与真实请求保持一致。
packages/onebot/src/ws/base.ts
规范化并在所有 HTTP 请求 URL、事件和错误处理中使用格式化后的 action 名称。
  • 在构建 URL 之前,将传入的 action 通过 _formatAction 处理后引入一个 realAction 变量。
  • 使用 realAction 构建 HTTP host URL,使实际请求的端点与格式化后的 action 相匹配。
  • 触发 SEND_API 事件时,将 action 设置为 realAction,以便更好地追踪实际使用的端点。
  • 当 HTTP 调用失败时,在 _formatApiError 中使用 realAction,使错误信息能反映真正调用的 action。
packages/onebot/src/http/http.ts

提示与命令

与 Sourcery 交互

  • 触发新的代码审阅: 在 pull request 中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审阅评论。
  • 从审阅评论生成 GitHub issue: 回复某条审阅评论,请求 Sourcery 从该评论创建 issue。你也可以直接回复该评论 @sourcery-ai issue 来创建 issue。
  • 生成 pull request 标题: 在 pull request 标题的任意位置写上 @sourcery-ai,即可随时生成标题。你也可以在 pull request 中评论 @sourcery-ai title 来(重新)生成标题。
  • 生成 pull request 摘要: 在 pull request 正文任意位置写上 @sourcery-ai summary,即可在指定位置生成 PR 摘要。你也可以在 pull request 中评论 @sourcery-ai summary 来(重新)生成摘要。
  • 生成审阅者指南: 在 pull request 中评论 @sourcery-ai guide,即可随时(重新)生成审阅者指南。
  • 一次性解决所有 Sourcery 评论: 在 pull request 中评论 @sourcery-ai resolve,即可将所有 Sourcery 评论标记为已解决。适用于你已处理所有评论且不想再看到它们时。
  • 一次性撤销所有 Sourcery 审阅: 在 pull request 中评论 @sourcery-ai dismiss,即可撤销所有现有的 Sourcery 审阅。特别适用于你想从头开始新的审阅 —— 别忘了再评论 @sourcery-ai review 来触发新的审阅!

自定义你的使用体验

访问你的 仪表盘 以:

  • 启用或禁用诸如 Sourcery 自动生成的 pull request 摘要、审阅者指南等审阅功能。
  • 更改审阅语言。
  • 添加、移除或编辑自定义审阅指令。
  • 调整其他审阅设置。

获取帮助

Original review guide in English
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures that both WebSocket and HTTP OneBot API calls consistently apply the internal _formatAction transformation before sending requests, emitting events, and formatting errors, so that the effective action name matches what downstream consumers expect.

Sequence diagram for formatted OneBot WebSocket API call

sequenceDiagram
  participant Caller
  participant OneBotWsBase
  participant Socket

  Caller->>OneBotWsBase: callApi(action, params, timeout)
  activate OneBotWsBase
  OneBotWsBase->>OneBotWsBase: _formatAction(action)
  Note right of OneBotWsBase: realAction computed from action
  OneBotWsBase->>OneBotWsBase: JSON.stringify({ echo, action: realAction, params })
  OneBotWsBase->>OneBotWsBase: setTimeout(reject with _formatApiError(realAction, request, timeout))
  OneBotWsBase-->>Caller: (Promise pending)

  OneBotWsBase->>OneBotWsBase: emit(OneBotEventKey.SEND_API, { echo, action: realAction, params, request })
  OneBotWsBase->>Socket: send(request)

  Socket-->>OneBotWsBase: response for echo
  OneBotWsBase->>OneBotWsBase: clearTimeout
  alt status === ok
    OneBotWsBase-->>Caller: resolve(data.data)
  else status !== ok
    OneBotWsBase->>OneBotWsBase: _formatApiError(realAction, request, data)
    OneBotWsBase-->>Caller: reject(error)
  end

  OneBotWsBase->>OneBotWsBase: emit(OneBotEventKey.RESPONSE, { echo, action: realAction, params, request, data })
  deactivate OneBotWsBase
Loading

Sequence diagram for formatted OneBot HTTP API call

sequenceDiagram
  participant Caller
  participant OneBotHttp
  participant HttpClient as http

  Caller->>OneBotHttp: callApi(action, params, timeout)
  activate OneBotHttp
  OneBotHttp->>OneBotHttp: _formatAction(action)
  Note right of OneBotHttp: realAction computed from action
  OneBotHttp->>OneBotHttp: host = httpHost + '/' + realAction
  OneBotHttp->>OneBotHttp: request = JSON.stringify(params)
  OneBotHttp->>OneBotHttp: emit(OneBotEventKey.SEND_API, { action: realAction, params, request, echo: '' })

  OneBotHttp->>HttpClient: post(host, request, { timeout, headers })
  HttpClient-->>OneBotHttp: data

  alt data.data.status === ok
    OneBotHttp-->>Caller: return data.data.data
  else data.data.status !== ok
    OneBotHttp->>OneBotHttp: _formatApiError(realAction, request, data.data)
    OneBotHttp-->>Caller: throw error
  end
  deactivate OneBotHttp
Loading

Class diagram for OneBotWsBase and OneBotHttp action formatting

classDiagram
  class OneBotCore {
    <<abstract>>
    - _options: OneBotOptions
    + _formatAction(action: string) string
    + _formatApiError(action: string, request: string, data: any) Error
  }

  class OneBotWsBase {
    <<abstract>>
    - _socket: WebSocket
    - echo: number
    + callApi<T>(action: keyof OneBotApi, params: OneBotApi[T][params], timeout: number) Promise<OneBotApi[T][response]>
  }

  class OneBotHttp {
    - _options: OneBotOptions
    + callApi<T>(action: keyof OneBotApi, params: OneBotApi[T][params], timeout: number) Promise<OneBotApi[T][response]>
  }

  class OneBotOptions {
    + httpHost: string
    + timeout: number
    + headers: any
  }

  OneBotCore <|-- OneBotWsBase
  OneBotCore <|-- OneBotHttp
  OneBotHttp o-- OneBotOptions
  OneBotWsBase o-- OneBotOptions

  class OneBotEventKey {
    <<enumeration>>
    SEND_API
    RESPONSE
  }

  OneBotWsBase ..> OneBotEventKey : emits
  OneBotHttp ..> OneBotEventKey : emits

  class OneBotApi {
  }

  OneBotWsBase ..> OneBotApi
  OneBotHttp ..> OneBotApi
Loading

File-Level Changes

Change Details Files
Normalize and use the formatted action name for all WebSocket request payloads, events, and error handling.
  • Introduce a realAction variable by passing the incoming action through _formatAction before sending the request.
  • Use realAction instead of the original action in the serialized WebSocket request body.
  • Emit SEND_API and RESPONSE events with action set to realAction so event consumers see the effective action name.
  • Use realAction when constructing timeout and response errors via _formatApiError to keep error context consistent with the actual request.
packages/onebot/src/ws/base.ts
Normalize and use the formatted action name for all HTTP request URLs, events, and error handling.
  • Introduce a realAction variable by passing the incoming action through _formatAction before constructing the URL.
  • Build the HTTP host URL using realAction so the outgoing endpoint matches the formatted action.
  • Emit SEND_API events with action set to realAction for better traceability of the effective endpoint.
  • Use realAction in _formatApiError when HTTP calls fail so errors reflect the actual called action.
packages/onebot/src/http/http.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@gemini-code-assist
Copy link

Summary of Changes

Hello @yusheng929, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决 OneBot 模块中部分接口的 action 参数未被正确格式化的问题。通过引入一个中间变量来存储 _formatAction 方法处理后的 action 值,确保了在 HTTP 和 WebSocket 两种通信方式下,action 参数在构建请求、事件发射和错误处理等所有相关环节中都使用统一且正确的格式,从而修复了潜在的接口调用问题。

Highlights

  • 统一动作格式化: 引入 realAction 变量来存储 _formatAction(action) 的结果,确保 action 参数在 API 调用生命周期中被正确格式化。
  • HTTP 接口修复: 在 HTTP 请求处理中,使用格式化后的 realAction 来构建请求 URL、触发 SEND_API 事件以及格式化 API 错误。
  • WebSocket 接口修复: 在 WebSocket 请求处理中,使用格式化后的 realAction 来构建请求 JSON 负载、处理请求超时、触发 SEND_APIRESPONSE 事件,以及格式化 API 错误。

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我发现了 1 个问题

AI 代理提示词
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `packages/onebot/src/ws/base.ts:108` </location>
<code_context>
   ): Promise<OneBotApi[T]['response']> {
     const echo = (++this.echo).toString()
-    const request = JSON.stringify({ echo, action, params })
+    const realAction = this._formatAction(action as string)
+    const request = JSON.stringify({ echo, action: realAction, params })

</code_context>

<issue_to_address>
**suggestion:** 通过让 `_formatAction` 的类型与 `action` 参数对齐,避免使用 `action as string` 类型断言。

`action as string` 的断言说明该方法的 `action` 参数类型与 `_formatAction` 的函数签名不一致,这会削弱类型安全,并且在 `action` 实际上不是字符串时掩盖错误。优先考虑更新 `_formatAction`,使其接受与 `action` 相同的类型(例如使用泛型/联合类型),或者将该方法中 `action` 的类型收窄为 `string`,从而不再需要类型断言。

建议实现:

```typescript
    timeout: number = this._options.timeout
  ): Promise<OneBotApi[T]['response']> {
    const echo = (++this.echo).toString()
    const realAction = this._formatAction(action)
    const request = JSON.stringify({ echo, action: realAction, params })

```

```typescript
  private _formatAction<T extends string>(action: T): string {

```

如果当前 `_formatAction` 的函数签名与 `private _formatAction(action: string) { ... }` 不同(例如有显式返回类型或不同的访问修饰符),请调整 `SEARCH` 模式,使其与真实签名匹配,并且只替换参数和返回类型的部分,保留修饰符和实现主体。如果 `_formatAction` 本身已经可以正确处理字符串,则其实现无需更改。
</issue_to_address>

Sourcery 对开源项目永久免费——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进评审质量。
Original comment in English

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `packages/onebot/src/ws/base.ts:108` </location>
<code_context>
   ): Promise<OneBotApi[T]['response']> {
     const echo = (++this.echo).toString()
-    const request = JSON.stringify({ echo, action, params })
+    const realAction = this._formatAction(action as string)
+    const request = JSON.stringify({ echo, action: realAction, params })

</code_context>

<issue_to_address>
**suggestion:** Avoid the `action as string` cast by aligning `_formatAction`'s type with the `action` parameter.

The `action as string` cast indicates a type mismatch between this method’s `action` parameter and `_formatAction`’s signature, which weakens type safety and can mask errors if `action` isn’t actually a string. Prefer updating `_formatAction` to accept the same type as `action` (e.g., generics/union) or narrowing this method’s `action` type to `string` so the cast is unnecessary.

Suggested implementation:

```typescript
    timeout: number = this._options.timeout
  ): Promise<OneBotApi[T]['response']> {
    const echo = (++this.echo).toString()
    const realAction = this._formatAction(action)
    const request = JSON.stringify({ echo, action: realAction, params })

```

```typescript
  private _formatAction<T extends string>(action: T): string {

```

If the existing `_formatAction` signature differs from `private _formatAction(action: string) { ... }` (for example, it has an explicit return type or different access modifier), adjust the `SEARCH` pattern to match the actual signature and only replace the parameter and return type portion, preserving modifiers and implementation body. The implementation of `_formatAction` itself should not need to change if it already works on strings.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

本次 PR 旨在修复部分接口 action 未被正确替换的问题。您通过在 httpwssendApi 方法中调用 _formatAction 来标准化 action 名称。

然而,这个改动似乎对所有的 action 都无差别地移除了 nc_lgl_ 前缀。这可能会对特定 OneBot 实现(如 NapCat, Lagrange)的扩展 API 造成问题,因为这些 API 的 action 名称需要保留前缀才能被服务器正确识别。我在代码中留下了具体的评论,指出了这个问题可能带来的风险,并建议重新审视这个格式化逻辑。请查看具体评论。

timeout: number = this._options.timeout
): Promise<OneBotApi[T]['response']> {
const host = `${this._options.httpHost}/${action}`
const realAction = this._formatAction(action)

Choose a reason for hiding this comment

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

high

这个改动无条件地移除了所有 actionnc_lgl_ 前缀。这可能会破坏特定实现(如 NapCat 或 Lagrange)的扩展 API 调用。

例如,当调用 nc_getGroupMsgHistory 时,它的 actionnc_getGroupMsgHistory。此改动会将其转换为 getGroupMsgHistory 再发送给服务器。但 nc_getGroupMsgHistory 的参数与标准 getGroupMsgHistory 不同(多了 reverse 参数)。服务器收到 getGroupMsgHistory 这个 action 时,可能无法正确处理这些参数,导致功能异常或调用失败。

建议重新考虑此处的 action 格式化逻辑。或许可以考虑只在需要时进行格式化,或者采用其他方式来兼容不同 OneBot 实现的 API。

): Promise<OneBotApi[T]['response']> {
const echo = (++this.echo).toString()
const request = JSON.stringify({ echo, action, params })
const realAction = this._formatAction(action as string)

Choose a reason for hiding this comment

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

high

这个改动无条件地移除了所有 actionnc_lgl_ 前缀。这可能会破坏特定实现(如 NapCat 或 Lagrange)的扩展 API 调用。

例如,当调用 nc_getGroupMsgHistory 时,它的 actionnc_getGroupMsgHistory。此改动会将其转换为 getGroupMsgHistory 再发送给服务器。但 nc_getGroupMsgHistory 的参数与标准 getGroupMsgHistory 不同(多了 reverse 参数)。服务器收到 getGroupMsgHistory 这个 action 时,可能无法正确处理这些参数,导致功能异常或调用失败。

建议重新考虑此处的 action 格式化逻辑。或许可以考虑只在需要时进行格式化,或者采用其他方式来兼容不同 OneBot 实现的 API。

Copy link

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/onebot/src/ws/base.ts (1)

102-131: Remove the unnecessary type cast for consistency.

The action as string cast on line 108 is redundant. The HTTP implementation (packages/onebot/src/http/http.ts:130) calls _formatAction(action) without a cast, demonstrating that TypeScript correctly infers the type. Since T extends keyof OneBotApi resolves to a string literal union, remove the cast:

const realAction = this._formatAction(action)

The wire protocol change (sending realAction instead of raw action) is intentional—_formatAction strips implementation-specific prefixes (nc_, lgl_) to produce clean action names for the OneBot protocol, which is consistent with how the HTTP adapter operates.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b441c1d and 4ef48d0.

📒 Files selected for processing (2)
  • packages/onebot/src/http/http.ts
  • packages/onebot/src/ws/base.ts
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Sourcery review
  • GitHub Check: publish-temp
🔇 Additional comments (1)
packages/onebot/src/http/http.ts (1)

125-141: The _formatAction implementation correctly handles all action types and the event emission is consistent across protocols.

The _formatAction method in OneBotCore removes protocol-specific prefixes (nc_, lgl_, or empty string) from action names via a simple regex replacement. This approach handles all action types generically without special cases.

The SEND_API event now consistently emits realAction (the formatted action) across both HTTP and WebSocket implementations. The WebSocket implementation already follows this pattern, so the HTTP change aligns the protocols rather than introducing a breaking change.

@github-actions
Copy link
Contributor

你可以通过以下命令安装该版本:

pnpm add https://pkg.pr.new/KarinJS/Karin/node-karin@dd37ed6 -w

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants