Skip to content

refactor: Improve async handling and file clearing logic#8835

Merged
Cristhianzl merged 9 commits into
mainfrom
cz/fix-chat-image-send
Jul 4, 2025
Merged

refactor: Improve async handling and file clearing logic#8835
Cristhianzl merged 9 commits into
mainfrom
cz/fix-chat-image-send

Conversation

@Cristhianzl
Copy link
Copy Markdown
Member

@Cristhianzl Cristhianzl commented Jul 2, 2025

This pull request focuses on improving the handling of asynchronous operations in the chat input and related components. The most important changes include ensuring sendMessage is treated as an asynchronous function throughout the codebase and improving the file clearing logic during message sending.

Improvements to asynchronous handling:

Improvements to file clearing logic:

Summary by CodeRabbit

  • Bug Fixes
    • Improved the handling of file attachments in chat by clearing selected files before sending messages, preventing potential duplicate sends.
    • Updated the "Run Flow" button and related actions to properly handle asynchronous message sending, ensuring smoother user interactions.

…diately after sending a message

📝 (chat-input.tsx): refactor code to improve readability and maintainability by extracting filesToSend logic into a separate variable
@Cristhianzl Cristhianzl self-assigned this Jul 2, 2025
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 2, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 2, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This update adjusts the timing of clearing the files state in the chat input, changes the sendMessage prop in a component to be asynchronous, and ensures that related handlers properly await the asynchronous operation. No exported or public entity declarations were added or removed.

Changes

File(s) Change Summary
src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx Modified the send function to clear the files state before sending the message and use a precomputed files array.
src/frontend/src/modals/IOModal/components/chatView/chatInput/components/no-input.tsx Changed sendMessage prop type to async (Promise<void>) and updated the button handler to await it.
src/frontend/src/modals/IOModal/playground-modal.tsx Updated onSubmit handler to be async and await the sendMessage promise.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ChatInput
    participant MessageSender

    User->>ChatInput: Click Send / Run Flow
    ChatInput->>ChatInput: Extract files to filesToSend
    ChatInput->>ChatInput: setFiles([])
    ChatInput->>MessageSender: await sendMessage({ files: filesToSend, ... })
    MessageSender-->>ChatInput: Promise resolved/rejected
    ChatInput-->>User: (Optional) Restore chat value on error
Loading

Possibly related PRs

Suggested labels

size:M, lgtm

Suggested reviewers

  • lucaseduoli
  • deon-sanchez
✨ Finishing Touches
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Post Copyable Unit Tests in a Comment
  • Commit Unit Tests in branch cz/fix-chat-image-send

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 auto-generate unit tests to generate unit tests 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.

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.

@dosubot dosubot Bot added the javascript Pull requests that update Javascript code label Jul 2, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 2, 2025
Copy link
Copy Markdown
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.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e280932 and 48953a2.

