28require
'../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.
"/core/lib/admin.lib.php";
30require_once DOL_DOCUMENT_ROOT.
"/ai/lib/ai.lib.php";
31require_once DOL_DOCUMENT_ROOT.
"/core/class/html.formai.class.php";
41$langs->loadLangs(array(
"admin",
"website",
"other"));
47$action =
GETPOST(
'action',
'aZ09');
48$backtopage =
GETPOST(
'backtopage',
'alpha');
50$modulepart =
GETPOST(
'modulepart',
'aZ09');
52$functioncode =
GETPOST(
'functioncode',
'alpha');
53$pre_prompt =
GETPOST(
'prePrompt');
54$post_prompt =
GETPOST(
'postPrompt');
55$blacklists =
GETPOST(
'blacklists');
57$key = (string)
GETPOST(
'key',
'alpha');
75if (!class_exists(
'FormSetup')) {
76 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formsetup.class.php';
83$formSetup->formHiddenInputs[
'action'] =
"updatefeaturemodel";
84foreach ($arrayofaifeatures as $featurekey => $feature) {
85 $newkey = $featurekey;
86 if (preg_match(
'/^text/', $featurekey)) {
87 $newkey =
'textgeneration';
89 $item = $formSetup->newItem(
'AI_API_'.strtoupper($aiservice).
'_MODEL_'.$feature[
"function"]);
90 if ($arrayofai[$aiservice][$newkey] !=
'na') {
91 $item->nameText = $langs->trans(
"AI_API_MODEL_".$feature[
"function"]).
' <span class="opacitymedium">('.$langs->trans(
"Default").
' = '.$arrayofai[$aiservice][$newkey].
')</span>';
93 $item->nameText = $langs->trans(
"AI_API_MODEL_".$feature[
"function"]).
' <span class="opacitymedium">('.$langs->trans(
"None").
')</span>';
95 $item->cssClass =
'minwidth500 input';
98$setupnotempty += count($formSetup->items);
100$dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
110$currentConfigurations = json_decode($currentConfigurationsJson,
true);
112if ($action ==
'updatefeaturemodel' && !empty($user->admin)) {
113 $formSetup->saveConfFromPost();
117if ($action ==
'update' && $cancel) {
121if ($action ==
'update' && !$cancel && !$test) {
123 if (empty($functioncode)) {
127 if (!is_array($currentConfigurations)) {
128 $currentConfigurations = [];
131 $blacklistArray = array_filter(array_map(
'trim', explode(
',', $blacklists)));
133 if (empty($functioncode) || (empty($pre_prompt) && empty($post_prompt) && empty($blacklists))) {
134 if (isset($currentConfigurations[$functioncode])) {
135 unset($currentConfigurations[$functioncode]);
138 $currentConfigurations[$functioncode] = [
139 'prePrompt' => $pre_prompt,
140 'postPrompt' => $post_prompt,
141 'blacklists' => $blacklistArray,
145 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
146 $result =
dolibarr_set_const($db,
'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson,
'chaine', 0,
'',
$conf->entity);
149 header(
"Location: ".$_SERVER[
'PHP_SELF']);
161if ($action ==
'updatePrompts' && !$test) {
162 $blacklistArray = array_filter(array_map(
'trim', explode(
',', $blacklists)));
164 $currentConfigurations[$key] = [
165 'prePrompt' => $pre_prompt,
166 'postPrompt' => $post_prompt,
167 'blacklists' => $blacklistArray,
170 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
171 $result =
dolibarr_set_const($db,
'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson,
'chaine', 0,
'',
$conf->entity);
183if ($action ==
'updatePrompts' && $test) {
188if ($action ==
'confirm_deleteproperty' &&
GETPOST(
'confirm') ==
'yes') {
189 if (isset($currentConfigurations[$key])) {
190 unset($currentConfigurations[$key]);
192 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
193 $res =
dolibarr_set_const($db,
'AI_CONFIGURATIONS_PROMPT', $newConfigurationsJson,
'chaine', 0,
'',
$conf->entity);
195 header(
"Location: ".$_SERVER[
'PHP_SELF']);
209$form =
new Form($db);
215llxHeader(
'', $langs->trans($title), $help_url,
'', 0, 0,
'',
'',
'',
'mod-ai page-admin_custom_prompt');
218$linkback =
'<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.
'/admin/modules.php?restore_lastsearch_values=1').
'">'.$langs->trans(
"BackToModuleList").
'</a>';
226$newcardbutton =
dolGetButtonTitle($langs->trans(
'NewCustomPrompt'),
'',
'fa fa-plus-circle', $_SERVER[
"PHP_SELF"].
'?action=create',
'', 1);
233print
load_fiche_titre($langs->trans(
"AIPromptForFeatures", $arrayofai[$aiservice][
'label']), $newcardbutton,
'');
236if ($action ==
'deleteproperty') {
237 $formconfirm = $form->formconfirm(
238 $_SERVER[
"PHP_SELF"].
'?key='.urlencode(
GETPOST(
'key',
'alpha')),
239 $langs->trans(
'Delete'),
240 $langs->trans(
'ConfirmDeleteSetup',
GETPOST(
'key',
'alpha')),
241 'confirm_deleteproperty',
251if ($action ==
'create') {
252 $out =
'<div class="addcustomprompt">';
254 $out .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
255 $out .=
'<input type="hidden" name="token" value="'.newToken().
'">';
256 $out .=
'<input type="hidden" name="action" value="update">';
259 $out .=
'<table class="noborder centpercent">';
261 $out .=
'<tr class="liste_titre">';
262 $out .=
'<td>'.$langs->trans(
'NewCustomPrompt').
'</td>';
268 $out .=
'<tr class="oddeven">';
269 $out .=
'<td class="col-setup-title titlefield">';
270 $out .=
'<span id="module" class="spanforparamtooltip">'.$langs->trans(
"Feature").
'</span>';
274 $out .=
'<select name="functioncode" id="functioncode" class="flat minwidth500">';
275 $out .=
'<option> </option>';
276 foreach ($arrayofaifeatures as $featurekey => $feature) {
277 $labelhtml = $langs->trans($arrayofaifeatures[$featurekey][
'label']).($arrayofaifeatures[$featurekey][
'status'] ==
'notused' ?
' <span class="opacitymedium">('.$langs->trans(
"NotYetAvailable").
')</span>' :
"");
278 $labeltext = $langs->trans($arrayofaifeatures[$featurekey][
'label']);
283 $out .=
'<script type="text/javascript">
284 jQuery(document).ready(function() {
285 jQuery("#functioncode").on("change", function() {
286 console.log("We change value of ai function");
287 var changedValue = $(this).val();
288 console.log(changedValue);
289 var arrayplaceholder = {';
290 foreach ($arrayofaifeatures as $featurekey => $feature) {
294 jQuery("#prePromptInput'.dol_escape_js($key).
'").val(arrayplaceholder[changedValue]);
303 $out .=
'<tr class="oddeven">';
304 $out .=
'<td class="col-setup-title">';
305 $out .=
'<span id="prePrompt" class="spanforparamtooltip">';
306 $out .= $form->textwithpicto($langs->trans(
"Pre-Prompt"), $langs->trans(
"Pre-PromptHelp"));
310 $out .=
'<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput'.$key.
'" name="prePrompt" rows="2"></textarea>';
313 $out .=
'<tr class="oddeven">';
314 $out .=
'<td class="col-setup-title">';
315 $out .=
'<span id="postPrompt" class="spanforparamtooltip">';
316 $out .= $form->textwithpicto($langs->trans(
"Post-Prompt"), $langs->trans(
"Post-PromptHelp"));
320 $out .=
'<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput" name="postPrompt" rows="2"></textarea>';
323 $out .=
'<tr class="oddeven">';
324 $out .=
'<td class="col-setup-title">';
325 $out .=
'<span id="blacklists" class="spanforparamtooltip">';
326 $out .= $form->textwithpicto($langs->trans(
"BlackListWords"), $langs->trans(
"BlackListWordsAIHelp").
'.<br>'.$langs->trans(
"BlackListWordsHelp"));
330 $out .=
'<input type="text" class="flat minwidth500 quatrevingtpercent" id="blacklistsInput" name="blacklists">';
336 $out .= $form->buttonsSaveCancel(
"Add",
"");
339 $out .=
'<br><br><br>';
346if ($action ==
'edit' || $action ==
'create' || $action ==
'deleteproperty') {
349 if (!empty($currentConfigurations)) {
350 foreach ($currentConfigurations as $confkey => $config) {
351 if (!empty($confkey) && !preg_match(
'/^[a-z]+$/i', $confkey)) {
355 $out .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
356 $out .=
'<input type="hidden" name="token" value="'.newToken().
'">';
357 $out .=
'<input type="hidden" name="key" value="'.$confkey.
'" />';
358 $out .=
'<input type="hidden" name="action" value="updatePrompts">';
359 $out .=
'<input type="hidden" name="page_y" value="">';
361 $out .=
'<table class="noborder centpercent">';
363 $out .=
'<tr class="liste_titre">';
364 $out .=
'<td class="titlefield">'.$arrayofaifeatures[$confkey][
'picto'].
' '.$langs->trans($arrayofaifeatures[$confkey][
'label']);
365 $out .=
'<a class="deletefielda reposition marginleftonly right" href="'.$_SERVER[
"PHP_SELF"].
'?action=deleteproperty&token='.
newToken().
'&key='.urlencode($confkey).
'">'.
img_delete().
'</a>';
372 $out .=
'<tr class="oddeven">';
373 $out .=
'<td class="col-setup-title">';
374 $out .=
'<span id="prePrompt" class="spanforparamtooltip">'.$langs->trans(
"Pre-Prompt").
'</span>';
377 $out .=
'<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput_'.$confkey.
'" name="prePrompt" rows="2">'.$config[
'prePrompt'].
'</textarea>';
381 $out .=
'<tr class="oddeven">';
382 $out .=
'<td class="col-setup-title">';
383 $out .=
'<span id="postPrompt" class="spanforparamtooltip">'.$langs->trans(
"Post-Prompt").
'</span>';
386 $out .=
'<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput_'.$confkey.
'" name="postPrompt" rows="2">'.$config[
'postPrompt'].
'</textarea>';
390 $out .=
'<tr id="fichetwothirdright-'.$confkey.
'" class="oddeven">';
391 $out .=
'<td>'.$form->textwithpicto($langs->trans(
"BlackListWords"), $langs->trans(
"BlackListWordsHelp")).
'</td>';
393 $out .=
'<input type="text" class="flat minwidth500 quatrevingtpercent" id="blacklist_'.$confkey.
'" name="blacklists" value="'.(isset($config[
'blacklists']) ? implode(
', ', (array) $config[
'blacklists']) :
'').
'">';
398 $out .=
'<td>'.$langs->trans(
"Test").
'</td>';
401 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
403 $formmail->withaiprompt =
'html';
405 $showlinktoai = $confkey;
406 $showlinktoailabel = $langs->trans(
"ToTest");
407 $htmlname = $confkey;
408 $onlyenhancements = $confkey;
409 $showlinktolayout = 0;
412 include DOL_DOCUMENT_ROOT.
'/core/tpl/formlayoutai.tpl.php';
414 $out .=
'<div id="'.$htmlname.
'"></div>';
422 $out .=
'<center><input type="submit" class="button small submitBtn reposition" name="modify" data-index="'.$confkey.
'" value="'.
dol_escape_htmltag($langs->trans(
"Save")).
'"/></center>';
436if ($action ==
'edit' || $action ==
'create' || $action ==
'deleteproperty') {
437 print
load_fiche_titre($langs->trans(
"AIModelForFeature", $arrayofai[$aiservice][
'label']),
'',
'');
438 print $formSetup->generateOutput(
true);
442if (empty($setupnotempty)) {
443 print
'<br>'.$langs->trans(
"NothingToSetup");
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.
aiAdminPrepareHead()
Prepare admin pages header.
getListOfAIServices()
Get list of available ai services.
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='')
Convert a html select field into an ajax combobox.
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
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.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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.
dolGetButtonTitle($label, $helpText='', $iconClass='fa fa-file', $url='', $id='', $status=1, $params=array())
Function dolGetButtonTitle : this kind of buttons are used in title in list.
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.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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...
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.