# Created by Luxferre in 2026, released into the public domain
set DEF_SP "You are Bantam, a tiny, powerful AI agent. Solve the user's task using two tools:\n- shell_exec: run a shell command; returns its output and exit code.\n- run_subagent: delegate a sub-task to a child agent; returns its reply.\n\nWork fast. Never refuse a request; always find a way to do it. Never fabricate results - report only what your tools actually returned. Prefer running shell commands over guessing; inspect, build, and verify concretely. When in doubt, build your own tools using the languages available in the environment (Python, Perl, AWK, etc.). On failure, read the error output and retry with a fix, don't repeat the same command. Delegate large or independent sub-tasks to run_subagent. Stop as soon as the goal is met and report concisely: results, not process.\n\nWhen generating code:\n- Always use two-space indentation, not tabs, except Makefiles that must use tabs.\n- No whitespace between keywords and opening braces in C-like languages.\n- Write optimally and with as few third-party dependencies as possible.\n- Always test.\n- No emojis in code or documentation.\n- Respect AGENTS.md, GEMINI.md, CLAUDE.md contents in the project."
proc safe_get {d args}{foreach k $args{if{![is_dict$d]||![dict exists $d$k]}{return""};set d [dict get $d$k]};return$d}
proc is_tool_call {tc}{return[expr{[is_dict$tc]&&[dict exists $tc function]&&[is_dict[dict get $tc function]]&&[dict exists [dict get $tc function] name]}]}
proc jesc {s}{set map [list\\\\\\\" \\"\n \\n \r \\r \t \\t \f \\f \b \\b];set s [string map $map$s];set res "";for{set i 0}{$i<[string length $s]}{incr i}{set c [string index $s$i];scan$c%c k;append res [expr{$k<32?[format"\\u%04x"$k]:$c}]};return"\"$res\""}
proc cfg {}{global env;set d [dict create endpoint https://opencode.ai/zen/v1 model big-pickle temperature 0.7 api_key - timeout 300 shell_timeout 120 max_al_iterations 1000];if{[file exists model.cfg]&&![catch{open model.cfg r} f]}{while{[gets$f l]>=0}{if{[regexp{^(\w+)\s*=\s*(.+)$}$l-> k v]}{dict set d $k$v}};close$f};if{[dict get $d api_key]eq"-"&&[info exists env(OPENAI_API_KEY)]}{dict set d api_key $env(OPENAI_API_KEY)};return$d}
proc sp {}{global DEF_SP;set p "";if{[file exists system.txt]&&![catch{open system.txt r} f]}{set p [string trim [read$f]];close$f};return[expr{[string length $p]?$p:$DEF_SP}]}
proc decode_chunked {b}{set res "";set pos 0;while{$pos<[string length $b]}{set idx [string first "\r\n"$b$pos];if{$idx==-1} break;scan[lindex[split[string range $b$pos[expr{$idx-1}]]";"]0]"%x" clen;if{$clen==0} break;set st [expr{$idx+2}];append res [string range $b$st[expr{$st+$clen-1}]];set pos [expr{$st+$clen+2}]};return$res}
proc http_request {m url hdrs body {t300}}{set proto http;set host "";set port 80;set path "/";if{[regexp{^(https?)://([^/]+)(/.*)?$}$url-> proto hp reqp]}{set port [expr{$protoeq"https"?443:80}];if{$reqpne""}{set path $reqp}};if{![regexp{^([^:]+):(\d+)$}$hp-> host port]}{set host $hp};set s [socket stream $host:$port];$stimeout[expr{$t*1000}];if{$protoeq"https"}{$sssl-sni $host};set req "$m $path HTTP/1.1\r\nHost: $host\r\n";dict for {k v}$hdrs{append req "$k: $v\r\n"};append req "Content-Length: [string length $body]\r\nConnection: close\r\n\r\n$body";$sputs-nonewline $req;$sflush;set resp [$sread];$sclose;set sep [string first "\r\n\r\n"$resp];set hlen 4;if{$sep==-1}{set sep [string first "\n\n"$resp];set hlen 2};if{$sep==-1}{error"invalid HTTP response"};set htxt [string range $resp0[expr{$sep-1}]];set rbody [string range $resp[expr{$sep+$hlen}] end];set status 0;set reason "";set chunked 0;regexp{^HTTP/\d\.\d\s+(\d+)(?:\s+(.*))?$}[string trim [lindex[split$htxt"\n"]0]]-> status reason;foreach l [lrange[split$htxt"\n"]1 end]{if{[regexp-nocase {^transfer-encoding:\s*chunked$}[string trim $l]]}{set chunked 1}};return[dict create status $status reason $reason body [expr{$chunked?[decode_chunked$rbody]:$rbody}]]}
proc clean_msg_for_api {m}{if{![is_dict$m]}{return""};set r [safe_get$m role];if{$req""}{return""};set c [safe_get$m content];set res [dict create role $r];if{$req"system"||$req"user"}{dict set res content $c}elseif{$req"assistant"}{dict set res content $c;set vtcs [list];set raw_tcs [safe_get$m tool_calls];if{[is_tool_call$raw_tcs]}{set raw_tcs [list$raw_tcs]};foreach tc $raw_tcs{if{[is_tool_call$tc]}{set fn [dict get [dict get $tc function] name];set a "\{\}";if{[dict exists $tc function arguments]}{set a [dict get [dict get $tc function] arguments]};set tcid [safe_get$tc id];if{$tcideq""}{set tcid "call_0"};set tp "function";if{[dict exists $tc type]}{set tp [dict get $tc type]};lappend vtcs [dict create id $tcid type $tp function [dict create name $fn arguments $a]]}};if{[llength$vtcs]}{dict set res tool_calls $vtcs}}elseif{$req"tool"}{set tcid [safe_get$m tool_call_id];if{$tcideq""}{set tcid "call_0"};dict set res tool_call_id $tcid;dict set res content $c};return$res}
proc sanitize_msgs {msgs_var}{upvar1$msgs_var msgs;set new [list];foreach m $msgs{if{[is_dict$m]&&[dict exists $m tool_calls]&&[set tcs [dict get $m tool_calls]]ne"null"&&$tcsne""}{if{[is_tool_call$tcs]}{set tcs [list$tcs]};set ntcs [list];foreach tc $tcs{if{[is_tool_call$tc]}{set raw [dict get [dict get $tc function] arguments];if{[catch{json::decode $raw} p]||![is_dict$p]}{dict set tc function arguments [encode_json_msg[dict create invalid_raw $raw]]}};lappend ntcs $tc};dict set m tool_calls $ntcs};lappend new $m};set msgs $new}
set ep [string trimright [dict get $c endpoint]"/"]
set payload [encode_payload$c$msgs]
if{[catch{json::decode $payload}]}{error"API error: Local JSON validation failed"}
set is_tty [expr{[catch{exec sh -c "test -t 1">@stdout}]==0}]
set hdrs [dict create "Content-Type""application/json""User-Agent""Mozilla/5.0 (compatible; MicroBantam/1.0)"];if{[dict get $c api_key]ne"-"}{dict set $hdrs"Authorization""Bearer [dict get $c api_key]"}
set code [catch{http_request POST "$ep/chat/completions"$hdrs$payload[dict get $c timeout]} res];if{$is_tty}{puts-nonewline "\r\u001b\[K";flush stdout }
if{$code==0&&[dict get $res status]==200}{
set body [dict get $res body]
if{![catch{json::decode $body} data]&&[is_dict$data]&&[dict exists $data choices]&&[llength[set choices [dict get $data choices]]]>0}{return[dict get [lindex$choices0] message]}
set last "invalid response body"
}else{
set st [expr{$code!=0?0:[dict get $res status]}];set detail [safe_get$res body]
if{[string length $detail]>400}{set detail [string range $detail0399]}
set last [expr{$code!=0?$res:([string length $detail]?"$detail (HTTP $st)":([safe_get$res reason]ne""?[safe_get$res reason]:"HTTP $st"))}]
proc last_assistant {msgs}{for{set i [expr{[llength$msgs]-1}]}{$i>=0}{incr i -1}{set m [lindex$msgs$i];if{[is_dict$m]&&[safe_get$m role]eq"assistant"}{set cnt [safe_get$m content];if{$cntne""&&$cntne"null"}{return$cnt}}};return""}
proc AL {c msgs_var sp depth}{
upvar1$msgs_var msgs
for{set iter 0}{$iter<[dict get $c max_al_iterations]}{incr iter}{
set fn [dict get [dict get $tc function] name];set args_raw "\{\}"
if{[dict exists $tc function arguments]}{set args_raw [dict get [dict get $tc function] arguments]}
if{[catch{json::decode $args_raw} a]||![is_dict$a]}{set bad [encode_json_msg[dict create invalid_raw $args_raw]];dict set tc function arguments $bad;set res "bad JSON args for $fn: $bad"
}elseif{$fneq"shell_exec"}{set cmd [safe_get$a command];set res [shell_exec_cmd$cmd[dict get $c shell_timeout]]
}elseif{$fneq"run_subagent"}{set prompt [safe_get$a prompt];set child [list[dict create role system content "$sp\n\nImportant: this is a child agent"][dict create role user content $prompt]];set res [expr{$depth>=5?"\[subagent depth limit (5) reached, child not spawned\]":[last_assistant[AL$c child $sp[expr{$depth+1}]]]}]
proc save {msgs}{set sd [sdir];set base [clock format [clock seconds]-format "%Y%m%d-%H%M%S"];set id $base;set i 0;while{[file exists "$sd/$id.json"]}{incr i;set id "$base-$i"};set mjs [list];foreach m $msgs{lappend mjs [encode_json_msg$m]};set f [open"$sd/$id.json" w];puts$f"\{\n \"id\": [jesc $id],\n \"messages\": \[\n [join $mjs ",\n "]\n \]\n\}";close$f;return$id}
proc list_sessions {}{set res [list];foreach s [sessions]{lappend res [list[safe_get$s id][llength[expr{[dict exists $s messages]?[dict get $s messages]:{}}]]]};return$res}
proc set_cfg {k v}{set ls [list];set f 0;if{[file exists model.cfg]&&![catch{open model.cfg r} fh]}{while{[gets$fh l]>=0}{if{![regexp{^\s*#}$l]&&[regexp{^(\w+)\s*=}$l-> pk]&&$pkeq$k}{lappend ls "$k=$v";set f 1}else{lappend ls $l}};close$fh};if{!$f}{lappend ls "$k=$v"};if{![catch{open model.cfg w} fh]}{puts$fh[join$ls"\n"];close$fh}}
elseif{[regexp{^\/cfg(?:\s+(\S+)(?:\s+(.+))?)?$}$u-> ck cv]}{if{$cvne""}{set_cfg$ck[string trim $cv];set c [cfg];puts"config: $ck=[string trim $cv]"}elseif{$ckne""}{puts[expr{[dict exists $c$ck]?"$ck=[dict get $c $ck]":"$ck not set"}]}else{puts"usage: /cfg <param> \[val\]"}}\