-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathoperator.const.tsx
More file actions
202 lines (176 loc) · 6.25 KB
/
operator.const.tsx
File metadata and controls
202 lines (176 loc) · 6.25 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React from "react";
import { Atom, Code, FileText, Film, Image, Music } from "lucide-react";
import { OperatorI } from "./operator.model";
import { formatDateTime } from "@/utils/unit.ts";
const getOperatorVisual = (
op: OperatorI,
t: (key: string) => string
): { modal: String; icon: React.ReactNode; iconColor?: string } => {
const type = (op?.type || "").toLowerCase();
const categories = (op?.categories || []).map((c) => (c || "").toLowerCase());
const inputs = (op?.inputs || "").toLowerCase();
const outputs = (op?.outputs || "").toLowerCase();
// 后端固定的分类 ID,兼容 categories 传 UUID 的情况
const CATEGORY_IDS = {
text: "d8a5df7a-52a9-42c2-83c4-01062e60f597",
image: "de36b61c-9e8a-4422-8c31-d30585c7100f",
audio: "42dd9392-73e4-458c-81ff-41751ada47b5",
video: "a233d584-73c8-4188-ad5d-8f7c8dda9c27",
multimodal: "4d7dbd77-0a92-44f3-9056-2cd62d4a71e4",
} as const;
const hasCategoryId = (key: keyof typeof CATEGORY_IDS) =>
(op?.categories || []).some((c) => c === CATEGORY_IDS[key]);
const isMultimodal =
["multimodal", "multi", "多模态"].some((k) =>
type.includes(k)
) ||
categories.some((c) => c.includes("multimodal") || c.includes("多模态")) ||
inputs.includes("multimodal") ||
outputs.includes("multimodal");
const isVideoOp =
["video", "视频"].includes(type) ||
categories.some((c) => c.includes("video") || c.includes("视频")) ||
inputs.includes("video") ||
outputs.includes("video") ||
hasCategoryId("video");
const isAudioOp =
["audio", "音频"].includes(type) ||
categories.some((c) => c.includes("audio") || c.includes("音频")) ||
inputs.includes("audio") ||
outputs.includes("audio") ||
hasCategoryId("audio");
const isImageOp =
["image", "图像", "图像类"].includes(type) ||
categories.some((c) => c.includes("image") || c.includes("图像")) ||
inputs.includes("image") ||
outputs.includes("image") ||
hasCategoryId("image");
const isTextOp =
["text", "文本", "文本类"].includes(type) ||
categories.some((c) => c.includes("text") || c.includes("文本")) ||
inputs.includes("text") ||
outputs.includes("text") ||
hasCategoryId("text");
if (isMultimodal) {
return {
modal: t("operatorMarket.const.modal.multimodal"),
icon: <Atom className="w-full h-full" />,
iconColor: "#F472B6",
};
}
if (isVideoOp) {
return {
modal: t("operatorMarket.const.modal.video"),
icon: <Film className="w-full h-full" />,
iconColor: "#22D3EE",
};
}
if (isAudioOp) {
return {
modal: t("operatorMarket.const.modal.audio"),
icon: <Music className="w-full h-full" />,
iconColor: "#F59E0B",
};
}
if (isImageOp) {
return {
modal: t("operatorMarket.const.modal.image"),
icon: <Image className="w-full h-full" />,
iconColor: "#38BDF8", // 图像算子背景色
};
}
if (isTextOp) {
return {
modal: t("operatorMarket.const.modal.text"),
icon: <FileText className="w-full h-full" />,
iconColor: "#A78BFA", // 文本算子背景色
};
}
return {
modal: t("operatorMarket.const.modal.multimodal"),
icon: <Code className="w-full h-full" />,
iconColor: undefined,
};
};
export const mapOperator = (op: OperatorI, t: (key: string) => string) => {
const visual = getOperatorVisual(op, t);
const FUNCTION_CATEGORY_IDS = {
cleaning: "8c09476a-a922-418f-a908-733f8a0de521",
annotation: "cfa9d8e2-5b5f-4f1e-9f12-1234567890ab",
system: "96a3b07a-3439-4557-a835-525faad60ca3"
} as const;
const categories = op?.categories || [];
const functionLabel = categories.includes(FUNCTION_CATEGORY_IDS.annotation)
? t("dataAnnotation.title")
: categories.includes(FUNCTION_CATEGORY_IDS.cleaning)
? t("dataCleansing.title")
: "-";
return {
...op,
icon: visual.icon,
iconColor: visual.iconColor,
createdAt: formatDateTime(op?.createdAt) || "--",
updatedAt:
formatDateTime(op?.updatedAt) ||
formatDateTime(op?.createdAt) ||
"--",
statistics: [
{
label: t("operatorMarket.const.usageCount"),
value: op?.usageCount || 0
},
{
label: t("operatorMarket.const.type"),
value: visual.modal || "text",
},
{
label: t("operatorMarket.const.size"),
value: categories?.includes(FUNCTION_CATEGORY_IDS.system) ? '-' : formatBytes(op?.fileSize),
},
{
label: t("operatorMarket.const.language"),
value: "Python",
},
// {
// label: t("operatorMarket.const.function"),
// value: functionLabel,
// },
],
};
};
export type MediaType = 'text' | 'image' | 'video' | 'audio' | 'multimodal';
const TEXT_EXTENSIONS = ['.txt', '.md', '.json', '.csv', '.doc', '.docx', '.pdf'];
const IMAGE_EXTENSIONS = ['.jpg', '.jpeg', '.png', '.gif', '.webp', '.svg', '.bmp'];
const VIDEO_EXTENSIONS = ['.mp4', '.mov', '.avi', '.mkv', '.webm'];
const AUDIO_EXTENSIONS = ['.mp3', '.wav', '.ogg', '.aac', '.flac'];
// 3. 定义 Map 对象
export const FileExtensionMap: Record<MediaType, string[]> = {
text: TEXT_EXTENSIONS,
image: IMAGE_EXTENSIONS,
video: VIDEO_EXTENSIONS,
audio: AUDIO_EXTENSIONS,
// 使用扩展运算符合并所有数组,生成全集
multimodal: [
...TEXT_EXTENSIONS,
...IMAGE_EXTENSIONS,
...VIDEO_EXTENSIONS,
...AUDIO_EXTENSIONS,
],
};
export const formatBytes = (bytes: number | null | undefined, decimals: number = 2): string => {
// 1. 处理特殊情况:0、null 或 undefined
if (bytes === null || bytes === undefined || bytes === 0) {
return '0 B';
}
// 2. 定义单位阶梯
const k = 1024;
const dm = decimals < 0 ? 0 : decimals; // 确保小数位数非负
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
// 3. 计算指数 (i)
// Math.log(bytes) / Math.log(k) 等同于以 1024 为底求 bytes 的对数
// floor 向下取整,得出它属于哪个单位级别 (0是B, 1是KB, 2是MB...)
const i = Math.floor(Math.log(bytes) / Math.log(k));
// 4. 格式化数值并拼接单位
// parseFloat 用于去掉末尾多余的 0 (例如 "1.20 MB" -> "1.2 MB")
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
};