Skip to content

Commit 1735fa9

Browse files
committed
validate agent connection
1 parent 48c8d16 commit 1735fa9

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Service/Agent/Validator.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020

2121
namespace Fusio\Impl\Service\Agent;
2222

23+
use Fusio\Engine\ConnectorInterface;
2324
use Fusio\Engine\Exception\ActionNotFoundException;
25+
use Fusio\Engine\Exception\ConnectionNotFoundException;
2426
use Fusio\Engine\Exception\FactoryResolveException;
2527
use Fusio\Engine\ProcessorInterface;
2628
use Fusio\Impl\Action\Scheme as ActionScheme;
2729
use Fusio\Impl\Service\Tenant\UsageLimiter;
2830
use Fusio\Impl\Table;
2931
use Fusio\Model\Backend\Agent;
3032
use PSX\Http\Exception as StatusCode;
33+
use Symfony\AI\Agent\AgentInterface;
3134

3235
/**
3336
* Validator
@@ -41,6 +44,7 @@
4144
public function __construct(
4245
private Table\Agent $agentTable,
4346
private Table\Action $actionTable,
47+
private ConnectorInterface $connector,
4448
private ProcessorInterface $processor,
4549
private UsageLimiter $usageLimiter
4650
) {
@@ -64,6 +68,15 @@ public function assert(Agent $agent, int $categoryId, ?string $tenantId, ?Table\
6468
$this->assertType($type);
6569
}
6670

71+
$connection = $agent->getConnection();
72+
if ($connection !== null) {
73+
$this->assertConnection($connection);
74+
} else {
75+
if ($existing === null) {
76+
throw new StatusCode\BadRequestException('Connection must not be empty');
77+
}
78+
}
79+
6780
$action = $agent->getAction();
6881
if ($action !== null) {
6982
$this->assertAction($action, $categoryId, $tenantId);
@@ -92,6 +105,19 @@ private function assertType(int $type): void
92105
}
93106
}
94107

108+
private function assertConnection(int $connectionId): void
109+
{
110+
try {
111+
$connection = $this->connector->getConnection($connectionId);
112+
} catch (ConnectionNotFoundException) {
113+
throw new StatusCode\BadRequestException('Provided connection is not available');
114+
}
115+
116+
if (!$connection instanceof AgentInterface) {
117+
throw new StatusCode\BadRequestException('Provided an invalid connection type, the connection must be an agent connection');
118+
}
119+
}
120+
95121
private function assertAction(string $action, int $categoryId, ?string $tenantId): void
96122
{
97123
$scheme = ActionScheme::wrap($action);

0 commit comments

Comments
 (0)