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.
42 lines
1.3 KiB
PHP
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());
|
|
}
|
|
}
|