Building a Claude Code Desktop Companion Mascot with Hooks and Rive
By the Charmotion team · Updated · How we write guides

Run a small hook script that Claude Code calls on events such as PreToolUse, Stop and Notification, have it write a mood number to a local file, and let a transparent desktop window read that number and set a number input in a Rive state machine. The hooks report what Claude is doing, and Rive draws the character.
Key takeaways
- Claude Code hooks report session events, so the mascot reacts to real work rather than a timer.
- One Node script maps each event to a mood number and writes it to a local state file.
- A transparent Electron window watches that file and sets a number input in a Rive state machine.
- Keep the script silent and exit with code 0 so a broken mascot never blocks a coding session.
- Export a .riv on a paid Rive plan before you rely on the mascot in a product.
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.
How does a Claude Code desktop companion work?
This guide shows how to build a Claude Code desktop companion mascot using hooks and Rive. The companion is a small character on your screen that changes expression as Claude Code works: thinking while it reads and edits, happy when a reply finishes, and alert when it asks for permission.
The design has four parts, and each one can be tested on its own.
| Part | Job | Tool |
|---|---|---|
| Hooks | Run your command when Claude Code events fire and pass event JSON to it | Claude Code hooks |
| Mood script | Turn each event into a mood number and write it to a file | Node.js |
| Companion window | Show the character above other windows on a transparent background | Electron |
| Character | Play each mood as a state in a Rive state machine | .riv file |
Hooks are why this works without polling. Claude Code runs your command at points in a session, so the mascot changes when something actually happens. A timer-based loop would look the same whether Claude was busy or idle.
Two rules keep the setup safe. The hook runs on your machine with your permissions, so keep the script short and readable. The companion is only a view, so it never needs to change how Claude Code behaves. For the general pattern of mapping agent events to mascot states, see mapping coding agent tool-call events to animated mascot states.
Which Claude Code hook events should drive the mascot?
Claude Code has several hook events, including SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Notification, SubagentStop, Stop and SessionEnd. Start with a small set. Six events cover the moods a desktop companion needs.
| Event | When it fires | Mood number | Pose |
|---|---|---|---|
| SessionStart | A session begins | 0 | idle |
| UserPromptSubmit | You send a prompt | 3 | thinking |
| PreToolUse | Before a tool runs | 3 | thinking |
| Notification | Claude Code sends a notification, such as a permission request | 9 | attention |
| Stop | Claude stops responding | 1 | happy |
| SessionEnd | The session closes | 8 | sleeping |
The numbers are yours to choose. The example uses the poses in this table, and your Rive file must use the same numbers. PostToolUse and SubagentStop can be added the same way if you want a reaction after each finished tool call.
The event list comes from Claude Code, so the mood table is where your design lives. Keep it in one place so you can change a mood without editing the Rive file.
How do you write the hook script?
A command hook receives a JSON object on stdin. Every event includes fields such as session_id, cwd and hook_event_name, and PreToolUse adds tool_name and tool_input. The script below reads only hook_event_name. It ignores tool_input, so prompts and file paths never reach the state file.
Save the script as ~/.claude/mascot/mood-hook.js. It writes to a temporary file and then renames it, so the desktop app never reads half-written JSON.
const fs = require('fs');
const os = require('os');
const path = require('path');
const MOODS = { SessionStart: 0, UserPromptSubmit: 3, PreToolUse: 3, Notification: 9, Stop: 1, SessionEnd: 8 };
let raw = '';
process.stdin.on('data', function (chunk) { raw += chunk; });
process.stdin.on('end', function () {
try {
const event = JSON.parse(raw);
const mood = MOODS[event.hook_event_name];
if (mood !== undefined) {
const dir = path.join(os.homedir(), '.claude', 'mascot');
fs.mkdirSync(dir, { recursive: true });
const file = path.join(dir, 'state.json');
const tmp = file + '.tmp';
fs.writeFileSync(tmp, JSON.stringify({ mood: mood, event: event.hook_event_name, at: Date.now() }));
fs.renameSync(tmp, file);
}
} catch (err) {
// Ignore bad input so the hook never blocks Claude Code.
}
process.exit(0);
});
The script always exits with code 0. A command hook that exits with 2 is a blocking error on events that can block, and a mascot should never stop Claude from working.
Test the script from a terminal before you connect it. Pipe in a sample event with echo '{"hook_event_name":"Stop"}' | node ~/.claude/mascot/mood-hook.js, then open state.json and confirm that mood is 1.
How do you register the hooks in settings.json?
Hooks sit under a "hooks" key. Put them in ~/.claude/settings.json to apply them to every project, or in .claude/settings.json to share them with a repository. Use .claude/settings.local.json for a setup that applies only to one machine. If the file already has a hooks key, merge the entries instead of replacing it.
Each entry names an event, an optional matcher such as a tool name, and a list of handlers. This example has no matcher, so the hook runs for every tool. Use the absolute path to the script on your system.
{
"hooks": {
"SessionStart": [ { "hooks": [ { "type": "command", "command": "node /Users/you/.claude/mascot/mood-hook.js" } ] } ],
"UserPromptSubmit": [ { "hooks": [ { "type": "command", "command": "node /Users/you/.claude/mascot/mood-hook.js" } ] } ],
"PreToolUse": [ { "hooks": [ { "type": "command", "command": "node /Users/you/.claude/mascot/mood-hook.js" } ] } ],
"Notification": [ { "hooks": [ { "type": "command", "command": "node /Users/you/.claude/mascot/mood-hook.js" } ] } ],
"Stop": [ { "hooks": [ { "type": "command", "command": "node /Users/you/.claude/mascot/mood-hook.js" } ] } ],
"SessionEnd": [ { "hooks": [ { "type": "command", "command": "node /Users/you/.claude/mascot/mood-hook.js" } ] } ]
}
}
One script serves every event, because it reads hook_event_name and picks the mood itself. Start a new Claude Code session, send a prompt, and check that state.json changes to mood 3.
How do you set up the Rive mascot states?
Design the character in the Rive Editor. If you start from a PNG or illustration, the steps in PNG to Rive animation cover the import. The state machine needs one number input and one state per mood.
- Create the poses. Make one state each for idle, happy, thinking, attention and sleeping.
- Add the mood input. Create a number input named mood in the state machine.
- Connect the transitions. From idle and from the other states, add transitions that fire when mood equals the matching number in the table, so mood 3 leads to thinking.
- Keep the motion short. Set transition durations so the face changes in a fraction of a second rather than snapping.
- Export a .riv. Files exported on the free plan play a Rive splash screen and cannot be exported for use in a product. See is Rive free for plan details as of December 2026.
Data binding is an alternative to the number input. A view model property can drive the same states, and Rive's React runtime documentation points to data binding for new work. For how inputs behave at runtime, read Rive state machine inputs.
How do you run the companion window in Electron?
Electron can open a frameless window with a transparent background that stays above other apps. The main process watches state.json and sends each new mood to the page. The window options used here are transparent, frame and alwaysOnTop. Test transparency on every operating system you plan to support, because the result depends on the platform.
const { app, BrowserWindow } = require('electron');
const fs = require('fs');
const os = require('os');
const path = require('path');
const STATE = path.join(os.homedir(), '.claude', 'mascot', 'state.json');
function createWindow() {
const win = new BrowserWindow({
width: 220,
height: 220,
transparent: true,
frame: false,
alwaysOnTop: true,
hasShadow: false,
webPreferences: { preload: path.join(__dirname, 'preload.js') }
});
win.loadFile('index.html');
let lastMood = null;
fs.watchFile(STATE, { interval: 300 }, function () {
try {
const state = JSON.parse(fs.readFileSync(STATE, 'utf8'));
if (state.mood !== lastMood) {
lastMood = state.mood;
win.webContents.send('mood', state.mood);
}
} catch (err) {
// The file may not exist yet, or may be mid-write. Try again on the next change.
}
});
}
app.whenReady().then(createWindow);
The preload script exposes one function to the page, so the page never gets direct access to Node.js:
const { contextBridge, ipcRenderer } = require('electron');
contextBridge.exposeInMainWorld('companion', {
onMood: function (callback) {
ipcRenderer.on('mood', function (event, mood) { callback(mood); });
}
});
In the page script, call window.companion.onMood and pass each mood to your Rive binding. With the React runtime, the useStateMachineInput hook is marked deprecated in favor of data binding hooks, so set the mood through a view model property where you can. Keep the window small, around 220 pixels, so it reads as a companion and not as an app.
How do you test the companion and avoid common problems?
Test each link in the chain before you trust the whole loop.
- Script: run the echo test from the hook section and confirm state.json changes.
- Hooks: start a new session, send a prompt, and confirm the mood becomes 3. If nothing changes, check that the command path is absolute and that the event names match the keys in settings.json.
- Window: with state.json changing, the character should follow. If it stays on idle, look for a typo in the file name or in the preload channel name.
- Session end: close the session and confirm the mascot moves to sleeping.
Expect frequent writes. PreToolUse fires before each tool call, so a busy session rewrites state.json often. The atomic rename keeps reads safe, and the 300 millisecond poll keeps the window responsive without reading the file constantly.
Nothing leaves your machine in this design. The hooks write a local file, and the window reads it. If you later want the mascot on a phone or in a shared dashboard, that is a different architecture, and the same mood numbers can be reused.
How to build a Claude Code desktop companion mascot using hooks and Rive
- Design the Rive character. Create one state per mood (idle, happy, thinking, attention, sleeping) in a Rive state machine, with a number input named mood.
- Write the mood script. Save a Node.js hook script that reads hook_event_name from stdin, maps it to a mood number, writes state.json through a temporary file and exits with code 0.
- Register the hooks. Add a hooks key to ~/.claude/settings.json with a command hook for SessionStart, UserPromptSubmit, PreToolUse, Notification, Stop and SessionEnd, using the absolute script path.
- Connect the mood input. Map each mood number in the Rive state machine transitions so that mood 3 leads to thinking and so on.
- Build the desktop window. Open a frameless, transparent, always-on-top Electron window that watches state.json and sends each new mood to the page through a preload bridge.
- Test the loop. Run the script by hand, start a Claude Code session, send a prompt and confirm the character changes to thinking, then close the session and confirm it sleeps.
Frequently asked questions
Will a hook slow down Claude Code?
A hook runs when its event fires, so keep the script short. This script makes one small file write and exits with code 0. Exit code 2 is a blocking error on events that can block, so the script never returns it. Check the Claude Code hooks reference for current behavior on each event.
Can I send events over HTTP instead of writing a file?
Yes. An http hook sends a JSON POST to a URL, so the desktop app can run a small server on localhost and receive events directly. The file approach is easier to debug, because you can open state.json and see the last mood the companion received.
Does this setup need a paid Rive plan?
Only to ship the result. Files exported on the free plan play a Rive splash screen and cannot be exported as .riv for use in a product. You can build and test on the free plan, then choose a paid plan before release. Plan details are in the is Rive free guide, as of December 2026.
Can other coding agents drive the same companion?
Not with these hooks. The event names and the settings file described here belong to Claude Code. Other agents have their own hook or MCP mechanisms, so check that tool's documentation before you wire it to the same state file.
Sources
- Claude Code hooks reference, Anthropic
- Rive Editor, Rive
- Rive pricing, Rive
- Rive React runtime, Rive
- Electron documentation, Electron
Related guides
- How to Animate a Logo or Character From Claude, Cursor or VS Code With MCP
- Mapping Coding Agent Tool-Call Events to Animated Mascot States
- Building a Transparent Floating AI Assistant Mascot in Electron and Tauri with Rive
- Rive State Machine: Inputs, Triggers & Moods
- How to Use Rive in React and Next.js