From 82809b17b4e2af2abe55000d96bc315be1c1cd24 Mon Sep 17 00:00:00 2001 From: Rami Bitar Date: Sat, 2 May 2026 09:53:48 -0400 Subject: [PATCH] use postMessage --- app/api/chat/route.ts | 17 +---------------- app/api/save-schema/route.ts | 21 --------------------- components/EditorClient.tsx | 11 ++++++----- lib/schema.server.ts | 36 ++---------------------------------- 4 files changed, 9 insertions(+), 76 deletions(-) delete mode 100644 app/api/save-schema/route.ts diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 3a05b9f..0496a59 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -10,7 +10,6 @@ import { reactEditorTools, getEditorContext, } from "@reacteditor/plugin-ai/server"; -import { patchRoute, readSchema } from "@/lib/schema.server"; import { shopifyFetch } from "@/editor/services/shopify/client"; import { GET_PRODUCTS_QUERY, @@ -31,20 +30,7 @@ type Body = { }; export async function POST(req: Request) { - const { messages, editorContext, route } = (await req.json()) as Body; - - const updatePage = tool({ - description: - "Persist the page schema (root + content) for a given route to app.schema.json.", - inputSchema: z.object({ - data: z.object({ root: z.any(), content: z.any() }), - }), - execute: async ({ data }) => { - const target = route || "/"; - await patchRoute(target, data); - return { ok: true, route: target }; - }, - }); + const { messages, editorContext } = (await req.json()) as Body; const generateImage = tool({ description: "Generate an image from a prompt and return its URL.", @@ -230,7 +216,6 @@ export async function POST(req: Request) { messages: await convertToModelMessages(messages), tools: { ...reactEditorTools, - updatePage, generateImage, searchProducts, getProductByHandle, diff --git a/app/api/save-schema/route.ts b/app/api/save-schema/route.ts deleted file mode 100644 index 7f2fbb2..0000000 --- a/app/api/save-schema/route.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { NextResponse } from "next/server"; -import { patchRoute } from "@/lib/schema.server"; - -export async function POST(request: Request) { - try { - const { route, data } = await request.json(); - if (typeof route !== "string" || !data || typeof data !== "object") { - return NextResponse.json( - { ok: false, error: "expected { route, data }" }, - { status: 400 }, - ); - } - const schema = await patchRoute(route, data); - return NextResponse.json({ ok: true, routes: Object.keys(schema) }); - } catch (err) { - return NextResponse.json( - { ok: false, error: err instanceof Error ? err.message : "save failed" }, - { status: 500 }, - ); - } -} diff --git a/components/EditorClient.tsx b/components/EditorClient.tsx index 2de2176..6f3deb8 100644 --- a/components/EditorClient.tsx +++ b/components/EditorClient.tsx @@ -88,11 +88,12 @@ export default function EditorClient({ const persist = useCallback(async (route: string, next: any) => { setSchema((s) => ({ ...s, [route]: next })); - await fetch("/api/save-schema", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ route, data: next }), - }); + if (typeof window !== "undefined" && window.parent !== window) { + window.parent.postMessage( + { type: "PUBLISH", path: route, data: next }, + "*", + ); + } }, []); const plugins = useMemo( diff --git a/lib/schema.server.ts b/lib/schema.server.ts index fa2ae39..00741fc 100644 --- a/lib/schema.server.ts +++ b/lib/schema.server.ts @@ -1,39 +1,7 @@ -import fs from "node:fs/promises"; -import path from "node:path"; - -export const SCHEMA_PATH = path.join(process.cwd(), "app.schema.json"); +import schemaJson from "../app.schema.json"; export type Schema = Record; -const FALLBACK: Schema = { - "/": { root: { props: { title: "Untitled" } }, content: [] }, -}; - export async function readSchema(): Promise { - try { - const raw = await fs.readFile(SCHEMA_PATH, "utf8"); - const parsed = JSON.parse(raw); - if (parsed && typeof parsed === "object") return parsed as Schema; - return FALLBACK; - } catch { - return FALLBACK; - } -} - -export async function writeSchema(schema: Schema): Promise { - await fs.writeFile( - SCHEMA_PATH, - JSON.stringify(schema, null, 2) + "\n", - "utf8", - ); -} - -export async function patchRoute( - route: string, - data: { root: any; content: any[] }, -): Promise { - const current = await readSchema(); - const next: Schema = { ...current, [route]: data }; - await writeSchema(next); - return next; + return schemaJson as Schema; }