From 05c4b613694f0d90849e81f634f61a7fe46b4267 Mon Sep 17 00:00:00 2001 From: Kirill Astakhov Date: Sun, 15 Sep 2024 03:10:10 +0300 Subject: [PATCH] Fixed errors that preventing chat from launching --- app/Agents/SmartHome/DeviceStateManager.php | 41 +++++++++++++++++++ .../TaskSplitter/GetProjectDescription.php | 2 +- app/Chat/SimpleChatService.php | 10 +++++ app/Models/Session.php | 10 +++++ app/Providers/SmartHomeServiceProvider.php | 14 +++++-- 5 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 app/Agents/SmartHome/DeviceStateManager.php diff --git a/app/Agents/SmartHome/DeviceStateManager.php b/app/Agents/SmartHome/DeviceStateManager.php new file mode 100644 index 0000000..4414c23 --- /dev/null +++ b/app/Agents/SmartHome/DeviceStateManager.php @@ -0,0 +1,41 @@ +cache->get('device_' . $id); + } + + public function update(SmartDevice $device): void + { + $this->cache->set( + 'device_' . $device->id, + $device, + CarbonInterval::seconds(self::DEVICE_STATE_TTL), + ); + + $this->cache->set(self::LAST_ACTION_KEY, \time(), CarbonInterval::seconds(self::LAST_ACTION_TTL)); + } + + public function getLastActionTime(): ?int + { + return $this->cache->get(self::LAST_ACTION_KEY) ?? null; + } +} diff --git a/app/Agents/TaskSplitter/GetProjectDescription.php b/app/Agents/TaskSplitter/GetProjectDescription.php index 129de71..3530727 100644 --- a/app/Agents/TaskSplitter/GetProjectDescription.php +++ b/app/Agents/TaskSplitter/GetProjectDescription.php @@ -4,7 +4,7 @@ namespace App\Agents\TaskSplitter; -use App\Agents\PhpTool; +use LLM\Agents\Tool\PhpTool; /** * @extends PhpTool diff --git a/app/Chat/SimpleChatService.php b/app/Chat/SimpleChatService.php index 644e0bd..4dd1315 100644 --- a/app/Chat/SimpleChatService.php +++ b/app/Chat/SimpleChatService.php @@ -229,4 +229,14 @@ private function callTool(SessionInterface $session, ToolCall $tool): ToolCallRe content: [$functionResult], ); } + + public function getLatestSession(): ?SessionInterface + { + return Session::latest()->first(); + } + + public function getLatestSessions(int $limit = 3): array + { + return Session::latest()->limit($limit)->get(); + } } diff --git a/app/Models/Session.php b/app/Models/Session.php index 7131a53..f0b58f6 100644 --- a/app/Models/Session.php +++ b/app/Models/Session.php @@ -57,4 +57,14 @@ public function isFinished(): bool { return $this->trashed(); } + + public function setDescription(string $description): void + { + $this->title = $description; + } + + public function getDescription(): ?string + { + return $this->title; + } } diff --git a/app/Providers/SmartHomeServiceProvider.php b/app/Providers/SmartHomeServiceProvider.php index 58f68a6..cfc7b7d 100644 --- a/app/Providers/SmartHomeServiceProvider.php +++ b/app/Providers/SmartHomeServiceProvider.php @@ -4,23 +4,29 @@ namespace App\Providers; +use App\Agents\SmartHome\DeviceStateManager; use Illuminate\Contracts\Foundation\Application; use Illuminate\Support\ServiceProvider; use LLM\Agents\Agent\SmartHomeControl\SmartHome\Devices\Light; use LLM\Agents\Agent\SmartHomeControl\SmartHome\Devices\SmartAppliance; use LLM\Agents\Agent\SmartHomeControl\SmartHome\Devices\Thermostat; use LLM\Agents\Agent\SmartHomeControl\SmartHome\Devices\TV; +use LLM\Agents\Agent\SmartHomeControl\SmartHome\DeviceStateRepositoryInterface; +use LLM\Agents\Agent\SmartHomeControl\SmartHome\DeviceStateStorageInterface; use LLM\Agents\Agent\SmartHomeControl\SmartHome\SmartHomeSystem; -use Psr\SimpleCache\CacheInterface; final class SmartHomeServiceProvider extends ServiceProvider { public function register(): void { - $this->app->singleton(SmartHomeSystem::class, static function (Application $app) { - $cache = $app->make(CacheInterface::class); + $this->app->singleton(DeviceStateStorageInterface::class, DeviceStateManager::class); + $this->app->singleton(DeviceStateRepositoryInterface::class, DeviceStateManager::class); - $smartHome = new SmartHomeSystem($cache); + $this->app->singleton(SmartHomeSystem::class, static function (Application $app) { + $smartHome = new SmartHomeSystem( + stateStorage: $app->make(DeviceStateStorageInterface::class), + stateRepository: $app->make(DeviceStateRepositoryInterface::class), + ); // Living Room Devices $livingRoomAirConditioner = new SmartAppliance(