Skip to content

Commit f2d2ac4

Browse files
committed
feat!: update ToolInterface::__constructor signature & add clear_cache config to resource/elements tools
1 parent be5c6f9 commit f2d2ac4

19 files changed

Lines changed: 174 additions & 30 deletions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ modAIAdmin.panel.ContextProvider = function (config) {
66

77
const configItems = [];
88

9-
if (config.record.classConfig && config.record.config) {
9+
if (config.record.classConfig) {
1010
Object.entries(config.record.classConfig).map(([key, cfg]) => {
11-
configItems.push(...modAIAdmin.formatConfigItem(key, cfg, config.record.config[key]));
11+
configItems.push(...modAIAdmin.formatConfigItem(key, cfg, config.record.config?.[key]));
1212
});
1313
}
1414

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Ext.extend(ModAIAdmin, Ext.Component, {
6767
},
6868

6969
formatConfigItem: function (key, cfg, value = undefined) {
70+
if (cfg.type === 'combo-boolean' || cfg.type === 'modx-combo-boolean') {
71+
cfg.type = 'modai-combo-boolean';
72+
cfg.extraProperties = cfg.extraProperties || {};
73+
cfg.extraProperties.useInt = true;
74+
}
75+
7076
return [
7177
{
7278
fieldLabel: cfg.name,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ modAIAdmin.panel.Tool = function (config) {
55
config.id = config.id || 'modai-panel-tool';
66

77
const configItems = [];
8-
if (config.record.classConfig && config.record.config) {
8+
if (config.record.classConfig) {
99
Object.entries(config.record.classConfig).map(([key, cfg]) => {
10-
configItems.push(...modAIAdmin.formatConfigItem(key, cfg, config.record.config[key]));
10+
configItems.push(...modAIAdmin.formatConfigItem(key, cfg, config.record.config?.[key]));
1111
});
1212
}
1313

core/components/modai/lexicon/en/default.inc.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
$_lang['modai.admin.tool.prompt'] = 'Prompt';
5252
$_lang['modai.admin.tool.prompt_desc'] = 'Description of the tool to the AI. By default it uses the default from the tool\'s class, but you can override it and customize.';
5353

54+
$_lang['modai.admin.tool.config.clear_cache'] = 'Clear Cache';
55+
$_lang['modai.admin.tool.config.clear_cache_desc'] = 'If enabled, cache will automatically clear after using this tool.';
56+
5457
$_lang['modai.admin.agent.name'] = 'Name';
5558
$_lang['modai.admin.agent.description'] = 'Description';
5659
$_lang['modai.admin.agent.enabled'] = 'Enabled';

core/components/modai/src/Config/FieldBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ public function extra(string $key, $value): self
5858
return $this;
5959
}
6060

61+
public function default($default): self
62+
{
63+
$this->field['defaultValue'] = $default;
64+
65+
return $this;
66+
}
67+
6168
public function build(): array
6269
{
6370
return $this->field;

core/components/modai/src/Tools/CreateCategory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function getConfig(modX $modx): array
5959
return [];
6060
}
6161

62-
public function __construct(modX $modx, array $config)
62+
public function __construct(modX $modx, array $config = [])
6363
{
6464
$this->modx = $modx;
6565
}

core/components/modai/src/Tools/CreateChunk.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace modAI\Tools;
44

5+
use modAI\Config\ConfigBuilder;
6+
use modAI\Config\FieldBuilder;
7+
use modAI\Utils;
58
use MODX\Revolution\modChunk;
69
use MODX\Revolution\modX;
710

811
class CreateChunk implements ToolInterface
912
{
1013
private $modx;
14+
private bool $clearCache;
1115

1216
public static function getSuggestedName(): string
1317
{
@@ -57,12 +61,23 @@ public static function getParameters(modX $modx): array
5761

5862
public static function getConfig(modX $modx): array
5963
{
60-
return [];
64+
return ConfigBuilder::new($modx)
65+
->addField('clear_cache', function (FieldBuilder $field) use ($modx) {
66+
return $field
67+
->name($modx->lexicon('modai.admin.tool.config.clear_cache'))
68+
->description($modx->lexicon('modai.admin.tool.config.clear_cache_desc'))
69+
->type('combo-boolean')
70+
->default(1)
71+
->build();
72+
})
73+
->build();
6174
}
6275

63-
public function __construct(modX $modx, array $config)
76+
public function __construct(modX $modx, array $config = [])
6477
{
6578
$this->modx = $modx;
79+
80+
$this->clearCache = intval(Utils::getConfigValue($modx, 'clear_cache', $config, '1')) === 1;
6681
}
6782

6883
/**
@@ -102,6 +117,9 @@ public function runTool(array $arguments): string
102117
];
103118
}
104119

120+
if ($this->clearCache) {
121+
$this->modx->cacheManager->refresh();
122+
}
105123

106124
return json_encode($output);
107125
}

core/components/modai/src/Tools/CreateResource.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace modAI\Tools;
44

5+
use modAI\Config\ConfigBuilder;
6+
use modAI\Config\FieldBuilder;
7+
use modAI\Utils;
58
use MODX\Revolution\modResource;
69
use MODX\Revolution\modX;
710

811
class CreateResource implements ToolInterface
912
{
1013
private $modx;
14+
private bool $clearCache;
1115

1216
public static function getSuggestedName(): string
1317
{
@@ -57,12 +61,23 @@ public static function getParameters(modX $modx): array
5761

5862
public static function getConfig(modX $modx): array
5963
{
60-
return [];
64+
return ConfigBuilder::new($modx)
65+
->addField('clear_cache', function (FieldBuilder $field) use ($modx) {
66+
return $field
67+
->name($modx->lexicon('modai.admin.tool.config.clear_cache'))
68+
->description($modx->lexicon('modai.admin.tool.config.clear_cache_desc'))
69+
->type('combo-boolean')
70+
->default(1)
71+
->build();
72+
})
73+
->build();
6174
}
6275

63-
public function __construct(modX $modx, array $config)
76+
public function __construct(modX $modx, array $config = [])
6477
{
6578
$this->modx = $modx;
79+
80+
$this->clearCache = intval(Utils::getConfigValue($modx, 'clear_cache', $config, '1')) === 1;
6681
}
6782

6883
/**
@@ -81,6 +96,8 @@ public function runTool(array $arguments): string
8196

8297
$output = [];
8398

99+
$contexts = [];
100+
84101
foreach ($arguments['resources'] as $data) {
85102
if (!isset($data['parent'])) {
86103
return json_encode(['success' => false, 'message' => 'parent is required.']);
@@ -113,13 +130,24 @@ public function runTool(array $arguments): string
113130
$resource->set('published', false);
114131
$resource->save();
115132

133+
$contexts[$resource->get('context_key')] = true;
134+
116135
$output[] = [
117136
'id' => $resource->get('id'),
118137
'pagetitle' => $resource->get('pagetitle'),
119138
'edit_url' => $this->modx->config['manager_url'] . '?a=resource/update&id=' . $resource->get('id'),
120139
];
121140
}
122141

142+
if ($this->clearCache && !empty($contexts)) {
143+
$contexts = array_keys($contexts);
144+
$this->modx->cacheManager->refresh([
145+
'db' => [],
146+
'auto_publish' => ['contexts' => $contexts],
147+
'context_settings' => ['contexts' => $contexts],
148+
'resource' => ['contexts' => $contexts],
149+
]);
150+
}
123151

124152
return json_encode($output);
125153
}

core/components/modai/src/Tools/CreateTemplate.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace modAI\Tools;
44

5+
use modAI\Config\ConfigBuilder;
6+
use modAI\Config\FieldBuilder;
7+
use modAI\Utils;
58
use MODX\Revolution\modTemplate;
69
use MODX\Revolution\modX;
710

811
class CreateTemplate implements ToolInterface
912
{
1013
private $modx;
14+
private bool $clearCache;
1115

1216
public static function getSuggestedName(): string
1317
{
@@ -57,12 +61,23 @@ public static function getParameters(modX $modx): array
5761

5862
public static function getConfig(modX $modx): array
5963
{
60-
return [];
64+
return ConfigBuilder::new($modx)
65+
->addField('clear_cache', function (FieldBuilder $field) use ($modx) {
66+
return $field
67+
->name($modx->lexicon('modai.admin.tool.config.clear_cache'))
68+
->description($modx->lexicon('modai.admin.tool.config.clear_cache_desc'))
69+
->type('combo-boolean')
70+
->default(1)
71+
->build();
72+
})
73+
->build();
6174
}
6275

63-
public function __construct(modX $modx, array $config)
76+
public function __construct(modX $modx, array $config = [])
6477
{
6578
$this->modx = $modx;
79+
80+
$this->clearCache = intval(Utils::getConfigValue($modx, 'clear_cache', $config, '1')) === 1;
6681
}
6782

6883
/**
@@ -102,6 +117,9 @@ public function runTool(array $arguments): string
102117
];
103118
}
104119

120+
if ($this->clearCache) {
121+
$this->modx->cacheManager->refresh();
122+
}
105123

106124
return json_encode($output);
107125
}

core/components/modai/src/Tools/EditChunk.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace modAI\Tools;
44

5+
use modAI\Config\ConfigBuilder;
6+
use modAI\Config\FieldBuilder;
7+
use modAI\Utils;
58
use MODX\Revolution\modChunk;
69
use MODX\Revolution\modX;
710

811
class EditChunk implements ToolInterface
912
{
1013
private $modx;
14+
private bool $clearCache;
1115

1216
public static function getSuggestedName(): string
1317
{
@@ -50,12 +54,23 @@ public static function getParameters(modX $modx): array
5054

5155
public static function getConfig(modX $modx): array
5256
{
53-
return [];
57+
return ConfigBuilder::new($modx)
58+
->addField('clear_cache', function (FieldBuilder $field) use ($modx) {
59+
return $field
60+
->name($modx->lexicon('modai.admin.tool.config.clear_cache'))
61+
->description($modx->lexicon('modai.admin.tool.config.clear_cache_desc'))
62+
->type('combo-boolean')
63+
->default(1)
64+
->build();
65+
})
66+
->build();
5467
}
5568

56-
public function __construct(modX $modx, array $config)
69+
public function __construct(modX $modx, array $config = [])
5770
{
5871
$this->modx = $modx;
72+
73+
$this->clearCache = intval(Utils::getConfigValue($modx, 'clear_cache', $config, '1')) === 1;
5974
}
6075

6176
/**
@@ -82,6 +97,10 @@ public function runTool(array $arguments): string
8297
return json_encode(['success' => true, 'message' => 'Chunk updated. Use with: [[$' . $chunk->get('name') . ']]']);
8398
}
8499

100+
if ($this->clearCache) {
101+
$this->modx->cacheManager->refresh();
102+
}
103+
85104
return json_encode(['success' => false, 'message' => 'Could not save chunk.']);
86105
}
87106

0 commit comments

Comments
 (0)