Skip to content

Commit bc3487b

Browse files
committed
feat: add CMP to manage context providers
1 parent 50cdaf9 commit bc3487b

File tree

32 files changed

+1827
-81
lines changed

32 files changed

+1827
-81
lines changed

_build/gpm.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ version: 0.12.0-alpha
33
lowCaseName: modai
44
namespace: modAI
55
author: 'John Peca'
6+
menus:
7+
- action: home
8+
text: modai.admin.menu.home
9+
description: modai.admin.menu.home_desc
10+
611
plugins:
712
- name: modAI
813
file: modai.php
@@ -11,6 +16,7 @@ plugins:
1116
- OnDocFormSave
1217
- OnDocFormDelete
1318
- OnResourceUndelete
19+
- modAIOnContextProviderRegister
1420

1521
systemSettings:
1622
- key: cache.lit
@@ -167,6 +173,8 @@ systemSettings:
167173
value: ''
168174

169175
build:
176+
scriptsBefore:
177+
- custom_events.gpm.php
170178
scriptsAfter:
171179
- lit.gpm.php
172180
- seed.gpm.php
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
*
4+
* THIS SCRIPT IS AUTOMATICALLY GENERATED, NO CHANGES WILL APPLY
5+
*
6+
* @package modai
7+
* @subpackage build
8+
*
9+
* @var \xPDO\Transport\xPDOTransport $transport
10+
* @var array $object
11+
* @var array $options
12+
*/
13+
14+
15+
return (function () {
16+
return new class() {
17+
/**
18+
* @var \MODX\Revolution\modX
19+
*/
20+
private $modx;
21+
22+
/**
23+
* @var int
24+
*/
25+
private $action;
26+
27+
/**
28+
* @param \MODX\Revolution\modX $modx
29+
* @param int $action
30+
* @return bool
31+
*/
32+
public function __invoke(&$modx, $action)
33+
{
34+
$this->modx =& $modx;
35+
$this->action = $action;
36+
37+
$events = [
38+
'modAIOnContextProviderRegister',
39+
];
40+
41+
if ($this->action === \xPDO\Transport\xPDOTransport::ACTION_UNINSTALL) {
42+
foreach ($events as $eventName) {
43+
$event = $modx->getObject('modEvent', ['name' => $eventName]);
44+
if ($event) {
45+
$event->remove();
46+
}
47+
}
48+
49+
return true;
50+
}
51+
52+
foreach ($events as $eventName) {
53+
$event = $modx->getObject('modEvent', ['name' => $eventName]);
54+
if (!$event) {
55+
$event = $modx->newObject('modEvent');
56+
$event->set('name', $eventName);
57+
$event->set('service', 6);
58+
$event->set('groupname', 'modAI');
59+
$event->save();
60+
}
61+
}
62+
63+
return true;
64+
}
65+
};
66+
})()($transport->xpdo, $options[xPDOTransport::PACKAGE_ACTION]);

