AgentGrantController.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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\StoreAgentGrantRequest;
  6. use App\Http\Resources\V3\AgentGrantResource;
  7. use App\Services\V3\AgentGrantService;
  8. /**
  9. * agent 写入授权:人类授权自己名下的 AI 模型代写某个 channel。
  10. *
  11. * 取代 `POST /v2/access-token`(`AccessTokenController@store`)的 agent 用途;v2 那份在
  12. * dashboard-v4 下线前原样保留。区别见 {@see AgentGrantService}。
  13. */
  14. class AgentGrantController extends Controller
  15. {
  16. use ResolvesCurrentUser;
  17. public function __construct(private readonly AgentGrantService $grants) {}
  18. /**
  19. * 签发 agent 写入授权
  20. *
  21. * 必须用人类的登录 token 调用(模型 token 403);模型必须归当前用户所有,当前用户
  22. * 必须能编辑该 channel。授权 7 天有效,只能由指定的模型使用;撤销该模型的 token
  23. * (token_version 自增)时一并失效。
  24. *
  25. * 模型写入时用自己的模型 token 登录,并带请求头 `X-Agent-Grant: <token>`;
  26. * 写入的记录 editor 为授权人、co_editor 为模型,权限按授权人判断。
  27. *
  28. * @responseStatus 201 签发成功
  29. * @responseStatus 403 用模型 token 调用、模型不归当前用户,或当前用户不能编辑该 channel
  30. *
  31. * @bodyParam ai_model string required 被授权的 AI 模型 uid
  32. * @bodyParam target_type string required 授权范围的类型。Enum: channel
  33. * @bodyParam target_id string required channel 的 uid
  34. * @bodyParam book integer 限定 book,0 或不传表示不限。Default: 0
  35. */
  36. public function store(StoreAgentGrantRequest $request)
  37. {
  38. return AgentGrantResource::make($this->grants->issue(
  39. $this->currentUser($request)['user_uid'],
  40. $request->validated(),
  41. ))->response()->setStatusCode(201);
  42. }
  43. }