29require
'../../main.inc.php';
37require_once DOL_DOCUMENT_ROOT.
"/core/lib/admin.lib.php";
38require_once DOL_DOCUMENT_ROOT.
"/ai/lib/ai.lib.php";
39require_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');
49$cancel =
GETPOST(
'cancel',
'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');
77if (!class_exists(
'FormSetup')) {
78 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formsetup.class.php';
86$formSetup->formHiddenInputs[
'action'] =
"updatefeaturemodel";
87foreach ($arrayofaifeatures as $featurekey => $feature) {
88 $newfeaturekey = $featurekey;
89 if (preg_match(
'/^text/', $featurekey)) {
90 $newfeaturekey =
'textgeneration';
92 $item = $formSetup->newItem(
'AI_API_'.strtoupper($aiservice).
'_MODEL_'.$feature[
"function"]);
93 if (!empty($arrayofai[$aiservice][$newfeaturekey][
'default']) && $arrayofai[$aiservice][$newfeaturekey][
'default'] !=
'na') {
94 $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>';
95 if (!empty($arrayofai[$aiservice][$newfeaturekey][
'examples'])) {
96 $htmltooltip = $langs->trans(
"Example").
': '.$arrayofai[$aiservice][$newfeaturekey][
'examples'];
97 $item->nameText .= $form->textwithpicto(
'', $htmltooltip);
100 $item->nameText = $langs->trans(
"AI_API_MODEL_".$feature[
"function"]).
' <span class="opacitymedium">('.$langs->trans(
"None").
')</span>';
102 $item->cssClass =
'minwidth500 input';
105$setupnotempty += count($formSetup->items);
107$dirmodels = array_merge(array(
'/'), (array)
$conf->modules_parts[
'models']);
117$currentConfigurations = json_decode($currentConfigurationsJson,
true);
119if ($action ==
'updatefeaturemodel' && !empty($user->admin)) {
120 $formSetup->saveConfFromPost();
124if ($action ==
'update' && $cancel) {
128if ($action ==
'update' && !$cancel && !$test) {
130 if (empty($functioncode)) {
134 if (!is_array($currentConfigurations)) {
135 $currentConfigurations = [];
138 $blacklistArray = array_filter(array_map(
'trim', explode(
',', $blacklists)));
140 if (empty($functioncode) || (empty($pre_prompt) && empty($post_prompt) && empty($blacklists))) {
141 if (isset($currentConfigurations[$functioncode])) {
142 unset($currentConfigurations[$functioncode]);
145 $currentConfigurations[$functioncode] = [
146 'prePrompt' => $pre_prompt,
147 'postPrompt' => $post_prompt,
148 'blacklists' => $blacklistArray,
152 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
156 header(
"Location: ".$_SERVER[
'PHP_SELF']);
168if ($action ==
'updatePrompts' && !$test) {
169 $blacklistArray = array_filter(array_map(
'trim', explode(
',', $blacklists)));
171 $currentConfigurations[$key] = [
172 'prePrompt' => $pre_prompt,
173 'postPrompt' => $post_prompt,
174 'blacklists' => $blacklistArray,
177 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
190if ($action ==
'updatePrompts' && $test) {
195if ($action ==
'confirm_deleteproperty' &&
GETPOST(
'confirm') ==
'yes') {
196 if (isset($currentConfigurations[$key])) {
197 unset($currentConfigurations[$key]);
199 $newConfigurationsJson = json_encode($currentConfigurations, JSON_UNESCAPED_UNICODE);
202 header(
"Location: ".$_SERVER[
'PHP_SELF']);
221llxHeader(
'', $langs->trans($title), $help_url,
'', 0, 0,
'',
'',
'',
'mod-ai page-admin_custom_prompt');
223$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>';
231$newcardbutton =
dolGetButtonTitle($langs->trans(
'NewCustomPrompt'),
'',
'fa fa-plus-circle', $_SERVER[
"PHP_SELF"].
'?action=create',
'', 1);
238print
load_fiche_titre($langs->trans(
"AIPromptForFeatures", $arrayofai[$aiservice][
'label']), $newcardbutton,
'');
241if ($action ==
'deleteproperty') {
242 $formconfirm = $form->formconfirm(
243 $_SERVER[
"PHP_SELF"].
'?key='.urlencode(
GETPOST(
'key',
'alpha')),
244 $langs->trans(
'Delete'),
245 $langs->trans(
'ConfirmDeleteSetup',
GETPOST(
'key',
'alpha')),
246 'confirm_deleteproperty',
254if ($action ==
'create') {
255 $out =
'<div class="addcustomprompt">';
257 $out .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
258 $out .=
'<input type="hidden" name="token" value="'.newToken().
'">';
259 $out .=
'<input type="hidden" name="action" value="update">';
262 $out .=
'<table class="noborder centpercent">';
264 $out .=
'<tr class="liste_titre">';
265 $out .=
'<td>'.$langs->trans(
'NewCustomPrompt').
'</td>';
271 $out .=
'<tr class="oddeven">';
272 $out .=
'<td class="col-setup-title titlefield">';
273 $out .=
'<span id="module" class="spanforparamtooltip">'.$langs->trans(
"Feature").
'</span>';
277 $out .=
'<select name="functioncode" id="functioncode" class="flat minwidth500">';
278 $out .=
'<option> </option>';
279 foreach ($arrayofaifeatures as $featurekey => $feature) {
280 $labelhtml = $langs->trans($arrayofaifeatures[$featurekey][
'label']).($arrayofaifeatures[$featurekey][
'status'] ==
'notused' ?
' <span class="opacitymedium">('.$langs->trans(
"NotYetAvailable").
')</span>' :
"");
281 $labeltext = $langs->trans($arrayofaifeatures[$featurekey][
'label']);
286 $out .=
'<script type="text/javascript">
287 jQuery(document).ready(function() {
288 jQuery("#functioncode").on("change", function() {
289 console.log("We change value of ai function");
290 var changedValue = $(this).val();
291 console.log(changedValue);
292 var arrayplaceholder = {';
293 foreach ($arrayofaifeatures as $featurekey => $feature) {
297 jQuery("#prePromptInput'.dol_escape_js($key).
'").val(arrayplaceholder[changedValue]);
306 $out .=
'<tr class="oddeven">';
307 $out .=
'<td class="col-setup-title">';
308 $out .=
'<span id="prePrompt" class="spanforparamtooltip">';
309 $out .= $form->textwithpicto($langs->trans(
"Pre-Prompt"), $langs->trans(
"Pre-PromptHelp"));
313 $out .=
'<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput'.$key.
'" name="prePrompt" rows="2"></textarea>';
316 $out .=
'<tr class="oddeven">';
317 $out .=
'<td class="col-setup-title">';
318 $out .=
'<span id="postPrompt" class="spanforparamtooltip">';
319 $out .= $form->textwithpicto($langs->trans(
"Post-Prompt"), $langs->trans(
"Post-PromptHelp"));
323 $out .=
'<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput" name="postPrompt" rows="2"></textarea>';
326 $out .=
'<tr class="oddeven">';
327 $out .=
'<td class="col-setup-title">';
328 $out .=
'<span id="blacklists" class="spanforparamtooltip">';
329 $out .= $form->textwithpicto($langs->trans(
"BlackListWords"), $langs->trans(
"BlackListWordsAIHelp").
'.<br>'.$langs->trans(
"BlackListWordsHelp"));
333 $out .=
'<input type="text" class="flat minwidth500 quatrevingtpercent" id="blacklistsInput" name="blacklists">';
339 $out .= $form->buttonsSaveCancel(
"Add",
"");
350if ($action ==
'edit' || $action ==
'create' || $action ==
'deleteproperty') {
353 if (empty($currentConfigurations)) {
354 if ($action !=
'create') {
355 print
'<!-- no custom prompt-->'.
"\n";
356 print
'<span class="opacitymedium">'.$langs->trans(
"None").
'</span>';
364 foreach ($currentConfigurations as $confkey => $config) {
365 if (!empty($confkey) && !preg_match(
'/^[a-z]+$/i', $confkey)) {
369 $out .=
'<form action="'.$_SERVER[
"PHP_SELF"].
'" method="POST">';
370 $out .=
'<input type="hidden" name="token" value="'.newToken().
'">';
371 $out .=
'<input type="hidden" name="key" value="'.$confkey.
'" />';
372 $out .=
'<input type="hidden" name="action" value="updatePrompts">';
373 $out .=
'<input type="hidden" name="page_y" value="">';
375 $out .=
'<table class="noborder centpercent">';
377 $out .=
'<tr class="liste_titre">';
378 $out .=
'<td class="titlefield">'.$arrayofaifeatures[$confkey][
'picto'].
' '.$langs->trans($arrayofaifeatures[$confkey][
'label']);
379 $out .=
'<a class="deletefielda reposition marginleftonly right" href="'.$_SERVER[
"PHP_SELF"].
'?action=deleteproperty&token='.
newToken().
'&key='.urlencode($confkey).
'">'.
img_delete().
'</a>';
386 $out .=
'<tr class="oddeven">';
387 $out .=
'<td class="col-setup-title">';
388 $out .=
'<span id="prePrompt" class="spanforparamtooltip">'.$langs->trans(
"Pre-Prompt").
'</span>';
391 $out .=
'<textarea class="flat minwidth500 quatrevingtpercent" id="prePromptInput_'.$confkey.
'" name="prePrompt" rows="2">'.$config[
'prePrompt'].
'</textarea>';
395 $out .=
'<tr class="oddeven">';
396 $out .=
'<td class="col-setup-title">';
397 $out .=
'<span id="postPrompt" class="spanforparamtooltip">'.$langs->trans(
"Post-Prompt").
'</span>';
400 $out .=
'<textarea class="flat minwidth500 quatrevingtpercent" id="postPromptInput_'.$confkey.
'" name="postPrompt" rows="2">'.$config[
'postPrompt'].
'</textarea>';
404 $out .=
'<tr id="fichetwothirdright-'.$confkey.
'" class="oddeven">';
405 $out .=
'<td>'.$form->textwithpicto($langs->trans(
"BlackListWords"), $langs->trans(
"BlackListWordsHelp")).
'</td>';
407 $out .=
'<input type="text" class="flat minwidth500 quatrevingtpercent" id="blacklist_'.$confkey.
'" name="blacklists" value="'.(isset($config[
'blacklists']) ? implode(
', ', (array) $config[
'blacklists']) :
'').
'">';
412 $out .=
'<td>'.$langs->trans(
"Test").
'</td>';
415 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
417 $formmail->withaiprompt =
'html';
419 $showlinktoai = $confkey;
420 $showlinktoailabel = $langs->trans(
"ToTest");
421 $htmlname = $confkey;
422 $onlyenhancements = $confkey;
423 $showlinktolayout = 0;
426 include DOL_DOCUMENT_ROOT.
'/core/tpl/formlayoutai.tpl.php';
428 $out .=
'<div id="'.$htmlname.
'"></div>';
436 $out .=
'<center><input type="submit" class="button small submitBtn reposition" name="modify" data-index="'.$confkey.
'" value="'.
dol_escape_htmltag($langs->trans(
"Save")).
'"/></center>';
450if ($action ==
'edit' || $action ==
'create' || $action ==
'deleteproperty') {
451 print
load_fiche_titre($langs->trans(
"AIModelForFeature", $arrayofai[$aiservice][
'label']),
'',
'');
453 print $formSetup->generateOutput(
true);
457if (empty($setupnotempty)) {
458 print
'<br>'.$langs->trans(
"NothingToSetup");
470print
'<datalist id="ai-model-ids"></datalist>'.
"\n";
471print
'<script nonce="'.getNonce().
'">
472fetch("'.
dol_buildpath(
'/ai/ajax/list_models.php', 1).
'").then(function (r) { return r.json(); }).then(function (j) {
473 if (!j || !j.models || !j.models.length) return;
474 var dl = document.getElementById("ai-model-ids");
475 j.models.forEach(function (id) { var o = document.createElement("option"); o.value = id; dl.appendChild(o); });
476 document.querySelectorAll("input[name*=\'_MODEL_\']").forEach(function (i) {
477 i.setAttribute("list", "ai-model-ids");
478 if (i.value && j.models.indexOf(i.value) < 0) {
479 var w = document.createElement("span");
480 w.className = "fas fa-exclamation-triangle pictowarning paddingleft";
481 w.title = "'.
dol_escape_js($langs->trans(
"AIModelNotInProviderList")).
'";
482 i.insertAdjacentElement("afterend", w);
485}).catch(function () {});
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.
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, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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.
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.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
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
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.