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 aiplacement_editor\external;
18
 
19
use aiplacement_editor\utils;
20
use core_external\external_api;
21
use core_external\external_function_parameters;
22
use core_external\external_value;
23
 
24
/**
25
 * External API to call an action for this placement.
26
 *
27
 * @package    aiplacement_editor
28
 * @copyright  2024 Matt Porritt <matt.porritt@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
class generate_image extends external_api {
32
    /**
33
     * Generate image parameters.
34
     *
35
     * @return external_function_parameters
36
     * @since  Moodle 4.5
37
     */
38
    public static function execute_parameters(): external_function_parameters {
39
        return new external_function_parameters([
40
            'contextid' => new external_value(
41
                PARAM_INT,
42
                'The context ID',
43
                VALUE_REQUIRED,
44
            ),
45
            'prompttext' => new external_value(
46
                PARAM_RAW,
47
                'The prompt text for the AI service',
48
                VALUE_REQUIRED,
49
            ),
50
            'aspectratio' => new external_value(
51
                PARAM_ALPHA,
52
                'The aspect ratio of the image',
53
                VALUE_REQUIRED,
54
            ),
55
            'quality' => new external_value(
56
                PARAM_ALPHA,
57
                'The quality of the image',
58
                VALUE_REQUIRED,
59
            ),
60
            'numimages' => new external_value(
61
                PARAM_INT,
62
                'The number of images to generate',
63
                VALUE_DEFAULT,
64
                1,
65
            ),
66
            'style' => new external_value(
67
                PARAM_ALPHA,
68
                'The style of the image',
69
                VALUE_DEFAULT,
70
                'natural',
71
            ),
72
        ]);
73
    }
74
 
75
    /**
76
     * Generate image from the AI placement.
77
     *
78
     * @param int $contextid The context ID.
79
     * @param string $prompttext The data encoded as a json array.
80
     * @param string $aspectratio The aspect ratio of the image.
81
     * @param string $quality The quality of the image.
82
     * @param string $numimages The number of images to generate.
83
     * @param string $style The style of the image.
84
     * @return array The generated content.
85
     * @since  Moodle 4.5
86
     */
87
    public static function execute(
88
        int $contextid,
89
        string $prompttext,
90
        string $aspectratio,
91
        string $quality,
92
        string $numimages,
93
        string $style = '',
94
    ): array {
95
        global $USER;
96
        // Parameter validation.
97
        [
98
            'contextid' => $contextid,
99
            'prompttext' => $prompttext,
100
            'aspectratio' => $aspectratio,
101
            'quality' => $quality,
102
            'numimages' => $numimages,
103
            'style' => $style,
104
        ] = self::validate_parameters(self::execute_parameters(), [
105
            'contextid' => $contextid,
106
            'prompttext' => $prompttext,
107
            'aspectratio' => $aspectratio,
108
            'quality' => $quality,
109
            'numimages' => $numimages,
110
            'style' => $style,
111
        ]);
112
        // Context validation and permission check.
113
        // Get the context from the passed in ID.
114
        $context = \context::instance_by_id($contextid);
115
 
116
        // Check the user has permission to use the AI service.
117
        self::validate_context($context);
118
        if (!utils::is_html_editor_placement_action_available($context, 'generate_text',
119
                \core_ai\aiactions\generate_image::class)) {
120
            throw new \moodle_exception('noeditor', 'aiplacement_editor');
121
        }
122
 
123
        // Prepare the action.
124
        $action = new \core_ai\aiactions\generate_image(
125
            contextid: $contextid,
126
            userid: $USER->id,
127
            prompttext: $prompttext,
128
            quality: $quality,
129
            aspectratio: $aspectratio,
130
            numimages: $numimages,
131
            style: $style,
132
        );
133
 
134
        // Send the action to the AI manager.
135
        $manager = \core\di::get(\core_ai\manager::class);
136
        $response = $manager->process_action($action);
137
 
138
        // If we have a successful response, generate the URL for the draft file.
139
        if ($response->get_success()) {
140
            $draftfile = $response->get_response_data()['draftfile'];
141
            $drafturl = \moodle_url::make_draftfile_url(
142
                $draftfile->get_itemid(),
143
                $draftfile->get_filepath(),
144
                $draftfile->get_filename(),
145
                false,
146
            )->out(false);
147
 
148
        } else {
149
            $drafturl = '';
150
        }
151
 
152
        // Return the response.
153
        return [
154
            'success' => $response->get_success(),
155
            'revisedprompt' => $response->get_response_data()['revisedprompt'] ?? '',
156
            'drafturl' => $drafturl,
157
            'errorcode' => $response->get_errorcode(),
158
            'error' => $response->get_errormessage(),
159
        ];
160
    }
161
 
162
    /**
163
     * Generate content return value.
164
     *
165
     * @return external_function_parameters
166
     * @since  Moodle 4.5
167
     */
168
    public static function execute_returns(): external_function_parameters {
169
        return new external_function_parameters([
170
            'success' => new external_value(
171
                PARAM_BOOL,
172
                'Was the request successful',
173
                VALUE_REQUIRED,
174
            ),
175
            'revisedprompt' => new external_value(
176
                PARAM_TEXT,
177
                'Revised prompt generated by the AI',
178
                VALUE_DEFAULT,
179
                '',
180
            ),
181
            'drafturl' => new external_value(
182
                PARAM_URL,
183
                'Draft file URL for the image',
184
                VALUE_DEFAULT,
185
                '',
186
            ),
187
            'errorcode' => new external_value(
188
                PARAM_INT,
189
                'Error code if any',
190
                VALUE_DEFAULT,
191
                0,
192
            ),
193
            'error' => new external_value(
194
                PARAM_TEXT,
195
                'Error message if any',
196
                VALUE_DEFAULT,
197
                '',
198
            ),
199
        ]);
200
    }
201
}