2026-07-26 14:24:52 +07:00
#!/usr/bin/env bash
# SessionStart hook for code-guide plugin.
2026-07-26 17:55:20 +07:00
# Reads ALL skills/*/SKILL.md files and injects them as additionalContext
# so every best-practice guide is always active from turn 1.
2026-07-26 14:24:52 +07:00
#
2026-07-26 17:55:20 +07:00
# Pattern: identical to explanatory-output-style's session-start hook
# which injects educational-output instructions into context.
# Instead of one static message, we dynamically iterate over all skills.
2026-07-26 14:24:52 +07:00
set -euo pipefail
SCRIPT_DIR = " $( cd " $( dirname " $0 " ) " && pwd ) "
PLUGIN_ROOT = " $( cd " ${ SCRIPT_DIR } /.." && pwd ) "
2026-07-26 17:55:20 +07:00
# Build combined content from all SKILL.md files.
combined = ""
while IFS = read -r -d '' skill_file; do
skill_name = $( basename " $( dirname " $skill_file " ) " )
content = $( cat " $skill_file " )
# Append skill with header separator — uses $'...' for real newlines
combined = " ${ combined } " $'\n\n\n' "===== code-guide: ${ skill_name } =====" $'\n\n' " ${ content } "
done < <( find " ${ PLUGIN_ROOT } /skills" -name 'SKILL.md' -print0 | sort -z)
2026-07-26 14:24:52 +07:00
2026-07-26 17:55:20 +07:00
intro = $'You have the code-guide plugin loaded with all best-practice guides directly injected.\nThe following skills are ALWAYS active in context — no manual invocation needed.\nWhen the current task matches a skill\'s domain, apply its guidance automatically.\n'
full_context = "<CODE_GUIDE_SKILLS>
${ intro }
${ combined }
</CODE_GUIDE_SKILLS>"
# JSON-escape the full context: \ → \\, " → \", newline → \n, etc.
2026-07-26 14:24:52 +07:00
escape_for_json() {
local s = " $1 "
s = " ${ s // \\ / \\\\ } "
s = " ${ s // \" / \\\" } "
s = " ${ s // $'\n' / \\ n } "
s = " ${ s // $'\r' / \\ r } "
s = " ${ s // $'\t' / \\ t } "
printf '%s' " $s "
}
2026-07-26 17:55:20 +07:00
escaped = $( escape_for_json " $full_context " )
2026-07-26 14:24:52 +07:00
if [ -n " ${ CLAUDE_PLUGIN_ROOT :- } " ] ; then
2026-07-26 17:55:20 +07:00
printf '{"hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":"%s"}}\n' " $escaped "
2026-07-26 14:24:52 +07:00
else
2026-07-26 17:55:20 +07:00
printf '{"additionalContext":"%s"}\n' " $escaped "
2026-07-26 14:24:52 +07:00
fi
exit 0