dolibarr 24.0.0-beta
assistant.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 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Coryright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
7 * Copyright (C) 2026 Nick Fragoulis
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY, without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
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";
44
45
46$langs->loadLangs(array("admin", "website", "other"));
47
48// Access control
49if (!$user->admin) {
51}
52if (!isModEnabled('ai')) {
53 accessforbidden('Module AI not activated.');
54}
55
56// Parameters
57$action = GETPOST('action', 'aZ09');
58$backtopage = GETPOST('backtopage', 'alpha');
59
60
65// Main Settings
66if ($action == 'update') {
67 $error = 0;
68
69 if (GETPOSTISSET('AI_ASK_FOR_CONFIRMATION')) {
70 $res = dolibarr_set_const($db, "AI_ASK_FOR_CONFIRMATION", GETPOSTINT("AI_ASK_FOR_CONFIRMATION"), 'int', 0, '', $conf->entity);
71 if ($res <= 0) $error++;
72 }
73
74 if (GETPOSTISSET('AI_LOG_RETENTION')) {
75 $res = dolibarr_set_const($db, "AI_LOG_RETENTION", GETPOST("AI_LOG_RETENTION"), 'int', 0, '', $conf->entity);
76 if ($res <= 0) {
77 $error++;
78 }
79 }
80
81 if (GETPOSTISSET('AI_DEFAULT_INPUT_MODE')) {
82 $res = dolibarr_set_const($db, "AI_DEFAULT_INPUT_MODE", GETPOST("AI_DEFAULT_INPUT_MODE"), 'chaine', 0, '', $conf->entity);
83 if ($res <= 0) {
84 $error++;
85 }
86 }
87
88 if (GETPOSTISSET('AI_INTENT_PROMPT')) {
89 $res = dolibarr_set_const($db, "AI_INTENT_PROMPT", GETPOST("AI_INTENT_PROMPT"), 'chaine', 0, '', $conf->entity);
90 if ($res <= 0) {
91 $error++;
92 }
93 }
94
95 if ($error) {
96 setEventMessages($langs->trans("ErrorSavingSettings"), null, 'errors');
97 } else {
98 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
99 }
100
101 header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home");
102 exit;
103}
104
105// Test connection
106if ($action == 'test_provider') {
107 $service = GETPOST('service_key', 'aZ09');
108 if ($service) {
109 $credential = getDolGlobalString('AI_API_' . strtoupper($service) . '_KEY');
110 $url = getDolGlobalString('AI_API_' . strtoupper($service) . '_URL');
111
112 // Decrypt if needed
113 if (preg_match('/^crypted:/', $credential)) {
114 $credential = dol_decode(substr($credential, 8));
115 } elseif (preg_match('/^dolcrypt:/', $credential)) {
116 $credential = dolDecrypt($credential, '');
117 }
118
119 // Only proceed if the key is valid (decrypted or not encrypted)
120 if ($credential !== null) {
121 $res = testAIConnection($service, $credential, $url);
122
123 if ($res['success']) {
124 setEventMessages($langs->trans("ConnectionSuccessful") . $res['message'], null, 'mesgs');
125 } else {
126 setEventMessages($langs->trans("ConnectionFailed") . $res['message'], null, 'errors');
127 }
128 }
129 }
130}
131
132// External Access Settings
133if ($action == 'update_external') {
134 $error = 0;
135
136 $user_id = GETPOSTINT("AI_MCP_USER_ID");
137 if (GETPOSTISSET('AI_MCP_USER_ID')) {
138 $res = dolibarr_set_const($db, "AI_MCP_USER_ID", $user_id, 'int', 0, '', $conf->entity);
139 if ($res <= 0) {
140 $error++;
141 }
142 }
143
144 if ($error) {
145 setEventMessages($langs->trans("ErrorSavingSettings"), null, 'errors');
146 } else {
147 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
148 }
149
150 header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home");
151 exit;
152}
153
154// Generate New API Key
155if ($action == 'generate_key') {
156 $newKey = dolGetRandomBytes(64);
157
158 if (empty($newKey)) {
159 setEventMessages($langs->trans("KeyGenerationFailed"), null, 'errors');
160 } else {
161 if (dolibarr_set_const($db, 'AI_MCP_API_KEY', $newKey, 'chaine', 0, '', $conf->entity) > 0) {
162 setEventMessages($langs->trans("KeyGenerationSuccessfull"), null, 'mesgs');
163 } else {
164 setEventMessages($langs->trans("KeySaveFailed"), null, 'errors');
165 }
166 }
167}
168
169
170/*
171 * VIEW
172 */
173
175$title = "AiSetup";
176
177llxHeader('', $langs->trans($title), $help_url, '', 0, 0, '', '', '', 'mod-ai page-admin');
178
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>';
180
181print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');
182
183
184$head = aiAdminPrepareHead();
185print dol_get_fiche_head($head, 'assistant', "MCP Server", -1, "ai");
186
187print '<span class="opacitymedium">' . $langs->trans("ConfigAssistantHelp") . '</span><br><br>';
188
189$form = new Form($db);
190
191// Load Current Values
192$apiKey = getDolGlobalString('AI_MCP_API_KEY');
193
194// Default Prompt Logic
195$defaultPromptText = " ROLE: Dolibarr ERP AI.
196GOAL: Map user intent to specific JSON commands.
197
198CONSTRAINTS:
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'.";
205
206$currentPrompt = getDolGlobalString('AI_INTENT_PROMPT', $defaultPromptText);
207
208// Settings
209print '<div class="neutral">';
210//print '<table class="noborder centpercent">';
211
212// Enable/Disable
213//print '<tr class="oddeven">';
214//print '<td class="titlefield" width="30%">'
215print $form->textwithpicto($langs->trans('EnableAssistant'), $langs->trans('DisableAssistant'));
216//print '</td>';
217//print '<td>';
218print ' &nbsp; ';
219print ajax_constantonoff('AI_ASSISTANT_ENABLED', array(), null, 0, 0, 1);
220//print ' <span class="opacitymedium">' . $langs->trans('DisableMCPAI') . '</span>';
221//print '</td>';
222//print '</tr>';
223
224//print '</table>';
225print '</div>';
226
227print '<br>';
228print '<br>';
229
230if (getDolGlobalString('AI_ASSISTANT_ENABLED')) {
231 print load_fiche_titre($langs->trans("PrivateModeTitle"), '', 'fas fa-lock');
232
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">';
236
237 print '<div class="div-table-responsive-no-min">';
238 print '<table class="noborder centpercent">';
239
240 // Input Mode
241 print '<tr class="oddeven">';
242 print '<td>';
243 print $form->textwithpicto('Default Interface', $langs->trans("InputMethodHelp"));
244 print '</td>';
245 print '<td>';
246 $input_modes = [
247 'text' => $langs->trans('OptionTextOnly'),
248 'native' => $langs->trans('OptionCloudFast') . ' - ' . $langs->trans('OptionCloudFasthelp'),
249 'whisper' => $langs->trans('OptionWhisperLocal') . ' - ' . $langs->trans('OptionWhisperLocalhelp')
250 ];
251 print $form->selectarray('AI_DEFAULT_INPUT_MODE', $input_modes, getDolGlobalString('AI_DEFAULT_INPUT_MODE'), 0, 0, 0);
252 print '</td>';
253 print '</tr>';
254
255 // Enhanced Privacy control
256 print '<tr class="oddeven">';
257 print '<td>' . $langs->trans('ObfuscatePIIData') . '</td>';
258 print '<td>';
259 print ajax_constantonoff('AI_PRIVACY_REDACTION');
260 print ' &nbsp; <span class="opacitymedium">' . $langs->trans("RedactionHelp") . '</span>';
261 print '</td>';
262 print '</tr>';
263
264 // Confirmation Level
265 print '<tr class="oddeven">';
266 print '<td>' . $form->textwithpicto($langs->trans("AskConfirmation"), $langs->trans("AskConfirmationHelp")).'</td>';
267 print '<td>';
268 $confirmation_options = [
269 '0' => $langs->trans("ConfirmNever"),
270 '1' => $langs->trans("ConfirmWriteOnly"),
271 '2' => $langs->trans("ConfirmAlways")
272 ];
273 print $form->selectarray('AI_ASK_FOR_CONFIRMATION', $confirmation_options, getDolGlobalInt('AI_ASK_FOR_CONFIRMATION', 1), 0, 0, 0);
274 print '</td>';
275 print '</tr>';
276
277 // Logging
278 print '<tr class="oddeven">';
279 print '<td>' . $langs->trans('EnableLogging') . '</td>';
280 print '<td>';
281 print ajax_constantonoff('AI_LOG_REQUESTS');
282 print ' &nbsp; <a href="' . DOL_URL_ROOT . '/ai/admin/log_viewer.php" target="_blank" style="padding-top: 4px; padding-bottom: 4px;">'.$langs->trans("ViewLogs").'</a>';
283 print '</td>';
284 print '</tr>';
285
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>';
289 print '</tr>';
290
291 // System Prompt
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();
297 print '</td>';
298 print '</tr>';
299
300 print '</table>';
301 print '</div>';
302
303 print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Save").'"></div>';
304 print '</form>';
305
306 print '<br>';
307 print '<br>';
308
309
310 // AI Provider Config and Connection testing
311 $services = getListOfAIServices();
312 $currentService = getDolGlobalString('AI_API_SERVICE');
313
314 print load_fiche_titre($langs->trans("AIProviderConfigTitle"), '', 'fa fa-plug');
315
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>';
318 } else {
319 print '<div class="div-table-responsive-no-min">';
320 print '<table class="noborder centpercent">';
321
322 print '<tr class="oddeven"><td class="titlefield">'.$langs->trans("AIProvider").'</td><td>'.$services[$currentService]['label'].'</td></tr>';
323
324 $prefix = 'AI_API_'.strtoupper($currentService);
325 $modelVal = getDolGlobalString($prefix.'_MODEL_TEXT', $services[$currentService]['textgeneration']['default']);
326
327 print '<tr class="oddeven"><td>'.$langs->trans("AI_API_MODEL").'</td><td>'.$modelVal.'</td></tr>';
328 print '</table></div>';
329
330 print '<div class="center">';
331
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>';
334 }
335
336 print '</div>';
337 }
338
339 print '<br><br>';
340
341 // External Access Configuration
342 /*
343 print load_fiche_titre($langs->trans("AiMcpExternalAccess"), '', 'fas fa-lock-open');
344
345 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
346 print '<input type="hidden" name="token" value="'.newToken().'">';
347 print '<input type="hidden" name="action" value="update_external">';
348
349 print '<div class="div-table-responsive-no-min">';
350 print '<table class="noborder centpercent">';
351
352 // Service User
353 print '<tr class="oddeven">';
354 print '<td>Service User <span class="fa fa-info-circle" title="' . $langs->trans("UserPermissionsTooltip") . '"></span></td>';
355 print '<td>';
356 print '<div style="display: flex; align-items: center;">';
357 print $form->select_dolusers(getDolGlobalInt('AI_MCP_USER_ID'), 'AI_MCP_USER_ID', 1);
358 print ' <input type="submit" class="button" value="'.$langs->trans("Save").'" style="margin-left: 20px;">';
359 print '</div>';
360 print '<span class="opacitymedium small">' . $langs->trans("DedicatedUserRecommendation") . '</span>';
361 print '</td>';
362 print '</tr>';
363
364 print '<tr class="oddeven">';
365 print '<td width="30%">API Key</td>';
366 print '<td>';
367 if ($apiKey) {
368 print '<input type="text" id="apikey" value="'.$apiKey.'" readonly style="width:400px; padding:6px; background:#f4f4f4; border:1px solid #ccc; color:#555;">';
369 print ' <a class="button smallpaddingimp" href="'.$_SERVER["PHP_SELF"].'?action=generate_key&token='.newToken().'">Generate New Key</a>';
370 } else {
371 print '<span class="opacitymedium">' . $langs->trans("NoKeyWarning") . '</span>';
372 print ' <a class="button smallpaddingimp" href="' . $_SERVER["PHP_SELF"] . '?action=generate_key&token=' . newToken() . '">' . $langs->trans("GenerateKey") . '</a>';
373 }
374 print '</td>';
375 print '</tr>';
376
377 $endpoint = dol_buildpath('/ai/server/mcp_server.php', 3);
378
379 print '<tr class="oddeven">';
380 print '<td>Endpoint URL</td>';
381 print '<td>';
382 print '<input type="text" id="endpoint" value="'.$endpoint.'" readonly style="width:600px; border:none; background:transparent;">';
383 print '</td>';
384 print '</tr>';
385
386 print '</table>';
387 print '</div>';
388
389 print '</form>';
390
391 // Configuration Examples
392 print '<br>';
393 print '<div style="background:#fcfcfc; border:1px solid #eee; padding:15px; border-radius:5px;">';
394 print '<strong>' . $langs->trans("ClaudeDesktopConfig") . '</strong><br>';
395 print '<pre style="background:#333; color:#fff; padding:10px; border-radius:4px; overflow:auto; margin-top:10px;">';
396 echo htmlspecialchars('
397 {
398 "mcpServers": {
399 "dolibarr": {
400 "command": "node",
401 "args": ["/path/to/mcp-bridge.js"],
402 "env": {
403 "DOLIBARR_URL": "'.$endpoint.'",
404 "DOLIBARR_API_KEY": "'.($apiKey ? $apiKey : "YOUR_KEY_HERE").'"
405 }
406 }
407 }
408 }');
409 print '</pre>';
410 print '</div>';
411 */
412}
413
414print dol_get_fiche_end();
415
416llxFooter();
417$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).
testAIConnection(string $service, string $key, string $url)
Tests the connection to an AI service using its API key and URL by sending message "Hello".
Definition ai.lib.php:212
aiAdminPrepareHead()
Prepare admin pages header.
Definition ai.lib.php:407
getListOfAIServices()
Get list of available ai services.
Definition ai.lib.php:68
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.
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 manage a WYSIWYG editor.
Class to manage generation of HTML components Only common components must be here.
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)
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.