dolibarr 25.0.0-alpha
parse_intent.php File Reference

File to handle MCP (Model Context Protocol) Intent Parsing This service receive a prompt, format and complete it with list of tools, send it to AI service and return the answer. More...

Go to the source code of this file.

Functions

catch(Throwable $e) aiQueryAsksForWrite ($query, $langs)
 Does the user's message ask for a write (create / update / delete / ...)? Translated keys of the current language plus the short verbs users type in French and English whatever the UI language; whole words only.
 
 aiLabelOfResolvedObject (array $readResult, array $writeArgs)
 Name of the object a write targets, taken from the read result that produced its id (so the confirmation names "Dupont SA", not "socid 2305").
 
 recursiveUnmaskValues ($data, ?PrivacyGuard $guard)
 Recursively unmask values in a dataset.
 
 isComplexScript (string $text)
 Detects if the query uses Non-Latin Scripts.
 
 aiNormalizeForMatch ($word, $asStem=false)
 Normalize a keyword or query for inflection-tolerant non-Latin matching.
 
 classifyIntentUniversal (string $query, Translate $langs)
 Detect intent categories from a user query.
 
 filterToolsProfessional (array $allTools, array $activeCategories)
 Filter a list of tools based on active intent categories.
 
 cleanToolSchemaForLLM (array $tools, bool $isLargeSchema=false)
 Compresses tool schema by removing optional parameters with defaults and stripping descriptions, relying on LLM inference of variable names.
 
 calculateConfidence ($intentJSON, $toolsSchema, $rawResponse)
 Calculate confidence score based on multiple factors.
 
 formatArgumentsForDisplay ($arguments)
 Format arguments for display in confirmation.
 
 extractActionFromTool ($toolName)
 Extract action from tool name.
 

Detailed Description

File to handle MCP (Model Context Protocol) Intent Parsing This service receive a prompt, format and complete it with list of tools, send it to AI service and return the answer.

Definition in file parse_intent.php.

Function Documentation

◆ aiLabelOfResolvedObject()

aiLabelOfResolvedObject ( array $readResult,
array $writeArgs )

Name of the object a write targets, taken from the read result that produced its id (so the confirmation names "Dupont SA", not "socid 2305").

Parameters
array<mixed>$readResult Result of the read tool (a row, a list of rows, or {data:[...]})
array<mixed>$writeArgs Arguments of the write tool
Returns
string Name / ref / label of the matching row, '' when not found

Definition at line 1189 of file parse_intent.php.

References dol_trunc().

◆ aiNormalizeForMatch()

aiNormalizeForMatch ( $word,
$asStem = false )

Normalize a keyword or query for inflection-tolerant non-Latin matching.

Translators translate UI keys as natural dictionary words - as they should. Inflected languages then break exact substring matching so the code derives a match-friendly form instead of asking humans for stems: lowercase, strip combining accents (Unicode NFD marks, when the intl Normalizer is available), and for keywords drop the trailing inflection-bearing characters. No language is special-cased.

Parameters
string$wordWord to normalize
bool$asStemTrue to also truncate the inflected tail (keywords); false for the query
Returns
string Normalized form ('' when too short to stem safely)

Definition at line 1328 of file parse_intent.php.

References dol_strlen(), dol_strtolower(), and dol_substr().

Referenced by classifyIntentUniversal().

◆ aiQueryAsksForWrite()

catch(Throwable $e) aiQueryAsksForWrite ( $query,
$langs )

Does the user's message ask for a write (create / update / delete / ...)? Translated keys of the current language plus the short verbs users type in French and English whatever the UI language; whole words only.

Parameters
string$queryMessage of the user
Translate$langsTranslations
Returns
bool True when a write verb is present

Definition at line 1159 of file parse_intent.php.

References dol_strtolower().

◆ calculateConfidence()

calculateConfidence ( $intentJSON,
$toolsSchema,
$rawResponse )

Calculate confidence score based on multiple factors.

This function analyzes the AI's response to determine if the intent was parsed correctly and if all required arguments were provided according to the tool's schema.

Parameters
array<string,mixed>$intentJSON The parsed intent (Keys: 'tool', 'arguments').
array<string,array<string,mixed>>$toolsSchema Available tools schema (Key=ToolName, Value=ToolDefinition).
string$rawResponseRaw response string from the AI provider.
Returns
float Confidence score between 0.0 and 1.0.

