Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@
"build:swc": "nest build -b swc"
},
"dependencies": {
"@ai-sdk/google": "^2.0.47",
"@ai-sdk/openai": "^2.0.88",
"@ai-sdk/openai-compatible": "^1.0.29",
"@ai-sdk/google": "^3.0.1",
"@ai-sdk/openai": "^3.0.1",
"@ai-sdk/openai-compatible": "^2.0.1",
"@fastify/helmet": "^13.0.2",
"@fastify/multipart": "^9.3.0",
"@fastify/static": "^8.3.0",
"@keyv/redis": "^5.1.5",
"@nestjs/bullmq": "^11.0.4",
"@nestjs/cache-manager": "^3.0.1",
"@nestjs/common": "^11.1.9",
"@nestjs/cache-manager": "^3.1.0",
"@nestjs/common": "^11.1.10",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.1.9",
"@nestjs/core": "^11.1.10",
"@nestjs/jwt": "^11.0.2",
"@nestjs/passport": "^11.0.5",
"@nestjs/platform-express": "^11.1.9",
"@nestjs/platform-fastify": "^11.1.9",
"@nestjs/platform-express": "^11.1.10",
"@nestjs/platform-fastify": "^11.1.10",
"@nestjs/swagger": "^11.2.3",
"@nestjs/terminus": "^11.0.0",
"@nestjs/throttler": "^6.5.0",
"@prisma/client": "^6.19.1",
"@toon-format/toon": "^2.1.0",
"@types/passport-jwt": "^4.0.1",
"ai": "^5.0.114",
"ai": "^6.0.3",
"bcrypt": "^6.0.0",
"bullmq": "^5.66.0",
"bullmq": "^5.66.4",
"cache-manager": "^7.2.7",
"cacheable": "^2.3.1",
"class-transformer": "^0.5.1",
Expand All @@ -72,17 +72,17 @@
"devDependencies": {
"@nestjs/cli": "^11.0.14",
"@nestjs/schematics": "^11.0.9",
"@nestjs/testing": "^11.1.9",
"@nestjs/testing": "^11.1.10",
"@swc/cli": "^0.7.9",
"@swc/core": "^1.15.5",
"@swc/core": "^1.15.7",
"@types/bcrypt": "^6.0.0",
"@types/express": "^5.0.6",
"@types/jest": "^30.0.0",
"@types/multer": "^2.0.0",
"@types/node": "^25.0.3",
"@types/supertest": "^6.0.3",
"@typescript-eslint/eslint-plugin": "^8.50.0",
"@typescript-eslint/parser": "^8.50.0",
"@typescript-eslint/eslint-plugin": "^8.50.1",
"@typescript-eslint/parser": "^8.50.1",
"eslint": "^9.39.2",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.4",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "combinations" ADD COLUMN "ai_description" TEXT;
2 changes: 1 addition & 1 deletion prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"
provider = "postgresql"
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ model Combination {
user User @relation(fields: [userId], references: [id])
isAIGenerated Boolean @default(false) @map("is_ai_generated")
occasions String[]
aiDescription String? @map("ai_description") @db.Text

items CombinationItem[]
savedCombinations SavedCombination[]
Expand Down
27 changes: 19 additions & 8 deletions src/modules/ai/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import {
Logger,
OnModuleInit,
} from '@nestjs/common';
import { generateObject, generateText, LanguageModel } from 'ai';
import { generateObject, generateText, LanguageModel, Output, Tool, ToolLoopAgent, ToolSet } from 'ai';
import { Schema } from 'zod';
import aiConfig from './config/ai.config';
import { ConfigType } from '@nestjs/config';
import { AIProviderEnum } from './enums/provider.enum';
import { createOllama } from 'ollama-ai-provider-v2';
// import { createOllama } from 'ollama-ai-provider-v2';
import { openai } from '@ai-sdk/openai';
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

Expand All @@ -30,12 +30,12 @@ export class AiService implements OnModuleInit {
this.logger.log('Usando Google Gemini', AiService.name);
this.textModelAI = google(this.envAI.textModel);
break;
case AIProviderEnum.OLLAMA:
this.logger.log('Usando Ollama', AiService.name);
this.textModelAI = createOllama({
baseURL: this.envAI.url,
}).languageModel(this.envAI.textModel);
break;
// case AIProviderEnum.OLLAMA:
// this.logger.log('Usando Ollama', AiService.name);
// this.textModelAI = createOllama({
// baseURL: this.envAI.url,
// }).languageModel(this.envAI.textModel);
// break;
case AIProviderEnum.OPENAI:
this.logger.log('Usando OpenAI', AiService.name);
this.textModelAI = openai(this.envAI.textModel);
Expand Down Expand Up @@ -82,4 +82,15 @@ export class AiService implements OnModuleInit {

return textGenerated;
}

async agent(prompt: string, outputSchema: Schema, systemPrompt?: string, tools?: ToolSet) {
const agent = new ToolLoopAgent({
model: this.textModelAI,
instructions: systemPrompt,
tools,
output: Output.object({schema: outputSchema}),
})

return (await agent.generate({prompt})).output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ describe('CombinationsController', () => {
page: 1,
};

const mockUser = {
id: 'user-123',
role: RoleEnum.USER,
};

it('should generate combinations successfully', async () => {
const expectedResult = {
message: 'Combinaciones generadas correctamente',
Expand All @@ -87,10 +92,10 @@ describe('CombinationsController', () => {
expectedResult,
);

const result = await controller.generateCombinations(createDto);
const result = await controller.generateCombinations(createDto, mockUser);

expect(result).toEqual(expectedResult);
expect(service.generateCombinations).toHaveBeenCalledWith(createDto);
expect(service.generateCombinations).toHaveBeenCalledWith(createDto, 'user-123');
expect(service.generateCombinations).toHaveBeenCalledTimes(1);
});

Expand All @@ -100,7 +105,7 @@ describe('CombinationsController', () => {
data: {},
});

await controller.generateCombinations(createDto);
await controller.generateCombinations(createDto, mockUser);

expect(service.generateCombinations).toHaveBeenCalledWith(
expect.objectContaining({
Expand All @@ -109,6 +114,7 @@ describe('CombinationsController', () => {
occasions: createDto.occasions,
description: createDto.description,
}),
mockUser.id,
);
});
});
Expand All @@ -119,9 +125,10 @@ describe('CombinationsController', () => {
description: 'A casual outfit',
occasions: ['Casual'],
isAIGenerated: true,
explanation: 'This is a great combination',
combinationItems: [
{ wardrobeItemId: 'item-1', explanation: 'Perfect shirt' },
{ wardrobeItemId: 'item-2', explanation: 'Nice pants' },
{ wardrobeItemId: 'item-1' },
{ wardrobeItemId: 'item-2' },
],
};

Expand Down Expand Up @@ -325,8 +332,8 @@ describe('CombinationsController', () => {
const addItemsDto: AddItemsToCombinationDto = {
combinationId: 'comb-1',
combinationItems: [
{ wardrobeItemId: 'item-1', explanation: 'Nice shirt' },
{ wardrobeItemId: 'item-2', explanation: 'Good pants' },
{ wardrobeItemId: 'item-1' },
{ wardrobeItemId: 'item-2' },
],
};

Expand Down Expand Up @@ -367,7 +374,7 @@ describe('CombinationsController', () => {
const singleItemDto = {
combinationId: 'comb-1',
combinationItems: [
{ wardrobeItemId: 'item-1', explanation: 'Nice shirt' },
{ wardrobeItemId: 'item-1' },
],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export class CombinationsController {
'Genera combinaciones de prendas a partir de una lista de prendas base y categorías seleccionadas. Estas combinaciones no se guardan automaticamente',
operationId: 'generateCombinations',
})
async generateCombinations(@Body() payload: CreateCombinationDto) {
return this.combinationsService.generateCombinations(payload);
async generateCombinations(@Body() payload: CreateCombinationDto, @CurrentSession() user: InfoUserInterface) {
return this.combinationsService.generateCombinations(payload, user.id);
}

@Post('save')
Expand Down
10 changes: 5 additions & 5 deletions src/modules/wardrobe/dtos/combinations.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ class CombinationItemDto {
@ApiProperty()
@IsString()
wardrobeItemId: string;

@ApiProperty()
@IsString()
@IsOptional()
explanation: string;
}

export class SaveCombinationDto extends PickType(CreateCombinationDto, [
Expand All @@ -82,6 +77,11 @@ export class SaveCombinationDto extends PickType(CreateCombinationDto, [
@IsArray()
@Type(() => CombinationItemDto)
combinationItems: CombinationItemDto[];

@ApiProperty()
@IsString()
@IsOptional()
explanation: string;
}

export class AddItemsToCombinationDto extends PickType(SaveCombinationDto, [
Expand Down
2 changes: 1 addition & 1 deletion src/modules/wardrobe/prompts/combinations.prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ ${encode(clothingItemsBase)}
${encode(clothingItems)}

# Categories:
${encode(categories)}$
${encode(categories)}

# Occasions:
${encode(occasions)}
Expand Down
Loading