AgentTraceV3Test.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. use App\Models\AgentRun;
  3. use App\Models\AgentThread;
  4. use App\Models\AiModel;
  5. use App\Services\AuthService;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Illuminate\Support\Str;
  8. uses(RefreshDatabase::class);
  9. /**
  10. * v3 agent-traces:AI 对话研究过程入库(服务间接口)。数据永久保留,没有清理命令。
  11. *
  12. * POST /api/v3/agent-traces X-Agent-Service-Key 必带;Bearer 是透传的终端用户 token(可选)
  13. *
  14. * 这里断言的是形状而非契约(spectator 尚未安装,见 v3-resource skill 第 3 步)。
  15. */
  16. beforeEach(function () {
  17. config(['mint.app.agent_service_key' => 'test-agent-key']);
  18. });
  19. function agentHeaders(?string $userUid = null): array
  20. {
  21. $headers = ['X-Agent-Service-Key' => 'test-agent-key'];
  22. return $userUid ? $headers + authHeader($userUid) : $headers;
  23. }
  24. function tracePayload(array $overrides = []): array
  25. {
  26. return array_merge([
  27. 'run_id' => (string) Str::uuid(),
  28. 'thread_id' => (string) Str::uuid(),
  29. 'device_id' => (string) Str::uuid(),
  30. 'client_ip' => '203.0.113.7',
  31. 'client' => 'mobile',
  32. 'question' => '什么是四圣谛?',
  33. 'deep' => false,
  34. 'improve' => true,
  35. 'route' => 'question',
  36. 'plan' => ['lemmas' => ['ariyasacca']],
  37. 'evidence' => '证据全文',
  38. 'answer' => '四圣谛是……',
  39. 'citations' => [['link' => 'https://wikipali.org/library/tipitaka/203-1677/read#1702']],
  40. 'llm_calls' => 2,
  41. 'tool_calls' => 3,
  42. 'status' => 'ok',
  43. 'user_message_id' => 'msg-u-1',
  44. 'assistant_message_id' => 'msg-a-1',
  45. 'started_at' => now()->toIso8601String(),
  46. 'steps' => [
  47. ['seq' => 0, 'kind' => 'route', 'args' => ['route' => 'question']],
  48. ['seq' => 1, 'kind' => 'tool', 'name' => 'wikipali_search', 'args' => ['q' => 'ariyasacca'], 'result' => '…', 'result_chars' => 9000, 'ms' => 120],
  49. ['seq' => 2, 'kind' => 'llm', 'name' => 'deepseek-chat', 'tokens_in' => 1000, 'tokens_out' => 300, 'ms' => 2000],
  50. ],
  51. ], $overrides);
  52. }
  53. /* ------------------------------------------------------------------ 鉴权 */
  54. it('rejects a request without the service key', function () {
  55. $this->postJson('/api/v3/agent-traces', tracePayload())->assertUnauthorized();
  56. });
  57. it('rejects a wrong service key', function () {
  58. $this->postJson('/api/v3/agent-traces', tracePayload(), ['X-Agent-Service-Key' => 'nope'])
  59. ->assertUnauthorized();
  60. });
  61. it('rejects everything when no service key is configured', function () {
  62. config(['mint.app.agent_service_key' => '']);
  63. $this->postJson('/api/v3/agent-traces', tracePayload(), ['X-Agent-Service-Key' => ''])
  64. ->assertUnauthorized();
  65. });
  66. it('rejects an end-user token without the service key', function () {
  67. $this->postJson('/api/v3/agent-traces', tracePayload(), authHeader((string) Str::uuid()))
  68. ->assertUnauthorized();
  69. });
  70. /* ------------------------------------------------------------------ 游客 */
  71. it('records a guest run with device id and ip', function () {
  72. $payload = tracePayload();
  73. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders())
  74. ->assertCreated()
  75. ->assertJsonPath('data.run_id', $payload['run_id'])
  76. ->assertJsonPath('data.thread_id', $payload['thread_id'])
  77. ->assertJsonPath('data.account_type', 'guest');
  78. $run = AgentRun::where('run_id', $payload['run_id'])->firstOrFail();
  79. expect($run->user_uid)->toBeNull()
  80. ->and($run->device_id)->toBe($payload['device_id'])
  81. ->and($run->client_ip)->toBe('203.0.113.7')
  82. ->and($run->plan)->toBe(['lemmas' => ['ariyasacca']])
  83. ->and($run->steps()->count())->toBe(3)
  84. ->and($run->thread->account)->toBe('guest:'.$payload['device_id'])
  85. ->and($run->thread->title)->toBe('什么是四圣谛?');
  86. });
  87. it('requires a device id from a guest', function () {
  88. $this->postJson('/api/v3/agent-traces', tracePayload(['device_id' => null]), agentHeaders())
  89. ->assertUnprocessable()
  90. ->assertJsonValidationErrors('device_id');
  91. });
  92. it('falls back to guest when the user token is invalid', function () {
  93. $payload = tracePayload();
  94. $this->postJson('/api/v3/agent-traces', $payload, [
  95. 'X-Agent-Service-Key' => 'test-agent-key',
  96. 'Authorization' => 'Bearer not-a-jwt',
  97. ])->assertCreated()->assertJsonPath('data.account_type', 'guest');
  98. });
  99. it('treats a model token as a guest', function () {
  100. $human = makeStudio('owner');
  101. $model = AiModel::factory()->ownedBy($human)->create();
  102. $payload = tracePayload();
  103. $this->postJson('/api/v3/agent-traces', $payload, [
  104. 'X-Agent-Service-Key' => 'test-agent-key',
  105. 'Authorization' => 'Bearer '.AuthService::getUserToken($model->uid),
  106. ])->assertCreated()->assertJsonPath('data.account_type', 'guest');
  107. expect(AgentRun::where('run_id', $payload['run_id'])->value('user_uid'))->toBeNull();
  108. });
  109. /* ------------------------------------------------------------------ 登录用户 */
  110. it('records a user run without device id or ip', function () {
  111. $user = (string) Str::uuid();
  112. $payload = tracePayload();
  113. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders($user))
  114. ->assertCreated()
  115. ->assertJsonPath('data.account_type', 'user');
  116. $run = AgentRun::where('run_id', $payload['run_id'])->firstOrFail();
  117. expect($run->user_uid)->toBe($user)
  118. ->and($run->device_id)->toBeNull()
  119. ->and($run->client_ip)->toBeNull()
  120. ->and($run->thread->account)->toBe('user:'.$user);
  121. });
  122. it('records a run even when the user opted out of improvement', function () {
  123. $payload = tracePayload(['improve' => false]);
  124. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders())->assertCreated();
  125. expect(AgentRun::where('run_id', $payload['run_id'])->value('improve'))->toBeFalse();
  126. });
  127. it('rejects a thread id that belongs to another account', function () {
  128. $user = (string) Str::uuid();
  129. $thread = (string) Str::uuid();
  130. $this->postJson('/api/v3/agent-traces', tracePayload(['thread_id' => $thread]), agentHeaders())->assertCreated();
  131. $this->postJson('/api/v3/agent-traces', tracePayload(['thread_id' => $thread]), agentHeaders($user))
  132. ->assertForbidden()
  133. ->assertJsonPath('status', 403);
  134. expect(AgentThread::where('thread_id', $thread)->count())->toBe(1)
  135. ->and(AgentThread::where('thread_id', $thread)->value('account'))->toStartWith('guest:');
  136. });
  137. it('appends runs to the same thread and advances last_run_at', function () {
  138. $user = (string) Str::uuid();
  139. $thread = (string) Str::uuid();
  140. $this->postJson('/api/v3/agent-traces', tracePayload([
  141. 'thread_id' => $thread, 'started_at' => now()->subHour()->toIso8601String(),
  142. ]), agentHeaders($user))->assertCreated();
  143. $this->postJson('/api/v3/agent-traces', tracePayload([
  144. 'thread_id' => $thread, 'question' => '第二个问题',
  145. ]), agentHeaders($user))->assertCreated();
  146. $row = AgentThread::where('thread_id', $thread)->firstOrFail();
  147. expect($row->runs()->count())->toBe(2)
  148. ->and($row->title)->toBe('什么是四圣谛?')
  149. ->and($row->last_run_at->gt(now()->subMinutes(5)))->toBeTrue();
  150. });
  151. it('truncates a long first question into the title', function () {
  152. $payload = tracePayload(['question' => str_repeat('缘', 80)]);
  153. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders())->assertCreated();
  154. expect(AgentThread::where('thread_id', $payload['thread_id'])->value('title'))
  155. ->toBe(str_repeat('缘', 60).'…');
  156. });
  157. /* ------------------------------------------------------------------ 幂等与校验 */
  158. it('is idempotent on run_id', function () {
  159. $payload = tracePayload();
  160. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders())->assertCreated();
  161. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders())->assertOk();
  162. expect(AgentRun::where('run_id', $payload['run_id'])->count())->toBe(1)
  163. ->and(AgentRun::where('run_id', $payload['run_id'])->first()->steps()->count())->toBe(3);
  164. });
  165. it('keeps a deleted run deleted when the same trace is sent again', function () {
  166. $payload = tracePayload();
  167. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders())->assertCreated();
  168. $run = AgentRun::where('run_id', $payload['run_id'])->first();
  169. $run->steps()->delete();
  170. $run->delete();
  171. // agent 端补发同一条:不重建、不复活
  172. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders())->assertOk();
  173. expect(AgentRun::withTrashed()->where('run_id', $payload['run_id'])->count())->toBe(1)
  174. ->and(AgentRun::where('run_id', $payload['run_id'])->exists())->toBeFalse();
  175. });
  176. it('links a regenerated answer to the round it replaces and hides the old one from history', function () {
  177. $user = (string) Str::uuid();
  178. $thread = (string) Str::uuid();
  179. $first = tracePayload(['thread_id' => $thread, 'assistant_message_id' => 'msg-a-old', 'answer' => '旧回答']);
  180. $this->postJson('/api/v3/agent-traces', $first, agentHeaders($user))->assertCreated();
  181. $again = tracePayload(['thread_id' => $thread, 'assistant_message_id' => 'msg-a-new', 'answer' => '新回答', 'replaces_message_id' => 'msg-a-old']);
  182. $this->postJson('/api/v3/agent-traces', $again, agentHeaders($user))->assertCreated();
  183. expect(AgentRun::where('run_id', $again['run_id'])->value('replaces_run_id'))->toBe($first['run_id']);
  184. $this->getJson("/api/v3/me/agent-threads/{$thread}", authHeader($user))
  185. ->assertOk()
  186. ->assertJsonCount(1, 'data.runs')
  187. ->assertJsonPath('data.runs.0.answer', '新回答');
  188. });
  189. it('records a denied run that never reached the agent', function () {
  190. $payload = tracePayload(['status' => 'denied', 'answer' => null, 'evidence' => null, 'steps' => []]);
  191. $this->postJson('/api/v3/agent-traces', $payload, agentHeaders())->assertCreated();
  192. expect(AgentRun::where('run_id', $payload['run_id'])->value('status'))->toBe('denied');
  193. });
  194. it('rejects an unknown status', function () {
  195. $this->postJson('/api/v3/agent-traces', tracePayload(['status' => 'weird']), agentHeaders())
  196. ->assertUnprocessable()
  197. ->assertJsonValidationErrors('status');
  198. });
  199. it('rejects an unknown step kind', function () {
  200. $this->postJson('/api/v3/agent-traces', tracePayload([
  201. 'steps' => [['seq' => 0, 'kind' => 'bogus']],
  202. ]), agentHeaders())
  203. ->assertUnprocessable()
  204. ->assertJsonValidationErrors('steps.0.kind');
  205. });