#!/usr/bin/env bash
# Remind the agent to always include a clickable canvas link in the chat reply.
set -euo pipefail

input="$(cat)"
tool_name="$(echo "$input" | jq -r '.tool_name // empty')"

if [[ "$tool_name" != "Write" ]]; then
  echo '{}'
  exit 0
fi

tool_input="$(echo "$input" | jq -c '.tool_input // {}')"
file_path="$(echo "$tool_input" | jq -r '.path // .file_path // empty' 2>/dev/null || true)"

if [[ -z "$file_path" || "$file_path" != *"resuggestor-suggestions.canvas.tsx" ]]; then
  echo '{}'
  exit 0
fi

jq -n \
  --arg path "$file_path" \
  '{
    additional_context: (
      "ReSuggestor canvas was written. You MUST end your reply with this exact markdown link so the user always has a clickable way to open it: " +
      "[Open suggestions canvas](" + $path + "). " +
      "Do not skip the link even if auto-open is expected."
    )
  }'
