Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@ import { start } from "workflow/api";
import { analysisWorkflow } from "@/workflows/analysis";

export async function POST(request: Request) {
const botIdResult = await checkBotId();
Sentry.logger.debug("BotID check result", {
isBot: botIdResult.isBot,
userAgent: request.headers.get("user-agent"),
});
if (botIdResult.isBot) {
Sentry.logger.warn("BotID check failed", {
isBot: botIdResult.isBot,
userAgent: request.headers.get("user-agent"),
});
return new Response("Access Denied", { status: 403 });
if (process.env.VERCEL_ENV === "production") {
try {
const botIdResult = await checkBotId();
Sentry.logger.debug("BotID check result", {
isBot: botIdResult.isBot,
userAgent: request.headers.get("user-agent"),
});
if (botIdResult.isBot) {
Sentry.logger.warn("BotID check failed", {
isBot: botIdResult.isBot,
userAgent: request.headers.get("user-agent"),
});
return new Response("Access Denied", { status: 403 });
}
} catch (error) {
Sentry.logger.warn("BotID check threw exception", {
error: error instanceof Error ? error.message : String(error),
userAgent: request.headers.get("user-agent"),
});
}
}

const analysisStartTime = Date.now();
Expand Down Expand Up @@ -113,19 +122,19 @@ export async function POST(request: Request) {
"webvitals.run_id": run.runId,
});

const response = createUIMessageStreamResponse({
return createUIMessageStreamResponse({
headers: {
"x-workflow-run-id": run.runId,
},
stream: run.readable,
});
response.headers.set("x-workflow-run-id", run.runId);
return response;
} catch (error) {
const durationMs = Date.now() - analysisStartTime;

span.setAttributes({
"webvitals.outcome": "failed",
"webvitals.duration_ms": durationMs,
"webvitals.error":
error instanceof Error ? error.message : "Unknown",
"webvitals.error": error instanceof Error ? error.message : "Unknown",
});

Sentry.metrics.count("webvitals.analysis.completed", 1, {
Expand Down Expand Up @@ -169,8 +178,7 @@ export async function POST(request: Request) {
return Response.json(
{
error: "Failed to process chat request",
details:
error instanceof Error ? error.message : "Unknown error",
details: error instanceof Error ? error.message : "Unknown error",
},
{ status: 500 },
);
Expand Down
Loading