ai-chatbot/lib/agents/psd-font-config.ts
Your Name 3ee9701a50
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: 联动PSD品类标签并支持批量上传字体
2026-04-03 19:41:59 +08:00

94 lines
2.4 KiB
TypeScript

export const AGENT_PSD_FONTS_KEY = "agent_psd_fonts";
export interface AgentPsdFont {
id: string;
name: string;
family: string;
brandName?: string;
fileName: string;
fileUrl: string;
filePath?: string;
contentType?: string;
createdAt: string;
updatedAt: string;
uploadedById?: string;
}
export interface AgentPsdFontListResponse {
success: boolean;
fonts: AgentPsdFont[];
error?: string;
}
export interface AgentPsdFontMutationResponse {
success: boolean;
font?: AgentPsdFont;
fonts?: AgentPsdFont[];
error?: string;
}
function normalizeText(value: unknown) {
return String(value || "").trim();
}
function normalizeUrl(value: unknown) {
const text = normalizeText(value);
return /^https?:\/\//i.test(text) ? text : "";
}
function toIsoDateString(value: unknown) {
const text = normalizeText(value);
if (!text) return null;
const date = new Date(text);
if (Number.isNaN(date.getTime())) return null;
return date.toISOString();
}
export function parseAgentPsdFonts(value: unknown) {
if (!Array.isArray(value)) return [] as AgentPsdFont[];
const fonts: AgentPsdFont[] = [];
for (const entry of value) {
if (!entry || typeof entry !== "object" || Array.isArray(entry)) {
continue;
}
const record = entry as Record<string, unknown>;
const id = normalizeText(record.id);
const name = normalizeText(record.name);
const family = normalizeText(record.family);
const fileName = normalizeText(record.fileName);
const fileUrl = normalizeUrl(record.fileUrl);
const createdAt = toIsoDateString(record.createdAt);
const updatedAt = toIsoDateString(record.updatedAt) || createdAt;
if (!id || !name || !family || !fileName || !fileUrl || !createdAt || !updatedAt) {
continue;
}
fonts.push({
id,
name,
family,
brandName: normalizeText(record.brandName) || undefined,
fileName,
fileUrl,
filePath: normalizeText(record.filePath) || undefined,
contentType: normalizeText(record.contentType) || undefined,
createdAt,
updatedAt,
uploadedById: normalizeText(record.uploadedById) || undefined,
});
}
return fonts.sort((left, right) => right.updatedAt.localeCompare(left.updatedAt));
}
export function upsertAgentPsdFont(fonts: AgentPsdFont[], font: AgentPsdFont) {
return [font, ...fonts.filter((entry) => entry.id !== font.id)].sort((left, right) =>
right.updatedAt.localeCompare(left.updatedAt)
);
}