lokalisierung vervollständigt, api provider testconnection, runware modellsuche aktiviert und style preview generation integriert

This commit is contained in:
2025-12-03 16:11:38 +01:00
parent 090ec2c44b
commit 52dc61ca16
15 changed files with 516 additions and 77 deletions

View File

@@ -2,16 +2,110 @@
namespace App\Filament\Resources\Styles\Pages;
use App\Api\Plugins\PluginLoader;
use App\Filament\Resources\Styles\StyleResource;
use Filament\Actions;
use App\Models\AiModel;
use App\Models\Image;
use App\Models\Style;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class CreateStyle extends CreateRecord
{
protected static string $resource = StyleResource::class;
protected function getHeaderActions(): array
{
return [
Action::make('generate_preview')
->label(__('filament.resource.style.preview.generate'))
->icon('heroicon-o-sparkles')
->color('gray')
->action(function (): void {
try {
$state = $this->form->getState();
$path = $state['preview_source'] ?? null;
$aiModelId = $state['ai_model_id'] ?? null;
$prompt = $state['prompt'] ?? null;
if (! $path) {
throw new \RuntimeException(__('filament.resource.style.preview.missing_image'));
}
if (! $aiModelId) {
throw new \RuntimeException(__('filament.resource.style.preview.missing_model'));
}
if (! $prompt) {
throw new \RuntimeException(__('filament.resource.style.preview.missing_prompt'));
}
$aiModel = AiModel::with('primaryApiProvider')->findOrFail($aiModelId);
$provider = $aiModel->primaryApiProvider;
if (! $provider || ! $provider->enabled || ! $provider->plugin || ! $provider->token) {
throw new \RuntimeException(__('filament.resource.style.preview.provider_invalid'));
}
$style = new Style([
'title' => $state['title'] ?? 'Preview',
'prompt' => $prompt,
'description' => $state['description'] ?? '',
'parameters' => $state['parameters'] ?? [],
]);
$style->setRelation('aiModel', $aiModel);
$image = new Image([
'path' => $path,
]);
$plugin = PluginLoader::getPlugin($provider->plugin, $provider);
$result = $plugin->processImageStyleChange($image, $style);
$imageData = null;
if (isset($result['base64Data'])) {
$imageData = base64_decode($result['base64Data']);
} elseif (isset($result['prompt_id'])) {
$fetched = $plugin->getStyledImage($result['prompt_id']);
if (str_starts_with($fetched, 'http')) {
$resp = Http::timeout(30)->get($fetched);
$resp->throw();
$imageData = $resp->body();
} else {
$imageData = base64_decode($fetched);
}
}
if (! $imageData) {
throw new \RuntimeException(__('filament.resource.style.preview.no_image'));
}
$fileName = 'style_previews/generated_'.Str::uuid().'.png';
Storage::disk('public')->put($fileName, $imageData);
$this->form->fill([
'preview_image' => $fileName,
]);
Notification::make()
->title(__('filament.resource.style.preview.done'))
->body(__('filament.resource.style.preview.saved_hint'))
->success()
->send();
} catch (\Throwable $e) {
Notification::make()
->title(__('filament.resource.style.preview.failed'))
->body($e->getMessage())
->danger()
->send();
}
}),
];
}
protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('index');
}
}
}

View File

