|
|
@@ -512,6 +512,120 @@ class DeepSeekService
|
|
|
Utils::throwError('20003:不支持的模型: ' . $model);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public function saveScriptChatHistory($data) {
|
|
|
+ $uid = Site::getUid();
|
|
|
+ $script_id = getProp($data, 'script_id');
|
|
|
+ $sequence = getProp($data, 'sequence', 0);
|
|
|
+ $messages = getProp($data, 'messages', []);
|
|
|
+
|
|
|
+ if (!$script_id) {
|
|
|
+ Utils::throwError('20003:请选择剧本');
|
|
|
+ }
|
|
|
+
|
|
|
+ $script = DB::table('mp_scripts')->where('id', $script_id)->where('is_deleted', 0)->first();
|
|
|
+ if (!$script) {
|
|
|
+ Utils::throwError('20003:剧本不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析 messages(可能是 JSON 字符串或数组)
|
|
|
+ if (is_string($messages)) {
|
|
|
+ $messages = json_decode($messages, true);
|
|
|
+ if (json_last_error() !== JSON_ERROR_NONE) {
|
|
|
+ Utils::throwError('20003:消息格式错误');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!is_array($messages) || empty($messages)) {
|
|
|
+ Utils::throwError('20003:消息不能为空');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 提取 role 为 user 和 assistant 的消息并保存
|
|
|
+ $records = [];
|
|
|
+ $now = now();
|
|
|
+
|
|
|
+ foreach ($messages as $message) {
|
|
|
+ if (!isset($message['role']) || !isset($message['content'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只处理 user 和 assistant 角色
|
|
|
+ if (!in_array($message['role'], ['user', 'assistant'])) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ $records[] = [
|
|
|
+ 'uid' => $uid,
|
|
|
+ 'script_id' => $script_id,
|
|
|
+ 'sequence' => $sequence,
|
|
|
+ 'role' => $message['role'],
|
|
|
+ 'content' => is_array($message['content']) ? json_encode($message['content'], JSON_UNESCAPED_UNICODE) : $message['content'],
|
|
|
+ 'created_at' => $now,
|
|
|
+ 'updated_at' => $now,
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (empty($records)) {
|
|
|
+ Utils::throwError('20003:没有有效的对话记录');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量插入数据
|
|
|
+ return DB::table('mp_script_records')->insert($records);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取剧本的历史对话记录
|
|
|
+ *
|
|
|
+ * @param array $data 请求参数
|
|
|
+ * @return array 返回历史记录数组
|
|
|
+ */
|
|
|
+ public function getScriptChatHistory($data) {
|
|
|
+ $uid = Site::getUid();
|
|
|
+ $script_id = getProp($data, 'script_id');
|
|
|
+ $sequence = getProp($data, 'sequence', null);
|
|
|
+
|
|
|
+ if (!$script_id) {
|
|
|
+ Utils::throwError('20003:请提供剧本ID');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证剧本是否存在
|
|
|
+ $script = DB::table('mp_scripts')
|
|
|
+ ->where('id', $script_id)
|
|
|
+ ->where('is_deleted', 0)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if (!$script) {
|
|
|
+ Utils::throwError('20003:剧本不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建查询
|
|
|
+ $query = DB::table('mp_script_records')
|
|
|
+ ->where('script_id', $script_id);
|
|
|
+ // ->where('uid', $uid);
|
|
|
+
|
|
|
+ // 如果提供了 sequence,则过滤指定剧集
|
|
|
+ if ($sequence !== null) {
|
|
|
+ $query->where('sequence', $sequence);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询记录并按 id 正序排序
|
|
|
+ $records = $query->select('script_id', 'sequence', 'role', 'content', 'created_at')
|
|
|
+ ->orderBy('created_at', 'asc')
|
|
|
+ ->orderBy('id', 'asc')
|
|
|
+ ->get()
|
|
|
+ ->map(function ($record) {
|
|
|
+ return [
|
|
|
+ 'script_id' => $record->script_id,
|
|
|
+ 'sequence' => $record->sequence,
|
|
|
+ 'role' => $record->role,
|
|
|
+ 'content' => $record->content,
|
|
|
+ 'created_at' => $record->created_at,
|
|
|
+ ];
|
|
|
+ })
|
|
|
+ ->toArray();
|
|
|
+
|
|
|
+ return $records;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 处理图片转换为base64
|
|
|
@@ -2006,6 +2120,7 @@ Q版卡通风格,头大身小,造型圆润可爱,线条简单,色彩明
|
|
|
public function createScript($data) {
|
|
|
$script_name = getProp($data, 'script_name');
|
|
|
$uid = Site::getUid(); // 获取当前用户ID
|
|
|
+ $cpid = Site::getCpid(); // 获取当前公司组ID
|
|
|
if (!$script_name) Utils::throwError('20003:剧本名称不能为空');
|
|
|
|
|
|
if (DB::table('mp_scripts')->where('script_name', $script_name)->exists()) {
|
|
|
@@ -2015,6 +2130,7 @@ Q版卡通风格,头大身小,造型圆润可爱,线条简单,色彩明
|
|
|
return DB::table('mp_scripts')->insertGetId([
|
|
|
'script_name' => $script_name,
|
|
|
'user_id' => $uid,
|
|
|
+ 'cpid' => $cpid,
|
|
|
'created_at' => date('Y-m-d H:i:s'),
|
|
|
'updated_at' => date('Y-m-d H:i:s')
|
|
|
]);
|