diff --git a/backend/src/songs/songs.module.ts b/backend/src/songs/songs.module.ts index cf756a3..61bba10 100644 --- a/backend/src/songs/songs.module.ts +++ b/backend/src/songs/songs.module.ts @@ -9,7 +9,6 @@ import { GenresModel } from '../../models/genres/genres.model'; import { SongDetailsModel } from '../../models/song_details/SongDetails.model'; import { SongArtistsModel } from '../../models/song_artists/songArtists.model'; import { SongGenresModel } from '../../models/song_genres/SongGenres.model'; -import { PubSub } from 'graphql-subscriptions'; import { GoogleGenAI } from '@google/genai'; @Module({ @@ -24,10 +23,7 @@ import { GoogleGenAI } from '@google/genai'; SongGenresModel, ]), ], - providers: [SongsResolver, SongsService, { - provide: 'PUB_SUB', - useValue: new PubSub(), - }, + providers: [SongsResolver, SongsService, { provide: 'AI', useValue: new GoogleGenAI({ diff --git a/backend/src/songs/songs.resolver.spec.ts b/backend/src/songs/songs.resolver.spec.ts index a8d20a2..14a3aca 100644 --- a/backend/src/songs/songs.resolver.spec.ts +++ b/backend/src/songs/songs.resolver.spec.ts @@ -12,7 +12,6 @@ import { InternalServerErrorException } from '@nestjs/common'; import { ConnectionResError, TimeoutResError } from 'src/utils/mockErrors'; import { USER_VECTOR } from '../../test/constants/constants'; import { expectSongProps } from 'src/utils/expectSongs'; -import { PubSub } from 'graphql-subscriptions'; import { GoogleGenAI } from '@google/genai'; describe('SongsResolver receives the expected songs array from the service', () => { @@ -23,10 +22,6 @@ describe('SongsResolver receives the expected songs array from the service', () const module: TestingModule = await Test.createTestingModule({ providers: [ SongsResolver, - { - provide: 'PUB_SUB', - useValue: new PubSub(), - }, { provide: 'AI', useValue: new GoogleGenAI({ @@ -121,10 +116,6 @@ describe('SongsResolver is able to communicate the error from the service to hel const module: TestingModule = await Test.createTestingModule({ providers: [ SongsResolver, - { - provide: 'PUB_SUB', - useValue: new PubSub(), - }, { provide: 'AI', useValue: new GoogleGenAI({ diff --git a/backend/src/songs/songs.resolver.ts b/backend/src/songs/songs.resolver.ts index ff55adc..3d251f0 100644 --- a/backend/src/songs/songs.resolver.ts +++ b/backend/src/songs/songs.resolver.ts @@ -1,16 +1,12 @@ -import { Args, Float, Int, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql'; +import { Args, Float, Int, Query, Resolver, Subscription } from '@nestjs/graphql'; import { SongsService } from './songs.service'; import { USER_VECTOR } from '../../test/constants/constants'; import { FullSongResponseDto } from './dto/FullSongResponse.dto'; import { SongResponseDto } from './dto/SongResponse.dto'; -import { Inject } from '@nestjs/common'; -import { PubSub } from 'graphql-subscriptions'; @Resolver() export class SongsResolver { - constructor(private readonly songsService: SongsService, - @Inject('PUB_SUB') private readonly pubSub: PubSub - ) {} + constructor(private readonly songsService: SongsService, ) {} @Query(() => Int, { name: 'getDBLength' }) async getDBLength(): Promise { @@ -31,8 +27,8 @@ export class SongsResolver { return this.songsService.getSongData(songID); } - @Subscription(() => String, { resolve: (payload: { aiResponse: string | undefined }) => payload.aiResponse, }) - async *responseSub( + @Subscription(() => String) + async *aiResponse( @Args('genres', { type: () => [String], defaultValue: ["Rock"] } ) genres: string[], @Args('userVector', { type: () => [Float], defaultValue: USER_VECTOR }) userVector: number[], ) : AsyncGenerator<{ aiResponse: string | undefined }> { @@ -42,27 +38,6 @@ export class SongsResolver { } } - @Subscription(() => String) - aiResponse() { - return this.pubSub.asyncIterableIterator('AI_RESPONSE') - } - - @Mutation(() => Boolean) - async streamAIResponse( - @Args('genres', { type: () => [String], defaultValue: ['Rock'] }) genres: string[], - @Args('userVector', { type: () => [Float], defaultValue: USER_VECTOR }) userVector: number[], - ) { - const streamResponse = await this.songsService.getAIStream(genres, userVector); - console.log(`Got the stream response:`) - console.log(streamResponse) - for await (const chunk of streamResponse) { - console.log(`loading chunk: ${chunk.text}`) - await this.pubSub.publish('AI_RESPONSE', { aiResponse: chunk.text }); - console.log("response published!") - } - return true; - } - @Query(() => [SongResponseDto], { name: 'getSongRecommendations' }) async getSongRecommendations( @Args('genres', { type: () => [String], defaultValue: ['Rock'] }) diff --git a/backend/src/songs/songs.service.spec.ts b/backend/src/songs/songs.service.spec.ts index aa573bf..80e3705 100644 --- a/backend/src/songs/songs.service.spec.ts +++ b/backend/src/songs/songs.service.spec.ts @@ -46,10 +46,6 @@ describe('SongsService retrieves, evaluates and parses songs data from the datab const module: TestingModule = await Test.createTestingModule({ providers: [ SongsService, - { - provide: 'PUB_SUB', - useValue: new PubSub(), - }, { provide: 'AI', useValue: new GoogleGenAI({ diff --git a/frontend/src/components/Utils/hooks/useSongRec.tsx b/frontend/src/components/Utils/hooks/useSongRec.tsx index b8a72d6..d9f69be 100644 --- a/frontend/src/components/Utils/hooks/useSongRec.tsx +++ b/frontend/src/components/Utils/hooks/useSongRec.tsx @@ -1,8 +1,8 @@ import { useState } from "react"; import { useAppSelector } from "../redux-hooks"; -import { responseSub } from "@/queries/LaraRecQuerie"; import { useSubscription } from "@apollo/client"; import generateUserVector from "../generateUserVector"; +import { aiSubscription } from "@/queries/LaraRecQuerie"; const useSongRec = () => { const [visibleCount, setVisibleCount] = useState(20); @@ -11,12 +11,11 @@ const useSongRec = () => { mood, acousticness } = useAppSelector(state => state.songData); const userVector = generateUserVector(tempo, danceability, energy, mood, speechLevel, acousticness, voiceType, sentiment); - - useSubscription(responseSub, { + + useSubscription(aiSubscription, { variables: { genres, userVector }, onData({ data }) { - console.log(data) - const chunkText = data.data.responseSub; + const chunkText = data.data.aiResponse; setAIResponse(prev => prev + chunkText); } }); diff --git a/frontend/src/queries/LaraRecQuerie.ts b/frontend/src/queries/LaraRecQuerie.ts index fdfee05..3592173 100644 --- a/frontend/src/queries/LaraRecQuerie.ts +++ b/frontend/src/queries/LaraRecQuerie.ts @@ -13,19 +13,7 @@ query getRecommendedSongs ($genres: [String!], $userVector: [Float!], $limit: In `; export const aiSubscription = gql` -subscription { - aiResponse -} -`; - -export const responseSub = gql` subscription GetAIResponse($genres: [String!], $userVector: [Float!]) { - responseSub(genres: $genres, userVector: $userVector) -} -`; - -export const streamAIAnswer = gql` -mutation streamResponse($genres: [String!], $userVector: [Float!]) { - streamAIResponse(genres: $genres, userVector: $userVector) + aiResponse(genres: $genres, userVector: $userVector) } `; \ No newline at end of file