dolibarr 24.0.0-beta
ai.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2022 Alice Adminson <aadminson@example.com>
3 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2026 Anthony Damhet <a.damhet@progiseize.fr>
6 * Copyright (C) 2026 Nick Fragoulis
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28include_once DOL_DOCUMENT_ROOT.'/ai/class/ai.class.php';
29
30
37{
38 global $langs;
39
40 $arrayofaifeatures = array(
41 'textgenerationemail' => array('label' => $langs->trans('TextGeneration').' ('.$langs->trans("EmailContent").')', 'picto' => '', 'status' => 'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_EMAIL),
42 'textgenerationwebpage' => array('label' => $langs->trans('TextGeneration').' ('.$langs->trans("WebsitePage").')', 'picto' => '', 'status' => 'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_WEBPAGE),
43 'textgeneration' => array('label' => $langs->trans('TextGeneration').' ('.$langs->trans("Other").')', 'picto' => '', 'status' => 'notused', 'function' => 'TEXT'),
44
45 'texttranslation' => array('label' => $langs->trans('TextTranslation'), 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_TEXT_TRANSLATION),
46 'textsummarize' => array('label' => $langs->trans('TextSummarize'), 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_TEXT_SUMMARIZE),
47 'textspellchecker' => array('label' => $langs->trans('TextSpellChecker'), 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_TEXT_SPELLCHECKER),
48 'textrephrase' => array('label' => $langs->trans('TextRephraser'), 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_TEXT_REPHRASER),
49
50 'textgenerationextrafield' => array('label' => $langs->trans('TextGeneration').' ('.$langs->trans("ExtrafieldFiller").')', 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_EXTRAFIELD_FILLER),
51
52 'imagegeneration' => array('label' => 'ImageGeneration', 'picto' => '', 'status' => 'notused', 'function' => 'IMAGE'),
53 'videogeneration' => array('label' => 'VideoGeneration', 'picto' => '', 'status' => 'notused', 'function' => 'VIDEO'),
54 'audiogeneration' => array('label' => 'AudioGeneration', 'picto' => '', 'status' => 'notused', 'function' => 'AUDIO'),
55 'transcription' => array('label' => 'AudioTranscription', 'picto' => '', 'status' => 'notused', 'function' => 'TRANSCRIPT'),
56 'translation' => array('label' => 'AudioTranslation', 'picto' => '', 'status' => 'notused', 'function' => 'TRANSLATE'),
57 'docparsing' => array('label' => 'DocumentParsing', 'picto' => '', 'status' => 'experimental', 'function' => 'DOCPARSING')
58 );
59
60 return $arrayofaifeatures;
61}
62
69{
70 global $langs;
71
72 $arrayofai = array(
73 '-1' => array('label' => $langs->trans('SelectAService')),
74 'chatgpt' => array(
75 'label' => 'ChatGPT (OpenAI)',
76 'url' => 'https://api.openai.com/v1/',
77 'setup' => 'https://platform.openai.com/account/api-keys',
78 'textgeneration' => array('default' => 'gpt-5.2'), // Flagship model released late 2025, updated Feb 2026
79 'imagegeneration' => array('default' => 'gpt-image-1.5'), // Replaced DALL-E 3; 4x faster and native to GPT-5
80 'audiogeneration' => array('default' => 'gpt-audio-1.5'), // New Feb 23, 2026 release for high-fidelity audio out
81 'videogeneration' => array('default' => 'sora-2'), // OpenAI's standard API video model
82 'transcription' => array('default' => 'whisper-large-v3-turbo'), // The current speed/accuracy benchmark for ASR
83 'translation' => array('default' => 'whisper-large-v3-turbo'), // Still the best for multi-language audio translation
84 'docparsing' => array('default' => 'gpt-5.2'), // Uses the new Responses API / Vision capabilities
85 'adapter_type' => 'openai'
86 ),
87 'groq' => array(
88 'label' => 'Groq (LPU Inference)',
89 'url' => 'https://api.groq.com/openai/v1/',
90 'setup' => 'https://console.groq.com/keys',
91 'textgeneration' => array('default' => 'llama-4-8b-instant'), // February 2026 flagship for extreme speed (1,000+ t/s)
92 'imagegeneration' => array('default' => 'na'),
93 'audiogeneration' => array('default' => 'na'),
94 'videogeneration' => array('default' => 'na'),
95 'transcription' => array('default' => 'whisper-large-v3-turbo'), // Groq's specialized high-speed Whisper implementation
96 'translation' => array('default' => 'whisper-large-v3-turbo'), // High-speed audio translation to English
97 'docparsing' => array('default' => 'llama-4-70b-versatile'), // Best for structured data extraction from text
98 'adapter_type' => 'openai'
99 ),
100 'mistral' => array(
101 'label' => 'Mistral AI',
102 'url' => 'https://api.mistral.ai/v1/',
103 'setup' => 'https://console.mistral.ai/api-keys/',
104 'textgeneration' => array('default' => 'mistral-small-latest', 'examples' => 'mistral-tiny-latest, mistral-small-latest, mistral-medium-latest, mistral-large-latest'), // Points to Mistral Small 3 (updated Feb 2026)
105 'imagegeneration' => array('default' => 'na'),
106 'audiogeneration' => array('default' => 'na'),
107 'videogeneration' => array('default' => 'na'),
108 'transcription' => array('default' => 'na'),
109 'translation' => array('default' => 'na'),
110 'docparsing' => array('default' => 'pixtral-12b-latest'), // Mistral's native vision/doc model
111 'adapter_type' => 'openai'
112 ),
113 'deepseek' => array(
114 'label' => 'DeepSeek',
115 'url' => 'https://api.deepseek.com',
116 'setup' => 'https://platform.deepseek.com/api_keys',
117 'textgeneration' => array('default' => 'deepseek-v4'), // Released Feb 2026, flagship MoE model
118 'imagegeneration' => array('default' => 'deepseek-janus-2'), // DeepSeek's latest multimodal vision/gen model
119 'audiogeneration' => array('default' => 'na'),
120 'videogeneration' => array('default' => 'na'),
121 'transcription' => array('default' => 'na'),
122 'translation' => array('default' => 'na'),
123 'docparsing' => array('default' => 'deepseek-v4'), // Massive 1M context support for parsing
124 'adapter_type' => 'openai'
125 ),
126 'perplexity' => array(
127 'label' => 'Perplexity (Sonar)',
128 'url' => 'https://api.perplexity.ai',
129 'setup' => 'https://www.perplexity.ai/settings/api',
130 'textgeneration' => array('default' => 'sonar-pro'), // Flagship search model as of Feb 2026
131 'imagegeneration' => array('default' => 'na'),
132 'audiogeneration' => array('default' => 'na'),
133 'videogeneration' => array('default' => 'na'),
134 'transcription' => array('default' => 'na'),
135 'translation' => array('default' => 'na'),
136 'docparsing' => array('default' => 'sonar-reasoning'), // Best for analyzing search-grounded docs
137 'adapter_type' => 'openai'
138 ),
139 'zai' => array(
140 'label' => 'Zhipu AI (GLM)',
141 'url' => 'https://api.z.ai/api/paas/v4',
142 'setup' => 'https://docs.z.ai/guides/overview/quick-start',
143 'textgeneration' => array('default' => 'glm-5'), // Flagship released February 11, 2026
144 'imagegeneration' => array('default' => 'cogview-4'), // Zhipu's latest SOTA image generator
145 'audiogeneration' => array('default' => 'cogvlm2-audio'), // High-fidelity conversational audio
146 'videogeneration' => array('default' => 'cogvideox-2'), // Flagship API video model
147 'transcription' => array('default' => 'na'),
148 'translation' => array('default' => 'na'),
149 'docparsing' => array('default' => 'glm-5'), // Top-tier agentic document processing
150 'adapter_type' => 'openai'
151 ),
152 'custom' => array(
153 'label' => 'Custom',
154 'url' => 'https://domainofapi.com/v1/',
155 'setup' => 'Ask your AI provider how to get your API key',
156 'textgeneration' => array('default' => 'tinyllama-1.1b'),
157 'imagegeneration' => array('default' => 'mixtral-8x7b-32768'),
158 'audiogeneration' => array('default' => 'mixtral-8x7b-32768'),
159 'videogeneration' => array('default' => 'na'),
160 'transcription' => array('default' => 'mixtral-8x7b-32768'),
161 'translation' => array('default' => 'mixtral-8x7b-32768'),
162 'docparsing' => array('default' => 'na'),
163 'adapter_type' => 'openai'
164 ),
165 // --- SPECIALIZED ADAPTERS ---
166 'anthropic' => array(
167 'label' => 'Anthropic (Claude)',
168 'url' => 'https://api.anthropic.com/v1/',
169 'setup' => 'https://console.anthropic.com/',
170 'textgeneration' => array('default' => 'claude-opus-4-6'), // Released Feb 2026; features a 1M context window
171 'imagegeneration' => array('default' => 'na'), // Anthropic remains focused on text/code logic
172 'audiogeneration' => array('default' => 'na'),
173 'videogeneration' => array('default' => 'na'),
174 'transcription' => array('default' => 'na'),
175 'translation' => array('default' => 'na'),
176 'docparsing' => array('default' => 'claude-opus-4-6'), // Leading model for "Computer Use" and PDF analysis
177 'adapter_type' => 'anthropic'
178 ),
179 'google' => array(
180 'label' => 'Google Gemini',
181 'url' => 'https://generativelanguage.googleapis.com/v1beta/',
182 'setup' => 'https://aistudio.google.com/',
183 'textgeneration' => array('default' => 'gemini-3.1-pro-preview'), // Flagship reasoning model released Feb 19, 2026
184 'imagegeneration' => array('default' => 'nano-banana-pro'), // Latest SOTA image model (Gemini 3 Pro Image)
185 'audiogeneration' => array('default' => 'gemini-2.5-pro-tts'), // High-fidelity native speech synthesis
186 'videogeneration' => array('default' => 'veo-3.1'), // Google's flagship cinematic video API
187 'transcription' => array('default' => 'gemini-3.1-pro-preview'), // Native multi-modal audio reasoning
188 'translation' => array('default' => 'gemini-3.1-pro-preview'), // Native audio-to-text translation
189 'docparsing' => array('default' => 'gemini-3.1-pro-preview'), // Massive 2M+ context window for full repo parsing
190 'adapter_type' => 'google'
191 )
192 );
193
194 return $arrayofai;
195}
196
212function testAIConnection(string $service, string $key, string $url): array
213{
214 if (empty($key)) {
215 return ['success' => false, 'message' => 'API Key is empty'];
216 }
217
218 // Load Defaults (Ensure this function exists or handle the error)
219 if (!function_exists('getListOfAIServices')) {
220 return ['success' => false, 'message' => 'Configuration helper function missing.'];
221 }
222
223 $list = getListOfAIServices();
224 $defUrl = $list[$service]['url'] ?? '';
225 // Use model from config, fallback to hardcoded if necessary
226 $defaultModel = $list[$service]['model'] ?? 'unknown';
227
228 // Normalize URL
229 if (empty($url)) {
230 $url = $defUrl;
231 }
232 $url = rtrim($url, '/');
233
234 $data = [];
235 $headers = ["Content-Type: application/json"];
236
237 $model = '';
238 if (empty($model)) {
239 $model = getDolGlobalString('AI_API_' . strtoupper($service) . '_MODEL_TEXT');
240 }
241
242 // GOOGLE
243 if ($service == 'google' || strpos($url, 'googleapis') !== false) {
244 if (strpos($url, ':generateContent') === false) {
245 if (strpos($url, 'models') === false) {
246 $url .= "/models/$model:generateContent";
247 } else {
248 $url .= "/$model:generateContent";
249 }
250 }
251 $url .= "?key=" . $key;
252 $data = ["contents" => [ ["parts" => [ ["text" => "Hello"] ] ] ], "generationConfig" => ["maxOutputTokens" => 5]];
253 } elseif ($service == 'anthropic' || strpos($url, 'anthropic') !== false) { // ANTHROPIC
254 if (strpos($url, 'messages') === false) $url .= '/messages';
255 $headers[] = "x-api-key: $key";
256 $headers[] = "anthropic-version: 2023-06-01";
257 $data = [
258 "model" => $model, // Uses Configured Model
259 "messages" => [["role" => "user", "content" => "Hello"]],
260 "max_tokens" => 5
261 ];
262 } else {
263 if (strpos($url, '/chat/completions') === false) $url .= '/chat/completions';
264 $headers[] = "Authorization: Bearer $key";
265
266 $data = [
267 "model" => $model, // Uses Configured Model (from Priority Chain)
268 "messages" => [["role" => "user", "content" => "Hello"]],
269 "max_tokens" => 5
270 ];
271 }
272
273 // Execute cURL
274 $ch = curl_init();
275 curl_setopt($ch, CURLOPT_URL, $url);
276 curl_setopt($ch, CURLOPT_POST, true);
277 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
278 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
279 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
280 curl_setopt($ch, CURLOPT_TIMEOUT, 10);
281 // Optional: Add SSL verification if behind a proxy with self-signed certs
282 // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
283
284 $result = curl_exec($ch);
285 $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
286 $err = curl_error($ch);
287 curl_close($ch);
288
289 if ($err) {
290 return ['success' => false, 'message' => "Curl Error: $err"];
291 }
292
293 if ($httpCode >= 200 && $httpCode < 300) {
294 return ['success' => true, 'message' => "OK (HTTP $httpCode)."];
295 } else {
296 $json = json_decode($result, true);
297 // Attempt to find the error message in various common structures
298 $msg = $json['error']['message'] ?? $json['message'] ?? substr($result, 0, 150);
299 return ['success' => false, 'message' => "HTTP $httpCode. Error: $msg"];
300 }
301}
302
319function ai_log_request($db, $user, $query, array $response, $provider, float $time, float $confidence, $status, $error = '', $rawReq = '', $rawRes = '')
320{
321 global $conf;
322
323 if (!getDolGlobalInt('AI_LOG_REQUESTS')) {
324 return 0;
325 }
326
327 $tool = isset($response['tool']) ? (string) $response['tool'] : '';
328
329 if (dol_strlen($rawReq) > 60000) {
330 $rawReq = dol_substr($rawReq, 0, 60000) . '... [Truncated]';
331 }
332
333 $rawResStr = (string) $rawRes;
334 if (dol_strlen($rawResStr) > 60000) {
335 $rawResStr = dol_substr($rawResStr, 0, 60000) . '... [Truncated]';
336 }
337
338 $sql = "INSERT INTO " . MAIN_DB_PREFIX . "ai_request_log (";
339 $sql .= "entity, date_request, fk_user, query_text, tool_name, provider, ";
340 $sql .= "execution_time, confidence, status, error_msg, raw_request_payload, raw_response_payload";
341 $sql .= ") VALUES (";
342 $sql .= ((int) $conf->entity) . ", ";
343 $sql .= "'" . $db->idate(dol_now()) . "', ";
344 $sql .= ((int) $user->id) . ", ";
345 $sql .= "'" . $db->escape($query) . "', ";
346 $sql .= "'" . $db->escape($tool) . "', ";
347 $sql .= "'" . $db->escape($provider) . "', ";
348 $sql .= ((float) $time) . ", ";
349 $sql .= ((float) $confidence) . ", ";
350 $sql .= "'" . $db->escape($status) . "', ";
351 $sql .= "'" . $db->escape($error) . "', ";
352 $sql .= "'" . $db->escape($rawReq) . "', ";
353 $sql .= "'" . $db->escape($rawResStr) . "'";
354 $sql .= ")";
355
356 $resql = $db->query($sql);
357 if (!$resql) {
359 }
360
361 return 0;
362}
363
370{
371 $arrayforaisummarize = array(
372 //'20_w' => 'SummarizeTwentyWords',
373 '50_w' => 'SummarizeFiftyWords',
374 '100_w' => 'SummarizeHundredWords',
375 '200_w' => 'SummarizeTwoHundredWords',
376 '1_p' => 'SummarizeOneParagraphs',
377 '2_p' => 'SummarizeTwoParagraphs',
378 '25_pc' => 'SummarizeTwentyFivePercent',
379 '50_pc' => 'SummarizeFiftyPercent',
380 '75_pc' => 'SummarizeSeventyFivePercent'
381 );
382
383 return $arrayforaisummarize;
384}
385
392{
393 $arrayforaierephrasestyle = array(
394 'spellchecker' => 'RephraseSpellChecker',
395 'professional' => 'RephraseStyleProfessional',
396 'humouristic' => 'RephraseStyleHumouristic',
397 );
398
399 return $arrayforaierephrasestyle;
400}
401
408{
409 global $langs, $conf;
410
411 $langs->load("agenda");
412
413 $h = 0;
414 $head = array();
415
416 $head[$h][0] = dol_buildpath("/ai/admin/setup.php", 1);
417 $head[$h][1] = $langs->trans("Settings");
418 $head[$h][2] = 'settings';
419 $h++;
420
421 $head[$h][0] = dol_buildpath("/ai/admin/custom_prompt.php", 1);
422 $head[$h][1] = $langs->trans("CustomPrompt");
423 $head[$h][2] = 'custom';
424 $h++;
425
426 if (getDolGlobalString("MAIN_FEATURES_LEVEL") >= 2) {
427 $head[$h][0] = dol_buildpath("/ai/admin/assistant.php", 1);
428 $head[$h][1] = $langs->trans("Assistant");
429 $head[$h][2] = 'assistant';
430 $h++;
431 }
432
433 if (getDolGlobalString("MAIN_FEATURES_LEVEL") >= 2) {
434 $head[$h][0] = dol_buildpath("/ai/admin/server_mcp.php", 1);
435 $head[$h][1] = $langs->trans("MCPServer");
436 $head[$h][2] = 'servermcp';
437 $h++;
438 }
439
440 if (getDolGlobalString("MAIN_FEATURES_LEVEL") >= 2) {
441 $head[$h][0] = dol_buildpath("/ai/admin/configure_tools.php", 1);
442 $head[$h][1] = $langs->trans("ToolAccessControl");
443 $head[$h][2] = 'tools';
444 $h++;
445 }
446
447 /*
448 $head[$h][0] = dol_buildpath("/ai/admin/myobject_extrafields.php", 1);
449 $head[$h][1] = $langs->trans("ExtraFields");
450 $head[$h][2] = 'myobject_extrafields';
451 $h++;
452 */
453
454 // Show more tabs from modules
455 // Entries must be declared in modules descriptor with line
456 //$this->tabs = array(
457 // 'entity:+tabname:Title:@ai:/ai/mypage.php?id=__ID__'
458 //); // to add new tab
459 //$this->tabs = array(
460 // 'entity:-tabname:Title:@ai:/ai/mypage.php?id=__ID__'
461 //); // to remove a tab
462 complete_head_from_modules($conf, $langs, null, $head, $h, 'ai@ai');
463
464 complete_head_from_modules($conf, $langs, null, $head, $h, 'ai@ai', 'remove');
465
466 return $head;
467}
468
478{
479 $serviceKey = getDolGlobalString('AI_API_SERVICE');
480 if (empty($serviceKey) || $serviceKey === '-1') {
481 return '';
482 }
483
484 $services = getListOfAIServices();
485
486 return isset($services[$serviceKey]['label']) ? (string) $services[$serviceKey]['label'] : (string) $serviceKey;
487}
488
496{
497 global $langs, $user;
498
499 $keys = array(
500 // General UI
501 'NoDataAvailable',
502 'Error',
503 'NoRecordFound',
504 'Download',
505 'Show',
506 'Confirm',
507 'ConfirmAiAction',
508 'ClearChatHistoryTitle',
509 'HistoryCleared',
510 'Send',
511 'TypeYourQuestion',
512
513 // Placeholders & Status
514 'TypeOrSpeak',
515 'UploadLocalDoc',
516 'UploadCloudDoc',
517 'DocLoaded',
518 'Listening',
519 'Transcribed',
520 'NoSpeech',
521 'ProcessingAudio',
522 'Timeout',
523 'Cancelled',
524
525 // Engine Specific
526 'CloudSpeechReady',
527 'WhisperReady',
528 'DownloadingModel',
529 'ModelLoading',
530
531 // Document Processing
532 'ProcessingFile',
533 'ReadingPdf',
534 'PdfError',
535 'UnsupportedFileType',
536 'TryingOCR',
537 'OcrProgress',
538 'SwitchingAIModel',
539 'OcrFailed',
540 'ReadingWord',
541 'ReadingExcel',
542 'ReadingOdf',
543
544 // Errors
545 'MicError',
546 'MicTooQuiet',
547 'ConnectionBlocked',
548 'ConnectionBlockedHelp',
549 'WorkerInitFailed',
550 'NetworkError',
551 'AIError',
552 'EmptyAIResponse',
553 'BrowserNotSupported',
554
555 // Actions & Dialogs
556 'YesProceed',
557 'Cancel',
558 'Submit',
559 'ActionCancelled',
560 'ExecutingTool',
561 'FetchingData',
562 'GeneratingLink',
563 'Found',
564 'TypeResponse',
565 'OpenVerb',
566
567 // Voice Confirmation
568 'VoiceYesNo',
569 'VoiceQuiet',
570 'PleaseRepeat',
571 'HeardText',
572
573 // Context
574 'DocContextIntro',
575 'DocContextOutro'
576 );
577
578 $ai_translations = array();
579 foreach ($keys as $key) {
580 $ai_translations[$key] = $langs->transnoentitiesnoconv($key);
581 }
582 $ai_translations['DownloadPdf'] = $langs->transnoentitiesnoconv("Download").' PDF';
583 $ai_translations['CloudVoiceRequiresSecureContext'] = $langs->trans(
584 "CloudVoiceRequiresSecureContext",
585 "HTTPS",
586 "localhost",
587 "Whisper"
588 );
589
590 // First letter of the current user name, shown in the "user" message avatar
591 $userinitial = '';
592 if (is_object($user)) {
593 $namesource = $user->firstname ? $user->firstname : ($user->login ? $user->login : '');
594 $userinitial = dol_strtoupper(dol_substr($namesource, 0, 1));
595 }
596
597 return array(
598 'mode' => getDolGlobalString('AI_DEFAULT_INPUT_MODE'),
599 'labels' => $ai_translations,
600 // Endpoints are called with absolute URLs so the chat also works when
601 // injected into another page (topbar popover) and not only when served
602 // from /ai/assistant/index.php.
603 'baseUrl' => dol_buildpath('/ai/assistant/', 1),
604 'token' => newToken(),
605 'userInitial' => $userinitial,
606 );
607}
608
617function getAiChatAssistantHtml($mode = 'page')
618{
619 global $langs, $user;
620
621 $out = '';
622
623 // Config travels as a data attribute: <script> tags injected via innerHTML
624 // are never executed by the browser, so a window.AI_CONFIG inline script
625 // would not work for the AJAX-loaded popover.
626 $out .= '<div class="ai-chat-container'.($mode === 'popover' ? ' ai-in-popover' : '').'"';
627 $out .= ' data-ai-config="'.dol_escape_htmltag(json_encode(getAiChatAssistantConfig())).'"';
628 if ($mode === 'page') {
629 $out .= ' data-ai-autoinit="1"';
630 }
631 $out .= '>';
632
633 // Header
634 $out .= '<div class="chat-header">';
635 if ($mode === 'popover') {
636 // In the popover the title links to the full standalone page
637 $title = img_picto('', 'fa-robot', '', 0, 0, 0, '', 'paddingright').$langs->trans("AIAssistant");
638 $title = '<a href="'.dol_buildpath('/ai/assistant/index.php', 1).'" class="ai-header-link" title="'.dol_escape_htmltag($langs->trans("AIOpenFullPage")).'">'.$title.'</a>';
639 $out .= '<h2>'.$title.'</h2>';
640 } else {
641 // Full page: assistant identity (avatar + title + current LLM model)
642 $out .= '<div class="chat-header-id">';
643 $out .= '<span class="chat-header-avatar">'.img_picto('', 'fa-robot').'</span>';
644 $out .= '<span class="chat-header-text">';
645 $out .= '<span class="chat-header-title">'.$langs->trans("AIAssistant").'</span>';
646 $aiprovider = getAiAssistantProviderLabel();
647 if ($aiprovider !== '') {
648 $out .= '<span class="chat-header-status" title="'.dol_escape_htmltag($langs->trans("AIProviderInUse")).'">'.dol_escape_htmltag($aiprovider).'</span>';
649 }
650 $out .= '</span>';
651 $out .= '</div>';
652 }
653 $out .= '<div class="header-controls">';
654 // Engine Switcher (restyled as a pill with a sparkle icon)
655 $out .= '<select id="engine-select" class="engine-select">';
656 $out .= '<option value="text">'.$langs->transnoentitiesnoconv("OptionTextOnly").'</option>';
657 $out .= '<option value="cloud">'.$langs->transnoentitiesnoconv("OptionCloudFast").'</option>';
658 $out .= '<option value="whisper">'.$langs->transnoentitiesnoconv("OptionWhisperLocal").'</option>';
659 $out .= '<option value="local_docs">'.$langs->transnoentitiesnoconv("OptionLocalParsing").'</option>';
660 $out .= '<option value="cloud_docs">'.$langs->transnoentitiesnoconv("OptionCloudParsing").'</option>';
661 $out .= '</select>';
662 // Clear Button
663 $out .= '<button type="button" id="clear-btn" class="icon-btn" title="'.dol_escape_htmltag($langs->trans("ClearChatHistoryTitle")).'">';
664 $out .= img_picto('', 'fa-trash').' <span class="ai-btn-label">'.$langs->trans("Clear").'</span>';
665 $out .= '</button>';
666 if ($mode === 'popover') {
667 // Window controls of the popover (handled by the bootstrap JS in main.inc.php)
668 $out .= '<button type="button" id="ai-expand-btn" class="icon-btn ai-window-btn" title="'.dol_escape_htmltag($langs->trans("AIExpandPanel")).'" data-title-expand="'.dol_escape_htmltag($langs->trans("AIExpandPanel")).'" data-title-reduce="'.dol_escape_htmltag($langs->trans("AIReducePanel")).'"><i class="fa fa-expand-alt"></i></button>';
669 $out .= '<button type="button" id="ai-close-btn" class="icon-btn ai-window-btn" title="'.dol_escape_htmltag($langs->trans("Close")).'"><i class="fa fa-times"></i></button>';
670 }
671 $out .= '</div>';
672 $out .= '</div>';
673
674 // Chat History
675 $out .= '<div id="chat-history" class="chat-history">';
676 if ($mode === 'popover') {
677 // Compact greeting line for the narrow popover
678 $out .= '<div class="msg system">'.$langs->trans("AIWelcomeMessage").'</div>';
679 } else {
680 // Full page: rich empty-state welcome screen (hidden by JS as soon as the
681 // conversation starts, restored on Clear). Quick cards send a localized
682 // ready-made prompt on click (data-prompt).
683 $welcomename = $user->firstname ? $user->firstname : (is_object($user) ? $user->login : '');
684 $quickcards = array(
685 array('icon' => 'fa-file-invoice-dollar', 'key' => 'Invoices'),
686 array('icon' => 'fa-chart-line', 'key' => 'Revenue'),
687 array('icon' => 'fa-coins', 'key' => 'Finance'),
688 array('icon' => 'fa-warehouse', 'key' => 'Inventory'),
689 );
690 $out .= '<div class="chat-welcome">';
691 $out .= '<div class="chat-welcome-avatar">'.img_picto('', 'fa-robot').'</div>';
692 $out .= '<h2 class="chat-welcome-title">'.dol_escape_htmltag($langs->trans("AIGreeting", $welcomename)).'</h2>';
693 $out .= '<p class="chat-welcome-subtitle">'.dol_escape_htmltag($langs->trans("AIGreetingSubtitle")).'</p>';
694 $out .= '<div class="chat-welcome-actions">';
695 foreach ($quickcards as $card) {
696 $out .= '<button type="button" class="ai-quick-card" data-prompt="'.dol_escape_htmltag($langs->transnoentitiesnoconv("AIQuick".$card['key']."Prompt")).'">';
697 $out .= '<span class="ai-quick-icon">'.img_picto('', $card['icon']).'</span>';
698 $out .= '<span class="ai-quick-text">';
699 $out .= '<span class="ai-quick-title">'.dol_escape_htmltag($langs->trans("AIQuick".$card['key']."Title")).'</span>';
700 $out .= '<span class="ai-quick-desc">'.dol_escape_htmltag($langs->trans("AIQuick".$card['key']."Desc")).'</span>';
701 $out .= '</span>';
702 $out .= '</button>';
703 }
704 $out .= '</div>';
705 $out .= '</div>';
706 }
707 $out .= '</div>';
708
709 // Controls: a single rounded "pill" holding the attach/mic buttons, the
710 // textarea and the send button.
711 $out .= '<div class="chat-controls">';
712 $out .= '<div class="chat-input-pill">';
713 // Upload Wrapper (Visible only in Doc modes)
714 $out .= '<div id="upload-wrapper" class="upload-wrapper hidden">';
715 $out .= '<input type="file" id="file-upload" accept=".pdf,.txt,.xml,.png,.jpg,.jpeg,.doc,.docx,.xls,.xlsx,.odt,.ods" style="display: none;">';
716 $out .= '<button type="button" id="upload-btn" class="round-btn" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("AttachFile")).'">'.img_picto('', 'fa-paperclip').'</button>';
717 $out .= '</div>';
718 // Microphone Wrapper (Visible only in Voice modes)
719 $out .= '<div id="mic-wrapper" class="mic-wrapper hidden">';
720 $out .= '<button type="button" id="mic-btn" class="round-btn mic-btn" title="'.dol_escape_htmltag($langs->trans("ToggleMicrophone")).'">'.img_picto('', 'fa-microphone').'</button>';
721 $out .= '</div>';
722 // Text Input
723 $out .= '<textarea id="user-input" class="ia-input" rows="1" placeholder="'.dol_escape_htmltag($langs->trans("TypeYourQuestion")).'" autocomplete="off" spellcheck="false"></textarea>';
724 // Send Button
725 $out .= '<button type="button" id="send-btn" class="chat-send-btn" title="'.dol_escape_htmltag($langs->trans("SendPrompt")).'">'.img_picto('', 'fa-paper-plane').'</button>';
726 $out .= '</div>';
727 $out .= '</div>';
728
729 $out .= '<div id="status-bar"></div>';
730
731 $out .= '</div>';
732
733 return $out;
734}
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
getListOfAIFeatures()
Prepare admin pages header.
Definition ai.lib.php:36
aiAdminPrepareHead()
Prepare admin pages header.
Definition ai.lib.php:407
getListOfAIServices()
Get list of available ai services.
Definition ai.lib.php:68
ai_log_request($db, $user, $query, array $response, $provider, float $time, float $confidence, $status, $error='', $rawReq='', $rawRes='')
Log AI Request with Raw Payloads.
Definition ai.lib.php:319
getAiChatAssistantConfig()
Build the configuration array consumed by the AI Assistant chat frontend (ai/js/ai_assistant....
Definition ai.lib.php:495
getListForAIRephraseStyle()
Get list for AI style of writing.
Definition ai.lib.php:391
getAiAssistantProviderLabel()
Resolve the AI provider/service currently configured for the AI Assistant (e.g.
Definition ai.lib.php:477
getListForAISummarize()
Get list for AI summarize.
Definition ai.lib.php:369
getAiChatAssistantHtml($mode='page')
Build the HTML of the AI Assistant chat interface.
Definition ai.lib.php:617
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.
dol_now($mode='gmt')
Return date for now.
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)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_strtoupper($string, $encoding="UTF-8")
Convert a string to upper.
dol_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
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...
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
buildzip.php