Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 20 additions & 4 deletions src/conversations/conversations.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,28 @@ export class ConversationsController {
}

@Delete(':id')
async remove(@Param('id') id: string) {
return await this.conversationsService.remove(id);
async remove(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Param('id') conversationId: string,
) {
return await this.conversationsService.remove(
namespaceId,
req.user.id,
conversationId,
);
}

@Post(':id/restore')
async restore(@Param('id') id: string) {
return await this.conversationsService.restore(id);
async restore(
@Req() req,
@Param('namespaceId') namespaceId: string,
@Param('id') conversationId: string,
) {
return await this.conversationsService.restore(
namespaceId,
req.user.id,
conversationId,
);
}
}
56 changes: 50 additions & 6 deletions src/conversations/conversations.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { DataSource, Repository } from 'typeorm';
import { Conversation } from 'omniboxd/conversations/entities/conversation.entity';
import { User } from 'omniboxd/user/entities/user.entity';
import { ConfigService } from '@nestjs/config';
Expand All @@ -15,6 +15,10 @@ import {
Message,
OpenAIMessageRole,
} from 'omniboxd/messages/entities/message.entity';
import { Index } from 'omniboxd/resources/wizard-task/index.service';
import { Task } from 'omniboxd/tasks/tasks.entity';

const TASK_PRIORITY = 5;

@Injectable()
export class ConversationsService {
Expand All @@ -23,6 +27,7 @@ export class ConversationsService {
constructor(
@InjectRepository(Conversation)
private readonly conversationRepository: Repository<Conversation>,
private readonly dataSource: DataSource,
private readonly messagesService: MessagesService,
private readonly configService: ConfigService,
) {
Expand Down Expand Up @@ -230,13 +235,46 @@ export class ConversationsService {
});
}

async remove(id: string) {
return await this.conversationRepository.softDelete(id);
async remove(namespaceId: string, userId: string, conversationId: string) {
await this.dataSource.transaction(async (manager) => {
await Index.deleteConversation(
namespaceId,
userId,
conversationId,
TASK_PRIORITY,
manager.getRepository(Task),
);
return await manager.softDelete(Conversation, {
namespaceId,
userId,
id: conversationId,
});
});
}

async restore(id: string) {
await this.conversationRepository.restore(id);
return await this.get(id);
async restore(namespaceId: string, userId: string, conversationId: string) {
const messages = await this.messagesService.findAll(userId, conversationId);
return await this.dataSource.transaction(async (manager) => {
const taskRepo = manager.getRepository(Task);
for (const message of messages) {
await Index.upsertMessageIndex(
TASK_PRIORITY,
userId,
namespaceId,
conversationId,
message,
taskRepo,
);
}
await manager.restore(Conversation, {
namespaceId,
userId,
id: conversationId,
});
return await manager.findOne(Conversation, {
where: { namespaceId, userId, id: conversationId },
});
});
}

async get(id: string) {
Expand All @@ -245,6 +283,12 @@ export class ConversationsService {
});
}

async has(id: string) {
return await this.conversationRepository.exists({
where: { id },
});
}

async listAll(offset: number, limit: number) {
return await this.conversationRepository.find({
skip: offset,
Expand Down
5 changes: 1 addition & 4 deletions src/resources/resources.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import {
Not,
Repository,
} from 'typeorm';
import {
Resource,
ResourceType,
} from 'omniboxd/resources/resources.entity';
import { Resource, ResourceType } from 'omniboxd/resources/resources.entity';
import { CreateResourceDto } from 'omniboxd/resources/dto/create-resource.dto';
import { UpdateResourceDto } from 'omniboxd/resources/dto/update-resource.dto';
import {
Expand Down
25 changes: 21 additions & 4 deletions src/resources/wizard-task/index.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { User } from 'omniboxd/user/entities/user.entity';
import {
Resource,
ResourceType,
} from 'omniboxd/resources/resources.entity';
import { Resource, ResourceType } from 'omniboxd/resources/resources.entity';
import { Repository } from 'typeorm';
import { Task } from 'omniboxd/tasks/tasks.entity';
import {
Expand Down Expand Up @@ -81,4 +78,24 @@ export class Index {
});
return await repo.save(task);
}

static async deleteConversation(
namespaceId: string,
userId: string,
conversationId: string,
priority: number,
repo: Repository<Task>,
) {
return repo.save(
repo.create({
function: 'delete_conversation',
priority,
input: {
conversation_id: conversationId,
},
namespaceId,
userId,
}),
);
}
}
3 changes: 3 additions & 0 deletions src/search/search.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export class SearchService {
continue;
}
seenConversationIds.add(message.conversationId);
if (!(await this.conversationsService.has(message.conversationId))) {
continue;
}
const messageDto: IndexedMessageDto = {
type: DocType.MESSAGE,
id: record.id,
Expand Down
5 changes: 1 addition & 4 deletions src/wizard/stream.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import {
WizardAgentRequestDto,
} from 'omniboxd/wizard/dto/agent-request.dto';
import { ResourcesService } from 'omniboxd/resources/resources.service';
import {
Resource,
ResourceType,
} from 'omniboxd/resources/resources.entity';
import { Resource, ResourceType } from 'omniboxd/resources/resources.entity';
import { ChatResponse } from 'omniboxd/wizard/dto/chat-response.dto';

interface HandlerContext {
Expand Down