use postMessage
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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(
|
||||
|
||||
@@ -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<string, { root: any; content: any[] }>;
|
||||
|
||||
const FALLBACK: Schema = {
|
||||
"/": { root: { props: { title: "Untitled" } }, content: [] },
|
||||
};
|
||||
|
||||
export async function readSchema(): Promise<Schema> {
|
||||
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<void> {
|
||||
await fs.writeFile(
|
||||
SCHEMA_PATH,
|
||||
JSON.stringify(schema, null, 2) + "\n",
|
||||
"utf8",
|
||||
);
|
||||
}
|
||||
|
||||
export async function patchRoute(
|
||||
route: string,
|
||||
data: { root: any; content: any[] },
|
||||
): Promise<Schema> {
|
||||
const current = await readSchema();
|
||||
const next: Schema = { ...current, [route]: data };
|
||||
await writeSchema(next);
|
||||
return next;
|
||||
return schemaJson as Schema;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user