UNPKG

morphbox

Version:

Docker-based AI sandbox for development with Claude integration

81 lines (78 loc) 2.67 kB
import { j as json } from './index-3BbzJtgI.js'; import { mkdir, access, writeFile } from 'fs/promises'; import { join } from 'path'; import { homedir } from 'os'; import { v as validateMorphFile } from './morph-f-EPjDPR.js'; const PANELS_DIR = join(homedir(), "morphbox", "panels"); function sanitizePanelId(id) { return id.toLowerCase().replace(/[^a-z0-9\-_]/g, "-").replace(/--+/g, "-").replace(/^-|-$/g, ""); } const POST = async ({ request }) => { try { const contentType = request.headers.get("content-type"); let morphData; if (contentType?.includes("application/json")) { morphData = await request.json(); } else if (contentType?.includes("multipart/form-data")) { const formData = await request.formData(); const file = formData.get("file"); if (!file) { return json({ error: "No file provided" }, { status: 400 }); } if (!file.name.endsWith(".morph")) { return json({ error: "File must have .morph extension" }, { status: 400 }); } const text = await file.text(); try { morphData = JSON.parse(text); } catch (e) { return json({ error: "Invalid JSON in .morph file" }, { status: 400 }); } } else { return json({ error: "Unsupported content type" }, { status: 400 }); } if (!validateMorphFile(morphData)) { return json({ error: "Invalid .morph file format" }, { status: 400 }); } await mkdir(PANELS_DIR, { recursive: true }); const originalId = morphData.metadata.id; let panelId = sanitizePanelId(originalId); let counter = 1; let finalId = panelId; while (true) { try { await access(join(PANELS_DIR, `${finalId}.morph`)); finalId = `${panelId}-${counter}`; counter++; } catch { break; } } if (finalId !== originalId) { morphData.metadata.id = finalId; } const now = (/* @__PURE__ */ new Date()).toISOString(); morphData.updatedAt = now; if (!morphData.createdAt) { morphData.createdAt = now; } const filepath = join(PANELS_DIR, `${finalId}.morph`); await writeFile(filepath, JSON.stringify(morphData, null, 2), "utf-8"); return json({ success: true, id: finalId, originalId, name: morphData.metadata.name, path: filepath, imported: true, idChanged: finalId !== originalId }); } catch (error) { console.error("Failed to import panel:", error); return json({ error: error instanceof Error ? error.message : "Failed to import panel" }, { status: 500 }); } }; export { POST }; //# sourceMappingURL=_server.ts-Bc0swg6W.js.map