Add convenience API for non-agent programmatic context retrieval
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3584838. -->
Reported by: [joshua1234511](https://www.drupal.org/user/3362218)
Related to !122
>>>
<p>The current public API for retrieving rendered context requires consumers to manually construct an AiContextRequest via <code>AiContextRequestFactory::fromParameters()</code>, run it through <code>AiContextSelector</code>, and process the <code>AiContextResult</code>. This works well for agent-based integrations (where <code>AiContextSystemPromptSubscriber</code> handles everything automatically), but non-agent modules that need CCC context face a multi-step process with several objects to understand.</p>
<p>As modules across the Drupal AI ecosystem begin integrating with CCC — for example, AI Empathy Evaluation needs to inject brand voice/tone guidelines into its scoring prompts (see <a href="https://www.drupal.org/project/ai_empathy/issues/3583739">#3583739</a>) — a simpler facade for the common case would lower the integration barrier.</p>
<p><strong>Proposed solution</strong><br>
Add a convenience service (or method on an existing service) for the most common non-agent use case: "give me the rendered context for these scopes."</p>
<p><strong>Something like:</strong></p>
<pre>// Today (multi-step):<br>$request = $requestFactory->fromParameters($paramsDto);<br>$result = $selector->select($request);<br>$rendered = $result->getRenderedContext();<br><br>// Proposed convenience:<br>/** @var \Drupal\ai_context\Service\AiContextRetriever $retriever */<br>$retriever = \Drupal::service('ai_context.retriever');<br>$context = $retriever->getRenderedContext(<br> scopes: ['global', 'use_case' => 'empathy_evaluation'],<br> token_limit: 2000,<br> target_entity: $node, // optional<br>);</pre><p>This would be a thin wrapper around the existing pipeline — no new logic, just a DX improvement that:</p>
<ul>
<li>Accepts scope filters, an optional token limit, and an optional target entity</li>
<li>Internally builds the AiContextRequest, runs the selector, and returns the rendered string</li>
<li>Returns an empty string if no matching context is found</li>
</ul>
<p><strong>Use cases beyond AI Empathy</strong><br>
Any non-agent module that wants CCC context benefits:</p>
<ul>
<li>AI Guardrails — retrieve tone guidelines to validate responses</li>
<li>Content suggestions — pull brand voice context before generating suggestions</li>
<li>Custom form integrations — surface relevant context in admin forms</li>
<li>Quality/evaluation tools — inject organisational context into scoring prompts</li>
</ul>
<p><strong>AI usage (if applicable)</strong></p>
<p>[ ] AI Assisted Issue<br>
This issue was generated with AI assistance, but was reviewed and refined by the creator.</p>
<p>[ ] AI Assisted Code<br>
This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.</p>
<p>[x] AI Generated Code<br>
This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.</p>
<p>[ ] Vibe Coded<br>
This code was generated by an AI and has only been functionally tested.</p>
> Related issue: [Issue #3583739](https://www.drupal.org/node/3583739)
issue