diff --git a/packages/dashboard/src/__tests__/chat-room-routes.test.ts b/packages/dashboard/src/__tests__/chat-room-routes.test.ts index 84a8ef8e1..2d561af7d 100644 --- a/packages/dashboard/src/__tests__/chat-room-routes.test.ts +++ b/packages/dashboard/src/__tests__/chat-room-routes.test.ts @@ -269,6 +269,32 @@ describe("Chat Room API Routes", () => { expect(del2.status).toBe(404); }); + it("clears all messages in a room", async () => { + const room = chatStore.createRoom({ name: "Clear API" }); + chatStore.addRoomMessage(room.id, { role: "user", content: "one" }); + chatStore.addRoomMessage(room.id, { role: "assistant", content: "two" }); + chatStore.addRoomMessage(room.id, { role: "user", content: "three" }); + + const res = await request(app, "DELETE", `/api/chat/rooms/${room.id}/messages`); + expect(res.status).toBe(200); + expect(res.body).toEqual({ success: true, deletedCount: 3 }); + expect(chatStore.getRoomMessages(room.id)).toEqual([]); + }); + + it("returns 404 when clearing messages for an unknown room", async () => { + const res = await request(app, "DELETE", "/api/chat/rooms/room-missing/messages"); + expect(res.status).toBe(404); + }); + + it("keeps per-message delete route behavior after adding room-clear endpoint", async () => { + const room = chatStore.createRoom({ name: "Route ordering" }); + const message = chatStore.addRoomMessage(room.id, { role: "user", content: "one" }); + + const res = await request(app, "DELETE", `/api/chat/rooms/${room.id}/messages/${message.id}`); + expect(res.status).toBe(200); + expect(chatStore.getRoomMessage(message.id)).toBeUndefined(); + }); + it("handles message attachments route", async () => { const room = chatStore.createRoom({ name: "Files" }); const message = chatStore.addRoomMessage(room.id, { role: "user", content: "hello" }); diff --git a/packages/dashboard/src/routes/register-chat-room-routes.ts b/packages/dashboard/src/routes/register-chat-room-routes.ts index 0e3fe7fa1..b16e2f709 100644 --- a/packages/dashboard/src/routes/register-chat-room-routes.ts +++ b/packages/dashboard/src/routes/register-chat-room-routes.ts @@ -332,6 +332,21 @@ export function registerChatRoomRoutes(ctx: ApiRoutesContext): void { } }); + router.delete("/chat/rooms/:id/messages", rateLimit(RATE_LIMITS.mutation), async (req, res) => { + try { + const roomId = String(req.params.id); + const { chatStore } = await resolveRoomScopedServices(req, getRequestedProjectId(req)); + const room = chatStore.getRoom(roomId); + if (!room) throw notFound(`Chat room ${roomId} not found`); + + const deletedCount = chatStore.clearRoomMessages(roomId); + res.json({ success: true, deletedCount }); + } catch (err: unknown) { + if (err instanceof ApiError) throw err; + rethrowAsApiError(err, "Failed to clear chat room messages"); + } + }); + router.post("/chat/rooms/:id/messages/:messageId/attachments", rateLimit(RATE_LIMITS.mutation), async (req, res) => { try { const roomId = String(req.params.id); @@ -371,6 +386,7 @@ export function registerChatRoomRoutes(ctx: ApiRoutesContext): void { "GET /chat/rooms/:id/messages", "POST /chat/rooms/:id/messages", "DELETE /chat/rooms/:id/messages/:messageId", + "DELETE /chat/rooms/:id/messages", "POST /chat/rooms/:id/messages/:messageId/attachments", ]; chatLogger.info("room routes registered", { chatRoomRoutes });