Hooks for Agentforce Vibes
Hooks let you inject custom logic into Agentforce Vibes workflows to validate operations and shape the agent's decisions. Hooks are scripts that run at key moments in the task lifecycle. Because they run at defined points with consistent inputs and outputs, hooks add determinism to AI models by enforcing guardrails, validations, and context injection. You can validate operations before they run, monitor tool usage, and influence how Agentforce Vibes makes decisions.
When using AI-powered features, you assume responsibility for reviewing outputs and ensuring they match your requirements. Hooks help you enforce policies programmatically, but always review hook logic before enabling it.
Hooks are currently unavailable on Windows.
- Stop operations before they cause problems, such as creating
.jsfiles in a TypeScript project. - Run linters or custom validators before files get saved.
- Prevent operations that violate security policies.
- Track everything for analytics or compliance.
- Trigger external tools or services at the right moments.
- Add context to the conversation based on what the agent is doing.
Agentforce Vibes supports eight hook types that run at different points in the task lifecycle:
| Hook Type | When It Runs |
|---|---|
TaskStart | When you start a new task |
TaskResume | When you resume an interrupted task |
TaskCancel | When you cancel a running task |
TaskComplete | When a task finishes successfully |
PreToolUse | Before the agent runs a tool |
PostToolUse | After a tool execution completes |
UserPromptSubmit | When you submit a message to the agent |
PreCompact | Before the agent truncates conversation history |
The hook lifecycle follows the conversation flow:
- When you start a task, either
TaskStart(new task) orTaskResume(interrupted task) runs first. - Each time you make a request,
UserPromptSubmitruns, then the agent processes your request. - When the agent decides to use a tool,
PreToolUseruns first. If allowed, the tool executes, thenPostToolUseruns. - If the conversation approaches context limits,
PreCompactruns before truncation. - The task ends with either
TaskComplete(success) orTaskCancel(user cancellation).
The cycle repeats as you continue the conversation.
Hooks are either global or in a project workspace, and are located in these directories:
- Global hooks:
~/Library/ApplicationSupport/Code/User/globalStorage/salesforce.salesforcedx-einstein-gpt/Hooks/ - Project hooks:
.a4drules/hooks/in your repo (can be committed to version control)
When both global and workspace hooks exist for the same hook type, both run. Global hooks execute first, then workspace hooks. If either returns cancel: true, the operation stops.
To create and enable your first hook:
- Click the
⚖️icon at the bottom of the Agentforce Vibes panel, to the left of the model selector. - Click the Hooks tab.
- Click the "New hook…" dropdown and select a hook type (for example,
PreToolUse,TaskStart). - Click the pencil icon to open and edit the hook script. Agentforce Vibes generates a template with examples to get you started.
- Toggle the switch to activate the hook after you understand what it does.
Always review a hook's code before enabling it. Hooks execute automatically during your workflow and can block operations or run shell commands.
Hooks are executable scripts that receive JSON input via stdin and return JSON output via stdout.
Every hook receives a JSON object with common fields plus hook-specific data:
The hook-specific field name matches the hook type in camelCase:
taskStart,taskResume,taskCancel,taskComplete– contain{ task: string }preToolUse– contains{ tool: string, parameters: object }postToolUse– contains{ tool: string, parameters: object, result: string, success: boolean, durationMs: number }userPromptSubmit– contains{ prompt: string }preCompact– contains{ conversationLength: number, estimatedTokens: number }
Hooks return a JSON object to stdout:
| Field | Type | Description |
|---|---|---|
cancel | boolean | If true, stops the operation (blocks the tool, cancels the task start, etc.) |
contextModification | string | Optional text injected into the conversation as context for the agent |
errorMessage | string | Shown to the user if cancel is true |
The contextModification field lets hooks inject information into the conversation. Use it for:
- Adding project-specific context when a task starts.
- Providing validation results that the agent should consider.
- Injecting environment information before tool execution.
For example, a PreToolUse hook could add: "Note: This file is auto-generated. Edits may be overwritten."
Block creation of .js files in a TypeScript project:
Log all tool executions to a file:
Inject project-specific information when a task begins:
Hook not running?
- Check that the file is executable (
chmod +x hookname). - Ensure that the hook file uses extensionless
<HookName>naming (for example,PreToolUse). - Verify that the hook is enabled (toggle is on in the Hooks tab).
- Check that Hooks are enabled globally in Settings.
Hook output not parsed?
- Ensure that output is valid JSON on a single-line to stdout.
- Use stderr (
>&2) for debug logging, not stdout. - Check for trailing characters or newlines before the JSON.
Hook blocking unexpectedly?
- Review the hook's logic and test with sample input.
- Check both global and workspace hooks (both run if they exist).
- Hooks execute automatically during your workflow. Review the hook code before enabling to avoid unintended blocking or side effects.
PostToolUsehooks can returncancel: trueto stop the task, but they can’t undo the tool execution that already happened.- When both global and workspace hooks exist, both run. If either returns
cancel: true, the operation stops. - Hooks run with the permissions of the user running Agentforce Vibes. Ensure that hooks don’t expose sensitive data or execute untrusted commands.