2
0

AgentThread.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Concerns\OwnedByAgentAccount;
  4. use App\Services\V3\AgentAccountService;
  5. use App\Services\V3\AgentThreadService;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\HasMany;
  9. use Illuminate\Database\Eloquent\SoftDeletes;
  10. /**
  11. * AI 对话的一个会话(AG-UI thread)。
  12. *
  13. * 归属由 account_type(user / guest)+ account_id(用户 uuid / 设备匿名 uuid)表达,按账号查询走
  14. * `ownedBy()`({@see OwnedByAgentAccount})。
  15. * thread_id(AG-UI threadId)由客户端生成、全局唯一,属于别的账号就拒绝写入(403,
  16. * {@see AgentAccountService::assertThreadOwnable()})。游客登录 / 退出后
  17. * 客户端换新的 threadId——游客会话不并入账号。
  18. *
  19. * 软删除=用户在历史里删掉了它:对用户隐藏,它的提问与步骤一起软删({@see AgentThreadService::hide()}),
  20. * 数据永久保留。外键不级联:数据库层面不做任何真删除。
  21. */
  22. class AgentThread extends Model
  23. {
  24. use HasFactory, OwnedByAgentAccount, SoftDeletes;
  25. public const CLIENTS = ['mobile', 'web'];
  26. /**
  27. * 客户端能改的只有 title(UpdateMeAgentThreadRequest 只放行它);其余列由 Service 写。
  28. */
  29. protected $guarded = ['id'];
  30. protected $casts = [
  31. 'archived_at' => 'datetime',
  32. 'last_run_at' => 'datetime',
  33. ];
  34. /**
  35. * @return HasMany<AgentRun, $this>
  36. */
  37. public function runs(): HasMany
  38. {
  39. return $this->hasMany(AgentRun::class);
  40. }
  41. }