octocode 0.12.0

AI-powered code indexer with semantic search, GraphRAG knowledge graphs, and MCP server for multi-language codebases
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
// Copyright 2025 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use uuid::Uuid;

// Arrow imports
use arrow_array::{
	Array, FixedSizeListArray, Float32Array, ListArray, RecordBatch, StringArray, UInt32Array,
};
use arrow_schema::{DataType, Field, Schema};

use crate::store::{CodeBlock, DocumentBlock, TextBlock};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BatchConverter {
	vector_dim: usize,
}

impl BatchConverter {
	pub fn new(vector_dim: usize) -> Self {
		Self { vector_dim }
	}

	// Convert a CodeBlock to a RecordBatch
	pub fn code_block_to_batch(
		&self,
		blocks: &[CodeBlock],
		embeddings: &[Vec<f32>],
	) -> Result<RecordBatch> {
		// Ensure we have the same number of blocks and embeddings
		if blocks.len() != embeddings.len() {
			return Err(anyhow::anyhow!(
				"Number of blocks and embeddings must match"
			));
		}

		if blocks.is_empty() {
			return Err(anyhow::anyhow!("Empty blocks array"));
		}

		// Check if all embedding vectors have the expected dimension
		for (i, embedding) in embeddings.iter().enumerate() {
			if embedding.len() != self.vector_dim {
				return Err(anyhow::anyhow!(
					"Embedding at index {} has dimension {} but expected {}",
					i,
					embedding.len(),
					self.vector_dim
				));
			}
		}

		// Create schema
		let schema = Arc::new(Schema::new(vec![
			Field::new("id", DataType::Utf8, false),
			Field::new("path", DataType::Utf8, false),
			Field::new("language", DataType::Utf8, false),
			Field::new("content", DataType::Utf8, false),
			Field::new("symbols", DataType::Utf8, true), // Storing serialized JSON of symbols
			Field::new("start_line", DataType::UInt32, false),
			Field::new("end_line", DataType::UInt32, false),
			Field::new("hash", DataType::Utf8, false),
			Field::new(
				"embedding",
				DataType::FixedSizeList(
					Arc::new(Field::new("item", DataType::Float32, true)),
					self.vector_dim as i32,
				),
				true,
			),
		]));

		// Create arrays
		let ids: Vec<String> = blocks.iter().map(|_| Uuid::new_v4().to_string()).collect();
		let paths: Vec<&str> = blocks.iter().map(|b| b.path.as_str()).collect();
		let languages: Vec<&str> = blocks.iter().map(|b| b.language.as_str()).collect();
		let contents: Vec<&str> = blocks.iter().map(|b| b.content.as_str()).collect();
		let symbols: Vec<String> = blocks
			.iter()
			.map(|b| serde_json::to_string(&b.symbols).unwrap_or_default())
			.collect();
		let start_lines: Vec<u32> = blocks.iter().map(|b| b.start_line as u32).collect();
		let end_lines: Vec<u32> = blocks.iter().map(|b| b.end_line as u32).collect();
		let hashes: Vec<&str> = blocks.iter().map(|b| b.hash.as_str()).collect();

		// Create the embedding fixed size list array
		let mut flattened_embeddings = Vec::with_capacity(blocks.len() * self.vector_dim);
		for embedding in embeddings {
			flattened_embeddings.extend_from_slice(embedding);
		}
		let values = Float32Array::from(flattened_embeddings);

		// Create the fixed size list array
		let embedding_array = FixedSizeListArray::new(
			Arc::new(Field::new("item", DataType::Float32, true)),
			self.vector_dim as i32,
			Arc::new(values),
			None, // No validity buffer - all values are valid
		);

		// Verify all arrays have the same length
		let expected_len = blocks.len();
		assert_eq!(ids.len(), expected_len, "ids array length mismatch");
		assert_eq!(paths.len(), expected_len, "paths array length mismatch");
		assert_eq!(
			languages.len(),
			expected_len,
			"languages array length mismatch"
		);
		assert_eq!(
			contents.len(),
			expected_len,
			"contents array length mismatch"
		);
		assert_eq!(symbols.len(), expected_len, "symbols array length mismatch");
		assert_eq!(
			start_lines.len(),
			expected_len,
			"start_lines array length mismatch"
		);
		assert_eq!(
			end_lines.len(),
			expected_len,
			"end_lines array length mismatch"
		);
		assert_eq!(hashes.len(), expected_len, "hashes array length mismatch");
		assert_eq!(
			embedding_array.len(),
			expected_len,
			"embedding array length mismatch"
		);

		// Create batch
		let batch = RecordBatch::try_new(
			schema,
			vec![
				Arc::new(StringArray::from(ids)),
				Arc::new(StringArray::from(paths)),
				Arc::new(StringArray::from(languages)),
				Arc::new(StringArray::from(contents)),
				Arc::new(StringArray::from(symbols)),
				Arc::new(UInt32Array::from(start_lines)),
				Arc::new(UInt32Array::from(end_lines)),
				Arc::new(StringArray::from(hashes)),
				Arc::new(embedding_array),
			],
		)?;

		Ok(batch)
	}

