feat: history cleanup (#7007)

fix AFF-1069
This commit is contained in:
darkskygit
2024-05-24 08:00:05 +00:00
parent 02564a8d8c
commit 937b8bf166
6 changed files with 188 additions and 77 deletions

View File

@@ -1,6 +1,6 @@
import { createHash } from 'node:crypto';
import { BadRequestException, Logger } from '@nestjs/common';
import { BadRequestException, Logger, NotFoundException } from '@nestjs/common';
import {
Args,
Field,
@@ -55,6 +55,18 @@ class CreateChatSessionInput {
promptName!: string;
}
@InputType()
class DeleteSessionInput {
@Field(() => String)
workspaceId!: string;
@Field(() => String)
docId!: string;
@Field(() => [String])
sessionIds!: string[];
}
@InputType()
class CreateChatMessageInput implements Omit<SubmittedMessage, 'content'> {
@Field(() => String)
@@ -264,6 +276,35 @@ export class CopilotResolver {
return session;
}
@Mutation(() => String, {
description: 'Cleanup sessions',
})
async cleanupCopilotSession(
@CurrentUser() user: CurrentUser,
@Args({ name: 'options', type: () => DeleteSessionInput })
options: DeleteSessionInput
) {
await this.permissions.checkCloudPagePermission(
options.workspaceId,
options.docId,
user.id
);
if (!options.sessionIds.length) {
return new NotFoundException('Session not found');
}
const lockFlag = `${COPILOT_LOCKER}:session:${user.id}:${options.workspaceId}`;
await using lock = await this.mutex.lock(lockFlag);
if (!lock) {
return new TooManyRequestsException('Server is busy');
}
const ret = await this.chatSession.cleanup({
...options,
userId: user.id,
});
return ret;
}
@Mutation(() => String, {
description: 'Create a chat message',
})