dolibarr 24.0.0-beta
parse_intent.php File Reference

File to handle MCP (Model Context Protocol) Intent Parsing. More...

Go to the source code of this file.

Functions

catch(Throwable $e) recursiveUnmaskValues ($data, ?PrivacyGuard $guard)
 Recursively unmask values in a dataset.
 
 isComplexScript (string $text)
 Detects if the query uses Non-Latin Scripts.
 
 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.

Definition in file parse_intent.php.

Function Documentation

◆ 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 916 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, and applies additional synonym matching for Latin-based queries. 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 or synonym 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 695 of file parse_intent.php.

References dol_string_unaccent(), 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 822 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 1013 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 787 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 994 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 638 of file parse_intent.php.

Referenced by classifyIntentUniversal().

◆ recursiveUnmaskValues()

catch(Throwable $e) 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 600 of file parse_intent.php.

References recursiveUnmaskValues().

Referenced by recursiveUnmaskValues().