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:
hugo committed 2026-07-07 13:25:16 +08:00
commit fc694a8f43
55 files changed
+2107 -192

No files matched your search

@@ -16,6 +16,21 @@ interface UseChatAssetHistoryOptions {
warn: (message: string) => void;
}
/**
* 把后端返回的技术性错误翻译成用户可理解的提示。
* AI 触发时机:当资产履历查询失败,但原始错误包含英文配置名、共享盘术语等技术细节时调用。
* 核心逻辑:优先识别已知的履历共享盘配置异常,返回面向业务用户的中文说明;其他错误保持原文,避免吞掉真实上下文。
* 参数约束:仅处理展示文案,不改变错误对象本身;返回值必须适合作为 toast/警告弹窗直接展示。
*/
function normalizeAssetHistoryErrorMessage(message: string): string {
const normalizedMessage = String(message || '').trim();
const lowerCaseMessage = normalizedMessage.toLowerCase();
if (lowerCaseMessage.includes('asset history share is not configured')) {
return '当前系统暂未配置资产履历文件目录,暂时无法查看履历,请联系管理员处理。';
}
return normalizedMessage || '检索资产履历失败';
}
export function useChatAssetHistory(options: UseChatAssetHistoryOptions) {
const open = ref(false);
const loading = ref(false);
@@ -35,7 +50,7 @@ export function useChatAssetHistory(options: UseChatAssetHistoryOptions) {
* 检索资产履历并直接打开最佳预览结果。
* AI 触发时机:当用户点击聊天表格中的“资产编号”,希望直接查看该资产履历时调用。
* 核心逻辑:点击后先切入“检索中”状态,再按“资产编号 + 资产名称”查询共享盘候选文件,命中后默认取排序最优的第一条直接预览;
* 未命中或查询失败时,仅给用户展示友好提示,不再弹出中间候选列表。
* 未命中或查询失败时,仅给用户展示友好提示,不再弹出中间候选列表;若后端返回技术性英文报错,会先转换成业务可读提示。
* 参数约束:至少要提供资产编号或资产名称之一;候选文件排序由后端负责,本方法默认信任第一条结果。
*/
const openHistory = async (nextAssetId: string, nextAssetName = '') => {
@@ -69,7 +84,8 @@ export function useChatAssetHistory(options: UseChatAssetHistoryOptions) {
}
await openHistoryFilePreview(bestMatch);
} catch (err: unknown) {
const message = err instanceof Error ? err.message : '检索资产履历失败';
const rawMessage = err instanceof Error ? err.message : '检索资产履历失败';
const message = normalizeAssetHistoryErrorMessage(rawMessage);
error.value = message;
options.warn(`履历查询失败:${message}`);
} finally {
@@ -0,0 +1,89 @@
/**
* features/chat/composables/useChatAssetImage.ts
* 用途:管理固定资产图片检索、友好提示与预览跳转。
* 依赖:chat.api、useChatSourceFiles 暴露的 openPreview、聊天提示消息。
* 逻辑:用户点击“图片 -> 查看”后,本 composable 统一向后端索要最佳图片,再复用现有文件预览弹窗在站内展示。
*/
import { ref } from 'vue';
import type { ComputedRef } from 'vue';
import { resolveAssetImagePreview } from '../api/chat.api';
import type { SourceFileLink } from '../types/chat.types';
interface UseChatAssetImageOptions {
agentId: ComputedRef<number>;
openPreviewGallery: (files: SourceFileLink[], initialIndex?: number) => Promise<void>;
warn: (message: string) => void;
}
/**
* 把后端返回的图片检索错误翻译成业务用户能看懂的提示。
* AI 触发时机:当用户点击“图片 -> 查看”失败,但原始报错包含接口路径、外部系统术语等技术细节时调用。
* 核心逻辑:优先识别已知的配置/检索异常并转成中文说明;未知错误保留原始文本,方便排查。
* 参数与输出约束:只负责展示文案,不吞掉真实错误对象;返回值必须适合作为 toast 直接展示。
*/
function normalizeAssetImageErrorMessage(message: string): string {
const normalizedMessage = String(message || '').trim();
const lowerCaseMessage = normalizedMessage.toLowerCase();
if (lowerCaseMessage.includes('固定资产图片文档库未配置')) {
return '当前系统暂未配置固定资产图片文档库,暂时无法查看图片,请联系管理员处理。';
}
if (lowerCaseMessage.includes('未找到资产')) {
return normalizedMessage;
}
return normalizedMessage || '检索固定资产图片失败';
}
export function useChatAssetImage(options: UseChatAssetImageOptions) {
const loading = ref(false);
/**
* 检索固定资产图片并直接打开预览。
* AI 触发时机:当用户点击聊天表格或 CSV 预览里的“图片 -> 查看”时调用。
* 核心逻辑:先向后端请求全部命中的图片文件,再把图片组交给通用 FilePreviewModal 预览。
* 参数约束:至少要提供资产编号或资产名称之一;后端负责登录外部文档库和挑选最佳图片。
*/
const openImage = async (nextAssetId: string, nextAssetName = '') => {
const normalizedAssetId = String(nextAssetId || '').trim();
const normalizedAssetName = String(nextAssetName || '').trim();
if (loading.value) return;
if (!normalizedAssetId && !normalizedAssetName) {
options.warn('当前资产缺少可用于检索图片的编号或名称');
return;
}
loading.value = true;
try {
const result = await resolveAssetImagePreview(options.agentId.value, {
asset_id: normalizedAssetId,
asset_name: normalizedAssetName,
});
const items = Array.isArray(result?.items) && result.items.length
? result.items
: (result?.item ? [result.item] : []);
if (!items.length) {
const displayName = normalizedAssetId || normalizedAssetName;
options.warn(`未找到资产 ${displayName} 的图片文件,请联系管理员补充。`);
return;
}
await options.openPreviewGallery(
items.map((item) => ({
name: item.name,
url: item.url,
actualExt: item.actual_ext,
})),
0,
);
} catch (err: unknown) {
const rawMessage = err instanceof Error ? err.message : '检索固定资产图片失败';
const message = normalizeAssetImageErrorMessage(rawMessage);
options.warn(`图片查询失败:${message}`);
} finally {
loading.value = false;
}
};
return {
loading,
openImage,
};
}
@@ -26,6 +26,7 @@ import { useChatSessions } from './useChatSessions';
import { useChatShipping } from './useChatShipping';
import { useChatSourceFiles } from './useChatSourceFiles';
import { useChatAssetHistory } from './useChatAssetHistory';
import { useChatAssetImage } from './useChatAssetImage';
import { useChatStore } from '../stores/chat.store';
import { useTrainingWorkspace } from './useTrainingWorkspace';
import { matchesTrainingAgent } from '../training/trainingWorkspace.utils';
@@ -159,6 +160,8 @@ export function useChatPage() {
const {
filePreviewVisible,
previewFile,
previewFiles,
currentPreviewIndex,
getSourceTitle,
getSourceMeta,
getSourceBrief,
@@ -167,6 +170,8 @@ export function useChatPage() {
getDisplayRetrieverResources,
shouldExpandSourceDetails,
openPreview,
openPreviewGallery,
setPreviewFileIndex,
parsePlmPreviewLink,
} = sourceFiles;
const assetHistory = useChatAssetHistory({
@@ -174,6 +179,11 @@ export function useChatPage() {
openPreview,
warn: (text) => msg.warning(text),
});
const assetImage = useChatAssetImage({
agentId: currentAgentId,
openPreviewGallery,
warn: (text) => msg.warning(text),
});
watch(currentAgentId, (id) => {
chatStore.setCurrentAgentId(id > 0 ? id : null);
@@ -318,6 +328,8 @@ export function useChatPage() {
resetPeripheralState,
filePreviewVisible,
previewFile,
previewFiles,
currentPreviewIndex,
});
({ loadConversation, startNewChat } = conversationApi);
@@ -366,6 +378,7 @@ export function useChatPage() {
parsePlmPreviewLink,
openPreview,
openAssetHistory: assetHistory.openHistory,
openAssetImage: assetImage.openImage,
getMessageLinks,
getDisplayRetrieverResources,
getSourceLinks,
@@ -507,6 +520,7 @@ export function useChatPage() {
queryPreviewError: queryPreview.error,
queryPreviewState: queryPreview.state,
assetHistory,
assetImage,
...agent,
agentGuides: chatGuides.guides,
showGuideModal: chatGuides.showModal,
@@ -517,6 +531,9 @@ export function useChatPage() {
...attachmentApi,
filePreviewVisible,
previewFile,
previewFiles,
currentPreviewIndex,
setPreviewFileIndex,
isSidebarOpen,
sidebarCollapsed,
toggleBrowserPanel: browser.toggle,
@@ -1,4 +1,4 @@
/**
/**
* features/chat/composables/useChatSourceFiles.ts
* 检索来源 / 源文件链接解析与预览。
*/
@@ -204,6 +204,8 @@ export interface UseChatSourceFilesOptions {
export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceFilesOptions) {
const filePreviewVisible = ref(false);
const previewFile = ref<{ name: string; url: string; actualExt?: string } | null>(null);
const previewFiles = ref<Array<{ name: string; url: string; actualExt?: string }>>([]);
const currentPreviewIndex = ref(0);
const getSourceTitle = (item: Record<string, unknown>) =>
String(item?.document_name || item?.title || item?.source || 'Untitled Source');
@@ -398,6 +400,8 @@ export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceF
window.location.href = resolvedFile.url;
return;
}
previewFiles.value = [resolvedFile];
currentPreviewIndex.value = 0;
previewFile.value = resolvedFile;
filePreviewVisible.value = true;
} catch (error: unknown) {
@@ -406,6 +410,55 @@ export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceF
}
};
/**
* 打开多文件预览组,并支持在同一弹窗内切换。
* AI 触发时机:当后端一次返回同资产的多张图片,需要在当前系统内左右切换查看时调用。
* 核心逻辑:先解析每个文件的可预览 URL,再把文件列表和初始索引写入共享弹窗状态。
* 参数与输出约束:files 必须至少有 1 个可预览项;initialIndex 会被钳制到合法范围。
*/
const openPreviewGallery = async (files: SourceFileLink[], initialIndex = 0) => {
const normalizedFiles = Array.isArray(files) ? files : [];
if (!normalizedFiles.length) {
msg.warning(t('chat.previewUnavailable'));
return;
}
try {
const resolvedFiles = await Promise.all(normalizedFiles.map((file) => resolveSourceLink(file)));
if (!resolvedFiles.length) {
msg.warning(t('chat.previewUnavailable'));
return;
}
const safeIndex = Math.min(Math.max(initialIndex, 0), resolvedFiles.length - 1);
const initialFile = resolvedFiles[safeIndex] || resolvedFiles[0];
if (!initialFile) {
msg.warning(t('chat.previewUnavailable'));
return;
}
if (shouldBypassEmbeddedPreview(initialFile)) {
window.location.href = initialFile.url;
return;
}
previewFiles.value = resolvedFiles;
currentPreviewIndex.value = safeIndex;
previewFile.value = initialFile;
filePreviewVisible.value = true;
} catch (error: unknown) {
const message = error instanceof Error ? error.message : t('chat.previewUnavailable');
msg.warning(message);
}
};
/**
* 切换当前预览中的文件索引。
* 用途:供 FilePreviewModal 在多图模式下切换上一张、下一张或缩略图点击时回写当前文件。
*/
const setPreviewFileIndex = (nextIndex: number) => {
if (!previewFiles.value.length) return;
const safeIndex = Math.min(Math.max(nextIndex, 0), previewFiles.value.length - 1);
currentPreviewIndex.value = safeIndex;
previewFile.value = previewFiles.value[safeIndex] || null;
};
const parsePlmPreviewLink = (href: string): SourceFileLink | null => {
const rawHref = (href || '').trim();
if (!rawHref.startsWith('#plm-preview?')) return null;
@@ -419,6 +472,8 @@ export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceF
return {
filePreviewVisible,
previewFile,
previewFiles,
currentPreviewIndex,
getSourceTitle,
getSourceMeta,
getSourceBrief,
@@ -427,6 +482,8 @@ export function useChatSourceFiles({ agentId, t, msg, isMobile }: UseChatSourceF
getDisplayRetrieverResources,
shouldExpandSourceDetails,
openPreview,
openPreviewGallery,
setPreviewFileIndex,
parsePlmPreviewLink,
};
}