Improve summarizer for AI request input and output data for logs and spans
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3566762. -->
Reported by: [murz](https://www.drupal.org/user/157092)
Related to !1330
>>>
<p>[Tracker]<br>
<strong>Update Summary: </strong><br>
<strong>Short Description: Improve summarizer for AI request input and output data for logs and spans</strong><br>
<strong>Check-in Date: </strong>01/10/2026<br>
<em>Metadata is used by the <a href="https://www.drupalstarforge.ai/" title="AI Tracker">AI Tracker.</a> Docs and additional fields <a href="https://www.drupalstarforge.ai/ai-dashboard/docs" title="AI Issue Tracker Documentation">here</a>.</em><br>
[/Tracker]</p>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>AI input and output data can be quite long. Logs and spans can consume large data objects, but in most cases it doesn't worth wasting storage resources to store the full input and output. But storing nothing is not good either!</p>
<p>Also, some input and output contain binary data like images as blobs, that better to exclude from logs too. </p>
<p>So, we need to somehow summarize the input and output data to store them in a compact way, but still with meaningful data.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>In the issue <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/ai/issues/3535281" title="Status: Closed (fixed)">#3535281: Add OpenTelemetry export to the AI Observability</a></span>, a function <code>stringifyAiPayload(array $payload)</code> was added, that does the summarizing of the input and output data.</p>
<p>For now, it simply replaces blobs with a summary "[blob size=XXX bytes]".</p>
<p>Would be great to extend this function with more features to make input and output more compact but still valuable:</p>
<ul>
<li>Truncate the middle of each long message in the messages array, but keep the start and end of the text (add a configuration value).</li>
<li>Decrease the total amount of messages in the array, keeping only the system prompt, the first, and the last several messages.</li>
<li>Calculate hashes of the full message text (and the blobs too?) to simplify identifying the identical texts.</li>
<li>Represent the array of messages with role and attachments as plain text instead of JSON, to simplify viewing them in the logs.</li>
<li>More ideas?</li>
</ul>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<h3>Optional: Other details as applicable (e.g., User interface changes, API changes, Data model changes)</h3>
> Related issue: [Issue #3535281](https://www.drupal.org/node/3535281)
> Related issue: [Issue #3533109](https://www.drupal.org/node/3533109)
issue