dolibarr 24.0.0-beta
custom_prompt.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2022 Alice Adminson <aadminson@example.com>
4 * Copyright (C) 2024-2026 Frédéric France <frederic.france@free.fr>
5 * Coryright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../../main.inc.php';
36require_once DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php";
37require_once DOL_DOCUMENT_ROOT."/ai/lib/ai.lib.php";
38require_once DOL_DOCUMENT_ROOT."/core/class/html.formai.class.php";
39
40$langs->loadLangs(array("admin", "website", "other"));
41
42$arrayofaifeatures = getListOfAIFeatures();
43$arrayofai = getListOfAIServices();
44
45// Parameters
46$action = GETPOST('action', 'aZ09');
47$backtopage = GETPOST('backtopage', 'alpha');
48$cancel = GETPOST('cancel', 'alpha');
49$modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
50
51$functioncode = GETPOST('functioncode', 'alpha');
52$pre_prompt = GETPOST('prePrompt');
53$post_prompt = GETPOST('postPrompt');
54$blacklists = GETPOST('blacklists');
55$test = GETPOST('test');
56$key = (string) GETPOST('key', 'alpha');
57
58if (empty($action)) {
59 $action = 'edit';
60}
61
62$error = 0;
63$setupnotempty = 0;
64
65// Access control
66if (!$user->admin) {
68}
69if (!isModEnabled('ai')) {
70 accessforbidden('Module AI not activated.');
71}
72
73// Set this to 1 to use the factory to manage constants. Warning, the generated module will be compatible with version v15+ only
74$useFormSetup = 1;
75
76if (!class_exists('FormSetup')) {
77 require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsetup.class.php';
78}
79
80$form = new Form($db);
81$formSetup = new FormSetup($db);
82$aiservice = getDolGlobalString('AI_API_SERVICE', 'chatgpt');
83
84// Setup conf for AI model
85$formSetup->formHiddenInputs['action'] = "updatefeaturemodel";
86foreach ($arrayofaifeatures as $featurekey => $feature) {
87 $newfeaturekey = $featurekey;
88 if (preg_match('/^text/', $featurekey)) {
89 $newfeaturekey = 'textgeneration';
90 }
91 $item = $formSetup->newItem('AI_API_'.strtoupper($aiservice).'_MODEL_'.$feature["function"]); // Name of constant must end with _KEY so it is encrypted when saved into database.
92 if (!empty($arrayofai[$aiservice][$newfeaturekey]['default']) && $arrayofai[$aiservice][$newfeaturekey]['default'] != 'na') {
93 $item->nameText = '<span class="valignmiddle">'.$langs->trans("AI_API_MODEL_".$feature["function"]).' </span><span class="opacitymedium valignmiddle">('.$langs->trans("Default").' = '.$arrayofai[$aiservice][$newfeaturekey]['default'].')</span>';
94 if (!empty($arrayofai[$aiservice][$newfeaturekey]['examples'])) {
95 $htmltooltip = $langs->trans("Example").': '.$arrayofai[$aiservice][$newfeaturekey]['examples'];
96 $item->nameText .= $form->textwithpicto('', $htmltooltip);
97 }
98 } else {
99 $item->nameText = $langs->trans("AI_API_MODEL_".$feature["function"]).' <span class="opacitymedium">('.$langs->trans("None").')</span>';
100 }
101 $item->cssClass = 'minwidth500 input';
102}
103
104$setupnotempty += count($formSetup->items);
105
106$dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
107
108
109/*
110 * Actions
111 */
112
113// get all configs in const AI
114
115$currentConfigurationsJson = getDolGlobalString('AI_CONFIGURATIONS_PROMPT');
116$currentConfigurations = json_decode($currentConfigurationsJson, true);
117
118if ($action == 'updatefeaturemodel' && !empty($user->admin)) {
119 $formSetup->saveConfFromPost();
120 $action = 'edit';
121}
122
123if ($action == 'update' && $cancel) {
124 $action = 'edit';
125}
126
127if ($action == 'update' && !$cancel && !$test) {
128 $error = 0;
129 if (empty($functioncode)) {
130 $error++;
131 setEventMessages($langs->trans('ErrorInputRequired'), null, 'errors');
132 }
133 if (!is_array($currentConfigurations)) {
134 $currentConfigurations = [];
135 }
136
137 $blacklistArray = array_filter(array_map('trim', explode(',', $blacklists)));
138
139 if (empty($functioncode) || (empty($pre_prompt) && empty($post_prompt) && empty($blacklists))) {
140 if (isset($currentConfigurations[$functioncode])) {
141 unset($currentConfigurations[$functioncode]);
142 }
143 } else {
144 $currentConfigurations[$functioncode] = [
145 'prePrompt' => $pre_prompt,
146 'postPrompt' => $post_prompt,
147 'blacklists' => $blacklistArray,
148 ];
149 }
150
151 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
152 $result = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity);
153 if (!$error) {
154 if ($result) {
155 header("Location: ".$_SERVER['PHP_SELF']);
156 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
157 exit;
158 } else {
159 setEventMessages($langs->trans("ErrorUpdating"), null, 'errors');
160 }
161 }
162
163 $action = 'edit';
164}
165
166// Update entry
167if ($action == 'updatePrompts' && !$test) {
168 $blacklistArray = array_filter(array_map('trim', explode(',', $blacklists)));
169
170 $currentConfigurations[$key] = [
171 'prePrompt' => $pre_prompt,
172 'postPrompt' => $post_prompt,
173 'blacklists' => $blacklistArray,
174 ];
175
176 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
177 $result = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity);
178 if (!$error) {
179 $action = 'edit';
180 if ($result) {
181 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
182 } else {
183 setEventMessages($langs->trans("ErrorUpdating"), null, 'errors');
184 }
185 }
186}
187
188// Test entry
189if ($action == 'updatePrompts' && $test) {
190 $action = 'edit';
191}
192
193// Delete entry
194if ($action == 'confirm_deleteproperty' && GETPOST('confirm') == 'yes') {
195 if (isset($currentConfigurations[$key])) {
196 unset($currentConfigurations[$key]);
197
198 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
199 $res = dolibarr_set_const($db, 'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson, 'chaine', 0, '', $conf->entity);
200 if ($res) {
201 header("Location: ".$_SERVER['PHP_SELF']);
202 setEventMessages($langs->trans("RecordDeleted"), null, 'mesgs');
203 exit;
204 } else {
205 setEventMessages($langs->trans("NoRecordDeleted"), null, 'errors');
206 }
207 }
208}
209
210
211/*
212 * View
213 */
214
215$formai = new FormAI($db);
216
217$help_url = '';
218$title = "AiSetup";
219
220llxHeader('', $langs->trans($title), $help_url, '', 0, 0, '', '', '', 'mod-ai page-admin_custom_prompt');
221
222$linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
223
224print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');
225
226// Configuration header
227$head = aiAdminPrepareHead();
228print dol_get_fiche_head($head, 'custom', $langs->trans($title), -1, "ai");
229
230$newcardbutton = dolGetButtonTitle($langs->trans('NewCustomPrompt'), '', 'fa fa-plus-circle', $_SERVER["PHP_SELF"].'?action=create', '', 1);
231/*
232$newbutton = '<a href="'.$_SERVER["PHP_SELF"].'?action=create" title="'.$langs->trans("NewCustomPrompt").'">';
233$newbutton .= img_picto('', 'add');
234$newbutton .= '</a>';
235*/
236
237print load_fiche_titre($langs->trans("AIPromptForFeatures", $arrayofai[$aiservice]['label']), $newcardbutton, '');
238
239
240if ($action == 'deleteproperty') {
241 $formconfirm = $form->formconfirm(
242 $_SERVER["PHP_SELF"].'?key='.urlencode(GETPOST('key', 'alpha')),
243 $langs->trans('Delete'),
244 $langs->trans('ConfirmDeleteSetup', GETPOST('key', 'alpha')),
245 'confirm_deleteproperty',
246 '',
247 0,
248 1
249 );
250 print $formconfirm;
251}
252
253if ($action == 'create') {
254 $out = '<div class="addcustomprompt">';
255
256 $out .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
257 $out .= '<input type="hidden" name="token" value="'.newToken().'">';
258 $out .= '<input type="hidden" name="action" value="update">';
259
260
261 $out .= '<table class="noborder centpercent">';
262 $out .= '<thead>';
263 $out .= '<tr class="liste_titre">';
264 $out .= '<td>'.$langs->trans('NewCustomPrompt').'</td>';
265 $out .= '<td></td>';
266 $out .= '</tr>';
267 $out .= '</thead>';
268
269 $out .= '<tbody>';
270 $out .= '<tr class="oddeven">';
271 $out .= '<td class="col-setup-title titlefield">';
272 $out .= '<span id="module" class="spanforparamtooltip">'.$langs->trans("Feature").'</span>';
273 $out .= '</td>';
274 $out .= '<td>';
275 // Combo list of AI features
276 $out .= '<select name="functioncode" id="functioncode" class="flat minwidth500">';
277 $out .= '<option>&nbsp;</option>';
278 foreach ($arrayofaifeatures as $featurekey => $feature) {
279 $labelhtml = $langs->trans($arrayofaifeatures[$featurekey]['label']).($arrayofaifeatures[$featurekey]['status'] == 'notused' ? ' <span class="opacitymedium">('.$langs->trans("NotYetAvailable").')</span>' : "");
280 $labeltext = $langs->trans($arrayofaifeatures[$featurekey]['label']);
281 $out .= '<option value="'.dol_escape_js($featurekey).'" data-html="'.dol_escape_htmltag($labelhtml).'">'.dol_escape_htmltag($labeltext).'</option>';
282 }
283 $out .= '</select>';
284 $out .= ajax_combobox("functioncode");
285 $out .= '<script type="text/javascript">
286 jQuery(document).ready(function() {
287 jQuery("#functioncode").on("change", function() {
288 console.log("We change value of ai function");
289 var changedValue = $(this).val();
290 console.log(changedValue);
291 var arrayplaceholder = {';
292 foreach ($arrayofaifeatures as $featurekey => $feature) {
293 $out .= dol_escape_js($featurekey).': \''.dol_escape_js(empty($feature['placeholder']) ? '' : $feature['placeholder']).'\',';
294 }
295 $out .= '}
296 jQuery("#prePromptInput'.dol_escape_js($key).'").val(arrayplaceholder[changedValue]);
297 });
298 });
299 </script>
300 ';
301
302 $out .= '</td>';
303 $out .= '</tr>';
304
305 $out .= '<tr class="oddeven">';
306 $out .= '<td class="col-setup-title">';
307 $out .= '<span id="prePrompt" class="spanforparamtooltip">';
308 $out .= $form->textwithpicto($langs->trans("Pre-Prompt"), $langs->trans("Pre-PromptHelp"));
309 $out .= '</span>';
310 $out .= '</td>';
311 $out .= '<td>';
312 $out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput'.$key.'" name="prePrompt" rows="2"></textarea>';
313 $out .= '</td>';
314 $out .= '</tr>';
315 $out .= '<tr class="oddeven">';
316 $out .= '<td class="col-setup-title">';
317 $out .= '<span id="postPrompt" class="spanforparamtooltip">';
318 $out .= $form->textwithpicto($langs->trans("Post-Prompt"), $langs->trans("Post-PromptHelp"));
319 $out .= '</span>';
320 $out .= '</td>';
321 $out .= '<td>';
322 $out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput" name="postPrompt" rows="2"></textarea>';
323 $out .= '</td>';
324 $out .= '</tr>';
325 $out .= '<tr class="oddeven">';
326 $out .= '<td class="col-setup-title">';
327 $out .= '<span id="blacklists" class="spanforparamtooltip">';
328 $out .= $form->textwithpicto($langs->trans("BlackListWords"), $langs->trans("BlackListWordsAIHelp").'.<br>'.$langs->trans("BlackListWordsHelp"));
329 $out .= '</span>';
330 $out .= '</td>';
331 $out .= '<td>';
332 $out .= '<input type="text" class="flat minwidth500 quatrevingtpercent" id="blacklistsInput" name="blacklists">';
333 $out .= '</td>';
334 $out .= '</tr>';
335 $out .= '</tbody>';
336 $out .= '</table>';
337
338 $out .= $form->buttonsSaveCancel("Add", "");
339 $out .= '</form>';
340
341 $out .= "<br>";
342 $out .= "<br>";
343
344 $out .= '</div>';
345
346 print $out;
347}
348
349if ($action == 'edit' || $action == 'create' || $action == 'deleteproperty') {
350 $out = '';
351
352 if (empty($currentConfigurations)) {
353 if ($action != 'create') {
354 print '<!-- no custom prompt-->'."\n";
355 print '<span class="opacitymedium">'.$langs->trans("None").'</span>';
356 print '<br>';
357 print '<br>';
358 }
359 print '<br>';
360 } else {
361 print '<br>';
362
363 foreach ($currentConfigurations as $confkey => $config) {
364 if (!empty($confkey) && !preg_match('/^[a-z]+$/i', $confkey)) { // Ignore empty saved setup
365 continue;
366 }
367
368 $out .= '<form action="'.$_SERVER["PHP_SELF"].'" method="POST">';
369 $out .= '<input type="hidden" name="token" value="'.newToken().'">';
370 $out .= '<input type="hidden" name="key" value="'.$confkey.'" />';
371 $out .= '<input type="hidden" name="action" value="updatePrompts">';
372 $out .= '<input type="hidden" name="page_y" value="">';
373
374 $out .= '<table class="noborder centpercent">';
375 $out .= '<thead>';
376 $out .= '<tr class="liste_titre">';
377 $out .= '<td class="titlefield">'.$arrayofaifeatures[$confkey]['picto'].' '.$langs->trans($arrayofaifeatures[$confkey]['label']);
378 $out .= '<a class="deletefielda reposition marginleftonly right" href="'.$_SERVER["PHP_SELF"].'?action=deleteproperty&token='.newToken().'&key='.urlencode($confkey).'">'.img_delete().'</a>';
379 $out .= '</td>';
380 $out .= '<td></td>';
381 $out .= '</tr>';
382 $out .= '</thead>';
383 $out .= '<tbody>';
384
385 $out .= '<tr class="oddeven">';
386 $out .= '<td class="col-setup-title">';
387 $out .= '<span id="prePrompt" class="spanforparamtooltip">'.$langs->trans("Pre-Prompt").'</span>';
388 $out .= '</td>';
389 $out .= '<td>';
390 $out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput_'.$confkey.'" name="prePrompt" rows="2">'.$config['prePrompt'].'</textarea>';
391 $out .= '</td>';
392 $out .= '</tr>';
393
394 $out .= '<tr class="oddeven">';
395 $out .= '<td class="col-setup-title">';
396 $out .= '<span id="postPrompt" class="spanforparamtooltip">'.$langs->trans("Post-Prompt").'</span>';
397 $out .= '</td>';
398 $out .= '<td>';
399 $out .= '<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput_'.$confkey.'" name="postPrompt" rows="2">'.$config['postPrompt'].'</textarea>';
400 $out .= '</td>';
401 $out .= '</tr>';
402
403 $out .= '<tr id="fichetwothirdright-'.$confkey.'" class="oddeven">';
404 $out .= '<td>'.$form->textwithpicto($langs->trans("BlackListWords"), $langs->trans("BlackListWordsHelp")).'</td>';
405 $out .= '<td>';
406 $out .= '<input type="text" class="flat minwidth500 quatrevingtpercent" id="blacklist_'.$confkey.'" name="blacklists" value="'.(isset($config['blacklists']) ? implode(', ', (array) $config['blacklists']) : '').'">';
407 $out .= '</td>';
408 $out .= '</tr>';
409
410 $out .= '<tr>';
411 $out .= '<td>'.$langs->trans("Test").'</td>';
412 $out .= '<td>';
413
414 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
415 $formmail = new FormMail($db);
416 $formmail->withaiprompt = 'html'; // set format
417
418 $showlinktoai = $confkey; // 'textgenerationemail', 'textgenerationwebpage', 'imagegeneration', ...
419 $showlinktoailabel = $langs->trans("ToTest");
420 $htmlname = $confkey;
421 $onlyenhancements = $confkey;
422 $showlinktolayout = 0;
423
424 // Fill $out
425 include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
426
427 $out .= '<div id="'.$htmlname.'"></div>';
428
429 $out .= '</td>';
430 $out .= '</tr>';
431
432 $out .= '</tbody>';
433 $out .= '</table>';
434
435 $out .= '<center><input type="submit" class="button small submitBtn reposition" name="modify" data-index="'.$confkey.'" value="'.dol_escape_htmltag($langs->trans("Save")).'"/></center>';
436
437 $out .= '</form>';
438
439 $out .= '<br><br>';
440 }
441 }
442
443 print $out;
444
445 print '<br>';
446}
447
448// Custom models
449if ($action == 'edit' || $action == 'create' || $action == 'deleteproperty') {
450 print load_fiche_titre($langs->trans("AIModelForFeature", $arrayofai[$aiservice]['label']), '', '');
451
452 print $formSetup->generateOutput(true);
453}
454
455
456if (empty($setupnotempty)) {
457 print '<br>'.$langs->trans("NothingToSetup");
458}
459
460
461// Page end
462print dol_get_fiche_end();
463
464llxFooter();
465$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
getListOfAIFeatures()
Prepare admin pages header.
Definition ai.lib.php:36
aiAdminPrepareHead()
Prepare admin pages header.
Definition ai.lib.php:407
getListOfAIServices()
Get list of available ai services.
Definition ai.lib.php:68
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
Definition ajax.lib.php:476
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to generate HTML forms for single email Usage: $formai = new FormAI($db) $formai->proprietes=1 ...
Class to manage generation of HTML components Only common components must be here.
Class to manage a HTML form to send a unitary email Usage: $formail = new FormMail($db) $formmail->pr...
This class help you create setup render.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
img_delete($titlealt='default', $other='class="pictodelete"', $morecss='')
Show delete logo.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into JavaScript code.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.