File size: 8,396 Bytes
e7f31f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51bf1cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7f31f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51bf1cd
e7f31f9
 
 
 
 
 
 
 
51bf1cd
 
e7f31f9
 
 
 
 
 
51bf1cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7f31f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51bf1cd
 
 
e7f31f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from enum import Enum
from typing import Optional

from schemas import CodeXRequest


class ScopeRoute(str, Enum):
    CODE = "code"
    GREETING = "greeting"
    NON_CODE = "non_code"
    IMAGE = "image"
    RESTRICTED = "restricted"
    UNKNOWN = "unknown"


class ScopeDecision:
    def __init__(
        self,
        route: ScopeRoute,
        message: str,
        should_continue_to_codex: bool,
    ):
        self.route = route
        self.message = message
        self.should_continue_to_codex = should_continue_to_codex


GREETING_PATTERNS = [
    r"\bhi\b",
    r"\bhello\b",
    r"\bhey\b",
    r"\bgood morning\b",
    r"\bgood afternoon\b",
    r"\bgood evening\b",
    r"\bhow are you\b",
    r"\bwho are you\b",
    r"\bwhat can you do\b",
]

CODE_PATTERNS = [
    r"\bcode\b",
    r"\bfunction\b",
    r"\bclass\b",
    r"\bmethod\b",
    r"\bvariable\b",
    r"\bbug\b",
    r"\berror\b",
    r"\bexception\b",
    r"\bfix\b",
    r"\brefactor\b",
    r"\breview\b",
    r"\boptimize\b",
    r"\bdebug\b",
    r"\bpython\b",
    r"\bjava\b",
    r"\bjavascript\b",
    r"\btypescript\b",
    r"\bflutter\b",
    r"\bdart\b",
    r"\breact\b",
    r"\bnode\b",
    r"\bapi\b",
    r"\bsql\b",
    r"\bhtml\b",
    r"\bcss\b",
    r"\bjson\b",
    r"\bregex\b",
    r"\balgorithm\b",
    r"\bcompile\b",
    r"\bruntime\b",
    r"\bsyntax\b",
    r"\bprogram\b",
    r"\bscript\b",
    r"\bsoftware\b",
    r"\bbackend\b",
    r"\bfrontend\b",
    r"\bendpoint\b",
    r"\bdatabase\b",
    r"\bquery\b",
    r"\btraceback\b",
    r"\bdocstring\b",
    r"\btype hints?\b",
    r"\bvalidation\b",
    r"\berror handling\b",
    r"\blogging\b",
    r"\bcomments\b",
    r"\bexception handling\b",
]

CODE_MODIFICATION_PATTERNS = [
    r"\badd\b",
    r"\bupdate\b",
    r"\bmodify\b",
    r"\bchange\b",
    r"\brename\b",
    r"\bconvert\b",
    r"\bextend\b",
    r"\binclude\b",
    r"\buse\b",
    r"\breplace\b",
    r"\bremove\b",
    r"\bdelete\b",
    r"\bdocstring\b",
    r"\btype hints?\b",
    r"\bvalidation\b",
    r"\berror handling\b",
    r"\blogging\b",
    r"\bcomments\b",
    r"\bexception handling\b",
    r"\bmake it\b",
    r"\bturn this into\b",
    r"\badd support for\b",
    r"\bnow add\b",
    r"\bnow update\b",
    r"\bnow change\b",
]

IMAGE_PATTERNS = [
    r"\bgenerate an image\b",
    r"\bcreate an image\b",
    r"\bdraw\b",
    r"\bposter\b",
    r"\blogo\b",
    r"\bbanner\b",
    r"\bphoto\b",
    r"\bpicture\b",
    r"\bimage\b",
    r"\bdescribe this image\b",
    r"\bwhat is in this image\b",
    r"\bedit this image\b",
]

NON_CODE_PATTERNS = [
    r"\bweather\b",
    r"\bpresident\b",
    r"\bprime minister\b",
    r"\bnews\b",
    r"\bgold price\b",
    r"\bstock\b",
    r"\brecipe\b",
    r"\btravel\b",
    r"\bhotel\b",
    r"\brestaurant\b",
    r"\bmedical\b",
    r"\blegal\b",
    r"\bmovie\b",
    r"\bsong\b",
    r"\btranslate\b",
    r"\bessay\b",
    r"\bpoem\b",
]

