Skip to content

Commit

Permalink
feat(web/server): more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mrevanzak committed May 26, 2024
1 parent 45b864a commit e64dd50
Showing 1 changed file with 59 additions and 51 deletions.
110 changes: 59 additions & 51 deletions apps/web/src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,62 +7,70 @@ import { fromZodError } from "zod-validation-error";
export const runtime = "edge";

export async function POST(req: Request) {
const session = await auth();
if (!session?.user) {
return new Response("Unauthorized", { status: 401 });
}
try {
const session = await auth();
if (!session?.user) {
return new Response("Unauthorized", { status: 401 });
}

const body = z
.object({
messages: z
.object({
role: z.enum(["user", "assistant"] as const),
content: z.string(),
})
.array(),
})
.safeParse(await req.json());
const body = z
.object({
messages: z
.object({
role: z.enum(["user", "assistant"] as const),
content: z.string(),
})
.array(),
})
.safeParse(await req.json());

if (body.error) {
const error = fromZodError(body.error).toString();
return new Response(error, { status: 400 });
}
if (body.error) {
const error = fromZodError(body.error).toString();
return new Response(error, { status: 400 });
}

const isTesting = await get("testing");
const res = await fetch(
env.BACKEND_URL + "/questions" + (isTesting && "/stream-generator"),
{
method: "POST",
headers: {
"Content-Type": "application/json",
const isTesting = await get("testing");
const res = await fetch(
env.BACKEND_URL + "/questions" + (isTesting && "/stream-generator"),
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
question: body.data.messages.pop()?.content,
isBahasa: true,
}),
},
body: JSON.stringify({
question: body.data.messages.pop()?.content,
isBahasa: true,
}),
},
);
);

if (!res.body) {
return new Response("No response body", { status: 500 });
}
const reader = res.body.getReader();
const stream = new ReadableStream({
async start(controller) {
while (true) {
const { done, value } = await reader.read();
if (done) {
controller.close();
break;
}
// Convert Uint8Array to string and remove "data: " prefix
const text = new TextDecoder().decode(value).replace(/^data:\s*/gm, "");
if (!res.body) {
return new Response("No response body", { status: 500 });
}
const reader = res.body.getReader();
const stream = new ReadableStream({
async start(controller) {
while (true) {
const { done, value } = await reader.read();
if (done) {
controller.close();
break;
}
// Convert Uint8Array to string and remove "data: " prefix
const text = new TextDecoder()
.decode(value)
.replace(/^data:\s*/gm, "");

// Enqueue the modified chunk back into the stream
controller.enqueue(new TextEncoder().encode(text));
}
},
});
// Enqueue the modified chunk back into the stream
controller.enqueue(new TextEncoder().encode(text));
}
},
});

return new Response(stream);
return new Response(stream);
} catch (error) {
if (error instanceof Error) {
return new Response(error.message, { status: 500 });
}
}
}

0 comments on commit e64dd50

Please sign in to comment.