Sfoglia il codice sorgente

1.新增新建对话功能2.优化剧本内容处理方法

lh 4 mesi fa
parent
commit
d46075e71f

+ 3 - 1
app/Console/Test/TestCommand.php

@@ -50,7 +50,9 @@ class TestCommand extends Command
      */
     public function handle()
     {
-
+        handleScriptContent('');
+        
+        exit();
         $url = 'https://zw-audiobook.tos-cn-beijing.volces.com/video/ai_generation_1772704798_69a9541e94bef';
         $video_name = 'ai_generation_' . time() . '_' . uniqid();
         $video_ext = getVideoExtFromUrl($url);

+ 74 - 0
app/Http/Controllers/DeepSeek/DeepSeekController.php

@@ -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'
+        ]);
+    }
         
 
     // 确认动漫剧本大纲

File diff suppressed because it is too large
+ 239 - 5
app/Libs/Helpers.php


File diff suppressed because it is too large
+ 615 - 184
app/Services/DeepSeek/DeepSeekService.php


+ 2 - 2
public/deepseek-stream-demo.html

@@ -321,7 +321,7 @@
                 <div class="form-row">
                     <div class="form-group">
                         <label>剧本 ID *</label>
-                        <input type="number" id="scriptId" name="script_id" required placeholder="请输入剧本ID">
+                        <input type="number" id="scriptId" name="script_id" value="1" required placeholder="请输入剧本ID">
                     </div>
                     <div class="form-group">
                         <label>模型选择</label>
@@ -537,7 +537,7 @@
 
             try {
                 // 发起流式请求,传入 signal 用于中断
-                const response = await fetch('/api/chatWithFileStream', {
+                const response = await fetch('/api/addChat', {
                     method: 'POST',
                     body: formData,
                     signal: abortController.signal // 关键:添加中断信号

+ 1 - 0
routes/api.php

@@ -131,6 +131,7 @@ Route::group(['middleware' => ['bindToken', 'bindExportToken', 'checkLogin']], f
     
 });
 
+Route::post('addChat', [DeepSeekController::class, 'addChat']);
 Route::get('exportScript', [DeepSeekController::class, 'exportScript']);
 Route::get('login', [AccountController::class, 'login']); // 登录
 Route::get('logout', [AccountController::class, 'logout']); // 退出