2020
2121namespace Fusio \Impl \Service \Agent ;
2222
23+ use Fusio \Engine \ConnectorInterface ;
2324use Fusio \Engine \Exception \ActionNotFoundException ;
25+ use Fusio \Engine \Exception \ConnectionNotFoundException ;
2426use Fusio \Engine \Exception \FactoryResolveException ;
2527use Fusio \Engine \ProcessorInterface ;
2628use Fusio \Impl \Action \Scheme as ActionScheme ;
2729use Fusio \Impl \Service \Tenant \UsageLimiter ;
2830use Fusio \Impl \Table ;
2931use Fusio \Model \Backend \Agent ;
3032use PSX \Http \Exception as StatusCode ;
33+ use Symfony \AI \Agent \AgentInterface ;
3134
3235/**
3336 * Validator
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