session-start 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env bash
  2. # SessionStart hook for OAC plugin
  3. set -euo pipefail
  4. # Determine plugin root directory
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
  6. PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
  7. SKILL_FILE="${PLUGIN_ROOT}/skills/using-oac/SKILL.md"
  8. # Read using-oac content
  9. using_oac_content=$(cat "${SKILL_FILE}" 2>&1 || echo "Error reading using-oac skill")
  10. # Escape string for JSON embedding
  11. # SECURITY: Prevents command injection attacks from malicious SKILL.md files
  12. escape_for_json() {
  13. local s="$1"
  14. # Escape backslashes FIRST - order matters!
  15. s="${s//\\/\\\\}"
  16. # Escape double quotes
  17. s="${s//\"/\\\"}"
  18. # Escape newlines, carriage returns, tabs
  19. s="${s//$'\n'/\\n}"
  20. s="${s//$'\r'/\\r}"
  21. s="${s//$'\t'/\\t}"
  22. printf '%s' "$s"
  23. }
  24. using_oac_escaped=$(escape_for_json "$using_oac_content")
  25. # Build skill catalogue from skills directory
  26. skill_catalogue=""
  27. if [ -d "${PLUGIN_ROOT}/skills" ]; then
  28. for skill_dir in "${PLUGIN_ROOT}/skills"/*/; do
  29. skill_name=$(basename "$skill_dir")
  30. skill_file="${skill_dir}SKILL.md"
  31. if [ -f "$skill_file" ]; then
  32. # Extract description from frontmatter
  33. description=$(grep -m1 '^description:' "$skill_file" 2>/dev/null | sed 's/^description: *//;s/^"//;s/"$//' || echo "")
  34. if [ -n "$description" ]; then
  35. skill_catalogue="${skill_catalogue}\n- oac:${skill_name} — ${description}"
  36. else
  37. skill_catalogue="${skill_catalogue}\n- oac:${skill_name}"
  38. fi
  39. fi
  40. done
  41. fi
  42. # Build warning message for first-time users
  43. # Context can be installed project-local (.claude/.context-manifest.json)
  44. # or globally (~/.claude/.context-manifest.json)
  45. PROJECT_MANIFEST="$(pwd)/.claude/.context-manifest.json"
  46. GLOBAL_MANIFEST="${HOME}/.claude/.context-manifest.json"
  47. warning_message=""
  48. if [[ ! -f "${PROJECT_MANIFEST}" ]] && [[ ! -f "${GLOBAL_MANIFEST}" ]]; then
  49. warning_message="\n\n<important-reminder>IN YOUR FIRST REPLY AFTER SEEING THIS MESSAGE YOU MUST TELL THE USER: **No context files found.** Invoke the context-setup skill now to set up context for this project — it will check your project type and recommend the right profile. Say: \"I notice you don't have context files set up. Let me get that sorted for you.\" Then immediately run the context-setup skill.</important-reminder>"
  50. fi
  51. warning_escaped=$(escape_for_json "$warning_message")
  52. skill_catalogue_escaped=$(escape_for_json "$skill_catalogue")
  53. # Build OAC system paths block
  54. PROTOCOL_PATH="${PLUGIN_ROOT}/skills/context-discovery/context-discovery-protocol.md"
  55. plugin_root_escaped=$(escape_for_json "$PLUGIN_ROOT")
  56. protocol_path_escaped=$(escape_for_json "$PROTOCOL_PATH")
  57. OAC_SYSTEM_PATHS="## OAC System Paths\n- Plugin Root: ${plugin_root_escaped}\n- Context Discovery Protocol: ${protocol_path_escaped}"
  58. # Context scout instruction — run proactively at session start when context is available
  59. CONTEXT_SCOUT_INSTRUCTION="## Context Discovery\nBefore responding to any coding or implementation request this session, use the 'oac:context-discovery' skill to locate the project's coding standards. This runs once per session — do not repeat it if already done."
  60. context_scout_escaped=$(escape_for_json "$CONTEXT_SCOUT_INSTRUCTION")
  61. # Build context string
  62. OAC_CONTEXT="<EXTREMELY_IMPORTANT>\nYou have OAC (OpenAgents Control) installed. BEFORE responding to the user's first message, you MUST invoke the Skill tool with name='using-oac'. This is mandatory. Do not skip it. Do not respond first and invoke later. Invoke the skill FIRST.\n\n**Below is the full content of your 'oac:using-oac' skill — your introduction to using OAC skills. For all other skills, use the 'Skill' tool:**\n\n${using_oac_escaped}\n\n## Available OAC Skills (invoke with the Skill tool):\n${skill_catalogue_escaped}\n\n${OAC_SYSTEM_PATHS}\n\n${context_scout_escaped}\n\n${warning_escaped}\n</EXTREMELY_IMPORTANT>"
  63. # Output dual-format JSON for cross-tool compatibility
  64. # - additionalContext: Claude Code (hookSpecificOutput)
  65. # - additional_context: Cursor / OpenCode / other tools
  66. cat <<EOF
  67. {
  68. "additional_context": "${OAC_CONTEXT}",
  69. "hookSpecificOutput": {
  70. "hookEventName": "SessionStart",
  71. "additionalContext": "${OAC_CONTEXT}"
  72. }
  73. }
  74. EOF
  75. exit 0