chore(addon/aikefu): 流式对话接口实现
This commit is contained in:
@@ -16,6 +16,11 @@ class Kefu extends BaseApi
|
||||
*/
|
||||
public function health()
|
||||
{
|
||||
// 检查请求方式是否为POST
|
||||
if (!request()->isPost()) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST请求'));
|
||||
}
|
||||
|
||||
$start_time = microtime(true);
|
||||
|
||||
// (必选) 获取站点ID和会员ID,可以通过事件数据传递
|
||||
@@ -49,7 +54,7 @@ class Kefu extends BaseApi
|
||||
|
||||
try {
|
||||
// 触发健康检查事件
|
||||
$result = Event::trigger('KefuHealthCheck', $event_data);
|
||||
$result = event('KefuHealthCheck', $event_data, true);
|
||||
|
||||
// 汇总检查结果
|
||||
$health_summary = [
|
||||
@@ -143,6 +148,11 @@ class Kefu extends BaseApi
|
||||
*/
|
||||
public function info()
|
||||
{
|
||||
// 检查请求方式是否为POST
|
||||
if (!request()->isPost()) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST请求'));
|
||||
}
|
||||
|
||||
// (可选)获取站点ID和会员ID,可以通过事件数据传递
|
||||
$site_id = $this->params['uniacid'] ?? $this->site_id;
|
||||
$member_id = $this->params['member_id'] ?? $this->member_id;
|
||||
@@ -167,7 +177,7 @@ class Kefu extends BaseApi
|
||||
];
|
||||
|
||||
// 触发获取配置信息事件
|
||||
$result = event('KefuGetInfo', $event_data);
|
||||
$result = event('KefuGetInfo', $event_data, true);
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
@@ -223,6 +233,11 @@ class Kefu extends BaseApi
|
||||
*/
|
||||
public function clearConversation()
|
||||
{
|
||||
// 检查请求方式是否为POST
|
||||
if (!request()->isPost()) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST请求'));
|
||||
}
|
||||
|
||||
// 获取请求参数
|
||||
$conversation_id = $this->params['conversation_id'] ?? '';
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
@@ -253,7 +268,7 @@ class Kefu extends BaseApi
|
||||
];
|
||||
|
||||
// 触发清除会话事件
|
||||
$result = event('KefuClearConversation', $event_data);
|
||||
$result = event('KefuClearConversation', $event_data, true);
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
@@ -286,11 +301,33 @@ class Kefu extends BaseApi
|
||||
*/
|
||||
public function chat()
|
||||
{
|
||||
// 检查请求方式是否为POST或者是EventSource请求
|
||||
$isPost = request()->isPost();
|
||||
$isGet = request()->isGet();
|
||||
$isEventSource = $isGet && request()->header('Accept') === 'text/event-stream';
|
||||
|
||||
if (!$isPost && !$isEventSource) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST或EventSource请求'));
|
||||
}
|
||||
|
||||
// 获取请求参数
|
||||
$message = $this->params['message'] ?? '';
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
$conversation_id = $this->params['conversation_id'] ?? '';
|
||||
$stream = $this->params['stream'] ?? false;
|
||||
// 对于GET请求,需要单独获取参数,因为BaseApi的构造函数可能没有正确处理GET参数
|
||||
if ($isGet) {
|
||||
$message = request()->get('message', '');
|
||||
$user_id = request()->get('user_id', $this->member_id);
|
||||
$conversation_id = request()->get('conversation_id', '');
|
||||
$stream = request()->get('stream', false);
|
||||
} else {
|
||||
$message = $this->params['message'] ?? '';
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
$conversation_id = $this->params['conversation_id'] ?? '';
|
||||
$stream = $this->params['stream'] ?? false;
|
||||
}
|
||||
|
||||
// 确保stream参数正确处理字符串'false'和'0'
|
||||
if (is_string($stream)) {
|
||||
$stream = filter_var($stream, FILTER_VALIDATE_BOOLEAN);
|
||||
}
|
||||
|
||||
// (可选)获取站点ID和会员ID,可以通过事件数据传递
|
||||
$site_id = $this->params['uniacid'] ?? $this->site_id; // 使用 uniacid, 方便以后迁移,而且uniacid 是唯一的, site_id 不是,同时被params给过滤了
|
||||
@@ -320,33 +357,39 @@ class Kefu extends BaseApi
|
||||
];
|
||||
|
||||
if ($stream) {
|
||||
// 设置SSE响应头
|
||||
header('Content-Type: text/event-stream');
|
||||
header('Cache-Control: no-cache');
|
||||
header('Connection: keep-alive');
|
||||
header('X-Accel-Buffering: no'); // 禁用Nginx缓冲
|
||||
|
||||
// 触发事件,让监听器处理流式响应
|
||||
event('KefuChat', $event_data);
|
||||
exit; // 流式请求直接输出并结束
|
||||
}
|
||||
|
||||
// 触发智能客服聊天事件(非流式)
|
||||
$result = event('KefuChat', $event_data);
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
'code' => 0,
|
||||
'message' => 'success',
|
||||
'data' => []
|
||||
];
|
||||
|
||||
if (is_array($result) && !empty($result)) {
|
||||
foreach ($result as $res) {
|
||||
if (isset($res['code']) && $res['code'] < 0) {
|
||||
$response = $res;
|
||||
break;
|
||||
}
|
||||
if (isset($res['data'])) {
|
||||
$response['data'] = array_merge($response['data'], $res['data']);
|
||||
} else {
|
||||
// 触发智能客服聊天事件(非流式)
|
||||
$result = event('KefuChat', $event_data);
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
'code' => 0,
|
||||
'message' => 'success',
|
||||
'data' => []
|
||||
];
|
||||
|
||||
if (is_array($result) && !empty($result)) {
|
||||
foreach ($result as $res) {
|
||||
if (isset($res['code']) && $res['code'] < 0) {
|
||||
$response = $res;
|
||||
break;
|
||||
}
|
||||
if (isset($res['data'])) {
|
||||
$response['data'] = array_merge($response['data'], $res['data']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->response($response);
|
||||
return $this->response($response);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return $this->response($this->error('请求失败:' . $e->getMessage()));
|
||||
}
|
||||
@@ -358,6 +401,11 @@ class Kefu extends BaseApi
|
||||
*/
|
||||
public function getHistory()
|
||||
{
|
||||
// 检查请求方式是否为POST
|
||||
if (!request()->isPost()) {
|
||||
return $this->response($this->error('请求方式错误,仅支持POST请求'));
|
||||
}
|
||||
|
||||
// 获取请求参数
|
||||
$conversation_id = $this->params['conversation_id'] ?? '';
|
||||
$user_id = $this->params['user_id'] ?? $this->member_id;
|
||||
@@ -387,7 +435,7 @@ class Kefu extends BaseApi
|
||||
];
|
||||
|
||||
// 触发获取历史消息事件
|
||||
$result = event('KefuGetHistory', $event_data);
|
||||
$result = event('KefuGetHistory', $event_data, true);
|
||||
|
||||
// 处理事件结果
|
||||
$response = [
|
||||
|
||||
Reference in New Issue
Block a user