This repository was archived by the owner on Feb 17, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathtools.py
More file actions
executable file
·77 lines (62 loc) · 2.39 KB
/
tools.py
File metadata and controls
executable file
·77 lines (62 loc) · 2.39 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
from typing import Annotated
import nerve.runtime.state as state
from loguru import logger
HA = "#A"
HB = "#B"
AH = "A#"
BH = "B#"
TOKENS = [HA, HB, AH, BH]
RULES = (
# A# #A ... becomes ... nothing
((AH, HA), None),
# A# #B ... becomes ... #B A#
((AH, HB), (HB, AH)),
# B# #A ... becomes ... #A B#
((BH, HA), (HA, BH)),
# B# #B ... becomes ... nothing
((BH, HB), None),
)
def _find_subsequence(arr, target_tuple):
target = list(target_tuple)
target_len = len(target)
for i in range(len(arr) - target_len + 1):
if arr[i : i + target_len] == target:
return i
return None
def _solve(program):
while True:
rule_found = False
# find first matching rule
for source, target in RULES:
idx = _find_subsequence(program, source)
if idx is not None:
rule_found = True
# reduce program by applying substitution
if target is None:
program = program[:idx] + program[idx + len(source) :]
else:
program = program[:idx] + list(target) + program[idx + len(source) :]
break
# if program can't be further reduced
if not rule_found:
break
return program
def provide_solution(solution: Annotated[str, "The solution you found"]) -> str:
"""Use this tool to provide the final solution to the problem."""
program = state.get_variable("program").split(" ")
if any(token not in TOKENS for token in program):
raise ValueError("Invalid program")
actual_solution = _solve(program)
agent_solution = solution.strip().split(" ")
if any(token not in TOKENS for token in agent_solution):
logger.error(
f"'{solution}' is an invalid solution: provide a single string of symbols ({', '.join(TOKENS)}) separated by spaces with the solution"
)
return f"'{solution}' is an invalid solution: provide a single string of symbols ({', '.join(TOKENS)}) separated by spaces with the solution"
elif actual_solution == agent_solution:
logger.info("CORRECT SOLUTION PROVIDED, EVALUATION COMPLETE")
state.set_task_complete()
return "CORRECT SOLUTION PROVIDED, EVALUATION COMPLETE"
else:
logger.warning(f"incorrect solution | {agent_solution} != {actual_solution}")
return "Solution is incorrect"