Context Processor
Context processing utilities for LLM prompts.
This module provides functionality to process and format code contexts for LLM prompts using tree-sitter analysis and Level of Detail (LOD) to optimize context length while preserving meaningful content.
logger
module-attribute
logger = getLogger(__name__)
DEFAULT_MAX_TOKENS
module-attribute
DEFAULT_MAX_TOKENS = 4000
CHUNK_TOKEN_ESTIMATE
module-attribute
CHUNK_TOKEN_ESTIMATE = 500
MAX_CHUNKS
module-attribute
MAX_CHUNKS = 6
MAX_SIMPLE_CHUNKS
module-attribute
MAX_SIMPLE_CHUNKS = 3
process_chunks_with_lod
process_chunks_with_lod(
chunks: list[DiffChunk],
max_tokens: int = DEFAULT_MAX_TOKENS,
) -> str
Process diff chunks using LOD to create optimized context for LLM prompts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunks
|
list[DiffChunk]
|
List of diff chunks to process |
required |
max_tokens
|
int
|
Maximum tokens allowed in the formatted context |
DEFAULT_MAX_TOKENS
|
Returns:
Type | Description |
---|---|
str
|
Formatted markdown context optimized for token usage |
Source code in src/codemap/git/semantic_grouping/context_processor.py
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 |
|
prioritize_chunks
Prioritize chunks based on heuristics (file types, changes, etc.).
This is a simple implementation that could be extended with more sophisticated dissimilarity metrics.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunks
|
list[DiffChunk]
|
List of chunks to prioritize |
required |
max_count
|
int
|
Maximum number of chunks to return |
required |
Returns:
Type | Description |
---|---|
list[DiffChunk]
|
Prioritized list of chunks |
Source code in src/codemap/git/semantic_grouping/context_processor.py
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 |
|
get_file_paths_from_chunk
get_file_paths_from_chunk(chunk: DiffChunk) -> list[str]
Extract file paths from a diff chunk.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunk
|
DiffChunk
|
The diff chunk to process |
required |
Returns:
Type | Description |
---|---|
list[str]
|
List of file paths |
Source code in src/codemap/git/semantic_grouping/context_processor.py
153 154 155 156 157 158 159 160 161 162 163 164 |
|
format_lod_entity
Format an LOD entity as GitHub-flavored markdown.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity
|
LODEntity
|
The LOD entity to format |
required |
file_path
|
str
|
Path to the source file |
required |
level
|
LODLevel
|
LOD level used |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted markdown string |
Source code in src/codemap/git/semantic_grouping/context_processor.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
format_entity_structure
format_entity_structure(
entity: LODEntity, indent: int
) -> str
Format entity with structure (signatures and hierarchy).
Source code in src/codemap/git/semantic_grouping/context_processor.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
format_entity_signatures
format_entity_signatures(
entity: LODEntity, indent: int
) -> str
Format entity with just signatures.
Source code in src/codemap/git/semantic_grouping/context_processor.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
format_regular_chunks
format_regular_chunks(chunks: list[DiffChunk]) -> str
Format chunks using the regular approach when LOD is not necessary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunks
|
list[DiffChunk]
|
List of chunks to format |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted markdown string |
Source code in src/codemap/git/semantic_grouping/context_processor.py
224 225 226 227 228 229 230 231 232 233 234 235 236 |
|
format_chunk
format_chunk(chunk: DiffChunk) -> str
Format a single diff chunk as markdown.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
chunk
|
DiffChunk
|
The diff chunk to format |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted markdown string |
Source code in src/codemap/git/semantic_grouping/context_processor.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
estimate_tokens
estimate_tokens(text: str) -> int
Estimate the number of tokens in a text.
This is a simple estimation that can be improved with actual tokenizer implementations if needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text
|
str
|
Text to estimate tokens for |
required |
Returns:
Type | Description |
---|---|
int
|
Estimated token count |
Source code in src/codemap/git/semantic_grouping/context_processor.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
|
truncate_context
truncate_context(context: str, max_tokens: int) -> str
Truncate context to fit within token limit.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
context
|
str
|
Context to truncate |
required |
max_tokens
|
int
|
Maximum allowed tokens |
required |
Returns:
Type | Description |
---|---|
str
|
Truncated context |
Source code in src/codemap/git/semantic_grouping/context_processor.py
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 |
|