📒 Files selected for processing (3)
  • src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx (1 hunks)
  • src/frontend/src/modals/IOModal/components/chatView/chatInput/components/no-input.tsx (2 hunks)
  • src/frontend/src/modals/IOModal/playground-modal.tsx (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
`src/frontend/**/*.{ts,tsx}`: Use React 18 with TypeScript for all UI components and frontend logic.

src/frontend/**/*.{ts,tsx}: Use React 18 with TypeScript for all UI components and frontend logic.

📄 Source: CodeRabbit Inference Engine (.cursor/rules/frontend_development.mdc)

List of files the instruction was applied to:

  • src/frontend/src/modals/IOModal/playground-modal.tsx
  • src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx
  • src/frontend/src/modals/IOModal/components/chatView/chatInput/components/no-input.tsx
`src/frontend/**/*.{ts,tsx,js,jsx,css,scss}`: Use Tailwind CSS for styling all frontend components.

src/frontend/**/*.{ts,tsx,js,jsx,css,scss}: Use Tailwind CSS for styling all frontend components.

📄 Source: CodeRabbit Inference Engine (.cursor/rules/frontend_development.mdc)

List of files the instruction was applied to:

  • src/frontend/src/modals/IOModal/playground-modal.tsx
  • src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx
  • src/frontend/src/modals/IOModal/components/chatView/chatInput/components/no-input.tsx
🔇 Additional comments (3)
src/frontend/src/modals/IOModal/playground-modal.tsx (1)

359-359: LGTM! Proper async handling implementation.

The change from synchronous to asynchronous handling correctly aligns with the updated sendMessage function signature and maintains consistency across the codebase.

src/frontend/src/modals/IOModal/components/chatView/chatInput/components/no-input.tsx (2)

10-10: LGTM! Correct type signature update.

The prop type correctly reflects that sendMessage now returns a Promise, maintaining type safety.


26-30: LGTM! Proper async/await implementation.

The onClick handler correctly uses async/await to handle the asynchronous sendMessage call.

Comment on lines 167 to 184
const send = async () => {
const storedChatValue = chatValue;
const filesToSend = files
.map((file) => file.path ?? "")
.filter((file) => file !== "");

// Clear files immediately when sending
setFiles([]);

try {
await sendMessage({
repeat: 1,
files: files
.map((file) => file.path ?? "")
.filter((file) => file !== ""),
files: filesToSend,
});
} catch (error) {
setChatValueStore(storedChatValue);
}

setFiles([]);
};
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling to restore files on send failure.

While clearing files immediately after preparing them prevents race conditions, the current error handling only restores chatValue but not the files. If sendMessage fails, users will lose their file selections, leading to poor UX.

const send = async () => {
  const storedChatValue = chatValue;
  const filesToSend = files
    .map((file) => file.path ?? "")
    .filter((file) => file !== "");
+  const storedFiles = [...files]; // Store files before clearing

  // Clear files immediately when sending
  setFiles([]);

  try {
    await sendMessage({
      repeat: 1,
      files: filesToSend,
    });
  } catch (error) {
    setChatValueStore(storedChatValue);
+    setFiles(storedFiles); // Restore files on error
  }
};
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const send = async () => {
const storedChatValue = chatValue;
const filesToSend = files
.map((file) => file.path ?? "")
.filter((file) => file !== "");
// Clear files immediately when sending
setFiles([]);
try {
await sendMessage({
repeat: 1,
files: files
.map((file) => file.path ?? "")
.filter((file) => file !== ""),
files: filesToSend,
});
} catch (error) {
setChatValueStore(storedChatValue);
}
setFiles([]);
};
const send = async () => {
const storedChatValue = chatValue;
const filesToSend = files
.map((file) => file.path ?? "")
.filter((file) => file !== "");
const storedFiles = [...files]; // Store files before clearing
// Clear files immediately when sending
setFiles([]);
try {
await sendMessage({
repeat: 1,
files: filesToSend,
});
} catch (error) {
setChatValueStore(storedChatValue);
setFiles(storedFiles); // Restore files on error
}
};
🤖 Prompt for AI Agents
In src/frontend/src/modals/IOModal/components/chatView/chatInput/chat-input.tsx
around lines 167 to 184, the error handling in the send function restores
chatValue on sendMessage failure but does not restore the files array, causing
loss of user file selections. Modify the catch block to also reset the files
state to the previously stored files before clearing, ensuring both chatValue
and files are restored on error.

@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 4, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 4, 2025
Copy link
Copy Markdown
Collaborator

@lucaseduoli lucaseduoli left a comment

Choose a reason for hiding this comment

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

LGTM!

@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 4, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jul 4, 2025

Frontend Unit Test Coverage Report

Coverage Summary

Lines Statements Branches Functions
Coverage: 71%
73.58% (117/159) 57.46% (77/134) 59.52% (25/42)

Unit Test Results

Tests Skipped Failures Errors Time
45 0 💤 0 ❌ 0 🔥 2.632s ⏱️

@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 4, 2025
Copy link
Copy Markdown
Contributor

@ogabrielluiz ogabrielluiz left a comment

Choose a reason for hiding this comment

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

Check coderabbit's comments

@dosubot dosubot Bot removed the lgtm This PR has been approved by a maintainer label Jul 4, 2025
@github-actions github-actions Bot removed the refactor Maintenance tasks and housekeeping label Jul 4, 2025
@Cristhianzl Cristhianzl requested a review from ogabrielluiz July 4, 2025 18:15
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jul 4, 2025
@ogabrielluiz ogabrielluiz enabled auto-merge July 4, 2025 18:28
@ogabrielluiz ogabrielluiz disabled auto-merge July 4, 2025 18:59
@ogabrielluiz ogabrielluiz added the fast-track Skip tests and sends PR into the merge queue label Jul 4, 2025
@carlosrcoelho carlosrcoelho added this pull request to the merge queue Jul 4, 2025
@Cristhianzl Cristhianzl removed this pull request from the merge queue due to a manual request Jul 4, 2025
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jul 4, 2025
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 4, 2025
@carlosrcoelho carlosrcoelho added this pull request to the merge queue Jul 4, 2025
github-merge-queue Bot pushed a commit that referenced this pull request Jul 4, 2025
* fix chat image sent

* 🐛 (chat-input.tsx): fix issue where files were not being cleared immediately after sending a message
📝 (chat-input.tsx): refactor code to improve readability and maintainability by extracting filesToSend logic into a separate variable

* Delete diff_output_ts.txt

* ♻️ (chat-input.tsx): refactor code to store and restore files when sending a message to prevent losing files if an error occurs during sending.

* ✅ (fileUploadComponent.spec.ts): update timeout values for better test reliability and performance

---------

Co-authored-by: Ítalo Johnny <italojohnnydosanjos@gmail.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
@ogabrielluiz ogabrielluiz removed this pull request from the merge queue due to a manual request Jul 4, 2025
@ogabrielluiz ogabrielluiz added this pull request to the merge queue Jul 4, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 4, 2025
@Cristhianzl Cristhianzl added this pull request to the merge queue Jul 4, 2025
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jul 4, 2025
…he correct attribute value to ensure accurate testing
@github-actions github-actions Bot added refactor Maintenance tasks and housekeeping and removed refactor Maintenance tasks and housekeeping labels Jul 4, 2025
@Cristhianzl Cristhianzl added this pull request to the merge queue Jul 4, 2025
Merged via the queue into main with commit db48cd3 Jul 4, 2025
13 checks passed
@Cristhianzl Cristhianzl deleted the cz/fix-chat-image-send branch July 4, 2025 20:28
rodrigosnader pushed a commit that referenced this pull request Jul 7, 2025
* fix chat image sent

* 🐛 (chat-input.tsx): fix issue where files were not being cleared immediately after sending a message
📝 (chat-input.tsx): refactor code to improve readability and maintainability by extracting filesToSend logic into a separate variable

* Delete diff_output_ts.txt

* ♻️ (chat-input.tsx): refactor code to store and restore files when sending a message to prevent losing files if an error occurs during sending.

* ✅ (fileUploadComponent.spec.ts): update timeout values for better test reliability and performance

* ✅ (fileUploadComponent.spec.ts): update test assertion to check for the correct attribute value to ensure accurate testing

---------

Co-authored-by: Ítalo Johnny <italojohnnydosanjos@gmail.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
smatiolids pushed a commit to smatiolids/langflow-dev that referenced this pull request Jul 10, 2025
…#8835)

* fix chat image sent

* 🐛 (chat-input.tsx): fix issue where files were not being cleared immediately after sending a message
📝 (chat-input.tsx): refactor code to improve readability and maintainability by extracting filesToSend logic into a separate variable

* Delete diff_output_ts.txt

* ♻️ (chat-input.tsx): refactor code to store and restore files when sending a message to prevent losing files if an error occurs during sending.

* ✅ (fileUploadComponent.spec.ts): update timeout values for better test reliability and performance

* ✅ (fileUploadComponent.spec.ts): update test assertion to check for the correct attribute value to ensure accurate testing

---------

Co-authored-by: Ítalo Johnny <italojohnnydosanjos@gmail.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fast-track Skip tests and sends PR into the merge queue javascript Pull requests that update Javascript code lgtm This PR has been approved by a maintainer refactor Maintenance tasks and housekeeping size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants