feat(AI): add ai manager admin and create powerpoint with ai
This commit is contained in:
54
app/Filament/Resources/AiProviders/AiProviderResource.php
Normal file
54
app/Filament/Resources/AiProviders/AiProviderResource.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\AiProviders;
|
||||
|
||||
use App\Filament\Resources\AiProviders\Pages\CreateAiProvider;
|
||||
use App\Filament\Resources\AiProviders\Pages\EditAiProvider;
|
||||
use App\Filament\Resources\AiProviders\Pages\ListAiProviders;
|
||||
use App\Filament\Resources\AiProviders\Schemas\AiProviderForm;
|
||||
use App\Filament\Resources\AiProviders\Tables\AiProvidersTable;
|
||||
use App\Models\AiProvider;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class AiProviderResource extends Resource
|
||||
{
|
||||
protected static ?string $model = AiProvider::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedSparkles;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'هوش مصنوعی';
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'name';
|
||||
|
||||
protected static ?string $modelLabel = 'سرویس هوش مصنوعی';
|
||||
|
||||
protected static ?string $pluralModelLabel = 'سرویسهای هوش مصنوعی';
|
||||
|
||||
protected static ?string $navigationLabel = 'سرویسهای AI';
|
||||
|
||||
protected static ?int $navigationSort = 30;
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return AiProviderForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return AiProvidersTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListAiProviders::route('/'),
|
||||
'create' => CreateAiProvider::route('/create'),
|
||||
'edit' => EditAiProvider::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\AiProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\AiProviders\AiProviderResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateAiProvider extends CreateRecord
|
||||
{
|
||||
protected static string $resource = AiProviderResource::class;
|
||||
}
|
||||
19
app/Filament/Resources/AiProviders/Pages/EditAiProvider.php
Normal file
19
app/Filament/Resources/AiProviders/Pages/EditAiProvider.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\AiProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\AiProviders\AiProviderResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditAiProvider extends EditRecord
|
||||
{
|
||||
protected static string $resource = AiProviderResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/AiProviders/Pages/ListAiProviders.php
Normal file
19
app/Filament/Resources/AiProviders/Pages/ListAiProviders.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\AiProviders\Pages;
|
||||
|
||||
use App\Filament\Resources\AiProviders\AiProviderResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListAiProviders extends ListRecords
|
||||
{
|
||||
protected static string $resource = AiProviderResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\AiProviders\Schemas;
|
||||
|
||||
use App\Enums\AiProviderDriver;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class AiProviderForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextInput::make('name')
|
||||
->label('نام')
|
||||
->required()
|
||||
->maxLength(255),
|
||||
Select::make('driver')
|
||||
->label('سرویسدهنده')
|
||||
->options([
|
||||
AiProviderDriver::OpenAi->value => AiProviderDriver::OpenAi->label(),
|
||||
AiProviderDriver::GapGpt->value => AiProviderDriver::GapGpt->label(),
|
||||
])
|
||||
->default(AiProviderDriver::OpenAi->value)
|
||||
->live()
|
||||
->required(),
|
||||
TextInput::make('model')
|
||||
->label('مدل')
|
||||
->helperText('مثال: gpt-4o-mini')
|
||||
->required()
|
||||
->maxLength(255),
|
||||
TextInput::make('base_url')
|
||||
->label('آدرس API (Base URL)')
|
||||
->url()
|
||||
->placeholder(fn (Get $get): string => AiProviderDriver::from($get('driver') ?? AiProviderDriver::OpenAi->value)->defaultBaseUrl())
|
||||
->helperText('در صورت خالی بودن، آدرس پیشفرض همان سرویسدهنده استفاده میشود.')
|
||||
->maxLength(255),
|
||||
TextInput::make('api_key')
|
||||
->label('API Key')
|
||||
->password()
|
||||
->revealable()
|
||||
->autocomplete('new-password')
|
||||
->required(fn (string $operation): bool => $operation === 'create')
|
||||
->saved(fn (?string $state): bool => filled($state))
|
||||
->maxLength(1000),
|
||||
Toggle::make('is_active')
|
||||
->label('فعال')
|
||||
->default(true),
|
||||
Toggle::make('is_default')
|
||||
->label('پیشفرض')
|
||||
->helperText('هنگام تولید محتوا در صورت وجود چند سرویس فعال، این سرویس اولویت دارد.'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\AiProviders\Tables;
|
||||
|
||||
use App\Enums\AiProviderDriver;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class AiProvidersTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('name')
|
||||
->label('نام')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('driver')
|
||||
->label('سرویسدهنده')
|
||||
->badge()
|
||||
->formatStateUsing(fn (AiProviderDriver $state): string => $state->label()),
|
||||
TextColumn::make('model')
|
||||
->label('مدل')
|
||||
->searchable(),
|
||||
TextColumn::make('base_url')
|
||||
->label('آدرس API')
|
||||
->formatStateUsing(fn (?string $state, $record): string => $state ?: $record->driver->defaultBaseUrl())
|
||||
->toggleable(),
|
||||
IconColumn::make('is_active')
|
||||
->label('فعال')
|
||||
->boolean(),
|
||||
IconColumn::make('is_default')
|
||||
->label('پیشفرض')
|
||||
->boolean(),
|
||||
TextColumn::make('created_at')
|
||||
->label('تاریخ ایجاد')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
TernaryFilter::make('is_active')
|
||||
->label('وضعیت')
|
||||
->placeholder('همه')
|
||||
->trueLabel('فعال')
|
||||
->falseLabel('غیرفعال'),
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\TemplateGenerations\Actions;
|
||||
|
||||
use App\Enums\TemplateGenerationStatus;
|
||||
use App\Models\TemplateGeneration;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
|
||||
class DownloadGenerationAction
|
||||
{
|
||||
public static function make(): Action
|
||||
{
|
||||
return Action::make('download')
|
||||
->label('دانلود')
|
||||
->icon(Heroicon::OutlinedArrowDownTray)
|
||||
->color('success')
|
||||
->visible(fn (TemplateGeneration $record): bool => $record->status === TemplateGenerationStatus::Completed && filled($record->output_path))
|
||||
->url(fn (TemplateGeneration $record): string => route('admin.generations.download', $record->uuid))
|
||||
->openUrlInNewTab();
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Filament\Resources\TemplateGenerations\Tables;
|
||||
|
||||
use App\Enums\TemplateGenerationStatus;
|
||||
use App\Filament\Resources\TemplateGenerations\Actions\DownloadGenerationAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
@@ -58,6 +59,7 @@ class TemplateGenerationsTable
|
||||
])
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
DownloadGenerationAction::make(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Templates\Actions;
|
||||
|
||||
use App\Enums\TemplateGenerationStatus;
|
||||
use App\Enums\TemplateKeyType;
|
||||
use App\Jobs\GenerateTemplateJob;
|
||||
use App\Models\AiProvider;
|
||||
use App\Models\Template;
|
||||
use App\Models\TemplateGeneration;
|
||||
use App\Services\Ai\OpenAiCompatibleTextGenerator;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\FileUpload;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Illuminate\Support\Str;
|
||||
use Throwable;
|
||||
|
||||
class GenerateWithAiAction
|
||||
{
|
||||
public static function make(): Action
|
||||
{
|
||||
return Action::make('generateWithAi')
|
||||
->label('تولید با هوش مصنوعی')
|
||||
->icon(Heroicon::OutlinedSparkles)
|
||||
->color('info')
|
||||
->schema(function (Template $record): array {
|
||||
$components = [
|
||||
Textarea::make('topic')
|
||||
->label('موضوع / محتوای خام')
|
||||
->helperText('هوش مصنوعی بر اساس این متن، کلیدهای متنی قالب را تکمیل میکند.')
|
||||
->required()
|
||||
->rows(4),
|
||||
];
|
||||
|
||||
foreach ($record->keys->where('type', TemplateKeyType::Image) as $key) {
|
||||
$components[] = FileUpload::make("images.{$key->key}")
|
||||
->label($key->label)
|
||||
->image()
|
||||
->disk('local')
|
||||
->directory('templates/generations/inputs')
|
||||
->required($key->is_required);
|
||||
}
|
||||
|
||||
return $components;
|
||||
})
|
||||
->action(function (array $data, Template $record): void {
|
||||
$provider = AiProvider::query()
|
||||
->where('is_active', true)
|
||||
->orderByDesc('is_default')
|
||||
->first();
|
||||
|
||||
if (! $provider) {
|
||||
Notification::make()
|
||||
->title('هیچ سرویس هوش مصنوعی فعالی تنظیم نشده است.')
|
||||
->body('ابتدا از بخش «سرویسهای AI» یک سرویس فعال تعریف کنید.')
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$values = app(OpenAiCompatibleTextGenerator::class)->generateForTemplate($record, $data['topic'], $provider);
|
||||
} catch (Throwable $exception) {
|
||||
Notification::make()
|
||||
->title('خطا در تولید محتوا با هوش مصنوعی')
|
||||
->body($exception->getMessage())
|
||||
->danger()
|
||||
->send();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($data['images'] ?? [] as $key => $path) {
|
||||
if ($path) {
|
||||
$values[$key] = $path;
|
||||
}
|
||||
}
|
||||
|
||||
$generation = TemplateGeneration::query()->create([
|
||||
'template_id' => $record->id,
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'status' => TemplateGenerationStatus::Pending,
|
||||
'values' => $values,
|
||||
'requested_by' => auth()->id(),
|
||||
]);
|
||||
|
||||
GenerateTemplateJob::dispatch($generation);
|
||||
|
||||
Notification::make()
|
||||
->title('درخواست تولید ثبت شد')
|
||||
->body('نتیجه را میتوانید در تب «تولیدها» مشاهده کنید.')
|
||||
->success()
|
||||
->send();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Filament\Resources\Templates\Pages;
|
||||
|
||||
use App\Filament\Resources\Templates\Actions\GenerateWithAiAction;
|
||||
use App\Filament\Resources\Templates\TemplateResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
@@ -13,6 +14,7 @@ class ViewTemplate extends ViewRecord
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
GenerateWithAiAction::make(),
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Filament\Resources\Templates\RelationManagers;
|
||||
|
||||
use App\Enums\TemplateGenerationStatus;
|
||||
use App\Filament\Resources\TemplateGenerations\Actions\DownloadGenerationAction;
|
||||
use Filament\Resources\RelationManagers\RelationManager;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
@@ -60,7 +61,9 @@ class GenerationsRelationManager extends RelationManager
|
||||
TemplateGenerationStatus::Failed->value => TemplateGenerationStatus::Failed->label(),
|
||||
]),
|
||||
])
|
||||
->recordActions([])
|
||||
->recordActions([
|
||||
DownloadGenerationAction::make(),
|
||||
])
|
||||
->toolbarActions([]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ class KeysRelationManager extends RelationManager
|
||||
->components([
|
||||
TextInput::make('key')
|
||||
->label('کلید')
|
||||
->helperText('در فایل پاورپوینت برای متن بهصورت {{key}} و برای عکس/چارت بهصورت نام شکل (shape name) استفاده میشود.')
|
||||
->helperText('فقط نام کلید را وارد کنید، بدون آکولاد. مثلاً اگر داخل پاورپوینت نوشتهاید {Main Title}، اینجا فقط Main Title را وارد کنید؛ آکولاد را خود سیستم موقع جایگزینی اضافه میکند. برای عکس/چارت بهجای این، نام شکل (shape name) را در فیلد پایین وارد کنید.')
|
||||
->required()
|
||||
->maxLength(255),
|
||||
TextInput::make('label')
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Filament\Resources\Templates\Tables;
|
||||
|
||||
use App\Filament\Resources\Templates\Actions\GenerateWithAiAction;
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
@@ -51,6 +52,7 @@ class TemplatesTable
|
||||
->recordActions([
|
||||
ViewAction::make(),
|
||||
EditAction::make(),
|
||||
GenerateWithAiAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
|
||||
Reference in New Issue
Block a user