-
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.
Merge pull request #299 from boostcampwm2023/release-1.0.0
- Loading branch information
Showing
188 changed files
with
9,489 additions
and
879 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AiController } from './ai.controller'; | ||
|
||
describe('AiController', () => { | ||
let controller: AiController; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AiController], | ||
}).compile(); | ||
|
||
controller = module.get<AiController>(AiController); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined(); | ||
}); | ||
}); |
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,19 @@ | ||
import { Body, Controller, Post } from '@nestjs/common'; | ||
import { AiRequestDto, AiResponseDto } from './dto/ai.dto'; | ||
import { AiService } from './ai.service'; | ||
import { ApiOperation, ApiResponse } from '@nestjs/swagger'; | ||
|
||
@Controller('api/v1/ai') | ||
export class AiController { | ||
constructor(private readonly aiService: AiService) {} | ||
@Post() | ||
@ApiOperation({ summary: 'AI ๋ต๋ณ์ ๋ฐ์์ต๋๋ค.' }) | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'AI ๋ต๋ณ์ ๋ฐ์์ต๋๋ค.', | ||
type: AiResponseDto, | ||
}) | ||
async ai(@Body() aiDto: AiRequestDto): Promise<AiResponseDto> { | ||
return await this.aiService.getApiResponse(aiDto.message); | ||
} | ||
} |
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 { Module } from '@nestjs/common'; | ||
import { AiController } from './ai.controller'; | ||
import { AiService } from './ai.service'; | ||
|
||
@Module({ | ||
controllers: [AiController], | ||
providers: [AiService], | ||
}) | ||
export class AiModule {} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AiService } from './ai.service'; | ||
|
||
describe('AiService', () => { | ||
let service: AiService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AiService], | ||
}).compile(); | ||
|
||
service = module.get<AiService>(AiService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,67 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import axios from 'axios'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { AiResponseDto } from './dto/ai.dto'; | ||
import { Logger } from 'winston'; | ||
import { preview } from '../common/util'; | ||
|
||
@Injectable() | ||
export class AiService { | ||
private readonly headers = { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}; | ||
private readonly instance = axios.create({ | ||
baseURL: | ||
'https://clovastudio.stream.ntruss.com/testapp/v1/chat-completions/HCX-002', | ||
timeout: 50000, | ||
headers: this.headers, | ||
}); | ||
|
||
constructor( | ||
private configService: ConfigService, | ||
@Inject('winston') private readonly logger: Logger, | ||
) { | ||
this.instance.interceptors.request.use((config) => { | ||
config.headers['X-NCP-CLOVASTUDIO-API-KEY'] = this.configService.get( | ||
'X_NCP_CLOVASTUDIO_API_KEY', | ||
); | ||
config.headers['X-NCP-APIGW-API-KEY'] = this.configService.get( | ||
'X_NCP_APIGW_API_KEY', | ||
); | ||
config.headers['X-NCP-CLOVASTUDIO-REQUEST-ID'] = this.configService.get( | ||
'X_NCP_CLOVASTUDIO_REQUEST_ID', | ||
); | ||
return config; | ||
}); | ||
} | ||
async getApiResponse(message: string): Promise<AiResponseDto> { | ||
const response = await this.instance.post('/', { | ||
messages: [ | ||
{ | ||
role: 'system', | ||
content: | ||
'- Git ์ ๋ฌธ๊ฐ์ ๋๋ค.\\n- Git์ ๋ํ ์ง๋ฌธ๋ง ๋๋ตํฉ๋๋ค.\\n- Git ์ฌ์ฉ์ด ๋ฏ์ ์ฌ๋๋ค์๊ฒ ์ง๋ฌธ์ ๋ฐ์ต๋๋ค.\\n- Git ์ค์น๋ ์ด๋ฏธ ๋ง์ณค์ต๋๋ค.\\n- ์ค๋ช ์ ์ดํดํ๊ธฐ ์ฝ๊ฒ ๋ช ๋ฃํ๊ณ ๊ฐ๋จํ๊ฒ ๋ช ๋ น์ด ์์ฃผ๋ก ๋๋ตํฉ๋๋ค.\\n- ์ง๋ฌธํ ๊ฒ๋ง ๋๋ตํฉ๋๋ค.\\n- Git ๋ช ๋ น์ด๋ก๋ง ํด๋ต์ ์ ์ํฉ๋๋ค.\\n- ์๋ฅผ ๋ค์ด ์ค๋ช ํ์ง ์๋๋ค.', | ||
}, | ||
{ | ||
role: 'user', | ||
content: message, | ||
}, | ||
], | ||
topP: 0.8, | ||
topK: 0, | ||
maxTokens: 512, | ||
temperature: 0.3, | ||
repeatPenalty: 5.0, | ||
stopBefore: [], | ||
includeAiFilters: true, | ||
}); | ||
|
||
this.logger.log( | ||
'info', | ||
`AI response: ${preview(response.data.result.message.content)}`, | ||
); | ||
|
||
return { message: response.data.result.message.content }; | ||
} | ||
} |
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,18 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
|
||
export class AiRequestDto { | ||
@ApiProperty({ | ||
description: '์ง๋ฌธํ ๋ด์ฉ', | ||
example: 'git์ด ๋ญ์ผ?', | ||
}) | ||
message: string; | ||
} | ||
|
||
export class AiResponseDto { | ||
@ApiProperty({ | ||
description: '๋ต๋ณ ๋ด์ฉ', | ||
example: | ||
'Git์ ๋ถ์ฐ ๋ฒ์ ๊ด๋ฆฌ ์์คํ (Distributed Version Control System)์ผ๋ก, ์์ค ์ฝ๋์ ๋ฒ์ ์ ๊ด๋ฆฌํ๊ณ ํ์ ์ ์ง์ํ๋ ๋๊ตฌ์ ๋๋ค. Git์ ๋ค์๊ณผ ๊ฐ์ ํน์ง์ ๊ฐ์ง๊ณ ์์ต๋๋ค.\\n\\n1. **๋ถ์ฐ ์ ์ฅ์**: Git์ ์ค์ ์ง์ค์ ์ ์ฅ์๊ฐ ์๋ ๋ถ์ฐ ์ ์ฅ์๋ฅผ ์ฌ์ฉํฉ๋๋ค. ๊ฐ ์ฌ์ฉ์๋ ์์ ์ ์ปดํจํฐ์ ์ ์ฅ์๋ฅผ ๊ฐ์ง๊ณ ์์ผ๋ฉฐ, ์ด๋ฅผ ๋ก์ปฌ ์ ์ฅ์๋ผ๊ณ ํฉ๋๋ค.\\n2. **๋น ๋ฅธ ์๋**: Git์ ๋น ๋ฅธ ์๋๋ก ํ์ผ์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. ์ด๋ Git์ด ๋ฐ์ดํฐ๋ฅผ ์์ถํ์ฌ ์ ์ฅํ๊ณ , ํด์ ํจ์๋ฅผ ์ด์ฉํ์ฌ ํ์ผ์ ๋น ๋ฅด๊ฒ ๊ฒ์ํ๊ธฐ ๋๋ฌธ์ ๋๋ค.\\n3. **๋ฒ์ ๊ด๋ฆฌ**: Git์ ์์ค ์ฝ๋์ ๋ฒ์ ์ ๊ด๋ฆฌํฉ๋๋ค. ์ฌ์ฉ์๋ ํ์ผ์ ์์ ํ๊ณ ์ปค๋ฐ(commit)ํ๋ฉด, ํด๋น ํ์ผ์ ์ด์ ๋ฒ์ ๊ณผ ์ดํ ๋ฒ์ ์ ๋ชจ๋ ์ ์ฅํ ์ ์์ต๋๋ค.\\n4. **ํ์ ์ง์**: Git์ ํ์ ์ ์ง์ํฉ๋๋ค. ์ฌ์ฉ์๋ ๋ค๋ฅธ ์ฌ์ฉ์์ ํจ๊ป ์์ ์ ํ ์ ์์ผ๋ฉฐ, ์๋ก์ ์์ ๋ด์ฉ์ ๊ณต์ ํ ์ ์์ต๋๋ค.\\n5. **๋ช ๋ น์ด ๊ธฐ๋ฐ**: Git์ ๋ช ๋ น์ด ๊ธฐ๋ฐ์ผ๋ก ๋์ํฉ๋๋ค. ์ฌ์ฉ์๋ Git ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ์ฌ ์ ์ฅ์๋ฅผ ๊ด๋ฆฌํ๊ณ , ํ์ผ์ ์์ ํ ์ ์์ต๋๋ค.\\n\\nGit์ ๋ค์ํ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ์ด์์ฒด์ ์์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋ง์ ๊ฐ๋ฐ์๋ค์ด Git์ ์ด์ฉํ์ฌ ์์ค ์ฝ๋๋ฅผ ๊ด๋ฆฌํ๊ณ ์์ต๋๋ค.', | ||
}) | ||
message: string; | ||
} |
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,8 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CommandService } from './command.service'; | ||
|
||
@Module({ | ||
providers: [CommandService], | ||
exports: [CommandService], | ||
}) | ||
export class CommandModule {} |
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,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CommandService } from './command.service'; | ||
|
||
describe('CommandService', () => { | ||
let service: CommandService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CommandService], | ||
}).compile(); | ||
|
||
service = module.get<CommandService>(CommandService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
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,50 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import axios from 'axios'; | ||
import { Logger } from 'winston'; | ||
import { preview, processCarriageReturns } from '../common/util'; | ||
|
||
@Injectable() | ||
export class CommandService { | ||
private readonly host: string; | ||
private readonly instance; | ||
constructor( | ||
private readonly configService: ConfigService, | ||
@Inject('winston') private readonly logger: Logger, | ||
) { | ||
this.host = this.configService.get<string>('CONTAINER_SERVER_HOST'); | ||
this.instance = axios.create({ | ||
baseURL: this.host, | ||
timeout: 10000, | ||
}); | ||
} | ||
|
||
async executeCommand( | ||
...commands: string[] | ||
): Promise<{ stdoutData: string; stderrData: string }> { | ||
try { | ||
const command = commands.join('; '); | ||
this.logger.log('info', `command: ${preview(command, 40)}`); | ||
const response = await this.instance.post('/', { command }); | ||
return { | ||
stdoutData: processCarriageReturns(response.data.stdoutData), | ||
stderrData: processCarriageReturns(response.data.stderrData), | ||
}; | ||
} catch (error) { | ||
this.logger.log('info', error); | ||
} | ||
} | ||
|
||
async executeCron( | ||
...commands: string[] | ||
): Promise<{ stdoutData: string; stderrData: string }> { | ||
try { | ||
const command = commands.join('; '); | ||
this.logger.log('info', `command: ${preview(command, 40)}`); | ||
const response = await this.instance.post('/cron', { command }); | ||
return response.data; | ||
} catch (error) { | ||
this.logger.log('info', error); | ||
} | ||
} | ||
} |
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,42 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class CommandGuard implements CanActivate { | ||
canActivate(context: ExecutionContext): boolean { | ||
const request = context.switchToHttp().getRequest<Request>(); | ||
const mode = request.body['mode']; | ||
const message = request.body['message']; | ||
if ( | ||
!( | ||
typeof mode === 'string' && | ||
typeof message === 'string' && | ||
(mode === 'editor' || | ||
(mode === 'command' && | ||
message.startsWith('git') && | ||
!this.isMessageIncluded(message, [ | ||
';', | ||
'>', | ||
'|', | ||
'<', | ||
'&', | ||
'$', | ||
'(', | ||
')', | ||
'{', | ||
'}', | ||
]))) | ||
) | ||
) { | ||
throw new ForbiddenException('๊ธ์ง๋ ๋ช ๋ น์ ๋๋ค'); | ||
} | ||
return true; | ||
} | ||
private isMessageIncluded(message: string, keywords: string[]): boolean { | ||
return keywords.some((keyword) => message.includes(keyword)); | ||
} | ||
} |
Oops, something went wrong.