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
use super::FileSignature;
use crate::config::Config;
use crate::store::{CodeBlock, DocumentBlock, TextBlock};
use anyhow::Result;

// Extracted rendering functions:
pub fn render_to_markdown<T: std::fmt::Display>(_title: &str, content: T) -> String {
	format!("{}", content)
}

/// Smart content truncation that preserves beginning and end when content is too long
/// Returns (truncated_content, was_truncated)
pub fn truncate_content_smartly(content: &str, max_characters: usize) -> (String, bool) {
	// If max_characters is 0, return full content (disabled)
	if max_characters == 0 {
		return (content.to_string(), false);
	}

	// If content fits within limit, return as-is
	if content.len() <= max_characters {
		return (content.to_string(), false);
	}

	let lines: Vec<&str> = content.lines().collect();

	// If it's just one long line, truncate it differently
	if lines.len() == 1 {
		let chars: Vec<char> = content.chars().collect();
		if chars.len() <= max_characters {
			return (content.to_string(), false);
		}

		// For single long line, show first and last parts
		let show_start = max_characters / 3;
		let show_end = max_characters / 3;
		let start_part: String = chars.iter().take(show_start).collect();
		let end_part: String = chars.iter().skip(chars.len() - show_end).collect();

		let truncated = format!(
			"{}\n[... {} characters omitted ...]\n{}",
			start_part.trim_end(),
			chars.len() - show_start - show_end,
			end_part.trim_start()
		);
		return (truncated, true);
	}

	// For multi-line content, work with lines
	let mut current_length = 0;
	let mut start_lines = Vec::new();
	let mut end_lines = Vec::new();

	// Reserve space for the middle message
	let middle_message_size = 50; // Approximate size of "[... X lines omitted ...]"
	let target_size = max_characters.saturating_sub(middle_message_size);
	let start_target = target_size / 2;
	let end_target = target_size / 2;

	// Collect start lines
	for line in &lines {
		let line_len = line.len() + 1; // +1 for newline
		if current_length + line_len <= start_target {
			start_lines.push(*line);
			current_length += line_len;
		} else {
			break;
		}
	}

	// Collect end lines (working backwards)
	current_length = 0;
	for line in lines.iter().rev() {
		let line_len = line.len() + 1; // +1 for newline
		if current_length + line_len <= end_target {
			end_lines.insert(0, *line);
			current_length += line_len;
		} else {
			break;
		}
	}

	// Ensure we don't overlap
	let start_count = start_lines.len();
	let end_count = end_lines.len();
	let total_lines = lines.len();

	if start_count + end_count >= total_lines {
		// If we would show most lines anyway, just show all
		return (content.to_string(), false);
	}

	let omitted_lines = total_lines - start_count - end_count;

	// Build the truncated content
	let mut result = String::new();

	// Add start lines
	for line in &start_lines {
		result.push_str(line);
		result.push('\n');
	}

	// Add truncation message
	if omitted_lines > 0 {
		result.push_str(&format!("[... {} more lines ...]\n", omitted_lines));
	}

	// Add end lines
	for line in &end_lines {
		result.push_str(line);
		result.push('\n');
	}

	// Remove trailing newline
	if result.ends_with('\n') {
		result.pop();
	}

	(result, true)
}

/// Render signatures as markdown string
pub fn signatures_to_markdown(signatures: &[FileSignature]) -> String {
	let mut markdown = String::new();

	if signatures.is_empty() {
		markdown.push_str("No signatures found.");
		return markdown;
	}

	markdown.push_str(&format!(
		"# Found signatures in {} files\n\n",
		signatures.len()
	));

	for file in signatures {
		markdown.push_str(&format!("## File: {}\n", file.path));
		markdown.push_str(&format!("**Language:** {}\n\n", file.language));

		// Show file comment if available
		if let Some(comment) = &file.file_comment {
			markdown.push_str("### File description\n");
			markdown.push_str(&format!("> {}\n\n", comment.replace("\n", "\n> ")));
		}

		if file.signatures.is_empty() {
			markdown.push_str("*No signatures found in this file.*\n\n");
		} else {
			for signature in &file.signatures {
				// Display line range if it spans multiple lines, otherwise just the start line
				let line_display = if signature.start_line == signature.end_line {
					format!("{}", signature.start_line)
				} else {
					format!("{}-{}", signature.start_line, signature.end_line)
				};

				markdown.push_str(&format!(
					"### {} `{}` (line {})\n",
					signature.kind, signature.name, line_display
				));

				// Show description if available
				if let Some(desc) = &signature.description {
					markdown.push_str(&format!("> {}\n\n", desc.replace("\n", "\n> ")));
				}

				// Format the signature for display
				markdown.push_str("```");

				// Add language identifier for syntax highlighting when possible
				if !file.language.is_empty() && file.language != "text" {
					markdown.push_str(&file.language);
				}
				markdown.push('\n');

				let lines = signature.signature.lines().collect::<Vec<_>>();
				if lines.len() > 5 {
					// Show first 2 lines and last 2 lines to maintain context
					for line in lines.iter().take(2) {
						markdown.push_str(line.as_ref());
						markdown.push('\n');
					}
					// If signature is too long, note how many lines are omitted
					markdown.push_str(&format!("// ... {} more lines\n", lines.len() - 4));
					for line in lines.iter().skip(lines.len() - 2) {
						markdown.push_str(line.as_ref());
						markdown.push('\n');
					}
				} else {
					for line in &lines {
						markdown.push_str(line.as_ref());
						markdown.push('\n');
					}
				}
				markdown.push_str("```\n\n");
			}
		}

		// Add spacing between files
		markdown.push_str("---\n\n");
	}

	markdown
}

