Skip to content

Commit

Permalink
Feat frontend api refactor (#24)
Browse files Browse the repository at this point in the history
#9 
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
	- Introduced a `LoginPage` component for user authentication.
- Added a multi-step registration component with enhanced user
experience.
- Implemented a new `AuthProvider` for managing authentication state
across the application.
- Added `RootProvider` to wrap components with necessary context
providers.
	- Enhanced `ChatBottombar` with voice input functionality.
	- Introduced `useModels` hook for fetching model tags with caching.
	- Added `LocalStore` enum for consistent local storage references.

- **Bug Fixes**
- Improved error handling in various components and services for better
user feedback.

- **Documentation**
- Updated README and other documentation files to reflect recent
changes.

- **Style**
- Modified CSS variables for improved UI consistency and updated
scrollbar styles.

- **Tests**
- Adjusted project API tests to maintain functionality after recent
changes.

- **Chores**
- Updated dependencies and configuration files for better performance
and compatibility.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
Sma1lboy and autofix-ci[bot] authored Oct 29, 2024
1 parent e39fa99 commit e17404b
Show file tree
Hide file tree
Showing 60 changed files with 15,825 additions and 11,325 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ module.exports = {
'no-console': 'warn',
'prefer-const': 'error',
'no-var': 'error',
'@typescript-eslint/no-explicit-any': 'warn',
},
};
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ dist/

# Models
models/
*/**/models/
*/**/models/

*/**/database.sqlite
./backend/src/database.sqlite
4 changes: 4 additions & 0 deletions .vscode/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.enablePromptUseWorkspaceTsdk": true
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["upsert"]
}
33 changes: 33 additions & 0 deletions backend/interceptor/LoggingInterceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
Logger,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { GqlExecutionContext } from '@nestjs/graphql';

@Injectable()
export class LoggingInterceptor implements NestInterceptor {
private readonly logger = new Logger('GraphQLRequest');

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const ctx = GqlExecutionContext.create(context);
const { operation, fieldName } = ctx.getInfo();
let variables = '';
try {
variables = ctx.getContext().req.body.variables;
} catch (error) {

Check warning on line 21 in backend/interceptor/LoggingInterceptor.ts

View workflow job for this annotation

GitHub Actions / autofix

'error' is defined but never used
variables = '';
}

this.logger.log(
`${operation.operation.toUpperCase()} \x1B[33m${fieldName}\x1B[39m${
variables ? ` Variables: ${JSON.stringify(variables)}` : ''
}`,
);

return next.handle();
}
}
3 changes: 3 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@
"class-validator": "^0.14.1",
"fs-extra": "^11.2.0",
"graphql": "^16.9.0",
"graphql-subscriptions": "^2.0.0",
"graphql-ws": "^5.16.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"sqlite3": "^5.1.7",
"subscriptions-transport-ws": "^0.11.0",
"typeorm": "^0.3.20"
},
"devDependencies": {
Expand Down
18 changes: 14 additions & 4 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,27 @@ import { RolesGuard } from './guard/roles.guard';
import { MenuGuard } from './guard/menu.guard';

Check warning on line 14 in backend/src/app.module.ts

View workflow job for this annotation

GitHub Actions / autofix

'MenuGuard' is defined but never used. Allowed unused vars must match /^_/u
import { User } from './user/user.model';
import { AppResolver } from './app.resolver';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { LoggingInterceptor } from 'interceptor/LoggingInterceptor';

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: join(process.cwd(), 'src/schema.gql'),
autoSchemaFile: join(process.cwd(), '../frontend/src/graphql/schema.gql'),
sortSchema: true,
playground: true,
installSubscriptionHandlers: true,
subscriptions: {
'graphql-ws': true,
'subscriptions-transport-ws': true,
},
context: ({ req, res }) => ({ req, res }),
}),
TypeOrmModule.forRoot({
type: 'sqlite',
database: join(process.cwd(), 'src/database.sqlite'),
synchronize: true,
logging: true,
entities: [__dirname + '/**/*.model{.ts,.js}'],
}),
InitModule,
Expand All @@ -41,6 +45,12 @@ import { AppResolver } from './app.resolver';
ChatModule,
TypeOrmModule.forFeature([User]),
],
providers: [AppResolver],
providers: [
AppResolver,
{
provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor,
},
],
})
export class AppModule {}
8 changes: 4 additions & 4 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class AuthService {

async login(
loginUserInput: LoginUserInput,
): Promise<{ access_token: string }> {
): Promise<{ accessToken: string }> {
const { username, password } = loginUserInput;

const user = await this.userRepository.findOne({
Expand All @@ -73,10 +73,10 @@ export class AuthService {
}

const payload = { userId: user.id, username: user.username };
const access_token = this.jwtService.sign(payload);
this.jwtCacheService.storeToken(access_token);
const accessToken = this.jwtService.sign(payload);
this.jwtCacheService.storeToken(accessToken);

return { access_token };
return { accessToken };
}

async validateToken(params: CheckTokenInput): Promise<boolean> {
Expand Down
38 changes: 27 additions & 11 deletions backend/src/chat/chat.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ import { Message } from 'src/chat/message.model';
import { SystemBaseModel } from 'src/system-base-model/system-base.model';
import { User } from 'src/user/user.model';

export enum StreamStatus {
STREAMING = 'streaming',
DONE = 'done',
}

registerEnumType(StreamStatus, {
name: 'StreamStatus',
});

@Entity()
@ObjectType()
export class Chat extends SystemBaseModel {
Expand All @@ -44,6 +53,18 @@ class ChatCompletionDelta {
content?: string;
}

@ObjectType('ChatCompletionChoiceType')
class ChatCompletionChoice {
@Field()
index: number;

@Field(() => ChatCompletionDelta)
delta: ChatCompletionDelta;

@Field({ nullable: true })
finishReason: string | null;
}

@ObjectType('ChatCompletionChunkType')
export class ChatCompletionChunk {
@Field()
Expand All @@ -59,20 +80,15 @@ export class ChatCompletionChunk {
model: string;

@Field({ nullable: true })
system_fingerprint: string | null;
systemFingerprint: string | null;

@Field(() => [ChatCompletionChoice])
choices: ChatCompletionChoice[];
}

@ObjectType('ChatCompletionChoiceType')
class ChatCompletionChoice {
@Field()
index: number;

@Field(() => ChatCompletionDelta)
delta: ChatCompletionDelta;
@Field(() => StreamStatus)
status: StreamStatus;
}

@Field({ nullable: true })
finish_reason: string | null;
export function isDoneStatus(chunk: ChatCompletionChunk): boolean {
return chunk.status === StreamStatus.DONE;
}
5 changes: 5 additions & 0 deletions backend/src/chat/chat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Message } from 'src/chat/message.model';
import { ChatGuard } from '../guard/chat.guard';
import { AuthModule } from '../auth/auth.module';
import { UserService } from 'src/user/user.service';
import { PubSub } from 'graphql-subscriptions';

@Module({
imports: [
Expand All @@ -24,6 +25,10 @@ import { UserService } from 'src/user/user.service';
ChatService,
ChatGuard,
UserService,
{
provide: 'PUB_SUB',
useValue: new PubSub(),
},
],
exports: [ChatService, ChatGuard],
})
Expand Down
Loading

0 comments on commit e17404b

Please sign in to comment.