(req: NextRequest)
| 15 | } |
| 16 | |
| 17 | export async function POST(req: NextRequest) { |
| 18 | let query = ""; |
| 19 | let settings: ModelSettings | null = null; |
| 20 | let conversationHistory: Message[] = []; |
| 21 | |
| 22 | try { |
| 23 | // 1. Parse request body and retrieve model settings |
| 24 | try { |
| 25 | const body = await req.json(); |
| 26 | query = body.query || ""; |
| 27 | conversationHistory = body.conversationHistory || []; |
| 28 | |
| 29 | if (body.settings) { |
| 30 | settings = body.settings as ModelSettings; |
| 31 | } else if (req.headers.get("x-api-key")) { |
| 32 | // Backward compatibility for old API key handling |
| 33 | settings = { |
| 34 | provider: "openai", |
| 35 | baseURL: "https://api.openai.com/v1", // Standard OpenAI base URL |
| 36 | model: "gpt-4o", |
| 37 | apiKey: req.headers.get("x-api-key") || "", |
| 38 | temperature: 0.7, |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | if (!settings || !settings.apiKey || !settings.baseURL || !settings.model) { |
| 43 | return NextResponse.json( |
| 44 | { |
| 45 | text: "模型配置不完整或无效。请检查您的 AI 模型设置(API密钥、BaseURL、模型名称)。", |
| 46 | locations: [], |
| 47 | error: true, |
| 48 | }, |
| 49 | { status: 401 }, // Unauthorized |
| 50 | ); |
| 51 | } |
| 52 | } catch (error) { |
| 53 | console.error("解析请求体时出错:", error); |
| 54 | return NextResponse.json( |
| 55 | { |
| 56 | text: "无效的请求体。请提供有效的查询。", |
| 57 | locations: [], |
| 58 | error: true, |
| 59 | }, |
| 60 | { status: 400 }, // Bad Request |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | if (!query) { |
| 65 | return NextResponse.json( |
| 66 | { |
| 67 | text: "缺少查询参数。请提供有效的查询。", |
| 68 | locations: [], |
| 69 | error: true, |
| 70 | }, |
| 71 | { status: 400 }, // Bad Request |
| 72 | ); |
| 73 | } |
| 74 |
nothing calls this directly
no outgoing calls
no test coverage detected