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