From ceb4d1a310c66090854e7a8e21fc218649a03b52 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 16 Jul 2026 14:48:20 +0800 Subject: [PATCH] feat(chat): add session-scoped temporary attachments with async parsing Let users upload documents in chat without ingesting them into a knowledge base: files are parsed in the background, referenced by ID on send, and expire after a configurable TTL. --- .env.example | 21 + .env.lite.example | 14 + docker-compose.yml | 4 + frontend/src/api/agent/index.ts | 10 + frontend/src/api/chat/streame.ts | 5 +- .../src/api/chat/temporary-attachments.ts | 55 ++ frontend/src/api/system/index.ts | 1 + frontend/src/components/AttachmentUpload.vue | 172 +++- .../ChatAttachmentPreviewDrawer.vue | 271 +++++++ frontend/src/components/Input-field.vue | 18 +- frontend/src/components/document-preview.vue | 92 ++- .../useChatAttachmentPreviewDrawer.ts | 48 ++ frontend/src/i18n/embed.ts | 30 + frontend/src/i18n/locales/en-US.ts | 49 +- frontend/src/i18n/locales/ko-KR.ts | 49 +- frontend/src/i18n/locales/ru-RU.ts | 49 +- frontend/src/i18n/locales/zh-CN.ts | 49 +- .../src/utils/agent-tool-display.test.mjs | 11 + frontend/src/utils/agent-tool-display.ts | 14 + frontend/src/utils/agent-tool-icons.ts | 3 + .../utils/attachmentParsingDisplay.test.mjs | 54 ++ .../src/utils/attachmentParsingDisplay.ts | 63 ++ frontend/src/utils/attachmentPreview.test.mjs | 17 + frontend/src/utils/attachmentPreview.ts | 36 + .../src/utils/rag-pipeline-history.test.mjs | 55 ++ frontend/src/utils/rag-pipeline-history.ts | 14 + frontend/src/views/agent/AgentEditorModal.vue | 102 ++- .../AgentStreamDisplay.style.test.mjs | 12 +- .../chat/components/AgentStreamDisplay.vue | 33 +- .../RagPipelineProgress.style.test.mjs | 6 + .../chat/components/RagPipelineProgress.vue | 16 +- .../src/views/chat/components/usermsg.vue | 37 +- frontend/src/views/chat/index.vue | 91 ++- .../knowledge/settings/KBParserSettings.vue | 177 ++-- .../views/settings/ParserEngineSettings.vue | 70 ++ frontend/src/views/system/RuntimeQueues.vue | 1 + internal/agent/tools/persist.go | 9 + .../repository/temporary_document.go | 77 ++ .../application/service/temporary_document.go | 764 ++++++++++++++++++ .../service/temporary_document_test.go | 270 +++++++ internal/container/container.go | 28 + internal/handler/session/handler.go | 3 + internal/handler/session/qa.go | 212 ++++- .../handler/session/temporary_document.go | 200 +++++ internal/handler/session/types.go | 1 + internal/handler/system_runtime_test.go | 4 +- internal/router/router.go | 5 + internal/router/sync_task.go | 2 + internal/router/task.go | 2 + internal/types/custom_agent.go | 40 + internal/types/custom_agent_test.go | 18 + .../types/interfaces/temporary_document.go | 33 + internal/types/message.go | 38 +- internal/types/message_attachment_test.go | 30 + internal/types/parser_engine_config_test.go | 21 + internal/types/task.go | 48 +- internal/types/task_queue_test.go | 16 +- internal/types/temporary_document.go | 108 +++ internal/types/tenant.go | 22 +- migrations/sqlite/000000_init.down.sql | 1 + migrations/sqlite/000000_init.up.sql | 30 + .../000070_temporary_documents.down.sql | 1 + .../000070_temporary_documents.up.sql | 29 + 63 files changed, 3513 insertions(+), 248 deletions(-) create mode 100644 frontend/src/api/chat/temporary-attachments.ts create mode 100644 frontend/src/components/ChatAttachmentPreviewDrawer.vue create mode 100644 frontend/src/composables/useChatAttachmentPreviewDrawer.ts create mode 100644 frontend/src/utils/attachmentParsingDisplay.test.mjs create mode 100644 frontend/src/utils/attachmentParsingDisplay.ts create mode 100644 frontend/src/utils/attachmentPreview.test.mjs create mode 100644 frontend/src/utils/attachmentPreview.ts create mode 100644 internal/application/repository/temporary_document.go create mode 100644 internal/application/service/temporary_document.go create mode 100644 internal/application/service/temporary_document_test.go create mode 100644 internal/handler/session/temporary_document.go create mode 100644 internal/types/interfaces/temporary_document.go create mode 100644 internal/types/message_attachment_test.go create mode 100644 internal/types/parser_engine_config_test.go create mode 100644 internal/types/temporary_document.go create mode 100644 migrations/versioned/000070_temporary_documents.down.sql create mode 100644 migrations/versioned/000070_temporary_documents.up.sql diff --git a/.env.example b/.env.example index 0f108e9b..4e30d8b9 100644 --- a/.env.example +++ b/.env.example @@ -779,3 +779,24 @@ DOCREADER_TRANSPORT=grpc # Background sweep that recovers knowledge stuck in "processing" past # DocumentProcessTimeout + 10m. Set to "false" to opt out (not recommended). # WEKNORA_HOUSEKEEPING_ENABLED=true + +# Lifetime of parsed chat attachments and their source/extracted image files. +# Messages keep only the selected prompt excerpts needed by conversation +# history. Default: 24 hours. +# WEKNORA_CHAT_ATTACHMENT_TTL_HOURS=24 + +# How long a chat turn waits (seconds) for still-parsing attachments before +# proceeding with only the finished ones. Large or scanned PDFs may need more +# time to parse; raise this if attachments are frequently skipped. Default: 60. +# A per-agent "Attachment Parse Wait Timeout" (when > 0) overrides this global. +# WEKNORA_CHAT_ATTACHMENT_WAIT_TIMEOUT_SEC=60 + +# Chat attachment VLM OCR tuning (scanned / image-only documents). The slow part +# of parsing is per-page VLM OCR, not the document reader. OCR_CONCURRENCY sets +# how many pages are OCR'd in parallel (default 8, matching the page cap so a +# scan finishes in one wave — raising it loads the VLM harder; this stays global +# because all agents share the VLM backend). OCR_MAX_PAGES caps how many pages of +# a scanned document are sent to the VLM (default 8); a per-agent "Scanned OCR +# Max Pages" (when > 0) overrides this global. +# WEKNORA_CHAT_ATTACHMENT_OCR_CONCURRENCY=8 +# WEKNORA_CHAT_ATTACHMENT_OCR_MAX_PAGES=8 diff --git a/.env.lite.example b/.env.lite.example index f88f5c54..3d9882a3 100644 --- a/.env.lite.example +++ b/.env.lite.example @@ -56,6 +56,20 @@ DISABLE_REGISTRATION=false # === 性能 === CONCURRENCY_POOL_SIZE=3 +# 聊天附件解析产物保留时长(小时),默认 24 小时 +# WEKNORA_CHAT_ATTACHMENT_TTL_HOURS=24 + +# 单轮问答等待附件解析完成的最长时间(秒),超时后未完成的附件会被跳过。 +# 大文件 / 扫描件解析较慢时可调大,默认 60 秒。智能体里的「附件解析等待超时」> 0 时优先于此全局值。 +# WEKNORA_CHAT_ATTACHMENT_WAIT_TIMEOUT_SEC=60 + +# 附件 VLM OCR 调优(扫描件 / 图片型文档)。解析慢的瓶颈是逐页 VLM OCR,而非文档解析器。 +# OCR_CONCURRENCY 为并行 OCR 的页数(默认 8,与页数上限一致,让扫描件一波跑完;调大会加重 VLM 负载, +# 所有智能体共享 VLM 后端,故保持全局);OCR_MAX_PAGES 为扫描件送入 VLM 的最大页数(默认 8), +# 智能体里的「扫描件 OCR 最大页数」> 0 时优先于此全局值。 +# WEKNORA_CHAT_ATTACHMENT_OCR_CONCURRENCY=8 +# WEKNORA_CHAT_ATTACHMENT_OCR_MAX_PAGES=8 + # Docreader 地址 DOCREADER_ADDR=127.0.0.1:50051 diff --git a/docker-compose.yml b/docker-compose.yml index 9e560f40..978ec944 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -181,6 +181,10 @@ services: - CRYPTO_SALT=${CRYPTO_SALT:-} # File size limit (in MB) - MAX_FILE_SIZE_MB=${MAX_FILE_SIZE_MB:-50} + - WEKNORA_CHAT_ATTACHMENT_TTL_HOURS=${WEKNORA_CHAT_ATTACHMENT_TTL_HOURS:-24} + - WEKNORA_CHAT_ATTACHMENT_WAIT_TIMEOUT_SEC=${WEKNORA_CHAT_ATTACHMENT_WAIT_TIMEOUT_SEC:-60} + - WEKNORA_CHAT_ATTACHMENT_OCR_CONCURRENCY=${WEKNORA_CHAT_ATTACHMENT_OCR_CONCURRENCY:-8} + - WEKNORA_CHAT_ATTACHMENT_OCR_MAX_PAGES=${WEKNORA_CHAT_ATTACHMENT_OCR_MAX_PAGES:-8} # Agent Skills Sandbox - WEKNORA_SANDBOX_MODE=${WEKNORA_SANDBOX_MODE:-docker} - WEKNORA_SANDBOX_TIMEOUT=${WEKNORA_SANDBOX_TIMEOUT:-60} diff --git a/frontend/src/api/agent/index.ts b/frontend/src/api/agent/index.ts index b74c95ab..99320165 100644 --- a/frontend/src/api/agent/index.ts +++ b/frontend/src/api/agent/index.ts @@ -79,6 +79,16 @@ export interface CustomAgentConfig { image_storage_provider?: string; // 图片存储提供商 audio_upload_enabled?: boolean; // 是否启用音频上传/ASR转录(默认: false) asr_model_id?: string; // ASR模型ID(音频转录用) + // 附件图片理解 / 扫描件 OCR 开关(默认: false,开启会增加解析耗时) + attachment_image_understanding?: boolean; + // 扫描件 OCR 最大页数(0 = 使用全局默认 WEKNORA_CHAT_ATTACHMENT_OCR_MAX_PAGES) + attachment_ocr_max_pages?: number; + // 单轮问答等待附件解析完成的最长时间(秒,0 = 使用全局默认 WEKNORA_CHAT_ATTACHMENT_WAIT_TIMEOUT_SEC) + attachment_parse_wait_timeout_sec?: number; + + // ===== 聊天附件解析引擎策略 ===== + // 按文件类型选择解析引擎;优先级:请求 parser_engine > 智能体规则 > 租户规则 > auto + chat_parser_engine_rules?: { file_types: string[]; engine: string }[]; // ===== 文件类型限制 ===== // 支持的文件类型(如 ["csv", "xlsx", "xls"]) diff --git a/frontend/src/api/chat/streame.ts b/frontend/src/api/chat/streame.ts index 4d4f6595..528f6db4 100644 --- a/frontend/src/api/chat/streame.ts +++ b/frontend/src/api/chat/streame.ts @@ -36,7 +36,7 @@ export function useStream() { let renderTimer: number | null = null // 启动流式请求 - const startStream = async (params: { session_id: any; query: any; knowledge_base_ids?: string[]; knowledge_ids?: string[]; tag_ids?: string[]; agent_enabled?: boolean; agent_id?: string; web_search_enabled?: boolean; enable_memory?: boolean; summary_model_id?: string; mcp_service_ids?: string[]; skill_names?: string[]; mentioned_items?: Array<{id: string; name: string; type: string; kb_type?: string; kb_id?: string; kb_name?: string; service_id?: string; skill_name?: string}>; images?: Array<{data: string}>; attachment_uploads?: Array<{data: string; file_name: string; file_size: number}>; suggestion_attribution?: { suggestion_set_id: string; question_id: string }; method: string; url: string; embed_token?: string; embed_session_sig?: string; embed_visitor_id?: string }) => { + const startStream = async (params: { session_id: any; query: any; knowledge_base_ids?: string[]; knowledge_ids?: string[]; tag_ids?: string[]; agent_enabled?: boolean; agent_id?: string; web_search_enabled?: boolean; enable_memory?: boolean; summary_model_id?: string; mcp_service_ids?: string[]; skill_names?: string[]; mentioned_items?: Array<{id: string; name: string; type: string; kb_type?: string; kb_id?: string; kb_name?: string; service_id?: string; skill_name?: string}>; images?: Array<{data: string}>; attachment_uploads?: Array<{data: string; file_name: string; file_size: number}>; attachment_ids?: string[]; suggestion_attribution?: { suggestion_set_id: string; question_id: string }; method: string; url: string; embed_token?: string; embed_session_sig?: string; embed_visitor_id?: string }) => { const myGeneration = ++streamGeneration // 重置状态 output.value = ''; @@ -133,6 +133,9 @@ export function useStream() { if (params.attachment_uploads !== undefined && params.attachment_uploads.length > 0) { postBody.attachment_uploads = params.attachment_uploads; } + if (params.attachment_ids !== undefined && params.attachment_ids.length > 0) { + postBody.attachment_ids = params.attachment_ids; + } if (params.suggestion_attribution) { postBody.suggestion_attribution = params.suggestion_attribution; } diff --git a/frontend/src/api/chat/temporary-attachments.ts b/frontend/src/api/chat/temporary-attachments.ts new file mode 100644 index 00000000..ca11d8b2 --- /dev/null +++ b/frontend/src/api/chat/temporary-attachments.ts @@ -0,0 +1,55 @@ +import { del, get, getDown, postUpload } from '@/utils/request'; + +export type TemporaryAttachmentStatus = 'uploaded' | 'processing' | 'ready' | 'failed'; + +export interface TemporaryAttachment { + id: string; + session_id: string; + file_name: string; + file_type: string; + file_size: number; + mime_type?: string; + status: TemporaryAttachmentStatus; + token_count: number; + chunk_count: number; + image_refs?: Array<{ original_ref?: string; url: string; mime_type?: string }>; + error_message?: string; + expires_at: string; +} + +interface AttachmentResponse { + success: boolean; + data: TemporaryAttachment; +} + +export function uploadTemporaryAttachment( + sessionId: string, + file: File, + agentId?: string, + parserEngine?: string, + onProgress?: (percent: number) => void, +): Promise { + const form = new FormData(); + form.append('file', file); + if (agentId) form.append('agent_id', agentId); + if (parserEngine) form.append('parser_engine', parserEngine); + return postUpload( + `/api/v1/sessions/${sessionId}/attachments`, + form, + (event) => { + if (event.total) onProgress?.(Math.round((event.loaded * 100) / event.total)); + }, + ); +} + +export function getTemporaryAttachment(sessionId: string, attachmentId: string): Promise { + return get(`/api/v1/sessions/${sessionId}/attachments/${attachmentId}`); +} + +export function previewTemporaryAttachment(sessionId: string, attachmentId: string): Promise { + return getDown(`/api/v1/sessions/${sessionId}/attachments/${attachmentId}/preview`); +} + +export function deleteTemporaryAttachment(sessionId: string, attachmentId: string): Promise { + return del(`/api/v1/sessions/${sessionId}/attachments/${attachmentId}`); +} diff --git a/frontend/src/api/system/index.ts b/frontend/src/api/system/index.ts index 01b105ca..92aa30e4 100644 --- a/frontend/src/api/system/index.ts +++ b/frontend/src/api/system/index.ts @@ -73,6 +73,7 @@ export interface ParserEngineInfo { /** 解析引擎配置(引擎相关存空间;docreader 地址由环境变量配置) */ export interface ParserEngineConfig { + chat_parser_engine_rules?: Array<{ file_types: string[]; engine: string }> docreader_addr?: string docreader_transport?: string mineru_endpoint?: string diff --git a/frontend/src/components/AttachmentUpload.vue b/frontend/src/components/AttachmentUpload.vue index 8f63f70d..70e9f62a 100644 --- a/frontend/src/components/AttachmentUpload.vue +++ b/frontend/src/components/AttachmentUpload.vue @@ -1,7 +1,15 @@ + + + + + + diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index f71bf5af..fedac9a0 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -1849,6 +1849,21 @@ const createSession = async (val: string) => { if (props.isReplying) { return MessagePlugin.error(t('input.messages.replying')); } + // Only block while the file is still uploading (no document ID yet). Once + // uploaded, sending is allowed even if parsing is still in progress: the + // backend shows a "parsing attachment" step on the timeline and waits. + const pendingAttachment = uploadedAttachments.value.find(item => + item.status === 'uploading' + ); + if (pendingAttachment) { + MessagePlugin.warning(t('chat.attachmentStillProcessing', { name: pendingAttachment.name })); + return; + } + const failedAttachment = uploadedAttachments.value.find(item => item.status === 'failed'); + if (failedAttachment) { + MessagePlugin.error(failedAttachment.error || t('chat.attachmentParseFailed')); + return; + } // Embed 渠道由后端绑定 agent/KB,勿走平台侧 agent 列表与就绪校验 if (props.embeddedMode) { @@ -2446,7 +2461,8 @@ defineExpose({ - diff --git a/frontend/src/components/document-preview.vue b/frontend/src/components/document-preview.vue index 6892f009..7a15bbc6 100644 --- a/frontend/src/components/document-preview.vue +++ b/frontend/src/components/document-preview.vue @@ -2,6 +2,7 @@