Skip to content

Commit

Permalink
MDL-83312 AI: Check provider has minimal config
Browse files Browse the repository at this point in the history
  • Loading branch information
davewoloszyn committed Oct 3, 2024
1 parent 0eacab8 commit 3b23f4d
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 7 deletions.
12 changes: 7 additions & 5 deletions ai/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ public static function get_providers_for_actions(array $actions, bool $enabledon
foreach ($actions as $action) {
$providers[$action] = [];
foreach ($plugins as $plugin) {
if ($enabledonly && (!$plugin->is_enabled() || !static::is_action_enabled($plugin->component, $action))) {
$pluginclassname = static::get_ai_plugin_classname($plugin->component);
$provider = new $pluginclassname();
// Check the plugin is enabled and the provider is configured before making the action available.
if ($enabledonly && (!$plugin->is_enabled() || !static::is_action_enabled($plugin->component, $action)) ||
$enabledonly && !$provider->is_provider_configured()) {
continue;
}
$pluginclassname = static::get_ai_plugin_classname($plugin->component);
$plugin = new $pluginclassname();
if (in_array($action, $plugin->get_action_list())) {
$providers[$action][] = $plugin;
if (in_array($action, $provider->get_action_list())) {
$providers[$action][] = $provider;
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions ai/classes/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,13 @@ public function get_action_settings(
): array {
return [];
}

/**
* Check if a provider has the minimal configuration to work.
*
* @return bool Return true if configured.
*/
public function is_provider_configured(): bool {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Feature: AI Course assist summarise
| aiplacement/courseassist:summarise_text | Prohibit | custom2 | Course | C1 |
And I log in as "admin"
And I enable "openai" "aiprovider" plugin
And the following config values are set as admin:
| apikey | 123 | aiprovider_openai |
| orgid | abc | aiprovider_openai |
And I enable "courseassist" "aiplacement" plugin

@javascript
Expand Down
3 changes: 2 additions & 1 deletion ai/placement/courseassist/tests/utils_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function test_is_course_assist_available(): void {
$this->assertFalse(utils::is_course_assist_available($context));

set_config('enabled', 1, 'aiprovider_openai');
set_config('apikey', '123', 'aiprovider_openai');
set_config('orgid', 'abc', 'aiprovider_openai');

// Plugin is not enabled.
$this->setUser($user1);
Expand All @@ -55,7 +57,6 @@ public function test_is_course_assist_available(): void {
// Plugin is enabled but user does not have capability.
assign_capability('aiplacement/courseassist:summarise_text', CAP_PROHIBIT, $teacherrole->id, $context);
$this->setUser($user2);
set_config('enabled', 1, 'aiprovider_openai');
set_config('enabled', 1, 'aiplacement_courseassist');
$this->assertFalse(utils::is_course_assist_available($context));

Expand Down
3 changes: 2 additions & 1 deletion ai/placement/editor/tests/utils_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public function test_is_html_editor_placement_action_available(
));

set_config('enabled', 1, 'aiprovider_openai');
set_config('apikey', '123', 'aiprovider_openai');
set_config('orgid', 'abc', 'aiprovider_openai');

// Plugin is not enabled.
$this->setUser($this->users[1]);
Expand All @@ -85,7 +87,6 @@ public function test_is_html_editor_placement_action_available(
// Plugin is enabled but user does not have capability.
assign_capability("aiplacement/editor:{$actionname}", CAP_PROHIBIT, $this->teacherrole->id, $this->context);
$this->setUser($this->users[2]);
set_config('enabled', 1, 'aiprovider_openai');
set_config('enabled', 1, 'aiplacement_editor');
$this->assertFalse(utils::is_html_editor_placement_action_available(
context: $this->context,
Expand Down
8 changes: 8 additions & 0 deletions ai/provider/azureai/classes/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,12 @@ public function get_action_settings(
return $settings;
}

/**
* Check this provider has the minimal configuration to work.
*
* @return bool Return true if configured.
*/
public function is_provider_configured(): bool {
return !empty($this->apikey) && !empty($this->apiendpoint);
}
}
23 changes: 23 additions & 0 deletions ai/provider/azureai/tests/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,27 @@ public function test_is_request_allowed(): void {
$this->assertFalse($result['success']);
$this->assertEquals('Global rate limit exceeded', $result['errormessage']);
}

/**
* Test is_provider_configured.
*/
public function test_is_provider_configured(): void {
$this->resetAfterTest();

// No configured values.
$provider = new \aiprovider_azureai\provider();
$this->assertFalse($provider->is_provider_configured());

// Partially configured values.
set_config('apikey', '123', 'aiprovider_azureai');
set_config('endpoint', '', 'aiprovider_azureai');
$provider = new \aiprovider_azureai\provider();
$this->assertFalse($provider->is_provider_configured());

// Properly configured values.
set_config('apikey', '123', 'aiprovider_azureai');
set_config('endpoint', 'abc', 'aiprovider_azureai');
$provider = new \aiprovider_azureai\provider();
$this->assertTrue($provider->is_provider_configured());
}
}
9 changes: 9 additions & 0 deletions ai/provider/openai/classes/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,13 @@ public function get_action_settings(

return $settings;
}

/**
* Check this provider has the minimal configuration to work.
*
* @return bool Return true if configured.
*/
public function is_provider_configured(): bool {
return !empty($this->apikey) && !empty($this->orgid);
}
}
23 changes: 23 additions & 0 deletions ai/provider/openai/tests/provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,27 @@ public function test_is_request_allowed(): void {
$this->assertFalse($result['success']);
$this->assertEquals('Global rate limit exceeded', $result['errormessage']);
}

/**
* Test is_provider_configured.
*/
public function test_is_provider_configured(): void {
$this->resetAfterTest();

// No configured values.
$provider = new \aiprovider_openai\provider();
$this->assertFalse($provider->is_provider_configured());

// Partially configured values.
set_config('apikey', '123', 'aiprovider_openai');
set_config('orgid', '', 'aiprovider_openai');
$provider = new \aiprovider_openai\provider();
$this->assertFalse($provider->is_provider_configured());

// Properly configured values.
set_config('apikey', '123', 'aiprovider_openai');
set_config('orgid', 'abc', 'aiprovider_openai');
$provider = new \aiprovider_openai\provider();
$this->assertTrue($provider->is_provider_configured());
}
}
6 changes: 6 additions & 0 deletions ai/tests/behat/admin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Feature: An administrator can manage AI subsystem settings
And I navigate to "AI > AI providers" in site administration
When I toggle the "Enable OpenAI API Provider" admin switch "on"
And I should see "OpenAI API Provider enabled."
And the following config values are set as admin:
| apikey | 123 | aiprovider_openai |
| orgid | abc | aiprovider_openai |
And I navigate to "AI > AI placements" in site administration
And I click on the "Settings" link in the table row containing "HTML Text Editor Placement"
Then I should not see "This action is unavailable."
Expand All @@ -52,6 +55,9 @@ Feature: An administrator can manage AI subsystem settings
And I navigate to "AI > AI providers" in site administration
When I toggle the "Enable OpenAI API Provider" admin switch "on"
And I should see "OpenAI API Provider enabled."
And the following config values are set as admin:
| apikey | 123 | aiprovider_openai |
| orgid | abc | aiprovider_openai |
And I click on the "Settings" link in the table row containing "OpenAI API Provider"
And I toggle the "Generate text" admin switch "off"
And I navigate to "AI > AI placements" in site administration
Expand Down
8 changes: 8 additions & 0 deletions ai/tests/manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public function test_get_supported_actions(): void {
public function test_get_providers_for_actions(): void {
$this->resetAfterTest();
set_config('enabled', 1, 'aiprovider_openai');
set_config('apikey', '123', 'aiprovider_openai');
set_config('orgid', 'abc', 'aiprovider_openai');

$manager = \core\di::get(manager::class);
$actions = [
Expand Down Expand Up @@ -141,7 +143,11 @@ public function test_process_action(): void {

// Enable the providers.
set_config('enabled', 1, 'aiprovider_openai');
set_config('apikey', '123', 'aiprovider_openai');
set_config('orgid', 'abc', 'aiprovider_openai');
set_config('enabled', 1, 'aiprovider_azureai');
set_config('apikey', '123', 'aiprovider_azureai');
set_config('endpoint', 'abc', 'aiprovider_azureai');

$managermock = $this->getMockBuilder(manager::class)
->onlyMethods(['call_action_provider'])
Expand Down Expand Up @@ -413,6 +419,8 @@ public function test_is_action_available(): void {
// Enable the plugin, actions will be enabled by default when the plugin is enabled.
$manager = \core_plugin_manager::resolve_plugininfo_class('aiprovider');
$manager::enable_plugin('openai', 1);
set_config('apikey', '123', 'aiprovider_openai');
set_config('orgid', 'abc', 'aiprovider_openai');

// Should now be available.
$result = manager::is_action_available($action);
Expand Down
3 changes: 3 additions & 0 deletions lib/editor/tiny/plugins/aiplacement/tests/behat/image.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ Feature: Generate image using AI
| aiplacement/editor:generate_text | Prohibit | user | System | |
| aiplacement/editor:generate_image | Prohibit | custom2 | Course | C1 |
And I enable "openai" "aiprovider" plugin
And the following config values are set as admin:
| apikey | 123 | aiprovider_openai |
| orgid | abc | aiprovider_openai |
And I enable "editor" "aiplacement" plugin

@javascript
Expand Down
3 changes: 3 additions & 0 deletions lib/editor/tiny/plugins/aiplacement/tests/behat/text.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ Feature: Generate text using AI
| aiplacement/editor:generate_text | Prohibit | custom2 | Course | C1 |
And I log in as "admin"
And I enable "openai" "aiprovider" plugin
And the following config values are set as admin:
| apikey | 123 | aiprovider_openai |
| orgid | abc | aiprovider_openai |
And I enable "editor" "aiplacement" plugin

@javascript
Expand Down

0 comments on commit 3b23f4d

Please sign in to comment.