Skip to content

πŸ› Bug: finalizeRunEvents appends TEXT_MESSAGE_END after RUN_ERROR β€” stop button crashes with "run has already errored"Β #5812

Description

@lukekh

♻️ Reproduction Steps

  1. Run the standard Node-proxy setup: a CopilotRuntime with an HttpAgent pointing at a remote AG-UI endpoint, served via copilotRuntimeNodeHttpEndpoint. Frontend uses CopilotChat.
  2. Start a chat and send a message that produces a streamed assistant response of a few seconds.
  3. While the assistant is still streaming text (i.e. between TEXT_MESSAGE_START and TEXT_MESSAGE_END), press the Stop button.
  4. Observe the console error (below). The assistant message is left in a broken state and the chat becomes unreliable.

Minimal server setup that reproduces:

const { CopilotRuntime, EmptyAdapter, copilotRuntimeNodeHttpEndpoint } = require('@copilotkit/runtime');
const { HttpAgent } = require('@ag-ui/client');

const runtime = new CopilotRuntime({
  agents: { my_agent: new HttpAgent({ url: 'http://localhost:8000/api' }) },
});
const handler = copilotRuntimeNodeHttpEndpoint({ runtime, serviceAdapter: new EmptyAdapter(), endpoint: '/copilotkit' });

…where http://localhost:8000/api is any AG-UI-compliant agent that streams a TEXT_MESSAGE_START β†’ TEXT_MESSAGE_CONTENT… response and does not close the message before the run is aborted (e.g. pydantic-ai's AGUIAdapter β€” see pydantic/pydantic-ai#3108).

βœ… Expected Behavior

Pressing Stop should end the run cleanly: the open text message is closed and a single terminal event (RUN_FINISHED) is emitted. No console error. The chat remains usable for the next message.

Per the AG-UI spec invariant: at most one terminal event per run, and all sub-events (TEXT_MESSAGE_END, TOOL_CALL_END) must close before the terminal.

❌ Actual Behavior

The browser throws:

Agent execution failed: Cannot send event type 'TEXT_MESSAGE_END':
The run has already errored with 'RUN_ERROR'. No further events can be sent.

Screenshots

No response

𝌚 CopilotKit Version

@copilotkit/react-core 1.62.2
@copilotkit/react-ui 1.62.2
@copilotkit/runtime 1.62.2

πŸ“„ Logs (Optional)

Error message in the browser:

Agent execution failed: D: Cannot send event type 'TEXT_MESSAGE_END': The run has already errored with 'RUN_ERROR'. No further events can be sent.


Example data in response

...

data: {"type":"TEXT_MESSAGE_CONTENT","timestamp":1783057434585,"messageId":"225f30a2-c662-4d0d-a05e-789cb51e17cc","delta":" frames"}

data: {"type":"RUN_ERROR","message":"This operation was aborted","code":"abort","rawEvent":{}}

data: {"type":"TEXT_MESSAGE_END","messageId":"225f30a2-c662-4d0d-a05e-789cb51e17cc"}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions