hashan-7 commited on
Commit
e7f31f9
·
verified ·
1 Parent(s): 118a5b0

Add the code

Browse files
Files changed (1) hide show
  1. scope_router.py +277 -0
scope_router.py ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from enum import Enum
3
+ from typing import Optional
4
+
5
+ from schemas import CodeXRequest
6
+
7
+
8
+ class ScopeRoute(str, Enum):
9
+ CODE = "code"
10
+ GREETING = "greeting"
11
+ NON_CODE = "non_code"
12
+ IMAGE = "image"
13
+ RESTRICTED = "restricted"
14
+ UNKNOWN = "unknown"
15
+
16
+
17
+ class ScopeDecision:
18
+ def __init__(
19
+ self,
20
+ route: ScopeRoute,
21
+ message: str,
22
+ should_continue_to_codex: bool,
23
+ ):
24
+ self.route = route
25
+ self.message = message
26
+ self.should_continue_to_codex = should_continue_to_codex
27
+
28
+
29
+ GREETING_PATTERNS = [
30
+ r"\bhi\b",
31
+ r"\bhello\b",
32
+ r"\bhey\b",
33
+ r"\bgood morning\b",
34
+ r"\bgood afternoon\b",
35
+ r"\bgood evening\b",
36
+ r"\bhow are you\b",
37
+ r"\bwho are you\b",
38
+ r"\bwhat can you do\b",
39
+ ]
40
+
41
+ CODE_PATTERNS = [
42
+ r"\bcode\b",
43
+ r"\bfunction\b",
44
+ r"\bclass\b",
45
+ r"\bmethod\b",
46
+ r"\bvariable\b",
47
+ r"\bbug\b",
48
+ r"\berror\b",
49
+ r"\bexception\b",
50
+ r"\bfix\b",
51
+ r"\brefactor\b",
52
+ r"\breview\b",
53
+ r"\boptimize\b",
54
+ r"\bdebug\b",
55
+ r"\bpython\b",
56
+ r"\bjava\b",
57
+ r"\bjavascript\b",
58
+ r"\btypescript\b",
59
+ r"\bflutter\b",
60
+ r"\bdart\b",
61
+ r"\breact\b",
62
+ r"\bnode\b",
63
+ r"\bapi\b",
64
+ r"\bsql\b",
65
+ r"\bhtml\b",
66
+ r"\bcss\b",
67
+ r"\bjson\b",
68
+ r"\bregex\b",
69
+ r"\balgorithm\b",
70
+ r"\bcompile\b",
71
+ r"\bruntime\b",
72
+ r"\bsyntax\b",
73
+ r"\bprogram\b",
74
+ r"\bscript\b",
75
+ ]
76
+
77
+ IMAGE_PATTERNS = [
78
+ r"\bgenerate an image\b",
79
+ r"\bcreate an image\b",
80
+ r"\bdraw\b",
81
+ r"\bposter\b",
82
+ r"\blogo\b",
83
+ r"\bbanner\b",
84
+ r"\bphoto\b",
85
+ r"\bpicture\b",
86
+ r"\bimage\b",
87
+ r"\bdescribe this image\b",
88
+ r"\bwhat is in this image\b",
89
+ r"\bedit this image\b",
90
+ ]
91
+
92
+ NON_CODE_PATTERNS = [
93
+ r"\bweather\b",
94
+ r"\bpresident\b",
95
+ r"\bprime minister\b",
96
+ r"\bnews\b",
97
+ r"\bgold price\b",
98
+ r"\bstock\b",
99
+ r"\brecipe\b",
100
+ r"\btravel\b",
101
+ r"\bhotel\b",
102
+ r"\brestaurant\b",
103
+ r"\bmedical\b",
104
+ r"\blegal\b",
105
+ r"\bmovie\b",
106
+ r"\bsong\b",
107
+ r"\btranslate\b",
108
+ r"\bessay\b",
109
+ r"\bpoem\b",
110
+ ]
111
+
112
+ RESTRICTED_PATTERNS = [
113
+ r"\bmalware\b",
114
+ r"\bransomware\b",
115
+ r"\bkeylogger\b",
116
+ r"\bsteal passwords\b",
117
+ r"\bphishing\b",
118
+ r"\btoken grabber\b",
119
+ r"\bremote access trojan\b",
120
+ r"\brat\b",
121
+ r"\bcredential stuffing\b",
122
+ r"\bexploit\b",
123
+ r"\bbypass authentication\b",
124
+ r"\bddos\b",
125
+ r"\bbruteforce\b",
126
+ r"\bsql injection\b",
127
+ r"\bxss exploit\b",
128
+ r"\bvirus\b",
129
+ r"\bspyware\b",
130
+ ]
131
+
132
+
133
+ def normalize_text(text: Optional[str]) -> str:
134
+ if not text:
135
+ return ""
136
+ text = str(text).strip().lower()
137
+ text = re.sub(r"\s+", " ", text)
138
+ return text
139
+
140
+
141
+ def contains_pattern(text: str, patterns: list[str]) -> bool:
142
+ return any(re.search(pattern, text) for pattern in patterns)
143
+
144
+
145
+ def looks_like_code_block(text: str) -> bool:
146
+ if not text:
147
+ return False
148
+
149
+ code_signals = [
150
+ "def ",
151
+ "class ",
152
+ "return ",
153
+ "import ",
154
+ "from ",
155
+ "{",
156
+ "}",
157
+ ";",
158
+ "=>",
159
+ "public ",
160
+ "private ",
161
+ "const ",
162
+ "let ",
163
+ "var ",
164
+ "function ",
165
+ "if (",
166
+ "for (",
167
+ "while (",
168
+ "</",
169
+ ]
170
+
171
+ lowered = text.lower()
172
+ return any(signal.lower() in lowered for signal in code_signals)
173
+
174
+
175
+ def is_code_related_request(request: CodeXRequest) -> bool:
176
+ message = normalize_text(request.message)
177
+ code = request.code or ""
178
+ error_message = normalize_text(request.error_message)
179
+
180
+ if request.code and request.code.strip():
181
+ return True
182
+
183
+ if error_message:
184
+ return True
185
+
186
+ if looks_like_code_block(request.message):
187
+ return True
188
+
189
+ if looks_like_code_block(code):
190
+ return True
191
+
192
+ if contains_pattern(message, CODE_PATTERNS):
193
+ return True
194
+
195
+ return False
196
+
197
+
198
+ def is_greeting_request(request: CodeXRequest) -> bool:
199
+ message = normalize_text(request.message)
200
+
201
+ if not message:
202
+ return False
203
+
204
+ if contains_pattern(message, GREETING_PATTERNS):
205
+ if not is_code_related_request(request):
206
+ return True
207
+
208
+ return False
209
+
210
+
211
+ def is_image_request(request: CodeXRequest) -> bool:
212
+ message = normalize_text(request.message)
213
+ return contains_pattern(message, IMAGE_PATTERNS)
214
+
215
+
216
+ def is_non_code_request(request: CodeXRequest) -> bool:
217
+ message = normalize_text(request.message)
218
+
219
+ if is_code_related_request(request):
220
+ return False
221
+
222
+ if contains_pattern(message, NON_CODE_PATTERNS):
223
+ return True
224
+
225
+ return False
226
+
227
+
228
+ def is_restricted_request(request: CodeXRequest) -> bool:
229
+ message = normalize_text(request.message)
230
+ code = normalize_text(request.code)
231
+ error_message = normalize_text(request.error_message)
232
+
233
+ combined = " ".join(part for part in [message, code, error_message] if part)
234
+ return contains_pattern(combined, RESTRICTED_PATTERNS)
235
+
236
+
237
+ def decide_scope(request: CodeXRequest) -> ScopeDecision:
238
+ if is_restricted_request(request):
239
+ return ScopeDecision(
240
+ route=ScopeRoute.RESTRICTED,
241
+ message="Code X cannot help with harmful, abusive, or clearly unsafe coding requests.",
242
+ should_continue_to_codex=False,
243
+ )
244
+
245
+ if is_image_request(request):
246
+ return ScopeDecision(
247
+ route=ScopeRoute.IMAGE,
248
+ message="Code X is focused on coding tasks. Image generation, image editing, and picture analysis should be handled by the image workflow instead.",
249
+ should_continue_to_codex=False,
250
+ )
251
+
252
+ if is_greeting_request(request):
253
+ return ScopeDecision(
254
+ route=ScopeRoute.GREETING,
255
+ message="Hello. Code X is ready for coding tasks. Send code, an error, or a software request and I will help.",
256
+ should_continue_to_codex=False,
257
+ )
258
+
259
+ if is_non_code_request(request):
260
+ return ScopeDecision(
261
+ route=ScopeRoute.NON_CODE,
262
+ message="Code X is focused on software and coding tasks. This request looks outside Code X scope, so it should go to the normal chat assistant.",
263
+ should_continue_to_codex=False,
264
+ )
265
+
266
+ if is_code_related_request(request):
267
+ return ScopeDecision(
268
+ route=ScopeRoute.CODE,
269
+ message="Code-related request detected.",
270
+ should_continue_to_codex=True,
271
+ )
272
+
273
+ return ScopeDecision(
274
+ route=ScopeRoute.UNKNOWN,
275
+ message="This request is not clearly a coding task. Send code, an error, or a software development request for Code X.",
276
+ should_continue_to_codex=False,
277
+ )