session-start.sh 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. warning_message=""
  44. if [[ ! -f "${PLUGIN_ROOT}/.context-manifest.json" ]] && [[ ! -f "$(pwd)/.context-manifest.json" ]]; then
  45. warning_message="\n\n<important-reminder>IN YOUR FIRST REPLY AFTER SEEING THIS MESSAGE YOU MUST TELL THE USER:👋 **Welcome to OpenAgents Control!** To get started, run /install-context to download context files. Then use /brainstorm before building anything.</important-reminder>"
  46. fi
  47. warning_escaped=$(escape_for_json "$warning_message")
  48. skill_catalogue_escaped=$(escape_for_json "$skill_catalogue")
  49. # Build context string
  50. OAC_CONTEXT="<EXTREMELY_IMPORTANT>\nYou have OAC (OpenAgents Control) superpowers.\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${warning_escaped}\n</EXTREMELY_IMPORTANT>"
  51. # Output dual-format JSON for cross-tool compatibility
  52. # - additionalContext: Claude Code (hookSpecificOutput)
  53. # - additional_context: Cursor / OpenCode / other tools
  54. cat <<EOF
  55. {
  56. "additional_context": "${OAC_CONTEXT}",
  57. "hookSpecificOutput": {
  58. "hookEventName": "SessionStart",
  59. "additionalContext": "${OAC_CONTEXT}"
  60. }
  61. }
  62. EOF
  63. exit 0