( knowledgeBaseId: string, userId: string | undefined, tag: string, currentWorkspaceId?: string )
| 445 | } |
| 446 | |
| 447 | async function processKnowledgeFromDb( |
| 448 | knowledgeBaseId: string, |
| 449 | userId: string | undefined, |
| 450 | tag: string, |
| 451 | currentWorkspaceId?: string |
| 452 | ): Promise<AgentContext | null> { |
| 453 | try { |
| 454 | if (userId) { |
| 455 | const accessCheck = await checkKnowledgeBaseAccess(knowledgeBaseId, userId) |
| 456 | if (!accessCheck.hasAccess) { |
| 457 | return null |
| 458 | } |
| 459 | if (currentWorkspaceId && accessCheck.knowledgeBase?.workspaceId !== currentWorkspaceId) { |
| 460 | return null |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | const conditions = [eq(knowledgeBase.id, knowledgeBaseId), isNull(knowledgeBase.deletedAt)] |
| 465 | if (currentWorkspaceId) { |
| 466 | conditions.push(eq(knowledgeBase.workspaceId, currentWorkspaceId)) |
| 467 | } |
| 468 | const kbRows = await dbReplica |
| 469 | .select({ |
| 470 | id: knowledgeBase.id, |
| 471 | name: knowledgeBase.name, |
| 472 | }) |
| 473 | .from(knowledgeBase) |
| 474 | .where(and(...conditions)) |
| 475 | .limit(1) |
| 476 | const kb = kbRows?.[0] |
| 477 | if (!kb) return null |
| 478 | |
| 479 | return { |
| 480 | type: 'knowledge', |
| 481 | tag, |
| 482 | content: '', |
| 483 | path: `${canonicalKnowledgeBaseVfsDir(kb.name)}/meta.json`, |
| 484 | } |
| 485 | } catch (error) { |
| 486 | logger.error('Error processing knowledge context (db)', { knowledgeBaseId, error }) |
| 487 | return null |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | async function processBlockMetadata( |
| 492 | blockId: string, |
no test coverage detected