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\aiactions\responses;
|
|
|
18 |
|
|
|
19 |
use core\exception\coding_exception;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Action response base class.
|
|
|
23 |
* Any method that processes an action must return an instance of this class.
|
|
|
24 |
*
|
|
|
25 |
* @package core_ai
|
|
|
26 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
abstract class response_base {
|
|
|
30 |
/** @var int The timestamp of when the response was created. */
|
|
|
31 |
private int $timecreated;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Constructor.
|
|
|
35 |
*
|
|
|
36 |
* @param bool $success The success status of the action.
|
|
|
37 |
* @param string $actionname The name of the action that was processed.
|
|
|
38 |
* @param int $errorcode Error code. Must exist if success is false.
|
|
|
39 |
* @param string $errormessage Error message. Must exist if success is false
|
|
|
40 |
* @param string $model The model used to generate the response.
|
|
|
41 |
*/
|
|
|
42 |
public function __construct(
|
|
|
43 |
/** @var bool The success status of the action. */
|
|
|
44 |
private bool $success,
|
|
|
45 |
/** @var string The name of the action that was processed. */
|
|
|
46 |
private string $actionname,
|
|
|
47 |
/** @var int Error code. Must exist if status is error. */
|
|
|
48 |
private int $errorcode = 0,
|
|
|
49 |
/** @var string Error message. Must exist if status is error */
|
|
|
50 |
private string $errormessage = '',
|
|
|
51 |
/** @var string The model used to generate the response (if available). */
|
|
|
52 |
protected ?string $model = null,
|
|
|
53 |
|
|
|
54 |
) {
|
|
|
55 |
$this->timecreated = \core\di::get(\core\clock::class)->time();
|
|
|
56 |
if (!$success && ($errorcode == 0 || empty($errormessage))) {
|
|
|
57 |
throw new coding_exception('Error code and message must exist in an error response.');
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
/**
|
|
|
62 |
* Set the response data returned by the AI provider.
|
|
|
63 |
*
|
|
|
64 |
* @param array $response The response data returned by the AI provider.
|
|
|
65 |
*/
|
|
|
66 |
abstract public function set_response_data(array $response): void;
|
|
|
67 |
|
|
|
68 |
/**
|
|
|
69 |
* Get the response data returned by the AI provider.
|
|
|
70 |
*
|
|
|
71 |
* @return array
|
|
|
72 |
*/
|
|
|
73 |
abstract public function get_response_data(): array;
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Get the success status of the action.
|
|
|
77 |
*
|
|
|
78 |
* @return bool
|
|
|
79 |
*/
|
|
|
80 |
public function get_success(): bool {
|
|
|
81 |
return $this->success;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
/**
|
|
|
85 |
* Get the timestamp of when the response was created.
|
|
|
86 |
*
|
|
|
87 |
* @return int
|
|
|
88 |
*/
|
|
|
89 |
public function get_timecreated(): int {
|
|
|
90 |
return $this->timecreated;
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/**
|
|
|
94 |
* Get the name of the action that was processed.
|
|
|
95 |
*
|
|
|
96 |
* @return string
|
|
|
97 |
*/
|
|
|
98 |
public function get_actionname(): string {
|
|
|
99 |
return $this->actionname;
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
/**
|
|
|
103 |
* Get the error code.
|
|
|
104 |
*
|
|
|
105 |
* @return int
|
|
|
106 |
*/
|
|
|
107 |
public function get_errorcode(): int {
|
|
|
108 |
return $this->errorcode;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* Get the error message.
|
|
|
113 |
*
|
|
|
114 |
* @return string
|
|
|
115 |
*/
|
|
|
116 |
public function get_errormessage(): string {
|
|
|
117 |
return $this->errormessage;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Get the model used to generate the response (if available).
|
|
|
122 |
*
|
|
|
123 |
* @return ?string
|
|
|
124 |
*/
|
|
|
125 |
public function get_model_used(): ?string {
|
|
|
126 |
return $this->model;
|
|
|
127 |
}
|
|
|
128 |
}
|