Skip to content
Hindsight
Esc
↑↓navigate↵open⌘Jpreview
On this page

Adding an adapter

How an editor or agent tells the daemon what's happening, and how the daemon hooks itself into that tool.

An adapter has two halves. One half runs inside the tool and sends the daemon small events. The other half lives in the daemon’s connect command, which installs the first half into the tool for you. This page covers both.

Four ways to reach the daemon

Pick the one the tool already makes easy.

Way Used by Good when
hindsight-daemon emit --source <name> with JSON on stdin Claude Code, Codex, Cursor, Antigravity, OpenCode The tool can run a command on events (hooks).
POST /v1/editor on the local API The VS Code extension The tool is an editor you can write an extension for.
POST /v1/events on the local API Anything else You can build whole events yourself.
The daemon as a language server (hindsight-daemon lsp) Zed The tool has no way to watch edits except by being a language server.

The daemon also answers WakaTime and Hackatime heartbeats, so old plugins can point at it, and it reads Zed’s own agent threads off disk since Zed’s agent has no hooks.

What an event looks like

Every route ends up storing the same thing: an event, as defined in crates/hindsight-core/src/event.rs.

{
  "schema_version": 1,
  "id": "01939b5e-6a3c-7c1e-8f2a-4d5b6c7d8e9f",
  "ts": 1799999000000,
  "source": { "other": "my-tool" },
  "kind": "tool_used",
  "project": "my-repo",
  "meta": { "tool_name": "Edit", "language": "Rust", "lines_added": 3, "lines_removed": 1 }
}

ts is unix milliseconds. id is a UUID (the daemon makes UUIDv7s). source is claude_code, editor, desktop, daemon, browser, git, or {"other": "<name>"} for everything else. These are the kinds:

Kind Means
prompt_submitted You sent the agent a prompt. A human signal.
agent_run_started, agent_run_finished, tool_used The agent is working.
editor_activity You edited a file.
diff_reviewed You looked at a diff or an agent’s edit.
checkin_answered You answered Byte, wrote a note or took a snapshot.
browser_research Research in a browser.
commit A commit landed.
user_active, focus_lost Keyboard and mouse presence, and leaving the agent’s window.
receipt The server’s receipt for an upload. The daemon writes these, not adapters.

meta is a closed set of fields: lengths, counts, a typed-versus-pasted ratio (typed_permille), keyed hashes, a tool name, a language, token counts on finished runs, and a few more. An unknown field makes the whole event fail to parse. There is nowhere to put a prompt, a path or a line of code, and that’s on purpose. Event::check_text also limits where each field may appear (token counts only on a finished run, a branch only on a commit or snapshot, and so on).

An event with a source name nobody recognizes is still stored. It just isn’t treated as a coding agent. The agents the daemon knows by name are listed in event.rs.

The hook route

emit is what every hook-based adapter runs. It reads a JSON payload from stdin (at most 8 MiB), turns it into events, stores them and exits 0. It prints nothing, because anything a hook prints can end up in the agent’s context. The two exceptions are Cursor and Antigravity, which wait for an answer. It never fails loudly: a hook that broke the agent would be worse than a lost event. If something does go wrong it appends a line to emit.log in the daemon’s data directory.

The payload follows Claude Code’s hook format. These fields are read, and anything else is ignored:

Field For
hook_event_name Picks what happens. See below.
session_id Keyed-hashed into session_hash, never stored raw.
cwd Names the project: the directory of the nearest .git above it, else its own name.
prompt Only its length and a keyed hash are kept.
tool_name, tool_input The tool’s name, plus a hashed file path, a language and lines added and removed.
duration_ms, is_interrupt Tool timing, and whether a failed call was aborted.
transcript_path Where a run’s token counts and your Esc presses are read from, on Stop.
agent_usage For tools with no transcript: model, reply_chars, input, output, cache_read, cache_write.

And how hook_event_name becomes events:

hook_event_name Event
UserPromptSubmit prompt_submitted plus agent_run_started. Only agent_run_started if the session is headless, it’s a subagent’s prompt, or Claude Code sent the prompt itself.
PostToolUse tool_used
PostToolUseFailure tool_used with tool_failed set
Stop agent_run_finished, with token counts attached when the run says them
Interrupt agent_run_finished with interrupts set to 1
AgentRunStart agent_run_started, for runs nobody prompted
SessionStart nothing. It only tells the daemon a session began.

Try it without touching your real data:

export HINDSIGHT_DATA_DIR=/tmp/hindsight-try
echo '{"hook_event_name":"UserPromptSubmit","session_id":"abc","cwd":"/home/me/my-repo","prompt":"fix the login test"}' \
  | hindsight-daemon emit --source claude-code
hindsight-daemon status

