1
- import { createOpenAI } from "@ai-sdk/openai" ;
2
- import { createGoogleGenerativeAI } from "@ai-sdk/google" ;
3
1
import { streamText , UIMessage , convertToModelMessages } from "ai" ;
2
+ import { getModel , requiresApiKey , type AIProvider } from "@/lib/ai/models" ;
3
+ import { buildSystemMessage } from "@/lib/ai/prompt" ;
4
4
5
- // Allow streaming responses up to 30 seconds
5
+ // 流式响应最长30秒
6
6
export const maxDuration = 30 ;
7
7
8
- export async function POST ( req : Request ) {
9
- const {
10
- messages,
11
- system,
12
- pageContext,
13
- provider,
14
- apiKey,
15
- } : {
16
- messages : UIMessage [ ] ;
17
- system ?: string ; // System message forwarded from AssistantChatTransport
18
- tools ?: unknown ; // Frontend tools forwarded from AssistantChatTransport
19
- pageContext ?: {
20
- title ?: string ;
21
- description ?: string ;
22
- content ?: string ;
23
- slug ?: string ;
24
- } ;
25
- provider ?: "openai" | "gemini" ;
26
- apiKey ?: string ;
27
- } = await req . json ( ) ;
28
-
29
- // Check if API key is provided
30
- if ( ! apiKey || apiKey . trim ( ) === "" ) {
31
- return Response . json (
32
- {
33
- error :
34
- "API key is required. Please configure your API key in the settings." ,
35
- } ,
36
- { status : 400 } ,
37
- ) ;
38
- }
8
+ interface ChatRequest {
9
+ messages : UIMessage [ ] ;
10
+ system ?: string ;
11
+ tools ?: unknown ;
12
+ pageContext ?: {
13
+ title ?: string ;
14
+ description ?: string ;
15
+ content ?: string ;
16
+ slug ?: string ;
17
+ } ;
18
+ provider ?: AIProvider ;
19
+ apiKey ?: string ;
20
+ }
39
21
22
+ export async function POST ( req : Request ) {
40
23
try {
41
- // Build system message with page context
42
- let systemMessage =
43
- system ||
44
- `You are a helpful AI assistant for a documentation website.
45
- You can help users understand the documentation, answer questions about the content,
46
- and provide guidance on the topics covered in the docs. Be concise and helpful.` ;
24
+ const {
25
+ messages,
26
+ system,
27
+ pageContext,
28
+ provider = "intern" , // 默认使用书生模型
29
+ apiKey,
30
+ } : ChatRequest = await req . json ( ) ;
47
31
48
- // Add current page context if available
49
- if ( pageContext ?. content ) {
50
- systemMessage += `\n\n--- CURRENT PAGE CONTEXT ---\n` ;
51
- if ( pageContext . title ) {
52
- systemMessage += `Page Title: ${ pageContext . title } \n` ;
53
- }
54
- if ( pageContext . description ) {
55
- systemMessage += `Page Description: ${ pageContext . description } \n` ;
56
- }
57
- if ( pageContext . slug ) {
58
- systemMessage += `Page URL: /docs/${ pageContext . slug } \n` ;
59
- }
60
- systemMessage += `Page Content:\n${ pageContext . content } ` ;
61
- systemMessage += `\n--- END OF CONTEXT ---\n\nWhen users ask about "this page", "current page", or refer to the content they're reading, use the above context to provide accurate answers. You can summarize, explain, or answer specific questions about the current page content.` ;
32
+ // 对指定Provider验证key是否存在
33
+ if ( requiresApiKey ( provider ) && ( ! apiKey || apiKey . trim ( ) === "" ) ) {
34
+ return Response . json (
35
+ {
36
+ error :
37
+ "API key is required. Please configure your API key in the settings." ,
38
+ } ,
39
+ { status : 400 } ,
40
+ ) ;
62
41
}
63
42
64
- // Select model based on provider
65
- let model ;
66
- if ( provider === "gemini" ) {
67
- const customGoogle = createGoogleGenerativeAI ( {
68
- apiKey : apiKey ,
69
- } ) ;
70
- model = customGoogle ( "models/gemini-2.0-flash" ) ;
71
- } else {
72
- // Default to OpenAI
73
- const customOpenAI = createOpenAI ( {
74
- apiKey : apiKey ,
75
- } ) ;
76
- model = customOpenAI ( "gpt-4.1-nano" ) ;
77
- }
43
+ // 构建系统消息,包含页面上下文
44
+ const systemMessage = buildSystemMessage ( system , pageContext ) ;
45
+
46
+ // 根据Provider获取 AI 模型实例
47
+ const model = getModel ( provider , apiKey ) ;
78
48
49
+ // 生成流式响应
79
50
const result = streamText ( {
80
51
model : model ,
81
52
system : systemMessage ,
@@ -85,6 +56,12 @@ export async function POST(req: Request) {
85
56
return result . toUIMessageStreamResponse ( ) ;
86
57
} catch ( error ) {
87
58
console . error ( "Chat API error:" , error ) ;
59
+
60
+ // 处理特定模型创建错误
61
+ if ( error instanceof Error && error . message . includes ( "API key" ) ) {
62
+ return Response . json ( { error : error . message } , { status : 400 } ) ;
63
+ }
64
+
88
65
return Response . json (
89
66
{ error : "Failed to process chat request" } ,
90
67
{ status : 500 } ,
0 commit comments