97 lines
2.7 KiB
PHP
97 lines
2.7 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 文件功能:聊天机器人控制器
|
|||
|
|
*
|
|||
|
|
* 处理用户与 AI 机器人的对话请求。
|
|||
|
|
* 先检查全局开关(sysparam: chatbot_enabled),再调用 AiChatService 获取回复。
|
|||
|
|
*
|
|||
|
|
* @author ChatRoom Laravel
|
|||
|
|
*
|
|||
|
|
* @version 1.0.0
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
namespace App\Http\Controllers;
|
|||
|
|
|
|||
|
|
use App\Models\Sysparam;
|
|||
|
|
use App\Services\AiChatService;
|
|||
|
|
use Illuminate\Http\JsonResponse;
|
|||
|
|
use Illuminate\Http\Request;
|
|||
|
|
use Illuminate\Support\Facades\Auth;
|
|||
|
|
|
|||
|
|
class ChatBotController extends Controller
|
|||
|
|
{
|
|||
|
|
/**
|
|||
|
|
* 构造函数:注入 AI 聊天服务
|
|||
|
|
*/
|
|||
|
|
public function __construct(
|
|||
|
|
private readonly AiChatService $aiChat,
|
|||
|
|
) {}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 与 AI 机器人对话
|
|||
|
|
*
|
|||
|
|
* 接收用户消息,检查全局开关后调用 AI 服务获取回复。
|
|||
|
|
* 支持自动故障转移:默认厂商失败时自动尝试备用厂商。
|
|||
|
|
*
|
|||
|
|
* @param Request $request 请求对象,需包含 message 和 room_id
|
|||
|
|
* @return JsonResponse 机器人回复或错误信息
|
|||
|
|
*/
|
|||
|
|
public function chat(Request $request): JsonResponse
|
|||
|
|
{
|
|||
|
|
// 验证请求参数
|
|||
|
|
$request->validate([
|
|||
|
|
'message' => 'required|string|max:2000',
|
|||
|
|
'room_id' => 'required|integer',
|
|||
|
|
]);
|
|||
|
|
|
|||
|
|
// 检查全局开关
|
|||
|
|
$enabled = Sysparam::getValue('chatbot_enabled', '0');
|
|||
|
|
if ($enabled !== '1') {
|
|||
|
|
return response()->json([
|
|||
|
|
'status' => 'error',
|
|||
|
|
'message' => 'AI 机器人功能已关闭,请联系管理员开启。',
|
|||
|
|
], 403);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$user = Auth::user();
|
|||
|
|
$message = $request->input('message');
|
|||
|
|
$roomId = $request->input('room_id');
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
$result = $this->aiChat->chat($user->id, $message, $roomId);
|
|||
|
|
|
|||
|
|
return response()->json([
|
|||
|
|
'status' => 'success',
|
|||
|
|
'reply' => $result['reply'],
|
|||
|
|
'provider' => $result['provider'],
|
|||
|
|
'model' => $result['model'],
|
|||
|
|
]);
|
|||
|
|
} catch (\Exception $e) {
|
|||
|
|
return response()->json([
|
|||
|
|
'status' => 'error',
|
|||
|
|
'message' => $e->getMessage(),
|
|||
|
|
], 500);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 清除当前用户的 AI 对话上下文
|
|||
|
|
*
|
|||
|
|
* 用于用户想要重新开始对话时使用。
|
|||
|
|
*
|
|||
|
|
* @param Request $request 请求对象
|
|||
|
|
* @return JsonResponse 操作结果
|
|||
|
|
*/
|
|||
|
|
public function clearContext(Request $request): JsonResponse
|
|||
|
|
{
|
|||
|
|
$user = Auth::user();
|
|||
|
|
$this->aiChat->clearContext($user->id);
|
|||
|
|
|
|||
|
|
return response()->json([
|
|||
|
|
'status' => 'success',
|
|||
|
|
'message' => '对话上下文已清除,可以开始新的对话了。',
|
|||
|
|
]);
|
|||
|
|
}
|
|||
|
|
}
|