-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b4895b
commit 392a77b
Showing
5 changed files
with
2,893 additions
and
2,828 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.