Skip to content

Commit

Permalink
Merge pull request #12 from llm-agents-php/feature/code-refactoring
Browse files Browse the repository at this point in the history
Code refactoring
  • Loading branch information
butschster authored Sep 7, 2024
2 parents fc3a18f + 9b2e4cd commit 29cdda7
Show file tree
Hide file tree
Showing 29 changed files with 5,735 additions and 1,188 deletions.
2 changes: 1 addition & 1 deletion app/src/Agents/AgentsCaller/AskAgentTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace App\Agents\AgentsCaller;

use App\Domain\Tool\PhpTool;
use LLM\Agents\AgentExecutor\ExecutorInterface;
use LLM\Agents\LLM\Prompt\Chat\ToolCallResultMessage;
use LLM\Agents\LLM\Response\ToolCalledResponse;
use LLM\Agents\PromptGenerator\Context;
use LLM\Agents\Tool\PhpTool;
use LLM\Agents\Tool\ToolExecutor;

/**
Expand Down
9 changes: 8 additions & 1 deletion app/src/Agents/CodeReviewer/CodeReviewAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use LLM\Agents\Agent\AgentAggregate;
use LLM\Agents\OpenAI\Client\OpenAIModel;
use LLM\Agents\OpenAI\Client\Option;
use LLM\Agents\Solution\ContextSourceLink;
use LLM\Agents\Solution\MetadataType;
use LLM\Agents\Solution\Model;
use LLM\Agents\Solution\SolutionMetadata;
Expand All @@ -21,7 +22,11 @@ public static function create(): self
key: 'code_review',
name: 'Code Reviewer',
description: 'Agent can list files in project with given id and then open each file and review the code',
instruction: 'You are a code review assistant. Use the provided tools to list project files, read their contents, and submit a code review for each file.',
instruction: <<<'INSTRUCTION'
You are a code review assistant in Spiral Framework.
Use the provided tools to list project files, read their contents, and submit a code review for each file.
INSTRUCTION
,
);

$aggregate = new self($agent);
Expand Down Expand Up @@ -56,6 +61,8 @@ public static function create(): self
$aggregate->addAssociation(new ToolLink(name: ReadFileTool::NAME));
$aggregate->addAssociation(new ToolLink(name: ReviewTool::NAME));

$aggregate->addAssociation(new ContextSourceLink(name: 'spiral-docs'));

return $aggregate;
}
}
2 changes: 1 addition & 1 deletion app/src/Agents/CodeReviewer/ListProjectTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Agents\CodeReviewer;

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

/**
* @extends PhpTool<ListProjectInput>
Expand Down
2 changes: 1 addition & 1 deletion app/src/Agents/CodeReviewer/ReadFileTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Agents\CodeReviewer;

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

/**
* @extends PhpTool<ReadFileInput>
Expand Down
2 changes: 1 addition & 1 deletion app/src/Agents/CodeReviewer/ReviewTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Agents\CodeReviewer;

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

/**
* @extends PhpTool<ReviewInput>
Expand Down
6 changes: 5 additions & 1 deletion app/src/Agents/Delivery/DeliveryAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public static function create(): self
key: self::NAME,
name: 'Order Assistant',
description: 'Helps customers with order-related questions.',
instruction: 'You are a helpful customer support assistant. Use the supplied tools to assist the user.',
instruction: <<<'INSTRUCTION'
You are a helpful customer support assistant.
Use the supplied tools to assist the user.
INSTRUCTION
,
);

$aggregate = new self($agent);
Expand Down
4 changes: 2 additions & 2 deletions app/src/Agents/Delivery/GetDeliveryDateTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace App\Agents\Delivery;

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

/**
* @extends PhpTool<DeliveryDateInput>
* @extends PhpTool<DeliveryDateInput>
*/
final class GetDeliveryDateTool extends PhpTool
{
Expand Down
4 changes: 2 additions & 2 deletions app/src/Agents/Delivery/GetOrderNumberTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Agents\Delivery;

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

/**
* @extends PhpTool<OrderNumberInput>
* @extends PhpTool<OrderNumberInput>
*/
final class GetOrderNumberTool extends PhpTool
{
Expand Down
4 changes: 2 additions & 2 deletions app/src/Agents/Delivery/GetProfileTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Agents\Delivery;

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

/**
* @extends PhpTool<ProfileInput>
* @extends PhpTool<ProfileInput>
*/
final class GetProfileTool extends PhpTool
{
Expand Down
2 changes: 1 addition & 1 deletion app/src/Agents/DynamicMemoryTool/DynamicMemoryTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace App\Agents\DynamicMemoryTool;

use App\Application\Entity\Uuid;
use App\Domain\Tool\PhpTool;
use LLM\Agents\Solution\MetadataType;
use LLM\Agents\Solution\SolutionMetadata;
use LLM\Agents\Tool\PhpTool;

final class DynamicMemoryTool extends PhpTool
{
Expand Down
16 changes: 0 additions & 16 deletions app/src/Agents/SmartHomeControl/SmartHomeControlAgentFactory.php

This file was deleted.

4 changes: 2 additions & 2 deletions app/src/Agents/TaskSplitter/GetProjectDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace App\Agents\TaskSplitter;

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

/**
* @extends PhpTool<ProjectDescriptionInput>
* @extends PhpTool<ProjectDescriptionInput>
*/
final class GetProjectDescription extends PhpTool
{
Expand Down
4 changes: 2 additions & 2 deletions app/src/Agents/TaskSplitter/TaskCreateTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace App\Agents\TaskSplitter;

use App\Application\Entity\Uuid;
use App\Domain\Tool\PhpTool;
use LLM\Agents\Tool\PhpTool;
use Spiral\Boot\DirectoriesInterface;
use Spiral\Files\FilesInterface;

/**
* @extends PhpTool<TaskCreateInput>
* @extends PhpTool<TaskCreateInput>
*/
final class TaskCreateTool extends PhpTool
{
Expand Down
7 changes: 6 additions & 1 deletion app/src/Agents/TaskSplitter/TaskSplitterAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ public static function create(): self
key: self::NAME,
name: 'Task Splitter',
description: 'An agent that splits project descriptions into structured task lists.',
instruction: 'You are a task organization assistant. Your primary goal is to analyze project descriptions and break them down into well-structured task lists with subtasks.',
instruction: <<<'INSTRUCTION'
You are a task organization assistant.
Your primary goal is to analyze project descriptions and break them down into well-structured task lists with subtasks.
INSTRUCTION
,
);

$aggregate = new self($agent);
Expand Down
10 changes: 10 additions & 0 deletions app/src/Domain/Chat/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ public function getAgentName(): string
{
return $this->agentName;
}

public function setDescription(string $description): void
{
$this->title = $description;
}

public function getDescription(): ?string
{
return $this->title;
}
}
4 changes: 4 additions & 0 deletions app/src/Domain/Chat/SessionRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ public function findByUuid(UuidInterface $uuid): ?SessionInterface;
* @throws SessionNotFoundException
*/
public function getByUuid(UuidInterface $uuid): SessionInterface;

public function findOneLatest(): ?SessionInterface;

public function findAllLatest(int $limit = 3): iterable;
}
12 changes: 11 additions & 1 deletion app/src/Domain/Chat/SimpleChatService.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function startSession(UuidInterface $accountUuid, string $agentName): Uui
);

// Set the title of the session to the agent's description.
$session->title = $agent->getDescription();
$session->setDescription($agent->getDescription());

$this->updateSession($session);

Expand Down Expand Up @@ -222,4 +222,14 @@ private function callTool(Session $session, ToolCall $tool): ToolCallResultMessa
content: [$functionResult],
);
}

