-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathModelSetting.php
55 lines (44 loc) · 1.58 KB
/
ModelSetting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace dokuwiki\plugin\aichat;
use dokuwiki\plugin\config\core\Setting\SettingString;
/**
* ModelSetting
*
* A setting for selecting a model. We're using a datalist to provide a list of known models but allow free input.
*/
class ModelSetting extends SettingString
{
protected $modeltype;
/** @inheritdoc */
public function __construct($key, $params = null)
{
parent::__construct($key, $params);
$this->modeltype = $params['type'] ?? 'chat';
}
/** @inheritdoc */
public function html(\admin_plugin_config $plugin, $echo = false)
{
[$label, $input] = parent::html($plugin, $echo);
$choices = [];
$jsons = glob(__DIR__ . '/Model/*/models.json');
foreach ($jsons as $json) {
$models = json_decode(file_get_contents($json), true);
if (!isset($models[$this->modeltype])) continue;
$namespace = basename(dirname($json));
foreach (array_keys($models[$this->modeltype]) as $model) {
$choices[] = "$namespace $model";
}
}
sort($choices);
$key = htmlspecialchars($this->key);
$input = substr($input, 0, -2); // remove the closing tag
$input = $input . ' list="config___' . $key . '_datalist" />';
$datalist = '<datalist id="config___' . $key . '_datalist">';
foreach ($choices as $choice) {
$datalist .= '<option value="' . htmlspecialchars($choice) . '">';
}
$datalist .= '</datalist>';
$input .= $datalist;
return [$label, $input];
}
}