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
+21 -3
View File
@@ -134,7 +134,9 @@ sub llm {
return $d->{choices}[0]{message} if $d && $d->{choices} && @{$d->{choices}};
}
my $status = $res->{status} // 0;
my $err = $res->{reason} || $res->{content} || "HTTP status $status";
my $body = $res->{content} // '';
$body = substr($body, 0, 500) if length($body) > 500;
my $err = length($body) ? "$body (HTTP $status)" : ($res->{reason} || "HTTP status $status");
print "\r\e[K" if $_col; STDOUT->flush();
die "[HTTP error: $err]\n" if $status >= 400 && $status < 500 && $status != 408 && $status != 429;
if ($i <= $#fib) { print c("[network error: $err, retrying in ${dly}s...]", 31), "\n"; sleep($dly); next; }
@@ -192,7 +194,9 @@ sub llm {
return \%msg;
}
my $status = $res->{status} // 0;
my $err = $res->{reason} || $res->{content} || "HTTP status $status";
my $body = $res->{content} // '';
$body = substr($body, 0, 500) if length($body) > 500;
my $err = length($body) ? "$body (HTTP $status)" : ($res->{reason} || "HTTP status $status");
print "\r\e[K" if $_col; STDOUT->flush();
die "[HTTP error: $err]\n" if $status >= 400 && $status < 500 && $status != 408 && $status != 429;
if ($i <= $#fib) { print c("[network error: $err, retrying in ${dly}s...]", 31), "\n"; sleep($dly); next; }
@@ -210,7 +214,21 @@ sub AL {
for my $i (1 .. $mx) {
my $m = eval { llm($cfg, $msgs, \@tools) };
if ($@) { print c($@, 31); return $msgs; }
if ($@) {
if ($@ =~ /Invalid assistant message|content or tool_calls must be set/) {
my $stripped = 0;
for (my $j = @$msgs - 1; $j >= 0; $j--) {
if (($msgs->[$j]{role} // '') eq 'assistant') {
print c("[stripped malformed assistant message]", 33), "\n";
splice @$msgs, $j, 1;
$stripped = 1;
last;
}
}
redo if $stripped;
}
print c($@, 31); return $msgs;
}
push @$msgs, $m;
if (!$st) {
my $reas = $m->{reasoning_content} // $m->{reasoning};