Skip to content

feat: support upload multiple images#4483

Merged
ensorrow merged 1 commit intomainfrom
feat/multiple-images
Mar 17, 2025
Merged

feat: support upload multiple images#4483
ensorrow merged 1 commit intomainfrom
feat/multiple-images

Conversation

@ensorrow
Copy link
Copy Markdown
Contributor

@ensorrow ensorrow commented Mar 17, 2025

Types

  • 🎉 New Features

Background or solution

feat: support upload multiple images

Changelog

feat: support upload multiple images

Summary by CodeRabbit

  • 新功能
    • 优化了图片上传,现支持一次选择和上传多张图片,自动验证文件格式并批量处理上传请求。
    • 改进了图片粘贴功能,可从剪贴板同时上传多张图片,并在处理完图像后保留文本内容,提升整体用户体验。

@opensumi opensumi Bot added the 🎨 feature feature required label Mar 17, 2025
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 17, 2025

Walkthrough

此次提交主要调整了图片上传及粘贴处理功能。ChatMentionInput 组件中将图片上传的处理函数从接收单个文件改为接收文件数组,并采用 Promise.all 进行并行上传,同时更新组件状态。MentionInput 组件的 paste 事件处理逻辑也被修改为异步方式,提取所有图片文件后再调用上传函数。此外,类型声明中对应的 onImageUpload 方法签名也已更新,支持接收多个文件和返回 Promise。

Changes

文件路径 变更摘要
packages/ai-native/src/.../ChatMentionInput.tsx 修改 handleImageUpload 函数:参数由单个 File 改为 File[],采用 Promise.all 异步上传并合并更新状态。
packages/ai-native/src/.../mention-input/mention-input.tsx 更新 handlePaste 函数:改为异步处理,收集剪贴板中的所有图片文件后异步调用 onImageUpload 上传。
packages/ai-native/src/.../mention-input/types.ts 更新 MentionInputProps 中 onImageUpload 的签名:从接收单个 File 返回 void 改为接收 File[] 返回 Promise。

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant CI as ChatMentionInput
    participant HU as handleImageUpload
    participant PA as Promise.all
    participant S as 组件状态

    U->>CI: 选择图片文件
    CI->>HU: 调用 handleImageUpload([file])
    HU->>HU: 验证并过滤文件
    HU->>PA: 异步上传所有有效文件
    PA-->>HU: 返回上传结果数组
    HU->>S: 更新状态,合并新上传图片
Loading
sequenceDiagram
    participant U as 用户
    participant MI as MentionInput
    participant HP as handlePaste
    participant IU as onImageUpload
    participant S as 组件状态

    U->>MI: 粘贴内容
    MI->>HP: handlePaste 触发
    HP->>HP: 提取剪贴板中所有图片文件
    HP->>IU: 异步调用 onImageUpload([files])
    IU-->>HP: 完成图片上传
    HP->>MI: 继续处理文本内容,更新状态
Loading

Possibly related PRs

Suggested reviewers

  • Aaaaash
  • Ricbet
  • life2015

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

yarn install v1.22.22
[1/4] Resolving packages...
warning [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.
warning eslint > @humanwhocodes/[email protected]: Use @eslint/config-array instead
warning eslint > @humanwhocodes/config-array > @humanwhocodes/[email protected]: Use @eslint/object-schema instead
warning eslint > file-entry-cache > flat-cache > [email protected]: Rimraf versions prior to v4 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > [email protected]: Glob versions prior to v9 are no longer supported
warning eslint > file-entry-cache > flat-cache > rimraf > glob > [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
warning eslint-import-resolver-typescript > [email protected]: Glob versions prior to v9 are no longer supported
error Couldn't find any versions for "@opensumi/ide-dev-tool" that matches "workspace:*"
info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.
✨ 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
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: 0

🧹 Nitpick comments (2)
packages/ai-native/src/browser/components/ChatMentionInput.tsx (1)

300-322: 多图上传处理逻辑实现良好

该实现正确处理了多图上传功能,包括类型验证和并行上传处理。使用 Promise.all 进行并行上传是提高性能的好方法。

建议将文件类型验证逻辑提取到单独的函数中,以提高代码重用性和可读性:

-      // Validate file types
-      const invalidFiles = files.filter((file) => !allowedTypes.includes(file.type));
-      if (invalidFiles.length > 0) {
-        messageService.error('Only JPG, PNG, WebP and GIF images are supported');
-        return;
-      }
+      // Validate file types
+      const validateImageFiles = (files: File[], allowedTypes: string[]) => {
+        const invalidFiles = files.filter((file) => !allowedTypes.includes(file.type));
+        if (invalidFiles.length > 0) {
+          messageService.error('Only JPG, PNG, WebP and GIF images are supported');
+          return false;
+        }
+        return true;
+      };
+      
+      if (!validateImageFiles(files, allowedTypes)) {
+        return;
+      }
packages/ai-native/src/browser/components/mention-input/mention-input.tsx (1)

451-472: 粘贴事件处理支持多图上传

异步处理粘贴事件并支持多图同时上传的实现是很好的改进。

有两点可以进一步优化:

  1. 可以在粘贴处理程序中添加图片类型验证,与 ChatMentionInput.tsx 中的验证保持一致。

  2. 考虑当剪贴板同时包含图片和文本时的处理。目前的实现在上传图片后会立即返回,这可能会阻止文本内容的粘贴。

  // 处理所有收集到的图片
  if (imageFiles.length > 0 && onImageUpload) {
    await onImageUpload(imageFiles);
-   return;
  }

  const text = e.clipboardData.getData('text/plain');
+ // 只有当有文本内容且没有图片或已处理完图片时才处理文本
+ if (text) {
    // 处理文本,保留换行和缩进
    const processedText = text
      .replace(/\t/g, '    ')
      .replace(/\n\s*\n/g, '\n\n')
      .replace(/[ \t]+$/gm, '');

    // ... 文本处理逻辑 ...
+ }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 942f272 and 7f451f1.

📒 Files selected for processing (3)
  • packages/ai-native/src/browser/components/ChatMentionInput.tsx (3 hunks)
  • packages/ai-native/src/browser/components/mention-input/mention-input.tsx (1 hunks)
  • packages/ai-native/src/browser/components/mention-input/types.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: build (ubuntu-latest, 20.x)
  • GitHub Check: build-windows
  • GitHub Check: build (macos-latest, 20.x)
  • GitHub Check: unittest (ubuntu-latest, 18.x, node)
  • GitHub Check: unittest (macos-latest, 18.x, jsdom)
  • GitHub Check: unittest (ubuntu-latest, 18.x, jsdom)
  • GitHub Check: unittest (macos-latest, 18.x, node)
  • GitHub Check: ubuntu-latest, Node.js 20.x
🔇 Additional comments (2)
packages/ai-native/src/browser/components/mention-input/types.ts (1)

79-79: 接口变更适应多图上传功能

更新的 onImageUpload 方法签名现在接受文件数组 File[] 并返回 Promise<void>,很好地支持了多图上传的新功能。

packages/ai-native/src/browser/components/ChatMentionInput.tsx (1)

266-266: 使用数组包装单个文件适应新的接口

现在使用数组 [file] 包装单个文件来调用 handleImageUpload,匹配了更新后的方法签名。

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 53.14%. Comparing base (b3eee42) to head (7f451f1).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4483   +/-   ##
=======================================
  Coverage   53.14%   53.14%           
=======================================
  Files        1665     1665           
  Lines      102628   102628           
  Branches    22221    22221           
=======================================
+ Hits        54537    54538    +1     
+ Misses      40008    40007    -1     
  Partials     8083     8083           
Flag Coverage Δ
jsdom 48.63% <ø> (+<0.01%) ⬆️
node 12.13% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ensorrow ensorrow merged commit 05fab51 into main Mar 17, 2025
@ensorrow ensorrow deleted the feat/multiple-images branch March 17, 2025 06:09
@erha19 erha19 mentioned this pull request May 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🎨 feature feature required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants