getNeighborChunks method

Future<List<ContentChunkModel>> getNeighborChunks(
  1. String topicId,
  2. List<int> chunkIndices, {
  3. int window = 1,
})

Fetch chunks neighboring the given chunkIndices within a topic.

For each index in chunkIndices, includes chunks within ±window. Results are deduplicated and sorted by chunk_index.

Implementation

Future<List<ContentChunkModel>> getNeighborChunks(
  String topicId,
  List<int> chunkIndices, {
  int window = 1,
}) async {
  final expanded = <int>{};
  for (final idx in chunkIndices) {
    for (int d = -window; d <= window; d++) {
      final neighbor = idx + d;
      if (neighbor >= 0) expanded.add(neighbor);
    }
  }
  final response = await _client
      .from('content_chunks')
      .select()
      .eq('topic_id', topicId)
      .inFilter('chunk_index', expanded.toList())
      .order('chunk_index', ascending: true);
  return (response as List<dynamic>)
      .map((row) => ContentChunkModel.fromJson(row as Map<String, dynamic>))
      .toList();
}