作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !1718
@@ -137,12 +137,8 @@ class SuppliersController extends BaseController @@ -137,12 +137,8 @@ class SuppliersController extends BaseController
137 if(isset($this->param['position'])){ 137 if(isset($this->param['position'])){
138 $param['position'] = $this->param['position']; 138 $param['position'] = $this->param['position'];
139 } 139 }
140 - try {  
141 - $res = $this->_action($api_url,$action_name,$param);  
142 - $this->response('success',Code::SUCCESS,$res);  
143 - }catch (\Exception $e){  
144 - $this->fail('请求失败,请联系管理员');  
145 - } 140 + $res = $this->_action($api_url,$action_name,$param);
  141 + $this->response('success',Code::SUCCESS,$res);
146 } 142 }
147 143
148 /** 144 /**
@@ -67,50 +67,38 @@ class ChatLogic extends BaseLogic @@ -67,50 +67,38 @@ class ChatLogic extends BaseLogic
67 header('Cache-Control: no-cache'); 67 header('Cache-Control: no-cache');
68 header('Connection: keep-alive'); 68 header('Connection: keep-alive');
69 $aiResponse = ''; 69 $aiResponse = '';
  70 + $buffer = '';
70 while (!$stream->eof()) { 71 while (!$stream->eof()) {
71 $chunk = $stream->read(1024); 72 $chunk = $stream->read(1024);
72 $chunk = str_replace(chr(1), '', $chunk); 73 $chunk = str_replace(chr(1), '', $chunk);
73 if ($chunk !== false) { 74 if ($chunk !== false) {
74 - $aiResponse .= $chunk; // 累积完整的 AI 回复  
75 - // 使用 en_sse_data 格式化流数据  
76 - echo $gptService->en_sse_data(trim($chunk));  
77 - ob_flush();  
78 - flush(); 75 + // 累积数据
  76 + $buffer .= $chunk;
  77 + // 持续解析完整的 JSON
  78 + while (preg_match('/^\{[^{}]*\}/', $buffer, $match)) {
  79 + $jsonStr = $match[0];
  80 + $jsonData = json_decode($jsonStr, true);
  81 + // 确保 JSON 解析成功
  82 + if (json_last_error() === JSON_ERROR_NONE) {
  83 + if (isset($jsonData['text'])) {
  84 + $aiResponse .= $jsonData['text'];
  85 + echo $gptService->en_sse_data(trim($jsonData['text']));
  86 + ob_flush();
  87 + flush();
  88 + }
  89 + // 移除已解析的 JSON,保留未完成的部分
  90 + $buffer = substr($buffer, strlen($jsonStr));
  91 + } else {
  92 + break;
  93 + }
  94 + }
79 } 95 }
80 } 96 }
81 - // 确保数据不是空的  
82 - if (!empty(trim($aiResponse)) && $chatId) {  
83 - $this->saveChatItem($chatId, trim($aiResponse), 1); // 存入数据库  
84 - } 97 + $this->saveChatItem($chatId, $aiResponse, 1);
85 return true; 98 return true;
86 } 99 }
87 100
88 /** 101 /**
89 - * @remark :获取一行数据  
90 - * @name :getStreamContentLine  
91 - * @author :lyh  
92 - * @method :post  
93 - * @time :2025/4/2 22:42  
94 - */  
95 - public function getStreamContentLine($stream){  
96 - $text = '';  
97 - while (!$stream->eof()){  
98 - // 读取一个字符串  
99 - $t = $this->stream->read(1);  
100 - $this->body .= $t;  
101 - if($t === "\n"){  
102 - break;  
103 - }  
104 - // 结束了  
105 - if(ord($t)==1){  
106 - break;  
107 - }  
108 - $text .= $t;  
109 - }  
110 - return $text;  
111 - }  
112 -  
113 - /**  
114 * @remark :创建一条新会话 102 * @remark :创建一条新会话
115 * @name :saveChat 103 * @name :saveChat
116 * @author :lyh 104 * @author :lyh