Fix: Handle multiple description candidates from Gemini

This commit is contained in:
hbrain 2026-05-30 01:55:56 +02:00
parent 76045d30da
commit 023b08eea2

View file

@ -74,7 +74,17 @@ $log = implode("\n", $out_lines);
$gfile = dirname($jobdir) . '/' . basename($jobdir) . '.json';
if (is_file($gfile)) {
$raw = @file_get_contents($gfile);
$json = @json_decode($raw, true);
$json_data = @json_decode($raw, true);
// Handle case where Gemini returns an array of candidates or a single object.
// We will always take the first valid description object we can find.
$json = null;
if (is_array($json_data) && isset($json_data[0]) && is_array($json_data[0])) {
$json = $json_data[0]; // It's an array of descriptions, take the first.
} elseif (is_array($json_data)) {
$json = $json_data; // It's already a single description object.
}
$description = '';
$teaser = '';
$tags = [];
@ -83,12 +93,13 @@ if (is_file($gfile)) {
$teaser = isset($json['teaser']) ? (string)$json['teaser'] : '';
$tags = isset($json['tags']) && is_array($json['tags']) ? $json['tags'] : [];
}
echo json_encode([
'status' => 'ok',
'description' => $description,
'teaser' => $teaser,
'tags' => $tags,
'raw_json' => $json,
'raw_json' => $json_data, // Send original raw data back for reference
'log' => $log,
'rc' => $rc,
], JSON_UNESCAPED_UNICODE);