Definition at line 1640 of file parse_intent.php.

◆ classifyIntentUniversal()

classifyIntentUniversal ( string $query,
Translate $langs )

Detect intent categories from a user query.

This function analyzes a natural language query and attempts to classify it into one or more predefined intent categories (e.g., billing, commercial, thirdparty, stock, project, reporting).

It leverages Dolibarr translations ($langs->trans()) to match localized keywords in the user's language and in the en_US reference. For non-Latin scripts, it performs a simpler substring search.

Matching strategy:

  • Latin queries: normalized (lowercase + unaccent) and matched using regex word boundaries.
  • Non-Latin queries: matched using case-insensitive substring search.

Each category is detected if at least one keyword matches.

Parameters
string$queryThe user input query to analyze.
Translate$langsThe Dolibarr translation object used to resolve localized keywords.
Returns
string[] Array of detected intent categories (e.g., ['billing', 'stock']).

Definition at line 1372 of file parse_intent.php.

References $conf, aiNormalizeForMatch(), dol_string_unaccent(), dol_substr(), and isComplexScript().

◆ cleanToolSchemaForLLM()

cleanToolSchemaForLLM ( array $tools,
bool $isLargeSchema = false )

Compresses tool schema by removing optional parameters with defaults and stripping descriptions, relying on LLM inference of variable names.

Parameters
array<int,array<string,mixed>>$tools Array of tool definitions.
bool$isLargeSchemaTrue if compression is needed.
Returns
array<int, array<string, mixed>>

Definition at line 1546 of file parse_intent.php.

◆ extractActionFromTool()

extractActionFromTool ( $toolName)

Extract action from tool name.

Parameters
string$toolNameThe tool name
Returns
string The extracted action

Definition at line 1737 of file parse_intent.php.

◆ filterToolsProfessional()

filterToolsProfessional ( array $allTools,
array $activeCategories )

Filter a list of tools based on active intent categories.

This function narrows down the available tools by matching their assigned categories against the detected intent categories. Tools tagged as "global" are always considered, but may be excluded when more specific categories are active to avoid overly generic matches.

Behavior:

  • If no categories are provided, or only "global" is present, all tools are returned.
  • Tools are included if they share at least one category with the target categories.
  • Tools with only the "global" category are excluded when specific categories are active.
  • If filtering results in fewer than 3 tools, the full tool list is returned as a fallback.
Parameters
array<int,array<string,mixed>>$allTools List of all available tools.
string[]$activeCategoriesDetected intent categories (e.g., ['billing', 'stock']).
Returns
array<int,array<string,mixed>> Filtered list of tools matching the active categories.

Definition at line 1511 of file parse_intent.php.

References dol_syslog().

◆ formatArgumentsForDisplay()

formatArgumentsForDisplay ( $arguments)

Format arguments for display in confirmation.

Parameters
array<string,mixed>$arguments The arguments to format (Key=ParamName, Value=Value)
Returns
string Formatted arguments string

Definition at line 1718 of file parse_intent.php.

◆ isComplexScript()

isComplexScript ( string $text)

Detects if the query uses Non-Latin Scripts.

Supports all Dolibarr Core Non-Latin languages:

  • CJK (Chinese, Japanese, Korean)
  • Cyrillic (Russian, Ukrainian, Serbian, Bulgarian)
  • Greek, Arabic, Hebrew, Thai
Parameters
string$textThe input text to be checked.
Returns
bool True if the text contains complex scripts, false otherwise.

Definition at line 1279 of file parse_intent.php.

Referenced by classifyIntentUniversal().

◆ recursiveUnmaskValues()

recursiveUnmaskValues ( $data,
?PrivacyGuard $guard )

Recursively unmask values in a dataset.

This helper walks through an array structure and applies the appropriate unmasking method on all string values. It ensures that any masked or placeholder data is restored before being used in actual tool execution.

Supported guard methods:

  • unmask(string $value): string
  • unmaskAiResponse(string $value): string

If both methods exist, unmask() takes precedence.

Parameters
mixed$dataThe input data (array, string, or scalar) to process.
PrivacyGuard | null$guardAn object providing unmasking methods.
Returns
mixed The data with all string values unmasked.
Parameters
mixed$item
Returns
mixed

Definition at line 1241 of file parse_intent.php.

References recursiveUnmaskValues().

Referenced by recursiveUnmaskValues().