| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?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 = match ($request->task) {
- 'edit' => ['reply' => 'Fake AI: I made the main colour darker.', 'theme' => ['colors' => ['primary' => '#3B2A20']]],
- 'pulse' => ['summary' => 'Fake AI: sales are steady this month. Ship your waiting orders first, then check your stock.'],
- 'logo' => ['logos' => [
- ['svg' => '<svg viewBox="0 0 400 100"><circle cx="50" cy="50" r="34" fill="#1F3D32"/><text x="100" y="64" font-size="40">Fake Logo</text></svg>'],
- ['svg' => '<svg viewBox="0 0 400 100"><rect x="8" y="16" width="68" height="68" rx="12" fill="#9CC5C0"/><text x="96" y="64" font-size="40">Fake Logo</text></svg>'],
- ['svg' => '<svg viewBox="0 0 400 100"><text x="10" y="64" font-size="44" font-weight="700">Fake Logo</text><rect x="10" y="76" width="120" height="4" fill="#1F3D32"/></svg>'],
- ]],
- default => 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';
- }
- }
|