#!/bin/sh
# Otty — Kimi Code CLI hook.
#
# Reports the agent's state to the Otty app over IPC so a terminal pane can
# show the processing / idle / awaiting-input badge. Otty registers this script
# in ~/.kimi-code/config.toml as an [[hooks]] entry (Settings → Agents → Install
# Hooks). It ships as a readable, code-signed file inside Otty.app so you can
# audit exactly what runs on every Kimi hook event — Otty injects only the
# per-install paths.
#
# Args:
#   $1  state    — processing | idle | awaiting
#   $2  kimi_pid — the Kimi process's pid (its $PPID at hook time), so Otty can
#                  pin the event to the pane whose process tree contains it
#   $3  "ctx"    — present only for PermissionRequest: forward the full hook
#                  stdin (base64) so Otty can offer auto-approve context
# Env:
#   OTTY_CLI    — absolute path to the bundled otty-cli (injected by Otty; falls
#                 back to the copy next to this script's bundle)
#   OTTY_SOCKET — Otty IPC socket path (injected by Otty)
state="$1"
kimi_pid="$2"
want_ctx="$3"

# Kimi Code passes the hook payload as JSON on stdin (hook_event_name,
# session_id, cwd, …) — the same shape as Claude Code.
input=$(cat)

# Extract the session id and cwd from the stdin JSON. cwd lets Otty fall back to
# a cwd match when the agent-pid tree scan misses.
sid=$(printf '%s' "$input" | sed -n 's/.*"session_id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)
cwd=$(printf '%s' "$input" | sed -n 's/.*"cwd"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)

# Otty injects OTTY_CLI; the fallback locates otty-cli relative to this script
# inside the bundle (…/Contents/Resources/agent-integration/kimi → …/MacOS).
cli="${OTTY_CLI:-$(CDPATH= cd -- "$(dirname -- "$0")/../../../MacOS" 2>/dev/null && pwd)/otty-cli}"

# Backgrounded so the hook never blocks Kimi. Errors are intentionally swallowed:
# a missing Otty app must not break the user's Kimi session.
if [ "$want_ctx" = "ctx" ]; then
    ctx=$(printf '%s' "$input" | base64)
    "$cli" state:kimi session-id="$sid" state="$state" agent-pid="$kimi_pid" cwd="$cwd" context-b64="$ctx" 2>/dev/null &
else
    "$cli" state:kimi session-id="$sid" state="$state" agent-pid="$kimi_pid" cwd="$cwd" 2>/dev/null &
fi
