AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\StorePlan\Ai;
  3. use App\Ai\AiException;
  4. use App\Ai\AiManager;
  5. use App\Models\Store;
  6. use App\StorePlan\PlanValidator;
  7. use Closure;
  8. use Illuminate\Support\Str;
  9. use Illuminate\Validation\ValidationException;
  10. /**
  11. * Turns AI answers into checked store plans.
  12. *
  13. * - generate(): design a whole store from the brand description.
  14. * - edit(): apply one small change asked for in plain words.
  15. *
  16. * Nothing leaves this class without passing PlanValidator. If an answer is rejected,
  17. * the AI gets one chance to correct it. After that the attempt fails and the store is left as it was.
  18. */
  19. class PlanWriter
  20. {
  21. public function __construct(
  22. private readonly AiManager $ai,
  23. private readonly PlanValidator $validator,
  24. ) {}
  25. /**
  26. * @throws AiException
  27. */
  28. public function generate(Store $store): PlanResult
  29. {
  30. return $this->askAndCheck(
  31. task: 'generate',
  32. system: PlanPrompt::generateSystem(),
  33. prompt: PlanPrompt::generatePrompt($store),
  34. build: fn (array $answer) => [$answer, null],
  35. );
  36. }
  37. /**
  38. * @throws AiException
  39. */
  40. public function edit(Store $store, string $instruction): PlanResult
  41. {
  42. $plan = $store->plan;
  43. if (! is_array($plan)) {
  44. throw new AiException('This store has no design to edit yet.', AiException::SETUP);
  45. }
  46. return $this->askAndCheck(
  47. task: 'edit',
  48. system: PlanPrompt::editSystem(),
  49. prompt: PlanPrompt::editPrompt($plan, $instruction),
  50. build: fn (array $answer) => [
  51. PlanChanges::apply($plan, $answer),
  52. is_string($answer['reply'] ?? null) ? Str::limit(trim($answer['reply']), 300) : null,
  53. ],
  54. );
  55. }
  56. /**
  57. * Ask the AI, check the answer, and retry once with the problems listed if it is rejected.
  58. *
  59. * @param Closure(array<string, mixed>): array{0: array<mixed>, 1: string|null} $build Turns the decoded answer into [plan, reply].
  60. *
  61. * @throws AiException
  62. */
  63. private function askAndCheck(string $task, string $system, string $prompt, Closure $build): PlanResult
  64. {
  65. $inputTokens = 0;
  66. $outputTokens = 0;
  67. $currentPrompt = $prompt;
  68. $errors = [];
  69. foreach ([1, 2] as $attempt) {
  70. $response = $this->ai->ask($task, $system, $currentPrompt);
  71. $inputTokens += $response->inputTokens;
  72. $outputTokens += $response->outputTokens;
  73. try {
  74. [$plan, $reply] = $build(JsonAnswer::decode($response->text));
  75. return new PlanResult(
  76. plan: $this->validator->validate($plan),
  77. provider: $response->provider,
  78. model: $response->model,
  79. inputTokens: $inputTokens,
  80. outputTokens: $outputTokens,
  81. reply: $reply,
  82. );
  83. } catch (ValidationException $e) {
  84. $errors = collect($e->errors())
  85. ->map(fn (array $messages, string $field) => $field.': '.implode(' ', $messages))
  86. ->values()
  87. ->all();
  88. } catch (AiException $e) {
  89. if ($e->reason !== AiException::INVALID_RESULT) {
  90. throw $e;
  91. }
  92. $errors = [$e->getMessage()];
  93. }
  94. $currentPrompt = PlanPrompt::repairPrompt($prompt, $response->text, array_values($errors));
  95. }
  96. throw new AiException(
  97. 'The AI answer failed the store plan check twice: '.implode(' | ', array_slice($errors, 0, 5)),
  98. AiException::INVALID_RESULT,
  99. );
  100. }
  101. }