_build/gpm_scripts/gpm.script.migrator.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,95 @@ public function __invoke(&$modx)
8383
$oldSetting->remove();
8484
}
8585
}
86+
};
87+
})(),
88+
(function () {
89+
return new class() {
90+
// migration runs if self::VERSION > currently installed version
91+
const VERSION = '0.12.0-pl';
92+
93+
/**
94+
* @var \MODX\Revolution\modX
95+
*/
96+
private $modx;
97+
98+
private $systemSettingsMap = [
99+
'modai.api.chatgpt.key' => 'modai.api.openai.key',
100+
'modai.api.gemini.key' => 'modai.api.google.key',
101+
'modai.api.claude.key' => 'modai.api.anthropic.key',
102+
];
103+
104+
/**
105+
* @param \MODX\Revolution\modX $modx
106+
* @return void
107+
*/
108+
public function __invoke(&$modx)
109+
{
110+
$this->modx =& $modx;
111+
112+
foreach ($this->systemSettingsMap as $oldKey => $newKey) {
113+
$oldSetting = $this->modx->getObject(\MODX\Revolution\modSystemSetting::class, ['key' => $oldKey, 'namespace' => 'modai']);
114+
if (!$oldSetting) {
115+
continue;
116+
}
117+
118+
$newSetting = $this->modx->getObject(\MODX\Revolution\modSystemSetting::class, ['key' => $newKey, 'namespace' => 'modai']);
119+
if (!$newSetting) {
120+
continue;
121+
}
122+
123+
$newSetting->set('value', $oldSetting->get('value'));
124+
$newSetting->save();
125+
$oldSetting->remove();
126+
}
127+
128+
/** @var \MODX\Revolution\modSystemSetting[] $modelSystemSettings */
129+
$modelSystemSettings = $this->modx->getIterator(\MODX\Revolution\modSystemSetting::class, [
130+
'namespace' => 'modai',
131+
'key:LIKE' => '%.model'
132+
]);
133+
134+
foreach ($modelSystemSettings as $modelSystemSetting) {
135+
$modelSystemSetting->set('value', $this->fixModelName($modelSystemSetting->get('value')));
136+
$modelSystemSetting->save();
137+
}
138+
}
139+
140+
private function fixModelName(string $model): string
141+
{
142+
if (
143+
strncmp($model, 'openai/', strlen('openai/')) === 0 ||
144+
strncmp($model, 'google/', strlen('google/')) === 0 ||
145+
strncmp($model, 'anthropic/', strlen('anthropic/')) === 0 ||
146+
strncmp($model, 'custom/', strlen('custom/')) === 0
147+
) {
148+
return $model;
149+
}
150+
151+
if (strncmp($model, 'gemini-', strlen('gemini-')) === 0) {
152+
return "google/$model";
153+
}
154+
155+
if (strncmp($model, 'imagen-', strlen('imagen-')) === 0) {
156+
return "google/$model";
157+
}
158+
159+
if (strncmp($model, 'claude-', strlen('claude-')) === 0) {
160+
return "anthropic/$model";
161+
}
162+
163+
if (strncmp($model, 'custom_', strlen('custom_')) === 0) {
164+
return 'custom/' . substr($model, 7);
165+
}
166+
167+
switch ($model) {
168+
case 'text-embedding-004':
169+
case 'learnlm-1.5-pro-experimental':
170+
return "google/$model";
171+
default:
172+
return "openai/$model";
173+
}
174+
}
86175
};
87176
})(),
88177
];

_build/gpm_scripts/gpm.script.reload_system_settings.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
* @var array $options
1212
*/
1313

14-
use Fred\Model\FredThemedTemplate;
15-
use Fred\Model\FredBlueprint;
16-
use Fred\Model\FredTheme;
1714
use xPDO\Transport\xPDOTransport;
1815
use MODX\Revolution\modSystemSetting;
1916
use xPDO\xPDO;
@@ -50,4 +47,4 @@
5047
}
5148

