diff --git a/package.json b/package.json index 6a532a7..00698d3 100644 --- a/package.json +++ b/package.json @@ -1,86 +1,87 @@ -{ - "name": "clipperia-api", - "version": "0.0.1", - "description": "", - "author": "Leonardo Mortari", - "private": true, - "license": "UNLICENSED", - "scripts": { - "build": "nest build", - "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", - "start": "nest start", - "start:dev": "cross-env NODE_ENV=development nest start --watch", - "start:debug": "cross-env NODE_ENV=development nest start --debug --watch", - "start:prod": "node dist/main", - "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", - "test": "jest", - "test:watch": "jest --watch", - "test:cov": "jest --coverage", - "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json" - }, - "dependencies": { - "@nestjs/common": "11.0.1", - "@nestjs/config": "4.0.2", - "@nestjs/core": "11.0.1", - "@nestjs/passport": "11.0.5", - "@nestjs/platform-express": "11.0.1", - "@prisma/client": "6.14.0", - "axios": "1.12.0", - "bcrypt": "6.0.0", - "class-transformer": "0.5.1", - "class-validator": "0.14.2", - "cross-env": "10.0.0", - "dayjs": "1.11.13", - "jwks-rsa": "3.2.0", - "passport": "0.7.0", - "passport-jwt": "4.0.1", - "reflect-metadata": "0.2.2", - "rxjs": "7.8.1" - }, - "devDependencies": { - "@eslint/eslintrc": "3.2.0", - "@eslint/js": "9.18.0", - "@nestjs/cli": "11.0.0", - "@nestjs/schematics": "11.0.0", - "@nestjs/testing": "11.0.1", - "@types/bcrypt": "6.0.0", - "@types/express": "5.0.0", - "@types/jest": "30.0.0", - "@types/node": "22.10.7", - "@types/passport-jwt": "4.0.1", - "@types/supertest": "6.0.2", - "eslint": "9.18.0", - "eslint-config-prettier": "10.0.1", - "eslint-plugin-prettier": "5.2.2", - "globals": "16.0.0", - "jest": "30.0.0", - "prettier": "3.4.2", - "prisma": "6.14.0", - "source-map-support": "0.5.21", - "supertest": "7.0.0", - "ts-jest": "29.2.5", - "ts-loader": "9.5.2", - "ts-node": "10.9.2", - "tsconfig-paths": "4.2.0", - "typescript": "5.7.3", - "typescript-eslint": "8.20.0" - }, - "jest": { - "moduleFileExtensions": [ - "js", - "json", - "ts" - ], - "rootDir": "src", - "testRegex": ".*\\.spec\\.ts$", - "transform": { - "^.+\\.(t|j)s$": "ts-jest" - }, - "collectCoverageFrom": [ - "**/*.(t|j)s" - ], - "coverageDirectory": "../coverage", - "testEnvironment": "node" - } -} +{ + "name": "clipperia-api", + "version": "0.0.1", + "description": "", + "author": "Leonardo Mortari", + "private": true, + "license": "UNLICENSED", + "scripts": { + "build": "nest build", + "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", + "start": "nest start", + "start:dev": "cross-env NODE_ENV=development nest start --watch", + "start:debug": "cross-env NODE_ENV=development nest start --debug --watch", + "start:prod": "node dist/main", + "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", + "test": "jest", + "test:watch": "jest --watch", + "test:cov": "jest --coverage", + "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", + "test:e2e": "jest --config ./test/jest-e2e.json" + }, + "dependencies": { + "@nestjs/common": "11.0.1", + "@nestjs/config": "4.0.2", + "@nestjs/core": "11.0.1", + "@nestjs/passport": "11.0.5", + "@nestjs/platform-express": "11.0.1", + "@prisma/client": "6.14.0", + "axios": "1.12.0", + "bcrypt": "6.0.0", + "class-transformer": "0.5.1", + "class-validator": "0.14.2", + "cross-env": "10.0.0", + "dayjs": "1.11.13", + "jwks-rsa": "3.2.0", + "ollama": "0.6.0", + "passport": "0.7.0", + "passport-jwt": "4.0.1", + "reflect-metadata": "0.2.2", + "rxjs": "7.8.1" + }, + "devDependencies": { + "@eslint/eslintrc": "3.2.0", + "@eslint/js": "9.18.0", + "@nestjs/cli": "11.0.0", + "@nestjs/schematics": "11.0.0", + "@nestjs/testing": "11.0.1", + "@types/bcrypt": "6.0.0", + "@types/express": "5.0.0", + "@types/jest": "30.0.0", + "@types/node": "22.10.7", + "@types/passport-jwt": "4.0.1", + "@types/supertest": "6.0.2", + "eslint": "9.18.0", + "eslint-config-prettier": "10.0.1", + "eslint-plugin-prettier": "5.2.2", + "globals": "16.0.0", + "jest": "30.0.0", + "prettier": "3.4.2", + "prisma": "6.14.0", + "source-map-support": "0.5.21", + "supertest": "7.0.0", + "ts-jest": "29.2.5", + "ts-loader": "9.5.2", + "ts-node": "10.9.2", + "tsconfig-paths": "4.2.0", + "typescript": "5.7.3", + "typescript-eslint": "8.20.0" + }, + "jest": { + "moduleFileExtensions": [ + "js", + "json", + "ts" + ], + "rootDir": "src", + "testRegex": ".*\\.spec\\.ts$", + "transform": { + "^.+\\.(t|j)s$": "ts-jest" + }, + "collectCoverageFrom": [ + "**/*.(t|j)s" + ], + "coverageDirectory": "../coverage", + "testEnvironment": "node" + } +} diff --git a/src/app.module.ts b/src/app.module.ts index 04b2093..de814c9 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -3,12 +3,13 @@ import { ConfigModule } from '@nestjs/config'; import { PrismaModule } from './prisma/prisma.module'; import { AppController } from './app.controller'; import { AppService } from './app.service'; -import { VideosModule } from './videos/videos.module'; -import { AuthModule } from './auth/auth.module'; -import { VideosController } from './videos/videos.controller'; -import { UsuariosModule } from './usuarios/usuarios.module'; +import { VideosModule } from './modules/videos/videos.module'; +import { AuthModule } from './modules/auth/auth.module'; +import { VideosController } from './modules/videos/videos.controller'; +import { UsuariosModule } from './modules/usuarios/usuarios.module'; import { LoggerMiddleware } from './middleware/logger.middleware'; -import { RolesGuard } from './auth/roles.guard'; +import { RolesGuard } from './modules/auth/roles.guard'; +import { OllamaModule } from './modules/ollama/ollama.module'; @Module({ imports: [ @@ -17,6 +18,7 @@ import { RolesGuard } from './auth/roles.guard'; VideosModule, AuthModule, UsuariosModule, + OllamaModule, ], controllers: [AppController], providers: [AppService, RolesGuard], diff --git a/src/auth/auth.controller.spec.ts b/src/modules/auth/auth.controller.spec.ts similarity index 100% rename from src/auth/auth.controller.spec.ts rename to src/modules/auth/auth.controller.spec.ts diff --git a/src/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts similarity index 100% rename from src/auth/auth.controller.ts rename to src/modules/auth/auth.controller.ts diff --git a/src/auth/auth.module.ts b/src/modules/auth/auth.module.ts similarity index 100% rename from src/auth/auth.module.ts rename to src/modules/auth/auth.module.ts diff --git a/src/auth/auth.service.spec.ts b/src/modules/auth/auth.service.spec.ts similarity index 100% rename from src/auth/auth.service.spec.ts rename to src/modules/auth/auth.service.spec.ts diff --git a/src/auth/auth.service.ts b/src/modules/auth/auth.service.ts similarity index 100% rename from src/auth/auth.service.ts rename to src/modules/auth/auth.service.ts diff --git a/src/auth/decorator/roles.decorator.ts b/src/modules/auth/decorator/roles.decorator.ts similarity index 100% rename from src/auth/decorator/roles.decorator.ts rename to src/modules/auth/decorator/roles.decorator.ts diff --git a/src/auth/dto/login.dto.ts b/src/modules/auth/dto/login.dto.ts similarity index 100% rename from src/auth/dto/login.dto.ts rename to src/modules/auth/dto/login.dto.ts diff --git a/src/auth/dto/loginResponse.dto.ts b/src/modules/auth/dto/loginResponse.dto.ts similarity index 100% rename from src/auth/dto/loginResponse.dto.ts rename to src/modules/auth/dto/loginResponse.dto.ts diff --git a/src/auth/keycloak-auth.guard.ts b/src/modules/auth/keycloak-auth.guard.ts similarity index 100% rename from src/auth/keycloak-auth.guard.ts rename to src/modules/auth/keycloak-auth.guard.ts diff --git a/src/auth/keycloak.strategy.ts b/src/modules/auth/keycloak.strategy.ts similarity index 100% rename from src/auth/keycloak.strategy.ts rename to src/modules/auth/keycloak.strategy.ts diff --git a/src/auth/roles.guard.ts b/src/modules/auth/roles.guard.ts similarity index 100% rename from src/auth/roles.guard.ts rename to src/modules/auth/roles.guard.ts diff --git a/src/modules/ollama/dto/chat.dto.ts b/src/modules/ollama/dto/chat.dto.ts new file mode 100644 index 0000000..6212385 --- /dev/null +++ b/src/modules/ollama/dto/chat.dto.ts @@ -0,0 +1,10 @@ +import { IsNotEmpty, IsString } from 'class-validator'; + +export class ChatDto { + @IsString() + @IsNotEmpty() + message: string; + + @IsString() + model: string; +} diff --git a/src/modules/ollama/ollama.controller.spec.ts b/src/modules/ollama/ollama.controller.spec.ts new file mode 100644 index 0000000..b0c2ecc --- /dev/null +++ b/src/modules/ollama/ollama.controller.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { OllamaController } from './ollama.controller'; + +describe('OllamaController', () => { + let controller: OllamaController; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [OllamaController], + }).compile(); + + controller = module.get(OllamaController); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); +}); diff --git a/src/modules/ollama/ollama.controller.ts b/src/modules/ollama/ollama.controller.ts new file mode 100644 index 0000000..81c829c --- /dev/null +++ b/src/modules/ollama/ollama.controller.ts @@ -0,0 +1,44 @@ +import { Body, Controller, Get, Post, Query } from '@nestjs/common'; +import { OllamaService } from './ollama.service'; +import { ChatDto } from './dto/chat.dto'; + +@Controller('ollama') +export class OllamaController { + constructor(private readonly ollamaService: OllamaService) {} + + @Get('models') + public async getModels( + @Query('onlyChats') { onlyChats }: { onlyChats: boolean }, + ) { + return await this.ollamaService.getModels({ onlyChats }); + } + + @Post('chat') + public async chat(@Body() body: ChatDto) { + const { message, model } = body; + + if (!message) { + throw new Error('O atributo message é obrigatório'); + } + + if (!model) { + const [defaultModel] = await this.ollamaService.getModels({ + onlyChats: true, + }); + + if (!defaultModel) { + throw new Error('Nenhum modelo encontrado'); + } + + return await this.ollamaService.generateChat({ + model: defaultModel.name, + message, + }); + } + + return await this.ollamaService.generateChat({ + model, + message, + }); + } +} diff --git a/src/modules/ollama/ollama.module.ts b/src/modules/ollama/ollama.module.ts new file mode 100644 index 0000000..d1b9b61 --- /dev/null +++ b/src/modules/ollama/ollama.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { OllamaController } from './ollama.controller'; +import { OllamaService } from './ollama.service'; + +@Module({ + controllers: [OllamaController], + providers: [OllamaService], +}) +export class OllamaModule {} diff --git a/src/modules/ollama/ollama.service.spec.ts b/src/modules/ollama/ollama.service.spec.ts new file mode 100644 index 0000000..e8b0746 --- /dev/null +++ b/src/modules/ollama/ollama.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { OllamaService } from './ollama.service'; + +describe('OllamaService', () => { + let service: OllamaService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [OllamaService], + }).compile(); + + service = module.get(OllamaService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/modules/ollama/ollama.service.ts b/src/modules/ollama/ollama.service.ts new file mode 100644 index 0000000..48e6e8d --- /dev/null +++ b/src/modules/ollama/ollama.service.ts @@ -0,0 +1,55 @@ +import { Ollama } from 'ollama'; +import { Injectable } from '@nestjs/common'; +import type { ListResponse, ModelResponse } from 'ollama'; + +@Injectable() +export class OllamaService { + private readonly ollama: Ollama; + + constructor() { + this.ollama = new Ollama({ host: 'http://154.12.229.181:11434' }); + } + + public async getModels({ onlyChats }: { onlyChats: boolean }) { + try { + const modelsData = await this.ollama.list(); + + if (onlyChats) { + return this.getChatModels(modelsData); + } + + return modelsData.models; + } catch (error) { + throw new Error(error as string); + } + } + + public getChatModels(modelsData: ListResponse): ModelResponse[] { + const excludeFamilies = ['nomic-bert', 'embed', 'embedding']; + + return modelsData.models.filter((model) => { + const families = model.details?.families || []; + + return !families.some((f) => excludeFamilies.includes(f.toLowerCase())); + }); + } + + public async generateChat({ + model, + message, + }: { + model: string; + message: string; + }) { + try { + const response = await this.ollama.chat({ + model, + messages: [{ role: 'user', content: message }], + }); + + return response.message.content; + } catch (error) { + throw new Error(error as string); + } + } +} diff --git a/src/usuarios/dto/create-usuario-dto.ts b/src/modules/usuarios/dto/create-usuario-dto.ts similarity index 100% rename from src/usuarios/dto/create-usuario-dto.ts rename to src/modules/usuarios/dto/create-usuario-dto.ts diff --git a/src/usuarios/dto/usuarios.response.ts b/src/modules/usuarios/dto/usuarios.response.ts similarity index 100% rename from src/usuarios/dto/usuarios.response.ts rename to src/modules/usuarios/dto/usuarios.response.ts diff --git a/src/usuarios/usuarios.controller.ts b/src/modules/usuarios/usuarios.controller.ts similarity index 100% rename from src/usuarios/usuarios.controller.ts rename to src/modules/usuarios/usuarios.controller.ts diff --git a/src/usuarios/usuarios.module.ts b/src/modules/usuarios/usuarios.module.ts similarity index 84% rename from src/usuarios/usuarios.module.ts rename to src/modules/usuarios/usuarios.module.ts index 66df050..f6f2f0d 100644 --- a/src/usuarios/usuarios.module.ts +++ b/src/modules/usuarios/usuarios.module.ts @@ -1,7 +1,7 @@ import { Module } from '@nestjs/common'; import { UsuariosService } from './usuarios.service'; import { UsuariosController } from './usuarios.controller'; -import { PrismaModule } from '../prisma/prisma.module'; +import { PrismaModule } from '@prisma/prisma.module'; @Module({ imports: [PrismaModule], diff --git a/src/usuarios/usuarios.service.ts b/src/modules/usuarios/usuarios.service.ts similarity index 96% rename from src/usuarios/usuarios.service.ts rename to src/modules/usuarios/usuarios.service.ts index 0277295..f67cd90 100644 --- a/src/usuarios/usuarios.service.ts +++ b/src/modules/usuarios/usuarios.service.ts @@ -4,8 +4,8 @@ import { plainToInstance } from 'class-transformer'; import { Prisma, Usuario } from 'generated/prisma'; -import { PrismaService } from '../prisma/prisma.service'; -import { PaginatedResponse } from '../shared/dto/paginated'; +import { PrismaService } from '@prisma/prisma.service'; +import { PaginatedResponse } from '@shared/dto/paginated'; import { UsuariosResponseDto } from './dto/usuarios.response'; import { CreateUsuarioDto } from './dto/create-usuario-dto'; @@ -24,7 +24,7 @@ const SELECT = { bloqueado_ate: true, criado_em: true, atualizado_em: true, -}; +} as const; @Injectable() export class UsuariosService { diff --git a/src/videos/dto/create-video-dto.ts b/src/modules/videos/dto/create-video-dto.ts similarity index 93% rename from src/videos/dto/create-video-dto.ts rename to src/modules/videos/dto/create-video-dto.ts index eb453fa..befd483 100644 --- a/src/videos/dto/create-video-dto.ts +++ b/src/modules/videos/dto/create-video-dto.ts @@ -7,7 +7,7 @@ import { IsUrl, MaxLength, } from 'class-validator'; -import { video_situation } from '../../../generated/prisma'; +import { video_situation } from '@root/generated/prisma'; export class CreateVideoDto { @IsUrl() diff --git a/src/videos/dto/list-videos-query.dto.ts b/src/modules/videos/dto/list-videos-query.dto.ts similarity index 100% rename from src/videos/dto/list-videos-query.dto.ts rename to src/modules/videos/dto/list-videos-query.dto.ts diff --git a/src/videos/dto/video-response.dto.ts b/src/modules/videos/dto/video-response.dto.ts similarity index 100% rename from src/videos/dto/video-response.dto.ts rename to src/modules/videos/dto/video-response.dto.ts diff --git a/src/videos/videos.controller.ts b/src/modules/videos/videos.controller.ts similarity index 100% rename from src/videos/videos.controller.ts rename to src/modules/videos/videos.controller.ts diff --git a/src/videos/videos.module.ts b/src/modules/videos/videos.module.ts similarity index 70% rename from src/videos/videos.module.ts rename to src/modules/videos/videos.module.ts index f1ecb87..dc2d14e 100644 --- a/src/videos/videos.module.ts +++ b/src/modules/videos/videos.module.ts @@ -1,8 +1,8 @@ import { Module } from '@nestjs/common'; import { VideosService } from './videos.service'; import { VideosController } from './videos.controller'; -import { PrismaModule } from '../prisma/prisma.module'; -import { KeycloakAuthGuard } from '../auth/keycloak-auth.guard'; +import { PrismaModule } from '@prisma/prisma.module'; +import { KeycloakAuthGuard } from '@modules/auth/keycloak-auth.guard'; @Module({ imports: [PrismaModule], diff --git a/src/videos/videos.service.ts b/src/modules/videos/videos.service.ts similarity index 93% rename from src/videos/videos.service.ts rename to src/modules/videos/videos.service.ts index d9aaee9..346a6e1 100644 --- a/src/videos/videos.service.ts +++ b/src/modules/videos/videos.service.ts @@ -4,11 +4,11 @@ import { plainToInstance } from 'class-transformer'; import { Prisma, videos, video_situation } from 'generated/prisma'; -import { PrismaService } from '../prisma/prisma.service'; +import { PrismaService } from '@prisma/prisma.service'; import { VideoResponseDto } from './dto/video-response.dto'; -import { PaginatedResponse } from '../shared/dto/paginated'; +import { PaginatedResponse } from '@shared/dto/paginated'; import { ListVideosQueryDto } from './dto/list-videos-query.dto'; -import { VideoMetadataDto } from 'src/shared/dto/video-metadata'; +import { VideoMetadataDto } from '@shared/dto/video-metadata'; @Injectable() export class VideosService { @@ -115,7 +115,6 @@ export class VideosService { } async getVideoMetadata(url: string): Promise { - console.log(url); try { const { data } = await axios.get( `${process.env.YOUTUBE_API_URL}/get-video-metadata`, @@ -129,8 +128,7 @@ export class VideosService { return plainToInstance(VideoMetadataDto, data, { excludeExtraneousValues: true, }); - } catch (error) { - console.log(error); + } catch { throw new Error('Erro ao obter metadados do vídeo'); } } diff --git a/tsconfig.json b/tsconfig.json index 89eb88f..8ca83cc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -20,6 +20,13 @@ "forceConsistentCasingInFileNames": true, "noImplicitAny": false, "strictBindCallApply": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, + "paths": { + "@/*": ["./src/*"], + "@modules/*": ["./src/modules/*"], + "@shared/*": ["./src/shared/*"], + "@prisma/*": ["./src/prisma/*"], + "@root/*": ["./*"] + } } }