Skip to content

Commit 29cdda7

Browse files
authored
Merge pull request #12 from llm-agents-php/feature/code-refactoring
Code refactoring
2 parents fc3a18f + 9b2e4cd commit 29cdda7

29 files changed

+5735
-1188
lines changed

app/src/Agents/AgentsCaller/AskAgentTool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace App\Agents\AgentsCaller;
66

7-
use App\Domain\Tool\PhpTool;
87
use LLM\Agents\AgentExecutor\ExecutorInterface;
98
use LLM\Agents\LLM\Prompt\Chat\ToolCallResultMessage;
109
use LLM\Agents\LLM\Response\ToolCalledResponse;
1110
use LLM\Agents\PromptGenerator\Context;
11+
use LLM\Agents\Tool\PhpTool;
1212
use LLM\Agents\Tool\ToolExecutor;
1313

1414
/**

app/src/Agents/CodeReviewer/CodeReviewAgent.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use LLM\Agents\Agent\AgentAggregate;
99
use LLM\Agents\OpenAI\Client\OpenAIModel;
1010
use LLM\Agents\OpenAI\Client\Option;
11+
use LLM\Agents\Solution\ContextSourceLink;
1112
use LLM\Agents\Solution\MetadataType;
1213
use LLM\Agents\Solution\Model;
1314
use LLM\Agents\Solution\SolutionMetadata;
@@ -21,7 +22,11 @@ public static function create(): self
2122
key: 'code_review',
2223
name: 'Code Reviewer',
2324
description: 'Agent can list files in project with given id and then open each file and review the code',
24-
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.',
25+
instruction: <<<'INSTRUCTION'
26+
You are a code review assistant in Spiral Framework.
27+
Use the provided tools to list project files, read their contents, and submit a code review for each file.
28+
INSTRUCTION
29+
,
2530
);
2631

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

64+
$aggregate->addAssociation(new ContextSourceLink(name: 'spiral-docs'));
65+
5966
return $aggregate;
6067
}
6168
}

app/src/Agents/CodeReviewer/ListProjectTool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace App\Agents\CodeReviewer;
66

7-
use App\Domain\Tool\PhpTool;
7+
use LLM\Agents\Tool\PhpTool;
88

99
/**
1010
* @extends PhpTool<ListProjectInput>

app/src/Agents/CodeReviewer/ReadFileTool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace App\Agents\CodeReviewer;
66

7-
use App\Domain\Tool\PhpTool;
7+
use LLM\Agents\Tool\PhpTool;
88

99
/**
1010
* @extends PhpTool<ReadFileInput>

app/src/Agents/CodeReviewer/ReviewTool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace App\Agents\CodeReviewer;
66

7-
use App\Domain\Tool\PhpTool;
7+
use LLM\Agents\Tool\PhpTool;
88

99
/**
1010
* @extends PhpTool<ReviewInput>

app/src/Agents/Delivery/DeliveryAgent.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public static function create(): self
2525
key: self::NAME,
2626
name: 'Order Assistant',
2727
description: 'Helps customers with order-related questions.',
28-
instruction: 'You are a helpful customer support assistant. Use the supplied tools to assist the user.',
28+
instruction: <<<'INSTRUCTION'
29+
You are a helpful customer support assistant.
30+
Use the supplied tools to assist the user.
31+
INSTRUCTION
32+
,
2933
);
3034

3135
$aggregate = new self($agent);

app/src/Agents/Delivery/GetDeliveryDateTool.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace App\Agents\Delivery;
66

7-
use App\Domain\Tool\PhpTool;
87
use Carbon\Carbon;
8+
use LLM\Agents\Tool\PhpTool;
99

1010
/**
11-
* @extends PhpTool<DeliveryDateInput>
11+
* @extends PhpTool<DeliveryDateInput>
1212
*/
1313
final class GetDeliveryDateTool extends PhpTool
1414
{

app/src/Agents/Delivery/GetOrderNumberTool.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace App\Agents\Delivery;
66

7-
use App\Domain\Tool\PhpTool;
7+
use LLM\Agents\Tool\PhpTool;
88

99
/**
10-
* @extends PhpTool<OrderNumberInput>
10+
* @extends PhpTool<OrderNumberInput>
1111
*/
1212
final class GetOrderNumberTool extends PhpTool
1313
{

app/src/Agents/Delivery/GetProfileTool.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace App\Agents\Delivery;
66

7-
use App\Domain\Tool\PhpTool;
7+
use LLM\Agents\Tool\PhpTool;
88

99
/**
10-
* @extends PhpTool<ProfileInput>
10+
* @extends PhpTool<ProfileInput>
1111
*/
1212
final class GetProfileTool extends PhpTool
1313
{

app/src/Agents/DynamicMemoryTool/DynamicMemoryTool.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace App\Agents\DynamicMemoryTool;
66

77
use App\Application\Entity\Uuid;
8-
use App\Domain\Tool\PhpTool;
98
use LLM\Agents\Solution\MetadataType;
109
use LLM\Agents\Solution\SolutionMetadata;
10+
use LLM\Agents\Tool\PhpTool;
1111

1212
final class DynamicMemoryTool extends PhpTool
1313
{

app/src/Agents/SmartHomeControl/SmartHomeControlAgentFactory.php

-16
This file was deleted.

app/src/Agents/TaskSplitter/GetProjectDescription.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
namespace App\Agents\TaskSplitter;
66

7-
use App\Domain\Tool\PhpTool;
7+
use LLM\Agents\Tool\PhpTool;
88

99
/**
10-
* @extends PhpTool<ProjectDescriptionInput>
10+
* @extends PhpTool<ProjectDescriptionInput>
1111
*/
1212
final class GetProjectDescription extends PhpTool
1313
{

app/src/Agents/TaskSplitter/TaskCreateTool.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace App\Agents\TaskSplitter;
66

77
use App\Application\Entity\Uuid;
8-
use App\Domain\Tool\PhpTool;
8+
use LLM\Agents\Tool\PhpTool;
99
use Spiral\Boot\DirectoriesInterface;
1010
use Spiral\Files\FilesInterface;
1111

1212
/**
13-
* @extends PhpTool<TaskCreateInput>
13+
* @extends PhpTool<TaskCreateInput>
1414
*/
1515
final class TaskCreateTool extends PhpTool
1616
{

app/src/Agents/TaskSplitter/TaskSplitterAgent.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public static function create(): self
2323
key: self::NAME,
2424
name: 'Task Splitter',
2525
description: 'An agent that splits project descriptions into structured task lists.',
26-
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.',
26+
instruction: <<<'INSTRUCTION'
27+
You are a task organization assistant.
28+
29+
Your primary goal is to analyze project descriptions and break them down into well-structured task lists with subtasks.
30+
INSTRUCTION
31+
,
2732
);
2833

2934
$aggregate = new self($agent);

app/src/Domain/Chat/Session.php

+10
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ public function getAgentName(): string
7979
{
8080
return $this->agentName;
8181
}
82+
83+
public function setDescription(string $description): void
84+
{
85+
$this->title = $description;
86+
}
87+
88+
public function getDescription(): ?string
89+
{
90+
return $this->title;
91+
}
8292
}

app/src/Domain/Chat/SessionRepositoryInterface.php

+4
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ public function findByUuid(UuidInterface $uuid): ?SessionInterface;
2525
* @throws SessionNotFoundException
2626
*/
2727
public function getByUuid(UuidInterface $uuid): SessionInterface;
28+
29+
public function findOneLatest(): ?SessionInterface;
30+
31+
public function findAllLatest(int $limit = 3): iterable;
2832
}

app/src/Domain/Chat/SimpleChatService.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function startSession(UuidInterface $accountUuid, string $agentName): Uui
6363
);
6464

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

6868
$this->updateSession($session);
6969

@@ -222,4 +222,14 @@ private function callTool(Session $session, ToolCall $tool): ToolCallResultMessa
222222
content: [$functionResult],
223223
);
224224
}
225+
226+
public function getLatestSession(): ?SessionInterface
227+
{
228+
return $this->sessions->findOneLatest();
229+
}
230+
231+
public function getLatestSessions(int $limit = 3): array
232+
{
233+
return \iterator_to_array($this->sessions->findAllLatest($limit));
234+
}
225235
}

