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.

ScriptedDriver.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace Tests\Support;
  3. use App\Ai\AiDriver;
  4. use App\Ai\AiException;
  5. use App\Ai\AiRequest;
  6. use App\Ai\AiResponse;
  7. /**
  8. * A test driver that gives back the answers it was told to, in order, and remembers what it was asked.
  9. */
  10. final class ScriptedDriver implements AiDriver
  11. {
  12. /** @var list<string|AiException> */
  13. public static array $answers = [];
  14. /** @var list<AiRequest> */
  15. public static array $requests = [];
  16. /**
  17. * @param array<string, mixed> $config
  18. */
  19. public function __construct(private readonly string $provider, private readonly array $config) {}
  20. /**
  21. * Point both tasks at this driver and queue up its answers.
  22. *
  23. * @param list<string|AiException> $answers
  24. */
  25. public static function use(array $answers): void
  26. {
  27. self::$answers = $answers;
  28. self::$requests = [];
  29. config([
  30. 'ai.providers.scripted' => ['driver' => self::class],
  31. 'ai.tasks.generate.provider' => 'scripted',
  32. 'ai.tasks.edit.provider' => 'scripted',
  33. ]);
  34. }
  35. public function complete(AiRequest $request): AiResponse
  36. {
  37. self::$requests[] = $request;
  38. $answer = array_shift(self::$answers) ?? throw new AiException('No scripted answer left.');
  39. if ($answer instanceof AiException) {
  40. throw $answer;
  41. }
  42. return new AiResponse($answer, $this->provider, $request->model, 100, 200);
  43. }
  44. }