/** Datei: plugins/agents/library/ARUNOX/Plugins/Agents/Tools/Model.php **/ Agents\Model::getValue( $agentId, "id" ), ])); } } /** Datei: plugins/agents/library/ARUNOX/Plugins/Agents/Tools/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Agents\Tools\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\REST; class Routes { public static function register() : void { Router::addAll( Controller::class ); Hooks::add('router::get->api.v1.rest.routes', function( $routes ) { $routes[] = [ "path" => "/agents/tools/{id}", "label" => "Bearbeiten", ]; return $routes; }); } } /** Datei: plugins/agents/library/ARUNOX/Plugins/Agents/Tools/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Agents\Tools\API\v1; use ARUNOX\API; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Agents; use ARUNOX\Database\Attribute; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "Tool nicht gefunden", "saved" => "Tool wurde erfolgreich gespeichert", "deleted" => "Tool wurde erfolgreich gelöscht", "created" => "Tool wurde erfolgreich erstellt", "statuschanged.0" => "Tool wurde erfolgreich deaktiviert", "statuschanged.1" => "Tool wurde erfolgreich aktiviert", "notcreated" => "Tool konnte nicht erstellt werden", "notsaved" => "Tool konnte nicht gespeichert werden", ]; /* ### PUBLIC FUNCTIONS ######################################### # */ /** * @api * @method GET * * @route.path /agents/{hashid}/tools * @route.link /agents/{hashid}#tools * @route.query.status integer|options=/agents/tools/options/status * @route.description Listet alle Tools eines Agenten */ public static function getAllOwn( string $hashid, array $where = [] ) : array { return Agents\Tools\Model::getByAgent( $hashid, $where ); } /** * @api * @method GET * * @route.path /agents/{hashid}/tools/{id} * @route.description Gibt ein einzelnes Tool eines Agenten zurück */ public static function get( int $origin, int $id ) : BaseEntity | HTTP\Response { if( $result = Agents\Tools\Model::get([ "id" => $id, "origin_id" => $origin ]) ) { return $result; } return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method GET * * @route.path /agents/tools/options/type * @route.description Gibt ein alle Optionen für type zurück */ public static function getTypeAsOptions() : array { return Agents\Tools\Type::getAll(); } /** * @api * @method GET * * @route.path /agents/tools/options/status * @route.description Gibt ein alle Optionen für status zurück */ public static function getStatusAsOptions() : array { return Attribute\Status::getAll(); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method PUT * * @route.path /agents/{hashid}/tools/{id} * @route.description Aktualisiert die Eigenschaften eines Tools */ public static function save( string $origin, int $id, array $data ) : HTTP\Response { $agent = Agents\Model::getValue( $origin ); if( $result = Agents\Tools\Model::get([ "id" => $id, "origin_id" => $agent ]) ) { if( $result->save( $data ) ) { return REST\Response::jsonResponse( 200, static::$NOTIFY["saved"] ); } return REST\Response::jsonResponse( 500, static::$NOTIFY["notsaved"] ); } return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method POST * * @route.path /agents/{hashid}/tools * @route.description Erstellt ein neues Tool für einen Agenten */ public static function create( string $origin, array $data ) : HTTP\Response { $data[ "origin_id" ] = Agents\Model::getValue( $origin ); if( $id = Agents\Tools\Model::save( $data ) ) { $result = Agents\Tools\Model::get( $id ); return REST\Response::jsonResponse( 200, static::$NOTIFY["created"], [ "data" => $result ] ); } return REST\Response::jsonResponse( 500, static::$NOTIFY["notfound"] ); } /** * @api * @method DELETE * * @route.path /agents/{hashid}/tools/{id} * @route.description Löscht ein Tool eines Agenten */ public static function delete( string $origin, int $id ) : HTTP\Response { $agent = Agents\Model::getValue( $origin ); if( Agents\Tools\Model::delete([ "id" => $id, "origin_id" => $agent ]) ) { return REST\Response::jsonResponse( 200, static::$NOTIFY["deleted"] ); } return REST\Response::jsonResponse( 500, static::$NOTIFY["notfound"] ); } /** * @api * @method PATCH * * @route.path /agents/{hashid}/tools/{id}/status * @route.description Umschalten des Status eines Tools */ public static function toggleStatus( string $origin, int $id ) : HTTP\Response { $agent = Agents\Model::getValue( $origin ); if( $result = Agents\Tools\Model::get([ "id" => $id, "origin_id" => $agent ]) ) { $result->toggleStatus( TRUE ); return REST\Response::jsonResponse( 200, static::$NOTIFY["statuschanged.{$result->status->getValue()}"], [ "data" => $result ] ); } return REST\Response::jsonResponse( 404, static::$NOTIFY["notfound"] ); } /* ### PUBLIC VALIDATE FUNCTIONS ################################ # */ /* ### PUBLIC REST FUNCTIONS #################################### # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method PUT * * @route.path /agents/{hashid}/tools/{id} * @route.description REST-Antwort nach dem Speichern eines Tools */ public static function restResponseSave( string $origin, int $id, array $data ) : HTTP\Response { $agent = Agents\Model::getValue( $origin ); $result = static::save( $agent, $id, $data ); if( $result->isStatus( 200 ) ) { $data = Agents\Tools\Model::get([ "id" => $id, "origin_id" => $agent ]); REST\Response::closeModals(); REST\Response::addUpdater( [ "data/table/row", "/api/v1/agents/{$origin}/tools", $id ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method POST * * @route.path /agents/{hashid}/tools * @route.description REST-Antwort nach dem Erstellen eines Tools */ public static function restResponseCreate( string $origin, array $data ) : HTTP\Response { $agent = Agents\Model::getValue( $origin ); $result = static::create( $agent, $data ); if( $result->isStatus( 200 ) ) { REST\Response::closeModals(); REST\Response::redirect( "/agents/{$origin}#tools" ); } return static::restResponse( $result ); } /** * @internal rest * @method DELETE * * @route.path /agents/{hashid}/tools/{id} * @route.description REST-Antwort nach dem Löschen eines Tools */ public static function restResponseDelete( string $origin, int $id ) : HTTP\Response { REST\Response::closeModals(); $agent = Agents\Model::getValue( $origin ); $result = static::delete( $agent, $id ); if( $result->isStatus( 200 ) ) { REST\Response::redirect( "/agents/{$origin}#tools" ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /agents/{hashid}/tools/{id}/status * @route.description REST-Antwort zur Statusänderung eines Tools */ public static function restResponseToggleStatus( string $origin, int $id ) : HTTP\Response { $agent = Agents\Model::getValue( $origin ); $result = static::toggleStatus( $agent, $id ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); REST\Response::addUpdater( [ "data/table/row", "/api/v1/agents/{$origin}/tools", $id ], $data ); } return static::restResponse( $result ); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /agents/{hashid}/tools/{id} * @route.description REST-Detailansicht für ein Tool */ public static function restConfigDetails( string $origin, int $id ) : REST\Generator\Base\BaseGenerator { $agent = Agents\Model::getValue( $origin ); $tool = Agents\Tools\Model::get([ "id" => $id, "origin_id" => $agent ]); $form = REST\Response::addModal( "details", "Tool bearbeiten" ) ->setData( $tool ) ->setTitle( $tool->type->getLabel() ) ->setIcon( $tool->type->getIcon() ) ->setColor( $tool->type->getColor() ) ->get() ->setAction( "/agents/{$origin}/tools/{id}", "PUT" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Speichern", "agents/{origin_id}/tools" )) ->addText( "Geben Sie diesem Tool eine für Sie passende Beschreibung, zur besseren Wiedererkennung." ) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "name", TRUE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "description", TRUE, "agents/{origin_id}/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "context", FALSE, "agents/tools" )) ->addHeadline( "Einstellungen", "3" ); switch( $tool->type->getValue() ) { case Agents\Tools\Type::SystemTool : static::restConfigFormTypeSystemTool( $form ); break; case Agents\Tools\Type::CallForward : static::restConfigFormTypeCallForward( $form ); break; case Agents\Tools\Type::Calendar : static::restConfigFormTypeCalendar( $form ); break; case Agents\Tools\Type::APIRequest : static::restConfigFormTypeAPIRequest( $form ); break; case Agents\Tools\Type::ConversionFlow : static::restConfigFormTypeConversionFlow( $form ); break; } return $form; } /** * @internal rest * @method GET * * @route.path /agents/{hashid}/tools/create * @route.query.type integer|required|options=/agents/tools/options/type * @route.description REST-Formularkonfiguration zum Erstellen eines Tools */ public static function restConfigCreate( string $origin, $args ) : REST\Generator\Base\BaseGenerator { $agent = Agents\Model::getValue( $origin ); $option = Agents\Tools\Type::get( $args["type"] ?? 3 ); $form = REST\Response::addModal( "details", "Tool erstellen" ) ->setTitle( $option["label"] ) ->setIcon( $option["icon"] ) ->setColor( $option["color"] ) ->get() ->setAction( "/agents/{$origin}/tools", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "agents/tools" )) ->addText( "Geben Sie diesem Tool eine für Sie passende Beschreibung, zur besseren Wiedererkennung." ) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "name", TRUE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "description", TRUE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "context", FALSE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->hidden", [], "type", $args["type"], "agents/tools" )) ->addHeadline( "Einstellungen", "3" ); switch( $args["type"] ) { case Agents\Tools\Type::SystemTool : static::restConfigFormTypeSystemTool( $form ); break; case Agents\Tools\Type::CallForward : static::restConfigFormTypeCallForward( $form ); break; case Agents\Tools\Type::Calendar : static::restConfigFormTypeCalendar( $form ); break; case Agents\Tools\Type::APIRequest : static::restConfigFormTypeAPIRequest( $form ); break; case Agents\Tools\Type::ConversionFlow : static::restConfigFormTypeConversionFlow( $form ); break; } return $form; } /** * @internal rest * @method GET * * @route.path /agents/{hashid}/tools/{id}/delete * @route.description REST-Bestätigungsdialog zum Löschen eines Tools */ public static function restConfigDeleteOwn( string $origin, int $id ) : REST\Generator\Base\BaseGenerator { $agent = Agents\Model::getValue( $origin ); return REST\Response::addModal( "details", "Tool Löschen" ) ->setSize( REST\Wrapper\Modal\Size::sm ) ->get() ->setAction( "/agents/{$origin}/tools/{$id}", "DELETE" ) ->addText( "Möchtest du wirklich den Datensatz (#{$id}) löschen? " ) ->addButton(...Hooks::filter( "rest.generator::button.type->cancel", [], NULL, "agents/{origin_id}/tools" )) ->addButton(...Hooks::filter( "rest.generator::button.type->delete", [], NULL, "agents/{origin_id}/tools" )); } /* ### PUBLIC REST EXTENSION FUNCTIONS ########################## # */ public static function restConfigTable( string $hashid ) : REST\Generator\Base\BaseGenerator { $table = REST\Generator\Data\Table::create() ->setApi( "/agents/{$hashid}/tools" ) ->setAction( "/agents/{$hashid}/tools/{id}" ) ->setOrder( "name", "asc" ) ->setSearch( TRUE ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [], "agents/{$hashid}/tools" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [ "icon" => "bi-ui-checks-grid", "options" => Agents\Tools\Type::getAll(), ], "type", [], "agents/{origin_id}/tools" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [], "status", [], "agents/{$hashid}/tools" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [ "label" => "Description", ], "output", TRUE, TRUE, TRUE, "agents/{origin_id}/tools" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [ "label" => "Kontext", ], "context", TRUE, TRUE, TRUE, "agents/{origin_id}/tools" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_edit", FALSE, FALSE, TRUE, "agents/{id}/tools" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->actions", [], [], FALSE, "agents/{id}/tools" ))->get() ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "agents/{$hashid}/tools" )) ->addButton(...Hooks::filter( "rest.generator::action.name->delete", [], "agents/{$hashid}/tools" )) ; $buttons = $table->top()->getButtons(); foreach( Agents\Tools\Type::getAll() as $type ) { $type["name"] = "type"; $buttons->addDropdown( ...Hooks::filter( "rest.generator::button.name->create", $type, "agents/{$hashid}/tools" ) ); } return $table; } /* ### PUBLIC REST TYPE FUNCTIONS ############################### # */ public static function restConfigFormTypeSystemTool( REST\Generator\Base\BaseGenerator $form ) { $form->top()->get() ->addField(...Hooks::filter( "rest.generator::control.type->textarea", [ "label" => "Dynamische Parameter", "value" => "[]", ], "parameters", FALSE, "agents/tools" )) ; } public static function restConfigFormTypeCallForward( REST\Generator\Base\BaseGenerator $form ) { $form->top()->get() ->addField(...Hooks::filter( "rest.generator::control.type->tel", [], "phone", TRUE, "agents/{origin_id}/tools" )) ; } public static function restConfigFormTypeCalendar( REST\Generator\Base\BaseGenerator $form ) { $form ->get()->setTitle( "Aktuell nur Platzhalter / Muster") ->top(); } public static function restConfigFormTypeAPIRequest( REST\Generator\Base\BaseGenerator $form ) { $form->top()->get() ->addField(...Hooks::filter( "rest.generator::control.type->text", [ "label" => "Ausführungstext", "hint" => "Text der während Ausführung gesprochen wird. Leer lassen für keine Antwort.", ], "response", FALSE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->url", [], "url", TRUE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "choose_id" => API\Method::GET, "options" => API\Method::getAll(), ], "method", TRUE, FALSE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->textarea", [ "value" => "[]", ], "header", FALSE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->textarea", [ "label" => "Dynamische Parameter", "value" => "[]", ], "parameters", FALSE, "agents/tools" )) ->addField(...Hooks::filter( "rest.generator::control.type->textarea", [ "label" => "Default / Fixe Parameter", "value" => "[]", ], "defaults", FALSE, "agents/tools" )) ; } public static function restConfigFormTypeConversionFlow( REST\Generator\Base\BaseGenerator $form ) { $form->top()->get() ->addField(...Hooks::filter( "rest.generator::control.type->textarea", [ "label" => "Step", "value" => "[]", ], "steps", FALSE, "agents/tools" )) ; } } /** Datei: plugins/agents/library/ARUNOX/Plugins/Agents/Tools/Type.php **/ [ "color" => REST\Generator\Config\Color::Default, "icon" => "bi bi-tools", "label" => "Systemtool" ], self::CallForward => [ "color" => REST\Generator\Config\Color::Primary, "icon" => "bi bi-telephone-forward", "label" => "Anrufweiterleitung" ], self::Calendar => [ "color" => REST\Generator\Config\Color::Secondary, "icon" => "bi bi-calendar-plus", "label" => "Terminbuchung" ], self::APIRequest => [ "color" => REST\Generator\Config\Color::Danger, "icon" => "bi bi-plugin", "label" => "API Request" ], self::ConversionFlow => [ "color" => REST\Generator\Config\Color::Info, "icon" => "bi bi-chat-dots", "label" => "Gesprächsablauf" ], ]; /* ### PUBLIC FUNCTIONS ######################################### # */ } /** Datei: plugins/agents/library/ARUNOX/Plugins/Agents/Tools/Entity.php **/ Attribute\Status::class, "date_create" => Attribute\DateTime::class, "date_edit" => Attribute\DateTime::class, "type" => Type::class, "output" => "output", ]; /* ### PUBLIC FUNCTIONS ######################################### # */ public function getOutput() { return match( $this->type->getValue() ) { Type::CallForward => "{$this->phone} » {$this->description}", default => $this->description, }; } public function toggleStatus( bool $save = FALSE ) { $this->status->toggle(); if( $save ) $this->save(); } }