Connecting CopilotKit and AG-UI Agent Events to an Interactive Rive Mascot
By the Charmotion team · Updated · How we write guides

Subscribe to the agent with CopilotKit's useAgent hook, map AG-UI run, text and tool events to mood numbers, and write each number into a Rive state machine input. The mascot shows thinking while a run works, talking while text streams, happy when the run finishes and sad on an error. The mapping lives in your React code.
Key takeaways
- AG-UI streams run, text, tool and state events from the agent to the browser over Server-Sent Events.
- CopilotKit's useAgent hook re-renders your component as the agent's state, messages and status change.
- One mapping function turns event types into mood numbers, and the component writes that number into the Rive state machine input.
- Use useFrontendTool only for a short list of reactions the agent may request, not for every mood.
Already have a character or logo? See examples in real app screens: splash, onboarding, loading and empty-state screens, with the animation that plays in each.
What are AG-UI agent events?
AG-UI is an open, event-based protocol that connects AI agents to user-facing applications. CopilotKit is built on it. The agent sends a stream of typed events to the frontend over Server-Sent Events (SSE), and the frontend decides what to show.
This guide covers how to connect CopilotKit and AG-UI agent events to a Rive mascot: which events to listen for, how to turn them into mood numbers, and how to write those numbers into a state machine in a React app.
The AG-UI repository lists 31 event types in eight families: run lifecycle, text messages, tool calls, state, reasoning, activity, subagents and custom events. A mascot needs only a few of them:
- Run lifecycle: RUN_STARTED, RUN_FINISHED and RUN_ERROR, plus STEP_STARTED and STEP_FINISHED for multi-step runs.
- Text messages: TEXT_MESSAGE_START, TEXT_MESSAGE_CONTENT and TEXT_MESSAGE_END, which show a reply streaming in.
- Tool calls: TOOL_CALL_START, TOOL_CALL_ARGS, TOOL_CALL_END and TOOL_CALL_RESULT, which show the agent waiting on a tool.
- State: STATE_SNAPSHOT and STATE_DELTA. The delta carries JSON Patch changes that keep shared state in sync.
Event names are the part to build against. Payload fields differ by event, so check the event reference for the AG-UI version you install before you read a field from one. If your agent is a voice agent, the event mapping in voice agent avatar event mapping covers a different event source.
How does CopilotKit expose AG-UI events in React?
CopilotKit's React hooks and components use AG-UI events to stream agent output, sync shared state and route tool calls between the frontend and the agent. You rarely need to parse SSE yourself.
Three pieces matter for a mascot:
- CopilotKitProvider from @copilotkit/react-core/v2 is the provider component. The headless v2 entry exposes hooks for custom surfaces, which suits a mascot that is not a chat window.
- useAgent returns the AG-UI agent instance. The component re-renders when the agent's state, messages or execution status changes, and the agent instance also accepts subscribers for the raw event stream.
- useFrontendTool registers a tool that runs in the browser. The registration exists while the component that declared it is mounted.
CopilotKit's docs do not describe animation features for characters, mascots or logos (as of December 2026). The mascot layer is code you write, and the rest of this guide covers that layer. A chat interface with a streaming avatar follows the same pattern, as shown in animating a chat avatar from streaming events.
Which agent events should map to which mascot moods?
The numbers below use a ten-mood list: idle 0, happy 1, wave 2, thinking 3, sad 4, loading 5, listening 6, talking 7, sleeping 8 and attention 9. Moods blend into each other, so the mascot eases from one state to the next instead of cutting. Any Rive mascot with numeric states can use the same mapping.
| AG-UI event | Mood | Number | Why |
|---|---|---|---|
| RUN_STARTED | thinking | 3 | The agent has the request and is planning |
| TOOL_CALL_START | loading | 5 | The agent is waiting on a tool |
| TOOL_CALL_RESULT | thinking | 3 | The tool answered and the agent is deciding the next step |
| TEXT_MESSAGE_START, TEXT_MESSAGE_CONTENT | talking | 7 | The reply is streaming in |
| RUN_FINISHED | happy | 1 | The run completed; return to idle (0) after a short timer |
| RUN_ERROR | sad | 4 | The run failed |
Leave the other events unmapped. State events change data, not the mascot's face, and mapping every text chunk to a new mood causes flicker. Write the mood only when it changes.
Keep the mapping in one function so you can test it without a running agent:
export const MOOD = { idle: 0, happy: 1, thinking: 3, sad: 4, loading: 5, talking: 7 } as const;
export function moodForEvent(type: string): number | null {
switch (type) {
case 'RUN_STARTED':
case 'TOOL_CALL_RESULT':
return MOOD.thinking;
case 'TOOL_CALL_START':
return MOOD.loading;
case 'TEXT_MESSAGE_START':
case 'TEXT_MESSAGE_CONTENT':
return MOOD.talking;
case 'RUN_FINISHED':
return MOOD.happy;
case 'RUN_ERROR':
return MOOD.sad;
default:
return null;
}
}Driving a mood from app state rather than events is covered in driving Rive mood states from app state.
How do you write the mood number into a Rive state machine?
Use a number input named mood on the state machine for the simplest path. If your .riv file also exposes a number property called mood through a view model, your app can write to that instead. The trade-offs are compared in Rive data binding vs state machine inputs.
Rive's React runtime, @rive-app/react-canvas, provides useRive, which returns the Rive instance and a component to render. The component below opens the agent subscription and finds the mood input by name. The subscriber callback names (onEvent and unsubscribe) follow the AG-UI client SDK, so confirm them against the version you install. If the input name does not match the one in your .riv file, the component writes nothing, so check the name in the editor first.
import { useEffect } from 'react';
import { useAgent } from '@copilotkit/react-core/v2';
import { useRive } from '@rive-app/react-canvas';
import { moodForEvent } from './mascotMood';
export function AgentMascot({ agentId }: { agentId: string }) {
const { agent } = useAgent({ agentId });
const { rive, RiveComponent } = useRive({
src: '/mascot.riv',
stateMachines: 'CharacterStateMachine',
autoplay: true,
});
useEffect(() => {
if (!agent || !rive) return;
const input = rive
.stateMachineInputs('CharacterStateMachine')
.find((i) => i.name === 'mood');
if (!input) return;
const sub = agent.subscribe({
onEvent: ({ event }) => {
const mood = moodForEvent(event.type);
if (mood !== null && input.value !== mood) input.value = mood;
},
});
return () => sub.unsubscribe();
}, [agent, rive]);
return <RiveComponent />;
}Rive's runtimes are open source and play any .riv file, so the same mapping works in other frameworks. For the React setup around this component, see how to use Rive in React and Next.js.
Can the agent trigger a mascot reaction with a frontend tool?
Sometimes the agent should choose a reaction, such as a celebration after a booking or a wave when a returning user arrives. useFrontendTool registers a client-side tool at component scope. Its handler runs in the browser, so it can write to the mascot's mood input directly, and the registration is removed when the component unmounts.
Keep the tool narrow. Give it a name such as show_mascot_reaction, a description that lists the reactions you designed, and a parameter that accepts only those values. Inside the handler, map the value to a mood number and write it the same way the event mapping does.
Limit the parameter to the reactions you designed, not every mood number, so a model cannot pick a state you did not intend. The mood set has no one-shot triggers, so show a success with happy and an error with sad. A reaction tool that signals success should set happy and let the mascot return to idle on a timer.
What should you check before shipping a mascot tied to agent events?
- Unsubscribe on unmount. The cleanup function in the effect stops the subscription when the mascot leaves the page. Without it, events keep writing to a removed instance.
- Handle runs that never finish. If a stream stops without RUN_FINISHED or RUN_ERROR, the mascot stays in talking. Reset to idle (0) after a set time with no events.
- Replay recorded runs in tests. Save a real event list and feed it to moodForEvent in order. You can check the mood sequence without calling a model.
- Give the state a text label. A mood alone is not readable by a screen reader. Show the status as text beside the animation, and mark the animation decorative when the text carries the meaning.
- Respect reduced motion. Rive plays what the file contains, so a reduced-motion setting needs a calmer .riv or fewer mood changes in code. Choose the file in the component based on the user's setting.
- Check the Rive plan before launch. On Rive's Free plan, exported files play a Rive splash screen, and paid plans export without it. Read Is Rive free? for current plan details (as of December 2026).
If you need the mascot artwork itself, Charmotion turns one character image into a Rive file with the mood input described above, plus web, React and SwiftUI code; see the AI chatbot avatar animation page.
How to connect CopilotKit and AG-UI agent events to a Rive mascot
- Install the packages. Add CopilotKit's React packages and Rive's React runtime, @rive-app/react-canvas, to the app.
- Wrap the app in the provider. Wrap the app in CopilotKitProvider from @copilotkit/react-core/v2 and connect it to your agent.
- Map events to mood numbers. Write one moodForEvent function that maps AG-UI event types such as RUN_STARTED, TEXT_MESSAGE_CONTENT, RUN_FINISHED and RUN_ERROR to mood numbers.
- Build the mascot component. In a component, call useAgent for the agent and useRive for the .riv file, find the mood input by name, and subscribe to agent events to write the mood only when it changes.
- Add reactions and test. Add useFrontendTool for a short list of reactions the agent may request, then replay a recorded event list to check the mood sequence before release.
Frequently asked questions
Does CopilotKit have built-in mascot animation?
CopilotKit's documentation covers streaming output, shared state and tool routing. As of December 2026 it does not describe character animation for mascots or logos. The mascot layer is code in your app, which is why the event-to-mood mapping in this guide lives in your own files.
Can I use the same mapping without CopilotKit?
Yes. The mapping needs only a stream of AG-UI events and a Rive state machine input. Any client that receives the events can call the same moodForEvent function, whether it reads SSE directly or uses another framework. The Rive side is the same because Rive's runtimes play any .riv file.
Why does my mascot stay in talking mode after a reply?
Talking is set by the first text message event, and nothing resets it until RUN_FINISHED or RUN_ERROR arrives. If a run ends without either event, add a timer in the component that returns the mood to idle (0) after a period with no events.
Sources
- AG-UI protocol, CopilotKit
- ag-ui repository, AG-UI Protocol (GitHub)
- AG-UI event types (.NET SDK reference), AG-UI
- useAgent hook reference, CopilotKit
- useFrontendTool hook reference, CopilotKit
- Rive editor, Rive