diff --git a/src/Forms/Actions/GenerateContentAction.php b/src/Forms/Actions/GenerateContentAction.php index c0fa167..2114e5e 100644 --- a/src/Forms/Actions/GenerateContentAction.php +++ b/src/Forms/Actions/GenerateContentAction.php @@ -62,9 +62,13 @@ public function execute($field, $record, $data, array $options = []) ->title('OpenAI API Key Missing') ->body('Please add your OpenAI API Key to the .env file before proceeding.') ->send(); - }else { + return; + } + + try { + $currentContent = $field->getState(); + if ($data['use_existing_content']) { - $currentContent = $field->getState(); $action = $data['existing_content_action']; switch ($action) { @@ -72,7 +76,7 @@ public function execute($field, $record, $data, array $options = []) $prompt = "Refine the following text: $currentContent"; break; case 'expand': - $prompt = "Expand on the following text: $currentContent"; + $prompt = "Expand on the following text by adding more details, examples, or explanations. Ensure that your response is a continuation of the existing content and forms complete sentences and paragraphs: $currentContent"; break; case 'shorten': $prompt = "Shorten the following text while maintaining its key points: $currentContent"; @@ -90,26 +94,61 @@ public function execute($field, $record, $data, array $options = []) $generatedContent = app(JitoneAi::class)->generateContent($prompt, $options); - if ($data['use_existing_content']) { - // Replace the existing content + $textInputContent = $generatedContent; + // Remove incomplete sentences + $generatedContent = $this->removeIncompleteSentences($generatedContent); + + if ($data['use_existing_content'] && $data['existing_content_action'] === 'expand') { + // Append the new content to the existing content + $newContent = $currentContent . "\n\n" . $generatedContent; + } elseif ($data['use_existing_content']) { + // Replace the existing content for 'refine' and 'shorten' actions $newContent = $generatedContent; } else { - // Append the new content to the existing content - $currentContent = $field->getState(); + // Append the new content to the existing content for non-existing content actions if ($field instanceof RichEditor) { $newContent = $currentContent . "\n\n" . $generatedContent; } elseif ($field instanceof Textarea) { $newContent = $currentContent . "\n" . $generatedContent; } else { - $newContent = trim($currentContent . ' ' . $generatedContent); + $newContent = trim($currentContent . ' ' . $textInputContent); } } // Set the new content $field->state($newContent); + + // Notify the user of successful content generation + Notification::make() + ->success() + ->title('Content Generated Successfully') + ->body('The AI-generated content has been added to the field.') + ->send(); + + } catch (\Exception $e) { + // Notify the user if an error occurs + Notification::make() + ->danger() + ->title('Error Generating Content') + ->body('An error occurred while generating content: ' . $e->getMessage()) + ->send(); } }) ->modalHeading('Generate Content with AI') ->modalButton('Generate'); } + + private function removeIncompleteSentences($content) + { + $sentences = preg_split('/(?<=[.!?])\s+/', $content, -1, PREG_SPLIT_NO_EMPTY); + $lastSentence = end($sentences); + + // Check if the last sentence ends with a period, exclamation mark, or question mark + if (!preg_match('/[.!?]$/', $lastSentence)) { + // Remove the last sentence if it's incomplete + array_pop($sentences); + } + + return implode(' ', $sentences); + } } \ No newline at end of file diff --git a/src/Forms/Actions/GenerateImageAction.php b/src/Forms/Actions/GenerateImageAction.php index 4b40537..68712b2 100644 --- a/src/Forms/Actions/GenerateImageAction.php +++ b/src/Forms/Actions/GenerateImageAction.php @@ -7,6 +7,7 @@ use Filament\Forms\Components\Actions\Action; use Jiten14\JitoneAi\JitoneAi; use Illuminate\Support\Facades\Storage; +use Filament\Notifications\Notification; class GenerateImageAction { @@ -32,19 +33,45 @@ public function execute($field, $record, $data, array $options = []) }), ]) ->action(function (array $data) use ($field, $options) { - $prompt = $data['ai_prompt'] ?? null; - - if (empty($prompt)) { - throw new \Exception("Image prompt is empty or null. Form data: " . json_encode($data)); + if (!env('OPENAI_API_KEY')) { + Notification::make() + ->warning() + ->title('OpenAI API Key Missing') + ->body('Please add your OpenAI API Key to the .env file before proceeding.') + ->send(); + return; + } + + try { + $prompt = $data['ai_prompt'] ?? null; + + if (empty($prompt)) { + throw new \Exception("Image prompt is empty or null. Form data: " . json_encode($data)); + } + + $imageUrl = app(JitoneAi::class)->generateImage($prompt, $options); + + // Convert the full URL to a relative path + $relativePath = $this->urlToRelativePath($imageUrl); + + // Set the field state with an array containing the relative path + $field->state([$relativePath]); + + // Notify the user of successful image generation + Notification::make() + ->success() + ->title('Image Generated Successfully') + ->body('The AI-generated image has been added to the field.') + ->send(); + + } catch (\Exception $e) { + // Notify the user if an error occurs + Notification::make() + ->danger() + ->title('Error Generating Image') + ->body('An error occurred while generating the image: ' . $e->getMessage()) + ->send(); } - - $imageUrl = app(JitoneAi::class)->generateImage($prompt, $options); - - // Convert the full URL to a relative path - $relativePath = $this->urlToRelativePath($imageUrl); - - // Set the field state with an array containing the relative path - $field->state([$relativePath]); }) ->modalHeading('Generate Image with AI') ->modalButton('Generate');