-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir_compiler.py
More file actions
66 lines (50 loc) · 1.27 KB
/
ir_compiler.py
File metadata and controls
66 lines (50 loc) · 1.27 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
class AgentIRCompiler:
"""
Converts agent reasoning + context into IR instructions.
This is a lightweight semantic-to-opcode compiler layer.
"""
def __init__(self, llm):
self.llm = llm
# -----------------------------------------
def compile(self, agent, goal, snapshot, prompt, role):
"""
Produces IR program as list of ops.
"""
system_prompt = f"""
You are an IR compiler.
Convert reasoning into structured opcode instructions.
RULES:
- output ONLY JSON list
- each instruction must have:
- opcode
- arg1 (optional)
- arg2 (optional)
GOAL:
{goal}
ROLE:
{role}
AGENT:
{agent.name}
CONTEXT:
{prompt}
"""
raw = self.llm.call(system_prompt)
return self._parse(raw)
# -----------------------------------------
def _parse(self, raw):
"""
Robust JSON extraction with fallback.
"""
import json
try:
if isinstance(raw, list):
return raw
# attempt direct JSON parse
return json.loads(raw)
except Exception:
# fallback: very weak structured recovery
return [{
"opcode": "NOP",
"arg1": str(raw)[:200],
"arg2": None
}]