@@ -2,9 +2,18 @@
namespace App\Filament\Resources\Styles\Pages;
use App\Api\Plugins\PluginLoader;
use App\Filament\Resources\Styles\StyleResource;
use App\Models\AiModel;
use App\Models\Image;
use App\Models\Style;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class EditStyle extends EditRecord
{
@@ -13,6 +22,88 @@ class EditStyle extends EditRecord
protected function getHeaderActions(): array
{
return [
Action::make('generate_preview')
->label(__('filament.resource.style.preview.generate'))
->icon('heroicon-o-sparkles')
->color('gray')
->action(function (): void {
try {
$state = $this->form->getState();
$path = $state['preview_source'] ?? null;
$aiModelId = $state['ai_model_id'] ?? null;
$prompt = $state['prompt'] ?? null;
if (! $path) {
throw new \RuntimeException(__('filament.resource.style.preview.missing_image'));
}
if (! $aiModelId) {
throw new \RuntimeException(__('filament.resource.style.preview.missing_model'));
}
if (! $prompt) {
throw new \RuntimeException(__('filament.resource.style.preview.missing_prompt'));
}
$aiModel = AiModel::with('primaryApiProvider')->findOrFail($aiModelId);
$provider = $aiModel->primaryApiProvider;
if (! $provider || ! $provider->enabled || ! $provider->plugin || ! $provider->token) {
throw new \RuntimeException(__('filament.resource.style.preview.provider_invalid'));
}
// Temporary style + image instances
$style = new Style([
'title' => $state['title'] ?? 'Preview',
'prompt' => $prompt,
'description' => $state['description'] ?? '',
'parameters' => $state['parameters'] ?? [],
]);
$style->setRelation('aiModel', $aiModel);
$image = new Image([
'path' => $path,
]);
$plugin = PluginLoader::getPlugin($provider->plugin, $provider);
$result = $plugin->processImageStyleChange($image, $style);
$imageData = null;
if (isset($result['base64Data'])) {
$imageData = base64_decode($result['base64Data']);
} elseif (isset($result['prompt_id'])) {
$fetched = $plugin->getStyledImage($result['prompt_id']);
if (str_starts_with($fetched, 'http')) {
$resp = Http::timeout(30)->get($fetched);
$resp->throw();
$imageData = $resp->body();
} else {
$imageData = base64_decode($fetched);
}
}
if (! $imageData) {
throw new \RuntimeException(__('filament.resource.style.preview.no_image'));
}
$fileName = 'style_previews/generated_'.Str::uuid().'.png';
Storage::disk('public')->put($fileName, $imageData);
// Update form state
$this->form->fill([
'preview_image' => $fileName,
]);
Notification::make()
->title(__('filament.resource.style.preview.done'))
->body(__('filament.resource.style.preview.saved_hint'))
->success()
->send();
} catch (\Throwable $e) {
Notification::make()
->title(__('filament.resource.style.preview.failed'))
->body($e->getMessage())
->danger()
->send();
}
}),
Actions\DeleteAction::make(),
];
}
@@ -21,4 +112,4 @@ class EditStyle extends EditRecord
{
return $this->getResource()::getUrl('index');
}
}
}

View File

