feat: add APIMart AI generation and guided Studio onboarding
This commit is contained in:
1 parent
9a6a2e0a8d
commit
60467a4c05
9 files changed
+455
-2
No files matched your search
@@ -1,7 +1,10 @@
|
||||
import type { Middleware } from "@wrnexus/core";
|
||||
import type { createStudioHandler } from "@wrnexus/design-studio/server";
|
||||
import { studioAI } from "../lib/studio-ai.ts";
|
||||
import { addStudioGuide, studioHelpScript } from "../lib/studio-guide.ts";
|
||||
|
||||
let studio: ReturnType<typeof createStudioHandler> | undefined;
|
||||
let ai: ReturnType<typeof studioAI> | undefined;
|
||||
|
||||
const designStudio: Middleware = async (ctx, next) => {
|
||||
const url = new URL(ctx.req.url);
|
||||
@@ -11,6 +14,19 @@ const designStudio: Middleware = async (ctx, next) => {
|
||||
headers: { location: "/design-studio/" },
|
||||
});
|
||||
if (!url.pathname.startsWith("/design-studio/")) return next();
|
||||
ai ??= studioAI(process.env.APIMART_API_KEY?.trim());
|
||||
const generated = await ai(ctx.req);
|
||||
if (generated) return generated;
|
||||
if (
|
||||
url.pathname === "/design-studio/site-help.js" &&
|
||||
ctx.req.method === "GET"
|
||||
)
|
||||
return new Response(studioHelpScript, {
|
||||
headers: {
|
||||
"content-type": "text/javascript",
|
||||
"cache-control": "no-store",
|
||||
},
|
||||
});
|
||||
// Load the installed package at runtime so its assets keep their package-relative URLs.
|
||||
const studioModule = import.meta.resolve("@wrnexus/design-studio/server");
|
||||
studio ??= import(studioModule)
|
||||
@@ -23,7 +39,23 @@ const designStudio: Middleware = async (ctx, next) => {
|
||||
studio = undefined;
|
||||
throw error;
|
||||
});
|
||||
return (await studio)(ctx.req);
|
||||
const response = await (await studio)(ctx.req);
|
||||
if (
|
||||
url.pathname === "/design-studio/" &&
|
||||
response.ok &&
|
||||
ctx.req.method === "GET"
|
||||
) {
|
||||
const headers = new Headers(response.headers);
|
||||
headers.delete("content-length");
|
||||
return new Response(
|
||||
addStudioGuide(
|
||||
await response.text(),
|
||||
!!process.env.APIMART_API_KEY?.trim(),
|
||||
),
|
||||
{ status: response.status, headers },
|
||||
);
|
||||
}
|
||||
return response;
|
||||
};
|
||||
|
||||
export default designStudio;
|
||||
Reference in new issue
Block a user