All files / src/seeding/data/tool-hooks example-hook.ts

100% Statements 2/2
100% Branches 0/0
100% Functions 0/0
100% Lines 2/2

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 6416x 16x                                                                                                                            
export const filename = 'example-hook.ts';
export const content = `/**
* Example Hook Script
* 
* Usage: 
* 1. Attach to 'patch' or 'overwrite_file' tool as a 'before' hook.
* 2. It will block edits to any file with 'lock' in the name.
*/
// --- Type Definitions ---
 
export interface HookAction {
  tool_name: string;
  args: Record<string, any>;
  // Only present for 'after' hooks
  result?: {
    status: 'SUCCESS' | 'FAILURE';
    output?: string;
    error?: string;
  };
}
 
export interface HookContext {
  hook_type: 'before' | 'after';
  action: HookAction;
  plan_context: any; // Shared state of current AI plan
}
 
export interface HookOutput {
  // If true, AI plan stops immediately.
  should_halt_plan?: boolean;
  
  // Optional message to log in UI
  message?: string;
  
  // Optional data to merge back into plan context
  update_context?: Record<string, any>;
}
 
// --- Hook Logic ---
 
export default async function(context: HookContext): Promise<HookOutput | void> {
  // 1. Extract Data
  const filePath = context.action?.args?.file_path || '';
  
  // Log to console (appears in backend logs)
  console.log(\`[Hook] analyzing action on: \${filePath}\`);
 
  // 2. Logic
  if (filePath.includes('lock')) {
    // Return Halt Signal
    return {
      should_halt_plan: true,
      message: \`BLOCKED: Editing files with "lock" in the name is forbidden by policy. File: \${filePath}\`
    };
  } else {
    // Return Pass Signal
    return {
      should_halt_plan: false,
      message: \`Hook allowed action on \${filePath}\`
    };
  }
}
`;