dolibarr 24.0.0-beta
ai.lib.php File Reference

Library files with common functions for Ai. More...

Go to the source code of this file.

Functions

 getListOfAIFeatures ()
 Prepare admin pages header.
 
 getListOfAIServices ()
 Get list of available ai services.
 
 testAIConnection (string $service, string $key, string $url)
 Tests the connection to an AI service using its API key and URL by sending message "Hello".
 
 ai_log_request ($db, $user, $query, array $response, $provider, float $time, float $confidence, $status, $error='', $rawReq='', $rawRes='')
 Log AI Request with Raw Payloads.
 
 getListForAISummarize ()
 Get list for AI summarize.
 
 getListForAIRephraseStyle ()
 Get list for AI style of writing.
 
 aiAdminPrepareHead ()
 Prepare admin pages header.
 
 getAiAssistantProviderLabel ()
 Resolve the AI provider/service currently configured for the AI Assistant (e.g.
 
 getAiChatAssistantConfig ()
 Build the configuration array consumed by the AI Assistant chat frontend (ai/js/ai_assistant.js).
 
 getAiChatAssistantHtml ($mode='page')
 Build the HTML of the AI Assistant chat interface.
 

Detailed Description

Library files with common functions for Ai.

Definition in file ai.lib.php.

Function Documentation

◆ ai_log_request()

ai_log_request ( $db,
$user,
$query,
array $response,
$provider,
float $time,
float $confidence,
$status,
$error = '',
$rawReq = '',
$rawRes = '' )

Log AI Request with Raw Payloads.

Parameters
DoliDB$dbDatabase object
User$userUser object
string$queryThe query sent to the AI
array<string,mixed>$response The full response from the AI
string$providerThe AI provider (e.g., 'OpenAI', 'Anthropic')
float$timeExecution time in seconds
float$confidenceConfidence score from the AI (if any)
string$statusStatus of the request (e.g., 'success', 'error')
string$errorError message, if any
string$rawReqRaw request payload
string$rawResRaw response payload
Returns
int Return 0

Definition at line 319 of file ai.lib.php.

References $conf, $db, dol_now(), dol_print_error(), dol_strlen(), dol_substr(), getDolGlobalInt(), and string.

Referenced by mcp_log_request().

◆ aiAdminPrepareHead()

aiAdminPrepareHead ( )

Prepare admin pages header.

Returns
array<array{0:string,1:string,2:string}>

Definition at line 407 of file ai.lib.php.

References $conf, complete_head_from_modules(), dol_buildpath(), and getDolGlobalString().

◆ getAiAssistantProviderLabel()

getAiAssistantProviderLabel ( )

Resolve the AI provider/service currently configured for the AI Assistant (e.g.

"ChatGPT (OpenAI)", "Google Gemini", "Anthropic (Claude)"), so it can be displayed in the chat header. The precise model name is intentionally not shown here, only which AI is in use.

Returns
string The provider label, or '' if no service is configured

Definition at line 477 of file ai.lib.php.

References getDolGlobalString(), getListOfAIServices(), and string.

Referenced by getAiChatAssistantHtml().

◆ getAiChatAssistantConfig()

getAiChatAssistantConfig ( )

Build the configuration array consumed by the AI Assistant chat frontend (ai/js/ai_assistant.js).

It is serialized as JSON into the data-ai-config attribute of the chat container.

Returns
array{mode:string,labels:array<string,string>,baseUrl:string,token:string,userInitial:string}

Definition at line 495 of file ai.lib.php.

References dol_buildpath(), dol_strtoupper(), dol_substr(), and getDolGlobalString().

Referenced by getAiChatAssistantHtml().

◆ getAiChatAssistantHtml()

getAiChatAssistantHtml ( $mode = 'page')

Build the HTML of the AI Assistant chat interface.

Shared by the standalone page (ai/assistant/index.php) and the topbar popover fragment (ai/assistant/popover.php) so both render the exact same chat.

Parameters
string$mode'page' for the standalone full page, 'popover' for the topbar popover fragment
Returns
string HTML content

Definition at line 617 of file ai.lib.php.

References dol_escape_htmltag(), getAiAssistantProviderLabel(), getAiChatAssistantConfig(), and img_picto().

◆ getListForAIRephraseStyle()

getListForAIRephraseStyle ( )

Get list for AI style of writing.

Returns
array<int|string,mixed>

Definition at line 391 of file ai.lib.php.

Referenced by FormAI\getSectionForAIEnhancement().

◆ getListForAISummarize()

getListForAISummarize ( )

Get list for AI summarize.

Returns
array<int|string,mixed>

Definition at line 369 of file ai.lib.php.

Referenced by FormAI\getSectionForAIEnhancement().

◆ getListOfAIFeatures()

getListOfAIFeatures ( )

Prepare admin pages header.

Returns
array<string,array<string,string>>

Definition at line 36 of file ai.lib.php.

◆ getListOfAIServices()

getListOfAIServices ( )

Get list of available ai services.

Returns
array<int|string,mixed>

Definition at line 68 of file ai.lib.php.

Referenced by Ai\generateContent(), getAiAssistantProviderLabel(), and testAIConnection().

◆ testAIConnection()

testAIConnection ( string $service,
string $key,
string $url )

Tests the connection to an AI service using its API key and URL by sending message "Hello".

This function supports multiple AI providers (Google Gemini, Anthropic Claude, and OpenAI-compatible APIs like Mistral, Groq, and DeepSeek). It constructs a minimal, provider-specific request payload and sends it to the given endpoint to verify that the API key is valid and the service is reachable.

Parameters
string$serviceThe identifier of the AI service (e.g., 'google', 'anthropic', 'openai', 'mistral').
string$keyThe API key for the service.
string$urlThe base URL of the AI service's API endpoint.
Returns
array{success: bool, message: string} An associative array indicating the result of the test.
  • 'success' is true on a successful connection (HTTP 2xx), false otherwise.
  • 'message' provides details, such as "OK (HTTP 200)" or an error description.

Definition at line 212 of file ai.lib.php.

References getDolGlobalString(), and getListOfAIServices().