refactor: Move post generation to SocialMediaService

This commit is contained in:
kushal-saha 2026-04-27 05:04:04 +00:00
parent 9c070e0f5c
commit b538bf8fee
5 changed files with 46 additions and 16 deletions

View File

@ -74,4 +74,3 @@ public function tools(): iterable
return []; return [];
} }
} }

View File

@ -22,9 +22,9 @@ class CreativeDirectorAgent implements Agent, Conversational, HasTools
*/ */
public function instructions(): Stringable|string public function instructions(): Stringable|string
{ {
return "You are a creative director. Read the following social media post and " . return 'You are a creative director. Read the following social media post and '.
"write a single, vivid, descriptive image prompt (max 30 words) for an " . 'write a single, vivid, descriptive image prompt (max 30 words) for an '.
"AI image generator. The scene must be visually compelling and directly " . 'AI image generator. The scene must be visually compelling and directly '.
"relevant to the post's topic. Return ONLY the image prompt — nothing else."; "relevant to the post's topic. Return ONLY the image prompt — nothing else.";
} }

View File

@ -0,0 +1,11 @@
<?php
namespace App\Data;
class SocialMediaPostResponseDto
{
public function __construct(
public string $post,
public string $image,
) {}
}

View File

@ -2,9 +2,8 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Ai\Agents\CreativeDirectorAgent;
use App\Ai\Agents\ContentWriterAgent;
use App\Http\Requests\SocialMediaPostRequest; use App\Http\Requests\SocialMediaPostRequest;
use App\Services\SocialMediaService;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
class SocialMediaPostController extends Controller class SocialMediaPostController extends Controller
@ -14,18 +13,13 @@ class SocialMediaPostController extends Controller
*/ */
public function generate( public function generate(
SocialMediaPostRequest $request, SocialMediaPostRequest $request,
ContentWriterAgent $socialMediaAgent, SocialMediaService $socialMediaService,
CreativeDirectorAgent $creativeDirectorAgent
): JsonResponse { ): JsonResponse {
$socialMediaResponse = $socialMediaAgent->prompt($request->input('prompt')); $prompt = $request->input('prompt');
$postText = $socialMediaResponse->text; $response = $socialMediaService->generatePostWithImage($prompt);
$imagePromptResponse = $creativeDirectorAgent->prompt($postText);
$imagePrompt = $imagePromptResponse->text;
return response()->json([ return response()->json([
'post' => $postText, 'post' => $response->post,
'image_prompt' => $imagePrompt, 'image_prompt' => $response->image,
]); ]);
} }
} }

View File

@ -0,0 +1,26 @@
<?php
namespace App\Services;
use App\Ai\Agents\ContentWriterAgent;
use App\Ai\Agents\CreativeDirectorAgent;
use App\Data\SocialMediaPostResponseDto;
class SocialMediaService
{
public function __construct(
private readonly ContentWriterAgent $contentWriterAgent,
private readonly CreativeDirectorAgent $creativeDirectorAgent,
) {}
public function generatePostWithImage(string $prompt): SocialMediaPostResponseDto
{
$socialMediaResponse = $this->contentWriterAgent->prompt($prompt);
$postText = $socialMediaResponse->text;
$imagePromptResponse = $this->creativeDirectorAgent->prompt($postText);
$imagePrompt = $imagePromptResponse->text;
return new SocialMediaPostResponseDto($postText, $imagePrompt);
}
}