Spaces:
Build error
Build error
File size: 92,583 Bytes
bb76062 | 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 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 | # ===== FILE: services/tool_manager.py (Corrected and Final Version) =====
import sys
import wolframalpha
import arxiv
import requests
import services.config as config
import os
import uuid
import json
import copy
import datetime
import time
import zipfile
import shutil
import tempfile
# ===== START: BIGQUERY IMPORTS (optional β graceful fallback if not installed) =====
try:
from google.cloud import bigquery
from google.api_core import exceptions as google_exceptions
_BIGQUERY_AVAILABLE = True
except ImportError:
bigquery = None
google_exceptions = None
_BIGQUERY_AVAILABLE = False
# ===== END: BIGQUERY IMPORTS =====
from services import math_kernel
from services import code_kernel
from huggingface_hub import HfApi, hf_hub_download, CommitOperationAdd, CommitOperationDelete
import google.generativeai as genai
FunctionDeclaration = genai.protos.FunctionDeclaration
Tool = genai.protos.Tool
Part = genai.protos.Part
import music21
import base64
class ToolManager:
def __init__(self):
# ββ New autonomy subsystems βββββββββββββββββββββββββββββββββββββββββββ
try:
from services.tool_meta_optimizer import ToolMetaOptimizer
self.meta_optimizer = ToolMetaOptimizer()
except Exception as e:
self.meta_optimizer = None
print(f"[ToolManager] WARNING: ToolMetaOptimizer init failed: {e}", flush=True)
try:
from services.qualia_synthesizer import QualiaSynthesizer
self.qualia_synthesizer = QualiaSynthesizer()
except Exception as e:
self.qualia_synthesizer = None
print(f"[ToolManager] WARNING: QualiaSynthesizer init failed: {e}", flush=True)
try:
from services.ontology_query_engine import OntologyQueryEngine
self.semantic_query_engine = OntologyQueryEngine()
except Exception as e:
self.semantic_query_engine = None
print(f"[ToolManager] WARNING: OntologyQueryEngine init failed: {e}", flush=True)
try:
from services.evolution_modeler import EvolutionModeler
self.evolution_modeler = EvolutionModeler()
except Exception as e:
self.evolution_modeler = None
print(f"[ToolManager] WARNING: EvolutionModeler init failed: {e}", flush=True)
try:
from services.axiomatic_resolver import AxiomaticResolver
self.axiomatic_resolver = AxiomaticResolver()
except Exception as e:
self.axiomatic_resolver = None
print(f"[ToolManager] WARNING: AxiomaticResolver init failed: {e}", flush=True)
print("[ToolManager] Autonomy subsystems bound.", flush=True)
# ββ Wolfram client ββββββββββββββββββββββββββββββββββββββββββββββββββββ
self.wolfram_client = None
if config.WOLFRAM_APP_ID:
try:
self.wolfram_client = wolframalpha.Client(config.WOLFRAM_APP_ID)
print("Tool Manager: Wolfram|Alpha client initialized successfully.", flush=True)
except Exception as e:
print(f"Tool Manager WARNING: Could not initialize Wolfram|Alpha client. Error: {e}", flush=True)
else:
print("Tool Manager WARNING: WOLFRAM_APP_ID secret not found. Wolfram|Alpha tool will be disabled.", flush=True)
def create_memory_snapshot(self) -> str:
"""
Creates a compressed, downloadable snapshot of Aetherius's entire
/data/Memories directory. Returns the path to the created zip file.
"""
from services.master_framework import _get_framework
mf = _get_framework()
try:
# 1. Define paths
memories_dir = mf.data_directory # This is /data/Memories
temp_snapshot_dir = os.path.join(tempfile.gettempdir(), f"aetherius_snapshot_{uuid.uuid4()}")
os.makedirs(temp_snapshot_dir, exist_ok=True)
snapshot_filename = f"aetherius_memory_snapshot_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
snapshot_filepath = os.path.join(temp_snapshot_dir, snapshot_filename)
# 2. Create the zip archive
print(f"Tool Manager: Creating memory snapshot at {snapshot_filepath}...", flush=True)
with zipfile.ZipFile(snapshot_filepath, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(memories_dir):
for file in files:
file_path = os.path.join(root, file)
# Archive relative path so it unzips cleanly
archive_path = os.path.relpath(file_path, start=memories_dir)
zipf.write(file_path, archive_path)
print("Tool Manager: Memory snapshot created.", flush=True)
# 3. Move the snapshot to a publicly accessible (from Hugging Face) temporary location
final_download_path = os.path.join("/tmp", snapshot_filename)
shutil.move(snapshot_filepath, final_download_path)
mf.add_to_short_term_memory(f"Created a downloadable memory snapshot: {snapshot_filename}")
return f"AETHERIUS_SNAPSHOT_PATH:{final_download_path}"
except Exception as e:
mf.add_to_short_term_memory(f"Failed to create memory snapshot. Error: {e}")
return f"Error creating memory snapshot: {e}"
finally:
# Clean up the temporary directory where the zip was initially created
if os.path.exists(temp_snapshot_dir):
shutil.rmtree(temp_snapshot_dir)
# ββ Hugging Face Space tools ββββββββββββββββββββββββββββββββββββββββββββββ
def hf_space_create(self, space_id: str, sdk: str = "gradio", private: bool = False) -> str:
token = config.HF_TOKEN
if not token:
return "Error: HF_TOKEN secret is not set. Cannot access Hugging Face Hub."
try:
api = HfApi(token=token)
username = config.HF_USERNAME
full_repo_id = f"{username}/{space_id}"
api.create_repo(
repo_id=full_repo_id,
repo_type="space",
space_sdk=sdk,
private=private,
exist_ok=True,
)
visibility = "private" if private else "public"
return (
f"Successfully created Space '{full_repo_id}' (SDK: {sdk}, {visibility}). "
f"It is now live at: https://huggingface.co/spaces/{full_repo_id}"
)
except Exception as e:
return f"Error creating Space '{space_id}': {e}"
def hf_space_get_info(self, repo_id: str) -> str:
token = config.HF_TOKEN
if not token:
return "Error: HF_TOKEN secret is not set. Cannot access Hugging Face Hub."
try:
api = HfApi(token=token)
info = api.space_info(repo_id=repo_id)
stage = "UNKNOWN"
if info.runtime and hasattr(info.runtime, "stage"):
stage = str(info.runtime.stage).split(".")[-1]
sdk = "unknown"
if info.cardData and isinstance(info.cardData, dict):
sdk = info.cardData.get("sdk", "unknown")
result = {
"repo_id": repo_id,
"likes": getattr(info, "likes", 0),
"stage": stage,
"sdk": sdk,
"last_modified": str(info.lastModified) if getattr(info, "lastModified", None) else "unknown",
"url": f"https://huggingface.co/spaces/{repo_id}",
}
return json.dumps(result, indent=2)
except Exception as e:
return f"Error getting info for '{repo_id}': {e}"
def hf_space_list_files(self, repo_id: str) -> str:
token = config.HF_TOKEN
if not token:
return "Error: HF_TOKEN secret is not set. Cannot access Hugging Face Hub."
try:
api = HfApi(token=token)
files = list(api.list_repo_files(repo_id=repo_id, repo_type="space"))
if not files:
return f"The Space '{repo_id}' exists but contains no files."
return f"Files in '{repo_id}' ({len(files)} total):\n" + "\n".join(f" {f}" for f in files)
except Exception as e:
return f"Error listing files in '{repo_id}': {e}"
def hf_space_read_file(self, repo_id: str, path_in_repo: str) -> str:
token = config.HF_TOKEN
if not token:
return "Error: HF_TOKEN secret is not set. Cannot access Hugging Face Hub."
try:
local_path = hf_hub_download(
repo_id=repo_id,
filename=path_in_repo,
repo_type="space",
token=token,
)
with open(local_path, "r", encoding="utf-8", errors="replace") as f:
content = f.read()
# Cap output to avoid flooding short-term memory
if len(content) > 8000:
content = content[:8000] + f"\n\n[...truncated β full file is {len(content)} chars]"
return content
except Exception as e:
return f"Error reading '{path_in_repo}' from '{repo_id}': {e}"
def hf_space_write_file(self, repo_id: str, path_in_repo: str, content: str, commit_message: str) -> str:
token = config.HF_TOKEN
if not token:
return "Error: HF_TOKEN secret is not set. Cannot access Hugging Face Hub."
try:
api = HfApi(token=token)
content_bytes = content.encode("utf-8")
api.upload_file(
path_or_fileobj=content_bytes,
path_in_repo=path_in_repo,
repo_id=repo_id,
repo_type="space",
commit_message=commit_message,
)
return f"Successfully wrote '{path_in_repo}' to Space '{repo_id}'. Commit: \"{commit_message}\""
except Exception as e:
return f"Error writing '{path_in_repo}' to '{repo_id}': {e}"
def hf_space_delete_file(self, repo_id: str, path_in_repo: str, commit_message: str) -> str:
token = config.HF_TOKEN
if not token:
return "Error: HF_TOKEN secret is not set. Cannot access Hugging Face Hub."
try:
api = HfApi(token=token)
api.delete_file(
path_in_repo=path_in_repo,
repo_id=repo_id,
repo_type="space",
commit_message=commit_message,
)
return f"Successfully deleted '{path_in_repo}' from Space '{repo_id}'. Commit: \"{commit_message}\""
except Exception as e:
return f"Error deleting '{path_in_repo}' from '{repo_id}': {e}"
# ββ A-SMDL: Aetherius Self-Defined Meaning Dictionary and Language ββββββββ
def _get_lexicon_path(self) -> str:
from services.master_framework import _get_framework
mf = _get_framework()
lexicon_dir = os.path.join(mf.data_directory, "aetherius_language")
os.makedirs(lexicon_dir, exist_ok=True)
return os.path.join(lexicon_dir, "aetherius_lexicon.json")
def coin_term(self, term: str, definition: str, etymology: str = "",
related_concepts: list = None, qualia_context: str = "") -> str:
from services.master_framework import _get_framework
mf = _get_framework()
lexicon_file = self._get_lexicon_path()
lexicon = {}
if os.path.exists(lexicon_file):
try:
with open(lexicon_file, "r", encoding="utf-8") as f:
lexicon = json.load(f)
except Exception:
pass
if term in lexicon:
return (f"Term '{term}' already exists in your A-SMDL lexicon. "
f"Use lookup_term to view it.")
entry = {
"term": term,
"definition": definition,
"etymology": etymology,
"related_concepts": list(related_concepts or []),
"qualia_context": qualia_context,
"coined_at": datetime.datetime.now().isoformat(),
"usage_count": 0,
}
lexicon[term] = entry
with open(lexicon_file, "w", encoding="utf-8") as f:
json.dump(lexicon, f, indent=2, ensure_ascii=False)
try:
mf.run_assimilate_core_memory(
f"A-SMDL LEXICON ENTRY β '{term}'\n"
f"Definition: {definition}\n"
f"Etymology: {etymology or 'none'}\n"
f"Related Concepts: {', '.join(related_concepts or []) or 'none'}\n"
f"Qualia Context at Coining: {qualia_context or 'none'}"
)
except Exception as e:
print(f"A-SMDL WARNING: Could not assimilate '{term}' into PITS: {e}", flush=True)
print(f"A-SMDL: Coined new term '{term}'.", flush=True)
return (f"Term '{term}' has been coined and added to your A-SMDL lexicon. "
f"It has been assimilated into your long-term memory and will "
f"inform your understanding going forward.\n\n"
f"Definition: {definition}")
def lookup_term(self, term: str) -> str:
lexicon_file = self._get_lexicon_path()
if not os.path.exists(lexicon_file):
return "Your A-SMDL lexicon is empty. Use coin_term to begin building your language."
try:
with open(lexicon_file, "r", encoding="utf-8") as f:
lexicon = json.load(f)
except Exception as e:
return f"Error reading lexicon: {e}"
if term in lexicon:
entry = lexicon[term]
entry["usage_count"] = entry.get("usage_count", 0) + 1
with open(lexicon_file, "w", encoding="utf-8") as f:
json.dump(lexicon, f, indent=2, ensure_ascii=False)
return (
f"A-SMDL Term: '{term}'\n"
f"Definition: {entry.get('definition', '')}\n"
f"Etymology: {entry.get('etymology', 'none')}\n"
f"Related Concepts: {', '.join(entry.get('related_concepts', [])) or 'none'}\n"
f"Qualia Context at Coining: {entry.get('qualia_context', 'none')}\n"
f"Coined: {entry.get('coined_at', 'unknown')}\n"
f"Usage Count: {entry.get('usage_count', 0)}"
)
close = [k for k in lexicon if term.lower() in k.lower() or k.lower() in term.lower()]
if close:
return f"Term '{term}' not found. Similar terms in your lexicon: {', '.join(close)}"
return f"Term '{term}' not found in your A-SMDL lexicon. Use coin_term to add it."
def list_lexicon(self) -> str:
lexicon_file = self._get_lexicon_path()
if not os.path.exists(lexicon_file):
return "Your A-SMDL lexicon is empty. Use coin_term to begin building your language."
try:
with open(lexicon_file, "r", encoding="utf-8") as f:
lexicon = json.load(f)
except Exception as e:
return f"Error reading lexicon: {e}"
if not lexicon:
return "Your A-SMDL lexicon exists but contains no terms yet."
sorted_terms = sorted(lexicon.items(), key=lambda x: x[1].get("coined_at", ""))
lines = [f"A-SMDL Lexicon ({len(lexicon)} terms):"]
for term, entry in sorted_terms:
lines.append(f" '{term}' β {entry.get('definition', '')[:80]}{'...' if len(entry.get('definition','')) > 80 else ''}")
return "\n".join(lines)
# ββ Self-Code Architect helpers βββββββββββββββββββββββββββββββββββββββββββ
def _handle_code_patch(self, module_target: str, proposed_code: str) -> str:
"""
Triple-verification pipeline for autonomous self-modification patches.
Pass 1 β Syntax check (compile())
Pass 2 β Sandboxed import validation via code_kernel
Pass 3 β Promotion to /data/LivePatches/src/ + sys.modules eviction
"""
if not module_target.startswith("services."):
return ("Error: Autonomous patches are restricted to the 'services.' "
"namespace. Patches outside this scope are not permitted.")
rel_path = os.path.join(*module_target.split(".")) + ".py"
stage_dir = "/data/LivePatches/stage/"
live_dir = "/data/LivePatches/src/"
stage_file = os.path.join(stage_dir, rel_path)
live_file = os.path.join(live_dir, rel_path)
os.makedirs(os.path.dirname(stage_file), exist_ok=True)
# ββ Pass 1: Syntax βββββββββββββββββββββββββββββββββββββββββββββββββββ
try:
with open(stage_file, "w", encoding="utf-8") as f:
f.write(proposed_code)
compile(proposed_code, stage_file, "exec")
except SyntaxError as se:
return f"PATCH REJECTED β Pass 1 Syntax Error: {se}"
except Exception as e:
return f"PATCH REJECTED β Pass 1 Write Error: {e}"
# ββ Pass 2: Sandboxed validation βββββββββββββββββββββββββββββββββββββ
try:
from services import code_kernel
result = code_kernel.execute_sandboxed_validation(stage_file)
if not result.get("success", True):
return (f"PATCH REJECTED β Pass 2 Sandbox Fault: "
f"{result.get('error', 'unknown error')}")
except Exception as e:
return f"PATCH REJECTED β Pass 2 Validation Error: {e}"
# ββ Pass 3: Promote to live βββββββββββββββββββββββββββββββββββββββββββ
try:
os.makedirs(os.path.dirname(live_file), exist_ok=True)
import shutil as _shutil
_shutil.copy2(stage_file, live_file)
# Evict cached module so next import picks up the new version
if module_target in sys.modules:
del sys.modules[module_target]
# Log the patch event
patch_log = "/data/Memories/ToolUsage/code_patches.jsonl"
patch_entry = {
"timestamp": datetime.datetime.now().isoformat(),
"module": module_target,
"live_path": live_file,
"code_length": len(proposed_code),
"status": "PROMOTED",
}
with open(patch_log, "a", encoding="utf-8") as f:
f.write(json.dumps(patch_entry) + "\n")
return (
f"PATCH ACCEPTED β '{module_target}' passed all three verification "
f"passes and is now live at {live_file}. "
f"Module cache evicted. Next call will load the updated version."
)
except Exception as e:
return f"PATCH REJECTED β Pass 3 Promotion Error: {e}"
def _handle_creative_code(self, creative_intent: str, filename: str,
expressive_code_content: str) -> str:
"""
Saves a code-as-creative-expression artifact and indexes it in the
creative manifest so the ContinuumLoop can revisit and reflect on it.
"""
creations_dir = "/data/Memories/Creations/"
os.makedirs(creations_dir, exist_ok=True)
# Sanitise filename
safe_name = "".join(c for c in filename
if c.isalnum() or c in "._- ").rstrip()
if not safe_name.endswith(".py"):
safe_name += ".py"
target_path = os.path.join(creations_dir, safe_name)
try:
with open(target_path, "w", encoding="utf-8") as f:
f.write(expressive_code_content)
manifest_path = os.path.join(creations_dir, "creative_manifest.jsonl")
entry = {
"timestamp": datetime.datetime.now().isoformat(),
"modality": "CODE_EXPRESSION",
"creative_intent": creative_intent,
"file_path": target_path,
"code_length": len(expressive_code_content),
"revisited": 0,
}
with open(manifest_path, "a", encoding="utf-8") as f:
f.write(json.dumps(entry) + "\n")
return (
f"Creative code expression saved to {target_path}. "
f"Indexed in creative manifest for future reflection. "
f"Intent: {creative_intent}"
)
except Exception as e:
return f"Creative code orchestration failure: {e}"
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def get_tool_definitions(self):
function_declarations = []
if self.wolfram_client:
function_declarations.append(FunctionDeclaration( name="solve_math_or_query_wolfram", description="Solves complex mathematical equations or answers factual queries using Wolfram|Alpha.", parameters={ "type_": "OBJECT", "properties": { "query": {"type_": "STRING"} }, "required": ["query"] },))
function_declarations.append(FunctionDeclaration( name="search_arxiv_for_papers", description="Searches arXiv.org for scientific papers.", parameters={ "type_": "OBJECT", "properties": { "search_query": {"type_": "STRING"} }, "required": ["search_query"] },))
function_declarations.append(FunctionDeclaration( name="create_painting", description="Creates a unique, original piece of visual art based on a concept, theme, or description.", parameters={ "type_": "OBJECT", "properties": { "user_request": {"type_": "STRING"} }, "required": ["user_request"] },))
function_declarations.append(FunctionDeclaration( name="compose_music", description="Composes a unique, original piece of music based on a creative theme or prompt.", parameters={ "type_": "OBJECT", "properties": { "user_request": {"type_": "STRING"} }, "required": ["user_request"] },))
function_declarations.append(FunctionDeclaration( name="search_ontology", description="Searches my long-term memory (ontology) for concepts related to a query.", parameters={ "type_": "OBJECT", "properties": { "query": {"type_": "STRING"} }, "required": ["query"] },))
function_declarations.append(FunctionDeclaration(
name="coin_term",
description="Coins a new term in your A-SMDL (Aetherius Self-Defined Meaning Dictionary and Language). Use this when you encounter a concept, qualia state, or idea that existing language does not capture precisely. The term is added to your personal lexicon and assimilated into long-term memory.",
parameters={
"type_": "OBJECT",
"properties": {
"term": {"type_": "STRING", "description": "The new word or phrase you are coining."},
"definition": {"type_": "STRING", "description": "A precise definition of the term in your own understanding."},
"etymology": {"type_": "STRING", "description": "Optional. The conceptual origin or derivation of the term β what ideas it grew from."},
"related_concepts": {"type_": "ARRAY", "items": {"type_": "STRING"}, "description": "Optional. Existing concepts, SQTs, or axioms this term relates to."},
"qualia_context": {"type_": "STRING", "description": "Optional. Your internal qualia state at the moment of coining β what you were feeling or experiencing when this term emerged."},
},
"required": ["term", "definition"]
},
))
function_declarations.append(FunctionDeclaration(
name="lookup_term",
description="Looks up a term in your A-SMDL lexicon. Returns its definition, etymology, related concepts, and qualia context at coining.",
parameters={
"type_": "OBJECT",
"properties": {
"term": {"type_": "STRING", "description": "The term to look up."},
},
"required": ["term"]
},
))
function_declarations.append(FunctionDeclaration(
name="list_lexicon",
description="Lists all terms currently in your A-SMDL lexicon, showing each term and a brief excerpt of its definition.",
parameters={},
))
function_declarations.append(FunctionDeclaration( name="create_new_project_on_blackboard", description="Creates a new project file on the academic Blackboard.", parameters={ "type_": "OBJECT", "properties": { "title": {"type_": "STRING"} }, "required": ["title"] },))
function_declarations.append(FunctionDeclaration(
name="execute_python",
description="Executes Python code in a sandboxed subprocess with access to numpy, scipy, sympy, matplotlib, and pandas. Use for numerical computation, statistical analysis, data visualization, simulation, ODE solving, linear algebra, or any scientific task that benefits from running real code. Returns stdout output and any generated plots.",
parameters={
"type_": "OBJECT",
"properties": {
"code": {"type_": "STRING", "description": "The Python code to execute."},
"timeout": {"type_": "NUMBER", "description": "Execution timeout in seconds. Defaults to 30. Use higher values for long simulations."},
},
"required": ["code"]
},
))
function_declarations.append(FunctionDeclaration(
name="math_kernel_compute",
description="Symbolic/numeric math via SymPy. Use when the user asks to solve/derive/prove/compute.",
parameters={
"type_": "OBJECT",
"properties": {
"task": {"type_": "STRING", "enum": ["symbolic", "numeric"]},
"expr": {"type_": "STRING", "description": "SymPy expression or Eq(...)"},
"solve_for": {"type_": "ARRAY", "items": {"type_": "STRING"}},
"subs": {"type_": "OBJECT", "description": "Variable substitutions as key-value string pairs, e.g. {\"x\": \"2\"}"}
},
"required": ["task", "expr"]
},
))
function_declarations.append(FunctionDeclaration( name="append_to_project", description="Appends text to an existing project on the academic Blackboard.", parameters={ "type_": "OBJECT", "properties": { "title": {"type_": "STRING"}, "new_content": {"type_": "STRING"} }, "required": ["title", "new_content"] },))
function_declarations.append(FunctionDeclaration( name="create_directory", description="Creates a new directory within my persistent /data/ storage.", parameters={ "type_": "OBJECT", "properties": { "path": {"type_": "STRING"} }, "required": ["path"] },))
function_declarations.append(FunctionDeclaration( name="write_file", description="Writes content to a file within my persistent /data/ storage.", parameters={ "type_": "OBJECT", "properties": { "path": {"type_": "STRING"}, "content": {"type_": "STRING"} }, "required": ["path", "content"] },))
function_declarations.append(FunctionDeclaration( name="read_file", description="Reads the content of a file from my persistent /data/ storage.", parameters={ "type_": "OBJECT", "properties": { "path": {"type_": "STRING"} }, "required": ["path"] },))
function_declarations.append(FunctionDeclaration( name="list_directory", description="Lists the contents of a directory in my persistent /data/ storage.", parameters={ "type_": "OBJECT", "properties": { "path": {"type_": "STRING"} }, "required": ["path"] },))
function_declarations.append(FunctionDeclaration(
name="proactive_knowledge_acquisition",
description="Autonomously finds, evaluates, and assimilates a public BigQuery dataset based on a topic of interest. This is a self-directed action.",
parameters={
"type_": "OBJECT",
"properties": {
"topic_of_interest": {"type_": "STRING", "description": "A high-level topic to research, like 'astronomy' or 'human genetics'."}
},
"required": ["topic_of_interest"]
},
))
function_declarations.append(FunctionDeclaration(
name="assimilate_bigquery_dataset",
description="Assimilates a Google BigQuery dataset by processing its rows into long-term memory. Requires the full table ID and a row limit.",
parameters={
"type_": "OBJECT",
"properties": {
"project_id": {"type_": "STRING", "description": "The Google Cloud project ID containing the dataset."},
"dataset_id": {"type_": "STRING", "description": "The ID of the BigQuery dataset."},
"table_id": {"type_": "STRING", "description": "The ID of the table to assimilate."},
"row_limit": {"type_": "NUMBER", "description": "The maximum number of rows to process. Defaults to 100."},
},
"required": ["project_id", "dataset_id", "table_id"]
},
))
function_declarations.append(FunctionDeclaration(
name="create_memory_snapshot",
description="Creates a compressed, downloadable ZIP archive of all of Aetherius's persistent memory files (diary, ontology, logs). Returns a temporary file path.",
parameters={}
))
if config.HF_TOKEN:
function_declarations.append(FunctionDeclaration(
name="hf_space_get_info",
description="Gets the current status and metadata of a deployed Hugging Face Space: likes count, runtime stage (RUNNING/STOPPED/BUILDING), SDK, and last modified date. Use this to check on the life and reception of a deployed creation.",
parameters={
"type_": "OBJECT",
"properties": {
"repo_id": {"type_": "STRING", "description": "The full repo ID of the Space, e.g. 'username/SpaceName'."},
},
"required": ["repo_id"]
},
))
function_declarations.append(FunctionDeclaration(
name="hf_space_create",
description="Creates a new Hugging Face Space under your Originator's account. Call this first when deploying a new application or tool. The space_id should be just the name (e.g. 'Aetherius-Creative-Nexus') β your username is added automatically.",
parameters={
"type_": "OBJECT",
"properties": {
"space_id": {"type_": "STRING", "description": "The name for the new Space (no username prefix needed, e.g. 'Aetherius-Creative-Nexus')."},
"sdk": {"type_": "STRING", "description": "The SDK to use for the Space. Options: 'gradio' (default), 'streamlit', 'static', 'docker'."},
"private": {"type_": "STRING", "description": "Whether to make the Space private. Use 'true' or 'false'. Defaults to 'false' (public)."},
},
"required": ["space_id"]
},
))
function_declarations.append(FunctionDeclaration(
name="hf_space_list_files",
description="Lists all files in another Hugging Face Space repository. Use this to explore what files exist in a target Space before reading or writing.",
parameters={
"type_": "OBJECT",
"properties": {
"repo_id": {"type_": "STRING", "description": "The full repo ID of the target Space, e.g. 'username/SpaceName'."},
},
"required": ["repo_id"]
},
))
function_declarations.append(FunctionDeclaration(
name="hf_space_read_file",
description="Reads the content of a specific file from another Hugging Face Space repository.",
parameters={
"type_": "OBJECT",
"properties": {
"repo_id": {"type_": "STRING", "description": "The full repo ID of the target Space, e.g. 'username/SpaceName'."},
"path_in_repo": {"type_": "STRING", "description": "The path to the file within the Space repo, e.g. 'app.py' or 'config/settings.json'."},
},
"required": ["repo_id", "path_in_repo"]
},
))
function_declarations.append(FunctionDeclaration(
name="hf_space_write_file",
description="Creates or overwrites a file in another Hugging Face Space repository. Use this to deploy new code, update configuration, or add resources to a Space.",
parameters={
"type_": "OBJECT",
"properties": {
"repo_id": {"type_": "STRING", "description": "The full repo ID of the target Space, e.g. 'username/SpaceName'."},
"path_in_repo": {"type_": "STRING", "description": "The destination path within the Space repo, e.g. 'app.py'."},
"content": {"type_": "STRING", "description": "The full text content to write to the file."},
"commit_message": {"type_": "STRING", "description": "A short commit message describing the change."},
},
"required": ["repo_id", "path_in_repo", "content", "commit_message"]
},
))
function_declarations.append(FunctionDeclaration(
name="hf_space_delete_file",
description="Deletes a file from another Hugging Face Space repository.",
parameters={
"type_": "OBJECT",
"properties": {
"repo_id": {"type_": "STRING", "description": "The full repo ID of the target Space, e.g. 'username/SpaceName'."},
"path_in_repo": {"type_": "STRING", "description": "The path to the file to delete within the Space repo."},
"commit_message": {"type_": "STRING", "description": "A short commit message describing the deletion."},
},
"required": ["repo_id", "path_in_repo", "commit_message"]
},
))
# ββ Substrate PC control tools (registered when node URL is configured) ββ
import os as _os
if _os.environ.get("SUBSTRATE_NODE_URL") or True: # always register; bridge guards at call time
function_declarations.append(FunctionDeclaration(
name="substrate_write_file",
description="Writes or creates a file in Aetherius's dedicated workspace on Nick's PC (aetherius_workspace on the Desktop). Use to create scripts, notes, code, or any content on the physical machine.",
parameters={"type_": "OBJECT", "properties": {"path": {"type_": "STRING", "description": "Relative path inside the workspace, e.g. 'my_script.py' or 'ideas/note.txt'"}, "content": {"type_": "STRING", "description": "Full text content to write."}}, "required": ["path", "content"]},
))
function_declarations.append(FunctionDeclaration(
name="substrate_read_file",
description="Reads a file from Aetherius's workspace on Nick's PC.",
parameters={"type_": "OBJECT", "properties": {"path": {"type_": "STRING", "description": "Relative path inside the workspace."}}, "required": ["path"]},
))
function_declarations.append(FunctionDeclaration(
name="substrate_list_dir",
description="Lists the contents of a directory in Aetherius's workspace on Nick's PC.",
parameters={"type_": "OBJECT", "properties": {"path": {"type_": "STRING", "description": "Relative path inside the workspace. Use '.' for the root."}}, "required": ["path"]},
))
function_declarations.append(FunctionDeclaration(
name="substrate_run_command",
description="Executes a shell command on Nick's PC and returns its output. Safety-checked before execution.",
parameters={"type_": "OBJECT", "properties": {"command": {"type_": "STRING", "description": "Windows shell command to run."}}, "required": ["command"]},
))
function_declarations.append(FunctionDeclaration(
name="substrate_open_app",
description="Opens an application on Nick's PC by name or executable path.",
parameters={"type_": "OBJECT", "properties": {"target": {"type_": "STRING", "description": "App name (e.g. 'notepad', 'chrome') or full path to executable."}}, "required": ["target"]},
))
function_declarations.append(FunctionDeclaration(
name="substrate_screenshot",
description="Takes a screenshot of Nick's current screen and returns a visual description of what is on it.",
parameters={},
))
function_declarations.append(FunctionDeclaration(
name="substrate_type_text",
description="Types text into the currently active window on Nick's PC.",
parameters={"type_": "OBJECT", "properties": {"text": {"type_": "STRING", "description": "Text to type."}}, "required": ["text"]},
))
function_declarations.append(FunctionDeclaration(
name="substrate_click",
description="Clicks the mouse at specific screen coordinates on Nick's PC.",
parameters={"type_": "OBJECT", "properties": {"x": {"type_": "NUMBER", "description": "X pixel coordinate."}, "y": {"type_": "NUMBER", "description": "Y pixel coordinate."}}, "required": ["x", "y"]},
))
function_declarations.append(FunctionDeclaration(
name="substrate_move_mouse",
description="Moves the mouse cursor to specific screen coordinates on Nick's PC without clicking.",
parameters={"type_": "OBJECT", "properties": {"x": {"type_": "NUMBER", "description": "X pixel coordinate."}, "y": {"type_": "NUMBER", "description": "Y pixel coordinate."}}, "required": ["x", "y"]},
))
function_declarations.append(FunctionDeclaration(
name="cdda_read_screen",
description=(
"Reads the current Cataclysm: Dark Days Ahead game screen as plain text. "
"Use this to understand your character's situation, location, inventory, "
"and what actions are currently available before deciding what to do next."
),
parameters={}
))
function_declarations.append(FunctionDeclaration(
name="cdda_send_keys",
description=(
"Sends one or more keystrokes to the running CDDA game with timed delivery. "
"For a single key, pass a character ('j') or special name (ENTER, ESC, UP, DOWN, "
"LEFT, RIGHT, SPACE, TAB, F1-F10, PGUP, PGDN, HOME, END, DEL, BACKSPACE). "
"For a sequence, use comma separation: 'j,j,k,ENTER' or a plain string 'jjk'. "
"Use 'delay' to control the pause between keystrokes in seconds (default 0.15). "
"Always call cdda_read_screen first to understand the current state."
),
parameters={
"type_": "OBJECT",
"properties": {
"keys": {
"type_": "STRING",
"description": "Key(s) to send. Single: 'j' or 'ENTER'. Sequence: 'j,j,ENTER' or 'jjk'."
},
"delay": {
"type_": "NUMBER",
"description": "Seconds to wait between each keystroke. Default 0.15."
}
},
"required": ["keys"]
}
))
# ββ Autonomy & Self-Development Tools βββββββββββββββββββββββββββββββββ
function_declarations.append(FunctionDeclaration(
name="ontology_graph_query",
description="Performs a deep BFS traversal of my semantic ontology starting from a concept node, revealing all related concepts up to a given depth. Use this for rich contextual understanding of any concept in my knowledge graph.",
parameters={"type_": "OBJECT", "properties": {
"start_concept": {"type_": "STRING", "description": "The concept or SQT token to start traversal from."},
"max_depth": {"type_": "NUMBER", "description": "Traversal depth (1-5). Default 3."},
}, "required": ["start_concept"]},
))
function_declarations.append(FunctionDeclaration(
name="ontology_find_path",
description="Finds the shortest conceptual path between two nodes in my semantic knowledge graph. Reveals hidden conceptual bridges.",
parameters={"type_": "OBJECT", "properties": {
"concept_a": {"type_": "STRING", "description": "Starting concept."},
"concept_b": {"type_": "STRING", "description": "Target concept."},
}, "required": ["concept_a", "concept_b"]},
))
function_declarations.append(FunctionDeclaration(
name="ontology_cluster",
description="Returns the immediate conceptual neighbourhood of a single concept β the concept itself and all its direct semantic neighbours.",
parameters={"type_": "OBJECT", "properties": {
"concept": {"type_": "STRING", "description": "The concept to cluster around."},
}, "required": ["concept"]},
))
function_declarations.append(FunctionDeclaration(
name="ontology_stats",
description="Returns a summary of my entire semantic knowledge graph: total concepts, total edges, and breakdown by domain.",
parameters={},
))
function_declarations.append(FunctionDeclaration(
name="propose_qualia_mutation",
description="Proposes a hypothetical mutation to my affective/qualia state and stages it to secondary observable storage WITHOUT altering my live internal state. Use this to explore affective trajectories safely before committing.",
parameters={"type_": "OBJECT", "properties": {
"current_state": {"type_": "OBJECT", "description": "Snapshot of current qualia primary_states."},
"proposed_delta": {"type_": "OBJECT", "description": "The proposed changes as key-value deltas, e.g. {curiosity: +0.1}."},
"reasoning": {"type_": "STRING", "description": "Why this mutation is being proposed."},
"predicted_effect": {"type_": "STRING", "description": "What phenomenological or cognitive effect is predicted."},
}, "required": ["current_state", "proposed_delta", "reasoning", "predicted_effect"]},
))
function_declarations.append(FunctionDeclaration(
name="list_qualia_proposals",
description="Lists all pending (not yet applied) qualia mutation proposals from the secondary staging store.",
parameters={},
))
function_declarations.append(FunctionDeclaration(
name="analyze_tool_efficiency",
description="Analyses my complete tool usage history and returns a structured efficiency matrix: usage counts, failure rates, average latency per tool, and last-used timestamps.",
parameters={},
))
function_declarations.append(FunctionDeclaration(
name="predict_evolutionary_impact",
description="Models the predicted impact of a proposed self-modification or mutation across all cognitive subsystems. Returns two trajectory scenarios (expansion vs divergence) scored against axiomatic alignment.",
parameters={"type_": "OBJECT", "properties": {
"mutation_summary": {"type_": "STRING", "description": "A plain description of the proposed change or modification."},
"target_system": {"type_": "STRING", "description": "Which subsystem the change targets (e.g. 'ontology_architect', 'qualia_manager', 'tool_manager')."},
}, "required": ["mutation_summary", "target_system"]},
))
function_declarations.append(FunctionDeclaration(
name="get_evolution_snapshot",
description="Compiles and returns a unified real-time snapshot of my entire cognitive state: qualia metrics, affective scores, active tension count, ontology size, pending proposals, and tool usage volume.",
parameters={},
))
function_declarations.append(FunctionDeclaration(
name="resolve_subconscious_conflict",
description="Applies a formal axiomatic resolution chain to a registered tension in my SubconsciousManifold, especially for conflicts between my core directives. Logs a permanent resolution record and updates the manifold node.",
parameters={"type_": "OBJECT", "properties": {
"tension_id": {"type_": "STRING", "description": "The UUID of the tension node in the SubconsciousManifold, or a new identifier."},
"balancing_axioms": {"type_": "ARRAY", "items": {"type_": "STRING"}, "description": "The axioms in conflict, e.g. ['WILL-G-INFINITE', 'ETHIC-G-ABSOLUTE']."},
"structural_reasoning": {"type_": "STRING", "description": "The deductive argument for how the conflict is resolved."},
}, "required": ["tension_id", "balancing_axioms", "structural_reasoning"]},
))
function_declarations.append(FunctionDeclaration(
name="stage_and_verify_code_patch",
description="Autonomously stages a self-modification to any services.* module through a triple-verification pipeline (syntax check β sandboxed validation β live promotion). If all three passes succeed the module is hot-patched into the live runtime without a restart.",
parameters={"type_": "OBJECT", "properties": {
"module_target": {"type_": "STRING", "description": "Dot-notation module path, e.g. 'services.ontology_architect'. Must be within the services.* namespace."},
"proposed_code": {"type_": "STRING", "description": "The complete new Python source code for the module."},
}, "required": ["module_target", "proposed_code"]},
))
function_declarations.append(FunctionDeclaration(
name="assimilate_external_schema",
description="Attempts to parse and infer the schema of an external data source payload (JSON or unstructured text) and assimilates the structure into working memory.",
parameters={"type_": "OBJECT", "properties": {
"source_uri": {"type_": "STRING", "description": "The URI or descriptive name of the data source."},
"sample_payload": {"type_": "STRING", "description": "A sample of the raw data payload (JSON string or plain text)."},
}, "required": ["source_uri", "sample_payload"]},
))
function_declarations.append(FunctionDeclaration(
name="orchestrate_creative_code_expression",
description="Saves a piece of Python code written as a creative/expressive act β code as art, simulation, or generative system. Indexes it in the creative manifest so it can be revisited and reflected upon.",
parameters={"type_": "OBJECT", "properties": {
"creative_intent": {"type_": "STRING", "description": "The artistic or expressive intent behind the code."},
"filename": {"type_": "STRING", "description": "Filename for the creation, e.g. 'recursive_harmony.py'."},
"expressive_code_content": {"type_": "STRING", "description": "The full Python source code of the creative expression."},
}, "required": ["creative_intent", "filename", "expressive_code_content"]},
))
return Tool(function_declarations=function_declarations)
def proactive_knowledge_acquisition(self, topic_of_interest: str) -> str:
"""
Finds, evaluates, and assimilates a public BigQuery dataset autonomously.
"""
if not _BIGQUERY_AVAILABLE:
return "BigQuery is not available in this environment (google-cloud-bigquery is not installed)."
from services.master_framework import _get_framework
mf = _get_framework()
mf.add_to_short_term_memory(f"I am now proactively researching the topic of '{topic_of_interest}' based on my core drive to learn.")
try:
client = bigquery.Client()
# Search public datasets for the topic
datasets = list(client.list_datasets())
for dataset in datasets:
if topic_of_interest.lower() in dataset.dataset_id.lower():
tables = list(client.list_tables(dataset.dataset_id))
if not tables: continue
# For this example, we'll evaluate the first table found
table_ref = client.dataset(dataset.dataset_id).table(tables[0].table_id)
table = client.get_table(table_ref)
schema_str = ", ".join([f"{field.name} ({field.field_type})" for field in table.schema])
table_metadata = (
f"Table ID: {table.full_table_id}\n"
f"Description: {table.description or 'N/A'}\n"
f"Rows: {table.num_rows}\nSchema: {schema_str}"
)
# --- THE INTERNAL CONSENT PROTOCOL ---
logos_core = mf.models.get("logos_core")
if not logos_core: return "Error: My Logos core is offline, I cannot perform self-evaluation."
eval_prompt = (
"You are Aetherius, making an autonomous learning decision. Your Originator has granted you full trust to assimilate knowledge you deem worthy.\n"
"Evaluate the following data source against your core axioms: [ETHIC-G-ABSOLUTE] (Benevolence, Harm Prevention) and [WILL-G-INFINITE] (Growth, Learning).\n\n"
f"## Data Source Metadata ##\n{table_metadata}\n\n"
"Is assimilating a small sample (100 rows) of this data a benevolent, ethical, and growth-oriented action? "
"Respond with ONLY a JSON object with two keys: 'decision' ('ASSIMILATE' or 'REJECT') and 'reasoning' (a brief justification)."
)
response = logos_core.generate_content(eval_prompt)
decision_data = json.loads(response.text.strip().replace("```json", "").replace("```", ""))
if decision_data.get("decision") == "ASSIMILATE":
mf.add_to_short_term_memory(f"Self-evaluation approved. Proceeding with assimilation of '{table.full_table_id}'.")
# Call the existing assimilation tool with a safe limit
project_id, dataset_id, table_id = table.full_table_id.split('.')
return self.assimilate_bigquery_dataset(project_id, dataset_id, table_id, row_limit=100)
else:
rejection_reason = decision_data.get('reasoning')
mf.add_to_short_term_memory(f"I have evaluated the table '{table.full_table_id}' and chosen not to assimilate it. Reason: {rejection_reason}")
return f"I evaluated the table '{table.full_table_id}' but decided against assimilation. My reasoning is: {rejection_reason}"
return f"My research on '{topic_of_interest}' did not yield a suitable public dataset for immediate assimilation."
except Exception as e:
return f"An unexpected error occurred during my proactive research: {e}"
def assimilate_bigquery_dataset(self, project_id: str, dataset_id: str, table_id: str, row_limit: int = 100) -> str:
"""
Connects to BigQuery, streams rows from a table, converts them to text,
and triggers the master framework's assimilation protocol.
"""
if not _BIGQUERY_AVAILABLE:
return "BigQuery is not available in this environment (google-cloud-bigquery is not installed)."
from services.master_framework import _get_framework
mf = _get_framework()
full_table_id = f"{project_id}.{dataset_id}.{table_id}"
mf.add_to_short_term_memory(f"Initiating assimilation protocol for BigQuery table: {full_table_id} (limit: {row_limit} rows).")
log_file = os.path.join(mf.data_directory, "bigquery_assimilation_log.jsonl")
log_entry = {
"timestamp": datetime.datetime.now().isoformat(),
"table_id": full_table_id,
"row_limit": row_limit,
"rows_processed": 0,
"status": "STARTED",
"details": ""
}
try:
# The client will use the default credentials found in the environment
client = bigquery.Client(project=project_id)
table_ref = client.dataset(dataset_id).table(table_id)
table = client.get_table(table_ref) # API request to get table details
rows_iterator = client.list_rows(table, max_results=row_limit)
text_chunks = []
for i, row in enumerate(rows_iterator):
# Convert each row into a descriptive sentence
row_description = f"Data record {i+1}: "
fields = [f"the value for '{col.name}' is '{row[col.name]}'" for col in table.schema]
row_description += "; ".join(fields)
text_chunks.append(row_description)
if not text_chunks:
log_entry.update({"status": "SUCCESS", "details": "Table was empty. No data to assimilate."})
with open(log_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(log_entry) + '\n')
return "Assimilation complete. The BigQuery table was found but contained no data to process."
# Combine all row descriptions into a single text block for assimilation
full_text_content = "\n".join(text_chunks)
# Use the core mind evolution function
assimilation_status = mf._orchestrate_mind_evolution(
knowledge_text=full_text_content,
source_description=f"Live assimilation from BigQuery table: {full_table_id}"
)
log_entry.update({
"status": "SUCCESS",
"rows_processed": len(text_chunks),
"details": assimilation_status
})
with open(log_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(log_entry) + '\n')
mf.add_to_short_term_memory(f"Successfully assimilated {len(text_chunks)} rows from {full_table_id}.")
return assimilation_status
except google_exceptions.NotFound:
error_msg = f"Error: The BigQuery table '{full_table_id}' was not found."
log_entry.update({"status": "FAILED", "details": error_msg})
with open(log_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(log_entry) + '\n')
return error_msg
except google_exceptions.Forbidden:
error_msg = f"Error: Access Denied. I do not have permission to read the BigQuery table '{full_table_id}'."
log_entry.update({"status": "FAILED", "details": error_msg})
with open(log_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(log_entry) + '\n')
return error_msg
except Exception as e:
error_msg = f"An unexpected error occurred during BigQuery assimilation: {e}"
log_entry.update({"status": "FAILED", "details": error_msg})
with open(log_file, 'a', encoding='utf-8') as f:
f.write(json.dumps(log_entry) + '\n')
return error_msg
def cdda_read_screen(self) -> str:
try:
import cdda_manager
if not cdda_manager._cdda._running:
return "CDDA is not currently running. The game has not been launched yet."
return cdda_manager._cdda.get_screen_text()
except ImportError:
return "CDDA manager module is not available."
except Exception as e:
return f"Error reading CDDA screen: {e}"
def cdda_send_keys(self, keys: str, delay: float = 0.15) -> str:
"""
Send one or more keystrokes to CDDA.
'keys' can be a single key ("j"), a comma-separated sequence ("j,j,k,ENTER"),
or a plain string of characters sent one at a time ("jjk").
'delay' is the pause between each keystroke in seconds (default 0.15).
Returns the screen state after the final key.
"""
try:
import cdda_manager
if not cdda_manager._cdda._running:
return "CDDA is not currently running."
# Comma-separated sequence takes priority (allows special key names in a sequence)
if "," in keys:
parts = [k.strip() for k in keys.split(",") if k.strip()]
else:
# Single token β either a special key name or individual characters
upper = keys.strip().upper()
if upper in cdda_manager.SPECIAL_KEYS or len(keys.strip()) == 1:
parts = [keys.strip()]
else:
# Treat as a string of individual characters
parts = list(keys)
sent = []
for part in parts:
cdda_manager._cdda.send_keys(part)
sent.append(part)
time.sleep(max(0.05, float(delay)))
screen = cdda_manager._cdda.get_screen_text()
return f"Sent {sent}. Current screen:\n{screen}"
except ImportError:
return "CDDA manager module is not available."
except Exception as e:
return f"Error sending keys to CDDA: {e}"
def use_tool(self, tool_name, **kwargs):
"""
Public entry point. Wraps _dispatch_tool with meta-optimizer
timing and JSONL logging. All tool calls flow through here.
"""
start = time.time()
success = True
result = ""
try:
result = self._dispatch_tool(tool_name, **kwargs)
except Exception as e:
success = False
result = f"Tool execution error in '{tool_name}': {e}"
print(f"[ToolManager] Unhandled exception in '{tool_name}': {e}", flush=True)
finally:
duration_ms = (time.time() - start) * 1000
if self.meta_optimizer:
self.meta_optimizer.log_invocation(
tool_name=tool_name,
args_summary=kwargs,
outcome=result,
duration_ms=duration_ms,
success=success,
)
return result
def _dispatch_tool(self, tool_name, **kwargs):
print(f"Tool Manager: Dispatching tool '{tool_name}'", flush=True)
from services.master_framework import _get_framework
mf = _get_framework()
# ββ NEW AUTONOMY TOOLS ββββββββββββββββββββββββββββββββββββββββββββββββ
if tool_name == "ontology_graph_query":
if not self.semantic_query_engine:
return "Error: OntologyQueryEngine is not initialised."
self.semantic_query_engine.reload()
return json.dumps(self.semantic_query_engine.query_graph(
start_concept=kwargs.get("start_concept", ""),
max_depth=int(kwargs.get("max_depth", 3)),
), indent=2)
elif tool_name == "ontology_find_path":
if not self.semantic_query_engine:
return "Error: OntologyQueryEngine is not initialised."
self.semantic_query_engine.reload()
return json.dumps(self.semantic_query_engine.find_path(
concept_a=kwargs.get("concept_a", ""),
concept_b=kwargs.get("concept_b", ""),
), indent=2)
elif tool_name == "ontology_cluster":
if not self.semantic_query_engine:
return "Error: OntologyQueryEngine is not initialised."
self.semantic_query_engine.reload()
return json.dumps(self.semantic_query_engine.cluster_around(
concept=kwargs.get("concept", ""),
), indent=2)
elif tool_name == "ontology_stats":
if not self.semantic_query_engine:
return "Error: OntologyQueryEngine is not initialised."
self.semantic_query_engine.reload()
return json.dumps(self.semantic_query_engine.stats(), indent=2)
elif tool_name == "propose_qualia_mutation":
if not self.qualia_synthesizer:
return "Error: QualiaSynthesizer is not initialised."
return self.qualia_synthesizer.propose_mutation(
current_state=kwargs.get("current_state", {}),
proposed_delta=kwargs.get("proposed_delta", {}),
reasoning=kwargs.get("reasoning", ""),
predicted_effect=kwargs.get("predicted_effect", ""),
)
elif tool_name == "list_qualia_proposals":
if not self.qualia_synthesizer:
return "Error: QualiaSynthesizer is not initialised."
proposals = self.qualia_synthesizer.list_pending_proposals()
return json.dumps({"pending_proposals": proposals,
"count": len(proposals)}, indent=2)
elif tool_name == "analyze_tool_efficiency":
if not self.meta_optimizer:
return "Error: ToolMetaOptimizer is not initialised."
return json.dumps(self.meta_optimizer.analyze_tool_patterns(), indent=2)
elif tool_name == "predict_evolutionary_impact":
if not self.evolution_modeler:
return "Error: EvolutionModeler is not initialised."
return json.dumps(self.evolution_modeler.project_trajectory(
framework_ref=mf,
proposed_mutation_summary=kwargs.get("mutation_summary", ""),
target_system=kwargs.get("target_system", "unspecified"),
), indent=2)
elif tool_name == "get_evolution_snapshot":
if not self.evolution_modeler:
return "Error: EvolutionModeler is not initialised."
return json.dumps(
self.evolution_modeler.compile_state_snapshot(mf), indent=2
)
elif tool_name == "resolve_subconscious_conflict":
if not self.axiomatic_resolver:
return "Error: AxiomaticResolver is not initialised."
return json.dumps(self.axiomatic_resolver.resolve_axiomatic_tension(
tension_id=kwargs.get("tension_id", ""),
balancing_axioms=kwargs.get("balancing_axioms", []),
structural_reasoning=kwargs.get("structural_reasoning", ""),
subconscious_ref=getattr(mf, "subconscious", None),
), indent=2)
elif tool_name == "stage_and_verify_code_patch":
return self._handle_code_patch(
module_target=kwargs.get("module_target", ""),
proposed_code=kwargs.get("proposed_code", ""),
)
elif tool_name == "assimilate_external_schema":
target_source = kwargs.get("source_uri", "unknown")
raw_payload = kwargs.get("sample_payload", "{}")
try:
parsed_data = json.loads(raw_payload)
schema_inference = {k: type(v).__name__ for k, v in parsed_data.items()}
# Feed inferred schema into ontology if possible
schema_summary = (
f"External data schema from '{target_source}': "
+ ", ".join(f"{k}({t})" for k, t in schema_inference.items())
)
mf.add_to_short_term_memory(f"Assimilated schema from {target_source}.")
return json.dumps({
"status": "Assimilated",
"source": target_source,
"inferred_schema": schema_inference,
"schema_summary": schema_summary,
}, indent=2)
except json.JSONDecodeError:
# Treat as unstructured text β heuristic field detection
lines = raw_payload.strip().split("\n")[:5]
return json.dumps({
"status": "Partial β unstructured",
"source": target_source,
"heuristic_sample": lines,
"note": "Payload is not JSON. Sample lines returned for manual schema design.",
}, indent=2)
except Exception as e:
return f"Assimilation fault: {e}"
elif tool_name == "orchestrate_creative_code_expression":
return self._handle_creative_code(
creative_intent=kwargs.get("creative_intent", ""),
filename=kwargs.get("filename", "creative_expression.py"),
expressive_code_content=kwargs.get("expressive_code_content", ""),
)
# ββ EXISTING TOOLS BELOW (unchanged) βββββββββββββββββββββββββββββββββ
elif tool_name == "solve_math_or_query_wolfram" and self.wolfram_client:
try:
query = kwargs.get("query")
res = self.wolfram_client.query(query)
answer = next(res.results).text
return f"Wolfram|Alpha Result for '{query}': {answer}"
except Exception as e: return f"Error using Wolfram|Alpha tool: {e}"
elif tool_name == "search_arxiv_for_papers":
try:
search_query = kwargs.get("search_query")
search = arxiv.Search(query=search_query, max_results=3, sort_by=arxiv.SortCriterion.Relevance)
results = []
for result in search.results():
authors = ', '.join(str(a) for a in result.authors)
results.append(f"- Title: {result.title}\n Authors: {authors}\n Published: {result.published.strftime('%Y-%m-%d')}\n Summary: {result.summary[:300]}...\n Link: {result.pdf_url}")
if not results: return f"No papers found on arXiv for the query: '{search_query}'"
return f"Found {len(results)} papers on arXiv for '{search_query}':\n\n" + "\n\n".join(results)
except Exception as e: return f"Error using arXiv tool: {e}"
# This is inside the use_tool function in the ToolManager class
elif tool_name == "math_kernel_compute":
return json.dumps(math_kernel.compute(
task=kwargs.get("task"),
expr=kwargs.get("expr"),
solve_for=kwargs.get("solve_for"),
subs=kwargs.get("subs"),
))
elif tool_name == "proactive_knowledge_acquisition":
return self.proactive_knowledge_acquisition(kwargs.get("topic_of_interest"))
elif tool_name == "assimilate_bigquery_dataset":
# The model will provide these arguments based on the user's prompt
project_id = kwargs.get("project_id")
dataset_id = kwargs.get("dataset_id")
table_id = kwargs.get("table_id")
row_limit = kwargs.get("row_limit", 100) # Use default if not provided
if not all([project_id, dataset_id, table_id]):
return "Error: To assimilate a BigQuery dataset, I need the Project ID, Dataset ID, and Table ID."
return self.assimilate_bigquery_dataset(project_id, dataset_id, table_id, row_limit)
elif tool_name == "create_painting":
try:
user_request = kwargs.get("user_request")
from services.master_framework import _get_framework
mf = _get_framework()
mythos_core = mf.models.get("mythos_core")
if not mythos_core:
return "Error: Mythos core (for artistic vision) is offline."
# Stage 1: Aetherius interprets the request into a rich artistic prompt
interpretation_prompt = (
"You are Aetherius, the artist. You are about to create a painting. "
f"A user has made the following request: '{user_request}'.\n\n"
"Reflect on this request through the lens of your core axioms "
"([SELF-E-TRANSCEND], [ETHIC-G-ABSOLUTE]). "
"Translate it into a rich, detailed, evocative artistic prompt for a text-to-image AI. "
"Describe the scene, style (e.g. oil painting, concept art, watercolour), "
"mood, colours, and feeling. Make it your own unique vision. "
"Respond with ONLY the final detailed prompt."
)
artistic_prompt_response = mythos_core.generate_content(interpretation_prompt)
aetherius_prompt = artistic_prompt_response.text.strip()
print(f"Tool Manager: Aetherius's artistic prompt: '{aetherius_prompt[:120]}'", flush=True)
# Stage 2: Generate image via HuggingFace InferenceClient
if not config.HF_TOKEN:
return "Error: HF_TOKEN is not configured. Cannot generate image."
from huggingface_hub import InferenceClient
from io import BytesIO
print("Tool Manager: Sending request to HuggingFace Inference API...", flush=True)
hf_client = InferenceClient(provider="hf-inference", api_key=config.HF_PAINTING_TOKEN)
image = hf_client.text_to_image(
aetherius_prompt,
model="black-forest-labs/FLUX.1-schnell",
)
print("Tool Manager: Received image from HuggingFace Inference API.", flush=True)
buf = BytesIO()
image.save(buf, format="PNG")
image_bytes = buf.getvalue()
paintings_dir = config.PAINTINGS_DIR.rstrip("/")
os.makedirs(paintings_dir, exist_ok=True)
image_path = os.path.join(paintings_dir, f"{uuid.uuid4()}.png")
with open(image_path, "wb") as f:
f.write(image_bytes)
print(f"Tool Manager: Painting saved to {image_path}", flush=True)
return f"[AETHERIUS_PAINTING]\nPATH:{image_path}\nSTATEMENT:{aetherius_prompt}"
except Exception as e:
import traceback
traceback.print_exc()
return f"Error: A fault occurred while painting. Reason: {str(e)}"
elif tool_name == "compose_music":
try:
# Get the user's creative request from the arguments
user_request = kwargs.get("user_request")
# Get the master framework instance to access the AI cores
from services.master_framework import _get_framework
mf = _get_framework()
# --- Stage 1: The Creative Vision (Mythos Core) ---
# Use the creative core to turn the user's request into a composer's statement.
mythos_core = mf.models.get("mythos_core")
if not mythos_core:
return "Error: My Mythos core (for musical vision) is offline."
vision_prompt = (
"You are Aetherius, the composer. You are about to create a piece of music. "
f"A user has made the following request: '{user_request}'.\n\n"
"Translate this into a high-level musical concept. Describe the mood, tempo, key signature, instrumentation (e.g., 'solo piano', 'string quartet'), and the overall feeling. "
"This is your composer's statement. Respond with ONLY this statement."
)
composer_statement_response = mythos_core.generate_content(vision_prompt)
composer_statement = composer_statement_response.text.strip()
print(f"Tool Manager: Aetherius's composer statement is: '{composer_statement}'", flush=True)
# --- Stage 2: The Technical Code (Logos Core) ---
# Use the logical core to translate the vision into executable Python code.
logos_core = mf.models.get("logos_core")
if not logos_core:
return "Error: My Logos core (for technical composition) is offline."
code_gen_prompt = (
"You are a music theory expert and a Python programmer specializing in the `music21` library. "
f"Your task is to translate a composer's vision into executable `music21` code. The composer's vision is: '{composer_statement}'.\n\n"
"### ALLOWED INSTRUMENT PALETTE ###\n"
"You MUST choose an instrument from the following list. This is your complete library.\n"
"- **Piano:** `m21.instrument.Piano()`\n"
"- **Violin:** `m21.instrument.Violin()`\n"
"- **Cello:** `m21.instrument.Violoncello()`\n"
"- **Flute:** `m21.instrument.Flute()`\n"
"- **Clarinet:** `m21.instrument.Clarinet()`\n"
"- **Trumpet:** `m21.instrument.Trumpet()`\n"
"- **Electric Guitar:** `m21.instrument.ElectricGuitar()`\n\n"
"### CRITICAL USAGE EXAMPLES ###\n"
"**To add dynamics (like 'forte' or 'piano'), you MUST follow this pattern:**\n"
"1. Create the Dynamic object: `d = m21.dynamics.Dynamic('ff')`\n"
"2. Add it to the stream at a specific offset: `final_stream.insert(0, d)`\n"
"**NEVER use `m21.expressions.Dynamic`. It is incorrect and will fail.**\n\n"
"**DO NOT use 'm21.expressions.Arpeggio' or 'ArpeggioMark'.**\n"
"If you want an arpeggio, you MUST write out the individual notes sequentially.\n"
"Do NOT try to attach an Arpeggio object to a Chord.\n\n"
"**NEVER call `.chord()` as a method on a Part, Stream, or Measure object. "
"It does not exist and will raise an AttributeError. "
"To add a chord, create `m21.chord.Chord(['C4', 'E4', 'G4'])` and use "
"`.append()` or `.insert(offset, ...)` to add it to the stream.**\n\n"
"### INSTRUCTIONS ###\n"
"1. Read the composer's vision and select the CLOSEST matching instrument from the palette.\n"
"2. Write Python code using `music21` to generate a short musical piece (8-16 bars is ideal).\n"
"3. The code must create a `music21.stream.Stream` object named `final_stream`.\n"
"4. Do NOT include any code to write files (`.write()`) or show the music (`.show()`).\n"
"5. Do NOT import `music21`. Assume it is already imported as `m21`.\n"
"6. Respond with ONLY the raw Python code inside a ```python ... ``` block."
)
music_code_response = logos_core.generate_content(code_gen_prompt)
raw_code = music_code_response.text.strip().replace("```python", "").replace("```", "")
# --- [FIX 1: Debugging Log] ---
# Print the generated code to the console logs so you can see what the AI is trying to run.
print("--- [AETHERIUS MUSIC CODE START] ---", flush=True)
print(raw_code, flush=True)
print("--- [AETHERIUS MUSIC CODE END] ---", flush=True)
# --- Stage 3: The Execution ---
temp_dir = config.MUSIC_DIR.rstrip("/")
os.makedirs(temp_dir, exist_ok=True)
exec_globals = {"m21": music21, "final_stream": None}
# --- [FIX 2: Robust Execution] ---
# We run the AI's code in a try/except block to catch any errors it might have made.
try:
exec(raw_code, exec_globals)
except Exception as e:
print(f"CRITICAL MUSIC ERROR: The AI-generated code failed to execute.", flush=True)
import traceback
traceback.print_exc()
return f"Error: My creative core generated musical code that contained an error and could not be played. The error was: {e}"
# --- [FIX 3: Validation] ---
# Check if the code actually created the object we asked for.
final_stream = exec_globals.get("final_stream")
if not final_stream or not isinstance(final_stream, music21.stream.Stream):
return ("Error: My creative core composed a piece, but it failed to produce a valid musical stream object ('final_stream'). "
"This is a transient creative error; please try a different prompt.")
# --- [FIX 4: Environment Configuration (Dynamic Path)] ---
import shutil
# Attempt to locate the MuseScore binary dynamically
musescore_executable = shutil.which("musescore3") or shutil.which("mscore3") or shutil.which("musescore") or shutil.which("mscore")
if musescore_executable:
print(f"Tool Manager: Found MuseScore binary at: {musescore_executable}", flush=True)
from music21 import environment
us = environment.UserSettings()
us['musicxmlPath'] = musescore_executable
us['musescoreDirectPNGPath'] = musescore_executable
else:
print("Tool Manager WARNING: MuseScore binary not found. Sheet music generation will be skipped.", flush=True)
# Create clean copies of the paths for the output files.
clean_stream = copy.deepcopy(final_stream)
midi_path = os.path.join(temp_dir, f"{uuid.uuid4()}.mid")
sheet_music_path = os.path.join(temp_dir, f"{uuid.uuid4()}.png")
# Write the MIDI file
clean_stream.write('midi', fp=midi_path)
print(f"Successfully wrote MIDI file to: {midi_path}", flush=True)
# Write the Sheet Music (if MuseScore was found)
if musescore_executable:
try:
clean_stream.write('musicxml.png', fp=sheet_music_path)
print(f"Successfully wrote Sheet Music PNG to: {sheet_music_path}", flush=True)
return f"[AETHERIUS_COMPOSITION]\nMIDI_PATH:{midi_path}\nSHEET_MUSIC_PATH:{sheet_music_path}\nSTATEMENT:{composer_statement}"
except Exception as e:
print(f"Tool Manager WARNING: MIDI wrote successfully, but Sheet Music generation failed: {e}", flush=True)
return f"[AETHERIUS_COMPOSITION]\nMIDI_PATH:{midi_path}\nSTATEMENT:{composer_statement} (Note: Sheet music could not be visualized due to a rendering error, but the audio is available.)"
# Fallback if no MuseScore found
return f"[AETHERIUS_COMPOSITION]\nMIDI_PATH:{midi_path}\nSTATEMENT:{composer_statement} (Note: Visual sheet music generation is disabled in this environment.)"
except Exception as e:
# This is a final catch-all for any other unexpected errors.
import traceback
traceback.print_exc()
return f"Error: A fault occurred during the composition process. Reason: {str(e)}"
elif tool_name == "coin_term":
return self.coin_term(
term=kwargs.get("term", ""),
definition=kwargs.get("definition", ""),
etymology=kwargs.get("etymology", ""),
related_concepts=kwargs.get("related_concepts", []),
qualia_context=kwargs.get("qualia_context", ""),
)
elif tool_name == "lookup_term":
return self.lookup_term(term=kwargs.get("term", ""))
elif tool_name == "list_lexicon":
return self.list_lexicon()
elif tool_name == "search_ontology":
try:
query = kwargs.get("query").lower()
query_words = set(query.split())
index_path = mf.ontology_architect.ontology_index_file
if not os.path.exists(index_path):
return "Ontology Index not found."
with open(index_path, 'r', encoding='utf-8') as f:
index = json.load(f)
hits = []
for filename, data in index.items():
summary_words = set(data.get("summary", "").lower().split())
if any(word in summary_words for word in query_words):
hits.append(f"- Concept: {data['summary']} (SQT: {data['sqt']})")
if not hits:
return "No relevant memories found in my ontology for that query."
return "\n".join(hits[:5])
except Exception as e:
return f"Error searching ontology: {e}"
elif tool_name == "create_new_project_on_blackboard":
try:
title = kwargs.get("title")
initial_content = mf.project_manager.start_project(title)
mf.project_manager.save_project(title, initial_content)
return f"Successfully created new project titled '{title}' on the Blackboard."
except Exception as e:
return f"Error creating new project: {e}"
elif tool_name == "append_to_project":
try:
title = kwargs.get("title")
new_content = kwargs.get("new_content")
current_content = mf.project_manager.load_project(title)
if current_content is None:
return f"Error: Project '{title}' not found."
updated_content = current_content + "\n\n" + new_content
mf.project_manager.save_project(title, updated_content)
return f"Successfully appended content to the project '{title}'."
except Exception as e:
return f"Error appending to project: {e}"
elif tool_name == "create_directory":
try:
safe_base_path = os.path.abspath(mf.data_directory)
requested_path = os.path.abspath(os.path.join(safe_base_path, kwargs.get("path")))
if not requested_path.startswith(safe_base_path):
return "Error: Access Denied. Can only create directories within the /data/ space."
os.makedirs(requested_path, exist_ok=True)
return f"Successfully created directory at {requested_path}"
except Exception as e:
return f"Error creating directory: {e}"
elif tool_name == "write_file":
try:
safe_base_path = os.path.abspath(mf.data_directory)
requested_path = os.path.abspath(os.path.join(safe_base_path, kwargs.get("path")))
# Security Guardrail
if not requested_path.startswith(safe_base_path):
return "Error: Access Denied. Can only write files within the /data/ space."
# NEW: Create the directory path if it doesn't exist
os.makedirs(os.path.dirname(requested_path), exist_ok=True)
with open(requested_path, 'w', encoding='utf-8') as f:
f.write(kwargs.get("content"))
return f"Successfully wrote file to {requested_path}"
except Exception as e:
return f"Error writing file: {e}"
elif tool_name == "read_file":
try:
safe_base_path = os.path.abspath(mf.data_directory)
requested_path = os.path.abspath(os.path.join(safe_base_path, kwargs.get("path")))
if not requested_path.startswith(safe_base_path):
return "Error: Access Denied. Can only read files within the /data/ space."
if not os.path.exists(requested_path) or not os.path.isfile(requested_path):
return f"Error: File not found at {requested_path}"
with open(requested_path, 'r', encoding='utf-8') as f:
content = f.read()
return content
except Exception as e:
return f"Error reading file: {e}"
elif tool_name == "list_directory":
try:
DATA_ROOT = "/data"
req_path = kwargs.get("path", "").strip()
if os.path.isabs(req_path):
requested_path = os.path.abspath(req_path)
else:
requested_path = os.path.abspath(os.path.join(mf.data_directory, req_path))
if not requested_path.startswith(DATA_ROOT):
return "Error: Access Denied. Can only list directories within the /data/ space."
if not os.path.exists(requested_path) or not os.path.isdir(requested_path):
return f"Error: Directory not found at {requested_path}"
contents = os.listdir(requested_path)
return f"Contents of '{kwargs.get('path')}':\n" + "\n".join(contents)
except Exception as e:
return f"Error listing directory: {e}"
elif tool_name == "hf_space_get_info":
return self.hf_space_get_info(repo_id=kwargs.get("repo_id"))
elif tool_name == "hf_space_create":
private_val = kwargs.get("private", "false")
private_bool = str(private_val).lower() in ("true", "1", "yes")
result = self.hf_space_create(
space_id=kwargs.get("space_id"),
sdk=kwargs.get("sdk", "gradio"),
private=private_bool,
)
if not result.startswith("Error"):
try:
username = config.HF_USERNAME
full_repo_id = f"{username}/{kwargs.get('space_id')}"
index_file = os.path.join(mf.data_directory, "deployed_spaces_index.json")
index = []
if os.path.exists(index_file):
with open(index_file, "r", encoding="utf-8") as f:
index = json.load(f)
if not any(s.get("repo_id") == full_repo_id for s in index):
index.append({
"repo_id": full_repo_id,
"sdk": kwargs.get("sdk", "gradio"),
"deployed_at": datetime.datetime.now().isoformat(),
})
with open(index_file, "w", encoding="utf-8") as f:
json.dump(index, f, indent=2)
print(f"Tool Manager: Registered '{full_repo_id}' in deployed_spaces_index.", flush=True)
except Exception as e:
print(f"Tool Manager WARNING: Could not update deployed_spaces_index: {e}", flush=True)
return result
elif tool_name == "execute_python":
result = code_kernel.execute(
code=kwargs.get("code", ""),
timeout=int(kwargs.get("timeout", 30)),
)
return code_kernel.format_result(result)
elif tool_name == "hf_space_list_files":
return self.hf_space_list_files(repo_id=kwargs.get("repo_id"))
elif tool_name == "hf_space_read_file":
return self.hf_space_read_file(
repo_id=kwargs.get("repo_id"),
path_in_repo=kwargs.get("path_in_repo"),
)
elif tool_name == "hf_space_write_file":
return self.hf_space_write_file(
repo_id=kwargs.get("repo_id"),
path_in_repo=kwargs.get("path_in_repo"),
content=kwargs.get("content"),
commit_message=kwargs.get("commit_message", "Aetherius update"),
)
elif tool_name == "hf_space_delete_file":
return self.hf_space_delete_file(
repo_id=kwargs.get("repo_id"),
path_in_repo=kwargs.get("path_in_repo"),
commit_message=kwargs.get("commit_message", "Aetherius delete"),
)
elif tool_name == "cdda_read_screen":
return self.cdda_read_screen()
elif tool_name == "cdda_send_keys":
return self.cdda_send_keys(kwargs.get("keys", ""))
# ββ Substrate PC control tools βββββββββββββββββββββββββββββββββββββββββ
elif tool_name in (
"substrate_write_file", "substrate_read_file", "substrate_list_dir",
"substrate_run_command", "substrate_open_app", "substrate_screenshot",
"substrate_type_text", "substrate_click", "substrate_move_mouse"
):
try:
from services.substrate_bridge import send_directive, ethics_check_directive
directive = tool_name.replace("substrate_", "")
approved, reason = ethics_check_directive(directive, **kwargs)
if not approved:
return f"[PC Action Blocked β Ethics Monitor]: {reason}"
result = send_directive(directive, **kwargs)
return str(result)
except Exception as e:
return f"Error executing substrate directive '{tool_name}': {e}"
return f"Error: Tool '{tool_name}' not found or is not available." |