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
Expand file tree
/
Copy pathinterface.py
More file actions
61 lines (43 loc) · 1.91 KB
/
interface.py
File metadata and controls
61 lines (43 loc) · 1.91 KB
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
import re
from abc import ABC, abstractmethod
from typing import Dict, Type
from codegate.clients.clients import ClientType
class ClientInterface(ABC):
"""Secure interface for client-specific message processing"""
@abstractmethod
def strip_code_snippets(self, message: str) -> str:
"""Remove code blocks and file listings to prevent context pollution"""
pass
class GenericClient(ClientInterface):
"""Default implementation with strict input validation"""
_MARKDOWN_CODE_REGEX = re.compile(r"```.*?```", re.DOTALL)
_MARKDOWN_FILE_LISTING = re.compile(r"⋮...*?⋮...\n\n", flags=re.DOTALL)
_ENVIRONMENT_DETAILS = re.compile(
r"<environment_details>.*?</environment_details>", flags=re.DOTALL
)
_CLI_REGEX = re.compile(r"^codegate\s+(.*)$", re.IGNORECASE)
def strip_code_snippets(self, message: str) -> str:
message = self._MARKDOWN_CODE_REGEX.sub("", message)
message = self._MARKDOWN_FILE_LISTING.sub("", message)
message = self._ENVIRONMENT_DETAILS.sub("", message)
return message
class ClineClient(ClientInterface):
"""Cline-specific client interface"""
_CLINE_FILE_REGEX = re.compile(
r"(?i)<\s*file_content\s*[^>]*>.*?</\s*file_content\s*>", re.DOTALL
)
def __init__(self):
self.generic_client = GenericClient()
def strip_code_snippets(self, message: str) -> str:
message = self.generic_client.strip_code_snippets(message)
return self._CLINE_FILE_REGEX.sub("", message)
class ClientFactory:
"""Secure factory with updated client mappings"""
_implementations: Dict[ClientType, Type[ClientInterface]] = {
ClientType.GENERIC: GenericClient,
ClientType.CLINE: ClineClient,
ClientType.KODU: ClineClient,
}
@classmethod
def create(cls, client_type: ClientType) -> ClientInterface:
return cls._implementations.get(client_type, GenericClient)()