validate([ 'message' => ['required', 'string', 'max:2000'], 'history' => ['nullable', 'array', 'max:20'], 'history.*.role' => ['nullable', 'string'], 'history.*.text' => ['nullable', 'string'], ]); if (! $afia->enabled()) { return response()->json(['message' => 'Support chat is not available right now.'], 503); } try { $reply = $afia->chat(trim($validated['message']), $validated['history'] ?? [], $this->context()); } catch (\Throwable $e) { report($e); return response()->json(['message' => 'We could not respond right now. Please try again.'], 502); } return response()->json(['data' => ['reply' => $reply]]); } /** @return array */ private function context(): array { $account = ladill_account(); return [ 'app' => 'Ladill Mini', 'description' => 'Ladill Mini is a payments app: merchants create payment QR codes and links, collect payments, and track takings and payouts.', 'account_name' => $account?->name, ]; } }