Hooks

Updated at:

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

Note

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 requests and PyYAML preinstalled. Import them as requests and yaml.

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 TurnEnd hook fails to execute, the original end status and reason of the current turn are retained.

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

event

String

The event type: TurnStart or TurnEnd.

userMessage

String

The user input of the current turn.

output

String

The text in the most recent model response when the hook is triggered. This value is updated with model responses.

requestParams

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

toolName

String

The tool name.

toolParams

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.

toolSucceeded

Boolean

Indicates whether the tool was executed successfully. This parameter is valid only in ToolEnd. In ToolStart, the value is always false.

toolResult

String

The tool execution result, available in ToolEnd.

event

String

The event type: ToolStart or ToolEnd.

userMessage

String

The user input of the current turn.

output

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.

requestParams

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

effect

String

Yes

The effect type. Valid values: Decide and Amend. Each response uses one effect type.

result

Object

Yes

The effect content. Its fields are determined by effect.

Decide: execution decision

When effect is Decide, result uses the following fields.

Parameter

Type

Required

Description

result.decision

String

Yes

The execution decision. The valid values depend on the event.

result.reason

String

Conditionally required

The reason for the decision. This parameter must be a non-empty string when reject, hil, or continue is returned.

The following table describes the valid values of result.decision for each event.

Event

Decision value

Description

TurnStart

allow and reject

allow allows the current turn to start. reject rejects the current turn and displays the reason in the intelligent session.

ToolStart

allow, reject, and hil

allow allows the tool execution process to proceed. reject rejects the execution. hil requests the user to confirm the current tool call.

ToolEnd

allow and reject

allow allows processing to continue. reject rejects the current tool result. The result output by the hook directly replaces the original tool result.

TurnEnd

finish and continue

finish accepts the end of the current turn and retains the original execution status. continue requires the digital employee to keep processing based on the result that the current turn has already produced. reason describes the tasks that still need to be completed.

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

result.target

String

Yes

The modification target, which depends on the event.

result.operation

String

Yes

The operation type, which depends on the event.

result.payload

Object

Yes

The modification data, which contains key and value.

result.payload.key

String

Yes

A non-empty identifier of the modified or supplementary content.

result.payload.value

JSON value

Yes

For Replace, a parameter object whose key is fixed as arguments and whose value is the JSON string to update in the tool call. For Append, any JSON value, including null.

The following table describes the targets and operations supported by each event.

Event

result.target

result.operation

Effect

TurnStart

user_message

Append

Appends context, which is provided to the digital employee together with the original user input.

ToolStart

tool_command

Replace

Updates the arguments of the current tool call with the parameters specified in payload.value. The subsequent tool process uses the updated parameters.

ToolEnd

tool_result

Append

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

command

String

Yes

The complete command to run.

description

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

file_path

String

Yes

The path of the file to read. A relative path is resolved based on the current working directory.

offset

Number

No

The line number from which to start reading text. The value starts from 1.

limit

Number

No

The number of lines of text to read.

pages

String

No

The PDF page number or page range. This parameter applies to PDF files uploaded to /home/starops/upload/. Up to 20 pages can be read at a time.

Write

When toolName is Write, the parsed toolParams contains the following parameters.

Parameter

Type

Required

Description

file_path

String

Yes

The path of the file to write. A relative path is resolved based on the current working directory.

content

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

file_path

String

Yes

The path of the file to modify. A relative path is resolved based on the current working directory.

old_string

String

Yes

The original text to replace. It is matched exactly against the file content.

new_string

String

Yes

The replacement text, which must be different from the original text. An empty string deletes the matched content.

replace_all

Boolean

No

Specifies whether to replace all matching items. The default value is false, which requires the original text to match uniquely in the file. If set to true, all matching items are replaced.

RequestParams struct reference

RequestParams contains metadata for the current session. The following table describes its parameters.

Field path

Type

Description

threadId

String

The session ID.

messageId

String

The message ID.

employeeId

String

The identifier of the digital employee.

callerUid

String

The UID of the calling user.

ownerId

String

The ID of the resource owner or Alibaba Cloud account.

userId

String

The ID of the current user.

userInputParams

Object

The user input parameters, including the time, time zone, language, and configuration.

userInputParams.timeStamp

String

The Unix timestamp in seconds, such as "1794468780".

userInputParams.timeZone

String

The time zone of the user, such as "Asia/Shanghai".

userInputParams.language

String

The language of the user, such as "zh".

userInputParams.config

Object

The custom configuration in the current session.