225 return [
'error' =>
'AI request log table is not available.'];
228 $entity = (int) (!empty($this->
conf->entity) ? $this->
conf->entity : 1);
230 $offset = max(0, (
int) ($args[
'offset'] ?? 0));
231 $actioncommId = (int) ($args[
'actioncomm_id'] ?? 0);
232 $collectorId = (int) ($args[
'collector_id'] ?? 0);
233 $messageId = trim((
string) ($args[
'message_id'] ??
''));
234 $contextProfileCode = trim((
string) ($args[
'context_profile_code'] ??
''));
238 $sql =
"SELECT rowid, fk_actioncomm, provider, confidence, status, raw_request_payload, date_request";
239 $sql .=
" FROM ".MAIN_DB_PREFIX.self::TABLE_AI_REQUEST_LOG;
240 $sql .=
" WHERE entity = ".((int) $entity);
241 $sql .=
" AND tool_name = '".$this->db->escape(self::TOOL_NAME).
"'";
243 if ($actioncommId > 0) {
244 $sql .=
" AND fk_actioncomm = ".((int) $actioncommId);
246 if ($collectorId > 0) {
247 $sql .=
" AND raw_request_payload LIKE '%\"collector_id\":".((int) $collectorId).
"%'";
249 if ($messageId !==
'') {
250 $sql .=
" AND raw_request_payload LIKE '%".$this->db->escape($messageId).
"%'";
252 if ($contextProfileCode !==
'') {
253 $sql .=
" AND raw_request_payload LIKE '%".$this->db->escape($contextProfileCode).
"%'";
255 if ($minConfidence !==
null) {
256 $sql .=
" AND confidence >= ".((float) $minConfidence);
258 if ($maxConfidence !==
null) {
259 $sql .=
" AND confidence <= ".((float) $maxConfidence);
262 $sql .=
" ORDER BY rowid DESC";
263 $sql .= $this->db->plimit($limit, $offset);
265 $resql = $this->db->query($sql);
267 return [
'error' =>
'Database error while listing email cleaner runs: '.$this->db->lasterror()];
271 while ($obj = $this->db->fetch_object($resql)) {
272 $inputMetadata = $this->
decodeJsonOrRaw((
string) $obj->raw_request_payload);
274 'ai_request_log_id' => (
int) $obj->rowid,
275 'cleaning_id' => (
int) $obj->rowid,
276 'actioncomm_id' => (
int) $obj->fk_actioncomm,
277 'collector_id' => (!empty($inputMetadata[
'collector_id']) ? (
int) $inputMetadata[
'collector_id'] :
null),
278 'message_id' => (!empty($inputMetadata[
'message_id']) ? (string) $inputMetadata[
'message_id'] :
null),
279 'cleaning_confidence' => (
float) $obj->confidence,
280 'cleaning_model' =>
null,
281 'prompt_code' => (!empty($inputMetadata[
'prompt_code']) ? (string) $inputMetadata[
'prompt_code'] :
null),
282 'prompt_version' => (!empty($inputMetadata[
'prompt_version']) ? (string) $inputMetadata[
'prompt_version'] :
null),
283 'context_profile_code' => (!empty($inputMetadata[
'context_profile_code']) ? (string) $inputMetadata[
'context_profile_code'] :
null),
284 'context_profile_version' => (!empty($inputMetadata[
'context_profile_version']) ? (string) $inputMetadata[
'context_profile_version'] :
null),
285 'status' => (!empty($obj->status) ? (string) $obj->status :
null),
286 'date_creation' => (!empty($obj->date_request) ? (string) $obj->date_request :
null),
289 $this->db->free($resql);
293 'count' => count($items),
296 'actioncomm_id' => ($actioncommId > 0 ? $actioncommId :
null),
297 'collector_id' => ($collectorId > 0 ? $collectorId :
null),
298 'message_id' => ($messageId !==
'' ? $messageId :
null),
299 'context_profile_code' => ($contextProfileCode !==
'' ? $contextProfileCode :
null),
300 'min_confidence' => $minConfidence,
301 'max_confidence' => $maxConfidence,
317 return [
'error' =>
'AI request log table is not available.'];
320 $entity = (int) (!empty($this->
conf->entity) ? $this->
conf->entity : 1);
321 $aiRequestLogId = (int) ($args[
'ai_request_log_id'] ?? ($args[
'cleaning_id'] ?? 0));
322 $messageId = trim((
string) ($args[
'message_id'] ??
''));
323 $collectorId = (int) ($args[
'collector_id'] ?? 0);
325 $sql =
"SELECT rowid, entity, fk_actioncomm, provider, input_hash, output_hash, security_hash, confidence, status, raw_request_payload, raw_response_payload, date_request";
326 $sql .=
" FROM ".MAIN_DB_PREFIX.self::TABLE_AI_REQUEST_LOG;
327 $sql .=
" WHERE entity = ".((int) $entity);
328 $sql .=
" AND tool_name = '".$this->db->escape(self::TOOL_NAME).
"'";
330 if ($aiRequestLogId > 0) {
331 $sql .=
" AND rowid = ".((int) $aiRequestLogId);
332 } elseif ($messageId !==
'') {
333 $sql .=
" AND raw_request_payload LIKE '%".$this->db->escape($messageId).
"%'";
334 if ($collectorId > 0) {
335 $sql .=
" AND raw_request_payload LIKE '%\"collector_id\":".((int) $collectorId).
"%'";
337 $sql .=
" ORDER BY rowid DESC";
339 return [
'error' =>
"Missing selector: use 'ai_request_log_id', 'cleaning_id' or 'message_id'."];
342 $sql .= $this->db->plimit(1);
344 $resql = $this->db->query($sql);
346 return [
'error' =>
'Database error while reading cleaner run: '.$this->db->lasterror()];
348 $obj = $this->db->fetch_object($resql);
349 $this->db->free($resql);
351 return [
'error' =>
'Email cleaner run not found.'];
354 $inputMetadata = $this->
decodeJsonOrRaw((
string) $obj->raw_request_payload);
355 $outputJson = $this->
decodeJsonOrRaw((
string) $obj->raw_response_payload);
356 $handoffJson = (!empty($outputJson[
'handoff_payload_json']) && is_array($outputJson[
'handoff_payload_json'])) ? $outputJson[
'handoff_payload_json'] : array();
359 'ai_request_log_id' => (
int) $obj->rowid,
360 'cleaning_id' => (
int) $obj->rowid,
361 'entity' => (
int) $obj->entity,
362 'actioncomm_id' => (
int) $obj->fk_actioncomm,
363 'collector_id' => (!empty($inputMetadata[
'collector_id']) ? (
int) $inputMetadata[
'collector_id'] : 0),
364 'message_id' => (!empty($inputMetadata[
'message_id']) ? (string) $inputMetadata[
'message_id'] :
''),
365 'raw_hash' => (!empty($inputMetadata[
'raw_hash']) ? (string) $inputMetadata[
'raw_hash'] :
null),
366 'clean_hash' => (!empty($inputMetadata[
'clean_hash']) ? (string) $inputMetadata[
'clean_hash'] :
null),
367 'clean_body' => (!empty($outputJson[
'clean_body']) ? (string) $outputJson[
'clean_body'] :
''),
368 'cleaning_confidence' => (
float) $obj->confidence,
369 'cleaning_model' => (!empty($outputJson[
'cleaning_model']) ? (string) $outputJson[
'cleaning_model'] :
null),
370 'prompt_code' => (!empty($inputMetadata[
'prompt_code']) ? (string) $inputMetadata[
'prompt_code'] :
null),
371 'prompt_version' => (!empty($inputMetadata[
'prompt_version']) ? (string) $inputMetadata[
'prompt_version'] :
null),
372 'context_profile_code' => (!empty($inputMetadata[
'context_profile_code']) ? (string) $inputMetadata[
'context_profile_code'] :
null),
373 'context_profile_version' => (!empty($inputMetadata[
'context_profile_version']) ? (string) $inputMetadata[
'context_profile_version'] :
null),
374 'input_hash' => (!empty($obj->input_hash) ? (string) $obj->input_hash :
null),
375 'output_hash' => (!empty($obj->output_hash) ? (string) $obj->output_hash :
null),
376 'security_hash' => (!empty($obj->security_hash) ? (string) $obj->security_hash :
null),
377 'status' => (!empty($obj->status) ? (string) $obj->status :
null),
378 'raw_request_payload' => $inputMetadata,
379 'cleaning_json' => $outputJson,
380 'handoff_payload_json' => $handoffJson,
381 'date_creation' => (!empty($obj->date_request) ? (string) $obj->date_request :
null),
394 if (!empty($run[
'error'])) {
398 $cleaningJson = (!empty($run[
'cleaning_json']) && is_array($run[
'cleaning_json'])) ? $run[
'cleaning_json'] : array();
399 $handoffJson = (!empty($run[
'handoff_payload_json']) && is_array($run[
'handoff_payload_json'])) ? $run[
'handoff_payload_json'] : array();
400 $emailContext = array();
402 if (!empty($cleaningJson[
'email_context']) && is_array($cleaningJson[
'email_context'])) {
403 $emailContext = $cleaningJson[
'email_context'];
404 } elseif (!empty($handoffJson[
'conversation_context']) && is_array($handoffJson[
'conversation_context'])) {
405 $emailContext = $handoffJson[
'conversation_context'];
409 'ai_request_log_id' => (
int) $run[
'ai_request_log_id'],
410 'cleaning_id' => (
int) $run[
'cleaning_id'],
411 'actioncomm_id' => (
int) $run[
'actioncomm_id'],
412 'collector_id' => (
int) $run[
'collector_id'],
413 'message_id' => (
string) $run[
'message_id'],
414 'email_context' => $emailContext,
415 'cleaning_confidence' => (
float) $run[
'cleaning_confidence'],
416 'context_profile_code' => (!empty($run[
'context_profile_code']) ? (
string) $run[
'context_profile_code'] :
null),
417 'context_profile_version' => (!empty($run[
'context_profile_version']) ? (string) $run[
'context_profile_version'] :
null),
461 if (isset($this->tableExistsCache[$tableWithoutPrefix])) {
462 return $this->tableExistsCache[$tableWithoutPrefix];
465 $full = MAIN_DB_PREFIX.$tableWithoutPrefix;
466 $ok = (bool) count($this->db->DDLInfoTable($full));
467 $this->tableExistsCache[$tableWithoutPrefix] = $ok;