Skip to content

Commit

Permalink
Fixed errors that preventing chat from launching
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Astakhov committed Sep 15, 2024
1 parent 870bdb3 commit 05c4b61
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
41 changes: 41 additions & 0 deletions app/Agents/SmartHome/DeviceStateManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Agents\SmartHome;

use Carbon\CarbonInterval;
use LLM\Agents\Agent\SmartHomeControl\SmartHome\Devices\SmartDevice;
use LLM\Agents\Agent\SmartHomeControl\SmartHome\DeviceStateRepositoryInterface;
use LLM\Agents\Agent\SmartHomeControl\SmartHome\DeviceStateStorageInterface;
use Psr\SimpleCache\CacheInterface;

final readonly class DeviceStateManager implements DeviceStateStorageInterface, DeviceStateRepositoryInterface
{
private const DEVICE_STATE_TTL = 3600;
private const LAST_ACTION_TTL = 3600;
private const LAST_ACTION_KEY = 'last_action';

public function __construct(
private CacheInterface $cache,
) {}

public function getDevice(string $id): ?SmartDevice
{
return $this->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;
}
}
2 changes: 1 addition & 1 deletion app/Agents/TaskSplitter/GetProjectDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Agents\TaskSplitter;

use App\Agents\PhpTool;
use LLM\Agents\Tool\PhpTool;

/**
* @extends PhpTool<ProjectDescriptionInput>
Expand Down
10 changes: 10 additions & 0 deletions app/Chat/SimpleChatService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
10 changes: 10 additions & 0 deletions app/Models/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 10 additions & 4 deletions app/Providers/SmartHomeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 05c4b61

Please sign in to comment.