Tars/src/Core/Agent/Agent.php
2025-03-14 18:27:56 +01:00

57 lines
1.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Core\Agent;
use App\Core\Services\AI\ChatServiceInterface;
use App\Core\Services\Calendar\CalendarService;
use DateTimeImmutable;
use function sprintf;
use function strtr;
class Agent
{
public function __construct(
private readonly PromptProvider $promptProvider,
private readonly CalendarService $calendarService,
private readonly ChatServiceInterface $chatGPTService,
) {
}
public function run(): array
{
$prompt = $this->getPrompt();
$response = $this->chatGPTService->sendMessage($prompt);
return [
'prompt' => $prompt,
'response' => $response,
];
}
public function getPrompt(): string
{
$now = new DateTimeImmutable();
$from = $now->modify('-1 day');
$to = $now->modify('+7 days');
$calendars = $this->calendarService->getCalendars();
$calendarEventsText = '';
foreach ($calendars as $calendar) {
$calendarEventsText .= sprintf(
"- %s: %s\n",
$calendar->getName(),
$calendar->getDescription(),
);
}
return strtr($this->promptProvider->getPromptTemplate(), [
'{calendar_events}' => $calendarEventsText,
'{current_time}' => $now->format('Y-m-d H:i:s'),
]);
}
}