-
Notifications
You must be signed in to change notification settings - Fork 17
fix: 修复部分接口action未被正确替换的问题 #601
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
base: main
Are you sure you want to change the base?
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit 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. WalkthroughAction strings are now normalized using Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: defaults Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ 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)
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. Comment |
审阅者指南(在小型 PR 中折叠)审阅者指南确保 WebSocket 和 HTTP 的 OneBot API 调用在发送请求、触发事件以及格式化错误之前,都一致地应用内部的 已格式化的 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
已格式化的 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
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
文件级变更
提示与命令与 Sourcery 交互
自定义你的使用体验访问你的 仪表盘 以:
获取帮助Original review guide in EnglishReviewer's guide (collapsed on small PRs)Reviewer's GuideEnsures 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 callsequenceDiagram
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
Sequence diagram for formatted OneBot HTTP API callsequenceDiagram
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
Class diagram for OneBotWsBase and OneBotHttp action formattingclassDiagram
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
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Summary of ChangesHello @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 模块中部分接口的 Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this 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>帮我变得更有用!请在每条评论上点 👍 或 👎,我会根据你的反馈改进评审质量。
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>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this 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 未被正确替换的问题。您通过在 http 和 ws 的 sendApi 方法中调用 _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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个改动无条件地移除了所有 action 的 nc_ 或 lgl_ 前缀。这可能会破坏特定实现(如 NapCat 或 Lagrange)的扩展 API 调用。
例如,当调用 nc_getGroupMsgHistory 时,它的 action 是 nc_getGroupMsgHistory。此改动会将其转换为 getGroupMsgHistory 再发送给服务器。但 nc_getGroupMsgHistory 的参数与标准 getGroupMsgHistory 不同(多了 reverse 参数)。服务器收到 getGroupMsgHistory 这个 action 时,可能无法正确处理这些参数,导致功能异常或调用失败。
建议重新考虑此处的 action 格式化逻辑。或许可以考虑只在需要时进行格式化,或者采用其他方式来兼容不同 OneBot 实现的 API。
packages/onebot/src/ws/base.ts
Outdated
| ): Promise<OneBotApi[T]['response']> { | ||
| const echo = (++this.echo).toString() | ||
| const request = JSON.stringify({ echo, action, params }) | ||
| const realAction = this._formatAction(action as string) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个改动无条件地移除了所有 action 的 nc_ 或 lgl_ 前缀。这可能会破坏特定实现(如 NapCat 或 Lagrange)的扩展 API 调用。
例如,当调用 nc_getGroupMsgHistory 时,它的 action 是 nc_getGroupMsgHistory。此改动会将其转换为 getGroupMsgHistory 再发送给服务器。但 nc_getGroupMsgHistory 的参数与标准 getGroupMsgHistory 不同(多了 reverse 参数)。服务器收到 getGroupMsgHistory 这个 action 时,可能无法正确处理这些参数,导致功能异常或调用失败。
建议重新考虑此处的 action 格式化逻辑。或许可以考虑只在需要时进行格式化,或者采用其他方式来兼容不同 OneBot 实现的 API。
There was a problem hiding this 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 stringcast 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. SinceT extends keyof OneBotApiresolves to a string literal union, remove the cast:const realAction = this._formatAction(action)The wire protocol change (sending
realActioninstead of rawaction) is intentional—_formatActionstrips 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
📒 Files selected for processing (2)
packages/onebot/src/http/http.tspackages/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_formatActionimplementation correctly handles all action types and the event emission is consistent across protocols.The
_formatActionmethod inOneBotCoreremoves 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.
|
你可以通过以下命令安装该版本: |
Summary by Sourcery
规范 OneBot 的 HTTP 和 WebSocket API 调用,在请求、错误报告和触发的事件中统一使用格式化后的动作名称。
Bug Fixes:
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:
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.