	// Convert a TextBlock to a RecordBatch
	pub fn text_block_to_batch(
		&self,
		blocks: &[TextBlock],
		embeddings: &[Vec<f32>],
	) -> Result<RecordBatch> {
		// Ensure we have the same number of blocks and embeddings
		if blocks.len() != embeddings.len() {
			return Err(anyhow::anyhow!(
				"Number of blocks and embeddings must match"
			));
		}

		if blocks.is_empty() {
			return Err(anyhow::anyhow!("Empty blocks array"));
		}

		// Check if all embedding vectors have the expected dimension
		for (i, embedding) in embeddings.iter().enumerate() {
			if embedding.len() != self.vector_dim {
				return Err(anyhow::anyhow!(
					"Embedding at index {} has dimension {} but expected {}",
					i,
					embedding.len(),
					self.vector_dim
				));
			}
		}

		// Create schema matching the actual TextBlock structure
		let schema = Arc::new(Schema::new(vec![
			Field::new("id", DataType::Utf8, false),
			Field::new("path", DataType::Utf8, false),
			Field::new("language", DataType::Utf8, false),
			Field::new("content", DataType::Utf8, false),
			Field::new("start_line", DataType::UInt32, false),
			Field::new("end_line", DataType::UInt32, false),
			Field::new("hash", DataType::Utf8, false),
			Field::new(
				"embedding",
				DataType::FixedSizeList(
					Arc::new(Field::new("item", DataType::Float32, true)),
					self.vector_dim as i32,
				),
				true,
			),
		]));

		// Create arrays
		let ids: Vec<String> = blocks.iter().map(|_| Uuid::new_v4().to_string()).collect();
		let paths: Vec<&str> = blocks.iter().map(|b| b.path.as_str()).collect();
		let languages: Vec<&str> = blocks.iter().map(|b| b.language.as_str()).collect();
		let contents: Vec<&str> = blocks.iter().map(|b| b.content.as_str()).collect();
		let start_lines: Vec<u32> = blocks.iter().map(|b| b.start_line as u32).collect();
		let end_lines: Vec<u32> = blocks.iter().map(|b| b.end_line as u32).collect();
		let hashes: Vec<&str> = blocks.iter().map(|b| b.hash.as_str()).collect();

		// Create the embedding fixed size list array
		let mut flattened_embeddings = Vec::with_capacity(blocks.len() * self.vector_dim);
		for embedding in embeddings {
			flattened_embeddings.extend_from_slice(embedding);
		}
		let values = Float32Array::from(flattened_embeddings);

		// Create the fixed size list array
		let embedding_array = FixedSizeListArray::new(
			Arc::new(Field::new("item", DataType::Float32, true)),
			self.vector_dim as i32,
			Arc::new(values),
			None, // No validity buffer - all values are valid
		);

		// Verify all arrays have the same length
		let expected_len = blocks.len();
		assert_eq!(ids.len(), expected_len, "ids array length mismatch");
		assert_eq!(paths.len(), expected_len, "paths array length mismatch");
		assert_eq!(
			languages.len(),
			expected_len,
			"languages array length mismatch"
		);
		assert_eq!(
			contents.len(),
			expected_len,
			"contents array length mismatch"
		);
		assert_eq!(
			start_lines.len(),
			expected_len,
			"start_lines array length mismatch"
		);
		assert_eq!(
			end_lines.len(),
			expected_len,
			"end_lines array length mismatch"
		);
		assert_eq!(hashes.len(), expected_len, "hashes array length mismatch");
		assert_eq!(
			embedding_array.len(),
			expected_len,
			"embedding array length mismatch"
		);

		// Create batch
		let batch = RecordBatch::try_new(
			schema,
			vec![
				Arc::new(StringArray::from(ids)),
				Arc::new(StringArray::from(paths)),
				Arc::new(StringArray::from(languages)),
				Arc::new(StringArray::from(contents)),
				Arc::new(UInt32Array::from(start_lines)),
				Arc::new(UInt32Array::from(end_lines)),
				Arc::new(StringArray::from(hashes)),
				Arc::new(embedding_array),
			],
		)?;

		Ok(batch)
	}

