2025-12-19 04:04:48 +08:00
|
|
|
<template>
|
|
|
|
|
<MessageInputComponent
|
|
|
|
|
ref="inputRef"
|
|
|
|
|
:model-value="modelValue"
|
|
|
|
|
@update:modelValue="updateValue"
|
|
|
|
|
:is-loading="isLoading"
|
|
|
|
|
:disabled="disabled"
|
|
|
|
|
:send-button-disabled="sendButtonDisabled"
|
|
|
|
|
:placeholder="placeholder"
|
|
|
|
|
@send="handleSend"
|
|
|
|
|
@keydown="handleKeyDown"
|
|
|
|
|
>
|
|
|
|
|
<template #top>
|
|
|
|
|
<ImagePreviewComponent
|
|
|
|
|
v-if="currentImage"
|
|
|
|
|
:image-data="currentImage"
|
|
|
|
|
@remove="handleImageRemoved"
|
|
|
|
|
class="image-preview-wrapper"
|
|
|
|
|
/>
|
|
|
|
|
</template>
|
|
|
|
|
<template #options-left>
|
|
|
|
|
<AttachmentOptionsComponent
|
|
|
|
|
v-if="supportsFileUpload"
|
|
|
|
|
:disabled="disabled"
|
|
|
|
|
@upload="handleAttachmentUpload"
|
|
|
|
|
@upload-image="handleImageUpload"
|
|
|
|
|
@upload-image-success="handleImageUploadSuccess"
|
|
|
|
|
/>
|
|
|
|
|
</template>
|
|
|
|
|
<template #actions-left>
|
|
|
|
|
<AttachmentStatusIndicator
|
|
|
|
|
:attachments="currentAttachments"
|
|
|
|
|
:disabled="disabled"
|
|
|
|
|
@remove="handleAttachmentRemove"
|
|
|
|
|
/>
|
|
|
|
|
</template>
|
|
|
|
|
</MessageInputComponent>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup>
|
2026-01-15 06:01:34 +08:00
|
|
|
import { ref, reactive, computed, watch, nextTick } from 'vue'
|
|
|
|
|
import { message } from 'ant-design-vue'
|
|
|
|
|
import MessageInputComponent from '@/components/MessageInputComponent.vue'
|
|
|
|
|
import ImagePreviewComponent from '@/components/ImagePreviewComponent.vue'
|
|
|
|
|
import AttachmentOptionsComponent from '@/components/AttachmentOptionsComponent.vue'
|
|
|
|
|
import AttachmentStatusIndicator from '@/components/AttachmentStatusIndicator.vue'
|
|
|
|
|
import { threadApi } from '@/apis'
|
|
|
|
|
import { AgentValidator } from '@/utils/agentValidator'
|
|
|
|
|
import { handleChatError, handleValidationError } from '@/utils/errorHandler'
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
modelValue: { type: String, default: '' },
|
|
|
|
|
isLoading: { type: Boolean, default: false },
|
|
|
|
|
disabled: { type: Boolean, default: false },
|
|
|
|
|
sendButtonDisabled: { type: Boolean, default: false },
|
|
|
|
|
placeholder: { type: String, default: '输入问题...' },
|
|
|
|
|
supportsFileUpload: { type: Boolean, default: false },
|
|
|
|
|
agentId: { type: String, default: '' },
|
|
|
|
|
threadId: { type: String, default: null },
|
|
|
|
|
ensureThread: { type: Function, required: true }
|
2026-01-15 06:01:34 +08:00
|
|
|
})
|
2025-12-19 04:04:48 +08:00
|
|
|
|
2026-01-15 06:01:34 +08:00
|
|
|
const emit = defineEmits(['update:modelValue', 'send', 'keydown'])
|
2025-12-19 04:04:48 +08:00
|
|
|
|
2026-01-15 06:01:34 +08:00
|
|
|
const inputRef = ref(null)
|
|
|
|
|
const currentImage = ref(null)
|
2025-12-19 04:04:48 +08:00
|
|
|
const attachmentState = reactive({
|
|
|
|
|
itemsByThread: {},
|
|
|
|
|
limits: null,
|
2026-01-15 06:01:34 +08:00
|
|
|
isUploading: false
|
|
|
|
|
})
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const updateValue = (val) => {
|
2026-01-15 06:01:34 +08:00
|
|
|
emit('update:modelValue', val)
|
|
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const currentAttachments = computed(() => {
|
2026-01-15 06:01:34 +08:00
|
|
|
if (!props.threadId) return []
|
|
|
|
|
return attachmentState.itemsByThread[props.threadId] || []
|
|
|
|
|
})
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const loadThreadAttachments = async (threadId, { silent = false } = {}) => {
|
2026-01-15 06:01:34 +08:00
|
|
|
if (!threadId) return
|
2025-12-19 04:04:48 +08:00
|
|
|
try {
|
2026-01-15 06:01:34 +08:00
|
|
|
const response = await threadApi.getThreadAttachments(threadId)
|
|
|
|
|
attachmentState.itemsByThread[threadId] = response.attachments || []
|
2025-12-19 04:04:48 +08:00
|
|
|
if (response.limits) {
|
2026-01-15 06:01:34 +08:00
|
|
|
attachmentState.limits = response.limits
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
if (silent) {
|
2026-01-15 06:01:34 +08:00
|
|
|
console.warn('Failed to load attachments:', error)
|
2025-12-19 04:04:48 +08:00
|
|
|
} else {
|
2026-01-15 06:01:34 +08:00
|
|
|
handleChatError(error, 'load')
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const handleImageUpload = (imageData) => {
|
|
|
|
|
if (imageData && imageData.success) {
|
2026-01-15 06:01:34 +08:00
|
|
|
currentImage.value = imageData
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const handleImageRemoved = () => {
|
2026-01-15 06:01:34 +08:00
|
|
|
currentImage.value = null
|
|
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const handleImageUploadSuccess = () => {
|
|
|
|
|
if (inputRef.value) {
|
2026-01-15 06:01:34 +08:00
|
|
|
inputRef.value.closeOptions()
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const handleAttachmentUpload = async (files) => {
|
2026-01-15 06:01:34 +08:00
|
|
|
if (!files?.length) return
|
|
|
|
|
if (!AgentValidator.validateAgentIdWithError(props.agentId, '上传附件', handleValidationError))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
const preferredTitle = files[0]?.name || '新的对话'
|
|
|
|
|
let threadId = props.threadId
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
if (!threadId) {
|
|
|
|
|
try {
|
2026-01-15 06:01:34 +08:00
|
|
|
threadId = await props.ensureThread(preferredTitle)
|
2025-12-19 04:04:48 +08:00
|
|
|
} catch (e) {
|
2026-01-15 06:01:34 +08:00
|
|
|
return
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-15 06:01:34 +08:00
|
|
|
|
2025-12-19 04:04:48 +08:00
|
|
|
if (!threadId) {
|
2026-01-15 06:01:34 +08:00
|
|
|
message.error('创建对话失败,无法上传附件')
|
|
|
|
|
return
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 06:01:34 +08:00
|
|
|
attachmentState.isUploading = true
|
2025-12-19 04:04:48 +08:00
|
|
|
try {
|
|
|
|
|
for (const file of files) {
|
2026-01-15 06:01:34 +08:00
|
|
|
await threadApi.uploadThreadAttachment(threadId, file)
|
|
|
|
|
message.success(`${file.name} 上传成功`)
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
2026-01-15 06:01:34 +08:00
|
|
|
await loadThreadAttachments(threadId, { silent: true })
|
2025-12-19 04:04:48 +08:00
|
|
|
} catch (error) {
|
2026-01-15 06:01:34 +08:00
|
|
|
handleChatError(error, 'upload')
|
2025-12-19 04:04:48 +08:00
|
|
|
} finally {
|
2026-01-15 06:01:34 +08:00
|
|
|
attachmentState.isUploading = false
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const handleAttachmentRemove = async (fileId) => {
|
2026-01-15 06:01:34 +08:00
|
|
|
if (!fileId || !props.threadId) return
|
2025-12-19 04:04:48 +08:00
|
|
|
try {
|
2026-01-15 06:01:34 +08:00
|
|
|
await threadApi.deleteThreadAttachment(props.threadId, fileId)
|
|
|
|
|
await loadThreadAttachments(props.threadId, { silent: true })
|
|
|
|
|
message.success('附件已删除')
|
2025-12-19 04:04:48 +08:00
|
|
|
} catch (error) {
|
2026-01-15 06:01:34 +08:00
|
|
|
handleChatError(error, 'delete')
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const handleSend = () => {
|
2026-01-15 06:01:34 +08:00
|
|
|
emit('send', { image: currentImage.value })
|
|
|
|
|
currentImage.value = null
|
|
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
const handleKeyDown = (e) => {
|
|
|
|
|
if (e.key === 'Enter' && !e.shiftKey) {
|
2026-01-15 06:01:34 +08:00
|
|
|
e.preventDefault()
|
|
|
|
|
handleSend()
|
2025-12-19 04:04:48 +08:00
|
|
|
} else {
|
2026-01-15 06:01:34 +08:00
|
|
|
emit('keydown', e)
|
2025-12-19 04:04:48 +08:00
|
|
|
}
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
2025-12-19 04:04:48 +08:00
|
|
|
|
2026-01-15 06:01:34 +08:00
|
|
|
watch(
|
|
|
|
|
() => props.threadId,
|
|
|
|
|
(newId) => {
|
|
|
|
|
if (newId) {
|
|
|
|
|
loadThreadAttachments(newId, { silent: true })
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{ immediate: true }
|
|
|
|
|
)
|
2025-12-19 04:04:48 +08:00
|
|
|
|
|
|
|
|
defineExpose({
|
|
|
|
|
focus: () => inputRef.value?.focus(),
|
|
|
|
|
closeOptions: () => inputRef.value?.closeOptions()
|
2026-01-15 06:01:34 +08:00
|
|
|
})
|
2025-12-19 04:04:48 +08:00
|
|
|
</script>
|