Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace aiprovider_ollama\form;
18
 
19
use aiprovider_ollama\helper;
20
use core_ai\form\action_settings_form;
21
 
22
/**
23
 * Base action settings form for Ollama provider.
24
 *
25
 * @package    aiprovider_ollama
26
 * @copyright  2025 Huong Nguyen <huongnv13@gmail.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class action_form extends action_settings_form {
30
    /**
31
     * @var array Action configuration.
32
     */
33
    protected array $actionconfig;
34
    /**
35
     * @var string|null Return URL.
36
     */
37
    protected ?string $returnurl;
38
    /**
39
     * @var string Action name.
40
     */
41
    protected string $actionname;
42
    /**
43
     * @var string Action class.
44
     */
45
    protected string $action;
46
    /**
47
     * @var int Provider ID.
48
     */
49
    protected int $providerid;
50
    /**
51
     * @var string Provider name.
52
     */
53
    protected string $providername;
54
 
55
    #[\Override]
56
    protected function definition(): void {
57
        $mform = $this->_form;
58
        $this->actionconfig = $this->_customdata['actionconfig']['settings'] ?? [];
59
        $this->returnurl = $this->_customdata['returnurl'] ?? null;
60
        $this->actionname = $this->_customdata['actionname'];
61
        $this->action = $this->_customdata['action'];
62
        $this->providerid = $this->_customdata['providerid'] ?? 0;
63
        $this->providername = $this->_customdata['providername'] ?? 'aiprovider_ollama';
64
 
65
        $mform->addElement('header', 'generalsettingsheader', get_string('general', 'core'));
66
    }
67
 
68
    #[\Override]
69
    public function set_data($data): void {
70
        if (!empty($data['modelextraparams'])) {
71
            $data['modelextraparams'] = json_encode(json_decode($data['modelextraparams']), JSON_PRETTY_PRINT);
72
        }
73
        parent::set_data($data);
74
    }
75
 
76
    #[\Override]
77
    public function get_data(): ?\stdClass {
78
        $data = parent::get_data();
79
 
80
        if ($data) {
81
            if (isset($data->modeltemplate)) {
82
                if ($data->modeltemplate === 'custom') {
83
                    $data->model = $data->custommodel;
84
                } else {
85
                    // Set the model to the selected model template.
86
                    $data->model = $data->modeltemplate;
87
                }
88
 
89
            }
90
            // Unset the model template.
91
            unset($data->custommodel);
92
            unset($data->modeltemplate);
93
 
94
            // Unset any false-y values.
95
            $data = (object) array_filter((array) $data);
96
        }
97
 
98
        return $data;
99
    }
100
 
101
    #[\Override]
102
    public function validation($data, $files): array {
103
        $errors = parent::validation($data, $files);
104
 
105
        // Validate the extra parameters.
106
        if (!empty($data['modelextraparams'])) {
107
            json_decode($data['modelextraparams']);
108
            if (json_last_error() !== JSON_ERROR_NONE) {
109
                $errors['modelextraparams'] = get_string('invalidjson', 'aiprovider_ollama');
110
            }
111
        }
112
 
113
        // Validate the model.
114
        if ($data['modeltemplate'] === 'custom' && empty($data['custommodel'])) {
115
            $errors['custommodel'] = get_string('required');
116
        }
117
 
118
        return $errors;
119
    }
120
 
121
    #[\Override]
122
    public function get_defaults(): array {
123
        $data = parent::get_defaults();
124
 
125
        unset(
126
            $data['modeltemplate'],
127
            $data['custommodel'],
128
            $data['modelextraparams'],
129
        );
130
 
131
        return $data;
132
    }
133
 
134
    /**
135
     * Add model fields to the form.
136
     *
137
     * @param int $modeltype Model type.
138
     */
139
    protected function add_model_fields(int $modeltype): void {
140
        global $PAGE;
141
        $PAGE->requires->js_call_amd('aiprovider_ollama/modelchooser', 'init');
142
        $mform = $this->_form;
143
 
144
        // Action model to use.
145
        $mform->addElement(
146
            'select',
147
            'modeltemplate',
148
            get_string("action:{$this->actionname}:model", 'aiprovider_ollama'),
149
            $this->get_model_list($modeltype),
150
            ['data-modelchooser-field' => 'selector'],
151
        );
152
        $mform->setType('modeltemplate', PARAM_TEXT);
153
        $mform->addRule('modeltemplate', null, 'required', null, 'client');
154
        if (!empty($this->actionconfig['model']) &&
155
                (!array_key_exists($this->actionconfig['model'], $this->get_model_list($modeltype)) ||
156
                !empty($this->actionconfig['modelextraparams']))) {
157
            $defaultmodel = 'custom';
158
        } else {
159
            $defaultmodel = $this->actionconfig['model'] ?? 'llama3.3';
160
        }
161
        $mform->setDefault('modeltemplate', $defaultmodel);
162
        $mform->addHelpButton('modeltemplate', "action:{$this->actionname}:model", 'aiprovider_ollama');
163
 
164
        $mform->addElement('hidden', 'model', $this->actionconfig['model'] ?? 'llama3.3');
165
        $mform->setType('model', PARAM_TEXT);
166
 
167
        $mform->addElement('text', 'custommodel', get_string('custom_model_name', 'aiprovider_ollama'));
168
        $mform->setType('custommodel', PARAM_TEXT);
169
        $mform->setDefault('custommodel', $this->actionconfig['model'] ?? '');
170
        $mform->hideIf('custommodel', 'modeltemplate', 'neq', 'custom');
171
 
172
        $mform->registerNoSubmitButton('updateactionsettings');
173
        $mform->addElement(
174
            'submit',
175
            'updateactionsettings',
176
            'updateactionsettings',
177
            ['data-modelchooser-field' => 'updateButton', 'class' => 'd-none']
178
        );
179
    }
180
 
181
    /**
182
     * Get the list of models.
183
     *
184
     * @param int $modeltype Model type.
185
     * @return array List of models.
186
     */
187
    protected function get_model_list(int $modeltype): array {
188
        $models = [];
189
        $models['custom'] = get_string('custom', 'core_form');
190
        foreach (helper::get_model_classes() as $class) {
191
            $model = new $class();
192
            if ($model->model_type() == $modeltype) {
193
                $models[$model->get_model_name()] = $model->get_model_display_name();
194
            }
195
        }
196
        return $models;
197
    }
198
}