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

Minor prompt improvements #17426

Merged
merged 2 commits into from
Mar 5, 2025
Merged

Minor prompt improvements #17426

merged 2 commits into from
Mar 5, 2025

Conversation

marekrjpolak
Copy link
Contributor

Description

Part of #16735 which could be merged beforehand as it does basically nothing.

@marekrjpolak marekrjpolak force-pushed the chore/minor-prompt-improvements branch from fac228f to 0280af3 Compare March 5, 2025 15:33
@mroz22 mroz22 added the no-project This label is used to specify that PR doesn't need to be added to a project label Mar 5, 2025
@mroz22 mroz22 marked this pull request as ready for review March 5, 2025 16:23
Copy link

coderabbitai bot commented Mar 5, 2025

Walkthrough

The changes refactor how event data is structured and passed in various modules. In several API methods (e.g., firmware upload and device wipe), the parameters for the device.emit calls have been consolidated into a single object containing nested properties instead of multiple separate arguments. In the core event handlers, array destructuring has been replaced with object destructuring for clearer parameter access. The DeviceEvents interface now specifies object types for event parameters, and error handling has been updated from using Error objects to strings, affecting both device action methods and prompt handling logic. Similar structural adjustments are applied in the HTTP and main window proxy modules, with type signatures simplified and a previously internal type exported for external use.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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 generate docstrings to generate docstrings for this 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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.

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

🧹 Nitpick comments (3)
packages/node-utils/src/http.ts (1)

78-83: The event type refactoring improves parameter structure clarity.

This change refactors the BaseEvents type from using function signatures to direct type annotations, which makes the event data structure more explicit. This is a positive simplification that aligns with similar changes across the codebase.

Consider replacing void with undefined for the event types that don't pass data. While void works, undefined is more semantically accurate when representing events that don't carry data:

type BaseEvents = {
    'server/listening': NonNullable<net.AddressInfo>;
-   'server/closing': void;
-   'server/closed': void;
+   'server/closing': undefined;
+   'server/closed': undefined;
    'server/error': string;
};

This addresses the static analysis warnings and follows TypeScript best practices for non-function type declarations.

🧰 Tools
🪛 Biome (1.9.4)

[error] 80-80: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)


[error] 81-81: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

packages/suite-desktop-core/src/libs/main-window-proxy.ts (1)

5-7: Improved type signature for better event handling

The event interface has been simplified from function signatures to direct type associations. This makes the interface cleaner while maintaining the same functionality.

While the implementation works as is, consider extending the TypedEmitter pattern by leveraging a more descriptive approach for event payloads:

interface MainWindowProxyEvents {
-   init: StrictBrowserWindow;
-   destroy: StrictBrowserWindow;
+   init: { window: StrictBrowserWindow };
+   destroy: { window: StrictBrowserWindow };
}

This would provide better semantic meaning and make it consistent with the object-based event pattern used elsewhere in the codebase.

packages/connect/src/device/Device.ts (1)

102-102: Consider using undefined instead of void for the event payload type

The static analyzer flagged this line because void is potentially confusing when used outside a return type or type parameter context.

- [DEVICE.PASSPHRASE_ON_DEVICE]: void;
+ [DEVICE.PASSPHRASE_ON_DEVICE]: undefined;

Using undefined here would be more semantically accurate for an event that doesn't require payload data.

🧰 Tools
🪛 Biome (1.9.4)

[error] 102-102: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9ceb675 and 0280af3.

📒 Files selected for processing (10)
  • packages/connect/src/api/firmware/uploadFirmware.ts (1 hunks)
  • packages/connect/src/api/wipeDevice.ts (1 hunks)
  • packages/connect/src/core/index.ts (5 hunks)
  • packages/connect/src/core/onCallFirmwareUpdate.ts (1 hunks)
  • packages/connect/src/device/Device.ts (4 hunks)
  • packages/connect/src/device/DeviceCommands.ts (1 hunks)
  • packages/connect/src/device/prompts.ts (3 hunks)
  • packages/node-utils/src/http.ts (1 hunks)
  • packages/suite-desktop-core/src/libs/main-window-proxy.ts (1 hunks)
  • packages/transport/src/transports/abstract.ts (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
packages/node-utils/src/http.ts

[error] 80-80: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)


[error] 81-81: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

packages/connect/src/device/Device.ts

[error] 102-102: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

🔇 Additional comments (31)
packages/node-utils/src/http.ts (5)

170-171: Event emission aligns with updated type definition.

The emission of the server/listening event properly passes the address object directly, which matches the updated type definition: 'server/listening': NonNullable<net.AddressInfo>.


183-184: Event emission aligns with updated type definition.

The emission of the server/closing event without arguments properly matches the updated type definition: 'server/closing': void.


190-191: Event emission aligns with updated type definition.

The emission of the server/closed event without arguments properly matches the updated type definition: 'server/closed': void.


