Merge branch 'dev' into hugo-shipping-notice
# Conflicts: # backend/app/api/chat.py # backend/app/services/agent_runtime_context/dify_inputs.py # backend/app/services/maintenance_chat_service.py
This commit is contained in:
commit
fc694a8f43
55 files changed
+2107
-192
No files matched your search
@@ -18,7 +18,7 @@ import { computed, ref, watch } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { marked } from 'marked';
|
||||
import * as XLSX from 'xlsx';
|
||||
import { Minus, Plus, RotateCcw } from 'lucide-vue-next';
|
||||
import { ChevronLeft, ChevronRight, Minus, Plus, RotateCcw } from 'lucide-vue-next';
|
||||
import VueOfficeDocx from '@vue-office/docx';
|
||||
import VueOfficeExcel from '@vue-office/excel';
|
||||
import VueOfficePdf from '@vue-office/pdf';
|
||||
@@ -46,12 +46,15 @@ type PreviewMode = 'markdown' | 'image' | 'iframe' | 'sheet' | 'sheetHtml' | 'of
|
||||
const props = defineProps<{
|
||||
modelValue: boolean;
|
||||
file: PreviewFile | null;
|
||||
files?: PreviewFile[] | null;
|
||||
currentIndex?: number;
|
||||
agentId?: number | null;
|
||||
authHeaders?: Record<string, string>;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'update:modelValue', value: boolean): void;
|
||||
(e: 'update:currentIndex', value: number): void;
|
||||
}>();
|
||||
const { t } = useI18n();
|
||||
|
||||
@@ -70,13 +73,34 @@ const EXCEL_ZOOM_STEP = 0.1;
|
||||
const EXCEL_ZOOM_MIN = 0.6;
|
||||
const EXCEL_ZOOM_MAX = 1.8;
|
||||
|
||||
const imagePreviewExts = new Set(['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp']);
|
||||
|
||||
const activeFiles = computed<PreviewFile[]>(() => {
|
||||
const files = Array.isArray(props.files) ? props.files.filter(Boolean) : [];
|
||||
if (files.length) return files;
|
||||
return props.file ? [props.file] : [];
|
||||
});
|
||||
|
||||
const activeIndex = computed(() => {
|
||||
if (!activeFiles.value.length) return 0;
|
||||
const rawIndex = Number(props.currentIndex ?? 0);
|
||||
if (!Number.isFinite(rawIndex)) return 0;
|
||||
return Math.min(Math.max(Math.trunc(rawIndex), 0), activeFiles.value.length - 1);
|
||||
});
|
||||
|
||||
const activeFile = computed<PreviewFile | null>(() => activeFiles.value[activeIndex.value] || props.file || null);
|
||||
|
||||
/**
|
||||
* 从 URL 或文件名中提取扩展名。
|
||||
* - 会先去掉 query 参数(兼容签名链接)。
|
||||
* - 兼容直接传入 `jpg` 这类原始扩展名。
|
||||
* - 返回小写扩展名(不含点号)。
|
||||
*/
|
||||
const getExt = (raw: string) => {
|
||||
const clean = (raw.split('?')[0] ?? '').toLowerCase();
|
||||
if (clean && !clean.includes('/') && !clean.includes('.')) {
|
||||
return clean;
|
||||
}
|
||||
const parts = clean.split('/');
|
||||
const lastPart = parts[parts.length - 1] ?? '';
|
||||
const i = lastPart.lastIndexOf('.');
|
||||
@@ -84,16 +108,16 @@ const getExt = (raw: string) => {
|
||||
};
|
||||
|
||||
const ext = computed(() => {
|
||||
const actual = getExt(props.file?.actualExt || '');
|
||||
const actual = getExt(activeFile.value?.actualExt || '');
|
||||
if (actual) return actual;
|
||||
// Display name extension has higher priority than URL.
|
||||
const byName = getExt(props.file?.name || '');
|
||||
const byName = getExt(activeFile.value?.name || '');
|
||||
if (byName) return byName;
|
||||
return getExt(props.file?.url || '');
|
||||
return getExt(activeFile.value?.url || '');
|
||||
});
|
||||
|
||||
const preferredMode = computed<PreviewMode>(() => {
|
||||
if (['jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp'].includes(ext.value)) return 'image';
|
||||
if (imagePreviewExts.has(ext.value)) return 'image';
|
||||
if (['md', 'markdown', 'txt', 'csv', 'vtt', 'properties', 'html', 'htm'].includes(ext.value)) return 'markdown';
|
||||
if (['xlsx', 'xls', 'docx'].includes(ext.value)) return 'office';
|
||||
if (['doc', 'ppt', 'pptx', 'pdf'].includes(ext.value)) return 'iframe';
|
||||
@@ -114,7 +138,7 @@ const needsOfficeFallback = computed(() => {
|
||||
* - 其余返回原始 URL,由浏览器自行处理。
|
||||
*/
|
||||
const iframeSrc = computed(() => {
|
||||
if (!props.file?.url) return '';
|
||||
if (!activeFile.value?.url) return '';
|
||||
if (officeObjectUrl.value) return officeObjectUrl.value;
|
||||
if (!props.agentId) return '';
|
||||
if (['pdf', 'doc', 'ppt', 'pptx'].includes(ext.value)) {
|
||||
@@ -122,12 +146,16 @@ const iframeSrc = computed(() => {
|
||||
// Never fallback to a protected iframe URL, otherwise it returns Unauthorized.
|
||||
return officeObjectUrl.value;
|
||||
}
|
||||
return props.file.url;
|
||||
return activeFile.value.url;
|
||||
});
|
||||
|
||||
const imageSrc = computed(() => officeObjectUrl.value || props.file?.url || '');
|
||||
const imageSrc = computed(() => officeObjectUrl.value || activeFile.value?.url || '');
|
||||
const officePreviewExts = new Set(['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx']);
|
||||
const isExcelPreviewFile = computed(() => ['xls', 'xlsx'].includes(ext.value));
|
||||
const isImageGallery = computed(() => (
|
||||
activeFiles.value.length > 1
|
||||
&& activeFiles.value.every((file) => imagePreviewExts.has(getExt(file.actualExt || file.name || file.url || '')))
|
||||
));
|
||||
const excelZoomPercent = computed(() => `${Math.round(excelZoom.value * 100)}%`);
|
||||
const excelPreviewScaleStyle = computed(() => ({
|
||||
transform: `scale(${excelZoom.value})`,
|
||||
@@ -137,6 +165,15 @@ const excelPreviewScaleStyle = computed(() => ({
|
||||
|
||||
const close = () => emit('update:modelValue', false);
|
||||
|
||||
const setActiveIndex = (nextIndex: number) => {
|
||||
if (!activeFiles.value.length) return;
|
||||
const safeIndex = Math.min(Math.max(nextIndex, 0), activeFiles.value.length - 1);
|
||||
emit('update:currentIndex', safeIndex);
|
||||
};
|
||||
|
||||
const showPrevImage = () => setActiveIndex(activeIndex.value - 1);
|
||||
const showNextImage = () => setActiveIndex(activeIndex.value + 1);
|
||||
|
||||
const previewTooLarge = computed(() => /file too large for online preview/i.test(error.value));
|
||||
|
||||
const clearOfficeObjectUrl = () => {
|
||||
@@ -250,13 +287,13 @@ const tryApplyExcelPreview = (buffer: ArrayBuffer) => {
|
||||
};
|
||||
|
||||
const loadSheetPreview = async () => {
|
||||
if (!props.file?.url) return;
|
||||
if (!activeFile.value?.url) return;
|
||||
if (!props.agentId) throw new Error('agent_id is required for excel preview');
|
||||
|
||||
const params = new URLSearchParams({
|
||||
agent_id: String(props.agentId),
|
||||
url: props.file.url,
|
||||
name: props.file.name || '',
|
||||
url: activeFile.value.url,
|
||||
name: activeFile.value.name || '',
|
||||
});
|
||||
const res = await fetch(`/api/chat/preview-excel?${params.toString()}`, {
|
||||
headers: props.authHeaders || {},
|
||||
@@ -274,13 +311,13 @@ const loadSheetPreview = async () => {
|
||||
* 约束:仅作为 Excel 预览兜底使用;解析失败时继续向上抛错,由统一错误 UI 呈现。
|
||||
*/
|
||||
const loadSheetPreviewFromRaw = async () => {
|
||||
if (!props.file?.url) return;
|
||||
if (!activeFile.value?.url) return;
|
||||
if (!props.agentId) throw new Error('agent_id is required for excel preview');
|
||||
|
||||
const params = new URLSearchParams({
|
||||
agent_id: String(props.agentId),
|
||||
url: props.file.url,
|
||||
name: props.file.name || '',
|
||||
url: activeFile.value.url,
|
||||
name: activeFile.value.name || '',
|
||||
});
|
||||
const res = await fetch(`/api/chat/preview-file-raw?${params.toString()}`, {
|
||||
headers: props.authHeaders || {},
|
||||
@@ -303,13 +340,13 @@ const loadSheetPreviewFromRaw = async () => {
|
||||
};
|
||||
|
||||
const loadOfficeContent = async () => {
|
||||
if (!props.file?.url) return;
|
||||
if (!activeFile.value?.url) return;
|
||||
if (!props.agentId) throw new Error('agent_id is required for office preview');
|
||||
|
||||
const params = new URLSearchParams({
|
||||
agent_id: String(props.agentId),
|
||||
url: props.file.url,
|
||||
name: props.file.name || '',
|
||||
url: activeFile.value.url,
|
||||
name: activeFile.value.name || '',
|
||||
});
|
||||
const res = await fetch(`/api/chat/preview-file-raw?${params.toString()}`, {
|
||||
headers: props.authHeaders || {},
|
||||
@@ -415,13 +452,13 @@ const loadPdfContent = async (): Promise<'iframe' | 'office' | 'sheet'> => {
|
||||
};
|
||||
|
||||
const loadBinaryContent = async () => {
|
||||
if (!props.file?.url) return;
|
||||
if (!activeFile.value?.url) return;
|
||||
if (!props.agentId) throw new Error('agent_id is required for binary preview');
|
||||
|
||||
const params = new URLSearchParams({
|
||||
agent_id: String(props.agentId),
|
||||
url: props.file.url,
|
||||
name: props.file.name || '',
|
||||
url: activeFile.value.url,
|
||||
name: activeFile.value.name || '',
|
||||
});
|
||||
const res = await fetch(`/api/chat/preview-file-raw?${params.toString()}`, {
|
||||
headers: props.authHeaders || {},
|
||||
@@ -451,8 +488,8 @@ const loadContent = async () => {
|
||||
excelPreview.value = null;
|
||||
resetExcelZoom();
|
||||
clearOfficeObjectUrl();
|
||||
if (!props.modelValue || !props.file) return;
|
||||
if (!props.file?.url) return;
|
||||
if (!props.modelValue || !activeFile.value) return;
|
||||
if (!activeFile.value?.url) return;
|
||||
resolvedMode.value = preferredMode.value;
|
||||
|
||||
if (['pdf', 'doc', 'docx', 'ppt', 'pptx'].includes(ext.value) && preferredMode.value === 'iframe') {
|
||||
@@ -516,7 +553,7 @@ const loadContent = async () => {
|
||||
return;
|
||||
}
|
||||
|
||||
if (preferredMode.value === 'image' || (preferredMode.value === 'iframe' && !/^https?:\/\//i.test(props.file.url || ''))) {
|
||||
if (preferredMode.value === 'image' || (preferredMode.value === 'iframe' && !/^https?:\/\//i.test(activeFile.value.url || ''))) {
|
||||
isLoading.value = true;
|
||||
try {
|
||||
await loadBinaryContent();
|
||||
@@ -539,8 +576,8 @@ const loadContent = async () => {
|
||||
}
|
||||
const params = new URLSearchParams({
|
||||
agent_id: String(props.agentId),
|
||||
url: props.file.url,
|
||||
name: props.file.name || '',
|
||||
url: activeFile.value.url,
|
||||
name: activeFile.value.name || '',
|
||||
ext: ext.value || '',
|
||||
});
|
||||
const res = await fetch(`/api/chat/preview-text?${params.toString()}`, {
|
||||
@@ -567,7 +604,7 @@ const loadContent = async () => {
|
||||
* - 文件名变化。
|
||||
*/
|
||||
watch(
|
||||
() => [props.modelValue, props.file?.url, props.file?.name],
|
||||
() => [props.modelValue, activeFile.value?.url, activeFile.value?.name, activeIndex.value, activeFiles.value.length],
|
||||
() => {
|
||||
loadContent();
|
||||
},
|
||||
@@ -585,12 +622,15 @@ watch(
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="modelValue && file" class="dialog-overlay z-[140] items-center">
|
||||
<div v-if="modelValue && activeFile" class="dialog-overlay z-[160] items-center">
|
||||
<div class="dialog-panel max-w-5xl h-[85vh] overflow-hidden">
|
||||
<div class="dialog-header dialog-header--compact items-start">
|
||||
<div class="min-w-0">
|
||||
<div class="text-sm font-medium text-slate-500 dark:text-slate-400 tracking-wide">{{ t('filePreview.title') }}</div>
|
||||
<div class="mt-1 font-semibold text-slate-900 dark:text-slate-100 break-words">{{ file.name }}</div>
|
||||
<div class="mt-1 font-semibold text-slate-900 dark:text-slate-100 break-words">{{ activeFile.name }}</div>
|
||||
<div v-if="isImageGallery" class="mt-1 text-xs text-slate-500 dark:text-slate-400">
|
||||
第 {{ activeIndex + 1 }} / {{ activeFiles.length }} 张
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex shrink-0 items-center gap-2">
|
||||
<div v-if="isExcelPreviewFile" class="flex items-center gap-2 rounded-full border border-default bg-surface px-2 py-1">
|
||||
@@ -713,8 +753,49 @@ watch(
|
||||
style="height: 100%; width: 100%;"
|
||||
/>
|
||||
</div>
|
||||
<div v-else-if="resolvedMode === 'image'" class="dialog-preview-frame h-full flex items-center justify-center p-2">
|
||||
<img :src="imageSrc" :alt="file.name" class="max-w-full max-h-full object-contain" />
|
||||
<div v-else-if="resolvedMode === 'image'" class="dialog-preview-frame h-full flex flex-col p-2">
|
||||
<div class="relative flex min-h-0 flex-1 items-center justify-center rounded-xl bg-slate-50 dark:bg-slate-900/40">
|
||||
<button
|
||||
v-if="isImageGallery"
|
||||
type="button"
|
||||
class="absolute left-3 top-1/2 z-10 inline-flex h-10 w-10 -translate-y-1/2 items-center justify-center rounded-full border border-slate-200 bg-white/90 text-slate-700 shadow transition hover:bg-white disabled:cursor-not-allowed disabled:opacity-40 dark:border-slate-700 dark:bg-slate-900/90 dark:text-slate-100"
|
||||
:disabled="activeIndex <= 0"
|
||||
aria-label="上一张"
|
||||
@click="showPrevImage"
|
||||
>
|
||||
<ChevronLeft class="h-5 w-5" />
|
||||
</button>
|
||||
<img :src="imageSrc" :alt="activeFile.name" class="max-w-full max-h-full object-contain" />
|
||||
<button
|
||||
v-if="isImageGallery"
|
||||
type="button"
|
||||
class="absolute right-3 top-1/2 z-10 inline-flex h-10 w-10 -translate-y-1/2 items-center justify-center rounded-full border border-slate-200 bg-white/90 text-slate-700 shadow transition hover:bg-white disabled:cursor-not-allowed disabled:opacity-40 dark:border-slate-700 dark:bg-slate-900/90 dark:text-slate-100"
|
||||
:disabled="activeIndex >= activeFiles.length - 1"
|
||||
aria-label="下一张"
|
||||
@click="showNextImage"
|
||||
>
|
||||
<ChevronRight class="h-5 w-5" />
|
||||
</button>
|
||||
</div>
|
||||
<div v-if="isImageGallery" class="mt-3 flex gap-3 overflow-x-auto pb-1">
|
||||
<button
|
||||
v-for="(preview, index) in activeFiles"
|
||||
:key="`${preview.name}-${index}`"
|
||||
type="button"
|
||||
:class="[
|
||||
'group shrink-0 overflow-hidden rounded-xl border bg-white text-left shadow-sm transition dark:bg-slate-900',
|
||||
index === activeIndex ? 'border-blue-500 ring-2 ring-blue-200 dark:ring-blue-900/60' : 'border-slate-200 hover:border-blue-300 dark:border-slate-700',
|
||||
]"
|
||||
@click="setActiveIndex(index)"
|
||||
>
|
||||
<div class="flex h-20 w-20 items-center justify-center bg-slate-100 text-sm font-semibold text-slate-500 dark:bg-slate-800 dark:text-slate-300">
|
||||
{{ index + 1 }}
|
||||
</div>
|
||||
<div class="w-20 truncate px-2 py-1 text-xs text-slate-600 dark:text-slate-300">
|
||||
{{ preview.name }}
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
v-else-if="['pdf', 'doc', 'ppt', 'pptx'].includes(ext) && officeObjectUrl"
|
||||
|
||||
Reference in new issue
Block a user