Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/cli/src/modules/chat-hub/chat-message.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { DataSource, EntityManager, Repository } from '@n8n/typeorm';

import { ChatHubMessage } from './chat-hub-message.entity';
import { ChatHubSessionRepository } from './chat-session.repository';
import type { IBinaryData } from 'n8n-workflow';
import { UnexpectedError, type IBinaryData } from 'n8n-workflow';
import { QueryDeepPartialEntity } from '@n8n/typeorm/query-builder/QueryPartialEntity';

@Service()
export class ChatHubMessageRepository extends Repository<ChatHubMessage> {
Expand All @@ -16,14 +17,19 @@ export class ChatHubMessageRepository extends Repository<ChatHubMessage> {
super(ChatHubMessage, dataSource.manager);
}

async createChatMessage(message: Partial<ChatHubMessage>, trx?: EntityManager) {
async createChatMessage(message: QueryDeepPartialEntity<ChatHubMessage>, trx?: EntityManager) {
const messageId = message.id;
if (typeof messageId === 'function') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check is needed because QueryDeepPartialEntity allows every property to be either a value or (() => string) for SQL functions like NOW().

throw new UnexpectedError('Message ID is required and must be a string value');
}

return await withTransaction(
this.manager,
trx,
async (em) => {
await em.insert(ChatHubMessage, message);
const saved = await em.findOneOrFail(ChatHubMessage, {
where: { id: message.id },
where: { id: messageId },
});
await this.chatSessionRepository.updateLastMessageAt(saved.sessionId, saved.createdAt, em);
return saved;
Expand Down
Loading