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
+7 -1
View File
@@ -41,7 +41,9 @@ sub llm { my ($c, $msgs) = @_; # one non-streaming chat completion
print "\r\e[K" if $tty; # clear the spinner line
my $d = $r->{success} ? eval { decode_json($r->{content}) } : undef;
return $d->{choices}[0]{message} if $d && $d->{choices} && @{$d->{choices}};
die "API error: " . ($r->{reason} || "HTTP $r->{status}") . "\n"; }
my $rb = $r->{content} // '';
$rb = substr($rb, 0, 500) if length($rb) > 500;
die "API error: " . (length($rb) ? "$rb (HTTP $r->{status})" : ($r->{reason} || "HTTP $r->{status}")) . "\n"; }
sub shell_exec { my ($cmd, $t) = @_; # run a command under a hard timeout
my $out = '';
@@ -56,6 +58,10 @@ sub AL { my ($c, $msgs, $sp, $depth) = @_; # the agentic loop: LLM <-> tools unt
$depth ||= 0;
for (1 .. $c->{max_al_iterations}) {
my $m = eval { llm($c, $msgs) };
if ($@ && $@ =~ /Invalid assistant message|content or tool_calls must be set/ && grep({ $_->{role} eq 'assistant' } @$msgs)) {
for (my $j = @$msgs - 1; $j >= 0; $j--) { if ($msgs->[$j]{role} eq 'assistant') { splice @$msgs, $j, 1; last; } }
print "[stripped malformed assistant message]\n"; redo;
}
if ($@) { print $@; return $msgs; }
push @$msgs, $m;
print $m->{content}, "\n" if defined $m->{content} && length $m->{content};