If the tool’s payloads look different from Claude Code’s, translate them in the daemon rather than in the tool. Codex, Cursor and Antigravity each have a module (codex.rs, cursor.rs, antigravity.rs) that maps their payloads into the same HookInput struct. The OpenCode plugin does the opposite and does its translating in TypeScript (adapters/opencode/src/payload.ts) before calling emit.

The local API route

The daemon’s API listens on 127.0.0.1:5292 only. Authenticate with the contents of the local-token file in the data directory, as Authorization: Bearer <token>. The WakaTime routes also take HTTP Basic with the token as the key.

POST /v1/editor takes an array of up to 200 reports. The extension says what happened and exactly how much of it:

kind Means Fields
edit You changed a file. typed_chars (keystroke-sized changes) and other_chars (pastes, completions, rewrites from outside), optionally deleted_keys.
review You spent a while looking at a diff you hadn’t seen. None of the counts.
read You spent a while reading a file. file. Only recorded if an agent edited that file since you last reviewed it.
lines The text of lines you typed. lines, up to 2,000. They’re hashed and the text dropped. A commit later uses them to count which lines were yours. Not an event.

Every report can carry ts (unix ms; not more than a minute ahead, not more than 10 minutes old), file (an absolute path, hashed), workspace (to name the project when the file isn’t in a git repo) and app (the editor’s own name, stored only if it’s one of the names in crates/hindsight-core/src/app.rs). Unknown fields are rejected, and so is the whole request if one report is bad.

curl -X POST http://127.0.0.1:5292/v1/editor \
  -H "Authorization: Bearer $(cat "$HINDSIGHT_DATA_DIR/local-token")" \
  -H "Content-Type: application/json" \
  -d '[{"kind":"edit","file":"/home/me/my-repo/src/lib.rs","typed_chars":30,"other_chars":0,"app":"Zed"}]'

POST /v1/events takes up to 1000 full events. All are checked first, so one bad event stores none of them.

The VS Code extension (adapters/vscode/src/daemon.ts) is a good model for being polite to the daemon. It re-reads the token after a 401, keeps up to 500 reports in an outbox while the daemon is down, sends 200 at most per request, and drops anything older than 9 minutes before sending, since the daemon would refuse it.

How connect hooks a tool in

hindsight-daemon connect runs in the installer and every time the desktop app starts. It goes through each tool in turn and does the same few things:

  1. Skips the tool if it isn’t installed. It looks for the tool’s config folder (~/.claude, $CODEX_HOME or ~/.codex, ~/.cursor, ~/.gemini, $XDG_CONFIG_HOME/opencode or ~/.config/opencode) or its command-line tool.
  2. Loads the tool’s config and the adapter’s template. The templates are adapters/*/hooks.json, compiled into the daemon with include_str!.
  3. Removes any hook of ours it finds (anything running hindsight-daemon ... emit), then adds the template’s hooks with the full path of the running daemon, so nothing has to be on PATH.
  4. If nothing changed, reports already connected and writes nothing. If something did, keeps a one-time backup (Claude Code’s is settings.json.before-hindsight) and replaces the file in one step, so a crash never leaves half a file.

disconnect removes the same entries. Neither one touches a config file it can’t parse. It says so and leaves it alone.

Tools differ in the details, and the code carries the scars. Codex skips hooks until you trust them, and trust is tied to a hook’s position in the file, so connect updates its hooks where they are and never moves them. Cursor and Codex on Windows run hooks through PowerShell, where a quoted path followed by arguments won’t parse, so those commands call the path with &. VS Code and Cursor get the extension through their own --install-extension. OpenCode gets the plugin files copied into its config folder, with a daemon-path file naming the binary. Zed gets only the daemon-path file, because its extension runs the daemon as a language server.

Steps to add a tool

Name its events

In hindsight-core/src/event.rs, add a constant for the source name, add it to OTHER_AGENTS, give it a constructor like Source::codex() and a line in Source::parse. Agents travel as Source::Other, so servers that predate yours still accept the events.

Write the hooks file

Add adapters/<tool>/hooks.json that runs hindsight-daemon emit --source <name>. Give the hooks a short timeout. The existing files use between 3 and 10 seconds.

Translate the payload

If it isn’t Claude Code’s shape, add a module in crates/hindsight-daemon/src that turns it into a HookInput, and call it from emit in main.rs.

Teach `connect` about it

Add connect_<tool> and disconnect_<tool> in connect.rs, with an Outcome for each case, and call them from connect() and disconnect(). Add tests for the config merging, like the ones already in that file.

Tell git who made a commit

If the tool’s shell sets an environment variable, add it to git::agent_from_env, so the commit hook can tell your commits from the agent’s. Pick a variable only the agent’s own commands get, not one a terminal you open from the tool would inherit.

Name its window

If it has a desktop window, add its names to the app table in hindsight-core/src/app.rs and the default agent apps in presence.rs. Presence uses them to tell when you’re in the agent’s window.

Then run cargo test --workspace, and read the contributing page for what CI checks and the one rule your adapter has to follow.

Was this page helpful?