Skip to content

Commit

Permalink
Minor Fixes & Notification Added
Browse files Browse the repository at this point in the history
  • Loading branch information
jiten14 committed Sep 19, 2024
1 parent 9a465bf commit 4bdaa62
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 20 deletions.
55 changes: 47 additions & 8 deletions src/Forms/Actions/GenerateContentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,21 @@ 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) {
case 'refine':
$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";
Expand All @@ -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);
}
}
51 changes: 39 additions & 12 deletions src/Forms/Actions/GenerateImageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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');
Expand Down

0 comments on commit 4bdaa62

Please sign in to comment.