client response

This commit is contained in:
Jethro Lin 2024-12-04 13:51:51 +08:00
parent 74790f6180
commit e1119a9426

View file

@ -38,17 +38,12 @@ public function __construct(
public function index(): JsonResponse public function index(): JsonResponse
{ {
try { try {
$clients = Client::with('llmProvider:id,name,service_name') $clients = Client::select([
->select([ 'id',
'id', 'name',
'name', 'llm_provider_id',
'llm_provider_id', 'created_at',
'rate_limit', ])->get();
'timeout',
'status',
'created_at',
])
->get();
return $this->success($clients); return $this->success($clients);
@ -83,21 +78,12 @@ public function store(Request $request): JsonResponse
'integer', 'integer',
'exists:llm_providers,id', 'exists:llm_providers,id',
], ],
'rate_limit' => [
'required',
'integer',
'min:1',
'max:1000',
],
'timeout' => [
'required',
'integer',
'min:1',
'max:300',
],
]); ]);
$validated['status'] = Client::STATUS_ACTIVE; $validated['status'] = Client::STATUS_ACTIVE;
$validated['rate_limit'] = config('llm.default_rate_limit', 60); // 默認每分鐘 60 次
$validated['timeout'] = config('llm.default_timeout', 30); // 默認 30 秒
$client = Client::create($validated); $client = Client::create($validated);
// 加載 LLM 提供商信息 // 加載 LLM 提供商信息
@ -113,14 +99,6 @@ public function store(Request $request): JsonResponse
'id' => $client->id, 'id' => $client->id,
'name' => $client->name, 'name' => $client->name,
'llm_provider_id' => $client->llm_provider_id, 'llm_provider_id' => $client->llm_provider_id,
'llm_provider' => [
'id' => $client->llmProvider->id,
'name' => $client->llmProvider->name,
'service_name' => $client->llmProvider->service_name,
],
'rate_limit' => $client->rate_limit,
'timeout' => $client->timeout,
'status' => $client->status,
'created_at' => $client->created_at->toIso8601String(), 'created_at' => $client->created_at->toIso8601String(),
]); ]);
@ -171,36 +149,8 @@ public function update(Request $request, int $id): JsonResponse
'integer', 'integer',
'exists:llm_providers,id', 'exists:llm_providers,id',
], ],
'rate_limit' => [
'required',
'integer',
'min:1',
'max:1000',
],
'timeout' => [
'required',
'integer',
'min:1',
'max:300',
],
'status' => [
'required',
'string',
'in:active,inactive',
],
]); ]);
// 如果要停用客戶,檢查是否有活躍的令牌
if ($validated['status'] === Client::STATUS_INACTIVE &&
$client->status === Client::STATUS_ACTIVE) {
if ($this->hasActiveTokens($client)) {
return $this->error(
ErrorCode::CLIENT_HAS_ACTIVE_TOKENS,
'該客戶有活躍的認證令牌,無法停用。'
);
}
}
$client->update($validated); $client->update($validated);
// 加載 LLM 提供商信息 // 加載 LLM 提供商信息
@ -216,14 +166,6 @@ public function update(Request $request, int $id): JsonResponse
'id' => $client->id, 'id' => $client->id,
'name' => $client->name, 'name' => $client->name,
'llm_provider_id' => $client->llm_provider_id, 'llm_provider_id' => $client->llm_provider_id,
'llm_provider' => [
'id' => $client->llmProvider->id,
'name' => $client->llmProvider->name,
'service_name' => $client->llmProvider->service_name,
],
'rate_limit' => $client->rate_limit,
'timeout' => $client->timeout,
'status' => $client->status,
'updated_at' => $client->updated_at->toIso8601String(), 'updated_at' => $client->updated_at->toIso8601String(),
]); ]);
@ -315,7 +257,7 @@ public function generateAuthToken(int $id): JsonResponse
if (!$this->admin->canManageClient($client->id)) { if (!$this->admin->canManageClient($client->id)) {
return $this->error( return $this->error(
ErrorCode::FORBIDDEN, ErrorCode::FORBIDDEN,
'您無管理該客戶。' '您無<EFBFBD><EFBFBD>管理該客戶。'
); );
} }
@ -334,7 +276,10 @@ public function generateAuthToken(int $id): JsonResponse
"Generated auth token for client: {$client->name}" "Generated auth token for client: {$client->name}"
); );
return $this->success($result); return $this->success([
'auth_token' => $result['token'],
'expires_at' => $result['expires_at'],
]);
} catch (ModelNotFoundException $e) { } catch (ModelNotFoundException $e) {
return $this->error( return $this->error(