|
|
@@ -386,6 +386,80 @@ class DeepSeekController extends BaseController
|
|
|
// 直接调用导出服务,该方法会直接输出文件并退出
|
|
|
$this->deepseekService->exportScript($data);
|
|
|
}
|
|
|
+
|
|
|
+ // 新建对话
|
|
|
+ public function addChat(Request $request) {
|
|
|
+ // 忽略所有超时限制
|
|
|
+ set_time_limit(0);
|
|
|
+ ini_set('max_execution_time', '0');
|
|
|
+
|
|
|
+
|
|
|
+ $data = $request->all();
|
|
|
+ return response()->stream(function () use ($data) {
|
|
|
+ // 禁用所有输出缓冲
|
|
|
+ if (ob_get_level()) {
|
|
|
+ ob_end_clean();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置输出缓冲为关闭
|
|
|
+ ini_set('output_buffering', 'off');
|
|
|
+ ini_set('zlib.output_compression', 'off');
|
|
|
+
|
|
|
+ // 立即刷新
|
|
|
+ if (function_exists('apache_setenv')) {
|
|
|
+ apache_setenv('no-gzip', '1');
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ $generator = $this->deepseekService->addChat($data);
|
|
|
+
|
|
|
+ foreach ($generator as $chunk) {
|
|
|
+ // 发送 SSE 格式的数据
|
|
|
+ $jsonData = json_encode($chunk, JSON_UNESCAPED_UNICODE);
|
|
|
+ echo "data: {$jsonData}\n\n";
|
|
|
+
|
|
|
+ // 强制刷新输出缓冲区
|
|
|
+ if (ob_get_level() > 0) {
|
|
|
+ ob_flush();
|
|
|
+ }
|
|
|
+ flush();
|
|
|
+
|
|
|
+ // 检查客户端是否断开连接
|
|
|
+ if (connection_aborted()) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 发送结束标记
|
|
|
+ echo "data: [DONE]\n\n";
|
|
|
+ if (ob_get_level() > 0) {
|
|
|
+ ob_flush();
|
|
|
+ }
|
|
|
+ flush();
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ // 发送错误信息
|
|
|
+ $error = [
|
|
|
+ 'type' => 'error',
|
|
|
+ 'message' => $e->getMessage(),
|
|
|
+ 'code' => $e->getCode()
|
|
|
+ ];
|
|
|
+ $jsonError = json_encode($error, JSON_UNESCAPED_UNICODE);
|
|
|
+ echo "data: {$jsonError}\n\n";
|
|
|
+ if (ob_get_level() > 0) {
|
|
|
+ ob_flush();
|
|
|
+ }
|
|
|
+ flush();
|
|
|
+ }
|
|
|
+ }, 200, [
|
|
|
+ 'Content-Type' => 'text/event-stream',
|
|
|
+ 'Cache-Control' => 'no-cache',
|
|
|
+ 'Connection' => 'keep-alive',
|
|
|
+ 'X-Accel-Buffering' => 'no', // 防止 Nginx 缓冲
|
|
|
+ 'Access-Control-Allow-Origin' => '*',
|
|
|
+ 'Access-Control-Allow-Credentials' => 'true'
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
// 确认动漫剧本大纲
|