	// Convert a DocumentBlock to a RecordBatch
	pub fn document_block_to_batch(
		&self,
		blocks: &[DocumentBlock],
		embeddings: &[Vec<f32>],
	) -> Result<RecordBatch> {
		// Ensure we have the same number of blocks and embeddings
		if blocks.len() != embeddings.len() {
			return Err(anyhow::anyhow!(
				"Number of blocks and embeddings must match"
			));
		}

		if blocks.is_empty() {
			return Err(anyhow::anyhow!("Empty blocks array"));
		}

		// Check if all embedding vectors have the expected dimension
		for (i, embedding) in embeddings.iter().enumerate() {
			if embedding.len() != self.vector_dim {
				return Err(anyhow::anyhow!(
					"Embedding at index {} has dimension {} but expected {}",
					i,
					embedding.len(),
					self.vector_dim
				));
			}
		}

		// Create schema matching the actual DocumentBlock structure
		let schema = Arc::new(Schema::new(vec![
			Field::new("id", DataType::Utf8, false),
			Field::new("path", DataType::Utf8, false),
			Field::new("title", DataType::Utf8, false),
			Field::new("content", DataType::Utf8, false),
			Field::new(
				"context",
				DataType::List(Arc::new(Field::new("item", DataType::Utf8, true))),
				true,
			),
			Field::new("level", DataType::UInt32, false),
			Field::new("start_line", DataType::UInt32, false),
			Field::new("end_line", DataType::UInt32, false),
			Field::new("hash", DataType::Utf8, false),
			Field::new(
				"embedding",
				DataType::FixedSizeList(
					Arc::new(Field::new("item", DataType::Float32, true)),
					self.vector_dim as i32,
				),
				true,
			),
		]));

		// Create arrays
		let ids: Vec<String> = blocks.iter().map(|_| Uuid::new_v4().to_string()).collect();
		let paths: Vec<&str> = blocks.iter().map(|b| b.path.as_str()).collect();
		let titles: Vec<&str> = blocks.iter().map(|b| b.title.as_str()).collect();
		let contents: Vec<&str> = blocks.iter().map(|b| b.content.as_str()).collect();

		// Create context list array
		let mut context_values = Vec::new();
		let mut context_offsets = vec![0i32];
		for block in blocks {
			for context_item in &block.context {
				context_values.push(context_item.as_str());
			}
			context_offsets.push(context_values.len() as i32);
		}
		let context_array = ListArray::new(
			Arc::new(Field::new("item", DataType::Utf8, true)),
			arrow::buffer::OffsetBuffer::new(context_offsets.into()),
			Arc::new(StringArray::from(context_values)),
			None,
		);

		let levels: Vec<u32> = blocks.iter().map(|b| b.level as u32).collect();
		let start_lines: Vec<u32> = blocks.iter().map(|b| b.start_line as u32).collect();
		let end_lines: Vec<u32> = blocks.iter().map(|b| b.end_line as u32).collect();
		let hashes: Vec<&str> = blocks.iter().map(|b| b.hash.as_str()).collect();

		// Create the embedding fixed size list array
		let mut flattened_embeddings = Vec::with_capacity(blocks.len() * self.vector_dim);
		for embedding in embeddings {
			flattened_embeddings.extend_from_slice(embedding);
		}
		let values = Float32Array::from(flattened_embeddings);

		// Create the fixed size list array
		let embedding_array = FixedSizeListArray::new(
			Arc::new(Field::new("item", DataType::Float32, true)),
			self.vector_dim as i32,
			Arc::new(values),
			None, // No validity buffer - all values are valid
		);

		// Verify all arrays have the same length
		let expected_len = blocks.len();
		assert_eq!(ids.len(), expected_len, "ids array length mismatch");
		assert_eq!(paths.len(), expected_len, "paths array length mismatch");
		assert_eq!(titles.len(), expected_len, "titles array length mismatch");
		assert_eq!(
			contents.len(),
			expected_len,
			"contents array length mismatch"
		);
		assert_eq!(levels.len(), expected_len, "levels array length mismatch");
		assert_eq!(
			start_lines.len(),
			expected_len,
			"start_lines array length mismatch"
		);
		assert_eq!(
			end_lines.len(),
			expected_len,
			"end_lines array length mismatch"
		);
		assert_eq!(hashes.len(), expected_len, "hashes array length mismatch");
		assert_eq!(
			embedding_array.len(),
			expected_len,
			"embedding array length mismatch"
		);
		assert_eq!(
			embedding_array.len(),
			expected_len,
			"embedding array length mismatch"
		);

		// Create batch
		let batch = RecordBatch::try_new(
			schema,
			vec![
				Arc::new(StringArray::from(ids)),
				Arc::new(StringArray::from(paths)),
				Arc::new(StringArray::from(titles)),
				Arc::new(StringArray::from(contents)),
				Arc::new(context_array),
				Arc::new(UInt32Array::from(levels)),
				Arc::new(UInt32Array::from(start_lines)),
				Arc::new(UInt32Array::from(end_lines)),
				Arc::new(StringArray::from(hashes)),
				Arc::new(embedding_array),
			],
		)?;

		Ok(batch)
	}

