Skip to content

Commit 30b26b1

Browse files
committed
feat: add basic modx tools
1 parent 78d82eb commit 30b26b1

File tree

14 files changed

+1049
-28
lines changed

14 files changed

+1049
-28
lines changed

assets/components/modai/mgr/js/modai.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ Ext.extend(ModAIAdmin, Ext.Component, {
6464

6565
return parts.join('&');
6666
},
67+
68+
formatConfigItem: function (key, cfg, value = undefined) {
69+
return [
70+
{
71+
fieldLabel: cfg.name,
72+
allowBlank: !cfg.required,
73+
xtype: cfg.type,
74+
name: `config_${key}`,
75+
hiddenName: `config_${key}`,
76+
value: value ?? cfg.defaultValue
77+
},
78+
{
79+
xtype: 'label',
80+
html: cfg.description,
81+
cls: 'desc-under'
82+
}
83+
];
84+
}
6785
});
6886
Ext.reg('modai-admin', ModAIAdmin);
6987
modAIAdmin = new ModAIAdmin();

assets/components/modai/mgr/js/related_agents/window.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
modAIAdmin.window.RelatedAgents = function (config) {
22
config = config || {};
33
Ext.applyIf(config, {
4-
title: _('modai.admin.agent_context_provider.create'),
4+
title: _('modai.admin.related_agent.create'),
55
closeAction: 'close',
66
url: MODx.config.connector_url,
77
action: 'modAI\\Processors\\RelatedAgents\\Create',

assets/components/modai/mgr/js/tool/panel.js

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,21 @@ modAIAdmin.panel.Tool = function (config) {
77

88
if (config.record.classConfig) {
99
Object.entries(config.record.classConfig).map((([key, cfg]) => {
10-
configItems.push({
11-
fieldLabel: cfg.name,
12-
allowBlank: !cfg.required,
13-
xtype: cfg.type,
14-
name: `config_${key}`,
15-
value: config.record.config[key]
16-
});
17-
18-
configItems.push({
19-
xtype: 'label',
20-
html: cfg.description,
21-
cls: 'desc-under'
22-
});
10+
configItems.push(...modAIAdmin.formatConfigItem(key, cfg, config.record.config[key]));
11+
12+
// configItems.push({
13+
// fieldLabel: cfg.name,
14+
// allowBlank: !cfg.required,
15+
// xtype: cfg.type,
16+
// name: `config_${key}`,
17+
// value: config.record.config[key]
18+
// });
19+
//
20+
// configItems.push({
21+
// xtype: 'label',
22+
// html: cfg.description,
23+
// cls: 'desc-under'
24+
// });
2325
}));
2426
}
2527

@@ -142,18 +144,7 @@ Ext.extend(modAIAdmin.panel.Tool, MODx.FormPanel, {
142144

143145
this.configSection.removeAll();
144146
Object.entries(record.data.config).forEach(([key, config]) => {
145-
this.configSection.add({
146-
fieldLabel: config.name,
147-
allowBlank: !config.required,
148-
xtype: config.type,
149-
name: `config_${key}`,
150-
});
151-
152-
this.configSection.add({
153-
xtype: 'label',
154-
html: config.description,
155-
cls: 'desc-under'
156-
});
147+
this.configSection.add(modAIAdmin.formatConfigItem(key, config));
157148
})
158149

159150
this.configSection.doLayout();
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
<?php
22
namespace modAI\Elements\Events;
33

4-
use modAI\Tools\GetWeather;
54

65
class modAIOnToolRegister extends Event
76
{
87
public function run()
98
{
10-
$this->modx->event->output(GetWeather::class);
9+
$this->modx->event->output([
10+
\modAI\Tools\GetWeather::class,
11+
12+
\modAI\Tools\GetCategories::class,
13+
\modAI\Tools\CreateCategory::class,
14+
15+
\modAI\Tools\GetChunks::class,
16+
\modAI\Tools\CreateChunk::class,
17+
\modAI\Tools\DeleteChunks::class,
18+
19+
\modAI\Tools\GetTemplates::class,
20+
\modAI\Tools\CreateTemplate::class,
21+
22+
\modAI\Tools\CreateResource::class,
23+
\modAI\Tools\GetResources::class,
24+
\modAI\Tools\GetResourceDetail::class,
25+
]);
1126
}
1227
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace modAI\Tools;
4+
5+
use MODX\Revolution\modCategory;
6+
use MODX\Revolution\modX;
7+
8+
class CreateCategory implements ToolInterface
9+
{
10+
private $modx;
11+
12+
public static function getSuggestedName(): string
13+
{
14+
return 'create_category';
15+
}
16+
17+
public static function getDescription(): string
18+
{
19+
return "ALWAYS ask for explicit user confirmation in a separate message before calling this function, even if user asks directly for creating categories, you HAVE TO ask for their confirmation in a separate message, provide a list of name, and parent category name that you want to create. If needed, use an appropriate tool to first list available categories. Creates new MODX categories and returns their IDs.";
20+
}
21+
22+
public static function getParameters(): array
23+
{
24+
return [
25+
'type' => 'object',
26+
'properties' => [
27+
'categories' => [
28+
'type' => 'array',
29+
'items' => [
30+
'type' => 'object',
31+
'properties' => [
32+
'name' => [
33+
'type' => 'string',
34+
"description" => 'Name of the category'
35+
],
36+
'parent_id' => [
37+
'type' => 'number',
38+
"description" => 'ID of the parent category, use 0 for root category.'
39+
],
40+
'children' => [
41+
'type' => 'array',
42+
'items' => [
43+
'type' => 'string',
44+
],
45+
"description" => 'List of child category names.'
46+
],
47+
],
48+
"required" => ["name", "parent_id"]
49+
],
50+
"description" => "List of categories to create"
51+
],
52+
],
53+
"required" => ["categories"]
54+
];
55+
}
56+
57+
public static function getConfig(): array
58+
{
59+
return [];
60+
}
61+
62+
public function __construct(modX $modx, array $config)
63+
{
64+
$this->modx = $modx;
65+
}
66+
67+
/**
68+
* @param array $parameters
69+
* @return string
70+
*/
71+
public function runTool($parameters): string
72+
{
73+
if (empty($parameters)) {
74+
throw new \Exception('Parameters are required.');
75+
}
76+
77+
$output = [];
78+
79+
foreach ($parameters['categories'] as $data) {
80+
$category = $this->modx->newObject(modCategory::class);
81+
$category->set('category', $data['name']);
82+
$category->set('parent', $data['parent_id']);
83+
$category->save();
84+
85+
$output[] = [
86+
'id' => $category->get('id'),
87+
'name' => $category->get('category'),
88+
'parent' => $category->get('parent'),
89+
];
90+
91+
if (!empty($data['children'])) {
92+
foreach ($data['children'] as $child) {
93+
$childCategory = $this->modx->newObject(modCategory::class);
94+
$childCategory->set('category', $child);
95+
$childCategory->set('parent', $category->get('id'));
96+
$childCategory->save();
97+
98+
$output[] = [
99+
'id' => $childCategory->get('id'),
100+
'name' => $childCategory->get('category'),
101+
'parent' => $childCategory->get('parent'),
102+
];
103+
}
104+
}
105+
}
106+
107+
return json_encode($output);
108+
}
109+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace modAI\Tools;
4+
5+
use MODX\Revolution\modChunk;
6+
use MODX\Revolution\modX;
7+
8+
class CreateChunk implements ToolInterface
9+
{
10+
private $modx;
11+
12+
public static function getSuggestedName(): string
13+
{
14+
return 'create_chunk';
15+
}
16+
17+
public static function getDescription(): string
18+
{
19+
return "ALWAYS ask for explicit user confirmation in a separate message before calling this function, even if user asks directly for creating chunk, you HAVE TO ask for their confirmation in a separate message, provide a list of name, description and category name, DON'T output content, that you want to create. If needed, use an appropriate tool to first create categories, wait for it's response and then continue with calling this tool. Creates new MODX chunks. Don't ask for the parameters, unless they were already provided.";
20+
}
21+
22+
public static function getParameters(): array
23+
{
24+
return [
25+
'type' => 'object',
26+
'properties' => [
27+
'chunks' => [
28+
'type' => 'array',
29+
'items' => [
30+
'type' => 'object',
31+
'properties' => [
32+
'name' => [
33+
'type' => 'string',
34+
"description" => 'Name of the chunk'
35+
],
36+
'description' => [
37+
'type' => 'string',
38+
"description" => 'Description of the chunk, summary of what the chunk renders'
39+
],
40+
'category_id' => [
41+
'type' => 'number',
42+
"description" => 'ID of a category, it MUST be obtained from an appropriate tool, DON\'T guess this value.'
43+
],
44+
'content' => [
45+
'type' => 'string',
46+
"description" => 'HTML content of the chunk'
47+
],
48+
],
49+
"required" => ["name", "description", 'content']
50+
],
51+
"description" => "List of chunks to create"
52+
],
53+
],
54+
"required" => ["chunks"]
55+
];
56+
}
57+
58+
public static function getConfig(): array
59+
{
60+
return [];
61+
}
62+
63+
public function __construct(modX $modx, array $config)
64+
{
65+
$this->modx = $modx;
66+
}
67+
68+
/**
69+
* @param array $parameters
70+
* @return string
71+
*/
72+
public function runTool($parameters): string
73+
{
74+
if (empty($parameters)) {
75+
throw new \Exception('Parameters are required.');
76+
}
77+
78+
$output = [];
79+
80+
foreach ($parameters['chunks'] as $data) {
81+
$chunk = $this->modx->newObject(modChunk::class);
82+
$chunk->set('name', $data['name']);
83+
$chunk->set('description', $data['description']);
84+
$chunk->set('category', $data['category_id']);
85+
$chunk->set('snippet', $data['content']);
86+
$chunk->save();
87+
88+
$output[] = [
89+
'id' => $chunk->get('id'),
90+
'name' => $chunk->get('name'),
91+
];
92+
}
93+
94+
95+
return json_encode($output);
96+
}
97+
}

0 commit comments

Comments
 (0)