AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FakeDriver.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Ai\Drivers;
  3. use App\Ai\AiDriver;
  4. use App\Ai\AiRequest;
  5. use App\Ai\AiResponse;
  6. use App\StorePlan\SamplePlan;
  7. use Illuminate\Support\Sleep;
  8. /**
  9. * A stand-in that needs no API key and costs nothing.
  10. * It answers "generate" with the built-in sample design and "edit" with one small colour change.
  11. */
  12. final class FakeDriver implements AiDriver
  13. {
  14. /**
  15. * @param array<string, mixed> $config
  16. */
  17. public function __construct(
  18. private readonly string $provider,
  19. private readonly array $config,
  20. ) {}
  21. public function complete(AiRequest $request): AiResponse
  22. {
  23. Sleep::for((int) ($this->config['delay'] ?? 0))->seconds();
  24. $answer = match ($request->task) {
  25. 'edit' => ['reply' => 'Fake AI: I made the main colour darker.', 'theme' => ['colors' => ['primary' => '#3B2A20']]],
  26. 'pulse' => ['summary' => 'Fake AI: sales are steady this month. Ship your waiting orders first, then check your stock.'],
  27. default => SamplePlan::forName($this->storeName($request->prompt)),
  28. };
  29. return new AiResponse(
  30. text: (string) json_encode($answer),
  31. provider: $this->provider,
  32. model: $request->model,
  33. inputTokens: 0,
  34. outputTokens: 0,
  35. );
  36. }
  37. private function storeName(string $prompt): string
  38. {
  39. return preg_match('/^Store name: (.+)$/m', $prompt, $match) === 1 ? trim($match[1]) : 'Sample Store';
  40. }
  41. }