Skip to content

Commit

Permalink
feat: 하나의 타입에 하나의 스레드만 생성되도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
JongMany committed Oct 16, 2024
1 parent cb5e289 commit 21b9179
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
},
"packageManager": "[email protected]+sha512.af78262d7d125afbfeed740602ace8c5e4405cd7f4735c08feb327286b2fdb2390fbca01589bfd1f50b1240548b74806767f5a063c94b67e431aabd0d86f7774"
}
2 changes: 2 additions & 0 deletions src/gpt/entities/thread.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
Unique,
} from 'typeorm';

@Entity('threads')
@Unique(['user', 'type']) //user와 type을 기반으로 유니크 제약조건 생성
export class ThreadEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
Expand Down
24 changes: 20 additions & 4 deletions src/gpt/gpt.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,26 @@ export class GptController {
@Body() createThreadDto: CreateThreadDto,
@Response() res,
) {
const thread = await this.gptService.createThread(createThreadDto);
return res.json({
threadId: thread.id,
});
try {
const threadId = await this.gptService.findThreadIdByType(
createThreadDto.type,
createThreadDto.email,
);
console.log('threadIds', threadId);
// TODO
if (threadId) {
return res.json({
threadId,
});
} else {
const thread = await this.gptService.createThread(createThreadDto);
return res.json({
threadId: thread.id,
});
}
} catch (error) {
console.error(error);
}
}

@Get('/messages/:threadId')
Expand Down
10 changes: 10 additions & 0 deletions src/gpt/gpt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,14 @@ export class GptService {
type: thread.type,
}));
}

async findThreadIdByType(assistantType: AssistantType, email: string) {
const user = await this.userRepository.findOne({
where: { email },
relations: ['threads'],
});

const thread = user.threads.find((thread) => thread.type === assistantType);
return thread?.threadId || '';
}
}

0 comments on commit 21b9179

Please sign in to comment.