dolibarr 25.0.0-alpha
EmailCleaner Class Reference

Class EmailCleaner. More...

Collaboration diagram for EmailCleaner:

Public Member Functions

 __construct ($db)
 Constructor.
 
 processEmailCollectorMessage ($parameters, &$object)
 Process one EmailCollector message through the isolated EmailCleaner workflow.
 
 runEmailCleaner ($subject, $from, $rawBody)
 Run AI cleaner with conservative fallback.
 

Static Public Member Functions

static isRuntimeAvailable ()
 Check if the runtime dependencies for EmailCleaner are available.
 
static isEmailCollectorHookAction ($action)
 Check if the EmailCollector hook action targets EmailCleaner.
 
static parseActionParamConfig ($raw)
 Parse action parameter configuration (JSON).
 
static buildNoiseSummaryFromSegments ($segments)
 Build minimal excluded-noise summary from segments.
 
static buildComplianceMetadata ($engine, $fallbackUsed, $confidence)
 Build compliance metadata for transparency and supervision.
 
static collectAttachmentRows ($attachments, $savedAttachments, $savedDir='')
 Extract attachment rows with stable metadata.
 
static detectPdfDocTypeFromAttachmentName ($filename)
 Detect a generic document-like type from attachment metadata.
 
static normalizeText ($text)
 Normalize text to UTF-8 LF-only.
 
static extractMessageIdFromHeader ($header)
 Extract Message-ID from raw RFC822 header.
 
static extractHeaderFieldValue ($header, $fieldName)
 Extract unfolded RFC822 header value.
 
static extractHeaderMessageIds ($raw)
 Parse message-id list from References-like header.
 
static normalizeMessageId ($messageId)
 Normalize one RFC message identifier for comparisons.
 
static normalizeMessageIdList ($messageIds)
 Normalize and de-duplicate RFC message identifiers without changing order.
 
static buildThreadKey ($messageId, $inReplyTo, $references)
 Build a stable technical thread key from RFC lineage.
 
static buildMessageScopesBasic ($body, $subject='', $from='', $date='', $messageId='', $inReplyTo='', $references=array())
 Build deterministic current/quoted scopes and a technical timeline.
 
static extractQuotedMessageBlocksBasic ($body, $maxBlocks=16)
 Extract quoted message blocks with neutral RFC metadata.
 
static extractReplyOnlyTextBasic ($body)
 Extract reply-only part with a conservative separator strategy.
 
static extractQuotedThreadSnippetBasic ($body, $maxLen=1200)
 Build compact quoted context snippet for auditing/context transfer.
 

Private Member Functions

 findActionCommIdByMessageId ($entity, $msgid)
 Find the agenda event created for the collected email.
 
 insertAiRequestLogRow ($entity, $actioncommId, $collectorId, $msgid, $rawBody, $cleanedText, $segments, $confidence, $engine, $model, $promptCode, $promptVersion, $contextProfileCode, $contextProfileVersion, $emailContext, $emailUnderstanding, $handoffPayload, $payload)
 Insert cleaner output in the shared AI request log.
 
 isAiRequestLogTableAvailable ()
 Check if the shared AI request log table is available.
 

Static Private Member Functions

static buildFallbackEmailUnderstanding ($cleanBody)
 Build a minimal understanding payload when AI structured preprocessing is unavailable.
 
static sanitizeEmailUnderstanding ($raw, $fallbackCleanBody='')
 Sanitize structured understanding returned by AI.
 
static sanitizeStringList ($items, $maxItems, $maxLen)
 Sanitize a list of short strings.
 
static extractLeadingQuotedHeaderBasic ($raw)
 Parse neutral metadata from the leading header of a quoted block.
 
static fallbackCleanBody ($rawBody)
 Conservative fallback cleaner.
 
static decodeJsonFromAiString ($raw)
 Decode JSON from AI output that may contain wrappers.
 

Detailed Description

Class EmailCleaner.

Definition at line 21 of file emailcleaner.class.php.

Constructor & Destructor Documentation

◆ __construct()

EmailCleaner::__construct ( $db)

Constructor.

Parameters
DoliDB$dbDatabase handler

Definition at line 35 of file emailcleaner.class.php.

Member Function Documentation

◆ buildComplianceMetadata()

static EmailCleaner::buildComplianceMetadata ( $engine,
$fallbackUsed,
$confidence )
static

