ai-chatbot/lib/agents/psd-font-config.ts
Your Name 17dcfcd55d
Some checks failed
Lint / build (20) (push) Has been cancelled
Playwright Tests / test (push) Has been cancelled
feat: add PSD template font rendering workflow
2026-03-27 11:01:13 +08:00

91 lines
2.3 KiB
TypeScript

export const AGENT_PSD_FONTS_KEY = "agent_psd_fonts";
export interface AgentPsdFont {
id: string;
name: string;
family: 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;
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,
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)
);
}