AgentRun.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Concerns\OwnedByAgentAccount;
  4. use App\Services\V3\AgentThreadService;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\HasMany;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. /**
  12. * AI 对话的一次提问(AG-UI run)及其研究过程汇总。
  13. *
  14. * 软删除:随会话一起软删(用户删除会话,见 {@see AgentThreadService::hide()}),
  15. * 软删后任何接口都读不到(将来分享对话也一样),数据永久保留。用 `DB::table('agent_runs')` 查时要自己加
  16. * `whereNull('deleted_at')`。
  17. */
  18. class AgentRun extends Model
  19. {
  20. use HasFactory, OwnedByAgentAccount, SoftDeletes;
  21. /**
  22. * denied:额度不足、没有作答——照样入库,防滥用要看得到被拦下的提问。
  23. *
  24. * @var list<string>
  25. */
  26. public const STATUSES = ['ok', 'error', 'cancelled', 'denied'];
  27. /**
  28. * 研究路径(agent-poc `backend/routing.py` 的路由结果):question 一般问题、term 术语 / 词、
  29. * passage 阅读页提问、chat 闲聊;还没路由就结束(额度拦截)时为 null。
  30. *
  31. * 唯一来源:Resource 的 `@return` 与控制器的 `@queryParam … Enum:` 里写的是同一组字面量(OpenAPI 据此出枚举,
  32. * 前端画图用),由 tests/Feature/AgentRouteEnumTest.php 守住一致;backend 的离线测试拿 OpenAPI 规格核对
  33. * routing.py 会产生的路由。入库时不认识的路由照样存、只记警告,不丢研究过程。
  34. *
  35. * @var list<string>
  36. */
  37. public const ROUTES = ['question', 'term', 'passage', 'chat'];
  38. protected $guarded = ['id'];
  39. protected $casts = [
  40. 'passage' => 'array',
  41. 'plan' => 'array',
  42. 'citations' => 'array',
  43. 'deep' => 'boolean',
  44. 'improve' => 'boolean',
  45. 'confidence' => 'float',
  46. 'started_at' => 'datetime',
  47. ];
  48. /**
  49. * @return BelongsTo<AgentThread, $this>
  50. */
  51. public function thread(): BelongsTo
  52. {
  53. return $this->belongsTo(AgentThread::class, 'agent_thread_id')->withTrashed();
  54. }
  55. /**
  56. * 提问的登录用户(游客为空)。uuid 列对 user_infos.userid,预加载用 UserService::eagerLoadActor() 的列白名单。
  57. *
  58. * @return BelongsTo<UserInfo, $this>
  59. */
  60. public function user(): BelongsTo
  61. {
  62. // 只对 account_type = user 有意义:游客的 account_id 是设备 id,碰巧与某个用户 uid 相同也不能当成他
  63. return $this->belongsTo(UserInfo::class, 'account_id', 'userid');
  64. }
  65. /**
  66. * @return HasMany<AgentRunStep, $this>
  67. */
  68. public function steps(): HasMany
  69. {
  70. return $this->hasMany(AgentRunStep::class)->orderBy('seq');
  71. }
  72. /**
  73. * 只留用户看得见的那一轮:被「重新生成」替换掉的旧轮次不算。
  74. *
  75. * @param Builder<AgentRun> $query
  76. */
  77. public function scopeVisible(Builder $query): void
  78. {
  79. $query->whereNotExists(function ($sub) {
  80. $sub->selectRaw('1')
  81. ->from('agent_runs as newer')
  82. ->whereNull('newer.deleted_at')
  83. ->whereColumn('newer.agent_thread_id', 'agent_runs.agent_thread_id')
  84. ->whereColumn('newer.replaces_run_id', 'agent_runs.run_id');
  85. });
  86. }
  87. }