/// Render signatures as text string (token-efficient)
pub fn render_signatures_text(signatures: &[FileSignature]) -> String {
	let mut output = String::new();

	if signatures.is_empty() {
		output.push_str("No signatures found.");
		return output;
	}

	output.push_str(&format!("SIGNATURES ({} files)\n\n", signatures.len()));

	for file in signatures {
		output.push_str(&format!("FILE: {}\n", file.path));
		output.push_str(&format!("Language: {}\n", file.language));

		// Show file comment if available
		if let Some(comment) = &file.file_comment {
			output.push_str(&format!("Description: {}\n", comment.replace('\n', " ")));
		}

		if file.signatures.is_empty() {
			output.push_str("No signatures found.\n");
		} else {
			for signature in &file.signatures {
				// Display line range
				// let line_display = if signature.start_line == signature.end_line {
				// 	format!("{}", signature.start_line)
				// } else {
				// 	format!("{}-{}", signature.start_line, signature.end_line)
				// };

				// output.push_str(&format!(
				// 	"{} {} (line {})\n",
				// 	signature.kind, signature.name, line_display
				// ));

				// Show description if available
				if let Some(desc) = &signature.description {
					output.push_str(&format!("// {}\n", desc.replace('\n', " ")));
				}

				// Add signature content with line numbers (smart truncation showing first and last lines)
				let lines = signature.signature.lines().collect::<Vec<_>>();
				let content_with_lines = if lines.len() > 5 {
					// Show first 2 lines, then "...", then last 2 lines to maintain context
					let first_lines: Vec<String> = lines
						.iter()
						.take(2)
						.enumerate()
						.map(|(i, line)| format!("{}: {}", signature.start_line + i, line))
						.collect();

					let last_lines: Vec<String> = lines
						.iter()
						.skip(lines.len() - 2)
						.enumerate()
						.map(|(i, line)| {
							let line_num = signature.start_line + (lines.len() - 2) + i;
							format!("{}: {}", line_num, line)
						})
						.collect();

					let mut result = first_lines.join("\n");
					result.push_str(&format!("\n// ... {} more lines", lines.len() - 4));
					result.push('\n');
					result.push_str(&last_lines.join("\n"));
					result
				} else {
					lines
						.iter()
						.enumerate()
						.map(|(i, line)| format!("{}: {}", signature.start_line + i, line))
						.collect::<Vec<_>>()
						.join("\n")
				};
				output.push_str(&content_with_lines);
				if !content_with_lines.ends_with('\n') {
					output.push('\n');
				}
				output.push('\n');
			}
		}
		output.push('\n');
	}

	output
}

/// Render code blocks (search results) as markdown string
pub fn code_blocks_to_markdown(blocks: &[CodeBlock]) -> String {
	code_blocks_to_markdown_with_config(blocks, &Config::default())
}

