AgentFeedbackService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Models\AgentEvent;
  4. use App\Models\AgentFeedback;
  5. use App\Models\AgentRun;
  6. use Illuminate\Contracts\Pagination\LengthAwarePaginator;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Validation\ValidationException;
  9. /**
  10. * AI 回答的用户评价与隐式信号。
  11. *
  12. * 一人对一条回答只留一条评价:登录用户按账号、游客按设备匿名 id(同研究过程入库的口径,
  13. * 见 {@see AgentAccountService})。再点就是更新,可撤回。评价挂在 AG-UI 回答消息 id 上,
  14. * 不要求那次提问已经入库。
  15. *
  16. * 评价即同意把这次问答用于改进(agent_runs.improve 置为 true),见 {@see rate()}。
  17. */
  18. class AgentFeedbackService
  19. {
  20. public function __construct(private readonly AgentAccountService $accounts) {}
  21. /**
  22. * 写入或更新评价。
  23. *
  24. * @param array{message_id: string, thread_id?: string|null, rating: int, tags?: list<string>|null, comment?: string|null, asked?: bool, device_id?: string|null, client?: string|null} $data
  25. *
  26. * @throws ValidationException 标签与评分不配套,或游客没带 device_id
  27. */
  28. public function rate(?string $bearerUid, array $data): AgentFeedback
  29. {
  30. $account = $this->accounts->resolve($bearerUid, $data['device_id'] ?? null);
  31. $tags = array_values(array_unique($data['tags'] ?? []));
  32. $allowed = $data['rating'] > 0 ? AgentFeedback::UP_TAGS : AgentFeedback::DOWN_REASONS;
  33. if (array_diff($tags, $allowed) !== []) {
  34. throw ValidationException::withMessages(['tags' => __('messages.agent_feedback_tags')]);
  35. }
  36. // 文字说明只跟「其他」走
  37. $comment = in_array('other', $tags, true) ? (trim((string) ($data['comment'] ?? '')) ?: null) : null;
  38. $feedback = AgentFeedback::firstOrNew([...$account->columns(), 'message_id' => $data['message_id']]);
  39. $feedback->fill([
  40. 'thread_id' => $data['thread_id'] ?? $feedback->thread_id,
  41. 'rating' => $data['rating'] > 0 ? 1 : -1,
  42. 'tags' => $tags ?: null,
  43. 'comment' => $comment,
  44. // 一旦是被主动询问问出来的,就一直算
  45. 'asked' => $feedback->asked || (bool) ($data['asked'] ?? false),
  46. 'client' => $data['client'] ?? $feedback->client,
  47. ])->save();
  48. // 评价=同意把这次问答用于改进:客户端在「用于改进」关着时先弹窗征得同意,
  49. // 不同意就不让评价。所以这条提问即使当时记的是 improve=false,也改成 true(只改本人自己的提问)
  50. AgentRun::where('assistant_message_id', $data['message_id'])
  51. ->where('improve', false)
  52. ->whereHas('thread', fn ($q) => $q->ownedBy($account))
  53. ->update(['improve' => true]);
  54. return $feedback;
  55. }
  56. /**
  57. * 撤回评价。没评价过也算成功(幂等)。
  58. *
  59. * @throws ValidationException 游客没带 device_id
  60. */
  61. public function retract(?string $bearerUid, string $messageId, ?string $deviceId): void
  62. {
  63. $account = $this->accounts->resolve($bearerUid, $deviceId);
  64. AgentFeedback::ownedBy($account)->where('message_id', $messageId)->delete();
  65. }
  66. /**
  67. * 记一条隐式信号。
  68. *
  69. * @param array{message_id: string, kind: string, device_id?: string|null, client?: string|null} $data
  70. */
  71. public function event(?string $bearerUid, array $data): AgentEvent
  72. {
  73. $account = $this->accounts->resolve($bearerUid, $data['device_id'] ?? null);
  74. return AgentEvent::create([
  75. ...$account->columns(),
  76. 'message_id' => $data['message_id'],
  77. 'kind' => $data['kind'],
  78. 'client' => $data['client'] ?? null,
  79. ]);
  80. }
  81. /**
  82. * 管理员审阅:评价列表,带对应提问的摘要(问题、路由、状态、质量指标)。最新的在前。
  83. *
  84. * @param array{filter?: array{rating?: string, tag?: string, has_comment?: bool|string}, from?: string, to?: string} $filters
  85. * @return LengthAwarePaginator<int, object>
  86. */
  87. public function review(array $filters, int $perPage): LengthAwarePaginator
  88. {
  89. $f = $filters['filter'] ?? [];
  90. return DB::table('agent_feedback as f')
  91. // 软删的提问读不到:评价还在,但不再带出那次问答
  92. ->leftJoin('agent_runs as r', fn ($j) => $j->on('r.assistant_message_id', '=', 'f.message_id')->whereNull('r.deleted_at'))
  93. ->select([
  94. 'f.id', 'f.message_id', 'f.rating', 'f.tags', 'f.comment', 'f.asked', 'f.client', 'f.created_at', 'f.updated_at',
  95. 'f.account_type', 'r.run_id', 'r.question', 'r.route', 'r.status', 'r.deep', 'r.latency_ms',
  96. ])
  97. ->when(isset($f['rating']), fn ($q) => $q->where('f.rating', (int) $f['rating'] > 0 ? 1 : -1))
  98. ->when($f['tag'] ?? null, fn ($q, $tag) => $q->whereRaw('jsonb_exists(f.tags, ?)', [$tag]))
  99. ->when(isset($f['has_comment']) && filter_var($f['has_comment'], FILTER_VALIDATE_BOOLEAN), fn ($q) => $q->whereNotNull('f.comment'))
  100. ->when($filters['from'] ?? null, fn ($q, $d) => $q->where('f.updated_at', '>=', $d))
  101. ->when($filters['to'] ?? null, fn ($q, $d) => $q->where('f.updated_at', '<', now('UTC')->parse($d)->addDay()))
  102. ->orderByDesc('f.updated_at')
  103. ->orderByDesc('f.id')
  104. ->paginate($perPage)
  105. ->through(function ($row) {
  106. $row->tags = $row->tags ? json_decode($row->tags, true) : [];
  107. return $row;
  108. });
  109. }
  110. }