Skip to content

Commit

Permalink
Multiple fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Remg committed Feb 5, 2024
1 parent 5b6ef48 commit 639afbc
Show file tree
Hide file tree
Showing 68 changed files with 972 additions and 247 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ LEGISLATIVES_AVECVOUS_HOST=legislatives.avecvous.code
RENAISSANCE_HOST=renaissance.code
APP_RENAISSANCE_HOST=renaissance.code
WEBHOOK_RENAISSANCE_HOST=webhook.renaissance.code
WEBHOOK_PROXY_HOST=
API_RENAISSANCE_HOST=api.renaissance.code
RENAISSANCE_QRCODE_HOST=qr.renaissance.code

Expand Down
6 changes: 3 additions & 3 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ security:
- ROLE_APP_ADMIN_AUDIENCE_ALL
ROLE_ADMIN_COMMUNICATION_QR_CODES:
- ROLE_APP_ADMIN_QR_CODE_ALL
ROLE_ADMIN_COMMUNICATION_CHATBOTS:
- ROLE_APP_ADMIN_CHATBOT_CHATBOT_ALL

## Adhérents
ROLE_ADMIN_ADHERENT_ADHERENTS:
Expand Down Expand Up @@ -309,12 +311,10 @@ security:
- ROLE_APP_ADMIN_CLIENT_ALL
ROLE_ADMIN_TECH_SCOPES:
- ROLE_APP_ADMIN_SCOPE_ALL
ROLE_ADMIN_TECH_CHATBOTS:
- ROLE_APP_ADMIN_CHATBOT_CHATBOT_ALL
ROLE_ADMIN_TECH_TELEGRAM_BOTS:
- ROLE_APP_ADMIN_TELEGRAM_BOT_ALL
ROLE_ADMIN_TECH_OPENAI_ASSISTANTS:
- ROLE_APP_ADMIN_OPEN_AI_ASSISTANT_ALL
- ROLE_APP_ADMIN_OPENAI_ASSISTANT_ALL

## Idées
ROLE_ADMIN_IDEES_OLDOLF:
Expand Down
12 changes: 12 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ services:
tags: ['app.acquisition_statistics.calculator']
App\Adherent\Tag\TagGenerator\TagGeneratorInterface:
tags: ['app.adherent.tag.generator']
App\Chatbot\Assistant\AssistantHandlerInterface:
tags: ['app.chatbot.assistant_handler']
App\Telegram\Update\UpdateHandlerInterface:
tags: ['app.telegram.update_handler']

# Imports
App\:
Expand Down Expand Up @@ -491,3 +495,11 @@ services:
App\Adhesion\Handler\GenerateActivationCodeCommandHandler:
arguments:
$loginLinkHandler: '@security.authenticator.login_link_handler.main'

App\Chatbot\Assistant:
arguments:
$assistantHandlers: !tagged_iterator 'app.chatbot.assistant_handler'

App\Telegram\UpdateHandler:
arguments:
$updateHandlers: !tagged_iterator 'app.telegram.update_handler'
8 changes: 8 additions & 0 deletions config/services/admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,14 @@
<argument />
</service>

<service id="app.admin.openai_assistant" class="App\Admin\OpenAI\AssistantAdmin">
<tag name="sonata.admin" manager-type="orm" label="Assistants OpenAI" group="Tech" />

<argument />
<argument>App\Entity\OpenAI\Assistant</argument>
<argument />
</service>

<!--
######################################################
# Formation
Expand Down
16 changes: 12 additions & 4 deletions config/services_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
public: false
bind:
$environment: "%kernel.environment%"
$webhookProxyHost: "%env(WEBHOOK_PROXY_HOST)%"

App\DataFixtures\:
resource: '../src/DataFixtures/'
Expand All @@ -26,10 +27,6 @@ services:

App\Ohme\ClientInterface: '@Tests\App\Ohme\DummyClient'

Tests\App\Chatbot\Provider\DummyProvider: ~

App\Chatbot\Provider\ProviderInterface: '@Tests\App\Chatbot\Provider\DummyProvider'

App\Mandrill\EmailClient:
class: 'Tests\App\Test\Mailer\NullEmailClient'