	// Convert a RecordBatch to a Vec of CodeBlocks
	pub fn batch_to_code_blocks(
		&self,
		batch: &RecordBatch,
		_embeddings: Option<&[Vec<f32>]>,
	) -> Result<Vec<CodeBlock>> {
		let mut code_blocks = Vec::new();

		// Extract columns from the batch
		let _id_array = batch
			.column_by_name("id")
			.ok_or_else(|| anyhow::anyhow!("id column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("id column is not a StringArray"))?;

		let path_array = batch
			.column_by_name("path")
			.ok_or_else(|| anyhow::anyhow!("path column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("path column is not a StringArray"))?;

		let language_array = batch
			.column_by_name("language")
			.ok_or_else(|| anyhow::anyhow!("language column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("language column is not a StringArray"))?;

		let content_array = batch
			.column_by_name("content")
			.ok_or_else(|| anyhow::anyhow!("content column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("content column is not a StringArray"))?;

		let symbols_array = batch
			.column_by_name("symbols")
			.ok_or_else(|| anyhow::anyhow!("symbols column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("symbols column is not a StringArray"))?;

		let start_line_array = batch
			.column_by_name("start_line")
			.ok_or_else(|| anyhow::anyhow!("start_line column not found"))?
			.as_any()
			.downcast_ref::<UInt32Array>()
			.ok_or_else(|| anyhow::anyhow!("start_line column is not a UInt32Array"))?;

		let end_line_array = batch
			.column_by_name("end_line")
			.ok_or_else(|| anyhow::anyhow!("end_line column not found"))?
			.as_any()
			.downcast_ref::<UInt32Array>()
			.ok_or_else(|| anyhow::anyhow!("end_line column is not a UInt32Array"))?;

		let hash_array = batch
			.column_by_name("hash")
			.ok_or_else(|| anyhow::anyhow!("hash column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("hash column is not a StringArray"))?;
		// Extract distance column from LanceDB search results
		let distance_array = batch
			.column_by_name("_distance")
			.and_then(|col| col.as_any().downcast_ref::<Float32Array>())
			.map(|arr| (0..arr.len()).map(|i| arr.value(i)).collect::<Vec<f32>>())
			.unwrap_or_default();
		for i in 0..batch.num_rows() {
			// Parse symbols JSON
			let symbols_json = symbols_array.value(i);
			let symbols: Vec<String> = if symbols_json.is_empty() {
				Vec::new()
			} else {
				serde_json::from_str(symbols_json).unwrap_or_default()
			};

			let code_block = CodeBlock {
				path: path_array.value(i).to_string(),
				language: language_array.value(i).to_string(),
				content: content_array.value(i).to_string(),
				symbols,
				start_line: (start_line_array.value(i) as usize) + 1, // Convert 0-indexed to 1-indexed
				end_line: (end_line_array.value(i) as usize) + 1,     // Convert 0-indexed to 1-indexed
				hash: hash_array.value(i).to_string(),
				distance: distance_array.get(i).copied(),
			};

			code_blocks.push(code_block);
		}

		Ok(code_blocks)
	}

	// Convert a RecordBatch to a Vec of TextBlocks
	pub fn batch_to_text_blocks(
		&self,
		batch: &RecordBatch,
		_embeddings: Option<&[Vec<f32>]>,
	) -> Result<Vec<TextBlock>> {
		let mut text_blocks = Vec::new();

		// Extract columns from the batch
		let path_array = batch
			.column_by_name("path")
			.ok_or_else(|| anyhow::anyhow!("path column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("path column is not a StringArray"))?;

		let language_array = batch
			.column_by_name("language")
			.ok_or_else(|| anyhow::anyhow!("language column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("language column is not a StringArray"))?;

		let content_array = batch
			.column_by_name("content")
			.ok_or_else(|| anyhow::anyhow!("content column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("content column is not a StringArray"))?;

		let start_line_array = batch
			.column_by_name("start_line")
			.ok_or_else(|| anyhow::anyhow!("start_line column not found"))?
			.as_any()
			.downcast_ref::<UInt32Array>()
			.ok_or_else(|| anyhow::anyhow!("start_line column is not a UInt32Array"))?;

		let end_line_array = batch
			.column_by_name("end_line")
			.ok_or_else(|| anyhow::anyhow!("end_line column not found"))?
			.as_any()
			.downcast_ref::<UInt32Array>()
			.ok_or_else(|| anyhow::anyhow!("end_line column is not a UInt32Array"))?;

		let hash_array = batch
			.column_by_name("hash")
			.ok_or_else(|| anyhow::anyhow!("hash column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("hash column is not a StringArray"))?;

		// Extract distance column from LanceDB search results
		let distance_array = batch
			.column_by_name("_distance")
			.and_then(|col| col.as_any().downcast_ref::<Float32Array>())
			.map(|arr| (0..arr.len()).map(|i| arr.value(i)).collect::<Vec<f32>>())
			.unwrap_or_default();

		// Process each row
		for i in 0..batch.num_rows() {
			let text_block = TextBlock {
				path: path_array.value(i).to_string(),
				language: language_array.value(i).to_string(),
				content: content_array.value(i).to_string(),
				start_line: (start_line_array.value(i) as usize) + 1, // Convert 0-indexed to 1-indexed
				end_line: (end_line_array.value(i) as usize) + 1,     // Convert 0-indexed to 1-indexed
				hash: hash_array.value(i).to_string(),
				distance: distance_array.get(i).copied(),
			};

			text_blocks.push(text_block);
		}

		Ok(text_blocks)
	}

	// Convert a RecordBatch to a Vec of DocumentBlocks
	pub fn batch_to_document_blocks(
		&self,
		batch: &RecordBatch,
		_embeddings: Option<&[Vec<f32>]>,
	) -> Result<Vec<DocumentBlock>> {
		let mut document_blocks = Vec::new();

		// Extract columns from the batch
		let path_array = batch
			.column_by_name("path")
			.ok_or_else(|| anyhow::anyhow!("path column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("path column is not a StringArray"))?;

		let title_array = batch
			.column_by_name("title")
			.ok_or_else(|| anyhow::anyhow!("title column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("title column is not a StringArray"))?;

		let content_array = batch
			.column_by_name("content")
			.ok_or_else(|| anyhow::anyhow!("content column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("content column is not a StringArray"))?;

		let context_array = batch
			.column_by_name("context")
			.ok_or_else(|| anyhow::anyhow!("context column not found"))?
			.as_any()
			.downcast_ref::<ListArray>()
			.ok_or_else(|| anyhow::anyhow!("context column is not a ListArray"))?;

		let start_line_array = batch
			.column_by_name("start_line")
			.ok_or_else(|| anyhow::anyhow!("start_line column not found"))?
			.as_any()
			.downcast_ref::<UInt32Array>()
			.ok_or_else(|| anyhow::anyhow!("start_line column is not a UInt32Array"))?;

		let end_line_array = batch
			.column_by_name("end_line")
			.ok_or_else(|| anyhow::anyhow!("end_line column not found"))?
			.as_any()
			.downcast_ref::<UInt32Array>()
			.ok_or_else(|| anyhow::anyhow!("end_line column is not a UInt32Array"))?;

		let hash_array = batch
			.column_by_name("hash")
			.ok_or_else(|| anyhow::anyhow!("hash column not found"))?
			.as_any()
			.downcast_ref::<StringArray>()
			.ok_or_else(|| anyhow::anyhow!("hash column is not a StringArray"))?;
		// Extract distance column from LanceDB search results
		let distance_array = batch
			.column_by_name("_distance")
			.and_then(|col| col.as_any().downcast_ref::<Float32Array>())
			.map(|arr| (0..arr.len()).map(|i| arr.value(i)).collect::<Vec<f32>>())
			.unwrap_or_default();
		for i in 0..batch.num_rows() {
			let context = if context_array.is_null(i) {
				Vec::new()
			} else {
				// Extract the list of strings from the ListArray
				let list_values = context_array.value(i);
				let string_array = list_values
					.as_any()
					.downcast_ref::<StringArray>()
					.ok_or_else(|| anyhow::anyhow!("Context list items are not strings"))?;

				let mut context_vec = Vec::new();
				for j in 0..string_array.len() {
					if !string_array.is_null(j) {
						context_vec.push(string_array.value(j).to_string());
					}
				}
				context_vec
			};

			// Check if level column exists
			let level = if let Some(level_col) = batch.column_by_name("level") {
				if let Some(level_array) = level_col.as_any().downcast_ref::<UInt32Array>() {
					level_array.value(i) as usize
				} else {
					0 // Default level
				}
			} else {
				0 // Default level if column doesn't exist
			};

			let document_block = DocumentBlock {
				path: path_array.value(i).to_string(),
				title: title_array.value(i).to_string(),
				content: content_array.value(i).to_string(),
				context,
				level,
				start_line: (start_line_array.value(i) as usize) + 1, // Convert 0-indexed to 1-indexed
				end_line: (end_line_array.value(i) as usize) + 1,     // Convert 0-indexed to 1-indexed
				hash: hash_array.value(i).to_string(),
				distance: distance_array.get(i).copied(),
			};

			document_blocks.push(document_block);
		}

		Ok(document_blocks)
	}
}