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 [];
}
}

View File

@ -22,9 +22,9 @@ class CreativeDirectorAgent implements Agent, Conversational, HasTools
*/
public function instructions(): Stringable|string
{
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 " .
"AI image generator. The scene must be visually compelling and directly " .
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 '.
'AI image generator. The scene must be visually compelling and directly '.
"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;
use App\Ai\Agents\CreativeDirectorAgent;
use App\Ai\Agents\ContentWriterAgent;
use App\Http\Requests\SocialMediaPostRequest;
use App\Services\SocialMediaService;
use Illuminate\Http\JsonResponse;
class SocialMediaPostController extends Controller
@ -14,18 +13,13 @@ class SocialMediaPostController extends Controller
*/
public function generate(
SocialMediaPostRequest $request,
ContentWriterAgent $socialMediaAgent,
CreativeDirectorAgent $creativeDirectorAgent
SocialMediaService $socialMediaService,
): JsonResponse {
$socialMediaResponse = $socialMediaAgent->prompt($request->input('prompt'));
$postText = $socialMediaResponse->text;
$imagePromptResponse = $creativeDirectorAgent->prompt($postText);
$imagePrompt = $imagePromptResponse->text;
$prompt = $request->input('prompt');
$response = $socialMediaService->generatePostWithImage($prompt);
return response()->json([
'post' => $postText,
'image_prompt' => $imagePrompt,
'post' => $response->post,
'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);
}
}