feat: attachment_support

This commit is contained in:
MaoMengww
2026-04-10 12:49:20 +08:00
parent 69785ad0a3
commit aaee5f044a
26 changed files with 1029 additions and 19 deletions
@@ -394,6 +394,35 @@
</a>
</div>
</div>
<!-- 音频上传开关 -->
<div class="setting-row">
<div class="setting-info">
<label>{{ $t('agentEditor.audioUpload.label') }}</label>
<p class="desc">{{ $t('agentEditor.audioUpload.desc') }}</p>
</div>
<div class="setting-control">
<t-switch v-model="formData.config.audio_upload_enabled" />
</div>
</div>
<!-- ASR模型(音频上传启用时) -->
<div v-if="formData.config.audio_upload_enabled" class="setting-row">
<div class="setting-info">
<label>{{ $t('agentEditor.audioUpload.asrModel') }}</label>
<p class="desc">{{ $t('agentEditor.audioUpload.asrModelDesc') }}</p>
</div>
<div class="setting-control">
<ModelSelector
model-type="ASR"
:selected-model-id="formData.config.asr_model_id"
:all-models="allModels"
@update:selected-model-id="(val: string) => formData.config.asr_model_id = val"
@add-model="handleAddModel('asr')"
:placeholder="$t('agentEditor.audioUpload.asrModelPlaceholder')"
/>
</div>
</div>
</div>
</div>
@@ -27,6 +27,15 @@
@click="previewImage($event)"
/>
</div>
<!-- 显示上传的附件 -->
<div v-if="hasAttachments" class="user_attachments">
<div v-for="(att, idx) in props.attachments" :key="idx" class="user_attachment_card">
<div class="attachment_card_icon">
<t-icon :name="getAttachmentIcon(att.file_name || att.file_type)" />
</div>
<div class="attachment_card_name">{{ att.file_name }}</div>
</div>
</div>
<div class="user_msg">
{{ content }}
</div>
@@ -56,6 +65,11 @@ const props = defineProps({
required: false,
default: () => []
},
attachments: {
type: Array,
required: false,
default: () => []
},
channel: {
type: String,
required: false,
@@ -79,6 +93,25 @@ const channelClass = computed(() => props.channel ? `channel-${props.channel}` :
const containerRef = ref(null);
const hasImages = computed(() => props.images && props.images.length > 0);
const hasAttachments = computed(() => props.attachments && props.attachments.length > 0);
const getAttachmentIcon = (fileNameOrType) => {
const ext = (fileNameOrType || '').split('.').pop()?.toLowerCase();
if (['pdf'].includes(ext)) return 'file-pdf';
if (['doc', 'docx'].includes(ext)) return 'file-word';
if (['xls', 'xlsx'].includes(ext)) return 'file-excel';
if (['ppt', 'pptx'].includes(ext)) return 'file-powerpoint';
if (['txt', 'md'].includes(ext)) return 'file-text';
if (['mp3', 'wav', 'm4a', 'flac', 'ogg', 'aac'].includes(ext)) return 'sound';
return 'file';
};
const formatFileSize = (bytes) => {
if (!bytes) return '';
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB';
return (bytes / (1024 * 1024)).toFixed(1) + ' MB';
};
const hydrateImages = async () => {
await nextTick();
@@ -198,6 +231,47 @@ const closePreImg = () => {
max-width: 100%;
}
.user_attachments {
display: flex;
flex-wrap: wrap;
gap: 8px;
justify-content: flex-end;
max-width: 100%;
}
.user_attachment_card {
width: 60px;
height: 60px;
border-radius: 8px;
border: 1px solid var(--td-border-level-1-color, #e7e7e7);
background: var(--td-bg-color-secondarycontainer, #f5f5f5);
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 4px;
overflow: hidden;
cursor: default;
.attachment_card_icon {
font-size: 22px;
color: var(--td-brand-color, #07C05F);
line-height: 1;
}
.attachment_card_name {
font-size: 10px;
color: var(--td-text-color-secondary, #666);
text-align: center;
width: 100%;
padding: 0 4px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
box-sizing: border-box;
}
}
.user_image_thumb {
width: 120px;
height: 120px;
+39 -7
View File
@@ -23,7 +23,7 @@
</div>
<div v-for="(session, id) in messagesList" :key='id'>
<div v-if="session.role == 'user'">
<usermsg :content="session.content" :mentioned_items="session.mentioned_items" :images="session.images"></usermsg>
<usermsg :content="session.content" :mentioned_items="session.mentioned_items" :images="session.images" :attachments="session.attachments"></usermsg>
</div>
<div v-if="session.role == 'assistant'">
<botmsg :content="session.content" :session="session" :user-query="getUserQuery(id)" @scroll-bottom="scrollToBottom"
@@ -43,7 +43,7 @@
<div style="min-height: 115px; margin: 16px auto 4px;width: 100%;max-width: 800px;">
<InputField
ref="inputFieldRef"
@send-msg="(query, modelId, mentionedItems, imageFiles) => sendMsg(query, modelId, mentionedItems, imageFiles)"
@send-msg="(query, modelId, mentionedItems, imageFiles, attachmentFiles) => sendMsg(query, modelId, mentionedItems, imageFiles, attachmentFiles)"
@stop-generation="handleStopGeneration"
:isReplying="isReplying"
:sessionId="session_id"
@@ -82,7 +82,7 @@ const useSettingsStoreInstance = useSettingsStore();
const uiStore = useUIStore();
const { navigateToKnowledgeBaseList } = useKnowledgeBaseCreationNavigation();
const { t } = useI18n();
const { menuArr, isFirstSession, firstQuery, firstMentionedItems, firstModelId, firstImageFiles } = storeToRefs(usemenuStore);
const { menuArr, isFirstSession, firstQuery, firstMentionedItems, firstModelId, firstImageFiles, firstAttachmentFiles } = storeToRefs(usemenuStore);
const { output, onChunk, isStreaming, isLoading, error, startStream, stopStream } = useStream();
const route = useRoute();
const router = useRouter();
@@ -418,7 +418,7 @@ const handleStopGeneration = () => {
// API 调用成功后,后端的 stop 事件会清空它
};
const sendMsg = async (value, modelId = '', mentionedItems = [], imageFiles = []) => {
const sendMsg = async (value, modelId = '', mentionedItems = [], imageFiles = [], attachmentFiles = []) => {
userquery.value = value;
isReplying.value = true;
loading.value = true;
@@ -441,8 +441,39 @@ const sendMsg = async (value, modelId = '', mentionedItems = [], imageFiles = []
}
}
// Convert attachment files to base64 for backend processing
let attachmentUploads = [];
if (attachmentFiles && attachmentFiles.length > 0) {
try {
for (const attachment of attachmentFiles) {
const reader = new FileReader();
const base64Promise = new Promise((resolve, reject) => {
reader.onload = () => {
const result = reader.result;
// Extract base64 content (remove data:...;base64, prefix)
const base64 = result.split(',')[1];
resolve(base64);
};
reader.onerror = reject;
reader.readAsDataURL(attachment.file);
});
const base64Data = await base64Promise;
attachmentUploads.push({
data: base64Data,
file_name: attachment.name,
file_size: attachment.size
});
}
} catch (e) {
console.error('[Attachment] Failed to read attachments:', e);
loading.value = false;
isReplying.value = false;
return;
}
}
// 将@提及的知识库和文件信息存入用户消息
messagesList.push({ content: value, role: 'user', mentioned_items: mentionedItems, images: userImages, channel: 'web' });
messagesList.push({ content: value, role: 'user', mentioned_items: mentionedItems, images: userImages, attachments: attachmentFiles.map(a => ({ file_name: a.name, file_size: a.size, file_type: '.' + a.name.split('.').pop()?.toLowerCase() })), channel: 'web' });
scrollToBottom();
// Get agent mode status from settings store
@@ -492,6 +523,7 @@ const sendMsg = async (value, modelId = '', mentionedItems = [], imageFiles = []
mcp_service_ids: mcpServiceIds,
mentioned_items: mentionedItems,
images: imageAttachments.length > 0 ? imageAttachments : undefined,
attachment_uploads: attachmentUploads.length > 0 ? attachmentUploads : undefined,
query: value,
method: 'POST',
url: endpoint
@@ -1078,8 +1110,8 @@ onMounted(async () => {
checkmenuTitle(session_id.value)
if (firstQuery.value) {
scrollLock.value = true;
sendMsg(firstQuery.value, firstModelId.value || '', firstMentionedItems.value || [], firstImageFiles.value || []);
usemenuStore.changeFirstQuery('', [], '', []);
sendMsg(firstQuery.value, firstModelId.value || '', firstMentionedItems.value || [], firstImageFiles.value || [], firstAttachmentFiles.value || []);
usemenuStore.changeFirstQuery('', [], '', [], []);
} else {
scrollLock.value = false;
let data = {
+6 -6
View File
@@ -162,11 +162,11 @@ const handleSuggestedQuestionClick = (question: string) => {
inputFieldRef.value?.triggerSend(question);
};
const sendMsg = (value: string, modelId: string, mentionedItems: any[], imageFiles: any[] = []) => {
createNewSession(value, modelId, mentionedItems, imageFiles);
const sendMsg = (value: string, modelId: string, mentionedItems: any[], imageFiles: any[] = [], attachmentFiles: any[] = []) => {
createNewSession(value, modelId, mentionedItems, imageFiles, attachmentFiles);
}
async function createNewSession(value: string, modelId: string, mentionedItems: any[] = [], imageFiles: any[] = []) {
async function createNewSession(value: string, modelId: string, mentionedItems: any[] = [], imageFiles: any[] = [], attachmentFiles: any[] = []) {
const selectedKbs = settingsStore.settings.selectedKnowledgeBases || [];
const selectedFiles = settingsStore.settings.selectedFiles || [];
@@ -186,7 +186,7 @@ async function createNewSession(value: string, modelId: string, mentionedItems:
try {
const res = await createSessions(sessionData);
if (res.data && res.data.id) {
await navigateToSession(res.data.id, value, modelId, mentionedItems, imageFiles);
await navigateToSession(res.data.id, value, modelId, mentionedItems, imageFiles, attachmentFiles);
} else {
console.error('[createChat] Failed to create session');
MessagePlugin.error(t('createChat.messages.createFailed'));
@@ -197,7 +197,7 @@ async function createNewSession(value: string, modelId: string, mentionedItems:
}
}
const navigateToSession = async (sessionId: string, value: string, modelId: string, mentionedItems: any[], imageFiles: any[] = []) => {
const navigateToSession = async (sessionId: string, value: string, modelId: string, mentionedItems: any[], imageFiles: any[] = [], attachmentFiles: any[] = []) => {
const now = new Date().toISOString();
let obj = {
title: t('createChat.newSessionTitle'),
@@ -210,7 +210,7 @@ const navigateToSession = async (sessionId: string, value: string, modelId: stri
};
usemenuStore.updataMenuChildren(obj);
usemenuStore.changeIsFirstSession(true);
usemenuStore.changeFirstQuery(value, mentionedItems, modelId, imageFiles);
usemenuStore.changeFirstQuery(value, mentionedItems, modelId, imageFiles, attachmentFiles);
router.push(`/platform/chat/${sessionId}`);
}