Expand All @@ -51,3 +48,14 @@ services:

Kreait\Firebase\Contract\DynamicLinks: '@Tests\App\Test\Firebase\DummyDynamicLinks'
Kreait\Firebase\Contract\Messaging: '@Tests\App\Test\Firebase\DummyMessaging'

Tests\App\OpenAI\Client\Client: ~
App\OpenAI\Client\ClientInterface: '@Tests\App\OpenAI\Client\Client'

Tests\App\Telegram\Client\ClientFactory: ~
App\Telegram\Client\ClientFactoryInterface: '@Tests\App\Telegram\Client\ClientFactory'

App\Telegram\Webhook\UrlGenerator: ~
Tests\App\Telegram\Webhook\ProxyUrlGenerator:
decorates: App\Telegram\Webhook\UrlGenerator
arguments: ['@.inner']
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,21 @@
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240123003828 extends AbstractMigration
final class Version20240204231024 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE openai_assistant (
id INT UNSIGNED AUTO_INCREMENT NOT NULL,
created_by_administrator_id INT DEFAULT NULL,
updated_by_administrator_id INT DEFAULT NULL,
name VARCHAR(255) NOT NULL,
identifier VARCHAR(255) NOT NULL,
open_ai_id VARCHAR(255) NOT NULL,
uuid CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\',
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
UNIQUE INDEX UNIQ_55C6718E5E237E06 (name),
UNIQUE INDEX UNIQ_55C6718E772E836A (identifier),
UNIQUE INDEX UNIQ_55C6718EF6E32D4D (open_ai_id),
UNIQUE INDEX UNIQ_55C6718ED17F50A6 (uuid),
INDEX IDX_55C6718E9DF5350C (created_by_administrator_id),
INDEX IDX_55C6718ECF1918FF (updated_by_administrator_id),
Expand Down Expand Up @@ -130,7 +125,12 @@ public function up(Schema $schema): void
SET
NULL');
$this->addSql('CREATE INDEX IDX_EDF1E88484E3FEC4 ON chatbot_message (run_id)');
$this->addSql('ALTER TABLE chatbot_run CHANGE external_id open_ai_id VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE
chatbot_run
ADD
open_ai_id VARCHAR(255) DEFAULT NULL,
CHANGE
external_id open_ai_status VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE chatbot_thread CHANGE external_id open_ai_id VARCHAR(255) DEFAULT NULL');
}

Expand Down Expand Up @@ -182,7 +182,14 @@ public function down(Schema $schema): void
text content LONGTEXT NOT NULL,
CHANGE
open_ai_id external_id VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE chatbot_run CHANGE open_ai_id external_id VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE
chatbot_run
ADD
external_id VARCHAR(255) DEFAULT NULL,
DROP
open_ai_status,
DROP
open_ai_id');
$this->addSql('ALTER TABLE chatbot_thread CHANGE open_ai_id external_id VARCHAR(255) DEFAULT NULL');
}
}
4 changes: 4 additions & 0 deletions src/Admin/Chatbot/ChatbotAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ protected function configureFormFields(FormMapper $form): void
->add('assistantType', AssistantTypeEnumType::class, [
'label' => 'Type d\'assistant',
])
->add('openAiAssistant', ModelType::class, [
'label' => 'Assistant OpenAI',
'required' => false,
])
->end()
;
}
Expand Down
64 changes: 64 additions & 0 deletions src/Admin/OpenAI/AssistantAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Admin\OpenAI;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;

class AssistantAdmin extends AbstractAdmin
{
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->remove('show');
}

protected function configureFormFields(FormMapper $form): void
{
$form
->with('Metadonnées 🧱', ['class' => 'col-md-6'])
->add('name', TextType::class, [
'label' => 'Nom',
])
->add('openAiId', TextType::class, [
'label' => 'ID OpenAI',
])
->end()
;
}

protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('name', null, [
'label' => 'Nom',
'show_filter' => true,
])
->add('openAiId', null, [
'label' => 'ID OpenAI',
'show_filter' => true,
])
;
}

