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 core_ai;
18
 
19
use core_ai\aiactions\base;
20
use core_ai\aiactions\responses\response_base;
21
 
22
/**
23
 * Base class for provider processors.
24
 *
25
 * Each provider processor should extend this class.
26
 *
27
 * @package    core_ai
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
abstract class process_base {
32
    /**
33
     * Class constructor.
34
     *
35
     * @param provider $provider The provider that will process the action.
36
     * @param base $action The action to process.
37
     */
38
    public function __construct(
39
        /** @var provider The provider that will process the action. */
40
        protected readonly provider $provider,
41
        /** @var base The action to process. */
42
        protected readonly base $action,
43
    ) {
44
    }
45
 
46
    /**
47
     * Process the AI request.
48
     *
49
     * @return response_base The result of the action.
50
     */
51
    public function process(): response_base {
52
        // Check the rate limiter.
53
        $ratelimitcheck = $this->provider->is_request_allowed($this->action);
54
        if ($ratelimitcheck !== true) {
55
            return $this->get_response(
56
                success: false,
57
                errorcode: $ratelimitcheck['errorcode'],
58
                errormessage: $ratelimitcheck['errormessage'],
59
            );
60
        }
61
 
62
        // Format the action response object.
63
        return $this->prepare_response($this->query_ai_api());
64
    }
65
 
66
    /**
67
     * Query the AI service.
68
     *
69
     * @return array The response from the AI service.
70
     */
71
    abstract protected function query_ai_api(): array;
72
 
73
    /**
74
     * Prepare the response object.
75
     *
76
     * @param array $responsedata The response object.
77
     * @return response_base The action response object.
78
     */
79
    private function prepare_response(array $responsedata): response_base {
80
        if ($responsedata['success']) {
81
            $response = $this->get_response(
82
                success: true,
83
            );
84
            $response->set_response_data($responsedata);
85
 
86
            return $response;
87
        } else {
88
            return $this->get_response(
89
                success: false,
90
                errorcode: $responsedata['errorcode'],
91
                errormessage: $responsedata['errormessage'],
92
            );
93
        }
94
    }
95
 
96
    /**
97
     * Get the instantiated Response Class for the action described by this processor.
98
     *
99
     * @param mixed ...$args The arguments to pass to the response class constructor.
100
     * @return response_base
101
     */
102
    protected function get_response(...$args): response_base {
103
        $responseclassname = $this->action::get_response_classname();
104
        return new $responseclassname(
105
            ...$args,
106
        );
107
    }
108
}