Skip to content

Commit 4f7d27b

Browse files
committed
fix: 修复一些报错
1 parent 4d0ccd5 commit 4f7d27b

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

app/api/chat/route.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { source } from "@/lib/source";
66
import fs from "fs/promises";
77
import path from "path";
88

9+
// 缓存解析后的 MDX 文本,减少重复的磁盘 I/O 和正则解析开销
10+
const mdxContentCache = new Map<string, string>();
11+
912
// 流式响应最长30秒
1013
export const maxDuration = 30;
1114

@@ -52,9 +55,24 @@ export async function POST(req: Request) {
5255
const page = source.getPage(slugArray);
5356

5457
if (page) {
55-
const fullFilePath = path.join(process.cwd(), "app/docs", page.path);
56-
const rawContent = await fs.readFile(fullFilePath, "utf-8");
57-
pageContext.content = extractTextFromMDX(rawContent);
58+
const cachedContent = mdxContentCache.get(page.path);
59+
60+
// 在生产环境下使用缓存,开发环境下不使用以支持文档热更新
61+
if (cachedContent) {
62+
console.log("[Cache hit!!!!]", page.path);
63+
pageContext.content = cachedContent;
64+
} else {
65+
const fullFilePath = path.join(
66+
process.cwd(),
67+
"app/docs",
68+
page.path,
69+
);
70+
const rawContent = await fs.readFile(fullFilePath, "utf-8");
71+
const content = extractTextFromMDX(rawContent);
72+
73+
mdxContentCache.set(page.path, content);
74+
pageContext.content = content;
75+
}
5876
}
5977
} catch (error) {
6078
console.warn(
@@ -80,7 +98,7 @@ export async function POST(req: Request) {
8098
const result = streamText({
8199
model: model,
82100
system: systemMessage,
83-
messages: convertToModelMessages(messages),
101+
messages: convertToModelMessages(messages || []),
84102
onFinish: async ({ text }) => {
85103
try {
86104
// 1. 保存/更新会话
@@ -92,13 +110,17 @@ export async function POST(req: Request) {
92110

93111
// 2. 保存用户消息 (取最后一条)
94112
// AI SDK v5 中,UIMessage 不再有 content 字段,内容在 parts 数组中
95-
const lastUserMessage = messages[messages.length - 1];
113+
const safeMessages = messages || [];
114+
const lastUserMessage = safeMessages[safeMessages.length - 1];
96115
if (lastUserMessage && lastUserMessage.role === "user") {
97116
// 从 parts 数组中提取所有文本内容并拼接
98-
const userContent = lastUserMessage.parts
99-
.filter((part) => part.type === "text")
100-
.map((part) => (part as { type: "text"; text: string }).text)
101-
.join("\n");
117+
const userContent = Array.isArray(lastUserMessage.parts)
118+
? lastUserMessage.parts
119+
.filter((part) => part.type === "text")
120+
.map((part) => (part as { type: "text"; text: string }).text)
121+
.join("\n")
122+
: (lastUserMessage as unknown as { content?: string })?.content ||
123+
"";
102124

103125
await prisma.message.create({
104126
data: {

data/event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
{
2929
"name": "Open.Onion",
30-
"discord": "https://discord.gg/mRcAKNnbTy?event=1476959641291325623",
30+
"discord": "https://discord.gg/kJZFMr5chU?event=1477581193582088304",
3131
"playback": "",
3232
"coverUrl": "./event/openOnion.webp",
3333
"deprecated": false

0 commit comments

Comments
 (0)