Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions templates/hooks/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ export interface NotificationPayload {
transcript_path: string
hook_event_name: 'Notification'
message: string
title?: string
}

export interface StopPayload {
Expand Down Expand Up @@ -316,6 +317,13 @@ export interface PreCompactPayload {
trigger: 'manual' | 'auto'
}

export interface SessionStartPayload {
session_id: string
transcript_path: string
hook_event_name: 'SessionStart'
source: string
}

export type HookPayload =
| (PreToolUsePayload & {hook_type: 'PreToolUse'})
| (PostToolUsePayload & {hook_type: 'PostToolUse'})
Expand All @@ -324,6 +332,7 @@ export type HookPayload =
| (SubagentStopPayload & {hook_type: 'SubagentStop'})
| (UserPromptSubmitPayload & {hook_type: 'UserPromptSubmit'})
| (PreCompactPayload & {hook_type: 'PreCompact'})
| (SessionStartPayload & {hook_type: 'SessionStart'})

// Base response fields available to all hooks
export interface BaseHookResponse {
Expand Down Expand Up @@ -368,6 +377,16 @@ export interface PreCompactResponse extends BaseHookResponse {
reason?: string
}

// SessionStart specific response
export interface SessionStartResponse extends BaseHookResponse {
decision?: 'approve' | 'block'
reason?: string
hookSpecificOutput?: {
hookEventName: 'SessionStart'
additionalContext?: string
}
}

// Legacy simple response for backward compatibility
export interface HookResponse {
action: 'continue' | 'block'
Expand All @@ -390,6 +409,7 @@ export type UserPromptSubmitHandler = (
payload: UserPromptSubmitPayload,
) => Promise<UserPromptSubmitResponse> | UserPromptSubmitResponse
export type PreCompactHandler = (payload: PreCompactPayload) => Promise<PreCompactResponse> | PreCompactResponse
export type SessionStartHandler = (payload: SessionStartPayload) => Promise<SessionStartResponse> | SessionStartResponse

export interface HookHandlers {
preToolUse?: PreToolUseHandler
Expand All @@ -399,6 +419,7 @@ export interface HookHandlers {
subagentStop?: SubagentStopHandler
userPromptSubmit?: UserPromptSubmitHandler
preCompact?: PreCompactHandler
sessionStart?: SessionStartHandler
}

// Logging utility
Expand Down Expand Up @@ -485,6 +506,15 @@ export function runHook(handlers: HookHandlers): void {
}
break

case 'SessionStart':
if (handlers.sessionStart) {
const response = await handlers.sessionStart(payload)
console.log(JSON.stringify(response))
} else {
console.log(JSON.stringify({}))
}
break

default:
console.log(JSON.stringify({}))
}
Expand Down
Loading