/// Render code blocks (search results) as markdown string with configuration
pub fn code_blocks_to_markdown_with_config(blocks: &[CodeBlock], config: &Config) -> String {
	let mut markdown = String::new();

	if blocks.is_empty() {
		markdown.push_str("No code blocks found for the query.");
		return markdown;
	}

	markdown.push_str(&format!("# Found {} code blocks\n\n", blocks.len()));

	// Group blocks by file path for better organization
	let mut blocks_by_file: std::collections::HashMap<String, Vec<&CodeBlock>> =
		std::collections::HashMap::new();

	for block in blocks {
		blocks_by_file
			.entry(block.path.clone())
			.or_default()
			.push(block);
	}

	// Print results organized by file
	for (file_path, file_blocks) in blocks_by_file.iter() {
		markdown.push_str(&format!("## File: {}\n\n", file_path));

		for (idx, block) in file_blocks.iter().enumerate() {
			markdown.push_str(&format!("### Block {} of {}\n", idx + 1, file_blocks.len()));
			markdown.push_str(&format!("**Language:** {}  ", block.language));
			markdown.push_str(&format!(
				"**Lines:** {}-{}  ",
				block.start_line, block.end_line
			));

			// Show similarity score if available
			if let Some(distance) = block.distance {
				markdown.push_str(&format!("**Similarity:** {:.4}  ", 1.0 - distance));
			}
			markdown.push('\n');

			if !block.symbols.is_empty() {
				markdown.push_str("**Symbols:**  \n");
				// Deduplicate symbols in display
				let mut display_symbols = block.symbols.clone();
				display_symbols.sort();
				display_symbols.dedup();

				for symbol in display_symbols {
					// Only show non-type symbols to users
					if !symbol.contains("_") {
						markdown.push_str(&format!("- `{}`  \n", symbol));
					}
				}
			}

			markdown.push_str("```");
			// Add language for syntax highlighting
			if !block.language.is_empty() && block.language != "text" {
				markdown.push_str(&block.language);
			}
			markdown.push('\n');

			// Use smart truncation based on configuration
			let max_chars = config.search.search_block_max_characters;
			let (content, was_truncated) = truncate_content_smartly(&block.content, max_chars);

			markdown.push_str(&content);
			if !content.ends_with('\n') {
				markdown.push('\n');
			}

			// Add note if content was truncated
			if was_truncated {
				markdown.push_str(&format!(
					"// Content truncated (limit: {} chars)\n",
					max_chars
				));
			}

			markdown.push_str("```\n\n");
		}

		markdown.push_str("---\n\n");
	}

	markdown
}

/// Render text blocks (text search results) as markdown string
pub fn text_blocks_to_markdown(blocks: &[TextBlock]) -> String {
	text_blocks_to_markdown_with_config(blocks, &Config::default())
}

/// Render text blocks (text search results) as markdown string with configuration
pub fn text_blocks_to_markdown_with_config(blocks: &[TextBlock], config: &Config) -> String {
	let mut markdown = String::new();

	if blocks.is_empty() {
		markdown.push_str("No text blocks found for the query.");
		return markdown;
	}

	markdown.push_str(&format!("# Found {} text blocks\n\n", blocks.len()));

	// Group blocks by file path for better organization
	let mut blocks_by_file: std::collections::HashMap<String, Vec<&TextBlock>> =
		std::collections::HashMap::new();

	for block in blocks {
		blocks_by_file
			.entry(block.path.clone())
			.or_default()
			.push(block);
	}

	// Print results organized by file
	for (file_path, file_blocks) in blocks_by_file.iter() {
		markdown.push_str(&format!("## File: {}\n\n", file_path));

		for (idx, block) in file_blocks.iter().enumerate() {
			markdown.push_str(&format!("### Block {} of {}\n", idx + 1, file_blocks.len()));
			markdown.push_str(&format!("**Language:** {}  ", block.language));
			markdown.push_str(&format!(
				"**Lines:** {}-{}  ",
				block.start_line, block.end_line
			));

			// Show relevance score if available
			if let Some(distance) = block.distance {
				markdown.push_str(&format!("**Relevance:** {:.4}  ", 1.0 - distance));
			}
			markdown.push_str("\n\n");

			// Use smart truncation based on configuration
			let max_chars = config.search.search_block_max_characters;
			let (content, was_truncated) = truncate_content_smartly(&block.content, max_chars);

			markdown.push_str(&content);
			if !content.ends_with('\n') {
				markdown.push('\n');
			}

			// Add note if content was truncated
			if was_truncated {
				markdown.push_str(&format!(
					"\n*Content truncated (limit: {} chars)*\n",
					max_chars
				));
			}

			markdown.push('\n');
		}

		markdown.push_str("---\n\n");
	}

	markdown
}

/// Render document blocks (documentation search results) as markdown string
pub fn document_blocks_to_markdown(blocks: &[DocumentBlock]) -> String {
	document_blocks_to_markdown_with_config(blocks, &Config::default())
}

