search method

Future<KnowledgeProjectSearch> search(
  1. List<ProjectBundle> bundles,
  2. String query, {
  3. int limit = 5,
  4. KnowledgeMetadataFilter? filters,
  5. StaleIndexPolicy stale = StaleIndexPolicy.refuse,
  6. WayfinderCancellation? cancel,
})

Searches the saved indexes of bundles with one query vector and ranks their passages together under one limit, from 1 to 100.

Every bundle must have a usable index; the first that does not fails the whole search. stale decides whether a stale index may be served while another operation refreshes it. filters select passages by frontmatter before ranking. Every lifecycle status stays eligible.

cancel is checked before each bundle, before the encoder is opened and the query encoded, and before the passages are scored; a cancelled search throws cancelled, without a bundle's root before its message, and keeps a retained encoder. A query vector being encoded is not interrupted.

Implementation

Future<KnowledgeProjectSearch> search(
  List<ProjectBundle> bundles,
  String query, {
  int limit = 5,
  KnowledgeMetadataFilter? filters,
  StaleIndexPolicy stale = StaleIndexPolicy.refuse,
  WayfinderCancellation? cancel,
}) => _admit(
  limit: limit,
  () => _encoding((acquire) async {
    BaseEmbedder? queryEmbedder;
    final responses = <KnowledgeSearchResponse>[];
    final freshness = <KnowledgeSearchFreshness>[];
    for (final bundle in bundles) {
      throwIfCancelled(cancel);
      try {
        final found = await _searchIndex(
          await _namespace(bundle),
          query,
          limit: limit,
          filters: filters,
          stale: stale,
          cancel: cancel,
          allContext: true,
          encoder: () async {
            final opened = await acquire();
            queryEmbedder ??= _SharedQueryEmbedder(opened.embedder);
            return WayfinderEncoder(
              queryEmbedder!,
              opened.countTokens,
              opened.maxTokens,
            );
          },
        );
        responses.add(found.response);
        freshness.add(found.freshness);
      } on WayfinderException catch (error) {
        if (error.code == 'cancelled') rethrow;
        throw WayfinderException(
          error.code,
          '${bundle.root}: ${error.message}',
        );
      }
    }
    return KnowledgeProjectSearch(
      bundles,
      responses,
      limit: limit,
      freshness: freshness,
    );
  }),
);