Skip to content

Commit 049f75e

Browse files
leftzzzzhuang letian
authored andcommitted
feature: support third platform file for knowledge base
1 parent ed0532e commit 049f75e

File tree

62 files changed

+782
-129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+782
-129
lines changed

backend/magic-service/app/Application/Flow/Service/AbstractFlowAppService.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace App\Application\Flow\Service;
99

1010
use App\Application\Kernel\AbstractKernelAppService;
11+
use App\Application\KnowledgeBase\Service\Strategy\KnowledgeBase\KnowledgeBaseStrategyInterface;
1112
use App\Application\Permission\Service\OperationPermissionAppService;
1213
use App\Domain\Agent\Service\MagicAgentDomainService;
1314
use App\Domain\Agent\Service\MagicAgentVersionDomainService;
@@ -65,6 +66,7 @@ public function __construct(
6566
protected readonly MagicAccountDomainService $magicAccountDomainService,
6667
protected readonly ServiceProviderDomainService $serviceProviderDomainService,
6768
protected readonly KnowledgeBaseDocumentDomainService $magicFlowDocumentDomainService,
69+
protected readonly KnowledgeBaseStrategyInterface $knowledgeBaseStrategy,
6870
) {
6971
}
7072

backend/magic-service/app/Application/Flow/Service/MagicFlowAppService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,7 @@ public function queryKnowledge(Authenticatable $authorization): array
407407
} else {
408408
$knowledge->setUserOperation(($resources[$knowledge->getCode()] ?? Operation::None)->value);
409409
}
410+
$knowledge->setSourceType($this->knowledgeBaseStrategy->getOrCreateDefaultSourceType($knowledge));
410411
}
411412
$knowledgeData['users'] = $this->magicUserDomainService->getByUserIds($this->createContactDataIsolation($dataIsolation), $userIds);
412413

backend/magic-service/app/Application/KnowledgeBase/Event/Subscribe/KnowledgeBaseDefaultDocumentSavedSubscriber.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace App\Application\KnowledgeBase\Event\Subscribe;
99

1010
use App\Domain\KnowledgeBase\Entity\KnowledgeBaseFragmentEntity;
11-
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeBaseDataIsolation;
11+
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeType;
1212
use App\Domain\KnowledgeBase\Entity\ValueObject\Query\KnowledgeBaseFragmentQuery;
1313
use App\Domain\KnowledgeBase\Event\KnowledgeBaseDefaultDocumentSavedEvent;
1414
use App\Domain\KnowledgeBase\Service\KnowledgeBaseFragmentDomainService;
@@ -43,7 +43,11 @@ public function process(object $event): void
4343
}
4444
$knowledge = $event->knowledgeBaseEntity;
4545
$documentEntity = $event->knowledgeBaseDocumentEntity;
46-
$dataIsolation = KnowledgeBaseDataIsolation::create($knowledge->getOrganizationCode(), $knowledge->getCreator());
46+
$dataIsolation = $event->dataIsolation;
47+
// 如果是基础知识库类型,则传知识库创建者,避免权限不足
48+
if (in_array($knowledge->getType(), KnowledgeType::getAll())) {
49+
$dataIsolation->setCurrentUserId($knowledge->getCreator())->setCurrentOrganizationCode($knowledge->getOrganizationCode());
50+
}
4751
/** @var KnowledgeBaseFragmentDomainService $knowledgeBaseFragmentDomainService */
4852
$knowledgeBaseFragmentDomainService = di(KnowledgeBaseFragmentDomainService::class);
4953

backend/magic-service/app/Application/KnowledgeBase/Event/Subscribe/KnowledgeBaseDocumentDestroySubscriber.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use App\Domain\KnowledgeBase\Entity\KnowledgeBaseFragmentEntity;
1111
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeBaseDataIsolation;
1212
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeSyncStatus;
13+
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeType;
1314
use App\Domain\KnowledgeBase\Entity\ValueObject\Query\KnowledgeBaseFragmentQuery;
1415
use App\Domain\KnowledgeBase\Event\KnowledgeBaseDocumentRemovedEvent;
1516
use App\Domain\KnowledgeBase\Service\KnowledgeBaseDocumentDomainService;
@@ -42,8 +43,13 @@ public function process(object $event): void
4243
if (! $event instanceof KnowledgeBaseDocumentRemovedEvent) {
4344
return;
4445
}
46+
$knowledge = $event->knowledgeBaseEntity;
4547
$document = $event->knowledgeBaseDocumentEntity;
46-
$dataIsolation = KnowledgeBaseDataIsolation::create($document->getOrganizationCode());
48+
$dataIsolation = $event->dataIsolation;
49+
// 如果是基础知识库类型,则传知识库创建者,避免权限不足
50+
if (in_array($knowledge->getType(), KnowledgeType::getAll())) {
51+
$dataIsolation->setCurrentUserId($knowledge->getCreator())->setCurrentOrganizationCode($knowledge->getOrganizationCode());
52+
}
4753
/** @var KnowledgeBaseDomainService $knowledgeBaseDomainService */
4854
$knowledgeBaseDomainService = di()->get(KnowledgeBaseDomainService::class);
4955
/** @var KnowledgeBaseFragmentDomainService $knowledgeBaseFragmentDomainService */

