-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathroute.ts
More file actions
59 lines (51 loc) · 1.46 KB
/
route.ts
File metadata and controls
59 lines (51 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { createOpenAI } from "@ai-sdk/openai";
import { stepCountIs, streamText } from "ai";
import { systemPrompt } from "@/lib/ai/system-prompt";
import { clientTools } from "@/lib/ai/tools";
import { getMCPTools } from "@/lib/ai/mcp-client";
import { z } from "zod";
const llmRouter = createOpenAI({
baseURL: process.env.LLM_ROUTER_URL,
apiKey: "-",
headers: {
"x-request-katalog-id": process.env.LLM_ROUTER_KATALOG_ID ?? "",
"x-request-katalog-name": process.env.LLM_ROUTER_KATALOG_NAME ?? "",
},
name: "llm-router",
});
const chatRequestSchema = z.object({
messages: z
.array(
z
.object({
role: z.string().min(1),
})
.passthrough(),
)
.min(1),
});
export async function POST(req: Request) {
let body: unknown;
try {
body = await req.json();
} catch {
return Response.json({ error: "Invalid JSON" }, { status: 400 });
}
const parsed = chatRequestSchema.safeParse(body);
if (!parsed.success) {
return Response.json({ error: "Invalid request body" }, { status: 400 });
}
const messages = parsed.data.messages as Parameters<typeof streamText>[0]["messages"];
const mcpTools = await getMCPTools();
const result = streamText({
model: llmRouter(process.env.LLM_ROUTER_MODEL ?? "openai/gpt-4o"),
system: systemPrompt,
messages,
tools: {
...clientTools,
...mcpTools,
},
stopWhen: stepCountIs(5),
});
return result.toUIMessageStreamResponse();
}