ShuanghongS 4 ay önce
ebeveyn
işleme
9ca3ba13e3
3 değiştirilmiş dosya ile 43 ekleme ve 6 silme
  1. 22 2
      service/robot.class.php
  2. 1 1
      utils/common.class.php
  3. 20 3
      utils/utils.class.php

+ 22 - 2
service/robot.class.php

@@ -639,7 +639,8 @@ class robot{
                 common::echo_json_encode(200,$return);            
                 exit();
             } catch (Exception $e) {
-                $return = array("type"=>"markdown","is_fixedAnswer_end"=>true,"data" =>$e->getMessage());
+                error_log("ai_chat_exception: ".$e->getMessage());
+                $return = array("type"=>"markdown","is_fixedAnswer_end"=>true,"data" =>"Invalid response");
                 common::echo_json_encode(200,$return);            
                 exit();
             }
@@ -902,7 +903,26 @@ class robot{
             exit();
         }
 
+        /**
+         * JSON_ERROR_NONE	0	没有错误
+         * JSON_ERROR_DEPTH	1	超出最大堆栈深度
+         * JSON_ERROR_STATE_MISMATCH	2	JSON 格式不一致或无效
+         * JSON_ERROR_CTRL_CHAR	3	控制字符错误,可能编码不对
+         * JSON_ERROR_SYNTAX	4	语法错误(如缺失括号、逗号等)
+         * JSON_ERROR_UTF8	5	编码错误,非 UTF-8 字符
+         * JSON_ERROR_RECURSION	6	检测到递归引用
+         * JSON_ERROR_INF_OR_NAN	7	不能编码 INF 或 NAN
+         * JSON_ERROR_UNSUPPORTED_TYPE	8	不支持的数据类型
+        */
         $message = json_decode(common::getChatAimessage($response['message']),true);
+        if ($message === null) {
+            // 获取最后一次 JSON 操作的错误码
+            $errorCode = json_last_error();
+            // 获取错误描述(PHP 5.5+)
+            $errorMsg = json_last_error_msg();
+            error_log("AI Message JSON Decode Error : {$errorCode} - {$errorMsg}");
+        }
+
         if($message["can_query"] == "true" && !empty($message["sql"])){
             $reference = $message["reference"];
             $answer_template = common::check_input($message["reference"]);
@@ -942,7 +962,7 @@ class robot{
         }else{
             $answer = $message["response"];
             if(!$response['success']){
-                $answer = $response["error"];
+                $answer = "Invalid response";
             }
         }
 

+ 1 - 1
utils/common.class.php

@@ -2540,7 +2540,7 @@ class common {
 
         // 从 from 之后的部分中查找 where
         $afterFrom = substr($sqlLower, $fromPos + 4); // +4 是跳过 'from'
-        if (stripos($afterFrom, 'WHERE') === false){
+        if (stripos($afterFrom, 'WHERE') === false && stripos($afterFrom, 'limit') === false){
             $data = false; // 简单的判断是否带条件查询
         }
 

+ 20 - 3
utils/utils.class.php

@@ -943,11 +943,19 @@ class utils {
         if(empty($data)){
             $fileds = common::extractSelectFields($new_sql);
             foreach($fileds as $key){
-                $replacements["{{$key}}"] = ""; 
-                $replacements["{{{$key}}}"] = "";
+                //如果遇到data没数据,但是有 total_count ,则处理成0
+                if($key == "total_count"){
+                    $replacements["{{$key}}"] = "0"; 
+                    $replacements["{{{$key}}}"] = "0";
+                } else {
+                    $replacements["{{$key}}"] = ""; 
+                    $replacements["{{{$key}}}"] = "";
+                }
             }
         }
         // 执行替换
+        error_log("replacements_single:".json_encode($replacements));
+        error_log("template_single:".$template);
         $result = strtr($template, $replacements);
         // 输出结果
         return $result;
@@ -981,6 +989,9 @@ class utils {
                 }
                 $generatedRows[] = strtr($explode_str, $replacements);
             }
+            if(empty($generatedRows) && empty($data)){
+                $generatedRows[] = "No Data";
+            }
 
             //如果 SQL字段是有多余未匹配的字段 调用一次个体替换
             $mapping  = $explode_data['mapping'];
@@ -1013,7 +1024,13 @@ class utils {
             preg_match_all('/\{\{(\w+)\}\}/', $line, $matches);
             $templateVars = $matches[1];
 
-            if (empty($templateVars)) continue;
+           
+            preg_match_all('/(?<=\|)([^|]+)(?=\|)/', $line, $matchesTwo);
+            // 去除每个匹配项前后的空白字符
+            $cells = $matchesTwo[1];
+
+
+            if (empty($templateVars) || count($templateVars) <> count($cells)) continue;
 
             // 检查每个变量是否都在 SQL 字段中
             foreach ($templateVars as $var) {