| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
-
- namespace App\Ai\Drivers;
-
- use App\Ai\AiDriver;
- use App\Ai\AiRequest;
- use App\Ai\AiResponse;
- use App\StorePlan\SamplePlan;
- use Illuminate\Support\Sleep;
-
- /**
- * A stand-in that needs no API key and costs nothing.
- * It answers "generate" with the built-in sample design and "edit" with one small colour change.
- */
- final class FakeDriver implements AiDriver
- {
- /**
- * @param array<string, mixed> $config
- */
- public function __construct(
- private readonly string $provider,
- private readonly array $config,
- ) {}
-
- public function complete(AiRequest $request): AiResponse
- {
- Sleep::for((int) ($this->config['delay'] ?? 0))->seconds();
-
- $answer = $request->task === 'edit'
- ? ['reply' => 'Fake AI: I made the main colour darker.', 'theme' => ['colors' => ['primary' => '#3B2A20']]]
- : SamplePlan::forName($this->storeName($request->prompt));
-
- return new AiResponse(
- text: (string) json_encode($answer),
- provider: $this->provider,
- model: $request->model,
- inputTokens: 0,
- outputTokens: 0,
- );
- }
-
- private function storeName(string $prompt): string
- {
- return preg_match('/^Store name: (.+)$/m', $prompt, $match) === 1 ? trim($match[1]) : 'Sample Store';
- }
- }
|