Skip to content

Commit

Permalink
Merge pull request #1 from llm-agents-php/feature/open-previous-session
Browse files Browse the repository at this point in the history
Adds an ability to open previous session
  • Loading branch information
butschster authored Sep 7, 2024
2 parents 6386800 + b636ddf commit 062c39f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 21 deletions.
10 changes: 10 additions & 0 deletions src/ChatServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ interface ChatServiceInterface
*/
public function getSession(UuidInterface $sessionUuid): SessionInterface;

public function getLatestSession(): ?SessionInterface;

/**
* Get latest sessions.
*
* @param int $limit
* @return SessionInterface[]
*/
public function getLatestSessions(int $limit = 3): array;

public function updateSession(SessionInterface $session): void;

/**
Expand Down
27 changes: 26 additions & 1 deletion src/Console/ChatHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use LLM\Agents\Chat\Event\ToolCallResult;
use LLM\Agents\Chat\Exception\ChatNotFoundException;
use LLM\Agents\Chat\Exception\SessionNotFoundException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -36,10 +37,34 @@ public function __construct(
$this->io = new ChatStyle($input, $output);
}

public function run(UuidInterface $sessionUuid): void
public function run(?UuidInterface $sessionUuid = null): void
{
if ($sessionUuid === null) {
$latestSessions = $this->chat->getLatestSessions();
if (empty($latestSessions)) {
throw new SessionNotFoundException('No active session found.');
}

$choices = [];
foreach ($latestSessions as $session) {
$choices[$session->getUuid()->toString()] = $session->getDescription() ?? $session->getAgentName();
}

$sessionUuid = $this->io->choice(
'Select chat session',
$choices,
\array_key_first($choices),
);

$sessionUuid = Uuid::fromString($sessionUuid);
}

$this->sessionUuid = $sessionUuid;

if ($this->sessionUuid === null) {
throw new SessionNotFoundException('No active session found.');
}

$this->io->write("\033\143");

$session = $this->chat->getSession($this->sessionUuid);
Expand Down
62 changes: 42 additions & 20 deletions src/Console/ChatSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,50 @@ public function __construct(
$this->cursor = new Cursor($output);
}

public function run(UuidInterface $accountUuid, string $binPath = 'app.php'): void
public function run(UuidInterface $accountUuid, string $binPath = 'app.php', bool $openLatest = false): void
{
$agent = $this->selectAgent();
// Open the latest session if it exists
$isExistingSession = false;

if ($openLatest) {
$session = $this->chat->getLatestSession();
if ($session) {
$this->sessionUuid = $session->getUuid();
$agent = $this->agents->get($session->getAgentName());
$isExistingSession = true;
}
}

$getCommand = $this->getCommand($agent);
if (!$isExistingSession) {
$agent = $this->selectAgent();
$this->sessionUuid = $this->chat->startSession(
accountUuid: $accountUuid,
agentName: $agent->getKey(),
);
}

$this->io->title($agent->getName());

// split the description into multiple lines by 200 characters
$this->io->block(\wordwrap($agent->getDescription(), 200, "\n", true));

$this->sessionUuid = $this->chat->startSession(
accountUuid: $accountUuid,
agentName: $agent->getKey(),
);
$rows = [];
foreach ($agent->getTools() as $tool) {
$tool = $this->tools->get($tool->name);
$rows[] = [$tool->name, \wordwrap($tool->description, 70, "\n", true)];
}
$this->io->table(['Tool', 'Description'], $rows);

$getCommand = $this->getCommand($agent);

$sessionInfo = [];
if ($this->io->isVerbose()) {
$sessionInfo = [
\sprintf('Session started with UUID: %s', $this->sessionUuid),
\sprintf(
'Session %s with UUID: %s',
$isExistingSession ? 'opened' : 'started',
$this->sessionUuid,
),
];
}

Expand All @@ -74,7 +103,11 @@ public function run(UuidInterface $accountUuid, string $binPath = 'app.php'): vo
}

if (!empty($message)) {
$this->chat->ask($this->sessionUuid, $message);
try {
$this->chat->ask($this->sessionUuid, $message);
} catch (\Throwable $e) {
$this->io->error($e->getMessage());
}
} else {
$this->io->warning('Message cannot be empty');
}
Expand All @@ -101,17 +134,6 @@ private function selectAgent(): AgentInterface
$this->cursor->clearOutput();

$agent = $this->agents->get($agentName);
$this->io->title($agent->getName());

// split the description into multiple lines by 200 characters
$this->io->block(\wordwrap($agent->getDescription(), 200, "\n", true));

$rows = [];
foreach ($agent->getTools() as $tool) {
$tool = $this->tools->get($tool->name);
$rows[] = [$tool->name, \wordwrap($tool->description, 70, "\n", true)];
}
$this->io->table(['Tool', 'Description'], $rows);

break;
}
Expand Down
4 changes: 4 additions & 0 deletions src/SessionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface SessionInterface
{
public function getUuid(): UuidInterface;

public function setDescription(string $description): void;

public function getDescription(): ?string;

public function getAgentName(): string;

public function updateHistory(array $messages): void;
Expand Down

0 comments on commit 062c39f

Please sign in to comment.