backend/magic-service/app/Application/KnowledgeBase/Event/Subscribe/KnowledgeBaseDocumentReSyncSubscriber.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
namespace App\Application\KnowledgeBase\Event\Subscribe;
99

1010
use App\Application\KnowledgeBase\Service\KnowledgeBaseVectorAppService;
11-
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeBaseDataIsolation;
1211
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeSyncStatus;
12+
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeType;
1313
use App\Domain\KnowledgeBase\Event\KnowledgeBaseDocumentSavedEvent;
1414
use App\Domain\KnowledgeBase\Service\KnowledgeBaseDocumentDomainService;
1515
use App\Infrastructure\Util\Locker\LockerInterface;
@@ -70,7 +70,11 @@ private function handle(
7070
): void {
7171
$knowledge = $event->knowledgeBaseEntity;
7272
$documentEntity = $event->knowledgeBaseDocumentEntity;
73-
$dataIsolation = KnowledgeBaseDataIsolation::create($knowledge->getOrganizationCode(), $knowledge->getCreator());
73+
$dataIsolation = $event->dataIsolation;
74+
// 如果是基础知识库类型,则传知识库创建者,避免权限不足
75+
if (in_array($knowledge->getType(), KnowledgeType::getAll())) {
76+
$dataIsolation->setCurrentUserId($knowledge->getCreator())->setCurrentOrganizationCode($knowledge->getOrganizationCode());
77+
}
7478
/** @var KnowledgeBaseDocumentDomainService $knowledgeBaseDocumentDomainService */
7579
$knowledgeBaseDocumentDomainService = di(KnowledgeBaseDocumentDomainService::class);
7680
/** @var LoggerInterface $logger */

backend/magic-service/app/Application/KnowledgeBase/Event/Subscribe/KnowledgeBaseDocumentSyncSubscriber.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
namespace App\Application\KnowledgeBase\Event\Subscribe;
99

1010
use App\Application\KnowledgeBase\Service\KnowledgeBaseVectorAppService;
11-
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeBaseDataIsolation;
1211
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeSyncStatus;
12+
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeType;
1313
use App\Domain\KnowledgeBase\Event\KnowledgeBaseDocumentSavedEvent;
1414
use App\Domain\KnowledgeBase\Service\KnowledgeBaseDocumentDomainService;
1515
use Dtyq\AsyncEvent\Kernel\Annotation\AsyncListener;
@@ -45,7 +45,11 @@ public function process(object $event): void
4545
}
4646
$knowledge = $event->knowledgeBaseEntity;
4747
$documentEntity = $event->knowledgeBaseDocumentEntity;
48-
$dataIsolation = KnowledgeBaseDataIsolation::create($knowledge->getOrganizationCode(), $knowledge->getCreator());
48+
$dataIsolation = $event->dataIsolation;
49+
// 如果是基础知识库类型,则传知识库创建者,避免权限不足
50+
if (in_array($knowledge->getType(), KnowledgeType::getAll())) {
51+
$dataIsolation->setCurrentUserId($knowledge->getCreator())->setCurrentOrganizationCode($knowledge->getOrganizationCode());
52+
}
4953
/** @var KnowledgeBaseDocumentDomainService $knowledgeBaseDocumentDomainService */
5054
$knowledgeBaseDocumentDomainService = di(KnowledgeBaseDocumentDomainService::class);
5155
/** @var LoggerInterface $logger */

