From ee78c29ee5923929418372bebabb9be081548f2c Mon Sep 17 00:00:00 2001 From: Li Xin Date: Mon, 21 Apr 2025 21:10:08 +0800 Subject: [PATCH] chore: remove proxy route --- web/src/app/api/chat/stream/route.ts | 75 ---------------------------- 1 file changed, 75 deletions(-) delete mode 100644 web/src/app/api/chat/stream/route.ts diff --git a/web/src/app/api/chat/stream/route.ts b/web/src/app/api/chat/stream/route.ts deleted file mode 100644 index d441852..0000000 --- a/web/src/app/api/chat/stream/route.ts +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates -// SPDX-License-Identifier: MIT - -import { NextResponse, type NextRequest } from "next/server"; - -import { env } from "~/env"; - -export async function POST(request: NextRequest) { - const requestBody = await request.json(); - - const stream = new ReadableStream({ - async start(controller) { - try { - const response = await fetch( - (env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000/api") + - "/chat/stream", - { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(requestBody), - }, - ); - if (!response.ok) { - const errorBody = await response.text(); - console.error("API error message:", errorBody); - controller.enqueue( - createErrorEvent(`API responded with status ${response.status}`), - ); - controller.close(); - return; - } - const reader = response.body - ?.pipeThrough(new TextDecoderStream()) - .getReader(); - if (!reader) { - controller.enqueue( - createErrorEvent("No data received from /api/chat/stream"), - ); - controller.close(); - return; - } - - while (true) { - const { done, value } = await reader.read(); - if (done) { - break; - } - controller.enqueue(value); - } - - controller.close(); - reader.releaseLock(); - } catch (error) { - console.error("Stream error:", error); - controller.enqueue(createErrorEvent("Stream interrupted")); - controller.close(); - } - }, - }); - - return new NextResponse(stream, { - headers: { - "Cache-Control": "no-cache, no-transform", - Connection: "keep-alive", - "Content-Type": "text/event-stream", - }, - status: 200, - }); -} - -function createErrorEvent(message: string) { - return `event: error\ndata: ${message}\n\n`; -}