|
| 1 | +# Initialization |
| 2 | + |
| 3 | +modAI is available globally in manager, if you need to init it elsewhere, you can follow the instructions below. |
| 4 | + |
| 5 | +## Loading up pre-requisites |
| 6 | +The following script will get the `modai` service (which can be `null` if it doesn't exist or if user doesn't have appropriate permissions). Then it loads lexicons needed for the modAI's UI and load the main JS file. |
| 7 | +```php |
| 8 | +if (!$modx->services->has('modai')) { |
| 9 | + return; |
| 10 | +} |
| 11 | + |
| 12 | +/** @var \modAI\modAI | null $modAI */ |
| 13 | +$modAI = $modx->services->get('modai'); |
| 14 | + |
| 15 | +if ($modAI === null) { |
| 16 | + return; |
| 17 | +} |
| 18 | + |
| 19 | +foreach ($modAI->getUILexiconTopics() as $topic) { |
| 20 | + $modx->controller->addLexiconTopic($topic); |
| 21 | +} |
| 22 | + |
| 23 | +$this->modx->regClientStartupScript($this->modAI->getJSFile()); |
| 24 | +``` |
| 25 | + |
| 26 | +## Initializing modAI |
| 27 | +When the main JS file is loaded, `ModAI.init` function will be available. It takes single `config` argument, with following parameters. We recommend using helper function to supply all necessary properties to the config `$modAI->getBaseConfig()`, you can check the usage in the example at the end of this page. |
| 28 | + |
| 29 | +### Properties |
| 30 | +- `name` (string, optional): Application name |
| 31 | +- `assetsURL` (string): Base URL for application assets |
| 32 | +- `apiURL` (string): Base URL for API endpoints |
| 33 | +- `cssURL` (string): URL for application stylesheets |
| 34 | +- `translateFn` (function, optional): Translation function for internationalization |
| 35 | +- `availableAgents` (Record<string, AvailableAgent>): Map of available AI agents |
| 36 | +- `permissions` (Record<Permissions, 1 | 0>): Map of user permissions |
| 37 | + |
| 38 | +### Returns |
| 39 | +An object containing initialized modules: |
| 40 | +- `chatHistory`: Chat history management |
| 41 | +- `history`: History tracking |
| 42 | +- `executor`: Command execution |
| 43 | +- `ui`: User interface components |
| 44 | +- `lng`: Language/translation utilities |
| 45 | +- `checkPermissions`: Function to check if user has specific permission |
| 46 | +- `initOnResource`: Resource initialization |
| 47 | +- `initGlobalButton`: Global button initialization |
| 48 | + |
| 49 | +## Example |
| 50 | + |
| 51 | +```php |
| 52 | +if (!$modx->services->has('modai')) { |
| 53 | + return; |
| 54 | +} |
| 55 | + |
| 56 | +/** @var \modAI\modAI | null $modAI */ |
| 57 | +$modAI = $modx->services->get('modai'); |
| 58 | + |
| 59 | +if ($modAI === null) { |
| 60 | + return; |
| 61 | +} |
| 62 | + |
| 63 | +foreach ($modAI->getUILexiconTopics() as $topic) { |
| 64 | + $modx->controller->addLexiconTopic($topic); |
| 65 | +} |
| 66 | + |
| 67 | +$baseConfig = $modAI->getBaseConfig(); |
| 68 | +$modx->controller->addHtml(' |
| 69 | + <script type="text/javascript"> |
| 70 | + if (typeof modAI === "undefined") { |
| 71 | + Ext.onReady(function() { |
| 72 | + const modAI = ModAI.init(' . json_encode($baseConfig) . '); |
| 73 | + window.modAI = modAI; |
| 74 | + }); |
| 75 | + } |
| 76 | + </script> |
| 77 | +'); |
| 78 | + |
| 79 | +$this->modx->regClientStartupScript($this->modAI->getJSFile()); |
| 80 | +``` |
0 commit comments