Add a new Compaction operation type (normalization)
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3573087. --> Reported by: [marcus_johansson](https://www.drupal.org/user/385947) Related to !1198 >>> <p>[Tracker]<br> <strong>Update Summary: </strong>[One-line status update for stakeholders]<br> <strong>Short Description: </strong>Add a new Compaction operation type (normalization) reusing ChatInput.<br> <strong>Check-in Date: </strong>MM/DD/YYYY<br> [/Tracker]</p> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>The AI module does not currently expose a dedicated OperationType for compaction, even though both OpenAI and Anthropic provide APIs for compacting conversation context.</p> <p>OpenAI exposes compaction through the Responses API (<a href="https://developers.openai.com/api/docs/guides/compaction">https://developers.openai.com/api/docs/guides/compaction</a>), while Anthropic supports compaction via the Messages API using context management edits (<a href="https://platform.claude.com/docs/en/build-with-claude/compaction">https://platform.claude.com/docs/en/build-with-claude/compaction</a>).</p> <p>After reviewing the existing OperationType implementations, particularly <a href="https://git.drupalcode.org/project/ai/-/tree/2.0.x/src/OperationType/Chat?ref_type=heads">Chat</a>, it appears that <strong>ChatInput</strong> already models the structure required for compaction (messages, roles, metadata). Since compaction operates on message arrays in both providers, introducing a new dedicated input DTO would duplicate existing structures unnecessarily.</p> <p>To keep the architecture consistent and avoid duplication, the Compaction OperationType should reuse <code>ChatInput</code> as its input type. The new operation should be implemented as a <em>stateless normalization layer</em>, meaning it receives messages and returns a compacted representation without managing conversation continuity internally.</p> <p>This will allow:<br> A unified API surface for compaction across providers.<br> Reuse of the existing ChatInput structure.<br> Future expansion into stateful compaction strategies without breaking API contracts.</p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>- Create a new OperationType under <code>src/OperationType/Compaction</code>, following the same structural pattern as Moderation and Chat.<br> - Reuse <code>ChatInput</code> as the input DTO for the Compaction operation.<br> - Introduce a normalized <code>CompactionResponse</code> object that returns:<br> - The provider&rsquo;s compacted item (opaque string or structured object).<br> - Optional metadata returned by the provider.<br> - Extend the provider interface to add a <code>compaction(ChatInput $input, ...)</code> method.<br> - Document the new OperationType and clarify that this is a stateless implementation.<br> - Add an API Explorer for this, that uses the ChatHistory object<br> - Add a test integration with either Anthropic or OpenAI</p> <p>Future issues can explore automatic compaction triggers, threshold-based compaction, or stateful integration with Chat workflows.</p> <p>- <strong>This issue was created with the help of AI</strong></p>
issue