backend/magic-service/app/Application/KnowledgeBase/Event/Subscribe/KnowledgeBaseSyncSubscriber.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use App\Application\KnowledgeBase\Service\Strategy\DocumentFile\DocumentFileStrategy;
1111
use App\Application\ModelGateway\Mapper\ModelGatewayMapper;
1212
use App\Domain\KnowledgeBase\Entity\KnowledgeBaseDocumentEntity;
13-
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeBaseDataIsolation;
1413
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeSyncStatus;
14+
use App\Domain\KnowledgeBase\Entity\ValueObject\KnowledgeType;
1515
use App\Domain\KnowledgeBase\Event\KnowledgeBaseSavedEvent;
1616
use App\Domain\KnowledgeBase\Service\KnowledgeBaseDocumentDomainService;
1717
use App\Domain\KnowledgeBase\Service\KnowledgeBaseDomainService;
@@ -41,7 +41,11 @@ public function process(object $event): void
4141
return;
4242
}
4343
$knowledge = $event->magicFlowKnowledgeEntity;
44-
$dataIsolation = KnowledgeBaseDataIsolation::create($knowledge->getOrganizationCode(), $knowledge->getCreator());
44+
$dataIsolation = $event->dataIsolation;
45+
// 如果是基础知识库类型,则传知识库创建者,避免权限不足
46+
if (in_array($knowledge->getType(), KnowledgeType::getAll())) {
47+
$dataIsolation->setCurrentUserId($knowledge->getCreator())->setCurrentOrganizationCode($knowledge->getOrganizationCode());
48+
}
4549
/** @var KnowledgeBaseDomainService $knowledgeBaseDomainService */
4650
$knowledgeBaseDomainService = $this->container->get(KnowledgeBaseDomainService::class);
4751

