Hooks
Hooks run custom logic at specified stages while a digital employee processes a task. A hook receives relevant input and returns an execution decision, a tool parameter modification, or supplementary context.
How hooks work
When a trigger event occurs, the digital employee passes the relevant input to the hooks that match the event. Each hook returns one of two effects: Decide makes an execution decision, and Amend modifies tool parameters or appends content. The returned effect determines how the digital employee proceeds with the current turn.
Before you configure hooks, note the following constraints:
Supported tools — Tool call hooks can intercept four types of tools: Read, Write, Bash, and Edit.
Data format — The input to a hook is a single-line JSON object, and a hook returns a single JSON object. Field names and enumeration values are case-sensitive.
Configure hooks
All hook operations start from the hook configuration page. In General Settings, click Hooks to open the page.
Add a hook
To add a hook, configure the following settings.
Scenarios
Select the scenarios in which the hook takes effect.
Option | Description |
All Scenarios | Takes effect in both conversations and long-running tasks. |
Conversation | Takes effect in conversations. |
Long-running Task | Takes effect in long-running tasks. |
Trigger events
A turn starts when an input is submitted and ends when the digital employee completes its response to that input. A turn can include multiple model inference steps and tool calls.
Option | Trigger time |
Turn Start · TurnStart | When the digital employee starts to process the input of the current turn. |
Turn End · TurnEnd | When the digital employee is about to end the processing of the current turn. |
Before Tool Execution · ToolStart | After the digital employee initiates a tool call and before the tool is executed. |
After Tool Execution · ToolEnd | After the tool call returns its final result. |
Hook types
Select how the hook processes input, and write the corresponding prompt or script. For input fields and return formats, see Input parameters and Output parameters.
Option | Content |
Prompt | Write the processing requirements, including how to make a decision based on the input and the JSON format to return. During execution, the system prepends a condensed history of the current turn to the hook prompt. The history includes the user input, the tool calls of the digital employee, and its output. You can ask for an evaluation of the current turn directly in the prompt. |
Bash Script | Write a Bash script that processes the input and returns a JSON result. |
Python3 Script | Write Python 3 code that processes the input and returns a JSON result. The sandbox has |
Debug Mode
When debug mode is enabled, the hook will output detailed inputs, outputs, and execution information when running in the agent session, making it easier to observe the hook's behavior and make targeted adjustments.
Advanced settings
Parameter | Valid values | Description |
Timeout | An integer from 1 to 30. Unit: seconds. | The total execution time limit of a single hook. The first execution and all retries share this time limit. |
Retries | An integer from 0 to 3. Unit: attempts. | The number of additional attempts after an execution fails. A value of 0 indicates that the hook is executed only once. The total number of attempts is at most the specified value plus 1. |
On Hook Failure | Allow and Reject. | The action to take when the hook fails to execute, including when the output format is invalid in Prompt mode or the execution times out. Allow keeps the original input and result and continues the task. Reject terminates the current call and returns rejection information in ToolStart and ToolEnd hooks. In TurnStart hooks, Reject terminates inference for the current turn. If a |
Adjust the execution order of hooks
Click the up arrow or down arrow on a hook card to adjust the execution order. Hooks that match the current event run from top to bottom. Appended content is retained in sequence. For tool parameters with the same name, the value from the hook that runs later takes effect.
Disable a hook
Turn off the switch on the right side of a hook card to disable the hook. To re-enable the hook, turn the switch on again.
Delete a hook
Click the Delete button on the right side of a hook card to permanently delete the hook. Subsequent intelligent sessions use the hook configuration after the deletion.
Input parameters
The input to a hook is a single-line JSON object. Bash and Python 3 scripts read this object from standard input. String fields can be empty.
TurnStart and TurnEnd hooks
TurnStart and TurnEnd hooks use the following input parameters.
Parameter | Type | Description |
| String | The event type: |
| String | The user input of the current turn. |
| String | The text in the most recent model response when the hook is triggered. This value is updated with model responses. |
| Object | Metadata for the current session. For more information, see RequestParams struct reference in the appendix. |
ToolStart and ToolEnd hooks
Tool call hooks use the following input parameters and can intercept four types of tools: Read, Write, Bash, and Edit.
Parameter | Type | Description |
| String | The tool name. |
| String | The tool parameters, passed as a JSON string. Parse the input object first, and then parse this field to read the specific parameters. For the parameters of each tool call and their meanings, see Tool call parameter reference in the appendix. |
| Boolean | Indicates whether the tool was executed successfully. This parameter is valid only in |
| String | The tool execution result, available in |
| String | The event type: |
| String | The user input of the current turn. |
| String | The text in the most recent model response when the hook is triggered. This value can be an empty string if the response contains only tool calls. |
| Object | Metadata for the current session. For more information, see RequestParams struct reference in the appendix. |
output is the text of the model response, and toolResult is the result returned by the tool. After the tool runs, output retains the text of the most recent response until the model generates a new response.
Output parameters
A hook returns a single JSON object that contains effect and result. Field names and enumeration values are case-sensitive.
Bash and Python 3 scripts return a complete JSON object through standard output (stdout) and write logs to standard error (stderr). The exit code is 0 on successful completion.
Parameter | Type | Required | Description |
| String | Yes | The effect type. Valid values: |
| Object | Yes | The effect content. Its fields are determined by |
Decide: execution decision
When effect is Decide, result uses the following fields.
Parameter | Type | Required | Description |
| String | Yes | The execution decision. The valid values depend on the event. |
| String | Conditionally required | The reason for the decision. This parameter must be a non-empty string when |
The following table describes the valid values of result.decision for each event.
Event | Decision value | Description |
|
|
|
|
|
|
|
|
|
|
|
|
If a hook returns reject, returns hil before tool execution, or returns continue at turn end, the remaining hooks for the current event are not processed.
Amend: tool parameter modification or content appending
When effect is Amend, result uses the following fields.
Parameter | Type | Required | Description |
| String | Yes | The modification target, which depends on the event. |
| String | Yes | The operation type, which depends on the event. |
| Object | Yes | The modification data, which contains |
| String | Yes | A non-empty identifier of the modified or supplementary content. |
| JSON value | Yes | For |
The following table describes the targets and operations supported by each event.
Event |
|
| Effect |
|
|
| Appends context, which is provided to the digital employee together with the original user input. |
|
|
| Updates the |
|
|
| Appends content, which is added to the context of the current intelligent session together with the original tool result. |
Replace updates parameters by top-level key: existing keys overwrite the original values, new keys are added to the parameters, and other parameters are retained. Object-type parameter values are replaced as a whole.
Appendix
Tool call parameter reference
The Read, Write, Bash, and Edit tools use the same parameter structure in ToolStart and ToolEnd. Quotation marks and line breaks in strings are escaped according to JSON rules.
Bash
When toolName is Bash, the parsed toolParams contains the following parameters.
Parameter | Type | Required | Description |
| String | Yes | The complete command to run. |
| String | No | A brief description of the purpose of the command. |
Read
When toolName is Read, the parsed toolParams contains the following parameters.
Parameter | Type | Required | Description |
| String | Yes | The path of the file to read. A relative path is resolved based on the current working directory. |
| Number | No | The line number from which to start reading text. The value starts from 1. |
| Number | No | The number of lines of text to read. |
| String | No | The PDF page number or page range. This parameter applies to PDF files uploaded to |
Write
When toolName is Write, the parsed toolParams contains the following parameters.
Parameter | Type | Required | Description |
| String | Yes | The path of the file to write. A relative path is resolved based on the current working directory. |
| String | Yes | The complete file content. If the file already exists, the original content is overwritten. |
Edit
When toolName is Edit, the parsed toolParams contains the following parameters.
Parameter | Type | Required | Description |
| String | Yes | The path of the file to modify. A relative path is resolved based on the current working directory. |
| String | Yes | The original text to replace. It is matched exactly against the file content. |
| String | Yes | The replacement text, which must be different from the original text. An empty string deletes the matched content. |
| Boolean | No | Specifies whether to replace all matching items. The default value is |
RequestParams struct reference
RequestParams contains metadata for the current session. The following table describes its parameters.
Field path | Type | Description |
| String | The session ID. |
| String | The message ID. |
| String | The identifier of the digital employee. |
| String | The UID of the calling user. |
| String | The ID of the resource owner or Alibaba Cloud account. |
| String | The ID of the current user. |
| Object | The user input parameters, including the time, time zone, language, and configuration. |
| String | The Unix timestamp in seconds, such as |
| String | The time zone of the user, such as |
| String | The language of the user, such as |
| Object | The custom configuration in the current session. |