trypost/app/Jobs/Ai/RegeneratePostMediaImage.php
Paulo Castellano eb52b6b699
Fix OpenRouter via laravel/ai default provider (revises #216) (#220)
* Fix OpenRouter support by using laravel/ai default provider config.

PR #216 patched Lab::OpenRouter into every agent match, but laravel/ai
already resolves config('ai.default') — including openrouter — when agents
omit provider(). Those matches also forced unknown providers to Gemini and
BrandAnalyzerRunner checked a non-existent services.openrouter key.

Remove the duplicated provider() overrides, gate availability on
ai.providers.*.key, and document OPENROUTER_API_KEY.

Co-authored-by: Paulo Castellano <hello@paulocastellano.com>

* chore(deps): bump laravel/ai to v0.10.3 for native OpenRouter support

v0.5.1's OpenRouter driver only covered text/embeddings via the legacy
Prism gateway. v0.10.3 ships a native OpenRouter gateway with image,
audio (TTS/STT), and web search support, and drops prism-php/prism as
a dependency. ai-sdk-development skill docs refreshed via boost:update
to match the installed version.

* fix: honor AI_IMAGE_PROVIDER instead of hardcoding OpenAI's gpt-image-2

AiImageClient always passed model: 'gpt-image-2' to Image::of()->generate(),
so AI_IMAGE_PROVIDER silently did nothing for any provider other than
OpenAI — gemini/xai/openrouter would fail against an OpenAI-only model id
and quietly fall back to a stock photo. Usage recording was hardcoded to
provider 'openai' too, so credits were billed against the wrong model
whenever a different provider actually ran.

Drop the hardcoded model so generation falls through to the SDK's own
config('ai.default_for_images') + per-provider default model, and read
the actual provider/model back off the response's meta for usage
recording and source_meta instead of assuming OpenAI.

* refactor: extract AiImageClient into single-purpose steps, fix uncaught exception

generate() built the prompt, called the SDK, and unpacked the response all
in one block, with bytes extraction happening after the try/catch — so a
response with an empty images collection threw an uncaught RuntimeException
from ImageResponse::firstImage() instead of returning null as documented.

Split into cleanKeywords(), buildPrompt(), resolveBrandContext(), and
toResult(), and moved response unpacking inside the try block so any
malformed response is treated as a failure like everything else. Added a
regression test with an empty-images fake response.

* fix: drop hardcoded default_text_model, resolve model per provider

default_text_model was the only per-modality model override in ai.php —
image, audio, transcription, embeddings, and reranking all just pick a
provider and let it use its own default model. Text had a config-pinned
model on top, forced into every agent's model() and into every usage
log's model field regardless of which provider actually ran. Switching
AI_TEXT_PROVIDER (e.g. to openrouter) kept sending OpenAI's model id to
whichever provider ended up handling the request.

Removed model() from all six agents so laravel/ai resolves the model
from the active provider's own default (OpenAI's default is already
'gpt-5.4', so no behavior change there). Usage-recording call sites now
read the actual provider/model back off the response's meta instead of
assuming config('ai.default')/default_text_model. StreamPostContent
needed the then() callback since broadcast()'s StreamableAgentResponse
doesn't expose meta directly.

* refactor: drop AiConfiguration wrapper, use data_get() for array reads

