Skip to content

Interactive

Interactive LLM interface for CodeMap.

RagUI

Interactive UI for the RAG process.

Source code in src/codemap/llm/rag/interactive.py
 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
class RagUI:
	"""Interactive UI for the RAG process."""

	def __init__(self) -> None:
		"""Initialize the RAG UI."""
		self.console = Console()

	def format_ask_response(self, response_text: str | None) -> Markdown:
		"""
		Formats the AI's response text using Rich Markdown.

		Args:
			response_text (Optional[str]): The text response from the AI.

		Returns:
			Markdown: A Rich Markdown object ready for printing.

		"""
		if response_text is None:
			response_text = "*No response generated.*"
		# Basic Markdown formatting. Can be enhanced later to detect code blocks,
		# file paths, etc., and apply specific styling or links.
		return Markdown(response_text)

	def print_ask_result(self, result: dict[str, Any]) -> None:
		"""
		Prints the structured result of the ask command using Rich.

		Args:
			result (Dict[str, Any]): The structured result containing 'answer' and 'context'.

		"""
		answer = result.get("answer")
		context = result.get("context", [])

		# Print the main answer
		rich_print(Panel(self.format_ask_response(answer), title="[bold green]Answer[/]", border_style="green"))

		# Print the context used if there are any items
		if context:
			# Build a single string with all context items as a numbered list
			context_list = []
			for i, item in enumerate(context, 1):
				file_path = item.get("file_path", "Unknown")
				start_line = item.get("start_line", -1)
				end_line = item.get("end_line", -1)
				distance = item.get("distance", -1.0)

				# Create the list item text
				location = f"{file_path}"
				if start_line > 0 and end_line > 0:
					location += f" (lines {start_line}-{end_line})"

				# Format with relevance info
				relevance = f"(similarity: {1 - distance:.2f})" if distance >= 0 else ""
				list_item = f"[bold cyan]{i}.[/bold cyan] {location} [dim]{relevance}[/dim]"
				context_list.append(list_item)

			# Join all list items into a single string
			context_content = "\n".join(context_list)

			# Print a single panel with all context items
			rich_print(
				Panel(
					context_content, title="[bold yellow]Context Used[/]", border_style="yellow", title_align="center"
				)
			)

			rich_print()

	def format_content_for_context(self, context_items: list[dict[str, Any]]) -> str:
		"""
		Format context items into a string suitable for inclusion in prompts.

		Args:
			context_items: List of context dictionaries with file_path, start_line, end_line, and content

		Returns:
			Formatted string with code snippets and file information

		"""
		if not context_items:
			return "No relevant code found in the repository."

		formatted_parts = []

		for i, item in enumerate(context_items, 1):
			# Extract file information
			file_path = item.get("file_path", "Unknown file")
			start_line = item.get("start_line", -1)
			end_line = item.get("end_line", -1)
			content = item.get("content", "")

			# Create a header with file info
			header = f"[{i}] {file_path}"
			if start_line > 0 and end_line > 0:
				header += f" (lines {start_line}-{end_line})"

			# Format the code snippet with the header
			formatted_parts.append(f"{header}\n{'-' * len(header)}\n{content}\n")

		return "\n\n".join(formatted_parts)

__init__

__init__() -> None

Initialize the RAG UI.

Source code in src/codemap/llm/rag/interactive.py
16
17
18
def __init__(self) -> None:
	"""Initialize the RAG UI."""
	self.console = Console()

console instance-attribute

console = Console()

format_ask_response

format_ask_response(response_text: str | None) -> Markdown

Formats the AI's response text using Rich Markdown.

Parameters:

Name Type Description Default
response_text Optional[str]

The text response from the AI.

required

Returns:

Name Type Description
Markdown Markdown

A Rich Markdown object ready for printing.

Source code in src/codemap/llm/rag/interactive.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def format_ask_response(self, response_text: str | None) -> Markdown:
	"""
	Formats the AI's response text using Rich Markdown.

	Args:
		response_text (Optional[str]): The text response from the AI.

	Returns:
		Markdown: A Rich Markdown object ready for printing.

	"""
	if response_text is None:
		response_text = "*No response generated.*"
	# Basic Markdown formatting. Can be enhanced later to detect code blocks,
	# file paths, etc., and apply specific styling or links.
	return Markdown(response_text)

print_ask_result

print_ask_result(result: dict[str, Any]) -> None

Prints the structured result of the ask command using Rich.

Parameters:

Name Type Description Default
result Dict[str, Any]

The structured result containing 'answer' and 'context'.

required
Source code in src/codemap/llm/rag/interactive.py
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
def print_ask_result(self, result: dict[str, Any]) -> None:
	"""
	Prints the structured result of the ask command using Rich.

	Args:
		result (Dict[str, Any]): The structured result containing 'answer' and 'context'.

	"""
	answer = result.get("answer")
	context = result.get("context", [])

	# Print the main answer
	rich_print(Panel(self.format_ask_response(answer), title="[bold green]Answer[/]", border_style="green"))

	# Print the context used if there are any items
	if context:
		# Build a single string with all context items as a numbered list
		context_list = []
		for i, item in enumerate(context, 1):
			file_path = item.get("file_path", "Unknown")
			start_line = item.get("start_line", -1)
			end_line = item.get("end_line", -1)
			distance = item.get("distance", -1.0)

			# Create the list item text
			location = f"{file_path}"
			if start_line > 0 and end_line > 0:
				location += f" (lines {start_line}-{end_line})"

			# Format with relevance info
			relevance = f"(similarity: {1 - distance:.2f})" if distance >= 0 else ""
			list_item = f"[bold cyan]{i}.[/bold cyan] {location} [dim]{relevance}[/dim]"
			context_list.append(list_item)

		# Join all list items into a single string
		context_content = "\n".join(context_list)

		# Print a single panel with all context items
		rich_print(
			Panel(
				context_content, title="[bold yellow]Context Used[/]", border_style="yellow", title_align="center"
			)
		)

		rich_print()

format_content_for_context

format_content_for_context(
	context_items: list[dict[str, Any]],
) -> str

Format context items into a string suitable for inclusion in prompts.

Parameters:

Name Type Description Default
context_items list[dict[str, Any]]

List of context dictionaries with file_path, start_line, end_line, and content

required

Returns:

Type Description
str

Formatted string with code snippets and file information

Source code in src/codemap/llm/rag/interactive.py
 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
def format_content_for_context(self, context_items: list[dict[str, Any]]) -> str:
	"""
	Format context items into a string suitable for inclusion in prompts.

	Args:
		context_items: List of context dictionaries with file_path, start_line, end_line, and content

	Returns:
		Formatted string with code snippets and file information

	"""
	if not context_items:
		return "No relevant code found in the repository."

	formatted_parts = []

	for i, item in enumerate(context_items, 1):
		# Extract file information
		file_path = item.get("file_path", "Unknown file")
		start_line = item.get("start_line", -1)
		end_line = item.get("end_line", -1)
		content = item.get("content", "")

		# Create a header with file info
		header = f"[{i}] {file_path}"
		if start_line > 0 and end_line > 0:
			header += f" (lines {start_line}-{end_line})"

		# Format the code snippet with the header
		formatted_parts.append(f"{header}\n{'-' * len(header)}\n{content}\n")

	return "\n\n".join(formatted_parts)