/// Render document blocks (documentation search results) as markdown string with configuration
pub fn document_blocks_to_markdown_with_config(
	blocks: &[DocumentBlock],
	config: &Config,
) -> String {
	let mut markdown = String::new();

	if blocks.is_empty() {
		markdown.push_str("No documentation found for the query.");
		return markdown;
	}

	markdown.push_str(&format!(
		"# Found {} documentation sections\n\n",
		blocks.len()
	));

	// Group blocks by file path for better organization
	let mut blocks_by_file: std::collections::HashMap<String, Vec<&DocumentBlock>> =
		std::collections::HashMap::new();

	for block in blocks {
		blocks_by_file
			.entry(block.path.clone())
			.or_default()
			.push(block);
	}

	// Print results organized by file
	for (file_path, file_blocks) in blocks_by_file.iter() {
		markdown.push_str(&format!("## File: {}\n\n", file_path));

		for (idx, block) in file_blocks.iter().enumerate() {
			markdown.push_str(&format!(
				"### {} (Section {} of {})\n",
				block.title,
				idx + 1,
				file_blocks.len()
			));
			markdown.push_str(&format!("**Level:** {}  ", block.level));
			markdown.push_str(&format!(
				"**Lines:** {}-{}  ",
				block.start_line, block.end_line
			));

			// Show relevance score if available
			if let Some(distance) = block.distance {
				markdown.push_str(&format!("**Relevance:** {:.4}  ", 1.0 - distance));
			}
			markdown.push_str("\n\n");

			// Use smart truncation based on configuration
			let max_chars = config.search.search_block_max_characters;
			let (content, was_truncated) = truncate_content_smartly(&block.content, max_chars);

			markdown.push_str(&content);
			if !content.ends_with('\n') {
				markdown.push('\n');
			}

			// Add note if content was truncated
			if was_truncated {
				markdown.push_str(&format!(
					"\n*Content truncated (limit: {} chars)*\n",
					max_chars
				));
			}

			markdown.push('\n');
		}

		markdown.push_str("---\n\n");
	}

	markdown
}

/// Render signatures as CLI output with box drawing
pub fn render_signatures_cli(signatures: &[FileSignature]) {
	if signatures.is_empty() {
		println!("No signatures found.");
		return;
	}

	println!("Found signatures in {} files:\n", signatures.len());

	for file in signatures {
		println!("╔══════════════════ File: {} ══════════════════", file.path);
		println!("║ Language: {}", file.language);

		// Show file comment if available
		if let Some(comment) = &file.file_comment {
			println!("");
			println!("║ File description:");
			for line in comment.lines() {
				println!("{}", line);
			}
		}

		if file.signatures.is_empty() {
			println!("");
			println!("║ No signatures found in this file.");
		} else {
			for signature in &file.signatures {
				println!("");

				// Display line range if it spans multiple lines, otherwise just the start line
				let line_display = if signature.start_line == signature.end_line {
					format!("{}", signature.start_line)
				} else {
					format!("{}-{}", signature.start_line, signature.end_line)
				};

				println!(
					"{} `{}` (line {})",
					signature.kind, signature.name, line_display
				);

				// Show description if available
				if let Some(desc) = &signature.description {
					println!("║ Description:");
					for line in desc.lines() {
						println!("{}", line);
					}
				}

				// Format the signature for display
				println!("║ Signature:");
				let lines = signature.signature.lines().collect::<Vec<_>>();
				if lines.len() > 1 {
					println!("║ ┌────────────────────────────────────");
					if lines.len() > 5 {
						// Show first 2 lines with line numbers
						for (i, line) in lines.iter().take(2).enumerate() {
							let line_num = signature.start_line + i;
							println!("║ │ {}: {}", line_num, line);
						}
						// Show truncation message
						println!("║ │ // ... {} more lines", lines.len() - 4);
						// Show last 2 lines with line numbers
						for (i, line) in lines.iter().skip(lines.len() - 2).enumerate() {
							let line_num = signature.start_line + (lines.len() - 2) + i;
							println!("║ │ {}: {}", line_num, line);
						}
					} else {
						// Show all lines with line numbers for short signatures
						for (i, line) in lines.iter().enumerate() {
							let line_num = signature.start_line + i;
							println!("║ │ {}: {}", line_num, line);
						}
					}
					println!("║ └────────────────────────────────────");
				} else if !lines.is_empty() {
					let line_num = signature.start_line;
					println!("{}: {}", line_num, lines[0]);
				}
			}
		}

		println!("╚════════════════════════════════════════\n");
	}
}

/// Render signatures as JSON
pub fn render_signatures_json(signatures: &[FileSignature]) -> Result<()> {
	let json = serde_json::to_string_pretty(signatures)?;
	println!("{}", json);
	Ok(())
}