Skip to content

Commit 13e91f1

Browse files
fix(ai): 文档助手按页面语言读正文,/en 页不再拿到中文原文
chat 路由 source.getPage(slugArray) 没传 locale,fumadocs 固定返回 zh 原文, 英文文档页的助手上下文一直是中文。DocsAssistant 的 pageContext 带上 locale, 路由用 hasLocale 校验后传给 getPage;非法值走 fallback(zh)。
1 parent fbff25a commit 13e91f1

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

‎app/[locale]/docs/[...slug]/page.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ export default async function DocPage({ params }: Param) {
214214
title: page.data.title,
215215
description: page.data.description,
216216
slug: slug?.join("/"),
217+
locale,
217218
}}
218219
/>
219220
</>

‎app/api/chat/route.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { streamText, UIMessage, convertToModelMessages } from "ai";
22
import { getModel, requiresApiKey, type AIProvider } from "@/lib/ai/models";
33
import { buildSystemMessage } from "@/lib/ai/prompt";
44
import { source } from "@/lib/source";
5+
import { routing } from "@/i18n/routing";
6+
import { hasLocale } from "next-intl";
57
import { limitChat, rateLimitResponse } from "@/lib/rate-limit";
68
import fs from "fs/promises";
79
import path from "path";
@@ -20,6 +22,7 @@ interface ChatRequest {
2022
description?: string;
2123
content?: string;
2224
slug?: string;
25+
locale?: string;
2326
};
2427
provider?: AIProvider;
2528
apiKey?: string;
@@ -140,7 +143,11 @@ export async function POST(req: Request) {
140143
if (pageContext?.slug && !pageContext.content) {
141144
try {
142145
const slugArray = pageContext.slug.split("/");
143-
const page = source.getPage(slugArray);
146+
// 不传 locale 会固定拿到 zh 原文,/en 页的助手就读中文;非法值交给 fallback(zh)
147+
const locale = hasLocale(routing.locales, pageContext.locale)
148+
? pageContext.locale
149+
: undefined;
150+
const page = source.getPage(slugArray, locale);
144151

145152
if (page) {
146153
const cachedContent = mdxContentCache.get(page.path);

‎app/components/DocsAssistant.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface PageContext {
1919
title?: string;
2020
description?: string;
2121
slug?: string;
22+
locale?: string;
2223
}
2324

2425
export interface WelcomeSuggestion {

0 commit comments

Comments
 (0)