humanUid($bearerUid); if ($userUid) { return AgentAccount::user($userUid); } if (! $deviceId) { throw ValidationException::withMessages([ 'device_id' => __('messages.agent_device_required'), ]); } return AgentAccount::guest($deviceId); } /** * 会话 id(AG-UI threadId)由客户端生成、全局唯一。已经属于别的账号的 id 不准再用: * 扣费时就拦下(不跑、不扣),入库时再兜一次。同账号的已删除会话不算别人的。 * * 游客登录(或退出)后客户端会换一个新的 threadId,所以这里不会误伤正常用户。 */ public function assertThreadOwnable(AgentAccount $account, ?string $threadId): void { if (! $threadId) { return; } $thread = AgentThread::withTrashed()->where('thread_id', $threadId)->first(['account_type', 'account_id']); if ($thread !== null && ! $thread->isOwnedBy($account)) { abort(403, __('messages.agent_thread_forbidden')); } } private function humanUid(?string $uid): ?string { if (! $uid || ! Str::isUuid($uid)) { return null; } return AiModel::where('uid', $uid)->exists() ? null : $uid; } }