]> git.joonet.de Git - adminer.git/commitdiff
AdminerSqlGemini: Display errors
authorJakub Vrana <jakub@vrana.cz>
Thu, 3 Apr 2025 15:43:27 +0000 (17:43 +0200)
committerJakub Vrana <jakub@vrana.cz>
Thu, 3 Apr 2025 15:43:27 +0000 (17:43 +0200)
externals/jush
plugins/sql-gemini.php

index 182e95066ca8d2a4561fe3336952b3ae1c2cabaa..071536a7c95eecf19aeb176ac32da16afbb7afc1 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 182e95066ca8d2a4561fe3336952b3ae1c2cabaa
+Subproject commit 071536a7c95eecf19aeb176ac32da16afbb7afc1
index 1dd53cb08c05b6a3836d5b1366a1e4603270c5b9..b1f50e6af069c51bba41ee2253d70b579a88a662 100644 (file)
@@ -28,23 +28,22 @@ class AdminerSqlGemini {
                        foreach (Adminer\tables_list() as $table => $type) {
                                $prompt .= Adminer\create_sql($table, false, "CREATE") . ";\n\n";
                        }
-                       $prompt .= "Prefer returning more columns including primary key.\n\n";
+                       $prompt .= "Prefer returning relevant columns including primary key.\n\n";
                        $prompt .= "Give me this SQL query and nothing else:\n\n$_POST[gemini]\n\n";
                        //~ echo $prompt; exit;
                        $context = stream_context_create(array("http" => array(
                                "method" => "POST",
                                "header" => array("User-Agent: AdminerSqlGemini", "Content-Type: application/json"),
                                "content" => '{"contents": [{"parts":[{"text": ' . json_encode($prompt) . '}]}]}',
+                               "ignore_errors" => true,
                        )));
                        $response = json_decode(file_get_contents("https://generativelanguage.googleapis.com/v1beta/models/$this->model:generateContent?key=$this->apiKey", false, $context));
-                       $text = $response->candidates[0]->content->parts[0]->text;
-                       $in_code = false;
-                       foreach (preg_split('~(^|\n)```(sql)?(\n|$)~', $text) as $part) {
-                               $part = trim($part);
-                               if ($part) {
-                                       echo ($in_code ? $part : "/*\n$part\n*/") . "\n\n";
-                               }
-                               $in_code = !$in_code;
+                       if (isset($response->error)) {
+                               echo "-- " . $response->error->message;
+                       } else {
+                               $text = $response->candidates[0]->content->parts[0]->text;
+                               $text = preg_replace('~(\n|^)```sql\n(.+)\n```(\n|$)~sU', "*/\n\n\\2\n\n/*", "/*\n$text*/\n");
+                               echo preg_replace('~/\*\s*\*/\n*~', '', $text);
                        }
                        exit;
                }