(
connectorId: string,
options?: { fullSync?: boolean }
)
| 387 | * listDocuments/getDocument methods. |
| 388 | */ |
| 389 | export async function executeSync( |
| 390 | connectorId: string, |
| 391 | options?: { fullSync?: boolean } |
| 392 | ): Promise<SyncResult> { |
| 393 | const result: SyncResult = { |
| 394 | docsAdded: 0, |
| 395 | docsUpdated: 0, |
| 396 | docsDeleted: 0, |
| 397 | docsUnchanged: 0, |
| 398 | docsFailed: 0, |
| 399 | } |
| 400 | |
| 401 | const connectorRows = await db |
| 402 | .select() |
| 403 | .from(knowledgeConnector) |
| 404 | .where( |
| 405 | and( |
| 406 | eq(knowledgeConnector.id, connectorId), |
| 407 | isNull(knowledgeConnector.archivedAt), |
| 408 | isNull(knowledgeConnector.deletedAt) |
| 409 | ) |
| 410 | ) |
| 411 | .limit(1) |
| 412 | |
| 413 | if (connectorRows.length === 0) { |
| 414 | logger.warn(`Skipping sync: connector ${connectorId} not found, archived, or deleted`) |
| 415 | return { ...result, error: 'connector_unavailable' } |
| 416 | } |
| 417 | |
| 418 | const connector = connectorRows[0] |
| 419 | |
| 420 | const connectorConfig = CONNECTOR_REGISTRY[connector.connectorType] |
| 421 | if (!connectorConfig) { |
| 422 | throw new Error(`Unknown connector type: ${connector.connectorType}`) |
| 423 | } |
| 424 | |
| 425 | const kbRows = await db |
| 426 | .select({ userId: knowledgeBase.userId, workspaceId: knowledgeBase.workspaceId }) |
| 427 | .from(knowledgeBase) |
| 428 | .where(and(eq(knowledgeBase.id, connector.knowledgeBaseId), isNull(knowledgeBase.deletedAt))) |
| 429 | .limit(1) |
| 430 | |
| 431 | if (kbRows.length === 0) { |
| 432 | logger.warn( |
| 433 | `Skipping sync: knowledge base ${connector.knowledgeBaseId} is deleted (connector ${connectorId})` |
| 434 | ) |
| 435 | await db |
| 436 | .update(knowledgeConnector) |
| 437 | .set({ |
| 438 | status: 'error', |
| 439 | nextSyncAt: null, |
| 440 | lastSyncError: 'Knowledge base deleted', |
| 441 | updatedAt: new Date(), |
| 442 | }) |
| 443 | .where(eq(knowledgeConnector.id, connectorId)) |
| 444 | return { ...result, error: 'knowledge_base_deleted' } |
| 445 | } |
| 446 |
no test coverage detected