Build compliance metadata for transparency and supervision.

Parameters
string$engineCleaner engine
bool$fallbackUsed1 if fallback mode was used
float$confidenceCleaner confidence
Returns
array<string,mixed>

Definition at line 494 of file emailcleaner.class.php.

References getDolGlobalInt(), and getDolGlobalString().

Referenced by processEmailCollectorMessage().

◆ buildFallbackEmailUnderstanding()

static EmailCleaner::buildFallbackEmailUnderstanding ( $cleanBody)
staticprivate

Build a minimal understanding payload when AI structured preprocessing is unavailable.

Parameters
string$cleanBodyCleaned body
Returns
array<string,mixed>

Definition at line 756 of file emailcleaner.class.php.

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

Referenced by processEmailCollectorMessage(), and sanitizeEmailUnderstanding().

◆ buildMessageScopesBasic()

static EmailCleaner::buildMessageScopesBasic ( $body,
$subject = '',
$from = '',
$date = '',
$messageId = '',
$inReplyTo = '',
$references = array() )
static

Build deterministic current/quoted scopes and a technical timeline.

Parameters
string$bodyRaw email body
string$subjectCurrent subject
string$fromCurrent sender
string$dateCurrent Date header
string$messageIdCurrent message identifier
string$inReplyToDirect parent identifier
array<int,string>$references Reference lineage
Returns
array<string,mixed>

Definition at line 1052 of file emailcleaner.class.php.

References dol_strlen(), dol_substr(), extractQuotedMessageBlocksBasic(), extractQuotedThreadSnippetBasic(), extractReplyOnlyTextBasic(), normalizeMessageId(), and normalizeText().

Referenced by processEmailCollectorMessage().

◆ buildNoiseSummaryFromSegments()

static EmailCleaner::buildNoiseSummaryFromSegments ( $segments)
static

Build minimal excluded-noise summary from segments.

Parameters
array<int,mixed>$segments Segments returned by the cleaner
Returns
array<string,mixed>

Definition at line 467 of file emailcleaner.class.php.

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

Referenced by processEmailCollectorMessage().

◆ buildThreadKey()

static EmailCleaner::buildThreadKey ( $messageId,
$inReplyTo,
$references )
static

Build a stable technical thread key from RFC lineage.

The oldest References entry is preferred because replies that are not consecutive still share it. This is only correlation metadata and never a business interpretation.

Parameters
string$messageIdCurrent message identifier
string$inReplyToDirect parent identifier
array<int,string>$references Reference lineage
Returns
string

Definition at line 1032 of file emailcleaner.class.php.

References normalizeMessageId(), and normalizeMessageIdList().

Referenced by processEmailCollectorMessage().

◆ collectAttachmentRows()

static EmailCleaner::collectAttachmentRows ( $attachments,
$savedAttachments,
$savedDir = '' )
static

Extract attachment rows with stable metadata.

Parameters
array<int,mixed>$attachments EmailCollector attachments
array<int,mixed>$savedAttachments Saved attachment metadata
string$savedDirDirectory where attachments were saved
Returns
array<int,array<string,mixed>>

Definition at line 520 of file emailcleaner.class.php.

References dol_strtolower().

Referenced by processEmailCollectorMessage().

◆ decodeJsonFromAiString()

static EmailCleaner::decodeJsonFromAiString ( $raw)
staticprivate

Decode JSON from AI output that may contain wrappers.

Parameters
string$rawRaw AI output
Returns
array<string,mixed>|null

Definition at line 1350 of file emailcleaner.class.php.

References dol_substr().

Referenced by runEmailCleaner().

◆ detectPdfDocTypeFromAttachmentName()

static EmailCleaner::detectPdfDocTypeFromAttachmentName ( $filename)
static

Detect a generic document-like type from attachment metadata.

Parameters
string$filenameAttachment filename
Returns
string

Definition at line 598 of file emailcleaner.class.php.

References dol_strtolower().

◆ extractHeaderFieldValue()

static EmailCleaner::extractHeaderFieldValue ( $header,
$fieldName )
static

Extract unfolded RFC822 header value.

Parameters
string$headerRaw RFC822 header
string$fieldNameHeader field name
Returns
string

Definition at line 922 of file emailcleaner.class.php.

References dol_strlen(), and dol_substr().

Referenced by processEmailCollectorMessage().

