#Agent Loading State
1 messages · Page 1 of 1 (latest)
Hey @copper moss, You could try this approach: To display a loading spinner while the agent state is being reloaded from a thread ID, you could use the isLoading property from useCopilotContext() in CopilotKit. This property should reflect the loading state during thread rehydration. Here's one way you could implement a loading indicator:
import { useCopilotContext } from "@copilotkit/react-core";
import { LoadingSpinnerIcon } from "@copilotkit/react-ui";
<!-- empty-paragraph -->
function LoadingIndicator() {
const { isLoading } = useCopilotContext();
return (
isLoading ? (
<div className="flex items-center gap-2">
<LoadingSpinnerIcon />
<span>Loading agent state...</span>
</div>
) : null
);
}
<!-- empty-paragraph -->
```You could try wrapping your chat or agent UI with this indicator. The `isLoading` state should become `true` while the agent is rehydrating from the thread ID, and you can customize the spinner or message however you'd like during this time.
Let me know if this approach works for your use case!
Hi yes I though that would work but the state of that hook doesn’t seem to change
The value of it seems to match the working eg when the agent is running
So the screen flashes when the ask/thinking button would switch
I'll check with the team about the hook state not changing properly and the flashing issue you're experiencing, then get back to you.
Hey @copper moss, have you tried using the useCoAgent hook to check the state of your agent?
https://docs.copilotkit.ai/reference/hooks/useCoAgent
Give this a try-
- Use the useCoAgent hook to access your agent's state and node name. These allow you to determine if the agent is still loading or processing.
- Implement custom loading logic by checking for specific properties in your agent's state (e.g., a missing property, an isProcessing flag, or a particular nodeName).
- Display a loading spinner in your UI based on this logic.
import { useCoAgent } from "@copilotkit/react-core";
const { state: agentState, nodeName } = useCoAgent({
name: "your_agent_name",
initialState: { /* your initial state */ },
});
const isLoading = !agentState?.specificProperty || agentState?.isProcessing;
return (
isLoading ? (
<div className="flex items-center space-x-2">
<LoaderCircleIcon className="animate-spin" />
<span>Reloading agent state...</span>
</div>
) : (
// Your agent content here
)
);
It would also be possible to combine this with the chat loading state from useCopilotChatHeadless_c if needed.
- There is no built-in thread persistence loading state, so custom logic is required.
- https://docs.copilotkit.ai/reference/hooks/useCopilotChatHeadless_c
The useCoAgent hook allows you to share state bidirectionally between your application and the agent.
I did try something along those lines the issue is that the messages state is empty and that for some use cases is the only state
I will look into it again today
The issue isn’t when the when the agent is running or not it’s specific to the rehydration that happens before it’s ready
Ok so headless lets me access the messages once loaded for an existing chat but the issue then is the same for new chats? Unfortunately the issue loading also does not trigger
I think some works needs to be done on loading vs running naming conventions
In the network trace you can see a is new thread flag
That could be useful if there is a way to get to that via the ui
That is in the load agent state response
I don’t think it’s doable without a few more hooks into the graph calls
You're right about the loading state limitations for thread rehydration. Here's another approach you could try:
import { useCopilotChatHeadless_c } from "@copilotkit/react-core";
import { useEffect, useState } from "react";
const { messages } = useCopilotChatHeadless_c();
const [isRehydrating, setIsRehydrating] = useState(true);
useEffect(() => {
if (messages?.length > 0) setIsRehydrating(false);
}, [messages]);
return isRehydrating ? <LoadingSpinner /> : <ChatUI />;
If this approach doesn't solve your specific rehydration scenario, would you be interested in submitting a feature request or potentially contributing a PR?
I did attempt that approach but it works only when you know it’s a reload, vs a new unused thread id. , ideally exposing a few flags would help it events along the way. I’ll put an issue up on GitHub and poke around the code to see where to wire it in