From 7ddac0385fc7ec78b30da0a6f9bc13da7d672517 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 16 Jul 2026 17:38:34 +0800 Subject: [PATCH] feat(attachments): enforce combined limit for images and attachments Implement a maximum limit of 5 for the total number of images and attachments that can be uploaded in a chat session. Introduce a warning message for users when the limit is exceeded. Additionally, add functionality to clear pending uploads when switching sessions to prevent errors related to stale document IDs. Update localization files to include new warning messages in English, Korean, Russian, and Chinese. --- frontend/src/components/Input-field.vue | 25 +++++++++++++++++++++++++ frontend/src/i18n/locales/en-US.ts | 1 + frontend/src/i18n/locales/ko-KR.ts | 1 + frontend/src/i18n/locales/ru-RU.ts | 1 + frontend/src/i18n/locales/zh-CN.ts | 1 + internal/types/message.go | 9 +++++++-- 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index fedac9a0..d3e14ed2 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -1874,6 +1874,19 @@ const createSession = async (val: string) => { return; } + // Images and non-embedded attachments both travel to the backend as + // `attachment_ids`, which enforces a combined cap (MaxTemporaryAttachmentsPerMessage). + // The per-picker limits (5 images / 5 attachments) are independent, so guard the + // merged total here to avoid a late 400 after the files are already uploaded. + const MAX_TOTAL_ATTACHMENTS = 5; + const combinedAttachmentCount = + uploadedImages.value.length + + uploadedAttachments.value.filter(item => item.status !== 'failed').length; + if (combinedAttachmentCount > MAX_TOTAL_ATTACHMENTS) { + MessagePlugin.warning(t('chat.attachmentTotalTooMany', { max: MAX_TOTAL_ATTACHMENTS })); + return; + } + if (!chatResources.isFresh('models')) { await loadChatModels() } @@ -2154,6 +2167,17 @@ const clearvalue = () => { query.value = ""; } +// Drop any pending images/attachments and stop their status polling. Used when +// switching sessions: leftover documentIds belong to the previous session, so +// keeping them would make polling 404 (falsely marking them failed) or send IDs +// the new session does not own ("attachment ... not found in this session"). +const clearPendingUploads = () => { + uploadedImages.value.forEach(img => URL.revokeObjectURL(img.preview)); + uploadedImages.value = []; + attachmentUploadRef.value?.clear(); + uploadedAttachments.value = []; +} + const onKeydown = (val: string, event: { e: { preventDefault(): unknown; keyCode: number; shiftKey: any; ctrlKey: any; }; }) => { if (showMention.value) { if (event.e.keyCode === 38) { // Up @@ -2433,6 +2457,7 @@ const handleStop = async () => { onBeforeRouteUpdate((to, from, next) => { clearvalue() + clearPendingUploads() next() }) diff --git a/frontend/src/i18n/locales/en-US.ts b/frontend/src/i18n/locales/en-US.ts index bbf9a307..76c19ac7 100755 --- a/frontend/src/i18n/locales/en-US.ts +++ b/frontend/src/i18n/locales/en-US.ts @@ -3298,6 +3298,7 @@ export default { attachmentUploadTooltip: 'Upload attachment (documents, audio, etc.)', attachmentWithCount: '{count} attachment(s) uploaded', attachmentTooMany: 'Maximum {max} attachments allowed', + attachmentTotalTooMany: 'Images and attachments combined can be at most {max}', attachmentTooLarge: 'File {name} exceeds {max}MB limit', attachmentTypeNotSupported: 'Unsupported file type: {name}', attachmentUploading: 'Uploading {progress}%', diff --git a/frontend/src/i18n/locales/ko-KR.ts b/frontend/src/i18n/locales/ko-KR.ts index 7a7c08aa..2fe9ce49 100755 --- a/frontend/src/i18n/locales/ko-KR.ts +++ b/frontend/src/i18n/locales/ko-KR.ts @@ -1055,6 +1055,7 @@ export default { attachmentUploadTooltip: "첨부 파일 업로드 (문서, 오디오 등)", attachmentWithCount: "{count}개 파일 업로드됨", attachmentTooMany: "최대 {max}개 파일까지 업로드 가능합니다", + attachmentTotalTooMany: "이미지와 첨부 파일은 합쳐서 최대 {max}개까지 가능합니다", attachmentTooLarge: "파일 {name}이(가) {max}MB 제한을 초과합니다", attachmentTypeNotSupported: "지원하지 않는 파일 형식: {name}", attachmentUploading: "업로드 중 {progress}%", diff --git a/frontend/src/i18n/locales/ru-RU.ts b/frontend/src/i18n/locales/ru-RU.ts index 152b9218..3fe5bd02 100755 --- a/frontend/src/i18n/locales/ru-RU.ts +++ b/frontend/src/i18n/locales/ru-RU.ts @@ -3969,6 +3969,7 @@ export default { attachmentUploadTooltip: 'Загрузить вложение (документы, аудио и т.д.)', attachmentWithCount: 'Загружено файлов: {count}', attachmentTooMany: 'Максимум {max} файлов', + attachmentTotalTooMany: 'Изображений и вложений вместе не более {max}', attachmentTooLarge: 'Файл {name} превышает лимит {max} МБ', attachmentTypeNotSupported: 'Неподдерживаемый тип файла: {name}', attachmentUploading: 'Загрузка {progress}%', diff --git a/frontend/src/i18n/locales/zh-CN.ts b/frontend/src/i18n/locales/zh-CN.ts index f7b12573..9984d37e 100755 --- a/frontend/src/i18n/locales/zh-CN.ts +++ b/frontend/src/i18n/locales/zh-CN.ts @@ -1063,6 +1063,7 @@ export default { attachmentUploadTooltip: "上传附件(文档、音频等)", attachmentWithCount: "已上传 {count} 个附件", attachmentTooMany: "最多上传 {max} 个附件", + attachmentTotalTooMany: "图片和附件合计最多 {max} 个", attachmentTooLarge: "文件 {name} 超过 {max}MB 限制", attachmentTypeNotSupported: "不支持的文件类型:{name}", attachmentUploading: "上传中 {progress}%", diff --git a/internal/types/message.go b/internal/types/message.go index 331175f7..0de41c7c 100644 --- a/internal/types/message.go +++ b/internal/types/message.go @@ -73,8 +73,13 @@ func (m *MessageImages) Scan(value interface{}) error { // MessageAttachment represents a file attachment in a chat message type MessageAttachment struct { - ID string `json:"id,omitempty"` // Temporary document ID for session-scoped uploads - URL string `json:"url"` // Storage URL (provider://path) + ID string `json:"id,omitempty"` // Temporary document ID for session-scoped uploads + // URL is the internal storage handle (provider://path / resource://...). It is + // an internal locator only: previews are served via the session-scoped + // attachment endpoints, so this handle must never reach the client. Kept out + // of both JSON responses and DB serialization to avoid leaking a cross-session + // downloadable reference (see /files tenant-only check). + URL string `json:"-"` // Storage URL (provider://path) FileName string `json:"file_name"` // Original filename FileType string `json:"file_type"` // File extension (e.g., ".pdf", ".docx") FileSize int64 `json:"file_size"` // File size in bytes