|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Fusio - Self-Hosted API Management for Builders. |
| 4 | + * For the current version and information visit <https://www.fusio-project.org/> |
| 5 | + * |
| 6 | + * Copyright (c) Christoph Kappestein <christoph.kappestein@gmail.com> |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Fusio\Impl\MessengerHandler; |
| 22 | + |
| 23 | +use Fusio\Engine\ConnectorInterface; |
| 24 | +use Fusio\Impl\Messenger\AgentSchemaTask; |
| 25 | +use Fusio\Impl\Service\Agent\Intent; |
| 26 | +use Fusio\Impl\Service\Agent\Sender; |
| 27 | +use Fusio\Impl\Table; |
| 28 | +use Fusio\Model\Backend\AgentMessageObject; |
| 29 | +use Fusio\Model\Backend\AgentMessageText; |
| 30 | +use Fusio\Model\Backend\AgentRequest; |
| 31 | +use PSX\Http\Exception\BadRequestException; |
| 32 | +use PSX\Json\Parser; |
| 33 | +use stdClass; |
| 34 | +use Symfony\AI\Agent\AgentInterface; |
| 35 | +use Symfony\Component\Messenger\Attribute\AsMessageHandler; |
| 36 | + |
| 37 | +/** |
| 38 | + * AgentSchemaTaskHandler |
| 39 | + * |
| 40 | + * @author Christoph Kappestein <christoph.kappestein@gmail.com> |
| 41 | + * @license http://www.apache.org/licenses/LICENSE-2.0 |
| 42 | + * @link https://www.fusio-project.org |
| 43 | + */ |
| 44 | +#[AsMessageHandler] |
| 45 | +readonly class AgentSchemaTaskHandler |
| 46 | +{ |
| 47 | + public function __construct(private Sender $sender, private ConnectorInterface $connector, private Table\Agent $agentTable) |
| 48 | + { |
| 49 | + } |
| 50 | + |
| 51 | + public function __invoke(AgentSchemaTask $action): void |
| 52 | + { |
| 53 | + $row = $action->getRow(); |
| 54 | + $connection = $this->getConnection($row->getConnectionId()); |
| 55 | + |
| 56 | + $input = new AgentMessageText(); |
| 57 | + $input->setType('text'); |
| 58 | + $input->setContent($action->getMessage()); |
| 59 | + |
| 60 | + $request = new AgentRequest(); |
| 61 | + $request->setIntent(Intent::SCHEMA->value); |
| 62 | + $request->setInput($input); |
| 63 | + |
| 64 | + $response = $this->sender->send( |
| 65 | + $connection, |
| 66 | + $row->getUserId(), |
| 67 | + $row->getConnectionId(), |
| 68 | + $request, |
| 69 | + ); |
| 70 | + |
| 71 | + $output = $response->getOutput(); |
| 72 | + |
| 73 | + if (!$output instanceof AgentMessageObject) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + $row->setMessage($this->appendSchemaToMessage($row->getMessage(), $action->getIndex(), $action->getType(), $output->getPayload())); |
| 78 | + |
| 79 | + $this->agentTable->update($row); |
| 80 | + } |
| 81 | + |
| 82 | + private function appendSchemaToMessage(string $rawMessage, int $operationIndex, string $type, mixed $schema): string |
| 83 | + { |
| 84 | + $message = Parser::decode($rawMessage); |
| 85 | + if (!$message instanceof stdClass) { |
| 86 | + return $rawMessage; |
| 87 | + } |
| 88 | + |
| 89 | + if (!isset($message->type) || $message->type !== 'object') { |
| 90 | + return $rawMessage; |
| 91 | + } |
| 92 | + |
| 93 | + if (!isset($message->payload) || !$message->payload instanceof stdClass) { |
| 94 | + return $rawMessage; |
| 95 | + } |
| 96 | + |
| 97 | + if (!isset($message->payload->schemas) || !is_array($message->payload->schemas)) { |
| 98 | + $message->payload->schemas = []; |
| 99 | + } |
| 100 | + |
| 101 | + $message->payload->schemas[] = [ |
| 102 | + 'index' => $operationIndex, |
| 103 | + 'type' => $type, |
| 104 | + 'schema' => $schema, |
| 105 | + ]; |
| 106 | + |
| 107 | + return Parser::encode($message); |
| 108 | + } |
| 109 | + |
| 110 | + private function getConnection(int $connectionId): AgentInterface |
| 111 | + { |
| 112 | + $connection = $this->connector->getConnection($connectionId); |
| 113 | + if (!$connection instanceof AgentInterface) { |
| 114 | + throw new BadRequestException('Provided an invalid connection'); |
| 115 | + } |
| 116 | + |
| 117 | + return $connection; |
| 118 | + } |
| 119 | +} |
0 commit comments