Skip to content

Commit 32a8e9e

Browse files
authored
Merge pull request #63 from import-ai/feature/chat_with_permission
Support permission in chat
2 parents 391690b + 0ea932d commit 32a8e9e

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

src/resources/resources.service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import { Task } from 'src/tasks/tasks.entity';
1919
import { User } from 'src/user/user.entity';
2020
import { MinioService } from 'src/resources/minio/minio.service';
2121
import { WizardTask } from 'src/resources/wizard.task.service';
22-
import { PermissionLevel } from 'src/permissions/permission-level.enum';
2322
import { SpaceType } from 'src/namespaces/entities/namespace.entity';
2423
import { PermissionsService } from 'src/permissions/permissions.service';
2524

@@ -265,7 +264,11 @@ export class ResourcesService {
265264
);
266265
}
267266

268-
async listUserResources(namespaceId: string, userId: string) {
267+
async listUserResources(
268+
namespaceId: string,
269+
userId: string,
270+
includeRoot?: boolean,
271+
) {
269272
const resources = await this.resourceRepository.find({
270273
where: { namespace: { id: namespaceId }, deletedAt: IsNull() },
271274
});
@@ -276,7 +279,7 @@ export class ResourcesService {
276279
resource.id,
277280
userId,
278281
);
279-
if (hasPermission) {
282+
if (hasPermission && (resource.parentId !== null || includeRoot)) {
280283
filtered.push(resource);
281284
}
282285
}
Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
1+
export type FloatPair = [number, number];
2+
3+
export class ToolDto {
4+
name: 'knowledge_search' | 'web_search';
5+
}
6+
7+
export class KnowledgeSearchToolDto extends ToolDto {
8+
declare name: 'knowledge_search';
9+
namespace_id: string;
10+
resource_ids?: Array<string>;
11+
parent_ids?: Array<string>;
12+
created_at?: FloatPair;
13+
updated_at?: FloatPair;
14+
}
15+
16+
export class WebSearchToolDto extends ToolDto {
17+
declare name: 'web_search';
18+
updated_at?: FloatPair;
19+
}
20+
121
export class AgentRequestDto {
222
conversation_id: string;
323
query: string;
424
parent_message_id?: string;
5-
tools?: Array<Record<string, any>>;
25+
tools?: Array<KnowledgeSearchToolDto | WebSearchToolDto>;
626
enable_thinking?: boolean = true;
727
}

src/wizard/stream.service.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import { Observable, Subscriber } from 'rxjs';
44
import { MessageEvent } from '@nestjs/common';
55
import { Message } from 'src/messages/entities/message.entity';
66
import { AgentRequestDto } from 'src/wizard/dto/agent-request.dto';
7+
import { ResourcesService } from 'src/resources/resources.service';
8+
import { Resource } from '../resources/resources.entity';
79

810
export class StreamService {
911
constructor(
1012
private readonly wizardBaseUrl: string,
1113
private readonly messagesService: MessagesService,
14+
private readonly resourcesService: ResourcesService,
1215
) {}
1316

1417
async stream(
@@ -160,6 +163,23 @@ export class StreamService {
160163
currentCiteCnt = buf.currentCiteCnt;
161164
}
162165

166+
if (body.tools) {
167+
for (const tool of body.tools) {
168+
if (
169+
tool.name === 'knowledge_search' &&
170+
tool.resource_ids === undefined &&
171+
tool.parent_ids === undefined
172+
) {
173+
const resources: Resource[] =
174+
await this.resourcesService.listUserResources(
175+
tool.namespace_id,
176+
user.id,
177+
);
178+
tool.resource_ids = resources.map((r) => r.id);
179+
}
180+
}
181+
}
182+
163183
return new Observable<MessageEvent>((subscriber) => {
164184
const handler = this.agentHandler(body.conversation_id, user, subscriber);
165185
this.stream(

src/wizard/wizard.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ export class WizardService {
3636
if (!baseUrl) {
3737
throw new Error('Environment variable OBB_WIZARD_BASE_URL is required');
3838
}
39-
this.streamService = new StreamService(baseUrl, this.messagesService);
39+
this.streamService = new StreamService(
40+
baseUrl,
41+
this.messagesService,
42+
this.resourcesService,
43+
);
4044
}
4145

4246
async create(partialTask: Partial<Task>) {

0 commit comments

Comments
 (0)