From 916a680be99d770e3e368e1a03e231c7103df924 Mon Sep 17 00:00:00 2001 From: Kirill Chernakov Date: Thu, 21 Nov 2024 14:14:50 +0400 Subject: [PATCH] refactor: move aws-markerplace and copilotkit routes to App router --- keep-ui/app/api/aws-marketplace/route.ts | 21 ++++++++++++++++++ keep-ui/app/api/copilotkit/route.ts | 24 ++++++++++++++++++++ keep-ui/pages/_error.jsx | 17 -------------- keep-ui/pages/api/aws-marketplace.tsx | 23 ------------------- keep-ui/pages/api/copilotkit.ts | 28 ------------------------ 5 files changed, 45 insertions(+), 68 deletions(-) create mode 100644 keep-ui/app/api/aws-marketplace/route.ts create mode 100644 keep-ui/app/api/copilotkit/route.ts delete mode 100644 keep-ui/pages/_error.jsx delete mode 100644 keep-ui/pages/api/aws-marketplace.tsx delete mode 100644 keep-ui/pages/api/copilotkit.ts diff --git a/keep-ui/app/api/aws-marketplace/route.ts b/keep-ui/app/api/aws-marketplace/route.ts new file mode 100644 index 000000000..f8c4bb07c --- /dev/null +++ b/keep-ui/app/api/aws-marketplace/route.ts @@ -0,0 +1,21 @@ +import { NextRequest } from "next/server"; +import { redirect } from "next/navigation"; + +export async function POST(request: NextRequest) { + try { + // In App Router, we need to parse the request body manually + const body = await request.json(); + + const token = body["x-amzn-marketplace-token"]; + const offerType = body["x-amzn-marketplace-offer-type"]; + + // Base64 encode the token + const base64EncodedToken = encodeURIComponent(btoa(token)); + + // In App Router, we use the redirect function for redirects + return redirect(`/signin?amt=${base64EncodedToken}`); + } catch (error) { + console.error("Error processing request:", error); + return new Response("Bad Request", { status: 400 }); + } +} diff --git a/keep-ui/app/api/copilotkit/route.ts b/keep-ui/app/api/copilotkit/route.ts new file mode 100644 index 000000000..d8ff2a8dc --- /dev/null +++ b/keep-ui/app/api/copilotkit/route.ts @@ -0,0 +1,24 @@ +import { + CopilotRuntime, + OpenAIAdapter, + copilotRuntimeNextJSAppRouterEndpoint, +} from "@copilotkit/runtime"; +import OpenAI from "openai"; +import { NextRequest } from "next/server"; + +const openai = new OpenAI({ + organization: process.env.OPEN_AI_ORGANIZATION_ID, + apiKey: process.env.OPEN_AI_API_KEY, +}); +const serviceAdapter = new OpenAIAdapter({ openai }); +const runtime = new CopilotRuntime(); + +export const POST = async (req: NextRequest) => { + const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({ + runtime, + serviceAdapter, + endpoint: "/api/copilotkit", + }); + + return handleRequest(req); +}; diff --git a/keep-ui/pages/_error.jsx b/keep-ui/pages/_error.jsx deleted file mode 100644 index 46a61d690..000000000 --- a/keep-ui/pages/_error.jsx +++ /dev/null @@ -1,17 +0,0 @@ -import * as Sentry from "@sentry/nextjs"; -import Error from "next/error"; - -const CustomErrorComponent = (props) => { - return ; -}; - -CustomErrorComponent.getInitialProps = async (contextData) => { - // In case this is running in a serverless function, await this in order to give Sentry - // time to send the error before the lambda exits - await Sentry.captureUnderscoreErrorException(contextData); - - // This will contain the status code of the response - return Error.getInitialProps(contextData); -}; - -export default CustomErrorComponent; diff --git a/keep-ui/pages/api/aws-marketplace.tsx b/keep-ui/pages/api/aws-marketplace.tsx deleted file mode 100644 index 8d5b8d203..000000000 --- a/keep-ui/pages/api/aws-marketplace.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse -) { - if (req.method === "POST") { - const { - "x-amzn-marketplace-token": token, - "x-amzn-marketplace-offer-type": offerType, - } = req.body; - - const base64EncodedToken = encodeURIComponent(btoa(token)); - - // Redirect to the sign-in page or wherever you want - // amt is amazon-marketplace-token - res.writeHead(302, { Location: `/signin?amt=${base64EncodedToken}` }); - res.end(); - } else { - // Handle any non-POST requests - res.status(405).send("Method Not Allowed"); - } -} diff --git a/keep-ui/pages/api/copilotkit.ts b/keep-ui/pages/api/copilotkit.ts deleted file mode 100644 index 2af1161ab..000000000 --- a/keep-ui/pages/api/copilotkit.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { NextApiRequest, NextApiResponse } from "next"; -import { - CopilotRuntime, - OpenAIAdapter, - copilotRuntimeNextJSPagesRouterEndpoint, -} from "@copilotkit/runtime"; -import OpenAI from "openai"; - -const openai = new OpenAI({ - organization: process.env.OPEN_AI_ORGANIZATION_ID, - apiKey: process.env.OPEN_AI_API_KEY, -}); - -export default async function handler( - req: NextApiRequest, - res: NextApiResponse -) { - const serviceAdapter = new OpenAIAdapter({ openai }); - const runtime = new CopilotRuntime(); - - const handleRequest = copilotRuntimeNextJSPagesRouterEndpoint({ - endpoint: "/api/copilotkit", - runtime, - serviceAdapter, - }); - - return await handleRequest(req, res); -}