MCPcopy Create free account
hub / github.com/Seanium/ChatMap / POST

Function POST

app/api/stream/route.ts:17–168  ·  view source on GitHub ↗
(req: NextRequest)

Source from the content-addressed store, hash-verified

15}
16
17export async function POST(req: NextRequest) {
18 try {
19 // 解析请求体
20 const body = await req.json();
21 const conversationHistory: Message[] = body.conversationHistory || [];
22 let settings: ModelSettings | null = body.settings || null;
23
24 // 验证设置
25 if (!settings) {
26 // 尝试从请求头中获取旧版API密钥
27 const legacyApiKey = req.headers.get("x-api-key");
28 if (!legacyApiKey) {
29 return new Response(
30 JSON.stringify({ error: "缺少模型配置或API密钥" }),
31 {
32 status: 401,
33 headers: { "Content-Type": "application/json" }
34 }
35 );
36 }
37
38 // 使用旧版API密钥
39 settings = {
40 provider: "openai",
41 baseURL: "https://api.openai.com/v1",
42 model: "gpt-4o",
43 apiKey: legacyApiKey,
44 temperature: 0.7,
45 };
46 }
47
48 // 验证对话历史
49 if (!Array.isArray(conversationHistory) || conversationHistory.length === 0) {
50 return new Response(
51 JSON.stringify({ error: "缺少有效的对话历史" }),
52 {
53 status: 400,
54 headers: { "Content-Type": "application/json" }
55 }
56 );
57 }
58
59 // 根据提供商调整API路径
60 let baseURL = settings.baseURL;
61
62 // 确保baseURL不包含具体的API路径(如'/chat/completions')
63 if (settings.provider === "openai" && baseURL.includes("/chat/completions")) {
64 baseURL = baseURL.replace("/chat/completions", "");
65 } else if (settings.provider === "siliconflow" && baseURL.includes("/chat/completions")) {
66 baseURL = baseURL.replace("/chat/completions", "");
67 }
68
69 console.log(`[Stream API] 使用API端点: ${baseURL}, 模型: ${settings.model}, 提供商: ${settings.provider}`);
70
71 // 创建OpenAI客户端
72 const openai = new OpenAI({
73 apiKey: settings.apiKey,
74 baseURL: baseURL,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected