openWorkspaceAttachment.ts
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { Router, RouteLocationNormalizedLoaded } from "vue-router";
import { ElLoading, ElMessage } from "element-plus";
import { getFileTypeFromNameForOpen } from "./attachmentFileMeta";
export interface OpenWorkspaceAttachmentInput {
fileName: string;
/** Codex 工作区相对路径,有则与 file-generated-card 相同走 downloadWorkspaceFile */
filePath?: string;
/** 标准类型,缺省从文件名推断 */
fileType?: string;
/** 知识库文件 id(无 filePath 时) */
knowledgeFileId?: number;
}
/**
* 在 Workspace 中打开附件:Codex 路径走 open-codex-file;知识库走路由或自定义事件。
*/
export async function openWorkspaceAttachment(
router: Router,
route: RouteLocationNormalizedLoaded,
input: OpenWorkspaceAttachmentInput,
): Promise<void> {
const fileName = input.fileName;
if (!fileName) {
ElMessage.warning("文件信息不完整,无法打开");
return;
}
const fileType =
input.fileType && input.fileType.length > 0
? input.fileType.toLowerCase()
: getFileTypeFromNameForOpen(fileName);
const loading = ElLoading.service({
lock: true,
text: "正在打开文件...",
background: "rgba(0, 0, 0, 0.7)",
});
let closeLoading = true;
try {
if (input.filePath) {
if (route.name === "Workspace") {
loading.close();
closeLoading = false;
window.dispatchEvent(
new CustomEvent("open-codex-file", {
detail: {
filePath: input.filePath,
fileName,
fileType,
},
}),
);
} else {
await router.push({
name: "Workspace",
query: {
fromCodex: "true",
filePath: input.filePath,
fileName,
fileType,
},
});
}
return;
}
const fid = input.knowledgeFileId;
if (fid != null && !Number.isNaN(Number(fid))) {
if (route.name === "Workspace") {
loading.close();
closeLoading = false;
window.dispatchEvent(
new CustomEvent("open-workspace-knowledge-file", {
detail: {
fileId: fid,
fileName,
fileType,
},
}),
);
} else {
await router.push({
name: "Workspace",
query: {
file: String(fid),
fileName,
fileType,
},
});
}
return;
}
ElMessage.warning("文件信息不完整,无法打开");
} catch (e: any) {
console.error("打开文件失败:", e);
ElMessage.error(e?.message || "打开文件失败,请重试");
} finally {
if (closeLoading) loading.close();
}
}