@@ -83,7 +87,7 @@ public function process(object $event): void
8387
->setRetrieveConfig($knowledge->getRetrieveConfig())
8488
->setVectorDb($knowledge->getVectorDb())
8589
->setDocumentFile($file);
86-
$knowledgeBaseDocumentDomainService->create($dataIsolation, $knowledge, $documentEntity);
90+
$knowledgeBaseDocumentDomainService->create(clone $dataIsolation, $knowledge, $documentEntity);
8791
}
8892
} catch (Throwable $throwable) {
8993
$logger->error($throwable->getMessage() . PHP_EOL . $throwable->getTraceAsString());

backend/magic-service/app/Application/KnowledgeBase/Service/KnowledgeBaseAppService.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public function save(Authenticatable $authorization, KnowledgeBaseEntity $magicF
105105
$knowledgeBaseEntity->setUserOperation($operation->value);
106106
$iconFileLink = $this->getFileLink($dataIsolation->getCurrentOrganizationCode(), $knowledgeBaseEntity->getIcon());
107107
$knowledgeBaseEntity->setIcon($iconFileLink?->getUrl() ?? '');
108+
$knowledgeBaseEntity->setSourceType($this->knowledgeBaseStrategy->getOrCreateDefaultSourceType($knowledgeBaseEntity));
108109
return $knowledgeBaseEntity;
109110
}
110111

@@ -114,7 +115,9 @@ public function saveProcess(Authenticatable $authorization, KnowledgeBaseEntity
114115
$savingKnowledgeEntity->setCreator($dataIsolation->getCurrentUserId());
115116
$this->checkKnowledgeBaseOperation($dataIsolation, 'w', $savingKnowledgeEntity->getCode());
116117

117-
return $this->knowledgeBaseDomainService->saveProcess($dataIsolation, $savingKnowledgeEntity);
118+
$entity = $this->knowledgeBaseDomainService->saveProcess($dataIsolation, $savingKnowledgeEntity);
119+
$entity->setSourceType($this->knowledgeBaseStrategy->getOrCreateDefaultSourceType($entity));
120+
return $entity;
118121
}
119122

120123
public function getByBusinessId(Authenticatable $authorization, string $businessId, ?int $type = null): ?KnowledgeBaseEntity
@@ -137,7 +140,9 @@ public function getByBusinessId(Authenticatable $authorization, string $business
137140
$query->setBusinessId($businessId);
138141
$query->setType($type);
139142
$result = $this->knowledgeBaseDomainService->queries($dataIsolation, $query, new Page(1, 1));
140-
return $result['list'][0] ?? null;
143+
$entity = $result['list'][0] ?? null;
144+
$entity && $entity->setSourceType($this->knowledgeBaseStrategy->getOrCreateDefaultSourceType($entity));
145+
return $entity;
141146
}
142147

143148
/**
@@ -159,6 +164,7 @@ public function queries(Authenticatable $authorization, KnowledgeBaseQuery $quer
159164
$iconFileLink = $iconFileLinks[$item->getIcon()] ?? null;
160165
$item->setIcon($iconFileLink?->getUrl() ?? '');
161166
$item->setUserOperation(($resources[$item->getCode()] ?? Operation::None)->value);
167+
$item->setSourceType($this->knowledgeBaseStrategy->getOrCreateDefaultSourceType($item));
162168
}
163169
$result['users'] = $this->magicUserDomainService->getByUserIds($this->createContactDataIsolationByBase($dataIsolation), $userIds);
164170
return $result;
@@ -170,6 +176,7 @@ public function show(Authenticatable $authorization, string $code): KnowledgeBas
170176
$operation = $this->checkKnowledgeBaseOperation($dataIsolation, 'r', $code);
171177
$knowledge = $this->knowledgeBaseDomainService->show($dataIsolation, $code, true);
172178
$knowledge->setUserOperation($operation->value);
179+
$knowledge->setSourceType($this->knowledgeBaseStrategy->getOrCreateDefaultSourceType($knowledge));
173180
$iconFileLink = $this->fileDomainService->getLink($dataIsolation->getCurrentOrganizationCode(), $knowledge->getIcon());
174181
$knowledge->setIcon($iconFileLink?->getUrl() ?? '');
175182
return $knowledge;

backend/magic-service/app/Application/KnowledgeBase/Service/KnowledgeBaseDocumentAppService.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,13 @@ public function reVectorizedByThirdFileId(Authenticatable $authorization, string
101101
$dataIsolation = $this->createKnowledgeBaseDataIsolation($authorization);
102102
$documents = $this->knowledgeBaseDocumentDomainService->getByThirdFileId($dataIsolation, $thirdPlatformType, $thirdFileId);
103103
$knowledgeEntities = $this->knowledgeBaseDomainService->getByCodes($dataIsolation, array_column($documents, 'knowledge_base_code'));
104+
/** @var array<string, KnowledgeBaseEntity> $knowledgeEntities */
105+
$knowledgeEntities = array_column($knowledgeEntities, null, 'code');
104106

105107
foreach ($documents as $document) {
106108
$knowledgeEntity = $knowledgeEntities[$document['knowledge_base_code']] ?? null;
107-
if ($knowledgeEntity) {
108-
$event = new KnowledgeBaseDocumentSavedEvent($knowledgeEntity, $document, false);
109+
if ($knowledgeEntity && $knowledgeEntity->getType() === KnowledgeType::UserKnowledgeBase->value) {
110+
$event = new KnowledgeBaseDocumentSavedEvent($dataIsolation, $knowledgeEntity, $document, false);
109111
AsyncEventUtil::dispatch($event);
110112
}
111113
}
@@ -133,9 +135,10 @@ public function destroy(Authenticatable $authorization, string $knowledgeBaseCod
133135
{
134136
$dataIsolation = $this->createKnowledgeBaseDataIsolation($authorization);
135137
$this->checkKnowledgeBaseOperation($dataIsolation, 'del', $knowledgeBaseCode, $documentCode);
138+
$knowledgeBaseEntity = $this->knowledgeBaseDomainService->show($dataIsolation, $knowledgeBaseCode);
136139

137140
// 调用领域服务删除文档
138-
$this->knowledgeBaseDocumentDomainService->destroy($dataIsolation, $knowledgeBaseCode, $documentCode);
141+
$this->knowledgeBaseDocumentDomainService->destroy($dataIsolation, $knowledgeBaseEntity, $documentCode);
139142
}
140143

141144
/**
@@ -155,6 +158,7 @@ public function reVectorized(Authenticatable $authorization, string $knowledgeBa
155158
}
156159
// 分发事件,重新向量化
157160
$documentSavedEvent = new KnowledgeBaseDocumentSavedEvent(
161+
$dataIsolation,
158162
$knowledgeBaseEntity,
159163
$documentEntity,
160164
false,

backend/magic-service/app/Application/KnowledgeBase/Service/KnowledgeBaseFragmentAppService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public function destroyByMetadataFilter(Authenticatable $authorization, string $
8787
public function fragmentPreview(Authenticatable $authorization, DocumentFileInterface $documentFile, FragmentConfig $fragmentConfig): array
8888
{
8989
$dataIsolation = $this->createKnowledgeBaseDataIsolation($authorization);
90+
$documentFile = $this->documentFileStrategy->preProcessDocumentFile($dataIsolation, $documentFile);
9091
$content = $this->documentFileStrategy->parseContent($dataIsolation, $documentFile);
9192
$fragmentContents = $this->knowledgeBaseFragmentDomainService->processFragmentsByContent($dataIsolation, $content, $fragmentConfig);
9293
return KnowledgeBaseFragmentEntity::fromFragmentContents($fragmentContents);

0 commit comments

Comments
 (0)