Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat: Custom encode and decode for thread messages in cloud #1745

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

lawnzapper
Copy link

@lawnzapper lawnzapper commented Mar 21, 2025

Hello,
I have integrated a custom encoder and decoder for thread messages into the AssistantCloud constructor. This enhancement allows an external cloud provider to manage not just text content, but also attachments or various types of data that need to be stored.


Important

Adds custom encode/decode functions for thread messages in AssistantCloud, enhancing data handling flexibility.

  • Behavior:
    • Adds encodeMessage and decodeMessage to AssistantCloud constructor for custom message handling.
    • Defaults to auiV0Encode and auiV0Decode if no custom functions provided.
  • Integration:
    • AssistantCloudThreads in AssistantCloud.tsx now uses custom encode/decode functions.
    • append() and load() in AssistantCloudThreadHistoryAdapter.tsx use cloud.encodeMessage and cloud.decodeMessage for message processing.

This description was created by Ellipsis for 4bfc568. It will automatically update as commits are pushed.

Copy link

vercel bot commented Mar 21, 2025

@lawnzapper is attempting to deploy a commit to the assistant-ui Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

PR Summary

Implemented custom message encoding/decoding functionality in AssistantCloud to support external cloud providers handling various data types beyond text content.

  • Missing imports for auiV0Encode and auiV0Decode in /packages/react/src/cloud/AssistantCloud.tsx
  • Potential null reference issue with cloudRef.current in /packages/react/src/cloud/AssistantCloudThreadHistoryAdapter.tsx
  • Type safety concerns in AssistantCloudConfig interface for encode/decode function definitions
  • Consider binding encode/decode methods in constructor to prevent context loss
  • Need validation for encoded message format compatibility with 'aui/v0' standard

💡 (1/5) You can manually trigger the bot by mentioning @greptileai in a comment!

2 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile

Copy link
Contributor

promptless bot commented Mar 21, 2025

📝 Documentation updates detected! You can review documentation updates here

Copy link
Contributor

coderabbitai bot commented Mar 21, 2025

Walkthrough

The changes add two new public properties, encodeMessage and decodeMessage, to the AssistantCloud class, initializing them in the constructor with values from a configuration parameter. If no custom functions are provided, they default to auiV0Encode and auiV0Decode. The instantiation of the threads property now passes these encoding and decoding functions as parameters to the AssistantCloudThreads instance. Additionally, modifications in the AssistantCloudThreadHistoryAdapter class update the append and load methods to use the cloud instance's encodeMessage and decodeMessage instead of the direct calls to the default functions. A local variable is introduced to reference the cloud instance, streamlining access to its methods for message handling.


🪧 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 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
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: 3

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 497384d and 4bfc568.

