Metadata-Version: 2.4
Name: typed-agent-hooks
Version: 0.3.0
Summary: Typed hook events and strict wire schemas for Codex and Claude Code.
Keywords: codex,claude-code,hooks,pydantic,schemas
Author: Nima Shoghi
Author-email: Nima Shoghi <nimashoghi@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Dist: pydantic>=2.8,<3
Requires-Python: >=3.10
Project-URL: Repository, https://github.com/nimashoghi/typed-agent-hooks
Description-Content-Type: text/markdown

# typed-agent-hooks

`typed-agent-hooks` gives OpenAI Codex and Anthropic Claude Code hooks one set of typed events. A hook file handles shared events, such as `ToolCallCompleted`. The package parses the host payload, maps it to the shared event, calls the handler, and writes the output in the format of that host. A hook file never names a host.

The host files hold the native event, matcher, timeout and command. The package does not generate or install host configuration.

## A hook file

Each hook is one executable PEP 723 file:

```python
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.13"
# dependencies = ["typed-agent-hooks==0.3.0"]
# [tool.uv]
# exclude-newer = "2026-10-25T00:00:00Z"
# ///
"""Suggest the notebook when a Bash command runs inline Python."""

from typed_agent_hooks import shared

app = shared.HookApp(name="inline-python")


@app.on(shared.events.ToolCallCompleted)
def tool_completed(event: shared.events.ToolCallCompleted) -> shared.outputs.Result:
    command = event.tool_input.get("command") if isinstance(event.tool_input, dict) else None
    if not isinstance(command, str) or "python -c" not in command:
        return None
    return shared.outputs.AddContext(text="For multi-step Python work, use the notebook.")


if __name__ == "__main__":
    app.main()
```

uv uses only uploads from before the `exclude-newer` date, for every package. Set it to a time after the upload of the pinned `typed-agent-hooks` release, or uv cannot find that release. The date also freezes the other dependencies. The date in the example comes from before the 0.3.0 upload; replace it when you pin a release.

## Host files

The host files call the hook with the host name as the only argument. The command ends with `|| true`, so that a failure before `main()` runs cannot block the action (see below). Codex, in `.codex/hooks.json`:

```json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Bash",
        "hooks": [{ "type": "command", "command": ".agent-hooks/inline-python.py codex || true", "timeout": 5 }]
      }
    ]
  }
}
```

Claude Code, in `.claude/settings.json`:

```json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.agent-hooks/inline-python.py claude || true",
            "timeout": 5
          }
        ]
      }
    ]
  }
}
```

## What `main()` does

`app.main()` reads the host from its first argument (`codex` or `claude`) and the payload from stdin. The payload's `hook_event_name` selects the event. `main()` calls the handler of that event and writes the host output to stdout.

- A handler that returns `None` writes nothing. Everything else that the process writes to stdout (a `print` at import or in a handler, a child process, a C library) goes to stderr, so stdout holds only the host output. Text that Python wrote to stdout before `main()` runs and without a buffer (`python -u` or `PYTHONUNBUFFERED`) cannot be recalled.
- A failure fails open: one line on stderr, nothing on stdout, exit 0. Failures include a payload that does not parse, an event that has no handler, an event that has no shared equivalent, a handler exception or `sys.exit`, and an output that the event cannot carry.
- A missing or unknown host argument is a programming error: a usage line on stderr and exit 1. Both hosts read exit 1 as a non-blocking error, so a wrong host file does not block a tool call or a prompt.

`main()` fails open only after the file runs. `uv run --script` can fail before that, for example when the uv cache is cold and the package index is not available. uv then exits 2, which both hosts read as a blocking error. The `|| true` in the host command turns that exit into 0. `main()` reports every block and deny decision as JSON with exit 0, so no intended behavior depends on the exit code.

`app.handle(host, payload)` does the same work in the current process and raises on each failure. Use it in tests.

## Check host files without a host

A test can load a hook file without running it and compare its events with the host files:

