AdminAgentRunResource.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Http\Resources\V3;
  3. use App\Http\Resources\Concerns\ResolvesActor;
  4. use App\Models\AgentRun;
  5. use Illuminate\Http\Request;
  6. /**
  7. * 管理员看到的一次提问。列表里只给摘要;详情(show)另带回答、证据、计划、引用与全部步骤。
  8. *
  9. * 游客只有设备匿名 id 与 IP;提问原文对 root / administrator 可见。**本类不查库**:
  10. * thread、user、steps 由 Service 预加载。
  11. *
  12. * @mixin AgentRun
  13. */
  14. class AdminAgentRunResource extends BaseResource
  15. {
  16. use ResolvesActor;
  17. /**
  18. * @param Request $request
  19. * @return array{
  20. * run_id: string,
  21. * thread_id: string|null,
  22. * thread_title: string|null,
  23. * account_type: 'user'|'guest',
  24. * user: array{id: string, nickName: string, userName: string, realName: string, sn: int, avatar: string|null, roles: array|null}|null,
  25. * device_id: string|null,
  26. * client_ip: string|null,
  27. * client: string|null,
  28. * app_version: string|null,
  29. * question: string,
  30. * route: 'question'|'term'|'passage'|'chat'|null,
  31. * deep: bool,
  32. * improve: bool,
  33. * status: string,
  34. * latency_ms: int|null,
  35. * tokens_in: int|null,
  36. * tokens_out: int|null,
  37. * llm_calls: int,
  38. * tool_calls: int,
  39. * started_at: string|null,
  40. * answer?: string|null,
  41. * evidence?: string|null,
  42. * plan?: object|null,
  43. * passage?: object|null,
  44. * citations?: array<int, object>|null,
  45. * error?: string|null,
  46. * model?: string|null,
  47. * prompt_version?: string|null,
  48. * steps?: array<int, array{seq: int, kind: string, name: string|null, args: object|null, result: string|null, result_chars: int|null, tokens_in: int|null, tokens_out: int|null, ms: int|null, error: string|null}>
  49. * }
  50. */
  51. public function toArray($request): array
  52. {
  53. $detail = $this->relationLoaded('steps');
  54. return [
  55. 'run_id' => $this->run_id,
  56. 'thread_id' => $this->thread?->thread_id,
  57. 'thread_title' => $this->thread?->title,
  58. 'account_type' => $this->account_type,
  59. // 只有登录用户才查得到人;游客的 account_id 是设备 id,不能拿去当用户 uid
  60. 'user' => $this->account_type === 'user' ? $this->actor($this->user) : null,
  61. 'device_id' => $this->account_type === 'guest' ? $this->account_id : null,
  62. 'client_ip' => $this->client_ip,
  63. 'client' => $this->client,
  64. 'app_version' => $this->app_version,
  65. 'question' => $this->question,
  66. 'route' => $this->route,
  67. 'deep' => $this->deep,
  68. 'improve' => $this->improve,
  69. 'status' => $this->status,
  70. 'latency_ms' => $this->latency_ms,
  71. 'tokens_in' => $this->tokens_in,
  72. 'tokens_out' => $this->tokens_out,
  73. 'llm_calls' => $this->llm_calls,
  74. 'tool_calls' => $this->tool_calls,
  75. 'started_at' => $this->started_at,
  76. $this->mergeWhen($detail, fn () => [
  77. 'answer' => $this->answer,
  78. 'evidence' => $this->evidence,
  79. 'plan' => $this->plan,
  80. 'passage' => $this->passage,
  81. 'citations' => $this->citations,
  82. 'error' => $this->error,
  83. 'model' => $this->model,
  84. 'prompt_version' => $this->prompt_version,
  85. 'steps' => $this->steps->map(fn ($s) => [
  86. 'seq' => $s->seq,
  87. 'kind' => $s->kind,
  88. 'name' => $s->name,
  89. 'args' => $s->args,
  90. 'result' => $s->result,
  91. 'result_chars' => $s->result_chars,
  92. 'tokens_in' => $s->tokens_in,
  93. 'tokens_out' => $s->tokens_out,
  94. 'ms' => $s->ms,
  95. 'error' => $s->error,
  96. ])->all(),
  97. ]),
  98. ];
  99. }
  100. }