Architecture: coding agent integration
Scope
- How coding agents (Claude Code, Codex, Gemini CLI, Cursor, Grok Build, Qoder, Kimi Code, and tools that use MCP) report their status to PlayPets friends, as it works today.
- For people adding a new integration or building tools on the agent side.
- The whole app's architecture: PlayPets app architecture.
What leaves the agent, and where it goes:
- The bridge stays inside this computer.
- Hook adapters leave out user prompts and tool inputs.
- They forward short notifications and Codex's last answer.
- MCP and direct commands can also supply sentences.
- The app keeps recent reports.
- When you chat, it adds them to the connected friend's AI prompt as labeled status notes.
- That AI may run outside this computer.
Diagram
A container-level diagram: from what the agent runs to what appears on the friend.
Components
| Component | Location | Responsibility |
|---|---|---|
| CLI mode | src-tauri/src/agent/cli.rs, agent/run.rs for run | With a command argument (hook, mcp, status, say, react, pets, run), the app executable runs in this mode with no window. Node.js is not needed |
| Hook translation | agent::hooks::hook_event | Turns each agent's hook payload into a state and a code. Prompts and tool inputs are not sent |
| MCP server | agent::cli::mcp_reply | An MCP server that takes JSON-RPC one line at a time over standard input and output. Four tools |
| Bridge | src-tauri/src/agent/bridge.rs | The HTTP server inside the app. A random port on 127.0.0.1, a token check on every request, browser requests refused |
| Event handling | src-tauri/src/agent/mod.rs | Turns codes into sentences in the current language, remembers them as recent activity, and shows them as the friend's status |
| Settings snippets | connection_snippet in agent/snippets.rs (the agent list is in agent/kinds.rs) | The snippet to paste into each agent's settings file, built by "Copy settings" in the Settings tab (details) |
| Connect | src-tauri/src/agent/connect.rs | Merges the same snippet into the agent's own settings file, and takes PlayPets' entries out again (details) |
Data flow
- When the app launches:
- The bridge opens on
127.0.0.1:0. - The app writes the assigned port and a new token (64 random characters) to
bridge.jsonin the data folder. - Both change every time the app restarts.
- The bridge opens on
- Hooks:
- At each step the agent runs
playpets hook claude --bridge <bridge.json>and passes the hook JSON on standard input. - Codex passes the notification JSON as the last argument.
- CLI mode turns it into a state and a code and sends it with
POST /events. - A hook never blocks the agent's work: it is quiet, gives up on a slow app within a few seconds, and exits with code 0 even when it fails.
- At each step the agent runs
- MCP:
- The agent starts
playpets mcpas an MCP server and sendsinitialize,tools/listandtools/call. - Tool calls go through the same bridge path.
- The agent starts
- Commands:
- A person or script can run commands directly:
playpets say "Tests passed". playpets run -- npm testsendsworkingwhile the command runs, thendoneorerrorwhen it ends.
- A person or script can run commands directly:
- Inside the app:
- The bridge checks the token and the payload.
- It picks the friend:
pet/petIdis an id or a name. Without one, it uses the first friend. - A code becomes a sentence in the current language (
@tool Bash→ "Using Bash") and shows above the friend. - It is remembered among that friend's 8 most recent activities.
- In the yard:
- The friend acts on the state (
src/yard/duty.js): working or failed, it stays where it stands; waiting, it comes to the front of the yard; done, it rests a moment. - In a place with stations (the garden workshop), it works and fails at a desk, waits at the approval spot and rests in the rest corner (agent workspaces).
- The friend acts on the state (
Import links
- A gallery adds a
playpets://import?url=<pet page address>link. - The operating system hands the link to PlayPets (
src-tauri/src/links.rs). - The app imports the pet through the same path as a pasted address, then opens the Invite tab.
- The scheme is registered under
plugins.deep-linkintauri.conf.json. - A link received by a second launch is handed over by the single-instance plugin.
- Other kinds of links are ignored.
Interfaces
CLI commands
| Common option | Default |
|---|---|
--pet <id or name> | The first friend, or PLAYPETS_PET |
--bridge <path to bridge.json> | The data folder, or PLAYPETS_BRIDGE |
| Command | What it does |
|---|---|
playpets pets | Friend list and states |
playpets status <state> [message] | Status notification |
playpets say <message> | Only a speech bubble, without changing the state |
playpets react <wave|jump|pet> | A one-off reaction |
playpets run -- <command…> | Status notifications while a command runs |
playpets hook <claude|gemini|cursor|grok|qoder|kimi> | Handles hook JSON on standard input |
playpets hook codex <json> | Handles a Codex notification |
playpets mcp | MCP server (standard input and output) |
States and reactions
- States:
idle,working,waiting,done,error. - Reactions:
wave,jump,pet.
Hook translation
- Events not in the table are not sent.
- Claude Code:
- Its
Notificationsays bynotification_typewhat it tells. Onlypermission_prompt,elicitation_dialog,elicitation_url_dialogandagent_needs_inputwait on the person; one with no type comes from an older version that only notified to ask. idle_promptfollows every turn about a minute later. A turn stopped with Esc fires no Stop, sohook claudesettles the friend with it only while the friend is still working, ashook grokdoes.StopFailurereplacesStopwhen a turn ends on an API error. Settings copied before 1.4.0 do not have it.
- Its
- Grok Build:
- It sends Claude's
hook_event_nameandtool_name; its other keys are camelCase. - Its
UserPromptSubmit,StopFailure,StopCancelledandSessionEndfrom a subagent (subagentTypeset) are not sent. - It also runs the hooks in
~/.claude/settings.jsonand~/.cursor/hooks.json. Ahook claudeorhook cursorit runs (GROK_SESSION_IDset) sends nothing, so its work shows only on the Grok Build friend. Other agents' hooks still report with the variable set. - Some turns end without a Stop (cancel-and-send, rewind, a finished
!command).idle_promptfollows every turn, sohook grokreadsGET /petsfirst and settles the friend only while it is still working.
- It sends Claude's
- Qoder:
- It names its events and payload fields as Claude Code does, so
hook qoderreads them ashook claudedoes,idle_promptincluded. - The Qoder IDE reads hook changes only after a restart; Qoder CLI reads them at once.
- It names its events and payload fields as Claude Code does, so
- Kimi Code:
- It waits on the person through
PermissionRequest. ItsNotificationonly tells of background tasks and is not sent. - A turn stopped with Esc fires
Interruptinstead ofStop. - It writes every payload key snake_case (
tool_name). Itserror_typeis the error's class name, soStopFailureis always@error. - On Windows it runs a hook through
cmd.exe; its folder is.kimi-codeunder the home folder, as on macOS and Linux.
- It waits on the person through
- The app turns codes into the current language.
bynames who reported:<agent>:<session id>from a hook, or<agent>alone when the payload names no session (Codex); cut at 80 characters; empty from older hooks and other commands./petsreturns thebyof the friend's last report, andsource:chatwhile the state is a chat answer on its way rather than an agent's turn.- An
idle_promptsettles the friend only when the last report has the sameby, so one session's notice leaves another session's turn alone.
| Agent event | State | Sentence sent |
|---|---|---|
Claude, Qoder, Grok, Kimi UserPromptSubmit, Gemini BeforeAgent, Cursor afterAgentThought | working | @thinking |
Claude, Qoder, Grok, Kimi PreToolUse, Gemini BeforeTool, Cursor postToolUse | working | @tool <tool name>, or @tool_any when there is no name |
Cursor afterFileEdit, afterShellExecution, subagentStart | working | @editing, @command, @subtask |
Claude, Qoder Notification of a type that asks (or no type), Gemini Notification, Grok Notification of type permission_prompt | waiting | The agent's notification sentence, or @confirm when there is none |
Kimi PermissionRequest | waiting | @confirm |
Claude, Qoder, Kimi Stop, Grok Stop with reason end_turn, Gemini AfterAgent, Cursor stop | done | @done |
Claude, Qoder, Grok StopFailure | error | @rate_limit, @sign_in (authentication_failed, oauth_org_not_allowed), or @error |
Kimi StopFailure | error | @error |
Claude, Qoder, Grok Notification of type idle_prompt | idle, only while the friend is working | None |
Claude, Qoder, Gemini, Grok, Kimi SessionEnd, Grok StopCancelled, Kimi Interrupt, Cursor sessionEnd | idle | None |
Codex agent-turn-complete | done | The last answer (120 characters), or @done when there is none |
playpets run start, failure | working, error | @running <command>, @failed <exit code> |
playpets run stopped by Ctrl-C, a TERM or its terminal closing (also a command that exits 130 or 143) | idle | @stopped |
HTTP bridge
- Every request needs
Authorization: Bearer <token from bridge.json>. - Requests with an
Originheader (browsers) are refused. - The body is JSON of 8 KB or less.
- Sentences are cut at 240 characters.
| Request | Body | Response |
|---|---|---|
GET /pets | None | [{id, name, state, source, by}], and {id, parked: true} for a friend kept for later |
POST /events | {pet or petId, state, message, by?} | {ok, petId} |
POST /say | {pet, message} | {ok, petId} |
POST /react | {pet, action} | {ok, petId} |
| Error | Meaning |
|---|---|
400 | Bad payload |
401 | Missing or wrong token |
403 | Browser request |
404 | Unknown path or friend |
413 | Over 8 KB |
MCP tools
- Protocol version
2024-11-05.
| Tool | Input | What it does |
|---|---|---|
list_pets | None | Friend list and states |
set_pet_state | state (required), message, pet | Status notification |
say | message (required), pet | Speech bubble |
react | action (required), pet | Reaction |
Environment and touchpoints
- The Settings tab's "Connect" writes the settings into the file below; "Copy settings" tells you which file to paste into.
| Agent | Settings file | What you paste |
|---|---|---|
| Claude Code | ~/.claude/settings.json | A hook claude command for each event |
| Codex CLI | Top of ~/.codex/config.toml | notify = [executable, "hook", "codex", …] |
| Gemini CLI | ~/.gemini/settings.json | A hook gemini command for each event |
| Cursor | ~/.cursor/hooks.json | A hook cursor command for each event |
| Grok Build | ~/.grok/hooks/playpets.json, a new file | A hook grok command for each event (timeouts in seconds) |
| Qoder | ~/.qoder/settings.json | A hook qoder command for each event, in Claude Code's shape |
| Kimi Code | End of ~/.kimi-code/config.toml | A [[hooks]] row with a hook kimi command for each event |
| Other MCP tools | Each tool's MCP settings | The playpets mcp server |
Settings snippets
- Built by
connection_snippetwhen you press "Copy settings" in the Settings tab. - Contains the executable path and the
bridge.jsonpath. - If a friend was picked for that agent in the Settings tab, it adds
--pet <friend id>(PLAYPETS_PETfor MCP). - The assignment is saved in
AppData.agent_petsand removed when that friend is sent away.
Connect
- "Connect" asks first and names the file, then merges the snippet into it (
agent::connect::connect). - Everything else in the file stays, other tools' hooks and
mcpServersincluded. New entries go after the file's own. - Formats (
Formatinagent/kinds.rs):
| Format | Agents | Where PlayPets' entries go |
|---|---|---|
Grouped | Claude Code, Gemini CLI, Grok Build, Qoder | A group with one handler in hooks.<event> |
Flat | Cursor | A handler in hooks.<event>; version only when the file has none |
Notify | Codex | The top-level notify key; comments and tables stay (toml_edit) |
HookRows | Kimi Code | [[hooks]] rows of event, command and timeout only; any other key stops Kimi Code loading its config |
- PlayPets' entries are the ones whose command has
hook <kind> --bridge, whoseargsstart withhook <kind>(Claude Code and Qoder on Windows), or a Codexnotifywith"hook", "codex". No marker key is added. agentLinksreports each agent ason,off,outdated,unreadableortaken:on: PlayPets' entries are exactly what Connect would write now. Connect then writes nothing.outdated: another Reacting friend, a moved app or older events. Connect replaces the entries.unreadable: not JSON or TOML (a comment, a typo), orhooksin another shape. Nothing is written; Copy settings stays.taken: Codex'snotifyruns another program. Codex runs only one, so nothing is written.
<name>.playpets-backupkeeps the file as it was before PlayPets was in it. A write to a file without PlayPets' entries copies it there first; a later Connect or Disconnect leaves the backup, unless there is none yet.- The write goes through
<name>.playpets-tmpand a rename, keeping the file's permissions. - A read-only file is left as it is, with the write error shown.
- A linked settings file (a dotfiles folder) is written through its link.
- "Disconnect" removes only PlayPets' entries and the groups and events they leave empty. A file left empty is removed, as before Connect made it.
- The home folder is the system's. An e2e run reads
PLAYPETS_E2E_HOMEinstead, and only in an e2e run; unit tests pass a temporary folder. - File work runs without the state lock; only the bridge path and the Reacting friend are read under it.
Hook commands on Windows (agent/snippets.rs):
- PowerShell takes a quoted path followed by arguments as a syntax error, so each agent gets the form its shell reads.
- Claude Code runs a failing
PreToolUsehook as a block on the tool call, so a command PowerShell cannot parse would stop every tool.
| Agent | Shell on Windows | Hook written |
|---|---|---|
| Claude Code, Qoder | Git Bash, or PowerShell without it | command is the app, the rest is in args (exec form); no shell reads it |
| Gemini CLI, Cursor | PowerShell | & '<app>' hook <kind> --bridge '<file>'; a ' in a path is doubled |
| Kimi Code | cmd.exe | The quoted form used on macOS and Linux |
| Grok Build | Not checked | The quoted form used on macOS and Linux |
| Codex | None (notify is an argument list) | Unchanged |
MSIX install on Windows:
- The executable lives under WindowsApps and cannot be run by path from outside.
- The settings snippet points at the execution alias instead:
%LOCALAPPDATA%\Microsoft\WindowsApps\playpets.exe. - The package declares the
playpetsscheme (windows.protocol), since a packaged app's own registry writes stay inside the package; Windows passes the link toplaypets.exeas its argument.
Related decisions
- Design record "Stronger agent integration, community import, personality and activity memory": hook-first integration, CLI mode without Node, activity memory
- "Strings live in two per-language tables, one for the page and one for the backend, and agent status is sent as codes": why hooks send codes
- Design record "Agent workspaces": what a friend does for each state, and the workshop's desks, approval spot and rest corner
Change history
- 2026-09-29 Moved hooks, MCP and status commands into the CLI mode of the app executable (no Node needed)
- 2026-09-30 Hook states are sent as codes so they show in the app's language. CLI and MCP text is fixed to English
- 2026-09-30 With an MSIX install, the settings snippet points at the execution alias
- 2026-10-28 Grok Build reports through
hook grok; hooks it borrows from Claude Code and Cursor send nothing - 2026-10-28
hook claudewaits only on notices that ask, settles onidle_promptand reportsStopFailure; the MSIX package declaresplaypets:// - 2026-10-30 Connect and Disconnect write and remove PlayPets' entries in each agent's own settings file
- 2026-10-30 Qoder reports through
hook qoderand Kimi Code throughhook kimi