dolibarr 25.0.0-alpha
parse_intent.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2026 Nick Fragoulis
4 * Copyright (C) 2026 Jose Martinez <jose.martinez@pichinov.com>
5 * Copyright (C) 2026 Anthony Damhet <a.damhet@progiseize.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY, without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * or see https://www.gnu.org/
20 */
21
29if (!defined('NOTOKENRENEWAL')) {
30 define('NOTOKENRENEWAL', 1);
31}
32if (!defined('NOREQUIREMENU')) {
33 define('NOREQUIREMENU', 1);
34}
35if (!defined('NOREQUIREHTML')) {
36 define('NOREQUIREHTML', 1);
37}
38if (!defined('NOREQUIREAJAX')) {
39 define('NOREQUIREAJAX', 1);
40}
41// The payload is read from the raw php://input body, so the CSRF token cannot be checked by
42// main.inc.php. It is checked explicitly below by aiCheckCsrfToken().
43if (!defined('NOCSRFCHECK')) {
44 define('NOCSRFCHECK', 1);
45}
46
47require '../../main.inc.php';
48require_once DOL_DOCUMENT_ROOT . '/ai/class/mcp.class.php';
49require_once DOL_DOCUMENT_ROOT . '/ai/lib/ai.lib.php';
50require_once DOL_DOCUMENT_ROOT . '/ai/class/llmadapter.class.php';
51require_once DOL_DOCUMENT_ROOT . '/ai/class/privacy_guard.class.php';
52require_once DOL_DOCUMENT_ROOT . '/core/lib/security2.lib.php';
53
54// Security check
55if (!isModEnabled('ai') || !getDolGlobalString('AI_ASSISTANT_ENABLED')) {
56 http_response_code(403);
57 accessforbidden('Module or feature not allowed');
58}
59
60global $db, $user, $conf, $langs;
61
62// Same per-user gate as the Assistant page that calls this endpoint, so a
63// user without 'ai/assistant/use' cannot reach the LLM through direct AJAX.
64if (!$user->hasRight('ai', 'assistant', 'use')) {
66}
67
68// This endpoint sends data to the LLM provider on behalf of the user and can chain tool
69// executions, so it must not be reachable from another site.
70aiCheckCsrfToken('ai/assistant/parse_intent.php');
71
72// Start to capture output
73ob_start();
74
75top_httphead('application/json');
76
77// Confirmation level: 0=no confirmation, 1=only create/update/delete, 2=all actions
78$askForConfirmation = getDolGlobalInt('AI_ASK_FOR_CONFIRMATION');
79
80// Confidence thresholds
81define('HIGH_CONFIDENCE', 0.8);
82define('MEDIUM_CONFIDENCE', 0.5);
83define('LOW_CONFIDENCE', 0.3);
84
85// Logging variables
86$startTime = microtime(true);
87$rawRequestLog = "";
88$rawResponseLog = "";
89$providerUsed = "offline";
90$errorDetails = "";
91
92$assistantEnabled = getDolGlobalInt('AI_ASSISTANT_ENABLED', 0);
93$serviceKey = getDolGlobalString('AI_API_SERVICE');
94$doRedact = getDolGlobalInt('AI_PRIVACY_REDACTION', 0); // 0=no protection, 1=sensitive data masked, 2=third parties also replaced by socid:N
95$doRedactNames = ($doRedact >= 2);
96$timeout = getDolGlobalInt('AI_REQUEST_TIMEOUT', 120);
97
98// Kill switch
99if (!$assistantEnabled) {
100 $response = [
101 "tool" => "respond_to_user",
102 "arguments" => [
103 "message" => "AI assistant service is currently disabled. Please contact your administrator to enable it."
104 ]
105 ];
106 ob_end_clean();
107 echo json_encode($response);
108 exit;
109}
110
111if (getDolGlobalString("AI_DEBUG")) {
112 dol_syslog("parse_intent.php ***** BEGIN", LOG_INFO, 0, '_ai');
113}
114
115set_time_limit($timeout + 5);
116
117try {
118 // Input
119 $raw_input = file_get_contents('php://input');
120 $data = json_decode($raw_input, true);
121 $query = isset($data['query']) ? trim($data['query']) : '';
122 // Set by the chat when it calls back after running the read tool this
123 // endpoint flagged with needs_followup: the result is the last history turn.
124 $isFollowup = !empty($data['followup']);
125
126 if (getDolGlobalString("AI_DEBUG")) {
127 dol_syslog("parse_intent.php isFollowup=".($isFollowup ? 1 : 0), LOG_DEBUG, 0, '_ai');
128 }
129
130 // --- Page context (optional): posted by the chat JS from the value the
131 // printCommonFooter hook emitted on the page being viewed. The POST is
132 // client-controlled, so nothing here is trusted: the element must be on
133 // the whitelist, the object must fetch, and the user must hold the read
134 // permission - otherwise the context is silently dropped. On success a
135 // one-line description is added to the system prompt so the model can
136 // resolve "this invoice" into real tool arguments.
137 $aiPageContextLine = '';
138 $ctxNamesToMask = array();
139 if (!empty($data['context']) && is_array($data['context'])) {
140 $ctxElement = isset($data['context']['element']) ? (string) $data['context']['element'] : '';
141 $ctxId = isset($data['context']['id']) ? (int) $data['context']['id'] : 0;
142 // element => [classfile, classname, label, rights module, rights perm(, rights subperm)]
143 // External modules with their own objects opt in through the
144 // AI_ASSISTANT_CONTEXT_ELEMENTS const: a JSON array of entries
145 // {"element":..,"classfile":"/mymodule/class/x.class.php","classname":..,
146 // "label":..,"rights":["mymodule","myobject","read"]} - same shape,
147 // same validation path (whitelist, fetch, hasRight, entity) as core
148 // elements. Their card pages already emit context automatically via
149 // the global hook; this const is the server-side acceptance half.
150 $ctxMap = array(
151 'facture' => array('/compta/facture/class/facture.class.php', 'Facture', 'customer invoice', 'facture', 'lire'),
152 'invoice_supplier' => array('/fourn/class/fournisseur.facture.class.php', 'FactureFournisseur', 'supplier invoice', 'fournisseur', 'facture', 'lire'),
153 'commande' => array('/commande/class/commande.class.php', 'Commande', 'sales order', 'commande', 'lire'),
154 'order_supplier' => array('/fourn/class/fournisseur.commande.class.php', 'CommandeFournisseur', 'supplier order', 'fournisseur', 'commande', 'lire'),
155 'propal' => array('/comm/propal/class/propal.class.php', 'Propal', 'commercial proposal', 'propal', 'lire'),
156 'supplier_proposal' => array('/supplier_proposal/class/supplier_proposal.class.php', 'SupplierProposal', 'supplier proposal', 'supplier_proposal', 'lire'),
157 'societe' => array('/societe/class/societe.class.php', 'Societe', 'thirdparty', 'societe', 'lire'),
158 'product' => array('/product/class/product.class.php', 'Product', 'product or service', 'produit', 'lire'),
159 'shipping' => array('/expedition/class/expedition.class.php', 'Expedition', 'shipment', 'expedition', 'lire'),
160 'reception' => array('/reception/class/reception.class.php', 'Reception', 'reception', 'reception', 'lire'),
161 'project' => array('/projet/class/project.class.php', 'Project', 'project', 'projet', 'lire'),
162 'project_task' => array('/projet/class/task.class.php', 'Task', 'project task', 'projet', 'lire'),
163 'contrat' => array('/contrat/class/contrat.class.php', 'Contrat', 'contract', 'contrat', 'lire'),
164 'fichinter' => array('/fichinter/class/fichinter.class.php', 'Fichinter', 'intervention', 'ficheinter', 'lire'),
165 'ticket' => array('/ticket/class/ticket.class.php', 'Ticket', 'support ticket', 'ticket', 'read'),
166 'member' => array('/adherents/class/adherent.class.php', 'Adherent', 'member', 'adherent', 'lire'),
167 'expensereport' => array('/expensereport/class/expensereport.class.php', 'ExpenseReport', 'expense report', 'expensereport', 'lire'),
168 'holiday' => array('/holiday/class/holiday.class.php', 'Holiday', 'leave request', 'holiday', 'read'),
169 'don' => array('/don/class/don.class.php', 'Don', 'donation', 'don', 'lire'),
170 'action' => array('/comm/action/class/actioncomm.class.php', 'ActionComm', 'agenda event', 'agenda', 'myactions', 'read'),
171 'bom' => array('/bom/class/bom.class.php', 'BOM', 'bill of materials', 'bom', 'read'),
172 'mo' => array('/mrp/class/mo.class.php', 'Mo', 'manufacturing order', 'mrp', 'read'),
173 'stock' => array('/product/stock/class/entrepot.class.php', 'Entrepot', 'warehouse', 'stock', 'lire'),
174 'contact' => array('/contact/class/contact.class.php', 'Contact', 'contact', 'societe', 'contact', 'lire'),
175 'bank_account' => array('/compta/bank/class/account.class.php', 'Account', 'bank account', 'banque', 'lire'),
176 'category' => array('/categories/class/categorie.class.php', 'Categorie', 'category (tag)', 'categorie', 'lire'),
177 'knowledgerecord' => array('/knowledgemanagement/class/knowledgerecord.class.php', 'KnowledgeRecord', 'knowledge article', 'knowledgemanagement', 'knowledgerecord', 'read'),
178 'recruitmentjobposition' => array('/recruitment/class/recruitmentjobposition.class.php', 'RecruitmentJobPosition', 'job position', 'recruitment', 'recruitmentjobposition', 'read'),
179 // Deliberately absent: user and salary (privacy/SEC precedent
180 // #40313) - personal data cards never feed the prompt.
181 );
182 $ctxExtra = getDolGlobalString('AI_ASSISTANT_CONTEXT_ELEMENTS'); // Additional elements from external sources
183 if ($ctxExtra) {
184 $extraArr = json_decode($ctxExtra, true);
185 if (is_array($extraArr)) {
186 foreach ($extraArr as $extra) {
187 if (!empty($extra['element']) && !empty($extra['classfile']) && !empty($extra['classname']) && !empty($extra['rights'][0]) && !isset($ctxMap[$extra['element']])) {
188 $ctxMap[(string) $extra['element']] = array(
189 (string) $extra['classfile'],
190 (string) $extra['classname'],
191 (string) ($extra['label'] ?? $extra['element']),
192 (string) $extra['rights'][0],
193 (string) ($extra['rights'][1] ?? 'read'),
194 (string) ($extra['rights'][2] ?? '')
195 );
196 }
197 }
198 }
199 }
200 if ($ctxId > 0 && isset($ctxMap[$ctxElement]) && $user->hasRight($ctxMap[$ctxElement][3], $ctxMap[$ctxElement][4], $ctxMap[$ctxElement][5] ?? '')) {
201 require_once DOL_DOCUMENT_ROOT.$ctxMap[$ctxElement][0];
202 $ctxObj = new $ctxMap[$ctxElement][1]($db);
203 if ($ctxObj->fetch($ctxId) > 0 && (empty($ctxObj->entity) || in_array((int) $ctxObj->entity, explode(',', getEntity($ctxElement))))) {
204 $ctxThirdpartyName = '';
205 if (empty($doRedactNames) && !empty($ctxObj->socid)) {
206 // The counterparty NAME lets the model use name-based search
207 // tools too; under redaction it is omitted - the ids suffice
208 // and names must not travel to the provider.
209 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
210 $ctxSoc = new Societe($db);
211 if ($ctxSoc->fetch((int) $ctxObj->socid) > 0) {
212 $ctxThirdpartyName = " (".dol_string_nohtmltag($ctxSoc->name).")";
213 }
214 }
215 if (empty($doRedactNames) && $ctxElement === 'societe' && !empty($ctxObj->name)) {
216 $ctxThirdpartyName = " (".dol_string_nohtmltag((string) $ctxObj->name).")";
217 }
218 // Under redaction, elements whose ref IS a personal/company name
219 // (societe: ref = company name) must not leak it - the privacy
220 // guard is pattern-based and cannot recognize arbitrary names.
221 $ctxNamesToMask[] = (string) $ctxObj->ref;
222 if (!empty($ctxObj->label)) {
223 $ctxNamesToMask[] = (string) $ctxObj->label;
224 }
225 if (!empty($ctxObj->name)) {
226 $ctxNamesToMask[] = (string) $ctxObj->name;
227 }
228 if (!empty($ctxObj->name_alias)) {
229 $ctxNamesToMask[] = (string) $ctxObj->name_alias; // the trade name is how people say it
230 }
231 $ctxRefPart = " with ref \"".$ctxObj->ref."\"";
232 if (!empty($doRedactNames) && in_array($ctxElement, array('societe', 'contact'), true)) {
233 $ctxRefPart = "";
234 }
235 $aiPageContextLine = "The user is currently viewing the ".$ctxMap[$ctxElement][2].$ctxRefPart." (id ".(int) $ctxObj->id.(!empty($ctxObj->socid) ? ", thirdparty id ".(int) $ctxObj->socid.$ctxThirdpartyName : $ctxThirdpartyName).").";
236 $aiPageContextLine .= " When the user says \"this\"/\"it\" or refers to the current document, use these identifiers as tool arguments";
237 if ($ctxElement === 'societe') {
238 $aiPageContextLine .= " - in particular, this thirdparty id is the socid/customer id for any create or search tool";
239 }
240 $aiPageContextLine .= ". NEVER ask the user for ids already given here; pass names/refs the user wrote (products, etc.) directly in the matching ref arguments - tools resolve them.";
241 if (getDolGlobalString("AI_DEBUG")) {
242 dol_syslog("AI Pro: page context accepted: ".$ctxElement." #".$ctxId, LOG_INFO, 0, '_ai');
243 }
244 } else {
245 if (getDolGlobalString("AI_DEBUG")) {
246 dol_syslog("AI Pro: page context rejected (fetch/entity): ".$ctxElement." #".$ctxId, LOG_WARNING, 0, '_ai');
247 }
248 }
249 } elseif ($ctxId > 0) {
250 if (getDolGlobalString("AI_DEBUG")) {
251 dol_syslog("AI Pro: page context rejected (whitelist/rights): ".$ctxElement." #".$ctxId, LOG_WARNING, 0, '_ai');
252 }
253 } elseif (!empty($data['context']['dashboard']) && is_string($data['context']['dashboard'])) {
254 $dash = dol_string_nohtmltag(dol_substr($data['context']['dashboard'], 0, 60));
255 if (preg_match('/^[a-z0-9 _-]+$/i', $dash)) {
256 $aiPageContextLine = "The user is currently on the ".$dash." dashboard page. Questions about \"here\"/\"this page\" concern that module's data.";
257 if (getDolGlobalString("AI_DEBUG")) {
258 dol_syslog("AI Pro: dashboard context accepted: ".$dash, LOG_INFO, 0, '_ai');
259 }
260 }
261 } elseif (!empty($data['context']['list']) && $ctxElement !== '' && (!empty($data['context']['filters']) || !empty($data['context']['ids']) || !empty($data['context']['selected']))) {
262 // List context: the user's own search inputs on their own list
263 // page, echoed back uninterpreted (sanitized + capped). Nothing is
264 // fetched, so no rights question arises; the model maps these onto
265 // tool arguments and the tools validate as always.
266 $parts = array();
267 $n = 0;
268 foreach ((is_array($data['context']['filters'] ?? null) ? $data['context']['filters'] : array()) as $fk => $fv) {
269 if (!is_string($fv) || !preg_match('/^(search_[a-z0-9_]+|sall|search_all|sortfield|sortorder)$/', (string) $fk)) {
270 continue;
271 }
272 $parts[] = $fk."='".dol_string_nohtmltag(dol_substr($fv, 0, 120))."'";
273 // Search values are names the user typed against real records:
274 // same class of arbitrary string as a thirdparty name.
275 $ctxNamesToMask[] = dol_string_nohtmltag(dol_substr($fv, 0, 120));
276 if (++$n >= 12) {
277 break;
278 }
279 }
280 $idsPart = '';
281 foreach (array('ids' => 100, 'selected' => 25) as $idkey => $cap) {
282 if (!empty($data['context'][$idkey]) && is_array($data['context'][$idkey])) {
283 $clean = array();
284 foreach ($data['context'][$idkey] as $v) {
285 if ((int) $v > 0) {
286 $clean[] = (int) $v;
287 }
288 if (count($clean) >= $cap) {
289 break;
290 }
291 }
292 if (!empty($clean)) {
293 $idsPart .= ($idkey === 'ids' ? " Visible row ids: " : " Checked/selected row ids (act on these when the user says the selected ones): ").implode(',', $clean).".";
294 }
295 }
296 }
297 if (!empty($parts) || $idsPart !== '') {
298 $aiPageContextLine = "The user is currently viewing the \"".preg_replace('/[^a-z0-9_]/', '', $ctxElement)."\" list".(!empty($parts) ? " filtered by: ".implode(', ', $parts) : "").".".$idsPart;
299 $aiPageContextLine .= " To act on \"this list\"/\"these records\"/\"the selected ones\", use these ids or translate the filters into the matching arguments of the list/report tools.";
300 if (getDolGlobalString("AI_DEBUG")) {
301 dol_syslog("AI Pro: list context accepted: ".$ctxElement." (".count($parts)." filters".($idsPart !== '' ? ", ids" : "").")", LOG_INFO, 0, '_ai');
302 }
303 }
304 }
305 }
306
307 // This is to allow easy test of the parse_intent.php by calling the URL with param query=test
308 if (empty($query) && GETPOST('query', 'alphanohtml') == '/tools') {
309 $query = '/tools';
310 }
311
312 if (empty($query)) {
313 ob_end_clean();
314 echo json_encode(["status" => "ok"]);
315 exit;
316 }
317
318 // Extract file attachments sent by the chat (paperclip flow). The JS embeds
319 // cloud-parsed documents as "__FILE_ATTACHMENT__[mime]::<base64>" markers in
320 // the query. They MUST be stripped here, before the privacy/thirdparty
321 // candidate pipeline (which would run regexes over megabytes of base64), and
322 // are handed to the LLM adapter as NATIVE multimodal parts — inlining base64
323 // into the text prompt makes every provider fail or hallucinate.
324 $attachments = array();
325 if (strpos($query, '__FILE_ATTACHMENT__') !== false) {
326 $query = preg_replace_callback(
327 '/__FILE_ATTACHMENT__\[([^\]]*)\]::([A-Za-z0-9+\/=\r\n]+)/',
332 static function (array $m) use (&$attachments) {
333 $attachments[] = array(
334 'mime' => ($m[1] !== '' ? $m[1] : 'application/octet-stream'),
335 'data' => preg_replace('/\s+/', '', $m[2])
336 );
337 return '[attached document]';
338 },
339 $query
340 );
341 $query = trim((string) $query);
342 if ($query === '' || $query === '[attached document]') {
343 $query = 'Analyze the attached document and describe its content.';
344 }
345 }
346
347 // Server-side gate on what the browser sent: MIME allowlist, size caps,
348 // and the privacy-redaction policy (documents cannot be masked, so under
349 // enforced redaction they must not go to a cloud provider at all).
350 $attachmenterror = '';
351 if (!ai_validate_attachments($attachments, $attachmenterror)) {
352 ob_end_clean();
353 echo json_encode(array(
354 "tool" => "respond_to_user",
355 "arguments" => array("message" => $attachmenterror)
356 ));
357 exit;
358 }
359
360 // Privacy (Name Resolution & Masking)
361 $langs->loadLangs(array("main", "bills", "orders", "propal", "supplier_invoice", "supplier_order", "projects", "other"));
362
363 // Translation key of Words we want to block in any language.
364 $blockKeys = [
365 // Objects (Nouns)
366 'Bill',
367 'Invoice',
368 'Order',
369 'Proposal',
370 'Shipment',
371 'Reception',
372 'Contract',
373 'SupplierInvoice',
374 'SupplierOrder',
375 'Project',
376 'Task',
377 'Product',
378 'Service',
379 'Ticket',
380 'Event',
381 'Agenda',
382 'Member',
383 'User',
384 'ThirdParty',
385 'Company',
386 'Contact',
387 // Actions (Verbs/Commands)
388 'Search',
389 'Find',
390 'List',
391 'Show',
392 'Create',
393 'Add',
394 'Modify',
395 'Delete',
396 'Validate',
397 'Send',
398 // Other
399 'Hello',
400 'Test'
401 ];
402
403 // Resolve keys to the actual current language
404 $dynamicStopWords = [];
405 foreach ($blockKeys as $key) {
406 $word = $langs->transnoentities($key);
407 if (!empty($word)) {
408 $dynamicStopWords[] = dol_strtolower($word);
409 }
410 }
411
412 // Additional stop words (for the moment hardcoded, to move in language files
413 $additionalStopWords = ['proposition', 'devis'];
414
415 // Add common short English/French/Spanish commands that users often type
416 // regardless of the UI language.
417 $commonCommands = ['show', 'find', 'search', 'list', 'get', 'voir', 'chercher', 'affiche', 'liste', 'montre', 'lista', 'buscar'];
418 $dynamicStopWords = array_unique(array_merge($dynamicStopWords, $additionalStopWords, $commonCommands)); // $dynamicStopWords is an array of words
419
420 if (getDolGlobalString("AI_DEBUG")) {
421 dol_syslog("parse_intent.php We have dynamicStopWords: ".implode(',', $dynamicStopWords), LOG_DEBUG, 0, '_ai');
422 }
423
424 // Third parties named in the request, found by matching the text against
425 // the words of their names and trade names in any order.
426 // The phrase that names one is resolved to its id before the model is called.
427 // Privacy guard level 2: the phrase becomes "socid:N" so the name never reaches the provider.
428 // When several fit, or for a single word that is part of the whole name, the user chooses before anything
429 // is sent: the answer is built locally and the choice comes back with the same request.
430 // Privacy guard level 0 or 1: the name stays and the id is added "Name (socid:N)" when only one fits.
431 // Otherwise the phrase is left as written and the model searches it (no question).
432 $aiNamesToMask = array(); // names masked in everything sent: query, history, tool results
433 $tpChoices = (!empty($data['thirdparty_choices']) && is_array($data['thirdparty_choices'])) ? $data['thirdparty_choices'] : array();
434 // An attached document is data, not the request: the third parties it names
435 // are not resolved to ids (that would rewrite the document the model is asked
436 // to read), and the offsets below are taken on the request alone. Under the
437 // privacy guard its names are still masked, like any other name.
438 $aiDocuments = aiExtractDocumentBlocks($query, $langs);
439 $query = $aiDocuments['text'];
440 if (!empty($doRedactNames)) {
441 foreach ($aiDocuments['blocks'] as $aiDocument) {
442 $aiNamesToMask = array_merge($aiNamesToMask, aiThirdpartyNamesToMask($db, $user, $aiDocument, $dynamicStopWords));
443 }
444 }
445
446 $tpFound = aiFindThirdpartiesInText($db, $user, $query, aiThirdpartyNatureHint($query, $langs), $dynamicStopWords);
447
448 $tpAsk = null;
449 $tpReplace = array();
450 foreach ($tpFound as $mt) {
451 $aiNamesToMask[] = $mt['raw'];
452 $visibleIds = array();
453 foreach ($mt['candidates'] as $cid => $c) {
454 $aiNamesToMask[] = $c['name'];
455 $aiNamesToMask[] = $c['name_alias'];
456 if ($c['visible']) {
457 $visibleIds[] = (int) $cid;
458 }
459 }
460 $socidChosen = -1;
461 if (isset($tpChoices[$mt['phrase']]) && is_numeric($tpChoices[$mt['phrase']])) {
462 $chosen = (int) $tpChoices[$mt['phrase']];
463 // Only one of the candidates offered for this phrase, or 0 (none).
464 if ($chosen === 0 || in_array($chosen, $visibleIds, true)) {
465 $socidChosen = $chosen;
466 }
467 }
468 // A weak match (one word that is not the whole name) is confirmed by the
469 // user, even when the word is not the start of the name ("Jensen" for "Jon Jensen LLC").
470 // Under the guard the question is local.
471 $weak = (!$mt['exact'] && $mt['words'] === 1 && (empty($doRedactNames) || !$mt['first']));
472 if ($socidChosen < 0) {
473 if (count($visibleIds) === 1 && !$weak) {
474 $socidChosen = $visibleIds[0];
475 } elseif (count($visibleIds) === 0) {
476 $socidChosen = 0; // nothing to offer: masked under the guard level 2, left as written otherwise
477 } elseif (empty($doRedactNames)) {
478 $socidChosen = 0; // several or a weak match: the name may reach the provider, so left as written for the model to search
479 } else {
480 if ($tpAsk === null) {
481 $tpAsk = $mt;
482 }
483 continue;
484 }
485 }
486 if ($socidChosen > 0) {
487 $tpReplace[] = array($mt['start'], $mt['end'], empty($doRedactNames) ? $mt['raw'].' (socid:'.$socidChosen.')' : 'socid:'.$socidChosen);
488 }
489 }
490
491 if ($tpAsk !== null) {
492 $langs->loadLangs(array('companies', 'other'));
493 $options = array();
494 foreach ($tpAsk['candidates'] as $cid => $c) {
495 if (!$c['visible']) {
496 continue;
497 }
498 $nat = array();
499 if (in_array($c['client'], array(1, 3))) {
500 $nat[] = $langs->transnoentitiesnoconv('Customer');
501 }
502 if (in_array($c['client'], array(2, 3))) {
503 $nat[] = $langs->transnoentitiesnoconv('Prospect');
504 }
505 if ($c['fournisseur'] == 1) {
506 $nat[] = $langs->transnoentitiesnoconv('Supplier');
507 }
508 $options[] = array(
509 'id' => (int) $cid,
510 'label' => $c['name'].($c['name_alias'] !== '' ? ' ('.$c['name_alias'].')' : '').(!empty($nat) ? ' - '.implode(', ', $nat) : '').($c['town'] !== '' ? ' - '.$c['town'] : '')
511 );
512 }
513 $finalResponse = array(
514 'tool' => 'ask_for_clarification',
515 'arguments' => array(
516 'question' => $langs->transnoentitiesnoconv(count($options) === 1 ? 'AIIsThisThirdparty' : 'AIWhichThirdparty', $tpAsk['raw']),
517 'thirdparty_choice' => array('phrase' => $tpAsk['phrase'], 'options' => $options)
518 )
519 );
520 if (getDolGlobalString("AI_DEBUG")) {
521 dol_syslog("parse_intent.php ".count($options)." third parties fit '".$tpAsk['phrase']."': the user chooses before anything is sent", LOG_DEBUG, 0, '_ai');
522 }
523 ai_log_request($db, $user, aiRestoreDocumentBlocks($query, $aiDocuments['blocks']), $finalResponse, 'local', microtime(true) - $startTime, 1.0, 'Confirm');
524 ob_end_clean();
525 echo json_encode($finalResponse);
526 exit;
527 }
528
529 // Right to left, so the offsets of the earlier phrases stay valid.
530 usort($tpReplace,
536 function ($a, $b) {
537 return $b[0] - $a[0];
538 });
539 foreach ($tpReplace as $r) {
540 $query = substr($query, 0, $r[0]).$r[2].substr($query, $r[1]);
541 }
542 $query = aiRestoreDocumentBlocks($query, $aiDocuments['blocks']);
543
544 // Token usage of the LLM call, filled after the adapter answered.
545 $usageContext = array();
546
547 // Apply privacy guard if enabled
548 $guard = null;
549 if ($doRedact && class_exists('PrivacyGuard')) {
550 $guard = new PrivacyGuard();
551 $query = $guard->mask($doRedactNames ? $guard->maskNames($query, $aiNamesToMask) : $query);
552 // In-context reinforcement, adjacent to the placeholders themselves:
553 // weak models weigh nearby text far more than distant system rules, and
554 // the system-rule variant alone proved insufficient in the field.
555 if (strpos($query, '[[') !== false) {
556 $query .= "\n\n(Note: tokens like [[REF_1]] or [[ADDR_2]] above are privacy-masked real values. Use them verbatim as tool argument values - they are replaced with the real data before execution. Do not refuse the task because of them and do not ask the user to re-provide masked details.)";
557 }
558 }
559
560 // AI Execution
561 $intentJSON = null;
562 $confidence = 0.0;
563 $allToolsSchema = [];
564
565 if ($serviceKey && $serviceKey !== '-1') {
566 $providerUsed = $serviceKey;
567 $mcp = new McpHandler($db, $user, $conf, McpHandler::CTX_ASSISTANT);
568 $mcp->loadTools(); // This fill array ->loadedTools and ->toolsByName from tools found into ai/tools/
569
570 // Two schemas are maintained:
571 // $allToolsSchema — full list including system tools; used ONLY for post-LLM validation.
572 // $llmToolsBase — system tools excluded (is_system=>true filtered out in McpHandler);
573 // This separation guarantees ask_for_confirmation, respond_to_user, etc. are
574 // never visible to the model, preventing the LLM from calling them directly.
575 $allToolsSchema = $mcp->getToolsSchema();
576 $llmToolsBase = $mcp->getToolsSchemaForLLM();
577
578 $s = '';
579
580 // Special case we ask debug info: the tool families found into ai/tools/
581 if ($query == '/tools') {
582 $s .= '<div class="ai-tools-debug">';
583 $s .= '<strong>Tool families loaded ('.count($mcp->loadedTools).')</strong>';
584 $s .= '<ul class="small">';
585 foreach ($mcp->loadedTools as $familyKey => $familyInstance) {
587 $nbInFamily = count(array_filter($mcp->toolsByName, function ($instance) use ($familyInstance): bool {
588 return $instance === $familyInstance;
589 }));
590 $s .= '<li><strong>'.dol_escape_htmltag((string) $familyKey).'</strong> <span class="opacitymedium">'.dol_escape_htmltag(get_class($familyInstance)).' - '.$nbInFamily.' tool(s)</span></li>';
591 }
592 $s .= '</ul>';
593 }
594
595 // Detect if query is in a Non-Latin language (Russian, Greek, Chinese, Arabic, etc.)
596 $isComplex = isComplexScript($query);
597
598 $toolsSchema = [];
599
600 if ($isComplex) {
601 // Non-Latin: the classifier matches translated keys (user language
602 // + en_US reference), so try to narrow the schema here too - a Greek
603 // query otherwise always ships all tools, which is the largest
604 // prompt this module can build (documents + full schema overflow
605 // small-context models). No categories detected = full schema, as
606 // before.
607 $detectedCategories = classifyIntentUniversal($query, $langs);
608 if (!empty($detectedCategories)) {
609 if (getDolGlobalString("AI_DEBUG")) {
610 dol_syslog("AI Pro: Non-Latin query classified into ".implode(',', $detectedCategories).". Filtering schema.", LOG_DEBUG, 0, '_ai');
611 }
612 $toolsSchema = filterToolsProfessional($llmToolsBase, $detectedCategories);
613 } else {
614 if (getDolGlobalString("AI_DEBUG")) {
615 dol_syslog("AI Pro: Non-Latin language detected, no category match. Sending full (cleaned) schema.", LOG_DEBUG, 0, '_ai');
616 }
617 $toolsSchema = $llmToolsBase;
618 }
619 } else {
620 // Detect in which business family the query is using Hybrid (Translations + Synonyms)
621 $detectedCategories = classifyIntentUniversal($query, $langs);
622
623 // Category filter applied to $llmToolsBase — system tools already excluded
624 $toolsSchema = filterToolsProfessional($llmToolsBase, $detectedCategories);
625
626 if (getDolGlobalString("AI_DEBUG")) {
627 dol_syslog("AI Pro: Latin script. Detected: " . json_encode($detectedCategories) . ". Filtered to " . count($toolsSchema) . " tools.", LOG_DEBUG, 0, '_ai');
628 }
629 }
630
631 // If we are sending a lot of tools (Non-Latin or Fallback), we strip descriptions.
632 // The 20-tool threshold was likely chosen for GPT-3.5 (4K context window). Modern
633 // LLMs handle the full schema trivially: Gemini 2.5 Flash has a 1M token context,
634 // GPT-4o has 128K, Claude Sonnet has 1M. Compression hurts more than it helps
635 // today because it also truncates tool *descriptions* (down to 3 words), which
636 // breaks tool selection (e.g. "create_other_document" becomes "Create documents
637 // other" -- the LLM then thinks supplier_invoice creation is not available).
638 // We raise the threshold to 100 to effectively disable compression for the
639 // default install (~30 tools), while still leaving a safety net for very large
640 // custom installs that register dozens of additional addMcpTools hooks.
641 $isLargeSchema = count($toolsSchema) > 100;
642 $toolsForLLM = cleanToolSchemaForLLM($toolsSchema, $isLargeSchema);
643
644
645 // Special case we ask debug info
646 if ($query == '/tools') {
647 // Then the tools as sent to the LLM, one folded block per tool, its
648 // parameters as indented JSON.
649 $s .= '<strong>Tools sent to the LLM ('.count($toolsForLLM).')</strong>';
650 foreach ($toolsForLLM as $toolDef) {
651 $toolName = (string) ($toolDef['name'] ?? '');
652 $s .= '<details class="ai-tools-tool">';
653 $s .= '<summary><strong>'.dol_escape_htmltag($toolName).'</strong>';
654 if (isset($mcp->toolsByName[$toolName])) {
655 $s .= ' <span class="opacitymedium small">'.dol_escape_htmltag(get_class($mcp->toolsByName[$toolName])).'</span>';
656 }
657 $s .= '</summary>';
658 $s .= '<div class="small">'.dol_nl2br(dol_escape_htmltag((string) ($toolDef['description'] ?? ''), 1, 1)).'</div>';
659 $s .= '<pre class="small">'.dol_escape_htmltag((string) json_encode($toolDef['parameters'] ?? array(), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 1, 1).'</pre>';
660 $s .= '</details>';
661 }
662 $s .= '</div>';
663
664 // 'html': already escaped here, shown as is by the chat instead of
665 // going through its markdown rendering.
666 $finalResponse = [
667 "tool" => "respond_to_user",
668 "arguments" => [
669 "message" => count($toolsForLLM)." tool(s)",
670 "html" => $s
671 ]
672 ];
673
674 // Log the low confidence response
675 //ai_log_request($db, $user, $query, $finalResponse, $providerUsed, microtime(true) - $startTime, $confidence, 'low_confidence', $errorDetails, $rawRequestLog, $rawResponseLog, $usageContext);
676
677 ob_end_clean();
678 echo json_encode($finalResponse);
679 exit;
680 }
681
682
683
684 // Build System Prompt
685 $systemPrompt = getDolGlobalString('AI_INTENT_PROMPT') ?: "You are an Assistant for Dolibarr ERP CRM ".DOL_VERSION." with access to a set of tools.";
686 $systemPrompt .= "\n\n";
687
688 $systemRules = "## Rules:\n";
689 $systemRules .= "* Respond ONLY JSON and ensure any json string does not contains special chars and are correctly json encoded.\n";
690 $systemRules .= '* Answer a JSON message: {"type": "message", "content": "..."} or {"tool":..., "arguments":{...}}'."\n";
691 $systemRules .= "* ALWAYS write user-facing text (the message/question/answer argument values) in the SAME LANGUAGE as the user's message. English context notes, tool names, or schemas never change the response language.\n";
692 $systemRules .= "* When a tool matches the user request, use it to fulfill the user's request - never explain limitations, and never claim a capability is missing while a matching tool is listed.\n";
693 if (!$isFollowup) {
694 // The chat runs the search, then calls back with its result so the action can follow.
695 $systemRules .= "* If you need to do a search before performing the action the user asked, answer with a search tool `api_theobjecttosearch_list` and add \"needs_followup\": true at the top level of the JSON, next to \"tool\", the result of the search is then sent back to you to perform the action. Do not add it the requested search is itself the FINAL answer.";
696 }
697 $systemRules .= "* Only when genuinely NO tool can fulfill the request, use `respond_to_user` to say so plainly instead of guessing a business reason.\n";
698 $systemRules .= "\n";
699
700 $systemPrompt .= $systemRules;
701
702 // If option is on, we disable all tools
703 if (getDolGlobalString('AI_ASSISTANT_DISABLE_TOOLS')) {
704 $toolsForLLM = array();
705 }
706
707 $systemPrompt .= "## Tools:\n" . json_encode($toolsForLLM, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)."\n";
708 $systemPrompt .= "\n";
709
710 $systemPrompt .= "### sqlfilters format\n";
711 $systemPrompt .= "- Criterion: (field:operator:value). Each criterion has its own parentheses.\n";
712 $systemPrompt .= "- field: [a-zA-Z0-9_.]+ (e.g. t.ref).\n";
713 $systemPrompt .= "- operator: = != < > <= >= like notlike in notin is isnot.\n";
714 $systemPrompt .= "- value:\n";
715 $systemPrompt .= " - 'text' or 'date' in single quotes, number, NULL, __NOW__ or __USER_ID__;\n";
716 $systemPrompt .= " - never ( ) or a quote inside;\n";
717 $systemPrompt .= " - like/notlike: % and _ wildcards;\n";
718 $systemPrompt .= " - in/notin: comma-separated list, no parentheses (1,2,3);\n";
719 $systemPrompt .= " - is/isnot: NULL only.\n";
720 $systemPrompt .= "- Combining: terms joined only by AND or OR (uppercase, surrounded by spaces).\n";
721 $systemPrompt .= " Free nesting with balanced parentheses. No NOT.\n";
722 $systemPrompt .= " Do not mix AND and OR at the same level without parentheses.\n";
723 // Disabled for the moment, as this could not be a generic rule of sqlfilters
724 // Wait test to see why we need this. We should not need it if field fk_soc is defined on each tool.
725 //$systemPrompt .= "- No sub-query (SELECT ...): to filter on a third party, use thirdparty_ids or (t.fk_soc:=:id) with its id.\n";
726 $systemPrompt .= "\n";
727 $systemPrompt .= "Valid: ((t.client:=:1) OR (t.client:in:2,3)) AND (t.nom:like:'a%')\n";
728 $systemPrompt .= "Invalid: t.ref:=:'A' | (t.ref = 'A') | (t.status:in:(1,2)) | NOT (t.status:=:1) | ((t.a:=:1)\n";
729 $systemPrompt .= "\n";
730
731 // When redaction is active, the model sees [[TYPE_N]] placeholders where
732 // PII was. Without this rule it refuses tasks needing those values
733 // with it, placeholders travel verbatim through tool arguments and are restored server-side
734 // (unmaskAiResponse on the raw intent JSON) before execution, so the
735 // cloud never sees the data and the task still completes.
736 if ($doRedact) {
737 $systemPrompt .= "## Privacy rules\n";
738 $systemPrompt .= "Privacy masking is active: values like [[REF_1]], [[ADDR_2]], [[EMAIL_3]], [[PHONE_4]], [[ZIP_5]], [[NAME_6]] are masking real data, and socid:N is a third party given by its id. Treat them as valid values: when a tool argument needs such a datum, pass the placeholder exactly as written — it is replaced by the real value before execution. Never refuse a task because values look masked, and never invent replacements for them.";
739 $systemPrompt .= "\n";
740 }
741
742 // A bare date is not enough for weaker models: state explicitly that
743 // relative periods are the assistant's job to resolve, not the user's.
744 // Use date ISO 8601
745 $date = new DateTime('now', new DateTimeZone('UTC'));
746 $systemPrompt .= "## Context\n";
747 $systemPrompt .= "Current UTC date: " . $date->format('Y-m-d\TH:i:sP')." and User timezone: ".($_SESSION['dol_tz_string'] ?? 'UTC').".\n";
748 if (!empty($aiPageContextLine)) {
749 // Masked like the query itself: under enforced redaction the ref
750 // becomes a placeholder that is restored server-side in tool
751 // arguments; the numeric ids the tools need stay usable.
752 // Mask once, use for both the system line and the user anchor:
753 // names first (dictionary - arbitrary strings the patterns cannot
754 // see), then the pattern pass for refs, emails, IBANs and the rest.
755 if (!empty($doRedact) && !empty($guard)) {
756 $aiPageContextLine = $guard->mask($doRedactNames ? $guard->maskNames($aiPageContextLine, $ctxNamesToMask) : $aiPageContextLine);
757 }
758 $systemPrompt .= "Page context: ".$aiPageContextLine."\n";
759 }
760 // The acting user's identity: the rowid alone lets the model resolve
761 // 'me' in sqlfilters (__USER_ID__ covers it too) and in user fields of
762 // write calls — no personal data sent to the provider for this.
763 $systemPrompt .= "Current user rowid: ".((int) $user->id)."\n";
764 $systemPrompt .= "Resolve relative periods yourself from the current date — today, yesterday, this week, this month, last month, this quarter, this year — into explicit YYYY-MM-DD values for date parameters (e.g. this month = first day of the current month to the current date). Never ask the user for dates you can compute.\n";
765
766 // Get API configuration
767 $servicesList = getListOfAIServices();
768 $apiKey = getDolGlobalString('AI_API_' . strtoupper($serviceKey) . '_KEY');
769
770 if (preg_match('/^crypt:/', $apiKey)) {
771 $apiKey = dolDecrypt($apiKey, $conf->file->instance_unique_id);
772 }
773
774 $defUrl = $servicesList[$serviceKey]['url'] ?? '';
775 $url = getDolGlobalString('AI_API_' . strtoupper($serviceKey) . '_URL') ?: $defUrl;
776 // The model defaults declared in getListOfAIServices() are nested:
777 // $servicesList[$key]['textgeneration'] = ['default' => 'model-name']
778 // Reading 'textgeneration' without ['default'] returns the inner array, which
779 // then fails the (string) type-hint of UniversalLLMAdapter's 4th argument with:
780 // "Argument #4 ($model) must be of type string, array given"
781 //
782 // The admin UI (htdocs/ai/admin/setup.php "Prompt and custom AI models" tab) also
783 // stores the per-function model under AI_API_<SERVICE>_MODEL_TEXT (matching the
784 // convention already used by Ai::generateContent() for the same data). The
785 // previous lookup used AI_API_<SERVICE>_MODEL which is never written by that
786 // form, so the user-configured model was silently ignored.
787 $rawDefault = $servicesList[$serviceKey]['textgeneration'] ?? null;
788 if (is_array($rawDefault)) {
789 $defModel = $rawDefault['default'] ?? 'gpt-4o-mini';
790 } else {
791 $defModel = $rawDefault ?: 'gpt-4o-mini';
792 }
793 $prefix = 'AI_API_' . strtoupper($serviceKey);
794 $model = getDolGlobalString($prefix . '_MODEL_TEXT')
795 ?: getDolGlobalString($prefix . '_MODEL')
796 ?: $defModel;
797 // Defensive: coerce to string if anyone stored an array in this constant
798 if (is_array($model)) {
799 $model = $model['default'] ?? $defModel;
800 }
801 if (!is_string($model) || $model === '') {
802 $model = (string) $defModel;
803 }
804 // Optional per-request model override sent by the chat model picker.
805 // Sanitized to the provider model-id charset; empty/invalid = keep default.
806 if (!empty($data['model']) && is_string($data['model'])) {
807 $reqModel = preg_replace('/[^a-zA-Z0-9._:\/-]/', '', $data['model']);
808 if ($reqModel !== '' && strlen($reqModel) <= 100) {
809 $model = $reqModel;
810 }
811 }
812 $adapterType = $servicesList[$serviceKey]['adapter_type'] ?? 'openai';
813
814
815 // The request.
816 // var_dump($query);
817
818 if (!empty($apiKey)) {
819 $adapter = new UniversalLLMAdapter($adapterType, $apiKey, $url, $model, $timeout);
820
821 dol_syslog("parse_intent.php Prepare call AI API for model=".$model, LOG_DEBUG);
822 if (getDolGlobalString("AI_DEBUG")) {
823 dol_syslog("parse_intent.php Prepare call AI API for model=".$model, LOG_DEBUG, 0, '_ai');
824 }
825
826 // In-context page-context reinforcement: weak models ignore context
827 // buried in the system prompt (same lesson as the privacy
828 // placeholders) - a short line adjacent to the query is what
829 // actually works. Masked like everything else under redaction.
830 if (!empty($aiPageContextLine)) {
831 // Keep the user-turn anchor MINIMAL: verbose instructions in
832 // the user message destabilize weaker models (field-observed:
833 // hallucinated tool names appeared with the long form). The
834 // full coaching stays in the system Page-context line above.
835 $aiPageContextShort = strtok($aiPageContextLine, ".").".";
836 $query .= "\n\n(Context: ".$aiPageContextShort.")";
837 }
838
839 // Pinned context turns: past exchanges the user EXPLICITLY selected in
840 // the chat (nothing is carried over by default - context is opt-in, so
841 // its token cost is a visible, deliberate choice). Hard-sanitized here:
842 // roles constrained, embedded attachment payloads stripped (attachments
843 // stay one-shot), per-turn and global caps, privacy redaction applied.
844 $history = array();
845 if (!empty($data['history']) && is_array($data['history'])) {
846 $histTurns = array_slice($data['history'], 0, 12);
847
848 // Persisted turns arrive as ids only: the text is rebuilt here from
849 // the stored conversation (llx_ai_chat_message.content_raw), so the
850 // client is not trusted for what the conversation was. Only rows of
851 // conversations owned by the current user resolve; anything else
852 // silently drops. Turns of a not-yet-persisted message (history
853 // disabled, save still in flight) keep the inline text fallback.
854 $histIds = array();
855 foreach ($histTurns as $turn) {
856 if (is_array($turn) && !empty($turn['id']) && (int) $turn['id'] > 0) {
857 $histIds[] = (int) $turn['id'];
858 }
859 }
860 $histRows = array();
861 if (!empty($histIds)) {
862 $sqlh = "SELECT m.rowid, m.role, m.content_raw FROM ".$db->prefix()."ai_chat_message as m";
863 $sqlh .= " INNER JOIN ".$db->prefix()."ai_chat_conversation as c ON c.rowid = m.fk_conversation";
864 $sqlh .= " AND c.fk_user = ".((int) $user->id)." AND c.entity = ".((int) getEntity('ai'));
865 $sqlh .= " WHERE m.rowid IN (".$db->sanitize(implode(',', $histIds)).")";
866 $resqlh = $db->query($sqlh);
867 if ($resqlh) {
868 while ($objh = $db->fetch_object($resqlh)) {
869 $histRows[(int) $objh->rowid] = array('role' => (string) $objh->role, 'text' => (string) $objh->content_raw);
870 }
871 }
872 }
873
874 $histBudget = 6000;
875 foreach ($histTurns as $turn) {
876 if (is_array($turn) && !empty($turn['id'])) {
877 $turn = $histRows[(int) $turn['id']] ?? null;
878 }
879 if (!is_array($turn) || empty($turn['text']) || !is_string($turn['text'])) {
880 continue;
881 }
882 $htext = preg_replace('/__FILE_ATTACHMENT__\[[^\]]*\]::[^\s]+/', '[attachment removed]', $turn['text']);
883 $htext = trim((string) $htext);
884 if ($htext === '') {
885 continue;
886 }
887 if ($guard && $doRedactNames) {
888 // Names before the cut: half a name would no longer match.
889 $hdata = json_decode((string) preg_replace('/^\[[^\]]*\]\s*/', '', $htext), true);
890 $htext = $guard->maskNames($htext, array_merge($aiNamesToMask, aiThirdpartyNamesToMask($db, $user, $htext, $dynamicStopWords, $hdata)));
891 }
892 if (dol_strlen($htext) > 1500) {
893 $htext = dol_substr($htext, 0, 1500).' ...';
894 }
895 if ($guard) {
896 $htext = $guard->mask($htext);
897 }
898 $histBudget -= dol_strlen($htext);
899 if ($histBudget < 0) {
900 break;
901 }
902 $history[] = array('role' => ((($turn['role'] ?? '') === 'assistant') ? 'assistant' : 'user'), 'text' => $htext);
903 }
904 }
905
906 // Cross-conversation memory (opt-in, off by default): recent turns of
907 // this same user in OTHER chats, so "the invoice I asked about on
908 // Friday" resolves on Monday. Admin enables it with the constant
909 // AI_CHAT_MEMORY_CROSS_CONVERSATIONS; per-turn privacy masking and a
910 // hard budget apply exactly like for pinned history above.
911 if (getDolGlobalInt('AI_CHAT_MEMORY_CROSS_CONVERSATIONS')) {
912 $memSql = "SELECT m.role, m.content_raw FROM ".$db->prefix()."ai_chat_message AS m JOIN ".$db->prefix()."ai_chat_conversation AS c ON c.rowid = m.fk_conversation WHERE c.entity IN (".getEntity('ai_chat_conversation').") AND c.fk_user = ".((int) $user->id)." AND m.is_error = 0 AND m.content_raw <> ''";
913 $memConvId = !empty($data['conversation_id']) ? (int) $data['conversation_id'] : 0;
914 if ($memConvId > 0) {
915 $memSql .= " AND c.rowid <> ".((int) $memConvId);
916 }
917 $memSql .= " ORDER BY m.rowid DESC LIMIT 24";
918 $memRes = $db->query($memSql);
919 if ($memRes) {
920 $memTurns = array();
921 $memHistory = array();
922 while ($mobj = $db->fetch_object($memRes)) {
923 $memTurns[] = $mobj;
924 }
925 $memBudget = 3000;
926 foreach (array_reverse($memTurns) as $mobj) {
927 $mtext = trim((string) $mobj->content_raw);
928 if ($mtext === '') {
929 continue;
930 }
931 if ($guard) {
932 $mtext = $doRedactNames ? $guard->maskNames($mtext, $aiNamesToMask) : $mtext;
933 $mtext = $guard->mask($mtext);
934 }
935 if (dol_strlen($mtext) > 800) {
936 $mtext = dol_substr($mtext, 0, 800).' ...';
937 }
938 $memBudget -= dol_strlen($mtext);
939 if ($memBudget < 0) {
940 break;
941 }
942 $memHistory[] = array('role' => ((string) $mobj->role === 'assistant' ? 'assistant' : 'user'), 'text' => '[earlier conversation] '.$mtext);
943 }
944 // Older context goes first: cross-conversation turns before pinned.
945 $history = array_merge($memHistory, $history);
946 $db->free($memRes);
947 }
948 }
949
950 // With past turns in the payload, the model must know what they are
951 // for. Two consecutive user turns (a request whose action the user
952 // cancelled, then a new one) read as "two things to do", and with a
953 // single tool call per answer the model picks the older one - field
954 // case: "set the phone of X" answered by creating "X bis".
955 if (!empty($history)) {
956 $systemPrompt .= "\n\nCONVERSATION CONTEXT: the earlier turns are context only, to resolve references needed for final action. The ONLY request to act on is the LAST user message. Never resume, redo or complete an earlier request, even one that looks unanswered or unfinished.";
957 }
958 if ($isFollowup) {
959 $systemPrompt .= "\n\nSTEP 2: the tool you chose for the last user message was already executed; its result is the last assistant turn. Now perform the action the user asked for in the last user message, with the ids found in that result. If the result does not identify one object with certainty, or the action cannot be done, answer with respond_to_user and say why. Do not run the same search again and do not add needs_followup.";
960 }
961
962 // The rules come before the long tools list. Small models weigh what is
963 // near the question far more, and drift to another language when this
964 // rule is only up there: repeat it last.
965 $systemPrompt .= "\n\nREMINDER: write all user-facing text (message, question, answer) in the SAME LANGUAGE as the user's message.";
966
967 if (getDolGlobalString("AI_DEBUG")) {
968 dol_syslog('parse_intent.php systemPrompt = '.$systemPrompt, LOG_DEBUG, 0, '_ai');
969 dol_syslog('parse_intent.php query = '.$query, LOG_DEBUG, 0, '_ai');
970 }
971
972 // Sent as separate messages, so not in the systemPrompt (masked when privacy masking is on).
973 if (getDolGlobalString("AI_DEBUG")) {
974 dol_syslog('parse_intent.php history = '.json_encode($history, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), LOG_DEBUG, 0, '_ai');
975 }
976
977 // Call LLM and get answer string
978 if (getDolGlobalString("AI_DEBUG")) {
979 dol_syslog("parse_intent.php call LLM - ".($isFollowup ? "step ".(count($history) + 1).": followup with the result of the search sent back by the chat" : "step 1: initial request")." (history turns: ".count($history).")", LOG_INFO, 0, '_ai');
980 }
981 $rawResponse = $adapter->generate($systemPrompt, $query, 'text', $attachments, $history);
982
983 // $rawResponse should be a json string with format '{"tool":..., "arguments":{text answer}}' but sometimes it is just 'text answer'
984 // Commented for production version, the raw response can already be seen into dolibarr_curl.log
985 //dol_syslog('parse_intent.php rawResponse='.var_export($rawResponse, true), LOG_DEBUG, 0, '_ai');
986 //var_dump($rawResponse);exit;
987
988 // Capture logs
989 $rawRequestLog = $adapter->lastRequest;
990 $rawResponseLog = $adapter->lastResponse;
991
992 // Token usage for the cost columns of the request log: reported by
993 // the provider inside the response, captured by the adapter.
994 if (!empty($adapter->lastUsage)) {
995 $usageContext = array(
996 'tokens_input' => (int) ($adapter->lastUsage['input'] ?? 0),
997 'tokens_output' => (int) ($adapter->lastUsage['output'] ?? 0),
998 'model' => (string) ($adapter->lastUsage['model'] ?? $model),
999 );
1000 }
1001
1002 // Process response
1003 if (is_string($rawResponse) && strpos($rawResponse, 'Error:') === 0) {
1004 $errorDetails = $rawResponse;
1005 } elseif ($rawResponse) {
1006 // Clean JSON response
1007 $clean = preg_replace('/^```json|```$/s', '', $rawResponse);
1008 $clean = preg_replace('/^<glm_block>|<\/glm_block>$/s', '', $clean);
1009 $clean = preg_replace('/^_tool_call/s', '', $clean);
1010 $clean = trim($clean);
1011
1012 $matches = array();
1013 if (preg_match('/^\{.*\}$/s', $clean, $matches)) {
1014 $clean = $matches[0];
1015 } elseif (preg_match('/\{.*\}/s', $clean, $matches) && strpos($matches[0], '"tool"') !== false) {
1016 // The model prefixed its tool call with a sentence ("I first need
1017 // to find the third party... {"tool":...}"): the call is the
1018 // answer, the sentence is not. Without this the whole text became
1019 // a respond_to_user and the tool never ran.
1020 $clean = $matches[0];
1021 }
1022
1023 // Unmask the JSON string
1024 if ($guard) {
1025 $clean = $guard->unmaskAiResponse($clean);
1026 }
1027
1028 // Removed carriage returns and newlines
1029 $clean = preg_replace('/[\r\n]/', ' ', $clean);
1030
1031 if (getDolGlobalString("AI_DEBUG")) {
1032 dol_syslog('parse_intent.php response var clean='.$clean, LOG_DEBUG, 0, '_ai');
1033 }
1034
1035 // If answer is a json string or not
1036 if (strpos($clean, '{') === 0 || strpos($clean, '[') === 0) {
1037 // This may be a json string
1038 // { "type": "message", "content": "... }
1039 // { "tool": "api_...", "arguments": { ... } }
1040 // [ "type": "function", "function": "name": "api_...", "arguments": { ... } ]
1041
1042 $intentJSON = json_decode($clean, true);
1043 if (getDolGlobalString("AI_DEBUG")) {
1044 dol_syslog('parse_intent.php intentJSON = '.var_export($intentJSON, true), LOG_DEBUG, 0, '_ai');
1045 }
1046
1047 if (is_array($intentJSON) && ($intentJSON['tool'] ?? '') === 'respond_to_user') {
1048 // Some models put the text under 'answer', 'response', 'text'... instead of 'message'
1049 if (empty($intentJSON['arguments']['message']) && isset($intentJSON['arguments']) && is_array($intentJSON['arguments'])) {
1050 foreach (array('content', 'answer', 'response', 'text', 'reply', 'output') as $altkey) {
1051 if (!empty($intentJSON['arguments'][$altkey]) && is_string($intentJSON['arguments'][$altkey])) {
1052 $intentJSON['arguments']['message'] = $intentJSON['arguments'][$altkey];
1053 break;
1054 }
1055 }
1056 }
1057 if (empty($intentJSON['arguments']['message']) && empty($intentJSON['arguments']['content'])) {
1058 // If answer is tool=respond_to_user but without message
1059 if (getDolGlobalString("AI_DEBUG")) {
1060 dol_syslog('parse_intent.php clean response is "respond_to_user" with a message', LOG_DEBUG, 0, '_ai');
1061 }
1062 $intentJSON['arguments']['message'] = "I don't understand the answer produced by the IA for this request. Please rephrase or add details.";
1063 } else {
1064 if (getDolGlobalString("AI_DEBUG")) {
1065 dol_syslog('parse_intent.php clean response is "respond_to_user" with a messageor content', LOG_DEBUG, 0, '_ai');
1066 }
1067 $intentJSON = [
1068 "tool" => "respond_to_user",
1069 'arguments' => [
1070 "message" => $intentJSON['arguments']['message'] ?? ($intentJSON['arguments']['content'] ?? '')
1071 ]
1072 ];
1073 }
1074 } elseif (is_array($intentJSON) && ($intentJSON['tool'] ?? '') === 'ask_for_clarification' && empty($intentJSON['arguments']['question'])) {
1075 // If answer is tool=ask_for_clarification but without message
1076 if (getDolGlobalString("AI_DEBUG")) {
1077 dol_syslog('parse_intent.php clean response is "ask_for_clarification" with a question', LOG_DEBUG, 0, '_ai');
1078 }
1079
1080 $a = isset($intentJSON['arguments']) && is_array($intentJSON['arguments']) ? $intentJSON['arguments'] : array();
1081 $qparts = array();
1082 if (!empty($a['reason'])) {
1083 $qparts[] = (string) $a['reason'];
1084 }
1085 if (!empty($a['missing_argument']) && stripos(implode(' ', $qparts), (string) $a['missing_argument']) === false) {
1086 $qparts[] = "Missing: ".(string) $a['missing_argument'];
1087 }
1088 if (empty($qparts) && !empty($a['message'])) {
1089 $qparts[] = (string) $a['message'];
1090 }
1091 $intentJSON['arguments']['question'] = !empty($qparts) ? implode(' ', $qparts) : 'Could you provide the missing information?';
1092 } elseif (is_array($intentJSON) && ($intentJSON['type'] ?? '') === 'message') {
1093 // If answer is type=message.
1094 if (empty($intentJSON['content'])) {
1095 if (getDolGlobalString("AI_DEBUG")) {
1096 dol_syslog('parse_intent.php clean response is type="message" but no content for message', LOG_DEBUG, 0, '_ai');
1097 }
1098
1099 $intentJSON = [
1100 "tool" => "respond_to_user",
1101 'arguments' => [
1102 "message" => 'I could not produce an answer for this request. Please rephrase or add details.'
1103 ]
1104 ];
1105 } else {
1106 if (getDolGlobalString("AI_DEBUG")) {
1107 dol_syslog('parse_intent.php clean response is type="message" with a content as message', LOG_DEBUG, 0, '_ai');
1108 }
1109
1110 $intentJSON = [
1111 "tool" => "respond_to_user",
1112 'arguments' => [
1113 "message" => $intentJSON['content'] ?? ''
1114 ]
1115 ];
1116 }
1117 } elseif (is_array($intentJSON) && ($intentJSON['name'] ?? '') !== '' && ($intentJSON['arguments'] ?? '') !== '') {
1118 // If answer is array name: "nameoftool", "arguments": ...
1119 if (getDolGlobalString("AI_DEBUG")) {
1120 dol_syslog('parse_intent.php clean response is name="toolname" and arguments="arguments"', LOG_DEBUG, 0, '_ai');
1121 }
1122
1123 $intentJSON = [
1124 "tool" => $intentJSON['name'],
1125 'arguments' => [
1126 "message" => $intentJSON['arguments'] ?? ''
1127 ]
1128 ];
1129 } elseif (is_array($intentJSON)) {
1130 // If answer is array of type=function.
1131 if (getDolGlobalString("AI_DEBUG")) {
1132 dol_syslog('parse_intent.php clean response is array', LOG_DEBUG, 0, '_ai');
1133 }
1134
1135 foreach ($intentJSON as $f) {
1136 if (($f['type'] ?? '') === 'function' && isset($f['function']) && !empty($f['function']['name']) && !empty($f['function']['arguments'])) {
1137 $intentJSON = [
1138 "tool" => $f['function']['name'] ?? '',
1139 'arguments' => json_decode($f['function']['arguments'], true) ?? []
1140 ];
1141 break;
1142 }
1143 }
1144 }
1145 } else {
1146 $intentJSON = [
1147 "tool" => "respond_to_user",
1148 'arguments' => [
1149 "message" => $clean
1150 ]
1151 ];
1152 }
1153
1154 // Ensure no placeholders remain in the data structure.
1155 if ($guard && isset($intentJSON['arguments'])) {
1156 $intentJSON['arguments'] = recursiveUnmaskValues($intentJSON['arguments'], $guard);
1157 }
1158
1159 // Validation check: Check if the AI selected a tool that actually exists in our filtered schema.
1160 if ($intentJSON && isset($intentJSON['tool'])) {
1161 $validToolNames = array_column($allToolsSchema, 'name');
1162 if (!in_array($intentJSON['tool'], $validToolNames)) {
1163 // Near-miss name: recover only on a single match, same verb, all tokens present.
1164 $reqTokens = explode('_', dol_strtolower((string) $intentJSON['tool']));
1165 $candidates = array();
1166 foreach ($validToolNames as $realName) {
1167 if (strpos($realName, $reqTokens[0].'_') !== 0) {
1168 continue;
1169 }
1170 if (!array_diff($reqTokens, explode('_', $realName))) {
1171 $candidates[] = $realName;
1172 }
1173 }
1174 if (count($candidates) === 1) {
1175 dol_syslog("parse_intent.php AI Validation: tool '".$intentJSON['tool']."' recovered to '".$candidates[0]."'.", LOG_INFO);
1176 $intentJSON['tool'] = $candidates[0];
1177 }
1178 }
1179 if (!in_array($intentJSON['tool'], $validToolNames)) {
1180 dol_syslog("parse_intent.php AI Validation: Tool '" . $intentJSON['tool'] . "' not found in filtered schema. Send error message via respond_to_user.", LOG_WARNING);
1181
1182 // Force the standard response for non-existent functionality
1183 $intentJSON = [
1184 "tool" => "respond_to_user",
1185 "arguments" => [
1186 "message" => "I apologize, but the requested functionality is not currently available in the system."
1187 ]
1188 ];
1189 $confidence = 1.0;
1190 }
1191 }
1192
1193 // Comment this line (too verbose, kept for debug need)
1194 if (getDolGlobalString("AI_DEBUG")) {
1195 dol_syslog("parse_intent.php AI Intent: " . json_encode(['query' => $query, 'intent' => $intentJSON]), LOG_DEBUG, 0, '_ai');
1196 }
1197
1198 // Calculate confidence (only if not manually set to 1.0 above)
1199 if ($intentJSON && $confidence === 0.0) {
1200 $mappedToolsSchema = array_column($toolsSchema, null, 'name');
1201 $confidence = calculateConfidence($intentJSON, $mappedToolsSchema, $rawResponse);
1202
1203 if (getDolGlobalString("AI_DEBUG")) {
1204 dol_syslog("parse_intent.php confidence => $confidence", LOG_DEBUG, 0, '_ai');
1205 }
1206 }
1207 }
1208 }
1209 }
1210
1211 // Handle no AI Intent
1212 if (!$intentJSON || !isset($intentJSON['tool'])) {
1213 $message = $langs->transnoentitiesnoconv('AICannotUnderstandRequest');
1214 // A provider failure is not a misunderstanding: asking the user to
1215 // rephrase when Gemini answers "503 high demand" sends them the wrong
1216 // way. Say the service failed, with the provider's own reason, and
1217 // for the transient cases (overloaded, rate limited) say to retry.
1218 if (strpos($errorDetails, 'Error:') === 0) {
1219 $reason = trim(preg_replace('/^Error:\s*(API|cURL #\d+)?\s*/', '', $errorDetails));
1220 $reason = dol_trunc(preg_replace('/\s+/', ' ', $reason), 200);
1221 // The request carried a document and the provider refused it: say what to
1222 // do (many OpenAI compatible gateways do not read PDF files). Checked first:
1223 // "file parts are not supported by this model" is no unknown model.
1224 $hasPdf = false;
1225 foreach ($attachments as $att) {
1226 $hasPdf = $hasPdf || ($att['mime'] === 'application/pdf');
1227 }
1228 $attachmentWords = '/\b(?:file|files|pdf|document|image_url|image|attachment|content.?type|mime|multimodal|vision)\b/i';
1229 if (!empty($attachments) && preg_match($attachmentWords, $reason)) {
1230 $message = $langs->transnoentitiesnoconv($hasPdf ? 'AIProviderRejectedPdf' : 'AIProviderRejectedAttachment', getAiAssistantProviderLabel(), $reason);
1232 // The chosen model is unknown or retired at the provider: rephrasing
1233 // will not help, choosing another model will.
1234 $message = $langs->transnoentitiesnoconv('AIModelNotAvailableAtProvider', (isset($model) ? $model : ''), getAiAssistantProviderLabel(), $reason);
1235 } elseif (preg_match('/high demand|overloaded|rate limit|quota|too many requests|try again|timed? ?out|HTTP (429|502|503|504)/i', $errorDetails)) {
1236 $message = $langs->transnoentitiesnoconv('AIProviderBusy', $reason);
1237 } elseif (!empty($attachments) && preg_match('/HTTP (?:400|415|422)/', $errorDetails)) {
1238 $message = $langs->transnoentitiesnoconv($hasPdf ? 'AIProviderRejectedPdf' : 'AIProviderRejectedAttachment', getAiAssistantProviderLabel(), $reason);
1239 } else {
1240 $message = $langs->transnoentitiesnoconv('AIProviderError', $reason);
1241 }
1242 }
1243 $finalResponse = [
1244 "tool" => "respond_to_user",
1245 "arguments" => [
1246 "message" => $message
1247 ]
1248 ];
1249 if (strpos($errorDetails, 'Error:') === 0) {
1250 // Lets the chat tell a failed call from a real answer (e.g. keep it
1251 // out of the conversation context by default).
1252 $finalResponse['status'] = 'error';
1253 }
1254
1255 // Log the failure
1256 ai_log_request($db, $user, $query, $finalResponse, $providerUsed, microtime(true) - $startTime, 0.0, $langs->transnoentitiesnoconv('Error'), $errorDetails, $rawRequestLog, $rawResponseLog, $usageContext);
1257
1258 ob_end_clean();
1259 echo json_encode($finalResponse);
1260 exit;
1261 }
1262
1263 // Check if confirmation needed
1264 $needsConfirmation = false;
1265 $toolName = $intentJSON['tool'] ?? '';
1266
1267 // Normalize the text answer key: some models (e.g. GPT-4o) fill
1268 // respond_to_user / reject_general_question under 'response', 'text',
1269 // 'answer'... instead of the 'message' key the frontend reads, which
1270 // otherwise surfaces as "Empty AI Response".
1271 if (in_array($toolName, array('respond_to_user', 'reject_general_question'), true) && isset($intentJSON['arguments']) && is_array($intentJSON['arguments'])) {
1272 if (empty($intentJSON['arguments']['message'])) {
1273 foreach (array('response', 'text', 'answer', 'content', 'reply', 'output') as $altkey) {
1274 if (!empty($intentJSON['arguments'][$altkey])) {
1275 $intentJSON['arguments']['message'] = $intentJSON['arguments'][$altkey];
1276 break;
1277 }
1278 }
1279 }
1280 }
1281
1282 // Ask_for_confirmation is ours, built below around a real tool call; a model
1283 // that emits it by itself (it does, on a follow-up question: "do you really
1284 // want to update the phone of X?") sends the client a confirmation with
1285 // nothing to confirm, which it rejects as malformed. Hand the question to
1286 // the user as a plain answer instead: he replies, and the next turn acts.
1287 if ($toolName === 'ask_for_confirmation' && empty($intentJSON['arguments']['original_intent'])) {
1288 $question = '';
1289 foreach (array('message', 'action', 'question', 'text') as $altkey) {
1290 if (!empty($intentJSON['arguments'][$altkey]) && is_string($intentJSON['arguments'][$altkey])) {
1291 $question = $intentJSON['arguments'][$altkey];
1292 break;
1293 }
1294 }
1295 dol_syslog("parse_intent.php model emitted ask_for_confirmation without an action, downgraded to respond_to_user", LOG_WARNING);
1296 $toolName = 'respond_to_user';
1297 $intentJSON = array('tool' => 'respond_to_user', 'arguments' => array('message' => ($question !== '' ? $question : $langs->transnoentitiesnoconv('AICannotUnderstandRequest'))));
1298 }
1299
1300 // Tools answered by the chat itself, never a step of a write.
1301 $systemTools = array('respond_to_user', 'reject_general_question', 'ask_for_clarification', 'ask_for_confirmation', 'navigate_to_page', 'generate_navigation_url');
1302
1303 // Followup round: name the object the search resolved, so the confirmation
1304 // says "NLTechno (id 10)" and not only "socid 10". The search result is sent
1305 // back by the chat for this display only, it never goes to the model.
1306 $resolvedLabel = '';
1307 if ($isFollowup && !empty($data['followup_result']) && is_array($data['followup_result']) && isset($intentJSON['arguments']) && is_array($intentJSON['arguments'])) {
1308 $resolvedLabel = aiLabelOfResolvedObject($data['followup_result'], $intentJSON['arguments']);
1309 }
1310
1311 if ($askForConfirmation > 0) {
1312 $isModifyOperation = preg_match('/(create|update|delete|add|remove|modify|edit)/i', $toolName);
1313
1314 if ($askForConfirmation == 1 && $isModifyOperation) {
1315 $needsConfirmation = true;
1316 } elseif ($askForConfirmation == 2) {
1317 $needsConfirmation = true;
1318 }
1319 }
1320
1321 // A write gated by its tool is confirmed whatever AI_ASK_FOR_CONFIRMATION
1322 // says: the tool will not run without the user's approval anyway, and
1323 // without this card the chat received the bare confirmation request and
1324 // nothing was ever written.
1325 if (!$needsConfirmation && is_array($intentJSON) && $toolName !== '' && isset($mcp) && is_object($mcp)) {
1326 $gatedTool = $mcp->toolsByName[$toolName] ?? null;
1327 $gatedArgs = (isset($intentJSON['arguments']) && is_array($intentJSON['arguments'])) ? $intentJSON['arguments'] : array();
1328 if (is_object($gatedTool) && method_exists($gatedTool, 'writeConfirmationPreview') && ((string) $gatedTool->writeConfirmationPreview($toolName, $gatedArgs)) !== McpTool::NO_WRITE) {
1329 $needsConfirmation = true;
1330 }
1331 }
1332 // A write the tool already refuses (its third party missing, unknown or
1333 // ambiguous) is answered now, with nothing to approve.
1334 if ($needsConfirmation && isset($mcp) && is_object($mcp)) {
1335 $gatedTool = $mcp->toolsByName[$toolName] ?? null;
1336 $gatedArgs = (isset($intentJSON['arguments']) && is_array($intentJSON['arguments'])) ? $intentJSON['arguments'] : array();
1337 $refusal = ($gatedTool instanceof McpTool) ? (string) $gatedTool->writePrecheck($toolName, $gatedArgs) : '';
1338 if ($refusal !== '') {
1339 $finalResponse = array('tool' => 'respond_to_user', 'arguments' => array('message' => $refusal));
1340 ai_log_request($db, $user, $query, $finalResponse, $providerUsed, microtime(true) - $startTime, $confidence, 'Error', $refusal, $rawRequestLog, $rawResponseLog, $usageContext);
1341 ob_end_clean();
1342 echo json_encode($finalResponse);
1343 exit;
1344 }
1345 }
1346
1347 if (getDolGlobalString("AI_DEBUG")) {
1348 dol_syslog("parse_intent.php needsConfirmation=" . (string) $needsConfirmation, LOG_DEBUG, 0, '_ai');
1349 }
1350
1351 // Handle confirmation
1352 if ($needsConfirmation) {
1353 $allToolsMap = !empty($allToolsSchema)
1354 ? array_column($allToolsSchema, null, 'name')
1355 : [];
1356 $toolDescription = $allToolsMap[$toolName]['description'] ?? 'No description available';
1357 $arguments = $intentJSON['arguments'] ?? [];
1358
1359 $details = formatArgumentsForDisplay($arguments);
1360 $action = extractActionFromTool($toolName);
1361
1362 // A write tool describes its own effect in a sentence, which is what the
1363 // user has to act on: prefer it over the raw argument dump, and keep the
1364 // dump underneath for the detail.
1365 $toolInstance = $mcp->toolsByName[$toolName] ?? null;
1366 if (is_object($toolInstance) && method_exists($toolInstance, 'writeConfirmationPreview')) {
1367 $preview = (string) $toolInstance->writeConfirmationPreview($toolName, $arguments);
1368 if ($preview !== McpTool::NO_WRITE) {
1369 $action = $preview;
1370 }
1371 }
1372 if ($resolvedLabel !== '') {
1373 // The id came from a search the user never saw: say which object it is.
1374 $action .= ' - '.dol_escape_htmltag($resolvedLabel);
1375 }
1376
1377 $confirmationResponse = [
1378 "tool" => "ask_for_confirmation",
1379 "arguments" => [
1380 "action" => $action,
1381 "details" => $details,
1382 "original_intent" => $intentJSON
1383 ]
1384 ];
1385
1386 // Log the confirmation request
1387 ai_log_request($db, $user, $query, $confirmationResponse, $providerUsed, microtime(true) - $startTime, $confidence, $langs->transnoentitiesnoconv("Confirm"), $errorDetails, $rawRequestLog, $rawResponseLog, $usageContext);
1388
1389 ob_end_clean();
1390 echo json_encode($confirmationResponse);
1391 exit;
1392 }
1393
1394 // Handle low confidence
1395 if ($confidence < LOW_CONFIDENCE) {
1396 $finalResponse = [
1397 "tool" => "respond_to_user",
1398 "arguments" => [
1399 "message" => "I'm not confident about understanding your request. Please try rephrasing it with more specific details."
1400 ]
1401 ];
1402
1403 // Log the low confidence response
1404 ai_log_request($db, $user, $query, $finalResponse, $providerUsed, microtime(true) - $startTime, $confidence, 'low_confidence', $errorDetails, $rawRequestLog, $rawResponseLog, $usageContext);
1405
1406 ob_end_clean();
1407 echo json_encode($finalResponse);
1408 exit;
1409 }
1410
1411 // Add confidence note
1412 if ($confidence < MEDIUM_CONFIDENCE && isset($intentJSON['arguments'])) {
1413 $intentJSON['arguments']['_confidence_note'] = "I'm moderately confident about this interpretation. Please verify the results.";
1414 }
1415
1416 // The model answered with a read that is only a step toward the action
1417 // asked: either it says so itself (needs_followup in its JSON), or a write
1418 // was asked and it answered with a read (a search for the id of the object
1419 // to change). Tell the chat to send the result of that read back here, with
1420 // followup=1, so the model can perform the action.
1421 // Never on a followup call itself: one extra round at most, no loop.
1422 $llmAsksFollowup = !empty($intentJSON['needs_followup']);
1423 unset($intentJSON['needs_followup']);
1424 if (!$isFollowup && isset($mcp) && is_object($mcp)
1425 && !in_array($toolName, $systemTools, true)
1426 && !aiIsWriteTool($mcp, $toolName, (isset($intentJSON['arguments']) && is_array($intentJSON['arguments'])) ? $intentJSON['arguments'] : array())
1427 && ($llmAsksFollowup || aiQueryAsksForWrite($query, $langs))) {
1428 $intentJSON['needs_followup'] = 1;
1429 }
1430 if (!empty($intentJSON['needs_followup'])) {
1431 if (getDolGlobalString("AI_DEBUG")) {
1432 dol_syslog("parse_intent.php needs_followup on ".$toolName." (".($llmAsksFollowup ? 'asked by the model' : 'write asked, read answered').")", LOG_DEBUG, 0, '_ai');
1433 }
1434 } else {
1435 if (getDolGlobalString("AI_DEBUG")) {
1436 dol_syslog("parse_intent.php needs_followup off ".$toolName, LOG_DEBUG, 0, '_ai');
1437 }
1438 }
1439
1440 if ($resolvedLabel !== '') {
1441 // For the confirmation the chat shows when the write tool asks for it.
1442 $intentJSON['resolved_label'] = $resolvedLabel;
1443 }
1444
1445 // Success!
1446 $finalResponse = $intentJSON;
1447 $execTime = microtime(true) - $startTime;
1448 ai_log_request($db, $user, $query, $finalResponse, $providerUsed, $execTime, $confidence, $langs->transnoentitiesnoconv("Success"), $errorDetails, $rawRequestLog, $rawResponseLog, $usageContext);
1449
1450 ob_end_clean();
1451 echo json_encode($finalResponse);
1452} catch (Throwable $e) {
1453 $friendlyResponse = [
1454 "tool" => "respond_to_user",
1455 "arguments" => [
1456 "message" => "I'm experiencing technical difficulties. Please try again later or contact your administrator."
1457 ]
1458 ];
1459
1460 $realErrorForLog = "PHP Exception: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine();
1461
1462 dol_syslog("AI Critical Error: " . $realErrorForLog, LOG_ERR);
1463 if (getDolGlobalString("AI_DEBUG")) {
1464 dol_syslog("AI Critical Error: " . $realErrorForLog, LOG_ERR, 0, '_ai');
1465 }
1466
1467 if (function_exists('ai_log_request') && is_object($db)) {
1469 $db,
1470 $user,
1471 $query ?? 'unknown',
1472 $friendlyResponse,
1473 $providerUsed,
1474 microtime(true) - $startTime,
1475 0.0,
1476 'error',
1477 $realErrorForLog,
1478 $rawRequestLog ?? '',
1479 $rawResponseLog ?? '',
1480 $usageContext ?? array()
1481 );
1482 }
1483
1484 ob_end_clean();
1485 echo json_encode($friendlyResponse);
1486}
1487
1488
1498function aiQueryAsksForWrite($query, $langs)
1499{
1500 $verbs = array();
1501 foreach (array('Create', 'Add', 'Modify', 'Update', 'Delete', 'Remove', 'Validate', 'Send') as $key) {
1502 $word = dol_strtolower((string) $langs->transnoentities($key));
1503 if ($word !== '' && $word !== dol_strtolower($key)) {
1504 $verbs[] = $word;
1505 }
1506 }
1507 $verbs = array_merge($verbs, array(
1508 'create', 'add', 'modify', 'update', 'delete', 'remove', 'validate', 'send', 'change', 'set', 'edit', 'rename', 'close', 'cancel',
1509 'crée', 'cree', 'créer', 'creer', 'ajoute', 'ajouter', 'modifie', 'modifier', 'mets', 'met', 'mettre', 'change', 'changer', 'passe', 'passer',
1510 'supprime', 'supprimer', 'efface', 'effacer', 'retire', 'retirer', 'valide', 'valider', 'envoie', 'envoyer', 'enregistre', 'enregistrer', 'renomme', 'renommer', 'clôture', 'cloture', 'annule', 'annuler'
1511 ));
1512 $escaped = array();
1513 foreach (array_unique(array_filter($verbs)) as $verb) {
1514 $escaped[] = preg_quote($verb, '/');
1515 }
1516 $pattern = '/(^|[^\p{L}])('.implode('|', $escaped).')([^\p{L}]|$)/iu';
1517 return (bool) preg_match($pattern, dol_strtolower((string) $query));
1518}
1519
1528function aiIsWriteTool($mcp, $name, array $args)
1529{
1530 if (preg_match('/(create|update|delete|add|remove|modify|edit|validate|pay|send)/i', $name)) {
1531 return true;
1532 }
1533 $inst = $mcp->toolsByName[$name] ?? null;
1534 if (is_object($inst) && method_exists($inst, 'writeConfirmationPreview')) {
1535 return ((string) $inst->writeConfirmationPreview($name, $args)) !== McpTool::NO_WRITE;
1536 }
1537 return false;
1538}
1539
1548function aiLabelOfResolvedObject(array $readResult, array $writeArgs)
1549{
1550 $ids = aiIdsOfArguments($writeArgs);
1551 if (empty($ids)) {
1552 return '';
1553 }
1554 $rows = $readResult;
1555 if (isset($rows['data']) && is_array($rows['data'])) {
1556 $rows = $rows['data'];
1557 }
1558 if (isset($rows['id']) || isset($rows['rowid'])) {
1559 $rows = array($rows);
1560 }
1561 foreach ($rows as $row) {
1562 if (!is_array($row)) {
1563 continue;
1564 }
1565 $rowid = (int) ($row['id'] ?? $row['rowid'] ?? 0);
1566 if ($rowid > 0 && in_array($rowid, $ids, true)) {
1567 foreach (array('name', 'nom', 'ref', 'label', 'subject', 'title', 'login') as $key) {
1568 if (!empty($row[$key]) && is_string($row[$key])) {
1569 return dol_trunc($row[$key], 80).' (id '.$rowid.')';
1570 }
1571 }
1572 }
1573 }
1574 return '';
1575}
1576
1584function aiIdsOfArguments(array $args)
1585{
1586 $ids = array();
1587 foreach ($args as $k => $v) {
1588 if (is_string($v) && preg_match('/^\s*\{/', $v)) {
1589 $decoded = json_decode($v, true);
1590 if (is_array($decoded)) {
1591 $v = $decoded;
1592 }
1593 }
1594 if (is_array($v)) {
1595 $ids = array_merge($ids, aiIdsOfArguments($v));
1596 } elseif ((is_int($v) || (is_string($v) && ctype_digit($v))) && preg_match('/(^id$|_id$|^socid$|^fk_)/', (string) $k)) {
1597 $ids[] = (int) $v;
1598 }
1599 }
1600 return $ids;
1601}
1602
1621function recursiveUnmaskValues($data, ?PrivacyGuard $guard)
1622{
1623 if ($guard === null) {
1624 return $data;
1625 }
1626
1627 if (is_array($data)) {
1628 return array_map(
1633 function ($item) use ($guard) {
1634 return recursiveUnmaskValues($item, $guard);
1635 },
1636 $data
1637 );
1638 }
1639
1640 if (is_string($data)) {
1641 return $guard->unmask($data);
1642 }
1643
1644 return $data;
1645}
1646
1659function isComplexScript(string $text)
1660{
1661 // CJK (Chinese, Japanese, Korean)
1662 if (preg_match('/\p{Han}|\p{Hiragana}|\p{Katakana}|\p{Hangul}/u', $text)) {
1663 return true;
1664 }
1665
1666 // Cyrillic (Russian, Ukrainian, Bulgarian, Serbian)
1667 if (preg_match('/\p{Cyrillic}/u', $text)) {
1668 return true;
1669 }
1670
1671 // Greek
1672 if (preg_match('/\p{Greek}/u', $text)) {
1673 return true;
1674 }
1675
1676 // Arabic
1677 if (preg_match('/\p{Arabic}/u', $text)) {
1678 return true;
1679 }
1680
1681 // Hebrew
1682 if (preg_match('/\p{Hebrew}/u', $text)) {
1683 return true;
1684 }
1685
1686 // Thai
1687 if (preg_match('/\p{Thai}/u', $text)) {
1688 return true;
1689 }
1690
1691 return false;
1692}
1693
1708function aiNormalizeForMatch($word, $asStem = false)
1709{
1710 $w = dol_strtolower(trim($word), 'UTF-8');
1711 if (class_exists('Normalizer')) {
1712 $decomposed = Normalizer::normalize($w, Normalizer::FORM_D);
1713 if ($decomposed !== false) {
1714 $w = (string) preg_replace('/\p{Mn}+/u', '', $decomposed);
1715 }
1716 }
1717 if ($asStem) {
1718 $len = dol_strlen($w);
1719 if ($len >= 6) {
1720 $w = dol_substr($w, 0, $len - 2); // drop the inflected tail
1721 } elseif ($len == 5) {
1722 $w = dol_substr($w, 0, 4);
1723 } elseif ($len < 3) {
1724 return ''; // too short to stem: matching it would be noise
1725 }
1726 }
1727 return $w;
1728}
1729
1752function classifyIntentUniversal(string $query, Translate $langs)
1753{
1754 $isLatin = !isComplexScript($query);
1755 $searchQuery = $isLatin ? strtolower(dol_string_unaccent($query)) : $query;
1756
1757 $langs->loadLangs(array("main", "bills", "orders", "propal", "companies", "products", "projects", "dict", "sendings", "receptions", "ticket", "members", "agenda", "interventions"));
1758
1759 // Vocabulary rule: every object family whose tools exist must light up the
1760 // categories those tools carry (see ApiBridge::ENDPOINT_CATEGORIES), or the
1761 // prompt filter drops them and the model claims the feature does not exist
1762 // (that is how receptions were lost before). Families WITHOUT any bridged
1763 // tool (bank accounts, donations, holidays) are deliberately absent: their
1764 // words would activate categories that hold no matching tool and only
1765 // narrow the prompt wrongly - add the endpoint first, the vocabulary second.
1766 $intentMap = [
1767 'billing' => [
1768 // Member/Subscription: members and subscriptions tools are
1769 // categorized ['thirdparty', 'billing'].
1770 'keys' => ['Bill', 'Invoice', 'Payment', 'Cheque', 'VAT', 'BillStatusUnpaid', 'BillStatusPaid', 'BillStatusDraft', 'Member', 'Subscription'],
1771 'synonyms' => ['paid', 'unpaid', 'pay', 'money', 'cost', 'amount', 'overdue', 'member', 'membership', 'subscription', 'cotisation', 'adhesion']
1772 ],
1773 'commercial' => [
1774 // 'Reception' and 'Shipment' matter: create_other_document (the tool
1775 // that creates receptions/shipments) is categorized 'commercial', so a
1776 // query like "create a reception from this delivery note" must light
1777 // this category up or the creation tool is filtered out of the prompt
1778 // and the model honestly answers it cannot create receptions.
1779 // Intervention: interventions tools are ['project', 'commercial'].
1780 'keys' => ['Order', 'Proposal', 'Quote', 'SupplierOrder', 'OrderStatusDraft', 'Reception', 'Shipment', 'Delivery', 'Intervention'],
1781 'synonyms' => ['sale', 'buy', 'purchase', 'contract', 'shipping', 'quote', 'reception', 'shipment', 'delivery', 'receive', 'intervention']
1782 ],
1783 'thirdparty' => [
1784 // Ticket and agenda-event tools are ['thirdparty', 'project'];
1785 // members/subscriptions are ['thirdparty', 'billing']; the
1786 // categories endpoint is ['thirdparty', 'stock'] (its 'Category'
1787 // UI key translates to 'Tag/category' - unusable as a keyword,
1788 // hence plain synonyms).
1789 'keys' => ['ThirdParty', 'Customer', 'Supplier', 'Contact', 'Company', 'Ticket', 'Member', 'Subscription', 'Event', 'Agenda'],
1790 'synonyms' => ['client', 'partner', 'address', 'phone', 'vendor', 'ticket', 'support', 'incident', 'member', 'adherent', 'membership', 'meeting', 'appointment', 'rdv', 'category', 'categorie', 'tag']
1791 ],
1792 'stock' => [
1793 'keys' => ['Product', 'Service', 'Stock', 'Warehouse'],
1794 'synonyms' => ['item', 'inventory', 'sku', 'location', 'qty', 'warehouse', 'category', 'categorie', 'tag']
1795 ],
1796 'project' => [
1797 'keys' => ['Project', 'Task', 'Ticket', 'Event', 'Agenda', 'Intervention'],
1798 'synonyms' => ['task', 'team', 'deadline', 'planning', 'milestone', 'ticket', 'event', 'meeting', 'appointment', 'rdv', 'intervention']
1799 ],
1800 'reporting' => [
1801 'keys' => ['Report', 'Statistics', 'Turnover', 'Revenue', 'Income'],
1802 'synonyms' => ['report', 'statistics', 'analytics', 'chart', 'total', 'turnover']
1803 ]
1804 ];
1805
1806 // en_US reference translator (loaded once; Translate caches files)
1807 static $langsEnUs = null;
1808 if ($langsEnUs === null) {
1809 global $conf;
1810 $langsEnUs = new Translate('', $conf);
1811 $langsEnUs->setDefaultLang('en_US');
1812 $langsEnUs->loadLangs(array('main', 'bills', 'companies', 'products', 'projects', 'orders', 'propal', 'stocks', 'other', 'ticket', 'members', 'agenda', 'interventions'));
1813 }
1814
1815 $detectedCategories = [];
1816 foreach ($intentMap as $category => $data) {
1817 $keywords = [];
1818 foreach ($data['keys'] as $key) {
1819 // Matched in the user's language AND in the en_US reference: keys
1820 // translate to the UI language only, but users routinely type
1821 // English terms on non-English installs. One mechanism, no
1822 // separate synonym lists to translate.
1823 foreach (array($langs->transnoentities($key), $langsEnUs->transnoentities($key)) as $trans) {
1824 if ($trans === '' || ($trans === $key && preg_match('/[A-Z]/', dol_substr($key, 1, 200)))) {
1825 // Untranslated composite key (e.g. 'BillStatusUnpaid'):
1826 // matching it would be noise. A plain word equal to its
1827 // key ('Customer' in en_US) is a real keyword - keep it.
1828 continue;
1829 }
1830 if ($isLatin) {
1831 $trans = strtolower(dol_string_unaccent($trans));
1832 }
1833 $keywords[] = $trans;
1834 }
1835 }
1836 $keywords = array_unique($keywords);
1837
1838 // Hardcoded English colloquialisms (words no UI key carries) - words no
1839 // UI key carries; translated vocabulary already arrives through the
1840 // dual-language keys above.
1841 foreach ($data['synonyms'] as $syn) {
1842 $keywords[] = $isLatin ? strtolower(dol_string_unaccent($syn)) : $syn;
1843 }
1844 $keywords = array_unique($keywords);
1845
1846 $normalizedQuery = aiNormalizeForMatch($searchQuery, false);
1847 foreach ($keywords as $word) {
1848 if (empty($word)) {
1849 continue;
1850 }
1851 if ($isLatin) {
1852 if (preg_match('/\b' . preg_quote($word, '/') . 's?\b/u', $searchQuery)) {
1853 $detectedCategories[] = $category;
1854 break;
1855 }
1856 } else {
1857 // Inflection-tolerant: stem the keyword, normalize the query,
1858 // then substring-match. Natural-word translations work as-is.
1859 $stem = aiNormalizeForMatch($word, true);
1860 $needleStem = $stem;
1861 if ($needleStem !== '' && mb_strpos($normalizedQuery, $needleStem) !== false) {
1862 $detectedCategories[] = $category;
1863 break;
1864 }
1865 }
1866 }
1867 }
1868 return $detectedCategories;
1869}
1870
1891function filterToolsProfessional(array $allTools, array $activeCategories)
1892{
1893 if (empty($activeCategories) || (count($activeCategories) === 1 && $activeCategories[0] === 'global')) {
1894 return $allTools;
1895 }
1896
1897 $targetCategories = array_merge(['global'], $activeCategories);
1898 $filtered = [];
1899
1900 foreach ($allTools as $tool) {
1901 $toolCats = $tool['categories'] ?? ['global'];
1902 if (count(array_intersect($toolCats, $targetCategories)) > 0) {
1903 if (count($activeCategories) > 0 && $toolCats === ['global']) {
1904 continue;
1905 }
1906 $filtered[] = $tool;
1907 }
1908 }
1909
1910 if (count($filtered) < 3) {
1911 dol_syslog("AI Filter: Too few tools (" . count($filtered) . "). Reverting to full schema.", LOG_WARNING);
1912 return $allTools;
1913 }
1914
1915 return $filtered;
1916}
1917
1926function cleanToolSchemaForLLM(array $tools, bool $isLargeSchema = false)
1927{
1928 $cleaned = [];
1929
1930 foreach ($tools as $tool) {
1931 // Tool descriptions are how the LLM selects the right tool -- never truncate
1932 // them, even when the schema is large. Truncating to 3 words ("Create documents
1933 // other", "Add a single") breaks tool selection. If the schema really is too
1934 // big for the chosen model, the right answer is to filter the toolset before
1935 // it reaches the LLM (which is what filterToolsProfessional() already does
1936 // upstream of this function), not to mutilate each tool's description.
1937 // Parameter-level compression (stripping defaults, descriptions of optional
1938 // fields, etc.) remains gated on $isLargeSchema below.
1939 $desc = $tool['description'];
1940
1941 // Get parameters
1942 $toolParams = $tool['parameters'] ?? $tool['inputSchema'] ?? [];
1943
1944 if ($isLargeSchema && isset($toolParams['properties']) && is_array($toolParams['properties'])) {
1945 $requiredList = $toolParams['required'] ?? [];
1946 $newProperties = [];
1947
1948 foreach ($toolParams['properties'] as $propKey => $propData) {
1949 $isRequired = in_array($propKey, $requiredList);
1950
1951 // -----------------------------------------------------------
1952 // Remove Optional Parameters with Defaults
1953 // -----------------------------------------------------------
1954 // If a parameter is optional and has a default value defined in
1955 // the schema, we assume the backend will handle it. We remove it
1956 // from the prompt entirely. This saves massive amounts of tokens
1957 // on list/search functions (limit, sortorder, sqlfilters, etc).
1958 // -----------------------------------------------------------
1959 if (!$isRequired && isset($propData['default'])) {
1960 continue;
1961 }
1962
1963 // Remove 'type' for string (LLM default), Keep others (int/bool/arr)
1964 if (isset($propData['type']) && $propData['type'] === 'string') {
1965 unset($propData['type']);
1966 }
1967
1968 // Handle Descriptions
1969 // Remove descriptions entirely. Rely on the key name (e.g. 'email', 'qty').
1970 // Exception: Keep 1 word if it's a required parameter with a confusing name.
1971 if (isset($propData['description'])) {
1972 unset($propData['description']);
1973 // If we want to keep a tiny hint for required params, uncomment below:
1974 // if ($isRequired) {
1975 // $propData['description'] = explode(' ', trim($propData['description']))[0];
1976 // }
1977 }
1978
1979 // Collapse Complex Objects
1980 // If a parameter is a deep object (like a complex filter), replace the
1981 // recursive properties definition with a generic string to save tokens.
1982 if (isset($propData['type']) && $propData['type'] === 'object' && isset($propData['properties'])) {
1983 unset($propData['properties']);
1984 unset($propData['required']);
1985 $propData['description'] = "JSON object"; // Minimal hint
1986 }
1987
1988 $newProperties[$propKey] = $propData;
1989 }
1990
1991 $toolParams['properties'] = $newProperties;
1992
1993 // Clean up root metadata
1994 unset($toolParams['type']);
1995 unset($toolParams['additionalProperties']);
1996 }
1997
1998 $cleaned[] = [
1999 'name' => $tool['name'],
2000 'description' => $desc,
2001 'parameters' => $toolParams
2002 ];
2003 }
2004
2005 return $cleaned;
2006}
2007
2020function calculateConfidence($intentJSON, $toolsSchema, $rawResponse)
2021{
2022 $confidence = 0.0;
2023 $factors = [];
2024
2025 // Factor 1: JSON parsing success (Weight: 40%)
2026 // If we are here, the JSON generally parsed, but we check if the structure is valid.
2027 $factors['parse_success'] = 0.4;
2028
2029 // Factor 2: Response completeness (Weight: 30%)
2030 // Check if we have a tool name and some arguments.
2031 $hasRequiredFields = !empty($intentJSON['tool']) && !empty($intentJSON['arguments']);
2032 $factors['completeness'] = $hasRequiredFields ? 0.3 : 0.0;
2033
2034 // Factor 3: Schema validation (Weight: 20%)
2035 $isValidSchema = false;
2036
2037 // Ensure the tool exists in our known schema
2038 if (isset($intentJSON['tool']) && isset($toolsSchema[$intentJSON['tool']])) {
2039 // Support both 'parameters' and 'inputSchema'
2040 $schema = $toolsSchema[$intentJSON['tool']]['parameters']
2041 ?? $toolsSchema[$intentJSON['tool']]['inputSchema']
2042 ?? [];
2043
2044 // Extract parameters provided by the AI
2045 $providedParams = array_keys($intentJSON['arguments'] ?? []);
2046
2047 // Standard JSON Schema structure uses 'properties' to list params and 'required' to list mandatory ones.
2048 $properties = $schema['properties'] ?? [];
2049 $requiredList = $schema['required'] ?? [];
2050
2051 $missingParams = [];
2052
2053 // Iterate through the schema properties to check required fields
2054 foreach ($properties as $paramKey => $paramDetails) {
2055 // Check if this specific parameter is marked as required in the schema
2056 if (in_array($paramKey, $requiredList)) {
2057 // If it is required but not in the AI's provided arguments, it's missing.
2058 if (!in_array($paramKey, $providedParams)) {
2059 $missingParams[] = $paramKey;
2060 }
2061 }
2062 }
2063
2064 // If no required parameters are missing, schema validation passes.
2065 $isValidSchema = empty($missingParams);
2066 }
2067
2068 $factors['schema_validation'] = $isValidSchema ? 0.2 : 0.0;
2069
2070 // Factor 4: Response quality (Weight: 10%)
2071 $qualityScore = 0.0;
2072 if (is_string($rawResponse)) {
2073 // Check for error indicators in the raw text (e.g., "I'm sorry", "Error")
2074 if (!preg_match('/error|fail|unable|cannot|sorry/i', $rawResponse)) {
2075 $qualityScore += 0.05;
2076 }
2077
2078 // Verify the tool actually exists in our registry (double check)
2079 if (isset($intentJSON['tool']) && isset($toolsSchema[$intentJSON['tool']])) {
2080 $qualityScore += 0.05;
2081 }
2082 }
2083 $factors['response_quality'] = $qualityScore;
2084
2085 // Calculate Total Confidence
2086 $confidence = array_sum($factors);
2087
2088 // Ensure confidence stays within bounds [0, 1]
2089 return max(0.0, min(1.0, $confidence));
2090}
2091
2098function formatArgumentsForDisplay($arguments)
2099{
2100 $formattedArgs = [];
2101 foreach ($arguments as $key => $value) {
2102 if (is_array($value)) {
2103 $formattedArgs[] = "- {$key}: " . (empty($value) ? "(empty)" : json_encode($value, JSON_PRETTY_PRINT));
2104 } else {
2105 $formattedArgs[] = "- {$key}: {$value}";
2106 }
2107 }
2108 return implode("\n", $formattedArgs);
2109}
2110
2117function extractActionFromTool($toolName)
2118{
2119 $matches = array();
2120 if (preg_match('/^(create|update|delete|list|show|find|search|get|view|validate|send)/i', $toolName, $matches)) {
2121 return strtolower($matches[1]);
2122 }
2123 return 'perform this action';
2124}
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
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
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
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
ai_validate_attachments(array $attachments, &$error)
Validate chat attachments before they reach any LLM provider.
Definition ai.lib.php:317
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
getAiAssistantProviderLabel()
Resolve the AI provider/service currently configured for the AI Assistant (e.g.
Definition ai.lib.php:618
aiCheckCsrfToken($context='')
Check the anti-CSRF token of a request sent to one of the AI Assistant endpoints.
Definition ai.lib.php:1260
$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 handle MCP (Model Context Protocol).
Definition mcp.class.php:40
Abstract base class for all MCP (Model Context Protocol) tools.
const NO_WRITE
Returned by writeConfirmationPreview() when the tool writes nothing.
Class to manage privacy data masking and unmasking.
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
static isModelUnavailableError(string $msg)
Tell if a provider error message says the requested model is unknown, retired or not available (not a...
dol_strtolower($string, $encoding="UTF-8")
Convert a string to lower.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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_substr($string, $start, $length=null, $stringencoding='', $trunconbytes=0)
Make a substring.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Definition html.lib.php:183
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
extractActionFromTool($toolName)
Extract action from tool name.
formatArgumentsForDisplay($arguments)
Format arguments for display in confirmation.
isComplexScript(string $text)
Detects if the query uses Non-Latin Scripts.
aiIsWriteTool($mcp, $name, array $args)
Does the tool write (create / update / delete / ...)?
calculateConfidence($intentJSON, $toolsSchema, $rawResponse)
Calculate confidence score based on multiple factors.
classifyIntentUniversal(string $query, Translate $langs)
Detect intent categories from a user query.
catch(Throwable $e) aiQueryAsksForWrite($query, $langs)
Does the user's message ask for a write (create / update / delete / ...)? Translated keys of the curr...
aiNormalizeForMatch($word, $asStem=false)
Normalize a keyword or query for inflection-tolerant non-Latin matching.
aiLabelOfResolvedObject(array $readResult, array $writeArgs)
Name of the object a write targets, taken from the read result that produced its id (so the confirmat...
filterToolsProfessional(array $allTools, array $activeCategories)
Filter a list of tools based on active intent categories.
recursiveUnmaskValues($data, ?PrivacyGuard $guard)
Recursively unmask values in a dataset.
cleanToolSchemaForLLM(array $tools, bool $isLargeSchema=false)
Compresses tool schema by removing optional parameters with defaults and stripping descriptions,...
aiIdsOfArguments(array $args)
Ids passed to a tool: values of the id-like keys (id, *_id, socid, fk_*), also inside nested arrays a...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.