-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
76 changed files
with
3,427 additions
and
92 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,30 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { type MiddlewareConsumer, Module } from '@nestjs/common'; | ||
import graphqlUploadExpress from 'graphql-upload/graphqlUploadExpress.js'; | ||
import { CacheModule } from '#api/common/service/cache/cache.module'; | ||
import { EnvModule } from '#api/common/service/env/env.module'; | ||
import { PubSubModule } from '#api/common/service/pubsub/pubsub.module'; | ||
import { GraphQLConfigModule } from '#api/config/graphql/graphql-config.module'; | ||
import { IdentificationNnModule } from '#api/infra/identification-nn/identification-nn.module'; | ||
import { LangchainModule } from '#api/infra/langchain/langchain.module'; | ||
import { PrismaModule } from '#api/infra/prisma/prisma.module'; | ||
import { SupabaseModule } from '#api/infra/supabase/supabase.module'; | ||
import { Modules } from '#api/module'; | ||
|
||
@Module({ | ||
imports: [EnvModule, GraphQLConfigModule, PrismaModule, CacheModule, PubSubModule, ...Modules], | ||
imports: [ | ||
EnvModule, | ||
GraphQLConfigModule, | ||
PrismaModule, | ||
LangchainModule, | ||
SupabaseModule, | ||
IdentificationNnModule, | ||
CacheModule, | ||
PubSubModule, | ||
...Modules, | ||
], | ||
}) | ||
export class AppModule {} | ||
export class AppModule { | ||
configure(consumer: MiddlewareConsumer) { | ||
consumer.apply(graphqlUploadExpress()).forRoutes('graphql'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { type CanActivate, type ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { EnvService } from '#api/common/service/env/env.service'; | ||
import { SupabaseService } from '#api/infra/supabase/supabase.service'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor( | ||
private readonly supabaseService: SupabaseService, | ||
private readonly envService: EnvService, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
if (this.envService.NodeEnv === 'development') { | ||
return true; | ||
} | ||
let accessToken: string | undefined; | ||
context.getArgs().forEach((arg) => { | ||
if (arg && arg.req && arg.req.headers) { | ||
if (arg.req.headers.authorization) { | ||
[, accessToken] = arg.req.headers.authorization.split(' '); | ||
} | ||
} | ||
}); | ||
if (!accessToken) { | ||
return false; | ||
} | ||
const user = await this.supabaseService.getUserByAccessToken(accessToken); | ||
if (!user) { | ||
throw new Error(`Invalid access token: ${accessToken}`); | ||
} | ||
return !!user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
apps/api/src/infra/identification-nn/identification-nn.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { IdentificationNnService } from './identification-nn.service'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [IdentificationNnService], | ||
exports: [IdentificationNnService], | ||
}) | ||
export class IdentificationNnModule {} |
60 changes: 60 additions & 0 deletions
60
apps/api/src/infra/identification-nn/identification-nn.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Inject, Injectable, Logger } from '@nestjs/common'; | ||
import { EnvService } from '#api/common/service/env/env.service'; | ||
|
||
type SimilarItem = { | ||
key: string; | ||
similarity: number; | ||
dateDifference: number; | ||
}; | ||
|
||
@Injectable() | ||
export class IdentificationNnService { | ||
private readonly url: string; | ||
|
||
private readonly logger = new Logger(IdentificationNnService.name); | ||
|
||
constructor(@Inject(EnvService) private readonly envService: EnvService) { | ||
this.url = this.envService.IdentifyNnEndpoint; | ||
|
||
this.logger.debug(`${IdentificationNnService.name} constructed`); | ||
} | ||
|
||
async identify(similarItems: SimilarItem[]): Promise<string | null> { | ||
type Request = { | ||
similarity: number; | ||
date_difference: number; | ||
}; | ||
|
||
type Response = { | ||
// NOTE: The first element of the tuple represents the probability of a match and the second element represents the probability of a mismatch. | ||
data: [number, number]; | ||
error: string; | ||
}; | ||
|
||
const identities = await Promise.all( | ||
similarItems.map(async (similarItem): Promise<[string, [number, number]]> => { | ||
const response = await fetch(this.url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
similarity: similarItem.similarity, | ||
date_difference: similarItem.dateDifference, | ||
} satisfies Request), | ||
}); | ||
|
||
const { data: identity, error } = (await response.json()) as Response; | ||
if (error) { | ||
throw new Error(String(error)); | ||
} | ||
|
||
return [similarItem.key, identity]; | ||
}), | ||
); | ||
|
||
const mostIdenticalKey = identities.reduce((prev, current) => (current[1][0] > prev[1][0] ? current : prev))[0]; | ||
|
||
return mostIdenticalKey; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { LangchainService } from './langchain.service'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [LangchainService], | ||
exports: [LangchainService], | ||
}) | ||
export class LangchainModule {} |
Oops, something went wrong.