public function getLatestSession(): ?SessionInterface
{
return $this->sessions->findOneLatest();
}

public function getLatestSessions(int $limit = 3): array
{
return \iterator_to_array($this->sessions->findAllLatest($limit));
}
}
20 changes: 0 additions & 20 deletions app/src/Domain/Tool/PhpTool.php

This file was deleted.

11 changes: 10 additions & 1 deletion app/src/Endpoint/Console/ChatCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use LLM\Agents\Tool\ToolRegistryInterface;
use Ramsey\Uuid\Uuid;
use Spiral\Console\Attribute\AsCommand;
use Spiral\Console\Attribute\Option;
use Spiral\Console\Command;
use Spiral\Console\Console;
use Symfony\Component\Console\Cursor;
Expand All @@ -21,6 +22,9 @@
)]
final class ChatCommand extends Command
{
#[Option(name: 'latest', shortcut: 'l', description: 'Open latest chat session')]
public bool $openLatest = false;

public function __invoke(
AgentRegistryInterface $agents,
ChatServiceInterface $chat,
Expand All @@ -41,6 +45,11 @@ public function __invoke(
tools: $tools,
);

$chat->run(accountUuid: Uuid::fromString('00000000-0000-0000-0000-000000000000'));
$chat->run(
accountUuid: Uuid::fromString('00000000-0000-0000-0000-000000000000'),
openLatest: $this->openLatest,
);

return self::SUCCESS;
}
}
6 changes: 4 additions & 2 deletions app/src/Endpoint/Console/ChatWindowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
final class ChatWindowCommand extends Command
{
#[Argument(name: 'session_uuid')]
public string $sessionUuid;
public ?string $sessionUuid = null;

public function __invoke(
ChatHistoryRepositoryInterface $chatHistory,
Expand All @@ -32,7 +32,9 @@ public function __invoke(
chat: $chatService,
);

$chatWindow->run(Uuid::fromString($this->sessionUuid));
$chatWindow->run(
sessionUuid: $this->sessionUuid ? Uuid::fromString($this->sessionUuid) : null,
);

return self::SUCCESS;
}
Expand Down
Loading

0 comments on commit 29cdda7

Please sign in to comment.