-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_parser.py
More file actions
573 lines (496 loc) · 21.1 KB
/
Copy pathaction_parser.py
File metadata and controls
573 lines (496 loc) · 21.1 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#!/usr/bin/env python3
"""
Multi-level robust tool call parser for NEMESIS CLI.
Parsing strategy (6 niveaux, tries in order):
1. YAML-style named code fences : ```tool_name\\nkey: value\\n```
2. Strict JSON in code fence : ```json\\n{ "tool": "...", ... }\\n```
3. Relaxed JSON (repaired) : same, but auto-fixes common LLM errors
4. JSON inline (sans fence) : ```...\\n{ "tool": ..., ... }\\n```
5. XML-style action tags : <ACTION type="...">...</ACTION>
6. Regex fallback : détection par motifs textuels
Avantages par rapport à l'ancien système (``_parse_tool_json``) :
- Le format YAML gère naturellement le contenu multi-lignes (``content: |``)
- Les guillemets et apostrophes dans le contenu ne cassent plus le parsing
- La détection XML couvre l'ancien format de l'agent
- Les réparations JSON progressives sont plus agressives
"""
import re
import json
from typing import Dict, Any, Optional, List, Tuple, Set
class ActionParser:
"""Parseur multi-niveaux d'appels d'outils."""
# Outils valides — synchronisé avec VALID_TOOLS dans agent.py
VALID_TOOLS: Set[str] = {
"read_file", "search_replace", "write_file", "bash",
"get_task_output", "kill_task", "list_dir", "grep",
"web_search", "web_fetch", "delete_file",
"mcp_list", "mcp_tools_list", "mcp_call",
# Anciens noms (legacy tools_schema.py)
"write", "read", "replace", "append",
"validate", "status", "kill_process", "stop_all",
"cleanup_logs", "list_agents", "delegate_task",
"check_reports", "update_tracker", "skills_list",
}
# Mapping anciens noms -> nouveaux noms
LEGACY_MAP: Dict[str, str] = {
"write": "write_file",
"read": "read_file",
"replace": "search_replace",
}
def __init__(self, extra_valid_tools: Optional[Set[str]] = None):
if extra_valid_tools:
self.VALID_TOOLS = self.VALID_TOOLS | extra_valid_tools
# ------------------------------------------------------------------
# Point d'entrée principal
# ------------------------------------------------------------------
def parse(self, raw_response: str) -> Dict[str, Any]:
"""Parse une réponse brute du LLM et extrait l'outil éventuel.
Retourne:
{'text': str, 'action': None|{'type': str, 'content': dict}}
"""
result: Dict[str, Any] = {'text': raw_response or '', 'action': None}
if not raw_response or not isinstance(raw_response, str):
return result
if raw_response.startswith("FEEDBACK:"):
result['text'] = raw_response
return result
# --- Level 1: YAML-style named code fence ---
action = self._parse_yaml_fence(raw_response)
if action:
return self._split_text_and_action(raw_response, action, r'```\w*\n.*?\n```', re.S)
# --- Level 2: JSON strict in code fence ---
action = self._parse_json_fence(raw_response, strict=True)
if action:
return self._split_text_and_action(raw_response, action, r'```(?:json|JSON)?\s*\n.*?\n?\s*```', re.S)
# --- Level 3: JSON relaxed (repaired) ---
action = self._parse_json_fence(raw_response, strict=False)
if action:
return self._split_text_and_action(raw_response, action, r'```(?:json|JSON)?\s*\n.*?\n?\s*```', re.S)
# --- Level 4: JSON inline sans fence (```{...}```) ---
action = self._parse_bare_code_block(raw_response)
if action:
return self._split_text_and_action(raw_response, action, r'```\s*\n?\{.*?\}\n?\s*```', re.S)
# --- Level 5: XML-style action tags ---
action = self._parse_xml_action(raw_response)
if action:
text = re.sub(r'<ACTION[^>]*>.*?</ACTION>', '', raw_response, flags=re.S).strip()
result['text'] = text or ""
result['action'] = action
return result
# --- Level 6: Regex fallback ---
action = self._parse_text_fallback(raw_response)
if action:
result['action'] = action
return result
# Aucune action detectée
if not result['text']:
result['text'] = ""
return result
# ------------------------------------------------------------------
# Level 1: YAML-style named code fence
# ------------------------------------------------------------------
def _parse_yaml_fence(self, text: str) -> Optional[Dict[str, Any]]:
"""Détecte ```tool_name\\nclé: valeur\\n``` et extrait les paramètres.
Supporte le bloc scalaire YAML | (literal block) pour le contenu
multi-lignes, ce qui résout le problème d'échappement JSON.
"""
# Patterns acceptés :
# ```write_file ```write
# file_path: test.py path: test.py
# content: | content: |
# line1 line1
# line2 line2
# ``` ```
pattern = re.compile(
r'```(\w+)\s*\n' # ouverture ```tool_name
r'(.*?)' # paramètres (non greedy)
r'\n\s*```' # fermeture ```
,
re.S
)
for match in pattern.finditer(text):
tool_name = match.group(1).strip()
yaml_block = match.group(2).strip()
if tool_name not in self.VALID_TOOLS:
continue
params = self._parse_yaml_block(yaml_block)
if params is not None:
tool_name = self.LEGACY_MAP.get(tool_name, tool_name)
return {'type': tool_name, 'content': params}
return None
def _parse_yaml_block(self, block: str) -> Optional[Dict[str, Any]]:
"""Parse un bloc YAML simple avec support des scalaires littéraux |.
Format:
key1: value1
key2: |
line1
line2
key3: value3
"""
params: Dict[str, Any] = {}
lines = block.split('\n')
i = 0
while i < len(lines):
line = lines[i]
if not line.strip():
i += 1
continue
# Détecter une clé: valeur
match = re.match(r'^(\w+):\s*(.*)', line)
if not match:
i += 1
continue
key = match.group(1)
value_part = match.group(2).strip()
# Détecter le bloc scalaire littéral |
if value_part == '|':
# Collecter toutes les lignes indentées qui suivent
multiline: List[str] = []
i += 1
while i < len(lines) and (lines[i].startswith(' ') or lines[i].startswith('\t') or not lines[i].strip()):
if lines[i].strip():
# Enlever 2 espaces d'indentation du bloc YAML
stripped = lines[i][2:] if lines[i].startswith(' ') else lines[i].lstrip()
multiline.append(stripped)
else:
multiline.append('')
i += 1
params[key] = '\n'.join(multiline)
continue
# Détecter le bloc scalaile > (folded)
elif value_part == '>':
multiline = []
i += 1
while i < len(lines) and (lines[i].startswith(' ') or lines[i].startswith('\t') or not lines[i].strip()):
if lines[i].strip():
stripped = lines[i][2:] if lines[i].startswith(' ') else lines[i].lstrip()
multiline.append(stripped)
else:
multiline.append('')
i += 1
# Remplacer les nouvelles lignes par des espaces (sauf lignes vides = paragraphe)
result = ''
for ml in multiline:
if ml == '':
result += '\n\n'
else:
result += ml + ' '
params[key] = result.strip()
continue
# Valeur simple ou liste
if value_part.startswith('[') and value_part.endswith(']'):
# Liste simple: [a, b, c]
items = [x.strip().strip('"').strip("'") for x in value_part[1:-1].split(',')]
params[key] = items
elif value_part.lower() == 'true':
params[key] = True
elif value_part.lower() == 'false':
params[key] = False
elif value_part.isdigit():
params[key] = int(value_part)
else:
# String simple — enlever les guillemets si présents
params[key] = value_part.strip('"').strip("'")
i += 1
return params if params else None
# ------------------------------------------------------------------
# Level 2 & 3: JSON in code fence (strict + relaxed)
# ------------------------------------------------------------------
def _parse_json_fence(self, text: str, strict: bool = True) -> Optional[Dict[str, Any]]:
"""Détecte ```json\\n{...}\\n``` et parse le JSON."""
# Pattern: ```json ... ``` ou ```JSON ... ``` ou ``` ... ```
patterns = [
r'```(?:json|JSON)\s*\n(.*?)```',
r'```\s*\n(.*?)```',
]
for pat in patterns:
for match in re.finditer(pat, text, re.S):
json_str = match.group(1).strip()
if not json_str.startswith('{'):
continue
action = self._parse_json(json_str, strict)
if action:
return action
return None
def _parse_json(self, raw: str, strict: bool = True) -> Optional[Dict[str, Any]]:
"""Parse un objet JSON représentant un appel d'outil."""
json_str = raw.strip()
if not json_str.startswith('{') or not json_str.endswith('}'):
return None
if strict:
# Tentative unique avec json.loads()
try:
obj = json.loads(json_str)
except (json.JSONDecodeError, ValueError):
return None
return self._validate_tool_obj(obj)
# Mode relaxé : tentatives progressives de réparation
for attempt in range(6):
candidate = json_str
if attempt > 0:
candidate = self._repair_json(json_str, attempt - 1)
try:
obj = json.loads(candidate)
except (json.JSONDecodeError, ValueError):
continue
result = self._validate_tool_obj(obj)
if result:
return result
return None
def _repair_json(self, s: str, attempt: int) -> str:
"""Réparations progressives d'un JSON invalide produit par un LLM."""
if attempt == 0:
# Étape 0: newlines et tabulations dans les chaînes
return self._fix_json_newlines(s)
elif attempt == 1:
# Étape 1: commentaires // et /* */, virgules traînantes
cleaned = re.sub(r'/\*.*?\*/', '', s, flags=re.S)
cleaned = re.sub(r'//[^\n]*', '', cleaned)
cleaned = re.sub(r',\s*}', '}', cleaned)
cleaned = re.sub(r',\s*]', ']', cleaned)
return cleaned
elif attempt == 2:
# Étape 2: clés sans guillemets
return re.sub(
r'([{,]\s*)([A-Za-z_][A-Za-z0-9_]*)(\s*:)',
r'\1"\2"\3',
s,
)
elif attempt == 3:
# Étape 3: quotes simples -> doubles quotes
return self._fix_single_quotes(s)
elif attempt == 4:
# Étape 4: JSON tronqué - fermer les accolades/crochets manquants
return self._fix_truncated_json(s)
elif attempt == 5:
# Étape 5: dernier recours - tenter de trouver n'importe quel JSON valide
# en extrayant la plus grande sous-chaîne parsable
return self._extract_largest_json(s)
return s
def _fix_json_newlines(self, s: str) -> str:
"""Échappe les newlines / tabulations réels à l'intérieur des chaînes JSON."""
result = []
in_string = False
escape_next = False
for ch in s:
if escape_next:
result.append(ch)
escape_next = False
continue
if ch == '\\':
result.append(ch)
escape_next = True
continue
if ch == '"':
in_string = not in_string
result.append(ch)
continue
if in_string and ch == '\n':
result.append('\\n')
continue
if in_string and ch == '\r':
result.append('\\r')
continue
if in_string and ch == '\t':
result.append('\\t')
continue
result.append(ch)
return ''.join(result)
def _fix_single_quotes(self, s: str) -> str:
"""Remplace les quotes simples par des doubles quotes dans un JSON.
Attention: ne remplace que les quotes simples qui sont clairement
des délimiteurs de chaîne, pas les apostrophes dans le texte.
"""
# Stratégie: remplacer 'key': par "key": et : 'value' par : "value"
# Utiliser des regex ciblées plutôt qu'un remplacement global
s = re.sub(r"'\s*:", '":', s)
s = re.sub(r":\s*'", ': "', s)
# Gérer les virgules: 'value', -> "value",
s = re.sub(r"'\s*,", '",', s)
s = re.sub(r",\s*'", ', "', s)
# Début/fin d'objet: {' -> {" et '} -> "}
s = re.sub(r"\{\s*'", '{"', s)
s = re.sub(r"'\s*\}", '"}', s)
s = re.sub(r"\[\s*'", '["', s)
s = re.sub(r"'\s*\]", '"]', s)
return s
def _fix_truncated_json(self, s: str) -> str:
"""Ajoute les crochets/accolades fermants manquants."""
depth = 0
in_string = False
escape_next = False
for ch in s:
if escape_next:
escape_next = False
continue
if ch == '\\':
escape_next = True
continue
if ch == '"':
in_string = not in_string
continue
if in_string:
continue
if ch in '{[':
depth += 1
elif ch in '}]':
depth -= 1
return s + ('}' if depth > 0 else '') * max(depth, 0)
def _extract_largest_json(self, s: str) -> str:
"""Trouve la plus grande sous-chaîne parsable comme JSON.
Utile quand le LLM ajoute du texte après le JSON valide.
"""
# Chercher le premier { et tout prendre jusqu'à la dernière }
# correspondante
start = s.find('{')
if start < 0:
return s
depth = 0
end = start
in_string = False
escape_next = False
for i in range(start, len(s)):
ch = s[i]
if escape_next:
escape_next = False
continue
if ch == '\\':
escape_next = True
continue
if ch == '"':
in_string = not in_string
continue
if in_string:
continue
if ch in '{[':
depth += 1
elif ch in '}]':
depth -= 1
if depth == 0:
end = i + 1
break
if end > start:
# Fermer si tronqué
candidate = s[start:end]
return self._fix_truncated_json(candidate)
return s
def _validate_tool_obj(self, obj: Any) -> Optional[Dict[str, Any]]:
"""Valide qu'un objet parsé est un appel d'outil NEMESIS valide."""
if not isinstance(obj, dict):
return None
tool_name = (
obj.get("tool")
or obj.get("name")
or obj.get("action")
or ""
)
if not tool_name or not isinstance(tool_name, str):
return None
if tool_name not in self.VALID_TOOLS:
return None
tool_name = self.LEGACY_MAP.get(tool_name, tool_name)
params = (
obj.get("parameters")
or obj.get("arguments")
or obj.get("params")
or {}
)
if not isinstance(params, dict):
params = {"value": params}
return {'type': tool_name, 'content': params}
# ------------------------------------------------------------------
# Level 4: Bare code block (```{...}```)
# ------------------------------------------------------------------
def _parse_bare_code_block(self, text: str) -> Optional[Dict[str, Any]]:
"""Détecte ```{...}``` sans marqueur json."""
pattern = re.compile(r'```\s*\n?(\{.*?\})\n?\s*```', re.S)
for match in pattern.finditer(text):
block = match.group(1).strip()
if block.startswith('{') and block.endswith('}'):
action = self._parse_json(block, strict=False)
if action:
return action
return None
# ------------------------------------------------------------------
# Level 5: XML-style action tags
# ------------------------------------------------------------------
def _parse_xml_action(self, text: str) -> Optional[Dict[str, Any]]:
"""Détecte <ACTION type="tool_name">...</ACTION> (ancien format)."""
# Format court: <ACTION type="bash">commande</ACTION>
short = re.compile(r'<ACTION\s+type="(\w+)"\s*>\n?(.*?)\n?</ACTION>', re.S)
for match in short.finditer(text):
tool_name = match.group(1)
content = match.group(2).strip()
if tool_name not in self.VALID_TOOLS:
continue
tool_name = self.LEGACY_MAP.get(tool_name, tool_name)
return {'type': tool_name, 'content': {"value": content}}
# Format long avec paramètres nommés:
# <ACTION type="write">
# <path>fichier</path>
# <content>contenu</content>
# </ACTION>
long = re.compile(
r'<ACTION\s+type="(\w+)"\s*>\s*\n'
r'(.*?)'
r'\n\s*</ACTION>',
re.S
)
for match in long.finditer(text):
tool_name = match.group(1)
inner = match.group(2)
if tool_name not in self.VALID_TOOLS:
continue
# Extraire les balises <nom_param>valeur</nom_param>
params: Dict[str, Any] = {}
for param_match in re.finditer(r'<(\w+)>(.*?)</\1>', inner, re.S):
param_name = param_match.group(1)
param_value = param_match.group(2).strip()
params[param_name] = param_value
if params:
tool_name = self.LEGACY_MAP.get(tool_name, tool_name)
return {'type': tool_name, 'content': params}
return None
# ------------------------------------------------------------------
# Level 6: Regex text fallback
# ------------------------------------------------------------------
def _parse_text_fallback(self, text: str) -> Optional[Dict[str, Any]]:
"""Dernier recours : détection par motifs textuels.
Cherche des patterns comme :
- "J'utilise read_file pour lire fichier.txt"
- "Exécution de bash: ls -la"
"""
# Pattern: "j'utilise <tool>" ou "je lance <tool>"
patterns = [
(r"(?:j'utilise|j'execute|je lance|je vais utiliser|j'appelle)\s+`?(\w+)`?\s*(?:pour|sur|dans|avec)?\s*(.*?)(?:\.|$)", False),
(r"(?:utilisation|appel|execution)\s+(?:de|d'|du)\s+`?(\w+)`?\s*(.*?)(?:\.|$)", False),
(r"`(\w+)`\s+(?:sera|va|doit|va etre)\s+(?:utilise|execute|appele)\s*(.*?)(?:\.|$)", False),
]
for pat, _ in patterns:
for match in re.finditer(pat, text, re.I):
tool_name = match.group(1).lower()
if tool_name in self.VALID_TOOLS:
tool_name = self.LEGACY_MAP.get(tool_name, tool_name)
extra = match.group(2).strip() if match.lastindex and match.lastindex >= 2 else ""
return {
'type': tool_name,
'content': {"value": extra} if extra else {},
}
return None
# ------------------------------------------------------------------
# Méthodes utilitaires
# ------------------------------------------------------------------
def _split_text_and_action(
self,
full_text: str,
action: Dict[str, Any],
fence_pattern: str,
flags: int = 0,
) -> Dict[str, Any]:
"""Sépare le texte de l'action et retourne le résultat structuré."""
text = re.sub(fence_pattern, '', full_text, flags=flags).strip()
if not text:
text = ""
return {'text': text, 'action': action}
def get_valid_tools(self) -> Set[str]:
return self.VALID_TOOLS.copy()