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.
This commit is contained in:
wizardchen
2026-07-16 17:38:34 +08:00
committed by lyingbug
parent 85d10d99e3
commit 7ddac0385f
6 changed files with 36 additions and 2 deletions
+25
View File
@@ -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()
})
+1
View File
@@ -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}%',
+1
View File
@@ -1055,6 +1055,7 @@ export default {
attachmentUploadTooltip: "첨부 파일 업로드 (문서, 오디오 등)",
attachmentWithCount: "{count}개 파일 업로드됨",
attachmentTooMany: "최대 {max}개 파일까지 업로드 가능합니다",
attachmentTotalTooMany: "이미지와 첨부 파일은 합쳐서 최대 {max}개까지 가능합니다",
attachmentTooLarge: "파일 {name}이(가) {max}MB 제한을 초과합니다",
attachmentTypeNotSupported: "지원하지 않는 파일 형식: {name}",
attachmentUploading: "업로드 중 {progress}%",
+1
View File
@@ -3969,6 +3969,7 @@ export default {
attachmentUploadTooltip: 'Загрузить вложение (документы, аудио и т.д.)',
attachmentWithCount: 'Загружено файлов: {count}',
attachmentTooMany: 'Максимум {max} файлов',
attachmentTotalTooMany: 'Изображений и вложений вместе не более {max}',
attachmentTooLarge: 'Файл {name} превышает лимит {max} МБ',
attachmentTypeNotSupported: 'Неподдерживаемый тип файла: {name}',
attachmentUploading: 'Загрузка {progress}%',
+1
View File
@@ -1063,6 +1063,7 @@ export default {
attachmentUploadTooltip: "上传附件(文档、音频等)",
attachmentWithCount: "已上传 {count} 个附件",
attachmentTooMany: "最多上传 {max} 个附件",
attachmentTotalTooMany: "图片和附件合计最多 {max} 个",
attachmentTooLarge: "文件 {name} 超过 {max}MB 限制",
attachmentTypeNotSupported: "不支持的文件类型:{name}",
attachmentUploading: "上传中 {progress}%",
+7 -2
View File
@@ -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