refactor: simplify cursor session reporting
replaces contributions from udirom
This commit is contained in:
parent
08ca798518
commit
73d17682ce
|
|
@ -1,92 +1,59 @@
|
|||
#!/bin/sh
|
||||
# installed by herdr
|
||||
# managed by herdr; reinstalling or updating the integration overwrites this file.
|
||||
# add custom hooks beside this file instead of editing it.
|
||||
# managed by herdr; reinstalling the integration replaces this file.
|
||||
# HERDR_INTEGRATION_ID=cursor
|
||||
# HERDR_INTEGRATION_VERSION=1
|
||||
|
||||
set -eu
|
||||
|
||||
action="${1:-}"
|
||||
hook_input_file="$(mktemp "${TMPDIR:-/tmp}/herdr-cursor-hook.XXXXXX")" || exit 0
|
||||
trap 'rm -f "$hook_input_file"' EXIT HUP INT TERM
|
||||
cat >"$hook_input_file" 2>/dev/null || true
|
||||
|
||||
case "$action" in
|
||||
session) ;;
|
||||
*) exit 0 ;;
|
||||
esac
|
||||
|
||||
[ "${1:-}" = "session" ] || exit 0
|
||||
[ "${HERDR_ENV:-}" = "1" ] || exit 0
|
||||
[ -n "${HERDR_SOCKET_PATH:-}" ] || exit 0
|
||||
[ -n "${HERDR_PANE_ID:-}" ] || exit 0
|
||||
command -v python3 >/dev/null 2>&1 || exit 0
|
||||
|
||||
HERDR_ACTION="$action" HERDR_HOOK_INPUT_FILE="$hook_input_file" python3 - <<'PY'
|
||||
python3 -c '
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
|
||||
source = "herdr:cursor"
|
||||
pane_id = os.environ.get("HERDR_PANE_ID")
|
||||
socket_path = os.environ.get("HERDR_SOCKET_PATH")
|
||||
hook_input_file = os.environ.get("HERDR_HOOK_INPUT_FILE")
|
||||
|
||||
if not pane_id or not socket_path:
|
||||
try:
|
||||
payload = json.load(sys.stdin)
|
||||
except Exception:
|
||||
raise SystemExit(0)
|
||||
|
||||
hook_input = {}
|
||||
if hook_input_file:
|
||||
try:
|
||||
with open(hook_input_file, encoding="utf-8") as handle:
|
||||
content = handle.read()
|
||||
if content.strip():
|
||||
hook_input = json.loads(content)
|
||||
except Exception:
|
||||
hook_input = {}
|
||||
|
||||
def first_text(*keys):
|
||||
for key in keys:
|
||||
value = hook_input.get(key)
|
||||
def first_text(*names):
|
||||
for name in names:
|
||||
value = payload.get(name)
|
||||
if isinstance(value, str) and value:
|
||||
return value
|
||||
return None
|
||||
|
||||
hook_event_name = first_text("hook_event_name", "hookEventName")
|
||||
if hook_event_name not in (None, "sessionStart"):
|
||||
event = first_text("hook_event_name", "hookEventName")
|
||||
if event not in (None, "sessionStart"):
|
||||
raise SystemExit(0)
|
||||
|
||||
request_id = f"{source}:{int(time.time() * 1000)}:{random.randrange(1_000_000):06d}"
|
||||
report_seq = time.time_ns()
|
||||
session_id = first_text("session_id", "sessionId", "conversation_id", "conversationId")
|
||||
agent_session_id = session_id if isinstance(session_id, str) and session_id else None
|
||||
if agent_session_id:
|
||||
request = {
|
||||
"id": request_id,
|
||||
"method": "pane.report_agent_session",
|
||||
"params": {
|
||||
"pane_id": pane_id,
|
||||
"source": source,
|
||||
"agent": "cursor",
|
||||
"seq": report_seq,
|
||||
"agent_session_id": agent_session_id,
|
||||
},
|
||||
}
|
||||
else:
|
||||
if session_id is None:
|
||||
raise SystemExit(0)
|
||||
|
||||
seq = time.time_ns()
|
||||
request = json.dumps({
|
||||
"id": f"herdr:cursor:{seq}",
|
||||
"method": "pane.report_agent_session",
|
||||
"params": {
|
||||
"pane_id": os.environ["HERDR_PANE_ID"],
|
||||
"source": "herdr:cursor",
|
||||
"agent": "cursor",
|
||||
"seq": seq,
|
||||
"agent_session_id": session_id,
|
||||
},
|
||||
})
|
||||
try:
|
||||
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
||||
client.settimeout(0.5)
|
||||
client.connect(socket_path)
|
||||
client.sendall((json.dumps(request) + "\n").encode())
|
||||
try:
|
||||
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as client:
|
||||
client.settimeout(0.5)
|
||||
client.connect(os.environ["HERDR_SOCKET_PATH"])
|
||||
client.sendall((request + "\n").encode())
|
||||
client.recv(4096)
|
||||
except Exception:
|
||||
pass
|
||||
client.close()
|
||||
except Exception:
|
||||
pass
|
||||
PY
|
||||
' 2>/dev/null || true
|
||||
|
|
|
|||
Loading…
Reference in New Issue