125892c36SAndreas Gohr<?php 225892c36SAndreas Gohr 325892c36SAndreas Gohrnamespace dokuwiki\plugin\aichat; 425892c36SAndreas Gohr 5*4dd0657eSAndreas Gohruse dokuwiki\plugin\config\core\Setting\SettingString; 625892c36SAndreas Gohr 7*4dd0657eSAndreas Gohr/** 8*4dd0657eSAndreas Gohr * ModelSetting 9*4dd0657eSAndreas Gohr * 10*4dd0657eSAndreas Gohr * A setting for selecting a model. We're using a datalist to provide a list of known models but allow free input. 11*4dd0657eSAndreas Gohr */ 12*4dd0657eSAndreas Gohrclass ModelSetting extends SettingString 132071dcedSAndreas Gohr{ 14*4dd0657eSAndreas Gohr protected $modeltype; 15*4dd0657eSAndreas Gohr 1625892c36SAndreas Gohr /** @inheritdoc */ 1725892c36SAndreas Gohr public function __construct($key, $params = null) 1825892c36SAndreas Gohr { 1925892c36SAndreas Gohr parent::__construct($key, $params); 20*4dd0657eSAndreas Gohr $this->modeltype = $params['type'] ?? 'chat'; 21*4dd0657eSAndreas Gohr } 2225892c36SAndreas Gohr 23*4dd0657eSAndreas Gohr /** @inheritdoc */ 24*4dd0657eSAndreas Gohr public function html(\admin_plugin_config $plugin, $echo = false) 25*4dd0657eSAndreas Gohr { 26*4dd0657eSAndreas Gohr [$label, $input] = parent::html($plugin, $echo); 2725892c36SAndreas Gohr 28*4dd0657eSAndreas Gohr $choices = []; 2925892c36SAndreas Gohr $jsons = glob(__DIR__ . '/Model/*/models.json'); 3025892c36SAndreas Gohr foreach ($jsons as $json) { 3125892c36SAndreas Gohr $models = json_decode(file_get_contents($json), true); 32*4dd0657eSAndreas Gohr if (!isset($models[$this->modeltype])) continue; 3325892c36SAndreas Gohr 3425892c36SAndreas Gohr $namespace = basename(dirname($json)); 35*4dd0657eSAndreas Gohr foreach (array_keys($models[$this->modeltype]) as $model) { 36*4dd0657eSAndreas Gohr $choices[] = "$namespace $model"; 3725892c36SAndreas Gohr } 3825892c36SAndreas Gohr } 39*4dd0657eSAndreas Gohr sort($choices); 40*4dd0657eSAndreas Gohr 41*4dd0657eSAndreas Gohr $key = htmlspecialchars($this->key); 42*4dd0657eSAndreas Gohr $input = substr($input, 0, -2); // remove the closing tag 43*4dd0657eSAndreas Gohr $input = $input . ' list="config___' . $key . '_datalist" />'; 44*4dd0657eSAndreas Gohr $datalist = '<datalist id="config___' . $key . '_datalist">'; 45*4dd0657eSAndreas Gohr foreach ($choices as $choice) { 46*4dd0657eSAndreas Gohr $datalist .= '<option value="' . htmlspecialchars($choice) . '">'; 4725892c36SAndreas Gohr } 48*4dd0657eSAndreas Gohr $datalist .= '</datalist>'; 49*4dd0657eSAndreas Gohr $input .= $datalist; 50*4dd0657eSAndreas Gohr 51*4dd0657eSAndreas Gohr return [$label, $input]; 52*4dd0657eSAndreas Gohr } 53*4dd0657eSAndreas Gohr 54*4dd0657eSAndreas Gohr 5525892c36SAndreas Gohr} 56