AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JsonAnswer.php 963B

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace App\StorePlan\Ai;
  3. use App\Ai\AiException;
  4. /**
  5. * Reads the JSON object out of an AI answer.
  6. * Models sometimes wrap JSON in ``` fences or add a sentence around it, so we cut from the first "{" to the last "}".
  7. */
  8. final class JsonAnswer
  9. {
  10. /**
  11. * @return array<string, mixed>
  12. *
  13. * @throws AiException
  14. */
  15. public static function decode(string $text): array
  16. {
  17. $start = strpos($text, '{');
  18. $end = strrpos($text, '}');
  19. if ($start === false || $end === false || $end < $start) {
  20. throw new AiException('The AI answer contains no JSON object.', AiException::INVALID_RESULT);
  21. }
  22. $data = json_decode(substr($text, $start, $end - $start + 1), true);
  23. if (! is_array($data) || array_is_list($data)) {
  24. throw new AiException('The AI answer is not valid JSON: '.json_last_error_msg(), AiException::INVALID_RESULT);
  25. }
  26. return $data;
  27. }
  28. }