282-284: Event emission aligns with updated type definition.

The emission of the server/error event with a string argument properly matches the updated type definition: 'server/error': string.


78-83:

❓ Verification inconclusive

Verify consistency with other event emitter usages.

This refactoring is part of a broader pattern to standardize event parameter handling across the codebase. Ensure this style is consistently applied in all other TypedEmitter implementations.


🏁 Script executed:

#!/bin/bash
# Description: Check for consistency in event type definitions

# Look for TypedEmitter usages across the codebase
echo "Searching for TypedEmitter usage patterns..."
rg --type typescript "extends TypedEmitter<" -A 10

# Look for event emission patterns to ensure they match the new type definitions
echo "Searching for event emission patterns..."
rg --type typescript "emit\('server/" -A 1 -B 1

Length of output: 365


Below is an updated verification script that uses globbing to target TypeScript files (i.e. *.ts) instead of relying on a built‑in file type. Please run this new script to re‑verify consistency across all implementations of TypedEmitter and event emission patterns:

#!/bin/bash
# Description: Re‑verify consistency for event emitter usage
# Using globbing to search all TypeScript files (*.ts)

echo "Searching for TypedEmitter usage patterns in .ts files..."
rg "extends TypedEmitter<" --glob "*.ts" -A 10

echo "Searching for event emission patterns for 'server/' events in .ts files..."
rg "emit\('server/" --glob "*.ts" -A 1 -B 1

Action: Please run the above script and manually verify that the usage and emission of server events align with the new event type definitions. Once confirmed, ensure that the standardized event handling strategy is applied consistently across the codebase.

🧰 Tools
🪛 Biome (1.9.4)

[error] 80-80: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)


[error] 81-81: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)

packages/transport/src/transports/abstract.ts (1)

59-72: Improved type visibility by exporting ReadWriteError

This change enhances code organization by explicitly exporting the ReadWriteError type, making it available for use in other modules. This is a good practice for increasing type reusability across the codebase.

packages/connect/src/core/onCallFirmwareUpdate.ts (1)

24-28: Enhanced event parameter destructuring

The change improves code readability by using object destructuring to directly access the payload.code property from the event parameters, which aligns with the new event emission structure implemented across the codebase.

packages/connect/src/api/firmware/uploadFirmware.ts (1)

13-13: Standardized event emission structure

The updated event emission now uses a consistent object structure with device and payload properties, which provides better organization and improves consistency with the event handling patterns used elsewhere in the codebase.

packages/connect/src/api/wipeDevice.ts (1)

35-38: Restructured event emission to use a standardized format

The change properly refactors the event emission to use a consistent object structure with device and payload properties. This provides better clarity and aligns with the event handling patterns used throughout the codebase.

packages/connect/src/device/DeviceCommands.ts (4)

430-430: LGTM: Standardized event payload structure

The DEVICE.BUTTON event emission has been refactored to follow a consistent object-based pattern with clear property names, improving readability and type safety.


437-447: Improved error handling pattern for PIN responses

The PIN handling logic has been refactored to a more readable early-return pattern that checks for success first before proceeding with the acknowledgment.

This pattern is more maintainable as it separates the error case from the success case cleanly, reducing nesting and making the code flow more logical.


451-460: LGTM: Consistent error handling pattern applied to passphrase responses

The passphrase handling follows the same improved error-handling pattern as the PIN responses, maintaining consistency across the codebase.


464-468: LGTM: Word request handling also updated for consistency

The word request handling has been updated to follow the same error-first pattern used in the other prompt handlers.

packages/connect/src/core/index.ts (5)

785-788: Improved event handler signature using destructuring

The handler now uses object destructuring to clearly extract properties from the DeviceEvents interface, making the code more explicit and type-safe.


824-826: LGTM: Consistent pattern for PIN event handling

The PIN handler now uses the same object destructuring pattern as the button handler, improving consistency across event handlers.


845-847: LGTM: Word handler follows the same improved pattern

Consistent use of object destructuring in the word handler aligns with the other event handlers.


872-874: LGTM: Passphrase handler adopts the same pattern

The passphrase handler now uses object destructuring, maintaining consistency with other event handlers.


900-902: LGTM: EmptyPassphrase handler updated for consistency

This handler also adopts the new pattern, ensuring all event handlers follow the same approach.

packages/connect/src/device/Device.ts (3)

87-105: Restructured event interface for improved type safety

The DeviceEvents interface has been updated to use object types instead of function signatures, providing clearer structure and better type checking for event handlers.

This change matches the implementation changes in the event emitters and handlers across the codebase, creating a more consistent approach to event handling.

🧰 Tools
🪛 Biome (1.9.4)

[error] 102-102: void is confusing outside a return type or a type parameter.

Unsafe fix: Use undefined instead.

(lint/suspicious/noConfusingVoidType)


170-170: Change in error type from Error to string

The cancelableAction property now accepts a string error instead of an Error object, simplifying error handling.