app/src/Domain/Tool/PhpTool.php

-20
This file was deleted.

app/src/Endpoint/Console/ChatCommand.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use LLM\Agents\Tool\ToolRegistryInterface;
1212
use Ramsey\Uuid\Uuid;
1313
use Spiral\Console\Attribute\AsCommand;
14+
use Spiral\Console\Attribute\Option;
1415
use Spiral\Console\Command;
1516
use Spiral\Console\Console;
1617
use Symfony\Component\Console\Cursor;
@@ -21,6 +22,9 @@
2122
)]
2223
final class ChatCommand extends Command
2324
{
25+
#[Option(name: 'latest', shortcut: 'l', description: 'Open latest chat session')]
26+
public bool $openLatest = false;
27+
2428
public function __invoke(
2529
AgentRegistryInterface $agents,
2630
ChatServiceInterface $chat,
@@ -41,6 +45,11 @@ public function __invoke(
4145
tools: $tools,
4246
);
4347

44-
$chat->run(accountUuid: Uuid::fromString('00000000-0000-0000-0000-000000000000'));
48+
$chat->run(
49+
accountUuid: Uuid::fromString('00000000-0000-0000-0000-000000000000'),
50+
openLatest: $this->openLatest,
51+
);
52+
53+
return self::SUCCESS;
4554
}
4655
}

app/src/Endpoint/Console/ChatWindowCommand.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
final class ChatWindowCommand extends Command
2020
{
2121
#[Argument(name: 'session_uuid')]
22-
public string $sessionUuid;
22+
public ?string $sessionUuid = null;
2323

2424
public function __invoke(
2525
ChatHistoryRepositoryInterface $chatHistory,
@@ -32,7 +32,9 @@ public function __invoke(
3232
chat: $chatService,
3333
);
3434

35-
$chatWindow->run(Uuid::fromString($this->sessionUuid));
35+
$chatWindow->run(
36+
sessionUuid: $this->sessionUuid ? Uuid::fromString($this->sessionUuid) : null,
37+
);
3638

3739
return self::SUCCESS;
3840
}

0 commit comments

Comments
 (0)