29require
'../../main.inc.php';
38require_once DOL_DOCUMENT_ROOT .
"/core/lib/admin.lib.php";
39require_once DOL_DOCUMENT_ROOT .
"/core/class/html.form.class.php";
40require_once DOL_DOCUMENT_ROOT .
"/core/class/doleditor.class.php";
41require_once DOL_DOCUMENT_ROOT .
"/ai/lib/ai.lib.php";
42require_once DOL_DOCUMENT_ROOT .
"/core/lib/functions2.lib.php";
43require_once DOL_DOCUMENT_ROOT .
"/core/lib/security.lib.php";
46$langs->loadLangs(array(
"admin",
"website",
"other"));
57$action =
GETPOST(
'action',
'aZ09');
58$backtopage =
GETPOST(
'backtopage',
'alpha');
66if ($action ==
'update') {
69 if (GETPOSTISSET(
'AI_ASK_FOR_CONFIRMATION')) {
71 if ($res <= 0) $error++;
74 if (GETPOSTISSET(
'AI_LOG_RETENTION')) {
81 if (GETPOSTISSET(
'AI_DEFAULT_INPUT_MODE')) {
88 if (GETPOSTISSET(
'AI_INTENT_PROMPT')) {
101 header(
"Location: ".$_SERVER[
"PHP_SELF"].
"?mainmenu=home");
106if ($action ==
'test_provider') {
107 $service =
GETPOST(
'service_key',
'aZ09');
113 if (preg_match(
'/^crypted:/', $credential)) {
114 $credential =
dol_decode(substr($credential, 8));
115 } elseif (preg_match(
'/^dolcrypt:/', $credential)) {
120 if ($credential !==
null) {
123 if ($res[
'success']) {
124 setEventMessages($langs->trans(
"ConnectionSuccessful") . $res[
'message'],
null,
'mesgs');
126 setEventMessages($langs->trans(
"ConnectionFailed") . $res[
'message'],
null,
'errors');
133if ($action ==
'update_external') {
137 if (GETPOSTISSET(
'AI_MCP_USER_ID')) {
150 header(
"Location: ".$_SERVER[
"PHP_SELF"].
"?mainmenu=home");
155if ($action ==
'generate_key') {
158 if (empty($newKey)) {
177llxHeader(
'', $langs->trans($title),
$help_url,
'', 0, 0,
'',
'',
'',
'mod-ai page-admin');
179$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>';
187print
'<span class="opacitymedium">' . $langs->trans(
"ConfigAssistantHelp") .
'</span><br><br>';
195$defaultPromptText =
" ROLE: Dolibarr ERP AI.
196GOAL: Map user intent to specific JSON commands.
1991. OUTPUT: Single valid JSON object ONLY. No Markdown. No text.
200 Format: {\"tool\": \"tool_name\", \"arguments\": {\"argument_name\": \"argument_value\", ...}}
2012. TOOLS: Use ONLY provided tools. If no tool or thirdparty fits, you must first use 'respond_to_user' to inform user.
2023. ARGS: strict adherence to schema. Do not invent parameters.
2034. MISSING INFO: If a required argument (like an ID) is missing, use 'ask_for_clarification'.
2045. SAFETY: For DELETE/UPDATE actions, you MUST use 'ask_for_confirmation'.";
209print
'<div class="neutral">';
215print $form->textwithpicto($langs->trans(
'EnableAssistant'), $langs->trans(
'DisableAssistant'));
219print ajax_constantonoff(
'AI_ASSISTANT_ENABLED', array(),
null, 0, 0, 1);
231 print
load_fiche_titre($langs->trans(
"PrivateModeTitle"),
'',
'fas fa-lock');
233 print
'<form method="POST" action="'.$_SERVER[
"PHP_SELF"].
'">';
234 print
'<input type="hidden" name="token" value="'.newToken().
'">';
235 print
'<input type="hidden" name="action" value="update">';
237 print
'<div class="div-table-responsive-no-min">';
238 print
'<table class="noborder centpercent">';
241 print
'<tr class="oddeven">';
243 print $form->textwithpicto(
'Default Interface', $langs->trans(
"InputMethodHelp"));
247 'text' => $langs->trans(
'OptionTextOnly'),
248 'native' => $langs->trans(
'OptionCloudFast') .
' - ' . $langs->trans(
'OptionCloudFasthelp'),
249 'whisper' => $langs->trans(
'OptionWhisperLocal') .
' - ' . $langs->trans(
'OptionWhisperLocalhelp')
251 print $form->selectarray(
'AI_DEFAULT_INPUT_MODE', $input_modes,
getDolGlobalString(
'AI_DEFAULT_INPUT_MODE'), 0, 0, 0);
256 print
'<tr class="oddeven">';
257 print
'<td>' . $langs->trans(
'ObfuscatePIIData') .
'</td>';
259 print ajax_constantonoff(
'AI_PRIVACY_REDACTION');
260 print
' <span class="opacitymedium">' . $langs->trans(
"RedactionHelp") .
'</span>';
265 print
'<tr class="oddeven">';
266 print
'<td>' . $form->textwithpicto($langs->trans(
"AskConfirmation"), $langs->trans(
"AskConfirmationHelp")).
'</td>';
268 $confirmation_options = [
269 '0' => $langs->trans(
"ConfirmNever"),
270 '1' => $langs->trans(
"ConfirmWriteOnly"),
271 '2' => $langs->trans(
"ConfirmAlways")
273 print $form->selectarray(
'AI_ASK_FOR_CONFIRMATION', $confirmation_options,
getDolGlobalInt(
'AI_ASK_FOR_CONFIRMATION', 1), 0, 0, 0);
278 print
'<tr class="oddeven">';
279 print
'<td>' . $langs->trans(
'EnableLogging') .
'</td>';
281 print ajax_constantonoff(
'AI_LOG_REQUESTS');
282 print
' <a href="' . DOL_URL_ROOT .
'/ai/admin/log_viewer.php" target="_blank" style="padding-top: 4px; padding-bottom: 4px;">'.$langs->trans(
"ViewLogs").
'</a>';
286 print
'<tr class="oddeven">';
287 print
'<td>' . $langs->trans(
"LogRetention") .
'</td>';
288 print
'<td><input class="width50" type="number" name="AI_LOG_RETENTION" value="' .
getDolGlobalInt(
'AI_LOG_RETENTION', 30) .
'"> (0 = Forever)</td>';
292 print
'<tr class="oddeven">';
293 print
'<td colspan="2">';
294 print $form->textwithpicto($langs->trans(
"SystemPrompt"), $langs->trans(
"SystemPromptHelp")) .
'<br><br>';
295 $doleditor =
new DolEditor(
'AI_INTENT_PROMPT', $currentPrompt,
'', 250,
'dolibarr_notes',
'In',
false,
false,
true, ROWS_8,
'90%');
296 $doleditor->Create();
303 print
'<div class="center"><input type="submit" class="button" value="'.$langs->trans(
"Save").
'"></div>';
314 print
load_fiche_titre($langs->trans(
"AIProviderConfigTitle"),
'',
'fa fa-plug');
316 if ((
string) $currentService ==
'-1') {
317 print
'<div class="warning">'.$langs->trans(
"NoAIProviderSelected").
' <a href="'.
dol_buildpath(
'/ai/admin/setup.php', 1).
'">'.$langs->trans(
"ConfigureHere").
'</a></div>';
319 print
'<div class="div-table-responsive-no-min">';
320 print
'<table class="noborder centpercent">';
322 print
'<tr class="oddeven"><td class="titlefield">'.$langs->trans(
"AIProvider").
'</td><td>'.$services[$currentService][
'label'].
'</td></tr>';
324 $prefix =
'AI_API_'.strtoupper($currentService);
325 $modelVal =
getDolGlobalString($prefix.
'_MODEL_TEXT', $services[$currentService][
'textgeneration'][
'default']);
327 print
'<tr class="oddeven"><td>'.$langs->trans(
"AI_API_MODEL").
'</td><td>'.$modelVal.
'</td></tr>';
328 print
'</table></div>';
330 print
'<div class="center">';
332 if ($currentService && $currentService !==
'-1') {
333 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.
Class to manage a WYSIWYG editor.
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)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
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.
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.