Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ui/src/api/API_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,10 @@ PUT `/v3/application/<applicationId>/chat/<chatId>/chat_record/<recordId>/vote`
`1`(不满意)字符串,`vote_reason` 为单选原因,`vote_other_content` 为补充说明。
满意原因支持 `accurate`、`complete`、`other`,不满意支持 `inaccurate`、`incomplete`、`other`。
已评价记录须先撤销才能再次评价;历史记录通过同名字段回显。

## URL 附件读取

Admin 与 Chat 的 `file.ts` 提供 `getUrlFile(applicationId, url)`,请求 `/oss/get_url/{applicationId}`,
返回共享类型 `UrlFileContent`:响应状态、Content-Type、Content-Length 和 content。
后端对 text/json 类型返回文本原文,其他内容返回 Base64。对话装配层注入各入口的文件 API,
输入组件负责类型、大小校验及需要落库的文件转上传,不跨入口复用请求客户端。
9 changes: 6 additions & 3 deletions ui/src/api/admin/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AxiosProgressEvent } from 'axios'
import { postUpload, del } from './core/request'
import type { FileSourceType } from '@/api/types'
import { postUpload, del, get } from './core/request'
import type { FileSourceType, UrlFileContent } from '@/api/types'

/** 上传资源文件,支持可选的进度回调与取消操作。 */
const postUploadFile = (
Expand All @@ -19,4 +19,7 @@ const postUploadFile = (
/** 删除已上传的资源文件。 */
const deleteFile = (fileId: string) => del(`/oss/file/${fileId}`)

export default { postUploadFile, deleteFile }
/** 获取 URL 文件内容、类型和大小。 */
const getUrlFile = (applicationId: string, url: string) => get<UrlFileContent>(`/oss/get_url/${applicationId}`, { url })

export default { getUrlFile, postUploadFile, deleteFile }
9 changes: 6 additions & 3 deletions ui/src/api/chat/file.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AxiosProgressEvent } from 'axios'
import { postUpload } from './core/request'
import type { FileSourceType } from '@/api/types'
import { postUpload, get } from './core/request'
import type { FileSourceType, UrlFileContent } from '@/api/types'

/** 上传资源文件,支持可选的进度回调与取消操作。 */
const postUploadFile = (
Expand All @@ -16,4 +16,7 @@ const postUploadFile = (
return postUpload<string>('/oss/file', formData, onProgress)
}

export default { postUploadFile }
/** 获取 URL 文件内容、类型和大小。 */
const getUrlFile = (applicationId: string, url: string) => get<UrlFileContent>(`/oss/get_url/${applicationId}`, { url })

export default { getUrlFile, postUploadFile }
8 changes: 8 additions & 0 deletions ui/src/api/types/file.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import type { FILE_SOURCE_TYPE } from '@/api/enums'

export type FileSourceType = (typeof FILE_SOURCE_TYPE)[keyof typeof FILE_SOURCE_TYPE]

/** 服务端读取远程 URL 后返回的内容与响应头。 */
export interface UrlFileContent {
status_code: number
'Content-Type': string
'Content-Length': string | number | null
content: string
}
136 changes: 129 additions & 7 deletions ui/src/conversation-panel/components/message-input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { pick } from 'lodash'
import { IMAGE_EXTENSIONS, DOCUMENT_EXTENSIONS, VIDEO_EXTENSIONS, AUDIO_EXTENSIONS } from '@/constants/file-type'
import { isImage, isDocument, isAudio, isVideo, getFileExtension } from '@/utils/icon'
import { MsgError, MsgWarning } from '@/utils/message'
import type { Dict } from '@/api/types'
import { URL_FILE_MIME_TYPES, createUrlFile } from './url-upload/index'
import type { UrlFileContent, Dict } from '@/api/types'
import { defaultFileUploadSetting } from '@/workflow-canvas/nodes/base-node/constant'
import type { BaseNodeForm, FileUploadSettingData } from '@/workflow-canvas/nodes/base-node/types'
import type { ChatMessage, Conversation, ConversationAppInfo, SendMessageOptions } from '../../core/types'
Expand All @@ -14,7 +15,8 @@ export interface FileItem {
uid: number
name: string
size: number
raw: File
raw?: File
attachmentType?: MessageAttachmentKey
url?: string
file_id?: string
previewUrl?: string
Expand All @@ -40,6 +42,7 @@ export interface QuestionOptions {

// 仅注入消息输入所需的会话、上传和发送能力。
export interface MessageInputDeps {
getUrlFile: (url: string) => Promise<UrlFileContent>
// 来自 conversation-list
conversations: Ref<Conversation[]>
getChatId: () => string
Expand Down Expand Up @@ -79,6 +82,7 @@ export function createMessageInputStore(deps: MessageInputDeps) {
const question = ref('')
const fileList = ref<FileItem[]>([])
const sending = ref(false)
const urlUploading = ref(false)

// 文件上传配置:优先读取开始节点,缺失时回退到智能体字段,再补齐默认值。
const baseNodeData = computed(() => {
Expand All @@ -92,7 +96,6 @@ export function createMessageInputStore(deps: MessageInputDeps) {
const fileUploadEnabled = computed(() => Boolean(baseNodeData.value.file_upload_enable ?? appInfo.value?.file_upload_enable))
/** 本地上传:控制上传按钮、拖拽与粘贴入口。 */
const uploadEnabled = computed(() => fileUploadEnabled.value && uploadSetting.value.local_upload)
// TODO: URL 上传入口未实现,页面接入时使用 urlUploadEnabled 控制显示,并复用数量、大小、类型限制。
const urlUploadEnabled = computed(() => fileUploadEnabled.value && uploadSetting.value.url_upload)
const maxFiles = computed(() => uploadSetting.value.maxFiles)
const maxSizeMB = computed(() => uploadSetting.value.fileLimit)
Expand All @@ -110,7 +113,13 @@ export function createMessageInputStore(deps: MessageInputDeps) {
})
const acceptList = computed(() => allowedExtensions.value.map((extension) => `.${extension}`).join(','))
const canUpload = computed(
() => uploadEnabled.value && !disabled.value && !streamLoading.value && !sending.value && fileList.value.length < maxFiles.value,
() =>
(uploadEnabled.value || urlUploadEnabled.value) &&
!disabled.value &&
!streamLoading.value &&
!sending.value &&
!urlUploading.value &&
fileList.value.length < maxFiles.value,
)

// 语音输入:智能体开启语音转文字且当前模式提供接口时可用
Expand All @@ -131,10 +140,118 @@ export function createMessageInputStore(deps: MessageInputDeps) {
!streamLoading.value &&
!sending.value &&
!transcribing.value &&
!urlUploading.value &&
attachmentsReady.value,
)

const imageFiles = computed(() => fileList.value.filter((attachment) => isImage(attachment.name)))
const imageFiles = computed(() =>
fileList.value.filter((attachment) => attachment.attachmentType === 'image_list' || (!attachment.attachmentType && isImage(attachment.name))),
)

// URL 上传:服务端读取远程文件,校验后按类型保留地址或转为本地上传。
const urlUploadTypes = computed(() =>
[
{ enabled: uploadSetting.value.document, value: 'document_list' as const, label: '文档' },
{ enabled: uploadSetting.value.image, value: 'image_list' as const, label: '图片' },
{ enabled: uploadSetting.value.audio, value: 'audio_list' as const, label: '音频' },
{ enabled: uploadSetting.value.video, value: 'video_list' as const, label: '视频' },
{ enabled: uploadSetting.value.other, value: 'other_list' as const, label: '其他' },
].filter((type) => type.enabled),
)
const addUrls = async (addresses: string[], attachmentType: MessageAttachmentKey): Promise<boolean> => {
if (
!addresses.length ||
urlUploading.value ||
!urlUploadEnabled.value ||
!canUpload.value ||
!urlUploadTypes.value.some((type) => type.value === attachmentType)
)
return false
if (fileList.value.length + addresses.length > maxFiles.value) {
MsgWarning(`单次最多上传 ${maxFiles.value} 个文件`)
return false
}
const validAddresses = addresses.filter((address) => {
try {
return ['http:', 'https:'].includes(new URL(address).protocol)
} catch {
return false
}
})
if (validAddresses.length !== addresses.length) MsgWarning('请输入有效的 HTTP 或 HTTPS URL 地址')
if (!validAddresses.length) return false
const extensions =
attachmentType === 'image_list'
? IMAGE_EXTENSIONS
: attachmentType === 'document_list'
? DOCUMENT_EXTENSIONS
: attachmentType === 'audio_list'
? AUDIO_EXTENSIONS
: attachmentType === 'video_list'
? VIDEO_EXTENSIONS
: (uploadSetting.value.otherExtensions ?? [])
const expectedMimes = extensions.flatMap((extension) => URL_FILE_MIME_TYPES[extension.toLowerCase()] ?? [])
// 先占用附件名额,避免并发上传超限,也阻止校验完成前发送。
const attachments = validAddresses.map((address) =>
reactive<FileItem>({
uid: Date.now() + Math.random(),
name: new URL(address).pathname.split('/').filter(Boolean).pop() || `file_${Date.now()}`,
size: 0,
url: address,
attachmentType,
status: 'uploading',
progress: 0,
}),
)
fileList.value.push(...attachments)
urlUploading.value = true
try {
const results = await Promise.all(
attachments.map(async (attachment) => {
try {
const response = await deps.getUrlFile(attachment.url!)
if (!fileList.value.includes(attachment)) return false
if (response.status_code !== 200) throw new Error('URL 地址无效')
const mime = (response['Content-Type'] || '').split(';')[0]!.trim().toLowerCase()
if (expectedMimes.length && !expectedMimes.includes(mime)) throw new Error('文件类型与所选类型不匹配')
const size = Number(response['Content-Length']) || 0
if (size > maxSizeMB.value * 1024 * 1024) throw new Error(`每个文件不能超过 ${maxSizeMB.value}MB`)
if (!attachment.name.includes('.')) {
const extension =
extensions.find((extension) => URL_FILE_MIME_TYPES[extension.toLowerCase()] === mime) ??
Object.keys(URL_FILE_MIME_TYPES).find((extension) => URL_FILE_MIME_TYPES[extension] === mime)
if (extension) attachment.name += `.${extension.toLowerCase()}`
}
attachment.size = size
if (attachmentType === 'document_list' || attachmentType === 'audio_list' || attachmentType === 'other_list') {
const file = createUrlFile(response.content, attachment.name, mime)
if (file.size > maxSizeMB.value * 1024 * 1024) throw new Error(`每个文件不能超过 ${maxSizeMB.value}MB`)
if (!file.size) throw new Error('不能上传空文件')
attachment.raw = file
attachment.size = file.size
attachment.url = undefined
attachment.status = 'ready'
await uploadAttachment(attachment)
return fileList.value.some((file) => file.uid === attachment.uid && file.status === 'success')
}
attachment.status = 'success'
attachment.progress = 100
return true
} catch (error) {
const index = fileList.value.indexOf(attachment)
if (index >= 0) {
removeFile(index)
MsgWarning(`${attachment.url || attachment.name}:${error instanceof Error ? error.message : '读取 URL 文件失败'}`)
}
return false
}
}),
)
return results.some(Boolean)
} finally {
urlUploading.value = false
}
}

// 附件上传与本地预览
/** 校验单个文件,返回不通过的提示;拖拽、粘贴不受 accept 限制,需校验扩展名。 */
Expand Down Expand Up @@ -171,9 +288,10 @@ export function createMessageInputStore(deps: MessageInputDeps) {
attachment.status = 'uploading'
attachment.progress = 0
attachment.errMsg = ''
void Promise.resolve()
return Promise.resolve()
.then(() => {
if (!fileList.value.includes(attachment)) return
if (!attachment.raw) return
return uploadFile(attachment.raw, getChatId(), (percent) => {
if (!fileList.value.includes(attachment)) return
// 文件传输结束后仍等待服务端处理,成功响应前最多显示 95%。
Expand Down Expand Up @@ -277,7 +395,8 @@ export function createMessageInputStore(deps: MessageInputDeps) {
const message: QuestionMessage = { type: 'QUESTION', content: question.value.trim() }
for (const attachment of fileList.value) {
let attachmentKey: MessageAttachmentKey = 'other_list'
if (isImage(attachment.name)) attachmentKey = 'image_list'
if (attachment.attachmentType) attachmentKey = attachment.attachmentType
else if (isImage(attachment.name)) attachmentKey = 'image_list'
else if (isDocument(attachment.name)) attachmentKey = 'document_list'
else if (isAudio(attachment.name)) attachmentKey = 'audio_list'
else if (isVideo(attachment.name)) attachmentKey = 'video_list'
Expand Down Expand Up @@ -344,6 +463,9 @@ export function createMessageInputStore(deps: MessageInputDeps) {
fileList,
uploadEnabled,
urlUploadEnabled,
urlUploadTypes,
urlUploading,
addUrls,
canUpload,
maxFiles,
maxSizeMB,
Expand Down
25 changes: 18 additions & 7 deletions ui/src/conversation-panel/components/message-input/index.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { Phone } from '@element-plus/icons-vue'
import UrlUpload from './url-upload/index.vue'
import { isAudio, isVideo } from '@/utils/icon'
import { useInputEvents } from '../../core/input-events'
import { useMessageInputStore } from './index'
Expand All @@ -13,6 +13,7 @@ const {
question,
fileList,
uploadEnabled,
urlUploadEnabled,
canUpload,
maxFiles,
maxSizeMB,
Expand All @@ -33,15 +34,22 @@ const {
} = useMessageInputStore()

// 所有附件入口共用禁用条件,上传和发送仍由输入 Store 处理。
const { fileInputRef, handleFileSelect, handlePaste, handleDragover, handleDrop, handleKeydown } = useInputEvents({
const { fileInputRef, handlePaste, handleDragover, handleDrop, handleKeydown } = useInputEvents({
text: question,
canUpload: () => canUpload.value,
canUpload: () => canUpload.value && uploadEnabled.value,
addFiles,
submit: () => {
if (canSend.value) send()
},
})

// 本地选择仅通过明确的上传按钮进入。
function handleLocalSelect(event: Event) {
const input = event.target as HTMLInputElement
if (uploadEnabled.value && canUpload.value) addFiles(input.files)
input.value = ''
}

// 语音输入:点击麦克风开始录音,点击停止后转写到输入框
const { recording, seconds: recordSeconds, waveLevels, start: startRecording, stop: stopRecording } = useVoiceRecorder(transcribe)
</script>
Expand Down Expand Up @@ -100,9 +108,12 @@ const { recording, seconds: recordSeconds, waveLevels, start: startRecording, st

<!-- 操作栏 -->
<div class="mt-3 flex items-center">
<!-- 用户输入参数表单 -->
<UserForm class="mr-3" />
<input v-if="uploadEnabled" ref="fileInputRef" type="file" multiple :accept="acceptList" class="hidden" @change="handleFileSelect" />
<MkTooltip v-if="uploadEnabled" placement="top">
<!-- 本地文件选择:供直接上传按钮和 URL 浮层的本地上传入口共用 -->
<input v-if="uploadEnabled" ref="fileInputRef" type="file" multiple :accept="acceptList" class="hidden" @change="handleLocalSelect" />
<!-- 仅本地上传模式:展示拖拽提示,点击附件按钮直接选择文件 -->
<MkTooltip v-if="uploadEnabled && !urlUploadEnabled" placement="top">
<template #content>
<div class="break-all whitespace-pre-wrap">
可拖拽到输入框内上传文件
Expand All @@ -112,11 +123,12 @@ const { recording, seconds: recordSeconds, waveLevels, start: startRecording, st
文件类型:{{ allowedExtensions.join('、') }}
</div>
</template>
<!-- 上传文件 -->
<el-button text :disabled="!canUpload" @click="fileInputRef?.click()">
<MkIcon name="icon_attachment_outlined" :size="18" class="text-N900!" />
</el-button>
</MkTooltip>
<!-- URL 上传模式:附件按钮打开浮层,本地上传事件触发文件选择 -->
<UrlUpload v-if="urlUploadEnabled" @local-upload="fileInputRef?.click()" />
<!-- 语音输入:点击开始录音 -->
<el-button
v-if="speechEnabled"
Expand All @@ -126,7 +138,6 @@ const { recording, seconds: recordSeconds, waveLevels, start: startRecording, st
:disabled="disabled || streamLoading || transcribing"
@click="startRecording"
>
<!-- todo 这块icon记得换 先用饿了么的了 -->
<MkIcon v-if="!transcribing" name="icon_mic_outlined" :size="18" class="text-N900!" />
</el-button>
<el-divider direction="vertical" class="ml-3! mr-4!" />
Expand Down
Loading
Loading