KnowledgeProjectSearch constructor
KnowledgeProjectSearch(
- List<
ProjectBundle> bundles, - List<
KnowledgeSearchResponse> responses, { - required int limit,
- List<
KnowledgeSearchFreshness> ? freshness,
Ranks the per-bundle responses, one for each of bundles in order,
keeping the best limit matches and at most limit context passages:
each match, followed by the relationship context reached from it.
freshness, when given, has one entry per bundle too.
Implementation
factory KnowledgeProjectSearch(
List<ProjectBundle> bundles,
List<KnowledgeSearchResponse> responses, {
required int limit,
List<KnowledgeSearchFreshness>? freshness,
}) {
final bundleList = List<ProjectBundle>.unmodifiable(bundles);
final responseList = List<KnowledgeSearchResponse>.unmodifiable(responses);
final freshnessList = List<KnowledgeSearchFreshness>.unmodifiable(
freshness ??
List.filled(bundles.length, const KnowledgeSearchFreshness()),
);
if (bundleList.length != responseList.length ||
freshnessList.length != bundleList.length) {
throw ArgumentError('Supply one response per bundle.');
}
final candidates =
<({ProjectBundle bundle, SearchResult hit})>[
for (final (i, bundle) in bundleList.indexed)
for (final hit in responseList[i].matches)
(bundle: bundle, hit: hit),
]..sort((a, b) {
final score = b.hit.similarity.compareTo(a.hit.similarity);
if (score != 0) return score;
final bundle = a.bundle.path.compareTo(b.bundle.path);
if (bundle != 0) return bundle;
final path = a.hit.chunk.sourcePath.compareTo(b.hit.chunk.sourcePath);
return path != 0 ? path : a.hit.chunk.id.compareTo(b.hit.chunk.id);
});
final ranked = candidates.take(limit).toList();
final context = <KnowledgeCitation>[];
final seen = <(String, String)>{};
void add(KnowledgeCitation citation) {
if (context.length < limit &&
seen.add((citation.bundle.root, citation.sourcePath))) {
context.add(citation);
}
}
KnowledgeSearchFreshness freshnessOf(ProjectBundle bundle) =>
freshnessList[bundleList.indexOf(bundle)];
for (final match in ranked) {
add(
KnowledgeCitation._of(
match.bundle,
match.hit,
reason: 'match',
freshness: freshnessOf(match.bundle),
),
);
final response = responseList[bundleList.indexOf(match.bundle)];
for (final related in response.context.where(
(hit) =>
hit.reason == 'relationship' &&
hit.viaPath == match.hit.chunk.sourcePath,
)) {
add(
KnowledgeCitation._context(
match.bundle,
related,
freshnessOf(match.bundle),
),
);
}
}
return KnowledgeProjectSearch._(
bundleList,
responseList,
freshnessList,
List.unmodifiable([
for (final match in ranked)
KnowledgeCitation._of(
match.bundle,
match.hit,
reason: 'match',
freshness: freshnessOf(match.bundle),
),
]),
List.unmodifiable(context),
List.unmodifiable([
for (final (i, bundle) in bundleList.indexed)
for (final notice in responseList[i].notices) notice.about(bundle),
]),
);
}