2
0

AiModelController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\StudioApi;
  4. use App\Http\Requests\StoreAiModelRequest;
  5. use App\Http\Requests\UpdateAiModelRequest;
  6. use App\Http\Resources\AiModelResource;
  7. use App\Models\AiModel;
  8. use App\Services\AuthService;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. use Illuminate\Support\Str;
  12. class AiModelController extends Controller
  13. {
  14. /**
  15. * 客户端可写的字段(name / privacy 另行处理:前者参与重名校验,后者建档时有默认值)。
  16. */
  17. private const EDITABLE_FIELDS = ['description', 'system_prompt', 'url', 'model', 'key'];
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return Response
  22. */
  23. public function index(Request $request)
  24. {
  25. //
  26. $user = AuthService::current($request);
  27. if (! $user) {
  28. return $this->error(__('auth.failed'), 401, 401);
  29. }
  30. switch ($request->input('view')) {
  31. case 'all':
  32. $table = AiModel::whereNotNull('owner_id');
  33. break;
  34. case 'studio':
  35. // 指定用户名下的记录
  36. $studioId = StudioApi::getIdByName($request->input('name'));
  37. $table = AiModel::where('owner_id', $studioId);
  38. break;
  39. case 'usable':
  40. $table = AiModel::where('owner_id', $request->input('user_id'))
  41. ->orWhere('privacy', 'public');
  42. break;
  43. case 'chat':
  44. $table = AiModel::where('owner_id', config('mint.admin.root_uuid'));
  45. break;
  46. }
  47. if ($request->has('keyword')) {
  48. $table = $table->where('name', 'like', '%'.$request->input('keyword').'%');
  49. }
  50. $count = $table->count();
  51. $table = $table->orderBy(
  52. $request->input('order', 'created_at'),
  53. $request->input('dir', 'asc')
  54. );
  55. $table = $table->skip($request->input('offset', 0))
  56. ->take($request->input('limit', 1000));
  57. $result = $table->get();
  58. return $this->ok(
  59. [
  60. 'rows' => AiModelResource::collection(resource: $result),
  61. 'count' => $count,
  62. ]
  63. );
  64. }
  65. /**
  66. * Store a newly created resource in storage.
  67. *
  68. * @return Response
  69. */
  70. public function store(StoreAiModelRequest $request)
  71. {
  72. //
  73. $user = AuthService::current($request);
  74. if (! $user) {
  75. return $this->error(__('auth.failed'), 401, 401);
  76. }
  77. $studioId = StudioApi::getIdByName($request->input('studio_name'));
  78. if (! self::canEdit($user['user_uid'], $studioId)) {
  79. return $this->error(__('auth.failed'), 403, 403);
  80. }
  81. // 同一 studio 内 name 必须唯一:客户端(wikipali-write Skill)靠 name 做幂等匹配,
  82. // 重名会让「查不到就创建」的流程反复建出同名记录
  83. $duplicated = AiModel::where('owner_id', $studioId)
  84. ->where('name', $request->input('name'))
  85. ->exists();
  86. if ($duplicated) {
  87. return $this->error(__('validation.unique', ['attribute' => 'name']), null, 409);
  88. }
  89. $new = new AiModel;
  90. $new->uid = Str::uuid();
  91. $new->real_name = Str::uuid();
  92. $new->owner_id = $studioId;
  93. $new->editor_id = $user['user_uid'];
  94. $new->name = $request->input('name');
  95. $new->privacy = $request->input('privacy', 'private');
  96. foreach (self::EDITABLE_FIELDS as $field) {
  97. if ($request->has($field)) {
  98. $new->{$field} = $request->input($field);
  99. }
  100. }
  101. $new->save();
  102. return $this->ok(new AiModelResource($new));
  103. }
  104. /**
  105. * Display the specified resource.
  106. *
  107. * @return Response
  108. */
  109. public function show(Request $request, AiModel $aiModel)
  110. {
  111. //
  112. $user = AuthService::current($request);
  113. if (! $user) {
  114. return $this->error(__('auth.failed'), 401, 401);
  115. }
  116. if (! self::canEdit($user['user_uid'], $aiModel->owner_id)) {
  117. return $this->error(__('auth.failed'), 403, 403);
  118. }
  119. return $this->ok(new AiModelResource($aiModel));
  120. }
  121. /**
  122. * Update the specified resource in storage.
  123. *
  124. * @return Response
  125. */
  126. public function update(UpdateAiModelRequest $request, AiModel $aiModel)
  127. {
  128. //
  129. $user = AuthService::current($request);
  130. if (! $user) {
  131. return $this->error(__('auth.failed'), 401, 401);
  132. }
  133. if (! self::canEdit($user['user_uid'], $aiModel->owner_id)) {
  134. return $this->error(__('auth.failed'), 403, 403);
  135. }
  136. if ($request->has('name')) {
  137. $duplicated = AiModel::where('owner_id', $aiModel->owner_id)
  138. ->where('name', $request->input('name'))
  139. ->where('uid', '<>', $aiModel->uid)
  140. ->exists();
  141. if ($duplicated) {
  142. return $this->error(__('validation.unique', ['attribute' => 'name']), null, 409);
  143. }
  144. }
  145. // 增量更新:只改请求里出现的字段。
  146. // 用 has() 而非 filled(),好让前端能把 description 之类的字段显式清空;
  147. // 但未提交的字段必须原样保留——否则客户端的局部 PUT 会把其余字段全置 null。
  148. foreach (array_merge(['name', 'privacy'], self::EDITABLE_FIELDS) as $field) {
  149. if ($request->has($field)) {
  150. $aiModel->{$field} = $request->input($field);
  151. }
  152. }
  153. $aiModel->editor_id = $user['user_uid'];
  154. $aiModel->save();
  155. return $this->ok(new AiModelResource($aiModel));
  156. }
  157. /**
  158. * Remove the specified resource from storage.
  159. *
  160. * @return Response
  161. */
  162. public function destroy(Request $request, AiModel $aiModel)
  163. {
  164. //
  165. $user = AuthService::current($request);
  166. if (! $user) {
  167. return $this->error(__('auth.failed'), 401, 401);
  168. }
  169. if (! self::canEdit($user['user_uid'], $aiModel->owner_id)) {
  170. return $this->error(__('auth.failed'), 403, 403);
  171. }
  172. $del = $aiModel->delete();
  173. return $this->ok($del);
  174. }
  175. public static function canEdit($user_uid, $owner_uid)
  176. {
  177. return $user_uid === $owner_uid;
  178. }
  179. }