|
1 | | -import { NextResponse } from "next/server"; |
2 | | -import { authGuard } from "@/adapters/guards/auth.guard"; |
3 | | -import { streamCompletionInputDtoSchema } from "@/application/dto/llm/stream-completion.dto"; |
4 | | -import { getInjection } from "@/common/di/container"; |
| 1 | +import { streamCompletionController } from "@/adapters/controllers/llm/stream-completion.controller"; |
5 | 2 |
|
6 | 3 | export async function POST(request: Request) { |
7 | | - const guardResult = await authGuard(); |
8 | | - |
9 | | - if (!guardResult.authenticated) { |
10 | | - return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
11 | | - } |
12 | | - |
13 | | - const body = await request.json(); |
14 | | - const parseResult = streamCompletionInputDtoSchema.safeParse(body); |
15 | | - |
16 | | - if (!parseResult.success) { |
17 | | - return NextResponse.json( |
18 | | - { error: parseResult.error.issues[0]?.message ?? "Invalid input" }, |
19 | | - { status: 400 }, |
20 | | - ); |
21 | | - } |
22 | | - |
23 | | - const useCase = getInjection("StreamCompletionUseCase"); |
24 | | - const result = await useCase.execute({ |
25 | | - ...parseResult.data, |
26 | | - userId: guardResult.session.user.id, |
27 | | - }); |
28 | | - |
29 | | - if (result.isFailure) { |
30 | | - return NextResponse.json({ error: result.getError() }, { status: 400 }); |
31 | | - } |
32 | | - |
33 | | - const { stream, model, provider } = result.getValue(); |
34 | | - |
35 | | - return new Response(stream, { |
36 | | - headers: { |
37 | | - "Content-Type": "text/plain; charset=utf-8", |
38 | | - "Transfer-Encoding": "chunked", |
39 | | - "X-LLM-Model": model, |
40 | | - "X-LLM-Provider": provider, |
41 | | - }, |
42 | | - }); |
| 4 | + return streamCompletionController(request); |
43 | 5 | } |
0 commit comments