|
| 1 | +from abc import ABC, abstractmethod |
| 2 | +from typing import List, Optional |
| 3 | + |
| 4 | +from codegate.extract_snippets.message_extractor import ( |
| 5 | + AiderCodeSnippetExtractor, |
| 6 | + ClineCodeSnippetExtractor, |
| 7 | + CodeSnippetExtractor, |
| 8 | + DefaultCodeSnippetExtractor, |
| 9 | + OpenInterpreterCodeSnippetExtractor, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class BodyCodeSnippetExtractorError(Exception): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +class BodyCodeSnippetExtractor(ABC): |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + # Initialize the extractor in parent class. The child classes will set the extractor. |
| 21 | + self._snippet_extractor: Optional[CodeSnippetExtractor] = None |
| 22 | + |
| 23 | + def _extract_from_user_messages(self, data: dict) -> set[str]: |
| 24 | + """ |
| 25 | + The method extracts the code snippets from the user messages in the data got from the |
| 26 | + clients. |
| 27 | +
|
| 28 | + It returns a set of filenames extracted from the code snippets. |
| 29 | + """ |
| 30 | + if self._snippet_extractor is None: |
| 31 | + raise BodyCodeSnippetExtractorError("Code Extractor not set.") |
| 32 | + |
| 33 | + filenames: List[str] = [] |
| 34 | + for msg in data.get("messages", []): |
| 35 | + if msg.get("role", "") == "user": |
| 36 | + extracted_snippets = self._snippet_extractor.extract_unique_snippets( |
| 37 | + msg.get("content") |
| 38 | + ) |
| 39 | + filenames.extend(extracted_snippets.keys()) |
| 40 | + return set(filenames) |
| 41 | + |
| 42 | + @abstractmethod |
| 43 | + def extract_unique_filenames(self, data: dict) -> set[str]: |
| 44 | + """ |
| 45 | + Extract the unique filenames from the data received by the clients (Cline, Continue, ...) |
| 46 | + """ |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | +class ContinueBodySnippetExtractor(BodyCodeSnippetExtractor): |
| 51 | + |
| 52 | + def __init__(self): |
| 53 | + self._snippet_extractor = DefaultCodeSnippetExtractor() |
| 54 | + |
| 55 | + def extract_unique_filenames(self, data: dict) -> set[str]: |
| 56 | + return self._extract_from_user_messages(data) |
| 57 | + |
| 58 | + |
| 59 | +class AiderBodySnippetExtractor(BodyCodeSnippetExtractor): |
| 60 | + |
| 61 | + def __init__(self): |
| 62 | + self._snippet_extractor = AiderCodeSnippetExtractor() |
| 63 | + |
| 64 | + def extract_unique_filenames(self, data: dict) -> set[str]: |
| 65 | + return self._extract_from_user_messages(data) |
| 66 | + |
| 67 | + |
| 68 | +class ClineBodySnippetExtractor(BodyCodeSnippetExtractor): |
| 69 | + |
| 70 | + def __init__(self): |
| 71 | + self._snippet_extractor = ClineCodeSnippetExtractor() |
| 72 | + |
| 73 | + def extract_unique_filenames(self, data: dict) -> set[str]: |
| 74 | + return self._extract_from_user_messages(data) |
| 75 | + |
| 76 | + |
| 77 | +class OpenInterpreterBodySnippetExtractor(BodyCodeSnippetExtractor): |
| 78 | + |
| 79 | + def __init__(self): |
| 80 | + self._snippet_extractor = OpenInterpreterCodeSnippetExtractor() |
| 81 | + |
| 82 | + def _is_msg_tool_call(self, msg: dict) -> bool: |
| 83 | + return msg.get("role", "") == "assistant" and msg.get("tool_calls", []) |
| 84 | + |
| 85 | + def _is_msg_tool_result(self, msg: dict) -> bool: |
| 86 | + return msg.get("role", "") == "tool" and msg.get("content", "") |
| 87 | + |
| 88 | + def _extract_args_from_tool_call(self, msg: dict) -> str: |
| 89 | + """ |
| 90 | + Extract the arguments from the tool call message. |
| 91 | + """ |
| 92 | + tool_calls = msg.get("tool_calls", []) |
| 93 | + if not tool_calls: |
| 94 | + return "" |
| 95 | + return tool_calls[0].get("function", {}).get("arguments", "") |
| 96 | + |
| 97 | + def _extract_result_from_tool_result(self, msg: dict) -> str: |
| 98 | + """ |
| 99 | + Extract the result from the tool result message. |
| 100 | + """ |
| 101 | + return msg.get("content", "") |
| 102 | + |
| 103 | + def extract_unique_filenames(self, data: dict) -> set[str]: |
| 104 | + messages = data.get("messages", []) |
| 105 | + if not messages: |
| 106 | + return set() |
| 107 | + |
| 108 | + filenames: List[str] = [] |
| 109 | + for i_msg in range(len(messages) - 1): |
| 110 | + msg = messages[i_msg] |
| 111 | + next_msg = messages[i_msg + 1] |
| 112 | + if self._is_msg_tool_call(msg) and self._is_msg_tool_result(next_msg): |
| 113 | + tool_args = self._extract_args_from_tool_call(msg) |
| 114 | + tool_response = self._extract_result_from_tool_result(next_msg) |
| 115 | + extracted_snippets = self._snippet_extractor.extract_unique_snippets( |
| 116 | + f"{tool_args}\n{tool_response}" |
| 117 | + ) |
| 118 | + filenames.extend(extracted_snippets.keys()) |
| 119 | + return set(filenames) |
0 commit comments