diff --git a/app/api/ai/route.ts b/app/api/ai/route.ts
index 01c5bb5..527e184 100644
--- a/app/api/ai/route.ts
+++ b/app/api/ai/route.ts
@@ -10,6 +10,11 @@ import type { AiRequestBody } from "@/lib/ai/types";
export const runtime = "nodejs";
+const STREAM_HEADERS = {
+ "Cache-Control": "no-cache, no-transform",
+ "X-Accel-Buffering": "no",
+};
+
export async function POST(req: Request) {
try {
const body = (await req.json()) as AiRequestBody;
@@ -33,12 +38,19 @@ export async function POST(req: Request) {
maxRetries: 0,
});
- return result.toTextStreamResponse();
+ const response = result.toTextStreamResponse();
+ for (const [key, value] of Object.entries(STREAM_HEADERS)) {
+ response.headers.set(key, value);
+ }
+ return response;
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
return new Response(message, {
status: 500,
- headers: { "Content-Type": "text/plain; charset=utf-8" },
+ headers: {
+ "Content-Type": "text/plain; charset=utf-8",
+ ...STREAM_HEADERS,
+ },
});
}
}
diff --git a/components/modes/bazi-form.tsx b/components/modes/bazi-form.tsx
index f0f8d8e..cf0b4c3 100644
--- a/components/modes/bazi-form.tsx
+++ b/components/modes/bazi-form.tsx
@@ -163,7 +163,7 @@ export default function BaziForm() {
)}
{showAi && (
-
+
+
{showAi && (
-
+
{
- if (
- !scrollRef.current ||
- scrollRef.current.scrollHeight ===
- scrollRef.current.clientHeight + scrollRef.current.scrollTop
- ) {
- return;
- }
- scrollRef.current.scrollTo(0, scrollRef.current.scrollHeight);
- });
- }
+ scrollRef.current?.scrollTo(0, scrollRef.current.scrollHeight);
+ }, [completion, autoScroll]);
function onScroll(e: HTMLElement) {
if (!isLoading) {
return;
}
const hitBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 15;
- if (hitBottom === autoScroll) {
- return;
+ if (hitBottom !== autoScroll) {
+ setAutoScroll(hitBottom);
}
- setAutoScroll(hitBottom);
}
return (
-
+
{isLoading && (
-
-
-
-
- AI 分析中...
-
+
+
+ AI 分析中...
)}
onScroll(e.currentTarget)}
- className="max-h-full overflow-auto rounded-md border p-3 shadow dark:border-0 dark:bg-secondary/90 dark:shadow-none sm:p-5"
+ className="min-h-[240px] max-h-[420px] overflow-y-auto rounded-md border bg-background p-3 shadow sm:p-5 dark:border-0 dark:bg-secondary/90 dark:shadow-none"
>
{error ? (
-
- ಠ_ಠ 请求出错了!
-
- {error}
+
+ ) : completion ? (
+
+ {completion}
+
+ ) : isLoading ? (
+
正在等待 AI 响应...
) : (
-
{completion}
+
暂无解读内容
)}
{!isLoading && (
-