30require
'../../main.inc.php';
39require_once DOL_DOCUMENT_ROOT .
"/core/lib/admin.lib.php";
40require_once DOL_DOCUMENT_ROOT .
"/core/class/html.form.class.php";
41require_once DOL_DOCUMENT_ROOT .
"/core/class/doleditor.class.php";
42require_once DOL_DOCUMENT_ROOT .
"/ai/lib/ai.lib.php";
43require_once DOL_DOCUMENT_ROOT .
"/core/lib/functions2.lib.php";
44require_once DOL_DOCUMENT_ROOT .
"/core/lib/security.lib.php";
47$langs->loadLangs(array(
"admin",
"website",
"other"));
58$action =
GETPOST(
'action',
'aZ09');
59$backtopage =
GETPOST(
'backtopage',
'alpha');
67if ($action ==
'update') {
72 if ($res <= 0) $error++;
111 setEventMessages($langs->trans(
"ErrorSavingSettings"),
null,
'errors');
113 setEventMessages($langs->trans(
"SetupSaved"),
null,
'mesgs');
116 header(
"Location: ".$_SERVER[
"PHP_SELF"].
"?mainmenu=home");
121if ($action ==
'test_provider') {
122 $service =
GETPOST(
'service_key',
'aZ09');
128 if (preg_match(
'/^crypted:/', $credential)) {
129 $credential =
dol_decode(substr($credential, 8));
130 } elseif (preg_match(
'/^dolcrypt:/', $credential)) {
135 if ($credential !==
null) {
138 if ($res[
'success']) {
139 setEventMessages($langs->trans(
"ConnectionSuccessful") . $res[
'message'],
null,
'mesgs');
141 setEventMessages($langs->trans(
"ConnectionFailed") . $res[
'message'],
null,
'errors');
148if ($action ==
'update_external') {
160 setEventMessages($langs->trans(
"ErrorSavingSettings"),
null,
'errors');
162 setEventMessages($langs->trans(
"SetupSaved"),
null,
'mesgs');
165 header(
"Location: ".$_SERVER[
"PHP_SELF"].
"?mainmenu=home");
170if ($action ==
'generate_key') {
173 if (empty($newKey)) {
174 setEventMessages($langs->trans(
"KeyGenerationFailed"),
null,
'errors');
177 setEventMessages($langs->trans(
"KeyGenerationSuccessfull"),
null,
'mesgs');
179 setEventMessages($langs->trans(
"KeySaveFailed"),
null,
'errors');
192llxHeader(
'', $langs->trans($title),
$help_url,
'', 0, 0,
'',
'',
'',
'mod-ai page-admin');
194$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>';
204$form =
new Form($db);
210$defaultPromptText =
" ROLE: Dolibarr ERP AI.
211GOAL: Map user intent to specific JSON commands.
2141. OUTPUT: Single valid JSON object ONLY. No Markdown. No text.
215 Format: {\"tool\": \"tool_name\", \"arguments\": {\"argument_name\": \"argument_value\", ...}}
2162. TOOLS: Use ONLY provided tools. If no tool or thirdparty fits, you must first use 'respond_to_user' to inform user.
2173. ARGS: strict adherence to schema. Do not invent parameters.
2184. MISSING INFO: If a required argument (like an ID) is missing, use 'ask_for_clarification'.
2195. SAFETY: For DELETE/UPDATE actions, you MUST use 'ask_for_confirmation'.";
224print
'<div class="neutral">';
230print $form->textwithpicto($langs->trans(
'EnableAssistant'), $langs->trans(
'DisableAssistant'));
234print ajax_constantonoff(
'AI_ASSISTANT_ENABLED', array(),
null, 0, 0, 1);
246 print
load_fiche_titre($langs->trans(
"PrivateModeTitle"),
'',
'fas fa-lock');
248 print
'<form method="POST" action="'.$_SERVER[
"PHP_SELF"].
'">';
249 print
'<input type="hidden" name="token" value="'.newToken().
'">';
250 print
'<input type="hidden" name="action" value="update">';
252 print
'<div class="div-table-responsive-no-min">';
253 print
'<table class="noborder centpercent">';
256 print
'<tr class="oddeven">';
258 print $form->textwithpicto(
'Default Interface', $langs->trans(
"InputMethodHelp"));
262 $labelcloud = $langs->trans(
'OptionCloudFast') .
' - ' . $langs->trans(
'OptionCloudFasthelp');
264 'text' => array(
'label' => $langs->trans(
'OptionTextOnly'),
'data-html' => img_picto(
'',
'fa-keyboard',
'class="pictofixedwidth"') . dolPrintHTML($langs->trans(
'OptionTextOnly'))),
265 'cloud' => array(
'label' => $labelcloud,
'data-html' => img_picto(
'',
'fa-cloud',
'class="pictofixedwidth"') . dolPrintHTML($labelcloud)),
269 $labelwhisper = $langs->trans(
'OptionWhisperLocal') .
' - ' . $langs->trans(
'OptionWhisperLocalhelp');
270 $input_modes[
'whisper'] = array(
'label' => $labelwhisper,
'data-html' => img_picto(
'',
'fa-microphone',
'class="pictofixedwidth"') . dolPrintHTML($labelwhisper) .
' <span class="opacitymedium">(' . $langs->trans(
'VersionExperimental') .
')</span>');
273 if ($defaultinputmode ==
'native') {
274 $defaultinputmode =
'cloud';
276 print $form->selectarray(
'AI_DEFAULT_INPUT_MODE', $input_modes, $defaultinputmode, 0, 0, 0,
'', 0, 0, 0,
'',
'minwidth300');
281 print
'<tr class="oddeven">';
282 print
'<td>' . $form->textwithpicto($langs->trans(
'ObfuscatePIIData'), $langs->trans(
"RedactionHelp")) .
'</td>';
284 $redaction_options = [
285 '0' => $langs->trans(
"RedactionNone"),
286 '1' => $langs->trans(
"RedactionSensitiveData"),
287 '2' => $langs->trans(
"RedactionSensitiveDataAndThirdparties")
289 print $form->selectarray(
'AI_PRIVACY_REDACTION', $redaction_options,
getDolGlobalInt(
'AI_PRIVACY_REDACTION', 0), 0, 0, 0,
'', 0, 0, 0,
'',
'minwidth300');
294 print
'<tr class="oddeven">';
295 print
'<td>' . $form->textwithpicto($langs->trans(
"AskConfirmation"), $langs->trans(
"AskConfirmationHelp")).
'</td>';
297 $confirmation_options = [
298 '0' => $langs->trans(
"ConfirmNever"),
299 '1' => $langs->trans(
"ConfirmWriteOnly"),
300 '2' => $langs->trans(
"ConfirmAlways")
302 print $form->selectarray(
'AI_ASK_FOR_CONFIRMATION', $confirmation_options,
getDolGlobalInt(
'AI_ASK_FOR_CONFIRMATION', 1), 0, 0, 0);
307 print
'<tr class="oddeven">';
308 print
'<td>' . $form->textwithpicto($langs->trans(
"AIContextAutoExchanges"), $langs->trans(
"AIContextAutoExchangesHelp")) .
'</td>';
309 print
'<td><input class="width50" type="number" min="0" name="AI_CHAT_CONTEXT_AUTO_EXCHANGES" value="' .
getDolGlobalInt(
'AI_CHAT_CONTEXT_AUTO_EXCHANGES', 2) .
'"></td>';
313 print
'<tr class="oddeven">';
314 print
'<td>' . $form->textwithpicto($langs->trans(
"AIPaletteApiTools"), $langs->trans(
"AIPaletteApiToolsHelp")) .
'</td>';
316 print ajax_constantonoff(
'AI_PALETTE_API_TOOLS');
321 print
'<tr class="oddeven">';
322 print
'<td>' . $langs->trans(
'EnableLogging') .
'</td>';
324 print ajax_constantonoff(
'AI_LOG_REQUESTS');
325 print
' <a href="' . DOL_URL_ROOT .
'/ai/admin/log_viewer.php" target="_blank" style="padding-top: 4px; padding-bottom: 4px;">'.$langs->trans(
"ViewLogs").
'</a>';
329 print
'<tr class="oddeven">';
330 print
'<td>' . $langs->trans(
"LogRetention") .
'</td>';
331 print
'<td><input class="width50" type="number" name="AI_LOG_RETENTION" value="' .
getDolGlobalInt(
'AI_LOG_RETENTION', 30) .
'"> (0 = Forever)</td>';
335 print
'<tr class="oddeven">';
336 print
'<td colspan="2">';
337 print $form->textwithpicto($langs->trans(
"SystemPrompt"), $langs->trans(
"SystemPromptHelp")) .
'<br><br>';
338 $doleditor =
new DolEditor(
'AI_INTENT_PROMPT', $currentPrompt,
'', 250,
'dolibarr_notes',
'In',
false,
false,
true, ROWS_8,
'90%');
339 $doleditor->Create();
346 print
'<div class="center"><input type="submit" class="button" value="'.$langs->trans(
"Save").
'"></div>';
357 print
load_fiche_titre($langs->trans(
"AIProviderConfigTitle"),
'',
'fa fa-plug');
359 if ((
string) $currentService ==
'-1') {
360 print
'<div class="warning">'.$langs->trans(
"NoAIProviderSelected").
' <a href="'.
dol_buildpath(
'/ai/admin/setup.php', 1).
'">'.$langs->trans(
"ConfigureHere").
'</a></div>';
362 print
'<div class="div-table-responsive-no-min">';
363 print
'<table class="noborder centpercent">';
365 print
'<tr class="oddeven"><td class="titlefield">'.$langs->trans(
"AIProvider").
'</td><td>'.$services[$currentService][
'label'].
'</td></tr>';
367 $prefix =
'AI_API_'.strtoupper($currentService);
368 $modelVal =
getDolGlobalString($prefix.
'_MODEL_TEXT', $services[$currentService][
'textgeneration'][
'default']);
370 print
'<tr class="oddeven"><td>'.$langs->trans(
"AI_API_MODEL").
'</td><td>'.$modelVal.
'</td></tr>';
371 print
'</table></div>';
373 print
'<div class="center">';
375 if ($currentService && $currentService !==
'-1') {
376 print
'<a class="reposition button smallpaddingimp" href="'.$_SERVER[
'PHP_SELF'].
'?action=test_provider&token='.
newToken().
'&service_key='.urlencode($currentService).
'">'.$langs->trans(
'TestConnection').
'</a>';
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).
testAIConnection(string $service, string $key, string $url)
Tests the connection to an AI service using its API key and URL by sending message "Hello".
aiAdminPrepareHead()
Prepare admin pages header.
getListOfAIServices()
Get list of available ai services.
if( $action=='update') if($action=='test_provider') if( $action=='update_external') if($action=='generate_key') $help_url
ACTIONS.
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.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage a WYSIWYG editor.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
dolGetRandomBytes($length)
Return a string of random bytes (hexa string) with length = $length for cryptographic purposes.
dol_decode($chain, $key='1')
Decode a base 64 encoded + specific delta change.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.