getChunksWithEmbeddings method

Future<List<(ContentChunkModel, List<double>)>> getChunksWithEmbeddings(
  1. String topicId
)

Fetch chunks for a topic including their embedding vectors (for local sync).

Returns a list of (chunk, embedding) pairs. Chunks without embeddings are included with an empty embedding list.

Implementation

Future<List<(ContentChunkModel, List<double>)>> getChunksWithEmbeddings(
  String topicId,
) async {
  final response = await _client
      .from('content_chunks')
      .select('*, embedding')
      .eq('topic_id', topicId)
      .order('chunk_index', ascending: true);
  return (response as List<dynamic>).map((row) {
    final map = row as Map<String, dynamic>;
    final chunk = ContentChunkModel.fromJson(map);
    final rawEmbedding = map['embedding'];
    List<double> embedding = [];
    if (rawEmbedding is String && rawEmbedding.isNotEmpty) {
      // pgvector returns embeddings as '[0.1,0.2,...]' string.
      final cleaned = rawEmbedding.replaceAll('[', '').replaceAll(']', '');
      embedding = cleaned
          .split(',')
          .map((s) => double.tryParse(s.trim()) ?? 0.0)
          .toList();
    } else if (rawEmbedding is List) {
      embedding = rawEmbedding.cast<num>().map((n) => n.toDouble()).toList();
    }
    return (chunk, embedding);
  }).toList();
}