AgentThreadService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Models\AgentRun;
  4. use App\Models\AgentRunStep;
  5. use App\Models\AgentThread;
  6. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  7. use Illuminate\Database\Eloquent\ModelNotFoundException;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 登录用户的 AI 对话会话历史:列表、详情、改名、存档、删除。
  11. *
  12. * 只认登录用户的账号(account_type = user)——游客没有历史接口(设备 id 不是秘密,不能凭它读)。
  13. * 别人的会话与不存在的会话一样回 404,不暴露「这个 thread 有主」。
  14. */
  15. class AgentThreadService
  16. {
  17. /**
  18. * 我的会话列表,最近提问的在前。用户删除过的不列。
  19. *
  20. * @param array{filter?: array{archived?: bool|string}} $filters
  21. * @return LengthAwarePaginator<int, AgentThread>
  22. */
  23. public function listForUser(string $userUid, array $filters, int $perPage): LengthAwarePaginator
  24. {
  25. $archived = $filters['filter']['archived'] ?? null;
  26. return AgentThread::ownedBy(AgentAccount::user($userUid))
  27. ->when($archived !== null, fn ($q) => filter_var($archived, FILTER_VALIDATE_BOOLEAN)
  28. ? $q->whereNotNull('archived_at')
  29. : $q->whereNull('archived_at'))
  30. ->orderByDesc('last_run_at')
  31. ->orderByDesc('id')
  32. ->paginate($perPage);
  33. }
  34. /**
  35. * 一个会话及其全部可见的问答轮次(被「重新生成」替换掉的不算),按提问先后排。
  36. */
  37. public function showForUser(string $userUid, string $threadId): AgentThread
  38. {
  39. return $this->findForUser($userUid, $threadId)->load([
  40. 'runs' => fn ($q) => $q->visible()->orderBy('started_at')->orderBy('id'),
  41. ]);
  42. }
  43. /**
  44. * @param array{title?: string} $data
  45. */
  46. public function update(string $userUid, string $threadId, array $data): AgentThread
  47. {
  48. $thread = $this->findForUser($userUid, $threadId);
  49. $thread->fill($data)->save();
  50. return $thread;
  51. }
  52. /**
  53. * 存档 / 取消存档。重复存档保留第一次的时间。
  54. */
  55. public function setArchived(string $userUid, string $threadId, bool $archived): AgentThread
  56. {
  57. $thread = $this->findForUser($userUid, $threadId);
  58. if ($archived && $thread->archived_at === null) {
  59. $thread->archived_at = now();
  60. } elseif (! $archived) {
  61. $thread->archived_at = null;
  62. }
  63. $thread->save();
  64. return $thread;
  65. }
  66. /**
  67. * 删除=软删会话,连同它的提问与步骤一起软删:之后任何接口(含将来的分享)都读不到,
  68. * 数据永久保留(研究过程要留着防滥用、改进回答)。
  69. */
  70. public function hide(string $userUid, string $threadId): AgentThread
  71. {
  72. $thread = $this->findForUser($userUid, $threadId);
  73. DB::transaction(function () use ($thread) {
  74. $runIds = AgentRun::where('agent_thread_id', $thread->id)->pluck('id');
  75. AgentRunStep::whereIn('agent_run_id', $runIds)->delete();
  76. AgentRun::whereIn('id', $runIds)->delete();
  77. $thread->delete();
  78. });
  79. return $thread;
  80. }
  81. /**
  82. * @throws ModelNotFoundException 不存在、不是我的、或已删除 → 404
  83. */
  84. private function findForUser(string $userUid, string $threadId): AgentThread
  85. {
  86. return AgentThread::ownedBy(AgentAccount::user($userUid))
  87. ->where('thread_id', $threadId)
  88. ->firstOrFail();
  89. }
  90. }