◆ extractHeaderMessageIds()

static EmailCleaner::extractHeaderMessageIds ( $raw)
static

Parse message-id list from References-like header.

Parameters
string$rawRaw References header
Returns
array<int,string>

Definition at line 958 of file emailcleaner.class.php.

Referenced by processEmailCollectorMessage().

◆ extractLeadingQuotedHeaderBasic()

static EmailCleaner::extractLeadingQuotedHeaderBasic ( $raw)
staticprivate

Parse neutral metadata from the leading header of a quoted block.

Parameters
string$rawQuoted block
Returns
array<string,mixed>
Parameters
array<int,string>$names Accepted header names
Returns
string

Definition at line 1179 of file emailcleaner.class.php.

References normalizeMessageId().

Referenced by extractQuotedMessageBlocksBasic().

◆ extractMessageIdFromHeader()

static EmailCleaner::extractMessageIdFromHeader ( $header)
static

Extract Message-ID from raw RFC822 header.

Parameters
string$headerRaw RFC822 header
Returns
string

Definition at line 902 of file emailcleaner.class.php.

Referenced by processEmailCollectorMessage().

◆ extractQuotedMessageBlocksBasic()

static EmailCleaner::extractQuotedMessageBlocksBasic ( $body,
$maxBlocks = 16 )
static

Extract quoted message blocks with neutral RFC metadata.

Parameters
string$bodyRaw email body
int$maxBlocksMaximum blocks
Returns
array<int,array<string,mixed>>
Parameters
array<int,array<string,mixed>>$blocks Parsed quoted blocks
array<int,string>$current Current block lines
bool$collectingWhether a quoted block has started
Returns
void

Definition at line 1110 of file emailcleaner.class.php.

References dol_strlen(), dol_substr(), extractLeadingQuotedHeaderBasic(), and normalizeText().

Referenced by buildMessageScopesBasic().

◆ extractQuotedThreadSnippetBasic()

static EmailCleaner::extractQuotedThreadSnippetBasic ( $body,
$maxLen = 1200 )
static

Build compact quoted context snippet for auditing/context transfer.

Parameters
string$bodyRaw email body
int$maxLenMaximum snippet length
Returns
string

Definition at line 1272 of file emailcleaner.class.php.

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

Referenced by buildMessageScopesBasic().

◆ extractReplyOnlyTextBasic()

static EmailCleaner::extractReplyOnlyTextBasic ( $body)
static

Extract reply-only part with a conservative separator strategy.

Parameters
string$bodyRaw email body
Returns
string

Definition at line 1226 of file emailcleaner.class.php.

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

Referenced by buildMessageScopesBasic().

◆ fallbackCleanBody()

static EmailCleaner::fallbackCleanBody ( $rawBody)
staticprivate

Conservative fallback cleaner.

Parameters
string$rawBodyRaw email body
Returns
string

Definition at line 1310 of file emailcleaner.class.php.

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

Referenced by runEmailCleaner().

◆ findActionCommIdByMessageId()

EmailCleaner::findActionCommIdByMessageId ( $entity,
$msgid )
private

Find the agenda event created for the collected email.

Parameters
int$entityEntity id
string$msgidMessage id
Returns
int

Definition at line 309 of file emailcleaner.class.php.

Referenced by processEmailCollectorMessage().

◆ insertAiRequestLogRow()

EmailCleaner::insertAiRequestLogRow ( $entity,
$actioncommId,
$collectorId,
$msgid,
$rawBody,
$cleanedText,
$segments,
$confidence,
$engine,
$model,
$promptCode,
$promptVersion,
$contextProfileCode,
$contextProfileVersion,
$emailContext,
$emailUnderstanding,
$handoffPayload,
$payload )
private

Insert cleaner output in the shared AI request log.

Parameters
int$entityEntity id
int$actioncommIdAgenda event id
int$collectorIdCollector id
string$msgidMessage id
string$rawBodyRaw message body
string$cleanedTextCleaned message body
array<int,array<string,mixed>>$segments Cleaner segments
float$confidenceCleaner confidence
string$engineCleaner engine
string | null$modelAI model identifier
string$promptCodePrompt code
string$promptVersionPrompt version
string$contextProfileCodeContext profile code
string$contextProfileVersionContext profile version
array<string,mixed>$emailContext Extracted email context
array<string,mixed>$emailUnderstanding Structured email understanding
array<string,mixed>$handoffPayload Handoff payload
array<string,mixed>$payload Full cleaner payload
Returns
int