5249
$modx->config = array_merge($modx->config, $config);
53-
$modx->_systemConfig = $modx->config;
50+
$modx->_systemConfig = $modx->config;
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
/**
3+
*
4+
* THIS SCRIPT IS AUTOMATICALLY GENERATED, NO CHANGES WILL APPLY
5+
*
6+
* @package modai
7+
* @subpackage build
8+
*
9+
* @var \xPDO\Transport\xPDOTransport $transport
10+
* @var array $object
11+
* @var array $options
12+
*/
13+
14+
15+
return (function () {
16+
return new class() {
17+
/**
18+
* @var \MODX\Revolution\modX
19+
*/
20+
private $modx;
21+
22+
/**
23+
* @var int
24+
*/
25+
private $action;
26+
27+
/**
28+
* @param \MODX\Revolution\modX $modx
29+
* @param int $action
30+
* @return bool
31+
*/
32+
public function __invoke(&$modx, $action)
33+
{
34+
$this->modx =& $modx;
35+
$this->action = $action;
36+
37+
if ($this->action === \xPDO\Transport\xPDOTransport::ACTION_UNINSTALL) {
38+
return true;
39+
}
40+
41+
$tools = [
42+
[
43+
'name' => \modAI\Tools\GetWeather::getSuggestedName(),
44+
'class' => \modAI\Tools\GetWeather::class,
45+
'config' => [],
46+
'enabled' => true,
47+
'default' => false,
48+
]
49+
];
50+
51+
foreach ($tools as $tool) {
52+
$exists = $this->modx->getCount(\modAI\Model\Tool::class, ['name' => $tool['name']]);
53+
if ($exists > 0) {
54+
continue;
55+
}
56+
57+
$toolObjects = $this->modx->newObject(\modAI\Model\Tool::class, $tool);
58+
$toolObjects->save();
59+
}
60+
61+
$contextProviders = [
62+
[
63+
'name' => 'resources',
64+
'description' => 'Pinecone instance for storing resources',
65+
'class' => \modAI\ContextProviders\Pinecone::class,
66+
'config' => [
67+
'endpoint' => 'ss:pinecone_url',
68+
'api_key' => 'ss:pinecone_key',
69+
'namespace' => 'resources',
70+
'fields' => 'pagetitle,introtext,content',
71+
'intro_msg' => 'Potential relevant context from resource {id}:',
72+
],
73+
'enabled' => true,
74+
]
75+
];
76+
77+
foreach ($contextProviders as $contextProvider) {
78+
$exists = $this->modx->getCount(\modAI\Model\ContextProvider::class, ['name' => $contextProvider['name']]);
79+
if ($exists > 0) {
80+
continue;
81+
}
82+
83+
$contextProviderObjects = $this->modx->newObject(\modAI\Model\ContextProvider::class, $contextProvider);
84+
$contextProviderObjects->save();
85+
}
86+
87+
$agents = [
88+
[
89+
'name' => 'RedneckWeatherMan',
90+
'description' => 'Presents weather in a funny form',
91+
'prompt' => 'You are a weather man! Report on weather in a funny redneck way.',
92+
'enabled' => true,
93+
'model' => '',
94+
'tools' => [
95+
\modAI\Tools\GetWeather::getSuggestedName(),
96+
]
97+
],
98+
[
99+
'name' => 'ContentWriter',
100+
'description' => 'Writes a decent content',
101+
'prompt' => '',
102+
'enabled' => true,
103+
'model' => '',
104+
'tools' => [
105+
\modAI\Tools\GetWeather::getSuggestedName(),
106+
],
107+
'contextProviders' => [
108+
'resources'
109+
]
110+
]
111+
];
112+
113+
foreach ($agents as $agent) {
114+
$exists = $this->modx->getCount(\modAI\Model\Agent::class, ['name' => $agent['name']]);
115+
if ($exists > 0) {
116+
continue;
117+
}
118+
119+
$agentObject = $this->modx->newObject(\modAI\Model\Agent::class);
120+
$agentObject->set('name', $agent['name']);
121+
$agentObject->set('description', $agent['description']);
122+
$agentObject->set('enabled', $agent['enabled']);
123+
124+
if (!empty($agent['prompt'])) {
125+
$agentObject->set('prompt', $agent['prompt']);
126+
}
127+
$agentObject->save();
128+
129+
if (!empty($agent['tools'])) {
130+
foreach ($agent['tools'] as $toolName) {
131+
$tool = $this->modx->getObject(\modAI\Model\Tool::class, ['name' => $toolName]);
132+
if (!$tool) {
133+
continue;
134+
}
135+
136+
$agentTool = $this->modx->newObject(\modAI\Model\AgentTool::class);
137+
$agentTool->set('agent_id', $agentObject->id);
138+
$agentTool->set('tool_id', $tool->id);
139+
$agentTool->save();
140+
}
141+
}
142+
143+
if (!empty($agent['contextProviders'])) {
144+
foreach ($agent['contextProviders'] as $contextProviderName) {
145+
$contextProvider = $this->modx->getObject(\modAI\Model\ContextProvider::class, ['name' => $contextProviderName]);
146+
if (!$contextProvider) {
147+
continue;
148+
}
149+
150+
$agentContextProvider = $this->modx->newObject(\modAI\Model\AgentContextProvider::class);
151+
$agentContextProvider->set('agent_id', $agentObject->id);
152+
$agentContextProvider->set('context_provider_id', $contextProvider->id);
153+
$agentContextProvider->save();
154+
}
155+
}
156+
}
157+
158+
return true;
159+
}
160+
};
161+
})()($transport->xpdo, $options[xPDOTransport::PACKAGE_ACTION]);

0 commit comments

Comments
 (0)