Skip to content

Commit

Permalink
move ai to api route
Browse files Browse the repository at this point in the history
  • Loading branch information
GraemeFulton committed Jul 29, 2024
1 parent 1b4895b commit 392a77b
Show file tree
Hide file tree
Showing 5 changed files with 2,893 additions and 2,828 deletions.
58 changes: 58 additions & 0 deletions app/api/interview/generate/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import OpenAI from "openai";

import { getIronSession } from "iron-session";
import { sessionOptions } from "@/lib/iron-session/session";
import { generate } from "@prototypr/paper-interview/dist/api/generate";
import { NextResponse } from "next/server";

export const runtime = "edge";
export const dynamic = "force-dynamic";

const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});


async function getSession(req) {
const res = new Response();
const session = await getIronSession(req, res, sessionOptions);
return session;
}

export async function POST(req) {
try {
const session = await getSession(req);
const user = session.user;

if (!user?.login?.jwt) {
console.log("no token");
return NextResponse.json(
{
status: 500,
message: "User is not authenticated - invalid token",
},
{ status: 401 }
);
}

const userId = user?.login?.user?.id;

if (!userId) {
return NextResponse.json(
{ message: "User is not authenticated" },
{ status: 401 }
);
}

// Call generate and get the stream
const stream = await generate({ req, user, openai });

// Return the stream directly
return new Response(stream, {
headers: { "Content-Type": "text/event-stream" },
});
} catch (error) {
console.error(error);
return NextResponse.json({ error: error.message }, { status: 500 });
}
}
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
"zod": "^3.23.8"
},
"optionalDependencies": {
"@prototypr/paper-interview": "^0.0.30",
"@prototypr/paper-interview": "0.0.31",
"@prototypr/prototypr-postie": "^1.0.91"
},
"resolutions": {
Expand Down
68 changes: 0 additions & 68 deletions pages/api/interview/generate.js

This file was deleted.

Loading

0 comments on commit 392a77b

Please sign in to comment.