@@ -5,7 +5,6 @@ namespace App\Filament\Resources\Styles;
use App\Models\Style;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Actions\Action\Step;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
@@ -41,6 +40,11 @@ class StyleResource extends Resource
return __('filament.navigation.groups.ai_models');
}
public static function getNavigationLabel(): string
{
return __('filament.navigation.styles');
}
public static function form(Schema $schema): Schema
{
return $schema
@@ -68,9 +72,17 @@ class StyleResource extends Resource
Textarea::make('description')
->required()
->rows(5),
FileUpload::make('preview_source')
->label('Referenzbild für Vorschau')
->helperText('Lade ein Bild hoch, um eine Vorschau mit dem aktuellen Prompt zu generieren.')
->disk('public')
->directory('style_previews/reference')
->image()
->dehydrated(false),
]),
Select::make('ai_model_id')
->relationship('aiModel', 'name')
->label(__('filament.resource.style.form.ai_model'))
->required(),
FileUpload::make('preview_image')
->disk('public')
@@ -82,9 +94,11 @@ class StyleResource extends Resource
Tab::make('Details')
->components([
TextInput::make('sort_order')
->label(__('filament.resource.style.form.sort_order'))
->numeric()
->default(0),
Textarea::make('parameters')
->label(__('filament.resource.style.form.parameters'))
->nullable()
->rows(15),
]),
@@ -97,12 +111,13 @@ class StyleResource extends Resource
return $table
->defaultSort('sort_order')
->columns([
TextColumn::make('title')->searchable()->sortable(),
TextColumn::make('title')->label(__('filament.resource.style.table.title'))->searchable()->sortable(),
IconColumn::make('enabled')
->label(__('filament.resource.style.table.enabled'))
->boolean(),
TextColumn::make('aiModel.name')->searchable()->sortable(),
ImageColumn::make('preview_image')->disk('public'),
TextColumn::make('sort_order')->sortable(),
TextColumn::make('aiModel.name')->label(__('filament.resource.style.table.ai_model'))->searchable()->sortable(),
ImageColumn::make('preview_image')->label(__('filament.resource.style.table.preview_image'))->disk('public'),
TextColumn::make('sort_order')->label(__('filament.resource.style.table.sort_order'))->sortable(),
])
->filters([
SelectFilter::make('ai_model')
@@ -111,30 +126,24 @@ class StyleResource extends Resource
->deferFilters(false)
->headerActions([
Action::make('import_styles')
->label('Import Styles (CSV)')
->label(__('filament.resource.style.import.title'))
->icon('heroicon-o-arrow-up-on-square-stack')
->steps([
Step::make('Datei')
->schema([
FileUpload::make('import_file')
->label('CSV-Datei (Titel,Prompt,[Beschreibung])')
->acceptedFileTypes([
'text/csv',
'text/plain',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
])
->directory('imports/styles')
->visibility('private')
->required(),
]),
Step::make('Zuordnung')
->schema([
Select::make('ai_model_id')
->label('AI Modell')
->relationship('aiModel', 'name')
->required(),
]),
->form([
FileUpload::make('import_file')
->label(__('filament.resource.style.import.file_label'))
->acceptedFileTypes([
'text/csv',
'text/plain',
'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
])
->directory('imports/styles')
->visibility('private')
->required(),
Select::make('ai_model_id')
->label(__('filament.resource.style.import.model_label'))
->relationship('aiModel', 'name')
->required(),
])
->action(function (array $data): void {
$path = storage_path('app/'.$data['import_file']);
@@ -146,7 +155,7 @@ class StyleResource extends Resource
$rows = self::parseImportFile($path);
if (empty($rows)) {
throw new \RuntimeException('Keine gültigen Zeilen gefunden.');
throw new \RuntimeException(__('filament.resource.style.import.empty'));
}
$created = 0;
@@ -176,8 +185,8 @@ class StyleResource extends Resource
}
\Filament\Notifications\Notification::make()
->title('Import abgeschlossen')
->body($created.' Styles importiert. Bitte Vorschau-Bilder ergänzen.')
->title(__('filament.resource.style.import.title'))
->body($created.' '.__('filament.resource.style.import.success'))
->success()
->send();
}),
@@ -185,7 +194,7 @@ class StyleResource extends Resource
->actions([
EditAction::make(),
Action::make('duplicate')
->label('Duplicate')
->label(__('filament.resource.style.action.duplicate'))
->icon('heroicon-o-document-duplicate')
->action(function (\App\Models\Style $record) {
$newStyle = $record->replicate();
@@ -199,17 +208,64 @@ class StyleResource extends Resource
BulkActionGroup::make([
DeleteBulkAction::make(),
BulkAction::make('enable')
->label('Enable Selected')
->label(__('filament.resource.style.action.enable_selected'))
->icon('heroicon-o-check-circle')
->action(function (\Illuminate\Support\Collection $records) {
$records->each->update(['enabled' => true]);
}),
BulkAction::make('disable')
->label('Disable Selected')
->label(__('filament.resource.style.action.disable_selected'))
->icon('heroicon-o-x-circle')
->action(function (\Illuminate\Support\Collection $records) {
$records->each->update(['enabled' => false]);
}),
BulkAction::make('reassignModel')
->label(__('filament.resource.style.action.reassign_model'))
->icon('heroicon-o-arrow-path')
->form([
Select::make('ai_model_id')
->label('Zielmodell')
->relationship('aiModel', 'name')
->required(),
])
->action(function (\Illuminate\Support\Collection $records, array $data) {
$count = 0;
foreach ($records as $record) {
$record->update(['ai_model_id' => $data['ai_model_id']]);
$count++;
}
\Filament\Notifications\Notification::make()
->title(__('filament.resource.style.action.reassign_model'))
->body("{$count} ".__('filament.resource.style.table.title'))
->success()
->send();
}),
BulkAction::make('duplicateToModel')
->label(__('filament.resource.style.action.duplicate_to_model'))
->icon('heroicon-o-document-duplicate')
->form([
Select::make('ai_model_id')
->label('Zielmodell')
->relationship('aiModel', 'name')
->required(),
])
->action(function (\Illuminate\Support\Collection $records, array $data) {
$created = 0;
foreach ($records as $record) {
$copy = $record->replicate();
$copy->title = $record->title.' (Copy)';
$copy->ai_model_id = $data['ai_model_id'];
$copy->save();
$created++;
}
\Filament\Notifications\Notification::make()
->title(__('filament.resource.style.action.duplicate'))
->body("{$created} Kopien erstellt.")
->success()
->send();
}),
]),
])
->emptyStateActions([