AgentFeedbackController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Http\Controllers\V3;
  3. use App\Http\Controllers\Concerns\ResolvesCurrentUser;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Requests\V3\DestroyAgentFeedbackRequest;
  6. use App\Http\Requests\V3\StoreAgentFeedbackRequest;
  7. use App\Http\Resources\V3\AgentFeedbackResource;
  8. use App\Services\V3\AgentFeedbackService;
  9. /**
  10. * 用户对 AI 回答的评价(点赞 / 点踩)。可选登录:登录用户按账号,游客带设备匿名 id。
  11. * 新资源,没有 v2 前身。
  12. */
  13. class AgentFeedbackController extends Controller
  14. {
  15. use ResolvesCurrentUser;
  16. public function __construct(private readonly AgentFeedbackService $feedback) {}
  17. /**
  18. * 评价一条回答(再次提交即更新)
  19. *
  20. * 一人对一条回答只留一条:首次 201,更新 200。点赞的 tags 取 accurate / useful_citations / clear / inspiring,
  21. * 点踩的 tags 是原因:citation_wrong / missed / off_topic / incorrect / too_long / too_academic / slow / other;
  22. * 只有选了 other 时 comment 才会保存。asked=这条是被主动询问问出来的。
  23. *
  24. * @unauthenticated
  25. *
  26. * @responseStatus 201 首次评价。响应体与 200 同形
  27. *
  28. * @bodyParam message_id string required AG-UI 回答消息 id
  29. * @bodyParam thread_id string 会话 id(uuid)
  30. * @bodyParam rating integer required 1 赞 / -1 踩(只能是这两个值)。
  31. * @bodyParam tags array 点赞标签或点踩原因
  32. * @bodyParam comment string 选了 other 时的文字说明,最长 1000 字
  33. * @bodyParam asked boolean 是否主动询问问出来的
  34. * @bodyParam device_id string 设备匿名 id(uuid)。未登录时必填
  35. * @bodyParam client string 客户端。Enum: mobile,web
  36. */
  37. public function store(StoreAgentFeedbackRequest $request)
  38. {
  39. return AgentFeedbackResource::make($this->feedback->rate($this->currentUserUid($request), $request->validated()));
  40. }
  41. /**
  42. * 撤回评价
  43. *
  44. * 没评价过也返回 204(幂等)。
  45. *
  46. * @unauthenticated
  47. *
  48. * @urlParam message string required AG-UI 回答消息 id
  49. *
  50. * @queryParam device_id string 设备匿名 id(uuid)。未登录时必填
  51. *
  52. * @responseStatus 204 已撤回,无响应体
  53. */
  54. public function destroy(DestroyAgentFeedbackRequest $request, string $message)
  55. {
  56. $this->feedback->retract($this->currentUserUid($request), $message, $request->validated('device_id'));
  57. return response()->noContent();
  58. }
  59. }