Skip to content

Commit af5b18d

Browse files
feat: add default sessionStart handler to template (#51)
1 parent d1d6343 commit af5b18d

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

β€Žadd-session-start.sedβ€Ž

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sed -f
2+
3+
# Add SessionStartPayload interface after PreCompactPayload
4+
/^}$/,/^export type HookPayload/ {
5+
/^export type HookPayload/ i\
6+
\
7+
export interface SessionStartPayload {\
8+
session_id: string\
9+
transcript_path: string\
10+
hook_event_name: 'SessionStart'\
11+
source: string\
12+
}
13+
}
14+
15+
# Add SessionStart to HookPayload union type
16+
/| (PreCompactPayload & {hook_type: 'PreCompact'})$/ a\
17+
| (SessionStartPayload & {hook_type: 'SessionStart'})
18+
19+
# Add SessionStartResponse interface after PreCompactResponse
20+
/^}$/,/^\/\/ Legacy simple response/ {
21+
/^\/\/ Legacy simple response/ i\
22+
\
23+
// SessionStart specific response\
24+
export interface SessionStartResponse extends BaseHookResponse {\
25+
decision?: 'approve' | 'block'\
26+
reason?: string\
27+
hookSpecificOutput?: {\
28+
hookEventName: 'SessionStart'\
29+
additionalContext?: string\
30+
}\
31+
}
32+
}
33+
34+
# Add SessionStartHandler type after PreCompactHandler
35+
/^export type PreCompactHandler.*$/ a\
36+
export type SessionStartHandler = (payload: SessionStartPayload) => Promise<SessionStartResponse> | SessionStartResponse
37+
38+
# Add sessionStart to HookHandlers interface
39+
/ preCompact\?: PreCompactHandler$/ a\
40+
sessionStart?: SessionStartHandler
41+
42+
# Add SessionStart case in runHook switch statement
43+
/case 'PreCompact':.*$/,/break$/ {
44+
/break$/ a\
45+
\
46+
case 'SessionStart':\
47+
if (handlers.sessionStart) {\
48+
const response = await handlers.sessionStart(payload)\
49+
console.log(JSON.stringify(response))\
50+
} else {\
51+
console.log(JSON.stringify({}))\
52+
}\
53+
break
54+
}

β€Žtemplates/hooks/index.tsβ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,41 @@ import type {
55
PostToolUseHandler,
66
PreCompactHandler,
77
PreToolUseHandler,
8+
SessionStartHandler,
89
StopHandler,
910
SubagentStopHandler,
1011
UserPromptSubmitHandler,
1112
} from './lib'
1213
import {runHook} from './lib'
1314
import {saveSessionData} from './session'
1415

16+
// SessionStart handler - called when a new Claude session starts
17+
const sessionStart: SessionStartHandler = async (payload) => {
18+
// Save session data (optional - remove if not needed)
19+
await saveSessionData('SessionStart', {...payload, hook_type: 'SessionStart'} as const)
20+
21+
// Example: Log session start with source
22+
console.log(`πŸš€ New session started from: ${payload.source}`)
23+
console.log(`πŸ“ Session ID: ${payload.session_id}`)
24+
25+
// Example: Load user preferences or configuration
26+
// const userConfig = await loadUserPreferences()
27+
28+
// Example: Set up session-specific resources
29+
// await initializeSessionResources(payload.session_id)
30+
31+
// Example: Apply different behavior based on session source
32+
if (payload.source === 'vscode') {
33+
console.log('πŸ‘¨β€πŸ’» VS Code session detected - enabling IDE-specific features')
34+
} else if (payload.source === 'web') {
35+
console.log('🌐 Web session detected')
36+
}
37+
38+
// Add your custom session initialization logic here
39+
40+
return {} // Empty object means continue normally
41+
}
42+
1543
// PreToolUse handler - called before Claude uses any tool
1644
// This handler can block tool execution by returning a deny decision
1745
const preToolUse: PreToolUseHandler = async (payload) => {
@@ -143,6 +171,7 @@ const preCompact: PreCompactHandler = async (payload) => {
143171

144172
// Run the hook with our handlers
145173
runHook({
174+
sessionStart,
146175
preToolUse,
147176
postToolUse,
148177
notification,

0 commit comments

Comments
Β (0)