Skip to content

Commit da1463b

Browse files
committed
add user input message to history
1 parent a6fec39 commit da1463b

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

backend/src/chat/chat.resolver.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ChatCompletionChunk } from './chat.model';
33
import { ChatProxyService, ChatService } from './chat.service';
44
import { UserService } from 'src/user/user.service';
55
import { Chat } from './chat.model';
6-
import { Message } from 'src/chat/message.model';
6+
import { Message, Role } from 'src/chat/message.model';
77
import {
88
NewChatInput,
99
UpateChatTitleInput,
@@ -27,13 +27,15 @@ export class ChatResolver {
2727
})
2828
async *chatStream(@Args('input') input: ChatInput) {
2929
const iterator = this.chatProxyService.streamChat(input.message);
30+
this.chatService.saveMessage(input.chatId, null, input.message, Role.User);
3031
try {
3132
for await (const chunk of iterator) {
3233
if (chunk) {
3334
await this.chatService.saveMessage(
3435
input.chatId,
3536
chunk.id,
3637
chunk.choices[0].delta.content,
38+
Role.Model,
3739
);
3840
yield chunk;
3941
}
@@ -58,6 +60,7 @@ export class ChatResolver {
5860
return this.chatService.getMessageById(messageId);
5961
}
6062

63+
@UseGuards(ChatGuard)
6164
@Query(() => [Message])
6265
async getChatHistory(@Args('chatId') chatId: string): Promise<Message[]> {
6366
return this.chatService.getChatHistory(chatId);
@@ -70,7 +73,7 @@ export class ChatResolver {
7073
}
7174

7275
// @Query(() => [Message])
73-
// getModelTags(@Args('chatId') chatId: string): Message[] {
76+
// getAvailableModelTags(@Args('chatId') chatId: string): Message[] {
7477
// return this.chatService.getChatHistory(chatId);
7578
// }
7679

backend/src/chat/chat.service.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,18 +225,19 @@ export class ChatService {
225225

226226
async saveMessage(
227227
chatId: string,
228-
chunkId: string,
228+
chunkId: string | null,
229229
messageContent: string,
230+
role: Role,
230231
): Promise<Message> {
231232
// Find the chat instance
232233
const chat = await this.chatRepository.findOne({ where: { id: chatId } });
233234
if (!chat) throw new Error('Chat not found');
234235

235236
// Create a new message associated with the chat
236237
const message = this.messageRepository.create({
237-
id: chunkId,
238+
id: chunkId || null,
238239
content: messageContent,
239-
role: Role.Model,
240+
role: role,
240241
chat,
241242
createdAt: new Date(),
242243
});

0 commit comments

Comments
 (0)