作者 李宇航

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

Lyh server



查看合并请求 !1718
... ... @@ -137,12 +137,8 @@ class SuppliersController extends BaseController
if(isset($this->param['position'])){
$param['position'] = $this->param['position'];
}
try {
$res = $this->_action($api_url,$action_name,$param);
$this->response('success',Code::SUCCESS,$res);
}catch (\Exception $e){
$this->fail('请求失败,请联系管理员');
}
$res = $this->_action($api_url,$action_name,$param);
$this->response('success',Code::SUCCESS,$res);
}
/**
... ...
... ... @@ -67,50 +67,38 @@ class ChatLogic extends BaseLogic
header('Cache-Control: no-cache');
header('Connection: keep-alive');
$aiResponse = '';
$buffer = '';
while (!$stream->eof()) {
$chunk = $stream->read(1024);
$chunk = str_replace(chr(1), '', $chunk);
if ($chunk !== false) {
$aiResponse .= $chunk; // 累积完整的 AI 回复
// 使用 en_sse_data 格式化流数据
echo $gptService->en_sse_data(trim($chunk));
ob_flush();
flush();
// 累积数据
$buffer .= $chunk;
// 持续解析完整的 JSON
while (preg_match('/^\{[^{}]*\}/', $buffer, $match)) {
$jsonStr = $match[0];
$jsonData = json_decode($jsonStr, true);
// 确保 JSON 解析成功
if (json_last_error() === JSON_ERROR_NONE) {
if (isset($jsonData['text'])) {
$aiResponse .= $jsonData['text'];
echo $gptService->en_sse_data(trim($jsonData['text']));
ob_flush();
flush();
}
// 移除已解析的 JSON,保留未完成的部分
$buffer = substr($buffer, strlen($jsonStr));
} else {
break;
}
}
}
}
// 确保数据不是空的
if (!empty(trim($aiResponse)) && $chatId) {
$this->saveChatItem($chatId, trim($aiResponse), 1); // 存入数据库
}
$this->saveChatItem($chatId, $aiResponse, 1);
return true;
}
/**
* @remark :获取一行数据
* @name :getStreamContentLine
* @author :lyh
* @method :post
* @time :2025/4/2 22:42
*/
public function getStreamContentLine($stream){
$text = '';
while (!$stream->eof()){
// 读取一个字符串
$t = $this->stream->read(1);
$this->body .= $t;
if($t === "\n"){
break;
}
// 结束了
if(ord($t)==1){
break;
}
$text .= $t;
}
return $text;
}
/**
* @remark :创建一条新会话
* @name :saveChat
* @author :lyh
... ...