Skip to content

Commit 2878fe0

Browse files
committed
initial add architect intent
1 parent 90031c0 commit 2878fe0

File tree

11 files changed

+425
-9
lines changed

11 files changed

+425
-9
lines changed

src/Messenger/AgentActionTask.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Messenger;
22+
23+
use Fusio\Impl\Table\Generated\AgentRow;
24+
25+
/**
26+
* AgentActionTask
27+
*
28+
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
29+
* @license http://www.apache.org/licenses/LICENSE-2.0
30+
* @link https://www.fusio-project.org
31+
*/
32+
readonly class AgentActionTask
33+
{
34+
public function __construct(
35+
private AgentRow $row,
36+
private int $index,
37+
private string $message,
38+
) {
39+
}
40+
41+
public function getRow(): AgentRow
42+
{
43+
return $this->row;
44+
}
45+
46+
public function getIndex(): int
47+
{
48+
return $this->index;
49+
}
50+
51+
public function getMessage(): string
52+
{
53+
return $this->message;
54+
}
55+
}

src/Messenger/AgentSchemaTask.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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\Messenger;
22+
23+
use Fusio\Impl\Table\Generated\AgentRow;
24+
25+
/**
26+
* AgentSchemaTask
27+
*
28+
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
29+
* @license http://www.apache.org/licenses/LICENSE-2.0
30+
* @link https://www.fusio-project.org
31+
*/
32+
readonly class AgentSchemaTask
33+
{
34+
public function __construct(
35+
private AgentRow $row,
36+
private int $index,
37+
private string $type,
38+
private string $message,
39+
) {
40+
}
41+
42+
public function getRow(): AgentRow
43+
{
44+
return $this->row;
45+
}
46+
47+
public function getIndex(): int
48+
{
49+
return $this->index;
50+
}
51+
52+
public function getType(): string
53+
{
54+
return $this->type;
55+
}
56+
57+
public function getMessage(): string
58+
{
59+
return $this->message;
60+
}
61+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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\Service\Agent\Intent;
25+
use Fusio\Impl\Table;
26+
use Fusio\Impl\Messenger\AgentActionTask;
27+
use Fusio\Impl\Service\Agent\Sender;
28+
use Fusio\Model\Backend\AgentMessageText;
29+
use Fusio\Model\Backend\AgentRequest;
30+
use PSX\Http\Exception\BadRequestException;
31+
use PSX\Json\Parser;
32+
use stdClass;
33+
use Symfony\AI\Agent\AgentInterface;
34+
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
35+
36+
/**
37+
* AgentActionTaskHandler
38+
*
39+
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
40+
* @license http://www.apache.org/licenses/LICENSE-2.0
41+
* @link https://www.fusio-project.org
42+
*/
43+
#[AsMessageHandler]
44+
readonly class AgentActionTaskHandler
45+
{
46+
public function __construct(private Sender $sender, private ConnectorInterface $connector, private Table\Agent $agentTable)
47+
{
48+
}
49+
50+
public function __invoke(AgentActionTask $action): void
51+
{
52+
$row = $action->getRow();
53+
$connection = $this->getConnection($row->getConnectionId());
54+
55+
$input = new AgentMessageText();
56+
$input->setType('text');
57+
$input->setContent($action->getMessage());
58+
59+
$request = new AgentRequest();
60+
$request->setIntent(Intent::ACTION->value);
61+
$request->setInput($input);
62+
63+
$response = $this->sender->send(
64+
$connection,
65+
$row->getUserId(),
66+
$row->getConnectionId(),
67+
$request,
68+
);
69+
70+
$output = $response->getOutput();
71+
72+
if (!$output instanceof AgentMessageText) {
73+
return;
74+
}
75+
76+
$row->setMessage($this->appendActionToMessage($row->getMessage(), $action->getIndex(), $output->getContent()));
77+
78+
$this->agentTable->update($row);
79+
}
80+
81+
private function appendActionToMessage(string $rawMessage, int $operationIndex, string $action): string
82+
{
83+
$message = Parser::decode($rawMessage);
84+
if (!$message instanceof stdClass) {
85+
return $rawMessage;
86+
}
87+
88+
if (!isset($message->type) || $message->type !== 'object') {
89+
return $rawMessage;
90+
}
91+
92+
if (!isset($message->payload) || !$message->payload instanceof stdClass) {
93+
return $rawMessage;
94+
}
95+
96+
if (!isset($message->payload->actions) || !is_array($message->payload->actions)) {
97+
$message->payload->actions = [];
98+
}
99+
100+
$message->payload->actions[] = [
101+
'index' => $operationIndex,
102+
'action' => $action,
103+
];
104+
105+
return Parser::encode($message);
106+
}
107+
108+
private function getConnection(int $connectionId): AgentInterface
109+
{
110+
$connection = $this->connector->getConnection($connectionId);
111+
if (!$connection instanceof AgentInterface) {
112+
throw new BadRequestException('Provided an invalid connection');
113+
}
114+
115+
return $connection;
116+
}
117+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

src/Service/Agent/Intent/ActionIntent.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use Fusio\Impl\Service\Agent\IntentInterface;
2424
use Fusio\Impl\Service\Agent\Serializer\ResultSerializer;
25+
use Fusio\Impl\Table\Generated\AgentRow;
2526
use Fusio\Model\Backend\AgentMessage;
2627
use Symfony\AI\Platform\Result\ResultInterface;
2728

@@ -136,4 +137,8 @@ public function transformResult(ResultInterface $result): AgentMessage
136137
{
137138
return $this->resultSerializer->serialize($result);
138139
}
140+
141+
public function onMessagePersisted(AgentRow $row, AgentMessage $message): void
142+
{
143+
}
139144
}

0 commit comments

Comments
 (0)