neoban/backend/app/Data/Chats/MessageDto.php
kushal-saha 20a56d4adc refactor(core): replace SocialMediaService with GeneratePostService, add message fetching and JSON:API resources
- Removed `SocialMediaService` and migrated core post generation logic to `GeneratePostService`.
- Added `GetAllChatMessagesAction` for fetching chat history.
- Introduced `MessageDto`, `MessageResource`, and `MessageCollection` for consistent backend API responses.
- Updated frontend state and services to support JSON:API-compliant chat messages and history retrieval.
- Improved typings and casting for chat message data.
2026-04-30 09:00:14 +00:00

68 lines
2.1 KiB
PHP

<?php
namespace App\Data\Chats;
use App\Models\ChatMessage;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Model;
use Laravel\Ai\Messages\AssistantMessage;
use Laravel\Ai\Responses\AgentResponse;
use Laravel\Ai\Responses\TextResponse;
final readonly class MessageDto
{
public function __construct(
public string $id,
public string $conversationId,
public string $role,
public string $content,
public string $agent,
public array $attachments,
public array $toolCalls,
public array $toolResults,
public array $usage,
public array $meta,
public CarbonInterface $createdAt,
public CarbonInterface $updatedAt,
) {}
public static function fromModel(ChatMessage|Model $message): MessageDto
{
return new self(
id: $message->id,
conversationId: $message->conversation_id,
role: $message->role,
content: $message->content,
agent: $message->agent,
attachments: $message->attachments,
toolCalls: $message->tool_calls,
toolResults: $message->tool_results,
usage: $message->usage,
meta: $message->meta,
createdAt: $message->created_at,
updatedAt: $message->updated_at,
);
}
public static function fromAgentResponse(AgentResponse|TextResponse $response, string $agent): MessageDto
{
/** @var AssistantMessage $message */
$message = $response->messages->first();
return new self(
id: $response->invocationId,
conversationId: $response->conversationId,
role: $message->role->value,
content: $message->content,
agent: $agent,
attachments: [],
toolCalls: $response->toolCalls->toArray(),
toolResults: $response->toolResults->toArray(),
usage: $response->usage->toArray(),
meta: $response->meta->toArray(),
createdAt: now(),
updatedAt: now(),
);
}
}