📒 Files selected for processing (2)
  • packages/react/src/cloud/AssistantCloud.tsx (1 hunks)
  • packages/react/src/cloud/AssistantCloudThreadHistoryAdapter.tsx (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/react/src/cloud/AssistantCloud.tsx (3)
packages/react/src/cloud/AssistantCloudAPI.tsx (2) (2)
  • AssistantCloudConfig (8-21)
  • AssistantCloudAPI (37-121)
packages/react/src/cloud/auiV0.ts (2) (2)
  • auiV0Encode (63-131)
  • auiV0Decode (133-154)
packages/react/src/cloud/AssistantCloudThreads.tsx (1) (1)
  • AssistantCloudThreads (45-79)
🔇 Additional comments (5)
packages/react/src/cloud/AssistantCloudThreadHistoryAdapter.tsx (5)

18-20: Good refactoring to improve code readability

Creating a local variable for this.cloudRef.current improves readability and avoids repeated access to the same reference.


26-26: Direct use of cloud.encodeMessage improves encapsulation

Switching from direct calls to encoding functions to using the cloud instance's methods enhances encapsulation and allows for custom encoding logic to be provided through configuration.


42-43: Simplified message retrieval with local cloud variable

Using the local cloud variable streamlines the code and makes it more readable.


50-50: Using cloud.decodeMessage allows for custom decoding

This change properly uses the cloud instance's decoding method instead of hard-coding to auiV0Decode, enabling custom decoding logic to be used when provided.


38-54: Verify type safety with the decodeMessage function

The code assumes that cloud.decodeMessage can be directly passed to map(), which requires it to maintain its function context.

You should ensure that cloud.decodeMessage properly maintains its this context when used directly in a map function. Consider using a bound function or arrow function to ensure correct behavior:

- .map(cloud.decodeMessage)
+ .map((message) => cloud.decodeMessage(message))

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: 2

♻️ Duplicate comments (1)
packages/react/src/cloud/AssistantCloud.tsx (1)

11-12: 🧹 Nitpick (assertive)

Missing TypeScript type annotations

The new properties encodeMessage and decodeMessage should have explicit TypeScript type annotations to ensure type safety and improve code documentation.

-  public readonly encodeMessage;
-  public readonly decodeMessage;
+  public readonly encodeMessage: (message: ThreadMessage) => any;
+  public readonly decodeMessage: (cloudMessage: CloudMessage & { format: "aui/v0" }) => ExportedMessageRepositoryItem;

You'll need to import the necessary types (ThreadMessage, CloudMessage, and ExportedMessageRepositoryItem) at the top of the file.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between 4bfc568 and 2af10da.

📒 Files selected for processing (1)
  • packages/react/src/cloud/AssistantCloud.tsx (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/react/src/cloud/AssistantCloud.tsx (3)
packages/react/src/cloud/AssistantCloudAPI.tsx (2) (2)
  • AssistantCloudConfig (8-21)
  • AssistantCloudAPI (37-121)
packages/react/src/cloud/auiV0.ts (2) (2)
  • auiV0Encode (63-131)
  • auiV0Decode (133-154)
packages/react/src/cloud/AssistantCloudThreads.tsx (1) (1)
  • AssistantCloudThreads (45-79)
🔇 Additional comments (1)
packages/react/src/cloud/AssistantCloud.tsx (1)

5-5: Import statement resolves previous issue

Good addition of the missing imports for auiV0Decode and auiV0Encode. This resolves the issue identified in previous reviews where these functions were being used but not imported.

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between 2af10da and 6445a37.

📒 Files selected for processing (1)
  • packages/react/src/cloud/AssistantCloudThreadHistoryAdapter.tsx (2 hunks)
🔇 Additional comments (4)
packages/react/src/cloud/AssistantCloudThreadHistoryAdapter.tsx (4)

18-19: Good addition of null check for cloud reference.

The initialization of a local variable and subsequent null check prevents potential runtime errors by ensuring the cloud reference is properly initialized before proceeding.


20-21: Improved code organization with local variable.

Using the local cloud variable makes the code more readable and maintainable by reducing repeated access to the nested property.


27-27: Properly using the cloud instance's encodeMessage function.

The code now correctly uses the custom encode function from the cloud instance, aligning with the PR objective to support custom encoding for thread messages.


51-51: Using cloud instance's decodeMessage method properly.

The code correctly leverages the cloud instance's decodeMessage method for consistent handling of thread messages.

Note: A previous review comment suggested using .bind(cloud) here, but as you clarified, the context is not needed for this method.

Copy link
Author

@lawnzapper lawnzapper left a comment

Choose a reason for hiding this comment

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

I reviewed the changes, added some fixes and accepted a couple of suggestions

Copy link

trag-bot bot commented Mar 21, 2025

👋 Hey! As a free user, you're receiving reviews for every 5th PR. Upgrade to get reviews on every pull request and boost your code quality! Learn more here 🚀

Copy link

trag-bot bot commented Mar 21, 2025

Pull request summary

  • Added auiV0Encode and auiV0Decode imports to AssistantCloud for message encoding and decoding.
  • Introduced encodeMessage and decodeMessage properties in the AssistantCloud class to allow custom encoding/decoding functions.
  • Updated the constructor of AssistantCloud to initialize encodeMessage and decodeMessage from the configuration or use default functions.
  • Refactored AssistantCloudThreadHistoryAdapter to utilize the encodeMessage method from the AssistantCloud instance when creating messages.
  • Modified the message loading logic in AssistantCloudThreadHistoryAdapter to use the decodeMessage method from the AssistantCloud instance for decoding messages.

@lawnzapper lawnzapper changed the title Custom encode and decode for thread messages in cloud feat: Custom encode and decode for thread messages in cloud Mar 21, 2025
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: 1

♻️ Duplicate comments (1)
packages/react/src/cloud/AssistantCloud.tsx (1)

11-12: 🛠️ Refactor suggestion

Add TypeScript type annotations to the encode/decode properties.

These properties should have proper type annotations to maintain code quality and make the API clearer to consumers.

Based on the learning that "encodeMessage can be strictly typed but decodeMessage needs to be more flexible", I recommend:

-  public readonly encodeMessage;
-  public readonly decodeMessage;
+  public readonly encodeMessage: (message: ThreadMessage) => any;
+  public readonly decodeMessage: (cloudMessage: CloudMessage) => ExportedMessageRepositoryItem;

You'll need to add imports for the relevant types.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between 6445a37 and 3f81291.

📒 Files selected for processing (1)
  • packages/react/src/cloud/AssistantCloud.tsx (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: lawnzapper
PR: assistant-ui/assistant-ui#1745
File: packages/react/src/cloud/AssistantCloud.tsx:18-19
Timestamp: 2025-03-21T17:11:08.624Z
Learning: In the Assistant UI codebase, when defining encode/decode functions for cloud messages, encodeMessage can be strictly typed with a specific input type (ThreadMessage), but decodeMessage needs to be more flexible to handle different message formats beyond the initial "aui/v0" format.
packages/react/src/cloud/AssistantCloud.tsx (1)
Learnt from: lawnzapper
PR: assistant-ui/assistant-ui#1745
File: packages/react/src/cloud/AssistantCloud.tsx:18-19
Timestamp: 2025-03-21T17:11:08.624Z
Learning: In the Assistant UI codebase, when defining encode/decode functions for cloud messages, encodeMessage can be strictly typed with a specific input type (ThreadMessage), but decodeMessage needs to be more flexible to handle different message formats beyond the initial "aui/v0" format.
🧬 Code Definitions (1)
packages/react/src/cloud/AssistantCloud.tsx (2)
packages/react/src/cloud/AssistantCloudAPI.tsx (2) (2)
  • AssistantCloudConfig (8-21)
  • AssistantCloudAPI (37-121)
packages/react/src/cloud/auiV0.ts (2) (2)
  • auiV0Encode (63-131)
  • auiV0Decode (133-154)
🔇 Additional comments (2)
packages/react/src/cloud/AssistantCloud.tsx (2)

5-5: LGTM: Import of encoding/decoding functions added.

The import of auiV0Decode and auiV0Encode functions has been added, which properly addresses the previous review comments about missing imports.


16-20: Verify that AssistantCloudConfig type has been updated to include these properties.

The code is accessing config.encodeMessage and config.decodeMessage, but based on the provided relevant code snippets, the AssistantCloudConfig type doesn't include these properties yet.

#!/bin/bash
# Check if AssistantCloudConfig type has been updated to include encodeMessage and decodeMessage
rg "AssistantCloudConfig" --type typescript -A 25

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