added some guards

This commit is contained in:
Luxferre
2026-08-11 11:36:04 +03:00
parent d60798f1a3
commit f039985c6f
7 changed files with 117 additions and 20 deletions
+18 -2
View File
@@ -2,7 +2,7 @@
# Bantam agent: tiny, powerful, DIY
# Created by Luxferre in 2026, released into the public domain
import sys, os, json, time, subprocess, urllib.request
import sys, os, json, re, time, subprocess, urllib.request
try: import readline
except ImportError: readline = None
@@ -86,6 +86,9 @@ def sanitize_msgs(msgs):
except Exception:
fn["arguments"] = json.dumps({"invalid_raw": astr} if astr else {})
def is_invalid_assistant_err(e):
return bool(re.search(r"Invalid assistant message|content or tool_calls must be set", str(e)))
fib = [1, 1, 2, 3, 5, 8, 13, 21, 34]
def llm(cfg, msgs, tools):
@@ -155,7 +158,20 @@ MAX_DEPTH = 5
def AL(cfg, msgs, sp, depth=0):
stime, mx, st = num(cfg, "shell_timeout", 120), num(cfg, "max_al_iterations", 1000), cfg.get("stream", "true").lower() in ("true", "1", "yes")
for _ in range(mx):
m = llm(cfg, msgs, TOOLS); msgs.append(m)
try:
m = llm(cfg, msgs, TOOLS)
except RuntimeError as e:
if is_invalid_assistant_err(e):
for i in range(len(msgs) - 1, -1, -1):
if msgs[i].get("role") == "assistant":
print(c("[stripped malformed assistant message]", 33))
del msgs[i]
break
else:
raise
continue
raise
msgs.append(m)
if not st:
reas = m.get("reasoning_content") or m.get("reasoning")
if reas: print(c("--- reasoning start ---", 36) + "\n" + c(reas, 2) + "\n" + c("--- reasoning end ---", 36) + "\n")