27if (!defined(
'NOTOKENRENEWAL')) {
28 define(
'NOTOKENRENEWAL', 1);
30if (!defined(
'NOREQUIREMENU')) {
31 define(
'NOREQUIREMENU', 1);
33if (!defined(
'NOREQUIREHTML')) {
34 define(
'NOREQUIREHTML', 1);
36if (!defined(
'NOREQUIREAJAX')) {
37 define(
'NOREQUIREAJAX', 1);
41if (!defined(
'NOCSRFCHECK')) {
42 define(
'NOCSRFCHECK', 1);
45require
'../../main.inc.php';
46require_once DOL_DOCUMENT_ROOT .
'/ai/class/mcp.class.php';
47require_once DOL_DOCUMENT_ROOT .
'/ai/lib/ai.lib.php';
54global $db, $user,
$conf;
57if (!$user->hasRight(
'ai',
'assistant',
'use')) {
72 dol_syslog(
"execute_tool.php ***** BEGIN", LOG_INFO, 0,
'_ai');
79 $raw = file_get_contents(
'php://input');
80 $input = json_decode($raw,
true);
82 if (!$input || empty($input[
'tool'])) {
83 throw new Exception(
"Invalid Request: No tool specified.");
88 $mcp =
new McpHandler($db, $user, $conf, McpHandler::CTX_ASSISTANT);
91 $tStart = microtime(
true);
94 dol_syslog(
"execute_tool.php tool = ".$input[
'tool'].
", arguments = ".json_encode($input[
'arguments'] ?? []), LOG_DEBUG, 0,
'_ai');
97 $result = $mcp->executeTool($input[
'tool'], $input[
'arguments'] ?? []);
107 if ((array_key_exists(
'success', $result) && empty($result[
'success'])) || !empty($result[
'error'])) {
109 $errorMsg = isset($result[
'error']) ? (string) $result[
'error'] :
'';
113 if (is_array($result) && ($result[
'resultType'] ??
'') ===
'input_required') {
119 '[Assistant] '.$input[
'tool'].
' '.
aiTruncateForLog((
string) json_encode($input[
'arguments'] ?? []), 20000),
120 [
'tool' => (
string) $input[
'tool']],
122 microtime(
true) - $tStart,
127 (
string) json_encode($result)
131 dol_syslog(
"execute_tool.php answer = ".var_export($result,
true), LOG_DEBUG, 0,
'_ai');
132 dol_syslog(
"execute_tool.php answer = ".count($result).
" lines", LOG_INFO, 0,
'_ai');
135 echo json_encode($result);
136}
catch (Throwable $e) {
138 http_response_code(400);
139 $toolName = (isset($input) && is_array($input) && !empty($input[
'tool'])) ? (
string) $input[
'tool'] :
'';
143 '[Assistant] '.($toolName !==
'' ? $toolName :
'invalid_request'),
144 [
'tool' => $toolName],
150 isset($raw) && is_string($raw) ? $raw :
'',
153 echo json_encode([
"error" => $e->getMessage()]);
aiTruncateForLog($text, $max=60000)
Trim a payload for the request log, keeping the beginning and the end.
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.
aiCheckCsrfToken($context='')
Check the anti-CSRF token of a request sent to one of the AI Assistant endpoints.
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).
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.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.