Skip to content

Comments

Replace callDriver with direct mail manager methods#1466

Merged
MrgSub merged 1 commit intostagingfrom
06-23-replace_calldriver
Jun 24, 2025
Merged

Replace callDriver with direct mail manager methods#1466
MrgSub merged 1 commit intostagingfrom
06-23-replace_calldriver

Conversation

@MrgSub
Copy link
Collaborator

@MrgSub MrgSub commented Jun 24, 2025

Refactor Mail Manager Methods in ZeroAgent

This PR refactors the ZeroAgent class by replacing the generic callDriver method with explicit, type-safe methods for each mail operation. Instead of dynamically calling driver methods through callDriver, the code now provides dedicated methods that directly access the driver functionality.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🎨 UI/UX improvement
  • ⚡ Performance improvement

Areas Affected

  • Email Integration (Gmail, IMAP, etc.)
  • API Endpoints

Testing Done

  • Manual testing performed

Checklist

  • I have performed a self-review of my code
  • My changes generate no new warnings
  • All tests pass locally

Additional Notes

This refactoring improves code readability and type safety by providing explicit methods for each mail operation rather than using a generic callDriver method. The implementation maintains the same functionality while making the code more maintainable and easier to understand.


By submitting this pull request, I confirm that my contribution is made under the terms of the project's license.

Summary by CodeRabbit

  • Refactor
    • Improved reliability and clarity of mail, label, and draft operations by switching to explicit method calls for all mail-related actions.
    • Enhanced error handling for mail operations when the mail driver is unavailable.
    • No changes to user-facing workflows or API request/response formats.

@MrgSub MrgSub marked this pull request as ready for review June 24, 2025 05:51
Copy link
Collaborator Author

MrgSub commented Jun 24, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@MrgSub MrgSub changed the title Replace callDriver Replace callDriver with direct mail manager methods Jun 24, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 24, 2025

Walkthrough

This change removes the generic callDriver method from the ZeroAgent class and introduces explicit asynchronous methods for each mail-related operation. All usages of callDriver in the TRPC route handlers are updated to use these new, direct method calls, resulting in clearer and more strongly-typed code.

Changes

File(s) Change Summary
apps/server/src/routes/chat.ts Removed callDriver from ZeroAgent; added explicit async methods for all mail operations, proxying to driver.
apps/server/src/trpc/routes/drafts.ts Replaced agent.callDriver invocations with direct calls to new explicit draft-related methods on agent.
apps/server/src/trpc/routes/label.ts Refactored to use direct label-related methods on agent instead of callDriver.
apps/server/src/trpc/routes/mail.ts Replaced all callDriver usages with direct calls to explicit mail-related methods on agent.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant TRPC_Router
    participant ZeroAgent
    participant MailManager

    Client->>TRPC_Router: API call (e.g., createDraft)
    TRPC_Router->>ZeroAgent: createDraft(input)
    ZeroAgent->>MailManager: createDraft(input)
    MailManager-->>ZeroAgent: result
    ZeroAgent-->>TRPC_Router: result
    TRPC_Router-->>Client: result
Loading

Possibly related PRs

  • Mail-0/Zero#595: Adds a toggleStar function using modifyLabels for mail threads, related to label management methods updated in this PR.

Poem

A rabbit hopped through fields of code,
Replacing calls both wide and broad.
No more guessing which method to send—
Now each mail task has its own friend!
Strongly-typed and clear to see,
Explicit methods set us free.
🐇✨

✨ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in 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
Collaborator Author

MrgSub commented Jun 24, 2025

Merge activity

  • Jun 24, 5:53 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Jun 24, 5:54 AM UTC: @MrgSub merged this pull request with Graphite.

@MrgSub MrgSub merged commit 027e913 into staging Jun 24, 2025
5 of 6 checks passed
@MrgSub MrgSub deleted the 06-23-replace_calldriver branch June 24, 2025 05: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: 0

