(
siteIndexingConfig: SiteIndexingConfig,
forceReindex: boolean = false,
)
| 433 | |
| 434 | // eslint-disable-next-line max-statements |
| 435 | async indexAndAdd( |
| 436 | siteIndexingConfig: SiteIndexingConfig, |
| 437 | forceReindex: boolean = false, |
| 438 | ): Promise<void> { |
| 439 | const { startUrl, useLocalCrawling, maxDepth, faviconUrl } = |
| 440 | siteIndexingConfig; |
| 441 | |
| 442 | // First, if indexing is already in process, don't attempt |
| 443 | // This queue is necessary because indexAndAdd is invoked circularly by config edits |
| 444 | // TODO shouldn't really be a gap between adding and checking in queue but probably fine |
| 445 | if (this.docsIndexingQueue.has(startUrl)) { |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | const { provider } = await this.getEmbeddingsProvider(); |
| 450 | if (!provider) { |
| 451 | console.warn("@docs indexAndAdd: no embeddings provider found"); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | const startedWithEmbedder = provider.embeddingId; |
| 456 | |
| 457 | // Check if doc has been successfully indexed with the given embedder |
| 458 | const indexExists = await this.hasMetadata(startUrl); |
| 459 | |
| 460 | // Build status update - most of it is fixed values |
| 461 | const fixedStatus: Omit< |
| 462 | IndexingStatus, |
| 463 | "progress" | "description" | "status" |
| 464 | > = { |
| 465 | type: "docs", |
| 466 | id: siteIndexingConfig.startUrl, |
| 467 | embeddingsProviderId: provider.embeddingId, |
| 468 | isReindexing: forceReindex && indexExists, |
| 469 | title: siteIndexingConfig.title, |
| 470 | debugInfo: `max depth: ${siteIndexingConfig.maxDepth ?? "unlimited"}`, |
| 471 | icon: siteIndexingConfig.faviconUrl, |
| 472 | url: siteIndexingConfig.startUrl, |
| 473 | }; |
| 474 | |
| 475 | // If not force-reindexing and has failed with same config, don't reattempt |
| 476 | if (!forceReindex) { |
| 477 | if (hasIndexingFailed(siteIndexingConfig)) { |
| 478 | console.log( |
| 479 | `Not reattempting to index ${siteIndexingConfig.startUrl}, has already failed with same config`, |
| 480 | ); |
| 481 | this.handleStatusUpdate({ |
| 482 | ...fixedStatus, |
| 483 | description: "Failed", |
| 484 | status: "failed", |
| 485 | progress: 1, |
| 486 | }); |
| 487 | return; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (indexExists && !forceReindex) { |
| 492 | this.handleStatusUpdate({ |
no test coverage detected