import { Controller, Post, Get, Body, Res, HttpCode, HttpStatus } from "@nestjs/common"; import { Response } from "express"; import { LlmService } from "./llm.service"; import { ChatRequestDto, ChatResponseDto, EmbedRequestDto, EmbedResponseDto } from "./dto"; import type { LlmProviderHealthStatus } from "./providers/llm-provider.interface"; @Controller("llm") export class LlmController { constructor(private readonly llmService: LlmService) {} @Get("health") async health(): Promise { return this.llmService.checkHealth(); } @Get("models") async listModels(): Promise<{ models: string[] }> { return { models: await this.llmService.listModels() }; } @Post("chat") @HttpCode(HttpStatus.OK) async chat( @Body() req: ChatRequestDto, @Res({ passthrough: true }) res: Response ): Promise { if (req.stream === true) { res.setHeader("Content-Type", "text/event-stream"); res.setHeader("Cache-Control", "no-cache"); res.setHeader("Connection", "keep-alive"); res.setHeader("X-Accel-Buffering", "no"); try { for await (const c of this.llmService.chatStream(req)) res.write("data: " + JSON.stringify(c) + "\n\n"); res.write("data: [DONE]\n\n"); res.end(); } catch (e: unknown) { res.write( "data: " + JSON.stringify({ error: e instanceof Error ? e.message : String(e) }) + "\n\n" ); res.end(); } return; } return this.llmService.chat(req); } @Post("embed") @HttpCode(HttpStatus.OK) async embed( @Body() req: EmbedRequestDto ): Promise { return this.llmService.embed(req); } }