Skip to content

Commit 512cac2

Browse files
committed
style: 将本pr涉及的代码(主要是AI模块)内的注释改成中文,便于协作
1 parent d2e764a commit 512cac2

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

app/api/chat/route.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import { streamText, UIMessage, convertToModelMessages } from "ai";
22
import { getModel, requiresApiKey, type AIProvider } from "@/lib/ai/models";
33
import { buildSystemMessage } from "@/lib/ai/prompt";
44

5-
// Allow streaming responses up to 30 seconds
5+
// 流式响应最长30秒
66
export const maxDuration = 30;
77

88
interface ChatRequest {
99
messages: UIMessage[];
10-
system?: string; // System message forwarded from AssistantChatTransport
11-
tools?: unknown; // Frontend tools forwarded from AssistantChatTransport
10+
system?: string;
11+
tools?: unknown;
1212
pageContext?: {
1313
title?: string;
1414
description?: string;
@@ -25,11 +25,11 @@ export async function POST(req: Request) {
2525
messages,
2626
system,
2727
pageContext,
28-
provider = "openai", // Default to OpenAI
28+
provider = "intern", // 默认使用书生模型
2929
apiKey,
3030
}: ChatRequest = await req.json();
3131

32-
// Validate API key for providers that require it
32+
// 对指定Provider验证key是否存在
3333
if (requiresApiKey(provider) && (!apiKey || apiKey.trim() === "")) {
3434
return Response.json(
3535
{
@@ -40,13 +40,13 @@ export async function POST(req: Request) {
4040
);
4141
}
4242

43-
// Build system message with page context
43+
// 构建系统消息,包含页面上下文
4444
const systemMessage = buildSystemMessage(system, pageContext);
4545

46-
// Get AI model instance based on provider
46+
// 根据Provider获取 AI 模型实例
4747
const model = getModel(provider, apiKey);
4848

49-
// Generate streaming response
49+
// 生成流式响应
5050
const result = streamText({
5151
model: model,
5252
system: systemMessage,
@@ -57,7 +57,7 @@ export async function POST(req: Request) {
5757
} catch (error) {
5858
console.error("Chat API error:", error);
5959

60-
// Handle specific model creation errors
60+
// 处理特定模型创建错误
6161
if (error instanceof Error && error.message.includes("API key")) {
6262
return Response.json({ error: error.message }, { status: 400 });
6363
}

lib/ai/models.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { createInternModel } from "./providers/intern";
55
export type AIProvider = "openai" | "gemini" | "intern";
66

77
/**
8-
* Model factory that returns the appropriate AI model based on provider
9-
* @param provider - The AI provider to use
10-
* @param apiKey - API key (not required for intern provider)
11-
* @returns Configured AI model instance
8+
* Model工厂 用于返回对应的 AI 模型实例
9+
* @param provider - 要用的provider
10+
* @param apiKey - API key (intern provider不需要用户提供 API key)
11+
* @returns 配置好的 AI 模型实例
1212
*/
1313
export function getModel(provider: AIProvider, apiKey?: string) {
1414
switch (provider) {
@@ -25,7 +25,7 @@ export function getModel(provider: AIProvider, apiKey?: string) {
2525
return createGeminiModel(apiKey);
2626

2727
case "intern":
28-
// Intern provider doesn't need API key from user
28+
// Intern 书生模型不需要用户提供 API key
2929
return createInternModel();
3030

3131
default:
@@ -34,9 +34,9 @@ export function getModel(provider: AIProvider, apiKey?: string) {
3434
}
3535

3636
/**
37-
* Check if the given provider requires an API key from the user
38-
* @param provider - The AI provider to check
39-
* @returns True if API key is required, false otherwise
37+
* 检查指定的提供者是否需要用户提供 API key
38+
* @param provider - 要检查的provider
39+
* @returns 如果需要 API key,返回 true,否则返回 false
4040
*/
4141
export function requiresApiKey(provider: AIProvider): boolean {
4242
return provider !== "intern";

lib/ai/prompt.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ interface PageContext {
66
}
77

88
/**
9-
* Build system message with page context for AI assistant
10-
* @param customSystem - Custom system message (optional)
11-
* @param pageContext - Current page context (optional)
12-
* @returns Complete system message string
9+
* 构建系统消息,包含页面上下文
10+
* @param customSystem - 自定义系统消息 (可选)
11+
* @param pageContext - 当前页面上下文 (可选)
12+
* @returns 完整的系统消息字符串
1313
*/
1414
export function buildSystemMessage(
1515
customSystem?: string,
1616
pageContext?: PageContext,
1717
): string {
18-
// Default system message for documentation assistant
18+
// 默认系统消息
1919
let systemMessage =
2020
customSystem ||
2121
`You are a helpful AI assistant for a documentation website.
2222
You can help users understand the documentation, answer questions about the content,
2323
and provide guidance on the topics covered in the docs. Be concise and helpful.`;
2424

25-
// Add current page context if available
25+
// 如果当前页面上下文可用,则添加到系统消息中
2626
if (pageContext?.content) {
2727
systemMessage += `\n\n--- CURRENT PAGE CONTEXT ---\n`;
2828

0 commit comments

Comments
 (0)