protected function configureListFields(ListMapper $list): void
{
$list
->addIdentifier('name', null, [
'label' => 'Code',
])
->add('openAiId', null, [
'label' => 'ID OpenAI',
])
->add(ListMapper::NAME_ACTIONS, null, [
'virtual_field' => true,
'actions' => [
'edit' => [],
],
])
;
}
}
56 changes: 56 additions & 0 deletions src/Admin/TelegramBotAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Admin;

use App\Entity\TelegramBot;
use App\Form\Admin\StringArrayType;
use App\Telegram\Webhook\UrlHandler;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
Expand All @@ -13,6 +15,9 @@

class TelegramBotAdmin extends AbstractAdmin
{
private ?UrlHandler $webhookUrlHandler = null;
private ?TelegramBot $beforeUpdate = null;

protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->remove('show');
Expand Down Expand Up @@ -83,4 +88,55 @@ protected function configureListFields(ListMapper $list): void
])
;
}

/**
* @param TelegramBot $object
*/
protected function alterObject(object $object): void
{
if (null === $this->beforeUpdate) {
$this->beforeUpdate = clone $object;
}
}

/**
* @param TelegramBot $object
*/
protected function prePersist(object $object): void
{
$object->generateSecret();

$this->webhookUrlHandler->setWebhook($object);
}

/**
* @param TelegramBot $object
*/
protected function preUpdate(object $object): void
{
if (
$this->beforeUpdate
&& ($this->beforeUpdate->getApiToken() !== $object->getApiToken())
) {
$object->generateSecret();

$this->webhookUrlHandler->setWebhook($object);
}
}

/**
* @param TelegramBot $object
*/
protected function preRemove(object $object): void
{
$this->webhookUrlHandler->removeWebhook($object);
}

/**
* @required
*/
public function setWebhookUrlHandler(UrlHandler $webhookUrlHandler): void
{
$this->webhookUrlHandler = $webhookUrlHandler;
}
}
32 changes: 32 additions & 0 deletions src/Chatbot/Enum/RunStatusEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Chatbot\Enum;

use App\OpenAI\Enum\RunStatusEnum as OpenAIRunStatusEnum;

enum RunStatusEnum: string
{
case QUEUED = 'queued';
case IN_PROGRESS = 'in_progress';
case CANCELLED = 'cancelled';
case FAILED = 'failed';
case COMPLETED = 'completed';
case UNKNOWN = 'unknown';

public const NEED_REFRESH = [
self::QUEUED,
self::IN_PROGRESS,
];

public static function fromOpenAI(OpenAIRunStatusEnum $runStatus): self
{
return match ($runStatus) {
OpenAIRunStatusEnum::QUEUED => self::QUEUED,
OpenAIRunStatusEnum::IN_PROGRESS => self::IN_PROGRESS,
OpenAIRunStatusEnum::COMPLETED => self::COMPLETED,
OpenAIRunStatusEnum::CANCELLING, OpenAIRunStatusEnum::CANCELLED => self::CANCELLED,
OpenAIRunStatusEnum::REQUIRES_ACTION, OpenAIRunStatusEnum::EXPIRED, OpenAIRunStatusEnum::FAILED => self::FAILED,
default => self::UNKNOWN
};
}
}
28 changes: 28 additions & 0 deletions src/Chatbot/EventListener/OpenAIAssistantMessageListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Chatbot\EventListener;

use App\Chatbot\Event\AssistantMessageEvent;
use App\Entity\Chatbot\Message;
use App\OpenAI\Event\AssistantMessageEvent as OpenAIAssistantMessageEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

#[AsEventListener]
class OpenAIAssistantMessageListener
{
public function __construct(private readonly EventDispatcherInterface $dispatcher)
{
}

public function __invoke(OpenAIAssistantMessageEvent $event): void
{
$message = $event->message;

if (!$message instanceof Message) {
return;
}

$this->dispatcher->dispatch(new AssistantMessageEvent($message));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use App\Chatbot\Event\AssistantMessageEvent;
use App\Telegram\Event\BotMessageEvent;
use App\Telegram\Message;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class AssistantMessageListener
#[AsEventListener]
class TelegramAssistantMessageListener
{
public function __construct(private readonly EventDispatcherInterface $dispatcher)
{
Expand Down
Loading

0 comments on commit 639afbc

Please sign in to comment.