neoban/backend/app/Services/SocialMediaService.php
kushal-saha 8e2ced8bed Implement new JSON:API Resource for the api responses.
Implement Authentication States, show logout options in auth store and services.
Make DB and controllers to make messages grouped by chats.
Refactor backend code to use DTO.
2026-04-28 12:38:46 +00:00

42 lines
1.3 KiB
PHP

<?php
namespace App\Services;
use App\Actions\Chats\StoreChatMessageAction;
use App\Ai\Agents\ContentWriterAgent;
use App\Ai\Agents\CreativeDirectorAgent;
use App\Data\SocialMediaPostResponseDto;
use App\Enums\Chats\ChatRoles;
use App\Models\Chat;
use App\Models\ChatMessage;
readonly class SocialMediaService
{
public function __construct(
private ContentWriterAgent $contentWriterAgent,
private CreativeDirectorAgent $creativeDirectorAgent,
private StoreChatMessageAction $chatMessage,
) {}
/**
* Generate a social media post with an image.
* We are not using database transactions here because we do not want to delete the user prompt
* if any of the ai agents fails.
*/
public function generatePostWithImage(string $prompt, Chat $chat): SocialMediaPostResponseDto
{
$this->chatMessage->store($chat, ChatRoles::USER, $prompt);
$socialMediaResponse = $this->contentWriterAgent->prompt($prompt);
$postText = $socialMediaResponse->text;
/* @var ChatMessage $aiChat */
$aiChat = $this->chatMessage->store($chat, ChatRoles::AI, $postText);
$imagePromptResponse = $this->creativeDirectorAgent->prompt($postText);
$imagePrompt = $imagePromptResponse->text;
return new SocialMediaPostResponseDto($aiChat->id, $postText, $imagePrompt, now());
}
}