This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 91
Extended the CodeSnippetExtractor functionality for muxing #945
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import List, Optional | ||
|
||
from codegate.extract_snippets.message_extractor import ( | ||
AiderCodeSnippetExtractor, | ||
ClineCodeSnippetExtractor, | ||
CodeSnippetExtractor, | ||
DefaultCodeSnippetExtractor, | ||
OpenInterpreterCodeSnippetExtractor, | ||
) | ||
|
||
|
||
class BodyCodeSnippetExtractorError(Exception): | ||
pass | ||
|
||
|
||
class BodyCodeSnippetExtractor(ABC): | ||
|
||
def __init__(self): | ||
# Initialize the extractor in parent class. The child classes will set the extractor. | ||
self._snippet_extractor: Optional[CodeSnippetExtractor] = None | ||
|
||
def _extract_from_user_messages(self, data: dict) -> set[str]: | ||
""" | ||
The method extracts the code snippets from the user messages in the data got from the | ||
clients. | ||
|
||
It returns a set of filenames extracted from the code snippets. | ||
""" | ||
if self._snippet_extractor is None: | ||
raise BodyCodeSnippetExtractorError("Code Extractor not set.") | ||
|
||
filenames: List[str] = [] | ||
for msg in data.get("messages", []): | ||
if msg.get("role", "") == "user": | ||
extracted_snippets = self._snippet_extractor.extract_unique_snippets( | ||
msg.get("content") | ||
) | ||
filenames.extend(extracted_snippets.keys()) | ||
return set(filenames) | ||
|
||
@abstractmethod | ||
def extract_unique_filenames(self, data: dict) -> set[str]: | ||
""" | ||
Extract the unique filenames from the data received by the clients (Cline, Continue, ...) | ||
""" | ||
pass | ||
|
||
|
||
class ContinueBodySnippetExtractor(BodyCodeSnippetExtractor): | ||
|
||
def __init__(self): | ||
self._snippet_extractor = DefaultCodeSnippetExtractor() | ||
|
||
def extract_unique_filenames(self, data: dict) -> set[str]: | ||
return self._extract_from_user_messages(data) | ||
|
||
|
||
class AiderBodySnippetExtractor(BodyCodeSnippetExtractor): | ||
|
||
def __init__(self): | ||
self._snippet_extractor = AiderCodeSnippetExtractor() | ||
|
||
def extract_unique_filenames(self, data: dict) -> set[str]: | ||
return self._extract_from_user_messages(data) | ||
|
||
|
||
class ClineBodySnippetExtractor(BodyCodeSnippetExtractor): | ||
|
||
def __init__(self): | ||
self._snippet_extractor = ClineCodeSnippetExtractor() | ||
|
||
def extract_unique_filenames(self, data: dict) -> set[str]: | ||
return self._extract_from_user_messages(data) | ||
|
||
|
||
class OpenInterpreterBodySnippetExtractor(BodyCodeSnippetExtractor): | ||
|
||
def __init__(self): | ||
self._snippet_extractor = OpenInterpreterCodeSnippetExtractor() | ||
|
||
def _is_msg_tool_call(self, msg: dict) -> bool: | ||
return msg.get("role", "") == "assistant" and msg.get("tool_calls", []) | ||
|
||
def _is_msg_tool_result(self, msg: dict) -> bool: | ||
return msg.get("role", "") == "tool" and msg.get("content", "") | ||
|
||
def _extract_args_from_tool_call(self, msg: dict) -> str: | ||
""" | ||
Extract the arguments from the tool call message. | ||
""" | ||
tool_calls = msg.get("tool_calls", []) | ||
if not tool_calls: | ||
return "" | ||
return tool_calls[0].get("function", {}).get("arguments", "") | ||
|
||
def _extract_result_from_tool_result(self, msg: dict) -> str: | ||
""" | ||
Extract the result from the tool result message. | ||
""" | ||
return msg.get("content", "") | ||
|
||
def extract_unique_filenames(self, data: dict) -> set[str]: | ||
messages = data.get("messages", []) | ||
if not messages: | ||
return set() | ||
|
||
filenames: List[str] = [] | ||
for i_msg in range(len(messages) - 1): | ||
msg = messages[i_msg] | ||
next_msg = messages[i_msg + 1] | ||
if self._is_msg_tool_call(msg) and self._is_msg_tool_result(next_msg): | ||
tool_args = self._extract_args_from_tool_call(msg) | ||
tool_response = self._extract_result_from_tool_result(next_msg) | ||
extracted_snippets = self._snippet_extractor.extract_unique_snippets( | ||
f"{tool_args}\n{tool_response}" | ||
) | ||
filenames.extend(extracted_snippets.keys()) | ||
return set(filenames) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from codegate.clients.clients import ClientType | ||
from codegate.extract_snippets.body_extractor import ( | ||
AiderBodySnippetExtractor, | ||
BodyCodeSnippetExtractor, | ||
ClineBodySnippetExtractor, | ||
ContinueBodySnippetExtractor, | ||
OpenInterpreterBodySnippetExtractor, | ||
) | ||
from codegate.extract_snippets.message_extractor import ( | ||
AiderCodeSnippetExtractor, | ||
ClineCodeSnippetExtractor, | ||
CodeSnippetExtractor, | ||
DefaultCodeSnippetExtractor, | ||
OpenInterpreterCodeSnippetExtractor, | ||
) | ||
|
||
|
||
class BodyCodeExtractorFactory: | ||
|
||
@staticmethod | ||
def create_snippet_extractor(detected_client: ClientType) -> BodyCodeSnippetExtractor: | ||
mapping_client_extractor = { | ||
ClientType.GENERIC: ContinueBodySnippetExtractor(), | ||
ClientType.CLINE: ClineBodySnippetExtractor(), | ||
ClientType.AIDER: AiderBodySnippetExtractor(), | ||
ClientType.OPEN_INTERPRETER: OpenInterpreterBodySnippetExtractor(), | ||
} | ||
return mapping_client_extractor.get(detected_client, ContinueBodySnippetExtractor()) | ||
|
||
|
||
class MessageCodeExtractorFactory: | ||
|
||
@staticmethod | ||
def create_snippet_extractor(detected_client: ClientType) -> CodeSnippetExtractor: | ||
mapping_client_extractor = { | ||
ClientType.GENERIC: DefaultCodeSnippetExtractor(), | ||
ClientType.CLINE: ClineCodeSnippetExtractor(), | ||
ClientType.AIDER: AiderCodeSnippetExtractor(), | ||
ClientType.OPEN_INTERPRETER: OpenInterpreterCodeSnippetExtractor(), | ||
} | ||
return mapping_client_extractor.get(detected_client, DefaultCodeSnippetExtractor()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.