-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathmessage_extractor.py
402 lines (332 loc) · 13.9 KB
/
message_extractor.py
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
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Dict, List, Optional, Self
import regex as re
import structlog
from pydantic import BaseModel, field_validator, model_validator
from pygments.lexers import guess_lexer
logger = structlog.get_logger("codegate")
CODE_BLOCK_PATTERN = re.compile(
r"```" # Opening backticks, no whitespace after backticks and before language
r"(?:(?P<language>[a-zA-Z0-9_+-]+)\s+)?" # Language must be followed by whitespace if present
r"(?:(?P<filename>[^\s\(\n]+))?" # Optional filename (cannot contain spaces or parentheses)
r"(?:\s+\([0-9]+-[0-9]+\))?" # Optional line numbers in parentheses
r"\s*\n" # Required newline after metadata
r"(?P<content>.*?)" # Content (non-greedy match)
r"```", # Closing backticks
re.DOTALL,
)
CODE_BLOCK_WITH_FILENAME_PATTERN = re.compile(
r"```" # Opening backticks, no whitespace after backticks and before language
r"(?:(?P<language>[a-zA-Z0-9_+-]+)\s+)?" # Language must be followed by whitespace if present
r"(?P<filename>[^\s\(\n]+)" # Mandatory filename (cannot contain spaces or parentheses)
r"(?:\s+\([0-9]+-[0-9]+\))?" # Optional line numbers in parentheses
r"\s*\n" # Required newline after metadata
r"(?P<content>.*?)" # Content (non-greedy match)
r"```", # Closing backticks
re.DOTALL,
)
CLINE_FILE_CONTENT_PATTERN = re.compile(
r"<file_content\s+path=\"(?P<filename>[^\"]+)\">" # Match the opening tag with mandatory file
r"(?P<content>.*?)" # Match the content (non-greedy)
r"</file_content>", # Match the closing tag
re.DOTALL,
)
AIDER_SUMMARIES_CONTENT_PATTERN = re.compile(
r"^(?P<filename>[^\n]+):\n" # Match the filepath as the header
r"(?P<content>.*?)" # Match the content (non-greedy)
r"⋮...\n\n", # Match the ending pattern with dots
re.DOTALL | re.MULTILINE,
)
AIDER_FILE_CONTENT_PATTERN = re.compile(
r"^(?P<filename>[^\n]+)\n" # Match the filepath as the header
r"```" # Match the opening triple backticks
r"(?P<content>.*?)" # Match the content (non-greedy)
r"```", # Match the closing triple backticks
re.DOTALL | re.MULTILINE,
)
OPEN_INTERPRETER_CONTENT_PATTERN = re.compile(
r"# Attempting to read the content of `(?P<filename>[^`]+)`" # Match the filename backticks
r".*?" # Match any characters non-greedily
r"File read successfully\.\n" # Match the "File read successfully." text
r"'(?P<content>.*?)'", # Match the content wrapped in single quotes
re.DOTALL,
)
OPEN_INTERPRETER_Y_CONTENT_PATTERN = re.compile(
r"# Open and read the contents of the (?P<filename>[^\s]+) file" # Match the filename
r".*?" # Match any characters non-greedily
r"\n\n" # Match the double line break
r"(?P<content>.*)", # Match everything that comes after the double line break
re.DOTALL,
)
KODU_CONTENT_PATTERN = re.compile(
r"<file\s+path=\"(?P<filename>[^\n>]+)\">" # Match the opening tag with path attribute
r"(?P<content>.*?)" # Match the content (non-greedy)
r"</file>", # Match the closing tag
re.DOTALL,
)
class MatchedPatternSnippet(BaseModel):
"""
Represents a match from the code snippet patterns.
Meant to be used by all CodeSnippetExtractors.
"""
language: Optional[str]
filename: Optional[str]
content: str
class CodeSnippet(BaseModel):
"""
Represents a code snippet with its programming language.
Args:
language: The programming language identifier (e.g., 'python', 'javascript')
code: The actual code content
"""
code: str
language: Optional[str]
filepath: Optional[str]
libraries: List[str] = []
file_extension: Optional[str] = None
@field_validator("language", mode="after")
@classmethod
def ensure_lowercase(cls, value: str) -> str:
if value is not None:
value = value.strip().lower()
return value
@model_validator(mode="after")
def fill_file_extension(self) -> Self:
if self.filepath is not None:
self.file_extension = Path(self.filepath).suffix
return self
def __hash__(self):
# Create a hashable representation using immutable fields
return hash(
(self.code, self.language, self.filepath, self.file_extension, tuple(self.libraries))
)
def __eq__(self, other):
if not isinstance(other, CodeSnippet):
return False
return (
self.code == other.code
and self.language == other.language
and self.filepath == other.filepath
and self.file_extension == other.file_extension
and self.libraries == other.libraries
)
class CodeSnippetExtractor(ABC):
def __init__(self):
self._extension_mapping = {
".py": "python",
".js": "javascript",
".ts": "typescript",
".tsx": "typescript",
".go": "go",
".rs": "rust",
".java": "java",
}
self._language_mapping = {
"py": "python",
"js": "javascript",
"ts": "typescript",
"tsx": "typescript",
"go": "go",
"rs": "rust",
"java": "java",
}
self._available_languages = [
"sh",
"bash",
"python",
"javascript",
"typescript",
"go",
"rust",
"java",
] # noqa: E501
@property
@abstractmethod
def codeblock_pattern(self) -> List[re.Pattern]:
"""
List of regex patterns to match code blocks without filenames.
"""
pass
@property
@abstractmethod
def codeblock_with_filename_pattern(self) -> List[re.Pattern]:
"""
List of regex patterns to match code blocks with filenames.
"""
pass
@abstractmethod
def _get_match_pattern_snippet(self, match: re.Match) -> MatchedPatternSnippet:
pass
def _choose_regex(self, require_filepath: bool) -> List[re.Pattern]:
if require_filepath:
return self.codeblock_with_filename_pattern
else:
return self.codeblock_pattern
def _ecosystem_from_filepath(self, filepath: str):
"""
Determine language from filepath.
Args:
filepath: Path to the file
Returns:
Determined language based on file extension
"""
# Get the file extension
path_filename = Path(filepath)
file_extension = path_filename.suffix.lower()
return self._extension_mapping.get(file_extension, None)
def _ecosystem_from_message(self, message: str):
"""
Determine language from message.
Args:
message: The language from the message. Some extensions send a different
format where the language is present in the snippet,
e.g. "py /path/to/file (lineFrom-lineTo)"
Returns:
Determined language based on message content
"""
return self._language_mapping.get(message, message)
def _get_snippet_for_match(self, match: re.Match) -> CodeSnippet:
matched_snippet = self._get_match_pattern_snippet(match)
# If we have a single word without extension after the backticks,
# it's a language identifier, not a filename. Typicaly used in the
# format ` ```python ` in output snippets
if (
matched_snippet.filename
and not matched_snippet.language
and "." not in matched_snippet.filename
):
lang = matched_snippet.filename
if lang not in self._available_languages:
# try to get it from the extension
lang = self._ecosystem_from_message(matched_snippet.filename)
if lang not in self._available_languages:
lang = None
matched_snippet.filename = None
else:
# Determine language from the message, either by the short
# language identifier or by the filename
lang = None
if matched_snippet.language:
lang = self._ecosystem_from_message(matched_snippet.language.strip())
if lang is None and matched_snippet.filename:
matched_snippet.filename = matched_snippet.filename.strip()
# Determine language from the filename
lang = self._ecosystem_from_filepath(matched_snippet.filename)
if lang is None:
# try to guess it from the code
lexer = guess_lexer(matched_snippet.content)
if lexer and lexer.name:
lang = lexer.name.lower()
# only add available languages
if lang not in self._available_languages:
lang = None
# just correct the typescript exception
lang_map = {"typescript": "javascript"}
if lang:
lang = lang_map.get(lang, lang)
return CodeSnippet(
filepath=matched_snippet.filename, code=matched_snippet.content, language=lang
)
def extract_snippets(self, message: str, require_filepath: bool = False) -> List[CodeSnippet]:
"""
Extract code snippets from a message.
Args:
message: Input text containing code snippets
Returns:
List of extracted code snippets
"""
regexes = self._choose_regex(require_filepath)
# Find all code block matches
if isinstance(message, str):
return [
self._get_snippet_for_match(match)
for regex in regexes
for match in regex.finditer(message)
]
return [
self._get_snippet_for_match(match)
for regex in regexes
for match in regex.finditer(message.get_text())
]
def extract_unique_snippets(self, message: str) -> Dict[str, CodeSnippet]:
"""
Extract unique filpaths from a message. Uses the filepath as key.
Args:
message: Input text containing code snippets
Returns:
Dictionary of unique code snippets with the filepath as key
"""
regexes = self._choose_regex(require_filepath=True)
unique_snippets: Dict[str, CodeSnippet] = {}
for regex in regexes:
for match in regex.finditer(message):
snippet = self._get_snippet_for_match(match)
filename = Path(snippet.filepath).name if snippet.filepath else None
if filename and filename not in unique_snippets:
unique_snippets[filename] = snippet
return unique_snippets
class DefaultCodeSnippetExtractor(CodeSnippetExtractor):
@property
def codeblock_pattern(self) -> re.Pattern:
return [CODE_BLOCK_PATTERN]
@property
def codeblock_with_filename_pattern(self) -> re.Pattern:
return [CODE_BLOCK_WITH_FILENAME_PATTERN]
def _get_match_pattern_snippet(self, match: re.Match) -> MatchedPatternSnippet:
matched_language = match.group("language") if match.group("language") else None
filename = match.group("filename") if match.group("filename") else None
content = match.group("content")
return MatchedPatternSnippet(language=matched_language, filename=filename, content=content)
class ClineCodeSnippetExtractor(CodeSnippetExtractor):
@property
def codeblock_pattern(self) -> re.Pattern:
return [CLINE_FILE_CONTENT_PATTERN]
@property
def codeblock_with_filename_pattern(self) -> re.Pattern:
return [CLINE_FILE_CONTENT_PATTERN]
def _get_match_pattern_snippet(self, match: re.Match) -> MatchedPatternSnippet:
# We don't have language in the cline pattern
matched_language = None
filename = match.group("filename")
content = match.group("content")
return MatchedPatternSnippet(language=matched_language, filename=filename, content=content)
class AiderCodeSnippetExtractor(CodeSnippetExtractor):
@property
def codeblock_pattern(self) -> re.Pattern:
return [AIDER_SUMMARIES_CONTENT_PATTERN, AIDER_FILE_CONTENT_PATTERN]
@property
def codeblock_with_filename_pattern(self) -> re.Pattern:
return [AIDER_SUMMARIES_CONTENT_PATTERN, AIDER_FILE_CONTENT_PATTERN]
def _get_match_pattern_snippet(self, match: re.Match) -> MatchedPatternSnippet:
# We don't have language in the cline pattern
matched_language = None
filename = match.group("filename")
content = match.group("content")
return MatchedPatternSnippet(language=matched_language, filename=filename, content=content)
class OpenInterpreterCodeSnippetExtractor(CodeSnippetExtractor):
@property
def codeblock_pattern(self) -> re.Pattern:
return [OPEN_INTERPRETER_CONTENT_PATTERN, OPEN_INTERPRETER_Y_CONTENT_PATTERN]
@property
def codeblock_with_filename_pattern(self) -> re.Pattern:
return [OPEN_INTERPRETER_CONTENT_PATTERN, OPEN_INTERPRETER_Y_CONTENT_PATTERN]
def _get_match_pattern_snippet(self, match: re.Match) -> MatchedPatternSnippet:
# We don't have language in the cline pattern
matched_language = None
filename = match.group("filename")
content = match.group("content")
return MatchedPatternSnippet(language=matched_language, filename=filename, content=content)
class KoduCodeSnippetExtractor(CodeSnippetExtractor):
@property
def codeblock_pattern(self) -> re.Pattern:
return [KODU_CONTENT_PATTERN]
@property
def codeblock_with_filename_pattern(self) -> re.Pattern:
return [KODU_CONTENT_PATTERN]
def _get_match_pattern_snippet(self, match: re.Match) -> MatchedPatternSnippet:
# We don't have language in the cline pattern
matched_language = None
filename = match.group("filename")
content = match.group("content")
return MatchedPatternSnippet(language=matched_language, filename=filename, content=content)