useAgent
React hook for accessing AG-UI agent instances in React Native
Overview
useAgent is a React hook that returns an AG-UI AbstractAgent instance. The hook subscribes to agent state changes and triggers re-renders when the agent's state, messages, or execution status changes.
Re-exported from @copilotkit/react-core/v2. It is identical to the React (V2) useAgent; only the import path differs.
Throws error if no agent is configured with the specified agentId.
Signature
import { useAgent } from "@copilotkit/react-native";
function useAgent(options?: UseAgentProps): { agent: AbstractAgent };Parameters
Prop
Type
Return Value
Prop
Type
Usage
Basic Usage
import { Text, View } from "react-native";
import { useAgent } from "@copilotkit/react-native";
function AgentStatus() {
const { agent } = useAgent();
return (
<View>
<Text>Agent: {agent.agentId}</Text>
<Text>Messages: {agent.messages.length}</Text>
<Text>Running: {agent.isRunning ? "Yes" : "No"}</Text>
</View>
);
}Accessing and Updating State
import { Text, TouchableOpacity, View } from "react-native";
import { useAgent } from "@copilotkit/react-native";
function StateController() {
const { agent } = useAgent();
return (
<View>
<Text>{JSON.stringify(agent.state, null, 2)}</Text>
<TouchableOpacity
onPress={() => agent.setState({ ...agent.state, count: 1 })}
>
<Text>Update State</Text>
</TouchableOpacity>
</View>
);
}Event Subscription
import { useEffect } from "react";
import { useAgent } from "@copilotkit/react-native";
function EventListener() {
const { agent } = useAgent();
useEffect(() => {
const { unsubscribe } = agent.subscribe({
onRunStartedEvent: () => console.log("Started"),
onRunFinalized: () => console.log("Finished"),
});
return unsubscribe;
}, []);
return null;
}Multiple Agents
import { Text, View } from "react-native";
import { useAgent } from "@copilotkit/react-native";
function MultiAgentView() {
const { agent: primary } = useAgent({ agentId: "primary" });
const { agent: support } = useAgent({ agentId: "support" });
return (
<View>
<Text>Primary: {primary.messages.length} messages</Text>
<Text>Support: {support.messages.length} messages</Text>
</View>
);
}Optimizing Re-renders
import { Text } from "react-native";
import { useAgent, UseAgentUpdate } from "@copilotkit/react-native";
// Only re-render when messages change
function MessageCount() {
const { agent } = useAgent({
updates: [UseAgentUpdate.OnMessagesChanged],
});
return <Text>Messages: {agent.messages.length}</Text>;
}Behavior
- Automatic Re-renders: Component re-renders when agent state, messages, or execution status changes (configurable via
updatesparameter) - Error Handling: Throws error if no agent exists with specified
agentId - State Synchronization: State updates via
setState()are immediately available to both app and agent - Event Subscriptions: Subscribe/unsubscribe pattern for lifecycle and custom events
Related
- AG-UI AbstractAgent - Full agent interface documentation
useCopilotKit- Low-level hook for accessing the CopilotKit core instance- React (V2) reference - The web equivalent this page mirrors