Make it possible to poll and see what the agent is doing.
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3543377. -->
Reported by: [marcus_johansson](https://www.drupal.org/user/385947)
Related to !181
>>>
<p>--- AI TRACKER METADATA ---<br>
<strong>Update Summary: </strong> Make it possible to poll agents. Most likely needed for the XB agents showcase.<br>
<strong>Check-in Date: </strong>MM/DD/YYYY (US format) [When we should see progress/get an update]<br>
<strong>Due Date:</strong> MM/DD/YYYY (US format) [When the issue should be fully completed]<br>
<strong>Blocked by:</strong> [#XXXXXX] (New issues on new lines)<br>
<strong>Additional Collaborators:</strong> @username1, @username2<br>
AI Tracker found here: <a href="https://www.drupalstarforge.ai/" title="AI Tracker">https://www.drupalstarforge.ai/</a><br>
--- END METADATA ---</p>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>Agents can be long running processes, and having ways of being able to get partial information on what is happening while they are processing, is vital for good UX/UI.</p>
<p>Also being able to show this in a structured manner, instead of just showing the textual outputs on each loop is important for specialized third party apps.</p>
<p>We have in agents a way to interupt each loop and proceed to run in later again via this issue <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/ai_agents/issues/3527979" title="Status: Closed (fixed)">#3527979: Make it possible to fetch and build the state of the agents as an array</a></span> and this issue <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/ai/issues/3527874" title="Status: Closed (fixed)">#3527874: Add Javascript orchestration for each loop in the AI Chatbot/Assistants API</a></span>. While that solves many of the timeout issues that hosters might have, where they kill off the processing, it doesn't solve the UX issue above.</p>
<p>We had a research and feedback issue here <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/ai/issues/3527867" title="Status: Closed (fixed)">#3527867: Research best way to return constant feedback and do long running tasks in a chatbot</a></span> and while it said that polling is not the optimal solution, it might be it for specific apps that does not have high traffic and needs to create specific structured output.</p>
<p>What we need is the possibility for the Agent to update every step it takes and feed that update partially, including:</p>
<ul>
<li>Start off an agent run.</li>
<li>Start off a loop with system prompts, tools available, chat history available, provider information (model, settings etc.)</li>
<li>End off a loop with text responses and tool choices.</li>
<li>Start off a tool execution with the structured inputs.</li>
<li>End off a tool execution with the results.</li>
<li>End off a loop</li>
<li>End off an agent run.</li>
<li>TBD</li>
</ul>
<p>Currently in the AI API Explorer we have a way where we do this and store it in custom entities, but this should be possible to store in the <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21TempStore%21PrivateTempStore.php/class/PrivateTempStore/11.x">PrivateTempStore</a>, <a href="https://www.drupal.orgKeyValueExpirable">KeyValueExpireable</a> or <a href="https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21TempStore%21SharedTempStore.php/class/SharedTempStore/11.x">SharedTempStore</a> and have a service that you can poll a unique id against and get an object of the latest updates for your API polling service to respond with.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<ul>
<li>Create a method for ConfigAiAgentInterface that enables/disables the progress service.</li>
<li>This will require that a jobId is set for the agent, otherwise warn and throw an exception when trying to run the agent.</li>
<li>If this is enabled while running an agent, make sure to use the events to update this to a storage of choice.</li>
<li>Create an interface to store this, so it can be easily swappable. PrivateTempStore works with AIAssistantsAPI, but requires a session, so it might be a bad choice here. PrivateTempStore is however naturally secure. Maybe persistant storage/entity is interesting at a later stage.</li>
<li>Create an interface called AgentStatusUpdateItemInterface, that holds what kind of status it is (enum?), when it started and a natural language description.</li>
<li>Create interfaces that extends this for different type of events, that might hold specific data like tool_id, tool_results, tool_input,</li>
<li>Create an interface called AgentStatusUpdateInterface, that should store the items in the order they come in.</li>
<li>Create implementation of the interfaces.</li>
<li>Create a service that takes the jobId and gives back the AgentStatusUpdate object if it exists.</li>
</ul>
<p>Open questions:</p>
<ul>
<li>Initial storage - should we do PrivateTempStorage and force sessions/login? Otherwise KeyValueExpireable might make sense. We could do cache as well if the full data is given back on each update.</li>
<li>If we do not do PrivateTempStorage we need some kind of permissions check for a jobId. Should the jobId itself have a uid on it or some other authentication?</li>
</ul>
issue