🧹 Nitpick comments (1)
apps/server/src/routes/chat.ts (1)

359-583: Excellent refactoring that improves type safety and code clarity.

The replacement of the generic callDriver method with explicit, strongly-typed methods is a significant improvement. Each method follows a consistent pattern of driver validation and delegation.

However, there are a few areas for improvement:

  1. Redundant methods: Some methods appear to have overlapping functionality:

    • listThreads (line 360) vs list (line 509)
    • markThreadsRead (line 380) vs markAsRead (line 522)
    • markThreadsUnread (line 390) vs markAsUnread (line 529)
  2. Type safety: Many parameters use any type, which could be improved with proper interfaces.

Consider consolidating redundant methods and improving type definitions:

- async markThreadsRead(threadIds: string[]) {
-   if (!this.driver) {
-     throw new Error('No driver available');
-   }
-   return await this.driver.modifyLabels(threadIds, {
-     addLabels: [],
-     removeLabels: ['UNREAD'],
-   });
- }

- async markThreadsUnread(threadIds: string[]) {
-   if (!this.driver) {
-     throw new Error('No driver available');
-   }
-   return await this.driver.modifyLabels(threadIds, {
-     addLabels: ['UNREAD'],
-     removeLabels: [],
-   });
- }

And improve type definitions for better type safety:

- async createDraft(draftData: any) {
+ async createDraft(draftData: DraftData) {
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9b87c2f and 2736145.

📒 Files selected for processing (4)
  • apps/server/src/routes/chat.ts (1 hunks)
  • apps/server/src/trpc/routes/drafts.ts (2 hunks)
  • apps/server/src/trpc/routes/label.ts (4 hunks)
  • apps/server/src/trpc/routes/mail.ts (19 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (12)
apps/server/src/trpc/routes/drafts.ts (3)

10-10: LGTM - Clean refactoring to explicit method call.

The change from callDriver('createDraft', input) to createDraft(input) improves type safety and code clarity.


16-16: LGTM - Consistent with the refactoring pattern.

Direct method call is clearer than string-based dispatch.


30-30: LGTM - Explicit method call improves readability.

The parameter transformation maintains the same structure while removing the generic callDriver invocation.

apps/server/src/trpc/routes/label.ts (4)

17-17: LGTM - Direct method call with proper async handling.

The addition of await ensures proper asynchronous execution.


73-73: LGTM - Clean parameter destructuring and method call.

The destructuring of id from input and passing the remaining properties is handled correctly.


86-86: LGTM - Straightforward method call replacement.

Direct access to input.id maintains the same functionality with better type safety.


47-47: ```shell
#!/bin/bash

Show the full createLabel signature in ZeroAgent class to verify accepted fields

rg -n -A15 -B5 "async createLabel" apps/server/src/routes/chat.ts


</details>
<details>
<summary>apps/server/src/trpc/routes/mail.ts (5)</summary>

`25-25`: **LGTM - Clean method call replacement.**

The `getThread` method call properly passes the thread ID parameter.

---

`47-52`: **LGTM - Proper handling of different draft vs thread listing.**

The conditional logic correctly uses `listDrafts` for draft folders and `list` for regular thread listing, maintaining the same parameter structures.



Also applies to: 54-60

---

`71-71`: **LGTM - Consistent read/unread operations.**

Both `markAsRead` and `markAsUnread` method calls maintain the same parameter passing pattern.



Also applies to: 82-82

---

`112-112`: **LGTM - Proper ID normalization and label modification.**

The sequence of `normalizeIds` followed by `modifyLabels` maintains the same operational flow with explicit method calls.



Also applies to: 116-116

---

`302-304`: **LGTM - Clean conditional logic for draft sending vs creating.**

The branching logic properly handles both `sendDraft` and `create` operations based on the presence of `draftId`.

</details>

</blockquote></details>

</details>

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

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.

1 participant