```python
import runpy
import sys

from typed_agent_hooks import shared

sys.path.insert(0, ".agent-hooks")  # for helper modules next to the hooks
app = runpy.run_path(".agent-hooks/inline-python.py")["app"]
for event_type in app.handlers:
    print(shared.native_event_name(event_type, "codex"), shared.native_event_name(event_type, "claude"))
```

`app.handlers` maps each shared event type to its handler. `shared.native_event_name(event_type, host)` returns the native event name, or `None` when the host has no such event. Codex has no `ToolCallFailed` event. Claude Code sends a failed tool call as `PostToolUseFailure`. Codex sends a shell command that exits non-zero as `PostToolUse`, and sends no hook for some other failures, such as an MCP tool error. A hook that must see failed shell commands in both hosts handles `ToolCallCompleted` and `ToolCallFailed`.

## Shared events

| Shared event (`shared.events`) | Codex | Claude Code |
| --- | --- | --- |
| `SessionStarted` | `SessionStart` | `SessionStart` |
| `PromptSubmitted` | `UserPromptSubmit` | `UserPromptSubmit` |
| `ToolCallProposed` | `PreToolUse` | `PreToolUse` |
| `PermissionRequested` | `PermissionRequest` | `PermissionRequest` |
| `ToolCallCompleted` | `PostToolUse` | `PostToolUse` |
| `ToolCallFailed` | none | `PostToolUseFailure` |
| `CompactionStarting` | `PreCompact` | `PreCompact` |
| `CompactionFinished` | `PostCompact` | `PostCompact` |
| `SubagentStarted` | `SubagentStart` | `SubagentStart` |
| `SubagentStopped` | `SubagentStop` | `SubagentStop` |
| `TurnStopped` | `Stop` | `Stop` |

Every event carries `event.context` with the host, session ID, working directory and other session data.

## Results

A handler returns one result from `shared.outputs`, or `None`. Each result is valid only on the events in this table. On any other event it raises `SharedOutputError` in both hosts, and `main()` fails open. The package does not guess a translation.

| Result | Valid events | Notes |
| --- | --- | --- |
| `None` | every event | Writes nothing. |
| `AddContext` | `SessionStarted`, `PromptSubmitted`, `ToolCallProposed`, `ToolCallCompleted`, `ToolCallFailed`, `SubagentStarted` | `ToolCallFailed` is Claude Code only. |
| `SystemMessage` | every event except `CompactionStarting` and `CompactionFinished` | Claude Code ignores `systemMessage` on compaction events. |
| `Block` | `PromptSubmitted`, `SubagentStopped`, `TurnStopped` | It carries no context: on a blocked prompt, Codex keeps the context for the next turn and Claude Code drops it. Not on `ToolCallCompleted`: Claude Code keeps the tool output next to the reason, and Codex replaces it. |
| `Stop` | `PromptSubmitted`, `SubagentStopped`, `TurnStopped` | A stated host difference: Claude Code keeps `stopReason` in the conversation, so the model sees it if the conversation continues, while Codex does not give it to the model. Not on `SessionStarted` (Claude Code has no decision control there) or `ToolCallCompleted` (Codex turns it into model feedback and continues). |
| `AllowTool` | `ToolCallProposed` | A stated host difference. Claude Code skips its permission prompt and applies `updated_input` when it is set. Codex only applies `updated_input`, and its normal approval still runs. Codex rejects an allow without `updated_input`, so that result raises for Codex. |
| `DenyTool` | `ToolCallProposed` | |
| `AllowPermission`, `DenyPermission` | `PermissionRequested` | |

The compaction events accept no result except `None`. For context after compaction, return `AddContext` from `SessionStarted` with `source == "compact"`.

## Host wire schemas

`typed_agent_hooks.codex` and `typed_agent_hooks.claude_code` hold the native schemas of each host, for the events in the table above. An input model declares only the fields that the shared events read. It ignores other fields, so a host change to a field that no hook reads cannot break a hook. Declared fields stay strictly typed. An output model holds only the fields that the shared results write, and it is closed and exact. A Claude Code event with no shared equivalent, such as `Notification`, is not modeled: `parse_input` raises `ValueError`, and `main()` fails open.

## Development

```console
uv run ruff format --check .
uv run ruff check .
uv run ty check
uv run pytest -q
```