This change aligns with the updates in other parts of the codebase where string errors are being used consistently for simpler serialization and handling.


485-485: Updated error handling in interruptionFromUser method

The method now passes error.toString() instead of the Error object directly, matching the updated cancelableAction type.

This change ensures consistent error handling throughout the device interaction flow.

packages/connect/src/device/prompts.ts (10)

2-2: Good addition of specific import type.

Adding an explicit import for the ReadWriteError type improves the code's clarity and type safety. This type is used effectively in the new PromptReturnType union type.


8-13: Well-structured type definitions enhance code clarity.

The introduction of these type definitions significantly improves type safety and makes the code intentions clearer:

  • DeviceEventArgs properly strips the callback from event arguments
  • DeviceEventCallback correctly extracts callback types
  • PromptReturnType provides a comprehensive union type for all possible return scenarios

This approach provides better static type checking and makes the API contract more explicit.


15-15: Good type update for consistent error handling.

Changing PromptCallback to use string errors instead of Error objects aligns with the broader refactoring effort to standardize error handling across the codebase.


23-23: Good use of as const for literal type inference.

Adding as const to the success: false property ensures TypeScript correctly infers this as the literal false rather than a boolean, which improves type safety for discriminated unions.


38-38: Improved function signature with object destructuring.

Changing the function signature to use object destructuring instead of spread arguments makes the code more maintainable and the parameter structure more explicit. This is a great improvement for code readability and maintainability.


40-54: Enhanced error handling structure with detailed return types.

The updated Promise resolution provides more structured and informative error responses, including distinguishing between transport errors and other types of errors. This makes error handling more robust for consumers of this API.


74-74: String error message is consistent with updated error handling approach.

Using a string error message directly instead of creating an Error object aligns with the updated PromptCallback type and the broader refactoring effort for standardized error handling.


78-78: Consistent update of helper functions.

The helper functions have been properly updated to use the new object parameter structure when calling the prompt function. This ensures consistency throughout the codebase.

Also applies to: 80-81, 83-84


48-48:

❓ Verification inconclusive

Potential logic inconsistency in isTransportError determination.

There seems to be a potential inconsistency in how isTransportError is determined. In line 48, it's set to !response.success, but in line 53, it's hardcoded to true. Make sure this logic correctly represents your intent for categorizing errors.


🏁 Script executed:

#!/bin/bash
# Investigate the cancelPrompt function to understand its return value

# Look for the cancelPrompt implementation
echo "Looking at cancelPrompt implementation..."
rg -t typescript "cancelPrompt" --include "*.ts" -A 10

# Check how isTransportError is used elsewhere
echo "Checking how isTransportError is used elsewhere..."
rg -t typescript "isTransportError" --include "*.ts" -A 5

Length of output: 1200


Below is an updated review comment reflecting our findings:

Attention: Review and Confirm Error Handling Logic

It appears there's an inconsistency with how isTransportError is set. On line 48 the property is computed as !response.success, yet a few lines later (line 53) it’s hardcoded to true. Please verify that this override is intentional. If not, adjust the logic so that the error classification is consistent throughout this function.

  • File: packages/connect/src/device/prompts.ts
    • Line 48: isTransportError: !response.success
    • Line 53: isTransportError: true

69-70:

❓ Verification inconclusive

Consider addressing the TypeScript error.

The @ts-expect-error comment indicates a potential type mismatch that should be addressed rather than suppressed. This could lead to runtime errors if the actual types don't match expectations.


🏁 Script executed:

#!/bin/bash
# Check what the Device.emit method expects and what's being passed

# Find the Device class definition
echo "Looking for Device class definition..."
rg -t typescript "class Device" --include "*.ts" -A 5

# Find the emit method in the Device class
echo "Looking for emit method in Device class..."
rg -t typescript "emit\(" --include "*.ts" -A 5

# Look for other uses of the emit method with this event pattern
echo "Looking for similar emit method usage patterns..."
rg -t typescript "emit\(.*callback" --include "*.ts" -A 5

Length of output: 1782


Attention: Review and update type handling for device.emit usage.
The current use of @ts-expect-error at lines 69–70 in packages/connect/src/device/prompts.ts suppresses a TypeScript error that may indicate an actual type mismatch between what is passed ({ device, callback, ...rest }) and what the device.emit method expects. This suppression risks hiding potential runtime issues. Please verify the expected parameter types for device.emit and update the type definitions or the payload accordingly. If suppressing the error is unavoidable, document the rationale clearly.

Copy link
Contributor

@mroz22 mroz22 left a comment

Choose a reason for hiding this comment

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

:shipit:

@mroz22 mroz22 merged commit d36e3ee into develop Mar 5, 2025
73 of 75 checks passed
@mroz22 mroz22 deleted the chore/minor-prompt-improvements branch March 5, 2025 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-project This label is used to specify that PR doesn't need to be added to a project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants