Skip to content

perf(web): compress large PNG uploads and reuse bounded LRU image cache - #1286

Merged
iruriQ merged 1 commit into
mainfrom
fix/peer-image-frontend
Nov 27, 2026
Merged

iruriQ merged 1 commit into
mainfrom
fix/peer-image-frontend

Conversation

@iruriQ

@iruriQ iruriQ commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

Purpose

Optimize Web UI image upload payload size and transcript image display latency, especially across remote Peer sessions and #1247 background session eviction:

  1. Compress large PNG screenshots (> 1 MiB or longest edge > 2048px) to high-clarity image/webp (0.9 quality, max edge 2048px) before upload, strictly falling back to scaled PNG (when WebP encoding is unsupported and scaled PNG is smaller) or the original file when output is not smaller or encoding fails.
  2. Replace the per-SessionBinding image URL map with a cross-session bounded LRU cache (32 entries / 24 MiB cap, automatic URL.revokeObjectURL on eviction and disposal) so historical images survive #1247 background session eviction without re-downloading.
  3. Cache locally uploaded image Blobs immediately on upload completion (indexed by exact attachmentId plus session-scoped content-leaf, (name, bytes), and unambiguous pending-name fallbacks for remote peer peer-image:* placeholders) and adopt seeded URLs without triggering a canonical readAttachment RPC round-trip.

Affected Files

  • apps/web-ui/packages/client/ui-conversation/src/client/prepare-photo.ts
  • apps/web-ui/packages/client/ui-conversation/src/client/conversation/historical-images.ts
  • apps/web-ui/packages/client/ui-conversation/src/client/conversation/assembly.ts
  • apps/web-ui/packages/client/ui-conversation/src/client/service.ts
  • apps/web-ui/packages/client/ui-conversation/test/prepare-photo.test.ts
  • apps/web-ui/packages/client/ui-conversation/test/historical-images.test.ts

UI / Visual Impact

No visual changes: client-side upload compression and in-memory image preview LRU caching only; no layout, styling, or component visual changes.

Validation

  • npx vitest run --config tests/vitest.config.js apps/web-ui/packages/client/ui-conversation/test/ — 8 test files, 33 tests passed (including 11 new tests in prepare-photo.test.ts and historical-images.test.ts, re-run after formatting)
  • npx tsc --noEmit -p apps/web-ui/tsconfig.json — passed
  • npm run build:web:ui — passed (built all 43 client plugins and Web bundle)
  • npx prettier --check on all 6 changed/added files — passed

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

Summary

Summary by CodeRabbit

  • 新功能
    • 支持在图片准备过程中处理 JPEG、PNG 和 WebP;符合条件时压缩图片,并在适用时将 PNG 转为 WebP。
    • 上传图片可在会话中复用,并支持将上传内容与回显图片关联;历史图片缓存支持按容量限制保留图片。
  • 问题修复
    • 图片转换失败或结果未缩小时保留原图,避免不必要的图片替换。
  • 测试
    • 新增图片缓存、上传图片复用及图片准备行为的测试覆盖。

图片处理现支持 PNG 预处理。历史图片缓存改为有容量限制的共享 LRU 缓存,并支持上传图片复用、回退匹配及提交回显绑定。

Changes: 会话图片处理

Layer / File(s) Summary
图片预处理
apps/web-ui/packages/client/ui-conversation/src/client/prepare-photo.ts, apps/web-ui/packages/client/ui-conversation/src/client/service.ts, apps/web-ui/packages/client/ui-conversation/test/prepare-photo.test.ts
可处理图片类型包含 JPEG、WebP 和 PNG。PNG 优先尝试编码为 WebP;结果不符合条件时保留原图,或在图像缩放后采用较小的 PNG。测试覆盖类型识别、编码结果和失败回退。
共享图片缓存
apps/web-ui/packages/client/ui-conversation/src/client/conversation/historical-images.ts, apps/web-ui/packages/client/ui-conversation/test/historical-images.test.ts
历史图片缓存改为按会话共享的 LRU 缓存,并支持条目数和字节数限制。缓存支持上传 Blob、别名绑定及回退匹配;测试覆盖缓存复用、淘汰和名称匹配歧义。
上传与回显接入
apps/web-ui/packages/client/ui-conversation/src/client/conversation/assembly.ts, apps/web-ui/packages/client/ui-conversation/src/client/service.ts
UiConversation 增加上传缓存、别名绑定和待处理项清理方法。service.ts 在恢复草稿、上传完成和观察到提交回显时调用图片缓存。

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant service as service.ts
  participant UiConversation
  participant HistoricalImageCache
  service->>UiConversation: cacheUploadedImage(sessionId, uploaded)
  UiConversation->>HistoricalImageCache: cacheUploaded(sessionId, uploaded)
  Note over service: 观察到提交回显
  service->>UiConversation: aliasUploadedImage(sessionId, uploadedAttachmentId, target)
  UiConversation->>HistoricalImageCache: aliasUploaded(sessionId, uploadedAttachmentId, target)
  alt 别名绑定成功
    service->>service: 撤销预览 URL
  else 别名绑定失败
    service->>UiConversation: seedImageUrl(sessionId, target, url, bytes)
    UiConversation->>HistoricalImageCache: seed(sessionId, target, url, bytes)
  end
Loading

Suggested reviewers: bytepioneer-ai

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@iruriQ
iruriQ merged commit 88b0eb7 into main Nov 27, 2026
5 of 8 checks passed
@iruriQ
iruriQ deleted the fix/peer-image-frontend branch November 27, 2026 03:33
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.

2 participants