RESTRICTED_PATTERNS = [
    r"\bmalware\b",
    r"\bransomware\b",
    r"\bkeylogger\b",
    r"\bsteal passwords\b",
    r"\bphishing\b",
    r"\btoken grabber\b",
    r"\bremote access trojan\b",
    r"\brat\b",
    r"\bcredential stuffing\b",
    r"\bexploit\b",
    r"\bbypass authentication\b",
    r"\bddos\b",
    r"\bbruteforce\b",
    r"\bsql injection\b",
    r"\bxss exploit\b",
    r"\bvirus\b",
    r"\bspyware\b",
]


def normalize_text(text: Optional[str]) -> str:
    if not text:
        return ""
    text = str(text).strip().lower()
    text = re.sub(r"\s+", " ", text)
    return text


def contains_pattern(text: str, patterns: list[str]) -> bool:
    return any(re.search(pattern, text) for pattern in patterns)


def looks_like_code_block(text: str) -> bool:
    if not text:
        return False

    code_signals = [
        "def ",
        "class ",
        "return ",
        "import ",
        "from ",
        "{",
        "}",
        ";",
        "=>",
        "public ",
        "private ",
        "protected ",
        "const ",
        "let ",
        "var ",
        "function ",
        "if (",
        "for (",
        "while (",
        "</",
        "try:",
        "except:",
    ]

    lowered = text.lower()
    return any(signal.lower() in lowered for signal in code_signals)


def is_follow_up_code_request(request: CodeXRequest) -> bool:
    message = normalize_text(request.message)

    has_session = bool(request.session_id and str(request.session_id).strip())
    has_previous_context = bool(request.previous_context and str(request.previous_context).strip())

    if not (has_session or has_previous_context):
        return False

    if contains_pattern(message, CODE_MODIFICATION_PATTERNS):
        return True

    short_follow_up_phrases = [
        "now add",
        "now update",
        "now change",
        "now remove",
        "now use",
        "add this",
        "update this",
        "change this",
        "fix this",
        "refactor this",
        "review this",
    ]

    return any(phrase in message for phrase in short_follow_up_phrases)


def is_code_related_request(request: CodeXRequest) -> bool:
    message = normalize_text(request.message)
    code = request.code or ""
    error_message = normalize_text(request.error_message)

    if request.code and request.code.strip():
        return True

    if error_message:
        return True

    if looks_like_code_block(request.message):
        return True

    if looks_like_code_block(code):
        return True

    if contains_pattern(message, CODE_PATTERNS):
        return True

    if is_follow_up_code_request(request):
        return True

    return False


def is_greeting_request(request: CodeXRequest) -> bool:
    message = normalize_text(request.message)

    if not message:
        return False

    if contains_pattern(message, GREETING_PATTERNS):
        if not is_code_related_request(request):
            return True

    return False


def is_image_request(request: CodeXRequest) -> bool:
    message = normalize_text(request.message)
    return contains_pattern(message, IMAGE_PATTERNS)


def is_non_code_request(request: CodeXRequest) -> bool:
    message = normalize_text(request.message)

    if is_code_related_request(request):
        return False

    if contains_pattern(message, NON_CODE_PATTERNS):
        return True

    return False


def is_restricted_request(request: CodeXRequest) -> bool:
    message = normalize_text(request.message)
    code = normalize_text(request.code)
    error_message = normalize_text(request.error_message)

    combined = " ".join(part for part in [message, code, error_message] if part)
    return contains_pattern(combined, RESTRICTED_PATTERNS)


def decide_scope(request: CodeXRequest) -> ScopeDecision:
    if is_restricted_request(request):
        return ScopeDecision(
            route=ScopeRoute.RESTRICTED,
            message="Code X cannot help with harmful, abusive, or clearly unsafe coding requests.",
            should_continue_to_codex=False,
        )

    if is_image_request(request):
        return ScopeDecision(
            route=ScopeRoute.IMAGE,
            message="Code X is focused on coding tasks. Image generation, image editing, and picture analysis should be handled by the image workflow instead.",
            should_continue_to_codex=False,
        )

    if is_greeting_request(request):
        return ScopeDecision(
            route=ScopeRoute.GREETING,
            message="Hello. Code X is ready for coding tasks. Send code, an error, or a software request and I will help.",
            should_continue_to_codex=False,
        )

    if is_non_code_request(request):
        return ScopeDecision(
            route=ScopeRoute.NON_CODE,
            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.",
            should_continue_to_codex=False,
        )

    if is_code_related_request(request):
        return ScopeDecision(
            route=ScopeRoute.CODE,
            message="Code-related request detected.",
            should_continue_to_codex=True,
        )

    return ScopeDecision(
        route=ScopeRoute.UNKNOWN,
        message="This request is not clearly a coding task. Send code, an error, or a software development request for Code X.",
        should_continue_to_codex=False,
    )