lh hai 2 semanas
pai
achega
e66b25b835
Modificáronse 1 ficheiros con 57 adicións e 23 borrados
  1. 57 23
      app/Services/DeepSeek/DeepSeekService.php

+ 57 - 23
app/Services/DeepSeek/DeepSeekService.php

@@ -1026,6 +1026,58 @@ class DeepSeekService
                         $fullContent = $this->extractJsonContent($fullContent);
                     }
                     $finished = true;
+
+                    // 保存对话记录并将 records 附加到 done 消息(无论 script_id 是否有值都返回 records 数组)
+                    $chunk['records'] = [];
+                    if (!empty($ctx['script_id'])) {
+                        try {
+                            $now = now();
+                            DB::table('mp_script_records')->insert([
+                                [
+                                    'uid' => $ctx['uid'],
+                                    'script_id' => $ctx['script_id'],
+                                    'sequence' => $ctx['sequence'],
+                                    'role' => 'user',
+                                    'content' => $ctx['original_prompt'],
+                                    'created_at' => $now,
+                                    'updated_at' => $now,
+                                ],
+                                [
+                                    'uid' => $ctx['uid'],
+                                    'script_id' => $ctx['script_id'],
+                                    'sequence' => $ctx['sequence'],
+                                    'role' => 'assistant',
+                                    'content' => $fullContent,
+                                    'created_at' => $now,
+                                    'updated_at' => $now,
+                                ],
+                            ]);
+
+                            $chunk['records'] = DB::table('mp_script_records')
+                                ->where('uid', $ctx['uid'])
+                                ->where('script_id', $ctx['script_id'])
+                                ->where('sequence', $ctx['sequence'])
+                                ->orderBy('id', 'desc')
+                                ->limit(2)
+                                ->get()
+                                ->map(function ($record) {
+                                    return [
+                                        'rid' => $record->id,
+                                        'script_id' => $record->script_id,
+                                        'sequence' => $record->sequence,
+                                        'role' => $record->role,
+                                        'content' => $record->content,
+                                        'created_at' => $record->created_at,
+                                    ];
+                                })
+                                ->reverse()
+                                ->values()
+                                ->toArray();
+                        } catch (\Exception $e) {
+                            dLog('deepseek')->error('保存剧本对话记录失败: ' . $e->getMessage());
+                            $chunk['records'] = [];
+                        }
+                    }
                 }
                 yield $chunk;
             }
@@ -1047,29 +1099,6 @@ class DeepSeekService
                         'source' => 'newGenerateText',
                     ]);
                 }
-                if (!empty($ctx['script_id'])) {
-                    $now = now();
-                    DB::table('mp_script_records')->insert([
-                        [
-                            'uid' => $ctx['uid'],
-                            'script_id' => $ctx['script_id'],
-                            'sequence' => $ctx['sequence'],
-                            'role' => 'user',
-                            'content' => $ctx['original_prompt'],
-                            'created_at' => $now,
-                            'updated_at' => $now,
-                        ],
-                        [
-                            'uid' => $ctx['uid'],
-                            'script_id' => $ctx['script_id'],
-                            'sequence' => $ctx['sequence'],
-                            'role' => 'assistant',
-                            'content' => $fullContent,
-                            'created_at' => $now,
-                            'updated_at' => $now,
-                        ],
-                    ]);
-                }
             }
         } catch (\Throwable $e) {
             if (!empty($ctx['generate_task_id'])) {
@@ -1568,6 +1597,11 @@ class DeepSeekService
                 dLog('deepseek')->error('保存剧本对话记录失败: ' . $e->getMessage());
             }
         }
+
+        // 未提供 script_id 或保存失败时,也返回 records 数组(保持返回结构一致)
+        if (!isset($aiResult['records'])) {
+            $aiResult['records'] = [];
+        }
         
         return $aiResult;
     }