Skip to content

Commit

Permalink
update save message and get messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHallen122 committed Oct 26, 2024
1 parent 1f2ab88 commit aaea83e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions backend/src/chat/chat.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class ChatResolver {
if (chunk) {
await this.chatService.saveMessage(
input.id,
chunk.id,
chunk.choices[0].delta.content,
);
yield chunk;
Expand All @@ -38,6 +39,13 @@ export class ChatResolver {
}
}

@Query(() => Message, { nullable: true })
async getMessageDetail(
@Args('messageId') messageId: string,
): Promise<Message> {
return this.chatService.getMessageById(messageId);
}

@Query(() => [Message])
async getChatHistory(@Args('chatId') chatId: string): Promise<Message[]> {
return this.chatService.getChatHistory(chatId);
Expand Down
13 changes: 12 additions & 1 deletion backend/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ export class ChatService {
return chat ? chat.messages : [];
}

async getMessageById(messageId: string): Promise<Message> {
return await this.messageRepository.findOne({
where: { id: messageId },
});
}

async getChatDetails(chatId: string): Promise<Chat> {
return this.chatRepository.findOne({
where: { id: chatId },
Expand Down Expand Up @@ -208,13 +214,18 @@ export class ChatService {
return null;
}

async saveMessage(chatId: string, messageContent: string): Promise<Message> {
async saveMessage(
chatId: string,
chunkId: string,
messageContent: string,
): Promise<Message> {
// Find the chat instance
const chat = await this.chatRepository.findOne({ where: { id: chatId } });
if (!chat) throw new Error('Chat not found');

// Create a new message associated with the chat
const message = this.messageRepository.create({
id: chunkId,
content: messageContent,
role: Role.Model,
chat,
Expand Down

0 comments on commit aaea83e

Please sign in to comment.