dolibarr 25.0.0-alpha
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 * Copyright (C) 2026 Jose Martinez <jose.martinez@pichinov.com>
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
29include_once DOL_DOCUMENT_ROOT.'/ai/class/ai.class.php';
30
31
38{
39 global $langs;
40
41 $arrayofaifeatures = array(
42 'textgenerationemail' => array('label' => $langs->trans('TextGeneration').' ('.$langs->trans("EmailContent").')', 'picto' => '', 'status' => 'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_EMAIL),
43 'textgenerationwebpage' => array('label' => $langs->trans('TextGeneration').' ('.$langs->trans("WebsitePage").')', 'picto' => '', 'status' => 'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_WEBPAGE),
44 'textgeneration' => array('label' => $langs->trans('TextGeneration').' ('.$langs->trans("Other").')', 'picto' => '', 'status' => 'notused', 'function' => 'TEXT'),
45
46 'texttranslation' => array('label' => $langs->trans('TextTranslation'), 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_TEXT_TRANSLATION),
47 'textsummarize' => array('label' => $langs->trans('TextSummarize'), 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_TEXT_SUMMARIZE),
48 'textspellchecker' => array('label' => $langs->trans('TextSpellChecker'), 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_TEXT_SPELLCHECKER),
49 'textrephraser' => array('label' => $langs->trans('TextRephraser'), 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_TEXT_REPHRASER),
50
51 'textgenerationextrafield' => array('label' => $langs->trans('TextGeneration').' ('.$langs->trans("ExtrafieldFiller").')', 'picto' => '', 'status'=>'dolibarr', 'function' => 'TEXT', 'placeholder' => Ai::AI_DEFAULT_PROMPT_FOR_EXTRAFIELD_FILLER),
52
53 'imagegeneration' => array('label' => 'ImageGeneration', 'picto' => '', 'status' => 'notused', 'function' => 'IMAGE'),
54 'videogeneration' => array('label' => 'VideoGeneration', 'picto' => '', 'status' => 'notused', 'function' => 'VIDEO'),
55 'audiogeneration' => array('label' => 'AudioGeneration', 'picto' => '', 'status' => 'notused', 'function' => 'AUDIO'),
56 'transcription' => array('label' => 'AudioTranscription', 'picto' => '', 'status' => 'notused', 'function' => 'TRANSCRIPT'),
57 'translation' => array('label' => 'AudioTranslation', 'picto' => '', 'status' => 'notused', 'function' => 'TRANSLATE'),
58 'docparsing' => array('label' => 'DocumentParsing', 'picto' => '', 'status' => 'experimental', 'function' => 'DOCPARSING')
59 );
60
61 return $arrayofaifeatures;
62}
63
70{
71 global $langs;
72
73 $arrayofai = array(
74 '-1' => array('label' => $langs->trans('SelectAService')),
75 'chatgpt' => array(
76 'label' => 'ChatGPT (OpenAI)',
77 'url' => 'https://api.openai.com/v1/',
78 'setup' => 'https://platform.openai.com/account/api-keys',
79 'textgeneration' => array('default' => 'gpt-5.6'), // updated Oct 2026
80 'imagegeneration' => array('default' => 'gpt-image-2'), // Replaced DALL-E 3; 4x faster and native to GPT-5
81 'audiogeneration' => array('default' => 'gpt-audio-1.5'), // New Feb 23, 2026 release for high-fidelity audio out
82 'videogeneration' => array('default' => 'sora-2'), // OpenAI's standard API video model
83 'transcription' => array('default' => 'gpt-transcribe'), // Dedicated model
84 'translation' => array('default' => 'gpt-5.6'), // Still the best for multi-language audio translation
85 'docparsing' => array('default' => 'gpt-5.6'), // Uses the new Responses API / Vision capabilities
86 'adapter_type' => 'openai'
87 ),
88 'groq' => array(
89 'label' => 'Groq (LPU Inference)',
90 'url' => 'https://api.groq.com/openai/v1/',
91 'setup' => 'https://console.groq.com/keys',
92 'textgeneration' => array('default' => 'llama-4-8b-instant'), // February 2026 flagship for extreme speed (1,000+ t/s)
93 'imagegeneration' => array('default' => 'na'),
94 'audiogeneration' => array('default' => 'na'),
95 'videogeneration' => array('default' => 'na'),
96 'transcription' => array('default' => 'whisper-large-v3-turbo'), // Groq's specialized high-speed Whisper implementation
97 'translation' => array('default' => 'whisper-large-v3-turbo'), // High-speed audio translation to English
98 'docparsing' => array('default' => 'llama-4-70b-versatile'), // Best for structured data extraction from text
99 'adapter_type' => 'openai'
100 ),
101 'mistral' => array(
102 'label' => 'Mistral AI',
103 'url' => 'https://api.mistral.ai/v1/',
104 'setup' => 'https://console.mistral.ai/api-keys/',
105 'textgeneration' => array('default' => 'mistral-medium-latest', 'examples' => 'mistral-tiny-latest, mistral-small-latest, mistral-medium-latest, mistral-large-latest'), // Points to Mistral Small 3 (updated Feb 2026)
106 'imagegeneration' => array('default' => 'na'),
107 'audiogeneration' => array('default' => 'na'),
108 'videogeneration' => array('default' => 'na'),
109 'transcription' => array('default' => 'na'),
110 'translation' => array('default' => 'na'),
111 'docparsing' => array('default' => 'mistral-ocr-latest'), // Model of the Mistral OCR endpoint, used to parse documents (PDF, images, ...)
112 'adapter_type' => 'openai'
113 ),
114 'deepseek' => array(
115 'label' => 'DeepSeek',
116 'url' => 'https://api.deepseek.com',
117 'setup' => 'https://platform.deepseek.com/api_keys',
118 'textgeneration' => array('default' => 'deepseek-v4'), // Released Feb 2026, flagship MoE model
119 'imagegeneration' => array('default' => 'deepseek-janus-2'), // DeepSeek's latest multimodal vision/gen model
120 'audiogeneration' => array('default' => 'na'),
121 'videogeneration' => array('default' => 'na'),
122 'transcription' => array('default' => 'na'),
123 'translation' => array('default' => 'na'),
124 'docparsing' => array('default' => 'deepseek-v4'), // Massive 1M context support for parsing
125 'adapter_type' => 'openai'
126 ),
127 'perplexity' => array(
128 'label' => 'Perplexity (Sonar)',
129 'url' => 'https://api.perplexity.ai',
130 'setup' => 'https://www.perplexity.ai/settings/api',
131 'textgeneration' => array('default' => 'sonar-pro'), // Flagship search model as of Feb 2026
132 'imagegeneration' => array('default' => 'na'),
133 'audiogeneration' => array('default' => 'na'),
134 'videogeneration' => array('default' => 'na'),
135 'transcription' => array('default' => 'na'),
136 'translation' => array('default' => 'na'),
137 'docparsing' => array('default' => 'sonar-reasoning'), // Best for analyzing search-grounded docs
138 'adapter_type' => 'openai'
139 ),
140 'zai' => array(
141 'label' => 'Zhipu AI (GLM)',
142 'url' => 'https://api.z.ai/api/paas/v4',
143 'setup' => 'https://docs.z.ai/guides/overview/quick-start',
144 'textgeneration' => array('default' => 'glm-5'), // Flagship released February 11, 2026
145 'imagegeneration' => array('default' => 'cogview-4'), // Zhipu's latest SOTA image generator
146 'audiogeneration' => array('default' => 'cogvlm2-audio'), // High-fidelity conversational audio
147 'videogeneration' => array('default' => 'cogvideox-2'), // Flagship API video model
148 'transcription' => array('default' => 'na'),
149 'translation' => array('default' => 'na'),
150 'docparsing' => array('default' => 'glm-5'), // Top-tier agentic document processing
151 'adapter_type' => 'openai'
152 ),
153 'custom' => array(
154 'label' => 'Custom',
155 'url' => 'https://domainofapi.com/v1/',
156 'setup' => 'Ask your AI provider how to get your API key',
157 'textgeneration' => array('default' => 'tinyllama-1.1b'),
158 'imagegeneration' => array('default' => 'mixtral-8x7b-32768'),
159 'audiogeneration' => array('default' => 'mixtral-8x7b-32768'),
160 'videogeneration' => array('default' => 'na'),
161 'transcription' => array('default' => 'mixtral-8x7b-32768'),
162 'translation' => array('default' => 'mixtral-8x7b-32768'),
163 'docparsing' => array('default' => 'na'),
164 'adapter_type' => 'openai'
165 ),
166 // --- SPECIALIZED ADAPTERS ---
167 'anthropic' => array(
168 'label' => 'Anthropic (Claude)',
169 'url' => 'https://api.anthropic.com/v1/',
170 'setup' => 'https://console.anthropic.com/',
171 'textgeneration' => array('default' => 'claude-opus-5'), // Current Anthropic flagship; 1M context window
172 'imagegeneration' => array('default' => 'na'), // Anthropic remains focused on text/code logic
173 'audiogeneration' => array('default' => 'na'),
174 'videogeneration' => array('default' => 'na'),
175 'transcription' => array('default' => 'na'),
176 'translation' => array('default' => 'na'),
177 'docparsing' => array('default' => 'claude-opus-5'), // Leading model for "Computer Use" and PDF analysis
178 'adapter_type' => 'anthropic'
179 ),
180 'google' => array(
181 'label' => 'Google Gemini',
182 'url' => 'https://generativelanguage.googleapis.com/v1beta/',
183 'setup' => 'https://aistudio.google.com/',
184 'textgeneration' => array('default' => 'gemini-3.1-pro-preview'), // Flagship reasoning model released Feb 19, 2026
185 'imagegeneration' => array('default' => 'nano-banana-pro'), // Latest SOTA image model (Gemini 3 Pro Image)
186 'audiogeneration' => array('default' => 'gemini-2.5-pro-tts'), // High-fidelity native speech synthesis
187 'videogeneration' => array('default' => 'veo-3.1'), // Google's flagship cinematic video API
188 'transcription' => array('default' => 'gemini-3.1-pro-preview'), // Native multi-modal audio reasoning
189 'translation' => array('default' => 'gemini-3.1-pro-preview'), // Native audio-to-text translation
190 'docparsing' => array('default' => 'gemini-3.1-pro-preview'), // Massive 2M+ context window for full repo parsing
191 'adapter_type' => 'google'
192 )
193 );
194
195 return $arrayofai;
196}
197
213function testAIConnection(string $service, string $key, string $url): array
214{
215 if (empty($key)) {
216 return ['success' => false, 'message' => 'API Key is empty'];
217 }
218
219 // Load Defaults (Ensure this function exists or handle the error)
220 if (!function_exists('getListOfAIServices')) {
221 return ['success' => false, 'message' => 'Configuration helper function missing.'];
222 }
223
224 $list = getListOfAIServices();
225 $defUrl = $list[$service]['url'] ?? '';
226 // Use model from config, fallback to hardcoded if necessary
227 $defaultModel = $list[$service]['model'] ?? 'unknown';
228
229 // Normalize URL
230 if (empty($url)) {
231 $url = $defUrl;
232 }
233 $url = rtrim($url, '/');
234
235 $data = [];
236 $headers = ["Content-Type: application/json"];
237
238 $model = '';
239 if (empty($model)) {
240 $model = getDolGlobalString('AI_API_' . strtoupper($service) . '_MODEL_TEXT');
241 }
242
243 // GOOGLE
244 if ($service == 'google' || strpos($url, 'googleapis') !== false) {
245 if (strpos($url, ':generateContent') === false) {
246 if (strpos($url, 'models') === false) {
247 $url .= "/models/$model:generateContent";
248 } else {
249 $url .= "/$model:generateContent";
250 }
251 }
252 $url .= "?key=" . $key;
253 $data = ["contents" => [ ["parts" => [ ["text" => "Hello"] ] ] ], "generationConfig" => ["maxOutputTokens" => 5]];
254 } elseif ($service == 'anthropic' || strpos($url, 'anthropic') !== false) { // ANTHROPIC
255 if (strpos($url, 'messages') === false) $url .= '/messages';
256 $headers[] = "x-api-key: $key";
257 $headers[] = "anthropic-version: 2023-06-01";
258 $data = [
259 "model" => $model, // Uses Configured Model
260 "messages" => [["role" => "user", "content" => "Hello"]],
261 "max_tokens" => 5
262 ];
263 } else {
264 if (strpos($url, '/chat/completions') === false) $url .= '/chat/completions';
265 $headers[] = "Authorization: Bearer $key";
266
267 $data = [
268 "model" => $model, // Uses Configured Model (from Priority Chain)
269 "messages" => [["role" => "user", "content" => "Hello"]],
270 "max_tokens" => 5
271 ];
272 }
273
274 // Execute request with the Dolibarr HTTP wrapper (handles proxy, SSL and logging)
275 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
276
277 // By default, we accept only external endpoints ($dolibarr_ai_allow_local_endpoints is not set).
278 // To allow local endpoints, we must set $dolibarr_ai_allow_local_endpoints to 1 or 2 in conf.php.
279 global $dolibarr_ai_allow_local_endpoints;
280 $localurl = empty($dolibarr_ai_allow_local_endpoints) ? 0 : 2;
281
282 $result = getURLContent($url, 'POST', json_encode($data), 1, $headers, array('http', 'https'), $localurl, -1, 0, 10);
283 $httpCode = (int) ($result['http_code'] ?? 0);
284 $responseContent = (string) ($result['content'] ?? '');
285
286 if (!empty($result['curl_error_no'])) {
287 return ['success' => false, 'message' => "Curl Error: ".($result['curl_error_msg'] ?? '')];
288 }
289
290 if ($httpCode >= 200 && $httpCode < 300) {
291 return ['success' => true, 'message' => "OK (HTTP $httpCode)."];
292 } else {
293 $json = json_decode($responseContent, true);
294 // Attempt to find the error message in various common structures
295 $msg = $json['error']['message'] ?? $json['message'] ?? substr($responseContent, 0, 150);
296 return ['success' => false, 'message' => "HTTP $httpCode. Error: $msg"];
297 }
298}
299
300
317function ai_validate_attachments(array $attachments, &$error)
318{
319 global $langs;
320
321 $error = '';
322 if (empty($attachments)) {
323 return true;
324 }
325 $langs->load("other"); // owns the AIAttachment* keys; callers load it later or not at all
326
327 if (getDolGlobalInt('AI_PRIVACY_REDACTION', 0)) {
328 $error = $langs->trans("AIAttachmentBlockedByPrivacy");
329
330 return false;
331 }
332
333 // Cap the number of attachments server-side too: the chat enforces it
334 // client-side only, and other callers may not. 0 means unlimited.
335 $maxfiles = getDolGlobalInt('AI_ATTACHMENT_MAX_FILES', 5);
336 if ($maxfiles > 0 && count($attachments) > $maxfiles) {
337 $error = $langs->trans("AIAttachmentTooMany", (string) $maxfiles);
338
339 return false;
340 }
341
342 $allowedmimes = array('application/pdf', 'image/png', 'image/jpeg', 'image/gif', 'image/webp');
343 // HEIC/HEIF reach this point only through the native-send fallback of the
344 // chat (browser unable to transcode): acceptable solely when the active
345 // provider consumes them (Gemini); other providers 400 on the MIME.
346 if ((getListOfAIServices()[getDolGlobalString('AI_API_SERVICE')]['adapter_type'] ?? '') === 'google') {
347 $allowedmimes[] = 'image/heic';
348 $allowedmimes[] = 'image/heif';
349 }
350 $maxbytes = getDolGlobalInt('AI_ATTACHMENT_MAX_MB', 10) * 1024 * 1024;
351 $totalbytes = 0;
352 foreach ($attachments as $att) {
353 if (!in_array($att['mime'], $allowedmimes, true)) {
354 $error = $langs->trans("AIAttachmentTypeNotAllowed", $att['mime']);
355
356 return false;
357 }
358 // 3/4 ratio: decoded size of a base64 payload without decoding it.
359 $bytes = (int) (strlen($att['data']) * 3 / 4);
360 $totalbytes += $bytes;
361 if ($bytes > $maxbytes || $totalbytes > $maxbytes) {
362 $error = $langs->trans("AIAttachmentTooLarge", (string) getDolGlobalInt('AI_ATTACHMENT_MAX_MB', 10));
363
364 return false;
365 }
366 }
367
368 return true;
369}
370
383function aiTruncateForLog($text, $max = 60000)
384{
385 $len = dol_strlen($text);
386 if ($len <= $max) {
387 return $text;
388 }
389 $head = (int) floor($max / 2);
390 $tail = $max - $head;
391
392 return dol_substr($text, 0, $head)
393 ."\n... [Truncated ".($len - $max)." chars] ...\n"
394 .dol_substr($text, $len - $tail, $tail);
395}
396
416function ai_log_request($db, $user, $query, array $response, $provider, float $time, float $confidence, $status, $error = '', $rawReq = '', $rawRes = '', array $context = array(), &$logId = null)
417{
418 global $conf;
419
420 $logId = 0;
421
422 if (!getDolGlobalInt('AI_LOG_REQUESTS')) {
423 return 0;
424 }
425
426 $tool = isset($response['tool']) ? (string) $response['tool'] : '';
427
428 // Keep both ends when trimming: a request payload starts with the tool
429 // schemas (tens of kB, identical on every call) and ends with the system
430 // rules, the user query and the page context - the part anyone reads a
431 // log for. Cutting only the tail threw exactly that away.
432 // Structured tool output must remain valid JSON for subsequent reads.
433 $rawResStr = (string) $rawRes;
434 if (empty($context['preserve_payloads'])) {
435 $rawReq = aiTruncateForLog($rawReq, 60000);
436 $rawResStr = aiTruncateForLog($rawResStr, 60000);
437 }
438
439 $sql = "INSERT INTO " . $db->prefix() . "ai_request_log (";
440 $sql .= "entity, date_request, fk_user, query_text, tool_name, provider, ";
441 $sql .= "execution_time, confidence, status, error_msg, raw_request_payload, raw_response_payload";
442 // Each optional group keys on ITS OWN entries, so a caller passing only
443 // token usage does not drag empty audit hashes along, and vice versa.
444 $hasAudit = isset($context['fk_actioncomm']) || isset($context['input_hash']) || isset($context['output_hash']) || isset($context['security_hash']);
445 $hasUsage = isset($context['tokens_input']) || isset($context['tokens_output']) || isset($context['model']);
446 if ($hasAudit) {
447 $sql .= ", fk_actioncomm, input_hash, output_hash, security_hash";
448 }
449 if ($hasUsage) {
450 $sql .= ", tokens_input, tokens_output, model";
451 }
452 $sql .= ") VALUES (";
453 $sql .= ((int) $conf->entity) . ", ";
454 $sql .= "'" . $db->idate(dol_now()) . "', ";
455 $sql .= ((int) $user->id) . ", ";
456 $sql .= "'" . $db->escape($query) . "', ";
457 $sql .= "'" . $db->escape($tool) . "', ";
458 $sql .= "'" . $db->escape($provider) . "', ";
459 $sql .= ((float) $time) . ", ";
460 $sql .= ((float) $confidence) . ", ";
461 $sql .= "'" . $db->escape($status) . "', ";
462 $sql .= "'" . $db->escape($error) . "', ";
463 $sql .= "'" . $db->escape($rawReq) . "', ";
464 $sql .= "'" . $db->escape($rawResStr) . "'";
465 if ($hasAudit) {
466 $sql .= ", ".(!empty($context['fk_actioncomm']) && $context['fk_actioncomm'] > 0 ? (int) $context['fk_actioncomm'] : 'NULL');
467 $sql .= ", '".$db->escape($context['input_hash'] ?? '')."'";
468 $sql .= ", '".$db->escape($context['output_hash'] ?? '')."'";
469 $sql .= ", '".$db->escape($context['security_hash'] ?? '')."'";
470 }
471 if ($hasUsage) {
472 $sql .= ", ".(isset($context['tokens_input']) ? (int) $context['tokens_input'] : 'NULL');
473 $sql .= ", ".(isset($context['tokens_output']) ? (int) $context['tokens_output'] : 'NULL');
474 $sql .= ", '".$db->escape((string) ($context['model'] ?? ''))."'";
475 }
476 $sql .= ")";
477
478 $resql = $db->query($sql);
479 if (!$resql) {
480 dol_syslog(__FUNCTION__.": ".$db->lasterror(), LOG_ERR);
481 } else {
482 $logId = (int) $db->last_insert_id($db->prefix()."ai_request_log");
483 }
484
485 return 0;
486}
487
494{
495 $arrayforaisummarize = array(
496 //'20_w' => 'SummarizeTwentyWords',
497 '50_w' => 'SummarizeFiftyWords',
498 '100_w' => 'SummarizeHundredWords',
499 '200_w' => 'SummarizeTwoHundredWords',
500 '1_p' => 'SummarizeOneParagraphs',
501 '2_p' => 'SummarizeTwoParagraphs',
502 '25_pc' => 'SummarizeTwentyFivePercent',
503 '50_pc' => 'SummarizeFiftyPercent',
504 '75_pc' => 'SummarizeSeventyFivePercent'
505 );
506
507 return $arrayforaisummarize;
508}
509
516{
517 $arrayforaierephrasestyle = array(
518 'spellchecker' => 'RephraseSpellChecker',
519 'professional' => 'RephraseStyleProfessional',
520 'humouristic' => 'RephraseStyleHumouristic',
521 );
522
523 return $arrayforaierephrasestyle;
524}
525
532function getAIErrorMessage($result)
533{
534 global $langs;
535
536 if (!empty($result['curl_error_no']) && $result['curl_error_no'] == 28) { // CURLE_OPERATION_TIMEDOUT
537 $langs->load("errors");
538 return $langs->transnoentitiesnoconv("ErrorAIServiceTimeout", (empty($result['model']) ? (empty($result['service']) ? '?' : $result['service']) : $result['model']));
539 }
540 return (empty($result['message']) ? 'Unknown error from AI service' : $result['message']);
541}
542
549{
550 global $langs, $conf;
551
552 $langs->load("agenda");
553
554 $h = 0;
555 $head = array();
556
557 $head[$h][0] = dol_buildpath("/ai/admin/setup.php", 1);
558 $head[$h][1] = $langs->trans("Settings");
559 $head[$h][2] = 'settings';
560 $h++;
561
562 $head[$h][0] = dol_buildpath("/ai/admin/custom_prompt.php", 1);
563 $head[$h][1] = $langs->trans("CustomPrompt");
564 $head[$h][2] = 'custom';
565 $h++;
566
567 if (getDolGlobalString("MAIN_FEATURES_LEVEL") >= 1) {
568 $head[$h][0] = dol_buildpath("/ai/admin/assistant.php", 1);
569 $head[$h][1] = $langs->trans("Assistant");
570 $head[$h][2] = 'assistant';
571 $h++;
572 }
573
574 if (getDolGlobalString("MAIN_FEATURES_LEVEL") >= 1) {
575 $head[$h][0] = dol_buildpath("/ai/admin/server_mcp.php", 1);
576 $head[$h][1] = $langs->trans("MCPServer");
577 $head[$h][2] = 'servermcp';
578 $h++;
579 }
580
581 if (getDolGlobalString("MAIN_FEATURES_LEVEL") >= 1) {
582 $head[$h][0] = dol_buildpath("/ai/admin/configure_tools.php", 1);
583 $head[$h][1] = $langs->trans("ToolAccessControl");
584 $head[$h][2] = 'tools';
585 $h++;
586 }
587
588 /*
589 $head[$h][0] = dol_buildpath("/ai/admin/myobject_extrafields.php", 1);
590 $head[$h][1] = $langs->trans("ExtraFields");
591 $head[$h][2] = 'myobject_extrafields';
592 $h++;
593 */
594
595 // Show more tabs from modules
596 // Entries must be declared in modules descriptor with line
597 //$this->tabs = array(
598 // 'entity:+tabname:Title:@ai:/ai/mypage.php?id=__ID__'
599 //); // to add new tab
600 //$this->tabs = array(
601 // 'entity:-tabname:Title:@ai:/ai/mypage.php?id=__ID__'
602 //); // to remove a tab
603 complete_head_from_modules($conf, $langs, null, $head, $h, 'ai@ai');
604
605 complete_head_from_modules($conf, $langs, null, $head, $h, 'ai@ai', 'remove');
606
607 return $head;
608}
609
619{
620 $serviceKey = getDolGlobalString('AI_API_SERVICE');
621 if (empty($serviceKey) || $serviceKey === '-1') {
622 return '';
623 }
624
625 $services = getListOfAIServices();
626
627 return isset($services[$serviceKey]['label']) ? (string) $services[$serviceKey]['label'] : (string) $serviceKey;
628}
629
637{
638 $serviceKey = getDolGlobalString('AI_API_SERVICE');
639 if (empty($serviceKey) || $serviceKey === '-1') {
640 return '';
641 }
642
643 $services = getListOfAIServices();
644 $prefix = 'AI_API_'.strtoupper($serviceKey);
645 $model = getDolGlobalString($prefix.'_MODEL_TEXT')
646 ?: getDolGlobalString($prefix.'_MODEL')
647 ?: ($services[$serviceKey]['textgeneration']['default'] ?? '');
648
649 return is_string($model) ? $model : '';
650}
651
659{
660 $serviceKey = getDolGlobalString('AI_API_SERVICE');
661 if (empty($serviceKey) || $serviceKey === '-1') {
662 return '';
663 }
664
665 $services = getListOfAIServices();
666
667 return (string) (getDolGlobalString('AI_API_'.strtoupper($serviceKey).'_URL') ?: ($services[$serviceKey]['url'] ?? ''));
668}
669
681function getAiProviderModelList($db, $forcerefresh = false)
682{
683 global $conf;
684
685 $serviceKey = getDolGlobalString('AI_API_SERVICE');
686 if (empty($serviceKey) || $serviceKey == '-1') {
687 return array('service' => '', 'models' => array());
688 }
689
690 if (!$forcerefresh) {
691 $cacheraw = getDolGlobalString('AI_MODELS_LIST_CACHE');
692 if ($cacheraw) {
693 $cache = json_decode($cacheraw, true);
694 if (is_array($cache) && !empty($cache['service']) && $cache['service'] === $serviceKey
695 && !empty($cache['ts']) && (dol_now() - (int) $cache['ts']) < 3600
696 && !empty($cache['models']) && is_array($cache['models'])) {
697 return array('service' => $serviceKey, 'models' => $cache['models']);
698 }
699 }
700 }
701
702 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
703 include_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
704
705 $servicesList = getListOfAIServices();
706 $adapterType = $servicesList[$serviceKey]['adapter_type'] ?? 'openai';
707 $defUrl = $servicesList[$serviceKey]['url'] ?? '';
708 $baseUrl = rtrim(getDolGlobalString('AI_API_'.strtoupper($serviceKey).'_URL') ?: $defUrl, '/');
709
710 $apiKey = getDolGlobalString('AI_API_'.strtoupper($serviceKey).'_KEY');
711 if (preg_match('/^crypt:/', $apiKey)) {
712 $apiKey = dolDecrypt($apiKey, $conf->file->instance_unique_id);
713 }
714 if (empty($apiKey) || empty($baseUrl)) {
715 return array('service' => $serviceKey, 'models' => array());
716 }
717
718 $models = array();
719 if ($adapterType === 'anthropic') {
720 $headers = array('x-api-key: '.$apiKey, 'anthropic-version: 2023-06-01');
721 $res = getURLContent($baseUrl.'/models?limit=100', 'GET', '', 1, $headers, array('http', 'https'), 2);
722 $json = json_decode($res['content'] ?? '', true);
723 foreach ((array) ($json['data'] ?? array()) as $m) {
724 if (!empty($m['id'])) {
725 $models[] = (string) $m['id'];
726 }
727 }
728 } elseif ($adapterType === 'google') {
729 $res = getURLContent($baseUrl.'/models?pageSize=200&key='.urlencode($apiKey), 'GET', '', 1, array(), array('http', 'https'), 2);
730 $json = json_decode($res['content'] ?? '', true);
731 foreach ((array) ($json['models'] ?? array()) as $m) {
732 if (!empty($m['name'])) {
733 $models[] = preg_replace('/^models\//', '', (string) $m['name']);
734 }
735 }
736 } else {
737 // OpenAI-compatible providers (OpenAI, Mistral, Groq, DeepSeek, custom...)
738 $headers = array('Authorization: Bearer '.$apiKey);
739 $res = getURLContent($baseUrl.'/models', 'GET', '', 1, $headers, array('http', 'https'), 2);
740 $json = json_decode($res['content'] ?? '', true);
741 foreach ((array) ($json['data'] ?? array()) as $m) {
742 if (!empty($m['id'])) {
743 $models[] = (string) $m['id'];
744 }
745 }
746 }
747
748 $models = array_values(array_unique($models));
749 sort($models);
750
751 if (count($models)) {
752 dolibarr_set_const($db, 'AI_MODELS_LIST_CACHE', json_encode(array('service' => $serviceKey, 'ts' => dol_now(), 'models' => $models)), 'chaine', 0, '', $conf->entity);
753 }
754
755 return array('service' => $serviceKey, 'models' => $models);
756}
757
768function aiSuggestClosestModel($missing, array $models)
769{
770 $best = '';
771 $bestScore = -1.0;
772 foreach ($models as $cand) {
773 $pct = 0.0;
774 if (similar_text(strtolower($missing), strtolower($cand), $pct) === 0) {
775 $pct = 0.0; // nothing in common
776 }
777 $score = $pct;
778 if (strtok(strtolower($missing), '-') === strtok(strtolower($cand), '-')) {
779 $score += 15.0; // same family beats a slightly closer string of another family
780 }
781 if ($score > $bestScore) {
782 $bestScore = $score;
783 $best = $cand;
784 }
785 }
786 return ($bestScore >= 50.0) ? $best : '';
787}
788
796{
797 global $conf, $langs, $user;
798
799 $langs->loadLangs(array('main', 'admin', 'bills', 'companies', 'products', 'other'));
800
801 $keys = array(
802 // Table header labels for common API fields (see FIELD_LABELS in ai_assistant.js)
803 'AIAttachmentBlockedByPrivacy', 'AIAttachmentHeicUnsupported', 'AIAttachmentTooMany', 'MissingInformation', 'CouldYouClarify',
804 'Ref', 'Label', 'ThirdParty', 'Customer', 'Paid', 'Status', 'Type', 'Email', 'Town', 'Date',
805 'DateInvoice', 'DateMaxPayment', 'AmountHT', 'AmountTTC', 'AmountVAT', 'RemainderToPay',
806 'Price', 'PriceTTC', 'VATRate', 'CustomerCode', 'SupplierCode', 'Supplier', 'TotalHT', 'TotalTTC',
807 // General UI
808 'NoDataAvailable',
809 'Error',
810 'NoRecordFound',
811 'Download',
812 'Show',
813 'Confirm',
814 'ConfirmAiAction', 'ConfirmAiWrite',
815 'ClearChatHistoryTitle',
816 'HistoryCleared',
817 'Send',
818 'TypeYourQuestion',
819 'VersionExperimental',
820
821 // Placeholders & Status
822 'TypeOrSpeak',
823 'DocLoaded',
824 'Listening',
825 'Transcribed',
826 'NoSpeech',
827 'ProcessingAudio',
828 'Timeout',
829 'Cancelled',
830
831 // Engine Specific
832 'CloudSpeechReady',
833 'WhisperReady',
834 'DownloadingModel',
835 'ModelLoading',
836
837 // Document Processing
838 'ProcessingFile',
839 'ReadingPdf',
840 'PdfError',
841 'UnsupportedFileType',
842 'TryingOCR',
843 'OcrProgress',
844 'SwitchingAIModel',
845 'OcrFailed',
846 'ReadingWord',
847 'ReadingExcel',
848 'ReadingOdf',
849
850 // Errors
851 'MicError',
852 'MicTooQuiet',
853 'ConnectionBlocked',
854 'ConnectionBlockedHelp',
855 'WorkerInitFailed',
856 'NetworkError',
857 'AIError',
858 'EmptyAIResponse',
859 'BrowserNotSupported',
860 'AISessionExpiredReload',
861
862 // Context pins
863 'AIContextPinOn',
864 'AIContextPinOff',
865 'AIContextCounter',
866 'AIContextLabel',
867 'AIContextTokens',
868 'AIContextAuto',
869 'AIContextAutoTitle',
870 'AIContextClear',
871 'None',
872 'AIContextClearTitle',
873 'AIContextAll',
874 'AIContextAllTitle',
875 'AIContextAttachmentOnly',
876
877 // Conversation history
878 'AIHistory',
879 'AINewConversation',
880 'AIHistoryToday',
881 'AIHistoryYesterday',
882 'AIHistoryLast7Days',
883 'AIHistoryOlder',
884 'AIHistoryDelete',
885 'AIHistoryRename',
886 'AIHideConversations',
887 'AIShowConversations',
888 'AIYou',
889 'AIAssistant',
890 'AIHistoryEmpty',
891
892 // Actions & Dialogs
893 'YesProceed',
894 'Cancel',
895 'Submit',
896 'ActionCancelled',
897 'ExecutingTool',
898 'FetchingData',
899 'AISearchDoneNowPreparingAction',
900 'AIRetry',
901 'GeneratingLink',
902 'Found',
903 'File',
904 'Preview',
905 'TypeResponse',
906 'AINoneOfThese',
907 'AICmdFreeText',
908 'Run',
909 'NoResults',
910 'AIPaletteHint',
911 'AIPaletteRunHint',
912 'AIMentionThirdpartyHint',
913 'AIMentionProductHint',
914 'AIHelpCommands',
915 'AIHelpPalette',
916 'AIHelpInternal',
917 'AIHelpHelp',
918 'AIHelpClear',
919 'AIHelpStatus',
920 'AIHelpTools',
921 'AIHelpReload',
922 'AIReloadDone',
923 'AIReloadNothing',
924 'AIHelpMentions',
925 'AIHelpMentionThirdparty',
926 'AIHelpMentionProduct',
927 'AIHelpFreeText',
928 'AIPaletteMore',
929 'OpenVerb',
930 'AIPdfReport',
931
932 // Voice Confirmation
933 'VoiceYesNo',
934 'VoiceQuiet',
935 'PleaseRepeat',
936 'HeardText',
937
938 // Context
939 'DocContextIntro',
940 'DocContextOutro',
941
942 // Model picker
943 'AIModelAuto',
944 'AIModelFast',
945 'AIModelBalanced',
946 'AIModelDeep',
947 'AIModelSavedGone'
948 );
949
950 $ai_translations = array();
951 foreach ($keys as $key) {
952 $ai_translations[$key] = $langs->transnoentitiesnoconv($key);
953 }
954 // Keys whose %s placeholders are consumed CLIENT-side: trans() always
955 // sprintf()s the string (empty defaults eat the %s - same trap as the
956 // TakePOS split-amount labels), so re-feed literal '%s' as parameters to
957 // keep the placeholders intact for the JS .replace() calls.
958 $ai_translations['AIContextCounter'] = $langs->transnoentitiesnoconv('AIContextCounter', '%s', '%s', '%s');
959 $ai_translations['AIContextAuto'] = $langs->transnoentitiesnoconv('AIContextAuto', '%s');
960 $ai_translations['AIContextTokens'] = $langs->transnoentitiesnoconv('AIContextTokens', '%s');
961 $ai_translations['AIContextAutoTitle'] = $langs->transnoentitiesnoconv('AIContextAutoTitle', '%s');
962 $ai_translations['AIAttachmentTooMany'] = $langs->transnoentitiesnoconv('AIAttachmentTooMany', '%s');
963 $ai_translations['ErrorFieldRequired'] = $langs->transnoentitiesnoconv('ErrorFieldRequired', '%s');
964 $ai_translations['AIPaletteBadJson'] = $langs->transnoentitiesnoconv('AIPaletteBadJson', '%s');
965 $ai_translations['AIReloadDone'] = $langs->transnoentitiesnoconv('AIReloadDone', '%s');
966 $ai_translations['AIPaletteTypeToSearch'] = $langs->transnoentitiesnoconv('AIPaletteTypeToSearch', '%s');
967 $ai_translations['DownloadPdf'] = $langs->transnoentitiesnoconv("Download").' PDF';
968 $ai_translations['CloudVoiceRequiresSecureContext'] = $langs->trans(
969 "CloudVoiceRequiresSecureContext",
970 "HTTPS",
971 "localhost",
972 "Whisper"
973 );
974
975 // First letter of the current user name, shown in the "user" message avatar
976 $userinitial = '';
977 if (is_object($user)) {
978 $namesource = $user->firstname ? $user->firstname : ($user->login ? $user->login : '');
979 $userinitial = dol_strtoupper(dol_substr($namesource, 0, 1));
980 }
981
982 $defaultmode = getDolGlobalString('AI_DEFAULT_INPUT_MODE');
983 if ($defaultmode === 'native') { // Old value saved by the setup page, now 'cloud'
984 $defaultmode = 'cloud';
985 }
986 // The whisper option is not rendered without the experimental features: fall back
987 // to the default text mode so the JS never selects a value missing from the combo.
988 if ($defaultmode === 'whisper' && getDolGlobalString("MAIN_FEATURES_LEVEL") < 1) {
989 $defaultmode = '';
990 }
991 // The Google cloud voice option is not rendered in the webview of a smartphone
992 // webapp (dol_use_jmobile): same fallback so the JS never selects a value
993 // missing from the combo.
994 if ($defaultmode === 'cloud' && !empty($conf->dol_use_jmobile)) {
995 $defaultmode = '';
996 }
997
998 return array(
999 'mode' => $defaultmode,
1000 'labels' => $ai_translations,
1001 // Presentation context for tool results: money, date and label
1002 // formatting happen client-side on raw API data.
1003 'privacyRedaction' => getDolGlobalInt('AI_PRIVACY_REDACTION', 0),
1004 // Attachment count cap, so the client mirrors the server-side guard
1005 // of ai_validate_attachments() instead of hardcoding its own.
1006 'maxAttachments' => getDolGlobalInt('AI_ATTACHMENT_MAX_FILES', 5),
1007 // Recent exchanges that follow the model by default (sliding window).
1008 // Off (0) until an administrator decides otherwise: past answers carry
1009 // business content the privacy masking does not cover, so sending them
1010 // back on every request is not a default the module takes by itself.
1011 'autoContext' => getDolGlobalInt('AI_CHAT_CONTEXT_AUTO_EXCHANGES', 2),
1012 // The "/" palette of the chat input, always on.
1013 'palette' => 1,
1014 // The raw API tools in the "/" palette (testing, advanced use); the commands are always there.
1015 'paletteApiTools' => getDolGlobalInt('AI_PALETTE_API_TOOLS', 0),
1016 // Fast/Balanced/Deep presets of the model picker: mapped onto the provider
1017 // model list by a name heuristic that is not reliable, so hidden by default.
1018 'modelPresets' => getDolGlobalInt('AI_SUGGESTS_PRESETS_TO_TRY_TO_CHANGE_MODEL_DYNAMICALLY', 0),
1019 // Gemini is the only wired provider taking HEIC natively; the chat JS
1020 // falls back to it when the browser cannot transcode HEIC to JPEG.
1021 'providerAcceptsHeic' => ((getListOfAIServices()[getDolGlobalString('AI_API_SERVICE')]['adapter_type'] ?? '') === 'google' ? 1 : 0),
1022 'currency' => $conf->currency,
1023 'locale' => str_replace('_', '-', $langs->getDefaultLang()),
1024 'urlRoot' => DOL_URL_ROOT,
1025 // Endpoints are called with absolute URLs so the chat also works when
1026 // injected into another page (topbar popover) and not only when served
1027 // from /ai/assistant/index.php.
1028 'baseUrl' => dol_buildpath('/ai/assistant/', 1),
1029 'token' => newToken(),
1030 'userInitial' => $userinitial,
1031 // Key of the conversation kept in the browser sessionStorage, so the chat
1032 // survives a page change (popover <-> full page) for this user and entity only.
1033 'historyKey' => 'aiChatHistory_'.((int) $conf->entity).'_'.((int) $user->id),
1034 );
1035}
1036
1046function getAiChatAssistantHtml($mode = 'page', $openconversation = 0)
1047{
1048 global $langs, $user, $conf;
1049
1050 $out = '';
1051
1052 // Config travels as a data attribute: <script> tags injected via innerHTML
1053 // are never executed by the browser, so a window.AI_CONFIG inline script
1054 // would not work for the AJAX-loaded popover.
1055 $out .= '<div class="ai-chat-container'.($mode === 'popover' ? ' ai-in-popover' : '').'"';
1056 $out .= ' data-ai-config="'.dol_escape_htmltag(json_encode(getAiChatAssistantConfig())).'"';
1057 if ($mode === 'page') {
1058 $out .= ' data-ai-autoinit="1"';
1059 if ((int) $openconversation > 0) {
1060 // Conversation to reopen on load (popover -> full page hand-over)
1061 $out .= ' data-ai-open-conversation="'.((int) $openconversation).'"';
1062 }
1063 }
1064 $out .= '>';
1065
1066 // Header
1067 $out .= '<div class="chat-header">';
1068 if ($mode === 'popover') {
1069 // In the popover the title links to the full standalone page
1070 $title = img_picto('', 'fa-robot', '', 0, 0, 0, '', 'paddingright').$langs->trans("AIAssistant");
1071 $title = '<a href="'.dol_buildpath('/ai/assistant/index.php', 1).'" class="ai-header-link" title="'.dol_escape_htmltag($langs->trans("AIOpenFullPage")).'">'.$title.'</a>';
1072 $out .= '<span class="chat-header-text">';
1073 $out .= '<h2>'.$title.'</h2>';
1074 // Provider in use under the title, as on the full page
1075 $aiprovider = getAiAssistantProviderLabel();
1076 if ($aiprovider !== '') {
1077 $out .= '<span class="chat-header-status" title="'.dol_escape_htmltag($langs->trans("AIProviderInUse")).'">'.dol_escape_htmltag($aiprovider).'</span>';
1078 }
1079 $out .= '</span>';
1080 } else {
1081 // Full page: assistant identity (avatar + title + current LLM model)
1082 $out .= '<div class="chat-header-id">';
1083 // Same picto as the title of the popover (no avatar box)
1084 //$out .= '<span class="chat-header-avatar">'.img_picto('', 'fa-robot').'</span>';
1085 $out .= '<span class="chat-header-text">';
1086 $out .= '<span class="chat-header-title hideonsmartphone">'.img_picto('', 'fa-robot', '', 0, 0, 0, '', 'paddingright').$langs->trans("AIAssistant").'</span>';
1087 $aiprovider = getAiAssistantProviderLabel();
1088 if ($aiprovider !== '') {
1089 $out .= '<span class="chat-header-status" title="'.dol_escape_htmltag($langs->trans("AIProviderInUse")).'">'.dol_escape_htmltag($aiprovider).'</span>';
1090 }
1091 $out .= '</span>';
1092 $out .= '</div>';
1093 }
1094 $out .= '<div class="header-controls">';
1095 // Model picker pill: 'Auto' (provider default) + presets + the dynamic model
1096 // list fetched from ajax/list_models.php by the JS. Choice kept in localStorage.
1097 $modelSelectTitle = $langs->transnoentitiesnoconv("AIModelToUse");
1098 $aiproviderurl = getAiAssistantProviderUrl();
1099 if ($aiproviderurl !== '') {
1100 $modelSelectTitle .= "\n".$langs->transnoentitiesnoconv("URL").': '.$aiproviderurl;
1101 }
1102 $out .= '<select id="model-select" class="engine-select model-select" title="'.dol_escape_htmltag($modelSelectTitle, 0, 1).'">';
1103 $autoLabel = $langs->transnoentitiesnoconv("AIModelAuto");
1104 $defaultModel = getAiAssistantDefaultModel();
1105 if ($defaultModel !== '') {
1106 $autoLabel .= ' ('.$defaultModel.')';
1107 }
1108 $out .= '<option value="" data-picto="ai-picto-sparkle">'.dol_escape_htmltag($autoLabel).'</option>';
1109 $out .= '</select>';
1110 //$out .= ajax_combobox("model-select");
1111
1112 // Engine Switcher (restyled as a pill with a sparkle icon)
1113 $out .= '<select id="engine-select" class="engine-select">';
1114 // data-picto is the css class of the picto shown before the label when the select is rendered with select2
1115 $out .= '<option value="text" data-picto="fa fa-keyboard">'.$langs->transnoentitiesnoconv("OptionTextOnly").'</option>';
1116 // The Google cloud voice mode uses the browser SpeechRecognition API, which
1117 // the webview of a smartphone webapp (dol_use_jmobile) does not provide:
1118 // the option is not offered in that mode.
1119 if (empty($conf->dol_use_jmobile)) {
1120 $out .= '<option value="cloud" data-picto="fa fa-cloud">'.$langs->transnoentitiesnoconv("OptionCloudFast").'</option>';
1121 }
1122 // The local whisper mode is experimental: only offered with the experimental features enabled
1123 if (getDolGlobalString("MAIN_FEATURES_LEVEL") >= 1) {
1124 $out .= '<option value="whisper" data-picto="fa fa-microphone" data-experimental="1">'.$langs->transnoentitiesnoconv("OptionWhisperLocal").'</option>';
1125 }
1126 // Note: the legacy 'local_docs'/'cloud_docs' selector modes are gone — documents
1127 // are now attached with the always-visible paperclip button and routed
1128 // automatically (local extraction first, cloud parsing as fallback).
1129 $out .= '</select>';
1130 // Clear button: with the conversations kept, it just starts a new one (the
1131 // current one stays in the list). "Reset" is a core key translated everywhere
1132 // with the right meaning ("Vider" in French, where "Clear" came out as "Clair").
1133 $out .= '<button type="button" id="clear-btn" class="icon-btn" title="'.dol_escape_htmltag($langs->trans("AINewConversation")).'">';
1134 $out .= img_picto('', 'fa-trash').' <span class="ai-btn-label">'.$langs->trans("Reset").'</span>';
1135 $out .= '</button>';
1136 // Past conversations (list / reopen / delete), on the page and in the popover alike.
1137 $out .= '<button type="button" id="ai-history-btn" class="icon-btn ai-window-btn" title="'.dol_escape_htmltag($langs->trans("AIHistory")).'"><i class="fa fa-history"></i></button>';
1138 if ($mode === 'popover') {
1139 // Window controls of the popover (handled by the bootstrap JS in main.inc.php).
1140 // The expand button opens the standalone full page (/ai/assistant/index.php)
1141 // in the current tab; the popover always stays in its large ("expanded") state.
1142 $out .= '<button type="button" id="ai-expand-btn" class="icon-btn ai-window-btn" title="'.dol_escape_htmltag($langs->trans("AIOpenFullPage")).'" data-fullscreen-url="'.dol_buildpath('/ai/assistant/index.php', 1).'"><i class="fa fa-expand"></i></button>';
1143 $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>';
1144 }
1145 $out .= '</div>';
1146 $out .= '</div>';
1147
1148 if ($mode === 'page') {
1149 // Full page: under the header, a permanent left column lists the past
1150 // conversations (like the mainstream chat applications) next to the chat itself.
1151 // The popover keeps its history button + panel and a flat layout.
1152 $out .= '<div class="ai-chat-body">';
1153 $out .= '<aside id="ai-history-sidebar" class="ai-history-sidebar" aria-label="'.dol_escape_htmltag($langs->trans("AIHistory")).'">';
1154 $out .= '<div class="ai-sidebar-head">';
1155 $out .= '<button type="button" id="ai-new-chat" class="ai-sidebar-new"><i class="fa fa-plus"></i> '.$langs->trans("AINewConversation").'</button>';
1156 $out .= '<button type="button" id="ai-sidebar-collapse" class="ai-sidebar-collapse" title="'.dol_escape_htmltag($langs->trans("AIHideConversations")).'"><i class="fa fa-angle-double-left"></i></button>';
1157 $out .= '</div>';
1158 $out .= '<div id="ai-sidebar-list" class="ai-sidebar-list"><div class="opacitymedium ai-sidebar-empty">…</div></div>';
1159 $out .= '</aside>';
1160 // Shown only while the column is collapsed: reopens it from the left edge
1161 $out .= '<button type="button" id="ai-sidebar-expand" class="ai-sidebar-expand" title="'.dol_escape_htmltag($langs->trans("AIShowConversations")).'"><i class="fa fa-angle-double-right"></i></button>';
1162 $out .= '<div class="ai-chat-main">';
1163 }
1164
1165 // Chat History
1166 $out .= '<div id="chat-history" class="chat-history">';
1167 if ($mode === 'popover') {
1168 // Compact greeting line for the narrow popover
1169 $out .= '<div class="msg system">'.$langs->trans("AIWelcomeMessage").'</div>';
1170 } else {
1171 // Full page: rich empty-state welcome screen (hidden by JS as soon as the
1172 // conversation starts, restored on Clear). Quick cards send a localized
1173 // ready-made prompt on click (data-prompt).
1174 $welcomename = $user->firstname ? $user->firstname : (is_object($user) ? $user->login : '');
1175 $quickcards = array(
1176 array('icon' => 'fa-file-invoice-dollar', 'key' => 'Invoices'),
1177 array('icon' => 'fa-chart-line', 'key' => 'Revenue'),
1178 array('icon' => 'fa-coins', 'key' => 'Finance'),
1179 array('icon' => 'fa-warehouse', 'key' => 'Inventory'),
1180 );
1181 $out .= '<div class="chat-welcome">';
1182 $out .= '<div class="chat-welcome-avatar">'.img_picto('', 'fa-robot').'</div>';
1183 $out .= '<h2 class="chat-welcome-title">'.dol_escape_htmltag($langs->trans("AIGreeting", $welcomename)).'</h2>';
1184 $out .= '<p class="chat-welcome-subtitle">'.dol_escape_htmltag($langs->trans("AIGreetingSubtitle")).'</p>';
1185 $out .= '<div class="chat-welcome-actions">';
1186 foreach ($quickcards as $card) {
1187 $out .= '<button type="button" class="ai-quick-card" data-prompt="'.dol_escape_htmltag($langs->transnoentitiesnoconv("AIQuick".$card['key']."Prompt")).'">';
1188 $out .= '<span class="ai-quick-icon">'.img_picto('', $card['icon']).'</span>';
1189 $out .= '<span class="ai-quick-text">';
1190 $out .= '<span class="ai-quick-title">'.dol_escape_htmltag($langs->trans("AIQuick".$card['key']."Title")).'</span>';
1191 $out .= '<span class="ai-quick-desc">'.dol_escape_htmltag($langs->trans("AIQuick".$card['key']."Desc")).'</span>';
1192 $out .= '</span>';
1193 $out .= '</button>';
1194 }
1195 $out .= '</div>';
1196 $out .= '</div>';
1197 }
1198 $out .= '</div>';
1199
1200 // Controls: a single rounded "pill" holding the attach/mic buttons, the
1201 // textarea and the send button.
1202 $out .= '<div class="chat-controls">';
1203 // Attached-file chip (icon + name + remove cross), shown above the input pill
1204 // once a document has been attached with the paperclip. The document content
1205 // itself NEVER appears in the input nor in the conversation.
1206 $out .= '<div id="file-chip-area" class="file-chip-area ai-hidden"></div>';
1207 $out .= '<div class="chat-input-pill">';
1208 // Upload Wrapper (always visible: documents can be attached in any mode)
1209 $out .= '<div id="upload-wrapper" class="upload-wrapper">';
1210 $out .= '<input type="file" id="file-upload" multiple accept=".pdf,.txt,.xml,.png,.jpg,.jpeg,.heic,.heif,.doc,.docx,.xls,.xlsx,.odt,.ods" style="display: none;">';
1211 $out .= '<button type="button" id="upload-btn" class="round-btn" title="'.dol_escape_htmltag($langs->transnoentitiesnoconv("AttachFile")).'">'.img_picto('', 'fa-paperclip').'</button>';
1212 $out .= '</div>';
1213 // Microphone Wrapper (Visible only in Voice modes)
1214 $out .= '<div id="mic-wrapper" class="mic-wrapper ai-hidden">';
1215 $out .= '<button type="button" id="mic-btn" class="round-btn mic-btn" title="'.dol_escape_htmltag($langs->trans("ToggleMicrophone")).'">'.img_picto('', 'fa-microphone').'</button>';
1216 $out .= '</div>';
1217 // Text Input
1218 $out .= '<textarea id="user-input" class="ia-input" rows="1"';
1219 if (empty($conf->dol_optimize_smallscreen)) {
1220 $out .= ' placeholder="'.dol_escape_htmltag($langs->trans("TypeYourQuestion")).'"';
1221 }
1222 $out .= ' autocomplete="off" spellcheck="false"></textarea>';
1223 // Send Button
1224 $out .= '<button type="button" id="send-btn" class="chat-send-btn" title="'.dol_escape_htmltag($langs->trans("SendPrompt")).'">'.img_picto('', 'fa-paper-plane').'</button>';
1225 $out .= '</div>';
1226 $out .= '</div>';
1227
1228 $out .= '<div id="status-bar"></div>';
1229
1230 if ($mode === 'page') {
1231 $out .= '</div>'; // .ai-chat-main
1232 $out .= '</div>'; // .ai-chat-body
1233 }
1234
1235 $out .= '</div>';
1236
1237 return $out;
1238}
1239
1261{
1262 $token = '';
1263 if (!empty($_SERVER['HTTP_X_CSRF_TOKEN'])) {
1264 $token = (string) $_SERVER['HTTP_X_CSRF_TOKEN'];
1265 } else {
1266 $token = GETPOST('token', 'alpha');
1267 }
1268
1269 $sessiontokens = array();
1270 if (!empty($_SESSION['token'])) {
1271 $sessiontokens[] = (string) $_SESSION['token'];
1272 }
1273 if (!empty($_SESSION['newtoken'])) {
1274 $sessiontokens[] = (string) $_SESSION['newtoken'];
1275 }
1276
1277 $valid = false;
1278 foreach ($sessiontokens as $sessiontoken) {
1279 if (!empty($token) && hash_equals($sessiontoken, $token)) {
1280 $valid = true;
1281 break;
1282 }
1283 }
1284
1285 if (!$valid) {
1286 dol_syslog(
1287 '[AI] Request to '.($context !== '' ? $context : $_SERVER['PHP_SELF'])
1288 .' refused by CSRF protection (invalid or missing token).',
1289 LOG_WARNING
1290 );
1291
1292 http_response_code(403);
1293 header('Content-Type: application/json');
1294 echo json_encode(array('error' => 'Invalid CSRF token'));
1295 exit;
1296 }
1297}
1298
1317function aiStripPersonalExtrafields($db, $payload, $elementtype)
1318{
1319 if (!is_array($payload) || $elementtype === '') {
1320 return $payload;
1321 }
1322
1323 static $cache = array();
1324 if (!isset($cache[$elementtype])) {
1325 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
1326 $extrafields = new ExtraFields($db);
1327 $extrafields->fetch_name_optionals_label($elementtype);
1328 $attrs = $extrafields->attributes[$elementtype] ?? array();
1329 $personal = array();
1330 foreach (($attrs['personal_data'] ?? array()) as $code => $flag) {
1331 if (!empty($flag)) {
1332 $personal[] = 'options_'.$code;
1333 }
1334 }
1335 $cache[$elementtype] = $personal;
1336 }
1337 if (empty($cache[$elementtype])) {
1338 return $payload;
1339 }
1340
1341 foreach ($payload as $key => $value) {
1342 if ($key === 'array_options' && is_array($value)) {
1343 foreach ($cache[$elementtype] as $personalKey) {
1344 unset($payload[$key][$personalKey]);
1345 }
1346 } elseif (is_array($value)) {
1347 // List responses: each row carries its own array_options.
1348 $payload[$key] = aiStripPersonalExtrafields($db, $value, $elementtype);
1349 }
1350 }
1351
1352 return $payload;
1353}
1354
1363function aiNameWords($text)
1364{
1365 $out = array();
1366 $m = array();
1367 if (preg_match_all('/[\p{L}\p{N}]+/u', (string) $text, $m, PREG_OFFSET_CAPTURE)) {
1368 foreach ($m[0] as $w) {
1369 $norm = dol_strtolower(dol_string_unaccent((string) $w[0]));
1370 if (class_exists('Normalizer')) {
1371 // Accents dol_string_unaccent() does not know (Greek tonos, ...).
1372 $norm = (string) preg_replace('/\p{Mn}+/u', '', (string) Normalizer::normalize($norm, Normalizer::FORM_D));
1373 }
1374 $out[] = array($norm, (int) $w[1], (int) $w[1] + strlen((string) $w[0]));
1375 }
1376 }
1377
1378 return $out;
1379}
1380
1391function aiThirdpartyNatureHint($text, $langs)
1392{
1393 $langs->loadLangs(array('companies', 'bills'));
1394 $stems = array(
1395 'supplier' => array('supplier', 'vendor', $langs->transnoentitiesnoconv('Supplier')),
1396 'customer' => array('customer', $langs->transnoentitiesnoconv('Customer'))
1397 );
1398 $words = array_column(aiNameWords($text), 0);
1399 $found = array();
1400 foreach ($stems as $nature => $list) {
1401 foreach ($list as $kw) {
1402 $kww = aiNameWords((string) $kw);
1403 $kw = isset($kww[0]) ? $kww[0][0] : '';
1404 // Inflected forms ("προμηθευτή", "suppliers") share the start of the word.
1405 $stem = dol_substr($kw, 0, max(5, dol_strlen($kw) - 2));
1406 foreach ($words as $w) {
1407 if (dol_strlen($stem) >= 5 && strpos($w, $stem) === 0) {
1408 $found[$nature] = true;
1409 break 2;
1410 }
1411 }
1412 }
1413 }
1414
1415 return count($found) === 1 ? (string) key($found) : '';
1416}
1417
1428function aiNameWordsEqual($a, $b)
1429{
1430 if ($a === $b) {
1431 return true;
1432 }
1433 $la = dol_strlen($a);
1434 $lb = dol_strlen($b);
1435 if (min($la, $lb) < 4 || abs($la - $lb) > 2) {
1436 return false;
1437 }
1438
1439 return ($la < $lb) ? (strpos($b, $a) === 0) : (strpos($a, $b) === 0);
1440}
1441
1450function aiNameWordsInSequence($phrase, $name)
1451{
1452 $lp = count($phrase);
1453 $ln = count($name);
1454 for ($j = 0; $j + $lp <= $ln; $j++) {
1455 $all = true;
1456 for ($k = 0; $k < $lp; $k++) {
1457 if (!aiNameWordsEqual($phrase[$k], $name[$j + $k])) {
1458 $all = false;
1459 break;
1460 }
1461 }
1462 if ($all) {
1463 return true;
1464 }
1465 }
1466
1467 return false;
1468}
1469
1490function aiFindThirdpartiesInText($db, $user, $text, $nature = '', $stopwords = array(), $maxwords = 80)
1491{
1492 $words = aiNameWords($text);
1493 if (empty($words)) {
1494 return array();
1495 }
1496
1497 $stop = array();
1498 foreach ($stopwords as $sw) {
1499 foreach (aiNameWords((string) $sw) as $sww) {
1500 $stop[$sww[0]] = true;
1501 }
1502 }
1503 // Legal forms say nothing about which company is meant: never a match alone.
1504 $legal = array_flip(array('llc', 'ltd', 'limited', 'inc', 'corp', 'co', 'plc', 'gmbh', 'ag', 'sa', 'sas', 'sarl', 'srl', 'spa', 'bv', 'nv', 'pc', 'αε', 'ικε', 'επε', 'οε', 'εε', 'μον'));
1505
1506 // Distinct words to look up: at least 3 letters, not a number. Keywords and
1507 // legal forms are looked up too, for a name made only of them ("Test Corp"),
1508 // which matches only when written in full.
1509 $lookup = array();
1510 foreach ($words as $w) {
1511 if (dol_strlen($w[0]) >= 3 && !ctype_digit($w[0])) {
1512 $lookup[$w[0]] = substr((string) $text, $w[1], $w[2] - $w[1]);
1513 if (count($lookup) >= $maxwords) {
1514 break;
1515 }
1516 }
1517 }
1518 if (empty($lookup)) {
1519 return array();
1520 }
1521
1522 // A word anywhere in the name or the trade name: names are written in any
1523 // order ("Jon Jensen LLC", "Jensen Jon LLC") and said in any order too.
1524 $sql = "SELECT s.rowid, s.nom, s.name_alias, s.client, s.fournisseur, s.town";
1525 $sql .= " FROM ".$db->prefix()."societe as s";
1526 $sql .= " WHERE s.entity IN (".getEntity('societe').")";
1527 $sql .= " AND (1 = 0";
1528 foreach ($lookup as $norm => $raw) {
1529 // The word as written and normalised, and without its last two letters
1530 // for a long word, so "solutions" also finds "Solution ...".
1531 $forms = array($raw, $norm);
1532 if (dol_strlen($norm) >= 6) {
1533 $forms[] = dol_substr($norm, 0, dol_strlen($norm) - 2);
1534 }
1535 foreach (array_unique($forms) as $w) {
1536 $sql .= " OR s.nom LIKE '".$db->escape($db->escapeforlike($w))."%' OR s.name_alias LIKE '".$db->escape($db->escapeforlike($w))."%'";
1537 }
1538 }
1539 $sql .= ")";
1540 $sql .= " LIMIT 1000";
1541 $resql = $db->query($sql);
1542 if (!$resql) {
1543 dol_syslog(__FUNCTION__." ".$db->lasterror(), LOG_ERR);
1544 return array();
1545 }
1546
1547 $companies = array();
1548 $wordlists = array();
1549 while ($obj = $db->fetch_object($resql)) {
1550 $id = (int) $obj->rowid;
1551 $companies[$id] = array('id' => $id, 'name' => (string) $obj->nom, 'name_alias' => (string) $obj->name_alias, 'client' => (int) $obj->client, 'fournisseur' => (int) $obj->fournisseur, 'town' => (string) $obj->town, 'visible' => false);
1552 foreach (array((string) $obj->nom, (string) $obj->name_alias) as $source) {
1553 $ws = array_column(aiNameWords($source), 0);
1554 if (!empty($ws)) {
1555 $wordlists[] = array($id, $ws);
1556 }
1557 }
1558 }
1559 $db->free($resql);
1560 if (empty($wordlists)) {
1561 return array();
1562 }
1563
1564 // At each position, the longest run of words (at most ten) whose words all
1565 // belong to one name, in any order. A run without a word that identifies
1566 // (4 letters or more, not a keyword, not a legal form) must be a whole name.
1567 $matches = array();
1568 $count = count($words);
1569 $i = 0;
1570 while ($i < $count) {
1571 $hit = null;
1572 for ($len = min(10, $count - $i); $len >= 1; $len--) {
1573 $phrase = array_column(array_slice($words, $i, $len), 0);
1574 $key = implode(' ', $phrase);
1575 // A weak word (3 letters or less - "for", "the", "jon" - a keyword, a
1576 // legal form, a number) says little on its own: it may join a phrase
1577 // only in the order and place it has in the name, or in a whole name.
1578 // Otherwise "for SOLUTION" would pick the one name holding both
1579 // "solution" and "for" and hide every other company with "solution".
1580 $distinctive = false;
1581 $hasWeak = false;
1582 foreach ($phrase as $pw) {
1583 if (dol_strlen($pw) >= 4 && empty($stop[$pw]) && !isset($legal[$pw]) && !ctype_digit($pw)) {
1584 $distinctive = true;
1585 } else {
1586 $hasWeak = true;
1587 }
1588 }
1589 if (dol_strlen($phrase[0]) < 2 || dol_strlen($phrase[$len - 1]) < 2) {
1590 continue;
1591 }
1592 if ($len === 1 && dol_strlen($key) < 4) {
1593 continue; // one short word alone would match half the names
1594 }
1595 $ids = array();
1596 $exactIds = array();
1597 $firstIds = array();
1598 foreach ($wordlists as $wl) {
1599 list($id, $ws) = $wl;
1600 if (count($ws) < $len) {
1601 continue;
1602 }
1603 // Each word of the phrase takes a different word of the name.
1604 $free = $ws;
1605 $strict = true;
1606 $same = true;
1607 foreach ($phrase as $pw) {
1608 $found = false;
1609 foreach ($free as $fk => $nw) {
1610 if (aiNameWordsEqual($pw, $nw)) {
1611 $strict = $strict && ($pw === $nw);
1612 unset($free[$fk]);
1613 $found = true;
1614 break;
1615 }
1616 }
1617 if (!$found) {
1618 $same = false;
1619 break;
1620 }
1621 }
1622 if (!$same) {
1623 continue;
1624 }
1625 $whole = ($strict && empty($free));
1626 if ($hasWeak && $len > 1 && !$whole && !aiNameWordsInSequence($phrase, $ws)) {
1627 continue;
1628 }
1629 $ids[$id] = true;
1630 if ($whole) {
1631 $exactIds[$id] = true; // the whole name, as written (any order)
1632 }
1633 if (aiNameWordsEqual($phrase[0], $ws[0])) {
1634 $firstIds[$id] = true; // starts like the name
1635 }
1636 }
1637 if (!$distinctive) {
1638 $ids = $exactIds; // only keywords or legal forms: the whole name, or nothing
1639 }
1640 if (empty($ids)) {
1641 continue;
1642 }
1643 $hit = array($len, !empty($exactIds) ? array_keys($exactIds) : array_keys($ids), $key, !empty($exactIds), count($firstIds) === count($ids));
1644 break;
1645 }
1646 if ($hit === null) {
1647 $i++;
1648 continue;
1649 }
1650 list($len, $ids, $key, $exact, $first) = $hit;
1651 $start = $words[$i][1];
1652 $end = $words[$i + $len - 1][2];
1653 $cands = array();
1654 foreach ($ids as $id) {
1655 $cands[$id] = $companies[$id];
1656 }
1657 if ($nature === 'supplier' || $nature === 'customer') {
1658 $narrow = array_filter($cands,
1663 function ($c) use ($nature) {
1664 return $nature === 'supplier' ? ($c['fournisseur'] == 1) : in_array($c['client'], array(1, 2, 3));
1665 }
1666 );
1667 if (!empty($narrow)) {
1668 $cands = $narrow;
1669 }
1670 }
1671 $matches[] = array('phrase' => $key, 'raw' => substr((string) $text, $start, $end - $start), 'start' => $start, 'end' => $end, 'exact' => $exact, 'first' => $first, 'words' => $len, 'candidates' => $cands);
1672 $i += $len;
1673 }
1674
1675 // Visibility: a user without the right to read third parties sees none, a
1676 // user restricted to his own customers sees only those.
1677 if (!empty($matches) && $user->hasRight('societe', 'lire')) {
1678 $allIds = array();
1679 foreach ($matches as $mt) {
1680 $allIds = array_merge($allIds, array_keys($mt['candidates']));
1681 }
1682 $seen = array();
1683 if ($user->hasRight('societe', 'client', 'voir')) {
1684 $seen = array_flip($allIds);
1685 } elseif (!empty($allIds)) {
1686 $sql = "SELECT sc.fk_soc FROM ".$db->prefix()."societe_commerciaux as sc";
1687 $sql .= " WHERE sc.fk_user = ".((int) $user->id)." AND sc.fk_soc IN (".$db->sanitize(implode(',', array_map('intval', $allIds))).")";
1688 $resql = $db->query($sql);
1689 while ($resql && ($obj = $db->fetch_object($resql))) {
1690 $seen[(int) $obj->fk_soc] = true;
1691 }
1692 }
1693 foreach ($matches as $mi => $mt) {
1694 foreach ($mt['candidates'] as $id => $c) {
1695 $matches[$mi]['candidates'][$id]['visible'] = isset($seen[$id]);
1696 }
1697 }
1698 }
1699
1700 return $matches;
1701}
1702
1714function aiExtractDocumentBlocks($text, $langs)
1715{
1716 $out = array('text' => (string) $text, 'blocks' => array());
1717 $intros = array_unique(array($langs->transnoentitiesnoconv('DocContextIntro'), 'Document Content:'));
1718 $outros = array_unique(array($langs->transnoentitiesnoconv('DocContextOutro'), 'End of document'));
1719 $introRe = array();
1720 foreach ($intros as $v) {
1721 $introRe[] = preg_quote((string) $v, '/');
1722 }
1723 $outroRe = array();
1724 foreach ($outros as $v) {
1725 $outroRe[] = preg_quote((string) $v, '/');
1726 }
1727 $introRe = implode('|', $introRe);
1728 $outroRe = implode('|', $outroRe);
1729 $m = array();
1730 if (!preg_match_all('/(?:'.$introRe.')\s.*?---\s*(?:'.$outroRe.')\s*---/su', $out['text'], $m)) {
1731 return $out;
1732 }
1733 foreach ($m[0] as $i => $block) {
1734 $mark = "\x02DOC".$i."\x02";
1735 $pos = strpos($out['text'], $block);
1736 if ($pos === false) {
1737 continue;
1738 }
1739 $out['text'] = substr($out['text'], 0, $pos).$mark.substr($out['text'], $pos + strlen($block));
1740 $out['blocks'][$mark] = $block;
1741 }
1742
1743 return $out;
1744}
1745
1753function aiRestoreDocumentBlocks($text, array $blocks)
1754{
1755 return empty($blocks) ? (string) $text : strtr((string) $text, $blocks);
1756}
1757
1771function aiThirdpartyNamesToMask($db, $user, $text, $stopwords = array(), $data = null)
1772{
1773 $names = array();
1774 foreach (aiFindThirdpartiesInText($db, $user, $text, '', $stopwords) as $mt) {
1775 $names[] = $mt['raw'];
1776 foreach ($mt['candidates'] as $c) {
1777 $names[] = $c['name'];
1778 $names[] = $c['name_alias'];
1779 }
1780 }
1781 if (is_array($data)) {
1782 $keys = array('name' => true, 'nom' => true, 'name_alias' => true, 'socname' => true, 'thirdparty_name' => true, 'company' => true);
1783 array_walk_recursive($data,
1789 function ($value, $key) use (&$names, $keys) {
1790 if (is_string($key) && isset($keys[$key]) && is_string($value)) {
1791 $names[] = $value;
1792 }
1793 }
1794 );
1795 }
1796
1797 return array_values(array_unique(array_filter($names,
1802 function ($n) {
1803 return $n !== '';
1804 }
1805 )));
1806}
1807
1816function aiUnitIdFromName($db, $name)
1817{
1818 global $langs;
1819 static $unitNames = null;
1820
1821 $want = dol_strtolower(trim((string) $name));
1822 if ($want === '') {
1823 return 0;
1824 }
1825 if (is_numeric($want)) {
1826 return (int) $want;
1827 }
1828 if ($unitNames === null) {
1829 // Read the dictionary once per request, not once per line.
1830 $langs->load('products');
1831 $unitNames = array();
1832 $resql = $db->query("SELECT rowid, code, label, short_label FROM ".$db->prefix()."c_units WHERE active = 1 LIMIT 500");
1833 while ($resql && ($obj = $db->fetch_object($resql))) {
1834 $names = array(dol_strtolower((string) $obj->code), dol_strtolower((string) $obj->short_label), dol_strtolower((string) $obj->label), dol_strtolower($langs->transnoentitiesnoconv((string) $obj->label)));
1835 if ($obj->code === 'P') {
1836 $names[] = 'pc';
1837 $names[] = 'pcs';
1838 }
1839 $unitNames[(int) $obj->rowid] = $names;
1840 }
1841 }
1842 $forms = array_unique(array($want, (string) preg_replace('/s$/', '', $want), (string) preg_replace('/es$/', '', $want)));
1843 $ids = array();
1844 foreach ($unitNames as $rowid => $names) {
1845 if (array_intersect($forms, $names)) {
1846 $ids[$rowid] = true;
1847 }
1848 }
1849
1850 return count($ids) === 1 ? (int) key($ids) : 0;
1851}
1852
1874function aiParseDocumentCommand($db, $text, $user = null, $side = 'sell')
1875{
1876 $out = array('name' => '', 'ref' => '', 'lines' => array(), 'error' => '');
1877 $text = trim((string) $text);
1878 $m = array();
1879 if (preg_match('/(?:^|\s)ref:(\S+)/i', $text, $m)) {
1880 $out['ref'] = $m[1];
1881 $text = trim((string) preg_replace('/(?:^|\s)ref:\S+/i', ' ', $text));
1882 }
1883 // A product in quotes ("23", "Blue widget", as the suggestions write a ref
1884 // that is a number or holds spaces) is one product: never a quantity, a
1885 // price or two words.
1886 $asName = array();
1887 $asProduct = array();
1888 if (preg_match_all('/"([^"]+)"/', $text, $m) > 0) {
1889 foreach ($m[0] as $i => $whole) {
1890 $mark = "\x01".$i."\x01";
1891 $text = str_replace($whole, $mark, $text);
1892 $asName[$mark] = trim($m[1][$i]);
1893 $asProduct[$mark] = '"'.trim($m[1][$i]).'"';
1894 }
1895 }
1896 $num = '\d+(?:[.,]\d+)?';
1897 if (!preg_match('/(?:^|\s)('.$num.')(?=\s|$)/', $text, $m, PREG_OFFSET_CAPTURE)) {
1898 $out['name'] = strtr($text, $asName);
1899 $out['error'] = 'nolines';
1900 return $out;
1901 }
1902 $start = (int) $m[1][1];
1903 $out['name'] = trim(strtr(substr($text, 0, $start), $asName));
1904 $rest = substr($text, $start);
1905 foreach (preg_split('/\s*;\s*|,\s+(?=\d)/', $rest) ?: array() as $group) {
1906 $tokens = preg_split('/\s+/', trim((string) $group), -1, PREG_SPLIT_NO_EMPTY) ?: array();
1907 foreach (aiParseDocumentGroup($db, $tokens, $asProduct, $user, $side) as $line) {
1908 $out['lines'][] = $line;
1909 }
1910 }
1911 if (empty($out['lines'])) {
1912 $out['error'] = 'nolines';
1913 }
1914
1915 return $out;
1916}
1917
1930function aiParseDocumentGroup($db, array $tokens, array $asProduct, $user, $side)
1931{
1932 $num = '/^\d+(?:[.,]\d+)?$/';
1933 if (empty($tokens) || !preg_match($num, $tokens[0])) {
1934 return array();
1935 }
1936 $line = array('qty' => (float) str_replace(',', '.', array_shift($tokens)));
1937 if (count($tokens) >= 2 && preg_match($num, (string) end($tokens))) {
1938 $line['subprice'] = (float) str_replace(',', '.', (string) array_pop($tokens));
1939 }
1940 if (count($tokens) >= 2 && aiUnitIdFromName($db, $tokens[0]) > 0) {
1941 $line['unit'] = array_shift($tokens);
1942 }
1943 if (empty($tokens)) {
1944 return array();
1945 }
1946 $line['product_ref'] = strtr(implode(' ', $tokens), $asProduct);
1947
1948 // "4 popularpt 2 pieces widget": a number followed by a word, after the
1949 // product, may start the next line. Only when the words are no product as
1950 // a whole ("Widget 2 XL" stays one) and every part is one.
1951 if ($user !== null && count($tokens) >= 3 && !aiDocumentProductExists($db, $user, $line['product_ref'], $side)) {
1952 $n = count($tokens);
1953 for ($i = 1; $i < $n - 1; $i++) {
1954 if (!preg_match($num, $tokens[$i]) || preg_match($num, $tokens[$i + 1])) {
1955 continue;
1956 }
1957 $left = array_slice($tokens, 0, $i);
1958 $first = $line;
1959 unset($first['subprice']);
1960 if (count($left) >= 2 && preg_match($num, (string) end($left))) {
1961 $first['subprice'] = (float) str_replace(',', '.', (string) array_pop($left));
1962 }
1963 $first['product_ref'] = strtr(implode(' ', $left), $asProduct);
1964 if (!aiDocumentProductExists($db, $user, $first['product_ref'], $side)) {
1965 continue;
1966 }
1967 $right = array_slice($tokens, $i);
1968 if (isset($line['subprice'])) {
1969 $right[] = (string) $line['subprice']; // the price at the end belongs to the last line
1970 }
1971 $more = aiParseDocumentGroup($db, $right, $asProduct, $user, $side);
1972 $allFound = !empty($more);
1973 foreach ($more as $next) {
1974 if (!aiDocumentProductExists($db, $user, (string) $next['product_ref'], $side)) {
1975 $allFound = false;
1976 break;
1977 }
1978 }
1979 if ($allFound) {
1980 return array_merge(array($first), $more);
1981 }
1982 }
1983 }
1984
1985 return array($line);
1986}
1987
1998function aiDocumentProductExists($db, $user, $typed, $side)
1999{
2000 $search = trim(str_replace('"', ' ', ltrim((string) $typed, '#')));
2001
2002 return $search !== '' && !empty(aiFindProducts($db, $user, $search, $side, 1));
2003}
2004
2018function aiFindProducts($db, $user, $text, $side = 'sell', $limit = 10)
2019{
2020 $types = array();
2021 if ($user->hasRight('produit', 'lire')) {
2022 $types[] = 0;
2023 }
2024 if ($user->hasRight('service', 'lire')) {
2025 $types[] = 1;
2026 }
2027 $words = preg_split('/\s+/', trim((string) $text), -1, PREG_SPLIT_NO_EMPTY) ?: array();
2028 if (empty($types) || empty($words)) {
2029 return array();
2030 }
2031 $sql = "SELECT p.rowid, p.ref, p.label, p.price, p.fk_unit, p.stock";
2032 $sql .= " FROM ".$db->prefix()."product as p";
2033 $sql .= " WHERE p.entity IN (".getEntity('product').")";
2034 $sql .= " AND p.fk_product_type IN (".$db->sanitize(implode(',', $types)).")";
2035 $sql .= ($side === 'buy') ? " AND p.tobuy = 1" : " AND p.tosell = 1";
2036 foreach (array_slice($words, 0, 6) as $w) {
2037 $like = $db->escape($db->escapeforlike($w));
2038 $sql .= " AND (p.ref LIKE '%".$like."%' OR p.label LIKE '%".$like."%')";
2039 }
2040 $sql .= " ORDER BY p.ref";
2041 $sql .= " LIMIT 200";
2042 $resql = $db->query($sql);
2043 if (!$resql) {
2044 dol_syslog(__FUNCTION__.' '.$db->lasterror(), LOG_ERR);
2045 return array();
2046 }
2047 $want = dol_strtolower(trim((string) $text));
2048 $exact = array();
2049 $other = array();
2050 while ($obj = $db->fetch_object($resql)) {
2051 $row = array('id' => (int) $obj->rowid, 'ref' => (string) $obj->ref, 'label' => (string) $obj->label, 'price' => (float) $obj->price, 'fk_unit' => (int) $obj->fk_unit, 'stock' => (float) $obj->stock, 'exact' => false);
2052 if (dol_strtolower($row['ref']) === $want || dol_strtolower($row['label']) === $want) {
2053 $row['exact'] = true;
2054 $exact[] = $row;
2055 } else {
2056 $other[] = $row;
2057 }
2058 }
2059 $db->free($resql);
2060
2061 return array_slice(array_merge($exact, $other), 0, $limit);
2062}
2063
2075function aiRecentProducts($db, $user, $socid, $side = 'sell', $limit = 8)
2076{
2077 if ($socid <= 0 || !$user->hasRight($side === 'buy' ? 'fournisseur' : 'facture', $side === 'buy' ? 'facture' : 'lire', $side === 'buy' ? 'lire' : '')
2078 || (!$user->hasRight('produit', 'lire') && !$user->hasRight('service', 'lire'))) {
2079 return array();
2080 }
2081 // What a third party bought is only told to a user who may see that third
2082 // party: same check as the REST API (sales representative, external user).
2083 if (!checkUserAccessToObject($user, array('societe'), (int) $socid, '', '', 'fk_soc', 'rowid')) {
2084 return array();
2085 }
2086 $doc = ($side === 'buy') ? 'facture_fourn' : 'facture';
2087 $det = ($side === 'buy') ? 'facture_fourn_det' : 'facturedet';
2088 $link = ($side === 'buy') ? 'fk_facture_fourn' : 'fk_facture';
2089 $sql = "SELECT p.rowid, p.ref, p.label, p.price, p.fk_unit, p.stock, MAX(f.rowid) as lastdoc";
2090 $sql .= " FROM ".$db->prefix().$db->sanitize($det)." as d";
2091 $sql .= " INNER JOIN ".$db->prefix().$db->sanitize($doc)." as f ON f.rowid = d.".$db->sanitize($link);
2092 $sql .= " INNER JOIN ".$db->prefix()."product as p ON p.rowid = d.fk_product";
2093 $sql .= " WHERE f.fk_soc = ".((int) $socid);
2094 $sql .= " AND f.entity IN (".getEntity($side === 'buy' ? 'supplier_invoice' : 'invoice').")";
2095 $sql .= ($side === 'buy') ? " AND p.tobuy = 1" : " AND p.tosell = 1";
2096 $sql .= " GROUP BY p.rowid, p.ref, p.label, p.price, p.fk_unit, p.stock";
2097 $sql .= " ORDER BY lastdoc DESC";
2098 $sql .= " LIMIT ".((int) $limit);
2099 $resql = $db->query($sql);
2100 $out = array();
2101 while ($resql && ($obj = $db->fetch_object($resql))) {
2102 $out[] = array('id' => (int) $obj->rowid, 'ref' => (string) $obj->ref, 'label' => (string) $obj->label, 'price' => (float) $obj->price, 'fk_unit' => (int) $obj->fk_unit, 'stock' => (float) $obj->stock, 'exact' => false);
2103 }
2104
2105 return $out;
2106}
2107
2119function aiPaletteToolTitle($langs, $name, $route)
2120{
2121 if (empty($route)) {
2122 return dol_ucfirst(str_replace('_', ' ', (string) $name));
2123 }
2124
2125 // The word Dolibarr already translates for the record: lang file, one, several.
2126 $nouns = array(
2127 'invoices' => array('bills', 'Invoice', 'Invoices'),
2128 'supplier_invoices' => array('bills', 'SupplierInvoice', 'BillsSuppliers'),
2129 'orders' => array('orders', 'Order', 'Orders'),
2130 'supplier_orders' => array('orders', 'SupplierOrder', 'SuppliersOrders'),
2131 'proposals' => array('propal', 'Proposal', 'Proposals'),
2132 'thirdparties' => array('companies', 'ThirdParty', 'ThirdParties'),
2133 'products' => array('products', 'Product', 'Products'),
2134 'projects' => array('main', 'Project', 'Projects'),
2135 'tasks' => array('projects', 'Task', 'Tasks'),
2136 'tickets' => array('ticket', 'Ticket', 'Tickets'),
2137 'warehouses' => array('stocks', 'Warehouse', 'Warehouses'),
2138 'stockmovements' => array('stocks', 'StockMovement', 'StockMovements'),
2139 'categories' => array('main', 'Category', 'Categories'),
2140 'interventions' => array('interventions', 'Intervention', 'Interventions'),
2141 'contracts' => array('contracts', 'Contract', 'Contracts'),
2142 'shipments' => array('sendings', 'Shipment', 'Shipments'),
2143 'receptions' => array('receptions', 'Reception', 'Receptions'),
2144 'members' => array('members', 'Member', 'Members'),
2145 'expensereports' => array('main', 'ExpenseReport', 'ExpenseReports'),
2146 'bankaccounts' => array('banks', 'BankAccount', 'BankAccounts'),
2147 'donations' => array('donations', 'Donation', 'Donations'),
2148 'agendaevents' => array('main', 'Event', 'Events'),
2149 'holidays' => array('holiday', 'Holiday', 'Holidays'),
2150 'users' => array('main', 'User', 'Users'),
2151 'supplier_proposals' => array('supplier_proposal', 'SupplierProposalShort', 'SupplierProposalsShort'),
2152 'boms' => array('mrp', 'BillOfMaterials', 'Bom'),
2153 'mos' => array('mrp', 'ManufacturingOrder', 'MOs'),
2154 'workstations' => array('mrp', 'Workstation', 'Workstations'),
2155 'productlots' => array('productbatch', 'Batch', ''),
2156 'memberstypes' => array('members', 'MemberType', 'MembersTypes'),
2157 'subscriptions' => array('members', 'Subscription', 'Subscriptions'),
2158 'partnerships' => array('partnership', 'Partnership', 'Partnerships'),
2159 'multicurrencies' => array('main', 'Currency', ''),
2160 'socialcontributions' => array('compta', 'SocialContribution', 'SocialContributions'),
2161 'vatpayments' => array('compta', 'VATDeclaration', 'VATDeclarations'),
2162 'variouspayments' => array('banks', 'VariousPayment', 'VariousPayments')
2163 );
2164 $endpoint = (string) $route['endpoint'];
2165 // Otherwise the label of the whitelist entry, without its explanation in brackets.
2166 $several = trim((string) preg_replace('/\s*\‍(.*\‍)\s*$/', '', (string) $route['label']));
2167 $several = ($several !== '') ? $several : str_replace('_', ' ', $endpoint);
2168 $one = (string) preg_replace(array('/ies$/', '/(?<!s)s$/'), array('y', ''), $several);
2169 if (isset($nouns[$endpoint])) {
2170 $langs->load($nouns[$endpoint][0]);
2171 $one = $langs->transnoentitiesnoconv($nouns[$endpoint][1]);
2172 if ($nouns[$endpoint][2] !== '') {
2173 $several = $langs->transnoentitiesnoconv($nouns[$endpoint][2]);
2174 }
2175 }
2176 $one = dol_strtolower($one);
2177 $several = dol_strtolower($several);
2178
2179 $verbs = array(
2180 'list' => array('AIToolTitleList', $several),
2181 'get' => array('AIToolTitleGet', $one),
2182 'get_by_ref' => array('AIToolTitleGetByRef', $one),
2183 'get_by_email' => array('AIToolTitleGetByEmail', $one),
2184 'get_by_barcode' => array('AIToolTitleGetByBarcode', $one),
2185 'get_by_code' => array('AIToolTitleGetByCode', $one),
2186 'get_by_track_id' => array('AIToolTitleGetByTrackId', $one),
2187 'get_by_thirdparty' => array('AIToolTitleGetByThirdparty', $one),
2188 'create' => array('AIToolTitleCreate', $one),
2189 'update' => array('AIToolTitleUpdate', $one),
2190 'delete' => array('AIToolTitleDelete', $one),
2191 'validate' => array('AIToolTitleValidate', $one),
2192 'add_payment' => array('AIToolTitleAddPayment', $one),
2193 'add_line' => array('AIToolTitleAddLine', $one)
2194 );
2195 $langs->load('other');
2196 $suffix = (string) $route['suffix'];
2197 if (isset($verbs[$suffix])) {
2198 return dol_ucfirst($langs->transnoentitiesnoconv($verbs[$suffix][0], $verbs[$suffix][1]));
2199 }
2200
2201 // Anything else ("payments_list", "lines"...): the record, then what is read,
2202 // in the user's language when Dolibarr has the word.
2203 $what = (string) preg_replace(array('/_list$/', '/^get_/'), '', $suffix);
2204 $words = array(
2205 'contacts' => array('companies', 'Contacts'),
2206 'payments' => array('bills', 'Payments'),
2207 'lines' => array('withdrawals', 'Lines'),
2208 'categories' => array('main', 'Categories'),
2209 'supplier_categories' => array('categories', 'SuppliersCategoriesShort'),
2210 'stock' => array('stocks', 'Stock'),
2211 'timespent' => array('projects', 'TimeSpent'),
2212 'discounts' => array('bills', 'Discounts'),
2213 'sales_representatives' => array('commercial', 'SalesRepresentatives'),
2214 'shipments' => array('sendings', 'Shipments'),
2215 'subscriptions' => array('members', 'Subscriptions')
2216 );
2217 if (isset($words[$what])) {
2218 $langs->load($words[$what][0]);
2219 $what = dol_strtolower($langs->transnoentitiesnoconv($words[$what][1]));
2220 } else {
2221 $what = str_replace('_', ' ', $what);
2222 }
2223
2224 return dol_ucfirst($langs->transnoentitiesnoconv('AIToolTitleOther', $one, $what));
2225}
2226
2234function aiPaletteToolDescription($description)
2235{
2236 $text = (string) preg_replace('/\s*through the Dolibarr REST API \‍(auto-generated tool\‍)\.?/i', '', (string) $description);
2237
2238 return trim((string) preg_replace('/\s{2,}/', ' ', $text));
2239}
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
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:213
getListOfAIFeatures()
Prepare admin pages header.
Definition ai.lib.php:37
aiFindProducts($db, $user, $text, $side='sell', $limit=10)
Products of the catalogue matching what a user typed: every word must be in the ref or the label,...
Definition ai.lib.php:2018
aiAdminPrepareHead()
Prepare admin pages header.
Definition ai.lib.php:548
getAiProviderModelList($db, $forcerefresh=false)
Return the list of model ids offered by the configured AI provider, with a 1-hour cache in the consta...
Definition ai.lib.php:681
getListOfAIServices()
Get list of available ai services.
Definition ai.lib.php:69
aiRestoreDocumentBlocks($text, array $blocks)
Put back, unchanged, the documents set aside by aiExtractDocumentBlocks().
Definition ai.lib.php:1753
aiUnitIdFromName($db, $name)
Unit id from the word a user used for it: code, short label or label, in the user's language,...
Definition ai.lib.php:1816
aiFindThirdpartiesInText($db, $user, $text, $nature='', $stopwords=array(), $maxwords=80)
Third parties named in a text, by the words of their name or trade name (name_alias),...
Definition ai.lib.php:1490
aiParseDocumentCommand($db, $text, $user=null, $side='sell')
Read a one-line document command of the chat palette, e.g.
Definition ai.lib.php:1874
aiThirdpartyNamesToMask($db, $user, $text, $stopwords=array(), $data=null)
Every form of the third party names found in a text or carried by a tool result, to mask with Privacy...
Definition ai.lib.php:1771
aiDocumentProductExists($db, $user, $typed, $side)
Whether the words of a command line name at least one product the user may put on that kind of docume...
Definition ai.lib.php:1998
getAiAssistantDefaultModel()
Resolve the text model used by the AI Assistant when the picker is on "Auto" (same resolution order a...
Definition ai.lib.php:636
aiNameWordsEqual($a, $b)
Whether two words of a name are the same word: equal, or the same word with a different ending ("solu...
Definition ai.lib.php:1428
aiThirdpartyNatureHint($text, $langs)
Nature of third party a text points at, from its own words: "supplier" when it speaks of a supplier (...
Definition ai.lib.php:1391
getAiChatAssistantConfig()
Build the configuration array consumed by the AI Assistant chat frontend (ai/js/ai_assistant....
Definition ai.lib.php:795
ai_validate_attachments(array $attachments, &$error)
Validate chat attachments before they reach any LLM provider.
Definition ai.lib.php:317
aiPaletteToolTitle($langs, $name, $route)
Plain name of a tool for the palette of the chat: "Get invoice" rather than api_invoices_get.
Definition ai.lib.php:2119
aiTruncateForLog($text, $max=60000)
Trim a payload for the request log, keeping the beginning and the end.
Definition ai.lib.php:383
getAiAssistantProviderUrl()
Resolve the API URL used by the AI Assistant for the configured provider (same resolution order as as...
Definition ai.lib.php:658
aiPaletteToolDescription($description)
Description of a tool for the palette: without the sentence every generated bridge tool repeats,...
Definition ai.lib.php:2234
getListForAIRephraseStyle()
Get list for AI style of writing.
Definition ai.lib.php:515
ai_log_request($db, $user, $query, array $response, $provider, float $time, float $confidence, $status, $error='', $rawReq='', $rawRes='', array $context=array(), &$logId=null)
Log AI Request with Raw Payloads.
Definition ai.lib.php:416
aiExtractDocumentBlocks($text, $langs)
Set aside the documents a chat message carries, so what is done to the request (third parties resolve...
Definition ai.lib.php:1714
getAIErrorMessage($result)
Return a user-friendly error message from the error array returned by Ai\generateContent()
Definition ai.lib.php:532
aiStripPersonalExtrafields($db, $payload, $elementtype)
Remove extrafields flagged as personal data from an API-shaped payload.
Definition ai.lib.php:1317
getAiAssistantProviderLabel()
Resolve the AI provider/service currently configured for the AI Assistant (e.g.
Definition ai.lib.php:618
getListForAISummarize()
Get list for AI summarize.
Definition ai.lib.php:493
aiNameWords($text)
Words of a text as the third party matcher compares them: runs of letters or digits,...
Definition ai.lib.php:1363
aiNameWordsInSequence($phrase, $name)
Whether the words of a phrase appear in a name one after the other, in the same order each word compa...
Definition ai.lib.php:1450
aiSuggestClosestModel($missing, array $models)
Suggest the closest available model id for a model that disappeared from the provider's list: same fa...
Definition ai.lib.php:768
aiCheckCsrfToken($context='')
Check the anti-CSRF token of a request sent to one of the AI Assistant endpoints.
Definition ai.lib.php:1260
aiParseDocumentGroup($db, array $tokens, array $asProduct, $user, $side)
Lines of one group of a document command: quantity, optional unit, product, optional unit price.
Definition ai.lib.php:1930
getAiChatAssistantHtml($mode='page', $openconversation=0)
Build the HTML of the AI Assistant chat interface.
Definition ai.lib.php:1046
aiRecentProducts($db, $user, $socid, $side='sell', $limit=8)
Products of a third party's latest documents, newest first: what is most likely to be sold (or bought...
Definition ai.lib.php:2075
$c
Definition line.php:353
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 standard extra fields.
$context
@method int call_trigger(string $triggerName, ?User $user)
Definition command.php:209
dol_now($mode='gmt')
Return date for now.
dol_ucfirst($string, $encoding="UTF-8")
Convert first character of the first word of a string to upper.
dol_strtolower($string, $encoding="UTF-8")
Convert a string to lower.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_string_unaccent($str)
Clean a string from all accent characters to be used as ref, login or by dol_sanitizeFileName.
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_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.
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_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1, $timeoutconnect=0, $timeoutresponse=0, $otherCurlOptions=array(), $morelogsuffix='')
Function to get a content from an URL (use proxy if proxy defined).
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183
checkUserAccessToObject($user, array $featuresarray, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='', $dbt_select='rowid', $parenttableforentity='')
Check that access by a given user to an object is ok.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.