Fix: Restore Stop Generation Button During Agent Session Execution Loop#5970
Conversation
…op (port of PR Mintplex-Labs#5970) Restore the stop generation button during agent WebSocket sessions by using event-based loading state management instead of unconditional true/false sandwhich. Changes: - Added agentEventLoadingState() dispatcher in utils/chat/agent.js to classify incoming socket events into three buckets: active work (keep stop button), awaiting user (show send button), and passive bookkeeping (leave unchanged) - Rewrote attachSocketListeners() message handler to call setLoadingResponse only when agentEventLoadingState returns a non-null opinion - Added setLoadingResponse(true) after AGENT_SESSION_START to restore loading state that the HTTP handoff statusResponse cleared - Guard statusResponse clearing in handleChat() so it doesn't hide the stop button once an agent WebSocket session is live - Reverted broken shouldShowStopButton reference back to isStreaming, fixing ReferenceError crash caused by incomplete revert in commit 733da21 Resolves: stop button never appearing during first agent loop, and stop button getting permanently stuck due to agentSessionActive always being true with persistent sockets.
…n-the-first-prompt-that-initiates-an-agent-session
shatfield4
left a comment
There was a problem hiding this comment.
Just 1 small nit here, other than that LGTM. It works and solves the bug in a simple way.
…n-the-first-prompt-that-initiates-an-agent-session
|
@angelplusultra can we get this moving? |
timothycarambat
left a comment
There was a problem hiding this comment.
Noting this because i was testing this prior to review:
With LMStudio, if I kill the response when generating a response - the UI properly shows "Agent session ended" and the button returns to a sendable state. However on the backend the message continues.
The intention for this PR is JUST to fix the button state, yeah? Because on reload of the page the completed message is present.
If this PR is JUST for button state, we need to stack PRs as merging just this to mater results in still a misleading state and we should merge this fix + actual stopping generation in a single commit.
Yes, the intention of this PR is just to restore the functionality of the abort feature as is. The state of the button was lost somehow in a v2 migration. We are splitting the true abort feature into 2 steps. First fix it as is. Then refactor to be a full end-to-end abort. |
…rompt-that-initiates-an-agent-session' of github.com:Mintplex-Labs/anything-llm into 5969-bug-stopgenerationbutton-disappears-on-the-first-prompt-that-initiates-an-agent-session
…tionbutton-disappears-on-the-first-prompt-that-initiates-an-agent-session
Pull Request Type
Relevant Issues
resolves #5969
Description
<StopGenerationButton />never appeared during the first execution loop of a new agent session. Two causes:statusResponse("Swapping over to agent chat"), andhandleChattreats everystatusResponseas end-of-exchange, clearingloadingResponseright as the agent starts working.setLoadingResponse(true)thensetLoadingResponse(false)in the same synchronous handler, which React 18 batches into a netfalseon every incoming agent event — so the loading state could never recover.Fix (frontend only):
loadingResponseis set on when the agent websocket is established, and the closing handoffstatusResponseis no longer allowed to clear it once the session is active (guards against either ordering of the handoff chunks).agentEventLoadingState()inutils/chat/agent.js: work events (status updates, text chunks, tool calls) keep it on; awaiting-user events (WAITING_ON_INPUT— previously ignored by the frontend entirely — plustoolApprovalRequest/clarificationRequest) turn it off; passive bookkeeping events (rename_thread,chatId,usageMetrics,citations, etc.) leave it untouched. The passive bucket matters becauserename_threadarrives after the async chat save + thread auto-rename complete, i.e. afterWAITING_ON_INPUT, and would otherwise re-show the stop button indefinitely.The stop button now stays visible for the entire execution loop and hands back to the send button when the agent is waiting on the user.
Visuals (if applicable)
Additional Information
Developer Validations
yarn lintfrom the root of the repo & committed changes