Definition at line 352 of file emailcleaner.class.php.

References $context, ai_log_request(), getDolGlobalInt(), getDolGlobalString(), and isAiRequestLogTableAvailable().

Referenced by processEmailCollectorMessage().

◆ isAiRequestLogTableAvailable()

EmailCleaner::isAiRequestLogTableAvailable ( )
private

Check if the shared AI request log table is available.

Returns
bool

Definition at line 437 of file emailcleaner.class.php.

Referenced by insertAiRequestLogRow().

◆ isEmailCollectorHookAction()

static EmailCleaner::isEmailCollectorHookAction ( $action)
static

Check if the EmailCollector hook action targets EmailCleaner.

Parameters
string$actionHook action
Returns
bool

Definition at line 58 of file emailcleaner.class.php.

References dol_strtolower().

Referenced by ActionsAi\doCollectImapOneCollector().

◆ isRuntimeAvailable()

static EmailCleaner::isRuntimeAvailable ( )
static

Check if the runtime dependencies for EmailCleaner are available.

Returns
bool

Definition at line 45 of file emailcleaner.class.php.

References isModEnabled().

Referenced by ActionsAi\addMoreActionsEmailCollector(), and ActionsAi\doCollectImapOneCollector().

◆ normalizeMessageId()

static EmailCleaner::normalizeMessageId ( $messageId)
static

Normalize one RFC message identifier for comparisons.

Parameters
string$messageIdMessage identifier
Returns
string

Definition at line 993 of file emailcleaner.class.php.

References dol_strtolower().

Referenced by buildMessageScopesBasic(), buildThreadKey(), extractLeadingQuotedHeaderBasic(), normalizeMessageIdList(), and processEmailCollectorMessage().

◆ normalizeMessageIdList()

static EmailCleaner::normalizeMessageIdList ( $messageIds)
static

Normalize and de-duplicate RFC message identifiers without changing order.

Parameters
array<int,string>$messageIds Message identifiers
Returns
array<int,string>

Definition at line 1006 of file emailcleaner.class.php.

References normalizeMessageId().

Referenced by buildThreadKey(), and processEmailCollectorMessage().

◆ normalizeText()

static EmailCleaner::normalizeText ( $text)
static

◆ parseActionParamConfig()

static EmailCleaner::parseActionParamConfig ( $raw)
static

Parse action parameter configuration (JSON).

Parameters
string$rawRaw action parameter
Returns
array<string,mixed>

Definition at line 451 of file emailcleaner.class.php.

Referenced by processEmailCollectorMessage().

◆ processEmailCollectorMessage()

◆ runEmailCleaner()

EmailCleaner::runEmailCleaner ( $subject,
$from,
$rawBody )

Run AI cleaner with conservative fallback.

Parameters
string$subjectEmail subject
string$fromSender address
string$rawBodyRaw email body
Returns
array<string,mixed>

Definition at line 612 of file emailcleaner.class.php.

References decodeJsonFromAiString(), dol_strlen(), dol_substr(), fallbackCleanBody(), getDolGlobalInt(), getDolGlobalString(), normalizeText(), and sanitizeEmailUnderstanding().

Referenced by processEmailCollectorMessage().

◆ sanitizeEmailUnderstanding()

static EmailCleaner::sanitizeEmailUnderstanding ( $raw,
$fallbackCleanBody = '' )
staticprivate

Sanitize structured understanding returned by AI.

Parameters
mixed$rawRaw AI understanding payload
string$fallbackCleanBodyCleaned body used as fallback
Returns
array<string,mixed>

Definition at line 805 of file emailcleaner.class.php.

References buildFallbackEmailUnderstanding(), and sanitizeStringList().

Referenced by runEmailCleaner().

◆ sanitizeStringList()

static EmailCleaner::sanitizeStringList ( $items,
$maxItems,
$maxLen )
staticprivate

Sanitize a list of short strings.

Parameters
mixed$itemsRaw list
int$maxItemsMaximum number of items
int$maxLenMaximum item length
Returns
array<int,string>

Definition at line 854 of file emailcleaner.class.php.

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

Referenced by sanitizeEmailUnderstanding().


The documentation for this class was generated from the following file: