update remove vite config aiPlugin
This commit is contained in:
@@ -1,6 +0,0 @@
|
|||||||
# Shopify (defaults to mock.shop if unset)
|
|
||||||
VITE_SHOPIFY_DOMAIN=mock.shop
|
|
||||||
VITE_SHOPIFY_STOREFRONT_ACCESS_TOKEN=
|
|
||||||
|
|
||||||
# Anthropic — used by /api/chat. Required for the AI panel.
|
|
||||||
ANTHROPIC_API_KEY=
|
|
||||||
@@ -1,56 +1,7 @@
|
|||||||
import { defineConfig, loadEnv, type Plugin } from "vite";
|
import { defineConfig, loadEnv } from "vite";
|
||||||
import react from "@vitejs/plugin-react";
|
import react from "@vitejs/plugin-react";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
function apiChatPlugin(): Plugin {
|
|
||||||
return {
|
|
||||||
name: "api-chat-middleware",
|
|
||||||
configureServer(server) {
|
|
||||||
server.middlewares.use("/api/chat", async (req, res, next) => {
|
|
||||||
if (req.method !== "POST") return next();
|
|
||||||
try {
|
|
||||||
const mod = await server.ssrLoadModule("/api/chat.ts");
|
|
||||||
const handler = (mod as { POST: (req: Request) => Promise<Response> })
|
|
||||||
.POST;
|
|
||||||
|
|
||||||
const chunks: Buffer[] = [];
|
|
||||||
for await (const c of req) chunks.push(c as Buffer);
|
|
||||||
const body = chunks.length ? Buffer.concat(chunks) : undefined;
|
|
||||||
|
|
||||||
const headers = new Headers();
|
|
||||||
for (const [k, v] of Object.entries(req.headers)) {
|
|
||||||
if (Array.isArray(v)) headers.set(k, v.join(", "));
|
|
||||||
else if (typeof v === "string") headers.set(k, v);
|
|
||||||
}
|
|
||||||
|
|
||||||
const fetchReq = new Request(
|
|
||||||
`http://localhost${req.url ?? "/api/chat"}`,
|
|
||||||
{ method: "POST", headers, body },
|
|
||||||
);
|
|
||||||
const response = await handler(fetchReq);
|
|
||||||
|
|
||||||
res.statusCode = response.status;
|
|
||||||
response.headers.forEach((value, key) => res.setHeader(key, value));
|
|
||||||
|
|
||||||
if (response.body) {
|
|
||||||
const reader = response.body.getReader();
|
|
||||||
while (true) {
|
|
||||||
const { done, value } = await reader.read();
|
|
||||||
if (done) break;
|
|
||||||
res.write(Buffer.from(value));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res.end();
|
|
||||||
} catch (err) {
|
|
||||||
console.error("/api/chat error", err);
|
|
||||||
res.statusCode = 500;
|
|
||||||
res.end(err instanceof Error ? err.message : "internal error");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineConfig(({ mode }) => {
|
export default defineConfig(({ mode }) => {
|
||||||
const env = loadEnv(mode, process.cwd(), "");
|
const env = loadEnv(mode, process.cwd(), "");
|
||||||
for (const k of Object.keys(env)) {
|
for (const k of Object.keys(env)) {
|
||||||
@@ -58,7 +9,7 @@ export default defineConfig(({ mode }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
plugins: [react(), apiChatPlugin()],
|
plugins: [react()],
|
||||||
resolve: {
|
resolve: {
|
||||||
alias: {
|
alias: {
|
||||||
"@": path.resolve(__dirname, "."),
|
"@": path.resolve(__dirname, "."),
|
||||||
|
|||||||
Reference in New Issue
Block a user