8gent Code
Reference

Overview

Hooks run custom code at key points in the agent workflow. They support shell commands, external scripts, and inline JavaScript. Configure hooks in ~/.8gent/hooks.json.

Configuration

{
  "hooks": [
    {
      "id": "hook_123",
      "type": "onComplete",
      "name": "Voice Notification",
      "description": "Speaks completion message via macOS TTS",
      "mode": "shell",
      "command": "say -v Moira 'Task completed'",
      "enabled": true,
      "async": true,
      "continueOnError": true
    }
  ],
  "globalTimeout": 30000,
  "enabled": true
}

Hook Types

TypeTrigger
beforeToolBefore any tool executes
afterToolAfter any tool completes
beforeCommandBefore a shell command runs
afterCommandAfter a shell command completes
onErrorWhen a tool or command fails
onCompleteWhen a task or conversation finishes
onStartWhen the agent session starts
onExitWhen the agent session ends

Hook Modes

Shell Mode

Run shell commands with variable substitution:

{
  "mode": "shell",
  "command": "echo 'Tool {tool} completed in {duration}ms'"
}

Script Mode

Run external script files:

{
  "mode": "script",
  "scriptPath": "~/.8gent/hooks/notify.ts"
}

Function Mode

Run inline JavaScript:

{
  "mode": "function",
  "functionBody": "console.log('Tool:', context.tool); return 'done';"
}

Context Variables

Available in shell mode (via {variable}) and function mode (via context.variable):

VariableDescription
{tool}Current tool name
{command}Shell command being run
{result}Tool or command output
{error}Error message if failed
{sessionId}Current session ID
{workingDirectory}Working directory
{duration}Execution time in milliseconds
{exitCode}Command exit code
{stdout}Command stdout
{stderr}Command stderr

Default Hooks

8gent ships with built-in hooks you can enable:

NameTypeDescription
Tool LoggerafterToolLogs tool executions
Command LoggerafterCommandLogs shell commands
Error LoggeronErrorLogs errors
Execution TimerafterToolTracks tool timing
macOS NotificationonCompleteShows a system notification
Voice NotificationonCompleteTTS announcement via say
Terminal BellonCompletePlays the terminal bell
Telegram NotificationonCompleteSends a Telegram message
Auto Git AddafterToolStages changed files after writes
Backup Before EditbeforeToolBacks up files before editing
Command ValidatorbeforeCommandWarns on dangerous commands
Auto LintafterToolRuns linter after file writes

Enable defaults programmatically:

import { setupNotificationHooks, setupGitHooks } from "8gent-code/hooks/defaults";

setupNotificationHooks(); // macOS notifications
setupGitHooks();          // auto git staging

API Usage

import { getHookManager, registerShellHook } from "8gent-code/hooks";

const manager = getHookManager();

// Register a custom hook
const hook = registerShellHook(
  "onComplete",
  "My Notification",
  "say 'Done: {result}'"
);

// Execute hooks manually
await manager.executeHooks("onComplete", {
  result: "Task finished",
  duration: 1500
});

// Disable a hook
manager.disableHook(hook.id);

CLI Commands

CommandDescription
/hooksList all registered hooks
/hooks enable <id>Enable a specific hook
/hooks disable <id>Disable a specific hook

Examples

Voice notification on task completion (macOS)

{
  "type": "onComplete",
  "mode": "shell",
  "command": "say -v Moira 'Task completed'",
  "async": true
}

Telegram notification

{
  "type": "onComplete",
  "mode": "shell",
  "command": "curl -s -X POST https://api.telegram.org/bot$BOT_TOKEN/sendMessage -d chat_id=$CHAT_ID -d text='8gent: {result}'",
  "async": true
}

Auto-lint after file writes

{
  "type": "afterTool",
  "mode": "shell",
  "command": "bunx biome check --write .",
  "continueOnError": true
}

Dangerous command warning

{
  "type": "beforeCommand",
  "mode": "function",
  "functionBody": "if (context.command.includes('rm -rf')) { console.warn('Dangerous command detected!'); }"
}