Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 0b4f4bf

Browse files
committed
feat: add delete message api
1 parent c1e5c74 commit 0b4f4bf

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

cortex-js/src/infrastructure/controllers/threads.controller.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { TransformInterceptor } from '../interceptors/transform.interceptor';
2727
import { ListMessagesResponseDto } from '../dtos/messages/list-message.dto';
2828
import { CreateMessageDto } from '../dtos/threads/create-message.dto';
2929
import { UpdateMessageDto } from '../dtos/threads/update-message.dto';
30+
import DeleteMessageDto from '../dtos/threads/delete-message.dto';
3031

3132
@ApiTags('Threads')
3233
@Controller('threads')
@@ -173,6 +174,29 @@ export class ThreadsController {
173174
);
174175
}
175176

177+
@ApiResponse({
178+
status: 200,
179+
description: 'Deletion status.',
180+
type: DeleteMessageDto,
181+
})
182+
@ApiParam({
183+
name: 'thread_id',
184+
required: true,
185+
description: 'The ID of the thread to which this message belongs.',
186+
})
187+
@ApiParam({
188+
name: 'message_id',
189+
required: true,
190+
description: 'The ID of the message to delete.',
191+
})
192+
@Delete(':thread_id/messages/:message_id')
193+
deleteMessage(
194+
@Param('thread_id') threadId: string,
195+
@Param('message_id') messageId: string,
196+
) {
197+
return this.threadsUsecases.deleteMessage(threadId, messageId);
198+
}
199+
176200
@ApiResponse({
177201
status: 200,
178202
description: 'Ok',
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
3+
export default class DeleteMessageDto {
4+
@ApiProperty()
5+
id: string;
6+
7+
@ApiProperty()
8+
object: string;
9+
10+
@ApiProperty()
11+
deleted: boolean;
12+
}

cortex-js/src/usecases/threads/threads.usecases.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ulid } from 'ulid';
1111
import { ContentType, MessageStatus } from '@/domain/models/message.interface';
1212
import { UpdateMessageDto } from '@/infrastructure/dtos/threads/update-message.dto';
1313
import { Thread } from '@/domain/models/thread.interface';
14+
import DeleteMessageDto from '@/infrastructure/dtos/threads/delete-message.dto';
1415

1516
@Injectable()
1617
export class ThreadsUsecases {
@@ -145,4 +146,26 @@ export class ThreadsUsecases {
145146
remove(id: string) {
146147
this.threadRepository.delete(id);
147148
}
149+
150+
async deleteMessage(
151+
_threadId: string,
152+
messageId: string,
153+
): Promise<DeleteMessageDto> {
154+
// we still allow user to delete message even if the thread is not there
155+
const message = await this.messageRepository.findOne({
156+
where: {
157+
id: messageId,
158+
},
159+
});
160+
if (!message) {
161+
throw new NotFoundException(`Message with id ${messageId} not found`);
162+
}
163+
await this.messageRepository.delete(messageId);
164+
165+
return {
166+
id: messageId,
167+
object: 'thread.message.deleted',
168+
deleted: true,
169+
};
170+
}
148171
}

0 commit comments

Comments
 (0)