AiConfiguration was a one-line static helper used by only two call
sites, with no laravel/ai equivalent to lean on (confirmed AiManager
and the Provider base class expose no isConfigured()/hasKey() check —
the package's model is try-then-catch, not pre-flight checks). Inlined
the filled(config(...)) check directly into HandleInertiaRequests and
BrandAnalyzerRunner instead of keeping a class around one line of logic.

Also swapped direct array-key reads for data_get() per project
convention across every file touched by the recent AI provider/model
fixes (agents' budget arrays, RunGenerateNode/StreamPostCreation's
humanizer merge, RegeneratePostMediaImage's baseContext/copy/rendered
access). Write/assignment sites (`$x['key'] = ...`) are left as-is —
data_get() only reads.

* feat: enhance AI configuration with new providers and options

Added strict types declaration and updated Azure OpenAI API version. Introduced new 'bedrock' provider configuration with AWS credentials and role assumptions. Enhanced existing providers with additional options, including image deployment for Azure and OpenAI, and updated URLs for Gemini and Ollama. Added support for an 'openai-compatible' driver to broaden integration capabilities.

* fix: remove trailing newline in AI configuration file

* feat: add support for OpenRouter and ElevenLabs API keys in configuration

Updated the production Docker Compose file and example environment file to include commented-out entries for OPENROUTER_API_KEY and ELEVENLABS_API_KEY. This enhances the configuration options for AI providers, allowing for easier integration of additional services.

* feat: allow per-provider model overrides for every AI modality

Adds a `models` array to each provider block, wired to env vars, so
self-hosted operators can pin a specific text/image/audio/transcription/
embeddings/reranking model instead of relying on the package's built-in
default for that provider. Only added for the modalities each provider
actually implements (verified against laravel/ai's Provider classes).

Azure is left untouched — it resolves models via deployment names
(AZURE_OPENAI_DEPLOYMENT etc.), not raw model strings, which was already
wired before this change.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-08-10 21:38:50 -03:00

469 lines
15 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs\Ai;
use App\Ai\Agents\PostImageRegenerator;
use App\Enums\Media\Source;
use App\Enums\Media\Type as MediaType;
use App\Events\Ai\PostMediaRegenerated;
use App\Models\Media;
use App\Models\Post;
use App\Models\SocialAccount;
use App\Models\Workspace;
use App\Services\Ai\RecordAiUsage;
use App\Services\Image\TemplateImageGenerator;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use RuntimeException;
use Throwable;
class RegeneratePostMediaImage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
public string $workspaceId,
public string $postId,
public string $userId,
public string $mediaId,
public string $regenerationId,
public string $instruction,
) {
$this->onQueue('ai');
}
public function failed(?Throwable $exception): void
{
Log::warning('RegeneratePostMediaImage failed', [
'post_id' => $this->postId,
'media_id' => $this->mediaId,
'regeneration_id' => $this->regenerationId,
'error' => $exception?->getMessage(),
]);
PostMediaRegenerated::dispatch(
userId: $this->userId,
regenerationId: $this->regenerationId,
postId: $this->postId,
media: null,
error: __('posts.ai.image_regenerate.errors.unavailable'),
);
}
public function handle(): void
{
$workspace = Workspace::query()->findOrFail($this->workspaceId);
$post = $this->loadPost($workspace);
$target = $this->resolveAiMediaTarget($post);
$baseContext = $this->buildSourceContext(
sourceMeta: is_array(data_get($target, 'source_meta')) ? data_get($target, 'source_meta') : [],
post: $post,
workspace: $workspace,
);
$copy = $this->regenerateSlideCopy($workspace, $post, $baseContext);
$rendered = $this->renderRegeneratedImage($workspace, $post, $copy, $baseContext);
$newMediaItem = $this->replaceMediaOnPost($post, $target, $workspace, $rendered);
PostMediaRegenerated::dispatch(
userId: $this->userId,
regenerationId: $this->regenerationId,
postId: $post->id,
media: $newMediaItem,
error: null,
);
}
private function loadPost(Workspace $workspace): Post
{
return Post::query()
->where('workspace_id', $workspace->id)
->with(['postPlatforms.socialAccount', 'workspace'])
->findOrFail($this->postId);
}
/**
* @return array<string, mixed>
*/
private function resolveAiMediaTarget(Post $post): array
{
$target = collect($post->media ?? [])
->first(fn ($item) => data_get($item, 'id') === $this->mediaId);
if (! is_array($target)) {
throw new RuntimeException('Media item no longer exists in post.');
}
if (data_get($target, 'source') !== Source::Ai->value) {
throw new RuntimeException('Only AI media can be regenerated.');
}
return $target;
}
/**
* @param array{
* title: string,
* body: string,
* keywords: array<int, string>,
* background_path: string,
* language: string,
* width: int,
* height: int
* } $baseContext
* @return array{
* title: string,
* body: string,
* keywords: array<int, string>,
* regenerate_image: bool,
* regenerate_text: bool,
* change_mode: 'image_only'|'text_only'|'both'
* }
*/
private function regenerateSlideCopy(Workspace $workspace, Post $post, array $baseContext): array
{
/** @var PostImageRegenerator $agent */
$agent = app(PostImageRegenerator::class, ['workspace' => $workspace]);
$response = $agent->prompt(json_encode([
'instruction' => $this->instruction,
'title' => data_get($baseContext, 'title'),
'body' => data_get($baseContext, 'body'),
'keywords' => data_get($baseContext, 'keywords'),
'language' => data_get($baseContext, 'language'),
], JSON_THROW_ON_ERROR));
RecordAiUsage::recordText(
workspace: $workspace,
promptTokens: $response->usage?->promptTokens ?? 0,
completionTokens: $response->usage?->completionTokens ?? 0,
provider: (string) $response->meta->provider,
model: (string) $response->meta->model,
userId: $this->userId,
postId: $post->id,
metadata: ['agent' => 'post_image_regenerator'],
);
return $this->mergeStructuredCopy($baseContext, $response->structured ?? []);
}
/**
* @param array{
* title: string,
* body: string,
* keywords: array<int, string>,
* background_path: string,
* language: string,
* width: int,
* height: int
* } $baseContext
* @param array<string, mixed> $structured
* @return array{
* title: string,
* body: string,
* keywords: array<int, string>,
* regenerate_image: bool,
* regenerate_text: bool,
* change_mode: 'image_only'|'text_only'|'both'
* }
*/
private function mergeStructuredCopy(array $baseContext, array $structured): array
{
$changeMode = $this->resolveChangeMode((string) data_get($structured, 'change_mode', 'both'));
$regenerateImage = in_array($changeMode, ['image_only', 'both'], true);
$regenerateText = in_array($changeMode, ['text_only', 'both'], true);
$keywords = $this->normalizeKeywords(data_get($structured, 'keywords', data_get($baseContext, 'keywords')));
return [
'title' => $regenerateText
? trim((string) data_get($structured, 'title', data_get($baseContext, 'title')))
: data_get($baseContext, 'title'),
'body' => $regenerateText
? trim((string) data_get($structured, 'body', data_get($baseContext, 'body')))
: data_get($baseContext, 'body'),
'keywords' => $regenerateImage && $keywords !== [] ? $keywords : data_get($baseContext, 'keywords'),
'regenerate_image' => $regenerateImage,
'regenerate_text' => $regenerateText,
'change_mode' => $changeMode,
];
}
/**
* @param array{
* title: string,
* body: string,
* keywords: array<int, string>,
* regenerate_image: bool,
* regenerate_text: bool,
* change_mode: 'image_only'|'text_only'|'both'
* } $copy
* @param array{
* title: string,
* body: string,
* keywords: array<int, string>,
* language: string,
* width: int,
* height: int
* } $baseContext
* @return array{path: string, source_meta: array<string, mixed>}
*/
private function renderRegeneratedImage(
Workspace $workspace,
Post $post,
array $copy,
array $baseContext,
): array {
$socialAccount = $this->resolveSocialAccount($post, $workspace);
if (! $socialAccount) {
throw new RuntimeException('No social account available for image footer rendering.');
}
$reusedBackgroundPath = null;
if (! data_get($copy, 'regenerate_image')) {
$reusedBackgroundPath = (string) data_get($baseContext, 'background_path', '');
if ($reusedBackgroundPath === '') {
$reusedBackgroundPath = null;
}
}
$rendered = app(TemplateImageGenerator::class)->render(
workspace: $workspace,
socialAccount: $socialAccount,
title: data_get($copy, 'title'),
body: data_get($copy, 'body'),
imageKeywords: data_get($copy, 'keywords'),
width: data_get($baseContext, 'width'),
height: data_get($baseContext, 'height'),
backgroundPath: $reusedBackgroundPath,
);
if (! $rendered) {
throw new RuntimeException('Image generator failed to produce media.');
}
return $rendered;
}
/**
* @param array<string, mixed> $target
* @param array{path: string, source_meta: array<string, mixed>} $rendered
* @return array<string, mixed>
*/
private function replaceMediaOnPost(
Post $post,
array $target,
Workspace $workspace,
array $rendered,
): array {
$renderedPath = data_get($rendered, 'path');
$newBackgroundPath = (string) data_get($rendered, 'source_meta.background_path', '');
$oldBackgroundPath = (string) data_get($target, 'source_meta.background_path', '');
try {
$newMediaItem = DB::transaction(function () use ($post, $rendered, $target, $workspace) {
$newMediaItem = $this->buildAiMediaItem($workspace, $rendered);
$fresh = Post::query()->whereKey($post->id)->lockForUpdate()->firstOrFail();
$items = collect($fresh->media ?? []);
$currentIndex = $items->search(fn ($item) => data_get($item, 'id') === $this->mediaId);
if ($currentIndex === false) {
throw new RuntimeException('Media item changed before regeneration completed.');
}
if (($meta = data_get($items->get($currentIndex), 'meta')) !== null) {
$newMediaItem['meta'] = $meta;
}
$items->put($currentIndex, $newMediaItem);
$fresh->update(['media' => $items->values()->all()]);
Media::query()->where('id', data_get($target, 'id'))->first()?->delete();
return $newMediaItem;
});
if ($oldBackgroundPath !== '' && $oldBackgroundPath !== $newBackgroundPath && Storage::exists($oldBackgroundPath)) {
Storage::delete($oldBackgroundPath);
}
return $newMediaItem;
} catch (Throwable $exception) {
$this->discardRenderedFile($renderedPath);
if ($newBackgroundPath !== '' && $newBackgroundPath !== $oldBackgroundPath && Storage::exists($newBackgroundPath)) {
Storage::delete($newBackgroundPath);
}
throw $exception;
}
}
private function discardRenderedFile(string $path): void
{
if ($path !== '' && Storage::exists($path)) {
Storage::delete($path);
}
}
/**
* @param array<string, mixed> $sourceMeta
* @return array{
* title: string,
* body: string,
* keywords: array<int, string>,
* background_path: string,
* language: string,
* width: int,
* height: int
* }
*/
private function buildSourceContext(array $sourceMeta, Post $post, Workspace $workspace): array
{
$title = trim((string) data_get($sourceMeta, 'title', ''));
$body = trim((string) data_get($sourceMeta, 'body', ''));
$keywords = $this->normalizeKeywords(data_get($sourceMeta, 'keywords', []));
if ($title === '' && $body === '') {
[$title, $body] = $this->titleAndBodyFromPostContent($post);
}
if ($title === '') {
$title = __('posts.ai.image_regenerate.fallback_title');
}
if ($keywords === []) {
$keywords = $this->keywordsFromCopy($title, $body);
}
if ($keywords === []) {
$keywords = ['social media', 'marketing'];
}
return [
'title' => $title,
'body' => $body,
'keywords' => $keywords,
'background_path' => (string) data_get($sourceMeta, 'background_path', ''),
'language' => (string) data_get($sourceMeta, 'language', $workspace->content_language),
'width' => (int) data_get($sourceMeta, 'width', TemplateImageGenerator::DEFAULT_WIDTH),
'height' => (int) data_get($sourceMeta, 'height', TemplateImageGenerator::DEFAULT_HEIGHT),
];
}
/**
* @return array{0: string, 1: string}
*/
private function titleAndBodyFromPostContent(Post $post): array
{
$lines = Str::of((string) $post->content)
->replace(["\r\n", "\r"], "\n")
->trim()
->explode("\n")
->map(fn (string $line) => trim($line))
->filter()
->values();
if ($lines->isEmpty()) {
return ['', ''];
}
return [
(string) $lines->first(),
$lines->slice(1)->implode(' '),
];
}
/**
* @return array<int, string>
*/
private function keywordsFromCopy(string $title, string $body): array
{
return Str::of("{$title} {$body}")
->squish()
->explode(' ')
->map(fn (string $word) => (string) Str::of($word)->trim(".,!?;:\"'()[]{}"))
->filter(fn (string $word) => mb_strlen($word) >= 4)
->take(8)
->values()
->all();
}
/**
* @return array<int, string>
*/
private function normalizeKeywords(mixed $keywords): array
{
return collect($keywords)
->filter(fn ($keyword) => is_string($keyword) && trim($keyword) !== '')
->map(fn (string $keyword) => trim($keyword))
->values()
->all();
}
/**
* @return 'image_only'|'text_only'|'both'
*/
private function resolveChangeMode(string $value): string
{
return match ($value) {
'image_only', 'text_only', 'both' => $value,
default => 'both',
};
}
private function resolveSocialAccount(Post $post, Workspace $workspace): ?SocialAccount
{
$enabledAccount = $post->postPlatforms
->first(fn ($platform) => $platform->enabled && $platform->socialAccount);
if ($enabledAccount?->socialAccount) {
return $enabledAccount->socialAccount;
}
$anyAccount = $post->postPlatforms
->first(fn ($platform) => $platform->socialAccount);
return $anyAccount?->socialAccount
?? $workspace->socialAccounts()->first();
}
/**
* @param array{path: string, source_meta: array<string, mixed>} $rendered
* @return array<string, mixed>
*/
private function buildAiMediaItem(Workspace $workspace, array $rendered): array
{
$renderedPath = data_get($rendered, 'path');
$media = $workspace->media()->create([
'collection' => 'ai-generated',
'type' => MediaType::Image,
'path' => $renderedPath,
'original_filename' => basename($renderedPath),
'mime_type' => 'image/webp',
'size' => Storage::size($renderedPath),
'order' => 0,
]);
return [
'id' => $media->id,
'path' => $media->path,
'url' => $media->url,
'type' => 'image',
'mime_type' => 'image/webp',
'source' => Source::Ai->value,
'source_meta' => data_get($rendered, 'source_meta'),
];
}
}